TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
configurers_utils.cpp
Go to the documentation of this file.
1 /**
2  * @file configurers_utils.cpp
3  *
4  * @author Lucas Haug <lucas.haug@thuneratz.org>
5  *
6  * @brief Configurers utilitary funtions
7  *
8  * @date 06/2021
9  *
10  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
11  *
12  */
13 
15 
16 /*****************************************
17  * Private Constants
18  *****************************************/
19 
20 #define IPV4_NUM_OF_BYTES 4U
21 
22 /*****************************************
23  * Public Funtions Bodies Definitions
24  *****************************************/
25 
26 namespace travesim {
27 bool ipv4_string_to_uint(std::string ip_string, uint* ip_uint) {
28  std::stringstream ip_stream(ip_string);
29 
30  char dot;
31 
32  if (ip_stream.rdbuf()->in_avail() == 0) {
33  return false;
34  } else {
35  ip_stream >> ip_uint[0];
36 
37  if (ip_stream.fail()) {
38  return false;
39  }
40  }
41 
42  for (uint i = 1; i < IPV4_NUM_OF_BYTES; i++) {
43  if (ip_stream.rdbuf()->in_avail() == 0) {
44  return false;
45  } else {
46  ip_stream >> dot;
47 
48  if (ip_stream.fail()) {
49  return false;
50  }
51 
52  if (dot != '.') {
53  return false;
54  }
55  }
56 
57  if (ip_stream.rdbuf()->in_avail() == 0) {
58  return false;
59  } else {
60  ip_stream >> ip_uint[i];
61 
62  if (ip_stream.fail()) {
63  return false;
64  }
65  }
66  }
67 
68  if (ip_stream.rdbuf()->in_avail() != 0) {
69  return false;
70  }
71 
72  return true;
73 }
74 
75 IPValidation check_valid_ip(std::string ip, std::string min_ip, std::string max_ip) {
76  uint ip_uint[IPV4_NUM_OF_BYTES] = {0};
77  uint min_ip_uint[IPV4_NUM_OF_BYTES] = {0};
78  uint max_ip_uint[IPV4_NUM_OF_BYTES] = {0};
79 
80  // Check formats
81  if (!ipv4_string_to_uint(ip, ip_uint)) {
83  }
84 
85  if (!ipv4_string_to_uint(min_ip, min_ip_uint)) {
87  }
88 
89  if (!ipv4_string_to_uint(max_ip, max_ip_uint)) {
91  }
92 
93  // Check numbers
94  for (uint i = 0; i < IPV4_NUM_OF_BYTES; i++) {
95  if (ip_uint[i] > 0xFF) {
97  }
98 
99  if (min_ip_uint[i] > 0xFF) {
101  }
102 
103  if (max_ip_uint[i] > 0xFF) {
105  }
106  }
107 
108  // Check range
109  uint32_t ip_num = 0;
110  uint32_t max_num = 0;
111  uint32_t min_num = 0;
112 
113  for (uint8_t i = 0; i < IPV4_NUM_OF_BYTES; i++) {
114  ip_num = ip_num << 8;
115  ip_num += ip_uint[i];
116  min_num = min_num << 8;
117  min_num += min_ip_uint[i];
118  max_num = max_num << 8;
119  max_num += max_ip_uint[i];
120  }
121 
122  if ((ip_num < min_num) || (ip_num > max_num)) {
124  }
125 
126  return IPValidation::VALID;
127 }
128 
129 std::string get_error_msg(IPValidation error) {
130  switch (error) {
132  return "The IP string is wrong formatted.";
133  }
134 
136  return "The numbers on the ip are not representable by 8 bits.";
137  }
138 
140  return "The IP is not in the specified range. Hover over the parameterto see the range.";
141  }
142 
143  default: {
144  return "No error.";
145  }
146  }
147 }
148 }
bool ipv4_string_to_uint(std::string ip_string, uint *ip_uint)
Converts a IPv4 in a string to a array of unsigned integers, where the most significant byte of the I...
IPValidation
Type used to validate a IP string.
#define IPV4_NUM_OF_BYTES
std::string get_error_msg(IPValidation error)
Get the error msg based on the validation type.
IPValidation check_valid_ip(std::string ip, std::string min_ip, std::string max_ip)
Checks if a IP is valid and is in the specified range.
Configurers utilitary funtions.