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" |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 9 | #include "core/network-interface.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 10 | |
| 11 | #include <boost/algorithm/string/predicate.hpp> |
| 12 | #include <pcap/pcap.h> |
| 13 | |
| 14 | namespace nfd { |
| 15 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 16 | NFD_LOG_INIT("EthernetFactory") |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 17 | |
| 18 | shared_ptr<EthernetFace> |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 19 | EthernetFactory::createMulticastFace(const shared_ptr<NetworkInterfaceInfo> &interface, |
| 20 | const ethernet::Address &address) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 21 | { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 22 | if (!address.isMulticast()) |
| 23 | throw Error(address.toString() + " is not a multicast address"); |
| 24 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 25 | const std::string& name = interface->name; |
| 26 | shared_ptr<EthernetFace> face = findMulticastFace(name, address); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 27 | if (face) |
| 28 | return face; |
| 29 | |
| 30 | shared_ptr<boost::asio::posix::stream_descriptor> socket = |
| 31 | make_shared<boost::asio::posix::stream_descriptor>(boost::ref(getGlobalIoService())); |
| 32 | |
| 33 | face = make_shared<EthernetFace>(boost::cref(socket), |
| 34 | boost::cref(interface), |
| 35 | boost::cref(address)); |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 36 | face->onFail += bind(&EthernetFactory::afterFaceFailed, |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 37 | this, name, address); |
| 38 | m_multicastFaces[std::make_pair(name, address)] = face; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 39 | |
| 40 | return face; |
| 41 | } |
| 42 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 43 | void |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 44 | EthernetFactory::afterFaceFailed(const std::string& interfaceName, |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 45 | const ethernet::Address& address) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 46 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 47 | NFD_LOG_DEBUG("afterFaceFailed: " << interfaceName << "/" << address); |
| 48 | m_multicastFaces.erase(std::make_pair(interfaceName, address)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | shared_ptr<EthernetFace> |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 52 | EthernetFactory::findMulticastFace(const std::string& interfaceName, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 53 | const ethernet::Address& address) const |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 54 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 55 | MulticastFacesMap::const_iterator i = m_multicastFaces.find(std::make_pair(interfaceName, address)); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 56 | if (i != m_multicastFaces.end()) |
| 57 | return i->second; |
| 58 | else |
| 59 | return shared_ptr<EthernetFace>(); |
| 60 | } |
| 61 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 62 | void |
| 63 | EthernetFactory::createFace(const FaceUri& uri, |
| 64 | const FaceCreatedCallback& onCreated, |
| 65 | const FaceConnectFailedCallback& onConnectFailed) |
| 66 | { |
| 67 | throw Error("EthernetFactory does not support 'createFace' operation"); |
| 68 | } |
| 69 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 70 | } // namespace nfd |