version 1.1.1.1, 2012/02/21 22:14:23
|
version 1.1.1.3, 2023/09/27 11:11:38
|
Line 1
|
Line 1
|
/* |
/* |
* $Id$ |
|
* |
|
* libnet |
* libnet |
* libnet_resolve.c - various name resolution type routines |
* libnet_resolve.c - various name resolution type routines |
* |
* |
Line 30
|
Line 28
|
* |
* |
*/ |
*/ |
|
|
#if (HAVE_CONFIG_H) | #include "common.h" |
#include "../include/config.h" | |
| #ifndef HAVE_GETHOSTBYNAME2 |
| struct hostent * |
| gethostbyname2(const char *name, int af) |
| { |
| return gethostbyname(name); |
| } |
#endif |
#endif |
#if (!(_WIN32) || (__CYGWIN__)) |
|
#include "../include/libnet.h" |
|
#else |
|
#include "../include/win32/libnet.h" |
|
#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 67 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 83 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 98 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 109 libnet_addr2name4_r(u_int32_t in, u_int8_t use_name, c
|
} |
} |
} |
} |
|
|
u_int32_t | uint32_t |
libnet_name2addr4(libnet_t *l, char *host_name, u_int8_t use_name) | libnet_name2addr4(libnet_t *l, const 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; | uint32_t 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 125 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", __func__, |
| #if (_WIN32) |
| "gethostbyname failure" |
| #else |
| /* FIXME doesn't exist on windows, needs WSAGetLastError()/FormatMessage */ |
| hstrerror(h_errno) |
| #endif |
| ); |
/* XXX - this is actually 255.255.255.255 */ |
/* XXX - this is actually 255.255.255.255 */ |
return (-1); |
return (-1); |
} |
} |
Line 145 libnet_name2addr4(libnet_t *l, char *host_name, u_int8
|
Line 151 libnet_name2addr4(libnet_t *l, char *host_name, u_int8
|
if (l) |
if (l) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): expecting dots and decimals\n", __func__); | "%s(): expecting dots and decimals", __func__); |
} |
} |
/* XXX - this is actually 255.255.255.255 */ |
/* XXX - this is actually 255.255.255.255 */ |
return (-1); |
return (-1); |
Line 167 libnet_name2addr4(libnet_t *l, char *host_name, u_int8
|
Line 173 libnet_name2addr4(libnet_t *l, char *host_name, u_int8
|
if (l) |
if (l) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): value greater than 255\n", __func__); | "%s(): value greater than 255", __func__); |
} |
} |
/* 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 193 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 228 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 237 libnet_name2addr6(libnet_t *l, char *host_name, u_int8
|
Line 249 libnet_name2addr6(libnet_t *l, char *host_name, u_int8
|
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", __func__); |
} |
} |
return (in6addr_error); |
return (in6addr_error); |
#else |
#else |
Line 247 libnet_name2addr6(libnet_t *l, char *host_name, u_int8
|
Line 259 libnet_name2addr6(libnet_t *l, char *host_name, u_int8
|
sizeof(struct in_addr), AF_INET6, NULL))) |
sizeof(struct in_addr), AF_INET6, NULL))) |
#else |
#else |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): %s\n", __func__, strerror(errno)); | "%s(): %s", __func__, strerror(errno)); |
return (in6addr_error); |
return (in6addr_error); |
#endif |
#endif |
#else |
#else |
Line 264 libnet_name2addr6(libnet_t *l, char *host_name, u_int8
|
Line 276 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.", __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) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): invalid IPv6 address\n", __func__); | "%s(): invalid IPv6 address", __func__); |
} |
} |
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", __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", __func__); |
return (in6addr_error); |
return (in6addr_error); |
} |
} |
|
#endif /* WIN32 */ |
|
|
#if !defined(__WIN32__) |
#if !defined(__WIN32__) |
u_int32_t | uint32_t |
libnet_get_ipaddr4(libnet_t *l) |
libnet_get_ipaddr4(libnet_t *l) |
{ |
{ |
struct ifreq ifr; |
struct ifreq ifr; |
Line 312 libnet_get_ipaddr4(libnet_t *l)
|
Line 372 libnet_get_ipaddr4(libnet_t *l)
|
if (fd == -1) |
if (fd == -1) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): socket(): %s\n", __func__, strerror(errno)); | "%s(): socket(): %s", __func__, strerror(errno)); |
return (-1); |
return (-1); |
} |
} |
|
|
Line 335 libnet_get_ipaddr4(libnet_t *l)
|
Line 395 libnet_get_ipaddr4(libnet_t *l)
|
if (ioctl(fd, SIOCGIFADDR, (int8_t*) &ifr) < 0) |
if (ioctl(fd, SIOCGIFADDR, (int8_t*) &ifr) < 0) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): ioctl(): %s\n", __func__, strerror(errno)); | "%s(): ioctl(): %s", __func__, strerror(errno)); |
close(fd); |
close(fd); |
return (-1); |
return (-1); |
} |
} |
Line 344 libnet_get_ipaddr4(libnet_t *l)
|
Line 404 libnet_get_ipaddr4(libnet_t *l)
|
} |
} |
#else |
#else |
#include <Packet32.h> |
#include <Packet32.h> |
u_int32_t | uint32_t |
libnet_get_ipaddr4(libnet_t *l) |
libnet_get_ipaddr4(libnet_t *l) |
{ |
{ |
long npflen = 0; | long npflen = 1; |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
struct npf_if_addr ipbuff; |
struct npf_if_addr ipbuff; |
|
|
memset(&sin,0,sizeof(sin)); |
memset(&sin,0,sizeof(sin)); |
memset(&ipbuff,0,sizeof(ipbuff)); |
memset(&ipbuff,0,sizeof(ipbuff)); |
|
|
npflen = sizeof(ipbuff); |
|
if (PacketGetNetInfoEx(l->device, &ipbuff, &npflen)) |
if (PacketGetNetInfoEx(l->device, &ipbuff, &npflen)) |
{ |
{ |
sin = *(struct sockaddr_in *)&ipbuff.IPAddress; |
sin = *(struct sockaddr_in *)&ipbuff.IPAddress; |
Line 363 libnet_get_ipaddr4(libnet_t *l)
|
Line 422 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 449 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 462 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) */ |
Line 411 libnet_hex_aton(int8_t *s, int *len)
|
Line 470 libnet_hex_aton(int8_t *s, int *len)
|
return (buf); |
return (buf); |
} |
} |
|
|
/* EOF */ | /** |
| * Local Variables: |
| * indent-tabs-mode: nil |
| * c-file-style: "stroustrup" |
| * End: |
| */ |