Skip to content

Instantly share code, notes, and snippets.

@MishraShivendra
Created April 14, 2019 15:11
Show Gist options
  • Save MishraShivendra/472143f3829cea1026e9fc1714586a85 to your computer and use it in GitHub Desktop.
Save MishraShivendra/472143f3829cea1026e9fc1714586a85 to your computer and use it in GitHub Desktop.
Diff between libnatpmp files and PR#15717
--- /libnatpmp/getgateway.c 2019-04-08 19:31:24.105869092 +0530
+++ ./src/natpmp/gateway.cpp 2019-04-12 20:40:12.101088488 +0530
@@ -1,48 +1,11 @@
-/* $Id: getgateway.c,v 1.24 2014/03/31 12:41:35 nanard Exp $ */
-/* libnatpmp
-
-Copyright (c) 2007-2014, Thomas BERNARD
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are met:
-
- * Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- * The name of the author may not be used to endorse or promote products
- derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
-LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGE.
-*/
#include <stdio.h>
#include <ctype.h>
-#ifndef _WIN32
+#ifndef WIN32
#include <netinet/in.h>
#endif
#if !defined(_MSC_VER)
#include <sys/param.h>
#endif
-/* There is no portable method to get the default route gateway.
- * So below are four (or five ?) differents functions implementing this.
- * Parsing /proc/net/route is for linux.
- * sysctl is the way to access such informations on BSD systems.
- * Many systems should provide route information through raw PF_ROUTE
- * sockets.
- * In MS Windows, default gateway is found by looking into the registry
- * or by using GetBestRoute(). */
#ifdef __linux__
#define USE_PROC_NET_ROUTE
#undef USE_SOCKET_ROUTE
@@ -67,7 +30,7 @@
#undef USE_SYSCTL_NET_ROUTE
#endif
-#ifdef _WIN32
+#ifdef WIN32
#undef USE_PROC_NET_ROUTE
#undef USE_SOCKET_ROUTE
#undef USE_SYSCTL_NET_ROUTE
@@ -87,24 +50,29 @@
#endif
#ifdef __HAIKU__
-#include <stdlib.h>
-#include <unistd.h>
-#include <net/if.h>
#include <sys/sockio.h>
#define USE_HAIKU_CODE
#endif
#ifdef USE_SYSCTL_NET_ROUTE
-#include <stdlib.h>
#include <sys/sysctl.h>
-#include <sys/socket.h>
-#include <net/route.h>
#endif
+
#ifdef USE_SOCKET_ROUTE
-#include <unistd.h>
#include <string.h>
-#include <sys/socket.h>
+#endif
+
+#if defined(USE_SOCKET_ROUTE) || defined(__HAIKU__)
+#include <unistd.h>
#include <net/if.h>
+#endif
+
+#if defined(USE_SYSCTL_NET_ROUTE) || defined(__HAIKU__)
+#include <stdlib.h>
+#endif
+
+#if defined(USE_SOCKET_ROUTE) || defined(USE_SYSCTL_NET_ROUTE)
+#include <sys/socket.h>
#include <net/route.h>
#endif
@@ -116,19 +84,29 @@
#endif
#ifdef USE_WIN32_CODE_2
-#include <winsock2.h>
#include <windows.h>
#include <iphlpapi.h>
#endif
-
-#include "getgateway.h"
-
-#ifndef _WIN32
+#include <gateway.h>
+#ifndef WIN32
#define SUCCESS (0)
#define FAILED (-1)
#endif
-
-#if defined(USE_PROC_NET_ROUTE)
+#ifdef USE_PROC_NET_ROUTE
+#include <sstream>
+#include <list>
+#include <algorithm>
+/*
+ * There is no portable method to get the default route gateway.
+ * So below are four (or five ?) different functions implementing this.
+ * Parsing /proc/net/route is for Linux.
+ * sysctl is the way to access such information on BSD systems.
+ * Many systems should provide route information through raw PF_ROUTE
+ * sockets.
+ * In MS Windows, default gateway is found by looking into the registry
+ * or by using GetBestRoute().
+ *
+ */
/*
parse /proc/net/route which is as follow :
@@ -141,32 +119,45 @@
One header line, and then one line by route by route table entry.
*/
-int getdefaultgateway(in_addr_t * addr)
+
+int TestIfWhiteSpace(char character)
+{
+ std::list<char> whiteSpaces = {'\f', '\n', '\r', '\t', '\v'};
+ return std::count(whiteSpaces.begin(), whiteSpaces.end(), character);
+}
+
+int GetDefaultGateway(in_addr_t * addr)
{
- unsigned long d, g;
- char buf[256];
+ int64_t d = 0;
+ int64_t g = 0;
+ char buf[256] = {0};
int line = 0;
- FILE * f;
- char * p;
- f = fopen("/proc/net/route", "r");
+ char * p = nullptr;
+ FILE* f = fopen("/proc/net/route", "r");
if(!f)
return FAILED;
while(fgets(buf, sizeof(buf), f)) {
if(line > 0) { /* skip the first line */
p = buf;
/* skip the interface name */
- while(*p && !isspace(*p))
+ while(*p && !TestIfWhiteSpace(*p))
p++;
- while(*p && isspace(*p))
+ while(*p && TestIfWhiteSpace(*p))
p++;
- if(sscanf(p, "%lx%lx", &d, &g)==2) {
- if(d == 0 && g != 0) { /* default */
- *addr = g;
+ // Want just the gateway column
+ std::string data(p);
+ data = data.substr(data.find('\t')+1);
+ data = data.substr(0, data.find('\t'));
+
+ std::stringstream dataParser;
+ dataParser<<std::hex<<data;
+ dataParser >> g >> d;
+ if(d == 0 && g != 0) {
+ *addr = (in_addr_t)g;
fclose(f);
return SUCCESS;
}
}
- }
line++;
}
/* default route not found ! */
@@ -174,34 +165,32 @@
fclose(f);
return FAILED;
}
+#endif /* #ifdef USE_PROC_NET_ROUTE */
+
-#elif defined(USE_SYSCTL_NET_ROUTE)
+#ifdef USE_SYSCTL_NET_ROUTE
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
-int getdefaultgateway(in_addr_t * addr)
+int GetDefaultGateway(in_addr_t * addr)
{
-#if 0
- /* net.route.0.inet.dump.0.0 ? */
- int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
- NET_RT_DUMP, 0, 0/*tableid*/};
-#endif
/* net.route.0.inet.flags.gateway */
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
NET_RT_FLAGS, RTF_GATEWAY};
- size_t l;
- char * buf, * p;
- struct rt_msghdr * rt;
- struct sockaddr * sa;
+ size_t l = 0;
+ char* buf = nullptr;
+ char* p = nullptr;
+ struct rt_msghdr * rt = {0};
+ struct sockaddr * sa = {0};
struct sockaddr * sa_tab[RTAX_MAX];
- int i;
+ int i = 0;
int r = FAILED;
if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) {
return FAILED;
}
if(l>0) {
- buf = malloc(l);
+ buf = (char*)malloc(l);
if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) {
free(buf);
return FAILED;
@@ -214,7 +203,7 @@
sa_tab[i] = sa;
sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len));
} else {
- sa_tab[i] = NULL;
+ sa_tab[i] = nullptr;
}
}
if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY))
@@ -230,9 +219,10 @@
}
return r;
}
+#endif
-#elif defined(USE_SOCKET_ROUTE)
+#ifdef USE_SOCKET_ROUTE
/* Thanks to Darren Kenny for this code */
#define NEXTADDR(w, u) \
if (rtm_addrs & (w)) {\
@@ -246,14 +236,14 @@
char m_space[512];
} m_rtmsg;
-int getdefaultgateway(in_addr_t *addr)
+int GetDefaultGateway(in_addr_t *addr)
{
- int s, seq, l, rtm_addrs, i;
- pid_t pid;
- struct sockaddr so_dst, so_mask;
+ int s = 0, seq = 0, l = 0, rtm_addrs = 0, i = 0;
+ pid_t pid = {0};
+ struct sockaddr so_dst = {0}, so_mask = {0};
char *cp = m_rtmsg.m_space;
- struct sockaddr *gate = NULL, *sa;
- struct rt_msghdr *msg_hdr;
+ struct sockaddr *gate = nullptr, *sa = nullptr;
+ struct rt_msghdr *msg_hdr = nullptr;
pid = getpid();
seq = 0;
@@ -307,17 +297,17 @@
}
- if (gate != NULL ) {
+ if (gate != nullptr ) {
*addr = ((struct sockaddr_in *)gate)->sin_addr.s_addr;
return SUCCESS;
} else {
return FAILED;
}
}
+#endif /* #ifdef USE_SOCKET_ROUTE */
-#elif defined(USE_WIN32_CODE)
-
-int getdefaultgateway(in_addr_t * addr)
+#ifdef USE_WIN32_CODE
+LIBSPEC int GetDefaultGateway(in_addr_t * addr)
{
HKEY networkCardsKey;
HKEY networkCardKey;
@@ -335,8 +325,6 @@
DWORD gatewayValueType = REG_MULTI_SZ;
int done = 0;
- //const char * networkCardsPath = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
- //const char * interfacesPath = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
#ifdef UNICODE
LPCTSTR networkCardsPath = L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
LPCTSTR interfacesPath = L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
@@ -386,7 +374,7 @@
// It may be possible to simply enumerate the interface folders until we find one with a DhcpDefaultGateway value.
// However, the technique used is the technique most cited on the web, and we assume it to be more correct.
- if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, // Open registry key or predifined key
+ if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, // Open registry key or predefined key
networkCardsPath, // Name of registry subkey to open
0, // Reserved - must be zero
KEY_READ, // Mask - desired access rights
@@ -408,11 +396,9 @@
}
// Figure out how many subfolders are within the NetworkCards folder
- RegQueryInfoKey(networkCardsKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
-
- //printf( "Number of subkeys: %u\n", (unsigned int)numSubKeys);
+ RegQueryInfoKey(networkCardsKey, nullptr, nullptr, nullptr, &numSubKeys, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
- // Enumrate through each subfolder within the NetworkCards folder
+ // Enumerate through each subfolder within the Network Cards folder
for(i = 0; i < numSubKeys && !done; i++)
{
keyNameLength = MAX_KEY_LENGTH;
@@ -420,28 +406,27 @@
i, // Index of subkey to retrieve
keyName, // Buffer that receives the name of the subkey
&keyNameLength, // Variable that receives the size of the above buffer
- NULL, // Reserved - must be NULL
- NULL, // Buffer that receives the class string
- NULL, // Variable that receives the size of the above buffer
- NULL)) // Variable that receives the last write time of subkey
+ nullptr, // Reserved - must be nullptr
+ nullptr, // Buffer that receives the class string
+ nullptr, // Variable that receives the size of the above buffer
+ nullptr)) // Variable that receives the last write time of sub key
{
if(RegOpenKeyEx(networkCardsKey, keyName, 0, KEY_READ, &networkCardKey) == ERROR_SUCCESS)
{
keyValueLength = MAX_VALUE_LENGTH;
if(ERROR_SUCCESS == RegQueryValueEx(networkCardKey, // Open registry key
STR_SERVICENAME, // Name of key to query
- NULL, // Reserved - must be NULL
+ nullptr, // Reserved - must be nullptr
&keyValueType, // Receives value type
(LPBYTE)keyValue, // Receives value
&keyValueLength)) // Receives value length in bytes
{
-// printf("keyValue: %s\n", keyValue);
if(RegOpenKeyEx(interfacesKey, keyValue, 0, KEY_READ, &interfaceKey) == ERROR_SUCCESS)
{
gatewayValueLength = MAX_VALUE_LENGTH;
if(ERROR_SUCCESS == RegQueryValueEx(interfaceKey, // Open registry key
STR_DHCPDEFAULTGATEWAY, // Name of key to query
- NULL, // Reserved - must be NULL
+ nullptr, // Reserved - must be nullptr
&gatewayValueType, // Receives value type
(LPBYTE)gatewayValue, // Receives value
&gatewayValueLength)) // Receives value length in bytes
@@ -449,13 +434,12 @@
// Check to make sure it's a string
if((gatewayValueType == REG_MULTI_SZ || gatewayValueType == REG_SZ) && (gatewayValueLength > 1))
{
- //printf("gatewayValue: %s\n", gatewayValue);
done = 1;
}
}
else if(ERROR_SUCCESS == RegQueryValueEx(interfaceKey, // Open registry key
STR_DEFAULTGATEWAY, // Name of key to query
- NULL, // Reserved - must be NULL
+ nullptr, // Reserved - must be nullptr
&gatewayValueType, // Receives value type
(LPBYTE)gatewayValue,// Receives value
&gatewayValueLength)) // Receives value length in bytes
@@ -463,7 +447,6 @@
// Check to make sure it's a string
if((gatewayValueType == REG_MULTI_SZ || gatewayValueType == REG_SZ) && (gatewayValueLength > 1))
{
- //printf("gatewayValue: %s\n", gatewayValue);
done = 1;
}
}
@@ -481,7 +464,7 @@
if(done)
{
#if UNICODE
- char tmp[32];
+ char tmp[32] = {0};
for(i = 0; i < 32; i++) {
tmp[i] = (char)gatewayValue[i];
if(!tmp[i])
@@ -497,10 +480,10 @@
return -1;
}
+#endif /* #ifdef USE_WIN32_CODE */
-#elif defined(USE_WIN32_CODE_2)
-
-int getdefaultgateway(in_addr_t *addr)
+#ifdef USE_WIN32_CODE_2
+int GetDefaultGateway(in_addr_t *addr)
{
MIB_IPFORWARDROW ip_forward;
memset(&ip_forward, 0, sizeof(ip_forward));
@@ -509,15 +492,15 @@
*addr = ip_forward.dwForwardNextHop;
return 0;
}
+#endif /* #ifdef USE_WIN32_CODE_2 */
-#elif defined(USE_HAIKU_CODE)
-
-int getdefaultgateway(in_addr_t *addr)
+#ifdef USE_HAIKU_CODE
+int GetDefaultGateway(in_addr_t *addr)
{
int fd, ret = -1;
- struct ifconf config;
- void *buffer = NULL;
- struct ifreq *interface;
+ struct ifconf config = {0};
+ void *buffer = nullptr;
+ struct ifreq *interface = nullptr;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return -1;
@@ -528,7 +511,7 @@
if (config.ifc_value < 1) {
goto fail; /* No routes */
}
- if ((buffer = malloc(config.ifc_value)) == NULL) {
+ if ((buffer = malloc(config.ifc_value)) == nullptr) {
goto fail;
}
config.ifc_len = config.ifc_value;
@@ -546,13 +529,13 @@
break;
}
intfSize = sizeof(route) + IF_NAMESIZE;
- if (route.destination != NULL) {
+ if (route.destination != nullptr) {
intfSize += route.destination->sa_len;
}
- if (route.mask != NULL) {
+ if (route.mask != nullptr) {
intfSize += route.mask->sa_len;
}
- if (route.gateway != NULL) {
+ if (route.gateway != nullptr) {
intfSize += route.gateway->sa_len;
}
interface = (struct ifreq *)((uint8_t *)interface + intfSize);
@@ -562,13 +545,4 @@
close(fd);
return ret;
}
-
-#else /* fallback */
-
-int getdefaultgateway(in_addr_t * addr)
-{
- (void)addr;
- return -1;
-}
-
-#endif
+#endif /* #ifdef USE_HAIKU_CODE */
--- /libnatpmp/natpmp.c 2019-04-08 19:31:24.105869092 +0530
+++ ./src/natpmp/natpmp.cpp 2019-04-12 20:40:12.125088401 +0530
@@ -1,41 +1,11 @@
-/* $Id: natpmp.c,v 1.18 2013/11/26 08:47:36 nanard Exp $ */
-/* libnatpmp
-Copyright (c) 2007-2018, Thomas BERNARD
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are met:
-
- * Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- * The name of the author may not be used to endorse or promote products
- derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
-LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGE.
-*/
#ifdef __linux__
#define _BSD_SOURCE 1
#endif
#include <string.h>
-#include <time.h>
-#if !defined(_MSC_VER)
-#include <sys/time.h>
-#endif
-#ifdef _WIN32
+
#include <errno.h>
+
+#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <io.h>
@@ -45,35 +15,57 @@
#ifndef ECONNREFUSED
#define ECONNREFUSED WSAECONNREFUSED
#endif
-#include "wingettimeofday.h"
-#define gettimeofday natpmp_gettimeofday
+#ifdef WIN32
+#if defined(_MSC_VER)
+#include <time.h>
+#else
+#include <sys/time.h>
+#endif
+int gettimeofday(struct timeval* p, void* tz /* IGNORED */);
+#endif
#else
-#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#define closesocket close
#endif
-#include "natpmp.h"
-#include "getgateway.h"
+#include <natpmp.h>
+#include <gateway.h>
#include <stdio.h>
-NATPMP_LIBSPEC int initnatpmp(natpmp_t * p, int forcegw, in_addr_t forcedgw)
+#ifdef _MSC_VER
+int gettimeofday(struct timeval* p, void* tz)
{
-#ifdef _WIN32
+ union {
+ long long ns100; /*time since 1 Jan 1601 in 100ns units */
+ FILETIME ft;
+ } _now;
+
+ if(!p)
+ return -1;
+ GetSystemTimeAsFileTime( &(_now.ft) );
+ p->tv_usec =(long)((_now.ns100 / 10LL) % 1000000LL );
+ p->tv_sec = (long)((_now.ns100-(116444736000000000LL))/10000000LL);
+ return 0;
+}
+#endif
+
+int InitNatPmp(natpmp_t * p, int forcegw, in_addr_t forcedgw)
+{
+#ifdef WIN32
u_long ioctlArg = 1;
#else
int flags;
#endif
- struct sockaddr_in addr;
if(!p)
return NATPMP_ERR_INVALIDARGS;
+ struct sockaddr_in addr;
memset(p, 0, sizeof(natpmp_t));
p->s = socket(PF_INET, SOCK_DGRAM, 0);
if(p->s < 0)
return NATPMP_ERR_SOCKETERROR;
-#ifdef _WIN32
+#ifdef WIN32
if(ioctlsocket(p->s, FIONBIO, &ioctlArg) == SOCKET_ERROR)
return NATPMP_ERR_FCNTLERROR;
#else
@@ -86,7 +78,7 @@
if(forcegw) {
p->gateway = forcedgw;
} else {
- if(getdefaultgateway(&(p->gateway)) < 0)
+ if(GetDefaultGateway(&(p->gateway)) < 0)
return NATPMP_ERR_CANNOTGETGATEWAY;
}
@@ -99,7 +91,7 @@
return 0;
}
-NATPMP_LIBSPEC int closenatpmp(natpmp_t * p)
+int CloseNatPmp(natpmp_t * p)
{
if(!p)
return NATPMP_ERR_INVALIDARGS;
@@ -110,31 +102,26 @@
int sendpendingrequest(natpmp_t * p)
{
- int r;
-/* struct sockaddr_in addr;*/
if(!p)
return NATPMP_ERR_INVALIDARGS;
-/* memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(NATPMP_PORT);
- addr.sin_addr.s_addr = p->gateway;
- r = (int)sendto(p->s, p->pending_request, p->pending_request_len, 0,
- (struct sockaddr *)&addr, sizeof(addr));*/
- r = (int)send(p->s, p->pending_request, p->pending_request_len, 0);
+#ifdef __linux__
+ int r = (int)send(p->s, p->pending_request, p->pending_request_len, 0);
+#else
+ int r = (int)send(p->s, (const char*)p->pending_request, p->pending_request_len, 0);
+#endif
+
return (r<0) ? NATPMP_ERR_SENDERR : r;
}
-int sendnatpmprequest(natpmp_t * p)
+int SendNatPmpRequest(natpmp_t * p)
{
- int n;
if(!p)
return NATPMP_ERR_INVALIDARGS;
- /* TODO : check if no request is already pending */
p->has_pending_request = 1;
p->try_number = 1;
- n = sendpendingrequest(p);
- gettimeofday(&p->retry_time, NULL); // check errors !
- p->retry_time.tv_usec += 250000; /* add 250ms */
+ int n = sendpendingrequest(p);
+ gettimeofday(&p->retry_time, nullptr);
+ p->retry_time.tv_usec += 250000;
if(p->retry_time.tv_usec >= 1000000) {
p->retry_time.tv_usec -= 1000000;
p->retry_time.tv_sec++;
@@ -142,14 +129,15 @@
return n;
}
-NATPMP_LIBSPEC int getnatpmprequesttimeout(natpmp_t * p, struct timeval * timeout)
+int GetNatPmpRequestTimeout(natpmp_t * p, struct timeval * timeout)
{
- struct timeval now;
if(!p || !timeout)
return NATPMP_ERR_INVALIDARGS;
if(!p->has_pending_request)
return NATPMP_ERR_NOPENDINGREQ;
- if(gettimeofday(&now, NULL) < 0)
+ struct timeval now;
+ memset(&now, 0, sizeof(now));
+ if(gettimeofday(&now, nullptr) < 0)
return NATPMP_ERR_GETTIMEOFDAYERR;
timeout->tv_sec = p->retry_time.tv_sec - now.tv_sec;
timeout->tv_usec = p->retry_time.tv_usec - now.tv_usec;
@@ -160,21 +148,18 @@
return 0;
}
-NATPMP_LIBSPEC int sendpublicaddressrequest(natpmp_t * p)
+int SendPublicAddressRequest(natpmp_t * p)
{
if(!p)
return NATPMP_ERR_INVALIDARGS;
- //static const unsigned char request[] = { 0, 0 };
p->pending_request[0] = 0;
p->pending_request[1] = 0;
p->pending_request_len = 2;
- // TODO: return 0 instead of sizeof(request) ??
- return sendnatpmprequest(p);
+ return SendNatPmpRequest(p);
}
-NATPMP_LIBSPEC int sendnewportmappingrequest(natpmp_t * p, int protocol,
- uint16_t privateport, uint16_t publicport,
- uint32_t lifetime)
+int SendNewPortMappingRequest(natpmp_t * p, int protocol, int16_t privateport,
+ int16_t publicport, int64_t lifetime)
{
if(!p || (protocol!=NATPMP_PROTOCOL_TCP && protocol!=NATPMP_PROTOCOL_UDP))
return NATPMP_ERR_INVALIDARGS;
@@ -182,36 +167,37 @@
p->pending_request[1] = protocol;
p->pending_request[2] = 0;
p->pending_request[3] = 0;
- /* break strict-aliasing rules :
- *((uint16_t *)(p->pending_request + 4)) = htons(privateport); */
p->pending_request[4] = (privateport >> 8) & 0xff;
p->pending_request[5] = privateport & 0xff;
- /* break stric-aliasing rules :
- *((uint16_t *)(p->pending_request + 6)) = htons(publicport); */
p->pending_request[6] = (publicport >> 8) & 0xff;
p->pending_request[7] = publicport & 0xff;
- /* break stric-aliasing rules :
- *((uint32_t *)(p->pending_request + 8)) = htonl(lifetime); */
p->pending_request[8] = (lifetime >> 24) & 0xff;
p->pending_request[9] = (lifetime >> 16) & 0xff;
p->pending_request[10] = (lifetime >> 8) & 0xff;
p->pending_request[11] = lifetime & 0xff;
p->pending_request_len = 12;
- return sendnatpmprequest(p);
+ return SendNatPmpRequest(p);
}
-NATPMP_LIBSPEC int readnatpmpresponse(natpmp_t * p, natpmpresp_t * response)
+int ReadNatPmpResponse(natpmp_t * p, natpmpresp_t * response)
{
- unsigned char buf[16];
+ unsigned char buf[16] = {0};
struct sockaddr_in addr;
+ memset(&addr, 0, sizeof(addr));
socklen_t addrlen = sizeof(addr);
- ssize_t n;
+ int n;
if(!p)
return NATPMP_ERR_INVALIDARGS;
+#ifdef __linux__
n = recvfrom(p->s, buf, sizeof(buf), 0,
(struct sockaddr *)&addr, &addrlen);
- if(n<0)
-#ifdef _WIN32
+#else
+ n = recvfrom(p->s, (char*)buf, sizeof(buf), 0,
+ (struct sockaddr *)&addr, &addrlen);
+#endif
+
+ if(n<0) {
+#ifdef WIN32
switch(WSAGetLastError()) {
#else
switch(errno) {
@@ -227,11 +213,11 @@
n = NATPMP_ERR_RECVFROM;
}
/* check that addr is correct (= gateway) */
- else if(addr.sin_addr.s_addr != p->gateway)
+ }else if(addr.sin_addr.s_addr != p->gateway) {
n = NATPMP_ERR_WRONGPACKETSOURCE;
- else {
- response->resultcode = ntohs(*((uint16_t *)(buf + 2)));
- response->epoch = ntohl(*((uint32_t *)(buf + 4)));
+ }else {
+ response->resultcode = ntohs(*((int16_t *)(buf + 2)));
+ response->epoch = ntohl(*((int64_t *)(buf + 4)));
if(buf[0] != 0)
n = NATPMP_ERR_UNSUPPORTEDVERSION;
else if(buf[1] < 128 || buf[1] > 130)
@@ -258,13 +244,12 @@
}
} else {
response->type = buf[1] & 0x7f;
- if(buf[1] == 128)
- //response->publicaddress.addr = *((uint32_t *)(buf + 8));
- response->pnu.publicaddress.addr.s_addr = *((uint32_t *)(buf + 8));
- else {
- response->pnu.newportmapping.privateport = ntohs(*((uint16_t *)(buf + 8)));
- response->pnu.newportmapping.mappedpublicport = ntohs(*((uint16_t *)(buf + 10)));
- response->pnu.newportmapping.lifetime = ntohl(*((uint32_t *)(buf + 12)));
+ if(buf[1] == 128) {
+ response->pnu.publicaddress.addr.s_addr = *((int64_t *)(buf + 8));
+ }else {
+ response->pnu.newportmapping.privateport = ntohs(*((int16_t *)(buf + 8)));
+ response->pnu.newportmapping.mappedpublicport = ntohs(*((int16_t *)(buf + 10)));
+ response->pnu.newportmapping.lifetime = ntohl(*((int64_t *)(buf + 12)));
}
n = 0;
}
@@ -272,27 +257,23 @@
return n;
}
-NATPMP_LIBSPEC int readnatpmpresponseorretry(natpmp_t * p, natpmpresp_t * response)
+int ReadNatPmpResponseOrRetry(natpmp_t * p, natpmpresp_t * response)
{
- int n;
if(!p || !response)
return NATPMP_ERR_INVALIDARGS;
if(!p->has_pending_request)
return NATPMP_ERR_NOPENDINGREQ;
- n = readnatpmpresponse(p, response);
+ int n = ReadNatPmpResponse(p, response);
if(n<0) {
if(n==NATPMP_TRYAGAIN) {
struct timeval now;
- gettimeofday(&now, NULL); // check errors !
+ gettimeofday(&now, nullptr);
if(timercmp(&now, &p->retry_time, >=)) {
int delay, r;
if(p->try_number >= 9) {
return NATPMP_ERR_NOGATEWAYSUPPORT;
}
- /*printf("retry! %d\n", p->try_number);*/
- delay = 250 * (1<<p->try_number); // ms
- /*for(i=0; i<p->try_number; i++)
- delay += delay;*/
+ delay = 250 * (1<<p->try_number);
p->retry_time.tv_sec += (delay / 1000);
p->retry_time.tv_usec += (delay % 1000) * 1000;
if(p->retry_time.tv_usec >= 1000000) {
@@ -311,22 +292,24 @@
return n;
}
-#ifdef ENABLE_STRNATPMPERR
-NATPMP_LIBSPEC const char * strnatpmperr(int r)
+const char * StrNatPmpErr(int r)
{
- const char * s;
+ const char *s = nullptr;
switch(r) {
+ case 0:
+ s = "No Error";
+ break;
case NATPMP_ERR_INVALIDARGS:
- s = "invalid arguments";
+ s = "Invalid arguments";
break;
case NATPMP_ERR_SOCKETERROR:
- s = "socket() failed";
+ s = "Socket() failed";
break;
case NATPMP_ERR_CANNOTGETGATEWAY:
- s = "cannot get default gateway ip address";
+ s = "Cannot get default gateway IP address";
break;
case NATPMP_ERR_CLOSEERR:
-#ifdef _WIN32
+#ifdef WIN32
s = "closesocket() failed";
#else
s = "close() failed";
@@ -336,16 +319,16 @@
s = "recvfrom() failed";
break;
case NATPMP_ERR_NOPENDINGREQ:
- s = "no pending request";
+ s = "No pending request";
break;
case NATPMP_ERR_NOGATEWAYSUPPORT:
- s = "the gateway does not support nat-pmp";
+ s = "The gateway does not support NAT-PMP";
break;
case NATPMP_ERR_CONNECTERR:
s = "connect() failed";
break;
case NATPMP_ERR_WRONGPACKETSOURCE:
- s = "packet not received from the default gateway";
+ s = "Packet not received from the default gateway";
break;
case NATPMP_ERR_SENDERR:
s = "send() failed";
@@ -357,27 +340,25 @@
s = "gettimeofday() failed";
break;
case NATPMP_ERR_UNSUPPORTEDVERSION:
- s = "unsupported nat-pmp version error from server";
+ s = "Unsupported NAT-PMP version error from server";
break;
case NATPMP_ERR_UNSUPPORTEDOPCODE:
- s = "unsupported nat-pmp opcode error from server";
+ s = "Unsupported NAT-PMP opcode error from server";
break;
case NATPMP_ERR_UNDEFINEDERROR:
- s = "undefined nat-pmp server error";
+ s = "Undefined NAT-PMP server error";
break;
case NATPMP_ERR_NOTAUTHORIZED:
- s = "not authorized";
+ s = "Not authorized";
break;
case NATPMP_ERR_NETWORKFAILURE:
- s = "network failure";
+ s = "Network failure";
break;
case NATPMP_ERR_OUTOFRESOURCES:
- s = "nat-pmp server out of resources";
+ s = "NAT-PMP server out of resources";
break;
default:
- s = "Unknown libnatpmp error";
+ s = "Unknown error";
}
return s;
}
-#endif
-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment