Diff for /embedaddon/libnet/src/libnet_write.c between versions 1.1.1.2 and 1.1.1.3

version 1.1.1.2, 2013/07/22 11:54:42 version 1.1.1.3, 2023/09/27 11:11:38
Line 1 Line 1
 /*  /*
  *  $Id$  
  *  
  *  libnet   *  libnet
  *  libnet_write.c - writes a prebuilt packet to the network   *  libnet_write.c - writes a prebuilt packet to the network
  *   *
  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>   *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
  *  All rights reserved.   *  All rights reserved.
 *  win32 specific code  *  win32 specific code
  *  Copyright (c) 2002 - 2003 Roberto Larcher <roberto.larcher@libero.it>   *  Copyright (c) 2002 - 2003 Roberto Larcher <roberto.larcher@libero.it>
  *   *
  * Redistribution and use in source and binary forms, with or without   * Redistribution and use in source and binary forms, with or without
Line 32 Line 30
  *   *
  */   */
   
#include <netinet/in.h>#include "common.h"
#include <sys/types.h> 
#include <netinet/udp.h> 
   
 #if (HAVE_CONFIG_H)  
 #include "../include/config.h"  
 #endif  
 #if (!(_WIN32) || (__CYGWIN__))   
 #include "../include/libnet.h"  
 #else  
 #include "../include/win32/libnet.h"  
 #include "../include/win32/config.h"  
 #include "packet32.h"  
 #include "Ntddndis.h"  
 #endif  
   
 int  int
 libnet_write(libnet_t *l)  libnet_write(libnet_t *l)
 {  {
Line 56  libnet_write(libnet_t *l) Line 40  libnet_write(libnet_t *l)
     uint8_t *packet = NULL;      uint8_t *packet = NULL;
   
     if (l == NULL)      if (l == NULL)
    {     {
         return (-1);          return (-1);
     }      }
   
Line 76  libnet_write(libnet_t *l) Line 60  libnet_write(libnet_t *l)
             if (len > LIBNET_MAX_PACKET)              if (len > LIBNET_MAX_PACKET)
             {              {
                 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,                  snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                        "%s(): packet is too large (%d bytes)\n",                        "%s(): packet is too large (%d bytes)",
                         __func__, len);                          __func__, len);
                 goto done;                  goto done;
             }              }
Line 92  libnet_write(libnet_t *l) Line 76  libnet_write(libnet_t *l)
             break;              break;
         default:          default:
             snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,              snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                        "%s(): unsuported injection type\n", __func__);                        "%s(): unsupported injection type", __func__);
             goto done;              goto done;
     }      }
   
Line 129  done: Line 113  done:
   
 #if defined (__WIN32__)  #if defined (__WIN32__)
 libnet_ptag_t  libnet_ptag_t
libnet_win32_build_fake_ethernet(uint8_t *dst, uint8_t *src, uint16_t type,libnet_win32_build_fake_ethernet (uint8_t *dst, uint8_t *src, uint16_t type,
uint8_t *payload, uint32_t payload_s, uint8_t *packet, libnet_t *l,                                  const uint8_t *payload, uint32_t payload_s,
libnet_ptag_t ptag)                                  uint8_t *packet, libnet_t *l, libnet_ptag_t ptag)
 {  {
     struct libnet_ethernet_hdr eth_hdr;      struct libnet_ethernet_hdr eth_hdr;
   
Line 140  libnet_ptag_t ptag) Line 124  libnet_ptag_t ptag)
         return (-1);          return (-1);
     }      }
   
    memset(&eth_hdr, 0, sizeof(eth_hdr));      memset(&eth_hdr, 0, sizeof(eth_hdr));
     eth_hdr.ether_type = htons(type);      eth_hdr.ether_type = htons(type);
     memcpy(eth_hdr.ether_dhost, dst, ETHER_ADDR_LEN);  /* destination address */      memcpy(eth_hdr.ether_dhost, dst, ETHER_ADDR_LEN);  /* destination address */
     memcpy(eth_hdr.ether_shost, src, ETHER_ADDR_LEN);  /* source address */      memcpy(eth_hdr.ether_shost, src, ETHER_ADDR_LEN);  /* source address */
Line 158  libnet_ptag_t ptag) Line 142  libnet_ptag_t ptag)
 }  }
   
 libnet_ptag_t  libnet_ptag_t
libnet_win32_build_fake_token(uint8_t *dst, uint8_t *src, uint16_t type,libnet_win32_build_fake_token (uint8_t *dst, uint8_t *src, uint16_t type,
uint8_t *payload, uint32_t payload_s, uint8_t *packet, libnet_t *l,                               const uint8_t *payload, uint32_t payload_s,
libnet_ptag_t ptag)                               uint8_t *packet, libnet_t *l, libnet_ptag_t ptag)
 {  {
     struct libnet_token_ring_hdr token_ring_hdr;      struct libnet_token_ring_hdr token_ring_hdr;
   
     if (!packet)      if (!packet)
     {      {
         return (-1);          return (-1);
     }      }
   
    memset(&token_ring_hdr, 0, sizeof(token_ring_hdr));     memset(&token_ring_hdr, 0, sizeof(token_ring_hdr));
     token_ring_hdr.token_ring_access_control    = 0x10;      token_ring_hdr.token_ring_access_control    = 0x10;
     token_ring_hdr.token_ring_frame_control     = 0x40;      token_ring_hdr.token_ring_frame_control     = 0x40;
     token_ring_hdr.token_ring_llc_dsap          = 0xaa;      token_ring_hdr.token_ring_llc_dsap          = 0xaa;
     token_ring_hdr.token_ring_llc_ssap          = 0xaa;      token_ring_hdr.token_ring_llc_ssap          = 0xaa;
     token_ring_hdr.token_ring_llc_control_field = 0x03;      token_ring_hdr.token_ring_llc_control_field = 0x03;
     token_ring_hdr.token_ring_type              = htons(type);      token_ring_hdr.token_ring_type              = htons(type);
    memcpy(token_ring_hdr.token_ring_dhost, dst, ETHER_ADDR_LEN);     memcpy(token_ring_hdr.token_ring_dhost, dst, ETHER_ADDR_LEN);
    memcpy(token_ring_hdr.token_ring_shost, libnet_get_hwaddr(l),     memcpy(token_ring_hdr.token_ring_shost, libnet_get_hwaddr(l),
           ETHER_ADDR_LEN);            ETHER_ADDR_LEN);
    
     if (payload && payload_s)      if (payload && payload_s)
     {      {
         /*          /*
Line 194  libnet_ptag_t ptag) Line 178  libnet_ptag_t ptag)
   
 int  int
 libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t *payload, uint32_t payload_s)  libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t *payload, uint32_t payload_s)
{    {
     static BYTE dst[ETHER_ADDR_LEN];      static BYTE dst[ETHER_ADDR_LEN];
     static BYTE src[ETHER_ADDR_LEN];      static BYTE src[ETHER_ADDR_LEN];
        
    uint8_t *packet      = NULL;    uint8_t *packet;
     uint32_t packet_s;      uint32_t packet_s;
   
    LPPACKET lpPacket   = NULL;    DWORD remoteip = 0;
    DWORD remoteip      = 0; 
    DWORD BytesTransfered; 
     NetType type;      NetType type;
     struct libnet_ipv4_hdr *ip_hdr = NULL;      struct libnet_ipv4_hdr *ip_hdr = NULL;
        
     memset(dst, 0, sizeof(dst));      memset(dst, 0, sizeof(dst));
     memset(src, 0, sizeof(src));      memset(src, 0, sizeof(src));
   
     packet_s = payload_s + l->link_offset;      packet_s = payload_s + l->link_offset;
    packet = (uint8_t *)malloc(packet_s);    packet = (uint8_t*) alloca(packet_s);
    if (packet == NULL)
     /* we have to do the IP checksum
      * FIXME: warning is correct, checksum modifies its input.
      *        Fix is to build checksum inside the allocated 'packet'
      */
     if (libnet_inet_checksum(l, (uint8_t*)payload, IPPROTO_IP, LIBNET_IPV4_H, payload, payload+payload_s) == -1)
     {      {
         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,  
                 "%s(): failed to allocate packet\n", __func__);  
         return (-1);  
     }  
           
     /* we have to do the IP checksum  */  
     if (libnet_do_checksum(l, payload, IPPROTO_IP, LIBNET_IPV4_H) == -1)  
     {  
         /* error msg set in libnet_do_checksum */          /* error msg set in libnet_do_checksum */
         return (-1);          return (-1);
    }     }
   
     /* MACs, IPs and other stuff... */      /* MACs, IPs and other stuff... */
     ip_hdr = (struct libnet_ipv4_hdr *)payload;      ip_hdr = (struct libnet_ipv4_hdr *)payload;
     memcpy(src, libnet_get_hwaddr(l), sizeof(src));      memcpy(src, libnet_get_hwaddr(l), sizeof(src));
     remoteip = ip_hdr->ip_dst.S_un.S_addr;      remoteip = ip_hdr->ip_dst.S_un.S_addr;
                
     /* check if the remote station is the local station */      /* check if the remote station is the local station */
     if (remoteip == libnet_get_ipaddr4(l))      if (remoteip == libnet_get_ipaddr4(l))
     {      {
Line 242  libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t Line 221  libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t
     }      }
   
     PacketGetNetType(l->lpAdapter, &type);      PacketGetNetType(l->lpAdapter, &type);
        
     switch(type.LinkType)      switch(type.LinkType)
     {      {
         case NdisMedium802_3:          case NdisMedium802_3:
Line 260  libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t Line 239  libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t
         case NdisMediumArcnet878_2:          case NdisMediumArcnet878_2:
         default:          default:
             snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,              snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): network type (%d) is not supported\n", __func__,                "%s(): network type (%d) is not supported", __func__,
                 type.LinkType);                  type.LinkType);
             return (-1);              return (-1);
             break;  
     }      
   
     BytesTransfered = -1;  
     if ((lpPacket = PacketAllocatePacket()) == NULL)  
     {  
         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,  
                 "%s(): failed to allocate the LPPACKET structure\n", __func__);  
             return (-1);  
     }      }
        return libnet_write_link (l, packet, packet_s);
    PacketInitPacket(lpPacket, packet, packet_s); 
 
    /* PacketSendPacket returns a BOOLEAN */ 
    if (PacketSendPacket(l->lpAdapter, lpPacket, TRUE)) 
    { 
        BytesTransfered = packet_s; 
    } 
 
    PacketFreePacket(lpPacket); 
    free(packet); 
 
    return (BytesTransfered); 
 }  }
   
 int  int
Line 311  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack Line 269  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack
     struct libnet_ipv4_hdr *ip_hdr;      struct libnet_ipv4_hdr *ip_hdr;
   
     if (l == NULL)      if (l == NULL)
    {     {
         return (-1);          return (-1);
    }     }
   
     ip_hdr = (struct libnet_ipv4_hdr *)packet;      ip_hdr = (struct libnet_ipv4_hdr *)packet;
   
Line 321  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack Line 279  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack
     /*      /*
      *  For link access, we don't need to worry about the inconsistencies of       *  For link access, we don't need to worry about the inconsistencies of
      *  certain BSD kernels.  However, raw socket nuances abound.  Certain       *  certain BSD kernels.  However, raw socket nuances abound.  Certain
     *  BSD implmentations require the ip_len and ip_off fields to be in host     *  BSD implementations require the ip_len and ip_off fields to be in host
      *  byte order.       *  byte order.
      */       */
     ip_hdr->ip_len = FIX(ip_hdr->ip_len);      ip_hdr->ip_len = FIX(ip_hdr->ip_len);
Line 331  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack Line 289  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack
     memset(&sin, 0, sizeof(sin));      memset(&sin, 0, sizeof(sin));
     sin.sin_family  = AF_INET;      sin.sin_family  = AF_INET;
     sin.sin_addr.s_addr = ip_hdr->ip_dst.s_addr;      sin.sin_addr.s_addr = ip_hdr->ip_dst.s_addr;
 #if (__WIN32__)  
     /* set port for TCP */  
     /*  
      *  XXX - should first check to see if there's a pblock for a TCP  
      *  header, if not we can use a dummy value for the port.  
      */  
     if (ip_hdr->ip_p == 6)  
     {  
         struct libnet_tcp_hdr *tcph_p =  
                 (struct libnet_tcp_hdr *)(packet + (ip_hdr->ip_hl << 2));  
         sin.sin_port = tcph_p->th_dport;  
     }  
     /* set port for UDP */  
     /*  
      *  XXX - should first check to see if there's a pblock for a UDP  
      *  header, if not we can use a dummy value for the port.  
      */  
     else if (ip_hdr->ip_p == 17)  
     {  
         struct libnet_udp_hdr *udph_p =  
                 (struct libnet_udp_hdr *)(packet + (ip_hdr->ip_hl << 2));  
        sin.sin_port = udph_p->uh_dport;  
     }  
 #endif /* __WIN32__ */  
   
     c = sendto(l->fd, packet, size, 0, (struct sockaddr *)&sin,      c = sendto(l->fd, packet, size, 0, (struct sockaddr *)&sin,
             sizeof(sin));              sizeof(sin));
Line 366  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack Line 300  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack
   
     if (c != size)      if (c != size)
     {      {
 #if !(__WIN32__)  
         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,          snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): %d bytes written (%s)\n", __func__, c,                "%s(): %d bytes written (%s)", __func__, c,
                 strerror(errno));                  strerror(errno));
 #else /* __WIN32__ */  
         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,  
                 "%s(): %d bytes written (%d)\n", __func__, c,  
                 WSAGetLastError());  
 #endif /* !__WIN32__ */  
     }      }
     return (c);      return (c);
 }  }
Line 382  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack Line 310  libnet_write_raw_ipv4(libnet_t *l, const uint8_t *pack
 int  int
 libnet_write_raw_ipv6(libnet_t *l, const uint8_t *packet, uint32_t size)  libnet_write_raw_ipv6(libnet_t *l, const uint8_t *packet, uint32_t size)
 {  {
       int c = -1;
   
 #if defined HAVE_SOLARIS && !defined HAVE_SOLARIS_IPV6  #if defined HAVE_SOLARIS && !defined HAVE_SOLARIS_IPV6
    snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): no IPv6 support\n",    snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): no IPv6 support",
             __func__, strerror(errno));              __func__, strerror(errno));
     return (-1);  
 }  
 #else  #else
     int c;  
     struct sockaddr_in6 sin;      struct sockaddr_in6 sin;
     struct libnet_ipv6_hdr *ip_hdr;      struct libnet_ipv6_hdr *ip_hdr;
   
     if (l == NULL)      if (l == NULL)
    {     {
         return (-1);          return (-1);
    }     }
   
     ip_hdr = (struct libnet_ipv6_hdr *)packet;      ip_hdr = (struct libnet_ipv6_hdr *)packet;
   
Line 403  libnet_write_raw_ipv6(libnet_t *l, const uint8_t *pack Line 330  libnet_write_raw_ipv6(libnet_t *l, const uint8_t *pack
     sin.sin6_family  = AF_INET6;      sin.sin6_family  = AF_INET6;
     memcpy(sin.sin6_addr.s6_addr, ip_hdr->ip_dst.libnet_s6_addr,      memcpy(sin.sin6_addr.s6_addr, ip_hdr->ip_dst.libnet_s6_addr,
             sizeof(ip_hdr->ip_dst.libnet_s6_addr));              sizeof(ip_hdr->ip_dst.libnet_s6_addr));
       
     c = sendto(l->fd, packet, size, 0, (struct sockaddr *)&sin, sizeof(sin));      c = sendto(l->fd, packet, size, 0, (struct sockaddr *)&sin, sizeof(sin));
     if (c != size)      if (c != size)
     {      {
 #if !(__WIN32__)  
         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,          snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): %d bytes written (%s)\n", __func__, c,                "%s(): %d bytes written (%s)", __func__, c,
                 strerror(errno));                  strerror(errno));
 #else /* __WIN32__ */  
         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,  
                 "%s(): %d bytes written (%d)\n", __func__, c,  
                 WSAGetLastError());  
 #endif /* !__WIN32__ */  
     }      }
   #endif  /* HAVE_SOLARIS && !HAVE_SOLARIS_IPV6 */
     return (c);      return (c);
 }  }
#endif#endif /* __WIN32__ */
#endif 
/* EOF *//**
  * Local Variables:
  *  indent-tabs-mode: nil
  *  c-file-style: "stroustrup"
  * End:
  */

Removed from v.1.1.1.2  
changed lines
  Added in v.1.1.1.3


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>