blob: 56888f6836b21fe6650a77d5195c8fed905d873c [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 Pesavento44deacc2014-02-19 10:48:07 +010024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#ifndef NFD_CORE_ETHERNET_HPP
26#define NFD_CORE_ETHERNET_HPP
Davide Pesavento44deacc2014-02-19 10:48:07 +010027
28#include "common.hpp"
29
30#include <boost/array.hpp>
31
32#define ETHERTYPE_NDN 0x8624
33
34namespace nfd {
35namespace ethernet {
36
Davide Pesavento44deacc2014-02-19 10:48:07 +010037const size_t ADDR_LEN = 6; ///< Octets in one Ethernet address
38const size_t TYPE_LEN = 2; ///< Octets in Ethertype field
39const size_t HDR_LEN = 14; ///< Total octets in Ethernet header (without 802.1Q tag)
40const size_t TAG_LEN = 4; ///< Octets in 802.1Q tag (TPID + priority + VLAN)
41const size_t MIN_DATA_LEN = 46; ///< Min octets in Ethernet payload (assuming no 802.1Q tag)
42const size_t MAX_DATA_LEN = 1500; ///< Max octets in Ethernet payload
43const size_t CRC_LEN = 4; ///< Octets in Ethernet frame check sequence
44
45
46class Address : public boost::array<uint8_t, ADDR_LEN>
47{
48public:
Davide Pesaventoedae3532014-04-09 03:07:11 +020049 /// Constructs a null Ethernet address (00:00:00:00:00:00)
Davide Pesavento44deacc2014-02-19 10:48:07 +010050 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 Pesaventoedae3532014-04-09 03:07:11 +020063 /// True if this is a broadcast address (ff:ff:ff:ff:ff:ff)
Davide Pesavento44deacc2014-02-19 10:48:07 +010064 bool
65 isBroadcast() const;
66
67 /// True if this is a multicast address
68 bool
69 isMulticast() const;
70
Davide Pesaventoedae3532014-04-09 03:07:11 +020071 /// True if this is a null address (00:00:00:00:00:00)
Davide Pesavento248d6bd2014-03-09 10:24:08 +010072 bool
73 isNull() const;
74
Davide Pesavento56fb5972014-03-12 06:18:37 +010075 /**
76 * @brief Converts the address to a human-readable string
77 *
78 * @param sep A character used to visually separate the octets,
Davide Pesaventoedae3532014-04-09 03:07:11 +020079 * usually ':' (the default value) or '-'
Davide Pesavento56fb5972014-03-12 06:18:37 +010080 */
Davide Pesavento44deacc2014-02-19 10:48:07 +010081 std::string
Davide Pesaventoedae3532014-04-09 03:07:11 +020082 toString(char sep = ':') const;
Davide Pesavento56fb5972014-03-12 06:18:37 +010083
84 /**
85 * @brief Creates an Address from a string containing an Ethernet address
Davide Pesaventoedae3532014-04-09 03:07:11 +020086 * in hexadecimal notation, with colons or hyphens as separators
Davide Pesavento56fb5972014-03-12 06:18:37 +010087 *
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 Pesavento44deacc2014-02-19 10:48:07 +010094};
95
Davide Pesaventoedae3532014-04-09 03:07:11 +020096/// Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff)
Davide Pesavento44deacc2014-02-19 10:48:07 +010097inline Address
98getBroadcastAddress()
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
105inline Address
106getDefaultMulticastAddress()
107{
108 static Address mcast(0x01, 0x00, 0x5E, 0x00, 0x17, 0xAA);
109 return mcast;
110}
111
112inline
113Address::Address()
114{
115 assign(0);
116}
117
118inline
119Address::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
129inline
130Address::Address(const uint8_t octets[])
131{
132 std::copy(octets, octets + size(), begin());
133}
134
135inline
136Address::Address(const Address& address)
137{
138 std::copy(address.begin(), address.end(), begin());
139}
140
141inline bool
142Address::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
148inline bool
149Address::isMulticast() const
150{
151 return (elems[0] & 1) != 0;
152}
153
Davide Pesavento248d6bd2014-03-09 10:24:08 +0100154inline bool
155Address::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 Pesavento56fb5972014-03-12 06:18:37 +0100161std::ostream&
162operator<<(std::ostream& o, const Address& a);
Davide Pesavento44deacc2014-02-19 10:48:07 +0100163
164} // namespace ethernet
165} // namespace nfd
166
167#endif // NFD_FACE_ETHERNET_HPP