This commit is contained in:
53
src/stoon.c
53
src/stoon.c
@@ -4,11 +4,13 @@
|
||||
#include<winsock2.h>
|
||||
#include<ws2tcpip.h>
|
||||
#include<ntsecapi.h>
|
||||
#include<iphlpapi.h>
|
||||
#else
|
||||
#include<netdb.h>
|
||||
#include<sys/socket.h>
|
||||
#include<sys/random.h>
|
||||
#include<arpa/inet.h>
|
||||
#include<ifaddrs.h>
|
||||
#endif
|
||||
#include<unistd.h>
|
||||
#include<sys/types.h>
|
||||
@@ -17,7 +19,6 @@
|
||||
#include<string.h>
|
||||
#include<stdio.h>
|
||||
#include<errno.h>
|
||||
#include<ifaddrs.h>
|
||||
|
||||
#define STUN_BINDING_REQUEST 0x0001
|
||||
#define STUN_BINDING_INDICATION 0x1100
|
||||
@@ -73,9 +74,14 @@ static int stoon_init_mini(struct Stoon *this, struct addrinfo *serv, uint16_t m
|
||||
freeaddrinfo(myaddrinfo);
|
||||
|
||||
#ifdef _WIN32
|
||||
IP_ADAPTER_ADDRESSES *addrs = malloc(1024 * 64);
|
||||
if(GetAdaptersAddresses(serv->ai_family, GAA_FLAG_SKIP_MULTICAST, NULL, &addrs) == NO_ERROR) {
|
||||
size_t sz = 1024 * 64;
|
||||
IP_ADAPTER_ADDRESSES *addrs = malloc(sz);
|
||||
if(GetAdaptersAddresses(serv->ai_family, GAA_FLAG_SKIP_MULTICAST, NULL, addrs, &sz) == NO_ERROR) {
|
||||
for(IP_ADAPTER_ADDRESSES *ifa = addrs; ifa; ifa = ifa->Next) {
|
||||
if(!ifa->FirstUnicastAddress) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(serv->ai_family == AF_INET6) {
|
||||
uint8_t *addr = (void*) &((struct sockaddr_in6*) ifa->FirstUnicastAddress->Address.lpSockaddr)->sin6_addr;
|
||||
if(addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] == 0 && addr[4] == 0 && addr[5] == 0 && addr[6] == 0 && addr[7] == 0 && addr[8] == 0 && addr[9] == 0 && addr[10] == 0 && addr[11] == 0 && addr[12] == 0 && addr[13] == 0 && addr[14] == 0 && (addr[15] == 0 || addr[15] == 1)) {
|
||||
@@ -100,26 +106,29 @@ static int stoon_init_mini(struct Stoon *this, struct addrinfo *serv, uint16_t m
|
||||
struct ifaddrs *ifaddr;
|
||||
if(getifaddrs(&ifaddr) >= 0) {
|
||||
for(struct ifaddrs *ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
|
||||
if(ifa->ifa_addr->sa_family == serv->ai_family) {
|
||||
if(serv->ai_family == AF_INET6) {
|
||||
uint8_t *addr = (void*) &((struct sockaddr_in6*) ifa->ifa_addr)->sin6_addr;
|
||||
if(addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] == 0 && addr[4] == 0 && addr[5] == 0 && addr[6] == 0 && addr[7] == 0 && addr[8] == 0 && addr[9] == 0 && addr[10] == 0 && addr[11] == 0 && addr[12] == 0 && addr[13] == 0 && addr[14] == 0 && (addr[15] == 0 || addr[15] == 1)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(this->peercode.localV6, addr, 16);
|
||||
memcpy(this->peercode.localP6, &myport, 2);
|
||||
} else {
|
||||
uint8_t *addr = (void*) &((struct sockaddr_in*) ifa->ifa_addr)->sin_addr;
|
||||
if(addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] == 0 || addr[0] == 127 && addr[1] == 0 && addr[2] == 0 && addr[3] == 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(this->peercode.localV4, addr, 4);
|
||||
memcpy(this->peercode.localP4, &myport, 2);
|
||||
}
|
||||
break;
|
||||
if(!ifa->ifa_addr || ifa->ifa_addr->sa_family != serv->ai_family) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(serv->ai_family == AF_INET6) {
|
||||
uint8_t *addr = (void*) &((struct sockaddr_in6*) ifa->ifa_addr)->sin6_addr;
|
||||
if(addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] == 0 && addr[4] == 0 && addr[5] == 0 && addr[6] == 0 && addr[7] == 0 && addr[8] == 0 && addr[9] == 0 && addr[10] == 0 && addr[11] == 0 && addr[12] == 0 && addr[13] == 0 && addr[14] == 0 && (addr[15] == 0 || addr[15] == 1)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(this->peercode.localV6, addr, 16);
|
||||
memcpy(this->peercode.localP6, &myport, 2);
|
||||
} else {
|
||||
uint8_t *addr = (void*) &((struct sockaddr_in*) ifa->ifa_addr)->sin_addr;
|
||||
if(addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] == 0 || addr[0] == 127 && addr[1] == 0 && addr[2] == 0 && addr[3] == 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(this->peercode.localV4, addr, 4);
|
||||
memcpy(this->peercode.localP4, &myport, 2);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
freeifaddrs(ifaddr);
|
||||
|
||||
Reference in New Issue
Block a user