• 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 #include "common.h"
19 
20 #include <android/api-level.h>
21 #include <arpa/inet.h>
22 #include <assert.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <iostream>
28 
29 
30 namespace {
31 
strEqual(const char * a,const char * b)32 bool strEqual(const char *a, const char *b) {
33     return strcmp(a, b) == 0;
34 }
35 
36 // Allow specifying network handles in decimal and hexadecimal.
parseNetworkHandle(const char * arg,net_handle_t * nethandle)37 bool parseNetworkHandle(const char *arg, net_handle_t *nethandle) {
38     if (arg == nullptr || !isdigit(arg[0]) || nethandle == nullptr) {
39         return false;
40     }
41 
42     net_handle_t nh;
43     char *end = nullptr;
44 
45     nh = strtoull(arg, &end, 0);
46     if (end != nullptr && *end == '\0') {
47         *nethandle = nh;
48         return true;
49     }
50     return false;
51 }
52 
53 }  // namespace
54 
55 
printUsage(const char * progname)56 void printUsage(const char *progname) {
57     std::cerr << "Usage: " << progname << " [--nethandle <nethandle>]"
58               << " [--mode explicit|process]"
59               << " [--family unspec|ipv4|ipv6]"
60               << " [--attempts <N>]"
61               << " --randomname | name" << std::endl
62               << std::endl
63               << "Learn nethandle values from 'dumpsys connectivity --short' "
64               << "or 'dumpsys connectivity --diag'" << std::endl;
65 }
66 
~Arguments()67 Arguments::~Arguments() {}
68 
parseArguments(int argc,const char * argv[])69 bool Arguments::parseArguments(int argc, const char* argv[]) {
70     if (argc < 1 || argv == nullptr) { return false; }
71 
72     for (int i = 1; i < argc; i++) {
73         if (strEqual(argv[i], "--nethandle")) {
74             i++;
75             if (argc == i) break;
76             if (!parseNetworkHandle(argv[i], &nethandle)) {
77                 std::cerr << "Failed to parse nethandle: '" << argv[i] << "'"
78                           << std::endl;
79                 break;
80             }
81         } else if (strEqual(argv[i], "--family")) {
82             i++;
83             if (argc == i) break;
84             if (strEqual(argv[i], "unspec")) {
85                 family = AF_UNSPEC;
86             } else if (strEqual(argv[i], "ipv4")) {
87                 family = AF_INET;
88             } else if (strEqual(argv[i], "ipv6")) {
89                 family = AF_INET6;
90             } else {
91                 break;
92             }
93         } else if (strEqual(argv[i], "--mode")) {
94             i++;
95             if (argc == i) break;
96             if (strEqual(argv[i], "explicit")) {
97                 api_mode = ApiMode::EXPLICIT;
98             } else if (strEqual(argv[i], "process")) {
99                 api_mode = ApiMode::PROCESS;
100             } else {
101                 break;
102             }
103         } else if (strEqual(argv[i], "--attempts")) {
104             i++;
105             if (argc == i) break;
106             char* endptr;
107             attempts = strtoul(argv[i], &endptr, 10);
108             if (*endptr != '\0') {
109                 std::cerr << "Failed to parse arguments: '" << argv[i] << "'" << std::endl;
110                 break;
111             }
112         } else if (strEqual(argv[i], "--randomname")) {
113             time_t t;
114             time(&t);
115             srand((unsigned long)time);
116             random_name = true;
117         } else if (arg1 == nullptr && !random_name) {
118             arg1 = argv[i];
119         } else {
120             arg1 = nullptr;
121             break;
122         }
123     }
124 
125     if (random_name || arg1 != nullptr) {
126         return true;
127     }
128 
129     printUsage(argv[0]);
130     return false;
131 }
132 
133 
inetSockaddrToString(const sockaddr * sa)134 std::string inetSockaddrToString(const sockaddr* sa) {
135     const bool is_ipv6 = (sa->sa_family == AF_INET6);
136     char host[INET6_ADDRSTRLEN];
137     char port[sizeof("65535")];
138     getnameinfo(sa, is_ipv6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in),
139                 host, sizeof(host),
140                 port, sizeof(port),
141                 NI_NUMERICHOST | NI_NUMERICSERV);
142 
143     if (port[0] == '0' || port[0] == '\0') {
144         return std::string(host);
145     }
146     return (is_ipv6 ? "[" : "") + std::string(host) + (is_ipv6 ? "]:" : ":") + std::string(port);
147 }
148