This commit is contained in:
Erik Frojdh 2019-05-16 14:27:16 +02:00
parent 1db1b0307c
commit 615d1b1f33
3 changed files with 71 additions and 8 deletions

View File

@ -6,13 +6,13 @@
#include "FixedCapacityContainer.h"
#include "ServerSocket.h"
#include "ServerInterface.h"
#include "slsReceiver.h"
#include "slsReceiverImplementation.h"
#include "slsReceiverTCPIPInterface.h"
#include "slsReceiverUsers.h"
#include "versionAPI.h"
#include "string_utils.h"
#include "sls_detector_exceptions.h"
#include <array>
#include <cstdlib>
@ -24,6 +24,8 @@
#include <syscall.h>
#include <vector>
using sls::SocketError;
slsReceiverTCPIPInterface::~slsReceiverTCPIPInterface() {
stop();
}
@ -725,7 +727,7 @@ int slsReceiverTCPIPInterface::setup_udp(sls::ServerInterface2 &socket){
}
FILE_LOG(logINFO) << "Receiver UDP IP: " << ip1;
// get eth
std::string temp = genericSocket::ipToName(ip1);
std::string temp = sls::IpToInterfaceName(ip1);
if (temp == "none"){
ret = FAIL;
strcpy(mess, "Failed to get ethernet interface or IP \n");
@ -747,7 +749,7 @@ int slsReceiverTCPIPInterface::setup_udp(sls::ServerInterface2 &socket){
}
//get mac address
if (ret != FAIL) {
temp = genericSocket::nameToMac(eth);
temp = sls::InterfaceNameToMac(eth).str();
if (temp=="00:00:00:00:00:00") {
ret = FAIL;
strcpy(mess,"failed to get mac adddress to listen to\n");
@ -771,7 +773,7 @@ int slsReceiverTCPIPInterface::setup_udp(sls::ServerInterface2 &socket){
receiver->setUDPPortNumber2(port2);
FILE_LOG(logINFO) << "Receiver UDP IP 2: " << ip2;
// get eth
temp = genericSocket::ipToName(ip2);
temp = sls::IpToInterfaceName(ip2);
if (temp == "none"){
ret = FAIL;
strcpy(mess, "Failed to get 2nd ethernet interface or IP \n");
@ -791,7 +793,7 @@ int slsReceiverTCPIPInterface::setup_udp(sls::ServerInterface2 &socket){
//get mac address
if (ret != FAIL) {
temp = genericSocket::nameToMac(eth);
temp = sls::InterfaceNameToMac(eth).str();
if (temp=="00:00:00:00:00:00") {
ret = FAIL;
strcpy(mess,"failed to get 2nd mac adddress to listen to\n");

View File

@ -4,8 +4,6 @@
namespace sls {
uint32_t HostnameToIp(const char *hostname);
class IpAddr {
private:
uint32_t addr_{0};
@ -58,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

@ -6,11 +6,14 @@
#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"
@ -93,4 +96,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