www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can i find my LAN IP Address using std.socket?

reply "TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
I'm trying to find my own ip address using std.socket with little 
success. How would i go about doing this? (It should be a 
AddressFamily.INET socket)
Feb 04 2014
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
wrote:
 I'm trying to find my own ip address using std.socket with 
 little success. How would i go about doing this? (It should be 
 a AddressFamily.INET socket)
You can have lot of different local IP addresses on a single machine so question can't be answered properly. However you may use in `Socket.hostName` and resolve it via DNS to find IP machine itself currently consuders as its main pulbic IP.
Feb 04 2014
parent "TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
On Tuesday, 4 February 2014 at 13:13:07 UTC, Dicebot wrote:
 On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
 wrote:
 I'm trying to find my own ip address using std.socket with 
 little success. How would i go about doing this? (It should be 
 a AddressFamily.INET socket)
You can have lot of different local IP addresses on a single machine so question can't be answered properly. However you may use in `Socket.hostName` and resolve it via DNS to find IP machine itself currently consuders as its main pulbic IP.
This works great thanks. I just need any (working) local IP address so getting more then one is not an issue for me.
Feb 04 2014
prev sibling next sibling parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
wrote:
 I'm trying to find my own ip address using std.socket with 
 little success. How would i go about doing this? (It should be 
 a AddressFamily.INET socket)
Create a connection to another LAN machine with a known address (e.g. gateway or router), then use Socket's localAddress property to get your IP. You cannot really do that before establishing a connection, as Dicebot already mentioned.
Feb 04 2014
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Tuesday, 4 February 2014 at 13:21:54 UTC, Stanislav Blinov 
wrote:
 Create a connection to another LAN machine with a known address 
 (e.g. gateway or router), then use Socket's localAddress 
 property to get your IP.
Worth noting that this solution is not reliable in general either because your server can possibly have complicated routing configurations that will make, for example, LAN destination packets go via different network interface than WAN destination ones. It is probably better to tell what high-level problem you are trying to solve to find most useful compromise.
Feb 04 2014
parent reply "TheFlyingFiddle" <kurtyan student.chalmers.se> writes:
On Tuesday, 4 February 2014 at 13:24:59 UTC, Dicebot wrote:
 On Tuesday, 4 February 2014 at 13:21:54 UTC, Stanislav Blinov 
 wrote:
Worth noting that this solution is not reliable in general 
either because your server can possibly have complicated routing 
configurations that will make, for example, LAN destination 
packets go via different network interface than WAN destination 
ones.

 It is probably better to tell what high-level problem you are 
 trying to solve to find most useful compromise.
I'm setting up a simple local network enabling me to connect phones to the computer through the local wi-fi. The simplest way i could think of to make this work without relying on an external server was to simply broadcast the ip and port to all machines in the network.(Btw by server i mean my / my project groups windows boxes). So well the problem is that i need a way for the phones to find running servers on the LAN.
Feb 04 2014
parent "Dicebot" <public dicebot.lv> writes:
On Tuesday, 4 February 2014 at 20:19:14 UTC, TheFlyingFiddle 
wrote:
 I'm setting up a simple local network enabling me to connect 
 phones to the computer through the local wi-fi. The simplest 
 way i could think of to make this work without relying on an 
 external server was to simply broadcast the ip and port to all 
 machines in the network.(Btw by server i mean my / my project 
 groups windows boxes).

 So well the problem is that i need a way for the phones to find 
 running servers on the LAN.
I think it is close to impossible to do in portable way. Most reliable approach is to get list of all configured network interfaces via posix functions (or via `system` call as least resort), filter out "lo" and broadcast message for every such interface. I think you can also filter only wireless interfaces that way relatively easily too.
Feb 04 2014
prev sibling parent reply "TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
On Tuesday, 4 February 2014 at 13:21:54 UTC, Stanislav Blinov 
wrote:
 On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
 wrote:
 I'm trying to find my own ip address using std.socket with 
 little success. How would i go about doing this? (It should be 
 a AddressFamily.INET socket)
Create a connection to another LAN machine with a known address (e.g. gateway or router), then use Socket's localAddress property to get your IP. You cannot really do that before establishing a connection, as Dicebot already mentioned.
Problem is that i don't know in what local network the server will be running, so this is unfortunatly not an option for me.
Feb 04 2014
parent "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 4 February 2014 at 13:31:27 UTC, TheFlyingFiddle 
wrote:

 Problem is that i don't know in what local network the server 
 will be running, so this is unfortunatly not an option for me.
But if that's the case, the hostname solution may as well just give you your loopback address. :)
Feb 04 2014
prev sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
wrote:
 I'm trying to find my own ip address using std.socket with 
 little success. How would i go about doing this? (It should be 
 a AddressFamily.INET socket)
This program will print all of your computer's IP addresses: import std.socket; import std.stdio; void main() { foreach (addr; getAddress(Socket.hostName)) writeln(addr.toAddrString()); } This includes IPv6 addresses. You can filter the address family by checking addr's addressFamily property.
Feb 04 2014
parent reply "Craig Dillabaugh" <cdillaba cg.scs.carleton.ca> writes:
On Tuesday, 4 February 2014 at 15:48:50 UTC, Vladimir Panteleev 
wrote:
 On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
 wrote:
 I'm trying to find my own ip address using std.socket with 
 little success. How would i go about doing this? (It should be 
 a AddressFamily.INET socket)
This program will print all of your computer's IP addresses: import std.socket; import std.stdio; void main() { foreach (addr; getAddress(Socket.hostName)) writeln(addr.toAddrString()); } This includes IPv6 addresses. You can filter the address family by checking addr's addressFamily property.
I am a bit lost in anything networking related, so I ran this on my machine just for fun and it printed: 127.0.0.1 127.0.0.1 127.0.0.1 which based on my understanding is the local loopback address (I am not completely, 100% ignorant). However if I run /sbin/ifconfig I get: enp7s0 Link encap:Ethernet HWaddr 50:E5:49:9B:29:49 inet addr:10.1.101.52 Bcast:10.1.101.255 Mask:255.255.255.0 inet6 addr: fe80::52e5:49ff:fe9b:2949/64 Scope:Link lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host This computer is on a network with dynamically assigned IP address (DHCP). So shouldn't the 10.1.101.52 address have been reported?
Feb 04 2014
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh 
wrote:
 However if I run /sbin/ifconfig I get:

 enp7s0    Link encap:Ethernet  HWaddr 50:E5:49:9B:29:49
           inet addr:10.1.101.52  Bcast:10.1.101.255  
 Mask:255.255.255.0
           inet6 addr: fe80::52e5:49ff:fe9b:2949/64 Scope:Link

 lo        Link encap:Local Loopback
           inet addr:127.0.0.1  Mask:255.0.0.0
           inet6 addr: ::1/128 Scope:Host

 This computer is on a network with dynamically assigned IP 
 address (DHCP).
 So shouldn't the 10.1.101.52 address have been reported?
It results in all addresses you hostname resolvs to. On all desktop linux machines /etc/hosts is configured to resolve hostname to "localhost" by default. On servers it usually resolves to externally accessible one.
Feb 04 2014
next sibling parent "Craig Dillabaugh" <cdillaba cg.scs.carleton.ca> writes:
On Tuesday, 4 February 2014 at 16:13:33 UTC, Dicebot wrote:
 On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh 
 wrote:
 However if I run /sbin/ifconfig I get:

 enp7s0    Link encap:Ethernet  HWaddr 50:E5:49:9B:29:49
          inet addr:10.1.101.52  Bcast:10.1.101.255  
 Mask:255.255.255.0
          inet6 addr: fe80::52e5:49ff:fe9b:2949/64 Scope:Link

 lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host

 This computer is on a network with dynamically assigned IP 
 address (DHCP).
 So shouldn't the 10.1.101.52 address have been reported?
It results in all addresses you hostname resolvs to. On all desktop linux machines /etc/hosts is configured to resolve hostname to "localhost" by default. On servers it usually resolves to externally accessible one.
Thanks.
Feb 04 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Tuesday, 4 February 2014 at 16:13:33 UTC, Dicebot wrote:
 It results in all addresses you hostname resolvs to. On all 
 desktop linux machines /etc/hosts is configured to resolve 
 hostname to "localhost" by default. On servers it usually 
 resolves to externally accessible one.
Update: I have just checked and this is actually distro-specific right now. My Arch box does not have that /etc/hosts entry and resolves host name to whatever first configured network interface has. Ubuntu does have explicit host name entry in /etc/hosts which resolves to localhost.
Feb 04 2014
prev sibling parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh 
wrote:

 This computer is on a network with dynamically assigned IP 
 address (DHCP).
 So shouldn't the 10.1.101.52 address have been reported?
Nope. In out-of-the-box simple network setups (i.e. home network in the form PC/laptop -> router -> internet) the address resolution won't go further than your hosts file, which in turn will always give you back the loopback address (more specifically, the address that is specified for the hostname in aforementioned file). That's why both I and Dicebot mentioned there isn't any real way to query your local addresses without any live connection: only when a socket has a connection within a particular network can you tell your own IP address in that network. The address itself would depend on the network setup, your local routing configuration, etc. Your machine can have several network adaptors (ethernet boards, Wi-Fi, etc.), each configured with (numerous) routing setups, plus the routers they're connected to have their own routing setups... This can go on and on.
Feb 04 2014
parent Johannes Pfau <nospam example.com> writes:
Am Tue, 04 Feb 2014 16:19:08 +0000
schrieb "Stanislav Blinov" <stanislav.blinov gmail.com>:

 On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh 
 wrote:
 
 This computer is on a network with dynamically assigned IP 
 address (DHCP).
 So shouldn't the 10.1.101.52 address have been reported?
Nope. In out-of-the-box simple network setups (i.e. home network in the form PC/laptop -> router -> internet) the address resolution won't go further than your hosts file, which in turn will always give you back the loopback address (more specifically, the address that is specified for the hostname in aforementioned file). That's why both I and Dicebot mentioned there isn't any real way to query your local addresses without any live connection: only when a socket has a connection within a particular network can you tell your own IP address in that network. The address itself would depend on the network setup, your local routing configuration, etc. Your machine can have several network adaptors (ethernet boards, Wi-Fi, etc.), each configured with (numerous) routing setups, plus the routers they're connected to have their own routing setups... This can go on and on.
As a last resort there are always OS specific APIs to iterate network interfaces. For example, for linux: http://man7.org/linux/man-pages/man3/getifaddrs.3.html (Of course this doesn't tell you at all whether a local IP-address is actually routable. And you probably have to figure out if you got a link local / ULA IPv6 or a global one)
Feb 04 2014