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