diff options
Diffstat (limited to 'lib/net/tcpconn.ex')
-rw-r--r-- | lib/net/tcpconn.ex | 9 |
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 |