Skip to content

Commit fb0e57b

Browse files
author
Daniel W
committed
Merge pull request tungd#58 from glaszig/fix-server
2 parents a32f699 + 87ccd6d commit fb0e57b

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

lib/dns/server.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@ defmodule DNS.Server do
2121
end
2222

2323
def init([port]) do
24-
socket = Socket.UDP.open!(port, as: :binary, mode: :active)
24+
{:ok, socket} = :gen_udp.open(port, [{:active, true}, {:mode, :binary}])
2525
IO.puts("Server listening at #{port}")
2626

2727
# accept_loop(socket, handler)
2828
{:ok, %{port: port, socket: socket}}
2929
end
3030

31-
def handle_info({:udp, client, ip, wtv, data}, state) do
31+
def handle_info({:udp, client, ip, port, data}, state) do
3232
record = DNS.Record.decode(data)
3333
response = handle(record, client)
34-
Socket.Datagram.send!(state.socket, DNS.Record.encode(response), {ip, wtv})
34+
:gen_udp.send(state.socket, convert_address(ip), port, DNS.Record.encode(response))
3535
{:noreply, state}
3636
end
37+
38+
defp convert_address(a) when is_binary(a), do: String.to_charlist(a)
39+
40+
defp convert_address(a), do: a
3741
end
3842
end
3943
end

0 commit comments

Comments
 (0)