mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 10:07:59 +02:00
finished
This commit is contained in:
@ -7,6 +7,7 @@ set(SOURCES
|
||||
src/DataSocket.cpp
|
||||
src/ServerSocket.cpp
|
||||
src/ServerInterface.cpp
|
||||
src/network_utils.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
@ -30,6 +31,7 @@ set(PUBLICHEADERS
|
||||
include/DataSocket.h
|
||||
include/ServerSocket.h
|
||||
include/ServerInterface.h
|
||||
include/network_utils.h
|
||||
)
|
||||
|
||||
add_library(slsSupportLib SHARED
|
||||
|
@ -17,7 +17,7 @@
|
||||
#endif
|
||||
|
||||
#ifndef FILELOG_MAX_LEVEL
|
||||
#define FILELOG_MAX_LEVEL logDEBUG1
|
||||
#define FILELOG_MAX_LEVEL logINFO
|
||||
#endif
|
||||
|
||||
|
||||
|
14
slsSupportLib/include/network_utils.h
Normal file
14
slsSupportLib/include/network_utils.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace sls {
|
||||
|
||||
std::string MacAddrToString(uint64_t mac);
|
||||
uint64_t MacStringToUint(std::string mac);
|
||||
|
||||
uint32_t IpStringToUint(const char* ipstr);
|
||||
std::string IpToString(uint32_t ip);
|
||||
|
||||
uint32_t HostnameToIp(const char *const hostname);
|
||||
|
||||
} // namespace sls
|
@ -81,7 +81,7 @@ void DataSocket::close() {
|
||||
if(::close(socketId_)){
|
||||
throw SocketError("could not close socket");
|
||||
}
|
||||
socketId_ = 0;
|
||||
socketId_ = -1;
|
||||
|
||||
} else {
|
||||
throw std::runtime_error("Socket ERROR: close called on bad socket\n");
|
||||
|
69
slsSupportLib/src/network_utils.cpp
Normal file
69
slsSupportLib/src/network_utils.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include "sls_detector_exceptions.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace sls {
|
||||
|
||||
std::string MacAddrToString(uint64_t mac) {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
ss << ((mac >> 40) & 0xFF);
|
||||
for (int i = 32; i >= 0; i -= 8) {
|
||||
ss << ':' << ((mac >> i) & 0xFF);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
uint64_t MacStringToUint(std::string mac) {
|
||||
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') ||
|
||||
(mac[11] != ':') || (mac[14] != ':')) {
|
||||
return 0;
|
||||
}
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
return std::stol(mac, nullptr, 16);
|
||||
}
|
||||
|
||||
uint32_t IpStringToUint(const char *ipstr) {
|
||||
uint32_t ip{0};
|
||||
inet_pton(AF_INET, ipstr, &ip);
|
||||
return ip;
|
||||
}
|
||||
|
||||
std::string IpToString(uint32_t ip) {
|
||||
char ipstring[INET_ADDRSTRLEN];
|
||||
if (inet_ntop(AF_INET, &ip, ipstring, INET_ADDRSTRLEN) == nullptr) {
|
||||
// handle error
|
||||
}
|
||||
// TODO! Check return
|
||||
return ipstring;
|
||||
}
|
||||
|
||||
uint32_t HostnameToIp(const char *const hostname) {
|
||||
struct addrinfo hints, *result;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
if (getaddrinfo(hostname, NULL, &hints, &result)) {
|
||||
freeaddrinfo(result);
|
||||
throw RuntimeError("Could not convert hostname to ip");
|
||||
}
|
||||
uint32_t ip = ((struct sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
|
||||
freeaddrinfo(result);
|
||||
return ip;
|
||||
}
|
||||
|
||||
} // namespace sls
|
||||
|
||||
// char ipstring[INET_ADDRSTRLEN];
|
||||
// inet_ntop(AF_INET, &detector_shm()->detectorIP, ipstring, INET_ADDRSTRLEN);
|
||||
|
||||
// inet_pton(AF_INET, DEFAULT_DET_IP, &(detector_shm()->detectorIP));
|
@ -10,6 +10,7 @@ set(SOURCES
|
||||
test-ClientInterface.cpp
|
||||
test-CmdLineParser.cpp
|
||||
test-container_utils.cpp
|
||||
test-network_utils.cpp
|
||||
test-string_utils.cpp
|
||||
test-Timer.cpp
|
||||
)
|
||||
|
44
slsSupportLib/tests/test-network_utils.cpp
Normal file
44
slsSupportLib/tests/test-network_utils.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "network_utils.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "string_utils.h"
|
||||
|
||||
using namespace sls;
|
||||
TEST_CASE("Convert mac address") {
|
||||
|
||||
std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028};
|
||||
std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4", "ee:50:22:46:d9:f4"};
|
||||
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||
auto mac = vec_addr[i];
|
||||
auto answer = vec_ans[i];
|
||||
|
||||
std::string string_addr = MacAddrToString(mac);
|
||||
CHECK(string_addr == answer);
|
||||
CHECK(MacStringToUint(string_addr) == mac);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert IP"){
|
||||
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
|
||||
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163", "129.129.202.160"};
|
||||
|
||||
for (int i=0; i!= vec_addr.size(); ++i){
|
||||
auto ip = vec_addr[i];
|
||||
auto answer = vec_ans[i];
|
||||
|
||||
auto string_addr = IpToString(ip);
|
||||
CHECK(string_addr == answer);
|
||||
CHECK(IpStringToUint(string_addr.c_str()) == ip);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("IP not valid"){
|
||||
|
||||
CHECK(IpStringToUint("hej") == 0);
|
||||
CHECK(IpStringToUint("mpc2408") == 0);
|
||||
}
|
||||
|
||||
// TEST_CASE("Lookup ip")
|
Reference in New Issue
Block a user