| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 1 | /* | 
|  | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 |  | 
|  | 32 | #include <stdio.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | #include <string.h> | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 34 | #include <errno.h> | 
|  | 35 | #include <sys/ioctl.h> | 
|  | 36 | #include <sys/types.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #include <sys/socket.h> | 
|  | 38 | #include <netinet/in.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | #include <arpa/inet.h> | 
|  | 40 | #include <linux/route.h> | 
|  | 41 |  | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 42 | static 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | } | 
|  | 45 |  | 
| Chung-yih Wang | 98c297f | 2009-04-14 18:45:32 -0700 | [diff] [blame] | 46 | /* current support the following routing entries */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | /* route add default dev wlan0 */ | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 48 | /* 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | int route_main(int argc, char *argv[]) | 
|  | 52 | { | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 53 | 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 Mehat | 83ec181 | 2009-05-13 17:18:27 -0700 | [diff] [blame] | 58 |  | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 59 | 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")) { | 
| Daniel Baeyens | e898fad | 2009-10-07 00:53:32 +0200 | [diff] [blame] | 64 | rt.rt_flags = RTF_UP; | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 65 | rt.rt_dev = argv[4]; | 
|  | 66 | errno = 0; | 
|  | 67 | goto apply; | 
|  | 68 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 |  | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 70 | /* route add default gw 192.168.1.1 dev wlan0 */ | 
|  | 71 | if (argc > 6 && !strcmp(argv[3], "gw") && !strcmp(argv[5], "dev")) { | 
| San Mehat | 83ec181 | 2009-05-13 17:18:27 -0700 | [diff] [blame] | 72 | rt.rt_flags = RTF_UP | RTF_GATEWAY; | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 73 | rt.rt_dev = argv[6]; | 
|  | 74 | if (set_address(argv[4], &rt.rt_gateway)) { | 
|  | 75 | errno = 0; | 
| San Mehat | 83ec181 | 2009-05-13 17:18:27 -0700 | [diff] [blame] | 76 | } | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 77 | goto apply; | 
| San Mehat | 83ec181 | 2009-05-13 17:18:27 -0700 | [diff] [blame] | 78 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 | } | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 80 |  | 
|  | 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") && | 
| Dries Harnie | 2a74373 | 2010-01-18 17:44:33 +0100 | [diff] [blame] | 83 | !strcmp(argv[4], "netmask")) { | 
|  | 84 | if (!strcmp(argv[6], "gw")) { | 
|  | 85 | rt.rt_flags = RTF_UP | RTF_GATEWAY; | 
|  | 86 | if (set_address(argv[3], &rt.rt_dst) && | 
|  | 87 | set_address(argv[5], &rt.rt_genmask) && | 
|  | 88 | set_address(argv[7], &rt.rt_gateway)) { | 
|  | 89 | errno = 0; | 
|  | 90 | } | 
|  | 91 | goto apply; | 
|  | 92 | } else if (!strcmp(argv[6], "dev")) { | 
|  | 93 | rt.rt_flags = RTF_UP; | 
|  | 94 | rt.rt_dev = argv[7]; | 
|  | 95 | if (set_address(argv[3], &rt.rt_dst) && | 
|  | 96 | set_address(argv[5], &rt.rt_genmask)) { | 
|  | 97 | errno = 0; | 
|  | 98 | } | 
|  | 99 | goto apply; | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 100 | } | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 101 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Chia-chi Yeh | 4a1465f | 2009-06-30 03:54:08 +0800 | [diff] [blame] | 104 | apply: | 
|  | 105 | if (!errno) { | 
|  | 106 | int s = socket(AF_INET, SOCK_DGRAM, 0); | 
|  | 107 | if (s != -1 && (ioctl(s, SIOCADDRT, &rt) != -1 || errno == EEXIST)) { | 
|  | 108 | return 0; | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 | puts(strerror(errno)); | 
|  | 112 | return errno; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | } |