Lorenzo Colitti | 65ae996 | 2018-03-13 04:08:01 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 31 | using android::base::StringPrintf; |
| 32 | |
| 33 | int 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 Colitti | f95d0c2 | 2018-02-08 19:58:31 +0900 | [diff] [blame] | 46 | // TODO: deduplicate this with system/netd/tests/binder_test.cpp. |
Lorenzo Colitti | 65ae996 | 2018-03-13 04:08:01 +0000 | [diff] [blame] | 47 | static 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 | |
| 68 | static 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 Colitti | f95d0c2 | 2018-02-08 19:58:31 +0900 | [diff] [blame] | 73 | int countMatchingIpRules(const std::string& regexString) { |
Lorenzo Colitti | 65ae996 | 2018-03-13 04:08:01 +0000 | [diff] [blame] | 74 | 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 | |
| 89 | int countRulesForFwmark(const uint32_t fwmark) { |
Lorenzo Colitti | f95d0c2 | 2018-02-08 19:58:31 +0900 | [diff] [blame] | 90 | // Skip top nibble, which differs between rules. |
| 91 | std::string regex = StringPrintf("from all fwmark 0x[0-9a-f]+%x/.* lookup ", fwmark); |
Lorenzo Colitti | 65ae996 | 2018-03-13 04:08:01 +0000 | [diff] [blame] | 92 | return countMatchingIpRules(regex); |
| 93 | } |
Lorenzo Colitti | f95d0c2 | 2018-02-08 19:58:31 +0900 | [diff] [blame] | 94 | |
| 95 | int 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 | } |