On Android, System.getSecurityManager should return null: http://developer.android.com/reference/java/lang/System.html#getSecurityManager()
In 2.1, an app (or its library) could have have installed a SecurityManager). Starting in 2.3, setting a non-null SecurityManager will throw a SecurityException. From the javadoc: http://developer.android.com/reference/java/lang/System.html#setSecurityManager(java.lang.SecurityManager) "Security managers do *not* provide a secure environment for executing untrusted code and are unsupported on Android. Untrusted code cannot be safely isolated within a single VM on Android." Here is the 2.1 code. It could just be returning because of the UnknownHostException because of a DNS problem: /** * Gets the fully qualified domain name for the host associated with this IP * address. If a security manager is set, it is checked if the method caller * is allowed to get the hostname. Otherwise, the textual representation in * a dotted-quad-notation is returned. * * @return the fully qualified domain name of this IP address. */ public String getCanonicalHostName() { String canonicalName; try { int address = 0; if (ipaddress.length == 4) { address = bytesToInt(ipaddress, 0); if (address == 0) { return ipAddressToString(ipaddress); } } canonicalName = getHostByAddrImpl(ipaddress).hostName; } catch (UnknownHostException e) { return ipAddressToString(ipaddress); } SecurityManager security = System.getSecurityManager(); try { // Only check host names, not addresses if (security != null && isHostName(canonicalName)) { security.checkConnect(canonicalName, -1); } } catch (SecurityException e) { return ipAddressToString(ipaddress); } return canonicalName; } -bri On Thu, Apr 21, 2011 at 2:33 AM, damcav <[email protected]> wrote: > Hi there > > I'm developing an Android app which relies heavily on TCP. > > getCanonicalHostName() on Android 2.1 seems to always return an IP > address instead of the host name. The documentation for > getCanonicalHostName states: > > "If there is a security manager, this method first calls its > checkConnect method with the hostname and -1 as its arguments to see > if the calling code is allowed to know the hostname for this IP > address, i.e., to connect to the host. If the operation is not > allowed, it will return the textual representation of the IP address." > > So I assume there's a security manager installed and that it's > disallowing my request. > > So my questions are, how can I check if a security manager is > installed, and can I replace/modify it so that getCanonicalHostName() > will work? > > Thanks! > > > -- > You received this message because you are subscribed to the Google Groups > "Android Security Discussions" group. > To post to this group, send email to > [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/android-security-discuss?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Android Security Discussions" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/android-security-discuss?hl=en.
