-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip.cc
414 lines (326 loc) · 8.04 KB
/
ip.cc
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "ip.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "util.h"
#include "error.h"
IPAddress MyIPAddr(getenv("MINET_IPADDR") ?
getenv("MINET_IPADDR") : (char*)Die("MINET_IPADDR is not set!"));
IPAddress::IPAddress() :addr(0)
{}
IPAddress::IPAddress(const char *s)
{
addr = ntohl(inet_addr(s));
}
IPAddress::IPAddress(const IPAddress &rhs) : addr(rhs.addr)
{}
IPAddress::IPAddress(const unsigned rhs) : addr(rhs)
{}
IPAddress & IPAddress::operator=(const IPAddress &rhs)
{
this->addr=rhs.addr;
return *this;
}
IPAddress & IPAddress::operator=(const unsigned rhs)
{
this->addr=rhs;
return *this;
}
bool IPAddress::operator==(const IPAddress &rhs) const
{
return addr==rhs.addr;
}
IPAddress::operator unsigned() const
{
return addr;
}
char *IPAddress::operator& () const
{
return (char*)&addr;
}
void IPAddress::Serialize(const int fd) const
{
if (writeall(fd,(const char *)&addr,sizeof(addr))!=sizeof(addr)) {
throw SerializationException();
}
}
void IPAddress::Unserialize(const int fd)
{
if (readall(fd,(char*)&addr,sizeof(addr))!=sizeof(addr)) {
throw SerializationException();
}
}
ostream & IPAddress::Print(ostream &os) const
{
struct in_addr in;
in.s_addr=htonl(addr);
os << "IPAddress("<<inet_ntoa(in)<<")";
return os;
}
unsigned short ip_id_counter=0;
IPHeader::IPHeader() : Header(Headers::IPHeader)
{
SetVersion(IP_HEADER_REQUIRED_VERSION);
SetHeaderLength(IP_HEADER_BASE_LENGTH_IN_WORDS); // 20 bytes, 5 words
SetTOS(IP_HEADER_DEFAULT_TOS);
SetTotalLength(IP_HEADER_BASE_LENGTH);
SetID(ip_id_counter++);
SetFlags(IP_HEADER_FLAG_DEFAULT);
SetFragOffset(0);
SetTTL(IP_HEADER_TTL_DEFAULT);
SetProtocol(IP_PROTO_RAW);
SetSourceIP(IPAddress(0U));
SetDestIP(IPAddress(0U));
RecomputeChecksum();
}
IPHeader::IPHeader(const Buffer &rhs) : Header(Headers::IPHeader,rhs)
{
}
IPHeader::IPHeader(const IPHeader &rhs) : Header(rhs)
{
}
IPHeader::IPHeader(const char *buf, unsigned len) : Header(Headers::IPHeader,buf,len)
{
}
IPHeader::~IPHeader()
{}
IPHeader & IPHeader::operator=(const IPHeader &rhs)
{
Header::operator=(rhs);
return *this;
}
// This is a helper to determine the header length prior to
// extracting it
// The assumption is that the ethernet header has already been stripped.
unsigned IPHeader::EstimateIPHeaderLength(Packet &p)
{
Buffer &b = p.GetPayload();
unsigned char len;
b.GetData((char*)&len,1,0);
len&=0xf;
len*=4;
return len;
}
// 4 bit version fields
// automatically set to
void IPHeader::GetVersion(unsigned char &version) const
{
GetData((char*)&version,1,0);
version&=0xf0;
version>>=4;
}
void IPHeader::SetVersion(const unsigned char &version)
{
unsigned char t;
GetData((char*)&t,1,0);
t=(t&0x0f)|((version<<4)&0xf0);
SetData((char*)&t,1,0);
RecomputeChecksum();
}
void IPHeader::GetHeaderLength(unsigned char &hlen) const
{
GetData((char*)&hlen,1,0);
hlen&=0x0f;
}
// note that the header length is recomputed automatically
void IPHeader::SetHeaderLength(const unsigned char &hlen)
{
unsigned char t;
GetData((char*)&t,1,0);
t=(t&0xf0)|(hlen&0x0f);
SetData((char*)&t,1,0);
RecomputeChecksum();
}
void IPHeader::GetTOS(unsigned char &tos) const
{
GetData((char*)&tos,1,1);
}
void IPHeader::SetTOS(const unsigned char &tos)
{
SetData((char*)&tos,1,1);
RecomputeChecksum();
}
void IPHeader::GetTotalLength(unsigned short &len) const
{
GetData((char*)&len,2,2);
len=ntohs(len);
}
// note that total length is automatically computed
void IPHeader::SetTotalLength(const unsigned short &len)
{
unsigned short l=htons(len);
SetData((char*)&l,2,2);
RecomputeChecksum();
}
void IPHeader::GetID(unsigned short &id) const
{
GetData((char*)&id,2,4);
id=ntohs(id);
}
void IPHeader::SetID(const unsigned short &id)
{
unsigned short i=htons(id);
SetData((char*)&i,2,4);
RecomputeChecksum();
}
void IPHeader::GetFlags(unsigned char &flags) const
{
GetData((char*)&flags,1,6);
flags=(0x7&(flags>>5));
}
void IPHeader::SetFlags(const unsigned char &flags)
{
unsigned char t;
GetData((char*)&t,1,6);
t&=0x1f;
t|=((0x7&flags)<<5);
SetData((char*)&t,1,6);
RecomputeChecksum();
}
void IPHeader::GetFragOffset(unsigned short &offset) const
{
GetData((char*)&offset,2,6);
offset=ntohs(offset);
offset&=0x1fff;
}
void IPHeader::SetFragOffset(const unsigned short &offset)
{
unsigned char f;
unsigned short o=htons(offset);
GetFlags(f);
SetData((char*)&o,2,6);
SetFlags(f);
RecomputeChecksum();
}
void IPHeader::GetTTL(unsigned char &ttl) const
{
GetData((char*)&ttl,1,8);
}
void IPHeader::SetTTL(const unsigned char &ttl)
{
SetData((char*)&ttl,1,8);
RecomputeChecksum();
}
void IPHeader::GetProtocol(unsigned char &proto) const
{
GetData((char*)&proto,1,9);
}
void IPHeader::SetProtocol(const unsigned char &proto)
{
SetData((char*)&proto,1,9);
RecomputeChecksum();
}
unsigned short IPHeader::ComputeChecksum() const
{
unsigned short buf[IP_HEADER_MAX_LENGTH/2];
unsigned char len;
GetHeaderLength(len);
len*=4;
GetData((char*)buf,len,0);
return ~(OnesComplementSum(buf,len/2));
}
bool IPHeader::IsChecksumCorrect() const
{
return ComputeChecksum()==0;
}
void IPHeader::RecomputeChecksum()
{
SetChecksum(0);
SetChecksum(ComputeChecksum());
}
void IPHeader::GetChecksum(unsigned short &checksum) const
{
GetData((char*)&checksum,2,10);
checksum=ntohs(checksum);
}
// Note that this will be recomputed every time one of the set calls is run
void IPHeader::SetChecksum(const unsigned short &checksum)
{
unsigned short c=htons(checksum);
SetData((char*)&c,2,10);
}
void IPHeader::GetSourceIP(IPAddress &addr) const
{
GetData(&addr,4,12);
addr=ntohl(addr);
}
void IPHeader::SetSourceIP(const IPAddress &addr)
{
unsigned a=htonl(addr);
SetData((char*)&a,4,12);
RecomputeChecksum();
}
void IPHeader::GetDestIP(IPAddress &addr) const
{
GetData(&addr,4,16);
addr=ntohl(addr);
}
void IPHeader::SetDestIP(const IPAddress &addr)
{
unsigned a=htonl(addr);
SetData((char*)&a,4,16);
RecomputeChecksum();
}
void IPHeader::GetOptions(IPOptions &opt) const
{
unsigned char len;
GetHeaderLength(len);
len=len*4;
if (len>IP_HEADER_BASE_LENGTH) {
GetData(opt.data,len-IP_HEADER_BASE_LENGTH,IP_HEADER_BASE_LENGTH);
opt.len=len-IP_HEADER_BASE_LENGTH;
} else {
opt.len=0;
}
}
void IPHeader::SetOptions(const IPOptions &opt)
{
SetData(opt.data,opt.len,IP_HEADER_BASE_LENGTH);
short len=(IP_HEADER_BASE_LENGTH+opt.len)/4;
SetHeaderLength(len);
RecomputeChecksum();
}
ostream & IPHeader::Print(ostream &os) const
{
bool isvalidchecksum;
unsigned char version, hlen, tos, flags, ttl, proto;
unsigned short len, id, fragoff, checksum, computedchecksum;
IPAddress src, dest;
GetVersion(version);
GetHeaderLength(hlen);
GetTOS(tos);
GetTotalLength(len);
GetID(id);
GetFlags(flags);
GetFragOffset(fragoff);
GetTTL(ttl);
GetProtocol(proto);
isvalidchecksum=IsChecksumCorrect();
GetChecksum(checksum);
GetSourceIP(src);
GetDestIP(dest);
computedchecksum=ComputeChecksum();
os << "IPHeader(version="<<unsigned(version)
<< ", hlen="<<unsigned(hlen)<<" ("<<unsigned(hlen)*4<<")"
<< ", tos="<<unsigned(tos)
<< ", len="<<len
<< ", id="<<id
<< ", flags="<<unsigned(flags) << "("
<< ((flags&IP_HEADER_FLAG_RESERVED) ? "RESERVED " : "")
<< ((flags&IP_HEADER_FLAG_DONTFRAG) ? "DO NOT FRAGMENT " : "OK TO FRAGMENT ")
<< ((flags&IP_HEADER_FLAG_MOREFRAG) ? "MORE FRAGMENTS" : "NO MORE FRAGMENTS") << ")"
<< ", fragoff="<<fragoff
<< ", ttl="<<unsigned(ttl)
<< ", proto="<<unsigned(proto) <<"("
<< ((proto==IP_PROTO_UDP) ? "UDP" :
(proto==IP_PROTO_TCP) ? "TCP" :
(proto==IP_PROTO_ICMP) ? "ICMP" :
(proto==IP_PROTO_RAW) ? "RAW" :
(proto==IP_PROTO_IP) ? "IP" : "UNKNOWN") <<")"
<< ", checksum="<<checksum<< (isvalidchecksum ? "(valid)" : "(INVALID)")
<< ", computedchecksum="<<computedchecksum
<< ", src="<< src
<< ", dst="<< dest
<< (((hlen*4)>IP_HEADER_BASE_LENGTH) ? "OPTIONS" : "noopts") << ")";
return os;
}