replaced socket

This commit is contained in:
Erik Frojdh
2020-02-05 19:04:25 +01:00
parent c3bbe45b68
commit 89175586b4
9 changed files with 277 additions and 106 deletions

View File

@ -0,0 +1,149 @@
/*
UdpRxSocket provies socket control to receive
data on a udp socket.
It provides a drop in replacement for
genericSocket. But please be careful since
this might be deprecated in the future.
*/
#include "genericSocket.h"
#include "network_utils.h"
#include "sls_detector_exceptions.h"
#include <cstdint>
#include <errno.h>
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <vector>
namespace sls {
class UdpRxSocket {
int port;
const ssize_t packet_size;
ssize_t buffer_size;
char *buff;
int fd = -1;
// If possible we could listen to only one source but for our setup
// we should have only our data on this port network???
// recvfrom(fd, buff, packet_size, 0, (struct sockaddr *)&src_addr,
// &src_addr_len);
// struct sockaddr_storage src_addr;
// socklen_t src_addr_len = sizeof(src_addr);
public:
UdpRxSocket(int port, ssize_t packet_size, const char *hostname = nullptr,
ssize_t buffer_size = 0)
: port(port), packet_size(packet_size), buffer_size(buffer_size) {
/* hostname = nullptr -> wildcard */
const std::string portname = std::to_string(port);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
struct addrinfo *res = 0;
if (getaddrinfo(hostname, portname.c_str(), &hints, &res)) {
throw RuntimeError("Failed getaddinfo");
}
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (fd == -1) {
throw RuntimeError("Failed creating socket");
}
if (bind(fd, res->ai_addr, res->ai_addrlen) == -1) {
throw RuntimeError("Failed to bind socket");
}
freeaddrinfo(res);
// If we get a specified buffer size that is larger than the set one
// we set it. Otherwise we leave it there since it could have been
// set by the rx_udpsocksize command
if (buffer_size) {
auto current = getBufferSize() / 2;
if (current < buffer_size) {
setBufferSize(buffer_size);
if (getBufferSize() / 2 < buffer_size) {
FILE_LOG(logWARNING) << "Could not set buffer size. Got: " << getBufferSize()/2 << " instead of " << buffer_size;
}
}
}
// Allocate at the end to avoid memory leak if we throw
buff = new char[packet_size];
}
// Delegating constructor to allow drop in replacement for old socket class
// This one might be removed in the future
UdpRxSocket(unsigned short int const port_number,
genericSocket::communicationProtocol p,
int ps = DEFAULT_PACKET_SIZE, const char *eth = NULL,
int hsize = 0, uint64_t buf_size = SOCKET_BUFFER_SIZE)
: UdpRxSocket(port_number, ps, InterfaceNameToIp(eth).str().c_str(),
buf_size) {}
const char *LastPacket() const { return buff; }
~UdpRxSocket() {
delete[] buff;
Shutdown();
}
// Receive one packet to the internal buffer of the socket class, preferred
// method?
bool ReceivePacket() {
ssize_t count = recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
return count == packet_size;
}
// Not sure we keep this
bool ReceivePacket(char *dst) {
ssize_t count = recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
return count == packet_size;
}
// Only for backwards compatibility will be removed
ssize_t ReceiveDataOnly(char *dst) {
return recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
}
ssize_t getBufferSize() const {
uint64_t ret_size = 0;
socklen_t optlen = sizeof(uint64_t);
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &ret_size, &optlen) == -1)
return -1;
else
return ret_size;
}
// Only for backwards compatibility will be removed
ssize_t getActualUDPSocketBufferSize() const { return getBufferSize(); }
// Only for backwards compatibility will be removed
void ShutDownSocket() { Shutdown(); }
void setBufferSize(ssize_t size) {
socklen_t optlen = sizeof(size);
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, optlen)) {
throw RuntimeError("Could not set socket buffer size");
}
}
void Shutdown() {
shutdown(fd, SHUT_RDWR);
if (fd >= 0) {
close(fd);
fd = -1;
}
}
};
} // namespace sls

View File

@ -1,56 +0,0 @@
#include <cstdint>
#include <errno.h>
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "sls_detector_exceptions.h"
namespace sls {
class UdpSocket {
int port;
size_t packet_size;
size_t buffer_size;
int fd = -1;
public:
UdpSocket(int port, size_t packet_size)
: port(port), packet_size(packet_size) {
const char *hostname = 0; /* wildcard */
const std::string portname = std::to_string(port);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
struct addrinfo *res = 0;
if (getaddrinfo(hostname, portname.c_str(), &hints, &res)){
throw RuntimeError("Failed getaddinfo");
}
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(fd==-1){
throw RuntimeError("Failed creating socket");
}
if (bind(fd, res->ai_addr, res->ai_addrlen) == -1) {
throw RuntimeError("Failed to bind socket");
}
freeaddrinfo(res);
}
int ReceivePacket() {
char buffer[549];
struct sockaddr_storage src_addr;
socklen_t src_addr_len = sizeof(src_addr);
ssize_t count = recvfrom(fd, buffer, sizeof(buffer), 0,
(struct sockaddr *)&src_addr, &src_addr_len);
return count;
}
};
} // namespace sls

View File

@ -62,7 +62,7 @@ class MacAddr {
IpAddr HostnameToIp(const char *hostname);
std::string IpToInterfaceName(const std::string& ip);
MacAddr InterfaceNameToMac(const std::string& inf);
IpAddr InterfaceNameToIp(const std::string& ifn);
std::ostream &operator<<(std::ostream &out, const IpAddr &addr);
std::ostream &operator<<(std::ostream &out, const MacAddr &addr);

View File

@ -1,36 +1,33 @@
#include "sls_detector_exceptions.h"
#include <algorithm>
#include <arpa/inet.h>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <ifaddrs.h>
#include <iomanip>
#include <sstream>
#include <sys/prctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netdb.h>
#include <sstream>
#include <sys/ioctl.h>
#include <sys/prctl.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_);
}
IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); }
std::string IpAddr::str() const {
return arr().data();
}
std::string IpAddr::str() const { return arr().data(); }
std::array<char, INET_ADDRSTRLEN> IpAddr::arr() const{
std::array<char, INET_ADDRSTRLEN> IpAddr::arr() const {
std::array<char, INET_ADDRSTRLEN> ipstring{};
inet_ntop(AF_INET, &addr_, ipstring.data(), INET_ADDRSTRLEN);
return ipstring;
@ -96,7 +93,7 @@ IpAddr HostnameToIp(const char *hostname) {
}
std::string IpToInterfaceName(const std::string &ip) {
//TODO! Copied from genericSocket needs to be refactored!
// TODO! Copied from genericSocket needs to be refactored!
struct ifaddrs *addrs, *iap;
struct sockaddr_in *sa;
@ -122,33 +119,58 @@ std::string IpToInterfaceName(const std::string &ip) {
return std::string(buf);
}
MacAddr InterfaceNameToMac(const 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);
IpAddr InterfaceNameToIp(const std::string &ifn) {
struct ifaddrs *ifaddr, *ifa;
int family, s;
char host[NI_MAXHOST];
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 (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
perror("ioctl(SIOCGIFHWADDR) ");
return MacAddr{};
}
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';
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if(sock!=1){
close(sock);
}
return MacAddr(mac);
if ((strcmp(ifa->ifa_name, ifn.c_str()) == 0) &&
(ifa->ifa_addr->sa_family == AF_INET)) {
}
}
}
freeifaddrs(ifaddr);
return IpAddr{host};
}
MacAddr InterfaceNameToMac(const 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 MacAddr{};
}
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