blob: 4b1d8c1c62716bb812a440c880bd4d1503e360ac [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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -06008#include "core/logger.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +01009#include "core/global-io.hpp"
Davide Pesaventob60cc122014-03-19 19:26:02 +010010#include "core/network-interface.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010011
12#include <boost/algorithm/string/predicate.hpp>
13#include <pcap/pcap.h>
14
15namespace nfd {
16
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080017NFD_LOG_INIT("EthernetFactory")
Davide Pesavento44deacc2014-02-19 10:48:07 +010018
19shared_ptr<EthernetFace>
Davide Pesaventob60cc122014-03-19 19:26:02 +010020EthernetFactory::createMulticastFace(const shared_ptr<NetworkInterfaceInfo> &interface,
21 const ethernet::Address &address)
Davide Pesavento44deacc2014-02-19 10:48:07 +010022{
Davide Pesavento44deacc2014-02-19 10:48:07 +010023 if (!address.isMulticast())
24 throw Error(address.toString() + " is not a multicast address");
25
Davide Pesaventob60cc122014-03-19 19:26:02 +010026 const std::string& name = interface->name;
27 shared_ptr<EthernetFace> face = findMulticastFace(name, address);
Davide Pesavento44deacc2014-02-19 10:48:07 +010028 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 Afanasyev0eb70652014-02-27 18:35:07 -080037 face->onFail += bind(&EthernetFactory::afterFaceFailed,
Davide Pesaventob60cc122014-03-19 19:26:02 +010038 this, name, address);
39 m_multicastFaces[std::make_pair(name, address)] = face;
Davide Pesavento44deacc2014-02-19 10:48:07 +010040
41 return face;
42}
43
Davide Pesavento44deacc2014-02-19 10:48:07 +010044void
Davide Pesaventob60cc122014-03-19 19:26:02 +010045EthernetFactory::afterFaceFailed(const std::string& interfaceName,
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080046 const ethernet::Address& address)
Davide Pesavento44deacc2014-02-19 10:48:07 +010047{
Davide Pesaventob60cc122014-03-19 19:26:02 +010048 NFD_LOG_DEBUG("afterFaceFailed: " << interfaceName << "/" << address);
49 m_multicastFaces.erase(std::make_pair(interfaceName, address));
Davide Pesavento44deacc2014-02-19 10:48:07 +010050}
51
52shared_ptr<EthernetFace>
Davide Pesaventob60cc122014-03-19 19:26:02 +010053EthernetFactory::findMulticastFace(const std::string& interfaceName,
Alexander Afanasyevd6655302014-02-28 08:41:28 -080054 const ethernet::Address& address) const
Davide Pesavento44deacc2014-02-19 10:48:07 +010055{
Davide Pesaventob60cc122014-03-19 19:26:02 +010056 MulticastFacesMap::const_iterator i = m_multicastFaces.find(std::make_pair(interfaceName, address));
Davide Pesavento44deacc2014-02-19 10:48:07 +010057 if (i != m_multicastFaces.end())
58 return i->second;
59 else
60 return shared_ptr<EthernetFace>();
61}
62
Alexander Afanasyevd6655302014-02-28 08:41:28 -080063void
64EthernetFactory::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 Pesavento44deacc2014-02-19 10:48:07 +010071} // namespace nfd