Connecting two Androids via TCP
Below is an email which I sent to android-developers concerning how to get two Androids talking to each other. This isn’t the solution, but is a path that might lead to a solution:
Hello,
I and it seems others would like to connect two Androids via TCP a previous
attempt failed with a segfault in the emulator. Digit and Alexey helped
resolve that, thanks.
I thought I’d report my progress and hopefully enlist some more
help. As I sad, my goal is to connect two Androids via TCP my path to a solution
is to allow the emulator(s) to appear on my local network by using a
bridge running in the host and having the qemu/Android use TUN/TAP
to connect to the bridge. This comes from http://compsoc.dur.ac.uk/~djw/qemu.html.
At the moment I have made some progress, I successfully bridged the
Android to my host as well as to the internet and was able to ping from
the Android console to my host and visa-versa. I also, was able to ping
an address on the internet, so progress was made.
Here is what I’ve done to get this far: (Note; this was done on Linux box, uname -a:
Linux ic2d1 2.6.22-14-generic #1 SMP Tue Dec 18 05:28:27 UTC 2007 x86_64 GNU/Linux)
This first series of steps only a single nic is needed, the second series I
tried using two nics.
1) Setup a bridge on my host.
a) Install bridge-utils
b) Besure TUN/TAP is configured in the kernel, the following
should print CONFIG_TUN=m or CONFIG_TUN=y
grep CONFIG_TUN= /boot/config-`uname -r`
c) You also need /dev/net/tun if it doesn’t exist then:
sudo mknod /dev/net/tun c 10 200
d) Create and initialize the bridge to your host ethernet
I created a script, br0, which creates the bridge,
attaches eth0, and then uses dhcp to get an address:
#!/bin/sh
set -x
# remove br0 if it exists
ifconfig br0 down
brctl delbr br0
# remove ip address from eth0
ifconfig eth0 0.0.0.0
# create a bridge
brctl addbr br0
# add eth0 to the bridge
brctl addif br0 eth0
# get br0 an address via dhcp
dhclient3 br0
Running br0 produces the following on my system:
wink@ic2d1:$ sudo ./br0
+ ifconfig br0 down
+ brctl delbr br0
+ ifconfig eth0 0.0.0.0
+ brctl addbr br0
+ brctl addif br0 eth0
+ dhclient3 br0
There is already a pid file /var/run/dhclient.pid with pid 13709
killed old client process, removed PID file
Internet Systems Consortium DHCP Client V3.0.5
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/
wmaster0: unknown hardware address type 801
wmaster0: unknown hardware address type 801
Listening on LPF/br0/00:17:31:e1:ce:a3
Sending on LPF/br0/00:17:31:e1:ce:a3
Sending on Socket/fallback
DHCPREQUEST on br0 to 255.255.255.255 port 67
DHCPREQUEST on br0 to 255.255.255.255 port 67
DHCPDISCOVER on br0 to 255.255.255.255 port 67 interval 8
DHCPOFFER from 192.168.0.2
DHCPREQUEST on br0 to 255.255.255.255 port 67
DHCPACK from 192.168.0.2
/etc/dhcp3/dhclient-exit-hooks.d/sendmail: line 28: /usr/share/sendmail/dynamic: No such file or directory
/etc/dhcp3/dhclient-exit-hooks.d/sendmail: line 31: update_interface: command not found
/etc/dhcp3/dhclient-exit-hooks.d/sendmail: line 40: update_host: command not found
/etc/dhcp3/dhclient-exit-hooks.d/sendmail: line 46: update_sendmail: command not found
bound to 192.168.0.133 — renewal in 36889 seconds.
Using brctl and ifconfig to show the configuration
wink@ic2d1:$ brctl show
bridge name bridge id STP enabled interfaces
br0 8000.001731e1cea3 no eth0
wink@ic2d1:$ ifconfig
br0 Link encap:Ethernet HWaddr 00:17:31:E1:CE:A3 inet addr:192.168.0.133 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::217:31ff:fee1:cea3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:121 errors:0 dropped:0 overruns:0 frame:0
TX packets:120 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:35187 (34.3 KB) TX bytes:15917 (15.5 KB)
eth0 Link encap:Ethernet HWaddr 00:17:31:E1:CE:A3 inet6 addr: fe80::217:31ff:fee1:cea3/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:10125 errors:0 dropped:0 overruns:0 frame:0
TX packets:7850 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5589385 (5.3 MB) TX bytes:842531 (822.7 KB)
Interrupt:19
And I can still ping, so my host is OK:
wink@ic2d1:$ ping google.com
PING google.com (72.14.207.99) 56(84) bytes of data.
64 bytes from eh-in-f99.google.com (72.14.207.99): icmp_seq=2 ttl=241 time=86.9 ms
64 bytes from eh-in-f99.google.com (72.14.207.99): icmp_seq=4 ttl=241 time=92.8 ms
64 bytes from eh-in-f99.google.com (72.14.207.99): icmp_seq=5 ttl=241 time=89.4 ms
64 bytes from eh-in-f99.google.com (72.14.207.99): icmp_seq=6 ttl=241 time=96.6 ms
64 bytes from eh-in-f99.google.com (72.14.207.99): icmp_seq=7 ttl=241 time=97.8 ms
— google.com ping statistics —
8 packets transmitted, 5 received, 37% packet loss, time 7016ms
rtt min/avg/max/mdev = 86.995/92.746/97.814/4.125 ms
2) Qemu needs to connect to the TUN/TAP and it uses a
script, which by default is /etc/qemu-ifup. For these tests
I ran as root (sudo -s) as permissions are correct
but thats a problem for later
Here is /etc/qemu-ifup:
#!/bin/sh
echo Executing /etc/qemu-ifup
echo Bringing up $1 for bridged mode…
sudo /sbin/ifconfig $1 0.0.0.0 promisc up
echo Adding $1 to br0…
sudo /usr/sbin/brctl addif br0 $1
echo Sleep a couple of secs
sleep 2
echo Complete /et/qemu-ifup
3) Execute the emulator (as root), this is the simplest set of qemu parameters
I could find that allowed the Android onto the local network and out to
the internet. NOTE: with this configuration there is only one nic, eth0.
I understand I should use two nics, see second series, but this is initially
sufficient to do something useful although adb and dns don’t work
maybe because it’s not at the expected ports/addresses
On host (as root) execute the emulator with qemu parameters:
emulator -console -qemu -net user -net nic,vlan=0 -net tap,vlan=0,ifname=tap0
From emulator console see that there is only one smc device:
# /data/busybox/ls -ld /sys/devices/platform/smc*
drwxr-xr-x 3 0 0 0 Jan 17 21:45 /sys/devices/platform/smc91x.0
#
An interesting point is that adb is working at the moment, maybe
because tap0 isn’t being used(?)
wink@ic2d1:$ adb devices
List of devices attached
1 emulator-tcp-5555 device 0
wink@ic2d1:$ adb shell
# exit
wink@ic2d1:$
4) To connect to my host’s network I then executed the following
from the Android console prompt:
ifconfig eth0 down
ifconfig eth0 192.168.0.213 up
route add default gw 192.168.0.2 dev eth0
5) ping the gateway from Android:
# ping -c 1 192.168.0.2
PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=13.4 ms
— 192.168.0.2 ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 13.400/13.400/13.400/0.000 ms
6) ping google.com (note dns doesn’t work so use address):
# ping -c 1 72.14.207.99
PING 72.14.207.99 (72.14.207.99) 56(84) bytes of data.
64 bytes from 72.14.207.99: icmp_seq=1 ttl=241 time=87.8 ms
— 72.14.207.99 ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 87.882/87.882/87.882/0.000 ms
7) From the host ping the Android:
wink@ic2d1:$ ping -c 1 192.168.0.213
PING 192.168.0.213 (192.168.0.213) 56(84) bytes of data.
64 bytes from 192.168.0.213: icmp_seq=1 ttl=64 time=0.532 ms
— 192.168.0.213 ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.532/0.532/0.532/0.000 ms
Finally, although adb can see the emulator the shell hangs
wink@ic2d1:$ adb devices
List of devices attached
1 emulator-tcp-5555 device 0
wink@ic2d1:$ adb shell
<- Used CTRL-C to break out
wink@ic2d1:$
============================
Second series trying to use two nics but can’t
get packets though to the host, I’m doing
something wrong!
============================
1) Now we’ll execute with two nic’s
emulator -console -qemu -net user -net nic -net nic,vlan=1 -net tap,vlan=1,ifname=tap0
2) Verify there are two smc devices:
# /data/busybox/ls -ld /sys/devices/platform/smc*
drwxr-xr-x 3 0 0 0 Jan 17 21:45 /sys/devices/platform/smc91x.0
drwxr-xr-x 3 0 0 0 Jan 17 21:45 /sys/devices/platform/smc91x.1
#
3) Bring up eth1 and ping it
# ifconfig eth1 192.168.0.213 up
# netcfg
gre0 DOWN 0.0.0.0 0.0.0.0 0×00000080
tunl0 DOWN 0.0.0.0 0.0.0.0 0×00000080
eth1 UP 192.168.0.213 255.255.255.0 0×00001043
eth0 UP 10.0.2.15 255.255.255.0 0×00001043
lo UP 127.0.0.1 255.0.0.0 0×00000049
#
# ping -c 1 192.168.0.213
PING 192.168.0.213 (192.168.0.213) 56(84) bytes of data.
64 bytes from 192.168.0.213: icmp_seq=1 ttl=64 time=1.15 ms
— 192.168.0.213 ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.157/1.157/1.157/0.000 ms
#
# /dev/busybox/ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:12:34:57 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:790 (790.0 B) TX bytes:0 (0.0 B)
Interrupt:13 DMA chan:ff
eth1 Link encap:Ethernet HWaddr 52:54:00:12:34:56 inet addr:192.168.0.213 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:12 Base address:0×2000 DMA chan:ff
lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
But I can’t get packets routed though eth1 to host
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.2.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1
default 10.0.2.2 0.0.0.0 UG 0 0 0 eth0
# route del default
# route add default gw 192.168.0.2 dev eth1
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.2.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1
default 192.168.0.2 0.0.0.0 UG 0 0 0 eth1
# ping -c 1 192.168.0.213
PING 192.168.0.213 (192.168.0.213): 56 data bytes
64 bytes from 192.168.0.213: seq=0 ttl=64 time=3.281 ms
— 192.168.0.213 ping statistics —
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 3.281/3.281/3.281 ms
# ping -c 1 192.168.0.2
PING 192.168.0.2 (192.168.0.2): 56 data bytes
— 192.168.0.2 ping statistics —
1 packets transmitted, 0 packets received, 100% packet loss
I’m guessing the problem might have to do that tap0 seems
to be connected to eth0 not eth1 as mac connected to the
bridge seems to be from eth0. Doing the following after
running the 2 nic configuration:
# export PATH=/data/busybox:$PATH
# ifconfig eth1 192.168.0.213 netmask 255.255.255.0 up
# route del default
# route add default 192.168.0.2 dev eth1
# ping -c 1 192.168.0.213
Then on the host, look at the macs connected to the bridge:
wink@ic2d1:$ brctl showmacs br0
port no mac addr is local? ageing timer
1 00:16:b6:81:90:ce no 2.29
1 00:17:31:e1:ce:a3 yes 0.00
2 00:ff:45:7f:1d:62 yes 0.00
2 52:54:00:12:34:57 no 0.06
We see from the ifconfig the mac addr (52:53:00:12:34:57) is from eth0
I would have expected we want it from eth1(52:54:00:12:34:56). It’s
also interesting that eth0 has mac addr :57 which is what I’d expect
eth1 to have. When running the emulator with no parameters the
mac addr for eth0 is :56.
# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:12:34:57 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:36 errors:0 dropped:0 overruns:0 frame:0
TX packets:15 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9762 (9.5 KiB) TX bytes:630 (630.0 B)
Interrupt:13 DMA chan:ff
eth1 Link encap:Ethernet HWaddr 52:54:00:12:34:56 inet addr:192.168.0.213 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:320 (320.0 B) TX bytes:126 (126.0 B)
Interrupt:12 Base address:0×2000 DMA chan:ff
lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:544 (544.0 B) TX bytes:544 (544.0 B)
============================
Anyway, if you got to here thanks all the more:)
If you have any suggestions they would be appreciated.
Wink Saville