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
程序代码:
- #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[] = "192.168.0.74";
- long r1, r2, r3; //long
- struct in_addr addr;
- r1 = inet_addr(ip); //返回网络字节序
- if(-1 == r1){
- printf("inet_addr return -1/n");
- }else{
- printf("inet_addr ip: %ld/n", r1);
- }
- r2 = inet_network(ip); //返回主机字节序
- if(-1 == r2){
- printf("inet_addr return -1/n");
- }else{
- printf("inet_network ip: %ld/n", r2);
- printf("inet_network ip: %ld/n", ntohl(r2)); //ntohl: 主机字节序 ——> 网络字节序
- }
- r3 = inet_aton(ip, &addr); //返回网络字节序
- if(0 == r3){
- printf("inet_aton return -1/n");
- }else{
- printf("inet_aton ip: %ld/n", addr.s_addr);
- }
- /***** 批量注释的一种方法 *****/
- #if 0
- r3 = inet_aton(ip, addr);
- if(0 == r3){
- printf("inet_aton return -1/n");
- }else{
- printf("inet_aton ip: %ld/n", ntohl(addr.s_addr));
- }
- #endif
- return 0;
- }
运行结果:
[work@db-testing-com06-vm3.db01.baidu.com net]$ gcc -W -o inet_addr inet_addr.c
[work@db-testing-com06-vm3.db01.baidu.com net]$ ./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
程序代码:
- #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[] = "192.168.0.74";
- char ip2[] = "211.100.21.179";
- struct in_addr addr1, addr2;
- long l1, l2;
- l1 = inet_addr(ip1); //IP字符串——》网络字节
- l2 = inet_addr(ip2);
- printf("IP1: %s/nIP2: %s/n", ip1, ip2);
- printf("Addr1: %ld/nAddr2: %ld/n", l1, l2);
- memcpy(&addr1, &l1, 4); //复制4个字节大小
- memcpy(&addr2, &l2, 4);
- printf("%s <--> %s/n", inet_ntoa(addr1), inet_ntoa(addr2)); //注意:printf函数自右向左求值、覆盖
- printf("%s/n", inet_ntoa(addr1)); //网络字节 ——》IP字符串
- printf("%s/n", inet_ntoa(addr2));
- return 0;
- }
运行结果:
[work@db-testing-com06-vm3.db01.baidu.com net]$ gcc -W -o inet_ntoa inet_ntoa.c
[work@db-testing-com06-vm3.db01.baidu.com net]$ ./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
程序代码:
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- int main()
- {
- char ip[] = "192.168.0.74";
- struct in_addr addr;
- int ret = inet_pton(AF_INET, ip, (void *)&addr); //IP字符串 ——》网络字节流
- if(0 == ret){
- printf("inet_pton error, return 0/n");
- return -1;
- }else{
- printf("inet_pton ip: %ld/n", addr.s_addr);
- printf("inet_pton ip: 0x%x/n", addr.s_addr);
- }
- const char *pstr = inet_ntop(AF_INET, (void *)&addr, ip, 128); //网络字节流 ——》IP字符串
- if(NULL == pstr){
- printf("inet_ntop error, return NULL/n");
- return -1;
- }else{
- printf("inet_ntop ip: %s/n", ip);
- }
- return 0;
- }
运行结果:
[work@db-testing-com06-vm3.db01.baidu.com net]$ gcc -W -o inet_ptoa inet_ptoa.c
[work@db-testing-com06-vm3.db01.baidu.com net]$ ./inet_ptoa
inet_pton ip: 1241557184
inet_pton ip: 0x4a00a8c0
inet_ntop ip: 192.168.0.74
--------------------------------------------------------------------------
IPv6 字符串 《——》网络字节流
inet_pton、inet_ntop
程序代码:
- #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[INET6_ADDRSTRLEN];
- if(argc != 3){
- fprintf(stderr, "usage: %s {i4|i6|<num>} string/n", argv[0]);
- exit(EXIT_FAILURE);
- }
- domain = (strcmp(argv[1], "i4") == 0) ? AF_INET:(strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);
- //IP字符串 ——》网络字节流
- s = inet_pton(domain, argv[2], buf);
- if(s<=0){
- if(0 == s)
- fprintf(stderr, "Not in presentation format/n");
- else
- perror("inet_pton");
- exit(EXIT_FAILURE);
- }
- //网络字节流 ——》IP字符串
- if(inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL){
- perror("inet ntop/n");
- exit(EXIT_FAILURE);
- }
- printf("%s/n", str);
- exit(EXIT_SUCCESS);
- }
运行结果:
[work@db-testing-com06-vm3.db01.baidu.com net]$ gcc -W -o inet_ptoa6 inet_ptoa6.c
[work@db-testing-com06-vm3.db01.baidu.com net]$ ./inet_ptoa6 i6 0:0:0:0:0:FFFF:204.152.189.116
::ffff:204.152.189.116
[work@db-testing-com06-vm3.db01.baidu.com net]$ ./inet_ptoa6 i4 204.152.189.116
204.152.189.116