androiddhcp

How to retrieve the domain name returned by DHCP on Android


We have some Windows CE applications we are porting to Android (Zebra devices). For support purposes, we log the domain name returned by DHCP (option 15) at application startup (lessons learned dealing with misconfigured infrastructure in the past).

In Windows CE this is available in the DomainName member of the FIXED_INFO struct returned by GetNetworkParams(). It also appears to be available in HKLM\Comm\Tcpip\Parms\DNSDomain in the registry.

I am having difficulty finding how to accomplish the same in Android. I did notice a DhcpInfo class for Android, but it does not contain the Domain Name.

getting the domain name of DHCP on Android asked a similar question but does not have any helpful answers.

Thanks for your help.


Solution

  • below function registers a network callback and retrieves network properties, specifically focusing on DHCP and domain name information if supported.

    The function obtains a reference to the Connectivity Manager by calling requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager. This allows it to interact with the device's network-related functionalities.

    Next, it registers a default network callback using registerDefaultNetworkCallback(). This callback is triggered when a network connection becomes available. Within the callback, the onAvailable() method is overridden to perform actions when a network becomes available.

    Inside the onAvailable() method, it first checks if the network has the capability NET_CAPABILITY_NOT_RESTRICTED, which indicates that the DHCP option 15 (Domain Name) is supported. If the capability is present, it proceeds with retrieving the network properties.

    The function then obtains the link properties for the network using connectivityManager.getLinkProperties(network). The link properties contain information such as IP addresses, DNS servers, and domain names associated with the network.

    The DHCP information is stored in the linkPropertiesInfo variable, which is obtained by calling linkProperties.toString(). The domain name associated with the network is extracted using linkProperties.domains and assigned to an unspecified variable.

    private fun getNetworkInformation(){        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val connectivityManager =requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            connectivityManager.registerDefaultNetworkCallback(object :
                ConnectivityManager.NetworkCallback() {
                override fun onAvailable(network: Network) {
                    super.onAvailable(network)
                    val networkCapabilities =
                        connectivityManager.getNetworkCapabilities(network)
                    // Check if DHCP option 15 (Domain Name) is supported from network
                    if (networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED) == true) {
                        val linkProperties = connectivityManager.getLinkProperties(network)
                        //Return dhcp information
                        val linkPropertiesInfo=linkProperties.toString()
                        // return domain name link to 
                        val =linkProperties.domains
                        logger.info("Network linkProperties properties ${linkProperties.toString()}")
                    }
                }
            })
        }
    }