-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathevent.rs
More file actions
180 lines (149 loc) · 5.75 KB
/
Copy pathevent.rs
File metadata and controls
180 lines (149 loc) · 5.75 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
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
use std::net::{IpAddr, SocketAddr};
use torrust_info_hash::InfoHash;
use torrust_metrics::label::{LabelSet, LabelValue};
use torrust_metrics::label_name;
use torrust_net_primitives::service_binding::{IpFamily, IpType, ServiceBinding};
use torrust_tracker_primitives::peer::PeerAnnouncement;
/// A UDP core event.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Event {
UdpConnect {
connection: ConnectionContext,
},
UdpAnnounce {
connection: ConnectionContext,
info_hash: InfoHash,
announcement: PeerAnnouncement,
},
UdpScrape {
connection: ConnectionContext,
},
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ConnectionContext {
client_socket_addr: SocketAddr,
server_service_binding: ServiceBinding,
}
impl ConnectionContext {
#[must_use]
pub fn new(client_socket_addr: SocketAddr, server_service_binding: ServiceBinding) -> Self {
Self {
client_socket_addr,
server_service_binding,
}
}
#[must_use]
pub fn client_socket_addr(&self) -> SocketAddr {
self.client_socket_addr
}
#[must_use]
pub fn server_socket_addr(&self) -> SocketAddr {
self.server_service_binding.bind_address()
}
#[must_use]
pub fn client_address_ip_family(&self) -> IpFamily {
self.client_socket_addr.ip().into()
}
#[must_use]
pub fn client_address_ip_type(&self) -> IpType {
match self.client_socket_addr.ip() {
IpAddr::V6(v6) if v6.to_ipv4_mapped().is_some() => IpType::V4MappedV6,
_ => IpType::Plain,
}
}
}
impl From<ConnectionContext> for LabelSet {
fn from(connection_context: ConnectionContext) -> Self {
LabelSet::from([
(
label_name!("server_binding_protocol"),
LabelValue::new(&connection_context.server_service_binding.protocol().to_string()),
),
(
label_name!("server_binding_ip"),
LabelValue::new(&connection_context.server_service_binding.bind_address().ip().to_string()),
),
(
label_name!("server_binding_address_ip_type"),
LabelValue::new(&connection_context.server_service_binding.bind_address_ip_type().to_string()),
),
(
label_name!("server_binding_address_ip_family"),
LabelValue::new(&connection_context.server_service_binding.bind_address_ip_family().to_string()),
),
(
label_name!("server_binding_port"),
LabelValue::new(&connection_context.server_service_binding.bind_address().port().to_string()),
),
(
label_name!("client_address_ip_family"),
LabelValue::new(&connection_context.client_address_ip_family().to_string()),
),
(
label_name!("client_address_ip_type"),
LabelValue::new(&connection_context.client_address_ip_type().to_string()),
),
])
}
}
pub mod sender {
use std::sync::Arc;
use super::Event;
pub type Sender = Option<Arc<dyn torrust_tracker_events::sender::Sender<Event = Event>>>;
pub type Broadcaster = torrust_tracker_events::broadcaster::Broadcaster<Event>;
}
pub mod receiver {
use super::Event;
pub type Receiver = Box<dyn torrust_tracker_events::receiver::Receiver<Event = Event>>;
}
pub mod bus {
use crate::event::Event;
pub type EventBus = torrust_tracker_events::bus::EventBus<Event>;
}
#[cfg(test)]
pub(crate) mod tests {
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use torrust_net_primitives::service_binding::{IpFamily, IpType, Protocol, ServiceBinding};
use super::ConnectionContext;
#[test]
fn client_address_ip_family_should_be_inet_for_ipv4() {
let ctx = ConnectionContext::new(
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 6969),
ServiceBinding::new(Protocol::UDP, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 6969)).unwrap(),
);
assert_eq!(ctx.client_address_ip_family(), IpFamily::Inet);
}
#[test]
fn client_address_ip_family_should_be_inet6_for_ipv6() {
let ctx = ConnectionContext::new(
SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 6969),
ServiceBinding::new(Protocol::UDP, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 6969)).unwrap(),
);
assert_eq!(ctx.client_address_ip_family(), IpFamily::Inet6);
}
#[test]
fn client_address_ip_type_should_be_plain_for_direct_ipv4() {
let ctx = ConnectionContext::new(
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 6969),
ServiceBinding::new(Protocol::UDP, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 6969)).unwrap(),
);
assert_eq!(ctx.client_address_ip_type(), IpType::Plain);
}
#[test]
fn client_address_ip_type_should_be_plain_for_native_ipv6() {
let ctx = ConnectionContext::new(
SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 6969),
ServiceBinding::new(Protocol::UDP, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 6969)).unwrap(),
);
assert_eq!(ctx.client_address_ip_type(), IpType::Plain);
}
#[test]
fn client_address_ip_type_should_be_v4_mapped_v6_for_ipv4_mapped_ipv6() {
let v4_mapped_v6_addr = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x0101)); // ::ffff:192.168.1.1
let ctx = ConnectionContext::new(
SocketAddr::new(v4_mapped_v6_addr, 6969),
ServiceBinding::new(Protocol::UDP, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 6969)).unwrap(),
);
assert_eq!(ctx.client_address_ip_type(), IpType::V4MappedV6);
}
}