blob: 7a8cff1e71760681dc4bed72f42beee55498ad33 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_UTIL_ETHERNET_HPP
23#define NDN_UTIL_ETHERNET_HPP
24
25#include "common.hpp"
26
27#include <boost/array.hpp>
28
29namespace ndn {
30namespace util {
31namespace ethernet {
32
33static const uint16_t ETHERTYPE_NDN = 0x8624;
34
35const size_t ADDR_LEN = 6; ///< Octets in one Ethernet address
36const size_t TYPE_LEN = 2; ///< Octets in Ethertype field
37const size_t HDR_LEN = 14; ///< Total octets in Ethernet header (without 802.1Q tag)
38const size_t TAG_LEN = 4; ///< Octets in 802.1Q tag (TPID + priority + VLAN)
39const size_t MIN_DATA_LEN = 46; ///< Min octets in Ethernet payload (assuming no 802.1Q tag)
40const size_t MAX_DATA_LEN = 1500; ///< Max octets in Ethernet payload
41const size_t CRC_LEN = 4; ///< Octets in Ethernet frame check sequence
42
43
44/** \brief represents an Ethernet hardware address
45 */
46class Address : public boost::array<uint8_t, ADDR_LEN>
47{
48public:
49 /// Constructs a null Ethernet address (00:00:00:00:00:00)
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
63 /// True if this is a broadcast address (ff:ff:ff:ff:ff:ff)
64 bool
65 isBroadcast() const;
66
67 /// True if this is a multicast address
68 bool
69 isMulticast() const;
70
71 /// True if this is a null address (00:00:00:00:00:00)
72 bool
73 isNull() const;
74
75 /**
76 * \brief Converts the address to a human-readable string
77 *
78 * \param sep A character used to visually separate the octets,
79 * usually ':' (the default value) or '-'
80 */
81 std::string
82 toString(char sep = ':') const;
83
84 /**
85 * \brief Creates an Address from a string containing an Ethernet address
86 * in hexadecimal notation, with colons or hyphens as separators
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);
94};
95
96/// Returns the Ethernet broadcast address (ff:ff:ff:ff:ff:ff)
97Address
98getBroadcastAddress();
99
100/// Returns the default Ethernet multicast address for NDN
101Address
102getDefaultMulticastAddress();
103
104std::ostream&
105operator<<(std::ostream& o, const Address& a);
106
107} // namespace ethernet
108} // namespace util
109} // namespace ndn
110
111#endif // NDN_UTIL_ETHERNET_HPP