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