-$Id: Changelog.txt,v 1.16 2009/07/13 08:36:02 nanard Exp $
+$Id: Changelog.txt,v 1.21 2010/02/02 18:24:43 nanard Exp $
+
+2010/02/02:
+ Fixed compilation under Mac OS X
+
+2009/12/19:
+ improve and fix building under Windows.
+ Project files for MS Visual Studio 2008
+ More simple (and working) code for Win32.
+ More checks in the /proc/net/route parsing. Add some comments.
+
+2009/08/04:
+ improving getgateway.c for windows
2009/07/13:
Adding Haiku code in getgateway.c
-libnatpmp
-Copyright (c) 2007, Thomas BERNARD <miniupnp@free.fr>
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+Copyright (c) 2007-2009, 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.
+
-libnatpmp is written by Thomas Bernard.
-Its homepage is http://miniupnp.tuxfamily.org/libnatpmp.html
-This code is from the libnatpmp-20090713 snapshot
+libnatpmp (c) 2007-2009 Thomas Bernard
+contact : miniupnp@free.fr
+
+see http://miniupnp.free.fr/libnatpmp.html
+or http://miniupnp.tuxfamily.org/libnatpmp.html
+for some documentation and code samples.
-/* $Id: getgateway.c,v 1.15 2009/07/13 08:36:02 nanard Exp $ */
+/* $Id: getgateway.c,v 1.19 2009/12/19 15:20:45 nanard Exp $ */
/* libnatpmp
- * Copyright (c) 2007-2008, Thomas BERNARD <miniupnp@free.fr>
+ * Copyright (c) 2007-2009, Thomas BERNARD <miniupnp@free.fr>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
#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 three differents functions implementing this.
+ * 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. */
+ * 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
#undef USE_PROC_NET_ROUTE
#undef USE_SOCKET_ROUTE
#undef USE_SYSCTL_NET_ROUTE
-#define USE_WIN32_CODE
+//#define USE_WIN32_CODE
+#define USE_WIN32_CODE_2
#endif
#ifdef __CYGWIN__
#include <net/if.h>
#include <net/route.h>
#endif
-#ifdef WIN32
+
+#ifdef USE_WIN32_CODE
#include <unknwn.h>
#include <winreg.h>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_LENGTH 16383
#endif
+
+#ifdef USE_WIN32_CODE_2
+#include <windows.h>
+#include <iphlpapi.h>
+#endif
+
#include "getgateway.h"
#ifndef WIN32
#endif
#ifdef USE_PROC_NET_ROUTE
+/*
+ parse /proc/net/route which is as follow :
+
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+wlan0 0001A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
+eth0 0000FEA9 00000000 0001 0 0 0 0000FFFF 0 0 0
+wlan0 00000000 0101A8C0 0003 0 0 0 00000000 0 0 0
+eth0 00000000 00000000 0001 0 0 1000 00000000 0 0 0
+
+ One header line, and then one line by route by route table entry.
+*/
int getdefaultgateway(in_addr_t * addr)
{
- long d, g;
+ unsigned long d, g;
char buf[256];
int line = 0;
FILE * f;
if(!f)
return FAILED;
while(fgets(buf, sizeof(buf), f)) {
- if(line > 0) {
+ if(line > 0) { /* skip the first line */
p = buf;
+ /* skip the interface name */
while(*p && !isspace(*p))
p++;
while(*p && isspace(*p))
p++;
if(sscanf(p, "%lx%lx", &d, &g)==2) {
- if(d == 0) { /* default */
+ if(d == 0 && g != 0) { /* default */
*addr = g;
fclose(f);
return SUCCESS;
DWORD gatewayValueType = REG_MULTI_SZ;
int done = 0;
- char networkCardsPath[] = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
- char interfacesPath[] = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
-
+ //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";
+#define STR_SERVICENAME L"ServiceName"
+#define STR_DHCPDEFAULTGATEWAY L"DhcpDefaultGateway"
+#define STR_DEFAULTGATEWAY L"DefaultGateway"
+#else
+ LPCTSTR networkCardsPath = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
+ LPCTSTR interfacesPath = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
+#define STR_SERVICENAME "ServiceName"
+#define STR_DHCPDEFAULTGATEWAY "DhcpDefaultGateway"
+#define STR_DEFAULTGATEWAY "DefaultGateway"
+#endif
// The windows registry lists its primary network devices in the following location:
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards
//
{
keyValueLength = MAX_VALUE_LENGTH;
if(ERROR_SUCCESS == RegQueryValueEx(networkCardKey, // Open registry key
- "ServiceName", // Name of key to query
+ STR_SERVICENAME, // Name of key to query
NULL, // Reserved - must be NULL
&keyValueType, // Receives value type
- keyValue, // Receives value
+ (LPBYTE)keyValue, // Receives value
&keyValueLength)) // Receives value length in bytes
{
- //printf("keyValue: %s\n", keyValue);
-
+// 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
- "DhcpDefaultGateway", // Name of key to query
+ STR_DHCPDEFAULTGATEWAY, // Name of key to query
NULL, // Reserved - must be NULL
&gatewayValueType, // Receives value type
- gatewayValue, // Receives value
+ (LPBYTE)gatewayValue, // Receives value
&gatewayValueLength)) // Receives value length in bytes
{
// Check to make sure it's a string
- if(gatewayValueType == REG_MULTI_SZ || gatewayValueType == REG_SZ)
+ 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
- "DefaultGateway", // Name of key to query
+ STR_DEFAULTGATEWAY, // Name of key to query
NULL, // Reserved - must be NULL
&gatewayValueType, // Receives value type
- gatewayValue, // Receives value
+ (LPBYTE)gatewayValue,// Receives value
&gatewayValueLength)) // Receives value length in bytes
{
// Check to make sure it's a string
- if(gatewayValueType == REG_MULTI_SZ || gatewayValueType == REG_SZ)
+ if((gatewayValueType == REG_MULTI_SZ || gatewayValueType == REG_SZ) && (gatewayValueLength > 1))
{
//printf("gatewayValue: %s\n", gatewayValue);
done = 1;
if(done)
{
+#if UNICODE
+ char tmp[32];
+ for(i = 0; i < 32; i++) {
+ tmp[i] = (char)gatewayValue[i];
+ if(!tmp[i])
+ break;
+ }
+ tmp[31] = '\0';
+ *addr = inet_addr(tmp);
+#else
*addr = inet_addr(gatewayValue);
+#endif
return 0;
}
}
#endif /* #ifdef USE_WIN32_CODE */
+#ifdef USE_WIN32_CODE_2
+int getdefaultgateway(in_addr_t *addr)
+{
+ MIB_IPFORWARDROW ip_forward;
+ memset(&ip_forward, 0, sizeof(ip_forward));
+ if(GetBestRoute(inet_addr("0.0.0.0"), 0, &ip_forward) != NO_ERROR)
+ return -1;
+ *addr = ip_forward.dwForwardNextHop;
+ return 0;
+}
+#endif /* #ifdef USE_WIN32_CODE_2 */
+
#ifdef USE_HAIKU_CODE
int getdefaultgateway(in_addr_t *addr)
{
-/* $Id: getgateway.h,v 1.3 2008/07/02 22:33:06 nanard Exp $ */
+/* $Id: getgateway.h,v 1.4 2009/12/19 12:00:00 nanard Exp $ */
/* libnatpmp
* Copyright (c) 2007, Thomas BERNARD <miniupnp@free.fr>
*
#define __GETGATEWAY_H__
#ifdef WIN32
+#if !defined(_MSC_VER)
#include <stdint.h>
+#else
+typedef unsigned long uint32_t;
+typedef unsigned short uint16_t;
+#endif
#define in_addr_t uint32_t
#endif
#include "declspec.h"
-/* $Id: natpmp.c,v 1.9 2009/06/04 22:27:53 nanard Exp $ */
+/* $Id: natpmp.c,v 1.12 2009/12/19 14:10:09 nanard Exp $ */
/* libnatpmp
- * Copyright (c) 2007-2008, Thomas BERNARD <miniupnp@free.fr>
+ * Copyright (c) 2007-2009, Thomas BERNARD <miniupnp@free.fr>
* http://miniupnp.free.fr/libnatpmp.html
*
* Permission to use, copy, modify, and/or distribute this software for any
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+#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>
#include <winsock2.h>
#include <io.h>
#define EWOULDBLOCK WSAEWOULDBLOCK
#define ECONNREFUSED WSAECONNREFUSED
+#include "wingettimeofday.h"
#else
#include <errno.h>
#include <unistd.h>
return 0;
}
-static int sendpendingrequest(natpmp_t * p)
+int sendpendingrequest(natpmp_t * p)
{
int r;
/* struct sockaddr_in addr;*/
return (r<0) ? NATPMP_ERR_SENDERR : r;
}
-static int sendnatpmprequest(natpmp_t * p)
+int sendnatpmprequest(natpmp_t * p)
{
int n;
if(!p)
-/* $Id: natpmp.h,v 1.11 2009/02/27 22:38:05 nanard Exp $ */
+/* $Id: natpmp.h,v 1.12 2009/12/19 12:00:00 nanard Exp $ */
/* libnatpmp
* Copyright (c) 2007-2008, Thomas BERNARD <miniupnp@free.fr>
* http://miniupnp.free.fr/libnatpmp.html
#define NATPMP_PORT (5351)
#include <time.h>
+#if !defined(_MSC_VER)
#include <sys/time.h>
+#endif
#ifdef WIN32
#include <winsock2.h>
+#if !defined(_MSC_VER)
#include <stdint.h>
+#else
+typedef unsigned long uint32_t;
+typedef unsigned short uint16_t;
+#endif
#define in_addr_t uint32_t
#include "declspec.h"
#else
--- /dev/null
+/* $Id: wingettimeofday.c,v 1.3 2009/12/19 12:00:00 nanard Exp $ */
+/* libnatpmp
+ * Copyright (c) 2007-2008, Thomas BERNARD <miniupnp@free.fr>
+ * http://miniupnp.free.fr/libnatpmp.html
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+#ifdef WIN32
+#if defined(_MSC_VER)
+struct timeval {
+ long tv_sec;
+ long tv_usec;
+};
+#else
+#include <sys/time.h>
+#endif
+
+typedef struct _FILETIME {
+ unsigned long dwLowDateTime;
+ unsigned long dwHighDateTime;
+} FILETIME;
+
+void __stdcall GetSystemTimeAsFileTime(FILETIME*);
+
+//int gettimeofday(struct timeval* p, void* tz /* IGNORED */);
+
+int gettimeofday(struct timeval* p, void* tz /* IGNORED */) {
+ 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
+
--- /dev/null
+/* $Id: wingettimeofday.h,v 1.1 2009/12/19 12:02:42 nanard Exp $ */
+/* libnatpmp
+ * Copyright (c) 2007-2008, Thomas BERNARD <miniupnp@free.fr>
+ * http://miniupnp.free.fr/libnatpmp.html
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+#ifndef __WINGETTIMEOFDAY_H__
+#define __WINGETTIMEOFDAY_H__
+#ifdef WIN32
+#if defined(_MSC_VER)
+#include <time.h>
+#else
+#include <sys/time.h>
+#endif
+int gettimeofday(struct timeval* p, void* tz /* IGNORED */);
+#endif
+#endif