Home | History | Annotate | Download | only in multinetwork
      1 /*
      2  * Copyright (C) 2016 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 
     18 #ifndef SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_
     19 #define SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_
     20 
     21 #include <sys/cdefs.h>
     22 #include <sys/socket.h>
     23 #include <unistd.h>
     24 #include <string>
     25 #include <android/multinetwork.h>
     26 
     27 enum class ApiMode {
     28     EXPLICIT,
     29     PROCESS,
     30 };
     31 
     32 
     33 struct Arguments {
     34     Arguments() : nethandle(NETWORK_UNSPECIFIED),
     35                   api_mode(ApiMode::EXPLICIT),
     36                   family(AF_UNSPEC),
     37                   arg1(nullptr) {}
     38     ~Arguments();
     39 
     40     bool parseArguments(int argc, const char* argv[]);
     41 
     42     net_handle_t nethandle;
     43     ApiMode api_mode;
     44     sa_family_t family;
     45     const char* arg1;
     46 };
     47 
     48 
     49 void printUsage(const char *progname);
     50 
     51 // If port is non-zero returns strings of the form "192.0.2.1:port" or
     52 // "[2001:db8::1]:port", else it returns the bare IP string literal.
     53 std::string inetSockaddrToString(const sockaddr* sa);
     54 
     55 
     56 struct FdAutoCloser {
     57     FdAutoCloser() : fd(-1) {}
     58     /* not explicit */ FdAutoCloser(int fd) : fd(fd) {}
     59     ~FdAutoCloser() {
     60         if (fd > -1) {
     61             close(fd);
     62         }
     63         fd = -1;
     64     }
     65 
     66     int fd;
     67 };
     68 
     69 #endif  // SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_
     70