summaryrefslogtreecommitdiffstats
path: root/tinyNET/src/tnet_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'tinyNET/src/tnet_utils.c')
-rwxr-xr-xtinyNET/src/tnet_utils.c916
1 files changed, 469 insertions, 447 deletions
diff --git a/tinyNET/src/tnet_utils.c b/tinyNET/src/tnet_utils.c
index 64b1ea8..d01facd 100755
--- a/tinyNET/src/tnet_utils.c
+++ b/tinyNET/src/tnet_utils.c
@@ -117,18 +117,18 @@ void tnet_getlasterror(tnet_error_t *error)
{
int err = tnet_geterrno();
memset(*error, 0, sizeof(*error));
-
+
#if TNET_UNDER_WINDOWS_RT
// FormatMessageA Not allowed on Market
static WCHAR wBuff[1024] = { 0 };
FormatMessageW(
- FORMAT_MESSAGE_FROM_SYSTEM,
- tsk_null,
- err,
- 0,
- wBuff,
- sizeof(wBuff)-1,
- tsk_null);
+ FORMAT_MESSAGE_FROM_SYSTEM,
+ tsk_null,
+ err,
+ 0,
+ wBuff,
+ sizeof(wBuff)-1,
+ tsk_null);
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wBuff, wcslen(wBuff), *error, sizeof(*error) - 1, NULL, NULL);
#elif TNET_UNDER_WINDOWS
{
@@ -138,13 +138,13 @@ void tnet_getlasterror(tnet_error_t *error)
FormatMessageA
#endif
(
- FORMAT_MESSAGE_FROM_SYSTEM,
- tsk_null,
- err,
- 0,
- *error,
- sizeof(*error) - 1,
- tsk_null);
+ FORMAT_MESSAGE_FROM_SYSTEM,
+ tsk_null,
+ err,
+ 0,
+ *error,
+ sizeof(*error) - 1,
+ tsk_null);
}
#else
strerror_r(err, *error, sizeof(*error));
@@ -175,7 +175,7 @@ int tnet_geterrno()
tnet_interfaces_L_t* tnet_get_interfaces()
{
tnet_interfaces_L_t * ifaces = tsk_list_create();
-
+
#if TNET_UNDER_WINDOWS/*=== WINDOWS XP/VISTA/7/CE===*/
#if TNET_UNDER_WINDOWS_RT
TSK_DEBUG_ERROR("Not implemented on your OS");
@@ -183,101 +183,95 @@ tnet_interfaces_L_t* tnet_get_interfaces()
#else /* !TNET_UNDER_WINDOWS_RT */
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
-
+
PIP_ADAPTER_INFO pAdapterInfo = NULL;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
-
+
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(sizeof(IP_ADAPTER_INFO));
- if (pAdapterInfo == NULL)
- {
+ if (pAdapterInfo == NULL) {
TSK_DEBUG_ERROR("Error allocating memory needed to call GetAdaptersinfo.");
goto bail;
}
// Make an initial call to GetAdaptersInfo to get the necessary size into the ulOutBufLen variable
- if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
- {
+ if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(ulOutBufLen);
- if (pAdapterInfo == NULL)
- {
+ if (pAdapterInfo == NULL) {
TSK_DEBUG_ERROR("Error allocating memory needed to call GetAdaptersinfo.");
goto bail;
}
}
-
- if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
- {
+
+ if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
- while (pAdapter)
- {
+ while (pAdapter) {
tnet_interface_t *iface;
-
- if (pAdapter->Type == MIB_IF_TYPE_LOOPBACK){
+
+ if (pAdapter->Type == MIB_IF_TYPE_LOOPBACK) {
continue;
}
-
+
iface = tnet_interface_create(pAdapter->Description, pAdapter->Address, pAdapter->AddressLength);
iface->index = pAdapter->Index;
tsk_list_push_back_data(ifaces, &(iface));
-
+
pAdapter = pAdapter->Next;
}
}
-
- if (pAdapterInfo)
- {
+
+ if (pAdapterInfo) {
FREE(pAdapterInfo);
}
-
-
+
+
#undef MALLOC
#undef FREE
#endif /* !TNET_UNDER_WINDOWS_RT */
-
+
#elif HAVE_IFADDRS_H && HAVE_GETIFADDRS /*=== Using getifaddrs ===*/
-
+
// see http://www.kernel.org/doc/man-pages/online/pages/man3/getifaddrs.3.html
struct ifaddrs *ifaddr = tsk_null, *ifa = tsk_null;
-
+
/* Get interfaces */
- if(getifaddrs(&ifaddr) == -1){
+ if(getifaddrs(&ifaddr) == -1) {
TSK_DEBUG_ERROR("getifaddrs failed and errno= [%d]", tnet_geterrno());
goto bail;
}
-
- for(ifa = ifaddr; ifa; ifa = ifa->ifa_next){
+
+ for(ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
if((ifa->ifa_flags & IFF_LOOPBACK) || !(ifa->ifa_flags & IFF_UP)) {
continue;
}
-
- if(ifa->ifa_addr->sa_family != AF_LINK){
+
+ if(ifa->ifa_addr->sa_family != AF_LINK) {
continue;
}
-
+
#if defined(__linux__) && 0 /* FIXME */
{
struct ifreq ifr;
tnet_fd_t fd = TNET_INVALID_FD;
-
- if((fd = socket(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP)) < 0){
+
+ if((fd = socket(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
TSK_DEBUG_ERROR("Failed to create new DGRAM socket and errno= [%d]", tnet_geterrno());
goto next;
}
-
+
ifr.ifr_addr.sa_family = ifa->ifa_addr->sa_family;
strcpy(ifr.ifr_name, ifa.ifa_name);
- if(tnet_ioctl(fd, SIOCGIFHWADDR, &ifr)<0){
+ if(tnet_ioctl(fd, SIOCGIFHWADDR, &ifr)<0) {
TSK_DEBUG_ERROR("tnet_ioctl(SIOCGIFHWADDR)", tnet_geterrno());
goto next;
}
- else{
+ else {
//sockaddr_dl* sdl = (struct sockaddr_dl *)ifa->ifa_addr;
tnet_interface_t *iface = tnet_interface_create(ifr->ifr_name, ifr->ifr_hwaddr.sa_data, 6);
tsk_list_push_back_data(ifaces, (void**)&(iface));
}
- next:
+next:
tnet_sockfd_close(&fd);
}
#else
@@ -288,66 +282,65 @@ tnet_interfaces_L_t* tnet_get_interfaces()
tsk_list_push_back_data(ifaces, (void**)&(iface));
}
#endif
-
+
}/* for */
-
+
freeifaddrs(ifaddr);
-
+
#else /*=== ANDROID,... --> Using SIOCGIFCONF and SIOCGIFHWADDR ===*/
-
+
tnet_fd_t fd = TNET_INVALID_FD;
char buffer[1024];
struct ifconf ifc;
-
+
struct sockaddr_in *sin;
struct ifreq *ifr;
-
- if((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
+
+ if((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
TSK_DEBUG_ERROR("Failed to create new DGRAM socket and errno= [%d]", tnet_geterrno());
goto done;
}
-
+
ifc.ifc_len = sizeof(buffer);
ifc.ifc_buf = buffer;
-
- if(ioctl(fd, SIOCGIFCONF, &ifc) < 0){
+
+ if(ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
TSK_DEBUG_ERROR("ioctl(SIOCGIFCONF) failed and errno= [%d]", tnet_geterrno());
goto done;
}
- if(!ifr || !ifc.ifc_req){
+ if(!ifr || !ifc.ifc_req) {
TSK_DEBUG_ERROR("ifr or ifc.ifc_req is null");
goto done;
}
-
- if(!ifr->ifr_name){
+
+ if(!ifr->ifr_name) {
TSK_DEBUG_ERROR("ifr->ifr_name is null");
goto done;
}
-
- for(ifr = ifc.ifc_req; ifr && !tsk_strempty(ifr->ifr_name); ifr++){
+
+ for(ifr = ifc.ifc_req; ifr && !tsk_strempty(ifr->ifr_name); ifr++) {
sin = (struct sockaddr_in *)&(ifr->ifr_addr);
// TODO: IPAddress if needed
- if(/*ioctl(fd, SIOCGIFFLAGS, &ifr) == 0*/1){
- if (!(ifr->ifr_flags & IFF_LOOPBACK) && (ifr->ifr_flags & IFF_UP)){
- if(/*ioctl(fd, SIOCGIFHWADDR, &ifr) == 0*/1){
+ if(/*ioctl(fd, SIOCGIFFLAGS, &ifr) == 0*/1) {
+ if (!(ifr->ifr_flags & IFF_LOOPBACK) && (ifr->ifr_flags & IFF_UP)) {
+ if(/*ioctl(fd, SIOCGIFHWADDR, &ifr) == 0*/1) {
tnet_interface_t *iface = tnet_interface_create(ifr->ifr_name, ifr->ifr_hwaddr.sa_data, 6);
tsk_list_push_back_data(ifaces, (void**)&(iface));
//iface->index = if_nametoindex(ifr->ifr_name);
}
}
}
- else
- {
+ else {
TSK_DEBUG_ERROR("ioctl(SIOCGIFFLAGS) failed and errno= [%d]", tnet_geterrno());
}
}
-
+
done:
tnet_sockfd_close(&fd);
-
-
+
+
#endif
-
+
bail:
return ifaces;
}
@@ -366,69 +359,71 @@ bail:
tnet_addresses_L_t* tnet_get_addresses(tnet_family_t family, tsk_bool_t unicast, tsk_bool_t anycast, tsk_bool_t multicast, tsk_bool_t dnsserver, long if_index)
{
tnet_addresses_L_t *addresses = tsk_list_create();
-
+
#if TNET_UNDER_WINDOWS
#if TNET_UNDER_WINDOWS_RT
TSK_DEBUG_ERROR("Not implemented on your OS");
#else /* !TSK_UNDER_WINDOWS_RT */
-
+
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
-
+
/* Declare and initialize variables */
tnet_ip_t ip;
DWORD dwSize = 0;
DWORD dwRetVal = 0;
-
+
int i = 0;
-
+
// Set the flags to pass to GetAdaptersAddresses
ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
-
+
LPVOID lpMsgBuf = NULL;
-
+
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
ULONG outBufLen = 0;
-
+
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
PIP_ADAPTER_ANYCAST_ADDRESS pAnycast = NULL;
PIP_ADAPTER_MULTICAST_ADDRESS pMulticast = NULL;
IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL;
IP_ADAPTER_PREFIX *pPrefix = NULL;
-
-
+
+
outBufLen = sizeof(IP_ADAPTER_ADDRESSES);
pAddresses = (IP_ADAPTER_ADDRESSES *)MALLOC(outBufLen);
-
+
// Make an initial call to GetAdaptersAddresses to get the
// size needed into the outBufLen variable
- if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW){
+ if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES *)MALLOC(outBufLen);
}
- else goto bail;
-
- if (pAddresses == NULL){
+ else {
+ goto bail;
+ }
+
+ if (pAddresses == NULL) {
TSK_DEBUG_ERROR("Memory allocation failed for IP_ADAPTER_ADDRESSES struct.");
goto bail;
}
-
+
dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
-
+
if (dwRetVal == NO_ERROR) {
pCurrAddresses = pAddresses;
- while (pCurrAddresses){
- if ((if_index != -1) && (pCurrAddresses->IfIndex != if_index && pCurrAddresses->Ipv6IfIndex != if_index)){
+ while (pCurrAddresses) {
+ if ((if_index != -1) && (pCurrAddresses->IfIndex != if_index && pCurrAddresses->Ipv6IfIndex != if_index)) {
goto next;
}
- if (pCurrAddresses->OperStatus != IfOperStatusUp){
+ if (pCurrAddresses->OperStatus != IfOperStatusUp) {
goto next;
}
-
+
/* == UNICAST addresses == */
pUnicast = pCurrAddresses->FirstUnicastAddress;
- while (unicast && pUnicast){
+ while (unicast && pUnicast) {
//memset(ip, '\0', sizeof(ip));
tnet_get_sockip(pUnicast->Address.lpSockaddr, &ip);
TSK_DEBUG_INFO("Found local IP address = AdapterName=%s Ip=%s", pCurrAddresses->AdapterName, ip);
@@ -438,13 +433,13 @@ tnet_addresses_L_t* tnet_get_addresses(tnet_family_t family, tsk_bool_t unicast,
address->unicast = 1;
tsk_list_push_ascending_data(addresses, &address);
}
-
+
pUnicast = pUnicast->Next;
}
-
+
/* == ANYCAST addresses == */
pAnycast = pCurrAddresses->FirstAnycastAddress;
- while (anycast && pAnycast){
+ while (anycast && pAnycast) {
//memset(ip, '\0', sizeof(ip));
tnet_get_sockip(pAnycast->Address.lpSockaddr, &ip);
{
@@ -453,13 +448,13 @@ tnet_addresses_L_t* tnet_get_addresses(tnet_family_t family, tsk_bool_t unicast,
address->anycast = 1;
tsk_list_push_ascending_data(addresses, &address);
}
-
+
pAnycast = pAnycast->Next;
}
-
+
/* == MULTYCAST addresses == */
pMulticast = pCurrAddresses->FirstMulticastAddress;
- while (multicast && pMulticast){
+ while (multicast && pMulticast) {
//memset(ip, '\0', sizeof(ip));
tnet_get_sockip(pMulticast->Address.lpSockaddr, &ip);
{
@@ -468,69 +463,69 @@ tnet_addresses_L_t* tnet_get_addresses(tnet_family_t family, tsk_bool_t unicast,
address->multicast = 1;
tsk_list_push_ascending_data(addresses, &address);
}
-
+
pMulticast = pMulticast->Next;
}
-
+
/* == DNS servers == */
pDnServer = pCurrAddresses->FirstDnsServerAddress;
- while (dnsserver && pDnServer){
+ while (dnsserver && pDnServer) {
//memset(ip, '\0', sizeof(ip));
- if (!tnet_get_sockip(pDnServer->Address.lpSockaddr, &ip)){
+ if (!tnet_get_sockip(pDnServer->Address.lpSockaddr, &ip)) {
tnet_address_t *address = tnet_address_create(ip);
address->family = pDnServer->Address.lpSockaddr->sa_family;
address->dnsserver = 1;
tsk_list_push_ascending_data(addresses, &address);
}
-
+
pDnServer = pDnServer->Next;
}
- next:
+next:
pCurrAddresses = pCurrAddresses->Next;
}
}
-
- if (pAddresses){
+
+ if (pAddresses) {
FREE(pAddresses);
}
-
+
#undef MALLOC
#undef FREE
-
- bail :
-
+
+bail :
+
#endif /* !TSK_UNDER_WINDOWS_RT */
-
+
#else /* !TSK_UNDER_WINDOWS (MAC OS X, UNIX, ANDROID ...) */
-
+
tnet_ip_t ip;
#if HAVE_IFADDRS_H && HAVE_GETIFADDRS /*=== Using getifaddrs ===*/
-
+
// see http://www.kernel.org/doc/man-pages/online/pages/man3/getifaddrs.3.html
struct ifaddrs *ifaddr = tsk_null, *ifa = tsk_null;
struct sockaddr *addr;
-
+
/* Get interfaces */
- if(getifaddrs(&ifaddr) == -1){
+ if(getifaddrs(&ifaddr) == -1) {
TSK_DEBUG_ERROR("getifaddrs failed and errno= [%d]", tnet_geterrno());
goto bail;
}
-
+
/* == Unicast addresses == */
- for(ifa = ifaddr; ifa; ifa = ifa->ifa_next){
- if(!ifa->ifa_addr){
+ for(ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
+ if(!ifa->ifa_addr) {
continue;
}
// Skip loopback
if ((ifa->ifa_flags & IFF_LOOPBACK) || !(ifa->ifa_flags & IFF_UP)) {
continue;
}
-
+
// Skip unwanted interface
if (if_index != -1 && if_nametoindex(ifa->ifa_name) != if_index) {
continue;
}
-
+
// Only deal with Unicast address
if (unicast) {
if (family == AF_INET && ifa->ifa_addr->sa_family != AF_INET) {
@@ -542,11 +537,11 @@ tnet_addresses_L_t* tnet_get_addresses(tnet_family_t family, tsk_bool_t unicast,
if (ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) {
continue;
}
-
+
// Get the IP string
addr = (struct sockaddr *) ifa->ifa_addr;
tnet_get_sockip(addr, &ip);
-
+
// Push a new address
tnet_address_t *address = tnet_address_create(ip);
address->family = ifa->ifa_addr->sa_family;
@@ -554,81 +549,81 @@ tnet_addresses_L_t* tnet_get_addresses(tnet_family_t family, tsk_bool_t unicast,
tsk_list_push_ascending_data(addresses, (void **) &address);
}
}
-
+
if (ifaddr) {
free(ifaddr);
}
-
+
#else /* ANDROID or any system without getifaddrs */
-
+
tnet_address_t *address;
tnet_fd_t fd = TNET_INVALID_FD;
struct ifconf ifc;
struct ifreq *ifr = 0;
memset(&ifc, 0, sizeof(ifc));
-
- if((fd = socket(family, SOCK_DGRAM, IPPROTO_UDP)) < 0){
+
+ if((fd = socket(family, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
TSK_DEBUG_ERROR("Failed to create new DGRAM socket and errno= [%d]", tnet_geterrno());
goto done;
}
-
- if(ioctl(fd, SIOCGIFCONF, &ifc) < 0){
+
+ if(ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
TSK_DEBUG_ERROR("ioctl(SIOCGIFCONF) failed and errno= [%d]", tnet_geterrno());
goto done;
}
-
+
if (!(ifr = (struct ifreq*) malloc(ifc.ifc_len))) {
TSK_DEBUG_ERROR("Could not malloc ifreq with size =%d", ifc.ifc_len);
goto done;
}
-
+
ifc.ifc_ifcu.ifcu_req = ifr;
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
TSK_DEBUG_ERROR("ioctl SIOCGIFCONF failed");
goto done;
}
-
+
int i;
- for(i = 0; i < ifc.ifc_len / sizeof(struct ifreq); ++i){
+ for(i = 0; i < ifc.ifc_len / sizeof(struct ifreq); ++i) {
if (unicast) {
-
+
}
// Skip unwanted interface
if (if_index != -1 && ifr->ifr_ifindex != if_index) {
continue;
}
-
+
// Get the IP string
- if(tnet_get_sockip(&ifr[i].ifr_addr, &ip) == 0){
+ if(tnet_get_sockip(&ifr[i].ifr_addr, &ip) == 0) {
// Push a new address
- if((address = tnet_address_create(ip))){
+ if((address = tnet_address_create(ip))) {
address->family = family;
address->unicast = unicast;
tsk_list_push_ascending_data(addresses, (void **) &address);
}
}
}
-
+
done:
TSK_FREE(ifr);
tnet_sockfd_close(&fd);
-
+
#endif /* HAVE_IFADDRS_H && HAVE_GETIFADDRS */
-
+
bail:
-
+
/* == DNS servers == */
- if(dnsserver){
+ if(dnsserver) {
TSK_DEBUG_INFO("Calling 'tnet_dns_resolvconf_parse()' to load DNS servers");
tnet_addresses_L_t * dns_servers;
- if((dns_servers = tnet_dns_resolvconf_parse("/etc/resolv.conf"))){
+ if((dns_servers = tnet_dns_resolvconf_parse("/etc/resolv.conf"))) {
tsk_list_pushback_list(addresses, dns_servers);
TSK_OBJECT_SAFE_FREE(dns_servers);
}
}
-
+
#endif
-
+
return addresses;
}
@@ -637,39 +632,39 @@ bail:
*/
int tnet_get_mac_address(tnet_mac_address* address)
{
- static const tsk_size_t __tnet_mac_address_len = sizeof(tnet_mac_address) / sizeof(uint8_t/*tnet_mac_address[0]*/);
- int ret = -1;
- if (!address) {
- TSK_DEBUG_ERROR("Invalid parameter");
- }
+ static const tsk_size_t __tnet_mac_address_len = sizeof(tnet_mac_address) / sizeof(uint8_t/*tnet_mac_address[0]*/);
+ int ret = -1;
+ if (!address) {
+ TSK_DEBUG_ERROR("Invalid parameter");
+ }
#if TNET_UNDER_WINDOWS
# if TNET_UNDER_WINDOWS_RT
- TSK_DEBUG_ERROR("Not implemented on your OS");
+ TSK_DEBUG_ERROR("Not implemented on your OS");
# else /* !TSK_UNDER_WINDOWS_RT */
- {
- IP_ADAPTER_INFO *info = NULL, *pos;
- DWORD size = 0;
- ULONG _ret;
-
- if ((_ret = GetAdaptersInfo(info, &size)) == ERROR_SUCCESS || _ret == ERROR_BUFFER_OVERFLOW) {
- info = (IP_ADAPTER_INFO *)tsk_calloc(size + 1, 1);
- if (info) {
- if ((_ret = GetAdaptersInfo(info, &size)) == ERROR_SUCCESS) {
- UINT i;
- for (pos = info; pos != NULL && ret != 0; pos = pos->Next) {
- if (pos->Type == MIB_IF_TYPE_LOOPBACK && pos->Next) { // skip loopback if we still have items to check
- continue;
- }
- for (i = 0; i < pos->AddressLength && i < __tnet_mac_address_len; ++i) {
- (*address)[i] = pos->Address[i];
- }
- ret = 0;
- }
- }
- }
- TSK_FREE(info);
- }
- }
+ {
+ IP_ADAPTER_INFO *info = NULL, *pos;
+ DWORD size = 0;
+ ULONG _ret;
+
+ if ((_ret = GetAdaptersInfo(info, &size)) == ERROR_SUCCESS || _ret == ERROR_BUFFER_OVERFLOW) {
+ info = (IP_ADAPTER_INFO *)tsk_calloc(size + 1, 1);
+ if (info) {
+ if ((_ret = GetAdaptersInfo(info, &size)) == ERROR_SUCCESS) {
+ UINT i;
+ for (pos = info; pos != NULL && ret != 0; pos = pos->Next) {
+ if (pos->Type == MIB_IF_TYPE_LOOPBACK && pos->Next) { // skip loopback if we still have items to check
+ continue;
+ }
+ for (i = 0; i < pos->AddressLength && i < __tnet_mac_address_len; ++i) {
+ (*address)[i] = pos->Address[i];
+ }
+ ret = 0;
+ }
+ }
+ }
+ TSK_FREE(info);
+ }
+ }
# endif /* TSK_UNDER_WINDOWS_RT */
#elif HAVE_IFADDRS_H && HAVE_GETIFADDRS && HAVE_STRUCT_SOCKADDR_DL
struct ifaddrs *ifaddrs, *ifaddr;
@@ -696,13 +691,13 @@ int tnet_get_mac_address(tnet_mac_address* address)
struct ifreq ifr;
struct ifconf ifc;
char buf[1024];
-
+
tnet_fd_t fd = tnet_soccket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd == TNET_INVALID_FD) {
TSK_DEBUG_ERROR("Failed to create socket");
return -1;
}
-
+
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) {
@@ -710,10 +705,10 @@ int tnet_get_mac_address(tnet_mac_address* address)
tnet_sockfd_close(&fd);
return -1;
}
-
+
struct ifreq* it = ifc.ifc_req;
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
-
+
for (; it != end; ++it) {
strcpy(ifr.ifr_name, it->ifr_name);
if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0) {
@@ -732,7 +727,7 @@ int tnet_get_mac_address(tnet_mac_address* address)
}
#endif
- return ret;
+ return ret;
}
/**@ingroup tnet_utils_group
@@ -747,82 +742,76 @@ int tnet_getbestsource(const char* destination, tnet_port_t port, tnet_socket_ty
{
int ret = -1;
struct sockaddr_storage destAddr;
-
+
#if TNET_UNDER_WINDOWS
long dwBestIfIndex = -1;
#endif
#if TNET_UNDER_WINDOWS_RT
Windows::Networking::Connectivity::ConnectionProfile^ profile;
#endif
-
- if (!destination || !source){
+
+ if (!destination || !source) {
TSK_DEBUG_ERROR("Invalid parameter");
goto bail;
}
-
+
memset(*source, '\0', sizeof(*source));
-
+
// special cases for Windows Phone device and emulator
#if TNET_UNDER_WINDOWS_PHONE
- if (tsk_strequals(destination, "127.0.0.1")){
+ if (tsk_strequals(destination, "127.0.0.1")) {
memcpy(*source, "127.0.0.1", 9);
- ret = 0; goto bail;
+ ret = 0;
+ goto bail;
}
- if(tsk_strequals(destination, "::1")){
+ if(tsk_strequals(destination, "::1")) {
memcpy(*source, "::1", 3);
- ret = 0; goto bail;
+ ret = 0;
+ goto bail;
}
#endif
-
- if((ret = tnet_sockaddr_init(destination, port, type, &destAddr))){
+
+ if((ret = tnet_sockaddr_init(destination, port, type, &destAddr))) {
goto bail;
}
-
+
#if TNET_UNDER_WINDOWS_RT /* Windows Phone 8, Surface or any RT */
profile = Windows::Networking::Connectivity::NetworkInformation::GetInternetConnectionProfile();
-
- if (profile != nullptr && profile->NetworkAdapter != nullptr){
+
+ if (profile != nullptr && profile->NetworkAdapter != nullptr) {
TSK_DEBUG_INFO("Network profile IanaInterfaceType = %d", profile->NetworkAdapter->IanaInterfaceType);
Windows::Foundation::Collections::IVectorView<Windows::Networking::HostName^>^ HostNames = Windows::Networking::Connectivity::NetworkInformation::GetHostNames();
-
- if(HostNames->Size > 0)
- {
+
+ if(HostNames->Size > 0) {
Windows::Foundation::Collections::IIterator<Windows::Networking::HostName^>^ HostName = HostNames->First();
- do
- {
+ do {
std::vector<char> CanonicalName = rt_tsk_str_to_native(HostName->Current->CanonicalName);
TSK_DEBUG_INFO("Checking IP address = %s", CanonicalName.data());
- if((TNET_SOCKET_TYPE_IS_IPV4(type) && HostName->Current->IPInformation->PrefixLength->Value > 32) || (TNET_SOCKET_TYPE_IS_IPV6(type) && HostName->Current->IPInformation->PrefixLength->Value > 128))
- {
+ if((TNET_SOCKET_TYPE_IS_IPV4(type) && HostName->Current->IPInformation->PrefixLength->Value > 32) || (TNET_SOCKET_TYPE_IS_IPV6(type) && HostName->Current->IPInformation->PrefixLength->Value > 128)) {
TSK_DEBUG_INFO("Type mismatch - Skiping IP address=%s, IanaInterfaceType=%d, PrefixLength=%d", CanonicalName.data(), HostName->Current->IPInformation->NetworkAdapter->IanaInterfaceType, HostName->Current->IPInformation->PrefixLength->Value);
continue;
}
-
-
- if(HostName->Current->IPInformation != nullptr)
- {
+
+
+ if(HostName->Current->IPInformation != nullptr) {
// http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.connectivity.networkadapter.networkadapterid.aspx
// HostName->Current->IPInformation->NetworkAdapter->NetworkAdapterId not implemented on WP8
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
tnet_socket_t* ss = tnet_socket_create(CanonicalName.data(), TNET_SOCKET_PORT_ANY, type);
- if(ss)
- {
+ if(ss) {
ret = connect(ss->fd, (const sockaddr*)&destAddr, tnet_get_sockaddr_size((const sockaddr*)&destAddr));
- if(ret && tnet_geterrno() == TNET_ERROR_EAGAIN)
- {
+ if(ret && tnet_geterrno() == TNET_ERROR_EAGAIN) {
ret = tnet_sockfd_waitUntilWritable(ss->fd, 500);
}
TSK_OBJECT_SAFE_FREE(ss);
}
# else
- if(HostName->Current->IPInformation->NetworkAdapter->IanaInterfaceType == profile->NetworkAdapter->IanaInterfaceType)
- {
+ if(HostName->Current->IPInformation->NetworkAdapter->IanaInterfaceType == profile->NetworkAdapter->IanaInterfaceType) {
ret = 0;
}
#endif /* */
-
- if(ret == 0)
- {
+
+ if(ret == 0) {
TSK_DEBUG_INFO("Using best IP address = %s :)", CanonicalName.data());
memcpy(*source, CanonicalName.data(), TSK_MIN(tsk_strlen(CanonicalName.data()), sizeof(*source)));
ret = 0;
@@ -834,30 +823,29 @@ int tnet_getbestsource(const char* destination, tnet_port_t port, tnet_socket_ty
while(HostName->MoveNext());
}
}
- else
- {
+ else {
TSK_DEBUG_ERROR("No network connection available");
}
-
+
#elif TNET_UNDER_WINDOWS /* Windows XP/Vista/7 and Windows Mobile */
- if(GetBestInterfaceEx((struct sockaddr*)&destAddr, &dwBestIfIndex) != NO_ERROR){
+ if(GetBestInterfaceEx((struct sockaddr*)&destAddr, &dwBestIfIndex) != NO_ERROR) {
ret = tnet_geterrno();
TNET_PRINT_LAST_ERROR("GetBestInterfaceEx() failed.");
goto bail;
}
- else{
+ else {
tnet_addresses_L_t* addresses = tsk_null;
const tsk_list_item_t* item;
-
- if (!(addresses = tnet_get_addresses(TNET_SOCKET_TYPE_IS_IPV6(type) ? AF_INET6 : AF_INET, tsk_true, tsk_false, tsk_false, tsk_false, dwBestIfIndex))){
+
+ if (!(addresses = tnet_get_addresses(TNET_SOCKET_TYPE_IS_IPV6(type) ? AF_INET6 : AF_INET, tsk_true, tsk_false, tsk_false, tsk_false, dwBestIfIndex))) {
ret = -2;
TSK_DEBUG_ERROR("Failed to retrieve addresses.");
goto bail;
}
-
- tsk_list_foreach(item, addresses){
+
+ tsk_list_foreach(item, addresses) {
const tnet_address_t* address = item->data;
- if (address && address->ip){
+ if (address && address->ip) {
memcpy(*source, address->ip, tsk_strlen(address->ip) > sizeof(*source) ? sizeof(*source) : tsk_strlen(address->ip));
ret = 0;
break; // First is good for us.
@@ -867,9 +855,9 @@ int tnet_getbestsource(const char* destination, tnet_port_t port, tnet_socket_ty
}
#elif HAVE_NET_ROUTE_H && HAVE_IFADDRS_H && HAVE_GETIFADDRS /* Mac OS X, iPhone, iPod Touch, iPad and Linux family except Android */
/* Thanks to Laurent Etiemble */
-
+
int sdl_index = -1;
-
+
#if HAVE_STRUCT_RT_METRICS && HAVE_STRUCT_SOCKADDR_DL
static int seq = 1234;
char buf[1024];
@@ -882,20 +870,20 @@ int tnet_getbestsource(const char* destination, tnet_port_t port, tnet_socket_ty
struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
struct sockaddr_dl so_ifp;
#endif /* HAVE_STRUCT_RT_METRICS && HAVE_STRUCT_SOCKADDR_DL */
-
+
struct sockaddr_storage so_dst = destAddr;
struct sockaddr *sa = NULL;
struct ifaddrs *ifaddr = 0, *ifa = tsk_null;
tnet_ip_t ip;
-
+
#if HAVE_STRUCT_RT_METRICS && HAVE_STRUCT_SOCKADDR_DL
bzero(rtm, 1024);
cp = (char *)(rtm + 1);
-
+
so_ifp.sdl_index = 0;
so_ifp.sdl_family = AF_LINK;
so_ifp.sdl_len = sizeof(struct sockaddr_dl);
-
+
rtm->rtm_type = RTM_GET;
rtm->rtm_flags = RTF_STATIC | RTF_UP | RTF_GATEWAY;
rtm->rtm_version = RTM_VERSION;
@@ -904,103 +892,104 @@ int tnet_getbestsource(const char* destination, tnet_port_t port, tnet_socket_ty
rtm->rtm_rmx = rt_metrics;
rtm->rtm_inits = rtm_inits;
rtm->rtm_index = 0;
-
+
/** Roundup value to a 4 bytes boundary. */
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
-
+
l = ROUNDUP(so_dst.ss_len);
memcpy(&so_dst, cp, l);
cp += l;
-
+
l = ROUNDUP(so_ifp.sdl_len);
memcpy(&so_ifp, cp, l);
cp += l;
-
+
l = cp - buf;
rtm->rtm_msglen = l;
-
+
s = socket(PF_ROUTE, SOCK_RAW, 0);
if (s < 0) {
// TODO
}
-
+
if ((rlen = write(s, rtm, l)) < 0) {
TSK_DEBUG_INFO("writing to routing socket");
// TODO
}
do {
l = read(s, rtm, 1024);
- } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
-
+ }
+ while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
+
/** Advance an address to the closest 4 bytes boundary. */
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
-
+
if (rtm->rtm_errno == 0 && rtm->rtm_addrs) {
cp = (char *)(rtm + 1);
for (i = 1; i; i <<= 1) {
if (i & rtm->rtm_addrs) {
sa = (struct sockaddr *)cp;
switch (i) {
- case RTA_IFP:
- ifp = (struct sockaddr_dl *) sa;
- break;
+ case RTA_IFP:
+ ifp = (struct sockaddr_dl *) sa;
+ break;
}
ADVANCE(cp, sa);
}
}
}
- if(ifp){
+ if(ifp) {
sdl_index = ifp->sdl_index;
}
#endif /* HAVE_STRUCT_RT_METRICS && HAVE_STRUCT_SOCKADDR_DL */
-
+
/* Get interfaces */
- if(getifaddrs(&ifaddr) == -1){
+ if(getifaddrs(&ifaddr) == -1) {
TNET_PRINT_LAST_ERROR("getifaddrs() failed.");
goto bail;
}
-
- for(ifa = ifaddr; ifa; ifa = ifa->ifa_next){
+
+ for(ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
if ((ifa->ifa_flags & IFF_LOOPBACK) || !(ifa->ifa_flags & IFF_UP)) {
continue;
}
-
+
if (sdl_index != -1 && if_nametoindex(ifa->ifa_name) != sdl_index) {
continue;
}
-
+
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != destAddr.ss_family) {
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)) {
+ IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *) &destAddr)->sin6_addr)) {
continue;
}
if (IN6_IS_ADDR_SITELOCAL(&((struct sockaddr_in6 *) ifa->ifa_addr)->sin6_addr) ^
- IN6_IS_ADDR_SITELOCAL(&((struct sockaddr_in6 *) &destAddr)->sin6_addr)) {
+ IN6_IS_ADDR_SITELOCAL(&((struct sockaddr_in6 *) &destAddr)->sin6_addr)) {
continue;
}
}
-
+
tnet_get_sockip((struct sockaddr *) ifa->ifa_addr, &ip);
-
+
memcpy(*source, ip, tsk_strlen(ip) > sizeof(*source) ? sizeof(*source) : tsk_strlen(ip));
ret = 0;
goto bail; // First is good for us.
}
-
-
+
+
#else /* All other systems (Google Android, Unix-Like systems, uLinux, ....) */
TSK_DEBUG_WARN("getbestroute() not supported on this OS");
memcpy(*source,
TNET_SOCKET_TYPE_IS_IPV6(type) ? "::" : "0.0.0.0",
TNET_SOCKET_TYPE_IS_IPV6(type) ? 2 : 7
- );
+ );
#endif
-
+
bail:
return ret;
}
@@ -1021,7 +1010,7 @@ bail:
int tnet_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
{
int ret = -1;
- if (hints && (ret = getaddrinfo(node, service, hints, res))){
+ if (hints && (ret = getaddrinfo(node, service, hints, res))) {
TSK_DEBUG_ERROR("getaddrinfo(family=%d, node=%s and service=%s) failed: [%s]", hints->ai_family, node, service, tnet_gai_strerror(ret));
}
return ret;
@@ -1035,7 +1024,7 @@ int tnet_getaddrinfo(const char *node, const char *service, const struct addrinf
**/
void tnet_freeaddrinfo(struct addrinfo *ai)
{
- if (ai){
+ if (ai) {
freeaddrinfo(ai);
}
}
@@ -1048,7 +1037,7 @@ void tnet_freeaddrinfo(struct addrinfo *ai)
*/
int tnet_getsockname(tnet_fd_t fd, struct sockaddr_storage *result)
{
- if (fd > 0 && result){
+ if (fd > 0 && result) {
socklen_t namelen = sizeof(*result);
return getsockname(fd, (struct sockaddr*)result, &namelen);
}
@@ -1057,7 +1046,7 @@ int tnet_getsockname(tnet_fd_t fd, struct sockaddr_storage *result)
int tnet_getpeername(tnet_fd_t fd, struct sockaddr_storage *result)
{
- if (fd > 0 && result){
+ if (fd > 0 && result) {
socklen_t namelen = sizeof(*result);
return getpeername(fd, (struct sockaddr*)result, &namelen);
}
@@ -1072,7 +1061,7 @@ int tnet_getpeername(tnet_fd_t fd, struct sockaddr_storage *result)
tnet_socket_type_t tnet_get_socket_type(tnet_fd_t fd)
{
tnet_socket_type_t type = tnet_socket_type_invalid;
-
+
/*if(fd >0)
{
struct sockaddr_storage ss;
@@ -1088,7 +1077,7 @@ tnet_socket_type_t tnet_get_socket_type(tnet_fd_t fd)
}
}
}*/
-
+
return type;
}
@@ -1102,39 +1091,39 @@ tnet_socket_type_t tnet_get_socket_type(tnet_fd_t fd)
tnet_family_t tnet_get_family(const char* host, tnet_port_t port)
{
tnet_family_t ret = AF_UNSPEC;
- if (host){
+ if (host) {
int status;
tsk_istr_t srv;
struct addrinfo *result = tsk_null;
struct addrinfo hints;
-
+
/* set the port: used as the default service */
- if (port){
+ if (port) {
tsk_itoa(port, &srv);
}
- else{
+ else {
memset(srv, '\0', sizeof(srv));
}
-
+
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
-
+
if ((status = tnet_getaddrinfo(host, srv, &hints, &result))) {
TNET_PRINT_LAST_ERROR("getaddrinfo(%s:%d) failed", host, port);
goto done;
}
-
+
/* Get the First result. */
if (result) {
ret = result->ai_family;
goto done;
}
- done:
+done:
tnet_freeaddrinfo(result);
}
-
+
return ret;
}
@@ -1148,47 +1137,45 @@ tnet_family_t tnet_get_family(const char* host, tnet_port_t port)
int tnet_get_sockip_n_port(const struct sockaddr *addr, tnet_ip_t *ip, tnet_port_t *port)
{
int status = -1;
-
- if (addr->sa_family == AF_INET){
+
+ if (addr->sa_family == AF_INET) {
const struct sockaddr_in *sin = (const struct sockaddr_in *)addr;
- if (port){
+ if (port) {
*port = tnet_ntohs(sin->sin_port);
status = 0;
}
- if (ip){
- if ((status = tnet_getnameinfo((const struct sockaddr*)sin, sizeof(*sin), *ip, sizeof(*ip), 0, 0, NI_NUMERICHOST))){
+ if (ip) {
+ if ((status = tnet_getnameinfo((const struct sockaddr*)sin, sizeof(*sin), *ip, sizeof(*ip), 0, 0, NI_NUMERICHOST))) {
return status;
}
}
}
- else if (addr->sa_family == AF_INET6)
- {
+ else if (addr->sa_family == AF_INET6) {
const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)addr;
#if TNET_UNDER_WINDOWS
int index;
#endif
- if (port){
+ if (port) {
*port = tnet_ntohs(sin6->sin6_port);
status = 0;
}
- if (ip){
- if ((status = tnet_getnameinfo((const struct sockaddr*)sin6, sizeof(*sin6), *ip, sizeof(*ip), 0, 0, NI_NUMERICHOST))){
+ if (ip) {
+ if ((status = tnet_getnameinfo((const struct sockaddr*)sin6, sizeof(*sin6), *ip, sizeof(*ip), 0, 0, NI_NUMERICHOST))) {
return status;
}
-
+
#if TNET_UNDER_WINDOWS
- if ((index = tsk_strindexOf(*ip, tsk_strlen(*ip), "%")) > 0){
+ if ((index = tsk_strindexOf(*ip, tsk_strlen(*ip), "%")) > 0) {
*(*ip + index) = '\0';
}
#endif
}
}
- else
- {
+ else {
TSK_DEBUG_ERROR("Unsupported address family.");
return status;
}
-
+
return status;
}
@@ -1202,24 +1189,24 @@ int tnet_get_sockip_n_port(const struct sockaddr *addr, tnet_ip_t *ip, tnet_port
*/
int tnet_get_peerip_n_port(tnet_fd_t localFD, tnet_ip_t *ip, tnet_port_t *port)
{
- if (port){
+ if (port) {
*port = 0;
}
-
- if (localFD > 0){
+
+ if (localFD > 0) {
int status;
socklen_t len;
struct sockaddr_storage ss;
-
+
len = sizeof(ss);
- if ((status = getpeername(localFD, (struct sockaddr *)&ss, &len))){
+ if ((status = getpeername(localFD, (struct sockaddr *)&ss, &len))) {
TSK_DEBUG_ERROR("TNET_GET_SOCKADDR has failed with status code: %d", status);
return -1;
}
-
+
return tnet_get_sockip_n_port(((struct sockaddr *)&ss), ip, port);
}
-
+
TSK_DEBUG_ERROR("Could not use an invalid socket description.");
return -1;
}
@@ -1230,27 +1217,27 @@ int tnet_get_peerip_n_port(tnet_fd_t localFD, tnet_ip_t *ip, tnet_port_t *port)
* @param getlocal Whether to get local or remote ip and port
* @param ip [out] The IP address of the local socket.
* @param port [out] The port of the local socket.
-
+
* @retval Zero if succeed and non-zero error code otherwise.
*/
int tnet_get_ip_n_port(tnet_fd_t fd, tsk_bool_t getlocal, tnet_ip_t *ip, tnet_port_t *port)
{
- if (port){
+ if (port) {
*port = 0;
}
-
- if (fd > 0){
+
+ if (fd > 0) {
int status;
struct sockaddr_storage ss;
status = getlocal ? tnet_getsockname(fd, &ss) : tnet_getpeername(fd, &ss);
- if (status){
+ if (status) {
TNET_PRINT_LAST_ERROR("TNET_GET_SOCKADDR has failed with status code: %d", status);
return -1;
}
-
+
return tnet_get_sockip_n_port(((struct sockaddr *)&ss), ip, port);
}
-
+
TSK_DEBUG_ERROR("Could not use an invalid socket description.");
return -1;
}
@@ -1310,7 +1297,7 @@ int tnet_get_fd_opened_count(tsk_size_t* count)
char buf[1024];
struct dirent *dp;
DIR *dir;
-
+
if (!count) {
TSK_DEBUG_ERROR("Invalid parameter");
return -1;
@@ -1457,7 +1444,7 @@ const char *tnet_inet_ntop(int af, const void *src, char *dst, int size)
}
addr.ss_family = AF_INET6;
((struct sockaddr_in6 *)&addr)->sin6_addr = *((struct in6_addr *)src);
-
+
if (tnet_get_sockip((const struct sockaddr *)&addr, &ip)) {
return tsk_null;
}
@@ -1495,29 +1482,29 @@ int tnet_sockfd_waitUntil(tnet_fd_t fd, long timeout, tsk_bool_t writable)
int ret = -1;
fd_set fds;
struct timeval timetowait;
-
- if (fd <= 0){
+
+ if (fd <= 0) {
goto bail;
}
-
- if (timeout >= 0){
+
+ if (timeout >= 0) {
timetowait.tv_sec = (timeout / 1000);
timetowait.tv_usec = (timeout % 1000) * 1000;
}
-
+
FD_ZERO(&fds);
FD_SET(fd, &fds);
-
+
ret = select(fd + 1, writable ? 0 : &fds, writable ? &fds : 0, 0, (timeout >= 0) ? &timetowait : 0);
-
- if (ret == 0){ /* timedout */
+
+ if (ret == 0) { /* timedout */
ret = -2;
}
- else if (ret == 1/* the total number of socket handles that are ready */){
+ else if (ret == 1/* the total number of socket handles that are ready */) {
ret = 0; // Ok
}
//else: error
-
+
bail:
return ret;
}
@@ -1530,21 +1517,21 @@ int tnet_sockfd_joingroup6(tnet_fd_t fd, const char* multiaddr, unsigned iface_i
int ret = -1;
//struct ipv6_mreq mreq6;
//struct sockaddr_storage ss;
-
+
//if((ret = tnet_sockaddr_init(multiaddr, 0, tnet_socket_type_udp_ipv6, &ss)))
//{
// return ret;
//}
-
+
//memcpy(&mreq6.ipv6mr_multiaddr, &((struct sockaddr_in6 *) &ss)->sin6_addr, sizeof(struct in6_addr));
//mreq6.ipv6mr_interface = iface_index;
-
+
//if((ret = setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (const char*)&mreq6, sizeof(mreq6))))
//{
// TNET_PRINT_LAST_ERROR("Failed to join IPv6 multicast group.");
// return ret;
//}
-
+
return ret;
}
/**@ingroup tnet_utils_group
@@ -1565,7 +1552,7 @@ int tnet_resolve(const char *fqdn, tnet_port_t port, tnet_socket_type_t type, tn
{
struct sockaddr_storage addr;
int ret = tnet_sockaddr_init(fqdn, port, type, &addr);
- if (ret == 0){
+ if (ret == 0) {
return tnet_get_sockip_n_port((const struct sockaddr *)&addr, out_ip, out_port);
}
return ret;
@@ -1581,26 +1568,26 @@ int tnet_sockaddrinfo_init(const char *host, tnet_port_t port, enum tnet_socket_
struct addrinfo *ptr = 0;
struct addrinfo hints;
tsk_istr_t p;
-
+
tsk_itoa(port, &p);
-
+
/* hints address info structure */
memset(&hints, 0, sizeof(hints));
hints.ai_family = TNET_SOCKET_TYPE_IS_IPV46(type) ? AF_UNSPEC : (TNET_SOCKET_TYPE_IS_IPV6(type) ? AF_INET6 : AF_INET);
hints.ai_socktype = TNET_SOCKET_TYPE_IS_STREAM(type) ? SOCK_STREAM : SOCK_DGRAM;
hints.ai_protocol = TNET_SOCKET_TYPE_IS_STREAM(type) ? IPPROTO_TCP : IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
-
+
/* Performs getaddrinfo */
- if ((status = tnet_getaddrinfo(host, p, &hints, &result))){
+ if ((status = tnet_getaddrinfo(host, p, &hints, &result))) {
TNET_PRINT_LAST_ERROR("getaddrinfo have failed.");
goto bail;
}
-
+
/* Find our address. */
- for (ptr = result; ptr; ptr = ptr->ai_next){
+ for (ptr = result; ptr; ptr = ptr->ai_next) {
/* Only IPv4 and IPv6 are supported */
- if (ptr->ai_family != AF_INET6 && ptr->ai_family != AF_INET){
+ if (ptr->ai_family != AF_INET6 && ptr->ai_family != AF_INET) {
continue;
}
/* duplicate addrinfo ==> Bad idea
@@ -1611,21 +1598,29 @@ int tnet_sockaddrinfo_init(const char *host, tnet_port_t port, enum tnet_socket_
(*ai)->ai_addrlen = ptr->ai_addrlen;
(*ai)->ai_next = 0;
(*ai)->ai_canonname = 0;*/
-
- if (ai_addr)memcpy(ai_addr, ptr->ai_addr, ptr->ai_addrlen);
- if (ai_family) *ai_family = ptr->ai_family;
- if (ai_socktype) *ai_socktype = ptr->ai_socktype;
- if (ai_protocol) *ai_protocol = ptr->ai_protocol;
-
+
+ if (ai_addr) {
+ memcpy(ai_addr, ptr->ai_addr, ptr->ai_addrlen);
+ }
+ if (ai_family) {
+ *ai_family = ptr->ai_family;
+ }
+ if (ai_socktype) {
+ *ai_socktype = ptr->ai_socktype;
+ }
+ if (ai_protocol) {
+ *ai_protocol = ptr->ai_protocol;
+ }
+
/* We prefer IPv4 but IPv6 can also work */
- if (ptr->ai_family == AF_INET){
+ if (ptr->ai_family == AF_INET) {
break;
}
}
-
+
bail:
tnet_freeaddrinfo(result);
-
+
return status;
}
@@ -1641,13 +1636,13 @@ int tnet_sockaddr_init(const char *host, tnet_port_t port, tnet_socket_type_t ty
{
int status;
struct sockaddr_storage ai_addr;
-
- if ((status = tnet_sockaddrinfo_init(host, port, type, &ai_addr, 0, 0, 0))){
+
+ if ((status = tnet_sockaddrinfo_init(host, port, type, &ai_addr, 0, 0, 0))) {
return status;
}
-
+
memcpy(addr, &ai_addr, sizeof(ai_addr));
-
+
return status;
}
@@ -1665,34 +1660,34 @@ int tnet_sockfd_init(const char *host, tnet_port_t port, enum tnet_socket_type_e
struct sockaddr_storage ai_addr;
int ai_family, ai_socktype, ai_protocol;
*fd = TNET_INVALID_SOCKET;
-
- if ((status = tnet_sockaddrinfo_init(host, port, type, &ai_addr, &ai_family, &ai_socktype, &ai_protocol))){
+
+ if ((status = tnet_sockaddrinfo_init(host, port, type, &ai_addr, &ai_family, &ai_socktype, &ai_protocol))) {
goto bail;
}
-
- if ((*fd = (tnet_fd_t)socket(ai_family, ai_socktype, ai_protocol)) == TNET_INVALID_SOCKET){
+
+ if ((*fd = (tnet_fd_t)socket(ai_family, ai_socktype, ai_protocol)) == TNET_INVALID_SOCKET) {
TNET_PRINT_LAST_ERROR("Failed to create new socket.");
goto bail;
}
-
+
#if TNET_USE_POLL || USE_POLL /* For win32 WSA* function the socket is auto. set to nonblocking mode. */
- if ((status = tnet_sockfd_set_nonblocking(*fd))){
+ if ((status = tnet_sockfd_set_nonblocking(*fd))) {
goto bail;
}
#endif
-
+
#if TNET_HAVE_SS_LEN
if((status = bind(*fd, (const struct sockaddr*)&ai_addr, ai_addr.ss_len)))
#else
- if ((status = bind(*fd, (const struct sockaddr*)&ai_addr, sizeof(ai_addr))))
+ if ((status = bind(*fd, (const struct sockaddr*)&ai_addr, sizeof(ai_addr))))
#endif
- {
- TNET_PRINT_LAST_ERROR("bind have failed.");
- tnet_sockfd_close(fd);
-
- goto bail;
- }
-
+ {
+ TNET_PRINT_LAST_ERROR("bind have failed.");
+ tnet_sockfd_close(fd);
+
+ goto bail;
+ }
+
bail:
return (*fd == TNET_INVALID_SOCKET) ? status : 0;
}
@@ -1705,8 +1700,7 @@ bail:
*/
int tnet_sockfd_set_mode(tnet_fd_t fd, int nonBlocking)
{
- if (fd != TNET_INVALID_FD)
- {
+ if (fd != TNET_INVALID_FD) {
#if TNET_UNDER_WINDOWS
ULONG mode = nonBlocking;
if (ioctlsocket(fd, FIONBIO, &mode))
@@ -1721,15 +1715,15 @@ int tnet_sockfd_set_mode(tnet_fd_t fd, int nonBlocking)
TNET_PRINT_LAST_ERROR("fcntl(F_GETFL) have failed.");
return -1;
}
- if(fcntl(fd, F_SETFL, flags | (nonBlocking ? O_NONBLOCK : ~O_NONBLOCK)) < 0){
+ if(fcntl(fd, F_SETFL, flags | (nonBlocking ? O_NONBLOCK : ~O_NONBLOCK)) < 0) {
TNET_PRINT_LAST_ERROR("fcntl(O_NONBLOCK/O_NONBLOCK) have failed.");
return -1;
}
#endif
-
+
// int on = 1;
// ioctl(fd, FIONBIO, (char *)&on);
-
+
}
return 0;
}
@@ -1775,7 +1769,7 @@ int tnet_sockfd_sendto(tnet_fd_t fd, const struct sockaddr *to, const void* buf,
{
tsk_size_t sent = 0;
int ret = -1;
-
+
if (fd == TNET_INVALID_FD) {
TSK_DEBUG_ERROR("Using invalid FD to send data.");
goto bail;
@@ -1785,7 +1779,7 @@ int tnet_sockfd_sendto(tnet_fd_t fd, const struct sockaddr *to, const void* buf,
ret = -2;
goto bail;
}
-
+
while (sent < size) {
int try_guard = 10;
#if TNET_UNDER_WINDOWS
@@ -1793,13 +1787,13 @@ int tnet_sockfd_sendto(tnet_fd_t fd, const struct sockaddr *to, const void* buf,
DWORD numberOfBytesSent = 0;
wsaBuffer.buf = ((CHAR*)buf) + sent;
wsaBuffer.len = (ULONG)(size - sent);
- try_again:
+try_again:
ret = WSASendTo(fd, &wsaBuffer, 1, &numberOfBytesSent, 0, to, tnet_get_sockaddr_size(to), 0, 0); // returns zero if succeed
if (ret == 0) {
ret = numberOfBytesSent;
}
#else
- try_again:
+try_again:
ret = sendto(fd, (((const uint8_t*)buf) + sent), (size - sent), 0, to, tnet_get_sockaddr_size(to)); // returns number of sent bytes if succeed
#endif
if (ret <= 0) {
@@ -1819,7 +1813,7 @@ int tnet_sockfd_sendto(tnet_fd_t fd, const struct sockaddr *to, const void* buf,
sent += ret;
}
}
-
+
bail:
return (int)((size == sent) ? sent : ret);
}
@@ -1838,12 +1832,12 @@ bail:
int tnet_sockfd_recvfrom(tnet_fd_t fd, void* buf, tsk_size_t size, int flags, struct sockaddr *from)
{
socklen_t fromlen;
-
- if (fd == TNET_INVALID_FD){
+
+ if (fd == TNET_INVALID_FD) {
TSK_DEBUG_ERROR("Using invalid FD to recv data.");
return -1;
}
-
+
fromlen = tnet_get_sockaddr_size(from);
return recvfrom(fd, (char*)buf, (int)size, flags, from, &fromlen);
}
@@ -1861,32 +1855,34 @@ tsk_size_t tnet_sockfd_send(tnet_fd_t fd, const void* buf, tsk_size_t size, int
{
int ret = -1;
tsk_size_t sent = 0;
-
- if (fd == TNET_INVALID_FD){
+
+ if (fd == TNET_INVALID_FD) {
TSK_DEBUG_ERROR("Using invalid FD to send data.");
goto bail;
}
-
- while (sent < size){
- if ((ret = send(fd, (((const char*)buf) + sent), (int)(size - sent), flags)) <= 0){
- if (tnet_geterrno() == TNET_ERROR_WOULDBLOCK){
- if ((ret = tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT))){
+
+ while (sent < size) {
+ if ((ret = send(fd, (((const char*)buf) + sent), (int)(size - sent), flags)) <= 0) {
+ if (tnet_geterrno() == TNET_ERROR_WOULDBLOCK) {
+ if ((ret = tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT))) {
break;
}
- else continue;
+ else {
+ continue;
+ }
}
- else{
+ else {
TNET_PRINT_LAST_ERROR("send failed");
// Under Windows XP if WSAGetLastError()==WSAEINTR then try to disable both the ICS and the Firewall
// More info about How to disable the ISC: http://support.microsoft.com/?scid=kb%3Ben-us%3B230112&x=6&y=11
goto bail;
}
}
- else{
+ else {
sent += ret;
}
}
-
+
bail:
//return (size == sent) ? sent : ret;
return sent;
@@ -1905,17 +1901,17 @@ bail:
int tnet_sockfd_recv(tnet_fd_t fd, void* buf, tsk_size_t size, int flags)
{
int ret = -1;
-
- if (fd == TNET_INVALID_FD){
+
+ if (fd == TNET_INVALID_FD) {
TSK_DEBUG_ERROR("Using invalid FD to recv data.");
goto bail;
}
-
- if ((ret = (int)recv(fd, (char*)buf, (int)size, flags)) <= 0){
+
+ if ((ret = (int)recv(fd, (char*)buf, (int)size, flags)) <= 0) {
TNET_PRINT_LAST_ERROR("recv failed.");
goto bail;
}
-
+
bail:
return ret;
}
@@ -1929,40 +1925,40 @@ bail:
int tnet_sockfd_connectto(tnet_fd_t fd, const struct sockaddr_storage *to)
{
int status = -1;
-
+
#if TNET_UNDER_WINDOWS
-
- if ((status = WSAConnect(fd, (LPSOCKADDR)to, sizeof(*to), NULL, NULL, NULL, NULL)) == SOCKET_ERROR){
+
+ if ((status = WSAConnect(fd, (LPSOCKADDR)to, sizeof(*to), NULL, NULL, NULL, NULL)) == SOCKET_ERROR) {
status = WSAGetLastError();
- if (status == TNET_ERROR_WOULDBLOCK || status == TNET_ERROR_ISCONN || status == TNET_ERROR_INTR || status == TNET_ERROR_INPROGRESS){
+ if (status == TNET_ERROR_WOULDBLOCK || status == TNET_ERROR_ISCONN || status == TNET_ERROR_INTR || status == TNET_ERROR_INPROGRESS) {
TSK_DEBUG_WARN("TNET_ERROR_WOULDBLOCK/TNET_ERROR_ISCONN/TNET_ERROR_INTR/TNET_ERROR_INPROGRESS -> you should use tnet_sockfd_waitUntilWritable() before trying to send data");
status = 0;
}
- else{
+ else {
TNET_PRINT_LAST_ERROR("WSAConnect have failed");
}
}
-
+
#else /* !TNET_UNDER_WINDOWS */
-
+
#if TNET_HAVE_SS_LEN
if ((status = connect(fd, (struct sockaddr*)to, to->ss_len)))
# else
- if((status = connect(fd, (struct sockaddr*)to, sizeof(*to))))
+ if((status = connect(fd, (struct sockaddr*)to, sizeof(*to))))
# endif
- {
- status = tnet_geterrno();
- if(status == TNET_ERROR_WOULDBLOCK || status == TNET_ERROR_ISCONN || status == TNET_ERROR_INPROGRESS || status == TNET_ERROR_EAGAIN){
- TSK_DEBUG_INFO("TNET_ERROR_WOULDBLOCK/TNET_ERROR_ISCONN/TNET_ERROR_INPROGRESS/TNET_ERROR_EAGAIN ==> use tnet_sockfd_waitUntilWritable.");
- status = 0;
- }
- else{
- TNET_PRINT_LAST_ERROR("connect have failed.");
- }
+ {
+ status = tnet_geterrno();
+ if(status == TNET_ERROR_WOULDBLOCK || status == TNET_ERROR_ISCONN || status == TNET_ERROR_INPROGRESS || status == TNET_ERROR_EAGAIN) {
+ TSK_DEBUG_INFO("TNET_ERROR_WOULDBLOCK/TNET_ERROR_ISCONN/TNET_ERROR_INPROGRESS/TNET_ERROR_EAGAIN ==> use tnet_sockfd_waitUntilWritable.");
+ status = 0;
}
-
+ else {
+ TNET_PRINT_LAST_ERROR("connect have failed.");
+ }
+ }
+
#endif /* TNET_UNDER_WINDOWS */
-
+
return status;
}
@@ -1970,10 +1966,12 @@ int tnet_sockfd_connectto(tnet_fd_t fd, const struct sockaddr_storage *to)
*/
int tnet_sockfd_listen(tnet_fd_t fd, int backlog)
{
- if (fd > 0){
+ if (fd > 0) {
return listen(fd, backlog);
}
- else return -1;
+ else {
+ return -1;
+ }
}
/**@ingroup tnet_utils_group
@@ -1981,11 +1979,11 @@ int tnet_sockfd_listen(tnet_fd_t fd, int backlog)
tnet_fd_t tnet_sockfd_accept(tnet_fd_t fd, struct sockaddr *addr, socklen_t *addrlen)
{
tnet_fd_t ret = TNET_INVALID_FD;
-
- if (fd > 0){
+
+ if (fd > 0) {
ret = (tnet_fd_t)accept(fd, addr, addrlen);
}
-
+
return ret;
}
@@ -1996,7 +1994,7 @@ tnet_fd_t tnet_sockfd_accept(tnet_fd_t fd, struct sockaddr *addr, socklen_t *add
*/
int tnet_sockfd_close(tnet_fd_t *fd)
{
- if (*fd != TNET_INVALID_FD){
+ if (*fd != TNET_INVALID_FD) {
int ret;
#if TNET_UNDER_WINDOWS
ret = closesocket(*fd);
@@ -2025,12 +2023,24 @@ int tnet_sockfd_shutdown(tnet_fd_t fd)
*/
tnet_proxy_type_t tnet_proxy_type_from_string(const char* type)
{
- if (tsk_striequals(type, "http")) return tnet_proxy_type_http;
- else if (tsk_striequals(type, "https")) return tnet_proxy_type_https;
- else if (tsk_striequals(type, "socks4")) return tnet_proxy_type_socks4;
- else if (tsk_striequals(type, "socks4a")) return tnet_proxy_type_socks4a;
- else if (tsk_striequals(type, "socks5")) return tnet_proxy_type_socks5;
- else return tnet_proxy_type_none;
+ if (tsk_striequals(type, "http")) {
+ return tnet_proxy_type_http;
+ }
+ else if (tsk_striequals(type, "https")) {
+ return tnet_proxy_type_https;
+ }
+ else if (tsk_striequals(type, "socks4")) {
+ return tnet_proxy_type_socks4;
+ }
+ else if (tsk_striequals(type, "socks4a")) {
+ return tnet_proxy_type_socks4a;
+ }
+ else if (tsk_striequals(type, "socks5")) {
+ return tnet_proxy_type_socks5;
+ }
+ else {
+ return tnet_proxy_type_none;
+ }
}
/**@ingroup tnet_utils_group
@@ -2038,12 +2048,18 @@ tnet_proxy_type_t tnet_proxy_type_from_string(const char* type)
const char* tnet_proxy_type_to_string(tnet_proxy_type_t type)
{
switch (type) {
- case tnet_proxy_type_http: return "http";
- case tnet_proxy_type_https: return "https";
- case tnet_proxy_type_socks4: return "socks4";
- case tnet_proxy_type_socks4a: return "socks4a";
- case tnet_proxy_type_socks5: return "socks5";
- default: return "none";
+ case tnet_proxy_type_http:
+ return "http";
+ case tnet_proxy_type_https:
+ return "https";
+ case tnet_proxy_type_socks4:
+ return "socks4";
+ case tnet_proxy_type_socks4a:
+ return "socks4a";
+ case tnet_proxy_type_socks5:
+ return "socks5";
+ default:
+ return "none";
}
}
@@ -2092,13 +2108,13 @@ const char* tnet_proxy_type_to_string(tnet_proxy_type_t type)
static tsk_object_t* tnet_interface_ctor(tsk_object_t * self, va_list * app)
{
tnet_interface_t *iface = (tnet_interface_t *)self;
- if (iface){
+ if (iface) {
const char* description = va_arg(*app, const char*);
const void* mac_address = va_arg(*app, const void*);
tsk_size_t mac_address_length = va_arg(*app, tsk_size_t);
-
+
iface->description = tsk_strdup(description);
- if ((iface->mac_address = (uint8_t*)tsk_calloc(mac_address_length, sizeof(uint8_t)))){
+ if ((iface->mac_address = (uint8_t*)tsk_calloc(mac_address_length, sizeof(uint8_t)))) {
memcpy(iface->mac_address, mac_address, mac_address_length);
}
iface->mac_address_length = mac_address_length;
@@ -2109,11 +2125,11 @@ static tsk_object_t* tnet_interface_ctor(tsk_object_t * self, va_list * app)
static tsk_object_t* tnet_interface_dtor(tsk_object_t * self)
{
tnet_interface_t *iface = (tnet_interface_t *)self;
- if (iface){
+ if (iface) {
TSK_FREE(iface->description);
TSK_FREE(iface->mac_address);
}
-
+
return self;
}
@@ -2121,16 +2137,19 @@ static int tnet_interface_cmp(const tsk_object_t *if1, const tsk_object_t *if2)
{
const tnet_interface_t *iface1 = (const tnet_interface_t *)if1;
const tnet_interface_t *iface2 = (const tnet_interface_t *)if2;
-
- if (iface1 && iface2){
+
+ if (iface1 && iface2) {
return tsk_stricmp(iface1->description, iface1->description);
}
- else if (!iface1 && !iface2) return 0;
- else return -1;
+ else if (!iface1 && !iface2) {
+ return 0;
+ }
+ else {
+ return -1;
+ }
}
-static const tsk_object_def_t tnet_interface_def_s =
-{
+static const tsk_object_def_t tnet_interface_def_s = {
sizeof(tnet_interface_t),
tnet_interface_ctor,
tnet_interface_dtor,
@@ -2147,7 +2166,7 @@ const tsk_object_def_t *tnet_interface_def_t = &tnet_interface_def_s;
static tsk_object_t* tnet_address_ctor(tsk_object_t * self, va_list * app)
{
tnet_address_t *address = (tnet_address_t *)self;
- if (address){
+ if (address) {
address->ip = tsk_strdup(va_arg(*app, const char*));
}
return self;
@@ -2156,10 +2175,10 @@ static tsk_object_t* tnet_address_ctor(tsk_object_t * self, va_list * app)
static tsk_object_t* tnet_address_dtor(tsk_object_t * self)
{
tnet_address_t *address = (tnet_address_t *)self;
- if (address){
+ if (address) {
TSK_FREE(address->ip);
}
-
+
return self;
}
@@ -2167,17 +2186,20 @@ static int tnet_address_cmp(const tsk_object_t *_a1, const tsk_object_t *_a2)
{
const tnet_address_t *a1 = (const tnet_address_t *)_a1;
const tnet_address_t *a2 = (const tnet_address_t *)_a2;
-
- if (a1 && a2){
+
+ if (a1 && a2) {
// to have AF_UNSPEC, AF_UNIX, AF_INET, ... first
return (a1->family - a2->family);
}
- else if (!a1 && !a2) return 0;
- else return -1;
+ else if (!a1 && !a2) {
+ return 0;
+ }
+ else {
+ return -1;
+ }
}
-static const tsk_object_def_t tnet_address_def_s =
-{
+static const tsk_object_def_t tnet_address_def_s = {
sizeof(tnet_address_t),
tnet_address_ctor,
tnet_address_dtor,
OpenPOWER on IntegriCloud