Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 24 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame^] | 25 | #ifndef NFD_CORE_ETHERNET_HPP |
| 26 | #define NFD_CORE_ETHERNET_HPP |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 27 | |
| 28 | #include "common.hpp" |
| 29 | |
| 30 | #include <boost/array.hpp> |
| 31 | |
| 32 | #define ETHERTYPE_NDN 0x8624 |
| 33 | |
| 34 | namespace nfd { |
| 35 | namespace ethernet { |
| 36 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 37 | const size_t ADDR_LEN = 6; ///< Octets in one Ethernet address |
| 38 | const size_t TYPE_LEN = 2; ///< Octets in Ethertype field |
| 39 | const size_t HDR_LEN = 14; ///< Total octets in Ethernet header (without 802.1Q tag) |
| 40 | const size_t TAG_LEN = 4; ///< Octets in 802.1Q tag (TPID + priority + VLAN) |
| 41 | const size_t MIN_DATA_LEN = 46; ///< Min octets in Ethernet payload (assuming no 802.1Q tag) |
| 42 | const size_t MAX_DATA_LEN = 1500; ///< Max octets in Ethernet payload |
| 43 | const size_t CRC_LEN = 4; ///< Octets in Ethernet frame check sequence |
| 44 | |
| 45 | |
| 46 | class Address : public boost::array<uint8_t, ADDR_LEN> |
| 47 | { |
| 48 | public: |
Davide Pesavento | edae353 | 2014-04-09 03:07:11 +0200 | [diff] [blame] | 49 | /// Constructs a null Ethernet address (00:00:00:00:00:00) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 50 | Address(); |
| 51 | |
| 52 | /// Constructs a new Ethernet address with the given octets |
| 53 | Address(uint8_t a1, uint8_t a2, uint8_t a3, |
| 54 | uint8_t a4, uint8_t a5, uint8_t a6); |
| 55 | |
| 56 | /// Constructs a new Ethernet address with the given octets |
| 57 | explicit |
| 58 | Address(const uint8_t octets[ADDR_LEN]); |
| 59 | |
| 60 | /// Copy constructor |
| 61 | Address(const Address& address); |
| 62 | |
Davide Pesavento | edae353 | 2014-04-09 03:07:11 +0200 | [diff] [blame] | 63 | /// True if this is a broadcast address (ff:ff:ff:ff:ff:ff) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 64 | bool |
| 65 | isBroadcast() const; |
| 66 | |
| 67 | /// True if this is a multicast address |
| 68 | bool |
| 69 | isMulticast() const; |
| 70 | |
Davide Pesavento | edae353 | 2014-04-09 03:07:11 +0200 | [diff] [blame] | 71 | /// True if this is a null address (00:00:00:00:00:00) |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 72 | bool |
| 73 | isNull() const; |
| 74 | |
Davide Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 75 | /** |
| 76 | * @brief Converts the address to a human-readable string |
| 77 | * |
| 78 | * @param sep A character used to visually separate the octets, |
Davide Pesavento | edae353 | 2014-04-09 03:07:11 +0200 | [diff] [blame] | 79 | * usually ':' (the default value) or '-' |
Davide Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 80 | */ |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 81 | std::string |
Davide Pesavento | edae353 | 2014-04-09 03:07:11 +0200 | [diff] [blame] | 82 | toString(char sep = ':') const; |
Davide Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * @brief Creates an Address from a string containing an Ethernet address |
Davide Pesavento | edae353 | 2014-04-09 03:07:11 +0200 | [diff] [blame] | 86 | * in hexadecimal notation, with colons or hyphens as separators |
Davide Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 87 | * |
| 88 | * @param str The string to be parsed |
| 89 | * @return Always an instance of Address, which will be null |
| 90 | * if the parsing fails |
| 91 | */ |
| 92 | static Address |
| 93 | fromString(const std::string& str); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
Davide Pesavento | edae353 | 2014-04-09 03:07:11 +0200 | [diff] [blame] | 96 | /// Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 97 | inline Address |
| 98 | getBroadcastAddress() |
| 99 | { |
| 100 | static Address bcast(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); |
| 101 | return bcast; |
| 102 | } |
| 103 | |
| 104 | /// Returns the default Ethernet multicast address for NDN |
| 105 | inline Address |
| 106 | getDefaultMulticastAddress() |
| 107 | { |
| 108 | static Address mcast(0x01, 0x00, 0x5E, 0x00, 0x17, 0xAA); |
| 109 | return mcast; |
| 110 | } |
| 111 | |
| 112 | inline |
| 113 | Address::Address() |
| 114 | { |
| 115 | assign(0); |
| 116 | } |
| 117 | |
| 118 | inline |
| 119 | Address::Address(uint8_t a1, uint8_t a2, uint8_t a3, uint8_t a4, uint8_t a5, uint8_t a6) |
| 120 | { |
| 121 | elems[0] = a1; |
| 122 | elems[1] = a2; |
| 123 | elems[2] = a3; |
| 124 | elems[3] = a4; |
| 125 | elems[4] = a5; |
| 126 | elems[5] = a6; |
| 127 | } |
| 128 | |
| 129 | inline |
| 130 | Address::Address(const uint8_t octets[]) |
| 131 | { |
| 132 | std::copy(octets, octets + size(), begin()); |
| 133 | } |
| 134 | |
| 135 | inline |
| 136 | Address::Address(const Address& address) |
| 137 | { |
| 138 | std::copy(address.begin(), address.end(), begin()); |
| 139 | } |
| 140 | |
| 141 | inline bool |
| 142 | Address::isBroadcast() const |
| 143 | { |
| 144 | return elems[0] == 0xFF && elems[1] == 0xFF && elems[2] == 0xFF && |
| 145 | elems[3] == 0xFF && elems[4] == 0xFF && elems[5] == 0xFF; |
| 146 | } |
| 147 | |
| 148 | inline bool |
| 149 | Address::isMulticast() const |
| 150 | { |
| 151 | return (elems[0] & 1) != 0; |
| 152 | } |
| 153 | |
Davide Pesavento | 248d6bd | 2014-03-09 10:24:08 +0100 | [diff] [blame] | 154 | inline bool |
| 155 | Address::isNull() const |
| 156 | { |
| 157 | return elems[0] == 0x0 && elems[1] == 0x0 && elems[2] == 0x0 && |
| 158 | elems[3] == 0x0 && elems[4] == 0x0 && elems[5] == 0x0; |
| 159 | } |
| 160 | |
Davide Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 161 | std::ostream& |
| 162 | operator<<(std::ostream& o, const Address& a); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 163 | |
| 164 | } // namespace ethernet |
| 165 | } // namespace nfd |
| 166 | |
| 167 | #endif // NFD_FACE_ETHERNET_HPP |