2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <android/api-level.h>
21 #include <arpa/inet.h>
32 bool strEqual(const char *a, const char *b) {
33 return strcmp(a, b) == 0;
36 // Allow specifying network handles in decimal and hexadecimal.
37 bool parseNetworkHandle(const char *arg, net_handle_t *nethandle) {
38 if (arg == nullptr || !isdigit(arg[0]) || nethandle == nullptr) {
45 nh = strtoull(arg, &end, 0);
46 if (end != nullptr && *end == '\0') {
56 void printUsage(const char *progname) {
57 std::cerr << "Usage: " << progname
58 << " [--nethandle <nethandle>]"
59 << " [--mode explicit|process]"
60 << " [--family unspec|ipv4|ipv6]"
63 std::cerr << std::endl;
64 std::cerr << "Learn nethandle values from 'dumpsys connectivity --short' "
65 << "or 'dumpsys connectivity --diag'"
69 Arguments::~Arguments() {}
71 bool Arguments::parseArguments(int argc, const char* argv[]) {
72 if (argc < 1 || argv == nullptr) { return false; }
74 for (int i = 1; i < argc; i++) {
75 if (strEqual(argv[i], "--nethandle")) {
78 if (!parseNetworkHandle(argv[i], &nethandle)) {
79 std::cerr << "Failed to parse nethandle: '" << argv[i] << "'"
83 } else if (strEqual(argv[i], "--family")) {
86 if (strEqual(argv[i], "unspec")) {
88 } else if (strEqual(argv[i], "ipv4")) {
90 } else if (strEqual(argv[i], "ipv6")) {
95 } else if (strEqual(argv[i], "--mode")) {
98 if (strEqual(argv[i], "explicit")) {
99 api_mode = ApiMode::EXPLICIT;
100 } else if (strEqual(argv[i], "process")) {
101 api_mode = ApiMode::PROCESS;
105 } else if (arg1 == nullptr) {
113 if (arg1 != nullptr) {
122 std::string inetSockaddrToString(const sockaddr* sa) {
123 const bool is_ipv6 = (sa->sa_family == AF_INET6);
124 char host[INET6_ADDRSTRLEN];
125 char port[sizeof("65535")];
126 getnameinfo(sa, is_ipv6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in),
129 NI_NUMERICHOST | NI_NUMERICSERV);
131 if (port[0] == '0' || port[0] == '\0') {
132 return std::string(host);
134 return (is_ipv6 ? "[" : "") + std::string(host) + (is_ipv6 ? "]:" : ":") + std::string(port);