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 "dhcpclient.h"
18 #include "log.h"
19 
usage(const char * program)20 static void usage(const char* program) {
21     ALOGE("Usage: %s [--no-gateway] -i <interface>", program);
22     ALOGE("  If the optional parameter --no-gateway is specified the client");
23     ALOGE("  will not configure the default gateway of the system.");
24 }
25 
main(int argc,char * argv[])26 int main(int argc, char* argv[]) {
27     if (argc < 3) {
28         usage(argv[0]);
29         return 1;
30     }
31     const char* interfaceName = nullptr;
32     uint32_t options = 0;
33 
34     for (int i = 1; i < argc; ++i) {
35         if (strcmp(argv[i], "-i") == 0) {
36             if (i + 1 < argc) {
37                 interfaceName = argv[++i];
38             } else {
39                 ALOGE("ERROR: -i parameter needs an argument");
40                 usage(argv[0]);
41                 return 1;
42             }
43         } else if (strcmp(argv[i], "--no-gateway") == 0) {
44             options |= static_cast<uint32_t>(ClientOption::NoGateway);
45         } else {
46             ALOGE("ERROR: unknown parameters %s", argv[i]);
47             usage(argv[0]);
48             return 1;
49         }
50     }
51     if (interfaceName == nullptr) {
52         ALOGE("ERROR: No interface specified");
53         usage(argv[0]);
54         return 1;
55     }
56 
57     DhcpClient client(options);
58     Result res = client.init(interfaceName);
59     if (!res) {
60         ALOGE("Failed to initialize DHCP client: %s\n", res.c_str());
61         return 1;
62     }
63 
64     res = client.run();
65     if (!res) {
66         ALOGE("DHCP client failed: %s\n", res.c_str());
67         return 1;
68     }
69     // This is weird and shouldn't happen, the client should run forever.
70     return 0;
71 }
72 
73