Skip to content

Commit

Permalink
Add otpbp_gen_udp with connect/2, send/{3,5}
Browse files Browse the repository at this point in the history
  • Loading branch information
Ledest committed Oct 26, 2023
1 parent 9703023 commit 6beee99
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ Funs = [{application, get_supervisor, 1},
{gen_server, start_monitor, 4},
{gen_statem, enter_loop, 4},
{gen_tcp, connect, 2},
{gen_udp, connect, 2},
{gen_udp, send, 3},
{gen_udp, send, 5},
{http_uri, decode, 1},
{http_uri, encode, 1},
{http_uri, parse, 1},
Expand Down
77 changes: 77 additions & 0 deletions src/otpbp_gen_udp.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
-module(otpbp_gen_udp).

-compile({parse_transform, otpbp_pt}).

-ifndef(HAVE_gen_udp__connect_2).
% OTP 24.3
-export([connect/2]).
-endif.
-ifndef(HAVE_gen_udp__send_3).
% OTP 24.3
-export([send/3]).
-endif.
-ifndef(HAVE_gen_udp__send_5).
% OTP 24.3
-export([send/5]).
-endif.

-ifndef(HAVE_gen_udp__send_3).
-ifdef(HAVE_gen_udp__send_5).
-import(gen_udp, [send/5]).
-endif.
-endif.

-ifndef(HAVE_gen_udp__connect_2).
connect(S, SockAddr) when is_port(S), is_map(SockAddr) ->
case inet_db:lookup_socket(S) of
{ok, Mod} -> Mod:connect(S, inet:ensure_sockaddr(SockAddr));
Error -> Error
end.
-endif.

-define(module_socket(Handler, Handle), {'$inet', Handler, Handle}).

-ifndef(HAVE_gen_udp__send_3).
send(?module_socket(GenUdpMod, _) = S, Destination, Packet) when is_atom(GenUdpMod) ->
GenUdpMod:?FUNCTION_NAME(S, Destination, Packet);
send(Socket, Destination, Packet) -> send(Socket, Destination, [], Packet).

send(S, #{family := Fam} = Destination, AncData, Packet)
when is_port(S), Fam =:= inet orelse Fam =:= inet6, is_list(AncData) ->
case inet_db:lookup_socket(S) of
{ok, Mod} -> Mod:send(S, inet:ensure_sockaddr(Destination), AncData, Packet);
Error -> Error
end;
send(S, {_, _} = Destination, 0, Packet) when is_port(S) ->
case inet_db:lookup_socket(S) of
{ok, Mod} -> Mod:send(S, Destination, [], Packet);
Error -> Error
end;
send(S, {_, _}, PortZero, _Packet) when is_port(S), is_integer(PortZero) -> {error, einval};
send(S, {_, _} = Destination, AncData, Packet) when is_port(S), is_list(AncData) ->
case inet_db:lookup_socket(S) of
{ok, Mod} -> Mod:send(S, Destination, AncData, Packet);
Error -> Error
end;
send(S, Host, Port, Packet) when is_port(S) -> send(S, Host, Port, [], Packet).
-endif.

-ifndef(HAVE_gen_udp__send_5).
send(?module_socket(GenUdpMod, _) = S, Host, Port, AncData, Packet) when is_atom(GenUdpMod) ->
GenUdpMod:?FUNCTION_NAME(S, Host, Port, AncData, Packet);
send(S, Host, Port, AncData, Packet) when is_port(S), is_list(AncData) ->
case inet_db:lookup_socket(S) of
{ok, Mod} ->
case Mod:getaddr(Host) of
{ok, IP} ->
case Mod:getserv(Port) of
{ok, P} -> Mod:send(S, {IP, P}, AncData, Packet);
{error, einval} -> exit(badarg);
Error -> Error
end;
{error, einval} -> exit(badarg);
Error -> Error
end;
Error -> Error
end.
-endif.
2 changes: 2 additions & 0 deletions src/otpbp_pt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
{{gen_server, start_monitor, [3, 4]}, otpbp_gen_server},
{{gen_statem, enter_loop, 4}, otpbp_gen_statem},
{{gen_tcp, connect, 2}, otpbp_gen_tcp},
{{gen_udp, connect, 2}, otpbp_gen_udp},
{{gen_udp, send, [3, 5]}, otpbp_gen_udp},
{{inet,
[ensure_sockaddr, gen_tcp_module, gen_udp_module, info,
ipv4_mapped_ipv6_address, is_ip_address, is_ipv4_address, is_ipv6_address],
Expand Down

0 comments on commit 6beee99

Please sign in to comment.