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
use crate::smtp::*;
use std::net::SocketAddr;
use std::time::{Duration, Instant};
pub use bytes::Bytes;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum WriteControl {
Shutdown,
StartData(SmtpReply),
StartTls(SmtpReply),
Reply(SmtpReply),
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ReadControl {
PeerConnected(Connection),
PeerShutdown,
Command(SmtpCommand),
Raw(Bytes),
MailDataChunk(Bytes),
EndOfMailData(Bytes),
EscapeDot(Bytes),
Empty(Bytes),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Connection {
local_addr: Option<SocketAddr>,
peer_addr: Option<SocketAddr>,
established: Instant,
extensions: ExtensionSet,
}
impl Connection {
pub fn new<L, P>(local: L, peer: P) -> Connection
where
L: Into<Option<SocketAddr>>,
P: Into<Option<SocketAddr>>,
{
Connection {
local_addr: local.into(),
peer_addr: peer.into(),
established: Instant::now(),
extensions: ExtensionSet::new(),
}
}
pub fn local_addr(&self) -> Option<SocketAddr> {
self.local_addr.clone()
}
pub fn peer_addr(&self) -> Option<SocketAddr> {
self.peer_addr.clone()
}
pub fn age(&self) -> Duration {
Instant::now() - self.established
}
pub fn extensions(&self) -> &ExtensionSet {
&self.extensions
}
pub fn extensions_mut(&mut self) -> &mut ExtensionSet {
&mut self.extensions
}
}
impl Default for Connection {
fn default() -> Connection {
Connection::new(None, None)
}
}
impl std::fmt::Display for Connection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "Connection from peer ")?;
if let Some(a) = self.peer_addr {
write!(f, "{}", a)?;
} else {
write!(f, "Unknown")?;
}
write!(f, " to local ")?;
if let Some(a) = self.local_addr {
write!(f, "{}", a)?;
} else {
write!(f, "Unknown")?;
}
write!(f, " established {:?} ago", self.age())?;
Ok(())
}
}