aboutsummaryrefslogtreecommitdiff
path: root/lib/net/tcpconn.ex
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-07-03 16:13:54 +0200
committerAlex Auvolat <alex@adnab.me>2018-07-03 16:13:54 +0200
commit66ab427e7ada3e8d9710eb79b1f768a8431b42eb (patch)
treed5a70563113b3cb3a9c164c1bc2cf66229a80c2d /lib/net/tcpconn.ex
parent8f3009715ee9ccdd7ecb54fea1244a32a29b62c0 (diff)
downloadshard-66ab427e7ada3e8d9710eb79b1f768a8431b42eb.tar.gz
shard-66ab427e7ada3e8d9710eb79b1f768a8431b42eb.zip
Add connection manager (to be fully fledged peer database)
Diffstat (limited to 'lib/net/tcpconn.ex')
-rw-r--r--lib/net/tcpconn.ex9
1 files changed, 5 insertions, 4 deletions
diff --git a/lib/net/tcpconn.ex b/lib/net/tcpconn.ex
index 5d6c912..301e931 100644
--- a/lib/net/tcpconn.ex
+++ b/lib/net/tcpconn.ex
@@ -25,11 +25,10 @@ defmodule SNet.TCPConn do
{:ok, challenge} = Salty.Random.buf 32
# Exchange public keys and challenge
- :gen_tcp.send(socket, srv_pkey <> sess_pkey <> challenge)
+ hello = {srv_pkey, sess_pkey, challenge, state.my_port}
+ :gen_tcp.send(socket, :erlang.term_to_binary hello)
{:ok, pkt} = :gen_tcp.recv(socket, 0)
- cli_pkey = binary_part(pkt, 0, Sign.publickeybytes)
- cli_sess_pkey = binary_part(pkt, Sign.publickeybytes, Box.publickeybytes)
- cli_challenge = binary_part(pkt, Sign.publickeybytes + Box.publickeybytes, 32)
+ {cli_pkey, cli_sess_pkey, cli_challenge, his_port} = :erlang.binary_to_term(pkt, [:safe])
# Do challenge and check their challenge
{:ok, cli_challenge_sign} = Sign.sign_detached(cli_challenge, srv_skey)
@@ -54,6 +53,7 @@ defmodule SNet.TCPConn do
addr: addr,
port: port
}
+ GenServer.cast(SNet.Manager, {:peer_up, self(), cli_pkey, addr, his_port})
Logger.info "New peer: #{print_id state} at #{inspect addr}:#{port}"
GenServer.cast(self(), :init_push)
@@ -98,6 +98,7 @@ defmodule SNet.TCPConn do
def handle_info({:tcp_closed, _socket}, state) do
Logger.info "Disconnected: #{print_id state} at #{inspect state.addr}:#{state.port}"
+ GenServer.cast(SNet.Manager, {:peer_down, state.his_pkey, state.addr, state.port})
exit(:normal)
end