-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw3.cpp
More file actions
102 lines (92 loc) · 2.44 KB
/
hw3.cpp
File metadata and controls
102 lines (92 loc) · 2.44 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
/*
* THIS FILE IS FOR IP FORWARD TEST
*/
#include "sysInclude.h"
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
// system support
extern void fwd_LocalRcv(char *pBuffer, int length);
extern void fwd_SendtoLower(char *pBuffer, int length, unsigned int nexthop);
extern void fwd_DiscardPkt(char *pBuffer, int type);
extern unsigned int getIpv4Address();
typedef struct stud_route_msg
{
unsigned int dest;
unsigned int masklen;
unsigned int nexthop;
} stud_route_msg;
map<int, int> routeTable;
// implemented by students
// calculate checksum
unsigned short checkSum(char *pBuffer)
{
int sum = 0;
unsigned short result;
for (int i = 0; i < 10; i++)
{
if (i != 5)
{
sum += (int)((unsigned char)pBuffer[2 * i] << 8);
sum += (int)((unsigned char)pBuffer[2 * i + 1]);
}
}
// transform to network-byte order(big endian)
// keep add lower 16 bit and higher 16 bits until high 16 bits become 0
while ((sum & 0xffff0000) != 0)
sum = (sum & 0xffff) + ((sum >> 16) & 0xffff);
result = sum;
// negate result
return htons(~result);
}
// init routing table
void stud_Route_Init()
{
routeTable.clear();
return;
}
// update routing table
void stud_route_add(stud_route_msg *proute)
{
int masklen = ntohl(proute->masklen);
int dest = (ntohl(proute->dest)) & (0xffffffff << (32 - masklen));
int next = ntohl(proute->nexthop);
routeTable.insert(make_pair(dest, next));
return;
}
// IPv4 packet routing and forwarding
int stud_fwd_deal(char *pBuffer, int length)
{
int ttl = (int)pBuffer[8];
int dest = ntohl(*(unsigned int *)(pBuffer + 16));
// localhost is destination
if (dest == getIpv4Address())
{
fwd_LocalRcv(pBuffer, length);
return 0;
}
// use up Time-to-live, discard package
if (ttl == 0)
{
fwd_DiscardPkt(pBuffer, STUD_FORWARD_TEST_TTLERROR);
return 1;
}
map<int, int>::iterator p = routeTable.find(dest);
// entry found in routing table
if (p != routeTable.end())
{
char *sendBuffer = new char[length];
memcpy(sendBuffer, pBuffer, length);
// decrement ttl
sendBuffer[8]--;
// need to re-calculate checkSum
unsigned int sendSum = checkSum(sendBuffer);
*((unsigned short *)(sendBuffer + 10)) = sendSum;
fwd_SendtoLower(sendBuffer, length, p->second);
return 0;
}
// discard package if entry is not found in routing table
fwd_DiscardPkt(pBuffer, STUD_FORWARD_TEST_NOROUTE);
return 1;
}