Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "tests/unit/net/network-configuration-detector.hpp" |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 24 | #include <boost/asio/io_context.hpp> |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include <boost/asio/ip/address.hpp> |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include <boost/asio/ip/udp.hpp> |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 28 | namespace ndn::tests { |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 30 | void |
| 31 | NetworkConfigurationDetector::detect() |
| 32 | { |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame] | 33 | static bool isInitialized = false; |
| 34 | if (isInitialized) { |
| 35 | return; |
| 36 | } |
| 37 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 38 | boost::asio::io_context io; |
| 39 | boost::asio::ip::udp::resolver resolver(io); |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 40 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 41 | boost::system::error_code ec; |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame] | 42 | // Use a hostname known to have both A and AAAA records |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 43 | auto results = resolver.resolve("a.root-servers.net", "", ec); |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 44 | if (!ec) { |
| 45 | for (const auto& i : results) { |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame] | 46 | s_hasIp = true; |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 47 | if (i.endpoint().address().is_v4()) { |
| 48 | s_hasIpv4 = true; |
| 49 | } |
| 50 | else if (i.endpoint().address().is_v6()) { |
| 51 | s_hasIpv6 = true; |
| 52 | } |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame] | 55 | |
| 56 | if (!s_hasIp) { |
| 57 | s_hasIp.message() << "IP connectivity is unavailable"; |
| 58 | } |
| 59 | if (!s_hasIpv4) { |
| 60 | s_hasIpv4.message() << "IPv4 connectivity is unavailable"; |
| 61 | } |
| 62 | if (!s_hasIpv6) { |
| 63 | s_hasIpv6.message() << "IPv6 connectivity is unavailable"; |
| 64 | } |
| 65 | isInitialized = true; |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 68 | } // namespace ndn::tests |