[erlang-questions] clarify: SO_REUSEADDR under darwin?

Paul Mineiro paul-trapexit@REDACTED
Thu Nov 8 19:24:32 CET 2007


I ended up doing the following.  It seems dangerous.

--------
% This is correct for darwin and freebsd 4.1, but no guarantees
SoReusePort = { raw, 16#ffff, 16#0200, <<1:32/native>> },
{ ok, Socket } =
  try gen_udp:open (Port, [ SoReusePort | Opts ])
  catch
    exit : badarg -> % perhaps no support for SO_REUSEPORT
      gen_udp:open (Port, Opts)
  end,
--------

The danger arises because 1) I'm counting on the BSD variants using 0x200 for
SO_REUSEPORT (i checked darwin and freebsd 4.1, this is true) and 2) I'm
counting on 0x200 not meaning anything to another nixes (i checked linux,
this is true).

It would be super cool for better portability if

1. soreuseport was supported and ignored as platform appropriate, or
2. there was a (public) way to get a socket that was unbound and then bind
it later.  the problem is i need to set this option before binding.
there is such a way with prim_inet but I thought it was more dangerous
to replicate the guts of inet_udp than to do the above try/catch, or
3. there was a way to specify optional options to gen_(udp|tcp):open
which were allowed to fail.  right now if any return error the entire
open fails.

If somebody has a better way of going about this, I'd love to hear it.

-- p

On Thu, 8 Nov 2007, Paul Mineiro wrote:

> Ok.
>
> This problem arose because a multicast application I'm writing won't let
> two nodes on the same box work under darwin but does under linux.
>
> However this link says that for multicast ports, SO_REUSEADDR is the same
> as SO_REUSEPORT.
>
> http://www.unixguide.net/network/socketfaq/4.11.shtml
>
> So my modified version of test (attached), still fails under darwin and
> succeeds under linux.
>
> Is this a known problem under darwin?
>
> Is there a way to specify SO_REUSEPORT for gen_udp:open ?
>
> Thanks,
>
> -- p
>
> On Thu, 8 Nov 2007, Valentin Micic wrote:
>
> > In your program, you're attempting to use the sampe port while the socket is
> > still active -- SO_REUSEADDR does not serve that purpose.
> > Rather, it means that when owner of the socket no longer needs it (i.e.
> > issue close socket, or terminates), another process may reuse the same port
> > immediately without waiting for TIME_WAIT period to expire.
> >
> > V.
> >
> >
> > ----- Original Message -----
> > From: "Paul Mineiro" <paul-trapexit@REDACTED>
> > To: <erlang-questions@REDACTED>
> > Sent: Thursday, November 08, 2007 6:25 AM
> > Subject: [erlang-questions] clarify: SO_REUSEADDR under darwin?
> >
> >
> > > hi.
> > >
> > > i've attached a very short erlang program which tries to open two udp
> > > ports with reuseaddr option.  it works under linux but fails under darwin.
> > >
> > > is this a known problem?
> > >
> > > i can see the following in the ktrace:
> > >
> > > --------
> > > % ktrace -id -t c erl -eval 'test:test ()' -noshell -noinput -s init stop
> > > --------
> > >
> > > the kdump.out contains:
> > >
> > > --------
> > > 28042 beam     CALL  socket(0x2,0x2,0x11)
> > > 28042 beam     RET   socket 9
> > > 28042 beam     CALL  fcntl(0x9,0x3,0)
> > > 28042 beam     RET   fcntl 2
> > > 28042 beam     CALL  fcntl(0x9,0x4,0x6)
> > > 28042 beam     RET   fcntl 0
> > > 28042 beam     CALL  setsockopt(0x9,0xffff,0x1002,0xbfffe1fc,0x4)
> > > 28042 beam     RET   setsockopt 0
> > > 28042 beam     CALL  setsockopt(0x9,0xffff,0x4,0xbfffe1fc,0x4)
> > > 28042 beam     RET   setsockopt 0
> > > 28042 beam     CALL  bind(0x9,0xbfffe0e0,0x10)
> > > 28042 beam     RET   bind -1 errno 48 Address already in use
> > > --------
> > >
> > > setsockopt(0x9,0xffff,0x4,0xbfffe1fc,0x4) looks right:
> > >
> > > --------
> > > % grep -e SO_REUSEADDR -e SOL_SOCKET /usr/include/sys/socket.h
> > > #define SO_REUSEADDR    0x0004          /* allow local address reuse */
> > > #define SOL_SOCKET      0xffff          /* options for socket level */
> > > --------
> > >
> > > the only thing i can't verify is that 0xbfffe1fc is a pointer to one.
> > >
> > > thanks,
> > >
> > > -- p
> > >
> > > % dpkg -s erlang-otp | grep Version
> > > Version: 11b-5-1
> > > % erl -version
> > > Erlang (ASYNC_THREADS) (BEAM) emulator version 5.5.5
> > >
> > > Optimism is an essential ingredient of innovation. How else can the
> > > individual favor change over security?
> > >
> > >  -- Robert Noyce
> >
> >
> > --------------------------------------------------------------------------------
> >
> >
> > > _______________________________________________
> > > erlang-questions mailing list
> > > erlang-questions@REDACTED
> > > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>
> Optimism is an essential ingredient of innovation. How else can the
> individual favor change over security?
>
>   -- Robert Noyce

Optimism is an essential ingredient of innovation. How else can the
individual favor change over security?

  -- Robert Noyce



More information about the erlang-questions mailing list