Skip to content

Commit 05e5651

Browse files
committed
Erlang - make UDP sending more robust
Previously the function to send UDP just used gen_udp:send which calls exit if the hostname can't be resolved - killing the process. As the hostname comes from the code, this could easily be set to the wrong value by the user. Therefore, rather than assume it's a valid error and exit as you should in Erlang, we first check the hostname. If it can be resolved we send it (still via gen_udp so some of these checks are duplicated) if not, we write a debug log message and ignore it. This should improve the robustness of the Erlang server in the case of these bad host names.
1 parent 228e9e9 commit 05e5651

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

app/server/erlang/sonic_pi_server/src/pi_server/pi_server_cue.erl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,25 @@ send_forward(Socket, Time, {Host, Port, Bin}) ->
213213
[Time, Now-Time]),
214214
ok.
215215

216-
send_udp(Socket, Host, Port, Bin) ->
217-
gen_udp:send(Socket, Host, Port, Bin),
218-
ok.
216+
send_udp(Socket, Host, Port, Bin)
217+
when is_port(Socket) ->
218+
%% check to see if host is correct and usable
219+
case inet_db:lookup_socket(Socket) of
220+
{ok, Mod} ->
221+
case Mod:getaddr(Host) of
222+
{ok,_} ->
223+
case Mod:getserv(Port) of
224+
{ok,_} ->
225+
debug("Sending UDP to - ~p ~p ~n", [Host, Port]),
226+
gen_udp:send(Socket, Host, Port, Bin);
227+
{error,einval} -> debug("Unable to send UDP - bad hostname (getserv einval): ~p~n", [Host]);
228+
Error -> debug("Unable to send UDP - bad hostname (getserv ~p): ~p~n", [Error, Host])
229+
end;
230+
{error,einval} -> debug("Unable to send UDP - bad hostname (getaddr einval): ~p~n", [Host]);
231+
Error -> debug("Unable to send UDP - bad hostname (getaddr: ~p ): ~p~n", [Error, Host])
232+
end;
233+
Error -> debug("Unable to send UDP - bad socket (~p): ~p~n", [Error, Host])
234+
end.
219235

220236
update_midi_in_ports(CueHost, CuePort, InSocket, Args) ->
221237
Bin = osc:encode(["/midi-ins", "erlang" | Args]),

0 commit comments

Comments
 (0)