salbahra / cordova-plugin-networkinterface

Provide a method for Cordova applications to retrieve the device IP Address.
http://albahra.com/projects
72 stars 51 forks source link

networkInterface.getCarrierIPAddress() returns 0.0.0.0 #38

Closed ezcliniq closed 3 years ago

ezcliniq commented 5 years ago

I am trying to log the public IP address of the client device when authenticating, but on carrier mode, I get IP address as 0.0.0.0, is this expected? Also on the wifi, I get the LAN IP and not the public IP.

abhijithp commented 5 years ago

Duplicate of #

MaddHatters commented 4 years ago

Duplicate of #

What is the duplicate issue#?

I also noticed this issue is occurring for getWiFiIPAddress on an iPhone 8+. It is working on Android for me.

sjnaveenkumar commented 4 years ago

@MaddHatters I also face the same issue while getting getWiFiIPAddress. But I can able to fix it by doing some changes in CDVNetworkInterface.m file.

I printed all the interfaces that it is looping in getInterfaceiIP function which the result looks like this

[
  "lo0 :: 127.0.0.1",
  "lo0 :: 0.0.0.0",
  "lo0 :: 0.0.0.0",
  "en0 :: 0.0.0.0",
  "en0 :: 192.168.18.8",
  "en0 :: 0.0.0.0",
  "en0 :: 0.0.0.0",
  "awdl0 :: 0.0.0.0",
  "utun0 :: 0.0.0.0",
  "utun1 :: 0.0.0.0"
]

Where the getInterfaceIP function loops all the en0 entries and return the last value to the callback.

So to fix this add a condition to not to update the address variable when the entry contains 0.0.0.0 value.

example: add ![addr isEqualToString:@"0.0.0.0"] to the if check on getInterfaceIP

if([name isEqualToString:interfaceName] && ![addr isEqualToString:@"0.0.0.0"]) {
     // Get NSString from C String
     address = addr;
     subnet = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
}

@ezcliniq for your case, I think this would be the root cause.

MaddHatters commented 4 years ago

@sjnaveenkumar Interesting, and thanks for sharing. The issue on my end was the conversion of IPv6 to IPv4. The following PR was recently merged to handle that case.

https://github.com/salbahra/cordova-plugin-networkinterface/pull/45

angelofan commented 4 years ago

When will this fix be updated to the npm package?

salbahra commented 3 years ago

I have added the check for 0.0.0.0 as suggested here and elsewhere. I will update the package soon with all the latest changes.

Thank you!