1 #include "otsdaq/NetworkUtilities/NetworkConverters.h"
4 #include "otsdaq/Macros/CoutMacros.h"
5 #include "otsdaq/MessageFacility/MessageFacility.h"
10 NetworkConverters::NetworkConverters() {}
13 NetworkConverters::~NetworkConverters() {}
16 std::string NetworkConverters::nameToStringIP(
const std::string& value)
18 struct sockaddr_in socketAddress;
19 if(inet_aton(value.c_str(), &socketAddress.sin_addr) == 0)
20 std::cout << __COUT_HDR_FL__ <<
"ERROR: Invalid IP address " << value << std::endl;
21 return networkToStringIP(socketAddress.sin_addr.s_addr);
25 std::string NetworkConverters::stringToNameIP(
const std::string& value)
27 struct sockaddr_in socketAddress;
28 socketAddress.sin_addr.s_addr = stringToNetworkIP(value);
29 return std::string(inet_ntoa(socketAddress.sin_addr));
32 uint32_t NetworkConverters::stringToNetworkIP(
const std::string& value) {
return unsignedToNetworkIP(stringToUnsignedIP(value)); }
35 std::string NetworkConverters::networkToStringIP(uint32_t value) {
return unsignedToStringIP(networkToUnsignedIP(value)); }
38 std::string NetworkConverters::unsignedToStringIP(uint32_t value)
40 std::string returnValue;
41 returnValue += (value >> 24) & 0xff;
42 returnValue += (value >> 16) & 0xff;
43 returnValue += (value >> 8) & 0xff;
44 returnValue += (value)&0xff;
49 uint32_t NetworkConverters::stringToUnsignedIP(
const std::string& value)
51 return ((((uint32_t)value[0]) & 0xff) << 24) + ((((uint32_t)value[1]) & 0xff) << 16) + ((((uint32_t)value[2]) & 0xff) << 8) + (((uint32_t)value[3]) & 0xff);
55 uint32_t NetworkConverters::unsignedToNetworkIP(uint32_t value) {
return htonl(value); }
58 uint32_t NetworkConverters::networkToUnsignedIP(uint32_t value) {
return ntohl(value); }
61 uint16_t NetworkConverters::stringToNetworkPort(
const std::string& value) {
return unsignedToNetworkPort(stringToUnsignedPort(value)); }
64 std::string NetworkConverters::networkToStringPort(uint16_t value) {
return unsignedToStringPort(networkToUnsignedPort(value)); }
67 std::string NetworkConverters::unsignedToStringPort(uint16_t value)
69 std::string returnValue;
70 returnValue += (value >> 8) & 0xff;
71 returnValue += (value)&0xff;
76 uint16_t NetworkConverters::stringToUnsignedPort(
const std::string& value) {
return ((((uint32_t)value[0]) & 0xff) << 8) + (((uint32_t)value[1]) & 0xff); }
79 uint16_t NetworkConverters::unsignedToNetworkPort(uint16_t value) {
return htons(value); }
82 uint16_t NetworkConverters::networkToUnsignedPort(uint16_t value) {
return ntohs(value); }