Changeset 1584


Ignore:
Timestamp:
Nov 17, 2007 10:27:34 AM (16 years ago)
Author:
bennylp
Message:

Ticket #414: Implement IP interface enumeration on Linux/Unix? with SIOCGIFCONF ioctl() call to a socket

Location:
pjproject/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/aconfigure

    r1502 r1584  
    56955695  cat >>confdefs.h <<\_ACEOF 
    56965696#define PJ_HAS_NETINET_IP_H 1 
     5697_ACEOF 
     5698 
     5699fi 
     5700 
     5701 
     5702if test "${ac_cv_header_net_if_h+set}" = set; then 
     5703  echo "$as_me:$LINENO: checking for net/if.h" >&5 
     5704echo $ECHO_N "checking for net/if.h... $ECHO_C" >&6 
     5705if test "${ac_cv_header_net_if_h+set}" = set; then 
     5706  echo $ECHO_N "(cached) $ECHO_C" >&6 
     5707fi 
     5708echo "$as_me:$LINENO: result: $ac_cv_header_net_if_h" >&5 
     5709echo "${ECHO_T}$ac_cv_header_net_if_h" >&6 
     5710else 
     5711  # Is the header compilable? 
     5712echo "$as_me:$LINENO: checking net/if.h usability" >&5 
     5713echo $ECHO_N "checking net/if.h usability... $ECHO_C" >&6 
     5714cat >conftest.$ac_ext <<_ACEOF 
     5715/* confdefs.h.  */ 
     5716_ACEOF 
     5717cat confdefs.h >>conftest.$ac_ext 
     5718cat >>conftest.$ac_ext <<_ACEOF 
     5719/* end confdefs.h.  */ 
     5720$ac_includes_default 
     5721#include <net/if.h> 
     5722_ACEOF 
     5723rm -f conftest.$ac_objext 
     5724if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5 
     5725  (eval $ac_compile) 2>conftest.er1 
     5726  ac_status=$? 
     5727  grep -v '^ *+' conftest.er1 >conftest.err 
     5728  rm -f conftest.er1 
     5729  cat conftest.err >&5 
     5730  echo "$as_me:$LINENO: \$? = $ac_status" >&5 
     5731  (exit $ac_status); } && 
     5732         { ac_try='test -z "$ac_c_werror_flag" 
     5733                         || test ! -s conftest.err' 
     5734  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 
     5735  (eval $ac_try) 2>&5 
     5736  ac_status=$? 
     5737  echo "$as_me:$LINENO: \$? = $ac_status" >&5 
     5738  (exit $ac_status); }; } && 
     5739         { ac_try='test -s conftest.$ac_objext' 
     5740  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 
     5741  (eval $ac_try) 2>&5 
     5742  ac_status=$? 
     5743  echo "$as_me:$LINENO: \$? = $ac_status" >&5 
     5744  (exit $ac_status); }; }; then 
     5745  ac_header_compiler=yes 
     5746else 
     5747  echo "$as_me: failed program was:" >&5 
     5748sed 's/^/| /' conftest.$ac_ext >&5 
     5749 
     5750ac_header_compiler=no 
     5751fi 
     5752rm -f conftest.err conftest.$ac_objext conftest.$ac_ext 
     5753echo "$as_me:$LINENO: result: $ac_header_compiler" >&5 
     5754echo "${ECHO_T}$ac_header_compiler" >&6 
     5755 
     5756# Is the header present? 
     5757echo "$as_me:$LINENO: checking net/if.h presence" >&5 
     5758echo $ECHO_N "checking net/if.h presence... $ECHO_C" >&6 
     5759cat >conftest.$ac_ext <<_ACEOF 
     5760/* confdefs.h.  */ 
     5761_ACEOF 
     5762cat confdefs.h >>conftest.$ac_ext 
     5763cat >>conftest.$ac_ext <<_ACEOF 
     5764/* end confdefs.h.  */ 
     5765#include <net/if.h> 
     5766_ACEOF 
     5767if { (eval echo "$as_me:$LINENO: \"$ac_cpp conftest.$ac_ext\"") >&5 
     5768  (eval $ac_cpp conftest.$ac_ext) 2>conftest.er1 
     5769  ac_status=$? 
     5770  grep -v '^ *+' conftest.er1 >conftest.err 
     5771  rm -f conftest.er1 
     5772  cat conftest.err >&5 
     5773  echo "$as_me:$LINENO: \$? = $ac_status" >&5 
     5774  (exit $ac_status); } >/dev/null; then 
     5775  if test -s conftest.err; then 
     5776    ac_cpp_err=$ac_c_preproc_warn_flag 
     5777    ac_cpp_err=$ac_cpp_err$ac_c_werror_flag 
     5778  else 
     5779    ac_cpp_err= 
     5780  fi 
     5781else 
     5782  ac_cpp_err=yes 
     5783fi 
     5784if test -z "$ac_cpp_err"; then 
     5785  ac_header_preproc=yes 
     5786else 
     5787  echo "$as_me: failed program was:" >&5 
     5788sed 's/^/| /' conftest.$ac_ext >&5 
     5789 
     5790  ac_header_preproc=no 
     5791fi 
     5792rm -f conftest.err conftest.$ac_ext 
     5793echo "$as_me:$LINENO: result: $ac_header_preproc" >&5 
     5794echo "${ECHO_T}$ac_header_preproc" >&6 
     5795 
     5796# So?  What about this header? 
     5797case $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in 
     5798  yes:no: ) 
     5799    { echo "$as_me:$LINENO: WARNING: net/if.h: accepted by the compiler, rejected by the preprocessor!" >&5 
     5800echo "$as_me: WARNING: net/if.h: accepted by the compiler, rejected by the preprocessor!" >&2;} 
     5801    { echo "$as_me:$LINENO: WARNING: net/if.h: proceeding with the compiler's result" >&5 
     5802echo "$as_me: WARNING: net/if.h: proceeding with the compiler's result" >&2;} 
     5803    ac_header_preproc=yes 
     5804    ;; 
     5805  no:yes:* ) 
     5806    { echo "$as_me:$LINENO: WARNING: net/if.h: present but cannot be compiled" >&5 
     5807echo "$as_me: WARNING: net/if.h: present but cannot be compiled" >&2;} 
     5808    { echo "$as_me:$LINENO: WARNING: net/if.h:     check for missing prerequisite headers?" >&5 
     5809echo "$as_me: WARNING: net/if.h:     check for missing prerequisite headers?" >&2;} 
     5810    { echo "$as_me:$LINENO: WARNING: net/if.h: see the Autoconf documentation" >&5 
     5811echo "$as_me: WARNING: net/if.h: see the Autoconf documentation" >&2;} 
     5812    { echo "$as_me:$LINENO: WARNING: net/if.h:     section \"Present But Cannot Be Compiled\"" >&5 
     5813echo "$as_me: WARNING: net/if.h:     section \"Present But Cannot Be Compiled\"" >&2;} 
     5814    { echo "$as_me:$LINENO: WARNING: net/if.h: proceeding with the preprocessor's result" >&5 
     5815echo "$as_me: WARNING: net/if.h: proceeding with the preprocessor's result" >&2;} 
     5816    { echo "$as_me:$LINENO: WARNING: net/if.h: in the future, the compiler will take precedence" >&5 
     5817echo "$as_me: WARNING: net/if.h: in the future, the compiler will take precedence" >&2;} 
     5818    ( 
     5819      cat <<\_ASBOX 
     5820## ------------------------------------ ## 
     5821## Report this to the pjproject lists.  ## 
     5822## ------------------------------------ ## 
     5823_ASBOX 
     5824    ) | 
     5825      sed "s/^/$as_me: WARNING:     /" >&2 
     5826    ;; 
     5827esac 
     5828echo "$as_me:$LINENO: checking for net/if.h" >&5 
     5829echo $ECHO_N "checking for net/if.h... $ECHO_C" >&6 
     5830if test "${ac_cv_header_net_if_h+set}" = set; then 
     5831  echo $ECHO_N "(cached) $ECHO_C" >&6 
     5832else 
     5833  ac_cv_header_net_if_h=$ac_header_preproc 
     5834fi 
     5835echo "$as_me:$LINENO: result: $ac_cv_header_net_if_h" >&5 
     5836echo "${ECHO_T}$ac_cv_header_net_if_h" >&6 
     5837 
     5838fi 
     5839if test $ac_cv_header_net_if_h = yes; then 
     5840  cat >>confdefs.h <<\_ACEOF 
     5841#define PJ_HAS_NET_IF_H 1 
    56975842_ACEOF 
    56985843 
  • pjproject/trunk/aconfigure.ac

    r1502 r1584  
    144144AC_CHECK_HEADER(netinet/in.h,[AC_DEFINE(PJ_HAS_NETINET_IN_H,1)]) 
    145145AC_CHECK_HEADER(netinet/ip.h,[AC_DEFINE(PJ_HAS_NETINET_IP_H,1)]) 
     146AC_CHECK_HEADER(net/if.h,[AC_DEFINE(PJ_HAS_NET_IF_H,1)]) 
    146147AC_CHECK_HEADER(setjmp.h,[AC_DEFINE(PJ_HAS_SETJMP_H,1)]) 
    147148AC_CHECK_HEADER(stdarg.h,[AC_DEFINE(PJ_HAS_STDARG_H,1)]) 
  • pjproject/trunk/pjlib/include/pj/compat/os_auto.h.in

    r920 r1584  
    5252#undef PJ_HAS_NETINET_IN_H 
    5353#undef PJ_HAS_NETINET_IP_H 
     54#undef PJ_HAS_NET_IF_H 
    5455#undef PJ_HAS_SETJMP_H 
    5556#undef PJ_HAS_STDARG_H 
  • pjproject/trunk/pjlib/include/pj/compat/socket.h

    r1235 r1584  
    5656/* To pull in IPTOS_* constants */ 
    5757#  include <netinet/ip.h> 
     58#endif 
     59 
     60#if defined(PJ_HAS_NET_IF_H) && PJ_HAS_NET_IF_H != 0 
     61/* For interface enumeration in ip_helper */ 
     62#   include <net/if.h> 
    5863#endif 
    5964 
  • pjproject/trunk/pjlib/src/pj/ip_helper_generic.c

    r1104 r1584  
    2222#include <pj/errno.h> 
    2323#include <pj/string.h> 
     24#include <pj/compat/socket.h> 
    2425 
    25 /* 
    26  * Enumerate the local IP interface currently active in the host. 
    27  */ 
    28 PJ_DEF(pj_status_t) pj_enum_ip_interface(unsigned *p_cnt, 
    29                                          pj_in_addr ifs[]) 
     26static pj_status_t dummy_enum_ip_interface(unsigned *p_cnt, 
     27                                          pj_in_addr ifs[]) 
    3028{ 
    3129    pj_status_t status; 
     
    4240    *p_cnt = 1; 
    4341    return PJ_SUCCESS; 
     42} 
     43 
     44#ifdef SIOCGIFCONF 
     45static pj_status_t sock_enum_ip_interface(unsigned *p_cnt, 
     46                                          pj_in_addr ifs[]) 
     47{ 
     48    pj_sock_t sock; 
     49    char buf[512]; 
     50    struct ifconf ifc; 
     51    struct ifreq *ifr; 
     52    int i, count; 
     53    pj_status_t status; 
     54 
     55    status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock); 
     56    if (status != PJ_SUCCESS) 
     57        return status; 
     58 
     59    /* Query available interfaces */ 
     60    ifc.ifc_len = sizeof(buf); 
     61    ifc.ifc_buf = buf; 
     62 
     63    if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { 
     64        int oserr = pj_get_netos_error(); 
     65        pj_sock_close(sock); 
     66        return PJ_RETURN_OS_ERROR(oserr); 
     67    } 
     68 
     69    /* Done with socket */ 
     70    pj_sock_close(sock); 
     71 
     72    /* Interface interfaces */ 
     73    ifr = (struct ifreq*) ifc.ifc_req; 
     74    count = ifc.ifc_len / sizeof(struct ifreq); 
     75    if (count > *p_cnt) 
     76        count = *p_cnt; 
     77    else 
     78        *p_cnt = count; 
     79    for (i=0; i<count; ++i) { 
     80        struct ifreq *itf = &ifr[i]; 
     81        ifs[i].s_addr = ((struct sockaddr_in *)&itf->ifr_addr)->sin_addr.s_addr; 
     82    } 
     83 
     84    return PJ_SUCCESS; 
     85} 
     86#endif /* SIOCGIFCONF */ 
     87 
     88/* 
     89 * Enumerate the local IP interface currently active in the host. 
     90 */ 
     91PJ_DEF(pj_status_t) pj_enum_ip_interface(unsigned *p_cnt, 
     92                                         pj_in_addr ifs[]) 
     93{ 
     94#ifdef SIOCGIFCONF 
     95    if (sock_enum_ip_interface(p_cnt, ifs) == PJ_SUCCESS) 
     96        return PJ_SUCCESS; 
     97#endif 
     98    return dummy_enum_ip_interface(p_cnt, ifs); 
    4499} 
    45100 
Note: See TracChangeset for help on using the changeset viewer.