blob: f8a88164e4b5b2d9ca789ad303b222d9f608fff2 [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
Alexander Afanasyev1286e022015-01-26 10:42:29 -080024#include <boost/asio/io_service.hpp>
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include <boost/asio/ip/address.hpp>
26#include <boost/asio/ip/basic_resolver.hpp>
27#include <boost/asio/ip/udp.hpp>
Alexander Afanasyev1286e022015-01-26 10:42:29 -080028#include <boost/range/iterator_range_core.hpp>
29
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::tests {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080031
32bool
33NetworkConfigurationDetector::hasIpv4()
34{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040035 if (!s_isInitialized) {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080036 detect();
37 }
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040038 return s_hasIpv4;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080039}
40
41bool
42NetworkConfigurationDetector::hasIpv6()
43{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040044 if (!s_isInitialized) {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080045 detect();
46 }
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040047 return s_hasIpv6;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080048}
49
50void
51NetworkConfigurationDetector::detect()
52{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040053 using BoostResolver = boost::asio::ip::basic_resolver<boost::asio::ip::udp>;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080054
55 boost::asio::io_service ioService;
56 BoostResolver resolver(ioService);
57
58 // The specified hostname must contain both A and AAAA records
59 BoostResolver::query query("a.root-servers.net", "");
60
61 boost::system::error_code errorCode;
62 BoostResolver::iterator begin = resolver.resolve(query, errorCode);
63 if (errorCode) {
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040064 s_isInitialized = true;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080065 return;
66 }
67 BoostResolver::iterator end;
68
69 for (const auto& i : boost::make_iterator_range(begin, end)) {
70 if (i.endpoint().address().is_v4()) {
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040071 s_hasIpv4 = true;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080072 }
73 else if (i.endpoint().address().is_v6()) {
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040074 s_hasIpv6 = true;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080075 }
76 }
77
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040078 s_isInitialized = true;
Alexander Afanasyev1286e022015-01-26 10:42:29 -080079}
80
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040081} // namespace ndn::tests