|
![]() | 作者: tuzi [tuzi]
![]() |
登录 |
微软的XP下是可以这样操作的: Microsoft Windows XP [版本 5.1.2600] © 版权所有 1985-2001 Microsoft Corp. C:\WINDOWS>ping 3330661145 Pinging 198.133.219.25 with 32 bytes of data: Reply from 198.133.219.25: bytes=32 time=863ms TTL=112 Reply from 198.133.219.25: bytes=32 time=928ms TTL=112 Reply from 198.133.219.25: bytes=32 time=898ms TTL=112 Reply from 198.133.219.25: bytes=32 time=858ms TTL=112 Ping statistics for 198.133.219.25: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 858ms, Maximum = 928ms, Average = 886ms C:\WINDOWS>tracert -d 3330661145 Tracing route to 198.133.219.25 over a maximum of 30 hops 1 3 ms 3 ms 3 ms xxx.xxx.xxx.xxx 2 3 ms 3 ms 9 ms xxx.xxx.xxx.xxx 3 37 ms 31 ms 31 ms xxx.xxx.xxx.xxx ... 3330661145=198*256*256*256+133*256*256+219*256+25 ------------------- 由此可见MS的内核是十进制的。 经过测试,2K也是可以这样操作的,HOHO! Microsoft Windows 2000 [Version 5.00.2195] © 版权所有 1985-2000 Microsoft Corp. C:\Documents and Settings\Administrator>ping 3330661145 Pinging 198.133.219.25 with 32 bytes of data: Request timed out. Request timed out. Request timed out. Request timed out. Ping statistics for 198.133.219.25: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms The IP address 203.154.87.15 is the 32-bit integer: (hex) CB9A570F. How would this be stored in computer memory (or a CPU register)? Representation byte 0 byte 1 byte 2 byte 3 x86 byte order: 0F 57 9A CB SPARC byte order: CB 9A 57 0F Network byte order: CB 9A 57 0F 在网络上传输的时候,IP地址采用Network byte order编码,在主机上的内存中,IP地址是按照Host byte order来存储的,Network byte order是一致的,Host byte order是根据机器的CPU类型来决定的(主要是高低位的问题) 造成偶最初的错误认识的原因是偶以前在写代码的时候不太规范,经常用gethostbyname()来得到network byte order的IP地址,所以才认为类似3415889679等东东是不可能ping得通的。 其实正规的代码是应该先用inet_addr函数来判断一个字符串是否是规范的IP地址,如果不是再使用gethostbyname来获取IP。 in_addr_t inet_addr(const char *cp); 输入参数是一个字符串,如果这个字符串是规范的IP地址,那么将会把这个IP地址转换成network byte order的IP地址并返回,其实类似inet_addr这样的函数,不仅仅可以认识dot分割的IP地址,也可以认识3415889679这样的十进制的IP地址,另外像类似0xCB9A570F的十六进制也是认识的,不过要注意,前面要加上0x的十六进制的前缀,否则就会当作一个hostname来解释了,第4种表示方法就是用八进制来当作参数了,前缀加上个0就OK了。 花了点时间写了段代码,也许更能说明这个问题: CODE #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> int main(int argc, char *argv[]){ char *hostname = "127.0.0.1"; char *dot_ipaddress; struct hostent *hostinfo; unsigned long host_ip, net_ip; if(argc != 2){ fprintf(stderr, "please input a hostname or ipaddress\n"); exit(1); } hostname = argv[1]; struct in_addr sin_addr; sin_addr.s_addr = inet_addr(hostname); if(sin_addr.s_addr != (unsigned)-1) { printf("input \"%s\" is a vaild ip address\n", hostname); } else { printf("input \"%s\" is not a vaild ip address, resolve it!\n", hostname); hostinfo = gethostbyname(hostname); if (hostinfo) { sin_addr = *(struct in_addr *) hostinfo->h_addr; } else { fprintf(stderr, "%s: unknown host %s\n", argv[0], hostname); exit(1); } } dot_ipaddress = inet_ntoa(sin_addr); printf("dot ip address: %s\n", dot_ipaddress); net_ip = sin_addr.s_addr; //network byte order printf("ip address(network byte order): %u\n", net_ip); printf("ip address(network byte order)(hex): 0x%X\n", net_ip); host_ip = ntohl(net_ip); //network unsigned 32-bit integer to host byte order. printf("ip address(host byte order): %u\n", host_ip); printf("ip address(host byte order)(oct): 0%o\n", host_ip); printf("ip address(host byte order)(hex): 0x%X\n", host_ip); return 0; } 看看测试结果: # addr 203.154.87.15 input "203.154.87.15" is a vaild ip address dot ip address: 203.154.87.15 ip address(network byte order): 257399499 ip address(network byte order)(hex): 0xF579ACB ip address(host byte order): 3415889679 ip address(host byte order)(oct): 031346453417 ip address(host byte order)(hex): 0xCB9A570F # addr 3415889679 input "3415889679" is a vaild ip address dot ip address: 203.154.87.15 ip address(network byte order): 257399499 ip address(network byte order)(hex): 0xF579ACB ip address(host byte order): 3415889679 ip address(host byte order)(oct): 031346453417 ip address(host byte order)(hex): 0xCB9A570F # addr 031346453417 input "031346453417" is a vaild ip address dot ip address: 203.154.87.15 ip address(network byte order): 257399499 ip address(network byte order)(hex): 0xF579ACB ip address(host byte order): 3415889679 ip address(host byte order)(oct): 031346453417 ip address(host byte order)(hex): 0xCB9A570F # addr 0xCB9A570F input "0xCB9A570F" is a vaild ip address dot ip address: 203.154.87.15 ip address(network byte order): 257399499 ip address(network byte order)(hex): 0xF579ACB ip address(host byte order): 3415889679 ip address(host byte order)(oct): 031346453417 ip address(host byte order)(hex): 0xCB9A570F 所以偶们通常认识的203.154.87.15这个IP,其实有4中写法: 203.154.87.15 3415889679 031346453417 0xCB9A570F 注意,这个是在偶们通常用的x86芯片的结果,在某些CPU中,host byte order是和x86不一样的 签名 ------------------------------- C:\Documents and Settings\root>ping 0xCB9A570F Pinging 203.154.87.15 with 32 bytes of data: Request timed out. Request timed out. MS可以ping 或trace 203.154.87.15 或 3415889679(十进制)、031346453417(八进制)、0xCB9A570F(十六进制)是真的。 Cisco 只支持203.154.87.15格式。 以上表现在RFC 1166中没有说明 |
地主 发表时间: 05-04-26 00:24 |
![]() | 回复: wxngzybb [wxngzybb] ![]() |
登录 |
何以见得"由此可见MS的内核是十进制的",我不懂你的意思?内核是十进制的,我第一次听说 win下的ping和socket API还有DNS解析都可以用八进制,十六进制,可是你说的那个网络字节序的ip实际上是无符号长整型 |
B1层 发表时间: 05-04-28 02:51 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号