From fb606adfe6101694e46b0fe4af78dfb6260b0333 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 13 Dec 2007 17:04:34 +0000 Subject: [PATCH] update to libnatpmp-20071213 --- third-party/libnatpmp/README | 2 +- third-party/libnatpmp/getgateway.c | 242 ++++++++++------------------- third-party/libnatpmp/natpmp.h | 12 +- 3 files changed, 88 insertions(+), 168 deletions(-) diff --git a/third-party/libnatpmp/README b/third-party/libnatpmp/README index fda0a5484..f7df69f24 100644 --- a/third-party/libnatpmp/README +++ b/third-party/libnatpmp/README @@ -1,4 +1,4 @@ libnatpmp is written by Thomas Bernard. Its homepage is http://miniupnp.tuxfamily.org/libnatpmp.html -This code is from the libnatpmp-20071202 snapshot +This code is from the libnatpmp-20071213 snapshot diff --git a/third-party/libnatpmp/getgateway.c b/third-party/libnatpmp/getgateway.c index 35c5604f5..363822e04 100644 --- a/third-party/libnatpmp/getgateway.c +++ b/third-party/libnatpmp/getgateway.c @@ -1,162 +1,31 @@ -#include -#include -#include -#include -#if defined(BSD) || defined(__APPLE__) - -#include -#include -#include -#include -#include /* struct in_addr */ -#include -#include - -static uint8_t * -getroute( int * buflen ); -static int -parseroutes( uint8_t * buf, int len, struct in_addr * addr ); - -int -getdefaultgateway( struct in_addr * addr ) -{ - uint8_t * buf; - int len; - - buf = getroute( &len ); - if( NULL == buf ) - { - fprintf(stderr, "failed to get default route (BSD)" ); - return 1; - } - - len = parseroutes( buf, len, addr ); - free( buf ); - - return len; -} - -#ifndef SA_SIZE -#define ROUNDUP( a, size ) \ - ( ( (a) & ( (size) - 1 ) ) ? ( 1 + ( (a) | ( (size) - 1 ) ) ) : (a) ) -#define SA_SIZE( sap ) \ - ( sap->sa_len ? ROUNDUP( (sap)->sa_len, sizeof( u_long ) ) : \ - sizeof( u_long ) ) -#endif /* !SA_SIZE */ -#define NEXT_SA( sap ) \ - (struct sockaddr *) ( (caddr_t) (sap) + ( SA_SIZE( (sap) ) ) ) - -static uint8_t * -getroute( int * buflen ) -{ - int mib[6]; - size_t len; - uint8_t * buf; - - mib[0] = CTL_NET; - mib[1] = PF_ROUTE; - mib[2] = 0; - mib[3] = AF_INET; - mib[4] = NET_RT_FLAGS; - mib[5] = RTF_GATEWAY; - - if( sysctl( mib, 6, NULL, &len, NULL, 0 ) ) - { - if( ENOENT != errno ) - { - fprintf(stderr, "sysctl net.route.0.inet.flags.gateway failed (%s)", - strerror( errno ) ); - } - *buflen = 0; - return NULL; - } - - buf = malloc( len ); - if( NULL == buf ) - { - *buflen = 0; - return NULL; - } - - if( sysctl( mib, 6, buf, &len, NULL, 0 ) ) - { - fprintf(stderr, "sysctl net.route.0.inet.flags.gateway failed (%s)", - strerror( errno ) ); - free( buf ); - *buflen = 0; - return NULL; - } - - *buflen = len; - - return buf; -} - -static int -parseroutes( uint8_t * buf, int len, struct in_addr * addr ) -{ - uint8_t * end; - struct rt_msghdr * rtm; - struct sockaddr * sa; - struct sockaddr_in * sin; - int ii; - struct in_addr dest, gw; - - end = buf + len; - while( end > buf + sizeof( *rtm ) ) - { - rtm = (struct rt_msghdr *) buf; - buf += rtm->rtm_msglen; - if( end >= buf ) - { - dest.s_addr = INADDR_NONE; - gw.s_addr = INADDR_NONE; - sa = (struct sockaddr *) ( rtm + 1 ); - - for( ii = 0; ii < RTAX_MAX && (uint8_t *) sa < buf; ii++ ) - { - if( buf < (uint8_t *) NEXT_SA( sa ) ) - { - break; - } - - if( rtm->rtm_addrs & ( 1 << ii ) ) - { - if( AF_INET == sa->sa_family ) - { - sin = (struct sockaddr_in *) sa; - switch( ii ) - { - case RTAX_DST: - dest = sin->sin_addr; - break; - case RTAX_GATEWAY: - gw = sin->sin_addr; - break; - } - } - sa = NEXT_SA( sa ); - } - } - - if( INADDR_ANY == dest.s_addr && INADDR_NONE != gw.s_addr ) - { - *addr = gw; - return 0; - } - } - } - - return 1; -} - -#elif defined( linux ) || defined( __linux ) || defined( __linux__ ) - +/* $Id: getgateway.c,v 1.6 2007/12/13 14:46:06 nanard Exp $ */ +/* libnatpmp + * Copyright (c) 2007, Thomas BERNARD + * + * 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. */ #include #include #include #include +#if defined(BSD) || defined(__APPLE__) +#include +#include +#include +#include +#endif +#include "getgateway.h" +#ifdef __linux__ int getdefaultgateway(in_addr_t * addr) { long d, g; @@ -189,13 +58,60 @@ int getdefaultgateway(in_addr_t * addr) fclose(f); return -1; } - -#else /* not BSD or Linux */ - -int getdefaultgateway( struct in_addr * addr ) -{ - printf( "don't know how to get default route on this platform" ); - return 1; -} - +#endif + +#if defined(BSD) || defined(__APPLE__) + +#define ROUNDUP(a) \ + ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) + +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; + struct sockaddr * sa_tab[RTAX_MAX]; + int i; + int r = -1; + if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) { + return -1; + } + if(l>0) { + buf = malloc(l); + if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) { + return -1; + } + for(p=buf; prtm_msglen) { + rt = (struct rt_msghdr *)p; + sa = (struct sockaddr *)(rt + 1); + for(i=0; irtm_addrs & (1 << i)) { + sa_tab[i] = sa; + sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len)); + } else { + sa_tab[i] = NULL; + } + } + if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY)) + && sa_tab[RTAX_DST]->sa_family == AF_INET + && sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) { + if(((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) { + *addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr; + r = 0; + } + } + } + free(buf); + } + return r; +} #endif diff --git a/third-party/libnatpmp/natpmp.h b/third-party/libnatpmp/natpmp.h index a194b6457..a6647f6d1 100644 --- a/third-party/libnatpmp/natpmp.h +++ b/third-party/libnatpmp/natpmp.h @@ -1,4 +1,4 @@ -/* $Id: natpmp.h,v 1.3 2007/12/02 00:12:47 nanard Exp $ */ +/* $Id: natpmp.h,v 1.6 2007/12/13 14:55:16 nanard Exp $ */ /* libnatpmp * Copyright (c) 2007, Thomas BERNARD * @@ -20,7 +20,7 @@ #define NATPMP_PORT (5351) #include -#include /* struct timeval */ +#include #include typedef struct { @@ -52,13 +52,14 @@ typedef struct { /* possible values for type field of natpmpresp_t */ #define NATPMP_RESPTYPE_PUBLICADDRESS (0) -#define NATPMP_RESPTYPE_TCPPORTMAPPING (1) -#define NATPMP_RESPTYPE_UDPPORTMAPPING (2) +#define NATPMP_RESPTYPE_UDPPORTMAPPING (1) +#define NATPMP_RESPTYPE_TCPPORTMAPPING (2) /* Values to pass to sendnewportmappingrequest() */ #define NATPMP_PROTOCOL_UDP (1) #define NATPMP_PROTOCOL_TCP (2) +/* return values */ /* NATPMP_ERR_INVALIDARGS : invalid arguments passed to the function */ #define NATPMP_ERR_INVALIDARGS (-1) /* NATPMP_ERR_SOCKETERROR : socket() failed. check errno for details */ @@ -130,6 +131,9 @@ int sendpublicaddressrequest(natpmp_t * p); * Arguments : * protocol is either NATPMP_PROTOCOL_TCP or NATPMP_PROTOCOL_UDP, * lifetime is in seconds. + * To remove a port mapping, set lifetime to zero. + * To remove all port mappings to the host, set lifetime and both ports + * to zero. * Return values : * 12 = OK (size of the request) * NATPMP_ERR_INVALIDARGS