Beej的网络编程指南–IP地址和数据结构(第三章)
从现在起我们开始讨论跟代码相关的问题。不过,首先我们还是把代码放到一边,先来简单的看看ip地址与端口(port),进而瞧瞧sockets API如何保存或修改ip地址以及其它的相关数据。
3.1 IP地址, 第4版和第6版
很久以前IP协议第四版(IPv4)就已经存在了,它的地址由4个字节(更专业点说应该是4个8位元组)构成,该地址形式为点分的4个0~255的整数,如:192.0.2.111 ,您应该看到过这种ip地址很多次吧!
事实上,在本文写作的现在,几乎网络上的所有站点都在使用IPv4,我们所有上网的人都在不知不觉的使用着它。
事情看起来一切都很好,直到有一天,互联网之父Vint Cerf出来警告说,IPv4很快会被用完!
嗯?IPv4的地址还能够被用完?这怎么可能?不是存在数十亿的IPv4的地址可以使用吗!我们的互联网上真会有多达数十亿的电脑?
是的!
最初,互联网只有很少的电脑,那时人们想十亿是一个很大的数字了,网络上不可能存在那么多的计算机,于是很多大公司就分配了数百万的ip地址以供自己使用(如:Xerox, MIT, Ford, HP, IBM, GE, AT&T, 还有一些稍微小点公司如Apple等等)。
但事实上要不是已经采取过一些权宜之计,我们的IPv4地址早已用光光了。现在我们讨论的是每个人,每台电脑,每个计算器,每个电话,每个计时停车计费器,每条狗都可以拥有一个ip地址。IPv6也因此而诞生了!
那IPv6到底意味着什么呢?意味着我们可以拥有更多的地址;意味着我们可以使用的地址不仅仅只是IPv4的2倍,十亿倍,也不只千万亿倍,而是79 MILLION BILLION TRILLION(2的96次方) 倍。
可能你会说,“是不是真的啊?我可对大数字不怎么信任哦”。IPv4的地址是32位的,而IPv6的地址是128位的,仅仅多了96位,会有那么大的差别吗?但请记住哦,我们这里谈的是指数哦:32位可以表示大约40亿个地址哦(232), 而128位可以表示大约340 万亿 万亿 万亿个地址哦(准确说来是2128个)。就相当于整个宇宙中的每个星球都拥有100万个IPv4网络。
IPv6的地址格式跟IPv4大不相同,IPv4用的是点分的十进制数来表示,而IPv6的地址我们用冒号分割的16进制数来表示,每组两个字节如:2001:0db8:c9d2:aee5:73e3:934a:a5ae:9551.
还有,我们平时使用的IPv6地址一般里面都包含了多个0,您可以把这些0压缩在两个冒号之间,而且每一组中的16进制数如果开头一个字节是0也可以省略不写(注意哦,我们要在不会引入歧义时才能省略),如:
2001:0db8:c9d2:0012:0000:0000:0000:0051
2001:db8:c9d2:12::51
2001:0db8:ab00:0000:0000:0000:0000:0000
2001:db8:ab00::
0000:0000:0000:0000:0000:0000:0000:0001
::1
地址 ::1 是回环地址,就跟IPv4中的127.0.0.1一样。
最后,我们可以使用于IPv4兼容的IPv6地址,如有一个IPv4地址:192.0.2.33,我们如果用兼容格式的IPv6地址格式来表示就是:::ffff:192.0.2.33
其实IPv6的开发者保留了大量的IPv6地址用于特殊用途,但这不影响我们可以自由获取大量的地址,每个男人,女人,小孩,小狗,乃至于银河系中的每个行星上的停车计时器都可以拥有一个IPv6版的地址。
3.1.1. 子网
由于便于组织管理的原因,通常我们的ip地址分成两部分,前面部分用于标识网络,后一部分用于标识主机。举个例子来说,在IPv4中,假如有个地址:192.0.2.12 ,默认情况下,我们可以说前面三个字节的数字代表了网络号,最后一个字节代表主机,或则换一种说法来表示这台主机:位于192.0.2.0上的12号主机。
And now for more outdated information! Ready? In the Ancient Times, there were “classes” of subnets, where the first one, two, or three bytes of the address was the network part. If you were lucky enough to have one byte for the network and three for the host, you could have 24 bits-worth of hosts on your network (24 million or so). That was a “Class A” network. On the opposite end was a “Class C”, with three bytes of network, and one byte of host (256 hosts, minus a couple that were reserved.)
So as you can see, there were just a few Class As, a huge pile of Class Cs, and some Class Bs in the middle.
The network portion of the IP address is described by something called the netmask, which you bitwise-AND with the IP address to get the network number out of it. The netmask usually looks something like 255.255.255.0. (E.g. with that netmask, if your IP is 192.0.2.12, then your network is 192.0.2.12 AND 255.255.255.0 which gives 192.0.2.0.)
Unfortunately, it turned out that this wasn’t fine-grained enough for the eventual needs of the Internet; we were running out of Class C networks quite quickly, and we were most definitely out of Class As, so don’t even bother to ask. To remedy this, The Powers That Be allowed for the netmask to be an arbitrary number of bits, not just 8, 16, or 24. So you might have a netmask of, say 255.255.255.252, which is 30 bits of network, and 2 bits of host allowing for four hosts on the network. (Note that the netmask is ALWAYS a bunch of 1-bits followed by a bunch of 0-bits.)
But it’s a bit unwieldy to use a big string of numbers like 255.192.0.0 as a netmask. First of all, people don’t have an intuitive idea of how many bits that is, and secondly, it’s really not compact. So the New Style came along, and it’s much nicer. You just put a slash after the IP address, and then follow that by the number of network bits in decimal. Like this: 192.0.2.12/30.
Or, for IPv6, something like this: 2001:db8::/32 or 2001:db8:5413:4028::9db9/64.
3.1.2. Port Numbers
If you’ll kindly remember, I presented you earlier with the Layered Network Model which had the Internet Layer (IP) split off from the Host-to-Host Transport Layer (TCP and UDP). Get up to speed on that before the next paragraph.
Turns out that besides an IP address (used by the IP layer), there is another address that is used by TCP (stream sockets) and, coincidentally, by UDP (datagram sockets). It is the port number. It’s a 16-bit number that’s like the local address for the connection.
Think of the IP address as the street address of a hotel, and the port number as the room number. That’s a decent analogy; maybe later I’ll come up with one involving the automobile industry.
Say you want to have a computer that handles incoming mail AND web services—how do you differentiate between the two on a computer with a single IP address?
Well, different services on the Internet have different well-known port numbers. You can see them all in the Big IANA Port List or, if you’re on a Unix box, in your /etc/services file. HTTP (the web) is port 80, telnet is port 23, SMTP is port 25, the game DOOM used port 666, etc. and so on. Ports under 1024 are often considered special, and usually require special OS privileges to use.
And that’s about it!
