Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 4 | * |
| 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 | |
| 30 | namespace ndn { |
| 31 | namespace tests { |
| 32 | |
| 33 | bool NetworkConfigurationDetector::m_isInitialized = false; |
| 34 | bool NetworkConfigurationDetector::m_hasIpv4 = false; |
| 35 | bool NetworkConfigurationDetector::m_hasIpv6 = false; |
| 36 | |
| 37 | bool |
| 38 | NetworkConfigurationDetector::hasIpv4() |
| 39 | { |
| 40 | if (!m_isInitialized) { |
| 41 | detect(); |
| 42 | } |
| 43 | return m_hasIpv4; |
| 44 | } |
| 45 | |
| 46 | bool |
| 47 | NetworkConfigurationDetector::hasIpv6() |
| 48 | { |
| 49 | if (!m_isInitialized) { |
| 50 | detect(); |
| 51 | } |
| 52 | return m_hasIpv6; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | NetworkConfigurationDetector::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 |