Showing posts with label netmask. Show all posts
Showing posts with label netmask. Show all posts

Friday, November 23, 2012

Problem in static IP:
===============


I ported the android x86 ethernet support to android board.
I used the same kernel to run ICS and jellybean OS.
I faced  the problem in setting static IP. while switching from DHCP to Static IP, entire system hangs.
 Ethernet patch is storing the value in Settings. while reading the value from settings and setting the static IP, it hangs...
Since ethernet is configured in bootup time, system hangs and doesnt proceed booting android.

    I analysed and found that providers/settings data will be stored in /data/data/com.android.settings.provider/settings.db.
All android applications/providers database values will be stored under /data/data/com.android.ApplicationName.
To clear the settings value, we can format /data/ partition , So that stored information will be deleted.
Now I am able to proceed with booting android.
   But still problem is there while setting static IP.I doubted the network environments/OS/my program.

  1)Through ifconfig, I am able to set static IP address. So there is no problem in network environment.    
  2) I doubted IP settings are not allowd in jellybean, any  new services have been added to change IP address. Since I am using same kernel, there wont be any changes in kernel level.
  3) I doubted my entire program is not working. But same code was previously working with ICS.

Previously with ICS, I am able to set static IP and tested well.Same code is not working. I doubted my static Ip setting itself is not working.
 To narrow down the issue, I hardcoded the netmask value, afterwards it is able to set static IP.
  In my code, I debugged and found that it is hanging in netmask to prefix conversion which is required for setting static IP address.

 Lessons Learnt:  1) All android applications store data/database values under /data/data/com.android.ApplicationName[Package name of the android]
  2) Always document the sample inputs/environments for success cases, So in future, if the system is failing, we can try with that input.
  3) always test lower limit and upper limit values... System hangs since It is failed in lower limit

 Please refer my previous post about netmask to prefix conversion problem.
http://sundararajana.blogspot.in/2012/11/netmask-prefix-length-netmask-is-also.html




   

Thursday, November 22, 2012

Netmask , prefix length :
==========================

  netmask is also measured as prefix length. The number of bits set in netmask value is called as prefix length.
Example :   Netmask:255.255.255.0 Prefix : 24 //number of bits set in 255.255.255.0 value.

 To convert the netmask to prefix I added below code in Java:

  netmask= lookupHost("255.255.255.128");
   while ( netmask !=0)
   {
      if( netmask & 1)
        ++prefixLength;
       netmask = netmask >> 1;
    
   }

  Simple code :
     The above code is working fine for sometime. Sometimes it is not working...
    I found that netmask to prefix conversion is breaking and makes the system hangs....

 The input 255.255.255.0 is working fine. But 255.255.255.128 is breaking ...

Finally figured that the problem is with the code.

    In a 32 bit signed integer, 255.255.255.0 is not giving any problem. So LSB is stored as a first byte in memory ...

   lookupHost converts the string as 0 255 255 255[bits set] as integer..[0x00ffffff].
when it comes to 255.255.255.128, 128 255 255 255 [bits set as 0x80ffffff. So netmask will always not equal to 0, So it is in infinite loop.
>> shift operator operates on bits not on sign bit. netmask's sign bit will always 1 & it satisfies netmask != 0 condition infinitely.

Solution:
   To avoid this problem,
    1)I have to use >>> operator which will also operates on sign bit.
    2) I can also check for the maximum limit.

   Above code can be modified as below:

      
  netmask= lookupHost("255.255.255.128");
   while ( netmask !=0)
   {
      if( netmask & 1)
      {
        ++prefixLength;
         if (prefixLength == 32)
         {
           break; //exit while loop since maximum allowed value is reached for prefix length for 32 bit IP address, maximum prefix len is 32
         }
      }
       netmask = netmask >>> 1;// >>>     Unsigned right shift
         
   }