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>       int Inet_aton (const char *CP, struct in_addr *inp);//Convert the DOT 4-segment IP address to the structure in_addr value       in_addr_t inet_addr (const char *CP);//convert string to structure in _addr value       in_addr_t inet_network (const char *CP);//Convert the network portion of the string address to the struct in_addr value       char *inet_ntoa (struct in_addr in) ;//Convert structure in_addr to string       struct in_addr inet_makeaddr (int net, int host)//combine network address and host address into IP address, return value is in_addr value       in_addr _t inet_lnaof (struct in_addr in);//Get the host part of the address       in_addr_t inet_netof (struct in_addr in);//Get the address of the network part
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>int main (int argc, 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[16];char *str=null,*str2=null;int err = 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 192.168.1.1 First, test 255.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, first Test 192.168.1.1, test 255.255.255.255* proof function non-reentrant */ip.s_addr = 192<<24|168<<16|1<<8| 1;STR = Inet_ntoa (IP), ip.s_addr = 255&LT;&LT;24|255&LT;&LT;16|255&LT;&LT;8|255;STR2 = Inet_ntoa (IP);p rintf ("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);p rintf ("inet_ntoa:string%s ip:0x%x \ n", str,ip.s_addr);/* Test function inet_lnaof, get the native address */inet_aton (ADDR1 , &ip); local.s_addr = htonl (ip.s_addr); local.s_addr = inet_lnaof (IP); str = inet_ntoa (local);p rintf ("Inet_lnaof: String%s ip:0x%x \ n ", str,local.s_addr);/* Test function Inet_netof, get the native address */network.s_addr = Inet_netof (IP);p rintf (" 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 Addrlen 16int Main (int argc, char *argv[]) {struct in_addr ip;char ipstr[]= "192.168.1.1" ; */* Network address string */char addr[addrlen];/* Save converted string IP address, 16 byte size */const char*str=null;int err = 0;/* return value *//* Test function Inet_pton Conversion 192 .168.1.1 is in binary form */err = Inet_pton (Af_inet, Ipstr, &IP);/* Converts a 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<<24|168<<16|12<<8|255);/* 192.168.12.255*//* converts a binary network byte order 192.168.12.255 to a 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.