blob: da377c466ca3a43c4a1bdd3307f306d995e6bd1b [file] [log] [blame]
Lorenzo Colitti65ae9962018-03-13 04:08:01 +00001/*
2 * Copyright 2018 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#include <regex>
18#include <string>
19#include <vector>
20
21#include <errno.h>
22#include <sys/socket.h>
23#include <sys/types.h>
24#include <unistd.h>
25
26#include <android-base/stringprintf.h>
27#include <android/multinetwork.h>
28
29#include "VtsHalNetNetdTestUtils.h"
30
31using android::base::StringPrintf;
32
33int checkNetworkExists(net_handle_t netHandle) {
34 int sock = socket(AF_INET6, SOCK_STREAM, 0);
35 if (sock == -1) {
36 return -ENOTSOCK;
37 }
38 int ret = android_setsocknetwork(netHandle, sock);
39 if (ret) {
40 ret = -errno;
41 }
42 close(sock);
43 return ret;
44}
45
Lorenzo Colittif95d0c22018-02-08 19:58:31 +090046// TODO: deduplicate this with system/netd/tests/binder_test.cpp.
Lorenzo Colitti65ae9962018-03-13 04:08:01 +000047static std::vector<std::string> runCommand(const std::string& command) {
48 std::vector<std::string> lines;
49 FILE* f;
50
51 if ((f = popen(command.c_str(), "r")) == nullptr) {
52 perror("popen");
53 return lines;
54 }
55
56 char* line = nullptr;
57 size_t bufsize = 0;
58 ssize_t linelen = 0;
59 while ((linelen = getline(&line, &bufsize, f)) >= 0) {
60 lines.push_back(std::string(line, linelen));
61 }
62
63 free(line);
64 pclose(f);
65 return lines;
66}
67
68static std::vector<std::string> listIpRules(const char* ipVersion) {
69 std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion);
70 return runCommand(command);
71}
72
Lorenzo Colittif95d0c22018-02-08 19:58:31 +090073int countMatchingIpRules(const std::string& regexString) {
Lorenzo Colitti65ae9962018-03-13 04:08:01 +000074 const std::regex regex(regexString, std::regex_constants::extended);
75 int matches = 0;
76
77 for (const char* version : {"-4", "-6"}) {
78 std::vector<std::string> rules = listIpRules(version);
79 for (const auto& rule : rules) {
80 if (std::regex_search(rule, regex)) {
81 matches++;
82 }
83 }
84 }
85
86 return matches;
87}
88
89int countRulesForFwmark(const uint32_t fwmark) {
Lorenzo Colittif95d0c22018-02-08 19:58:31 +090090 // Skip top nibble, which differs between rules.
91 std::string regex = StringPrintf("from all fwmark 0x[0-9a-f]+%x/.* lookup ", fwmark);
Lorenzo Colitti65ae9962018-03-13 04:08:01 +000092 return countMatchingIpRules(regex);
93}
Lorenzo Colittif95d0c22018-02-08 19:58:31 +090094
95int checkReachability(net_handle_t netHandle, const char* addrStr) {
96 addrinfo *ai, hints = {.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV};
97 int ret = getaddrinfo(addrStr, "53", &hints, &ai);
98 if (ret) {
99 return -EINVAL;
100 }
101
102 int sock = socket(ai->ai_family, SOCK_DGRAM, 0);
103 if (sock == -1 || android_setsocknetwork(netHandle, sock) == -1) {
104 ret = -errno;
105 freeaddrinfo(ai);
106 return ret;
107 }
108
109 ret = connect(sock, ai->ai_addr, ai->ai_addrlen);
110 close(sock);
111 if (ret == -1) {
112 ret = -errno;
113 }
114 freeaddrinfo(ai);
115 return ret;
116}