Changeset 4151 for trunk/third-party/libnatpmp
- Timestamp:
- Dec 13, 2007, 5:04:34 PM (14 years ago)
- Location:
- trunk/third-party/libnatpmp
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/third-party/libnatpmp/README
r4095 r4151 1 1 libnatpmp is written by Thomas Bernard. 2 2 Its homepage is http://miniupnp.tuxfamily.org/libnatpmp.html 3 This code is from the libnatpmp-200712 02snapshot3 This code is from the libnatpmp-20071213 snapshot 4 4 -
trunk/third-party/libnatpmp/getgateway.c
r4139 r4151 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <errno.h> 5 #if defined(BSD) || defined(__APPLE__) 6 7 #include <sys/types.h> 8 #include <sys/socket.h> 9 #include <netinet/in.h> 10 #include <arpa/inet.h> 11 #include <netinet/in.h> /* struct in_addr */ 12 #include <sys/sysctl.h> 13 #include <net/route.h> 14 15 static uint8_t * 16 getroute( int * buflen ); 17 static int 18 parseroutes( uint8_t * buf, int len, struct in_addr * addr ); 19 20 int 21 getdefaultgateway( struct in_addr * addr ) 22 { 23 uint8_t * buf; 24 int len; 25 26 buf = getroute( &len ); 27 if( NULL == buf ) 28 { 29 fprintf(stderr, "failed to get default route (BSD)" ); 30 return 1; 31 } 32 33 len = parseroutes( buf, len, addr ); 34 free( buf ); 35 36 return len; 37 } 38 39 #ifndef SA_SIZE 40 #define ROUNDUP( a, size ) \ 41 ( ( (a) & ( (size) - 1 ) ) ? ( 1 + ( (a) | ( (size) - 1 ) ) ) : (a) ) 42 #define SA_SIZE( sap ) \ 43 ( sap->sa_len ? ROUNDUP( (sap)->sa_len, sizeof( u_long ) ) : \ 44 sizeof( u_long ) ) 45 #endif /* !SA_SIZE */ 46 #define NEXT_SA( sap ) \ 47 (struct sockaddr *) ( (caddr_t) (sap) + ( SA_SIZE( (sap) ) ) ) 48 49 static uint8_t * 50 getroute( int * buflen ) 51 { 52 int mib[6]; 53 size_t len; 54 uint8_t * buf; 55 56 mib[0] = CTL_NET; 57 mib[1] = PF_ROUTE; 58 mib[2] = 0; 59 mib[3] = AF_INET; 60 mib[4] = NET_RT_FLAGS; 61 mib[5] = RTF_GATEWAY; 62 63 if( sysctl( mib, 6, NULL, &len, NULL, 0 ) ) 64 { 65 if( ENOENT != errno ) 66 { 67 fprintf(stderr, "sysctl net.route.0.inet.flags.gateway failed (%s)", 68 strerror( errno ) ); 69 } 70 *buflen = 0; 71 return NULL; 72 } 73 74 buf = malloc( len ); 75 if( NULL == buf ) 76 { 77 *buflen = 0; 78 return NULL; 79 } 80 81 if( sysctl( mib, 6, buf, &len, NULL, 0 ) ) 82 { 83 fprintf(stderr, "sysctl net.route.0.inet.flags.gateway failed (%s)", 84 strerror( errno ) ); 85 free( buf ); 86 *buflen = 0; 87 return NULL; 88 } 89 90 *buflen = len; 91 92 return buf; 93 } 94 95 static int 96 parseroutes( uint8_t * buf, int len, struct in_addr * addr ) 97 { 98 uint8_t * end; 99 struct rt_msghdr * rtm; 100 struct sockaddr * sa; 101 struct sockaddr_in * sin; 102 int ii; 103 struct in_addr dest, gw; 104 105 end = buf + len; 106 while( end > buf + sizeof( *rtm ) ) 107 { 108 rtm = (struct rt_msghdr *) buf; 109 buf += rtm->rtm_msglen; 110 if( end >= buf ) 111 { 112 dest.s_addr = INADDR_NONE; 113 gw.s_addr = INADDR_NONE; 114 sa = (struct sockaddr *) ( rtm + 1 ); 115 116 for( ii = 0; ii < RTAX_MAX && (uint8_t *) sa < buf; ii++ ) 117 { 118 if( buf < (uint8_t *) NEXT_SA( sa ) ) 119 { 120 break; 121 } 122 123 if( rtm->rtm_addrs & ( 1 << ii ) ) 124 { 125 if( AF_INET == sa->sa_family ) 126 { 127 sin = (struct sockaddr_in *) sa; 128 switch( ii ) 129 { 130 case RTAX_DST: 131 dest = sin->sin_addr; 132 break; 133 case RTAX_GATEWAY: 134 gw = sin->sin_addr; 135 break; 136 } 137 } 138 sa = NEXT_SA( sa ); 139 } 140 } 141 142 if( INADDR_ANY == dest.s_addr && INADDR_NONE != gw.s_addr ) 143 { 144 *addr = gw; 145 return 0; 146 } 147 } 148 } 149 150 return 1; 151 } 152 153 #elif defined( linux ) || defined( __linux ) || defined( __linux__ ) 154 1 /* $Id: getgateway.c,v 1.6 2007/12/13 14:46:06 nanard Exp $ */ 2 /* libnatpmp 3 * Copyright (c) 2007, Thomas BERNARD <miniupnp@free.fr> 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 155 16 #include <stdio.h> 156 17 #include <ctype.h> 157 18 #include <netinet/in.h> 158 19 #include <sys/param.h> 20 #if defined(BSD) || defined(__APPLE__) 21 #include <stdlib.h> 22 #include <sys/sysctl.h> 23 #include <sys/socket.h> 24 #include <net/route.h> 25 #endif 26 #include "getgateway.h" 159 27 28 #ifdef __linux__ 160 29 int getdefaultgateway(in_addr_t * addr) 161 30 { … … 190 59 return -1; 191 60 } 61 #endif 192 62 193 # else /* not BSD or Linux */63 #if defined(BSD) || defined(__APPLE__) 194 64 195 int getdefaultgateway( struct in_addr * addr ) 65 #define ROUNDUP(a) \ 66 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 67 68 int getdefaultgateway(in_addr_t * addr) 196 69 { 197 printf( "don't know how to get default route on this platform" ); 198 return 1; 70 #if 0 71 /* net.route.0.inet.dump.0.0 ? */ 72 int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET, 73 NET_RT_DUMP, 0, 0/*tableid*/}; 74 #endif 75 /* net.route.0.inet.flags.gateway */ 76 int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET, 77 NET_RT_FLAGS, RTF_GATEWAY}; 78 size_t l; 79 char * buf, * p; 80 struct rt_msghdr * rt; 81 struct sockaddr * sa; 82 struct sockaddr * sa_tab[RTAX_MAX]; 83 int i; 84 int r = -1; 85 if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) { 86 return -1; 87 } 88 if(l>0) { 89 buf = malloc(l); 90 if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) { 91 return -1; 92 } 93 for(p=buf; p<buf+l; p+=rt->rtm_msglen) { 94 rt = (struct rt_msghdr *)p; 95 sa = (struct sockaddr *)(rt + 1); 96 for(i=0; i<RTAX_MAX; i++) { 97 if(rt->rtm_addrs & (1 << i)) { 98 sa_tab[i] = sa; 99 sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len)); 100 } else { 101 sa_tab[i] = NULL; 102 } 103 } 104 if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY)) 105 && sa_tab[RTAX_DST]->sa_family == AF_INET 106 && sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) { 107 if(((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) { 108 *addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr; 109 r = 0; 110 } 111 } 112 } 113 free(buf); 114 } 115 return r; 199 116 } 200 201 117 #endif -
trunk/third-party/libnatpmp/natpmp.h
r4141 r4151 1 /* $Id: natpmp.h,v 1. 3 2007/12/02 00:12:47nanard Exp $ */1 /* $Id: natpmp.h,v 1.6 2007/12/13 14:55:16 nanard Exp $ */ 2 2 /* libnatpmp 3 3 * Copyright (c) 2007, Thomas BERNARD <miniupnp@free.fr> … … 21 21 22 22 #include <time.h> 23 #include <sys/time.h> /* struct timeval */23 #include <sys/time.h> 24 24 #include <netinet/in.h> 25 25 … … 53 53 /* possible values for type field of natpmpresp_t */ 54 54 #define NATPMP_RESPTYPE_PUBLICADDRESS (0) 55 #define NATPMP_RESPTYPE_ TCPPORTMAPPING (1)56 #define NATPMP_RESPTYPE_ UDPPORTMAPPING (2)55 #define NATPMP_RESPTYPE_UDPPORTMAPPING (1) 56 #define NATPMP_RESPTYPE_TCPPORTMAPPING (2) 57 57 58 58 /* Values to pass to sendnewportmappingrequest() */ … … 60 60 #define NATPMP_PROTOCOL_TCP (2) 61 61 62 /* return values */ 62 63 /* NATPMP_ERR_INVALIDARGS : invalid arguments passed to the function */ 63 64 #define NATPMP_ERR_INVALIDARGS (-1) … … 131 132 * protocol is either NATPMP_PROTOCOL_TCP or NATPMP_PROTOCOL_UDP, 132 133 * lifetime is in seconds. 134 * To remove a port mapping, set lifetime to zero. 135 * To remove all port mappings to the host, set lifetime and both ports 136 * to zero. 133 137 * Return values : 134 138 * 12 = OK (size of the request)
Note: See TracChangeset
for help on using the changeset viewer.