-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathpath_table.rs
More file actions
151 lines (130 loc) · 4.78 KB
/
path_table.rs
File metadata and controls
151 lines (130 loc) · 4.78 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
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
use std::{collections::HashMap, time::Instant};
use crate::{
hash::{AddressHash, Hash},
packet::{DestinationType, Header, HeaderType, IfacFlag, Packet, PacketType},
};
pub struct PathEntry {
pub timestamp: Instant,
pub received_from: AddressHash,
pub hops: u8,
pub iface: AddressHash,
pub packet_hash: Hash,
}
pub struct PathTable {
map: HashMap<AddressHash, PathEntry>,
}
impl PathTable {
pub fn new() -> Self {
Self {
map: HashMap::new(),
}
}
pub fn next_hop_full(&self, destination: &AddressHash) -> Option<(AddressHash, AddressHash)> {
self.map.get(destination).map(|entry| (entry.received_from, entry.iface))
}
pub fn next_hop_iface(&self, destination: &AddressHash) -> Option<AddressHash> {
self.map.get(destination).map(|entry| entry.iface)
}
pub fn next_hop(&self, destination: &AddressHash) -> Option<AddressHash> {
self.map.get(destination).map(|entry| entry.received_from)
}
pub fn handle_announce(
&mut self,
announce: &Packet,
transport_id: Option<AddressHash>,
iface: AddressHash,
) {
let hops = announce.header.hops + 1;
if let Some(existing_entry) = self.map.get(&announce.destination) {
if hops >= existing_entry.hops {
return;
}
}
let received_from = transport_id.unwrap_or(announce.destination);
let new_entry = PathEntry {
timestamp: Instant::now(),
received_from,
hops,
iface,
packet_hash: announce.hash(),
};
self.map.insert(announce.destination, new_entry);
log::info!(
"{} is now reachable over {} hops through {}",
announce.destination,
hops,
received_from,
);
}
pub fn handle_inbound_packet(
&self,
original_packet: &Packet,
lookup: Option<AddressHash>,
) -> (Packet, Option<AddressHash>) {
let lookup = lookup.unwrap_or(original_packet.destination);
let entry = match self.map.get(&lookup) {
Some(entry) => entry,
None => return (*original_packet, None),
};
(
Packet {
header: Header {
ifac_flag: IfacFlag::Authenticated,
header_type: HeaderType::Type2,
context_flag: original_packet.header.context_flag,
propagation_type: original_packet.header.propagation_type,
destination_type: original_packet.header.destination_type,
packet_type: original_packet.header.packet_type,
hops: original_packet.header.hops + 1,
},
ifac: None,
destination: original_packet.destination,
transport: Some(entry.received_from),
context: original_packet.context,
data: original_packet.data,
},
Some(entry.iface),
)
}
pub fn refresh(&mut self, destination: &AddressHash) {
if let Some(entry) = self.map.get_mut(destination) {
entry.timestamp = Instant::now();
}
}
pub fn handle_packet(&mut self, original_packet: &Packet) -> (Packet, Option<AddressHash>) {
if original_packet.header.header_type == HeaderType::Type2 {
return (*original_packet, None);
}
if original_packet.header.packet_type == PacketType::Announce {
return (*original_packet, None);
}
if original_packet.header.destination_type == DestinationType::Plain
|| original_packet.header.destination_type == DestinationType::Group
{
return (*original_packet, None);
}
let entry = match self.map.get(&original_packet.destination) {
Some(entry) => entry,
None => return (*original_packet, None),
};
(
Packet {
header: Header {
ifac_flag: IfacFlag::Authenticated,
header_type: HeaderType::Type2,
context_flag: original_packet.header.context_flag,
propagation_type: original_packet.header.propagation_type,
destination_type: original_packet.header.destination_type,
packet_type: original_packet.header.packet_type,
hops: original_packet.header.hops,
},
ifac: original_packet.ifac,
destination: original_packet.destination,
transport: Some(entry.received_from),
context: original_packet.context,
data: original_packet.data,
},
Some(entry.iface),
)
}
}