Network address translation Correlation function usage (INET_ADDR,INET_NTOA,INET_ADDR)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.