blob: 84e1244405a852979a560060c5041a17e54b79b0 [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/*
3 * Copyright (c) 2013-2018 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
30namespace ndn {
31namespace tests {
32
33bool NetworkConfigurationDetector::m_isInitialized = false;
34bool NetworkConfigurationDetector::m_hasIpv4 = false;
35bool NetworkConfigurationDetector::m_hasIpv6 = false;
36
37bool
38NetworkConfigurationDetector::hasIpv4()
39{
40 if (!m_isInitialized) {
41 detect();
42 }
43 return m_hasIpv4;
44}
45
46bool
47NetworkConfigurationDetector::hasIpv6()
48{
49 if (!m_isInitialized) {
50 detect();
51 }
52 return m_hasIpv6;
53}
54
55void
56NetworkConfigurationDetector::detect()
57{
58 typedef boost::asio::ip::basic_resolver<boost::asio::ip::udp> BoostResolver;
59
60 boost::asio::io_service ioService;
61 BoostResolver resolver(ioService);
62
63 // The specified hostname must contain both A and AAAA records
64 BoostResolver::query query("a.root-servers.net", "");
65
66 boost::system::error_code errorCode;
67 BoostResolver::iterator begin = resolver.resolve(query, errorCode);
68 if (errorCode) {
69 m_isInitialized = true;
70 return;
71 }
72 BoostResolver::iterator end;
73
74 for (const auto& i : boost::make_iterator_range(begin, end)) {
75 if (i.endpoint().address().is_v4()) {
76 m_hasIpv4 = true;
77 }
78 else if (i.endpoint().address().is_v6()) {
79 m_hasIpv6 = true;
80 }
81 }
82
83 m_isInitialized = true;
84}
85
86} // namespace tests
87} // namespace ndn