Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "ethernet-channel-factory.hpp" |
| 8 | #include "core/global-io.hpp" |
| 9 | |
| 10 | #include <boost/algorithm/string/predicate.hpp> |
| 11 | #include <pcap/pcap.h> |
| 12 | |
| 13 | namespace nfd { |
| 14 | |
| 15 | NFD_LOG_INIT("EthernetChannelFactory") |
| 16 | |
| 17 | shared_ptr<EthernetFace> |
| 18 | EthernetChannelFactory::createMulticast(const ethernet::Endpoint& interface, |
| 19 | const ethernet::Address& address) |
| 20 | { |
| 21 | std::vector<ethernet::Endpoint> ifs = findAllInterfaces(); |
| 22 | if (std::find(ifs.begin(), ifs.end(), interface) == ifs.end()) |
| 23 | throw Error(interface + " does not exist"); |
| 24 | |
| 25 | if (!address.isMulticast()) |
| 26 | throw Error(address.toString() + " is not a multicast address"); |
| 27 | |
| 28 | shared_ptr<EthernetFace> face = findMulticast(interface, address); |
| 29 | if (face) |
| 30 | return face; |
| 31 | |
| 32 | shared_ptr<boost::asio::posix::stream_descriptor> socket = |
| 33 | make_shared<boost::asio::posix::stream_descriptor>(boost::ref(getGlobalIoService())); |
| 34 | |
| 35 | face = make_shared<EthernetFace>(boost::cref(socket), |
| 36 | boost::cref(interface), |
| 37 | boost::cref(address)); |
| 38 | face->onFail += bind(&EthernetChannelFactory::afterFaceFailed, |
| 39 | this, interface, address); |
| 40 | m_multicastFaces[std::make_pair(interface, address)] = face; |
| 41 | |
| 42 | return face; |
| 43 | } |
| 44 | |
| 45 | std::vector<ethernet::Endpoint> |
| 46 | EthernetChannelFactory::findAllInterfaces() |
| 47 | { |
| 48 | std::vector<ethernet::Endpoint> interfaces; |
| 49 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 50 | errbuf[0] = '\0'; |
| 51 | |
| 52 | pcap_if_t* alldevs; |
| 53 | if (pcap_findalldevs(&alldevs, errbuf) < 0) |
| 54 | { |
| 55 | NFD_LOG_WARN("pcap_findalldevs() failed: " << errbuf); |
| 56 | return interfaces; |
| 57 | } |
| 58 | |
| 59 | for (pcap_if_t* device = alldevs; device != 0; device = device->next) |
| 60 | { |
| 61 | if ((device->flags & PCAP_IF_LOOPBACK) != 0) |
| 62 | // ignore loopback devices |
| 63 | continue; |
| 64 | |
| 65 | ethernet::Endpoint interface(device->name); |
| 66 | if (interface == "any") |
| 67 | // ignore libpcap "any" pseudo-device |
| 68 | continue; |
| 69 | if (boost::starts_with(interface, "nflog") || |
| 70 | boost::starts_with(interface, "nfqueue")) |
| 71 | // ignore Linux netfilter devices |
| 72 | continue; |
| 73 | |
| 74 | // maybe add interface addresses too |
| 75 | // interface.addAddress ... |
| 76 | interfaces.push_back(interface); |
| 77 | } |
| 78 | |
| 79 | pcap_freealldevs(alldevs); |
| 80 | return interfaces; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | EthernetChannelFactory::afterFaceFailed(const ethernet::Endpoint& interface, |
| 85 | const ethernet::Address& address) |
| 86 | { |
| 87 | NFD_LOG_DEBUG("afterFaceFailed: " << interface << "/" << address); |
| 88 | m_multicastFaces.erase(std::make_pair(interface, address)); |
| 89 | } |
| 90 | |
| 91 | shared_ptr<EthernetFace> |
| 92 | EthernetChannelFactory::findMulticast(const ethernet::Endpoint& interface, |
| 93 | const ethernet::Address& address) const |
| 94 | { |
| 95 | MulticastFacesMap::const_iterator i = m_multicastFaces.find(std::make_pair(interface, address)); |
| 96 | if (i != m_multicastFaces.end()) |
| 97 | return i->second; |
| 98 | else |
| 99 | return shared_ptr<EthernetFace>(); |
| 100 | } |
| 101 | |
| 102 | } // namespace nfd |