Home | History | Annotate | Download | only in server
      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 
     24 static void usage(const char* program) {
     25     ALOGE("Usage: %s -i <interface> -r <", program);
     26 }
     27 
     28 int main(int argc, char* argv[]) {
     29     char* excludeInterfaceName = nullptr;
     30     unsigned int excludeInterfaceIndex = 0;
     31     for (int i = 1; i < argc; ++i) {
     32         if (strcmp("--exclude-interface", argv[i]) == 0) {
     33             if (i + 1 >= argc) {
     34                 ALOGE("ERROR: Missing argument to "
     35                      "--exclude-interfaces parameter");
     36                 usage(argv[0]);
     37                 return 1;
     38             }
     39             excludeInterfaceName = argv[i + 1];
     40             excludeInterfaceIndex = if_nametoindex(excludeInterfaceName);
     41             if (excludeInterfaceIndex == 0) {
     42                 ALOGE("ERROR: Invalid argument '%s' to --exclude-interface",
     43                      argv[i + 1]);
     44                 usage(argv[0]);
     45                 return 1;
     46             }
     47         }
     48     }
     49 
     50     DhcpServer server(excludeInterfaceIndex);
     51     Result res = server.init();
     52     if (!res) {
     53         ALOGE("Failed to initialize DHCP server: %s\n", res.c_str());
     54         return 1;
     55     }
     56 
     57     res = server.run();
     58     if (!res) {
     59         ALOGE("DHCP server failed: %s\n", res.c_str());
     60         return 1;
     61     }
     62     // This is weird and shouldn't happen, the server should run forever.
     63     return 0;
     64 }
     65 
     66 
     67