blob: e9b39dffb25e7cac7a361dc881eafbfd4bc5cb26 [file] [log] [blame]
Alexander Afanasyev1286e022015-01-26 10:42:29 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
Davide Pesavento13260512024-02-01 21:16:04 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Alexander Afanasyev1286e022015-01-26 10:42:29 -08004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "tests/unit/net/network-configuration-detector.hpp"
Alexander Afanasyev1286e022015-01-26 10:42:29 -080023
Davide Pesavento2f46d652023-11-09 23:40:01 -050024#include <boost/asio/io_context.hpp>
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include <boost/asio/ip/address.hpp>
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include <boost/asio/ip/udp.hpp>
Alexander Afanasyev1286e022015-01-26 10:42:29 -080027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080029
Alexander Afanasyev1286e022015-01-26 10:42:29 -080030void
31NetworkConfigurationDetector::detect()
32{
Davide Pesavento13260512024-02-01 21:16:04 -050033 static bool isInitialized = false;
34 if (isInitialized) {
35 return;
36 }
37
Davide Pesavento2f46d652023-11-09 23:40:01 -050038 boost::asio::io_context io;
39 boost::asio::ip::udp::resolver resolver(io);
Alexander Afanasyev1286e022015-01-26 10:42:29 -080040
Davide Pesavento2f46d652023-11-09 23:40:01 -050041 boost::system::error_code ec;
Davide Pesavento13260512024-02-01 21:16:04 -050042 // Use a hostname known to have both A and AAAA records
Davide Pesavento2f46d652023-11-09 23:40:01 -050043 auto results = resolver.resolve("a.root-servers.net", "", ec);
Davide Pesavento2f46d652023-11-09 23:40:01 -050044 if (!ec) {
45 for (const auto& i : results) {
Davide Pesavento13260512024-02-01 21:16:04 -050046 s_hasIp = true;
Davide Pesavento2f46d652023-11-09 23:40:01 -050047 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 Afanasyev1286e022015-01-26 10:42:29 -080053 }
54 }
Davide Pesavento13260512024-02-01 21:16:04 -050055
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 Afanasyev1286e022015-01-26 10:42:29 -080066}
67
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040068} // namespace ndn::tests