Address Problems in Linux Network Programming

Source: Internet
Author: User

The IP address we usually use is a string of the type 192.168.1.11, and an IP address expressed in binary format in the Linux kernel. In programming, the conversion between IP addresses in string expression mode and binary IP addresses is often used.
In the face of many address functions in network programming, do you hold it? tiger did not hold it, so I wrote this article, hoping that everyone can hold network programming.
The struct in_addr structure struct in_addr is in the file <netinet/in. h> defined in, the structure in_addr has an unsigned long int type member variable s_addr. generally, the binary form of the IP address is stored in the member variable s_addr.
The prototype of the structure struct in_addr is as follows: structin_addr {unsigned long int s_addr;/* IP Address */} 1. String IP address to a binary IP address function 1. the prototype of the inet series functions: # include <sys/socket. h> # include <netinet/in. h> # include <apra/inet. h> int inet_aton (const char * cp, struct in_addr * indium); int addr_t inet_addr (const char * cp); int addr_t inet_network (const char * cp ); char * struct (struct in_addr in); struct in_addr inet_makeaddr (int net, int host); struct inet_lnaof (struct in_addr in); 2. inet_aton () function int inet_aton (const char * cp, struct in_addr * indium)
1> function: The inet_ation () function converts an IP address of the dot-decimal string type stored in cp to a binary IP address, the converted value is stored in the structure struct in_addr pointed to by the P pointer.
2> parameters? Const char * cp: point to a character-type IP address. For example, "192.168.1.11 "? Struct in_addr * indium: IP address structin_addr {unsignedlong s_addr} pointing to the binary network byte sequence. 3> if the function is successfully executed, a non-0 value is returned. If the parameter is invalid, 0 is returned. 4> instance: # include <stdio. h> # include <stdlib. h> # include <string. h>
# Include <sys/socket. h> # include <netinet/in. h> # include <arpa/inet. h> int main ()
{Char buffer [32]; structin_addr in; in. s_addr = 0;/* enter a string-type ip Address */printf ("pleaseinput the ip addrss \ n"); scanf ("% s", buffer ); buffer [31] = '\ 0'; printf ("original IP Address: % s \ n", buffer); if (0 = inet_aton (buffer, & in )) {perror ("inet_aton"); exit (1) ;}else {printf ("after transfer: 0x % 0x \ n", in. s_addr);} 5> the inet_aton function finally calculates the binary IP address in the byte sequence of the network. 3. inet_addr () function in_addr_t inet_addr (const char * cp)
1> function: it converts the string-type IP address ("192.168.1.11") pointed by the parameter cp to a binary network IP address in byte order. The disadvantage of this function is: if the IP address is 255.255.255.255. after the inet_addr () function is called,-1 is returned (because the complement code of-1 is 0 xFFFFFFFF ). Therefore, the inet_addr () function is not recommended. Instead, the inet_aton () function is used.
2> function parameter: const char * cp: cp points to an IP address in the string format.
3> function return value: returns the IP address (struct in_add) of the binary network in the byte sequence after the function is successful. Otherwise,-1 is returned. 4> inet_addr calculates the binary IP address in the byte sequence of the network. 5> function instance: # include <stdio. h> # include <stdlib. h> # include <string. h>
# Include <sys/socket. h> # include <netinet/in. h> # include <arpa/inet. h> int main ()
{Char buffer [32]; structin_addr in; in. s_addr = 0;/* enter a string-type ip Address */printf ("pleaseinput the ip addrss \ n"); scanf ("% s", buffer ); buffer [31] = '\ 0'; printf ("original IPaddress: % s \ n", buffer); if (in. s_addr = inet_addr (buffer) = INADDR_NONE) {perror ("inet_addr"); exit (1) ;}else {printf ("after transfer: 0x % 0x \ n ", in. s_addr);} 4. inet_network in_addr_t inet_network (constchar * cp)
1> function: converts a network address in string format directed by the parameter cp to a binary IP address in the bytes sequence of the host.
2> function parameters? Const char * cp: cp points to an IP address in the string format.
3> function return value: After successful execution, the converted result is returned. If the parameter is invalid,-1 is returned. 4> inet_network returns host byte order 5> function instance: # include <stdio. h> # include <stdlib. h> # include <string. h>
# Include <sys/socket. h> # include <netinet/in. h> # include <arpa/inet. h> int main ()
{Char buffer [32]; structin_addr in; in. s_addr = 0;/* enter a string-type ip Address */printf ("pleaseinput the ip addrss \ n"); scanf ("% s", buffer ); buffer [31] = '\ 0'; printf ("original IPaddress: % s \ n", buffer); if (in. s_addr = inet_addr (buffer) = INADDR_NONE) {perror ("inet_addr"); exit (1) ;}else {printf ("after transfer: 0x % 0x \ n ", in. s_addr);} Summary: 1. the inet_addr and inet_network functions are used to convert the string form to the integer form. 2. inet_addr returns an integer in the byte order of the network, while inet_network returns Is in the host byte order.
3. inet_addr and inet_network have a small defect, that is, when the IP address is 255.255.255.255.255, these two functions will think that this is an invalid IP address, which is a historical problem. In fact, most of the current routers, the 255.255.255.255 IP address is valid.
4. inet_aton considers 255.255.255.255 to be valid. Therefore, we recommend that you use inet_aton. And the inet_aton function returns the IP address of the network in the byte sequence.
II. A string-type IP address. char * inet_ntoa (struct in_addr in); 1> function: convert a binary IP address in the bytes of the network whose value is in to a string IP address.
2> parameter: struct in_addr in: pointing to the binary IP address 3> function return value: pointer to the string returned after successful execution. If the parameter is invalid, NULL is returned. 4> function instance: # include <stdio. h> # include <stdlib. h> # include <string. h>
# Include <sys/socket. h> # include <netinet/in. h> # include <arpa/inet. h> int main ()
{Char buffer [32]; char * str; structin_addr in; in. s_addr = 0;/* enter a string-type ip Address */printf ("pleaseinput the ip addrss \ n"); scanf ("% s", buffer ); buffer [31] = '\ 0'; printf ("original IP Address: % s \ n", buffer); if (0 = inet_aton (buffer, & in )) {perror ("inet_aton"); exit (1) ;}else {printf ("the first transfer: 0x % 0x \ n", in. s_addr);} printf ("begin two process: \ n"); if (str = inet_ntoa (in) = NULL) {printf ("inet_ntoa: argumentinvalid \ n ");} else {printf (" thesecond transfer: % s \ n ", str);} Description: 1. the Return Value of the inet_ntoa () function is a pointer to a string. This memory function inet_ntoa () will be overwritten every time it is called. Therefore, the function is insecure and may have some potential risks. 2. instance: # include <stdio. h> # include <string. h> # include <stdlib. h>
# Include <sys/socket. h> # include <netinet/in. h> # include <arpa/inet. h> int main ()
{Structin_addr ip1, ip2; char * str1; char * str2; ip1.s _ addr = 192 <24 | 168 <16 | 1 <8 | 1; ip2.s _ addr = 255 <24 | 255 <16 | 255 <8 | 255; str1 = inet_ntoa (ip1); str2 = inet_ntoa (ip2); printf ("ip1: 0x % xà % s \ n ", ip1.s _ addr, str1); printf (" ip2: 0x % xà % s \ n ", ip2.s _ addr, str2 );} the output result is ip1: 0xc0a80101 à 255. 255.255.255; ip2: 0xffffffffà 255. 255.255.255; indicates that the function inet_ntoa cannot be reentrant during the conversion from a binary IP address to a string IP address. This function converts two different IP addresses to get the same result. After a function is called, The result needs to be retrieved immediately. The same function cannot be called before the result is retrieved.
3. The newest address conversion functions inet_ton () and inet_ntop () can process ipv4 and ipv6. they are relatively new functions. The following is a prototype of the inet_pton function: ["Point-to-decimal"-> "integer"]. These two functions are a set of secure protocol-independent address conversion functions. The so-called security means that these two functions can be reentrant, and these functions support multiple address types, including IPv4 and IPv6. 1. inet_ton () function 1> function inet_ton () function converts an IP address of the string type to a binary IP address.
2> function prototype # include <sys/types. h> # include <sys/socket. h> # include <arpa/inet. h> intinet_ton (int af, const char * src, void * dst); 3> function parameters :? Int af: af indicates the protocol family of the network type. The value in IPv4 is AF_INET ;? Src: stores the string to be converted? Dst: stores the converted result. In IPv4, dst points to the pointer of the structure struct in_addr.
4> function return value when the return value of the function inet_ton () is-1, it is usually caused by the protocol family not supported by af. In this case, the returned value of errno is EAFNOSUPPORT; if the return value of the function is 0, it indicates that the value pointed to by src is not a valid IP address. If the return value of the function is positive, the conversion is successful.
2. inet_ntop () function 1> function inet_ton () function converts a binary network IP address to a string.
2> function prototype: # include <sys/types. h> # include <sys/socket. h> # include <arpa/inet. h> intinet_nton (int af, const void * src, char * dst, socklen_t cnt); 3> function parameters :? Int af: af indicates the protocol family of the network type. The value in IPv4 is AF_INET ;? Src: the binary IP address to be converted. In IPv4, src points to a structin_addr structure pointer .? What is the pointer to the result buffer when dst points? The value of cnt is the size of the dst buffer 4> the return value of the function Inet_ntop () function returns a pointer to dst. If an error occurs, NULL is returned. when the Protocol family set by af is not supported, errno is set to EAFNOSUPPORT; when the dst buffer size is too small, errno is set to ENOSPC. 3. function instance: # include <stdio. h> # include <string. h> # include <stdlib. h>
# Include <sys/socket. h> # include <sys/types. h> # include <arpa/inet. h> int main (int argc, char * argv [])
{Struct in_addr ip; char ipstr [] = "192.168.1.1"; char addr [ADDRLEN]; const char * str = NULL; int err = 0; if (err> 0) {printf ("inet_ton: ip % s value is: 0x % x \ n", ipstr, ip. s_addr);} // converts 192.168.12.255 to a network byte ip address. s_addr = htonl (192 <24 | 168 <16 | 12 <8 | 255); str = (const char *) inet_ntop (AF_INET, (void *) & ip, (char *) & addr [0], ADDRLEN); if (str) {printf ("inet_ntop: ip 0x % x is % s \ n", ip. s_addr, str);} 4. Inet_makeaddr () function, inet_lnaof () function and inet_netof () function 1. struct in_addr inet_makeaddr (int net, int host)
1> function: the IP address of a host is divided into the network address and host address, inet_makeaddr () the function combines the network address net and host address host in the byte sequence of the host into an IP address in the network.
2> function parameters :? Int net: stores network number parameters (in binary format, the host's byte order )? Int host: host number address (in binary format, host byte)
3> function return value: return an IP address in the network byte order 4> function instance: unsigned long net, host; net = 0x0000007F; host = 0x00000001; struct in_addr ip = inet_makeaddr (net, hst); 2. in_addr_t inet_lnaof (struct in_addr in)
1> function: This function extracts the host address from the parameter in. After successful execution, the host address in the host byte sequence is returned.
For example, if 172.17.242.131 belongs to Class B address, the host number is 16 bits, the host address is 0.0.242.131, and the output in bytes of the host is 0xf283. 2> function parameter? Struct in_addr in: in stores IP addresses in the binary format of the host in bytes. 3> function return value: returns the value of the IP host in the binary format of the host in bytes. 4> function instance: const char * addr = "127.0.0.1"; unsigned long ip = inet_network (addr); unsigned long host_id = inet_lnaof (ip); 3. in_addr_t inet_netof (struct in_addr in)
1> function: This function extracts the network address from the parameter in and returns the network address in the byte sequence of the host after successful execution.
For example, if 172.17.242.131 belongs to Class B address, the high 16 bits indicate the network number, and the network address is 172.17.0.0. 2> function parameter? Struct in_addr in: in stores IP addresses in the binary format of the host in bytes. 3> function return value: returns the value of the IP address in the binary format of the host in bytes. 4> function instance: const char * addr = "127.0.0.1"; unsigned long ip = inet_network (addr); unsigned long network_id = inet_netof (ip );

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.