blob: d5eb780069baa8f5a0c6ee8fbed437bf1c7a1104 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shie3ef6ee2014-10-05 14:40:54 -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 * 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
28#include "ethernet.hpp"
29
30#include <stdio.h>
31
32namespace ndn {
33namespace util {
34namespace ethernet {
35
36Address
37getBroadcastAddress()
38{
39 static Address bcast(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
40 return bcast;
41}
42
43Address
44getDefaultMulticastAddress()
45{
46 static Address mcast(0x01, 0x00, 0x5E, 0x00, 0x17, 0xAA);
47 return mcast;
48}
49
50Address::Address()
51{
52 assign(0);
53}
54
55Address::Address(uint8_t a1, uint8_t a2, uint8_t a3, uint8_t a4, uint8_t a5, uint8_t a6)
56{
57 elems[0] = a1;
58 elems[1] = a2;
59 elems[2] = a3;
60 elems[3] = a4;
61 elems[4] = a5;
62 elems[5] = a6;
63}
64
65Address::Address(const uint8_t octets[])
66{
67 std::copy(octets, octets + size(), begin());
68}
69
70Address::Address(const Address& address)
71{
72 std::copy(address.begin(), address.end(), begin());
73}
74
75bool
76Address::isBroadcast() const
77{
78 return elems[0] == 0xFF && elems[1] == 0xFF && elems[2] == 0xFF &&
79 elems[3] == 0xFF && elems[4] == 0xFF && elems[5] == 0xFF;
80}
81
82bool
83Address::isMulticast() const
84{
85 return (elems[0] & 1) != 0;
86}
87
88bool
89Address::isNull() const
90{
91 return elems[0] == 0x0 && elems[1] == 0x0 && elems[2] == 0x0 &&
92 elems[3] == 0x0 && elems[4] == 0x0 && elems[5] == 0x0;
93}
94
95std::string
96Address::toString(char sep) const
97{
98 char s[18]; // 12 digits + 5 separators + null terminator
99 ::snprintf(s, sizeof(s), "%02x%c%02x%c%02x%c%02x%c%02x%c%02x",
100 elems[0], sep, elems[1], sep, elems[2], sep,
101 elems[3], sep, elems[4], sep, elems[5]);
102 return std::string(s);
103}
104
105Address
106Address::fromString(const std::string& str)
107{
108 unsigned short temp[ADDR_LEN];
109 char sep[5][2]; // 5 * (1 separator char + 1 null terminator)
110 int n = 0; // num of chars read from the input string
111
112 // ISO C++98 does not support the 'hh' type modifier
113 /// \todo use SCNx8 (cinttypes) when we enable C++11
114 int ret = ::sscanf(str.c_str(), "%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%n",
115 &temp[0], &sep[0][0], &temp[1], &sep[1][0], &temp[2], &sep[2][0],
116 &temp[3], &sep[3][0], &temp[4], &sep[4][0], &temp[5], &n);
117
118 if (ret < 11 || static_cast<size_t>(n) != str.length())
119 return Address();
120
121 Address a;
122 for (size_t i = 0; i < ADDR_LEN; ++i)
123 {
124 // check that all separators are actually the same char (: or -)
125 if (i < 5 && sep[i][0] != sep[0][0])
126 return Address();
127
128 // check that each value fits into a uint8_t
129 if (temp[i] > 0xFF)
130 return Address();
131
132 a[i] = static_cast<uint8_t>(temp[i]);
133 }
134
135 return a;
136}
137
138std::ostream&
139operator<<(std::ostream& o, const Address& a)
140{
141 return o << a.toString();
142}
143
144} // namespace ethernet
145} // namespace util
146} // namespace ndn