Open.NAT icon indicating copy to clipboard operation
Open.NAT copied to clipboard

Find Device's Local Ip Address

Open p30arena opened this issue 10 years ago • 8 comments

Hi, is it possible to determine the device's local IP? (not ext) I want to compare the default gateway to device's IP address to see if it is the active connection for internet.

p30arena avatar Mar 22 '16 08:03 p30arena

@p30arena that is not possible with the library as it is. However, you modify the source code to publish the LocalAddress that is in the UpnpNatDeviceInfo class (if you use upnp of course).

https://github.com/lontivero/Open.NAT/blob/master/Open.Nat/Upnp/UpnpNatDeviceInfo.cs.

lontivero avatar Mar 22 '16 13:03 lontivero

Does the broadcast message go over all available networks? If I'm not mistaken I can remember that Windows doesn't broadcast on all interfaces, it just does it on the default interface. If that's true, then only the UPNP device on the default network will be discovered and my problem is resolved.

It seems you use multicast address "239.255.255.250" instead of global broadcast address "255.255.255.255". I don't know if that's True for multicast or not.

A Link for myself: https://en.wikipedia.org/wiki/Simple_Service_Discovery_Protocol

p30arena avatar Mar 22 '16 20:03 p30arena

https://social.technet.microsoft.com/Forums/windows/en-US/72e7387a-9f2c-4bf4-a004-c89ddde1c8aa/how-to-fix-the-global-broadcast-address-255255255255-behavior-on-windows?forum=w7itpronetworking

  • Windows 7 only sends broadcast packets to one interface. You can choose which one, but that's not the point here.
  • Windows XP sends broadcast packets to all interfaces, but it only sends them correctly to one interface, which in practice is equivalent to the Windows 7 behavior.

p30arena avatar Mar 22 '16 20:03 p30arena

OK, I read your code, you send the discovery message over all of the interfaces.

I think it's better to edit this file, so only the main interface is involved. what do you think?

https://github.com/lontivero/Open.NAT/blob/02419cfea2a3b923bb9efd3621a73246c70e128d/Open.Nat/Utils/IPAddressesProvider.cs

p30arena avatar Mar 22 '16 20:03 p30arena

I'm not sure i am understanding. Do you want to modify the code in your local copy only for you or are you suggesting a modification in the library for the next version?

In case you talk about a modification in the library to be released in the next version then, could you detail a bit more why that is a good idea in your opinion?

Btw, thank you for the links, they are very informative.

lontivero avatar Mar 23 '16 02:03 lontivero

I was talking about my local copy, but for the next version, I'm sure it will be great to expose more info on NatDevice class, like device's IP Address, and so on.

plus better error codes using enums.

p30arena avatar Mar 23 '16 04:03 p30arena

Ok, perfect. I take note. Thank you

lontivero avatar Mar 23 '16 12:03 lontivero

I Absolutelly agree, having the device local ip is essential to map routes that last for some time... To be more specific it is usefull when we want to write some error handling, I usually create routes that lasts forever (since i can't extend their duration) and i delete them when the application closes or i don't need them anymore, but i can't prevent the user from shutting down wifi, or unconnect the cable, so sometimes the route stays there, so i need to check if i have a route with same description same port and same local ip address ... Also i can't assume next time i'll connect to that modem/router my ip will be the same... so i need to delete old routes etc. More than the device local ip address what is really useful is the machine local ip connected to that device, because we usualy have more than one network card. I saw that Open.NAT have all this informations but they are kept private, i use reflection to get them, not a good approach

TrabacchinLuigi avatar Oct 19 '17 13:10 TrabacchinLuigi