xref: /aosp_15_r20/system/extras/multinetwork/common.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker 
18*288bf522SAndroid Build Coastguard Worker #ifndef SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_
19*288bf522SAndroid Build Coastguard Worker #define SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker #include <sys/cdefs.h>
22*288bf522SAndroid Build Coastguard Worker #include <sys/socket.h>
23*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
24*288bf522SAndroid Build Coastguard Worker #include <string>
25*288bf522SAndroid Build Coastguard Worker #include <android/multinetwork.h>
26*288bf522SAndroid Build Coastguard Worker 
27*288bf522SAndroid Build Coastguard Worker enum class ApiMode {
28*288bf522SAndroid Build Coastguard Worker     EXPLICIT,
29*288bf522SAndroid Build Coastguard Worker     PROCESS,
30*288bf522SAndroid Build Coastguard Worker };
31*288bf522SAndroid Build Coastguard Worker 
32*288bf522SAndroid Build Coastguard Worker 
33*288bf522SAndroid Build Coastguard Worker struct Arguments {
ArgumentsArguments34*288bf522SAndroid Build Coastguard Worker     Arguments()
35*288bf522SAndroid Build Coastguard Worker         : nethandle(NETWORK_UNSPECIFIED),
36*288bf522SAndroid Build Coastguard Worker           api_mode(ApiMode::EXPLICIT),
37*288bf522SAndroid Build Coastguard Worker           family(AF_UNSPEC),
38*288bf522SAndroid Build Coastguard Worker           attempts(1),
39*288bf522SAndroid Build Coastguard Worker           random_name(false),
40*288bf522SAndroid Build Coastguard Worker           arg1(nullptr) {}
41*288bf522SAndroid Build Coastguard Worker     ~Arguments();
42*288bf522SAndroid Build Coastguard Worker 
43*288bf522SAndroid Build Coastguard Worker     bool parseArguments(int argc, const char* argv[]);
44*288bf522SAndroid Build Coastguard Worker 
45*288bf522SAndroid Build Coastguard Worker     net_handle_t nethandle;
46*288bf522SAndroid Build Coastguard Worker     ApiMode api_mode;
47*288bf522SAndroid Build Coastguard Worker     sa_family_t family;
48*288bf522SAndroid Build Coastguard Worker     unsigned attempts;
49*288bf522SAndroid Build Coastguard Worker     bool random_name;
50*288bf522SAndroid Build Coastguard Worker     const char* arg1;
51*288bf522SAndroid Build Coastguard Worker };
52*288bf522SAndroid Build Coastguard Worker 
53*288bf522SAndroid Build Coastguard Worker 
54*288bf522SAndroid Build Coastguard Worker void printUsage(const char *progname);
55*288bf522SAndroid Build Coastguard Worker 
56*288bf522SAndroid Build Coastguard Worker // If port is non-zero returns strings of the form "192.0.2.1:port" or
57*288bf522SAndroid Build Coastguard Worker // "[2001:db8::1]:port", else it returns the bare IP string literal.
58*288bf522SAndroid Build Coastguard Worker std::string inetSockaddrToString(const sockaddr* sa);
59*288bf522SAndroid Build Coastguard Worker 
60*288bf522SAndroid Build Coastguard Worker 
61*288bf522SAndroid Build Coastguard Worker struct FdAutoCloser {
FdAutoCloserFdAutoCloser62*288bf522SAndroid Build Coastguard Worker     FdAutoCloser() : fd(-1) {}
FdAutoCloserFdAutoCloser63*288bf522SAndroid Build Coastguard Worker     /* not explicit */ FdAutoCloser(int fd) : fd(fd) {}
~FdAutoCloserFdAutoCloser64*288bf522SAndroid Build Coastguard Worker     ~FdAutoCloser() {
65*288bf522SAndroid Build Coastguard Worker         if (fd > -1) {
66*288bf522SAndroid Build Coastguard Worker             close(fd);
67*288bf522SAndroid Build Coastguard Worker         }
68*288bf522SAndroid Build Coastguard Worker         fd = -1;
69*288bf522SAndroid Build Coastguard Worker     }
70*288bf522SAndroid Build Coastguard Worker 
71*288bf522SAndroid Build Coastguard Worker     int fd;
72*288bf522SAndroid Build Coastguard Worker };
73*288bf522SAndroid Build Coastguard Worker 
74*288bf522SAndroid Build Coastguard Worker #endif  // SYSTEM_EXTRAS_MULTINETWORK_COMMON_H_
75