Getting IP, Subnet Mask, DNS and Default Gateway

Status
Not open for further replies.

DKCERT

Member
Licensed User
Longtime User
Hey all,

I would like to get all the basic network settings for a given device.

The IP addy is easy: ServerSocket.GetMyIp. But how do I get the Subnet mask, DNS-server(s) and the Default Gateway?

The normal Default Gateway can be calculated by using the IP and Subnet mask - but in some scenarios it can be placed strangely in the ip-scope. That's why I would like to get the assigned one.

So is there a way to get this information? Maybe using the reflection lib? or?

Thanks in advance.
 
Last edited:

DKCERT

Member
Licensed User
Longtime User
Hi Erel,

Strange that such basic network info isn't available??

Anyway, I made this sub (dirty code) to get the info. I have tested on 4 different Android versions (not rooted). I would appreciate if you guys could report if it works on your devices as well (brand + android version)

B4X:
Sub GetDhcpInfo As String
   ' Msgbox(GetDhcpInfo,"DHCP Info")
   Dim i As Int
   Dim P As Phone
   Dim sb As StringBuilder
   Dim PropData() As String
   Dim oStr As String
   Dim sWifi As String
   
   sb.Initialize
   P.Shell("getprop", Null, sb, Null)
   PropData = Regex.Split(CRLF,sb.ToString)
   
   ' What is the Wifi Interface called?
   For i = 0 To PropData.Length - 1
      If PropData(i).Contains("[wifi.interface]") = True Then
         sWifi = PropData(i)
         sWifi = sWifi.Replace("[","")
         sWifi = sWifi.Replace("]","")
         sWifi = sWifi.SubString(sWifi.IndexOf(" ")).Trim
         Exit
      End If
   Next
   
   ' Get the ip, subnet mask, gateway, dns1 and dns2 information
   For i = 0 To PropData.Length - 1
      If PropData(i).Contains("[dhcp." & sWifi & ".ipaddress]") Then oStr = oStr & PropData(i) & CRLF
      If PropData(i).Contains("[dhcp." & sWifi & ".mask]") Then oStr = oStr & PropData(i) & CRLF
      If PropData(i).Contains("[dhcp." & sWifi & ".gateway]") Then oStr = oStr & PropData(i) & CRLF
      If PropData(i).Contains("[dhcp." & sWifi & ".dns1]") Then oStr = oStr & PropData(i) & CRLF
      If PropData(i).Contains("[dhcp." & sWifi & ".dns2]") Then oStr = oStr & PropData(i) & CRLF
   Next

   ' Remove junk
   oStr = oStr.Replace("[","")
   oStr = oStr.Replace("]","")
   oStr = oStr.Replace("dhcp." & sWifi & ".","")
   
   Return sWifi & CRLF & oStr
End Sub
 
Last edited:
Upvote 0

DKCERT

Member
Licensed User
Longtime User
Thank you NJDude :)

To take my own medicine I have tested the code on:

Model: Samsung S4 Mini (I9195)
Android v/4.2.2
SDK Level 17
Status: Works

Model: HTC Desire
Android v/2.2.2
SDK Level 8
Status: Works

Model: HTC Desire S
Android v/2.3.5
SDK Level 10
Status: Works

Model: DEM752HCF
Android v/4.0.3
SDK Level 15
Status: Works

Model: Ricomagic MK802IIIS
Android v/4.1.1
SDK Level 16
Status: Works

Model: Huawei U8185
Android v/2.3.6
SDK Level 10
Status: Works

B4X:
Sub GetPropInfo As String
   Dim i As Int
   Dim P As Phone
   Dim sb As StringBuilder
   Dim PropData() As String
   Dim sWifi, sModel, sSdk, sVersion, sIP, sMask, sGate, sD1, sD2 As String
  
   sb.Initialize
   P.Shell("getprop", Null, sb, Null)
   PropData = Regex.Split(CRLF,sb.ToString)
  
   For i = 0 To PropData.Length - 1

      ' What is the Wifi Interface called?
      If PropData(i).Contains("[wifi.interface]") Then
         sWifi = PropData(i)
         sWifi = sWifi.Replace("[","")
         sWifi = sWifi.Replace("]","")
         sWifi = sWifi.SubString(sWifi.IndexOf(" ")).Trim
         Exit
      End If
     
   Next

   If sWifi.Length > 0 Then

      ' Get the model, android version, sdk version, ip, subnet mask, gateway, dns1 and dns2
      For i = 0 To PropData.Length - 1

         If PropData(i).Contains("[ro.product.model]") Then
            sModel = PropData(i)
            sModel = sModel.Replace("[","")
            sModel = sModel.Replace("]","")
            sModel = sModel.SubString(sModel.IndexOf(" ")).Trim
         End If
        
         If PropData(i).Contains("[ro.build.version.sdk]") Then
            sSdk = PropData(i)
            sSdk = sSdk.Replace("[","")
            sSdk = sSdk.Replace("]","")
            sSdk = sSdk.SubString(sSdk.IndexOf(" ")).Trim
         End If
        
         If PropData(i).Contains("[ro.build.version.release]") Then
            sVersion = PropData(i)
            sVersion = sVersion.Replace("[","")
            sVersion = sVersion.Replace("]","")
            sVersion = sVersion.SubString(sVersion.IndexOf(" ")).Trim
         End If
        
         ' *******************************************************************
        
         If PropData(i).Contains("[dhcp." & sWifi & ".ipaddress]") Then
            sIP = PropData(i)
            sIP = sIP.Replace("[","")
            sIP = sIP.Replace("]","")
            sIP = sIP.SubString(sIP.IndexOf(" ")).Trim
         End If
        
         If PropData(i).Contains("[dhcp." & sWifi & ".mask]") Then
            sMask = PropData(i)
            sMask = sMask.Replace("[","")
            sMask = sMask.Replace("]","")
            sMask = sMask.SubString(sMask.IndexOf(" ")).Trim
         End If
        
         If PropData(i).Contains("[dhcp." & sWifi & ".gateway]") Then
            sGate = PropData(i)
            sGate = sGate.Replace("[","")
            sGate = sGate.Replace("]","")
            sGate = sGate.SubString(sGate.IndexOf(" ")).Trim
         End If
        
         If PropData(i).Contains("[dhcp." & sWifi & ".dns1]") Then
            sD1 = PropData(i)
            sD1 = sD1.Replace("[","")
            sD1 = sD1.Replace("]","")
            sD1 = sD1.SubString(sD1.IndexOf(" ")).Trim
         End If
        
         If PropData(i).Contains("[dhcp." & sWifi & ".dns2]") Then
            sD2 = PropData(i)
            sD2 = sD2.Replace("[","")
            sD2 = sD2.Replace("]","")
            sD2 = sD2.SubString(sD2.IndexOf(" ")).Trim
         End If

      Next
     
      Return   "Model: " & sModel & CRLF & _
            "Android v/ " & sVersion & CRLF & _
            "SDK v/ " & sSdk & CRLF & CRLF & _
            "Wifi Interface: " & sWifi & CRLF & _
            "IP Address: " & sIP & CRLF & _
            "Subnet Mask: " & sMask & CRLF & _
            "Default Gateway: " & sGate & CRLF & _
            "DNS #1: " & sD1 & CRLF & _
            "DNS #2: " & sD2 & CRLF

   Else
      Return "Wifi Interface not found."
   End If
  
End Sub

Msgbox(GetPropInfo,"DHCP Info")
 
Last edited:
Upvote 0

DKCERT

Member
Licensed User
Longtime User
Minor update to calculate the broadcast address. Still semi-messy code, but it works :)

B4X:
Sub GetPropInfo As String
   ' Libs: Core (v/2.47), Phone (v/2.01)
   Dim P As Phone
   Dim sb As StringBuilder
   Dim i, iNetMask, iIP As Int
   Dim PropData(), sCurrentIP(), sNetMask() As String
   Dim sWifi, sModel, sSdk, sVersion, sIP, sMask, sGate, sD1, sD2, sBCAddr, sTmp As String
   
   ProgressDialogShow("Please wait")
   
   sb.Initialize
   P.Shell("getprop", Null, sb, Null)
   PropData = Regex.Split(CRLF,sb.ToString)
   
   For i = 0 To PropData.Length - 1
      ' What is the Wifi Interface called?
      If PropData(i).Contains("[wifi.interface]") Then
         sWifi = PropData(i)
         sWifi = sWifi.Replace("[","")
         sWifi = sWifi.Replace("]","")
         sWifi = sWifi.SubString(sWifi.IndexOf(" ")).Trim
         Exit
      End If
   Next

   If sWifi.Length > 0 Then

      For i = 0 To PropData.Length - 1

         If PropData(i).Contains("[ro.product.model]") Then
            sModel = PropData(i)
            sModel = sModel.Replace("[","")
            sModel = sModel.Replace("]","")
            sModel = sModel.SubString(sModel.IndexOf(" ")).Trim
         End If
         
         If PropData(i).Contains("[ro.build.version.sdk]") Then
            sSdk = PropData(i)
            sSdk = sSdk.Replace("[","")
            sSdk = sSdk.Replace("]","")
            sSdk = sSdk.SubString(sSdk.IndexOf(" ")).Trim
         End If
         
         If PropData(i).Contains("[ro.build.version.release]") Then
            sVersion = PropData(i)
            sVersion = sVersion.Replace("[","")
            sVersion = sVersion.Replace("]","")
            sVersion = sVersion.SubString(sVersion.IndexOf(" ")).Trim
         End If
         
         ' *******************************************************************
         
         If PropData(i).Contains("[dhcp." & sWifi & ".ipaddress]") Then 
            sIP = PropData(i)
            sIP = sIP.Replace("[","")
            sIP = sIP.Replace("]","")
            sIP = sIP.SubString(sIP.IndexOf(" ")).Trim
         End If
         
         If PropData(i).Contains("[dhcp." & sWifi & ".mask]") Then 
            sMask = PropData(i)
            sMask = sMask.Replace("[","")
            sMask = sMask.Replace("]","")
            sMask = sMask.SubString(sMask.IndexOf(" ")).Trim
         End If
         
         If PropData(i).Contains("[dhcp." & sWifi & ".gateway]") Then 
            sGate = PropData(i)
            sGate = sGate.Replace("[","")
            sGate = sGate.Replace("]","")
            sGate = sGate.SubString(sGate.IndexOf(" ")).Trim
         End If
         
         If PropData(i).Contains("[dhcp." & sWifi & ".dns1]") Then 
            sD1 = PropData(i)
            sD1 = sD1.Replace("[","")
            sD1 = sD1.Replace("]","")
            sD1 = sD1.SubString(sD1.IndexOf(" ")).Trim
         End If
         
         If PropData(i).Contains("[dhcp." & sWifi & ".dns2]") Then 
            sD2 = PropData(i)
            sD2 = sD2.Replace("[","")
            sD2 = sD2.Replace("]","")
            sD2 = sD2.SubString(sD2.IndexOf(" ")).Trim
         End If
         
         DoEvents

      Next

      sCurrentIP = Regex.Split("\.",sIP)
      sNetMask = Regex.Split("\.",sMask)
      sBCAddr = ""
      
      For i = 0 To 3
         iIP = Val(sCurrentIP(i).Trim)
         iNetMask = Val(sNetMask(i).Trim)
         sTmp = DecToBin(iNetMask,8)
         sTmp = ReverseBits(sTmp)
         iNetMask = BinToDec(sTmp)
         sBCAddr = sBCAddr & Bit.OR(iIP,iNetMask)
         If i < 3 Then sBCAddr = sBCAddr & "."
      Next
      
      ProgressDialogHide
      
      Return   "Model: " & sModel & CRLF & _
            "Android v/ " & sVersion & CRLF & _
            "SDK v/ " & sSdk & CRLF & CRLF & _
            "Wifi Interface: " & sWifi & CRLF & _
            "IP Address: " & sIP & CRLF & _
            "Subnet Mask: " & sMask & CRLF & _
            "Broadcast Addr.: " & sBCAddr & CRLF & _
            "Default Gateway: " & sGate & CRLF & _
            "DNS #1: " & sD1 & CRLF & _
            "DNS #2: " & sD2 & CRLF
   Else
      Return "Wifi Interface not found."
   End If
   
End Sub

Sub DecToBin(decVal As Int, NumBit As Int) As String 
   Dim bits As Int
   Dim dec2bin As StringBuilder 
   dec2bin.Initialize 
   If NumBit = 0 Then 
       Do While decVal > Power(2,bits)-1
           bits = bits + 1
       Loop
   Else
       bits = NumBit
   End If
   For i = bits-1 To 0 Step -1
       dec2bin.Append((Bit.AND(decVal, Power(2,i)))/Power(2,i))
   Next
   Return dec2bin
End Sub

Sub BinToDec(binVal As String) As Int
   Dim i, v, dec As Int 
   i = binVal.Length
   v = 1
   For pos = i To 1 Step -1
       If binVal.SubString2(pos-1, pos) = "1" Then dec = dec + v
      v = v * 2
   Next 
   Return dec
End Sub

Sub Val(sNum As String) As Int
   If IsNumber(sNum.Trim) Then 
      Return sNum.Trim
   Else
      Return -1
   End If
End Sub

Sub ReverseBits(sBin As String) As String
   Dim i As Int
   Dim sOut As String
   For i = 0 To sBin.Length - 1
      If sBin.CharAt(i) = "0" Then
         sOut = sOut & "1"
      Else If sBin.CharAt(i) = "1" Then
         sOut = sOut & "0"
      End If
   Next
   Return sOut
End Sub
 
Upvote 0

DKCERT

Member
Licensed User
Longtime User
Allow me to contribute to that "mess" :D.

I have added "Lease time" and attached a project for easy handling.

Hehehe.... thank you for adding to the "mess" NJDude :)

Btw: Do you have a Android 3.x to test the code on? I havn't been able to get my hands on one for testing.
 
Upvote 0

EduardoElias

Well-Known Member
Licensed User
Longtime User
It worked!

Model: TA 9107w (toshiba 10 tablet)
Android v/ 4.0.3
SDK v/ 15

Model: GT7220S (genesis 7 tablet)
Android v/4.0.4
SDK v/15

Model: MB525 (motorola cellphone defy)
Android V/ 2.2.1
SDK v/8

Model: generic (this is a cheap chinese slow bad 8 tablet)
Android: v/ 2.2
SDK v/ 8

Very nice stuff, thank you that will help also.

I wonder if you know how to do a uPnP query, I need to find a device on the local network that permit to make port maping for routing access from outside internet to the local device.

Thank You Eduardo
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Minor update to calculate the broadcast address. Still semi-messy code, but it works :)

Just what I need (I think). I am finishing an old WOL (Wake On LAN) project I started to develop more than 2 years ago and which I never finished (like so many others :p )
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I'm using this in order to get the broadcast IP for sending a request to retrieve UPnP devices but I'm not sure if the broadcast IP returned is correct.

Partial code (just showing the request I am sending). BCAddy is the broadcast address:
B4X:
Dim msg As String
msg = "M-SEARCH * HTTP/1.1" & Chr(13) & Chr(10) & _
"Man: " & Chr(34) & "ssdp:discover" & Chr(34) & Chr(13) & Chr(10)  & _
"Mx: 3" & Chr(13) & Chr(10) & _
"Host: " & BCAddy & ":1900" & Chr(13) & Chr(10) & _
"St: ssdp:all" & Chr(13) & Chr(10) & Chr(13) & Chr(10)'  ST: upnp:rootdevice or ssdp:all
UDPSendMsg(msg, BCAddy)

The problem I have is that not all devices are returned. If I use the broadcast address returned by the code in this thread, it says it is 192.168.1.255. However, when I do a UPnP scan using other apps, it is using 239.255.255.250 as the broadcast address. Using the latter address returns more devices, but still some are missing compared to other UPnP scanners.

Since I am using the "standard" discover broadcast, I don't know why some devices do not respond. The other thing I don't understand is why the code in this thread returns 192.168.1.255 as the broadcast address yet all other apps I have tried (including a UPnP SDK for Windows) use 239.255.255.250 as the broadcast address. I know this address can change depending on the network environment so I want to be sure I am using the correct one for all possible networks.

Anyone have any thoughts or comments?
 
Upvote 0

EduardoElias

Well-Known Member
Licensed User
Longtime User
Since I am using the "standard" discover broadcast.

I was having these problems take a look on this thread: http://www.b4x.com/android/forum/threads/android-broadcast-address.31971/

Just to give some warnings to help you:

- 255.255.255.255 is a standard broadcast address as documented, however will not work in many networks, because are not routed to eliminate threats
- in a network for example 192.168.0.1 address and mask of 255.255.255.0 the broadcast ip will be 192.168.0.1.255 but this is only because the mask is that one. If the mask change the network broadcast change! THe broadcast ip is the only realible way to use udp broadcast that cross the majority of local routers. But even that is no guarantee. You can calculate the broadcast address in any network if you get the ip and mask, that is part of the standard. It is a common mistake of many (was mine also) to not consider the network mask. Routing is made based on that, not only on IP.
- on the link I posted above you can find a tiny library I made. Very simple. First time... but it is working. However it only works on API 9 or higher. This library escapes the local adapter (127) and at the first network it finds return the calculated broadcast ip. It will eventually fail if you have a android with wifi and ethernet at same time, because it will send the first ip that could not be the one you are looking for. Consider that working with IP means always a list of IPs, commonly is only one, but the entire spec is based on the possibility of many other cases.

Right now I am using this tiny library for api 9 or higher and 255.255.255.255 for 8 and below. Major of my users are 9 > that makes easier. Even that there are AP and routers that calculates and block the broadcast ip. There is also the fact that some IP route the broadcast on the wifi side, not broadcasting to LAN side (in case you have a server on LAN side). But I have found that the chance of working is much higher if everything in on the same type of connection, WIFI for exemple.

Good luck

Eduardo
 
Upvote 0

DKCERT

Member
Licensed User
Longtime User
You are mixing up the types. My code calculates the LAN Broadcast address based on current network IP-address and Subnet Mask. A confined area so to speak. The mentioned 239.255.255.250 is a Multicast address also called Simple Service Discovery Protocol (SSDP) used for discovery of network services/devices across segments. It is a text-based protocol that uses UDP port 1900 (as seen in your code).

http://en.wikipedia.org/wiki/Broadcast_address
http://en.wikipedia.org/wiki/Multicast_address
http://en.wikipedia.org/wiki/Simple_Service_Discovery_Protocol
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
You are mixing up the types. My code calculates the LAN Broadcast address based on current network IP-address and Subnet Mask. A confined area so to speak. The mentioned 239.255.255.250 is a Multicast address also called Simple Service Discovery Protocol (SSDP) used for discovery of network services/devices across segments. It is a text-based protocol that uses UDP port 1900 (as seen in your code).

http://en.wikipedia.org/wiki/Broadcast_address
http://en.wikipedia.org/wiki/Multicast_address
http://en.wikipedia.org/wiki/Simple_Service_Discovery_Protocol

Thank you very much! I was indeed mixing them up... honestly I thought they were the same thing. :oops:

So after reading those links, it sounds like 239.255.255.250 will always be the multicast address on any IPv4 network? If so then I needn't worry about it. I also added permissions to my app to be a "broadcast receiver". Does this mean that permission isn't even necessary?

Thanks so much!
 
Upvote 0

EduardoElias

Well-Known Member
Licensed User
Longtime User
Really seems we are talking different things, with somewhat similar functions.

According to the documentation the 239.255.255.250 will be always the multicast address. Get some tool that let you look your PC local routing tablet and this address should be there translating to your local IP.

But I was refering firstly to other 2 types of broadcast IPs. The limited one 255.255.255.255, and the DIRECT IP BROADCAST, that I can use on UDP to send a message to all on the network.

I am not using MULTICAST. I am using UDP to send a signal to all the connected devices using the DIRECT IP Broadcast. I was using 255.255.255.255 before, but it fails in many networks, since it is bloqued by defaul on some routers. But if you see on your local routing table there is one another address that translate to your local IP either. This entry is calculated and it is the one I was refering also. You can do BROADCAST with it and have a better chance with the majority of the routers.

This address is used by some softwares as a way of discovering each other. But it is a particular way of implementation and do not follow the SSDP protocol.

I am getting it a bit longer to try to clarify and see if I am not getting it wrong either, since I am no expert on it and I am learning by experience.
 
Upvote 0
Status
Not open for further replies.
Top