Project

General

Profile

Issue #2127 ยป usb_networking_device.sh

ariel enter, 09/18/2020 12:35 AM

 
1
#!/system/bin/sh
2

    
3
# Replicant USB Networking
4
# ========================
5
#
6
# Copyright (C) 2011,2014 Paul Kocialkowski and Riku Saikkonen
7
# Copyright (C) 2017 Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de>
8
#
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, either version 3 of the License, or
12
# (at your option) any later version.
13
#
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
18
#
19
# You should have received a copy of the GNU General Public License
20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21

    
22
set -e
23

    
24
USB_IFACE="rndis0"
25

    
26
# OpenNIC DNS servers
27
DNS1="193.183.98.154"
28
DNS2="87.98.175.85"
29

    
30
start_rndis () {
31
    svc usb setFunction rndis
32
}
33

    
34
stop_rndis () {
35
    svc usb setFunction
36
}
37

    
38
if_up () {
39
    ifconfig $USB_IFACE up
40
}
41

    
42
if_down () {
43
    ifconfig $USB_IFACE down
44
}
45

    
46
clear_iface () {
47
    echo "Clearing network configuration"
48
    ndc network destroy 1
49
    ndc interface clearaddrs "$iface"
50
}
51

    
52
setup_iface () {
53
    gateway=$(ip route show 0.0.0.0/0 dev $USB_IFACE | cut -d\  -f3)
54

    
55
    if [ "$gateway" = "" ]; then
56
	echo "Error: no route available"
57
	echo "Make sure your PC is configured correctly for usb networking"
58
	exit 1
59
    fi
60

    
61
    echo "Your gateway is $gateway"
62
    ndc network create 1
63
    ndc network interface add 1 "$USB_IFACE"
64
    ndc network route add 1 "$USB_IFACE"  0.0.0.0/0 "$gateway"
65
    ndc resolver setnetdns 1 "" "$DNS1" "$DNS2"
66
    ndc network default set 1
67
}
68

    
69
configure () {
70
    clear_iface
71

    
72
    echo "Configuring DHCP, please wait"
73
    if_up
74

    
75
    if [ $(dhcpcd $USB_IFACE) ]; then
76
       echo "Error configuring DHCP"
77
    fi
78

    
79
    setup_iface
80

    
81
    echo "DHCP configured"
82
}
83

    
84
case "$1" in
85
    start1)
86
	echo "Starting Replicant USB networking, phase 1"
87
	start_rndis
88
	;;
89
    start2)
90
	echo "Starting Replicant USB networking, phase 2"
91
	configure
92
	;;
93
    stop)
94
	echo "Stopping Replicant USB networking"
95
	if_down
96
	stop_rndis
97
	;;
98
    *)
99
	echo "Usage: sh $0 {start1|start2|stop}"
100
	;;
101
esac
102

    
103
exit 0
    (1-1/1)