tedwhy 发表于 2015-11-24 07:49:27

Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton函数

知识背景:
210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式。英语叫做IPv4 numbers-and-dots notation。
如果把210.25.132.181转换为整数形式,是3524887733,这个就是整数形式的IP地址。英语叫做binary data。(其实binary是二进制的意思)
详细介绍,请参考: 网络字节序与主机字节序的转换


问题所在:
如何在字符串形式的IP和整数形式的IP之间转换呢?


转换函数:
int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);
in_addr_t inet_network(const char *cp);
int inet_pton(int af, const char *src, void *dst);
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);

参考:http://beej.us/guide/bgnet/output/html/multipage/inet_ntopman.html
=============================================================
IPv4:

IP字符串 ——》 网络字节流
inet_addr、inet_network、inet_aton

程序代码:

viewplainprint?
[*]#include <stdio.h>
[*]#include <stdlib.h>
[*]#include <unistd.h>
[*]#include <string.h>
[*]#include <netinet/in.h>
[*]#include <sys/socket.h>
[*]#include <sys/types.h>
[*]
[*]int main()
[*]{
[*]    char ip[] = &quot;192.168.0.74&quot;;
[*]    long r1, r2, r3;//long
[*]    struct in_addr addr;
[*]
[*]    r1 = inet_addr(ip); //返回网络字节序
[*]    if(-1 == r1){
[*]      printf(&quot;inet_addr return -1/n&quot;);
[*]    }else{
[*]      printf(&quot;inet_addr ip: %ld/n&quot;, r1);
[*]    }
[*]      
[*]    r2 = inet_network(ip);    //返回主机字节序
[*]    if(-1 == r2){
[*]      printf(&quot;inet_addr return -1/n&quot;);
[*]    }else{
[*]      printf(&quot;inet_network ip: %ld/n&quot;, r2);   
[*]      printf(&quot;inet_network ip: %ld/n&quot;, ntohl(r2));   //ntohl: 主机字节序 ——> 网络字节序
[*]    }
[*]      
[*]    r3 = inet_aton(ip, &addr);//返回网络字节序
[*]    if(0 == r3){
[*]      printf(&quot;inet_aton return -1/n&quot;);
[*]    }else{
[*]      printf(&quot;inet_aton ip: %ld/n&quot;, addr.s_addr);
[*]    }
[*]
[*]/*****批量注释的一种方法*****/
[*]#if 0   
[*]    r3 = inet_aton(ip, addr);
[*]    if(0 == r3){
[*]      printf(&quot;inet_aton return -1/n&quot;);
[*]    }else{
[*]      printf(&quot;inet_aton ip: %ld/n&quot;, ntohl(addr.s_addr));
[*]    }
[*]#endif
[*]
[*]    return 0;
[*]}
  
运行结果:
$ gcc -W -o inet_addr inet_addr.c
$ ./inet_addr                     
inet_addr ip: 1241557184
inet_network ip: -1062731702
inet_network ip: 1241557184
inet_aton ip: 1241557184

--------------------------------------------------------------------------

IP字符串 《——》 网络字节流
inet_addr、inet_aton、inet_ntoa

程序代码:

viewplainprint?
[*]#include <stdio.h>
[*]#include <sys/socket.h>
[*]#include <netinet/in.h>
[*]#include <arpa/inet.h>
[*]#include <string.h>
[*]int main(int argc, char *argv[])
[*]{
[*]    char ip1[] = &quot;192.168.0.74&quot;;
[*]    char ip2[] = &quot;211.100.21.179&quot;;
[*]    struct in_addr addr1, addr2;
[*]    long l1, l2;
[*]    l1 = inet_addr(ip1);   //IP字符串——》网络字节
[*]    l2 = inet_addr(ip2);
[*]    printf(&quot;IP1: %s/nIP2: %s/n&quot;, ip1, ip2);
[*]    printf(&quot;Addr1: %ld/nAddr2: %ld/n&quot;, l1, l2);
[*]      
[*]    memcpy(&addr1, &l1, 4); //复制4个字节大小
[*]    memcpy(&addr2, &l2, 4);
[*]    printf(&quot;%s <--> %s/n&quot;, inet_ntoa(addr1), inet_ntoa(addr2)); //注意:printf函数自右向左求值、覆盖
[*]    printf(&quot;%s/n&quot;, inet_ntoa(addr1)); //网络字节 ——》IP字符串
[*]    printf(&quot;%s/n&quot;, inet_ntoa(addr2));
[*]    return 0;
[*]}
  
运行结果:

$ gcc -W -o inet_ntoa inet_ntoa.c
$ ./inet_ntoa                                    
IP1: 192.168.0.74
IP2: 211.100.21.179
Addr1: 1241557184
Addr2: 3004523731
192.168.0.74 <--> 192.168.0.74
192.168.0.74
211.100.21.179

=============================================================

IPv6:

IPv4 字符串 《——》网络字节流
inet_pton、inet_ntop

程序代码:


viewplainprint?
[*]#include <stdio.h>
[*]#include <sys/types.h>
[*]#include <sys/socket.h>
[*]#include <arpa/inet.h>
[*]
[*]int main()
[*]{
[*]    char ip[] = &quot;192.168.0.74&quot;;   
[*]    struct in_addr addr;
[*]      
[*]    int ret = inet_pton(AF_INET, ip, (void *)&addr);   //IP字符串 ——》网络字节流
[*]    if(0 == ret){
[*]      printf(&quot;inet_pton error, return 0/n&quot;);
[*]      return -1;
[*]    }else{
[*]      printf(&quot;inet_pton ip: %ld/n&quot;, addr.s_addr);
[*]      printf(&quot;inet_pton ip: 0x%x/n&quot;, addr.s_addr);
[*]    }
[*]
[*]    const char *pstr = inet_ntop(AF_INET, (void *)&addr, ip, 128);//网络字节流 ——》IP字符串
[*]    if(NULL == pstr){
[*]      printf(&quot;inet_ntop error, return NULL/n&quot;);
[*]      return -1;
[*]    }else{
[*]      printf(&quot;inet_ntop ip: %s/n&quot;, ip);
[*]    }
[*]      
[*]    return 0;
[*]}
  
运行结果:
$ gcc -W -o inet_ptoa inet_ptoa.c
$ ./inet_ptoa                     
inet_pton ip: 1241557184
inet_pton ip: 0x4a00a8c0
inet_ntop ip: 192.168.0.74


--------------------------------------------------------------------------
IPv6 字符串 《——》网络字节流
inet_pton、inet_ntop

程序代码:

viewplainprint?
[*]#include <stdio.h>
[*]#include <stdlib.h>
[*]#include <string.h>
[*]#include <arpa/inet.h>
[*]
[*]int   
[*]main(int argc, char **argv)
[*]{
[*]    unsigned char buf[sizeof(struct in6_addr)];
[*]    int domain, s;
[*]    char str;
[*]
[*]    if(argc != 3){
[*]      fprintf(stderr, &quot;usage: %s {i4|i6|<num>} string/n&quot;, argv);
[*]      exit(EXIT_FAILURE);
[*]    }
[*]
[*]    domain = (strcmp(argv, &quot;i4&quot;) == 0) ? AF_INET:(strcmp(argv, &quot;i6&quot;) == 0) ? AF_INET6 : atoi(argv);
[*]      
[*]    //IP字符串 ——》网络字节流
[*]    s = inet_pton(domain, argv, buf);
[*]    if(s<=0){
[*]      if(0 == s)
[*]            fprintf(stderr, &quot;Not in presentation format/n&quot;);
[*]      else
[*]            perror(&quot;inet_pton&quot;);
[*]      exit(EXIT_FAILURE);
[*]    }
[*]
[*]    //网络字节流 ——》IP字符串
[*]    if(inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL){   
[*]      perror(&quot;inet ntop/n&quot;);
[*]      exit(EXIT_FAILURE);
[*]    }
[*]
[*]    printf(&quot;%s/n&quot;, str);
[*]    exit(EXIT_SUCCESS);
[*]}
运行结果:  
$ gcc -W -o inet_ptoa6 inet_ptoa6.c                              
$ ./inet_ptoa6 i6 0:0:0:0:0:FFFF:204.152.189.116   
::ffff:204.152.189.116
$ ./inet_ptoa6 i4 204.152.189.116               
204.152.189.116

参考:http://www.kernel.org/doc/man-pages/online/pages/man3/inet_pton.3.html
页: [1]
查看完整版本: Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton函数