Address Resolution Functions
The address resolution functions provide libnet programmers with a convenient way to resolve address issues of one kind or another. Table 3.2 details the symbolic constants that the use_name variable can be in the first three functions.CONSTANT | MEANING |
---|---|
LIBNET_RESOLVE | Attempt to resolve the IP address into a hostname or vice-versa. |
LIBNET_DONT_RESOLVE | Do not attempt a name lookup. |
u_char *libnet_addr2name4(u_long in, u_short use_name);
libnet_addr2name4() converts a big-endian IPv4 address to a presentation format human-readable string. If use_name is LIBNET_DONT_RESOLVE or if the internal lookup fails, the function will return a string consisting of dots and decimals (for example, "192.168.2.100"). If use_name is LIBNET_RESOLVE, the function attempts to resolve the IP address into a hostname, which might incur a Domain Name Service (DNS) or Yellow Pages (YP) lookup and could take some time to complete. Upon success, the function returns a pointer to a human-readable string. Ostensibly, the function cannot fail.
void libnet_addr2name4_r(u_long in, u_short use_name, u_char
*hostname, int hostname_len);
libnet_addr2name4_r() provides the same functionality as libnet_addr2name4() with the notable difference of being reentrant. hostname, which should be a preallocated buffer of size hostname_len will hold the results of the function.
u_long libnet_name2addr4(u_char *hostname, u_short use_name);
libnet_name2addr4() resolves hostname into a big-endian IPv4 address. If use_name is LIBNET_RESOLVE, the function expects host_name to be a presentation format hostname (for example, "foobar.com"). This situation has the potential to incur a DNS or YP reverse lookup, which again could take an appreciable amount of time to complete. If this internal lookup fails, the function cannot recover. If use_name is LIBNET_DONT_RESOLVE, the function expects hostname to be a dots and decimals presentation format string. Upon success, the function returns a little-endian IPv4 address; upon failure, the function returns -1 and libnet_geterror() can tell you why.
Note | The error value of -1 is actually the IP address 255.255.255.255. Professionals so rarely encounter this situation in practice that we overlook this one fringe case here. |
u_long libnet_get_ipaddr4(libnet_t *1);
libnet_get_ipaddr4() returns the little-endian IPv4 address of the interface associated with 1. Upon success, the function returns a little-endian IPv4 address; upon failure, the function returns -1 and libnet_geterror() can tell you why.
Note | The error value of -1 is actually the IP address 255.255.255.255. This situation is so rarely encountered in practice that we overlook this one fringe case here. |
void libnet_addr2name6_r(struct libnet_in6_addr, u_short
use_name, u_char *hostname, int hostname_len);
libnet_addr2name6() converts an IPv6 address to a presentation format human-readable string. If use_name is LIBNET_DONT_RESOLVE or if the internal lookup fails, the function will return a string consisting of dots and decimals (for example, "3ffe:3700:402:0:210:a4ff:fe12:fec4"). If use_name is LIBNET_RESOLVE, the function attempts to resolve the IP address into a hostname, which might incur a DNS lookup and could take some time to complete. Upon success, the function returns a pointer to a human-readable string. Ostensibly, the function cannot fail.
struct libnet_in6_addr libnet_name2addr6(libnet_t *1, u_char
*hostname, u_short use_name);
libnet_name2addr6() resolves hostname into an IPv6 address. If use_name is LIBNET_RESOLVE, the function expects host_name to be a presentation format hostname (for example, "foobar.com"). This situation has the potential to incur a DNS lookup, which could take an appreciable amount of time to complete. If this internal lookup fails, the function cannot recover. If use_name is LIBNET_DONT_RESOLVE, the function expects host_name to be a colons and hexidecimals presentation format string. Upon success, the function returns an IPv6 address structure; upon failure, the function returns in6addr_error and libnet_geterror() can tell you why.
struct ether_addr *libnet_get_hwaddr (libnet_t *1);
libnet_get_hwaddr() culls the hardware address from the device that 1 references. Upon success, the function returns the hardware address of the device. Upon failure, the function returns NULL and libnet_geterror() tells you why.
Note | While this function's name implies "Hardware Address," it actually returns Ethernet addresses only. |
u_char *libnet_hex_aton(char *s, int *len);
libnet_hex_aton() parses an arbitrarily sized hexadecimal character string s and returns its byte string equivalent, storing the length in len. Upon success, the function returns a byte string suitable for use in subsequent libnet functions and len will contain the size of the byte string; upon failure, the function returns NULL. The function can only fail if an illegal token is encountered within the character string. The function expects s to be of the format "xx:xx:xx … xx:xx:xx" where "xx" is a hexadecimal digit.
Note | This function does an implicit malloc() and as such, the returned string should be free()'d when it is finished being used. |