Linux Network Programming--string IP and binary IP conversion

Source: Internet
Author: User

One: unsafe (non-reentrant) inet_xxx () function family

Since computer-understood IP is stored in binary form, the conversion of string IP and binary IP is often required in network programming, and the Linux system has a set of functions to be used for network address translation, as follows:

       #include <sys/socket.h>       #include <netinet/in.h>       #include <arpa/inet.h>       intInet_aton (Const Char*CP,structIn_addr *INP);//Convert the IP address of the point 4-segment to the structure IN_ADDR valuein_addr_t inet_addr (Const Char*CP);//Convert string to struct IN_ADDR valuein_addr_t Inet_network (Const Char*CP);//Convert the network part of the string address to the structure in_addr value       Char*inet_ntoa (structIn_addrinch);//Convert structure in_addr to string       structIN_ADDR inet_makeaddr (intNetintHost);//The network address and host address are synthesized as IP addresses, and the return value is in_addr valuein_addr_t inet_lnaof (structIn_addrinch);//Get the host part of the addressin_addr_t Inet_netof (structIn_addrinch);//Get the network portion of the address

Some of the above functions are defective, for example:
The Inet_ntoa function return value is a pointer to a string, this memory will be overwritten every time the Inet_nota function is called, if you do not take the data in time, there will be unexpected errors, so the function is unsafe, there is a hidden danger;
The return value of the Inet_addr,inet_network function is 1 when it represents an error, occupies the value of address 255.255.255.255, there is a flaw, buried the hidden trouble.
Here's a piece of code that shows you how to use and what's hidden:

#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <string.h>intMainintARGC, Char*ARGV[]) {struct IN_ADDR IP,Local, network; Char addr1[]="192.168.1.1";/ * Network Address String * /Char addr2[]="255.255.255.255"; Char addr3[]="192.16.1"; Char addr[ -]; Char*str=null,*STR2=null;intErr =0;/ * Test function Inet_aton * /Err = Inet_aton (ADDR1, &AMP;IP);if(ERR) {printf("inet_aton:string %s value is:0x%x\ n", ADDR1, IP.S_ADDR); }Else{printf("inet_aton:string %s error\n", ADDR1); }/* inet_addr, test first192.168.1.1, in the test255.255.255.255 */IP.S_ADDR = inet_addr (ADDR1);if(Err! =-1){printf("inet_addr:string %s value is:0x%x\ n", ADDR1, IP.S_ADDR); }Else{printf("inet_addr:string %s error\n", ADDR1);    }; IP.S_ADDR = inet_addr (ADDR2);if(Ip.s_addr! =-1){printf("inet_addr:string %s value is:0x%x\ n", ADDR2, IP.S_ADDR); }Else{printf("inet_addr:string %s error\n", ADDR2); };/ * Inet_ntoa, test 192.168.1.1 First, test 255.255.255.255 * Proof function non-reentrant * /IP.S_ADDR =192<< -|168<< -|1<<8|1;    str = INET_NTOA (IP); IP.S_ADDR =255<< -|255<< -|255<<8|255; STR2 = Inet_ntoa (IP);printf("inet_ntoa:ip:0x%x string1 %s, Pre is:%s \ n", IP.S_ADDR,STR2,STR);/ * Test function inet_addr * /IP.S_ADDR = inet_addr (ADDR3);if(Err! =-1){printf("inet_addr:string %s value is:0x%x\ n", ADDR3, IP.S_ADDR); }Else{printf("inet_addr:string %s error\n", ADDR3);    }; str = INET_NTOA (IP);printf("inet_ntoa:string %s ip:0x%x \ n", STR,IP.S_ADDR);/ * Test function inet_lnaof, get native address * /Inet_aton (ADDR1, &AMP;IP);Local. s_addr = htonl (IP.S_ADDR);Local. s_addr = inet_lnaof (IP); str = INET_NTOA (Local);printf("inet_lnaof:string %s ip:0x%x \ n"StrLocal. s_addr);/ * Test function Inet_netof, get native address * /NETWORK.S_ADDR = Inet_netof (IP);printf("inet_netof:value:0x%x \ n", NETWORK.S_ADDR);return 0; }

Operation Result:

two. Secure Address conversion function Inet_pton (), Inet_ntop ()

The function Inet_pton (), Inet_ntop (), is reentrant and supports multiple address types, including IPV4 and IPV6.
Function Description:


Here's a piece of code to explain how to use it:

#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>#include <stdio.h>#include <string.h>#define AddrlenintMainintargcChar*argv[]) {structIN_ADDR IP;Charipstr[]="192.168.1.1";/ * Network Address String * /    CharAddr[addrlen];/ * Save converted string IP address, 16 byte size * /    Const Char*str=null;intErr =0;/ * return value * /    / * Test function Inet_pton convert 192.168.1.1 to binary form * /Err = Inet_pton (Af_inet, Ipstr, &AMP;IP);/ * Convert string to binary * /    if(Err >0){printf("inet_pton:ip,%s value is:0x%x\n", IPSTR,IP.S_ADDR); }/ * Test function Inet_ntop convert 192.168.1.1 to String * /IP.S_ADDR = htonl (192<< -|168<< -| A<<8|255);/*192.168.12.255*/    / * Convert binary network byte order 192.168.12.255 to String * /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); }return 0; }

Operation Result:
inet_pton:ip,192.168.1.1 value IS:0X101A8C0
INET_NTOP:IP,0XFF0CA8C0 is 192.168.12.255

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux Network Programming--string IP and binary IP conversion

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.