forked from Patchett/Simple-Router
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsr_utils.c
More file actions
Latest commit
224 lines (189 loc) · 6.43 KB
/
Copy pathsr_utils.c
File metadata and controls
224 lines (189 loc) · 6.43 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"sr_protocol.h"
#include"sr_utils.h"
#include"assert.h"
uint16_tcksum(constvoid*_data, intlen)
{
constuint8_t*data=_data;
uint32_tsum;
for (sum=0; len >= 2; data+=2, len-=2) {
sum+=data[0] << 8 | data[1];
}
if (len>0) {
sum+=data[0] << 8;
}
while (sum>0xffff) {
sum= (sum >> 16) + (sum&0xffff);
}
sum=htons(~sum);
returnsum ? sum : 0xffff;
}
uint16_tethertype(uint8_t*buf)
{
sr_ethernet_hdr_t*ehdr= (sr_ethernet_hdr_t*)buf;
returnntohs(ehdr->ether_type);
}
uint8_tip_protocol(uint8_t*buf)
{
sr_ip_hdr_t*iphdr= (sr_ip_hdr_t*)(buf);
returniphdr->ip_p;
}
/* Prints out formatted Ethernet address, e.g. 00:11:22:33:44:55 */
voidprint_addr_eth(uint8_t*addr)
{
intpos=0;
uint8_tcur;
for (; pos<ETHER_ADDR_LEN; pos++) {
cur=addr[pos];
if (pos>0) {
fprintf(stderr, ":");
}
fprintf(stderr, "%02X", cur);
}
fprintf(stderr, "\n");
}
/* Prints out IP address as a string from in_addr */
voidprint_addr_ip(structin_addraddress)
{
charbuf[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, &address, buf, 100) ==NULL) {
fprintf(stderr, "inet_ntop error on address conversion\n");
} else {
fprintf(stderr, "%s\n", buf);
}
}
/* Prints out IP address from integer value */
voidprint_addr_ip_int(uint32_tip)
{
uint32_tcurOctet=ip >> 24;
fprintf(stderr, "%d.", curOctet);
curOctet= (ip << 8) >> 24;
fprintf(stderr, "%d.", curOctet);
curOctet= (ip << 16) >> 24;
fprintf(stderr, "%d.", curOctet);
curOctet= (ip << 24) >> 24;
fprintf(stderr, "%d\n", curOctet);
}
/* Prints out fields in Ethernet header. */
voidprint_hdr_eth(uint8_t*buf)
{
sr_ethernet_hdr_t*ehdr= (sr_ethernet_hdr_t*)buf;
fprintf(stderr, "ETHERNET header:\n");
fprintf(stderr, "\tdestination: ");
print_addr_eth(ehdr->ether_dhost);
fprintf(stderr, "\tsource: ");
print_addr_eth(ehdr->ether_shost);
fprintf(stderr, "\ttype: %d\n", ntohs(ehdr->ether_type));
}
/* Prints out fields in IP header. */
voidprint_hdr_ip(uint8_t*buf)
{
sr_ip_hdr_t*iphdr= (sr_ip_hdr_t*)(buf);
fprintf(stderr, "IP header:\n");
fprintf(stderr, "\tversion: %d\n", iphdr->ip_v);
fprintf(stderr, "\theader length: %d\n", iphdr->ip_hl);
fprintf(stderr, "\ttype of service: %d\n", iphdr->ip_tos);
fprintf(stderr, "\tlength: %d\n", ntohs(iphdr->ip_len));
fprintf(stderr, "\tid: %d\n", ntohs(iphdr->ip_id));
if (ntohs(iphdr->ip_off) &IP_DF) {
fprintf(stderr, "\tfragment flag: DF\n");
} elseif (ntohs(iphdr->ip_off) &IP_MF) {
fprintf(stderr, "\tfragment flag: MF\n");
} elseif (ntohs(iphdr->ip_off) &IP_RF) {
fprintf(stderr, "\tfragment flag: R\n");
}
fprintf(stderr, "\tfragment offset: %d\n", ntohs(iphdr->ip_off) &IP_OFFMASK);
fprintf(stderr, "\tTTL: %d\n", iphdr->ip_ttl);
fprintf(stderr, "\tprotocol: %d\n", iphdr->ip_p);
/*Keep checksum in NBO*/
fprintf(stderr, "\tchecksum: %d\n", iphdr->ip_sum);
fprintf(stderr, "\tsource: ");
print_addr_ip_int(ntohl(iphdr->ip_src));
fprintf(stderr, "\tdestination: ");
print_addr_ip_int(ntohl(iphdr->ip_dst));
}
/* Prints out ICMP header fields */
voidprint_hdr_icmp(uint8_t*buf)
{
sr_icmp_hdr_t*icmp_hdr= (sr_icmp_hdr_t*)(buf);
fprintf(stderr, "ICMP header:\n");
fprintf(stderr, "\ttype: %d\n", icmp_hdr->icmp_type);
fprintf(stderr, "\tcode: %d\n", icmp_hdr->icmp_code);
/* Keep checksum in NBO */
fprintf(stderr, "\tchecksum: %d\n", icmp_hdr->icmp_sum);
}
/* Prints out fields in ARP header */
voidprint_hdr_arp(uint8_t*buf)
{
sr_arp_hdr_t*arp_hdr= (sr_arp_hdr_t*)(buf);
fprintf(stderr, "ARP header\n");
fprintf(stderr, "\thardware type: %d\n", ntohs(arp_hdr->ar_hrd));
fprintf(stderr, "\tprotocol type: %d\n", ntohs(arp_hdr->ar_pro));
fprintf(stderr, "\thardware address length: %d\n", arp_hdr->ar_hln);
fprintf(stderr, "\tprotocol address length: %d\n", arp_hdr->ar_pln);
fprintf(stderr, "\topcode: %d\n", ntohs(arp_hdr->ar_op));
fprintf(stderr, "\tsender hardware address: ");
print_addr_eth(arp_hdr->ar_sha);
fprintf(stderr, "\tsender ip address: ");
print_addr_ip_int(ntohl(arp_hdr->ar_sip));
fprintf(stderr, "\ttarget hardware address: ");
print_addr_eth(arp_hdr->ar_tha);
fprintf(stderr, "\ttarget ip address: ");
print_addr_ip_int(ntohl(arp_hdr->ar_tip));
}
/* Prints out all possible headers, starting from Ethernet */
voidprint_hdrs(uint8_t*buf, uint32_tlength)
{
/* Ethernet */
intminlength=sizeof(sr_ethernet_hdr_t);
if (length<minlength) {
fprintf(stderr, "Failed to print ETHERNET header, insufficient length\n");
return;
}
uint16_tethtype=ethertype(buf);
print_hdr_eth(buf);
if (ethtype==ethertype_ip) { /* IP */
minlength+=sizeof(sr_ip_hdr_t);
if (length<minlength) {
fprintf(stderr, "Failed to print IP header, insufficient length\n");
return;
}
print_hdr_ip(buf+sizeof(sr_ethernet_hdr_t));
uint8_tip_proto=ip_protocol(buf+sizeof(sr_ethernet_hdr_t));
if (ip_proto==ip_protocol_icmp) { /* ICMP */
minlength+=4;
if (length<minlength) {
fprintf(stderr, "Failed to print ICMP header, insufficient length\n");
} else {
print_hdr_icmp(buf+sizeof(sr_ethernet_hdr_t) +sizeof(sr_ip_hdr_t));
}
}
} elseif (ethtype==ethertype_arp) { /* ARP */
minlength+=sizeof(sr_arp_hdr_t);
if (length<minlength) {
fprintf(stderr, "Failed to print ARP header, insufficient length\n");
} else {
print_hdr_arp(buf+sizeof(sr_ethernet_hdr_t));
}
} else {
fprintf(stderr, "Unrecognized Ethernet Type: %d\n", ethtype);
}
}
sr_icmp_hdr_t*extract_icmp_header(uint8_t*packet)
{
return (sr_icmp_hdr_t*)(packet+sizeof(sr_ip_hdr_t) +sizeof(sr_ethernet_hdr_t));
}
sr_ethernet_hdr_t*extract_ethernet_header(uint8_t*packet)
{
return (sr_ethernet_hdr_t*)packet;
}
sr_ip_hdr_t*extract_ip_header(uint8_t*packet)
{
return (sr_ip_hdr_t*)(packet+sizeof(sr_ethernet_hdr_t));
}
sr_arp_hdr_t*extract_arp_header(uint8_t*packet)
{
return (sr_arp_hdr_t*)(packet+sizeof(sr_ethernet_hdr_t));
}