-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_utils.c
executable file
·192 lines (162 loc) · 5.86 KB
/
sr_utils.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "sr_protocol.h"
#include "sr_utils.h"
uint16_t cksum (const void *_data, int len) {
const uint8_t *data = _data;
uint32_t sum;
/* add all pairs of bytes in the input buffer */
for (sum = 0;len >= 2; data += 2, len -= 2)
sum += data[0] << 8 | data[1];
/* handle odd number of bytes in buffer */
if (len > 0)
sum += data[0] << 8;
/* fold 32-bit sum into 16-bit var */
while (sum > 0xffff)
sum = (sum >> 16) + (sum & 0xffff);
sum = htons (~sum);
return sum;
}
uint16_t ethertype(uint8_t *buf) {
sr_ethernet_hdr_t *ehdr = (sr_ethernet_hdr_t *)buf;
return ntohs(ehdr->ether_type);
}
uint8_t ip_protocol(uint8_t *buf) {
sr_ip_hdr_t *iphdr = (sr_ip_hdr_t *)(buf);
return iphdr->ip_p;
}
/* Prints out formatted Ethernet address, e.g. 00:11:22:33:44:55 */
void print_addr_eth(uint8_t *addr) {
int pos = 0;
uint8_t cur;
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 */
void print_addr_ip(struct in_addr address) {
char buf[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 */
void print_addr_ip_int(uint32_t ip) {
uint32_t curOctet = 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);
}
void copy_addr_ip_int(char *buf, uint32_t ip) {
sprintf(buf, "%d.%d.%d.%d", (ip << 24) >> 24, (ip << 16) >> 24, (ip << 8) >> 24, ip >> 24);
}
/* Prints out fields in Ethernet header. */
void print_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. */
void print_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");
else if (ntohs(iphdr->ip_off) & IP_MF)
fprintf(stderr, "\tfragment flag: MF\n");
else if (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", ntohs(iphdr->ip_sum));*/
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 */
void print_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 */
void print_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 */
void print_hdrs(uint8_t *buf, uint32_t length) {
/* Ethernet */
int minlength = sizeof(sr_ethernet_hdr_t);
if (length < minlength) {
fprintf(stderr, "Failed to print ETHERNET header, insufficient length\n");
return;
}
uint16_t ethtype = 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_t ip_proto = ip_protocol(buf + sizeof(sr_ethernet_hdr_t));
if (ip_proto == ip_protocol_icmp) { /* ICMP */
minlength += sizeof(sr_icmp_hdr_t);
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));
}
}
else if (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);
}
}