diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 168ca95..ac3f100 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -12,10 +12,7 @@ "ENABLE_DEBUG=1", "USE_GPSD=1" ], - "group": { - "kind": "build", - "isDefault": true - }, + "group": "build", "problemMatcher": [] }, { @@ -28,7 +25,10 @@ "ENABLE_DEBUG=1", "USE_GPSD=1" ], - "group": "build", + "group": { + "kind": "build", + "isDefault": true + }, "problemMatcher": [] } ] diff --git a/NetUtils.cpp b/NetUtils.cpp new file mode 100644 index 0000000..b3b283d --- /dev/null +++ b/NetUtils.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 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" + +bool CNetUtils::lookupPreferV6(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) +{ + struct addrinfo *res; + + int err = getaddrinfo(hostname.c_str(), nullptr, &hints, &res); + if(err != 0) { + lookup("255.255.255.255", addr, hints); + return false; + } + + ::memcpy(&addr, res->ai_addr, res->ai_addrlen); + + ::freeaddrinfo(res); + + return true; +} \ No newline at end of file diff --git a/NetUtils.h b/NetUtils.h new file mode 100644 index 0000000..31b4678 --- /dev/null +++ b/NetUtils.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 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 +#include +#include + +class CNetUtils +{ +public: + static bool lookupPreferV6(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/lookup.cpp b/Tests/NetUtils/lookup.cpp new file mode 100644 index 0000000..f55f23c --- /dev/null +++ b/Tests/NetUtils/lookup.cpp @@ -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 + +#include + +#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); + } +} \ No newline at end of file diff --git a/Tests/NetUtils/lookupPreferV6.cpp b/Tests/NetUtils/lookupPreferV6.cpp new file mode 100644 index 0000000..27d874a --- /dev/null +++ b/Tests/NetUtils/lookupPreferV6.cpp @@ -0,0 +1,58 @@ +/* + * 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_lookupPreferV6 : public ::testing::Test { + + }; + + TEST_F(NetUtils_lookupPreferV6, googleShallAlwaysSucceed) + { + sockaddr_storage addr; + struct addrinfo hints; + ::memset(&hints, 0, sizeof(hints)); + + bool res = CNetUtils::lookup("google.fr", addr, hints); + + EXPECT_EQ(addr.ss_family, AF_INET6); + EXPECT_TRUE(res); + } + + TEST_F(NetUtils_lookupPreferV6, 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); + } +} \ No newline at end of file