我们正常上网时使用的 IP 地址通常是: 192.168.x.x、10.10.x.x ,这些 IP 地址在局域网内有用,但是在公网上没有用,这时候我们就需要为我们的局域网找一个代理,作为我们上网的公网 IP 地址, NAT 就起到了这样的作用,通过 NAT 技术我们就实现了 私网 IP 和公网通信的功能。
NAT
网络地址转换 NAT:在专用网连接到因特网的路由器上安装 NAT 软件,安装了 NAT 软件的路由器称为 NAT 路由器,它至少有一个有效的全球 IP 地址
NAT 通过重写 Ip Packet
的 Source IP
或者 Destination IP
技术,来解决私有地址和外部通信.
现在我们假设:
Source IP | Destination IP |
---|---|
192.168.2.2 | 9.9.9.9 |
SNAT
SNAT : 源地址转换,即重写的是 Ip Packet
的 Source IP
重写后如下:
Source IP | Destination IP |
---|---|
1.1.1.1 | 9.9.9.9 |
DNAT
DNAT: 目标地址转换,重写的是 Destination IP
重写后如下:
Source IP | Destination IP |
---|---|
192.168.2.2 | 2.2.2.2 |
PAT
PAT: 端口地址转换, 通过重写传输层的 Source Port
或者 Destination Port
的技术
原始数据包:
Source IP | Source Port | Destination IP | Destination Port |
---|---|---|---|
192.168.2.2 | 6666 | 9.9.9.9 | 80 |
<font style="color:rgb(50, 50, 50);">Destination Port</font>
后如下:
Source IP | Source Port | Destination IP | Destination Port |
---|---|---|---|
192.168.2.2 | 6666 | 9.9.9.9 | 8080 |
NAPT
即 NAT 和 PAT 的混合体.
最常见的是重写私有地址端口192.168.2.2:6666为公网的IP和端口1.1.1.1:7777,以此可以实现私有地址访问公网的目的。重写后如下 :
Source IP | Source Port | Destination IP | Destination Port |
---|---|---|---|
1.1.1.1 | 7777 | 9.9.9.9 | 80 |
Source IP | Source Port | Destination IP | Destination Port |
---|---|---|---|
192.168.2.2 | 6666 | 2.2.2.2 | 8080 |