Jayson Vantuyl kagato@REDACTED
Wed Dec 2 09:01:07 CET 2009

Dear Erlangers,

Is this call guaranteed to be stable?

I have a unique problem.  My application is going to be deployed in a range of environments where it is not trivial to know what your IP address is.  It's also critical that I determine the address without generating traffic and in a way that gets its local address, not the address that might be assigned by NAT.  That's largely because its IP address is being used to detect NAT, so I can't just ask a public server.

It turns out that this is a fairly open problem in with sockets, especially if DNS isn't configured just right.

The best option I've found is opening a UDP socket, connecting it to some presumably non-local address, and then asking what the source socket is.  In the Erlang shell, this looks something like this:

> Erlang R13B (erts-5.7.1) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
> Eshell V5.7.1  (abort with ^G)
> 1> {ok,U} = gen_udp:open(0).
> {ok,#Port<0.435>}
> 2> gen_udp:connect(U,"",1234).
> ok
> 3> inet:sockname(U).
> {ok,{{192,168,3,239},59163}}
> 4> 

The catch is that gen_udp:connect/3 isn't documented.  Is there some guarantee that this API will be supported (and hopefully someday documented)?

Also, does anybody have any idea if this works well across Windows versions?


Jayson Vantuyl

More information about the erlang-questions mailing list