Android: DNS Setup for Developing and Testing Against Local Web Services

Most “interesting” smartphone applications do not run only on the smartphone device; they rely on supporting web services that can be run both by the deploying organization and 3rd parties. One of the challenges we have run into when developing Android application is setting up a suitable development environment because of issues resolving DNS entries for test versions of services.

Scenario:  We have a development workstation where we want to run the Android application being developed as well as a development version of a web service.  Let’s say the production web service runs at http://services.smartphonesdumbapps.com/ and we need to emulate that in our workstation environment.

First thought: Let’s set our workstation /etc/hosts file so that it points services.smartphonesdumbapps.com to localhost.

Great idea, except that the Android emulator does not use the workstation’s /etc/hosts files to resolve DNS entries.  And localhost for the emulator is different than our workstation’s localhost.  Being a Linux system, it has its very own fancy /etc/hosts file that is uses to support its DNS operations.  So we need to change the emulator’s /etc/hosts file.  But what do we want to map services.smartphonesdumbapps.com to?

Android emulator networking has a couple of special IP addresses it maintains to help you route traffic to different useful locations.  For our purposes we want to route our traffic to 10.0.2.2 because that goes to the host workstation’s loopback interface.  (See Emulator Networking for more information)

So now we know we want to add an entry to the Android emulator’s /etc/hosts file to map services.smartphonesdumbapps.com to 10.0.2.2.  Should be easy – let’s fire up the emulator, pull the original /etc/hosts file over to our workstation, make our update and then push it back to the emulator.

So we do:

$ emulator -avd AndroidPhone_test &

$ adb pull /etc/hosts ./

{modify local copy of hosts to include an entry for 10.0.2.2 for services.smartphonesdumbapps.com}

$ adb push hosts /etc/hosts

That should just about take care of it for us, except that we see:

failed to copy ‘hosts’ to ‘/etc/hosts’: Read-only file system

Crap.  Guess we need to remount the filesystem to be read/write.  (See Mount a filesystem read-write for more information)

So to mount the filesystem read-write we do:

$ adb shell

# mount -o rw,remount -t yaffs2 /dev/block/mtdblock0 /system

# exit

That should take care of things.  But it doesn’t because you see:

failed to copy ‘hosts’ to ‘/etc/hosts’: Out of memory

Crap!  Crap!  Crap!  My workstation has about a million billion gigs of memory and my emulator setup ought to as well.  What is going on?  As it turns out we need to run the emulator with a larger partition size (More info on Android emulator partition sizes here)

So we need to turn off the old emulator and start a new one with a bigger partition size.  Once we have the old emulator turned off, we run the new one with:

$ emulator -avd AndroidPhone_test -partition-size 128 &

Then we mount the filesystem read-write with:

$ adb shell

# mount -o rw,remount -t yaffs2 /dev/block/mtdblock0 /system

# exit

Finally we pull and push the emulator /etc/hosts file with:

$ adb pull /etc/hosts ./

{modify local copy of hosts to include an entry for 10.0.2.2 for services.smartphonesdumbapps.com}

$ adb push hosts /etc/hosts

The whole sequence ends up looking like:

$ emulator -avd AndroidPhone_test -partition-size 128 &

$ adb shell

# mount -o rw,remount -t yaffs2 /dev/block/mtdblock0 /system

# exit

$ adb pull /etc/hosts ./

{modify local copy of hosts to include an entry for 10.0.2.2 for services.smartphonesdumbapps.com}

$ adb push hosts /etc/hosts

Now when the Android application tries to resolve services.smartphonesdumbapps.com it will send that traffic to the local development version running on the workstation.  Much ado?  Yes.  About nothing?  Well … this wasted an hour or so of my time a couple months ago so I figured it couldn’t hurt to get up on the web in a consolidated post.

Contact us for help building and securing your smartphone applications.

–Dan

dan _at_ denimgroup.com

@danielcornell

Posted via email from Denim Group’s Posterous

About Dan Cornell

A globally recognized application security expert, Dan Cornell holds over 15 years of experience architecting, developing and securing web-based software systems. As the Chief Technology Officer and a Principal at Denim Group, Ltd., he leads the technology team to help Fortune 500 companies and government organizations integrate security throughout the development process. He is also the original creator of ThreadFix, Denim Group's industry leading application vulnerability management platform.
More Posts by Dan Cornell

Categories: Uncategorized

One Response to “Android: DNS Setup for Developing and Testing Against Local Web Services”

  1. infinity blade 3 hack

    splinter splitter splodged splodges sploshed sploshes splotchy splurged. cubical cubicle cubicly cubisms cubists cubital cubitus cuboids. colour colter colugo column colure colzas comade comake.

Leave a Reply

Your email address will not be published. Required fields are marked *