tcp/ip协议速查

date: 2013.05.23

目录:

1 总体

1.1 名词解释

1.2 分层

<tr>
    <th>层次</th>  <th>具体协议</th> <th>功能</th>
</tr>
<tr>
    <td>应用层</td>  <td>Telnet, FTP, Email</td> <td>处理应用程序细节</td>
</tr>
<tr>
    <td>运输层</td>  <td>TCP, UDP</td> <td>为主机上的应用程序提供端到端的通信</td>
</tr>
<tr>
    <td>网络层 / 互联网层</td>  <td>IP, ICMP, IGMP</td> <td>处理分组在网络中的活动</td>
</tr>
<tr>
    <td>(数据)链路层 / 网络接口层</td>  <td>Ether</td> <td>驱动, 网卡硬件</td>
</tr>

层次图:

   +---------+  +---------+  +---------+  +---------+
   |   user  |  |   user  |  |   user  |  |   user  |     应用层
   | process |  | process |  | process |  | process |
   +---------+  +---------+  +---------+  +---------+
        \              \         |          /
         \              \        |         /
          \              \       |        /
           \            +-----+  |   +-----+
            \           | TCP |  |   | UDP |              运输层
             \          +-----+  |   +-----+
              \             \    |     /
               \             \   |    /
                \             \  |   /
             +------+         +------+          +------+
             | ICMP | ------- |  IP  | -------- | IGMP |  网络层
             +------+         +------+          +------+
                                 |
                                 |
                                 |
             +------+       +----------+        +------+
             | ARP  | ----- |  Driver  | ------ | RARP |  链路层
             +------+       | Hardware |        +------+
                            +----------+

1.3 IP地址分类

   +---+-------------+---------------------------+
A  | 0 | Network No. |          Host No.         |    0.0.0.0 ~ 127.255.255.255
   +---+-------------+---------------------------+
           7 Bits               24 Bits

   +---+---+-------------+-----------------------+
B  | 1 | 0 | Network No. |         Host No.      |  128.0.0.0 ~ 191.255.255.255
   +---+---+-------------+-----------------------+
              14 Bits              16 Bits

   +---+---+---+---------------------+-----------+
B  | 1 | 1 | 0 |     Network No.     |  Host No. |  192.0.0.0 ~ 223.255.255.255
   +---+---+---+---------------------+-----------+
                      21 Bits           8 Bits

   +---+---+---+---+-----------------------------+
D  | 1 | 1 | 1 | 0 |     Multicast Group No.     |  224.0.0.0 ~ 239.255.255.255
   +---+---+---+---+-----------------------------+
                              28 Bits

   +---+---+---+---+---+-------------------------+
E  | 1 | 1 | 1 | 1 | 0 |        Reserved         |  240.0.0.0 ~ 247.255.255.255
   +---+---+---+---+---+-------------------------+
                                27 Bits

1.4 各层封装

                                      +------+
                                      | User |
                                      | Data |
                                      +------+
                                          | App
                                          v
                             +--------+------+
                             |  App   | User |
                             | Header | Data |
                             +--------+------+
                                          | TCP
                                          v
                    +--------+---------------+
                    |  TCP   |      App      |  TCP section
                    | Header |      Data     |
                    +--------+---------------+
                                          | IP
                                          v
           +--------+--------+---------------+
           |   IP   |  TCP   |      App      |  IP datagram
           | Header | Header |      Data     |
           +--------+--------+---------------+
                                          | Ethernet Driver
                                          v
+----------+--------+--------+---------------+----------+
| Ethernet |   IP   |  TCP   |      App      | Ethernet |  Ethernet Frame
|  Header  | Header | Header |      Data     | Footer   |
+----------+--------+--------+---------------+----------+
|<-- 14 -->|<- 20 ->|<- 20 ->|               |<--- 4 -->|
           |<-------- 46 ~ 1500 Bytes ------>|

1.5 特殊IP

<th>特殊地址</th> <th>NETID</th> <th>HOSTID</th> <th>源地址或目的地址</th> <th>示例说明</th>
<td>网络地址</td> <td>特定的</td> <td>全0</td> <td>都不是</td> <td>不分配给任何主机, 仅用于表示某个网络的网络地址; 例: 202.114.206.0</td>
<td>直接广播地址</td> <td>特定的</td> <td>全1</td> <td>目的地址</td> <td>不分配给任何主机, 用作广播地址, 对应分组传递给该网络中的所有结点(能否执行广播, 则依赖于支撑的物理网络是否具有广播的功能); 例: 202.114.206.255</td>
<td>受限广播地址</td> <td>全1</td> <td>全1</td> <td>目的地址</td> <td>称为有限广播地址, 通常由无盘工作站启动时使用, 希望从网络IP地址服务器处获得一个IP地址; 例: 255.255.255.255</td>
<td>本网络的本主机</td> <td>全0</td> <td>全0</td> <td>源地址</td> <td>表示本身本机地址, 仅在系统启动时允许使用, 并且永远不是一个有效的目的地址; 例: 0.0.0.0</td>
<td>本网络的特定主机</td> <td>全0</td> <td>特定的</td> <td>目的地址</td> <td>本网络的特定主机; 例: 0.0.0.126</td>
<td>回送地址</td> <td>127</td> <td>任意</td> <td>目的地址</td> <td>常用于本机上软件测试和本机上网络应用程序之间的通信地址; 例: 127.0.0.1 localhost</td>

1.6 专用IP

RFC1918定义的专用IP地址:

2 链路层

2.1 以太网数据帧

帧格式:

+---------+---------+------+---------------------+-----+
| dst-MAC | src-MAC | type |         data        | CRC |
+---------+---------+------+---------------------+-----+
     6         6       2            46~1500         4 
以太帧:
                    +------+---------------------+
                    | type |     IP datagram     |
                    | 0800 |                     |
                    +------+---------------------+
                       2            46~1500    
ARP帧:
                    +------+---------------+-----+
                    | type |  ARP request/ | PAD |
                    | 0806 |  ARP answer   |     |
                    +------+---------------+-----+
                       2         28         18
RARP帧:
                    +------+---------------+-----+
                    | type | RARP request/ | PAD |
                    | 0835 | RARP answer   |     |
                    +------+---------------+-----+
                       2          28         18

说明: * 以太网的MTU(最大传输单元,出去以太帧头和帧尾)为1500. * 如果IP层的数据报大于链路层MTU则需要分片(fragmentation).

具体参考: rfc894

3 IP层

3.1 IP数据报首部(Datagram Header)

 0                   1                   2                   3   
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version|  IHL  |Type of Service|          Total Length         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Identification        |Flags|      Fragment Offset    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Time to Live |    Protocol   |         Header Checksum       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Source Address                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Destination Address                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                 Example Internet Datagram Header

具体参考: rfc791

4 考考资料

http://course.cug.edu.cn/cugfourth/jsjyyjch/book2/Chapter02/2.3.3.htm