-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @file | ||
* @author Nicholas Logozzo <[email protected]> | ||
* | ||
* @section LICENSE | ||
* | ||
* 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 3 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 at | ||
* https://www.gnu.org/copyleft/gpl.html | ||
* | ||
* @section DESCRIPTION | ||
* | ||
* Functions for working with DNS. | ||
*/ | ||
|
||
#ifndef DNS_H | ||
#define DNS_H | ||
|
||
#include <string> | ||
#include <vector> | ||
#include "ipv4address.h" | ||
|
||
namespace Nickvision::Network::DNS | ||
{ | ||
/** | ||
* @brief Resolves a hostname to IPv4 addresses. | ||
* @param hostname The hostname to resolve | ||
* @return A list of IPv4 addresses that the hostname resolves to | ||
*/ | ||
std::vector<IPv4Address> resolve(const std::string& hostname); | ||
} | ||
|
||
#endif //DNS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
/** | ||
* @file | ||
* @author Nicholas Logozzo <[email protected]> | ||
* | ||
* @section LICENSE | ||
* | ||
* 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 3 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 at | ||
* https://www.gnu.org/copyleft/gpl.html | ||
* | ||
* @section DESCRIPTION | ||
* | ||
* A model of an IPv4 address. | ||
*/ | ||
|
||
#ifndef IPV4ADDRESS_H | ||
#define IPV4ADDRESS_H | ||
|
||
|
@@ -20,6 +42,11 @@ namespace Nickvision::Network | |
* @param fourth The fourth byte of the address | ||
*/ | ||
IPv4Address(unsigned char first, unsigned char second, unsigned char third, unsigned char fourth); | ||
/** | ||
* @brief Constructs an IPv4Address. | ||
* @param address The address as a long | ||
*/ | ||
IPv4Address(unsigned long address); | ||
/** | ||
* @brief Gets the first byte of the address. | ||
* @return The first byte of the address | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
/** | ||
* @file | ||
* @author Nicholas Logozzo <[email protected]> | ||
* | ||
* @section LICENSE | ||
* | ||
* 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 3 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 at | ||
* https://www.gnu.org/copyleft/gpl.html | ||
* | ||
* @section DESCRIPTION | ||
* | ||
* A model of a MAC address. | ||
*/ | ||
|
||
#ifndef MACADDRESS_H | ||
#define MACADDRESS_H | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "network/dns.h" | ||
#ifdef _WIN32 | ||
#include <windows.h> | ||
#include <windns.h> | ||
#elif defined(__linux__) | ||
#include <gio/gio.h> | ||
#endif | ||
|
||
namespace Nickvision::Network | ||
{ | ||
std::vector<IPv4Address> DNS::resolve(const std::string& hostname) | ||
{ | ||
std::vector<IPv4Address> addresses; | ||
#ifdef _WIN32 | ||
DNS_RECORDA* record{ nullptr }; | ||
if(DnsQuery_A(hostname.c_str(), DNS_TYPE_A, DNS_QUERY_STANDARD, nullptr, &record, nullptr) == 0) | ||
{ | ||
DNS_RECORDA* next{ record }; | ||
while(next) | ||
{ | ||
if(next->wType == DNS_TYPE_A) | ||
{ | ||
addresses.push_back({ next->Data.A.IpAddress }); | ||
} | ||
next = next->pNext; | ||
} | ||
} | ||
DnsFree(record, DnsFreeRecordList); | ||
|
||
#else | ||
GResolver* resolver{ g_resolver_get_default() }; | ||
GList* records{ g_resolver_lookup_by_name(resolver, hostname.c_str(), nullptr, nullptr) }; | ||
if(records) | ||
{ | ||
GList* next{ records }; | ||
while(next) | ||
{ | ||
GInetAddress* address{ G_INET_ADDRESS(next->data) }; | ||
if(g_inet_address_get_family(address) == G_SOCKET_FAMILY_IPV4) | ||
{ | ||
gchar* str{ g_inet_address_to_string(address) }; | ||
addresses.push_back(*IPv4Address::parse(str)); | ||
g_free(str); | ||
} | ||
next = next->next; | ||
} | ||
g_resolver_free_addresses(records); | ||
} | ||
g_object_unref(resolver); | ||
#endif | ||
return addresses; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters