地址转换函数:在ASCII地址字符串(IPv4的点分十进制或IPv6的格式)与网络字节序的二进制值之间的转换
a:ASCILL n:network
//inet_aton/inet_addr/inet_ntoa 适用于IPv4
int inet_aton(const char *cp, struct in_addr *inp);
返回值:成功(cp有效)返回1,否则为0
注意:如果inp为NULL,对cp依然做有效性检查,但不存储结果。
in_addr_t inet_addr(const char *cp);
返回值:成功(cp有效)返回32位二进制IPv4地址,否则返回INADDR_NONE(通常情况下为常值0xffffffff,但在某些编译器中为常值-1)
注意:当解析到的IP地址本来就是255.255.255.255时,与失败返回值冲突。
char *inet_ntoa(struct in_addr in);
返回值:返回指向一个点分十进制字符串的指针
//inet_pton/inet_ntop 适用于IPv4和IPv6
int inet_pton(int af, const char *src, void *dst);
返回值:成功返回1,cp无效或与af不对应返回0,出错(af不支持的地址族)返回-1
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
返回值:成功返回指向一个ASCII地址字符串的指针,出错(af不支持的地址族)返回NULL
-
#include <stdio.h>
-
#include <strings.h>
-
#include <arpa/inet.h>
-
#include <netinet/in.h>
-
-
int main(void)
-
{
-
int ret = 0;
-
struct sockaddr_in addr_in;
-
char *str = NULL;
-
char str_addr[256] = { 0 };
-
char *ip_string = "10.240.197.172";
-
unsigned int ip_int = 0xacc5f00a;
-
-
/* inet_aton */
-
addr_in.sin_addr.s_addr = 0;
-
ret = inet_aton(ip_string, &addr_in.sin_addr);
-
printf("inet_aton 000, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);
-
-
addr_in.sin_addr.s_addr = 0;
-
ret = inet_aton(ip_string, NULL);
-
printf("inet_aton 111, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);
-
-
addr_in.sin_addr.s_addr = 0;
-
ret = inet_aton("aaa", &addr_in.sin_addr);
-
printf("inet_aton 222, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);
-
-
/* inet_addr */
-
addr_in.sin_addr.s_addr = 0;
-
addr_in.sin_addr.s_addr = inet_addr("aaa");
-
printf("inet_addr aaa, addr = %x\n", addr_in.sin_addr.s_addr);
-
-
addr_in.sin_addr.s_addr = 0;
-
addr_in.sin_addr.s_addr = inet_addr(ip_string);
-
printf("inet_addr bbb, addr = %x\n", addr_in.sin_addr.s_addr);
-
-
/* inet_ntoa */
-
addr_in.sin_addr.s_addr = 123;
-
str = inet_ntoa(addr_in.sin_addr);
-
printf("inet_ntoa 333, addr = %s\n", str);
-
-
addr_in.sin_addr.s_addr = ip_int;
-
str = inet_ntoa(addr_in.sin_addr);
-
printf("inet_ntoa 444, addr = %s\n", str);
-
-
/* inet_pton */
-
addr_in.sin_addr.s_addr = 0;
-
ret = inet_pton(AF_INET, ip_string, &addr_in.sin_addr);
-
printf("inet_pton ccc, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);
-
-
addr_in.sin_addr.s_addr = 0;
-
ret = inet_pton(AF_INET, "aaa", &addr_in.sin_addr);
-
printf("inet_pton ddd, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);
-
-
addr_in.sin_addr.s_addr = 0;
-
ret = inet_pton(AF_INET6, ip_string, &addr_in.sin_addr);
-
printf("inet_pton eee, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);
-
-
addr_in.sin_addr.s_addr = 0;
-
ret = inet_pton(AF_LOCAL, ip_string, &addr_in.sin_addr);
-
printf("inet_pton fff, ret = %d, addr = %x\n", ret, addr_in.sin_addr.s_addr);
-
-
/* inet_ntop */
-
addr_in.sin_addr.s_addr = ip_int;
-
str = (char *)inet_ntop(AF_INET, &addr_in.sin_addr, str_addr, INET_ADDRSTRLEN);
-
printf("inet_ntop 555, str = %s, addr = %s\n", str, str_addr);
-
-
bzero(str_addr, 256);
-
str = (char *)inet_ntop(AF_INET6, &addr_in.sin_addr, str_addr, INET6_ADDRSTRLEN);
-
printf("inet_ntop 666, str = %s, addr = %s\n", str, str_addr);
-
-
bzero(str_addr, 256);
-
str = (char *)inet_ntop(AF_LOCAL, &addr_in.sin_addr, str_addr, INET_ADDRSTRLEN);
-
printf("inet_ntop 777, str = %s, addr = %s\n", str, str_addr);
-
-
return;
-
}
--result
inet_aton 000, ret = 1, addr = acc5f00a
inet_aton 111, ret = 1, addr = 0
inet_aton 222, ret = 0, addr = 0
inet_addr aaa, addr = ffffffff
inet_addr bbb, addr = acc5f00a
inet_ntoa 333, addr = 123.0.0.0
inet_ntoa 444, addr = 10.240.197.172
inet_pton ccc, ret = 1, addr = acc5f00a
inet_pton ddd, ret = 0, addr = 0
inet_pton eee, ret = 0, addr = 0
inet_pton fff, ret = -1, addr = 0
inet_ntop 555, str = 10.240.197.172, addr = 10.240.197.172
inet_ntop 666, str = af0:c5ac:850a:4000::a0fb:a02a, addr = af0:c5ac:850a:4000::a0fb:a02a
inet_ntop 777, str = (null), addr =
阅读(1721) | 评论(0) | 转发(0) |