msantos / procket

Erlang interface to low level socket operations
http://blog.listincomprehension.com/search/label/procket
BSD 3-Clause "New" or "Revised" License
283 stars 80 forks source link

unable to open second socket on same Port #46

Open geib opened 3 years ago

geib commented 3 years ago

I am working on an app which must join multiple source-specific multicasts, SSM, and all the multicasts use the same group address and port. Only the source address changes. I have tested and this works fine if the group and port are different for each multicast. I have a function:

open_ssm_mc(Group, Port, Source, Interface) ->
    % add_source_membership

    LocalIp = ip_to_binary(Interface),
    GroupIp = ip_to_binary(Group),
    SourceIp = ip_to_binary(Source),
    Bin = << GroupIp/binary, LocalIp/binary, SourceIp/binary >>,

    {ok, Fd} = procket:open(Port, [{protocol, udp}, {type, dgram}, {family, inet}]),
    ok = procket:setsockopt(Fd, 'SOL_SOCKET', 'SO_REUSEADDR',  <<1:32>>),
    ok = procket:setsockopt(Fd, 'SOL_SOCKET', 'SO_REUSEPORT',  <<1:32>>),
    ok = procket:setsockopt(Fd, 0, 39,  Bin),
    % ok = procket:bind(Fd, GroupIp),

    {ok, Fd, Port}.

Where the returned FD is then used in a gen_udp:open() like:

{ok, Fd, Port} = cse_socket:open_ssm_mc(Addr, Port, Source, LAddr),
{ok, Socket} = gen_udp:open(0, [{fd, Fd}, binary, {active, true}, {recbuf, RecBuf}, {buffer, Buffer}, {read_packets, ReadPackets}]),
?LOGIT_INFO("SSM socket open and active", [{fd, Fd}, {socket, Socket}, {port, Port}, {group, Addr}, {source, Source}, {interface, LAddr}])

As I said, only the "Source" changes for each instance, and the second instance fails with {error,eaddrinuse} on the procket:open(Port...) call. I am missing some other option that needs set on the FD, or Socket.??

Thanks, Mark.

msantos commented 3 years ago

@geib Sorry for the delay, didn't get the notification for this issue!

Some hints on debugging: