Diff for /embedaddon/libnet/src/libnet_resolve.c between versions 1.1.1.1 and 1.1.1.2

version 1.1.1.1, 2012/02/21 22:14:23 version 1.1.1.2, 2013/07/22 11:54:42
Line 39 Line 39
 #include "../include/win32/libnet.h"  #include "../include/win32/libnet.h"
 #endif  #endif
   
   #ifndef HAVE_GETHOSTBYNAME2
   struct hostent *
   gethostbyname2(const char *name, int af)
   {
           return gethostbyname(name);
   }
   #endif
   
 char *  char *
libnet_addr2name4(u_int32_t in, u_int8_t use_name)libnet_addr2name4(uint32_t in, uint8_t use_name)
 {  {
         #define HOSTNAME_SIZE 512          #define HOSTNAME_SIZE 512
     static char hostname[HOSTNAME_SIZE+1], hostname2[HOSTNAME_SIZE+1];      static char hostname[HOSTNAME_SIZE+1], hostname2[HOSTNAME_SIZE+1];
    static u_int16_t which;    static uint16_t which;
    u_int8_t *p;    uint8_t *p;
   
     struct hostent *host_ent = NULL;      struct hostent *host_ent = NULL;
     struct in_addr addr;      struct in_addr addr;
Line 68  libnet_addr2name4(u_int32_t in, u_int8_t use_name) Line 76  libnet_addr2name4(u_int32_t in, u_int8_t use_name)
     if (!host_ent)      if (!host_ent)
     {      {
   
        p = (u_int8_t *)∈        p = (uint8_t *)∈
                 snprintf(((which % 2) ? hostname : hostname2), HOSTNAME_SIZE,                  snprintf(((which % 2) ? hostname : hostname2), HOSTNAME_SIZE,
                  "%d.%d.%d.%d",                   "%d.%d.%d.%d",
                  (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));                   (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
Line 84  libnet_addr2name4(u_int32_t in, u_int8_t use_name) Line 92  libnet_addr2name4(u_int32_t in, u_int8_t use_name)
 }  }
   
 void  void
libnet_addr2name4_r(u_int32_t in, u_int8_t use_name, char *hostname,libnet_addr2name4_r(uint32_t in, uint8_t use_name, char *hostname,
         int hostname_len)          int hostname_len)
 {  {
    u_int8_t *p;    uint8_t *p;
     struct hostent *host_ent = NULL;      struct hostent *host_ent = NULL;
     struct in_addr addr;      struct in_addr addr;
   
Line 99  libnet_addr2name4_r(u_int32_t in, u_int8_t use_name, c Line 107  libnet_addr2name4_r(u_int32_t in, u_int8_t use_name, c
     }      }
     if (!host_ent)      if (!host_ent)
     {      {
        p = (u_int8_t *)∈        p = (uint8_t *)∈
         snprintf(hostname, hostname_len, "%d.%d.%d.%d",          snprintf(hostname, hostname_len, "%d.%d.%d.%d",
                 (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));                  (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
     }      }
Line 110  libnet_addr2name4_r(u_int32_t in, u_int8_t use_name, c Line 118  libnet_addr2name4_r(u_int32_t in, u_int8_t use_name, c
     }      }
 }  }
   
u_int32_tuint32_t
libnet_name2addr4(libnet_t *l, char *host_name, u_int8_t use_name)libnet_name2addr4(libnet_t *l, char *host_name, uint8_t use_name)
 {  {
     struct in_addr addr;      struct in_addr addr;
     struct hostent *host_ent;       struct hostent *host_ent; 
    u_int32_t m;    uint32_t m;
    u_int val;    uint val;
     int i;      int i;
   
     if (use_name == LIBNET_RESOLVE)      if (use_name == LIBNET_RESOLVE)
Line 126  libnet_name2addr4(libnet_t *l, char *host_name, u_int8 Line 134  libnet_name2addr4(libnet_t *l, char *host_name, u_int8
             if (!(host_ent = gethostbyname(host_name)))              if (!(host_ent = gethostbyname(host_name)))
             {              {
                 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,                  snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                        "%s(): %s\n", __func__, strerror(errno));                        "%s(): %s\n", __func__, hstrerror(h_errno));
                 /* XXX - this is actually 255.255.255.255 */                  /* XXX - this is actually 255.255.255.255 */
                 return (-1);                  return (-1);
             }              }
Line 187  libnet_name2addr4(libnet_t *l, char *host_name, u_int8 Line 195  libnet_name2addr4(libnet_t *l, char *host_name, u_int8
 }  }
   
 void  void
libnet_addr2name6_r(struct libnet_in6_addr addr, u_int8_t use_name,libnet_addr2name6_r(struct libnet_in6_addr addr, uint8_t use_name,
             char *host_name, int host_name_len)              char *host_name, int host_name_len)
 {  {
     struct hostent *host_ent = NULL;      struct hostent *host_ent = NULL;
Line 222  libnet_addr2name6_r(struct libnet_in6_addr addr, u_int Line 230  libnet_addr2name6_r(struct libnet_in6_addr addr, u_int
   
 const struct libnet_in6_addr in6addr_error = IN6ADDR_ERROR_INIT;  const struct libnet_in6_addr in6addr_error = IN6ADDR_ERROR_INIT;
   
   int
   libnet_in6_is_error(struct libnet_in6_addr addr)
   {
       return 0 == memcmp(&addr, &in6addr_error, sizeof(addr));
   }
   
 struct libnet_in6_addr  struct libnet_in6_addr
libnet_name2addr6(libnet_t *l, char *host_name, u_int8_t use_name)libnet_name2addr6(libnet_t *l, const char *host_name, uint8_t use_name)
 {  {
 #if !defined (__WIN32__)  #if !defined (__WIN32__)
     struct libnet_in6_addr addr;      struct libnet_in6_addr addr;
Line 264  libnet_name2addr6(libnet_t *l, char *host_name, u_int8 Line 278  libnet_name2addr6(libnet_t *l, char *host_name, u_int8
     }      }
     else      else
     {      {
                #if defined(__WIN32__) /* Silence Win32 warning */#if defined(__WIN32__) /* Silence Win32 warning */
                if (l)        if (l)
         {                  {        
                snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,                 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                 "%s(): can't resolve IPv6 addresses.\n", __func__);                  "%s(): can't resolve IPv6 addresses.\n", __func__);
         }          }
         return (in6addr_error);          return (in6addr_error);
        #else#else
         if(!inet_pton(AF_INET6, host_name, &addr))          if(!inet_pton(AF_INET6, host_name, &addr))
         {          {
             if (l)              if (l)
Line 282  libnet_name2addr6(libnet_t *l, char *host_name, u_int8 Line 296  libnet_name2addr6(libnet_t *l, char *host_name, u_int8
             return (in6addr_error);              return (in6addr_error);
         }          }
         return (addr);          return (addr);
        #endif#endif
     }      }
 }  }
   
   #ifdef HAVE_GETIFADDRS
   
   #include <ifaddrs.h>
   
 struct libnet_in6_addr  struct libnet_in6_addr
 libnet_get_ipaddr6(libnet_t *l)  libnet_get_ipaddr6(libnet_t *l)
 {  {
       struct ifaddrs *ifaddr, *p;
       struct libnet_in6_addr addr;
   
       if (l == NULL)
       {
           return (in6addr_error);
       }
   
       if (getifaddrs(&ifaddr) != 0)
       {
           snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                   "%s(): getifaddrs(): %s\n", __func__, strerror(errno));
           return (in6addr_error);
       }
   
       if (l->device == NULL)
       {
           if (libnet_select_device(l) == -1)
           {
               /* error msg set in libnet_select_device() */
               return (in6addr_error);
           }
       }
   
       for (p = ifaddr; p != NULL; p = p->ifa_next)
       {
           if ((strcmp(p->ifa_name, l->device) == 0) && (p->ifa_addr != NULL) &&
                   (p->ifa_addr->sa_family == AF_INET6))
           {
               memcpy(&addr.__u6_addr,
                       ((struct sockaddr_in6*)p->ifa_addr)->sin6_addr.s6_addr, 16);
               freeifaddrs(ifaddr);
               return (addr);
           }
       }
   
       freeifaddrs(ifaddr);
       return (in6addr_error);
   }
   #else
   struct libnet_in6_addr
   libnet_get_ipaddr6(libnet_t *l)
   {
     snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,      snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
            "%s(): not yet Implemented\n", __func__);             "%s(): not yet Implemented\n", __func__);
     return (in6addr_error);      return (in6addr_error);
 }  }
   #endif /* WIN32 */
   
 #if !defined(__WIN32__)  #if !defined(__WIN32__)
u_int32_tuint32_t
 libnet_get_ipaddr4(libnet_t *l)  libnet_get_ipaddr4(libnet_t *l)
 {  {
     struct ifreq ifr;      struct ifreq ifr;
Line 344  libnet_get_ipaddr4(libnet_t *l) Line 406  libnet_get_ipaddr4(libnet_t *l)
 }  }
 #else  #else
 #include <Packet32.h>  #include <Packet32.h>
u_int32_tuint32_t
 libnet_get_ipaddr4(libnet_t *l)  libnet_get_ipaddr4(libnet_t *l)
 {  {
     long npflen = 0;      long npflen = 0;
Line 363  libnet_get_ipaddr4(libnet_t *l) Line 425  libnet_get_ipaddr4(libnet_t *l)
 }  }
 #endif /* WIN32 */  #endif /* WIN32 */
   
u_int8_t *uint8_t *
libnet_hex_aton(int8_t *s, int *len)libnet_hex_aton(const char *s, int *len)
 {  {
    u_int8_t *buf;    uint8_t *buf;
     int i;      int i;
     int32_t l;      int32_t l;
    int8_t *pp;    char *pp;
                   
     while (isspace(*s))      while (isspace(*s))
     {      {
Line 390  libnet_hex_aton(int8_t *s, int *len) Line 452  libnet_hex_aton(int8_t *s, int *len)
     /* expect len hex octets separated by ':' */      /* expect len hex octets separated by ':' */
     for (i = 0; i < *len + 1; i++)      for (i = 0; i < *len + 1; i++)
     {      {
        l = strtol(s, (char **)&pp, 16);        l = strtol(s, &pp, 16);
         if (pp == s || l > 0xff || l < 0)          if (pp == s || l > 0xff || l < 0)
         {          {
             *len = 0;              *len = 0;
Line 403  libnet_hex_aton(int8_t *s, int *len) Line 465  libnet_hex_aton(int8_t *s, int *len)
             free(buf);              free(buf);
             return (NULL);              return (NULL);
         }          }
        buf[i] = (u_int8_t)l;        buf[i] = (uint8_t)l;
         s = pp + 1;          s = pp + 1;
     }      }
     /* return int8_tacter after the octets ala strtol(3) */      /* return int8_tacter after the octets ala strtol(3) */

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


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