Skip to content

Commit

Permalink
fix server
Browse files Browse the repository at this point in the history
the elixir socket library was removed in 933c5c4 in favor
of erlang stdlib so gen_udp has to be used in the server as well
instead of Socket.

closes tungd#56
  • Loading branch information
glaszig committed Mar 27, 2023
1 parent a32f699 commit 87ccd6d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/dns/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ defmodule DNS.Server do
end

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

# accept_loop(socket, handler)
{:ok, %{port: port, socket: socket}}
end

def handle_info({:udp, client, ip, wtv, data}, state) do
def handle_info({:udp, client, ip, port, data}, state) do
record = DNS.Record.decode(data)
response = handle(record, client)
Socket.Datagram.send!(state.socket, DNS.Record.encode(response), {ip, wtv})
:gen_udp.send(state.socket, convert_address(ip), port, DNS.Record.encode(response))
{:noreply, state}
end

defp convert_address(a) when is_binary(a), do: String.to_charlist(a)

defp convert_address(a), do: a
end
end
end

0 comments on commit 87ccd6d

Please sign in to comment.