• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "dhcpserver.h"
18 
19 #include "log.h"
20 
21 #include <arpa/inet.h>
22 #include <net/if.h>
23 
usage(const char * program)24 static void usage(const char* program) {
25     ALOGE("Usage: %s -i <interface> -r <", program);
26 }
27 
main(int argc,char * argv[])28 int main(int argc, char* argv[]) {
29     in_addr_t rangeStart = 0;
30     in_addr_t rangeEnd = 0;
31     in_addr_t gateway = 0;
32     in_addr_t netmask = 0;
33     char* excludeInterfaceName = nullptr;
34     unsigned int excludeInterfaceIndex = 0;
35     for (int i = 1; i < argc; ++i) {
36         if (strcmp("--range", argv[i]) == 0) {
37             if (i + 1 >= argc) {
38                 ALOGE("ERROR: Missing argument to --range parameter");
39                 usage(argv[0]);
40                 return 1;
41             }
42             char* divider = strchr(argv[i + 1], ',');
43             if (divider != nullptr) {
44                 *divider = '\0';
45                 struct in_addr address;
46                 if (inet_pton(AF_INET, argv[i + 1], &address) > 0) {
47                     rangeStart = address.s_addr;
48                 } else {
49                     ALOGE("ERROR: Invalid start address '%s'", argv[i + 1]);
50                     usage(argv[0]);
51                     return 1;
52                 }
53                 char* next = divider + 1;
54                 if (inet_pton(AF_INET, next, &address) > 0) {
55                     rangeEnd = address.s_addr;
56                 } else {
57                     ALOGE("ERROR: Invalid end address '%s'", next);
58                     usage(argv[0]);
59                     return 1;
60                 }
61             } else {
62                 ALOGE("ERROR: Invalid --range parameter '%s'", argv[i + 1]);
63                 usage(argv[0]);
64                 return 1;
65             }
66             ++i;
67         } else if (strcmp("--gateway", argv[i]) == 0) {
68             if (i + 1 >= argc) {
69                 ALOGE("ERROR: Missing argument to --gateway parameter");
70                 usage(argv[0]);
71                 return 1;
72             }
73             struct in_addr address;
74             if (inet_pton(AF_INET, argv[i + 1], &address) > 0) {
75                 gateway = address.s_addr;
76             } else {
77                 ALOGE("ERROR: Invalid gateway '%s'", argv[i + 1]);
78                 usage(argv[0]);
79                 return 1;
80             }
81         } else if (strcmp("--netmask", argv[i]) == 0) {
82             if (i + 1 >= argc) {
83                 ALOGE("ERROR: Missing argument to --netmask parameter");
84                 usage(argv[0]);
85                 return 1;
86             }
87             struct in_addr address;
88             if (inet_pton(AF_INET, argv[i + 1], &address) > 0) {
89                 netmask = address.s_addr;
90             } else {
91                 ALOGE("ERROR: Invalid netmask '%s'", argv[i + 1]);
92                 usage(argv[0]);
93                 return 1;
94             }
95         } else if (strcmp("--exclude-interface", argv[i]) == 0) {
96             if (i + 1 >= argc) {
97                 ALOGE("ERROR: Missing argument to "
98                      "--exclude-interfaces parameter");
99                 usage(argv[0]);
100                 return 1;
101             }
102             excludeInterfaceName = argv[i + 1];
103             excludeInterfaceIndex = if_nametoindex(excludeInterfaceName);
104             if (excludeInterfaceIndex == 0) {
105                 ALOGE("ERROR: Invalid argument '%s' to --exclude-interface",
106                      argv[i + 1]);
107                 usage(argv[0]);
108                 return 1;
109             }
110         }
111     }
112 
113     if (rangeStart == 0 || rangeEnd == 0) {
114         ALOGE("ERROR: Missing or invalid --range argument");
115         usage(argv[0]);
116         return 1;
117     }
118     if (gateway == 0) {
119         ALOGE("ERROR: Missing or invalid --gateway argument");
120         usage(argv[0]);
121         return 1;
122     }
123     if (netmask == 0) {
124         ALOGE("ERROR: Missing or invalid --netmask argument");
125         usage(argv[0]);
126         return 1;
127     }
128 
129     DhcpServer server(rangeStart,
130                       rangeEnd,
131                       netmask,
132                       gateway,
133                       excludeInterfaceIndex);
134     Result res = server.init();
135     if (!res) {
136         ALOGE("Failed to initialize DHCP server: %s\n", res.c_str());
137         return 1;
138     }
139 
140     res = server.run();
141     if (!res) {
142         ALOGE("DHCP server failed: %s\n", res.c_str());
143         return 1;
144     }
145     // This is weird and shouldn't happen, the server should run forever.
146     return 0;
147 }
148 
149 
150