Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | b5f8bcc | 2017-02-05 17:58:05 -0500 | [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 | * The University of Memphis. |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
| 28 | #ifndef NDN_UTIL_ETHERNET_HPP |
| 29 | #define NDN_UTIL_ETHERNET_HPP |
| 30 | |
Davide Pesavento | 51dc2ef | 2014-11-04 20:04:19 +0100 | [diff] [blame] | 31 | #include <array> |
| 32 | #include <cstdint> |
Davide Pesavento | dfb8a61 | 2014-11-25 19:14:11 +0100 | [diff] [blame] | 33 | #include <functional> |
Davide Pesavento | 51dc2ef | 2014-11-04 20:04:19 +0100 | [diff] [blame] | 34 | #include <string> |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
| 37 | namespace util { |
| 38 | namespace ethernet { |
| 39 | |
Davide Pesavento | 51dc2ef | 2014-11-04 20:04:19 +0100 | [diff] [blame] | 40 | const uint16_t ETHERTYPE_NDN = 0x8624; |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 41 | |
| 42 | const size_t ADDR_LEN = 6; ///< Octets in one Ethernet address |
| 43 | const size_t TYPE_LEN = 2; ///< Octets in Ethertype field |
| 44 | const size_t HDR_LEN = 14; ///< Total octets in Ethernet header (without 802.1Q tag) |
| 45 | const size_t TAG_LEN = 4; ///< Octets in 802.1Q tag (TPID + priority + VLAN) |
| 46 | const size_t MIN_DATA_LEN = 46; ///< Min octets in Ethernet payload (assuming no 802.1Q tag) |
| 47 | const size_t MAX_DATA_LEN = 1500; ///< Max octets in Ethernet payload |
| 48 | const size_t CRC_LEN = 4; ///< Octets in Ethernet frame check sequence |
| 49 | |
| 50 | |
| 51 | /** \brief represents an Ethernet hardware address |
| 52 | */ |
Davide Pesavento | 51dc2ef | 2014-11-04 20:04:19 +0100 | [diff] [blame] | 53 | class Address : public std::array<uint8_t, ADDR_LEN> |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 54 | { |
| 55 | public: |
| 56 | /// Constructs a null Ethernet address (00:00:00:00:00:00) |
| 57 | Address(); |
| 58 | |
| 59 | /// Constructs a new Ethernet address with the given octets |
| 60 | Address(uint8_t a1, uint8_t a2, uint8_t a3, |
| 61 | uint8_t a4, uint8_t a5, uint8_t a6); |
| 62 | |
| 63 | /// Constructs a new Ethernet address with the given octets |
| 64 | explicit |
| 65 | Address(const uint8_t octets[ADDR_LEN]); |
| 66 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 67 | /// True if this is a broadcast address (ff:ff:ff:ff:ff:ff) |
| 68 | bool |
| 69 | isBroadcast() const; |
| 70 | |
| 71 | /// True if this is a multicast address |
| 72 | bool |
| 73 | isMulticast() const; |
| 74 | |
| 75 | /// True if this is a null address (00:00:00:00:00:00) |
| 76 | bool |
| 77 | isNull() const; |
| 78 | |
| 79 | /** |
| 80 | * \brief Converts the address to a human-readable string |
| 81 | * |
| 82 | * \param sep A character used to visually separate the octets, |
| 83 | * usually ':' (the default value) or '-' |
| 84 | */ |
| 85 | std::string |
| 86 | toString(char sep = ':') const; |
| 87 | |
| 88 | /** |
| 89 | * \brief Creates an Address from a string containing an Ethernet address |
| 90 | * in hexadecimal notation, with colons or hyphens as separators |
| 91 | * |
| 92 | * \param str The string to be parsed |
| 93 | * \return Always an instance of Address, which will be null |
| 94 | * if the parsing fails |
| 95 | */ |
| 96 | static Address |
| 97 | fromString(const std::string& str); |
| 98 | }; |
| 99 | |
| 100 | /// Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff) |
| 101 | Address |
| 102 | getBroadcastAddress(); |
| 103 | |
| 104 | /// Returns the default Ethernet multicast address for NDN |
| 105 | Address |
| 106 | getDefaultMulticastAddress(); |
| 107 | |
| 108 | std::ostream& |
| 109 | operator<<(std::ostream& o, const Address& a); |
| 110 | |
| 111 | } // namespace ethernet |
| 112 | } // namespace util |
| 113 | } // namespace ndn |
| 114 | |
Davide Pesavento | dfb8a61 | 2014-11-25 19:14:11 +0100 | [diff] [blame] | 115 | namespace std { |
| 116 | |
| 117 | // specialize std::hash<> for ethernet::Address |
| 118 | template<> |
| 119 | struct hash<ndn::util::ethernet::Address> |
| 120 | { |
| 121 | size_t |
| 122 | operator()(const ndn::util::ethernet::Address& a) const noexcept; |
| 123 | }; |
| 124 | |
| 125 | } // namespace std |
| 126 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 127 | #endif // NDN_UTIL_ETHERNET_HPP |