blob: a180fb55ccf10c517a3ba87eea2d69a9d729436f [file] [log] [blame]
Erik Kline871e63d2016-01-22 09:07:44 +09001/*
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
27enum class ApiMode {
28 EXPLICIT,
29 PROCESS,
30};
31
32
33struct Arguments {
Lorenzo Colittifa48d722020-11-05 19:44:46 +090034 Arguments()
35 : nethandle(NETWORK_UNSPECIFIED),
36 api_mode(ApiMode::EXPLICIT),
37 family(AF_UNSPEC),
38 attempts(1),
39 random_name(false),
40 arg1(nullptr) {}
Erik Kline871e63d2016-01-22 09:07:44 +090041 ~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;
Lorenzo Colittifa48d722020-11-05 19:44:46 +090048 unsigned attempts;
49 bool random_name;
Erik Kline871e63d2016-01-22 09:07:44 +090050 const char* arg1;
51};
52
53
54void 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.
58std::string inetSockaddrToString(const sockaddr* sa);
59
60
61struct FdAutoCloser {
62 FdAutoCloser() : fd(-1) {}
63 /* not explicit */ FdAutoCloser(int fd) : fd(fd) {}
64 ~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_