Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
| 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 | * Parts of this implementation is based on daemondo command of MacPorts |
| 23 | * (https://www.macports.org/): |
| 24 | * |
| 25 | * Copyright (c) 2005-2007 James Berry <jberry@macports.org> |
| 26 | * All rights reserved. |
| 27 | * |
| 28 | * Redistribution and use in source and binary forms, with or without |
| 29 | * modification, are permitted provided that the following conditions |
| 30 | * are met: |
| 31 | * 1. Redistributions of source code must retain the above copyright |
| 32 | * notice, this list of conditions and the following disclaimer. |
| 33 | * 2. Redistributions in binary form must reproduce the above copyright |
| 34 | * notice, this list of conditions and the following disclaimer in the |
| 35 | * documentation and/or other materials provided with the distribution. |
| 36 | * 3. Neither the name of The MacPorts Project nor the names of its contributors |
| 37 | * may be used to endorse or promote products derived from this software |
| 38 | * without specific prior written permission. |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 41 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 44 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 45 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 46 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 47 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 48 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 50 | * POSSIBILITY OF SUCH DAMAGE. |
| 51 | */ |
| 52 | |
| 53 | #include "ndn-cxx-config.hpp" |
| 54 | |
| 55 | #include "network-monitor.hpp" |
| 56 | #include "scheduler.hpp" |
| 57 | #include "scheduler-scoped-event-id.hpp" |
| 58 | |
| 59 | #if defined(NDN_CXX_HAVE_COREFOUNDATION_COREFOUNDATION_H) |
| 60 | |
| 61 | #include <CoreFoundation/CoreFoundation.h> |
| 62 | #include <SystemConfiguration/SystemConfiguration.h> |
| 63 | |
| 64 | namespace ndn { |
| 65 | namespace util { |
| 66 | |
| 67 | class NetworkMonitor::Impl |
| 68 | { |
| 69 | public: |
| 70 | Impl(boost::asio::io_service& io) |
| 71 | : scheduler(io) |
| 72 | , cfLoopEvent(scheduler) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | scheduleCfLoop() |
| 78 | { |
| 79 | // poll each second for new events |
| 80 | cfLoopEvent = scheduler.scheduleEvent(time::seconds(1), bind(&Impl::pollCfLoop, this)); |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | afterNotificationCenterEvent(CFNotificationCenterRef center, void *observer, CFStringRef name, |
| 85 | const void *object, CFDictionaryRef userInfo) |
| 86 | { |
| 87 | static_cast<NetworkMonitor*>(observer)->onNetworkStateChanged(); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | |
| 92 | void |
| 93 | pollCfLoop() |
| 94 | { |
| 95 | // this should dispatch ready events and exit |
| 96 | CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); |
| 97 | scheduleCfLoop(); |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | Scheduler scheduler; |
| 102 | scheduler::ScopedEventId cfLoopEvent; |
| 103 | }; |
| 104 | |
| 105 | NetworkMonitor::NetworkMonitor(boost::asio::io_service& io) |
| 106 | : m_impl(new Impl(io)) |
| 107 | { |
| 108 | m_impl->scheduleCfLoop(); |
| 109 | |
| 110 | // Potentially useful System Configuration regex patterns: |
| 111 | // |
| 112 | // State:/Network/Interface/.*/Link |
| 113 | // State:/Network/Interface/.*/IPv4 |
| 114 | // State:/Network/Interface/.*/IPv6 |
| 115 | // |
| 116 | // State:/Network/Global/DNS |
| 117 | // State:/Network/Global/IPv4 |
| 118 | // |
| 119 | // Potentially useful notifications from Darwin Notify Center: |
| 120 | // |
| 121 | // com.apple.system.config.network_change |
| 122 | |
| 123 | // network change observations |
| 124 | CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 125 | static_cast<void*>(this), |
| 126 | &NetworkMonitor::Impl::afterNotificationCenterEvent, |
| 127 | CFSTR("com.apple.system.config.network_change"), |
| 128 | nullptr, // object to observe |
| 129 | CFNotificationSuspensionBehaviorDeliverImmediately); |
| 130 | } |
| 131 | |
| 132 | NetworkMonitor::~NetworkMonitor() |
| 133 | { |
| 134 | CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 135 | static_cast<void*>(this)); |
| 136 | } |
| 137 | |
| 138 | } // namespace util |
| 139 | } // namespace ndn |
| 140 | |
| 141 | // done with defined(NDN_CXX_HAVE_COREFOUNDATION_COREFOUNDATION_H) |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame^] | 142 | #elif defined(NDN_CXX_HAVE_RTNETLINK) |
| 143 | |
| 144 | #include <boost/asio.hpp> |
| 145 | |
| 146 | #include <netinet/in.h> |
| 147 | #include <linux/netlink.h> |
| 148 | #include <linux/rtnetlink.h> |
| 149 | #include <net/if.h> |
| 150 | |
| 151 | #include <cerrno> |
| 152 | #include <cstring> |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 153 | |
| 154 | namespace ndn { |
| 155 | namespace util { |
| 156 | |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame^] | 157 | const size_t NETLINK_BUFFER_SIZE = 4096; |
| 158 | |
| 159 | class NetworkMonitor::Impl |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 160 | { |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame^] | 161 | public: |
| 162 | Impl(NetworkMonitor& nm, boost::asio::io_service& io) |
| 163 | : m_nm(nm) |
| 164 | , m_socket(io) |
| 165 | { |
| 166 | int fd = ::socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| 167 | if (fd < 0) |
| 168 | throw Error(std::string("Cannot create netlink socket (") + std::strerror(errno) + ")"); |
| 169 | |
| 170 | sockaddr_nl addr{}; |
| 171 | addr.nl_family = AF_NETLINK; |
| 172 | addr.nl_groups = RTMGRP_LINK | |
| 173 | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE | |
| 174 | RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE; |
| 175 | |
| 176 | if (::bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) { |
| 177 | throw Error(std::string("Cannot bind on netlink socket (") + std::strerror(errno) + ")"); |
| 178 | } |
| 179 | |
| 180 | m_socket.assign(fd); |
| 181 | |
| 182 | m_socket.async_read_some(boost::asio::buffer(m_buffer, NETLINK_BUFFER_SIZE), |
| 183 | bind(&Impl::onReceiveRtNetlink, this, _1, _2)); |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | void |
| 188 | onReceiveRtNetlink(const boost::system::error_code& error, size_t nBytesReceived) |
| 189 | { |
| 190 | if (error) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | const nlmsghdr* nlh = reinterpret_cast<const nlmsghdr*>(m_buffer); |
| 195 | while ((NLMSG_OK(nlh, nBytesReceived)) && (nlh->nlmsg_type != NLMSG_DONE)) { |
| 196 | if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR || |
| 197 | nlh->nlmsg_type == RTM_NEWLINK || nlh->nlmsg_type == RTM_DELLINK || |
| 198 | nlh->nlmsg_type == RTM_NEWROUTE || nlh->nlmsg_type == RTM_DELROUTE) { |
| 199 | m_nm.onNetworkStateChanged(); |
| 200 | break; |
| 201 | } |
| 202 | nlh = NLMSG_NEXT(nlh, nBytesReceived); |
| 203 | } |
| 204 | |
| 205 | m_socket.async_read_some(boost::asio::buffer(m_buffer, NETLINK_BUFFER_SIZE), |
| 206 | bind(&Impl::onReceiveRtNetlink, this, _1, _2)); |
| 207 | } |
| 208 | |
| 209 | private: |
| 210 | NetworkMonitor& m_nm; |
| 211 | uint8_t m_buffer[NETLINK_BUFFER_SIZE]; |
| 212 | |
| 213 | boost::asio::posix::stream_descriptor m_socket; |
| 214 | }; |
| 215 | |
| 216 | |
| 217 | |
| 218 | NetworkMonitor::NetworkMonitor(boost::asio::io_service& io) |
| 219 | : m_impl(new Impl(*this, io)) |
| 220 | { |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | NetworkMonitor::~NetworkMonitor() |
| 224 | { |
| 225 | } |
| 226 | |
| 227 | } // namespace util |
| 228 | } // namespace ndn |
| 229 | |
Alexander Afanasyev | 7b3080f | 2015-01-28 21:21:01 -0800 | [diff] [blame^] | 230 | // done with defined(NDN_CXX_HAVE_RTNETLINK) |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 231 | #else // do not support network monitoring operations |
| 232 | |
| 233 | namespace ndn { |
| 234 | namespace util { |
| 235 | |
| 236 | class NetworkMonitor::Impl |
| 237 | { |
Alexander Afanasyev | e6c65e2 | 2015-01-28 19:56:03 -0800 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | NetworkMonitor::NetworkMonitor(boost::asio::io_service&) |
| 241 | { |
| 242 | throw Error("Network monitoring is not supported on this platform"); |
| 243 | } |
| 244 | |
| 245 | NetworkMonitor::~NetworkMonitor() |
| 246 | { |
| 247 | } |
| 248 | |
| 249 | } // namespace util |
| 250 | } // namespace ndn |
| 251 | |
| 252 | #endif // do not support network monitoring operations |