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 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 7 | #include "ethernet-factory.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 8 | #include "core/global-io.hpp" |
| 9 | |
| 10 | #include <boost/algorithm/string/predicate.hpp> |
| 11 | #include <pcap/pcap.h> |
| 12 | |
| 13 | namespace nfd { |
| 14 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 15 | NFD_LOG_INIT("EthernetFactory") |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 16 | |
| 17 | shared_ptr<EthernetFace> |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 18 | EthernetFactory::createMulticast(const ethernet::Endpoint& interface, |
| 19 | const ethernet::Address& address) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 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)); |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 38 | face->onFail += bind(&EthernetFactory::afterFaceFailed, |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 39 | this, interface, address); |
| 40 | m_multicastFaces[std::make_pair(interface, address)] = face; |
| 41 | |
| 42 | return face; |
| 43 | } |
| 44 | |
| 45 | std::vector<ethernet::Endpoint> |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 46 | EthernetFactory::findAllInterfaces() |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 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; |
Alexander Afanasyev | ef968e1 | 2014-02-27 15:17:50 -0800 | [diff] [blame] | 73 | if (boost::starts_with(interface, "fw")) |
| 74 | // ignore OSX firewire interface |
| 75 | continue; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 76 | |
| 77 | // maybe add interface addresses too |
| 78 | // interface.addAddress ... |
| 79 | interfaces.push_back(interface); |
| 80 | } |
| 81 | |
| 82 | pcap_freealldevs(alldevs); |
| 83 | return interfaces; |
| 84 | } |
| 85 | |
| 86 | void |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 87 | EthernetFactory::afterFaceFailed(const ethernet::Endpoint& interface, |
| 88 | const ethernet::Address& address) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 89 | { |
| 90 | NFD_LOG_DEBUG("afterFaceFailed: " << interface << "/" << address); |
| 91 | m_multicastFaces.erase(std::make_pair(interface, address)); |
| 92 | } |
| 93 | |
| 94 | shared_ptr<EthernetFace> |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 95 | EthernetFactory::findMulticast(const ethernet::Endpoint& interface, |
| 96 | const ethernet::Address& address) const |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 97 | { |
| 98 | MulticastFacesMap::const_iterator i = m_multicastFaces.find(std::make_pair(interface, address)); |
| 99 | if (i != m_multicastFaces.end()) |
| 100 | return i->second; |
| 101 | else |
| 102 | return shared_ptr<EthernetFace>(); |
| 103 | } |
| 104 | |
| 105 | } // namespace nfd |