<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">I believe it’s a BSD thing rather than Linux - but we’ve had that in our low level UDP code for such a long time that tbh I’ve forgotten the details!</div> <br> <div id="bloop_sign_1460489101253592064" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br><br>Sent with Airmail</div></div> <br><p class="airmail_on">On 12 April 2016 at 20:19:43, Peter Morgan (<a href="mailto:peter.james.morgan@gmail.com">peter.james.morgan@gmail.com</a>) wrote:</p> <blockquote type="cite" class="clean_bq"><span><div dir="auto"><div></div><div>
<title></title>
<div>Thanks! So does Linux do SO_REUSEPORT by default?</div>
<div><br></div>
<div><br></div>
<div>On 12 Apr 2016, at 20:00, <a href="mailto:steve@srstrong.com">steve@srstrong.com</a> wrote:<br>
<br></div>
<blockquote type="cite">
<div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
It’s setting the SO_REUSEPORT option - <a href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/setsockopt.2.html">https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/setsockopt.2.html<br>
</a></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<a href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/setsockopt.2.html">
<br></a></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<a href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/setsockopt.2.html">
1</a>6#ffff == SOL_SOCKET</div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
16#0200 == SO_REUSEPORT</div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
1:32 == true </div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<br></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
Some -defines would probably have made it more readable :)</div>
<div id="bloop_sign_1460487517152286976" class="bloop_sign">
<div style="font-family:helvetica,arial;font-size:13px">
-- <br>
<br>
Sent with Airmail</div>
</div>
<br>
<p class="airmail_on">On 12 April 2016 at 19:54:27, Peter Morgan
(<a href="mailto:peter.james.morgan@gmail.com">peter.james.morgan@gmail.com</a>)
wrote:</p>
<blockquote type="cite" class="clean_bq">
<div dir="auto">
<div>
<div><span>Hi Steve -</span></div>
<div><span><br></span></div>
<div><span>Whoa! What magic is that doing?</span></div>
<div><span><br></span></div>
<div><span>Thanks</span></div>
<div><span>Peter</span></div>
<div><span><br>
On 12 Apr 2016, at 19:43, <a href="mailto:steve@srstrong.com">steve@srstrong.com</a> wrote:<br>
<br></span></div>
<blockquote type="cite">
<div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span>Hi Peter,</span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span><br></span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span>On OSX, try adding:</span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span><br></span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span>{ raw, 16#ffff, 16#0200, <<1:32/native>>
}</span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span><br></span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span>into the options list on the gen_udp open…</span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span><br></span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span>Cheers,</span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span><br></span></div>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span>Steve</span></div>
<span><br></span>
<div id="bloop_sign_1460486536612421120" class="bloop_sign">
<div style="font-family:helvetica,arial;font-size:13px">
<span>-- <br>
<br>
Sent with Airmail</span></div>
</div>
<span><br></span>
<p class="airmail_on"><span>On 12 April 2016 at 19:41:00, Peter
Morgan (<a href="mailto:peter.james.morgan@gmail.com">peter.james.morgan@gmail.com</a>)
wrote:</span></p>
<blockquote type="cite" class="clean_bq">
<div>
<div><span><span>Hello -<br>
<br>
It appears that OSX doesn’t like to reuse a port on gen_udp:open/2
when sending, whereas Linux appears to be quite happy doing
so:<br>
<br>
I had for both sender and receiver:<br>
<br>
gen_udp:open(5353, [{mode, binary},<br>
{reuseaddr, true},<br>
{ip, {224, 0, 0, 251}},<br>
{multicast_ttl, 4},<br>
{multicast_loop, true},<br>
{broadcast, true},<br>
{add_membership, {{224, 0, 0, 251}, {0, 0, 0, 0}}},<br>
{active, once}]).<br>
<br>
Where 5353 is the port for mDNS - already being used by mDNS
daemons on both Linux and OSX. The above works fine for Linux (to
send and receive), whereas on OSX it only appears to work for
receiving.<br>
<br>
To make it work on OSX, I need to call gen_udp:open/2 slightly
differently depending on whether I want to call gen_udp:send/4 on
the returned socket:<br>
<br>
{ok, Sender} = gen_udp:open(0, [binary]).<br>
gen_udp:send(Sender, {224, 0, 0, 251}, 5353, <<“hello
world”>>).<br>
<br>
Note the “0” as the Port parameter to gen_udp:open/2 above.<br>
<br>
Whereas the receiver must be (as originally):<br>
<br>
{ok, Sender} = gen_udp:open(5353, [{mode, binary},<br>
{reuseaddr, true},<br>
{ip, {224, 0, 0, 251}},<br>
{multicast_ttl, 4},<br>
{multicast_loop, true},<br>
{broadcast, true},<br>
{add_membership, {{224, 0, 0, 251}, {0, 0, 0, 0}}},<br>
{active, once}]).<br>
<br>
On Linux, it seems quite happy for the sender and receiver to use
the {reuseaddr, true} form immediately above and port 5353 as the
parameter to gen_udp:open/2. OSX is OK until gen_udp:send/4 is
called it responds with: {error, eaddrnotavail}.<br>
<br>
The updated code is <a href="https://github.com/shortishly/mdns/blob/develop/src/mdns_udp.erl">https://github.com/shortishly/mdns/blob/develop/src/mdns_udp.erl</a>
and now works for both OSX and Linux.<br>
<br>
<br>
Thanks,<br>
Peter.<br>
<br>
<br>
<br>
<br>
<br>
<br>
> On 12 Apr 2016, at 17:06, Peter Morgan <<a href="mailto:peter.james.morgan@gmail.com">peter.james.morgan@gmail.com</a>>
wrote:<br>
><br>
> Hi Torben, Max,<br>
><br>
> On OSX I can receive multicast traffic using the following
options (the same as Max’s pretty much):<br>
><br>
> [binary,<br>
> inet,<br>
> {reuseaddr, true},<br>
> {ip, MulticastAddress},<br>
> {multicast_ttl, 4},<br>
> {multicast_loop, true},<br>
> {add_membership, {MulticastAddress, {0, 0, 0, 0}}},<br>
> {active, once}].<br>
><br>
> Where MulticastAddress is {224, 0, 0, 251}. I’m using that
address because it is one used by OSX, Linux/everything for
multicast DNS :)<br>
><br>
> On OSX I can happily receive multicast traffic on a gen_udp
socket that is opened with the above parameters.<br>
><br>
> However, on OSX I cannot send to the same socket using:<br>
><br>
> gen_udp:send(Socket, MulticastAddress, Port,
SomeMessage).<br>
><br>
> On Linux (Fedora 23) the above send does work. On OSX I get
{error, eaddrnotavail} on send.<br>
><br>
> I’m sure I have had this working on OSX and Linux together, so
I’m probably doing some rather stupid…<br>
><br>
> OSX is El Capitan (10.11.4 beta). Erlang is 18.3 (brew).<br>
><br>
> I’ve tried replacing the {0, 0, 0, 0} in {add_membership,
{MulticastAddress, {0, 0, 0, 0}}} with IP address of the interface,
but it didn’t make any difference.<br>
><br>
> Thanks,<br>
> Peter.<br>
><br>
><br>
><br>
><br>
>> On 11 Apr 2016, at 16:58, Max Lapshin <<a href="mailto:max.lapshin@gmail.com">max.lapshin@gmail.com</a>>
wrote:<br>
>><br>
>> This is how it is working for me:<br>
>><br>
>> {ok, Addr} = inet_parse:address(Host),<br>
>> Common =
[binary,{reuseaddr,true},{recbuf,2*1024*1024},inet,{ip,Addr},{read_packets,100}],<br>
>> Options = case is_multicast(Addr) of<br>
>> true -><br>
>> Multicast =
[{multicast_ttl,4},{multicast_loop,true},{add_membership,{Addr,GwIP}}],<br>
>> Common ++ Multicast;<br>
>> false -><br>
>> Common<br>
>> end,<br>
>><br>
><br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</span></span></div>
</div>
</blockquote>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</blockquote>
</div></div></span></blockquote></body></html>