Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 844b093 | 2018-05-07 01:00:16 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 Regents of the University of California, |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
| 28 | #include "address-converter.hpp" |
| 29 | |
Davide Pesavento | 844b093 | 2018-05-07 01:00:16 -0400 | [diff] [blame^] | 30 | #include <net/if.h> // for if_indextoname() |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace ip { |
| 34 | |
| 35 | optional<std::string> |
| 36 | scopeNameFromId(unsigned int scopeId) |
| 37 | { |
| 38 | char buffer[IFNAMSIZ]; |
| 39 | auto scopeName = if_indextoname(scopeId, buffer); |
| 40 | if (scopeName != nullptr) { |
| 41 | return std::string(scopeName); |
| 42 | } |
| 43 | return nullopt; |
| 44 | } |
| 45 | |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 46 | boost::asio::ip::address |
| 47 | addressFromString(const std::string& address, boost::system::error_code& ec) |
| 48 | { |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 49 | return boost::asio::ip::address::from_string(address, ec); |
Yanbiao Li | ac8b4ca | 2017-07-10 02:27:50 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | boost::asio::ip::address |
| 53 | addressFromString(const std::string& address) |
| 54 | { |
| 55 | boost::system::error_code ec; |
| 56 | auto addr = addressFromString(address, ec); |
| 57 | if (ec) { |
| 58 | BOOST_THROW_EXCEPTION(boost::system::system_error(ec)); |
| 59 | } |
| 60 | return addr; |
| 61 | } |
| 62 | |
| 63 | boost::asio::ip::address_v6 |
| 64 | addressV6FromString(const std::string& address, boost::system::error_code& ec) |
| 65 | { |
| 66 | auto addr = addressFromString(address, ec); |
| 67 | if (ec || addr.is_v4()) { |
| 68 | ec = boost::asio::error::invalid_argument; |
| 69 | return {}; |
| 70 | } |
| 71 | return addr.to_v6(); |
| 72 | } |
| 73 | |
| 74 | boost::asio::ip::address_v6 |
| 75 | addressV6FromString(const std::string& address) |
| 76 | { |
| 77 | boost::system::error_code ec; |
| 78 | auto addr = addressV6FromString(address, ec); |
| 79 | if (ec) { |
| 80 | BOOST_THROW_EXCEPTION(boost::system::system_error(ec)); |
| 81 | } |
| 82 | return addr; |
| 83 | } |
| 84 | |
| 85 | } // namespace ip |
| 86 | } // namespace ndn |