blob: 4f66201b0ed26eb86806dff8d0703b1e11a8a560 [file] [log] [blame]
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +08001/*
2 * Copyright (c) 2009, The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
32#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include <string.h>
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080034#include <errno.h>
35#include <sys/ioctl.h>
36#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include <sys/socket.h>
38#include <netinet/in.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039#include <arpa/inet.h>
40#include <linux/route.h>
41
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080042static inline int set_address(const char *address, struct sockaddr *sa) {
43 return inet_aton(address, &((struct sockaddr_in *)sa)->sin_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044}
45
Chung-yih Wang98c297f2009-04-14 18:45:32 -070046/* current support the following routing entries */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047/* route add default dev wlan0 */
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080048/* route add default gw 192.168.1.1 dev wlan0 */
49/* route add -net 192.168.1.2 netmask 255.255.255.0 gw 192.168.1.1 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
51int route_main(int argc, char *argv[])
52{
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080053 struct rtentry rt = {
54 .rt_dst = {.sa_family = AF_INET},
55 .rt_genmask = {.sa_family = AF_INET},
56 .rt_gateway = {.sa_family = AF_INET},
57 };
San Mehat83ec1812009-05-13 17:18:27 -070058
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080059 errno = EINVAL;
60 if (argc > 2 && !strcmp(argv[1], "add")) {
61 if (!strcmp(argv[2], "default")) {
62 /* route add default dev wlan0 */
63 if (argc > 4 && !strcmp(argv[3], "dev")) {
64 rt.rt_flags = RTF_UP | RTF_HOST;
65 rt.rt_dev = argv[4];
66 errno = 0;
67 goto apply;
68 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080070 /* route add default gw 192.168.1.1 dev wlan0 */
71 if (argc > 6 && !strcmp(argv[3], "gw") && !strcmp(argv[5], "dev")) {
San Mehat83ec1812009-05-13 17:18:27 -070072 rt.rt_flags = RTF_UP | RTF_GATEWAY;
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080073 rt.rt_dev = argv[6];
74 if (set_address(argv[4], &rt.rt_gateway)) {
75 errno = 0;
San Mehat83ec1812009-05-13 17:18:27 -070076 }
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080077 goto apply;
San Mehat83ec1812009-05-13 17:18:27 -070078 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 }
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080080
81 /* route add -net 192.168.1.2 netmask 255.255.255.0 gw 192.168.1.1 */
82 if (argc > 7 && !strcmp(argv[2], "-net") &&
83 !strcmp(argv[4], "netmask") && !strcmp(argv[6], "gw")) {
84 rt.rt_flags = RTF_UP | RTF_GATEWAY;
85 if (set_address(argv[3], &rt.rt_dst) &&
86 set_address(argv[5], &rt.rt_genmask) &&
87 set_address(argv[7], &rt.rt_gateway)) {
88 errno = 0;
89 }
90 goto apply;
91 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092 }
93
Chia-chi Yeh4a1465f2009-06-30 03:54:08 +080094apply:
95 if (!errno) {
96 int s = socket(AF_INET, SOCK_DGRAM, 0);
97 if (s != -1 && (ioctl(s, SIOCADDRT, &rt) != -1 || errno == EEXIST)) {
98 return 0;
99 }
100 }
101 puts(strerror(errno));
102 return errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103}