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 <cstdint>
40 #include <iomanip>
41 #include <sstream>
42 #include <stdexcept>
43 #include <algorithm>
44 #include <array>
45 
46 inline std::string mac2string(const uint64_t mac)
47 {
48  std::ostringstream out;
49 
50  out << std::hex << std::setfill('0');
51  out << std::setw(2) << ((mac>>40)&0xff) << ':' << std::setw(2) << ((mac>>32)&0xff) << ':'
52  << std::setw(2) << ((mac>>24)&0xff) << ':' << std::setw(2) << ((mac>>16)&0xff) << ':'
53  << std::setw(2) << ((mac>>8)&0xff) << ':' << std::setw(2) << (mac&0xff);
54 
55  return out.str();
56 }
57 
58 inline std::string ip2string(const uint32_t ip)
59 {
60  std::ostringstream out;
61 
62  out << ((ip>>24)&0xff) << '.' << ((ip>>16)&0xff) << '.'
63  << ((ip>>8)&0xff) << '.' << (ip&0xff);
64 
65  return out.str();
66 }
67 
68 template<uint32_t n>
69 std::array<std::string, n> split(const std::string& s, const char sep)
70 {
71  std::array<std::string, n> result;
72 
73  if (!s.empty())
74  {
75  if (s.front() == sep || s.back() == sep)
76  {
77  throw std::invalid_argument("strings starts or ends with separator");
78  }
79  }
80 
81  std::istringstream iss(s);
82  for (uint32_t i = 0; i < n; ++i)
83  {
84  if (!std::getline(iss, result[i], sep))
85  {
86  throw std::out_of_range("n");
87  }
88  }
89 
90  std::string tmp;
91  if (std::getline(iss, tmp, sep))
92  {
93  throw std::out_of_range("n");
94  }
95 
96  return result;
97 }
98 
99 template<uint32_t n>
100 std::array<uint8_t, n> string2byte(const std::string& s,
101  const int base,
102  const char sep)
103 {
104  const auto splitted = split<n>(s, sep);
105 
106  std::array<uint8_t, n> result;
107 
108  std::transform(std::begin(splitted),
109  std::end(splitted),
110  std::begin(result),
111  [&base](const std::string& s) -> std::uint8_t
112  {
113  const auto v = std::stoul(s, nullptr, base);
114  if (v > 255)
115  {
116  throw std::out_of_range("number is larger than 255");
117  }
118  return static_cast<std::uint8_t>(v);
119  });
120 
121  return result;
122 }
123 
124 inline std::array<uint8_t, 6> string2mac(const std::string& mac)
125 {
126  return string2byte<6>(mac, 16, ':');
127 }
128 
129 inline std::array<uint8_t, 4> string2ip(const std::string& ip)
130 {
131  return string2byte<4>(ip, 10, '.');
132 }
133 
134 template<std::size_t N> struct MinFittingType { };
135 template<> struct MinFittingType<1> { using type = std::uint8_t; };
136 template<> struct MinFittingType<2> { using type = std::uint16_t; };
137 template<> struct MinFittingType<3> { using type = std::uint32_t; };
138 template<> struct MinFittingType<4> { using type = std::uint32_t; };
139 template<> struct MinFittingType<5> { using type = std::uint64_t; };
140 template<> struct MinFittingType<6> { using type = std::uint64_t; };
141 template<> struct MinFittingType<7> { using type = std::uint64_t; };
142 template<> struct MinFittingType<8> { using type = std::uint64_t; };
143 
144 template<std::size_t N>
146 byteArrayToInt(const std::array<std::uint8_t, N> &a)
147 {
148  using ReturnType = typename MinFittingType<N>::type;
149  ReturnType result{};
150  for (std::size_t i = 0; i < N; ++i)
151  {
152  result |= (static_cast<ReturnType>(a[i]) << ((N - 1 - i) * 8));
153  }
154  return result;
155 }
156 
157 inline bool wildcardMatch(std::string::const_iterator str_first,
158  std::string::const_iterator str_last,
159  std::string::const_iterator p_first,
160  std::string::const_iterator p_last)
161 {
162  if (str_first == str_last && p_first == p_last)
163  { return true; }
164 
165  if (str_first == str_last)
166  {
167  if (*p_first == '*')
168  {
169  // if there is no more character after * => match
170  return std::next(p_first) == p_last;
171  }
172  }
173 
174  if (p_first == p_last)
175  {
176  return false;
177  }
178 
179  if (*p_first == '?' || std::tolower(*p_first) == std::tolower(*str_first))
180  {
181  return wildcardMatch(std::next(str_first), str_last,
182  std::next(p_first), p_last);
183  }
184 
185  if (*p_first == '*')
186  {
187  return wildcardMatch(std::next(str_first), str_last, p_first, p_last) ||
188  wildcardMatch(str_first, str_last, std::next(p_first), p_last);
189  }
190 
191  return false;
192 }
193 
194 #endif // RCDISCOVER_UTILS_H
string2ip
std::array< uint8_t, 4 > string2ip(const std::string &ip)
Definition: utils.h:129
ip2string
std::string ip2string(const uint32_t ip)
Definition: utils.h:58
MinFittingType
Definition: utils.h:134
MinFittingType< 5 >::type
std::uint64_t type
Definition: utils.h:139
split
std::array< std::string, n > split(const std::string &s, const char sep)
Definition: utils.h:69
string2byte
std::array< uint8_t, n > string2byte(const std::string &s, const int base, const char sep)
Definition: utils.h:100
MinFittingType< 6 >::type
std::uint64_t type
Definition: utils.h:140
byteArrayToInt
MinFittingType< N >::type byteArrayToInt(const std::array< std::uint8_t, N > &a)
Definition: utils.h:146
MinFittingType< 8 >::type
std::uint64_t type
Definition: utils.h:142
MinFittingType< 1 >::type
std::uint8_t type
Definition: utils.h:135
MinFittingType< 2 >::type
std::uint16_t type
Definition: utils.h:136
string2mac
std::array< uint8_t, 6 > string2mac(const std::string &mac)
Definition: utils.h:124
mac2string
std::string mac2string(const uint64_t mac)
Definition: utils.h:46
MinFittingType< 3 >::type
std::uint32_t type
Definition: utils.h:137
MinFittingType< 4 >::type
std::uint32_t type
Definition: utils.h:138
MinFittingType< 7 >::type
std::uint64_t type
Definition: utils.h:141
wildcardMatch
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:157


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Thu Aug 1 2024 02:55:56