summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSystem Administrator <root@Mamadous-MacBook-Pro.local>2016-05-30 17:34:26 +0200
committerSystem Administrator <root@Mamadous-MacBook-Pro.local>2016-05-30 17:34:26 +0200
commite43677df4b037ce214f8e223c0fa26d8178e84d9 (patch)
tree61b682bf6a67f7fbcd4b8095800f3a0a59c46527
parent2167a933b24518c04142fa8bee3736befc76b376 (diff)
downloaddoubango-e43677df4b037ce214f8e223c0fa26d8178e84d9.zip
doubango-e43677df4b037ce214f8e223c0fa26d8178e84d9.tar.gz
Ignore IPv4 linklocal addresses when retrieving bestlocal IP
-rwxr-xr-xtinyNET/src/tnet_utils.c45
-rwxr-xr-xtinyNET/src/tnet_utils.h3
2 files changed, 48 insertions, 0 deletions
diff --git a/tinyNET/src/tnet_utils.c b/tinyNET/src/tnet_utils.c
index 56a436a..31e9c13 100755
--- a/tinyNET/src/tnet_utils.c
+++ b/tinyNET/src/tnet_utils.c
@@ -962,14 +962,23 @@ int tnet_getbestsource(const char* destination, tnet_port_t port, tnet_socket_ty
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != destAddr.ss_family) {
continue;
}
+
+ if (destAddr.ss_family == AF_INET) {
+ if (tnet_is_linklocal(ifa->ifa_addr) ^ tnet_is_linklocal(((struct sockaddr *)&destAddr))) {
+ TSK_DEBUG_INFO("Ignoring IPv4 linklocal address");
+ continue;
+ }
+ }
if (destAddr.ss_family == AF_INET6) {
if (IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *) ifa->ifa_addr)->sin6_addr) ^
IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *) &destAddr)->sin6_addr)) {
+ TSK_DEBUG_INFO("Ignoring IPv6 linklocal address");
continue;
}
if (IN6_IS_ADDR_SITELOCAL(&((struct sockaddr_in6 *) ifa->ifa_addr)->sin6_addr) ^
IN6_IS_ADDR_SITELOCAL(&((struct sockaddr_in6 *) &destAddr)->sin6_addr)) {
+ TSK_DEBUG_INFO("Ignoring IPv6 sitelocal address");
continue;
}
}
@@ -1262,6 +1271,42 @@ int tnet_get_ip_n_port(tnet_fd_t fd, tsk_bool_t getlocal, tnet_ip_t *ip, tnet_po
}
/**@ingroup tnet_utils_group
+ */
+tsk_bool_t tnet_is_loopback(const struct sockaddr *sa)
+{
+ if (!sa || (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)) {
+ TSK_DEBUG_ERROR("Invalid paramete");
+ return tsk_false;
+ }
+ if (sa->sa_family == AF_INET) {
+ const uint8_t* u8 = (const uint8_t*)&((const struct sockaddr_in*)sa)->sin_addr;
+ return u8[0] == 127;
+ }
+ else {
+ const uint32_t* u32 = (const uint32_t*)&((const struct sockaddr_in6*)sa)->sin6_addr;
+ return (u32[0] == 0) && (u32[4] == 0) && (u32[8] == 0) && (u32[12] == tnet_ntohl(1));
+ }
+}
+
+/**@ingroup tnet_utils_group
+ */
+tsk_bool_t tnet_is_linklocal(const struct sockaddr *sa)
+{
+ if (!sa || (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)) {
+ TSK_DEBUG_ERROR("Invalid paramete");
+ return tsk_false;
+ }
+ if (sa->sa_family == AF_INET) {
+ const uint8_t* u8 = (const uint8_t*)&((const struct sockaddr_in*)sa)->sin_addr;
+ return u8[0] == 169 && u8[1] == 254;
+ }
+ else {
+ const uint8_t* u8 = (const uint8_t*)&((const struct sockaddr_in6*)sa)->sin6_addr;
+ return ((u8[0] == 0xfe) && ((u8[1] & 0xc0) == 0x80));
+ }
+}
+
+/**@ingroup tnet_utils_group
* Gets the maximum number of file descriptors (FDs) this process is allowed to open.
*/
int tnet_get_fd_max_allowed(tsk_size_t* fd_size)
diff --git a/tinyNET/src/tnet_utils.h b/tinyNET/src/tnet_utils.h
index 9ed6765..f410c00 100755
--- a/tinyNET/src/tnet_utils.h
+++ b/tinyNET/src/tnet_utils.h
@@ -106,6 +106,9 @@ TINYNET_API int tnet_get_peerip_n_port(tnet_fd_t localFD, tnet_ip_t *ip, tnet_po
#define tnet_get_peerip(localFD, ip) tnet_get_peerip_n_port(localFD, ip, 0)
#define tnet_get_peerport(localFD, port) tnet_get_peerip_n_port(localFD, 0, port)
+TINYNET_API tsk_bool_t tnet_is_loopback(const struct sockaddr *sa);
+TINYNET_API tsk_bool_t tnet_is_linklocal(const struct sockaddr *sa);
+
#if TNET_HAVE_SA_LEN
# define tnet_get_sockaddr_size(psockaddr) (psockaddr)->sa_len
#else
OpenPOWER on IntegriCloud