This commit is contained in:
Erik Frojdh
2019-05-28 15:43:20 +02:00
14 changed files with 815 additions and 565 deletions

View File

@ -6,6 +6,7 @@ set(SOURCES
src/DataSocket.cpp
src/ServerSocket.cpp
src/ServerInterface.cpp
src/ServerInterface2.cpp
src/network_utils.cpp
)
@ -27,7 +28,7 @@ set(PUBLICHEADERS
include/ClientSocket.h
include/DataSocket.h
include/ServerSocket.h
include/ServerInterface.h
include/ServerInterface2.h
include/network_utils.h
include/FixedCapacityContainer.h
)

View File

@ -18,9 +18,11 @@ class DataSocket {
int getSocketId() const {
return socketId_;
}
size_t sendData(const void *buffer, size_t size);
size_t receiveData(void *buffer, size_t size);
int sendData(const void *buffer, size_t size);
int receiveData(void *buffer, size_t size);
int read(void *buffer, size_t size);
int setTimeOut(int t_seconds);
int setReceiveTimeout(int us);
void close();
void shutDownSocket();

View File

@ -0,0 +1,28 @@
#pragma once
#include "DataSocket.h"
namespace sls {
class ServerInterface2;
}
#include "ServerSocket.h"
#include "sls_detector_defs.h"
namespace sls {
class ServerInterface2 : public DataSocket {
using defs = slsDetectorDefs;
public:
ServerInterface2(int socketId) : DataSocket(socketId){}
int sendResult(bool update, int ret, void *retval, int retvalSize,
char *mess = nullptr);
int receiveArg(int &ret, char *mess, void *arg, int sizeofArg);
private:
};
} // namespace sls

View File

@ -1,7 +1,8 @@
#pragma once
#include "DataSocket.h"
#include "ServerInterface2.h"
#include "network_utils.h"
#include <cstdint>
#include <netdb.h>
#include <string>
@ -13,16 +14,20 @@ namespace sls {
class ServerSocket : public DataSocket {
public:
ServerSocket(int port);
DataSocket accept();
const std::string &getLastClient();
ServerInterface2 accept();
IpAddr getLastClient() noexcept { return lastClient; }
IpAddr getThisClient() noexcept { return thisClient; }
IpAddr getLockedBy() noexcept { return lockedBy; }
void setLockedBy(IpAddr addr) { lockedBy = addr; }
void setLastClient(IpAddr addr) { lastClient = addr; }
int getPort() const;
void SendResult(int &ret, void *retval, int retvalSize, char* mess);
void SendResult(int &ret, void *retval, int retvalSize, char *mess);
private:
std::string lastClient_ = std::string(INET_ADDRSTRLEN, '\0');
std::string thisClient_ = std::string(INET_ADDRSTRLEN, '\0');
IpAddr thisClient;
IpAddr lastClient;
IpAddr lockedBy;
int serverPort;
// char lastClient_[INET_ADDRSTRLEN]{};
};
}; // namespace sls

View File

@ -17,8 +17,8 @@
#endif
#ifndef FILELOG_MAX_LEVEL
#define FILELOG_MAX_LEVEL logINFO
// #define FILELOG_MAX_LEVEL logDEBUG5
// #define FILELOG_MAX_LEVEL logINFO
#define FILELOG_MAX_LEVEL logDEBUG5
#endif

View File

@ -4,13 +4,12 @@
namespace sls {
uint32_t HostnameToIp(const char *hostname);
class IpAddr {
private:
uint32_t addr_{0};
public:
constexpr IpAddr() noexcept{}
constexpr IpAddr(uint32_t address) noexcept : addr_{address} {}
IpAddr(const std::string &address);
IpAddr(const char *address);
@ -57,6 +56,10 @@ class MacAddr {
constexpr uint64_t uint64() const noexcept { return addr_; }
};
uint32_t HostnameToIp(const char *hostname);
std::string IpToInterfaceName(const std::string& ip);
MacAddr InterfaceNameToMac(std::string inf);
std::ostream &operator<<(std::ostream &out, const IpAddr &addr);
std::ostream &operator<<(std::ostream &out, const MacAddr &addr);

View File

@ -9,6 +9,7 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
namespace sls {
@ -39,18 +40,31 @@ DataSocket &DataSocket::operator=(DataSocket &&move) noexcept {
return *this;
}
size_t DataSocket::receiveData(void *buffer, size_t size) {
int DataSocket::receiveData(void *buffer, size_t size) {
size_t dataRead = 0;
while (dataRead < size) {
dataRead +=
read(getSocketId(), reinterpret_cast<char *>(buffer) + dataRead,
::read(getSocketId(), reinterpret_cast<char *>(buffer) + dataRead,
size - dataRead);
}
return dataRead;
}
size_t DataSocket::sendData(const void *buffer, size_t size) {
size_t dataSent = 0;
int DataSocket::read(void *buffer, size_t size){
return ::read(getSocketId(), reinterpret_cast<char *>(buffer), size);
}
int DataSocket::setReceiveTimeout(int us) {
timeval t{};
t.tv_sec = 0;
t.tv_usec = us;
return ::setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, &t,
sizeof(struct timeval));
}
int DataSocket::sendData(const void *buffer, size_t size) {
int dataSent = 0;
while (dataSent < size) {
dataSent +=
write(getSocketId(), reinterpret_cast<const char *>(buffer) + dataSent,

View File

@ -0,0 +1,32 @@
#include "ServerInterface2.h"
namespace sls {
int ServerInterface2::sendResult(bool update, int ret, void *retval,
int retvalSize, char *mess) {
// if (update && ret == defs::OK && server_->DifferentClients()) {
// ret = defs::FORCE_UPDATE;
// }
sendData(&ret, sizeof(ret));
if (ret == defs::FAIL) {
// send error message
if (mess)
sendData(mess, MAX_STR_LENGTH);
// debugging feature. should not happen.
else
FILE_LOG(logERROR) << "No error message provided for this "
"failure. Will mess up TCP\n";
}
sendData(retval, retvalSize);
return ret;
}
int ServerInterface2::receiveArg(int &ret, char *mess, void *arg,
int sizeofArg) {
if (sizeofArg && receiveData(arg, sizeofArg) < 0)
return defs::FAIL;
return defs::OK;
}
} // namespace sls

View File

@ -1,4 +1,6 @@
#include "ServerInterface2.h"
#include "ServerSocket.h"
#include "DataSocket.h"
#include "logger.h"
#include "sls_detector_defs.h"
@ -27,7 +29,7 @@ ServerSocket::ServerSocket(int port)
if (bind(getSocketId(), (struct sockaddr *)&serverAddr,
sizeof(serverAddr)) != 0) {
close();
throw std::runtime_error("Server ERROR: cannot bind socket");
throw sls::SocketError("Server ERROR: cannot bind socket");
}
if (listen(getSocketId(), DEFAULT_BACKLOG) != 0) {
close();
@ -35,25 +37,21 @@ ServerSocket::ServerSocket(int port)
}
}
DataSocket ServerSocket::accept() {
ServerInterface2 ServerSocket::accept() {
lastClient = thisClient; //update from previous connection
struct sockaddr_in clientAddr;
socklen_t addr_size = sizeof clientAddr;
int newSocket =
::accept(getSocketId(), (struct sockaddr *)&clientAddr, &addr_size);
if (newSocket == -1) {
throw std::runtime_error("Server ERROR: socket accept failed\n");
throw sls::SocketError("Server ERROR: socket accept failed\n");
}
inet_ntop(AF_INET, &(clientAddr.sin_addr), &thisClient_.front(),
INET_ADDRSTRLEN);
std::cout << "lastClient: " << lastClient_ << " thisClient: " << thisClient_
<< '\n';
// Here goes any check for locks etc
lastClient_ = thisClient_;
return DataSocket(newSocket);
char tc[INET_ADDRSTRLEN]{};
inet_ntop(AF_INET, &(clientAddr.sin_addr), tc, INET_ADDRSTRLEN);
thisClient = tc;
return ServerInterface2(newSocket);
}
const std::string &ServerSocket::getLastClient() { return lastClient_; }
int ServerSocket::getPort() const { return serverPort; }

View File

@ -6,16 +6,20 @@
#include <cstring>
#include <iomanip>
#include <sstream>
#include <sys/prctl.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include "network_utils.h"
namespace sls {
IpAddr::IpAddr(const std::string &address) {
inet_pton(AF_INET, address.c_str(), &addr_);
}
@ -86,4 +90,60 @@ uint32_t HostnameToIp(const char *hostname) {
return ip;
}
std::string IpToInterfaceName(const std::string &ip) {
//TODO! Copied from genericSocket needs to be refactored!
struct ifaddrs *addrs, *iap;
struct sockaddr_in *sa;
char buf[32];
const int buf_len = sizeof(buf);
memset(buf, 0, buf_len);
strcpy(buf, "none");
getifaddrs(&addrs);
for (iap = addrs; iap != NULL; iap = iap->ifa_next) {
if (iap->ifa_addr && (iap->ifa_flags & IFF_UP) &&
iap->ifa_addr->sa_family == AF_INET) {
sa = (struct sockaddr_in *)(iap->ifa_addr);
inet_ntop(iap->ifa_addr->sa_family, (void *)&(sa->sin_addr), buf,
buf_len);
if (ip == std::string(buf)) {
strcpy(buf, iap->ifa_name);
break;
}
}
}
freeifaddrs(addrs);
return std::string(buf);
}
MacAddr InterfaceNameToMac(std::string inf) {
//TODO! Copied from genericSocket needs to be refactored!
struct ifreq ifr;
char mac[32];
const int mac_len = sizeof(mac);
memset(mac,0,mac_len);
int sock=socket(PF_INET, SOCK_STREAM, 0);
strncpy(ifr.ifr_name,inf.c_str(),sizeof(ifr.ifr_name)-1);
ifr.ifr_name[sizeof(ifr.ifr_name)-1]='\0';
if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
perror("ioctl(SIOCGIFHWADDR) ");
return std::string("00:00:00:00:00:00");
}
for (int j=0, k=0; j<6; j++) {
k+=snprintf(mac+k, mac_len-k-1, j ? ":%02X" : "%02X",
(int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
}
mac[mac_len-1]='\0';
if(sock!=1){
close(sock);
}
return MacAddr(mac);
}
} // namespace sls

View File

@ -101,4 +101,4 @@ TEST_CASE("MAC Output operator gives same result as string", "[support]") {
CHECK(os.str() == addr.str());
}
//TODO!(Erik) Look up a real hostname and verify the IP
//TODO!(Erik) Look up a real hostname and verify the IP