Tuesday, June 12, 2012

Networking on a Headless Android System

Android isn't the most intuitive environment for setting up networking on the command line. After playing with several different options here's what I've found.

Summary

# netcfg eth0 dhcp
# route add default gw 192.168.0.1 dev eth0
# setprop net.dns1 208.67.222.222
# setprop net.dns2 208.67.222.220

Details


The Android tools are quite different from the tools found on other embedded Linux systems, so most people I've talked to have had difficulty figuring out what utilities to use and how to use them. The most interesting one is ifconfig. When you type ifconfig on an Android system nothing happens. On a standard busybox or gnu based Linux system you will see the available networking interfaces and their associated settings (ok, technically ifconfig -a will show you that information). Since ifconfig doesn't show anything, most people assume (incorrectly) that it doesn't work.

On my development platform, to set a static IP address, I did the following:
# ifconfig eth0 192.168.0.84 netmask 255.255.255.0

Which didn't give any response, but DID show the following when I did netcfg.
  
# netcfg
lo       UP    127.0.0.1       255.0.0.0       0x00000049
eth0     UP    192.168.0.84    255.255.255.0   0x00001043
usb0     DOWN  0.0.0.0         0.0.0.0         0x00001002
tunl0    DOWN  0.0.0.0         0.0.0.0         0x00000080
gre0     DOWN  0.0.0.0         0.0.0.0         0x00000080
sit0     DOWN  0.0.0.0         0.0.0.0         0x00000080

So what about DHCP you may ask?

# netcfg eth0 dhcp

Which yields the following.

# netcfg
lo       UP    127.0.0.1       255.0.0.0       0x00000049
eth0     UP    192.168.0.227   255.255.255.0   0x00001043
usb0     DOWN  0.0.0.0         0.0.0.0         0x00001002
tunl0    DOWN  0.0.0.0         0.0.0.0         0x00000080
gre0     DOWN  0.0.0.0         0.0.0.0         0x00000080
sit0     DOWN  0.0.0.0         0.0.0.0         0x00000080

Just because you have an IP address now doesn't mean the applications on your Android device can get to the network. For that you have to set a default route and set the DNS resolver addresses.  Again, with route keep in mind it won't show the usual output that a standard busybox or gnu based embedded Linux system would show.

To set the default route:

# route add default gw 192.168.0.1 dev eth0

And to set the DNS (I'm using OpenDNS addresses here):

# setprop net.dns1 208.67.222.222
# setprop net.dns2 208.67.222.220

And that allows the holy grail of a DNS resolved ping to a domain on the Internet.

# ping google.com
PING google.com (74.125.224.134) 56(84) bytes of data.
 

64 bytes from nuq04s09-in-f6.1e100.net (74.125.224.134): icmp_seq=1 ttl=57 time=41.5 ms
64 bytes from nuq04s09-in-f6.1e100.net (74.125.224.134): icmp_seq=2 ttl=57 time=43.2 ms


References


I found some great information in the following links.

http://www.anddev.org/advanced_networking_with_android-linux-t155.html

http://elinux.org/Android_Networking






No comments:

Post a Comment