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