blob: a42f88987674f8a93f367c82848c5d3ce0ee12e4 [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
7#ifndef NFD_FACE_ETHERNET_FACE_HPP
8#define NFD_FACE_ETHERNET_FACE_HPP
9
10#include "ethernet.hpp"
11#include "face.hpp"
12
13#ifndef HAVE_PCAP
14#error "Cannot include this file when pcap is not available"
15#endif
16
17// forward declarations
18struct pcap;
19typedef pcap pcap_t;
20
21namespace nfd {
22
Davide Pesaventob60cc122014-03-19 19:26:02 +010023class NetworkInterfaceInfo;
24
Davide Pesavento44deacc2014-02-19 10:48:07 +010025/**
26 * \brief Implementation of Face abstraction that uses raw
27 * Ethernet frames as underlying transport mechanism
28 */
29class EthernetFace : public Face
30{
31public:
32 /**
33 * \brief EthernetFace-related error
34 */
35 struct Error : public Face::Error
36 {
37 Error(const std::string& what) : Face::Error(what) {}
38 };
39
40 EthernetFace(const shared_ptr<boost::asio::posix::stream_descriptor>& socket,
Davide Pesaventob60cc122014-03-19 19:26:02 +010041 const shared_ptr<NetworkInterfaceInfo>& interface,
Davide Pesavento44deacc2014-02-19 10:48:07 +010042 const ethernet::Address& address);
43
44 virtual
45 ~EthernetFace();
46
47 /// send an Interest
48 virtual void
49 sendInterest(const Interest& interest);
50
51 /// send a Data
52 virtual void
53 sendData(const Data& data);
54
55 /**
56 * \brief Close the face
57 *
58 * This terminates all communication on the face and cause
59 * onFail() method event to be invoked
60 */
61 virtual void
62 close();
63
64private:
65 void
66 pcapInit();
67
68 void
69 setPacketFilter(const char* filterString);
70
71 void
72 sendPacket(const ndn::Block& block);
73
74 void
75 handleRead(const boost::system::error_code& error,
76 size_t nBytesRead);
77
78 void
79 processErrorCode(const boost::system::error_code& error);
80
Davide Pesavento44deacc2014-02-19 10:48:07 +010081 size_t
82 getInterfaceMtu() const;
83
84private:
85 shared_ptr<boost::asio::posix::stream_descriptor> m_socket;
Davide Pesaventob60cc122014-03-19 19:26:02 +010086 std::string m_interfaceName;
87 ethernet::Address m_srcAddress;
Davide Pesavento44deacc2014-02-19 10:48:07 +010088 ethernet::Address m_destAddress;
89 size_t m_interfaceMtu;
90 pcap_t* m_pcap;
91};
92
93} // namespace nfd
94
95#endif // NFD_FACE_ETHERNET_FACE_HPP