Changeset 6268


Ignore:
Timestamp:
Jun 30, 2008, 7:51:26 PM (13 years ago)
Author:
charles
Message:

upgrade to libnatpmp-20080630

Location:
trunk/third-party/libnatpmp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/third-party/libnatpmp/README

    r5970 r6268  
    11libnatpmp is written by Thomas Bernard.
    22Its homepage is http://miniupnp.tuxfamily.org/libnatpmp.html
    3 This code is from the libnatpmp-20080529 snapshot
     3This code is from the libnatpmp-20080630 snapshot
    44
  • trunk/third-party/libnatpmp/getgateway.c

    r4151 r6268  
    1 /* $Id: getgateway.c,v 1.6 2007/12/13 14:46:06 nanard Exp $ */
     1/* $Id: getgateway.c,v 1.8 2008/06/30 14:15:40 nanard Exp $ */
    22/* libnatpmp
    3  * Copyright (c) 2007, Thomas BERNARD <miniupnp@free.fr>
     3 * Copyright (c) 2007-2008, Thomas BERNARD <miniupnp@free.fr>
    44 *
    55 * Permission to use, copy, modify, and/or distribute this software for any
     
    1818#include <netinet/in.h>
    1919#include <sys/param.h>
    20 #if defined(BSD) || defined(__APPLE__)
     20/* There is no portable method to get the default route gateway.
     21 * So below are three differents functions implementing this.
     22 * Parsing /proc/net/route is for linux.
     23 * sysctl is the way to access such informations on BSD systems.
     24 * Many systems should provide route information through raw PF_ROUTE
     25 * sockets. */
     26#ifdef __linux__
     27#define USE_PROC_NET_ROUTE
     28#endif
     29
     30#ifdef BSD
     31/*#define USE_SYSCTL_NET_ROUTE*/
     32#define USE_SOCKET_ROUTE
     33#endif
     34
     35#ifdef __APPLE__
     36#define USE_SYSCTL_NET_ROUTE
     37#endif
     38
     39#if (defined(sun) && defined(__SVR4))
     40#define USE_SOCKET_ROUTE
     41#endif
     42
     43#ifdef USE_SYSCTL_NET_ROUTE
    2144#include <stdlib.h>
    2245#include <sys/sysctl.h>
     
    2447#include <net/route.h>
    2548#endif
     49#ifdef USE_SOCKET_ROUTE
     50#include <unistd.h>
     51#include <string.h>
     52#include <sys/socket.h>
     53#include <net/if.h>
     54#include <net/route.h>
     55#endif
    2656#include "getgateway.h"
    2757
    28 #ifdef __linux__
     58#define SUCCESS (0)
     59#define FAILED  (-1)
     60
     61#ifdef USE_PROC_NET_ROUTE
    2962int getdefaultgateway(in_addr_t * addr)
    3063{
     
    3669        f = fopen("/proc/net/route", "r");
    3770        if(!f)
    38                 return -1;
     71                return FAILED;
    3972        while(fgets(buf, sizeof(buf), f)) {
    4073                if(line > 0) {
     
    4881                                        *addr = g;
    4982                                        fclose(f);
    50                                         return 0;
     83                                        return SUCCESS;
    5184                                }
    5285                        }
     
    5487                line++;
    5588        }
    56         /* not found ! */
     89        /* default route not found ! */
    5790        if(f)
    5891                fclose(f);
    59         return -1;
     92        return FAILED;
    6093}
    61 #endif
    62 
    63 #if defined(BSD) || defined(__APPLE__)
     94#endif /* #ifdef USE_PROC_NET_ROUTE */
     95
     96
     97#ifdef USE_SYSCTL_NET_ROUTE
    6498
    6599#define ROUNDUP(a) \
     
    82116        struct sockaddr * sa_tab[RTAX_MAX];
    83117        int i;
    84         int r = -1;
     118        int r = FAILED;
    85119        if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) {
    86                 return -1;
     120                return FAILED;
    87121        }
    88122        if(l>0) {
    89123                buf = malloc(l);
    90124                if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) {
    91                         return -1;
     125                        return FAILED;
    92126                }
    93127                for(p=buf; p<buf+l; p+=rt->rtm_msglen) {
     
    107141                                if(((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) {
    108142                                        *addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr;
    109                                         r = 0;
     143                                        r = SUCCESS;
    110144                                }
    111145                        }
     
    115149        return r;
    116150}
    117 #endif
     151#endif /* #ifdef USE_SYSCTL_NET_ROUTE */
     152
     153
     154#ifdef USE_SOCKET_ROUTE
     155/* Thanks to Darren Kenny for this code */
     156#define NEXTADDR(w, u) \
     157        if (rtm_addrs & (w)) {\
     158            l = sizeof(struct sockaddr); memmove(cp, &(u), l); cp += l;\
     159        }
     160
     161#define rtm m_rtmsg.m_rtm
     162
     163struct {
     164  struct rt_msghdr m_rtm;
     165  char       m_space[512];
     166} m_rtmsg;
     167
     168int getdefaultgateway(in_addr_t *addr)
     169{
     170  int s, seq, l, rtm_addrs, i;
     171  pid_t pid;
     172  struct sockaddr so_dst, so_mask;
     173  char *cp = m_rtmsg.m_space;
     174  struct sockaddr *gate = NULL, *sa;
     175  struct rt_msghdr *msg_hdr;
     176
     177  pid = getpid();
     178  seq = 0;
     179  rtm_addrs = RTA_DST | RTA_NETMASK;
     180
     181  memset(&so_dst, 0, sizeof(so_dst));
     182  memset(&so_mask, 0, sizeof(so_mask));
     183  memset(&rtm, 0, sizeof(struct rt_msghdr));
     184
     185  rtm.rtm_type = RTM_GET;
     186  rtm.rtm_flags = RTF_UP | RTF_GATEWAY;
     187  rtm.rtm_version = RTM_VERSION;
     188  rtm.rtm_seq = ++seq;
     189  rtm.rtm_addrs = rtm_addrs;
     190
     191  so_dst.sa_family = AF_INET;
     192  so_mask.sa_family = AF_INET;
     193
     194  NEXTADDR(RTA_DST, so_dst);
     195  NEXTADDR(RTA_NETMASK, so_mask);
     196
     197  rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
     198
     199  s = socket(PF_ROUTE, SOCK_RAW, 0);
     200
     201  if (write(s, (char *)&m_rtmsg, l) < 0) {
     202      close(s);
     203      return FAILED;
     204  }
     205
     206  do {
     207    l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
     208  } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
     209                       
     210  close(s);
     211
     212  msg_hdr = &rtm;
     213
     214  cp = ((char *)(msg_hdr + 1));
     215  if (msg_hdr->rtm_addrs) {
     216    for (i = 1; i; i <<= 1)
     217      if (i & msg_hdr->rtm_addrs) {
     218        sa = (struct sockaddr *)cp;
     219        if (i == RTA_GATEWAY )
     220          gate = sa;
     221
     222        cp += sizeof(struct sockaddr);
     223      }
     224  } else {
     225      return FAILED;
     226  }
     227
     228
     229  if (gate != NULL ) {
     230      *addr = ((struct sockaddr_in *)gate)->sin_addr.s_addr;
     231      return SUCCESS;
     232  } else {
     233      return FAILED;
     234  }
     235}
     236#endif /* #ifdef USE_SOCKET_ROUTE */
     237
Note: See TracChangeset for help on using the changeset viewer.