Davide Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * 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 Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 24 | |
| 25 | #include "ethernet.hpp" |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | namespace nfd { |
| 30 | namespace ethernet { |
| 31 | |
| 32 | std::string |
| 33 | Address::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 | |
| 42 | Address |
| 43 | Address::fromString(const std::string& str) |
| 44 | { |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 45 | 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 Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 58 | Address a; |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 59 | 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 Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 64 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 65 | // check that each value fits into a uint8_t |
| 66 | if (temp[i] > 0xFF) |
| 67 | return Address(); |
Davide Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 68 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 69 | a[i] = static_cast<uint8_t>(temp[i]); |
| 70 | } |
Davide Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 71 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 72 | return a; |
Davide Pesavento | 56fb597 | 2014-03-12 06:18:37 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | std::ostream& |
| 76 | operator<<(std::ostream& o, const Address& a) |
| 77 | { |
| 78 | return o << a.toString(); |
| 79 | } |
| 80 | |
| 81 | } // namespace ethernet |
| 82 | } // namespace nfd |