• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
ArgumentsArguments34     Arguments()
35         : nethandle(NETWORK_UNSPECIFIED),
36           api_mode(ApiMode::EXPLICIT),
37           family(AF_UNSPEC),
38           attempts(1),
39           random_name(false),
40           arg1(nullptr) {}
41     ~Arguments();
42 
43     bool parseArguments(int argc, const char* argv[]);
44 
45     net_handle_t nethandle;
46     ApiMode api_mode;
47     sa_family_t family;
48     unsigned attempts;
49     bool random_name;
50     const char* arg1;
51 };
52 
53 
54 void printUsage(const char *progname);
55 
56 // If port is non-zero returns strings of the form "192.0.2.1:port" or
57 // "[2001:db8::1]:port", else it returns the bare IP string literal.
58 std::string inetSockaddrToString(const sockaddr* sa);
59 
60 
61 struct FdAutoCloser {
FdAutoCloserFdAutoCloser62     FdAutoCloser() : fd(-1) {}
FdAutoCloserFdAutoCloser63     /* not explicit */ FdAutoCloser(int fd) : fd(fd) {}
~FdAutoCloserFdAutoCloser64     ~FdAutoCloser() {
65         if (fd > -1) {
66             close(fd);
67         }
68         fd = -1;
69     }
70 
71     int fd;
72 };
73 
74 #endif  // SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_
75