Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.

Commit

Permalink
Bug 969518 - Autophone - allow use of usb networking on Linux, r=mcote.
Browse files Browse the repository at this point in the history
  • Loading branch information
bclary committed Mar 6, 2014
1 parent 270db9e commit 14cdbd7
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 3 deletions.
12 changes: 12 additions & 0 deletions USAGE.md
Expand Up @@ -28,6 +28,15 @@ to see them. Some important ones are
if Autophone can't find one. (FIXME: This may no longer be
needed?)

--usb-network: IP or network address for ppp over usb connections.
If specified, set up adb ppp over usb
connections so that all traffic from the
devices to the host or network specified by
usb_network passes through the ppp over usb
connection. Otherwise, use the default 'network.

--usb-gateway: Ethernet device over which to route usb network traffic.

--logfile: Log main Autophone system messages to this file. Defaults to
autophone.log. Devices will log to their own files in the
format <logfile base>-<phone id>.<logfile extension>, e.g.
Expand All @@ -45,6 +54,8 @@ to see them. Some important ones are
clear_cache
ipaddr
port
usb_network
usb_gateway
cachefile
logfile
loglevel
Expand All @@ -60,6 +71,7 @@ to see them. Some important ones are

Settings for internal parameters:

usb_ip
build_cache_size
build_cache_expires
devicemanager_retry_limit
Expand Down
40 changes: 38 additions & 2 deletions autophone.py
Expand Up @@ -15,6 +15,7 @@
import os
import signal
import socket
import subprocess
import sys
import threading
import urlparse
Expand All @@ -35,6 +36,9 @@

class AutoPhone(object):

# The starting address to be used for usbnet.
USB_IP = '192.168.1.200'

class CmdTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):

allow_reuse_address = True
Expand Down Expand Up @@ -334,6 +338,8 @@ def get_user_cfg(self):
def register_cmd(self, data):
# Un-url encode it
data = urlparse.parse_qs(data.lower())
autophone_directory = os.path.dirname(os.path.abspath(sys.argv[0]))
usbnet_script = '%s/usbnet.sh' % autophone_directory

try:
# Map MAC Address to ip and user name for phone
Expand All @@ -348,14 +354,30 @@ def register_cmd(self, data):
machinetype=data['hardware'][0],
osver=data['os'][0],
abi=data['abi'][0],
ipaddr=self.options[IPADDR])
ipaddr=self.options[IPADDR],
usb_network=self.options[USB_NETWORK],
usb_gateway=self.options[USB_GATEWAY])
if self.logger.getEffectiveLevel() == logging.DEBUG:
self.logger.debug('register_cmd: phone_cfg: %s' % phone_cfg)
if phoneid in self.phone_workers:
self.logger.debug('Received registration message for known phone '
'%s.' % phoneid)
worker = self.phone_workers[phoneid]
if worker.phone_cfg != phone_cfg:
if worker.phone_cfg == phone_cfg:
if phone_cfg[USB_NETWORK]:
usb_ip_parts = self.options[USB_IP].split('.')
usb_ip_parts[-1] = str(int(usb_ip_parts[-1]) +
worker.worker_num + 1)
phone_usb_ip = '.'.join(usb_ip_parts)
output = subprocess.check_output([
usbnet_script,
'-s', phone_cfg['serial'],
'-d', self.options[USB_GATEWAY],
'-h', self.options[USB_IP],
'-r', self.options[USB_NETWORK],
'-p', phone_usb_ip])
self.logger.debug(output)
else:
# This won't update the subprocess, but it will allow
# us to write out the updated values right away.
worker.phone_cfg = phone_cfg
Expand Down Expand Up @@ -520,6 +542,8 @@ def set_value(o, p, v):
if p not in o:
o[p] = v

set_value(options, USB_IP,
AutoPhone.USB_IP)
set_value(options, BUILD_CACHE_SIZE,
builds.BuildCache.MAX_NUM_BUILDS)
set_value(options, BUILD_CACHE_EXPIRES,
Expand Down Expand Up @@ -670,6 +694,18 @@ def sigterm_handler(signum, frame):
default=28001,
help='Port to listen for incoming connections, defaults '
'to 28001')
parser.add_option('--usb-network', type='string', dest='usb_network',
default=None,
help='IP or network address for ppp over usb connections. '
'If specified, set up adb ppp over usb connections '
'so that all traffic from the devices to the host or network '
'specified by usb_network passes through the '
'ppp over usb connection. Otherwise, use the default '
'network.')
parser.add_option('--usb-gateway', type='string', dest='usb_gateway',
default=None,
help='Ethernet device over which to route usb network '
'traffic. ')
parser.add_option('--cache', action='store', type='string', dest='cachefile',
default='autophone_cache.json',
help='Cache file to use, defaults to autophone_cache.json '
Expand Down
7 changes: 6 additions & 1 deletion options.py
Expand Up @@ -6,6 +6,8 @@
CLEAR_CACHE = 'clear_cache'
IPADDR = 'ipaddr'
PORT = 'port'
USB_NETWORK = 'usb_network'
USB_GATEWAY = 'usb_gateway'
CACHEFILE = 'cachefile'
LOGFILE = 'logfile'
LOGLEVEL = 'loglevel'
Expand All @@ -20,6 +22,7 @@
BUILD_CACHE_PORT = 'build_cache_port'

# ini file internal options
USB_IP = 'usb_ip'
BUILD_CACHE_SIZE = 'build_cache_size'
BUILD_CACHE_EXPIRES = 'build_cache_expires'
DEVICEMANAGER_RETRY_LIMIT = 'devicemanager_retry_limit'
Expand All @@ -38,6 +41,8 @@
CLEAR_CACHE: 'getboolean',
IPADDR: 'get',
PORT: 'getint',
USB_NETWORK: 'get',
USB_GATEWAY: 'get',
CACHEFILE: 'get',
LOGFILE: 'get',
LOGLEVEL: 'get',
Expand All @@ -54,6 +59,7 @@

# application configuration settings.
INI_OPTION_NAMES = {
USB_IP: 'get',
BUILD_CACHE_SIZE: 'getint',
BUILD_CACHE_EXPIRES: 'getint',
DEVICEMANAGER_RETRY_LIMIT: 'getint',
Expand All @@ -66,4 +72,3 @@
PHONE_CRASH_WINDOW: 'getint',
PHONE_CRASH_LIMIT: 'getint'
}

93 changes: 93 additions & 0 deletions usbnet.sh
@@ -0,0 +1,93 @@
#!/bin/bash

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

help() {
cat<<EOF
usage: usbnet.sh -d host_eth_device -s phone_serial_no -p phone_id -h host_ip -r usb_network
EOF
exit 2
}

getppp() {
local ppp
ppp=$(ip address list | grep $phone_ip | tail -1 | sed 's|.*\(ppp[0-9]*\)|\1|')
echo $ppp
}

options="d:s:p:h:r:"

while getopts $options optname; do
case $optname in
d)
# host ethernet device
host_device=$OPTARG
;;
s)
# device serialnumber
serialno=$OPTARG
;;
p)
# local/phone ip
phone_ip=$OPTARG
;;
h)
# remote/host ip
host_ip=$OPTARG
;;
r) # ip address or network address for usbnet destinations.
destination_net=$OPTARG
;;
esac
done

if [[ -z "$host_device" || -z "$serialno" || -z "$phone_ip" || -z "$host_ip" ]]; then
help
fi

echo "waiting for device $serialno"
sudo -i adb -s $serialno wait-for-device

sleep 10

#adb -s $serialno shell "svc wifi disable"

if [[ "$(sysctl net.ipv4.ip_forward)" != "net.ipv4.ip_forward = 1" ]]; then
echo "turning on ip forwarding"
sudo sysctl net.ipv4.ip_forward=1
fi

ppp=$(getppp)

if [[ -z "$ppp" ]]; then

echo "creating ppp"

sudo -i adb -s $serialno ppp "shell:pppd nodetach noauth noipdefault defaultroute /dev/tty" \
nodetach noauth noipdefault notty $host_ip:$phone_ip

sleep 10

if [[ -n "$destination_net" ]]; then
# Set the phone's route to use the usbnet host as gateway for the the destination_net.
sudo -i adb -s $serialno shell "ip route add $destination_net via $host_ip dev ppp0"
fi
fi

if ! sudo iptables -t nat -S POSTROUTING | grep -q $phone_ip; then

ppp=$(getppp)

if [[ -z "$ppp" ]]; then
echo "unable to get ppp device"
exit 1
fi
echo "creating $ppp for $serialno phone $phone_ip, host $host_ip"

echo "setting up nat forwarding for $ppp phone: $phone_ip, host: $host_device"
sudo iptables -t nat -A POSTROUTING -s $phone_ip -j MASQUERADE -o $host_device
sudo iptables -A FORWARD --in-interface $ppp -j ACCEPT
sudo iptables -A INPUT --in-interface $ppp -j ACCEPT
fi

0 comments on commit 14cdbd7

Please sign in to comment.