blob: 2801c4f05abaf729cd5456ed72c8d1165baba683 [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- 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 Afanasyev0eb70652014-02-27 18:35:07 -08007#include "ethernet-factory.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +01008#include "core/global-io.hpp"
Davide Pesaventob60cc122014-03-19 19:26:02 +01009#include "core/network-interface.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010010
11#include <boost/algorithm/string/predicate.hpp>
12#include <pcap/pcap.h>
13
14namespace nfd {
15
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080016NFD_LOG_INIT("EthernetFactory")
Davide Pesavento44deacc2014-02-19 10:48:07 +010017
18shared_ptr<EthernetFace>
Davide Pesaventob60cc122014-03-19 19:26:02 +010019EthernetFactory::createMulticastFace(const shared_ptr<NetworkInterfaceInfo> &interface,
20 const ethernet::Address &address)
Davide Pesavento44deacc2014-02-19 10:48:07 +010021{
Davide Pesavento44deacc2014-02-19 10:48:07 +010022 if (!address.isMulticast())
23 throw Error(address.toString() + " is not a multicast address");
24
Davide Pesaventob60cc122014-03-19 19:26:02 +010025 const std::string& name = interface->name;
26 shared_ptr<EthernetFace> face = findMulticastFace(name, address);
Davide Pesavento44deacc2014-02-19 10:48:07 +010027 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 Afanasyev0eb70652014-02-27 18:35:07 -080036 face->onFail += bind(&EthernetFactory::afterFaceFailed,
Davide Pesaventob60cc122014-03-19 19:26:02 +010037 this, name, address);
38 m_multicastFaces[std::make_pair(name, address)] = face;
Davide Pesavento44deacc2014-02-19 10:48:07 +010039
40 return face;
41}
42
Davide Pesavento44deacc2014-02-19 10:48:07 +010043void
Davide Pesaventob60cc122014-03-19 19:26:02 +010044EthernetFactory::afterFaceFailed(const std::string& interfaceName,
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080045 const ethernet::Address& address)
Davide Pesavento44deacc2014-02-19 10:48:07 +010046{
Davide Pesaventob60cc122014-03-19 19:26:02 +010047 NFD_LOG_DEBUG("afterFaceFailed: " << interfaceName << "/" << address);
48 m_multicastFaces.erase(std::make_pair(interfaceName, address));
Davide Pesavento44deacc2014-02-19 10:48:07 +010049}
50
51shared_ptr<EthernetFace>
Davide Pesaventob60cc122014-03-19 19:26:02 +010052EthernetFactory::findMulticastFace(const std::string& interfaceName,
Alexander Afanasyevd6655302014-02-28 08:41:28 -080053 const ethernet::Address& address) const
Davide Pesavento44deacc2014-02-19 10:48:07 +010054{
Davide Pesaventob60cc122014-03-19 19:26:02 +010055 MulticastFacesMap::const_iterator i = m_multicastFaces.find(std::make_pair(interfaceName, address));
Davide Pesavento44deacc2014-02-19 10:48:07 +010056 if (i != m_multicastFaces.end())
57 return i->second;
58 else
59 return shared_ptr<EthernetFace>();
60}
61
Alexander Afanasyevd6655302014-02-28 08:41:28 -080062void
63EthernetFactory::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 Pesavento44deacc2014-02-19 10:48:07 +010070} // namespace nfd