IP Address Translation function

Source: Internet
Author: User

These days have been watching "UNP", feel the need to do some things to summarize (in fact, just copy a transcription), deepen the impression.

There are two sets of IP address translation functions that can convert IP addresses between strings and binary values of the network byte order

Functions that apply to IPV4 addresses

1#include <arpa/inet.h>2 3 /*Convert Internet host address from numbers-and-dots notation in CP4 into binary data and store the result in the structure INP. */5 intInet_aton (Const Char*__CP,structIN_ADDR *__INP);6 7 /*Convert Internet host address from numbers-and-dots notation in CP8 into binary data in network byte order. */9in_addr_t inet_addr (Const Char*__cp);Ten  One /*Convert Internet number in to ASCII representation. The return value A Is a pointer to an internal array containing the string. */ - Char*inet_ntoa (structIn_addr __in);

The above three functions convert IPv4 addresses between dotted decimal string (such as "127.0.0.1") and 32-bit network byte-order binary values.

Inet_aton __CP The string to the network order address exists __INP point to the address structure. Successfully returns 1, otherwise 0 is returned. (According to the book, if the __INP pointer is empty, the function still checks the input string for validity but does not store any results)

The inet_addr function is similar to Inet_aton, but inet_addr returns a Inaddr_none constant (usually a value of 32 bits of 1) when an error occurs. This means that at least one IPV4 address (usually broadcast address 255.255.255.255) cannot be handled by the function. It is recommended to use Inet_aton instead of inet_addr.

Inet_ntoa Converts the network order binary IPv4 address into dotted decimal string. The function's return value points to a string that resides in static memory. This means that the function is non-reentrant. It is also important to note that the function takes a struct as a parameter instead of a common one as a struct-body pointer.

Functions that apply to both the IPV4 address and the IPV6 address

1#include <arpa/inet.h>2 3 /*Convert from presentation format of a Internet number in buffer4 starting at CP to the binary network format and store result for5 interface type AF in buffer starting at BUF. */6 intInet_pton (int__AF,Const Char*__restrict __CP,7               void*__restrict __buf);8 9 /*Convert a Internet address in binary network format for interfaceTen type AF in buffer starting on CP to presentation form and place One result in buffer of length LEN astarting at BUF. */ A Const Char*inet_ntop (int__AF,Const void*__restrict __CP, -                   Char*__restrict __buf, socklen_t __len);

The __AF parameters of these two functions can be either af_inet or AF_INET6. If an unsupported address family is used as a parameter, an error is returned and errno is set to Eafnosupport.

Inet_pton The string to the corresponding network order binary value,inet_ntop do the opposite thing, where the __len parameter specifies the size of the target buffer, defined in the <netinet/in.h> header file

1 #define Inet_addrstrlen/    * for IPv4 dotted-decimal */2#define inet6_addrstrlen/     * for IPV6 Hex St Ring */

If the given __len is too small, then a null pointer is returned, and the errno is ENOSPC.

IP Address Translation function

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.