aboutsummaryrefslogtreecommitdiff
path: root/src/conn.rs
blob: 9b60d2a390ba6849e91d7f0721703b2bcca9c8de (plain) (blame)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::atomic::{self, AtomicU16};
use std::sync::Arc;

use async_trait::async_trait;
use bytes::Bytes;
use log::{debug, trace};

use sodiumoxide::crypto::sign::ed25519;
use tokio::io::split;
use tokio::net::TcpStream;
use tokio::sync::{mpsc, oneshot, watch};

use kuska_handshake::async_std::{
	handshake_client, handshake_server, BoxStream, TokioCompatExt, TokioCompatExtRead,
	TokioCompatExtWrite,
};

use crate::error::*;
use crate::message::*;
use crate::netapp::*;
use crate::proto::*;
use crate::util::*;

pub struct ServerConn {
	netapp: Arc<NetApp>,
	pub remote_addr: SocketAddr,
	pub peer_pk: ed25519::PublicKey,
	resp_send: mpsc::UnboundedSender<(RequestID, RequestPriority, Vec<u8>)>,
	close_send: watch::Sender<bool>,
}

impl ServerConn {
	pub(crate) async fn run(netapp: Arc<NetApp>, socket: TcpStream) -> Result<(), Error> {
		let mut asyncstd_socket = TokioCompatExt::wrap(socket);
		let handshake = handshake_server(
			&mut asyncstd_socket,
			netapp.netid.clone(),
			netapp.pubkey.clone(),
			netapp.privkey.clone(),
		)
		.await?;
		let peer_pk = handshake.peer_pk.clone();

		let tokio_socket = asyncstd_socket.into_inner();
		let remote_addr = tokio_socket.peer_addr().unwrap();

		debug!(
			"Handshake complete (server) with {}@{}",
			hex::encode(&peer_pk),
			remote_addr
		);

		let (read, write) = split(tokio_socket);

		let read = TokioCompatExtRead::wrap(read);
		let write = TokioCompatExtWrite::wrap(write);

		let (box_stream_read, box_stream_write) =
			BoxStream::from_handshake(read, write, handshake, 0x8000).split_read_write();

		let (resp_send, resp_recv) = mpsc::unbounded_channel();

		let (close_send, close_recv) = watch::channel(false);

		let conn = Arc::new(ServerConn {
			netapp: netapp.clone(),
			remote_addr,
			peer_pk: peer_pk.clone(),
			resp_send,
			close_send,
		});

		netapp.connected_as_server(peer_pk.clone(), conn.clone());

		let conn2 = conn.clone();
		let conn3 = conn.clone();
		tokio::try_join!(
			conn2.recv_loop(box_stream_read, close_recv.clone()),
			conn3.send_loop(resp_recv, box_stream_write, close_recv.clone()),
		)
		.map(|_| ())
		.log_err("ServerConn recv_loop/send_loop");

		netapp.disconnected_as_server(&peer_pk, conn);

		Ok(())
	}

	pub fn close(&self) {
		self.close_send.broadcast(true).unwrap();
	}
}

impl SendLoop for ServerConn {}

#[async_trait]
impl RecvLoop for ServerConn {
	async fn recv_handler(self: Arc<Self>, id: u16, bytes: Vec<u8>) {
		let bytes: Bytes = bytes.into();

		let prio = bytes[0];

		let mut kind_bytes = [0u8; 4];
		kind_bytes.copy_from_slice(&bytes[1..5]);
		let kind = u32::from_be_bytes(kind_bytes);

		if let Some(handler) = self.netapp.msg_handlers.load().get(&kind) {
			let resp = handler(self.peer_pk.clone(), bytes.slice(5..)).await;
			self.resp_send
				.send((id, prio, resp))
				.log_err("ServerConn recv_handler send resp");
		}
	}
}
pub struct ClientConn {
	pub netapp: Arc<NetApp>,
	pub remote_addr: SocketAddr,
	pub peer_pk: ed25519::PublicKey,
	query_send: mpsc::UnboundedSender<(RequestID, RequestPriority, Vec<u8>)>,
	next_query_number: AtomicU16,
	resp_send: mpsc::UnboundedSender<(RequestID, Vec<u8>)>,
	resp_notify_send: mpsc::UnboundedSender<(RequestID, oneshot::Sender<Vec<u8>>)>,
	close_send: watch::Sender<bool>,
}

impl ClientConn {
	pub(crate) async fn init(
		netapp: Arc<NetApp>,
		socket: TcpStream,
		remote_pk: ed25519::PublicKey,
	) -> Result<(), Error> {
		let mut asyncstd_socket = TokioCompatExt::wrap(socket);

		let handshake = handshake_client(
			&mut asyncstd_socket,
			netapp.netid.clone(),
			netapp.pubkey.clone(),
			netapp.privkey.clone(),
			remote_pk.clone(),
		)
		.await?;

		let tokio_socket = asyncstd_socket.into_inner();
		let remote_addr = tokio_socket.peer_addr().unwrap();

		debug!(
			"Handshake complete (client) with {}@{}",
			hex::encode(&remote_pk),
			remote_addr
		);

		let (read, write) = split(tokio_socket);

		let read = TokioCompatExtRead::wrap(read);
		let write = TokioCompatExtWrite::wrap(write);

		let (box_stream_read, box_stream_write) =
			BoxStream::from_handshake(read, write, handshake, 0x8000).split_read_write();

		let (query_send, query_recv) = mpsc::unbounded_channel();
		let (resp_send, resp_recv) = mpsc::unbounded_channel();
		let (resp_notify_send, resp_notify_recv) = mpsc::unbounded_channel();

		let (close_send, close_recv) = watch::channel(false);

		let conn = Arc::new(ClientConn {
			netapp: netapp.clone(),
			remote_addr,
			peer_pk: remote_pk.clone(),
			next_query_number: AtomicU16::from(0u16),
			query_send,
			resp_send,
			resp_notify_send,
			close_send,
		});

		netapp.connected_as_client(remote_pk.clone(), conn.clone());

		tokio::spawn(async move {
			let conn2 = conn.clone();
			let conn3 = conn.clone();
			let conn4 = conn.clone();
			tokio::try_join!(
				conn2.send_loop(query_recv, box_stream_write, close_recv.clone()),
				conn3.recv_loop(box_stream_read, close_recv.clone()),
				conn4.dispatch_resp(resp_recv, resp_notify_recv, close_recv.clone()),
			)
			.map(|_| ())
			.log_err("ClientConn send_loop/recv_loop/dispatch_loop");

			netapp.disconnected_as_client(&remote_pk, conn);
		});

		Ok(())
	}

	pub fn close(&self) {
		self.close_send.broadcast(true).unwrap();
	}

	async fn dispatch_resp(
		self: Arc<Self>,
		mut resp_recv: mpsc::UnboundedReceiver<(RequestID, Vec<u8>)>,
		mut resp_notify_recv: mpsc::UnboundedReceiver<(RequestID, oneshot::Sender<Vec<u8>>)>,
		mut must_exit: watch::Receiver<bool>,
	) -> Result<(), Error> {
		let mut resps: HashMap<RequestID, Vec<u8>> = HashMap::new();
		let mut resp_notify: HashMap<RequestID, oneshot::Sender<Vec<u8>>> = HashMap::new();
		while !*must_exit.borrow() {
			tokio::select! {
				resp = resp_recv.recv() => {
					if let Some((id, resp)) = resp {
						trace!("dispatch_resp: got resp to {}, {} bytes", id, resp.len());
						if let Some(ch) = resp_notify.remove(&id) {
							ch.send(resp).map_err(|_| Error::Message("Could not dispatch reply".to_string()))?;
						} else {
							resps.insert(id, resp);
						}
					}
				}
				resp_ch = resp_notify_recv.recv() => {
					if let Some((id, resp_ch)) = resp_ch {
						trace!("dispatch_resp: got resp_ch {}", id);
						if let Some(rs) = resps.remove(&id) {
							resp_ch.send(rs).map_err(|_| Error::Message("Could not dispatch reply".to_string()))?;
						} else {
							resp_notify.insert(id, resp_ch);
						}
					}
				}
				exit = must_exit.recv() => {
					if exit == Some(true) {
						break;
					}
				}
			}
		}
		Ok(())
	}

	pub async fn request<T>(
		self: Arc<Self>,
		rq: T,
		prio: RequestPriority,
	) -> Result<<T as Message>::Response, Error>
	where
		T: Message,
	{
		let id = self
			.next_query_number
			.fetch_add(1u16, atomic::Ordering::Relaxed);
		let mut bytes = vec![prio];
		bytes.extend_from_slice(&u32::to_be_bytes(T::KIND)[..]);
		bytes.extend_from_slice(&rmp_to_vec_all_named(&rq)?[..]);

		let (resp_send, resp_recv) = oneshot::channel();
		self.resp_notify_send.send((id, resp_send))?;

		trace!("request: query_send {}, {} bytes", id, bytes.len());
		self.query_send.send((id, prio, bytes))?;

		let resp = resp_recv.await?;

		rmp_serde::decode::from_read_ref::<_, Result<<T as Message>::Response, String>>(&resp[..])?
			.map_err(Error::Remote)
	}
}

impl SendLoop for ClientConn {}

#[async_trait]
impl RecvLoop for ClientConn {
	async fn recv_handler(self: Arc<Self>, id: RequestID, msg: Vec<u8>) {
		self.resp_send
			.send((id, msg))
			.log_err("ClientConn::recv_handler");
	}
}