UNIX network Programming--IP processing Functions Inet_aton (), gethostbyname (), etc.

Source: Internet
Author: User

The IP address is essentially a 32-bit unsigned integer that is stored in the following structure

1 struct  23   int4 };

For historical reasons, although the IP address is just a scalar, it is stored with a struct.

Because the host can have different host byte order, that is, big-endian machine or small end machine. But TCP/IP defines a uniform network byte order, big endian byte order.

UNIX provides two functions to convert between host bytes and network bytes:

I, htonl () and Ntohl ()

Under the Linux system:

#include <arpa/inet.h>

Some systems contain header files that are <netinet/in.h> instead of <arpa/inet.h>

unsigned long int htonl (unsigned long int hostlong); Returns the value by network byte order

unsigned long int ntohl (unsigned long int hostlong); Returns the value by host byte order

"H" means host hosts, "N" means network networks, "to" represents the transformation.

II, Inet_aton () and Inet_ntoa ()

#include <arpa/inet.h>

int Inet_aton (const char *cp,strcut in_addr *INP); If 1 is returned successfully, the error returns 0

int Inet_ntoa (Strcut in_addr in); Returns a pointer to a dotted decimal point

"N" means network networks, "a" means application application, "to" represents the transformation.

The Inet_aton function converts a dot 10 string into a network byte order IP address, similarly, the Inet_ntoa function converts an IP address of a network byte order to its corresponding dotted decimal string. Note that the pointer is passed to the Inet_aton, while the Inet_ntoa is the struct itself.

III, gethostbyname () and gethostbyaddr ()

The DNS database has many DNS entries to store the mappings between the domain name and the IP, as shown in the structure below

1 structhostent2 {3   Char* H_NAME;//official domain name of host4   Char* * h_aliases;//null-terminated arraty of domain names5    ShortH_addrtype;//Host Address Type (AF_INET)6    ShortH_length;//Length of an address,in bytes7   Char* * H_ADDR_LIST;//null-terminated Arraty of in_addr structs8};

#include <netdb.h>

#include <sys/socket.h>

struct hostent *gethostbyname (const char *name); If a non-null pointer is returned successfully, if an error returns null, set H_errno

struct hostent *gethostbyaddr (const char *addr,int len,0); If a non-null pointer is returned successfully, if an error returns null, set H_errno

The gethostbyname function returns the host entry associated with the domain name name (such as www.baidu.com)

The GETHOSTBYADDR function returns the host entry associated with the IP address addr (the address of the network byte order), and the second parameter gives the length of the first IP address, which is always four bytes for the current Internet. For our requirements, the third parameter is always 0.

The specific contents of the host entry, see the above structure. IP addresses and domain names may be one-to-many, multi-pair, multiple-to-many, or even domain names that may not be mapped to any IP.

UNIX network Programming--IP processing Functions Inet_aton (), gethostbyname (), etc.

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.