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 | |
| 7 | #ifndef NFD_FACE_ETHERNET_HPP |
| 8 | #define NFD_FACE_ETHERNET_HPP |
| 9 | |
| 10 | #include "common.hpp" |
| 11 | |
| 12 | #include <boost/array.hpp> |
| 13 | |
| 14 | #define ETHERTYPE_NDN 0x8624 |
| 15 | |
| 16 | namespace nfd { |
| 17 | namespace ethernet { |
| 18 | |
| 19 | typedef std::string Endpoint; |
| 20 | |
| 21 | const size_t ADDR_LEN = 6; ///< Octets in one Ethernet address |
| 22 | const size_t TYPE_LEN = 2; ///< Octets in Ethertype field |
| 23 | const size_t HDR_LEN = 14; ///< Total octets in Ethernet header (without 802.1Q tag) |
| 24 | const size_t TAG_LEN = 4; ///< Octets in 802.1Q tag (TPID + priority + VLAN) |
| 25 | const size_t MIN_DATA_LEN = 46; ///< Min octets in Ethernet payload (assuming no 802.1Q tag) |
| 26 | const size_t MAX_DATA_LEN = 1500; ///< Max octets in Ethernet payload |
| 27 | const size_t CRC_LEN = 4; ///< Octets in Ethernet frame check sequence |
| 28 | |
| 29 | |
| 30 | class Address : public boost::array<uint8_t, ADDR_LEN> |
| 31 | { |
| 32 | public: |
| 33 | /// Constructs a null Ethernet address (00-00-00-00-00-00) |
| 34 | Address(); |
| 35 | |
| 36 | /// Constructs a new Ethernet address with the given octets |
| 37 | Address(uint8_t a1, uint8_t a2, uint8_t a3, |
| 38 | uint8_t a4, uint8_t a5, uint8_t a6); |
| 39 | |
| 40 | /// Constructs a new Ethernet address with the given octets |
| 41 | explicit |
| 42 | Address(const uint8_t octets[ADDR_LEN]); |
| 43 | |
| 44 | /// Copy constructor |
| 45 | Address(const Address& address); |
| 46 | |
| 47 | /// True if this is a broadcast address (FF-FF-FF-FF-FF-FF) |
| 48 | bool |
| 49 | isBroadcast() const; |
| 50 | |
| 51 | /// True if this is a multicast address |
| 52 | bool |
| 53 | isMulticast() const; |
| 54 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame^] | 55 | /// True if this is a null address (00-00-00-00-00-00) |
| 56 | bool |
| 57 | isNull() const; |
| 58 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 59 | std::string |
| 60 | toString(char sep = '-') const; |
| 61 | }; |
| 62 | |
| 63 | /// Returns the Ethernet broadcast address (FF-FF-FF-FF-FF-FF) |
| 64 | inline Address |
| 65 | getBroadcastAddress() |
| 66 | { |
| 67 | static Address bcast(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); |
| 68 | return bcast; |
| 69 | } |
| 70 | |
| 71 | /// Returns the default Ethernet multicast address for NDN |
| 72 | inline Address |
| 73 | getDefaultMulticastAddress() |
| 74 | { |
| 75 | static Address mcast(0x01, 0x00, 0x5E, 0x00, 0x17, 0xAA); |
| 76 | return mcast; |
| 77 | } |
| 78 | |
| 79 | inline |
| 80 | Address::Address() |
| 81 | { |
| 82 | assign(0); |
| 83 | } |
| 84 | |
| 85 | inline |
| 86 | Address::Address(uint8_t a1, uint8_t a2, uint8_t a3, uint8_t a4, uint8_t a5, uint8_t a6) |
| 87 | { |
| 88 | elems[0] = a1; |
| 89 | elems[1] = a2; |
| 90 | elems[2] = a3; |
| 91 | elems[3] = a4; |
| 92 | elems[4] = a5; |
| 93 | elems[5] = a6; |
| 94 | } |
| 95 | |
| 96 | inline |
| 97 | Address::Address(const uint8_t octets[]) |
| 98 | { |
| 99 | std::copy(octets, octets + size(), begin()); |
| 100 | } |
| 101 | |
| 102 | inline |
| 103 | Address::Address(const Address& address) |
| 104 | { |
| 105 | std::copy(address.begin(), address.end(), begin()); |
| 106 | } |
| 107 | |
| 108 | inline bool |
| 109 | Address::isBroadcast() const |
| 110 | { |
| 111 | return elems[0] == 0xFF && elems[1] == 0xFF && elems[2] == 0xFF && |
| 112 | elems[3] == 0xFF && elems[4] == 0xFF && elems[5] == 0xFF; |
| 113 | } |
| 114 | |
| 115 | inline bool |
| 116 | Address::isMulticast() const |
| 117 | { |
| 118 | return (elems[0] & 1) != 0; |
| 119 | } |
| 120 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame^] | 121 | inline bool |
| 122 | Address::isNull() const |
| 123 | { |
| 124 | return elems[0] == 0x0 && elems[1] == 0x0 && elems[2] == 0x0 && |
| 125 | elems[3] == 0x0 && elems[4] == 0x0 && elems[5] == 0x0; |
| 126 | } |
| 127 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 128 | inline std::string |
| 129 | Address::toString(char sep) const |
| 130 | { |
| 131 | char s[18]; // 12 digits + 5 separators + null terminator |
| 132 | ::snprintf(s, sizeof(s), "%02x%c%02x%c%02x%c%02x%c%02x%c%02x", |
| 133 | elems[0], sep, elems[1], sep, elems[2], sep, |
| 134 | elems[3], sep, elems[4], sep, elems[5]); |
| 135 | return std::string(s); |
| 136 | } |
| 137 | |
| 138 | static std::ostream& |
| 139 | operator<<(std::ostream& o, const Address& a) |
| 140 | { |
| 141 | return o << a.toString(); |
| 142 | } |
| 143 | |
| 144 | } // namespace ethernet |
| 145 | } // namespace nfd |
| 146 | |
| 147 | #endif // NFD_FACE_ETHERNET_HPP |