最近看了这两本书 《网络是怎样连接的》 和 《图解 TCP_IP》,书本图文结合,通俗易懂。重温和扫盲了不少网络知识点,深感基础实在太重要了!

1. 网络通信流程

图片来源:《网络是怎样连接的》
2. OSI 七层网络协议


图片来源:《图解 TCP_IP》
3. 数据传输封包格式


图片来源:《图解 TCP_IP》

图片来源:《网络是怎样连接的》

图片来源:《网络是怎样连接的》
3.1. TCP
3.1.1. TCP 头部格式

图片来源:《图解 TCP_IP》 – 6.7 TCP 首部格式

图片来源:《网络是怎样连接的》
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* tcp.h */
struct tcphdr {
__be16 source;
__be16 dest;
__be32 seq;
__be32 ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u16 res1:4,
doff:4,
fin:1,
syn:1,
rst:1,
psh:1,
ack:1,
urg:1,
ece:1,
cwr:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
__u16 doff:4,
res1:4,
cwr:1,
ece:1,
urg:1,
ack:1,
psh:1,
rst:1,
syn:1,
fin:1;
#else
#error "Adjust your <asm/byteorder.h> defines"
#endif
__be16 window;
__sum16 check;
__be16 urg_ptr;
};
3.1.2. TCP 数据传输

图片来源:《网络是怎样连接的》
3.1.3. TCP 握手挥手

图片来源:抓包分析 tcp 握手和挥手
3.2. IPv4 头部格式

《图解 TCP_IP》 – 4.7 IPv4 首部

图片来源:《网络是怎样连接的》
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* ip.h */
struct iphdr {
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u8 ihl:4,
version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
__u8 version:4,
ihl:4;
#else
#error "Please fix <asm/byteorder.h>"
#endif
__u8 tos;
__be16 tot_len;
__be16 id;
__be16 frag_off;
__u8 ttl;
__u8 protocol;
__sum16 check;
__be32 saddr;
__be32 daddr;
/*The options start here. */
};
3.3. MAC 头部格式

图片来源:《网络是怎样连接的》
3.4. UDP 头部格式

图片来源:《图解 TCP_IP》 – 6.6 UDP 的首部格式

图片来源:《网络是怎样连接的》
1
2
3
4
5
6
7
/* udp.h */
struct udphdr {
__be16 source;
__be16 dest;
__be16 len;
__sum16 check;
};
5. 参考
- 《网络是怎样连接的》
- 《图解 TCP_IP》
- TCP,UDP,IP包头格式及说明
- icmp的目的主机不可达消息为什么由其他ip发出?
- tcp连接关闭方式:close,shutdown和RST包