IP address conversion function-PHP source code

Source: Internet
Author: User
The IP address translation function has two sets of IP address translation functions, which can be used to convert IP addresses between strings and binary values in the byte sequence of the network.

Functions applicable to IPv4 addresses

#include /* Convert Internet host address from numbers-and-dots notation in CP   into binary data and store the result in the structure INP.  */int inet_aton (const char *__cp, struct in_addr *__inp);/* Convert Internet host address from numbers-and-dots notation in CP   into binary data in network byte order.  */in_addr_t inet_addr (const char *__cp);/* Convert Internet number in IN to ASCII representation.  The return value   is a pointer to an internal array containing the string.  */char *inet_ntoa (struct in_addr __in);

The preceding three functions are used to convert IPv4 addresses between the dot-decimal number string (for example, "127.0.0.1") and the 32-bit network byte binary values.

Inet_aton converts the string pointed to by _ cp into an address structure pointed to by _ p in the network order. 1 is returned. otherwise, 0 is returned. (According to the book, if the _ indium pointer is null, the function still checks the validity of the input string, but does not store any results)

The inet_addr function is similar to the inet_aton function. However, if an inet_addr error occurs, the INADDR_NONE constant value is returned (usually the value of 32 bits and 1 bits ), this means that at least one IPv4 address (usually broadcast address 255.255.255.255.255) cannot be processed by this function. We recommend that you use inet_aton instead of inet_addr.

Inet_ntoa converts the binary IPv4 address in the network sequence to a dotted-decimal number string. The string to which the return value of this function points resides in static memory. This means that the function cannot be reentrant. At the same time, we should also note that this function takes a struct as a parameter rather than a common struct pointer as a parameter.

Functions applicable to both IPv4 and IPv6 addresses

#include /* Convert from presentation format of an Internet number in buffer   starting at CP to the binary network format and store result for   interface type AF in buffer starting at BUF.  */int inet_pton (int __af, const char *__restrict __cp,              void *__restrict __buf);/* Convert a Internet address in binary network format for interface   type AF in buffer starting at CP to presentation form and place   result in buffer of length LEN astarting at BUF.  */const char *inet_ntop (int __af, const void *__restrict __cp,                  char *__restrict __buf, socklen_t __len);

The _ af parameter of these two functions can be either AF_INET or AF_INET6. If an unsupported address family is used as the parameter, an error is returned and errno is set to EAFNOSUPPORT.

Inet_pton converts the string to the binary value of the corresponding network sequence. inet_ntop is used to do the opposite. The _ len parameter specifies the size of the target buffer zone. The header file defines

1 #define INET_ADDRSTRLEN 16    /* for IPv4 dotted-decimal */2 #define INET6_ADDRSTRLEN 46     /* for IPv6 hex string */

If _ len is too small, a null pointer is returned, and errno is ENOSPC.

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.