From e0a80736ab0f95882527af0d701c894398eb424f Mon Sep 17 00:00:00 2001 From: itojun Date: Thu, 19 Sep 2002 01:43:33 +0000 Subject: [PATCH] avoid use of sa_len, to be friendly with linux (but there are unsafe assumption made) --- kame/kame/mdnsd/configure | 115 +++++++++++++++++++++-------------- kame/kame/mdnsd/configure.in | 9 ++- kame/kame/mdnsd/mainloop.c | 17 +++++- kame/kame/mdnsd/mdnsd.c | 30 ++++++--- 4 files changed, 115 insertions(+), 56 deletions(-) diff --git a/kame/kame/mdnsd/configure b/kame/kame/mdnsd/configure index f14bec69d2..48023d5193 100755 --- a/kame/kame/mdnsd/configure +++ b/kame/kame/mdnsd/configure @@ -527,7 +527,7 @@ fi ac_aux_dir= -for ac_dir in ${GNUSYSTEM_AUX_DIR} $srcdir $srcdir/.. $srcdir/../..; do +for ac_dir in $srcdir $srcdir/.. $srcdir/../..; do if test -f $ac_dir/install-sh; then ac_aux_dir=$ac_dir ac_install_sh="$ac_aux_dir/install-sh -c" @@ -1399,7 +1399,7 @@ cat > conftest.$ac_ext < #include int main() { -socklen_t x; x = 1; return x; +socklen_t x; x = 1; return x; ; return 0; } EOF if { (eval echo configure:1406: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then @@ -1445,14 +1445,39 @@ fi fi rm -f conftest* +echo $ac_n "checking for sa_len""... $ac_c" 1>&6 +echo "configure:1450: checking for sa_len" >&5 +cat > conftest.$ac_ext < +#include +int main() { +struct sockaddr sa; sa.sa_len = sizeof(sa); return sa.sa_len; +; return 0; } +EOF +if { (eval echo configure:1460: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then + rm -rf conftest* + echo "$ac_t""yes" 1>&6 + cat >> confdefs.h <<\EOF +#define HAVE_SA_LEN 1 +EOF + +else + echo "configure: failed program was:" >&5 + cat conftest.$ac_ext >&5 + rm -rf conftest* + echo "$ac_t""no" 1>&6 +fi +rm -f conftest* echo $ac_n "checking for working const""... $ac_c" 1>&6 -echo "configure:1451: checking for working const" >&5 +echo "configure:1476: checking for working const" >&5 if eval "test \"`echo '$''{'ac_cv_c_const'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext <&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:1530: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* ac_cv_c_const=yes else @@ -1522,12 +1547,12 @@ EOF fi echo $ac_n "checking for size_t""... $ac_c" 1>&6 -echo "configure:1526: checking for size_t" >&5 +echo "configure:1551: checking for size_t" >&5 if eval "test \"`echo '$''{'ac_cv_type_size_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #if STDC_HEADERS @@ -1555,12 +1580,12 @@ EOF fi echo $ac_n "checking whether time.h and sys/time.h may both be included""... $ac_c" 1>&6 -echo "configure:1559: checking whether time.h and sys/time.h may both be included" >&5 +echo "configure:1584: checking whether time.h and sys/time.h may both be included" >&5 if eval "test \"`echo '$''{'ac_cv_header_time'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #include @@ -1569,7 +1594,7 @@ int main() { struct tm *tp; ; return 0; } EOF -if { (eval echo configure:1573: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:1598: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* ac_cv_header_time=yes else @@ -1590,12 +1615,12 @@ EOF fi echo $ac_n "checking whether struct tm is in sys/time.h or time.h""... $ac_c" 1>&6 -echo "configure:1594: checking whether struct tm is in sys/time.h or time.h" >&5 +echo "configure:1619: checking whether struct tm is in sys/time.h or time.h" >&5 if eval "test \"`echo '$''{'ac_cv_struct_tm'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #include @@ -1603,7 +1628,7 @@ int main() { struct tm *tp; tp->tm_sec; ; return 0; } EOF -if { (eval echo configure:1607: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:1632: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* ac_cv_struct_tm=time.h else @@ -1624,12 +1649,12 @@ EOF fi echo $ac_n "checking for tm_zone in struct tm""... $ac_c" 1>&6 -echo "configure:1628: checking for tm_zone in struct tm" >&5 +echo "configure:1653: checking for tm_zone in struct tm" >&5 if eval "test \"`echo '$''{'ac_cv_struct_tm_zone'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #include <$ac_cv_struct_tm> @@ -1637,7 +1662,7 @@ int main() { struct tm tm; tm.tm_zone; ; return 0; } EOF -if { (eval echo configure:1641: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:1666: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* ac_cv_struct_tm_zone=yes else @@ -1657,12 +1682,12 @@ EOF else echo $ac_n "checking for tzname""... $ac_c" 1>&6 -echo "configure:1661: checking for tzname" >&5 +echo "configure:1686: checking for tzname" >&5 if eval "test \"`echo '$''{'ac_cv_var_tzname'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #ifndef tzname /* For SGI. */ @@ -1672,7 +1697,7 @@ int main() { atoi(*tzname); ; return 0; } EOF -if { (eval echo configure:1676: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then +if { (eval echo configure:1701: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then rm -rf conftest* ac_cv_var_tzname=yes else @@ -1695,7 +1720,7 @@ fi echo $ac_n "checking whether getpgrp takes no argument""... $ac_c" 1>&6 -echo "configure:1699: checking whether getpgrp takes no argument" >&5 +echo "configure:1724: checking whether getpgrp takes no argument" >&5 if eval "test \"`echo '$''{'ac_cv_func_getpgrp_void'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -1703,7 +1728,7 @@ else { echo "configure: error: cannot check getpgrp if cross compiling" 1>&2; exit 1; } else cat > conftest.$ac_ext <&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:1787: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_func_getpgrp_void=yes else @@ -1783,13 +1808,13 @@ fi if test $ac_cv_prog_gcc = yes; then echo $ac_n "checking whether ${CC-cc} needs -traditional""... $ac_c" 1>&6 -echo "configure:1787: checking whether ${CC-cc} needs -traditional" >&5 +echo "configure:1812: checking whether ${CC-cc} needs -traditional" >&5 if eval "test \"`echo '$''{'ac_cv_prog_gcc_traditional'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else ac_pattern="Autoconf.*'x'" cat > conftest.$ac_ext < Autoconf TIOCGETP @@ -1807,7 +1832,7 @@ rm -f conftest* if test $ac_cv_prog_gcc_traditional = no; then cat > conftest.$ac_ext < Autoconf TCGETA @@ -1829,7 +1854,7 @@ echo "$ac_t""$ac_cv_prog_gcc_traditional" 1>&6 fi echo $ac_n "checking whether setpgrp takes no argument""... $ac_c" 1>&6 -echo "configure:1833: checking whether setpgrp takes no argument" >&5 +echo "configure:1858: checking whether setpgrp takes no argument" >&5 if eval "test \"`echo '$''{'ac_cv_func_setpgrp_void'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -1837,7 +1862,7 @@ else { echo "configure: error: cannot check setpgrp if cross compiling" 1>&2; exit 1; } else cat > conftest.$ac_ext <&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:1886: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_func_setpgrp_void=no else @@ -1881,12 +1906,12 @@ EOF fi echo $ac_n "checking return type of signal handlers""... $ac_c" 1>&6 -echo "configure:1885: checking return type of signal handlers" >&5 +echo "configure:1910: checking return type of signal handlers" >&5 if eval "test \"`echo '$''{'ac_cv_type_signal'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #include @@ -1903,7 +1928,7 @@ int main() { int i; ; return 0; } EOF -if { (eval echo configure:1907: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:1932: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* ac_cv_type_signal=void else @@ -1924,12 +1949,12 @@ EOF for ac_func in mktime select socket arc4random do echo $ac_n "checking for $ac_func""... $ac_c" 1>&6 -echo "configure:1928: checking for $ac_func" >&5 +echo "configure:1953: checking for $ac_func" >&5 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext <&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then +if { (eval echo configure:1981: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then rm -rf conftest* eval "ac_cv_func_$ac_func=yes" else @@ -1978,9 +2003,9 @@ done echo $ac_n "checking for IPv6 resolver type""... $ac_c" 1>&6 -echo "configure:1982: checking for IPv6 resolver type" >&5 +echo "configure:2007: checking for IPv6 resolver type" >&5 cat > conftest.$ac_ext < #include @@ -1990,7 +2015,7 @@ int main() { _res_ext.nsaddr_list[0].__ss_family = 1; ; return 0; } EOF -if { (eval echo configure:1994: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:2019: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* cat >> confdefs.h <<\EOF #define RESOLVER_KAME 1 @@ -2002,7 +2027,7 @@ else cat conftest.$ac_ext >&5 rm -rf conftest* cat > conftest.$ac_ext < #include @@ -2012,7 +2037,7 @@ int main() { _res_ext.nsaddr_list[0].ss_family = 1; ; return 0; } EOF -if { (eval echo configure:2016: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:2041: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* cat >> confdefs.h <<\EOF #define RESOLVER_KAME 1 @@ -2024,7 +2049,7 @@ else cat conftest.$ac_ext >&5 rm -rf conftest* cat > conftest.$ac_ext < #include @@ -2034,7 +2059,7 @@ int main() { _res.nsaddr_list_un[0].sin6.sin6_family = 1; ; return 0; } EOF -if { (eval echo configure:2038: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:2063: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* cat >> confdefs.h <<\EOF #define RESOLVER_OLDKAME 1 @@ -2046,7 +2071,7 @@ else cat conftest.$ac_ext >&5 rm -rf conftest* cat > conftest.$ac_ext < #include @@ -2056,7 +2081,7 @@ int main() { _res.nsaddr_list[0].sin_family = 1; ; return 0; } EOF -if { (eval echo configure:2060: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:2085: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* cat >> confdefs.h <<\EOF #define RESOLVER_V4 1 @@ -2080,7 +2105,7 @@ echo "$ac_t""$resolver" 1>&6 echo $ac_n "checking if --enable-pedant option is specified""... $ac_c" 1>&6 -echo "configure:2084: checking if --enable-pedant option is specified" >&5 +echo "configure:2109: checking if --enable-pedant option is specified" >&5 # Check whether --enable-pedant or --disable-pedant was given. if test "${enable_pedant+set}" = set; then enableval="$enable_pedant" @@ -2094,7 +2119,7 @@ fi echo "$ac_t""$enableval" 1>&6; echo $ac_n "checking if --enable-debug option is specified""... $ac_c" 1>&6 -echo "configure:2098: checking if --enable-debug option is specified" >&5 +echo "configure:2123: checking if --enable-debug option is specified" >&5 # Check whether --enable-debug or --disable-debug was given. if test "${enable_debug+set}" = set; then enableval="$enable_debug" diff --git a/kame/kame/mdnsd/configure.in b/kame/kame/mdnsd/configure.in index a636cf0609..d280f9e72f 100644 --- a/kame/kame/mdnsd/configure.in +++ b/kame/kame/mdnsd/configure.in @@ -75,10 +75,17 @@ AC_CHECK_HEADERS(fcntl.h sys/ioctl.h sys/time.h syslog.h unistd.h ifaddrs.h) AC_MSG_CHECKING(for socklen_t) AC_TRY_COMPILE([#include #include ], - [socklen_t x; x = 1; return x;], + [socklen_t x; x = 1; return x;], [AC_MSG_RESULT(yes)], [AC_MSG_RESULT(no) AC_CHECK_TYPE(socklen_t, unsigned int)]) +AC_MSG_CHECKING(for sa_len) +AC_TRY_COMPILE([#include +#include ], + [struct sockaddr sa; sa.sa_len = sizeof(sa); return sa.sa_len; ], + [AC_MSG_RESULT(yes) + AC_DEFINE(HAVE_SA_LEN)], + [AC_MSG_RESULT(no)]) dnl Checks for typedefs, structures, and compiler characteristics. AC_C_CONST diff --git a/kame/kame/mdnsd/mainloop.c b/kame/kame/mdnsd/mainloop.c index 94ec17c2e0..0277416d39 100644 --- a/kame/kame/mdnsd/mainloop.c +++ b/kame/kame/mdnsd/mainloop.c @@ -1,4 +1,4 @@ -/* $KAME: mainloop.c,v 1.93 2002/09/10 02:10:33 itojun Exp $ */ +/* $KAME: mainloop.c,v 1.94 2002/09/19 01:43:34 itojun Exp $ */ /* * Copyright (C) 2000 WIDE Project. @@ -1126,8 +1126,23 @@ match_ptrquery(n) continue; if (ifmatch && strcmp(ifa->ifa_name, ifmatch) != 0) continue; +#ifdef HAVE_SA_LEN if (ifa->ifa_addr->sa_len < off + alen) continue; +#else + /* + * We assume that sa_len is the same if sa_family is the same, + * however, it is not a safe assumption to make, so we + * check if sa_family is the ones we know of. + */ + switch (ifa->ifa_addr->sa_family) { + case AF_INET: + case AF_INET6: + break; + default: + continue; + } +#endif if (memcmp(((char *)ifa->ifa_addr) + off, addr, alen) == 0) { freeifaddrs(ifap); return 1; diff --git a/kame/kame/mdnsd/mdnsd.c b/kame/kame/mdnsd/mdnsd.c index 9633bb7e0c..080d63622b 100644 --- a/kame/kame/mdnsd/mdnsd.c +++ b/kame/kame/mdnsd/mdnsd.c @@ -1,4 +1,4 @@ -/* $KAME: mdnsd.c,v 1.56 2002/09/10 02:04:08 itojun Exp $ */ +/* $KAME: mdnsd.c,v 1.57 2002/09/19 01:43:34 itojun Exp $ */ /* * Copyright (C) 2000 WIDE Project. @@ -601,7 +601,6 @@ addserv(n, ttl, comment) struct addrinfo hints, *res; struct sockaddr_in *sin; struct sockaddr_in6 *sin6; - int flags; struct nsdb *ns; int multicast; @@ -694,8 +693,7 @@ ismyaddr(sa, salen) } #endif h1[0] = h2[0] = '\0'; - if (getnameinfo((struct sockaddr *)&ss[0], - ((struct sockaddr *)&ss[0])->sa_len, h1, sizeof(h1), + if (getnameinfo((struct sockaddr *)&ss[0], salen, h1, sizeof(h1), p, sizeof(p), niflag) != 0) return 0; #if 1 /*just for experiment - to run two servers on a single node*/ @@ -709,11 +707,26 @@ ismyaddr(sa, salen) for (ifa = ifap; ifa; ifa = ifa->ifa_next) { if (ifa->ifa_addr->sa_family != sa->sa_family) continue; - if (ifa->ifa_addr->sa_len != sa->sa_len || +#ifdef HAVE_SA_LEN + if (ifa->ifa_addr->sa_len != salen || ifa->ifa_addr->sa_len > sizeof(ss[1])) { continue; } - memcpy(&ss[1], ifa->ifa_addr, ifa->ifa_addr->sa_len); +#else + /* + * We assume that sa_len is the same if sa_family is the same, + * however, it is not a safe assumption to make, so we + * check if sa_family is the ones we know of. + */ + switch (ifa->ifa_addr->sa_family) { + case AF_INET: + case AF_INET6: + break; + default: + continue; + } +#endif + memcpy(&ss[1], ifa->ifa_addr, salen); scope[1] = 0; #ifdef __KAME__ if (((struct sockaddr *)&ss[1])->sa_family == AF_INET6) { @@ -728,9 +741,8 @@ ismyaddr(sa, salen) } } #endif - if (getnameinfo((struct sockaddr *)&ss[1], - ((struct sockaddr *)&ss[1])->sa_len, h2, sizeof(h2), - NULL, 0, niflag) != 0) + if (getnameinfo((struct sockaddr *)&ss[1], salen, + h2, sizeof(h2), NULL, 0, niflag) != 0) continue; if (strcmp(h1, h2) != 0) continue;