blob: 44cae399cd8ef9895638e481807ccc282c4cbfe6 [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
30bool
31NetworkConfigurationDetector::hasIpv4()
32{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040033 if (!s_isInitialized) {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080034 detect();
35 }
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040036 return s_hasIpv4;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080037}
38
39bool
40NetworkConfigurationDetector::hasIpv6()
41{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040042 if (!s_isInitialized) {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080043 detect();
44 }
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040045 return s_hasIpv6;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080046}
47
48void
49NetworkConfigurationDetector::detect()
50{
Davide Pesavento2f46d652023-11-09 23:40:01 -050051 boost::asio::io_context io;
52 boost::asio::ip::udp::resolver resolver(io);
Alexander Afanasyev1286e022015-01-26 10:42:29 -080053
Davide Pesavento2f46d652023-11-09 23:40:01 -050054 boost::system::error_code ec;
55 // The specified hostname must have both A and AAAA records
56 auto results = resolver.resolve("a.root-servers.net", "", ec);
Alexander Afanasyev1286e022015-01-26 10:42:29 -080057
Davide Pesavento2f46d652023-11-09 23:40:01 -050058 if (!ec) {
59 for (const auto& i : results) {
60 if (i.endpoint().address().is_v4()) {
61 s_hasIpv4 = true;
62 }
63 else if (i.endpoint().address().is_v6()) {
64 s_hasIpv6 = true;
65 }
Alexander Afanasyev1286e022015-01-26 10:42:29 -080066 }
67 }
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040068 s_isInitialized = true;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080069}
70
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040071} // namespace ndn::tests