Advanced networking with android/linux

Tutorials with advanced 'difficulty' and more Lines of Code.

Did this tutorial help you

yes
12
92%
no
1
8%
 
Total votes : 13

Advanced networking with android/linux

Postby rtreffer » Thu Nov 29, 2007 10:17 pm

*** Warnings ***

linux as host os is required, windows might be possible but is untested
You can easily destroy your emulator. Backup your emulator
Root access required


Preface

${android_sdk} refers to your android sdk root folder

Your emulator consists of three images:
  • ${android_sdk}/tools/lib/images/ramdisk.img (cpio[cnew] archive, gzip compressed)
  • ${android_sdk}/tools/lib/images/system.img (yaffs2 formated)
  • ${android_sdk}/tools/lib/images/userdata.img (yaffs2 formated)
By now noone has managed to mounting the yaffs2 formated partitions. Loopback mounting does not work. So for now the best way to hack the emulator is ${android_sdk}/tools/lib/images/ramdisk.img

Step 0 - backup
Code: Select all
cp ${android_sdk}/tools/lib/images/ramdisk.img ${android_sdk}/tools/lib/images/ramdisk.img.old


Step 1 - extracting the ramdisk
Code: Select all
mkdir ramdisk
cd ramdisk
cat ${android_sdk}/tools/lib/images/ramdisk.img | gunzip | sudo cpio -i

This will populate "ramdisk" with the ramdisks content.

Step 2 - looking at a simple boot skript
Android requires some special code to boot in the emulator, the code can be found in etc/qemu-init.sh
Code: Select all
sudo vim etc/qemu-init.sh

Here is the original content:
Code: Select all
#!/system/bin/sh

# Some special case stuff for running under emulation
qemu=`getprop ro.kernel.qemu`
case "$qemu" in
    "1" )
    ifconfig eth0 10.0.2.15 netmask 255.255.255.0 up
    route add default gw 10.0.2.2 dev eth0
    radio_ril=`getprop ro.kernel.android.ril`
    case "$radio_ril" in
        ttyS*) # a modem is emulated as a serial device
            ;;
        *) # no need for the radio interface daemon
            # telephony is entirely emulated in Java
            setprop ro.radio.noril yes
            stop ril-daemon
            ;;
    esac
    setprop net.eth0.dns1 10.0.2.3
    setprop net.gprs.local-ip 10.0.2.15
    setprop ro.radio.use-ppp no
    setprop ro.config.nocheckin yes
    setprop status.battery.state Slow
    setprop status.battery.level 5
    setprop status.battery.level_raw  50
    setprop status.battery.level_scale 9
    stop dund
    stop usbd
    ;;
esac


Step 3 - the target networking environment
Android is hardcoded to use 10.0.2.3 and 10.0.2.2 for comunication - especially with adb. You can not use anything except this userspace emulation for eth0.
Android
  • device: eth0, ip: 10.0.2.15, net: 10.0.2.0/255.255.255.0 - this will keep adb happy
  • device: eth1, ip: 192.168.1.2, net: 192.168.1.0/255.255.255.0 - this will be used for real communication
  • default gateway: 192.168.1.1 - or whatever suits your network
  • DNS: 10.0.2.3 - we simply keep this, no disadvantages except harder debugging
Host
  • net_android, virtual android network device
  • br0, network bridge linking eth0 and net_android
Depending on what you want to test you might want to turn your host system into a nat gateway, a dhcp server, transparent proxy, routing gateway or whatever you need.

Step 4 - altering the android network
Code: Select all
#!/system/bin/sh

# Some special case stuff for running under emulation
qemu=`getprop ro.kernel.qemu`
case "$qemu" in
    "1" )

    if ifconfig eth1 up; then
        ifconfig eth1 down
        # Should we really check for eth1 that way?

        # DHCP - broken?
        # netcfg eth1 dhcp

        ifconfig eth1 192.168.1.2 netmask 255.255.255.0 up
        route add default gw 192.168.1.1 dev eth1

        ifconfig eth0 10.0.2.15 netmask 255.255.255.0 up

        setprop net.eth0.dns1 10.0.2.3
        setprop net.eth1.dns1 10.0.2.3
        setprop net.gprs.local-ip 192.168.1.2
    else
        ifconfig eth0 10.0.2.15 netmask 255.255.255.0 up
        route add default gw 10.0.2.2 dev eth0
        setprop net.eth0.dns1 10.0.2.3
        setprop net.gprs.local-ip 10.0.2.15
    fi

    # Proxy support - broken too?
    # setprop net.gprs.http-proxy http://proxy:8080/

    # No longer needed
    # ifconfig eth0 10.0.2.15 netmask 255.255.255.0 up
    # route add default gw 10.0.2.2 dev eth0
    radio_ril=`getprop ro.kernel.android.ril`
    case "$radio_ril" in
        ttyS*) # a modem is emulated as a serial device
            ;;
        *) # no need for the radio interface daemon
            # telephony is entirely emulated in Java
            setprop ro.radio.noril yes
            stop ril-daemon
            ;;
    esac
    # no longer needed
    # setprop net.eth0.dns1 10.0.2.3
    # setprop net.gprs.local-ip 10.0.2.15
    setprop ro.radio.use-ppp no
    setprop ro.config.nocheckin yes
    setprop status.battery.state Slow
# Interresting - is this the way to check battery status?
    setprop status.battery.level 5
    setprop status.battery.level_raw  50
    setprop status.battery.level_scale 9
    stop dund
    stop usbd
    ;;
esac

This will keep your classic config if eth1 doesn't exist and if it exists adb will still work.

Step 5 - packing your new ramdisk
Code: Select all
sudo find | sudo cpio -o -H newc | gzip -9 > ${android_sdk}/tools/lib/images/ramdisk.img


Step 6 - Host configuration
Code: Select all
sudo brctl addbr br0
sudo tunctl -u $USER -t net_android
sudo ifconfig eth0 0.0.0.0 promisc up
sudo ifconfig net_android 0.0.0.0 promisc up
sudo brctl addif br0 eth0
sudo brctl addif br0 net_android
sudo dhclient3 br0

Depending on you sodu configuration you might need to substitute $USER with your username. Depending on your local network you might need to substitute sudo dhclient3 br0 with your normal network initialization.

Step 7 - boot and test
Code: Select all
${android_sdk}/tools/emulator -debug-kernel -qemu -net nic -net user -net nic -net tap,ifname=net_android

Now on your host system do
Code: Select all
ping 192.168.2.2
adb shell

If everything went well both commands will work.

Enjoy!

TODO1: Tutorial how to use the new network link from within dalvik.
TODO2: Find a windows user who will try the ramdisk with windows and openvpn/tun/tap driver
root@localhost# : ( ) { : | : & } ; :
rtreffer
Junior Developer
Junior Developer
 
Posts: 15
Joined: Fri Nov 23, 2007 4:14 pm

Top

Postby JimCummins » Fri Nov 30, 2007 2:31 am

This looks very cool. At the moment its out of my league but I hope to try it sometime. Wish I could help you on the windows ramdisk but my windows testing box is out of commission with a dead psu.
"I have no reason to learn this, which is what makes it interesting."
JimCummins
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Nov 28, 2007 6:06 pm
Location: Ames, IA

Postby plusminus » Fri Nov 30, 2007 7:55 am

Hello rtreffer,

btw: thank you for posting your experiences here as a tutorial :!:
Unfortunately I'm running, so I would try it out otherwise ;)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sergg » Wed Dec 05, 2007 3:08 pm

thanks for this tutorial. It is very usefull!
Can it be used for connection via http proxy (with default ramdisk.img browser and maps didnt works)? And how? =)

Thanks,
sergg
sergg
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 05, 2007 2:55 pm
Location: Russia, Obninsk

Postby plusminus » Wed Dec 05, 2007 10:53 pm

Hello sergg,

Have a look at the following issue that is probably exactly what you need:go there.

Best Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sergg » Thu Dec 06, 2007 9:45 am

Hello plusminus,

Thanks a lot, now it is work (and maps and browser)!

regards,
sergg
sergg
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 05, 2007 2:55 pm
Location: Russia, Obninsk

Top

Where to download the tools of cat and sudo for windows XP?

Postby fengForky » Wed Jan 09, 2008 3:33 pm

cat ${android_sdk}/tools/lib/images/ramdisk.img | gunzip | sudo
fengForky
Freshman
Freshman
 
Posts: 4
Joined: Mon Jan 07, 2008 4:34 pm

Postby fengForky » Sat Jan 12, 2008 4:07 pm

please send your new ramdisk.img to me or post it.

sergg wrote:Hello plusminus,

Thanks a lot, now it is work (and maps and browser)!

regards,
sergg
fengForky
Freshman
Freshman
 
Posts: 4
Joined: Mon Jan 07, 2008 4:34 pm

I tried it on windows. Need help

Postby fengForky » Sat Jan 12, 2008 4:21 pm

I get to internet by ISDN. And my ip is the same as the ip 192.168.1.1 and 1.2 that you've configged on linux OS.
I did it according to your message. By the help of CYGWin. But the cpio is lacked. So I download a windows version of cpio and copy it to the bin of CYGWin.
I did so. But I don't know whether this is right. And I do not know what's the meaning of "Function not implemented".
And the other steps sames right. But at last the simulator won't show the android screen.
Who know how to make Android on windows to connect to the internet?

The results i did:
step 1:
Code: Select all
$ cat ../ramdisk.img | gunzip | cpio -i
D:Programscygwinbincpio.exe: etc/default.prop: Function not implemented
D:Programscygwinbincpio.exe: etc/firmware/brf6150.bin: Function not implemented
D:Programscygwinbincpio.exe: etc/firmware/brf6300.bin: Function not implemented
D:Programscygwinbincpio.exe: etc/hcid.conf: Function not implemented
D:Programscygwinbincpio.exe: etc/hosts: Function not implemented
D:Programscygwinbincpio.exe: etc/init.gprs-pppd: Function not implemented
D:Programscygwinbincpio.exe: etc/init.rc: Function not implemented
D:Programscygwinbincpio.exe: etc/init.ril: Function not implemented
D:Programscygwinbincpio.exe: etc/init.testmenu: Function not implemented
D:Programscygwinbincpio.exe: etc/ppp/chap-secrets: Function not implemented
D:Programscygwinbincpio.exe: etc/ppp/ip-down: Function not implemented
D:Programscygwinbincpio.exe: etc/ppp/ip-up: Function not implemented
D:Programscygwinbincpio.exe: etc/qemu-init.sh: Function not implemented
D:Programscygwinbincpio.exe: etc/qemu-init.sh.bak: Function not implemented
D:Programscygwinbincpio.exe: etc/system.conf: Function not implemented
D:Programscygwinbincpio.exe: etc/system.d/bluez-hcid.conf: Function not implemented
D:Programscygwinbincpio.exe: etc/usbd.conf: Function not implemented
D:Programscygwinbincpio.exe: init: Function not implemented
D:Programscygwinbincpio.exe: sbin/recovery: Function not implemented
737 blocks

fengforky@ff /cygdrive/d/Programs/android_sdk_windows_m3-rc37a/tools/lib/images/
ramdisk
$


step 2:
Code: Select all
     modify qemu-init.sh to the same as what you post on BBS.

step 3:
Code: Select all
  $ find | cpio -o -H newc | gzip -9 > ../ramdisk.img
737 blocks

fengforky@ff /cygdrive/d/Programs/android_sdk_windows_m3-rc37a/tools/lib/images/
ramdisk
$


step 4:
Code: Select all
   start Android in Eclipse. But the screen do not show up. Even if I do not do the step 2 and just  do the step 3. It does not show the screen.
fengForky
Freshman
Freshman
 
Posts: 4
Joined: Mon Jan 07, 2008 4:34 pm

Postby oho » Thu Mar 27, 2008 3:19 pm

Hello,

I followed your instructions carefully, but I get an error message when trying to run the emulator.
First, is this tutorial still working with m5-rc15?

I am using m5-rc15 under Ubuntu.

After I complete Step 6 successfully, I issue:

Code: Select all
${android_sdk}/tools/emulator -debug-kernel -qemu -net nic -net user -net nic -net tap,ifname=net_android


and get the following error message:

Code: Select all
/etc/qemu-ifup: could not launch network script
   Could not initialize device 'tap'



Do I need to install any additional software? Or do I need to create the qemu-ifup script manually? I googled, but
didn't find anything that helped my explicitly.

Thank you for your help,
Oho
oho
Freshman
Freshman
 
Posts: 4
Joined: Thu Mar 27, 2008 3:05 pm

Postby oho » Tue Apr 08, 2008 11:08 am

Hello,

I am now using a very simple solution, I have installed ssh on the emulator and establish a ssh tunnel to get to
a visible PC and from there back to my local PC. A bit long winded but very easy to setup.

Oho
oho
Freshman
Freshman
 
Posts: 4
Joined: Thu Mar 27, 2008 3:05 pm

Postby www.rzr.online.fr » Thu Feb 05, 2009 10:13 am

Does this thread need to be updated to current version of SDK

I just want to ping my android from outside the emulator (on host)..

Regards
User avatar
www.rzr.online.fr
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 03, 2009 11:39 am
Location: fr/35/rennes

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests