blob: 1aeaca69efe826731734f4cc2eda65933c1dc1b1 [file] [log] [blame]
Davide Pesavento56fb5972014-03-12 06:18:37 +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 Pesavento56fb5972014-03-12 06:18:37 +010024
25#include "ethernet.hpp"
26
27#include <stdio.h>
28
29namespace nfd {
30namespace ethernet {
31
32std::string
33Address::toString(char sep) const
34{
35 char s[18]; // 12 digits + 5 separators + null terminator
36 ::snprintf(s, sizeof(s), "%02x%c%02x%c%02x%c%02x%c%02x%c%02x",
37 elems[0], sep, elems[1], sep, elems[2], sep,
38 elems[3], sep, elems[4], sep, elems[5]);
39 return std::string(s);
40}
41
42Address
43Address::fromString(const std::string& str)
44{
Davide Pesavento1bdef282014-04-08 20:59:50 +020045 unsigned short temp[ADDR_LEN];
46 char sep[5][2]; // 5 * (1 separator char + 1 null terminator)
47 int n = 0; // num of chars read from the input string
48
49 // ISO C++98 does not support the 'hh' type modifier
50 /// \todo use SCNx8 (cinttypes) when we enable C++11
51 int ret = ::sscanf(str.c_str(), "%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%1[:-]%2hx%n",
52 &temp[0], &sep[0][0], &temp[1], &sep[1][0], &temp[2], &sep[2][0],
53 &temp[3], &sep[3][0], &temp[4], &sep[4][0], &temp[5], &n);
54
55 if (ret < 11 || static_cast<size_t>(n) != str.length())
56 return Address();
57
Davide Pesavento56fb5972014-03-12 06:18:37 +010058 Address a;
Davide Pesavento1bdef282014-04-08 20:59:50 +020059 for (size_t i = 0; i < ADDR_LEN; ++i)
60 {
61 // check that all separators are actually the same char (: or -)
62 if (i < 5 && sep[i][0] != sep[0][0])
63 return Address();
Davide Pesavento56fb5972014-03-12 06:18:37 +010064
Davide Pesavento1bdef282014-04-08 20:59:50 +020065 // check that each value fits into a uint8_t
66 if (temp[i] > 0xFF)
67 return Address();
Davide Pesavento56fb5972014-03-12 06:18:37 +010068
Davide Pesavento1bdef282014-04-08 20:59:50 +020069 a[i] = static_cast<uint8_t>(temp[i]);
70 }
Davide Pesavento56fb5972014-03-12 06:18:37 +010071
Davide Pesavento1bdef282014-04-08 20:59:50 +020072 return a;
Davide Pesavento56fb5972014-03-12 06:18:37 +010073}
74
75std::ostream&
76operator<<(std::ostream& o, const Address& a)
77{
78 return o << a.toString();
79}
80
81} // namespace ethernet
82} // namespace nfd