[erlang-questions] Problems with gen_udp on multi-homed hosts

Michael Santos michael.santos@REDACTED
Tue Nov 16 17:48:21 CET 2010

On Tue, Nov 16, 2010 at 10:31:47AM -0500, Gregory Haskins wrote:
> Hi All,
> I am trying to develop a custom DHCP server against 14B on that is
> sitting on a multi-homed linux host.  I only want the server to
> listen/respond on one of these interfaces, but I am having trouble
> getting gen_udp to behave in a way I would expect.
> My system has the following interfaces:
> 3> inet:getiflist().
> {ok,["lo","br0","virbr0"]}
> 4> inet:ifget("virbr0", [addr]).
> {ok,[{addr,{192,168,122,1}}]}
> 5> inet:ifget("br0", [addr]).
> {ok,[{addr,{151,155,230,24}}]}
> and I am interested in packets only on
> So if I gen_udp:open() _without_ {ip, ?ADDR} set, I can see the
> broadcasts coming from (as well as from the other
> interfaces) as I would expect.  However, the {udp, Socket, ..} message I
> get has a inet:sockname() of {}, and if I try to reply with a
> broadcast of {} to the same socket, it goes out on the
> "br0" interface.

Does it work if you send the packet out using a more specific broadcast
address? e.g.,

> So the obvious solution to me after googling was to use the {ip,
> {}} option on the open.  The open() call succeeds with this
> option set, but I never see any packets (even though I have sniffed the
> line and they are indeed being sent.  As a sanity check, I also set a
> bogus IP in the open(), and it does indeed exception out, so it seems
> like it is taking the option I set.

Not sure why this isn't working, maybe the packet is still going out
your default interface.

> I am running out of ideas outside of abandoning gen_udp() in favor of a
> ports driver, and/or not writing the DHCP portion of my code in Erlang.
>  Neither is particularly attractive, especially given that this seems
> like a fairly straight forward sockets app and I would think it could be
> made to work.

On Linux, you can try binding the socket to an interface [1]. klaar added
support to do this for his quite interesting dhcp server, edhcpd [2].

[1] https://github.com/msantos/procket
[2] https://github.com/klaar/edhcpd

More information about the erlang-questions mailing list