22 #include "ndn-cxx-config.hpp" 24 #ifdef NDN_CXX_HAVE_RTNETLINK 28 #include <netinet/in.h> 29 #include <linux/netlink.h> 30 #include <linux/rtnetlink.h> 43 int fd = ::socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
45 BOOST_THROW_EXCEPTION(Error(std::string(
"Cannot create netlink socket (") +
46 std::strerror(errno) +
")"));
49 addr.nl_family = AF_NETLINK;
50 addr.nl_groups = RTMGRP_LINK |
51 RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE |
52 RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE;
54 if (::bind(fd, reinterpret_cast<sockaddr*>(&addr),
sizeof(addr)) == -1) {
55 BOOST_THROW_EXCEPTION(Error(std::string(
"Cannot bind on netlink socket (") +
56 std::strerror(errno) +
")"));
62 bind(&Impl::onReceiveRtNetlink,
this, _1, _2));
66 NetworkMonitor::Impl::onReceiveRtNetlink(
const boost::system::error_code& error,
size_t nBytesReceived)
72 const nlmsghdr* nlh =
reinterpret_cast<const nlmsghdr*
>(m_buffer);
73 while ((NLMSG_OK(nlh, nBytesReceived)) && (nlh->nlmsg_type != NLMSG_DONE)) {
74 if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR ||
75 nlh->nlmsg_type == RTM_NEWLINK || nlh->nlmsg_type == RTM_DELLINK ||
76 nlh->nlmsg_type == RTM_NEWROUTE || nlh->nlmsg_type == RTM_DELROUTE) {
80 nlh = NLMSG_NEXT(nlh, nBytesReceived);
84 bind(&Impl::onReceiveRtNetlink,
this, _1, _2));
90 #endif // NDN_CXX_HAVE_RTNETLINK Impl(NetworkMonitor &nm, boost::asio::io_service &io)
Copyright (c) 2011-2015 Regents of the University of California.
const size_t NETLINK_BUFFER_SIZE
Signal< NetworkMonitor > onNetworkStateChanged