Aa
Correlation function prototypes and parameter types:
Function Prototypes:
intInet_aton (Const Char*CP,structIN_ADDR *INP); in_addr_t inet_addr (Const Char*CP); in_addr_t Inet_network (Const Char*CP);Char*inet_ntoa (structIn_addrinch);structIN_ADDR inet_makeaddr (intNetinthost); in_addr_t inet_lnaof (structIn_addrinch); in_addr_t Inet_netof (structIn_addrinch);
int Inet_pton (int af, const char *src, void *DST);
Inet_pton () returns 1 on success (network address is successfully converted). 0 is returned if SRC does not contain a character string representing a valid network address in the specified
Address family. If AF does not contain a valid address family,-1 is returned and errno are set to Eafnosupport.
Parameter type:
/* Internet address. */
typedef uint32_t in_addr_t;
struct IN_ADDR
{
in_addr_t s_addr;
};
inet_addr function: Converts the dotted decimal address of a IPv4 to a network byte order
#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<arpa/inet.h>int main (int argc, char *argv[])
{
struct IN_ADDR addr;
if (argc! = 2) {
fprintf (stderr, "%s <dotted-address>\n", argv[0]);
Exit (Exit_failure);
}
if (Inet_aton (argv[1], &addr) = = 0) {
Perror ("Inet_aton");
Exit (Exit_failure);
}
printf ("addr = 0x%-10x\n", addr.s_addr);
printf ("%s\n", Inet_ntoa (addr));
Exit (exit_success);
}
[Email protected]:~/tmp/network$./a.out 127.0.0.1
addr = 0x100007f
127.0.0.1
Char *inet_ntoa (structin) { staticchar buf[inet_ Ntoa_max_len]; return inet_ntoa_r (in, buf);}
Attention:
1, the return value of Inet_ntoa is a static type of char * pointer, so you need to pay attention when using (non-reentrant, you can use the thread-safe inet_ntoa_r function instead)
2. The inet_addr() function converts the Internet host address CP from IPV4 numbers-and-dots notation to binary Data in network byte order. If the input is invalid, Inaddr_none (usually-1) is
Returned. Use of this function is problematic (problematic) because-1 is a valid address (255.255.255.255). Avoid its use in favor of Inet_aton (), Inet_pton (3), or getaddrinfo (3) which provide a cleaner
The to indicate error return. (because its return value is in_addr_t, when 1 is returned, there are two semantics- ---two meaning)
3, Inet_aton () returns nonzero if the address is valid and zero if not.
4. Inet_aton/inet_addr/inet_ntoa only applicable to IPv4 address
5, Inet_pton/inet_ntop for V4 and V6 addresses are applicable
Network address translation Correlation function usage (INET_ADDR,INET_NTOA,INET_ADDR)