Merge branch 'bugfix/SimultaneousG2_#16' into develop closes #16
commit
1e6fee9d4d
@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "Log.h"
|
||||
#include "G2ProtocolHandlerPool.h"
|
||||
|
||||
const unsigned int G2_BUFFER_LENGTH = 255U;
|
||||
|
||||
CG2ProtocolHandlerPool::CG2ProtocolHandlerPool(unsigned short port, const std::string& address) :
|
||||
m_address(address),
|
||||
m_basePort(port),
|
||||
m_socket(address, port)
|
||||
{
|
||||
assert(port > 0U);
|
||||
m_index = m_pool.end();
|
||||
}
|
||||
|
||||
CG2ProtocolHandlerPool::~CG2ProtocolHandlerPool()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool CG2ProtocolHandlerPool::open()
|
||||
{
|
||||
bool res = m_socket.open();
|
||||
return res;
|
||||
}
|
||||
|
||||
void CG2ProtocolHandlerPool::close()
|
||||
{
|
||||
for(auto handler : m_pool) {
|
||||
delete handler;
|
||||
}
|
||||
m_pool.clear();
|
||||
m_index = m_pool.end();
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
G2_TYPE CG2ProtocolHandlerPool::read()
|
||||
{
|
||||
bool res = true;
|
||||
while(res)
|
||||
res = readPackets();
|
||||
|
||||
if(m_index == m_pool.end())
|
||||
m_index = m_pool.begin();
|
||||
|
||||
while(m_index != m_pool.end()) {
|
||||
if((*m_index)->getType() != GT_NONE) {
|
||||
return (*m_index)->getType();
|
||||
}
|
||||
m_index++;
|
||||
}
|
||||
|
||||
return GT_NONE;
|
||||
}
|
||||
|
||||
CAMBEData * CG2ProtocolHandlerPool::readAMBE()
|
||||
{
|
||||
if(m_index == m_pool.end() || (*m_index)->getType() != GT_AMBE)
|
||||
return nullptr;
|
||||
|
||||
return (*m_index)->readAMBE();
|
||||
}
|
||||
|
||||
CHeaderData * CG2ProtocolHandlerPool::readHeader()
|
||||
{
|
||||
if(m_index == m_pool.end() || (*m_index)->getType() != GT_HEADER)
|
||||
return nullptr;
|
||||
|
||||
return (*m_index)->readHeader();
|
||||
}
|
||||
|
||||
bool CG2ProtocolHandlerPool::readPackets()
|
||||
{
|
||||
unsigned char buffer[G2_BUFFER_LENGTH];
|
||||
struct sockaddr_storage addr;
|
||||
::memset(&addr, 0, sizeof(sockaddr_storage));
|
||||
|
||||
// No more data?
|
||||
int length = m_socket.read(buffer, G2_BUFFER_LENGTH, addr);
|
||||
if(length <= 0) return false;
|
||||
|
||||
if(length == 1 && buffer[0] == 0U) {
|
||||
CLog::logDebug("G2 Nat traversal packet received");
|
||||
}
|
||||
|
||||
CG2ProtocolHandler * handler = findHandler(addr, IMT_ADDRESS_AND_PORT);
|
||||
if(handler == nullptr) {
|
||||
CLog::logTrace("new incoming G2 %s:%u", inet_ntoa(TOIPV4(addr)->sin_addr), ntohs(TOIPV4(addr)->sin_port));
|
||||
handler = new CG2ProtocolHandler(&m_socket, addr, G2_BUFFER_LENGTH);
|
||||
m_pool.push_back(handler);
|
||||
m_index = m_pool.end();
|
||||
}
|
||||
|
||||
bool res = handler->setBuffer(buffer, length);
|
||||
return res;
|
||||
}
|
||||
|
||||
void CG2ProtocolHandlerPool::traverseNat(const std::string& address)
|
||||
{
|
||||
unsigned char buffer = 0x00U;
|
||||
|
||||
in_addr addr = CUDPReaderWriter::lookup(address);
|
||||
|
||||
CLog::logInfo("G2 Punching hole to %s", address.c_str());
|
||||
|
||||
m_socket.write(&buffer, 1U, addr, G2_DV_PORT);
|
||||
}
|
||||
|
||||
bool CG2ProtocolHandlerPool::writeHeader(const CHeaderData& header)
|
||||
{
|
||||
auto handler = findHandler(header.getDestination(), IMT_ADDRESS_AND_PORT);
|
||||
if(handler == nullptr)
|
||||
handler = findHandler(header.getDestination(), IMT_ADDRESS_ONLY);
|
||||
|
||||
if(handler == nullptr) {
|
||||
handler = new CG2ProtocolHandler(&m_socket, header.getDestination(), G2_BUFFER_LENGTH);
|
||||
m_pool.push_back(handler);
|
||||
m_index = m_pool.end();
|
||||
}
|
||||
return handler->writeHeader(header);
|
||||
}
|
||||
|
||||
bool CG2ProtocolHandlerPool::writeAMBE(const CAMBEData& data)
|
||||
{
|
||||
auto handler = findHandler(data.getDestination(), IMT_ADDRESS_AND_PORT);
|
||||
if(handler == nullptr)
|
||||
handler = findHandler(data.getDestination(), IMT_ADDRESS_ONLY);
|
||||
|
||||
if(handler == nullptr) {
|
||||
handler = new CG2ProtocolHandler(&m_socket, data.getDestination(), G2_BUFFER_LENGTH);
|
||||
m_pool.push_back(handler);
|
||||
m_index = m_pool.end();
|
||||
}
|
||||
|
||||
return handler->writeAMBE(data);
|
||||
}
|
||||
|
||||
CG2ProtocolHandler * CG2ProtocolHandlerPool::findHandler(const struct sockaddr_storage& addr, IPMATCHTYPE matchType) const
|
||||
{
|
||||
for(auto handler : m_pool) {
|
||||
if(handler != nullptr && CNetUtils::match(addr, handler->getDestination(), matchType))
|
||||
return handler;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CG2ProtocolHandler * CG2ProtocolHandlerPool::findHandler(in_addr addr, unsigned int port, IPMATCHTYPE matchType) const
|
||||
{
|
||||
struct sockaddr_storage addrStorage;
|
||||
addrStorage.ss_family = AF_INET;
|
||||
TOIPV4(addrStorage)->sin_addr = addr;
|
||||
TOIPV4(addrStorage)->sin_port = port;
|
||||
|
||||
return findHandler(addrStorage, matchType);
|
||||
}
|
||||
|
||||
void CG2ProtocolHandlerPool::clock(unsigned int ms)
|
||||
{
|
||||
for(auto it = m_pool.begin(); it != m_pool.end();) {
|
||||
(*it)->clock(ms);
|
||||
if((*it)->isInactive()) {
|
||||
delete (*it);
|
||||
it = m_pool.erase(it);
|
||||
m_index = m_pool.end();
|
||||
}
|
||||
else {
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sys/socket.h>
|
||||
#include <boost/container_hash/hash.hpp>
|
||||
|
||||
#include "G2ProtocolHandler.h"
|
||||
#include "NetUtils.h"
|
||||
|
||||
struct sockaddr_storage_map {
|
||||
struct compAddrAndPort {
|
||||
bool operator() (const struct sockaddr_storage& a, const struct sockaddr_storage& b) const {
|
||||
return CNetUtils::match(a, b, IMT_ADDRESS_AND_PORT);
|
||||
}
|
||||
};
|
||||
struct hash {
|
||||
std::size_t operator() (const sockaddr_storage& a) const {
|
||||
switch(a.ss_family)
|
||||
{
|
||||
case AF_INET: {
|
||||
auto ptr4 = ((struct sockaddr_in *)&a);
|
||||
size_t res = AF_INET;
|
||||
boost::hash_combine(res, ptr4->sin_port);
|
||||
boost::hash_combine(res, ptr4->sin_addr.s_addr);
|
||||
return res;
|
||||
}
|
||||
case AF_INET6: {
|
||||
auto ptr6 = ((struct sockaddr_in6 *)&a);
|
||||
size_t res = AF_INET6;
|
||||
boost::hash_combine(res, ptr6->sin6_port);
|
||||
auto in6Ptr = (unsigned int *)&(ptr6->sin6_addr);
|
||||
boost::hash_combine(res, in6Ptr[0]);
|
||||
boost::hash_combine(res, in6Ptr[1]);
|
||||
boost::hash_combine(res, in6Ptr[2]);
|
||||
boost::hash_combine(res, in6Ptr[3]);
|
||||
return res;
|
||||
}
|
||||
default:
|
||||
return 0U;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
class CG2ProtocolHandlerPool
|
||||
{
|
||||
public:
|
||||
CG2ProtocolHandlerPool(unsigned short g2Port, const std::string& address = "");
|
||||
~CG2ProtocolHandlerPool();
|
||||
|
||||
bool open();
|
||||
void close();
|
||||
G2_TYPE read();
|
||||
CAMBEData * readAMBE();
|
||||
CHeaderData * readHeader();
|
||||
|
||||
bool writeAMBE(const CAMBEData& data);
|
||||
bool writeHeader(const CHeaderData& header);
|
||||
|
||||
void traverseNat(const std::string& address);
|
||||
|
||||
void clock(unsigned int ms);
|
||||
|
||||
private:
|
||||
bool readPackets();
|
||||
CG2ProtocolHandler * findHandler(const struct sockaddr_storage& addr, IPMATCHTYPE matchType) const;
|
||||
CG2ProtocolHandler * findHandler(in_addr addr, unsigned int port, IPMATCHTYPE matchType) const;
|
||||
|
||||
std::string m_address;
|
||||
unsigned int m_basePort;
|
||||
CUDPReaderWriter m_socket;
|
||||
std::vector<CG2ProtocolHandler *> m_pool;
|
||||
std::vector<CG2ProtocolHandler *>::iterator m_index;
|
||||
};
|
||||
@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright (c) 2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
* Copyright (C) 2009-2011,2013,2015,2016,2020 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <cstring>
|
||||
|
||||
#include "NetUtils.h"
|
||||
|
||||
bool CNetUtils::lookupV4(const std::string& hostname, sockaddr_storage& addr)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
::memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
|
||||
return lookup(hostname, addr, hints);
|
||||
}
|
||||
|
||||
bool CNetUtils::lookupV6(const std::string& hostname, sockaddr_storage& addr)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
::memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET6;
|
||||
|
||||
return lookup(hostname, addr, hints);
|
||||
}
|
||||
|
||||
bool CNetUtils::lookup(const std::string& hostname, sockaddr_storage& addr)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
::memset(&hints, 0, sizeof(hints));
|
||||
return lookup(hostname, addr, hints);
|
||||
}
|
||||
|
||||
bool CNetUtils::lookup(const std::string& hostname, sockaddr_storage& addr, struct addrinfo& hints)
|
||||
{
|
||||
struct addrinfo *res;
|
||||
|
||||
int err = getaddrinfo(hostname.c_str(), nullptr, &hints, &res);
|
||||
if(err != 0) {
|
||||
::memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
lookup("255.255.255.255", addr, hints);
|
||||
return false;
|
||||
}
|
||||
|
||||
::memcpy(&addr, res->ai_addr, res->ai_addrlen);
|
||||
|
||||
::freeaddrinfo(res);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CNetUtils::match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type)
|
||||
{
|
||||
if (addr1.ss_family != addr2.ss_family)
|
||||
return false;
|
||||
|
||||
if (type == IMT_ADDRESS_AND_PORT) {
|
||||
switch (addr1.ss_family) {
|
||||
case AF_INET:
|
||||
struct sockaddr_in *in_1, *in_2;
|
||||
in_1 = (struct sockaddr_in*)&addr1;
|
||||
in_2 = (struct sockaddr_in*)&addr2;
|
||||
return (in_1->sin_addr.s_addr == in_2->sin_addr.s_addr) && (in_1->sin_port == in_2->sin_port);
|
||||
case AF_INET6:
|
||||
struct sockaddr_in6 *in6_1, *in6_2;
|
||||
in6_1 = (struct sockaddr_in6*)&addr1;
|
||||
in6_2 = (struct sockaddr_in6*)&addr2;
|
||||
return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr) && (in6_1->sin6_port == in6_2->sin6_port);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
} else if (type == IMT_ADDRESS_ONLY) {
|
||||
switch (addr1.ss_family) {
|
||||
case AF_INET:
|
||||
struct sockaddr_in *in_1, *in_2;
|
||||
in_1 = (struct sockaddr_in*)&addr1;
|
||||
in_2 = (struct sockaddr_in*)&addr2;
|
||||
return in_1->sin_addr.s_addr == in_2->sin_addr.s_addr;
|
||||
case AF_INET6:
|
||||
struct sockaddr_in6 *in6_1, *in6_2;
|
||||
in6_1 = (struct sockaddr_in6*)&addr1;
|
||||
in6_2 = (struct sockaddr_in6*)&addr2;
|
||||
return IN6_ARE_ADDR_EQUAL(&in6_1->sin6_addr, &in6_2->sin6_addr);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void CNetUtils::setPort(struct sockaddr_storage& addr, in_port_t port)
|
||||
{
|
||||
switch (addr.ss_family)
|
||||
{
|
||||
case AF_INET:
|
||||
TOIPV4(addr)->sin_port = port;
|
||||
break;
|
||||
case AF_INET6:
|
||||
TOIPV6(addr)->sin6_port = port;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
* Copyright (C) 2009-2011,2013,2015,2016,2020 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#define TOIPV6(s) ((struct sockaddr_in6*)&s)
|
||||
#define TOIPV4(s) (((struct sockaddr_in*)&s))
|
||||
#define GETPORT(s) (s.ss_family == AF_INET6 ? TOIPV6(s)->sin6_port : TOIPV4(s)->sin_port)
|
||||
#define SETPORT(s, p) (if(s.ss_family == AF_INET6)TOIPV6(s)->sin6_port = p;else TOIPV4(s)->sin_port = p;)
|
||||
|
||||
enum IPMATCHTYPE {
|
||||
IMT_ADDRESS_AND_PORT,
|
||||
IMT_ADDRESS_ONLY
|
||||
};
|
||||
|
||||
class CNetUtils
|
||||
{
|
||||
public:
|
||||
static bool lookupV6(const std::string& hostname, sockaddr_storage& addr);
|
||||
static bool lookupV4(const std::string& hostname, sockaddr_storage& addr);
|
||||
static bool lookup(const std::string& hostname, sockaddr_storage& addr);
|
||||
static bool lookup(const std::string& hostname, sockaddr_storage& addr, struct addrinfo& hints);
|
||||
static bool match(const sockaddr_storage& addr1, const sockaddr_storage& addr2, IPMATCHTYPE type);
|
||||
static void setPort(struct sockaddr_storage& addr, in_port_t port);
|
||||
};
|
||||
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <netdb.h>
|
||||
|
||||
#include "../../NetUtils.h"
|
||||
|
||||
namespace NetUtilsTests
|
||||
{
|
||||
class NetUtils_lookup: public ::testing::Test {
|
||||
|
||||
};
|
||||
|
||||
TEST_F(NetUtils_lookup, googleShallAlwaysSucceed)
|
||||
{
|
||||
sockaddr_storage addr;
|
||||
struct addrinfo hints;
|
||||
::memset(&hints, 0, sizeof(hints));
|
||||
|
||||
bool res = CNetUtils::lookup("google.fr", addr, hints);
|
||||
|
||||
bool familyOk = addr.ss_family == AF_INET6 || addr.ss_family == AF_INET;
|
||||
EXPECT_TRUE(familyOk);
|
||||
EXPECT_TRUE(res);
|
||||
}
|
||||
|
||||
TEST_F(NetUtils_lookup, erroneousAddress)
|
||||
{
|
||||
sockaddr_storage addr;
|
||||
struct addrinfo hints;
|
||||
::memset(&hints, 0, sizeof(hints));
|
||||
|
||||
bool res = CNetUtils::lookup("gfilufgclqsegfuligyhfcguyhfguilfguils4df64sdw46fcq6sfgvd6f6d7f67d6f7c6sd7f6s7gfv6fc7d6f76tf.fr", addr, hints);
|
||||
|
||||
EXPECT_EQ(addr.ss_family, AF_INET);
|
||||
|
||||
auto ptr = (sockaddr_in*)(&addr);
|
||||
|
||||
EXPECT_EQ((uint32_t)(ptr->sin_addr.s_addr), (uint32_t)INADDR_NONE);
|
||||
EXPECT_FALSE(res);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <netdb.h>
|
||||
|
||||
#include "../../NetUtils.h"
|
||||
|
||||
namespace NetUtilsTests
|
||||
{
|
||||
class NetUtils_lookupV4 : public ::testing::Test {
|
||||
|
||||
};
|
||||
|
||||
TEST_F(NetUtils_lookupV4, googleShallAlwaysSucceed)
|
||||
{
|
||||
sockaddr_storage addr;
|
||||
|
||||
bool res = CNetUtils::lookupV4("google.fr", addr);
|
||||
|
||||
EXPECT_EQ(addr.ss_family, AF_INET);
|
||||
EXPECT_TRUE(res);
|
||||
}
|
||||
|
||||
TEST_F(NetUtils_lookupV4, erroneousAddress)
|
||||
{
|
||||
sockaddr_storage addr;
|
||||
|
||||
bool res = CNetUtils::lookupV4("gfilufgclqsegfuligyhfcguyhfguilfguils4df64sdw46fcq6sfgvd6f6d7f67d6f7c6sd7f6s7gfv6fc7d6f76tf.fr", addr);
|
||||
|
||||
EXPECT_EQ(addr.ss_family, AF_INET);
|
||||
|
||||
auto ptr = (sockaddr_in*)(&addr);
|
||||
|
||||
EXPECT_EQ((uint32_t)(ptr->sin_addr.s_addr), (uint32_t)INADDR_NONE);
|
||||
EXPECT_FALSE(res);
|
||||
}
|
||||
|
||||
TEST_F(NetUtils_lookupV4, addressWithNoIPV4)
|
||||
{
|
||||
sockaddr_storage addr;
|
||||
|
||||
bool res = CNetUtils::lookupV4("ircv6.openquad.net", addr);
|
||||
|
||||
EXPECT_EQ(addr.ss_family, AF_INET);
|
||||
|
||||
auto ptr = (sockaddr_in*)(&addr);
|
||||
|
||||
EXPECT_EQ((uint32_t)(ptr->sin_addr.s_addr), (uint32_t)INADDR_NONE);
|
||||
EXPECT_FALSE(res);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <netdb.h>
|
||||
|
||||
#include "../../NetUtils.h"
|
||||
|
||||
namespace NetUtilsTests
|
||||
{
|
||||
class NetUtils_lookupV6 : public ::testing::Test {
|
||||
|
||||
};
|
||||
|
||||
TEST_F(NetUtils_lookupV6, googleShallAlwaysSucceed)
|
||||
{
|
||||
sockaddr_storage addr;
|
||||
|
||||
bool res = CNetUtils::lookupV6("google.fr", addr);
|
||||
|
||||
EXPECT_EQ(addr.ss_family, AF_INET6);
|
||||
EXPECT_TRUE(res);
|
||||
}
|
||||
|
||||
TEST_F(NetUtils_lookupV6, erroneousAddress)
|
||||
{
|
||||
sockaddr_storage addr;
|
||||
|
||||
bool res = CNetUtils::lookupV6("gfilufgclqsegfuligyhfcguyhfguilfguils4df64sdw46fcq6sfgvd6f6d7f67d6f7c6sd7f6s7gfv6fc7d6f76tf.fr", addr);
|
||||
|
||||
EXPECT_EQ(addr.ss_family, AF_INET);
|
||||
|
||||
auto ptr = (sockaddr_in*)(&addr);
|
||||
|
||||
EXPECT_EQ((uint32_t)(ptr->sin_addr.s_addr), (uint32_t)INADDR_NONE);
|
||||
EXPECT_FALSE(res);
|
||||
}
|
||||
|
||||
TEST_F(NetUtils_lookupV6, addressWithNoIPV6)
|
||||
{
|
||||
sockaddr_storage addr;
|
||||
|
||||
bool res = CNetUtils::lookupV6("ircv4.openquad.net", addr);
|
||||
|
||||
EXPECT_EQ(addr.ss_family, AF_INET);
|
||||
|
||||
auto ptr = (sockaddr_in*)(&addr);
|
||||
|
||||
EXPECT_EQ((uint32_t)(ptr->sin_addr.s_addr), (uint32_t)INADDR_NONE);
|
||||
EXPECT_FALSE(res);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <netdb.h>
|
||||
|
||||
#include "../../NetUtils.h"
|
||||
|
||||
namespace NetUtilsTests
|
||||
{
|
||||
class NetUtils_match: public ::testing::Test {
|
||||
|
||||
};
|
||||
|
||||
TEST_F(NetUtils_match, MatchIPAndPort_differentFamilySamePort)
|
||||
{
|
||||
struct sockaddr_storage addr1, addr2;
|
||||
addr1.ss_family = AF_INET6;
|
||||
addr2.ss_family = AF_INET;
|
||||
|
||||
((struct sockaddr_in6 *)&addr1)->sin6_addr = IN6ADDR_LOOPBACK_INIT;
|
||||
((struct sockaddr_in6 *)&addr1)->sin6_port = 123;
|
||||
|
||||
((struct sockaddr_in *)&addr2)->sin_addr.s_addr = INADDR_LOOPBACK;
|
||||
((struct sockaddr_in *)&addr2)->sin_port = 123;
|
||||
|
||||
EXPECT_FALSE(CNetUtils::match(addr1, addr2, IMT_ADDRESS_AND_PORT));
|
||||
}
|
||||
|
||||
TEST_F(NetUtils_match, MatchIPAndPort_SameFamilySamePort)
|
||||
{
|
||||
struct sockaddr_storage addr1, addr2;
|
||||
addr1.ss_family = AF_INET6;
|
||||
addr2.ss_family = AF_INET6;
|
||||
|
||||
((struct sockaddr_in6 *)&addr1)->sin6_addr = IN6ADDR_LOOPBACK_INIT;
|
||||
((struct sockaddr_in6 *)&addr1)->sin6_port = 123;
|
||||
|
||||
((struct sockaddr_in6 *)&addr2)->sin6_addr = IN6ADDR_LOOPBACK_INIT;
|
||||
((struct sockaddr_in6 *)&addr2)->sin6_port = 123;
|
||||
|
||||
EXPECT_TRUE(CNetUtils::match(addr1, addr2, IMT_ADDRESS_AND_PORT));
|
||||
}
|
||||
|
||||
TEST_F(NetUtils_match, MatchIPAndPort_SameFamilyDifferentPort)
|
||||
{
|
||||
struct sockaddr_storage addr1, addr2;
|
||||
addr1.ss_family = AF_INET6;
|
||||
addr2.ss_family = AF_INET6;
|
||||
|
||||
((struct sockaddr_in6 *)&addr1)->sin6_addr = IN6ADDR_LOOPBACK_INIT;
|
||||
((struct sockaddr_in6 *)&addr1)->sin6_port = 123;
|
||||
|
||||
((struct sockaddr_in6 *)&addr2)->sin6_addr = IN6ADDR_LOOPBACK_INIT;
|
||||
((struct sockaddr_in6 *)&addr2)->sin6_port = 456;
|
||||
|
||||
EXPECT_FALSE(CNetUtils::match(addr1, addr2, IMT_ADDRESS_AND_PORT));
|
||||
}
|
||||
|
||||
TEST_F(NetUtils_match, MatchIP_SameFamilyDifferentPort)
|
||||
{
|
||||
struct sockaddr_storage addr1, addr2;
|
||||
addr1.ss_family = AF_INET6;
|
||||
addr2.ss_family = AF_INET6;
|
||||
|
||||
((struct sockaddr_in6 *)&addr1)->sin6_addr = IN6ADDR_LOOPBACK_INIT;
|
||||
((struct sockaddr_in6 *)&addr1)->sin6_port = 123;
|
||||
|
||||
((struct sockaddr_in6 *)&addr2)->sin6_addr = IN6ADDR_LOOPBACK_INIT;
|
||||
((struct sockaddr_in6 *)&addr2)->sin6_port = 456;
|
||||
|
||||
EXPECT_TRUE(CNetUtils::match(addr1, addr2, IMT_ADDRESS_ONLY));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue