-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_host_protocol.cpp
More file actions
71 lines (59 loc) · 2.11 KB
/
ip_host_protocol.cpp
File metadata and controls
71 lines (59 loc) · 2.11 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
#include "ip_host_protocol.h"
void ip_host_protocol::dinternal(double t) {
if (!lower_layer_ctrl_in.empty()) {
link::Control c = lower_layer_ctrl_in.front();
this->processLinkControl(c);
lower_layer_ctrl_in.pop();
next_internal = process_link_control_time;
return;
}
if (!higher_layer_data_in.empty()) {
udp::Segment d = higher_layer_data_in.front();
this->processUDPSegment(d);
higher_layer_data_in.pop();
next_internal = process_udp_segment_time;
return;
}
if (!lower_layer_data_in.empty()) {
logger.debug("Process ip Datagram input.");
ip::Datagram p = lower_layer_data_in.front();
this->processIPDatagram(p);
lower_layer_data_in.pop();
next_internal = process_ip_datagram_time;
return;
}
}
/***********************************/
/********* HELPER METHODS **********/
/***********************************/
void ip_host_protocol::processIPDatagram(ip::Datagram p) {
IPv4 dest_ip = p.header.dest_ip;
Event o;
logger.info("Processing arrived ip packet: " + dest_ip.as_string());
if (!this->verifychecksum(p.header)) { // silent discard
logger.info("Discard packet: checksum verification faild for packet with dest_ip: " + dest_ip.as_string());
return;
}
if (!this->matchesHostIps(dest_ip)) { // Silent discard
logger.info("Discard packet: host ip does not match for packet with dest_ip: " + dest_ip.as_string());
return;
}
// Delivering segment to the next top layer
higher_layer_data_out.push(p.data);
return;
}
void ip_host_protocol::processUDPSegment(udp::Segment d) {
ip::Datagram p;
p.data = d;
// version 0100 (IPv4) IHL 0101 (5 no option field)
// DSCP 0000 (currently not used) ECN 0000 (currently not used)
p.header.vide = 0x4500;
p.header.total_length = sizeof(ip::Header) + d.psd_header.udp_lenght;
// set ttl in hexa FF (max posible TTL)
// set protocol in hexa 11 dec 17 is the protocol number of UDP
p.header.ttlp = 0xFF11;
p.header.src_ip = d.psd_header.src_ip;
p.header.dest_ip = d.psd_header.dest_ip;
p.header.header_checksum = this->calculateChecksum(p.header);
this->routeIPDatagram(p);
}