blob: d4b8f47fd9059eb0d63661c9b3462e168c31fdd7 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi25467942017-06-30 02:53:14 +00003 * Copyright (c) 2014-2017 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 Shi77dcadd2014-10-05 14:40:54 -070010 *
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
Junxiao Shi25467942017-06-30 02:53:14 +000028#ifndef NDN_NET_ETHERNET_HPP
29#define NDN_NET_ETHERNET_HPP
Junxiao Shi77dcadd2014-10-05 14:40:54 -070030
Davide Pesavento51dc2ef2014-11-04 20:04:19 +010031#include <array>
32#include <cstdint>
Davide Pesaventodfb8a612014-11-25 19:14:11 +010033#include <functional>
Davide Pesavento51dc2ef2014-11-04 20:04:19 +010034#include <string>
Junxiao Shi77dcadd2014-10-05 14:40:54 -070035
36namespace ndn {
Junxiao Shi77dcadd2014-10-05 14:40:54 -070037namespace ethernet {
38
Davide Pesavento51dc2ef2014-11-04 20:04:19 +010039const uint16_t ETHERTYPE_NDN = 0x8624;
Junxiao Shi77dcadd2014-10-05 14:40:54 -070040
41const size_t ADDR_LEN = 6; ///< Octets in one Ethernet address
42const size_t TYPE_LEN = 2; ///< Octets in Ethertype field
43const size_t HDR_LEN = 14; ///< Total octets in Ethernet header (without 802.1Q tag)
44const size_t TAG_LEN = 4; ///< Octets in 802.1Q tag (TPID + priority + VLAN)
45const size_t MIN_DATA_LEN = 46; ///< Min octets in Ethernet payload (assuming no 802.1Q tag)
46const size_t MAX_DATA_LEN = 1500; ///< Max octets in Ethernet payload
47const size_t CRC_LEN = 4; ///< Octets in Ethernet frame check sequence
48
49
50/** \brief represents an Ethernet hardware address
51 */
Davide Pesavento51dc2ef2014-11-04 20:04:19 +010052class Address : public std::array<uint8_t, ADDR_LEN>
Junxiao Shi77dcadd2014-10-05 14:40:54 -070053{
54public:
55 /// Constructs a null Ethernet address (00:00:00:00:00:00)
56 Address();
57
58 /// Constructs a new Ethernet address with the given octets
59 Address(uint8_t a1, uint8_t a2, uint8_t a3,
60 uint8_t a4, uint8_t a5, uint8_t a6);
61
62 /// Constructs a new Ethernet address with the given octets
63 explicit
64 Address(const uint8_t octets[ADDR_LEN]);
65
Junxiao Shi77dcadd2014-10-05 14:40:54 -070066 /// True if this is a broadcast address (ff:ff:ff:ff:ff:ff)
67 bool
68 isBroadcast() const;
69
70 /// True if this is a multicast address
71 bool
72 isMulticast() const;
73
74 /// True if this is a null address (00:00:00:00:00:00)
75 bool
76 isNull() const;
77
78 /**
79 * \brief Converts the address to a human-readable string
80 *
81 * \param sep A character used to visually separate the octets,
82 * usually ':' (the default value) or '-'
83 */
84 std::string
85 toString(char sep = ':') const;
86
87 /**
88 * \brief Creates an Address from a string containing an Ethernet address
89 * in hexadecimal notation, with colons or hyphens as separators
90 *
91 * \param str The string to be parsed
92 * \return Always an instance of Address, which will be null
93 * if the parsing fails
94 */
95 static Address
96 fromString(const std::string& str);
97};
98
99/// Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff)
100Address
101getBroadcastAddress();
102
103/// Returns the default Ethernet multicast address for NDN
104Address
105getDefaultMulticastAddress();
106
107std::ostream&
108operator<<(std::ostream& o, const Address& a);
109
110} // namespace ethernet
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700111} // namespace ndn
112
Davide Pesaventodfb8a612014-11-25 19:14:11 +0100113namespace std {
114
115// specialize std::hash<> for ethernet::Address
116template<>
Junxiao Shi25467942017-06-30 02:53:14 +0000117struct hash<ndn::ethernet::Address>
Davide Pesaventodfb8a612014-11-25 19:14:11 +0100118{
119 size_t
Junxiao Shi25467942017-06-30 02:53:14 +0000120 operator()(const ndn::ethernet::Address& a) const noexcept;
Davide Pesaventodfb8a612014-11-25 19:14:11 +0100121};
122
123} // namespace std
124
Junxiao Shi25467942017-06-30 02:53:14 +0000125#endif // NDN_NET_ETHERNET_HPP