-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #3235 - Specify network interface in thunderscope
- Loading branch information
1 parent
a8b1261
commit 30e5b7d
Showing
32 changed files
with
859 additions
and
151 deletions.
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
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
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
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,44 @@ | ||
#include "software/networking/udp/network_utils.h" | ||
|
||
#include <arpa/inet.h> | ||
|
||
bool getLocalIp(const std::string& interface, std::string& ip_address, bool ipv4) | ||
{ | ||
struct ifaddrs* ifAddrStruct = nullptr; | ||
struct ifaddrs* ifa = nullptr; | ||
|
||
getifaddrs(&ifAddrStruct); | ||
|
||
for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) | ||
{ | ||
if (ifa->ifa_name == interface) | ||
{ | ||
if (ipv4 && ifa->ifa_addr->sa_family == AF_INET) | ||
{ | ||
char addressBuffer[INET_ADDRSTRLEN]; | ||
struct sockaddr_in* sa = (struct sockaddr_in*)ifa->ifa_addr; | ||
inet_ntop(AF_INET, &sa->sin_addr, addressBuffer, INET_ADDRSTRLEN); | ||
freeifaddrs(ifAddrStruct); | ||
ip_address = addressBuffer; | ||
return true; | ||
} | ||
else if (!ipv4 && ifa->ifa_addr->sa_family == AF_INET6) | ||
{ | ||
char addressBuffer[INET6_ADDRSTRLEN]; | ||
struct sockaddr_in6* sa = (struct sockaddr_in6*)ifa->ifa_addr; | ||
inet_ntop(AF_INET6, &sa->sin6_addr, addressBuffer, INET6_ADDRSTRLEN); | ||
freeifaddrs(ifAddrStruct); | ||
ip_address = addressBuffer; | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool isIpv6(const std::string& ip_address) | ||
{ | ||
struct sockaddr_in6 sa; | ||
return inet_pton(AF_INET6, ip_address.c_str(), &(sa.sin6_addr)) != 0; | ||
} |
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,30 @@ | ||
#pragma once | ||
|
||
#include <ifaddrs.h> | ||
|
||
#include <string> | ||
|
||
/** | ||
* Given an interface, get the IP address associated with that interface | ||
* | ||
* The modified ip_address is valid only if the function returns true | ||
* | ||
* @param interface The interface to get the IP address from | ||
* @param ip_address A reference to the std::string that will store the IP address if | ||
* found | ||
* @param ipv4 If true, get the IPv4 address, otherwise get the IPv6 address | ||
* | ||
* @return true if the IP address was found, false otherwise | ||
*/ | ||
bool getLocalIp(const std::string& interface, std::string& ip_address, bool ipv4 = true); | ||
|
||
/** | ||
* Check if the given string follows the IPv6 address format | ||
* | ||
* Addresses that are actually an "embedded IPv4 address" are still considered as an IPv6 | ||
* address since it follows the IPv6 address format | ||
* | ||
* @param ip_address The string to check | ||
* @return true if the string is a valid IPv6 address, false otherwise | ||
*/ | ||
bool isIpv6(const std::string& ip_address); |
Oops, something went wrong.