- Timestamp:
- Dec 3, 2007 4:03:17 AM (17 years ago)
- Location:
- pjproject/trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/aconfigure
r1585 r1612 5840 5840 cat >>confdefs.h <<\_ACEOF 5841 5841 #define PJ_HAS_NET_IF_H 1 5842 _ACEOF 5843 5844 fi 5845 5846 5847 if test "${ac_cv_header_ifaddrs_h+set}" = set; then 5848 echo "$as_me:$LINENO: checking for ifaddrs.h" >&5 5849 echo $ECHO_N "checking for ifaddrs.h... $ECHO_C" >&6 5850 if test "${ac_cv_header_ifaddrs_h+set}" = set; then 5851 echo $ECHO_N "(cached) $ECHO_C" >&6 5852 fi 5853 echo "$as_me:$LINENO: result: $ac_cv_header_ifaddrs_h" >&5 5854 echo "${ECHO_T}$ac_cv_header_ifaddrs_h" >&6 5855 else 5856 # Is the header compilable? 5857 echo "$as_me:$LINENO: checking ifaddrs.h usability" >&5 5858 echo $ECHO_N "checking ifaddrs.h usability... $ECHO_C" >&6 5859 cat >conftest.$ac_ext <<_ACEOF 5860 /* confdefs.h. */ 5861 _ACEOF 5862 cat confdefs.h >>conftest.$ac_ext 5863 cat >>conftest.$ac_ext <<_ACEOF 5864 /* end confdefs.h. */ 5865 $ac_includes_default 5866 #include <ifaddrs.h> 5867 _ACEOF 5868 rm -f conftest.$ac_objext 5869 if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5 5870 (eval $ac_compile) 2>conftest.er1 5871 ac_status=$? 5872 grep -v '^ *+' conftest.er1 >conftest.err 5873 rm -f conftest.er1 5874 cat conftest.err >&5 5875 echo "$as_me:$LINENO: \$? = $ac_status" >&5 5876 (exit $ac_status); } && 5877 { ac_try='test -z "$ac_c_werror_flag" 5878 || test ! -s conftest.err' 5879 { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 5880 (eval $ac_try) 2>&5 5881 ac_status=$? 5882 echo "$as_me:$LINENO: \$? = $ac_status" >&5 5883 (exit $ac_status); }; } && 5884 { ac_try='test -s conftest.$ac_objext' 5885 { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 5886 (eval $ac_try) 2>&5 5887 ac_status=$? 5888 echo "$as_me:$LINENO: \$? = $ac_status" >&5 5889 (exit $ac_status); }; }; then 5890 ac_header_compiler=yes 5891 else 5892 echo "$as_me: failed program was:" >&5 5893 sed 's/^/| /' conftest.$ac_ext >&5 5894 5895 ac_header_compiler=no 5896 fi 5897 rm -f conftest.err conftest.$ac_objext conftest.$ac_ext 5898 echo "$as_me:$LINENO: result: $ac_header_compiler" >&5 5899 echo "${ECHO_T}$ac_header_compiler" >&6 5900 5901 # Is the header present? 5902 echo "$as_me:$LINENO: checking ifaddrs.h presence" >&5 5903 echo $ECHO_N "checking ifaddrs.h presence... $ECHO_C" >&6 5904 cat >conftest.$ac_ext <<_ACEOF 5905 /* confdefs.h. */ 5906 _ACEOF 5907 cat confdefs.h >>conftest.$ac_ext 5908 cat >>conftest.$ac_ext <<_ACEOF 5909 /* end confdefs.h. */ 5910 #include <ifaddrs.h> 5911 _ACEOF 5912 if { (eval echo "$as_me:$LINENO: \"$ac_cpp conftest.$ac_ext\"") >&5 5913 (eval $ac_cpp conftest.$ac_ext) 2>conftest.er1 5914 ac_status=$? 5915 grep -v '^ *+' conftest.er1 >conftest.err 5916 rm -f conftest.er1 5917 cat conftest.err >&5 5918 echo "$as_me:$LINENO: \$? = $ac_status" >&5 5919 (exit $ac_status); } >/dev/null; then 5920 if test -s conftest.err; then 5921 ac_cpp_err=$ac_c_preproc_warn_flag 5922 ac_cpp_err=$ac_cpp_err$ac_c_werror_flag 5923 else 5924 ac_cpp_err= 5925 fi 5926 else 5927 ac_cpp_err=yes 5928 fi 5929 if test -z "$ac_cpp_err"; then 5930 ac_header_preproc=yes 5931 else 5932 echo "$as_me: failed program was:" >&5 5933 sed 's/^/| /' conftest.$ac_ext >&5 5934 5935 ac_header_preproc=no 5936 fi 5937 rm -f conftest.err conftest.$ac_ext 5938 echo "$as_me:$LINENO: result: $ac_header_preproc" >&5 5939 echo "${ECHO_T}$ac_header_preproc" >&6 5940 5941 # So? What about this header? 5942 case $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in 5943 yes:no: ) 5944 { echo "$as_me:$LINENO: WARNING: ifaddrs.h: accepted by the compiler, rejected by the preprocessor!" >&5 5945 echo "$as_me: WARNING: ifaddrs.h: accepted by the compiler, rejected by the preprocessor!" >&2;} 5946 { echo "$as_me:$LINENO: WARNING: ifaddrs.h: proceeding with the compiler's result" >&5 5947 echo "$as_me: WARNING: ifaddrs.h: proceeding with the compiler's result" >&2;} 5948 ac_header_preproc=yes 5949 ;; 5950 no:yes:* ) 5951 { echo "$as_me:$LINENO: WARNING: ifaddrs.h: present but cannot be compiled" >&5 5952 echo "$as_me: WARNING: ifaddrs.h: present but cannot be compiled" >&2;} 5953 { echo "$as_me:$LINENO: WARNING: ifaddrs.h: check for missing prerequisite headers?" >&5 5954 echo "$as_me: WARNING: ifaddrs.h: check for missing prerequisite headers?" >&2;} 5955 { echo "$as_me:$LINENO: WARNING: ifaddrs.h: see the Autoconf documentation" >&5 5956 echo "$as_me: WARNING: ifaddrs.h: see the Autoconf documentation" >&2;} 5957 { echo "$as_me:$LINENO: WARNING: ifaddrs.h: section \"Present But Cannot Be Compiled\"" >&5 5958 echo "$as_me: WARNING: ifaddrs.h: section \"Present But Cannot Be Compiled\"" >&2;} 5959 { echo "$as_me:$LINENO: WARNING: ifaddrs.h: proceeding with the preprocessor's result" >&5 5960 echo "$as_me: WARNING: ifaddrs.h: proceeding with the preprocessor's result" >&2;} 5961 { echo "$as_me:$LINENO: WARNING: ifaddrs.h: in the future, the compiler will take precedence" >&5 5962 echo "$as_me: WARNING: ifaddrs.h: in the future, the compiler will take precedence" >&2;} 5963 ( 5964 cat <<\_ASBOX 5965 ## ------------------------------------ ## 5966 ## Report this to the pjproject lists. ## 5967 ## ------------------------------------ ## 5968 _ASBOX 5969 ) | 5970 sed "s/^/$as_me: WARNING: /" >&2 5971 ;; 5972 esac 5973 echo "$as_me:$LINENO: checking for ifaddrs.h" >&5 5974 echo $ECHO_N "checking for ifaddrs.h... $ECHO_C" >&6 5975 if test "${ac_cv_header_ifaddrs_h+set}" = set; then 5976 echo $ECHO_N "(cached) $ECHO_C" >&6 5977 else 5978 ac_cv_header_ifaddrs_h=$ac_header_preproc 5979 fi 5980 echo "$as_me:$LINENO: result: $ac_cv_header_ifaddrs_h" >&5 5981 echo "${ECHO_T}$ac_cv_header_ifaddrs_h" >&6 5982 5983 fi 5984 if test $ac_cv_header_ifaddrs_h = yes; then 5985 cat >>confdefs.h <<\_ACEOF 5986 #define PJ_HAS_IFADDRS_H 1 5842 5987 _ACEOF 5843 5988 -
pjproject/trunk/aconfigure.ac
r1585 r1612 145 145 AC_CHECK_HEADER(netinet/ip.h,[AC_DEFINE(PJ_HAS_NETINET_IP_H,1)]) 146 146 AC_CHECK_HEADER(net/if.h,[AC_DEFINE(PJ_HAS_NET_IF_H,1)]) 147 AC_CHECK_HEADER(ifaddrs.h,[AC_DEFINE(PJ_HAS_IFADDRS_H,1)]) 147 148 AC_CHECK_HEADER(setjmp.h,[AC_DEFINE(PJ_HAS_SETJMP_H,1)]) 148 149 AC_CHECK_HEADER(stdarg.h,[AC_DEFINE(PJ_HAS_STDARG_H,1)]) -
pjproject/trunk/pjlib/include/pj/compat/os_auto.h.in
r1585 r1612 53 53 #undef PJ_HAS_NETINET_IP_H 54 54 #undef PJ_HAS_NET_IF_H 55 #undef PJ_HAS_IFADDRS_H 55 56 #undef PJ_HAS_SETJMP_H 56 57 #undef PJ_HAS_STDARG_H -
pjproject/trunk/pjlib/include/pj/compat/socket.h
r1611 r1612 100 100 /* For interface enumeration in ip_helper */ 101 101 # include <net/if.h> 102 #endif 103 104 #if defined(PJ_HAS_IFADDRS_H) && PJ_HAS_IFADDRS_H != 0 105 /* Interface enum with getifaddrs() which works with IPv6 */ 106 # include <ifaddrs.h> 102 107 #endif 103 108 -
pjproject/trunk/pjlib/src/pj/ip_helper_generic.c
r1611 r1612 23 23 #include <pj/string.h> 24 24 #include <pj/compat/socket.h> 25 26 static pj_status_t dummy_enum_ip_interface(int af, 27 unsigned *p_cnt, 28 pj_sockaddr ifs[]) 29 { 30 pj_status_t status; 31 32 PJ_ASSERT_RETURN(p_cnt && *p_cnt > 0 && ifs, PJ_EINVAL); 33 34 pj_bzero(ifs, sizeof(ifs[0]) * (*p_cnt)); 35 36 /* Just get one default route */ 37 status = pj_getdefaultipinterface(af, &ifs[0]); 38 if (status != PJ_SUCCESS) 39 return status; 40 41 *p_cnt = 1; 42 return PJ_SUCCESS; 43 } 44 45 #ifdef SIOCGIFCONF 46 static pj_status_t sock_enum_ip_interface(int af, 47 unsigned *p_cnt, 48 pj_sockaddr ifs[]) 25 #include <pj/sock.h> 26 27 /* Set to 1 to enable tracing */ 28 #if 0 29 # include <pj/log.h> 30 # define THIS_FILE "ip_helper_generic.c" 31 # define TRACE_(exp) PJ_LOG(5,exp) 32 static const char *get_os_errmsg(void) 33 { 34 static char errmsg[PJ_ERR_MSG_SIZE]; 35 pj_strerror(pj_get_os_error(), errmsg, sizeof(errmsg)); 36 return errmsg; 37 } 38 static const char *get_addr(void *addr) 39 { 40 static char txt[PJ_INET6_ADDRSTRLEN]; 41 struct sockaddr *ad = (struct sockaddr*)addr; 42 if (ad->sa_family != PJ_AF_INET && ad->sa_family != PJ_AF_INET6) 43 return "?"; 44 return pj_inet_ntop2(ad->sa_family, pj_sockaddr_get_addr(ad), 45 txt, sizeof(txt)); 46 } 47 #else 48 # define TRACE_(exp) 49 #endif 50 51 52 #if 0 53 /* dummy */ 54 55 #elif defined(PJ_HAS_IFADDRS_H) && PJ_HAS_IFADDRS_H != 0 56 /* Using getifaddrs() is preferred since it can work with both IPv4 and IPv6 */ 57 static pj_status_t if_enum_by_af(int af, 58 unsigned *p_cnt, 59 pj_sockaddr ifs[]) 60 { 61 struct ifaddrs *ifap = NULL, *it; 62 unsigned max; 63 64 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL); 65 66 TRACE_((THIS_FILE, "Starting interface enum with getifaddrs() for af=%d", 67 af)); 68 69 if (getifaddrs(&ifap) != 0) { 70 TRACE_((THIS_FILE, " getifarrds() failed: %s", get_os_errmsg())); 71 return PJ_RETURN_OS_ERROR(pj_get_netos_error()); 72 } 73 74 it = ifap; 75 max = *p_cnt; 76 *p_cnt = 0; 77 for (; it!=NULL && *p_cnt < max; it = it->ifa_next) { 78 struct sockaddr *ad = it->ifa_addr; 79 80 TRACE_((THIS_FILE, " checking %s", it->ifa_name)); 81 82 if ((it->ifa_flags & IFF_UP)==0) { 83 TRACE_((THIS_FILE, " interface is down")); 84 continue; /* Skip when interface is down */ 85 } 86 87 if (it->ifa_flags & IFF_LOOPBACK) { 88 TRACE_((THIS_FILE, " loopback interface")); 89 continue; /* Skip loopback interface */ 90 } 91 92 if (ad->sa_family != af) { 93 TRACE_((THIS_FILE, " address %s ignored (af=%d)", 94 get_addr(ad), ad->sa_family)); 95 continue; /* Skip when interface is down */ 96 } 97 98 TRACE_((THIS_FILE, " address %s (af=%d) added at index %d", 99 get_addr(ad), ad->sa_family, *p_cnt)); 100 101 pj_bzero(&ifs[*p_cnt], sizeof(ifs[0])); 102 pj_memcpy(&ifs[*p_cnt], ad, pj_sockaddr_get_len(ad)); 103 (*p_cnt)++; 104 } 105 106 freeifaddrs(ifap); 107 TRACE_((THIS_FILE, "done, found %d address(es)", *p_cnt)); 108 return (*p_cnt != 0) ? PJ_SUCCESS : PJ_ENOTFOUND; 109 } 110 111 #elif defined(SIOCGIFCONF) 112 /* Note: this does not work with IPv6 */ 113 static pj_status_t if_enum_by_af(int af, 114 unsigned *p_cnt, 115 pj_sockaddr ifs[]) 49 116 { 50 117 pj_sock_t sock; … … 57 124 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL); 58 125 126 TRACE_((THIS_FILE, "Starting interface enum with SIOCGIFCONF for af=%d", 127 af)); 128 59 129 status = pj_sock_socket(af, PJ_SOCK_DGRAM, 0, &sock); 60 130 if (status != PJ_SUCCESS) … … 67 137 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { 68 138 int oserr = pj_get_netos_error(); 139 TRACE_((THIS_FILE, " ioctl(SIOCGIFCONF) failed: %s", get_os_errmsg())); 69 140 pj_sock_close(sock); 70 141 return PJ_RETURN_OS_ERROR(oserr); … … 79 150 if (count > *p_cnt) 80 151 count = *p_cnt; 81 else 82 *p_cnt = count;152 153 *p_cnt = 0; 83 154 for (i=0; i<count; ++i) { 84 155 struct ifreq *itf = &ifr[i]; 85 156 struct sockaddr *ad = &itf->ifr_addr; 86 157 87 ifs[i].addr.sa_family = ad->sa_family; 88 pj_memcpy(pj_sockaddr_get_addr(&ifs[i]), 89 pj_sockaddr_get_addr(ad), 90 pj_sockaddr_get_addr_len(ad)); 91 } 92 158 TRACE_((THIS_FILE, " checking interface %s", itf->ifr_name)); 159 160 /* Skip address with different family */ 161 if (ad->sa_family != af) { 162 TRACE_((THIS_FILE, " address %s (af=%d) ignored", 163 get_addr(ad), (int)ad->sa_family)); 164 continue; 165 } 166 167 if ((itf->ifr_flags & IFF_UP)==0) { 168 TRACE_((THIS_FILE, " interface is down")); 169 continue; /* Skip when interface is down */ 170 } 171 172 if (itf->ifr_flags & IFF_LOOPBACK) { 173 TRACE_((THIS_FILE, " loopback interface")); 174 continue; /* Skip loopback interface */ 175 } 176 177 TRACE_((THIS_FILE, " address %s (af=%d) added at index %d", 178 get_addr(ad), ad->sa_family, *p_cnt)); 179 180 pj_bzero(&ifs[*p_cnt], sizeof(ifs[0])); 181 pj_memcpy(&ifs[*p_cnt], ad, pj_sockaddr_get_len(ad)); 182 (*p_cnt)++; 183 } 184 185 TRACE_((THIS_FILE, "done, found %d address(es)", *p_cnt)); 186 return (*p_cnt != 0) ? PJ_SUCCESS : PJ_ENOTFOUND; 187 } 188 189 #elif defined(PJ_HAS_NET_IF_H) && PJ_HAS_NET_IF_H != 0 190 /* Note: this does not work with IPv6 */ 191 static pj_status_t if_enum_by_af(int af, unsigned *p_cnt, pj_sockaddr ifs[]) 192 { 193 struct if_nameindex *if_list; 194 struct ifreq ifreq; 195 pj_sock_t sock; 196 unsigned i, max_count; 197 pj_status_t status; 198 199 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL); 200 201 TRACE_((THIS_FILE, "Starting if_nameindex() for af=%d", af)); 202 203 status = pj_sock_socket(af, PJ_SOCK_DGRAM, 0, &sock); 204 if (status != PJ_SUCCESS) 205 return status; 206 207 if_list = if_nameindex(); 208 if (if_list == NULL) 209 return PJ_ENOTFOUND; 210 211 max_count = *p_cnt; 212 *p_cnt = 0; 213 for (i=0; if_list[i].if_index && *p_cnt<max_count; ++i) { 214 int rc; 215 216 strncpy(ifreq.ifr_name, if_list[i].if_name, IFNAMSIZ); 217 218 TRACE_((THIS_FILE, " checking interface %s", ifreq.ifr_name)); 219 220 if ((rc=ioctl(sock, SIOCGIFFLAGS, &ifreq)) != 0) { 221 TRACE_((THIS_FILE, " ioctl(SIOCGIFFLAGS) failed: %s", 222 get_os_errmsg())); 223 continue; /* Failed to get flags, continue */ 224 } 225 226 if ((ifreq.ifr_flags & IFF_UP)==0) { 227 TRACE_((THIS_FILE, " interface is down")); 228 continue; /* Skip when interface is down */ 229 } 230 231 if (ifreq.ifr_flags & IFF_LOOPBACK) { 232 TRACE_((THIS_FILE, " loopback interface")); 233 continue; /* Skip loopback interface */ 234 } 235 236 /* Note: SIOCGIFADDR does not work for IPv6! */ 237 if ((rc=ioctl(sock, SIOCGIFADDR, &ifreq)) != 0) { 238 TRACE_((THIS_FILE, " ioctl(SIOCGIFADDR) failed: %s", 239 get_os_errmsg())); 240 continue; /* Failed to get address, continue */ 241 } 242 243 if (ifreq.ifr_addr.sa_family != af) { 244 TRACE_((THIS_FILE, " address %s family %d ignored", 245 get_addr(&ifreq.ifr_addr), 246 ifreq.ifr_addr.sa_family)); 247 continue; /* Not address family that we want, continue */ 248 } 249 250 /* Got an address ! */ 251 TRACE_((THIS_FILE, " address %s (af=%d) added at index %d", 252 get_addr(&ifreq.ifr_addr), ifreq.ifr_addr.sa_family, *p_cnt)); 253 254 pj_bzero(&ifs[*p_cnt], sizeof(ifs[0])); 255 pj_memcpy(&ifs[*p_cnt], &ifreq.ifr_addr, 256 pj_sockaddr_get_len(&ifreq.ifr_addr)); 257 (*p_cnt)++; 258 } 259 260 if_freenameindex(if_list); 261 pj_sock_close(sock); 262 263 TRACE_((THIS_FILE, "done, found %d address(es)", *p_cnt)); 264 return (*p_cnt != 0) ? PJ_SUCCESS : PJ_ENOTFOUND; 265 } 266 267 #else 268 static pj_status_t if_enum_by_af(int af, 269 unsigned *p_cnt, 270 pj_sockaddr ifs[]) 271 { 272 pj_status_t status; 273 274 PJ_ASSERT_RETURN(p_cnt && *p_cnt > 0 && ifs, PJ_EINVAL); 275 276 PJ_UNUSED_ARG(&get_addr); 277 PJ_UNUSED_ARG(&get_os_errmsg); 278 279 pj_bzero(ifs, sizeof(ifs[0]) * (*p_cnt)); 280 281 /* Just get one default route */ 282 status = pj_getdefaultipinterface(af, &ifs[0]); 283 if (status != PJ_SUCCESS) 284 return status; 285 286 *p_cnt = 1; 93 287 return PJ_SUCCESS; 94 288 } … … 102 296 pj_sockaddr ifs[]) 103 297 { 104 #ifdef SIOCGIFCONF 105 if (sock_enum_ip_interface(af, p_cnt, ifs) == PJ_SUCCESS) 106 return PJ_SUCCESS; 107 #endif 108 return dummy_enum_ip_interface(af, p_cnt, ifs); 298 unsigned start; 299 pj_status_t status; 300 301 start = 0; 302 if (af==PJ_AF_INET6 || af==PJ_AF_UNSPEC) { 303 unsigned max = *p_cnt; 304 status = if_enum_by_af(PJ_AF_INET6, &max, &ifs[start]); 305 if (status == PJ_SUCCESS) { 306 start += max; 307 (*p_cnt) -= max; 308 } 309 } 310 311 if (af==PJ_AF_INET || af==PJ_AF_UNSPEC) { 312 unsigned max = *p_cnt; 313 status = if_enum_by_af(PJ_AF_INET, &max, &ifs[start]); 314 if (status == PJ_SUCCESS) { 315 start += max; 316 (*p_cnt) -= max; 317 } 318 } 319 320 *p_cnt = start; 321 322 return (*p_cnt != 0) ? PJ_SUCCESS : PJ_ENOTFOUND; 109 323 } 110 324
Note: See TracChangeset
for help on using the changeset viewer.