blob: 9aa196b3e5002edae7d21b729f448655161f29fa [file] [log] [blame]
Alexander Afanasyev1286e022015-01-26 10:42:29 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi25467942017-06-30 02:53:14 +00003 * Copyright (c) 2013-2017 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
22#include "network-configuration-detector.hpp"
23
24#include <boost/asio/ip/address.hpp>
25#include <boost/asio/ip/udp.hpp>
26#include <boost/asio/ip/basic_resolver.hpp>
27#include <boost/asio/io_service.hpp>
28#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