跟我一起学TCP/IP
发表于 : 2023-02-26 10:06
参考书:史蒂文斯先生的名著《TCP/IP Illustrated Volume 1: The Protocols》第一版
欢迎有兴趣的同学一起加入学习讨论
欢迎有兴趣的同学一起加入学习讨论
代码: 全选
$ netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR 囗囗囗
enp0s3 1500 1397582 0 0 0 696458 0 0 0 BMRU
lo 65536 759726 0 0 0 759726 0 0 0 LRU
代码: 全选
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("usage: mtu interface\n");
exit(1);
}
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("socket");
exit(1);
}
struct ifreq req;
bzero(&req, sizeof(req));
strcpy(req.ifr_name, argv[1]);
if (ioctl(sockfd, SIOCGIFMTU, &req) < 0) {
perror("ioctl");
exit(1);
}
printf("%ld\n", (long)req.ifr_mtu);
}
代码: 全选
$ cc mtu.cpp -o mtu
$ ./mtu enp0s3
1500
代码: 全选
int sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
代码: 全选
#include <net/if.h>
unsigned int if_nametoindex(const char *ifname);
代码: 全选
sudo ./linkdump enp0s3 | head -5
broadcast:60:FFFFFFFFFFFF2C6104BAFFFA080600010800060400012C6104BAFFFAC0A80001FFFFFFFFFFFFC0A80001000000000000000000000000000000000000
outgoing:238:909C4AC0BED0080027A2A34A0800451000E04D5E400040066AE5C0A8006EC0A800060016C2A8147D9ED4083A090B801801F5829700000101080A45FE
broadcast:60:FFFFFFFFFFFF2C6104BAFFFA080600010800060400012C6104BAFFFAC0A80001FFFFFFFFFFFFC0A800B3000000000000000000000000000000000000
host:66:080027A2A34A909C4AC0BED0080045480034000040004006B8B7C0A80006C0A8006EC2A80016083A090B147D9F80801007FD870C00000101080A2E1A
outgoing:502:909C4AC0BED0080027A2A34A0800451001E84D5F4000400669DCC0A8006EC0A800060016C2A8147D9F80083A090B801801F5839F00000101080A45FE
代码: 全选
$ netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
$
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
代码: 全选
$ sudo route add -host 192.168.0.139 dev enp0s3
$ sudo route add -host 110.242.68.66 gw 192.168.0.1 dev enp0s3
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 enp0s3
110.242.68.66 192.168.0.1 255.255.255.255 UGH 0 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
192.168.0.139 0.0.0.0 255.255.255.255 UH 0 0 0 enp0s3
代码: 全选
// 配置ip地址和子网掩码
# ifconfig eth0 192.168.0.101 netmask 255.255.0.0
// 查看ip地址、子网掩码以及其他接口相关信息
$ ifconfig
代码: 全选
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
代码: 全选
// 我通过Ubuntu的网络接口配置界面配置子网掩码为255.255.255.128,后查看接口信息
$ ifconfig enp0s3
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.22 netmask 255.255.255.128 broadcast 192.168.0.127
inet6 fe80::8253:abe9:8fab:a110 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:a2:a3:4a txqueuelen 1000 (Ethernet)
RX packets 1318603 bytes 300408996 (300.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1195589 bytes 524945167 (524.9 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
// 查看路由表,发现Genmask一列变化了
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.128 U 100 0 0 enp0s3
代码: 全选
$ arping 192.168.0.5
ARPING 192.168.0.5 from 192.168.0.110 enp0s3
Unicast reply from 192.168.0.5 [90:9C:4A:C0:BE:D0] 0.770ms
Unicast reply from 192.168.0.5 [90:9C:4A:C0:BE:D0] 1.130ms
Unicast reply from 192.168.0.5 [90:9C:4A:C0:BE:D0] 1.227ms
Unicast reply from 192.168.0.5 [90:9C:4A:C0:BE:D0] 1.328ms
代码: 全选
$ sudo ./arpdump | grep '192.168.0.5'
Request: who-has 192.168.0.5 tell 192.168.0.110
Reply: 192.168.0.5 is-at 90:9c:4a:c0:be:d0
Request: who-has 192.168.0.5 tell 192.168.0.110
Reply: 192.168.0.5 is-at 90:9c:4a:c0:be:d0
Request: who-has 192.168.0.5 tell 192.168.0.110
Reply: 192.168.0.5 is-at 90:9c:4a:c0:be:d0
Request: who-has 192.168.0.5 tell 192.168.0.110
Reply: 192.168.0.5 is-at 90:9c:4a:c0:be:d0
代码: 全选
// 查询arp cache
$ arp -n
Address HWtype HWaddress Flags Mask Iface
192.168.0.2 ether a4:45:19:6b:e4:d8 C enp0s3
192.168.0.5 ether 90:9c:4a:c0:be:d0 C enp0s3
192.168.0.1 ether 2c:61:04:ba:ff:fa C enp0s3
// 删除arp条目
$ sudo arp -d 192.168.0.2
$ arp -n
Address HWtype HWaddress Flags Mask Iface
192.168.0.5 ether 90:9c:4a:c0:be:d0 C enp0s3
192.168.0.1 ether 2c:61:04:ba:ff:fa C enp0s3
// 添加arp条目
$ sudo arp -s 192.168.0.2 a4:45:19:6b:e4:d8
$ arp -n
Address HWtype HWaddress Flags Mask Iface
192.168.0.2 ether a4:45:19:6b:e4:d8 CM enp0s3
192.168.0.5 ether 90:9c:4a:c0:be:d0 C enp0s3
192.168.0.1 ether 2c:61:04:ba:ff:fa C enp0s3
代码: 全选
$ sudo arp-scan -I enp0s3 --localnet
Interface: enp0s3, datalink type: EN10MB (Ethernet)
Starting arp-scan 1.9 with 256 hosts (http://www.nta-monitor.com/tools/arp-scan/)
192.168.0.2 90:9c:4a:c0:be:d0 (Unknown)
192.168.0.1 2c:61:04:ba:ff:fa (Unknown)
192.168.0.4 a4:45:19:6b:e4:d8 (Unknown)
192.168.0.110 08:00:27:a2:a3:4a CADMUS COMPUTER SYSTEMS
5 packets received by filter, 0 packets dropped by kernel
Ending arp-scan 1.9: 256 hosts scanned in 2.656 seconds (96.39 hosts/sec). 4 responded
代码: 全选
$ sudo arp-scan -I enp0s3 192.168.0.0/24
Interface: enp0s3, datalink type: EN10MB (Ethernet)
Starting arp-scan 1.9 with 256 hosts (http://www.nta-monitor.com/tools/arp-scan/)
192.168.0.2 90:9c:4a:c0:be:d0 (Unknown)
192.168.0.1 2c:61:04:ba:ff:fa (Unknown)
192.168.0.4 a4:45:19:6b:e4:d8 (Unknown)
192.168.0.110 08:00:27:a2:a3:4a CADMUS COMPUTER SYSTEMS
5 packets received by filter, 0 packets dropped by kernel
Ending arp-scan 1.9: 256 hosts scanned in 3.050 seconds (83.93 hosts/sec). 4 responded
代码: 全选
$ ifconfig enp0s3
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.5 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::6d9c:c498:dc35:4ad5 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:ee:d5:16 txqueuelen 1000 (Ethernet)
RX packets 36883 bytes 36477198 (36.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22955 bytes 2436178 (2.4 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
代码: 全选
$ sudo arptables -t filter -A OUTPUT --source-mac 08:00:27:ee:d5:16 -j DROP
代码: 全选
$ sudo ./icmptime 192.168.0.1
orig = 51748027, recv = 51747219, xmit = 51747219, rtt = 3 ms
difference = -808 ms
代码: 全选
$ sudo ./icmpdump -x
IP 192.168.0.6 > 192.168.0.1: ICMP time stamp query, id 44883, seq 0, length 20
0x0000: 2c61 04ba fffa 0800 27c2 f377 0800 4500
0x0010: 0028 fd2b 4000 4001 bc51 c0a8 0006 c0a8
0x0020: 0001 0d00 a3db af53 0000 0315 9cbb 0000
0x0030: 0000 0000 0000
IP 192.168.0.1 > 192.168.0.6: ICMP time stamp reply, id 44883, seq 0, org 14:22:28.027, recv 14:22:27.219, xmit 14:22:27.219, length 20
0x0000: 0800 27c2 f377 2c61 04ba fffa 0800 4500
0x0010: 0028 f498 0000 4001 04e5 c0a8 0001 c0a8
0x0020: 0006 0e00 698a af53 0000 0315 9cbb 0315
0x0030: 9993 0315 9993 0000 0000 0000
代码: 全选
$ ./udpcli 192.168.0.1 8888
hello // 输入hello,回车发送udp数据报
代码: 全选
$ sudo ./icmpdump -x
IP 192.168.0.1 > 192.168.0.6: ICMP 192.168.0.1 udp port 8888 unreachable, length 41
0x0000: 0800 27c2 f377 2c61 04ba fffa 0800 45c0
0x0010: 003d d53d 0000 4001 236b c0a8 0001 c0a8
0x0020: 0006 0303 7e73 0000 0000 4500 0021 04db
0x0030: 4000 4011 b499 c0a8 0006 c0a8 0001 a819
0x0040: 22b8 000d 6fd8 6865 6c6c 6f84
代码: 全选
./udpcli -c 192.168.0.1 8888
hello
sendto: Connection refused
代码: 全选
$ sudo iptables -A INPUT -p icmp -m icmp --icmp-type echo-request -j DROP
代码: 全选
$ sudo iptables -F
代码: 全选
$ traceroute -n 222.190.59.9
traceroute to 222.190.59.9 (222.190.59.9), 30 hops max, 60 byte packets
1 192.168.0.1 10.235 ms 10.193 ms 10.320 ms
2 100.99.128.1 16.782 ms 16.771 ms 16.761 ms
3 221.226.71.49 15.256 ms 15.233 ms 15.151 ms
4 222.190.59.9 17.345 ms 17.334 ms 17.484 ms
代码: 全选
$ sudo ./icmpdump
IP 192.168.0.1 > 192.168.0.6: ICMP time exceeded in-transit, length 37
IP 100.99.128.1 > 192.168.0.6: ICMP time exceeded in-transit, length 36
IP 218.2.127.213 > 192.168.0.6: ICMP 218.2.127.213 udp port 34567 unreachable, length 36
代码: 全选
$ ./udpclittl
usage: udpclittl host port ttl
$ ./udpclittl 218.2.127.213 34567 1
$ ./udpclittl 218.2.127.213 34567 2
$ ./udpclittl 218.2.127.213 34567 2
$ ./udpclittl 218.2.127.213 34567 3
代码: 全选
netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
代码: 全选
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
代码: 全选
$ netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
$ sudo route del default
$ netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
$ ping 192.168.1.2
connect: Network is unreachable
$ errno --list | grep unreach
ENETUNREACH 101 Network is unreachable
当路由器转发ip数据报时,如果没有找到匹配的route,则会向源主机回送ICMP host unreachable消息Notice that ENETUNREACH is not listed in Figure A.15, even when the error indicates that the destination network is unreachable. Network unreachables are considered obsolete, and applications should just treat ENETUNREACH and EHOSTUNREACH as the same error.
代码: 全选
# echo 1 > /proc/sys/net/ipv4/ip_forward
代码: 全选
$ sudo route add -net 192.168.2.0 netmask 255.255.255.0 metric 1024 reject
$ netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
192.168.2.0 - 255.255.255.0 ! - - - -
代码: 全选
$ sudo ./icmpdump
IP 192.168.0.6 > 192.168.0.3: ICMP host 192.168.2.2 unreachable, length 38
IP 192.168.0.6 > 192.168.0.3: ICMP host 192.168.2.2 unreachable, length 38
代码: 全选
$ nc -u 192.168.2.2 8888
a
$ nc -u 192.168.2.2 8888
a
代码: 全选
$ ping baidu.com
PING baidu.com (110.242.68.66) 56(84) bytes of data.
From _gateway (192.168.0.6): icmp_seq=1 Redirect Host(New nexthop: 192.168.0.1 (192.168.0.1))
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=51 time=27.2 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=50 time=27.4 ms (DUP!)
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=51 time=33.6 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=50 time=33.9 ms (DUP!)
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=3 ttl=51 time=33.5 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=3 ttl=50 time=33.7 ms (DUP!)
^C
--- baidu.com ping statistics ---
3 packets transmitted, 3 received, +3 duplicates, 0% packet loss, time 2144ms
rtt min/avg/max/mdev = 27.220/31.615/33.929/3.023 ms
代码: 全选
$ sudo ./icmpdump
IP 192.168.0.110 > 110.242.68.66: ICMP echo query, id 3855, seq 1, length 64
IP 192.168.0.6 > 192.168.0.110: ICMP redirect 110.242.68.66 to host 192.168.0.1, length 92
IP 192.168.0.110 > 110.242.68.66: ICMP echo query, id 3855, seq 1, length 64
IP 192.168.0.6 > 114.114.114.114: ICMP redirect 192.168.0.110 to host 192.168.0.110, length 155
IP 110.242.68.66 > 192.168.0.110: ICMP echo reply, id 3855, seq 1, length 64
IP 192.168.0.6 > 110.242.68.66: ICMP redirect 192.168.0.110 to host 192.168.0.110, length 92
IP 110.242.68.66 > 192.168.0.110: ICMP echo reply, id 3855, seq 1, length 64
IP 192.168.0.6 > 114.114.114.114: ICMP redirect 192.168.0.110 to host 192.168.0.110, length 169
IP 110.242.68.66 > 192.168.0.110: ICMP echo reply, id 3855, seq 2, length 64
IP 192.168.0.6 > 110.242.68.66: ICMP redirect 192.168.0.110 to host 192.168.0.110, length 92
IP 110.242.68.66 > 192.168.0.110: ICMP echo reply, id 3855, seq 2, length 64
IP 110.242.68.66 > 192.168.0.110: ICMP echo reply, id 3855, seq 3, length 64
IP 192.168.0.6 > 110.242.68.66: ICMP redirect 192.168.0.110 to host 192.168.0.110, length 92
IP 110.242.68.66 > 192.168.0.110: ICMP echo reply, id 3855, seq 3, length 64
代码: 全选
netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.0.6 0.0.0.0 UG 0 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp0s3
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3