DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ip_format.hpp
Go to the documentation of this file.
1
9#ifndef SNBMODULES_INCLUDE_SNBMODULES_IP_FORMAT_HPP_
10#define SNBMODULES_INCLUDE_SNBMODULES_IP_FORMAT_HPP_
11
12// #define _CRT_SECURE_NO_WARNINGS
13
14#include <cstring>
15#include <filesystem>
16#include <iostream>
17#include <stdexcept>
18#include <string>
19#include <utility>
20#include <vector>
21
22namespace dunedaq::snbmodules {
26{
27
28public:
29 // Constructors
33 explicit IPFormat(const std::string& ip = "0.0.0.0", int port = 0)
34 {
35 set_port(port);
36 set_ip(ip);
37 }
38
41 {
43 set_ip(o.m_ip);
44 }
45
47 IPFormat& operator=(IPFormat const&) = default;
48
50 bool operator==(IPFormat const& o) const { return m_ip == o.m_ip && m_port == o.m_port; }
51
53 bool operator<(IPFormat const& o) const { return m_ip.compare(o.m_ip) || (m_ip == o.m_ip && m_port < o.m_port); }
54
57 std::string get_ip_port() const { return m_ip + ":" + std::to_string(m_port); }
58
61 inline bool is_default() const { return m_ip == "0.0.0.0" && m_port == 0; }
62
63 // Setters
64 void set_port(int port)
65 {
66
67 if (port < 0 || port > 65535) {
68 throw std::invalid_argument("Port must be between 0 and 65535");
69 }
70
71 m_port = port;
72 }
73
74#ifndef _MSC_VER
75 inline char* strtok_s(char* s, const char* delim, char** context) { return strtok_r(s, delim, context); }
76#endif // !_MSC_VER
77
80 void set_ip(const std::string& ip)
81 {
82 if (ip.empty()) {
83 throw std::invalid_argument("IP address cannot be empty");
84 }
85
86 // Splitting the string IP:PORT or IP
87 std::vector<std::string> ip_port_pair;
88 char* next_token = nullptr;
89
90 char* ip_char = new char[ip.length() + 1];
91 strcpy(ip_char, ip.c_str());
92
93 char* token = strtok_s(ip_char, ":", &next_token);
94 while (token != nullptr) {
95 ip_port_pair.emplace_back(std::string(token));
96 token = strtok_s(nullptr, ":", &next_token);
97 }
98
99 delete[] ip_char;
100
101 if (ip_port_pair.size() != 1 && ip_port_pair.size() != 2) {
102 throw std::invalid_argument("Invalid IP address format (IP:PORT or IP)");
103 }
104
105 // TODO Aug-14-2022 Leo Joly leo.vincent.andre.joly@cern.ch : Check if the IPv4 address is valid
106
107 // set values
108 if (ip_port_pair.size() == 2) {
109 set_port(std::stoi(ip_port_pair[1]));
110 }
111
112 m_ip = ip_port_pair[0];
113 }
114
115 // Getters
116 std::string get_ip() const { return m_ip; }
117 int get_port() const { return m_port; }
118
119private:
121 std::string m_ip = "0.0.0.0";
123 int m_port = 0;
124};
125} // namespace dunedaq::snbmodules
126#endif // SNBMODULES_INCLUDE_SNBMODULES_IP_FORMAT_HPP_
Class that represents an IP address and a port TODO: should be replaced by something better ?
Definition ip_format.hpp:26
IPFormat(const IPFormat &o)
Copy constructor.
Definition ip_format.hpp:40
std::string get_ip_port() const
Get the IP address and the port in the format "ip:port".
Definition ip_format.hpp:57
bool operator<(IPFormat const &o) const
< operator
Definition ip_format.hpp:53
char * strtok_s(char *s, const char *delim, char **context)
Definition ip_format.hpp:75
std::string get_ip() const
bool is_default() const
Check if the IP address and the port are set to the default values.
Definition ip_format.hpp:61
IPFormat(const std::string &ip="0.0.0.0", int port=0)
Constructor that set the IP address and the port.
Definition ip_format.hpp:33
bool operator==(IPFormat const &o) const
== operator
Definition ip_format.hpp:50
void set_ip(const std::string &ip)
Set the IP address, must be called after set_port() if the port is specified in the IP address ex 0....
Definition ip_format.hpp:80
IPFormat & operator=(IPFormat const &)=default
Defualt = operator.
std::string m_ip
IP address.