ip_conversion.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
16 #ifndef PSEN_SCAN_V2_STANDALONE_UTIL_IP_CONVERSION_H
17 #define PSEN_SCAN_V2_STANDALONE_UTIL_IP_CONVERSION_H
18 
19 #include <string>
20 #include <cstdint>
21 #include <cassert>
22 #include <stdexcept>
23 #include <limits>
24 
25 #ifdef __linux__
26 #include <arpa/inet.h>
27 #endif
28 #ifdef _WIN32
29 #include <WinSock2.h>
30 #endif
31 
33 {
34 namespace util
35 {
36 #ifdef __linux__
37 inline uint32_t convertIP(const std::string& ip)
38 {
39  const auto ip_number = inet_network(ip.c_str());
40  if (static_cast<in_addr_t>(-1) == ip_number)
41  {
42  throw std::invalid_argument("IP invalid");
43  }
44  assert(sizeof(ip_number) == 4 && "ip_number has not the expected size");
45  return static_cast<uint32_t>(ip_number);
46 }
47 #endif
48 
49 #ifdef _WIN32
50 inline uint32_t convertIP(const std::string& ip)
51 {
52  const auto ip_number = inet_addr(ip.c_str());
53  if (INADDR_NONE == ip_number)
54  {
55  throw std::invalid_argument("IP invalid");
56  }
57  assert(sizeof(ip_number) == 4 && "ip_number has not the expected size");
58  return htonl(static_cast<uint32_t>(ip_number));
59 }
60 #endif
61 
62 inline uint16_t convertPort(const int& port)
63 {
64  if (port < std::numeric_limits<uint16_t>::min() || port > std::numeric_limits<uint16_t>::max())
65  {
66  throw std::out_of_range("Port out of range");
67  }
68  return static_cast<uint16_t>(port);
69 }
70 
71 } // namespace util
72 } // namespace psen_scan_v2_standalone
73 
74 #endif
psen_scan_v2_standalone::util::convertPort
uint16_t convertPort(const int &port)
Definition: ip_conversion.h:62
psen_scan_v2_standalone
Root namespace in which the software components to communicate with the scanner (firmware-version: 2)...
Definition: udp_client.h:41


psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Nov 25 2023 03:46:26