Why does inet:listen_options(....) return {0,0,0,0}?

Hello, Iam trying to understand how Erlang get my ip address, so tracking gen_tcp:listen(....) results in the function inet:listen_options(....) that extracts the ip address of the host.

But when I tried this function manually, it returns a map with an ip address {0,0,0,0}, so : why I had this address ? and how to set my ip address correctly ?

I noted that inet:listen_options(....) uses application:get_env(kernel,inet_default_listen_options) so I thinked that maybe should set it at start time of the node with the -kernel option but in fact I don’t know if that’s true and I don’t know how to do that so any help and thank you.

2 Likes

0.0.0.0 is a special value (INADDR_ANY) that means your server will accept connections from any network interface. Typically when using gen_tcp:listen you’ll use 0.0.0.0 if you want to accept connections from the outside, and 127.0.0.1 if you only want to allow connections from the same local machine (e.g. for development / testing).

4 Likes

Thank you @domi , after some researches I found that if the 0.0.0.0 ip address is used as parameter in the bind(SockFd,....) system call function, then the SockFd socket will be bound with all ip addresses available in that machine (all network interfaces as you said) and this is the only way to bind a socket with 2 or more ip addresses.

but if I have 2 network interfaces that are connected with 2 ip addresses and just one of these addresses is Static, “Hosted and Published” (with the Hostname) by DNS Servers and I want to bind my sockets with this Public ip address and NOT bind it with the Second address : what should I do ?

2 Likes

You can pass the ip argument to gen_tcp:listen, unless I misunderstood the question?

3 Likes

Yes I can set the address in the given Options, but I search to set it to be the DEFAULT address that will be used in all Applications and all Services for that Node, in other terms I search how to replace the DEFAULT 0.0.0.0 by an other DEFAULT address like my own address or the Loopback address (127.0.0.1).

I tracked the gen_tcp:listen call and the 0.0.0.0 address is extracted by the function inet:listen_options(....) and exactly by the call application:get_env(kernel,inet_default_listen_options) so I searched about and I find that this variable can be modified at start time by using the -kernel option like this example :

$ erl -sname test -kernel \
inet_default_connect_options '[{delay_send,true}]' \
inet_default_listen_options '[{delay_send,true}]'

so I think that the ip address given in this Options List will be the default ip, I didn’t try that but I will do,
Thank you for help @domi.

1 Like