From 708341e9171578f5020d0724e1c98b47d1ac3800 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sat, 22 Jan 2022 17:08:45 +0100 Subject: [PATCH] #16 Add lookup V4 --- NetUtils.cpp | 13 +++- NetUtils.h | 3 +- Tests/NetUtils/lookupV4.cpp | 68 +++++++++++++++++++ .../{lookupPreferV6.cpp => lookupV6.cpp} | 28 +++++--- 4 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 Tests/NetUtils/lookupV4.cpp rename Tests/NetUtils/{lookupPreferV6.cpp => lookupV6.cpp} (63%) diff --git a/NetUtils.cpp b/NetUtils.cpp index b3b283d..f4046d8 100644 --- a/NetUtils.cpp +++ b/NetUtils.cpp @@ -22,7 +22,16 @@ #include "NetUtils.h" -bool CNetUtils::lookupPreferV6(const std::string& hostname, sockaddr_storage& addr) +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)); @@ -37,6 +46,8 @@ bool CNetUtils::lookup(const std::string& hostname, sockaddr_storage& addr, stru 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; } diff --git a/NetUtils.h b/NetUtils.h index 31b4678..b3dd779 100644 --- a/NetUtils.h +++ b/NetUtils.h @@ -25,6 +25,7 @@ class CNetUtils { public: - static bool lookupPreferV6(const std::string& hostname, sockaddr_storage& addr); + 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, struct addrinfo& hints); }; \ No newline at end of file diff --git a/Tests/NetUtils/lookupV4.cpp b/Tests/NetUtils/lookupV4.cpp new file mode 100644 index 0000000..5f554fb --- /dev/null +++ b/Tests/NetUtils/lookupV4.cpp @@ -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 + +#include + +#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); + } +} \ No newline at end of file diff --git a/Tests/NetUtils/lookupPreferV6.cpp b/Tests/NetUtils/lookupV6.cpp similarity index 63% rename from Tests/NetUtils/lookupPreferV6.cpp rename to Tests/NetUtils/lookupV6.cpp index 27d874a..1a6c9bf 100644 --- a/Tests/NetUtils/lookupPreferV6.cpp +++ b/Tests/NetUtils/lookupV6.cpp @@ -24,29 +24,39 @@ namespace NetUtilsTests { - class NetUtils_lookupPreferV6 : public ::testing::Test { + class NetUtils_lookupV6 : public ::testing::Test { }; - TEST_F(NetUtils_lookupPreferV6, googleShallAlwaysSucceed) + TEST_F(NetUtils_lookupV6, googleShallAlwaysSucceed) { sockaddr_storage addr; - struct addrinfo hints; - ::memset(&hints, 0, sizeof(hints)); - bool res = CNetUtils::lookup("google.fr", addr, hints); + bool res = CNetUtils::lookupV6("google.fr", addr); EXPECT_EQ(addr.ss_family, AF_INET6); EXPECT_TRUE(res); } - TEST_F(NetUtils_lookupPreferV6, erroneousAddress) + TEST_F(NetUtils_lookupV6, erroneousAddress) { sockaddr_storage addr; - struct addrinfo hints; - ::memset(&hints, 0, sizeof(hints)); - bool res = CNetUtils::lookup("gfilufgclqsegfuligyhfcguyhfguilfguils4df64sdw46fcq6sfgvd6f6d7f67d6f7c6sd7f6s7gfv6fc7d6f76tf.fr", addr, hints); + 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);