utils.h
Go to the documentation of this file.
1 /*
2  * rcdiscover - the network discovery tool for Roboception devices
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Raphael Schaller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef RCDISCOVER_UTILS_H
37 #define RCDISCOVER_UTILS_H
38 
39 #include <iomanip>
40 #include <sstream>
41 #include <stdexcept>
42 #include <algorithm>
43 #include <array>
44 
45 inline std::string mac2string(const uint64_t mac)
46 {
47  std::ostringstream out;
48 
49  out << std::hex << std::setfill('0');
50  out << std::setw(2) << ((mac>>40)&0xff) << ':' << std::setw(2) << ((mac>>32)&0xff) << ':'
51  << std::setw(2) << ((mac>>24)&0xff) << ':' << std::setw(2) << ((mac>>16)&0xff) << ':'
52  << std::setw(2) << ((mac>>8)&0xff) << ':' << std::setw(2) << (mac&0xff);
53 
54  return out.str();
55 }
56 
57 inline std::string ip2string(const uint32_t ip)
58 {
59  std::ostringstream out;
60 
61  out << ((ip>>24)&0xff) << '.' << ((ip>>16)&0xff) << '.'
62  << ((ip>>8)&0xff) << '.' << (ip&0xff);
63 
64  return out.str();
65 }
66 
67 template<uint32_t n>
68 std::array<std::string, n> split(const std::string& s, const char sep)
69 {
70  std::array<std::string, n> result;
71 
72  if (!s.empty())
73  {
74  if (s.front() == sep || s.back() == sep)
75  {
76  throw std::invalid_argument("strings starts or ends with separator");
77  }
78  }
79 
80  std::istringstream iss(s);
81  for (uint32_t i = 0; i < n; ++i)
82  {
83  if (!std::getline(iss, result[i], sep))
84  {
85  throw std::out_of_range("n");
86  }
87  }
88 
89  std::string tmp;
90  if (std::getline(iss, tmp, sep))
91  {
92  throw std::out_of_range("n");
93  }
94 
95  return result;
96 }
97 
98 template<uint32_t n>
99 std::array<uint8_t, n> string2byte(const std::string& s,
100  const int base,
101  const char sep)
102 {
103  const auto splitted = split<n>(s, sep);
104 
105  std::array<uint8_t, n> result;
106 
107  std::transform(std::begin(splitted),
108  std::end(splitted),
109  std::begin(result),
110  [&base](const std::string& s) -> std::uint8_t
111  {
112  const auto v = std::stoul(s, nullptr, base);
113  if (v > 255)
114  {
115  throw std::out_of_range("number is larger than 255");
116  }
117  return static_cast<std::uint8_t>(v);
118  });
119 
120  return result;
121 }
122 
123 inline std::array<uint8_t, 6> string2mac(const std::string& mac)
124 {
125  return string2byte<6>(mac, 16, ':');
126 }
127 
128 inline std::array<uint8_t, 4> string2ip(const std::string& ip)
129 {
130  return string2byte<4>(ip, 10, '.');
131 }
132 
133 template<std::size_t N> struct MinFittingType { };
134 template<> struct MinFittingType<1> { using type = std::uint8_t; };
135 template<> struct MinFittingType<2> { using type = std::uint16_t; };
136 template<> struct MinFittingType<3> { using type = std::uint32_t; };
137 template<> struct MinFittingType<4> { using type = std::uint32_t; };
138 template<> struct MinFittingType<5> { using type = std::uint64_t; };
139 template<> struct MinFittingType<6> { using type = std::uint64_t; };
140 template<> struct MinFittingType<7> { using type = std::uint64_t; };
141 template<> struct MinFittingType<8> { using type = std::uint64_t; };
142 
143 template<std::size_t N>
145 byteArrayToInt(const std::array<std::uint8_t, N> &a)
146 {
147  using ReturnType = typename MinFittingType<N>::type;
148  ReturnType result{};
149  for (std::size_t i = 0; i < N; ++i)
150  {
151  result |= (static_cast<ReturnType>(a[i]) << ((N - 1 - i) * 8));
152  }
153  return result;
154 }
155 
156 inline bool wildcardMatch(std::string::const_iterator str_first,
157  std::string::const_iterator str_last,
158  std::string::const_iterator p_first,
159  std::string::const_iterator p_last)
160 {
161  if (str_first == str_last && p_first == p_last)
162  { return true; }
163 
164  if (str_first == str_last)
165  {
166  if (*p_first == '*')
167  {
168  // if there is no more character after * => match
169  return std::next(p_first) == p_last;
170  }
171  }
172 
173  if (p_first == p_last)
174  {
175  return false;
176  }
177 
178  if (*p_first == '?' || std::tolower(*p_first) == std::tolower(*str_first))
179  {
180  return wildcardMatch(std::next(str_first), str_last,
181  std::next(p_first), p_last);
182  }
183 
184  if (*p_first == '*')
185  {
186  return wildcardMatch(std::next(str_first), str_last, p_first, p_last) ||
187  wildcardMatch(str_first, str_last, std::next(p_first), p_last);
188  }
189 
190  return false;
191 }
192 
193 #endif // RCDISCOVER_UTILS_H
std::uint64_t type
Definition: utils.h:141
std::array< uint8_t, 6 > string2mac(const std::string &mac)
Definition: utils.h:123
MinFittingType< N >::type byteArrayToInt(const std::array< std::uint8_t, N > &a)
Definition: utils.h:145
bool wildcardMatch(std::string::const_iterator str_first, std::string::const_iterator str_last, std::string::const_iterator p_first, std::string::const_iterator p_last)
Definition: utils.h:156
std::uint64_t type
Definition: utils.h:139
std::string ip2string(const uint32_t ip)
Definition: utils.h:57
std::uint16_t type
Definition: utils.h:135
std::array< uint8_t, n > string2byte(const std::string &s, const int base, const char sep)
Definition: utils.h:99
std::uint32_t type
Definition: utils.h:136
std::string mac2string(const uint64_t mac)
Definition: utils.h:45
std::uint64_t type
Definition: utils.h:140
std::array< std::string, n > split(const std::string &s, const char sep)
Definition: utils.h:68
std::uint64_t type
Definition: utils.h:138
std::array< uint8_t, 4 > string2ip(const std::string &ip)
Definition: utils.h:128
std::uint8_t type
Definition: utils.h:134
std::uint32_t type
Definition: utils.h:137


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Sun Apr 18 2021 02:16:32