-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip_module.cc
212 lines (159 loc) · 5.68 KB
/
ip_module.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
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include "Minet.h"
#define DEBUG_SEND 1
#define DEBUG_RECV 1
int SendPacket(MinetHandle ðermux, MinetHandle &arp, Packet &p)
{
IPHeader iph = p.FindHeader(Headers::IPHeader);
IPAddress ipaddr;
iph.GetDestIP(ipaddr);
ARPRequestResponse req(ipaddr,
EthernetAddr(ETHERNET_BLANK_ADDR),
ARPRequestResponse::REQUEST);
ARPRequestResponse resp;
MinetSend(arp,req);
MinetReceive(arp,resp);
if (resp.ethernetaddr!=ETHERNET_BLANK_ADDR) {
resp.flag=ARPRequestResponse::RESPONSE_OK;
}
if ((resp.flag==ARPRequestResponse::RESPONSE_OK) ||
(ipaddr == "255.255.255.255")) {
// set src and dest addrs in header
// set protocol ip
EthernetHeader h;
h.SetSrcAddr(MyEthernetAddr);
h.SetDestAddr(resp.ethernetaddr);
h.SetProtocolType(PROTO_IP);
p.PushHeader(h);
RawEthernetPacket e(p);
#if DEBUG_SEND
cout << "ABOUT TO SEND OUT: " << endl;
Packet check(e);
check.ExtractHeaderFromPayload<EthernetHeader>(ETHERNET_HEADER_LEN);
check.ExtractHeaderFromPayload<IPHeader>(IPHeader::EstimateIPHeaderLength(check));
EthernetHeader eh = check.FindHeader(Headers::EthernetHeader);
IPHeader iph = check.FindHeader(Headers::IPHeader);
eh.Print(cerr); cerr << endl;
iph.Print(cerr); cerr << endl;
cout << "END OF PACKET" << endl << endl;
#endif
MinetSend(ethermux,e);
return 0;
} else {
MinetSendToMonitor(MinetMonitoringEvent("Discarding packet because there is no arp entry"));
cerr << "Discarded IP packet because there is no arp entry\n";
return -1;
}
}
int main(int argc, char *argv[])
{
MinetHandle ethermux, ipmux, arp;
MinetInit(MINET_IP_MODULE);
ethermux=MinetIsModuleInConfig(MINET_ETHERNET_MUX) ? \
MinetConnect(MINET_ETHERNET_MUX) : MINET_NOHANDLE;
arp=MinetIsModuleInConfig(MINET_ARP_MODULE) ? \
MinetConnect(MINET_ARP_MODULE) : MINET_NOHANDLE;
ipmux=MinetIsModuleInConfig(MINET_IP_MUX) ? \
MinetAccept(MINET_IP_MUX) : MINET_NOHANDLE;
if (ethermux==MINET_NOHANDLE && MinetIsModuleInConfig(MINET_ETHERNET_MUX)) {
MinetSendToMonitor(MinetMonitoringEvent("Can't connect to ethermux"));
return -1;
}
if (ipmux==MINET_NOHANDLE && MinetIsModuleInConfig(MINET_ETHERNET_MUX)) {
MinetSendToMonitor(MinetMonitoringEvent("Can't accept from ipmux"));
return -1;
}
if (arp==MINET_NOHANDLE && MinetIsModuleInConfig(MINET_ETHERNET_MUX)) {
MinetSendToMonitor(MinetMonitoringEvent("Can't connect to arp_module"));
return -1;
}
MinetSendToMonitor(MinetMonitoringEvent("ip_module handling IP traffic........"));
MinetEvent event;
while (MinetGetNextEvent(event)==0) {
if (event.eventtype!=MinetEvent::Dataflow
|| event.direction!=MinetEvent::IN) {
MinetSendToMonitor(MinetMonitoringEvent("Unknown event ignored."));
} else {
if (event.handle==ethermux) {
RawEthernetPacket raw;
MinetReceive(ethermux,raw);
Packet p(raw);
p.ExtractHeaderFromPayload<EthernetHeader>(ETHERNET_HEADER_LEN);
p.ExtractHeaderFromPayload<IPHeader>(IPHeader::EstimateIPHeaderLength(p));
IPHeader iph;
iph=p.FindHeader(Headers::IPHeader);
#if DEBUG_RECV
cerr << "Received Packet: " << endl;
iph.Print(cerr); cerr << endl; p.Print(cerr); cerr << endl;
#endif
IPAddress toip;
iph.GetDestIP(toip);
if (toip==MyIPAddr || toip==IPAddress(IP_ADDRESS_BROADCAST)) {
if (!(iph.IsChecksumCorrect())) {
// discard the packet
MinetSendToMonitor(MinetMonitoringEvent \
("Discarding packet because header checksum is wrong."));
cerr << "Discarding following packet because header checksum is wrong: "<<p<<"\n";
IPAddress src; iph.GetSourceIP(src);
// "2" specifies the octet that is wrong (in this case, the checksum)
ICMPPacket error(src, PARAMETER_PROBLEM, 2, p);
MinetSendToMonitor(MinetMonitoringEvent("ICMP error message has been sent to host"));
SendPacket(ethermux,arp,error);
continue;
}
unsigned char ttl;
iph.GetTTL(ttl);
if (ttl==0) {
// discard the packet
MinetSendToMonitor(MinetMonitoringEvent \
("Discarding packet because TTL is zero."));
cerr << "Discarding following packet because TTL is zero: "<<p<<"\n";
IPAddress src; iph.GetSourceIP(src);
ICMPPacket error(src, TIME_EXCEEDED,TTL_EQUALS_ZERO_DURING_TRANSIT, p);
MinetSendToMonitor(MinetMonitoringEvent("ICMP error message has been sent to host"));
SendPacket(ethermux,arp,error);
continue;
}
unsigned short fragoff;
unsigned char flags;
iph.GetFlags(flags);
iph.GetFragOffset(fragoff);
if ((flags&IP_HEADER_FLAG_MOREFRAG) || (fragoff!=0)) {
MinetSendToMonitor(MinetMonitoringEvent \
("Discarding packet because it is a fragment"));
cerr << "Discarding following packet because it is a fragment: "<<p<<"\n";
IPAddress src; iph.GetSourceIP(src);
ICMPPacket error(src, DESTINATION_UNREACHABLE ,FRAGMENTATION_NEEDED, p);
MinetSendToMonitor(MinetMonitoringEvent("ICMP error message has been sent to host"));
SendPacket(ethermux,arp,error);
continue;
}
// route here - would send icmp dest unreachable if fails
// unfragment here
MinetSend(ipmux,p);
} else {
; // discarded due to different target address
}
}
if (event.handle==ipmux) {
Packet p;
MinetReceive(ipmux,p);
// Route Packet Here - would send icmp dest unreachable if fails
// Fragment Packet Here
SendPacket(ethermux,arp,p);
}
}
}
MinetDeinit();
return 0;
}