Index: zebra/acconfig.h
diff -u zebra/acconfig.h:1.1.2.1 zebra/acconfig.h:1.3
--- zebra/acconfig.h:1.1.2.1	Fri Dec  7 14:24:52 2001
+++ zebra/acconfig.h	Fri Dec  7 14:39:56 2001
@@ -100,6 +100,9 @@
 /* Define if disable-bgp-announce option is specified. */
 #undef DISABLE_BGP_ANNOUNCE
 
+/* Define if bgp_dnssort option is specified. */
+#undef BGP_DNSSORT
+
 /* PAM support */
 #undef USE_PAM
 
Index: zebra/config.h.in
diff -u zebra/config.h.in:1.1.2.1 zebra/config.h.in:1.3
--- zebra/config.h.in:1.1.2.1	Fri Dec  7 14:24:52 2001
+++ zebra/config.h.in	Fri Dec  7 14:39:56 2001
@@ -110,6 +110,9 @@
 /* Define if disable-bgp-announce option is specified. */
 #undef DISABLE_BGP_ANNOUNCE
 
+/* Define if bgp_dnssort option is specified. */
+#undef BGP_DNSSORT
+
 /* PAM support */
 #undef USE_PAM
 
Index: zebra/configure
diff -u zebra/configure:1.1.2.1 zebra/configure:1.3
--- zebra/configure:1.1.2.1	Fri Dec  7 14:24:53 2001
+++ zebra/configure	Fri Dec  7 14:39:56 2001
@@ -43,6 +43,8 @@
   --enable-nssa           enable OSPF NSSA option"
 ac_help="$ac_help
   --enable-multipath=ARG  enable multipath function, ARG must be digit"
+ac_help="$ac_help
+  --enable-bgp-dnssort    enable DNSsort protocol"
 
 # Initialize some variables set by options.
 # The variables have the same names as the options, with
@@ -584,7 +586,7 @@
 # SVR4 /usr/ucb/install, which tries to use the nonexistent group "staff"
 # ./install, which can be erroneously created by make from ./install.sh.
 echo $ac_n "checking for a BSD compatible install""... $ac_c" 1>&6
-echo "configure:588: checking for a BSD compatible install" >&5
+echo "configure:590: checking for a BSD compatible install" >&5
 if test -z "$INSTALL"; then
 if eval "test \"`echo '$''{'ac_cv_path_install'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -637,7 +639,7 @@
 test -z "$INSTALL_DATA" && INSTALL_DATA='${INSTALL} -m 644'
 
 echo $ac_n "checking whether build environment is sane""... $ac_c" 1>&6
-echo "configure:641: checking whether build environment is sane" >&5
+echo "configure:643: checking whether build environment is sane" >&5
 # Just in case
 sleep 1
 echo timestamp > conftestfile
@@ -694,7 +696,7 @@
 test "$program_transform_name" = "" && program_transform_name="s,x,x,"
 
 echo $ac_n "checking whether ${MAKE-make} sets \${MAKE}""... $ac_c" 1>&6
-echo "configure:698: checking whether ${MAKE-make} sets \${MAKE}" >&5
+echo "configure:700: checking whether ${MAKE-make} sets \${MAKE}" >&5
 set dummy ${MAKE-make}; ac_make=`echo "$2" | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_prog_make_${ac_make}_set'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -740,7 +742,7 @@
 
 missing_dir=`cd $ac_aux_dir && pwd`
 echo $ac_n "checking for working aclocal""... $ac_c" 1>&6
-echo "configure:744: checking for working aclocal" >&5
+echo "configure:746: checking for working aclocal" >&5
 # Run test in a subshell; some versions of sh will print an error if
 # an executable is not found, even if stderr is redirected.
 # Redirect stdin to placate older versions of autoconf.  Sigh.
@@ -753,7 +755,7 @@
 fi
 
 echo $ac_n "checking for working autoconf""... $ac_c" 1>&6
-echo "configure:757: checking for working autoconf" >&5
+echo "configure:759: checking for working autoconf" >&5
 # Run test in a subshell; some versions of sh will print an error if
 # an executable is not found, even if stderr is redirected.
 # Redirect stdin to placate older versions of autoconf.  Sigh.
@@ -766,7 +768,7 @@
 fi
 
 echo $ac_n "checking for working automake""... $ac_c" 1>&6
-echo "configure:770: checking for working automake" >&5
+echo "configure:772: checking for working automake" >&5
 # Run test in a subshell; some versions of sh will print an error if
 # an executable is not found, even if stderr is redirected.
 # Redirect stdin to placate older versions of autoconf.  Sigh.
@@ -779,7 +781,7 @@
 fi
 
 echo $ac_n "checking for working autoheader""... $ac_c" 1>&6
-echo "configure:783: checking for working autoheader" >&5
+echo "configure:785: checking for working autoheader" >&5
 # Run test in a subshell; some versions of sh will print an error if
 # an executable is not found, even if stderr is redirected.
 # Redirect stdin to placate older versions of autoconf.  Sigh.
@@ -792,7 +794,7 @@
 fi
 
 echo $ac_n "checking for working makeinfo""... $ac_c" 1>&6
-echo "configure:796: checking for working makeinfo" >&5
+echo "configure:798: checking for working makeinfo" >&5
 # Run test in a subshell; some versions of sh will print an error if
 # an executable is not found, even if stderr is redirected.
 # Redirect stdin to placate older versions of autoconf.  Sigh.
@@ -814,13 +816,15 @@
 
 
 
+
+
 # Make sure we can run config.sub.
 if ${CONFIG_SHELL-/bin/sh} $ac_config_sub sun4 >/dev/null 2>&1; then :
 else { echo "configure: error: can not run $ac_config_sub" 1>&2; exit 1; }
 fi
 
 echo $ac_n "checking host system type""... $ac_c" 1>&6
-echo "configure:826: checking host system type" >&5
+echo "configure:828: checking host system type" >&5
 
 host_alias=$host
 case "$host_alias" in
@@ -846,7 +850,7 @@
 # Extract the first word of "gcc", so it can be a program name with args.
 set dummy gcc; ac_word=$2
 echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
-echo "configure:852: checking for $ac_word" >&5
+echo "configure:854: checking for $ac_word" >&5
 if eval "test \"`echo '$''{'ac_cv_prog_CC'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -876,7 +880,7 @@
   # Extract the first word of "cc", so it can be a program name with args.
 set dummy cc; ac_word=$2
 echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
-echo "configure:882: checking for $ac_word" >&5
+echo "configure:884: checking for $ac_word" >&5
 if eval "test \"`echo '$''{'ac_cv_prog_CC'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -927,7 +931,7 @@
       # Extract the first word of "cl", so it can be a program name with args.
 set dummy cl; ac_word=$2
 echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
-echo "configure:933: checking for $ac_word" >&5
+echo "configure:935: checking for $ac_word" >&5
 if eval "test \"`echo '$''{'ac_cv_prog_CC'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -959,7 +963,7 @@
 fi
 
 echo $ac_n "checking whether the C compiler ($CC $CFLAGS $LDFLAGS) works""... $ac_c" 1>&6
-echo "configure:965: checking whether the C compiler ($CC $CFLAGS $LDFLAGS) works" >&5
+echo "configure:967: checking whether the C compiler ($CC $CFLAGS $LDFLAGS) works" >&5
 
 ac_ext=c
 # CFLAGS is not in ac_cpp because -g, -O, etc. are not valid cpp options.
@@ -970,12 +974,12 @@
 
 cat > conftest.$ac_ext << EOF
 
-#line 976 "configure"
+#line 978 "configure"
 #include "confdefs.h"
 
 main(){return(0);}
 EOF
-if { (eval echo configure:981: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:983: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   ac_cv_prog_cc_works=yes
   # If we can't run a trivial program, we are probably using a cross compiler.
   if (./conftest; exit) 2>/dev/null; then
@@ -1001,12 +1005,12 @@
   { echo "configure: error: installation or configuration problem: C compiler cannot create executables." 1>&2; exit 1; }
 fi
 echo $ac_n "checking whether the C compiler ($CC $CFLAGS $LDFLAGS) is a cross-compiler""... $ac_c" 1>&6
-echo "configure:1007: checking whether the C compiler ($CC $CFLAGS $LDFLAGS) is a cross-compiler" >&5
+echo "configure:1009: checking whether the C compiler ($CC $CFLAGS $LDFLAGS) is a cross-compiler" >&5
 echo "$ac_t""$ac_cv_prog_cc_cross" 1>&6
 cross_compiling=$ac_cv_prog_cc_cross
 
 echo $ac_n "checking whether we are using GNU C""... $ac_c" 1>&6
-echo "configure:1012: checking whether we are using GNU C" >&5
+echo "configure:1014: checking whether we are using GNU C" >&5
 if eval "test \"`echo '$''{'ac_cv_prog_gcc'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -1015,7 +1019,7 @@
   yes;
 #endif
 EOF
-if { ac_try='${CC-cc} -E conftest.c'; { (eval echo configure:1021: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }; } | egrep yes >/dev/null 2>&1; then
+if { ac_try='${CC-cc} -E conftest.c'; { (eval echo configure:1023: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }; } | egrep yes >/dev/null 2>&1; then
   ac_cv_prog_gcc=yes
 else
   ac_cv_prog_gcc=no
@@ -1034,7 +1038,7 @@
 ac_save_CFLAGS="$CFLAGS"
 CFLAGS=
 echo $ac_n "checking whether ${CC-cc} accepts -g""... $ac_c" 1>&6
-echo "configure:1040: checking whether ${CC-cc} accepts -g" >&5
+echo "configure:1042: checking whether ${CC-cc} accepts -g" >&5
 if eval "test \"`echo '$''{'ac_cv_prog_cc_g'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -1072,7 +1076,7 @@
 fi
 
 echo $ac_n "checking how to run the C preprocessor""... $ac_c" 1>&6
-echo "configure:1078: checking how to run the C preprocessor" >&5
+echo "configure:1080: checking how to run the C preprocessor" >&5
 # On Suns, sometimes $CPP names a directory.
 if test -n "$CPP" && test -d "$CPP"; then
   CPP=
@@ -1087,13 +1091,13 @@
   # On the NeXT, cc -E runs the code through the compiler's parser,
   # not just through cpp.
   cat > conftest.$ac_ext <<EOF
-#line 1093 "configure"
+#line 1095 "configure"
 #include "confdefs.h"
 #include <assert.h>
 Syntax Error
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:1099: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:1101: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"`
 if test -z "$ac_err"; then
   :
@@ -1104,13 +1108,13 @@
   rm -rf conftest*
   CPP="${CC-cc} -E -traditional-cpp"
   cat > conftest.$ac_ext <<EOF
-#line 1110 "configure"
+#line 1112 "configure"
 #include "confdefs.h"
 #include <assert.h>
 Syntax Error
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:1116: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:1118: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"`
 if test -z "$ac_err"; then
   :
@@ -1121,13 +1125,13 @@
   rm -rf conftest*
   CPP="${CC-cc} -nologo -E"
   cat > conftest.$ac_ext <<EOF
-#line 1127 "configure"
+#line 1129 "configure"
 #include "confdefs.h"
 #include <assert.h>
 Syntax Error
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:1133: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:1135: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"`
 if test -z "$ac_err"; then
   :
@@ -1163,7 +1167,7 @@
 # SVR4 /usr/ucb/install, which tries to use the nonexistent group "staff"
 # ./install, which can be erroneously created by make from ./install.sh.
 echo $ac_n "checking for a BSD compatible install""... $ac_c" 1>&6
-echo "configure:1169: checking for a BSD compatible install" >&5
+echo "configure:1171: checking for a BSD compatible install" >&5
 if test -z "$INSTALL"; then
 if eval "test \"`echo '$''{'ac_cv_path_install'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -1216,7 +1220,7 @@
 test -z "$INSTALL_DATA" && INSTALL_DATA='${INSTALL} -m 644'
 
 echo $ac_n "checking whether ${MAKE-make} sets \${MAKE}""... $ac_c" 1>&6
-echo "configure:1222: checking whether ${MAKE-make} sets \${MAKE}" >&5
+echo "configure:1224: checking whether ${MAKE-make} sets \${MAKE}" >&5
 set dummy ${MAKE-make}; ac_make=`echo "$2" | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_prog_make_${ac_make}_set'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -1243,7 +1247,7 @@
 fi
 
 echo $ac_n "checking build system type""... $ac_c" 1>&6
-echo "configure:1249: checking build system type" >&5
+echo "configure:1251: checking build system type" >&5
 
 build_alias=$build
 case "$build_alias" in
@@ -1269,7 +1273,7 @@
 # Extract the first word of "${ac_tool_prefix}ar", so it can be a program name with args.
 set dummy ${ac_tool_prefix}ar; ac_word=$2
 echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
-echo "configure:1275: checking for $ac_word" >&5
+echo "configure:1277: checking for $ac_word" >&5
 if eval "test \"`echo '$''{'ac_cv_prog_AR'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -1301,7 +1305,7 @@
 # Extract the first word of "${ac_tool_prefix}ranlib", so it can be a program name with args.
 set dummy ${ac_tool_prefix}ranlib; ac_word=$2
 echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
-echo "configure:1307: checking for $ac_word" >&5
+echo "configure:1309: checking for $ac_word" >&5
 if eval "test \"`echo '$''{'ac_cv_prog_RANLIB'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -1333,7 +1337,7 @@
   # Extract the first word of "ranlib", so it can be a program name with args.
 set dummy ranlib; ac_word=$2
 echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
-echo "configure:1339: checking for $ac_word" >&5
+echo "configure:1341: checking for $ac_word" >&5
 if eval "test \"`echo '$''{'ac_cv_prog_RANLIB'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -1367,9 +1371,9 @@
 
 
 echo $ac_n "checking for AIX""... $ac_c" 1>&6
-echo "configure:1373: checking for AIX" >&5
+echo "configure:1375: checking for AIX" >&5
 cat > conftest.$ac_ext <<EOF
-#line 1375 "configure"
+#line 1377 "configure"
 #include "confdefs.h"
 #ifdef _AIX
   yes
@@ -1487,6 +1491,12 @@
   :
 fi
 
+# Check whether --enable-bgp-dnssort or --disable-bgp-dnssort was given.
+if test "${enable_bgp_dnssort+set}" = set; then
+  enableval="$enable_bgp_dnssort"
+  :
+fi
+
 
 if test "${enable_broken_aliases}" = "yes"; then
   if test "${enable_netlink}" = "yes"
@@ -1534,12 +1544,12 @@
 
 
 echo $ac_n "checking for ANSI C header files""... $ac_c" 1>&6
-echo "configure:1540: checking for ANSI C header files" >&5
+echo "configure:1548: checking for ANSI C header files" >&5
 if eval "test \"`echo '$''{'ac_cv_header_stdc'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 1545 "configure"
+#line 1553 "configure"
 #include "confdefs.h"
 #include <stdlib.h>
 #include <stdarg.h>
@@ -1547,7 +1557,7 @@
 #include <float.h>
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:1553: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:1561: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"`
 if test -z "$ac_err"; then
   rm -rf conftest*
@@ -1564,7 +1574,7 @@
 if test $ac_cv_header_stdc = yes; then
   # SunOS 4.x string.h does not declare mem*, contrary to ANSI.
 cat > conftest.$ac_ext <<EOF
-#line 1570 "configure"
+#line 1578 "configure"
 #include "confdefs.h"
 #include <string.h>
 EOF
@@ -1582,7 +1592,7 @@
 if test $ac_cv_header_stdc = yes; then
   # ISC 2.0.2 stdlib.h does not declare free, contrary to ANSI.
 cat > conftest.$ac_ext <<EOF
-#line 1588 "configure"
+#line 1596 "configure"
 #include "confdefs.h"
 #include <stdlib.h>
 EOF
@@ -1603,7 +1613,7 @@
   :
 else
   cat > conftest.$ac_ext <<EOF
-#line 1609 "configure"
+#line 1617 "configure"
 #include "confdefs.h"
 #include <ctype.h>
 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
@@ -1614,7 +1624,7 @@
 exit (0); }
 
 EOF
-if { (eval echo configure:1620: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:1628: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
 then
   :
 else
@@ -1641,17 +1651,17 @@
 do
 ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'`
 echo $ac_n "checking for $ac_hdr""... $ac_c" 1>&6
-echo "configure:1647: checking for $ac_hdr" >&5
+echo "configure:1655: checking for $ac_hdr" >&5
 if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 1652 "configure"
+#line 1660 "configure"
 #include "confdefs.h"
 #include <$ac_hdr>
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:1657: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:1665: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"`
 if test -z "$ac_err"; then
   rm -rf conftest*
@@ -1679,12 +1689,12 @@
 
 
 echo $ac_n "checking for working const""... $ac_c" 1>&6
-echo "configure:1685: checking for working const" >&5
+echo "configure:1693: 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 <<EOF
-#line 1690 "configure"
+#line 1698 "configure"
 #include "confdefs.h"
 
 int main() {
@@ -1733,7 +1743,7 @@
 
 ; return 0; }
 EOF
-if { (eval echo configure:1739: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:1747: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_const=yes
 else
@@ -1754,12 +1764,12 @@
 fi
 
 echo $ac_n "checking return type of signal handlers""... $ac_c" 1>&6
-echo "configure:1760: checking return type of signal handlers" >&5
+echo "configure:1768: 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 <<EOF
-#line 1765 "configure"
+#line 1773 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <signal.h>
@@ -1776,7 +1786,7 @@
 int i;
 ; return 0; }
 EOF
-if { (eval echo configure:1782: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:1790: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_type_signal=void
 else
@@ -1803,7 +1813,7 @@
 EOF
 
       echo $ac_n "checking for main in -lxnet""... $ac_c" 1>&6
-echo "configure:1809: checking for main in -lxnet" >&5
+echo "configure:1817: checking for main in -lxnet" >&5
 ac_lib_var=`echo xnet'_'main | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -1811,14 +1821,14 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lxnet  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 1817 "configure"
+#line 1825 "configure"
 #include "confdefs.h"
 
 int main() {
 main()
 ; return 0; }
 EOF
-if { (eval echo configure:1824: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1832: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -1853,7 +1863,7 @@
 EOF
 
       echo $ac_n "checking for main in -lsocket""... $ac_c" 1>&6
-echo "configure:1859: checking for main in -lsocket" >&5
+echo "configure:1867: checking for main in -lsocket" >&5
 ac_lib_var=`echo socket'_'main | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -1861,14 +1871,14 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lsocket  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 1867 "configure"
+#line 1875 "configure"
 #include "confdefs.h"
 
 int main() {
 main()
 ; return 0; }
 EOF
-if { (eval echo configure:1874: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1882: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -1896,7 +1906,7 @@
 fi
 
       echo $ac_n "checking for main in -lnsl""... $ac_c" 1>&6
-echo "configure:1902: checking for main in -lnsl" >&5
+echo "configure:1910: checking for main in -lnsl" >&5
 ac_lib_var=`echo nsl'_'main | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -1904,14 +1914,14 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lnsl  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 1910 "configure"
+#line 1918 "configure"
 #include "confdefs.h"
 
 int main() {
 main()
 ; return 0; }
 EOF
-if { (eval echo configure:1917: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1925: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -1949,7 +1959,7 @@
   ;;
   *-nec-sysv4*)
       echo $ac_n "checking for gethostbyname in -lnsl""... $ac_c" 1>&6
-echo "configure:1955: checking for gethostbyname in -lnsl" >&5
+echo "configure:1963: checking for gethostbyname in -lnsl" >&5
 ac_lib_var=`echo nsl'_'gethostbyname | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -1957,7 +1967,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lnsl  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 1963 "configure"
+#line 1971 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -1968,7 +1978,7 @@
 gethostbyname()
 ; return 0; }
 EOF
-if { (eval echo configure:1974: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:1982: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -1996,7 +2006,7 @@
 fi
 
       echo $ac_n "checking for socket in -lsocket""... $ac_c" 1>&6
-echo "configure:2002: checking for socket in -lsocket" >&5
+echo "configure:2010: checking for socket in -lsocket" >&5
 ac_lib_var=`echo socket'_'socket | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2004,7 +2014,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lsocket  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2010 "configure"
+#line 2018 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -2015,7 +2025,7 @@
 socket()
 ; return 0; }
 EOF
-if { (eval echo configure:2021: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2029: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2060,7 +2070,7 @@
       opsys=bsdi
       OTHER_METHOD="mtu_kvm.o"
       echo $ac_n "checking for main in -lkvm""... $ac_c" 1>&6
-echo "configure:2066: checking for main in -lkvm" >&5
+echo "configure:2074: checking for main in -lkvm" >&5
 ac_lib_var=`echo kvm'_'main | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2068,14 +2078,14 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lkvm  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2074 "configure"
+#line 2082 "configure"
 #include "confdefs.h"
 
 int main() {
 main()
 ; return 0; }
 EOF
-if { (eval echo configure:2081: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2089: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2121,7 +2131,7 @@
 EOF
 
 	 echo $ac_n "checking for tputs in -ltinfo""... $ac_c" 1>&6
-echo "configure:2127: checking for tputs in -ltinfo" >&5
+echo "configure:2135: checking for tputs in -ltinfo" >&5
 ac_lib_var=`echo tinfo'_'tputs | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2129,7 +2139,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-ltinfo  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2135 "configure"
+#line 2143 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -2140,7 +2150,7 @@
 tputs()
 ; return 0; }
 EOF
-if { (eval echo configure:2146: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2154: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2166,7 +2176,7 @@
 else
   echo "$ac_t""no" 1>&6
 echo $ac_n "checking for tputs in -lncurses""... $ac_c" 1>&6
-echo "configure:2172: checking for tputs in -lncurses" >&5
+echo "configure:2180: checking for tputs in -lncurses" >&5
 ac_lib_var=`echo ncurses'_'tputs | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2174,7 +2184,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lncurses  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2180 "configure"
+#line 2188 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -2185,7 +2195,7 @@
 tputs()
 ; return 0; }
 EOF
-if { (eval echo configure:2191: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2199: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2215,7 +2225,7 @@
 fi
 
          echo $ac_n "checking for main in -lreadline""... $ac_c" 1>&6
-echo "configure:2221: checking for main in -lreadline" >&5
+echo "configure:2229: checking for main in -lreadline" >&5
 ac_lib_var=`echo readline'_'main | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2223,14 +2233,14 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lreadline  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2229 "configure"
+#line 2237 "configure"
 #include "confdefs.h"
 
 int main() {
 main()
 ; return 0; }
 EOF
-if { (eval echo configure:2236: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2244: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2262,17 +2272,17 @@
          fi
 	 ac_safe=`echo "readline/history.h" | sed 'y%./+-%__p_%'`
 echo $ac_n "checking for readline/history.h""... $ac_c" 1>&6
-echo "configure:2268: checking for readline/history.h" >&5
+echo "configure:2276: checking for readline/history.h" >&5
 if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 2273 "configure"
+#line 2281 "configure"
 #include "confdefs.h"
 #include <readline/history.h>
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:2278: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:2286: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"`
 if test -z "$ac_err"; then
   rm -rf conftest*
@@ -2303,7 +2313,7 @@
 
 if test "$with_libpam" = "yes"; then
 echo $ac_n "checking for pam_start in -lpam""... $ac_c" 1>&6
-echo "configure:2309: checking for pam_start in -lpam" >&5
+echo "configure:2317: checking for pam_start in -lpam" >&5
 ac_lib_var=`echo pam'_'pam_start | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2311,7 +2321,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lpam  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2317 "configure"
+#line 2325 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -2322,7 +2332,7 @@
 pam_start()
 ; return 0; }
 EOF
-if { (eval echo configure:2328: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2336: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2338,7 +2348,7 @@
 if eval "test \"`echo '$ac_cv_lib_'$ac_lib_var`\" = yes"; then
   echo "$ac_t""yes" 1>&6
   echo $ac_n "checking for misc_conv in -lpam""... $ac_c" 1>&6
-echo "configure:2344: checking for misc_conv in -lpam" >&5
+echo "configure:2352: checking for misc_conv in -lpam" >&5
 ac_lib_var=`echo pam'_'misc_conv | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2346,7 +2356,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lpam  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2352 "configure"
+#line 2360 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -2357,7 +2367,7 @@
 misc_conv()
 ; return 0; }
 EOF
-if { (eval echo configure:2363: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2371: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2376,7 +2386,7 @@
 #define USE_PAM 1
 EOF
 
-  LIBPAM="-lpam"
+     LIBPAM="-lpam"
 else
   echo "$ac_t""no" 1>&6
 cat >> confdefs.h <<\EOF
@@ -2391,7 +2401,7 @@
 else
   echo "$ac_t""no" 1>&6
 echo $ac_n "checking for pam_end in -lpam""... $ac_c" 1>&6
-echo "configure:2397: checking for pam_end in -lpam" >&5
+echo "configure:2405: checking for pam_end in -lpam" >&5
 ac_lib_var=`echo pam'_'pam_end | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2399,7 +2409,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lpam -ldl $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2405 "configure"
+#line 2413 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -2410,7 +2420,7 @@
 pam_end()
 ; return 0; }
 EOF
-if { (eval echo configure:2416: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2424: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2426,7 +2436,7 @@
 if eval "test \"`echo '$ac_cv_lib_'$ac_lib_var`\" = yes"; then
   echo "$ac_t""yes" 1>&6
   echo $ac_n "checking for misc_conv in -lpam""... $ac_c" 1>&6
-echo "configure:2432: checking for misc_conv in -lpam" >&5
+echo "configure:2440: checking for misc_conv in -lpam" >&5
 ac_lib_var=`echo pam'_'misc_conv | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2434,7 +2444,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lpam  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2440 "configure"
+#line 2448 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -2445,7 +2455,7 @@
 misc_conv()
 ; return 0; }
 EOF
-if { (eval echo configure:2451: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2459: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2489,14 +2499,14 @@
 
 
 echo $ac_n "checking whether byte ordering is bigendian""... $ac_c" 1>&6
-echo "configure:2495: checking whether byte ordering is bigendian" >&5
+echo "configure:2503: checking whether byte ordering is bigendian" >&5
 if eval "test \"`echo '$''{'ac_cv_c_bigendian'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   ac_cv_c_bigendian=unknown
 # See if sys/param.h defines the BYTE_ORDER macro.
 cat > conftest.$ac_ext <<EOF
-#line 2502 "configure"
+#line 2510 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/param.h>
@@ -2507,11 +2517,11 @@
 #endif
 ; return 0; }
 EOF
-if { (eval echo configure:2513: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:2521: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   # It does; now see whether it defined to BIG_ENDIAN or not.
 cat > conftest.$ac_ext <<EOF
-#line 2517 "configure"
+#line 2525 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/param.h>
@@ -2522,7 +2532,7 @@
 #endif
 ; return 0; }
 EOF
-if { (eval echo configure:2528: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:2536: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_bigendian=yes
 else
@@ -2542,7 +2552,7 @@
   ac_cv_c_bigendian=no
 else
   cat > conftest.$ac_ext <<EOF
-#line 2548 "configure"
+#line 2556 "configure"
 #include "confdefs.h"
 main () {
   /* Are we little or big endian?  From Harbison&Steele.  */
@@ -2555,7 +2565,7 @@
   exit (u.c[sizeof (long) - 1] == 1);
 }
 EOF
-if { (eval echo configure:2561: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:2569: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
 then
   ac_cv_c_bigendian=no
 else
@@ -2583,12 +2593,12 @@
 for ac_func in bcopy bzero strerror inet_aton daemon snprintf vsnprintf strlcat strlcpy if_nametoindex if_indextoname getifaddrs
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:2589: checking for $ac_func" >&5
+echo "configure:2597: 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 <<EOF
-#line 2594 "configure"
+#line 2602 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -2611,7 +2621,7 @@
 
 ; return 0; }
 EOF
-if { (eval echo configure:2617: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2625: \"$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
@@ -2638,12 +2648,12 @@
 for ac_func in setproctitle
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:2644: checking for $ac_func" >&5
+echo "configure:2652: 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 <<EOF
-#line 2649 "configure"
+#line 2657 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -2666,7 +2676,7 @@
 
 ; return 0; }
 EOF
-if { (eval echo configure:2672: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2680: \"$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
@@ -2688,7 +2698,7 @@
 else
   echo "$ac_t""no" 1>&6
 echo $ac_n "checking for setproctitle in -lutil""... $ac_c" 1>&6
-echo "configure:2694: checking for setproctitle in -lutil" >&5
+echo "configure:2702: checking for setproctitle in -lutil" >&5
 ac_lib_var=`echo util'_'setproctitle | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -2696,7 +2706,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lutil  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 2702 "configure"
+#line 2710 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -2707,7 +2717,7 @@
 setproctitle()
 ; return 0; }
 EOF
-if { (eval echo configure:2713: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2721: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -2735,7 +2745,7 @@
 
 
 echo $ac_n "checking zebra between kernel interface method""... $ac_c" 1>&6
-echo "configure:2741: checking zebra between kernel interface method" >&5
+echo "configure:2749: checking zebra between kernel interface method" >&5
 if test x"$opsys" = x"gnu-linux"; then
   if test "${enable_netlink}" = "yes";then
     echo "$ac_t""netlink" 1>&6
@@ -2751,7 +2761,7 @@
     netlink=no
   else
     cat > conftest.$ac_ext <<EOF
-#line 2757 "configure"
+#line 2765 "configure"
 #include "confdefs.h"
 #include <linux/autoconf.h>
 #include <linux/version.h>
@@ -2792,7 +2802,7 @@
    echo "$ac_t""socket" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 2798 "configure"
+#line 2806 "configure"
 #include "confdefs.h"
 #include <errno.h>
 #include <sys/types.h>
@@ -2808,7 +2818,7 @@
   exit (0);
 }
 EOF
-if { (eval echo configure:2814: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:2822: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null
 then
   cat >> confdefs.h <<\EOF
 #define HAVE_AF_ROUTE 1
@@ -2834,7 +2844,7 @@
 
 
 echo $ac_n "checking route read method check""... $ac_c" 1>&6
-echo "configure:2840: checking route read method check" >&5
+echo "configure:2848: checking route read method check" >&5
 if eval "test \"`echo '$''{'zebra_rtread'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -2861,7 +2871,7 @@
 
 
 echo $ac_n "checking interface looking up method""... $ac_c" 1>&6
-echo "configure:2867: checking interface looking up method" >&5
+echo "configure:2875: checking interface looking up method" >&5
 if test "$netlink" = yes; then
   echo "$ac_t""netlink" 1>&6
   IF_METHOD=if_netlink.o
@@ -2904,7 +2914,7 @@
 
 
 echo $ac_n "checking ipforward method check""... $ac_c" 1>&6
-echo "configure:2910: checking ipforward method check" >&5
+echo "configure:2918: checking ipforward method check" >&5
 if eval "test \"`echo '$''{'zebra_ipforward_path'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -2933,12 +2943,12 @@
 for ac_func in getaddrinfo
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:2939: checking for $ac_func" >&5
+echo "configure:2947: 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 <<EOF
-#line 2944 "configure"
+#line 2952 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -2961,7 +2971,7 @@
 
 ; return 0; }
 EOF
-if { (eval echo configure:2967: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:2975: \"$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
@@ -2988,7 +2998,7 @@
 
 
 echo $ac_n "checking whether does this OS have IPv6 stack""... $ac_c" 1>&6
-echo "configure:2994: checking whether does this OS have IPv6 stack" >&5
+echo "configure:3002: checking whether does this OS have IPv6 stack" >&5
 if test "${enable_ipv6}" = "no"; then
   echo "$ac_t""disabled" 1>&6
 else
@@ -3049,7 +3059,7 @@
 
 if test "${enable_ipv6}" = "yes"; then
    cat > conftest.$ac_ext <<EOF
-#line 3055 "configure"
+#line 3063 "configure"
 #include "confdefs.h"
 dnl
    #include <linux/version.h>
@@ -3080,9 +3090,9 @@
 EOF
 
    echo $ac_n "checking for GNU libc 2.1""... $ac_c" 1>&6
-echo "configure:3086: checking for GNU libc 2.1" >&5
+echo "configure:3094: checking for GNU libc 2.1" >&5
    cat > conftest.$ac_ext <<EOF
-#line 3088 "configure"
+#line 3096 "configure"
 #include "confdefs.h"
 
 #include <features.h>
@@ -3165,6 +3175,13 @@
 
 fi
 
+if test "${enable_bgp_dnssort}" = "yes";then
+  cat >> confdefs.h <<\EOF
+#define BGP_DNSSORT 1
+EOF
+
+fi
+
 
 
 
@@ -3175,7 +3192,7 @@
 
 
 echo $ac_n "checking for inet_ntop in -lc""... $ac_c" 1>&6
-echo "configure:3181: checking for inet_ntop in -lc" >&5
+echo "configure:3196: checking for inet_ntop in -lc" >&5
 ac_lib_var=`echo c'_'inet_ntop | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3183,7 +3200,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lc  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3189 "configure"
+#line 3204 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -3194,7 +3211,7 @@
 inet_ntop()
 ; return 0; }
 EOF
-if { (eval echo configure:3200: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3215: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3218,7 +3235,7 @@
 fi
 
 echo $ac_n "checking for inet_pton in -lc""... $ac_c" 1>&6
-echo "configure:3224: checking for inet_pton in -lc" >&5
+echo "configure:3239: checking for inet_pton in -lc" >&5
 ac_lib_var=`echo c'_'inet_pton | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3226,7 +3243,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lc  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3232 "configure"
+#line 3247 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -3237,7 +3254,7 @@
 inet_pton()
 ; return 0; }
 EOF
-if { (eval echo configure:3243: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3258: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3261,7 +3278,7 @@
 fi
 
 echo $ac_n "checking for crypt in -lcrypt""... $ac_c" 1>&6
-echo "configure:3267: checking for crypt in -lcrypt" >&5
+echo "configure:3282: checking for crypt in -lcrypt" >&5
 ac_lib_var=`echo crypt'_'crypt | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3269,7 +3286,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lcrypt  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3275 "configure"
+#line 3290 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -3280,7 +3297,7 @@
 crypt()
 ; return 0; }
 EOF
-if { (eval echo configure:3286: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3301: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3308,7 +3325,7 @@
 fi
 
 echo $ac_n "checking for res_init in -lresolv""... $ac_c" 1>&6
-echo "configure:3314: checking for res_init in -lresolv" >&5
+echo "configure:3329: checking for res_init in -lresolv" >&5
 ac_lib_var=`echo resolv'_'res_init | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3316,7 +3333,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lresolv  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3322 "configure"
+#line 3337 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -3327,7 +3344,7 @@
 res_init()
 ; return 0; }
 EOF
-if { (eval echo configure:3333: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3348: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3355,7 +3372,7 @@
 fi
 
 echo $ac_n "checking for main in -lm""... $ac_c" 1>&6
-echo "configure:3361: checking for main in -lm" >&5
+echo "configure:3376: checking for main in -lm" >&5
 ac_lib_var=`echo m'_'main | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3363,14 +3380,14 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lm  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3369 "configure"
+#line 3384 "configure"
 #include "confdefs.h"
 
 int main() {
 main()
 ; return 0; }
 EOF
-if { (eval echo configure:3376: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3391: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3399,12 +3416,12 @@
 
 
 echo $ac_n "checking for __inet_ntop""... $ac_c" 1>&6
-echo "configure:3405: checking for __inet_ntop" >&5
+echo "configure:3420: checking for __inet_ntop" >&5
 if eval "test \"`echo '$''{'ac_cv_func___inet_ntop'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 3410 "configure"
+#line 3425 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char __inet_ntop(); below.  */
@@ -3427,7 +3444,7 @@
 
 ; return 0; }
 EOF
-if { (eval echo configure:3433: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3448: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func___inet_ntop=yes"
 else
@@ -3450,12 +3467,12 @@
 fi
 
 echo $ac_n "checking for __inet_pton""... $ac_c" 1>&6
-echo "configure:3456: checking for __inet_pton" >&5
+echo "configure:3471: checking for __inet_pton" >&5
 if eval "test \"`echo '$''{'ac_cv_func___inet_pton'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 3461 "configure"
+#line 3476 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char __inet_pton(); below.  */
@@ -3478,7 +3495,7 @@
 
 ; return 0; }
 EOF
-if { (eval echo configure:3484: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3499: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func___inet_pton=yes"
 else
@@ -3501,12 +3518,12 @@
 fi
 
 echo $ac_n "checking for __inet_aton""... $ac_c" 1>&6
-echo "configure:3507: checking for __inet_aton" >&5
+echo "configure:3522: checking for __inet_aton" >&5
 if eval "test \"`echo '$''{'ac_cv_func___inet_aton'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 3512 "configure"
+#line 3527 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char __inet_aton(); below.  */
@@ -3529,7 +3546,7 @@
 
 ; return 0; }
 EOF
-if { (eval echo configure:3535: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3550: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func___inet_aton=yes"
 else
@@ -3553,7 +3570,7 @@
 
 
 echo $ac_n "checking for regexec in -lc""... $ac_c" 1>&6
-echo "configure:3559: checking for regexec in -lc" >&5
+echo "configure:3574: checking for regexec in -lc" >&5
 ac_lib_var=`echo c'_'regexec | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3561,7 +3578,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lc  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3567 "configure"
+#line 3582 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -3572,7 +3589,7 @@
 regexec()
 ; return 0; }
 EOF
-if { (eval echo configure:3578: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3593: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3605,7 +3622,7 @@
   LIBS="-L/usr/local/lib"
   unset ac_cv_lib_snmp_asn_parse_int
   echo $ac_n "checking for asn_parse_int in -lsnmp""... $ac_c" 1>&6
-echo "configure:3611: checking for asn_parse_int in -lsnmp" >&5
+echo "configure:3626: checking for asn_parse_int in -lsnmp" >&5
 ac_lib_var=`echo snmp'_'asn_parse_int | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3613,7 +3630,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lsnmp  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3619 "configure"
+#line 3634 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -3624,7 +3641,7 @@
 asn_parse_int()
 ; return 0; }
 EOF
-if { (eval echo configure:3630: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3645: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3647,7 +3664,7 @@
   if test "${HAVE_SNMP}" = ""; then
     unset ac_cv_lib_snmp_asn_parse_int
     echo $ac_n "checking for main in -lcrypto""... $ac_c" 1>&6
-echo "configure:3653: checking for main in -lcrypto" >&5
+echo "configure:3668: checking for main in -lcrypto" >&5
 ac_lib_var=`echo crypto'_'main | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3655,14 +3672,14 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lcrypto  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3661 "configure"
+#line 3676 "configure"
 #include "confdefs.h"
 
 int main() {
 main()
 ; return 0; }
 EOF
-if { (eval echo configure:3668: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3683: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3684,7 +3701,7 @@
 
 	if test "${NEED_CRYPTO}" = ""; then 
 		echo $ac_n "checking for asn_parse_int in -lsnmp""... $ac_c" 1>&6
-echo "configure:3690: checking for asn_parse_int in -lsnmp" >&5
+echo "configure:3705: checking for asn_parse_int in -lsnmp" >&5
 ac_lib_var=`echo snmp'_'asn_parse_int | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3692,7 +3709,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lsnmp  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3698 "configure"
+#line 3713 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -3703,7 +3720,7 @@
 asn_parse_int()
 ; return 0; }
 EOF
-if { (eval echo configure:3709: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3724: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3725,7 +3742,7 @@
 
 	else
 	    echo $ac_n "checking for asn_parse_int in -lsnmp""... $ac_c" 1>&6
-echo "configure:3731: checking for asn_parse_int in -lsnmp" >&5
+echo "configure:3746: checking for asn_parse_int in -lsnmp" >&5
 ac_lib_var=`echo snmp'_'asn_parse_int | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3733,7 +3750,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lsnmp "-lcrypto" $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3739 "configure"
+#line 3754 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -3744,7 +3761,7 @@
 asn_parse_int()
 ; return 0; }
 EOF
-if { (eval echo configure:3750: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3765: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3772,7 +3789,7 @@
 	old_libs="${LIBS}"
 	LIBS="-L/usr/local/lib"
 	echo $ac_n "checking for asn_parse_int in -lsnmp""... $ac_c" 1>&6
-echo "configure:3778: checking for asn_parse_int in -lsnmp" >&5
+echo "configure:3793: checking for asn_parse_int in -lsnmp" >&5
 ac_lib_var=`echo snmp'_'asn_parse_int | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -3780,7 +3797,7 @@
   ac_save_LIBS="$LIBS"
 LIBS="-lsnmp  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 3786 "configure"
+#line 3801 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -3791,7 +3808,7 @@
 asn_parse_int()
 ; return 0; }
 EOF
-if { (eval echo configure:3797: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:3812: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -3843,9 +3860,9 @@
 fi
 
 echo $ac_n "checking whether struct sockaddr has a sa_len field""... $ac_c" 1>&6
-echo "configure:3849: checking whether struct sockaddr has a sa_len field" >&5
+echo "configure:3864: checking whether struct sockaddr has a sa_len field" >&5
 cat > conftest.$ac_ext <<EOF
-#line 3851 "configure"
+#line 3866 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -3854,7 +3871,7 @@
 static struct sockaddr ac_i;int ac_j = sizeof (ac_i.sa_len);
 ; return 0; }
 EOF
-if { (eval echo configure:3860: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:3875: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   echo "$ac_t""yes" 1>&6
  cat >> confdefs.h <<\EOF
@@ -3870,9 +3887,9 @@
 rm -f conftest*
 
 echo $ac_n "checking whether struct sockaddr_in has a sin_len field""... $ac_c" 1>&6
-echo "configure:3876: checking whether struct sockaddr_in has a sin_len field" >&5
+echo "configure:3891: checking whether struct sockaddr_in has a sin_len field" >&5
 cat > conftest.$ac_ext <<EOF
-#line 3878 "configure"
+#line 3893 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <netinet/in.h>
@@ -3881,7 +3898,7 @@
 static struct sockaddr_in ac_i;int ac_j = sizeof (ac_i.sin_len);
 ; return 0; }
 EOF
-if { (eval echo configure:3887: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:3902: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   echo "$ac_t""yes" 1>&6
  cat >> confdefs.h <<\EOF
@@ -3897,9 +3914,9 @@
 rm -f conftest*
 
 echo $ac_n "checking whether struct sockaddr_un has a sun_len field""... $ac_c" 1>&6
-echo "configure:3903: checking whether struct sockaddr_un has a sun_len field" >&5
+echo "configure:3918: checking whether struct sockaddr_un has a sun_len field" >&5
 cat > conftest.$ac_ext <<EOF
-#line 3905 "configure"
+#line 3920 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/un.h>
@@ -3908,7 +3925,7 @@
 static struct sockaddr_un ac_i;int ac_j = sizeof (ac_i.sun_len);
 ; return 0; }
 EOF
-if { (eval echo configure:3914: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:3929: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   echo "$ac_t""yes" 1>&6
  cat >> confdefs.h <<\EOF
@@ -3925,9 +3942,9 @@
 
 if test "$zebra_cv_ipv6" = yes; then
   echo $ac_n "checking whether struct sockaddr_in6 has a sin6_scope_id field""... $ac_c" 1>&6
-echo "configure:3931: checking whether struct sockaddr_in6 has a sin6_scope_id field" >&5
+echo "configure:3946: checking whether struct sockaddr_in6 has a sin6_scope_id field" >&5
   cat > conftest.$ac_ext <<EOF
-#line 3933 "configure"
+#line 3948 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <netinet/in.h>
@@ -3936,7 +3953,7 @@
 static struct sockaddr_in6 ac_i;int ac_j = sizeof (ac_i.sin6_scope_id);
 ; return 0; }
 EOF
-if { (eval echo configure:3942: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:3957: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   echo "$ac_t""yes" 1>&6
  cat >> confdefs.h <<\EOF
@@ -3953,9 +3970,9 @@
 fi
 
 echo $ac_n "checking whther socklen_t is defined""... $ac_c" 1>&6
-echo "configure:3959: checking whther socklen_t is defined" >&5
+echo "configure:3974: checking whther socklen_t is defined" >&5
 cat > conftest.$ac_ext <<EOF
-#line 3961 "configure"
+#line 3976 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -3965,7 +3982,7 @@
 socklen_t ac_x;
 ; return 0; }
 EOF
-if { (eval echo configure:3971: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:3986: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   echo "$ac_t""yes" 1>&6
  cat >> confdefs.h <<\EOF
@@ -3981,9 +3998,9 @@
 rm -f conftest*
 
 echo $ac_n "checking whether struct sockaddr_dl exist""... $ac_c" 1>&6
-echo "configure:3987: checking whether struct sockaddr_dl exist" >&5
+echo "configure:4002: checking whether struct sockaddr_dl exist" >&5
 cat > conftest.$ac_ext <<EOF
-#line 3989 "configure"
+#line 4004 "configure"
 #include "confdefs.h"
 #include <net/if_dl.h>
 EOF
@@ -4003,9 +4020,9 @@
 
 
 echo $ac_n "checking whether struct ifaliasreq exist""... $ac_c" 1>&6
-echo "configure:4009: checking whether struct ifaliasreq exist" >&5
+echo "configure:4024: checking whether struct ifaliasreq exist" >&5
 cat > conftest.$ac_ext <<EOF
-#line 4011 "configure"
+#line 4026 "configure"
 #include "confdefs.h"
 #include <net/if.h>
 EOF
@@ -4025,9 +4042,9 @@
 
 
 echo $ac_n "checking whether struct if6_aliasreq exist""... $ac_c" 1>&6
-echo "configure:4031: checking whether struct if6_aliasreq exist" >&5
+echo "configure:4046: checking whether struct if6_aliasreq exist" >&5
 cat > conftest.$ac_ext <<EOF
-#line 4033 "configure"
+#line 4048 "configure"
 #include "confdefs.h"
 #include <netinet6/in6_var.h>
 EOF
@@ -4047,9 +4064,9 @@
 
 
 echo $ac_n "checking whether struct rt_addrinfo exist""... $ac_c" 1>&6
-echo "configure:4053: checking whether struct rt_addrinfo exist" >&5
+echo "configure:4068: checking whether struct rt_addrinfo exist" >&5
 cat > conftest.$ac_ext <<EOF
-#line 4055 "configure"
+#line 4070 "configure"
 #include "confdefs.h"
 #include <net/route.h>
 EOF
@@ -4069,9 +4086,9 @@
 
 
 echo $ac_n "checking whether struct in_pktinfo exist""... $ac_c" 1>&6
-echo "configure:4075: checking whether struct in_pktinfo exist" >&5
+echo "configure:4090: checking whether struct in_pktinfo exist" >&5
 cat > conftest.$ac_ext <<EOF
-#line 4077 "configure"
+#line 4092 "configure"
 #include "confdefs.h"
 #include <netinet/in.h>
 
@@ -4079,7 +4096,7 @@
 struct in_pktinfo ac_x;
 ; return 0; }
 EOF
-if { (eval echo configure:4085: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:4100: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   echo "$ac_t""yes" 1>&6
  cat >> confdefs.h <<\EOF
@@ -4099,7 +4116,7 @@
 
 
 echo $ac_n "checking pid file directory""... $ac_c" 1>&6
-echo "configure:4105: checking pid file directory" >&5
+echo "configure:4120: checking pid file directory" >&5
 if eval "test \"`echo '$''{'ac_piddir'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -4141,13 +4158,13 @@
 
 
 echo $ac_n "checking for working htonl""... $ac_c" 1>&6
-echo "configure:4147: checking for working htonl" >&5
+echo "configure:4162: checking for working htonl" >&5
 if eval "test \"`echo '$''{'ac_cv_htonl_works'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   
 cat > conftest.$ac_ext <<EOF
-#line 4153 "configure"
+#line 4168 "configure"
 #include "confdefs.h"
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
@@ -4162,7 +4179,7 @@
 htonl (0);
 ; return 0; }
 EOF
-if { (eval echo configure:4168: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:4183: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   ac_cv_htonl_works=yes
 else
Index: zebra/configure.in
diff -u zebra/configure.in:1.1.2.1 zebra/configure.in:1.3
--- zebra/configure.in:1.1.2.1	Fri Dec  7 14:24:53 2001
+++ zebra/configure.in	Fri Dec  7 14:39:56 2001
@@ -87,6 +87,8 @@
 [  --enable-nssa           enable OSPF NSSA option])
 AC_ARG_ENABLE(multipath,
 [  --enable-multipath=ARG  enable multipath function, ARG must be digit])
+AC_ARG_ENABLE(bgp-dnssort,
+[  --enable-bgp-dnssort    enable DNSsort protocol])
 
 if test "${enable_broken_aliases}" = "yes"; then
   if test "${enable_netlink}" = "yes"
@@ -546,6 +548,10 @@
 
 if test "${enable_bgp_announce}" = "no";then
   AC_DEFINE(DISABLE_BGP_ANNOUNCE)
+fi
+
+if test "${enable_bgp_dnssort}" = "yes";then
+  AC_DEFINE(BGP_DNSSORT)
 fi
 
 AC_SUBST(ZEBRA)
Index: zebra/bgpd/Makefile.am
diff -u zebra/bgpd/Makefile.am:1.1 zebra/bgpd/Makefile.am:1.2
--- zebra/bgpd/Makefile.am:1.1	Fri Dec  7 14:20:46 2001
+++ zebra/bgpd/Makefile.am	Fri Dec  7 14:25:49 2001
@@ -13,13 +13,14 @@
 	bgp_debug.c bgp_route.c bgp_zebra.c bgp_open.c bgp_routemap.c \
 	bgp_packet.c bgp_network.c bgp_filter.c bgp_regex.c bgp_clist.c \
 	bgp_dump.c bgp_snmp.c bgp_ecommunity.c bgp_mplsvpn.c bgp_nexthop.c \
-	bgp_damp.c
+	bgp_damp.c bgp_dnssort.c
 
 noinst_HEADERS = \
 	bgp_aspath.h bgp_attr.h bgp_community.h bgp_debug.h bgp_fsm.h \
 	bgp_network.h bgp_open.h bgp_packet.h bgp_regex.h bgp_route.h \
 	bgpd.h bgp_filter.h bgp_clist.h bgp_dump.h bgp_zebra.h \
-	bgp_ecommunity.h bgp_mplsvpn.h bgp_nexthop.h bgp_damp.h
+	bgp_ecommunity.h bgp_mplsvpn.h bgp_nexthop.h bgp_damp.h \
+	bgp_dnssort.h
 
 bgpd_SOURCES = \
 	bgp_main.c $(libbgp_a_SOURCES)
@@ -166,3 +167,7 @@
  ../bgpd/bgp_clist.h ../bgpd/bgp_fsm.h ../bgpd/bgp_packet.h \
  ../bgpd/bgp_zebra.h ../bgpd/bgp_open.h ../bgpd/bgp_filter.h \
  ../bgpd/bgp_nexthop.h ../bgpd/bgp_damp.h
+bgp_dnssort.o: bgp_dnssort.c ../lib/thread.h ../lib/memory.h \
+ ../lib/sockunion.h ../lib/sockopt.h ../lib/log.h ../lib/prefix.h \
+ ../lib/table.h ../lib/command.h ../bgpd/bgpd.h ../bgpd/bgp_attr.h \
+ ../bgpd/bgp_aspath.h ../bgpd/bgp_route.h ../bgpd/bgp_dnssort.h
Index: zebra/bgpd/Makefile.in
diff -u zebra/bgpd/Makefile.in:1.1 zebra/bgpd/Makefile.in:1.2
--- zebra/bgpd/Makefile.in:1.1	Fri Dec  7 14:20:46 2001
+++ zebra/bgpd/Makefile.in	Fri Dec  7 14:25:49 2001
@@ -1,4 +1,4 @@
-# Makefile.in generated automatically by automake 1.4 from Makefile.am
+# Makefile.in generated automatically by automake 1.4-p2 from Makefile.am
 
 # Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc.
 # This Makefile.in is free software; the Free Software Foundation
@@ -94,10 +94,10 @@
 sbin_PROGRAMS = bgpd
 #noinst_PROGRAMS = bgp_btoa
 
-libbgp_a_SOURCES =  	bgpd.c bgp_fsm.c bgp_aspath.c bgp_community.c bgp_attr.c 	bgp_debug.c bgp_route.c bgp_zebra.c bgp_open.c bgp_routemap.c 	bgp_packet.c bgp_network.c bgp_filter.c bgp_regex.c bgp_clist.c 	bgp_dump.c bgp_snmp.c bgp_ecommunity.c bgp_mplsvpn.c bgp_nexthop.c 	bgp_damp.c
+libbgp_a_SOURCES =  	bgpd.c bgp_fsm.c bgp_aspath.c bgp_community.c bgp_attr.c 	bgp_debug.c bgp_route.c bgp_zebra.c bgp_open.c bgp_routemap.c 	bgp_packet.c bgp_network.c bgp_filter.c bgp_regex.c bgp_clist.c 	bgp_dump.c bgp_snmp.c bgp_ecommunity.c bgp_mplsvpn.c bgp_nexthop.c 	bgp_damp.c bgp_dnssort.c
 
 
-noinst_HEADERS =  	bgp_aspath.h bgp_attr.h bgp_community.h bgp_debug.h bgp_fsm.h 	bgp_network.h bgp_open.h bgp_packet.h bgp_regex.h bgp_route.h 	bgpd.h bgp_filter.h bgp_clist.h bgp_dump.h bgp_zebra.h 	bgp_ecommunity.h bgp_mplsvpn.h bgp_nexthop.h bgp_damp.h
+noinst_HEADERS =  	bgp_aspath.h bgp_attr.h bgp_community.h bgp_debug.h bgp_fsm.h 	bgp_network.h bgp_open.h bgp_packet.h bgp_regex.h bgp_route.h 	bgpd.h bgp_filter.h bgp_clist.h bgp_dump.h bgp_zebra.h 	bgp_ecommunity.h bgp_mplsvpn.h bgp_nexthop.h bgp_damp.h 	bgp_dnssort.h
 
 
 bgpd_SOURCES =  	bgp_main.c $(libbgp_a_SOURCES)
@@ -126,14 +126,14 @@
 bgp_attr.o bgp_debug.o bgp_route.o bgp_zebra.o bgp_open.o \
 bgp_routemap.o bgp_packet.o bgp_network.o bgp_filter.o bgp_regex.o \
 bgp_clist.o bgp_dump.o bgp_snmp.o bgp_ecommunity.o bgp_mplsvpn.o \
-bgp_nexthop.o bgp_damp.o
+bgp_nexthop.o bgp_damp.o bgp_dnssort.o
 PROGRAMS =  $(sbin_PROGRAMS)
 
 bgpd_OBJECTS =  bgp_main.o bgpd.o bgp_fsm.o bgp_aspath.o bgp_community.o \
 bgp_attr.o bgp_debug.o bgp_route.o bgp_zebra.o bgp_open.o \
 bgp_routemap.o bgp_packet.o bgp_network.o bgp_filter.o bgp_regex.o \
 bgp_clist.o bgp_dump.o bgp_snmp.o bgp_ecommunity.o bgp_mplsvpn.o \
-bgp_nexthop.o bgp_damp.o
+bgp_nexthop.o bgp_damp.o bgp_dnssort.o
 bgpd_DEPENDENCIES =  ../lib/libzebra.a
 bgpd_LDFLAGS = 
 CFLAGS = @CFLAGS@
@@ -270,7 +270,7 @@
 	@for file in $(DISTFILES); do \
 	  d=$(srcdir); \
 	  if test -d $$d/$$file; then \
-	    cp -pr $$/$$file $(distdir)/$$file; \
+	    cp -pr $$d/$$file $(distdir)/$$file; \
 	  else \
 	    test -f $(distdir)/$$file \
 	    || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
@@ -484,6 +484,10 @@
  ../bgpd/bgp_clist.h ../bgpd/bgp_fsm.h ../bgpd/bgp_packet.h \
  ../bgpd/bgp_zebra.h ../bgpd/bgp_open.h ../bgpd/bgp_filter.h \
  ../bgpd/bgp_nexthop.h ../bgpd/bgp_damp.h
+bgp_dnssort.o: bgp_dnssort.c ../lib/thread.h ../lib/memory.h \
+ ../lib/sockunion.h ../lib/sockopt.h ../lib/log.h ../lib/prefix.h \
+ ../lib/table.h ../lib/command.h ../bgpd/bgpd.h ../bgpd/bgp_attr.h \
+ ../bgpd/bgp_aspath.h ../bgpd/bgp_route.h ../bgpd/bgp_dnssort.h
 
 # Tell versions [3.59,3.63) of GNU make to not export all variables.
 # Otherwise a system limit (for SysV at least) may be exceeded.
Index: zebra/bgpd/bgp_dnssort.c
diff -u /dev/null zebra/bgpd/bgp_dnssort.c:1.18
--- /dev/null	Tue Jan 15 18:21:42 2002
+++ zebra/bgpd/bgp_dnssort.c	Thu Dec 20 22:39:57 2001
@@ -0,0 +1,1028 @@
+/* Implemetation of the DNS sort protocol.
+ * Copyright (C) 2001 Claudio Jeker
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Zebra; see the file COPYING.  If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.  
+ */
+#include <zebra.h>
+
+#ifdef BGP_DNSSORT
+
+/* BGP DNSsort
+   How does it works:
+     With the DNSsort protocol it is possible to select the best A entry
+     in a multihomed environment.
+     It is thought as a replacement of the DNS round robin algorithm.
+     
+     The DNS server sends a DNS sort request to the BGP server how performs 
+     following actions.
+     
+     1. show ip bgp CLIENT_IP  # CLIENT_IP = IP of requestor
+     2. select best path
+     3. take leftmost ASnum
+     4. show ip bgp regexp ^ASnum$
+     5. cycle through routing table and compare prefixes with all SERVER_IP
+        # SERVER_IP = ips of the requested server
+     6. set preferences according to their match
+     7. return packet to DNS server
+     
+		 The 4th step needs the most time but the good news are that it is possible to
+		 run this command in the background and cache the results. 
+		 
+     The DNS sort message is so designed that minimal changes have to be performed 
+     by the BGP server.
+     
+     At the moment only IPv4 server requests are handled.
+     
+*/
+#include "thread.h"
+#include "sockunion.h"
+#include "sockopt.h"
+#include "memory.h"
+#include "log.h"
+#include "prefix.h"
+#include "table.h"
+#include "command.h"
+
+
+#include "bgpd/bgpd.h"
+#include "bgpd/bgp_attr.h"
+#include "bgpd/bgp_aspath.h"
+#include "bgpd/bgp_route.h"
+#include "bgpd/bgp_dnssort.h"
+
+/* definitions of the DNSsort messages */
+typedef struct bgp_dnsort_msg_t {
+	u_int16_t version;     /* protocol version: 1 = IPv4, 2 = IPv6 */
+	u_char querytype;      /* querytype: 1 = client sorting, 2 = server sorting */
+	u_char requesttype;    /* requesttype: 1 = request, 2 = answer */
+	u_int32_t requestid;   /* request id: unique id for this request */
+	u_int32_t RFU[2];      /* reserved, for future use */
+	u_int32_t numobjects;  /* # of valid request objects in packet */
+	/* start of data */
+} bgp_dnssort_msg;
+
+typedef struct bgp_dnssort_val_IPv4_t {
+	union {
+		u_char addr;       /* special access for compares with prefixes */
+		u_int32_t addr4;   /* address: IPv4 address */
+		struct in_addr prefix4;
+	} u;
+	u_int32_t preference;  /* preference: preferenc of this address, the higher the better,
+	                                      set to zero in requests */
+} bgp_dnssort_val_IPv4;
+
+#define MAX_DNSSORTMSG_LEN 1024 /* enough for 127 ipv4 or 59 ipv6 entries */
+
+#define BGP_DNSSORT_FAILURE 10
+
+#define BGP_DNSSORT_DEFAULTPORT 179 /* same as BGP but UDP not TCP */
+
+/* DNSsort statistics */
+u_int64_t bgp_dnssort_pkg_recv_cnt;
+u_int64_t bgp_dnssort_pkg_send_cnt;
+u_int64_t bgp_dnssort_pkg_drop_cnt;
+
+/******************   DNSsort queue functions    ********************/
+int bgp_dnssort_queue_max;
+int bgp_dnssort_queue_cur;
+
+/* simple FIFO for the queueing of responses */
+typedef struct bgp_dnssort_queue_elm_t {
+	char *buf;
+	int  blen;
+	struct sockaddr *to;
+	int tlen;
+	struct bgp_dnssort_queue_elm_t *next;
+} bgp_dnssort_queue_elm, *bgp_dnssort_queue;
+
+struct bgp_dnssort_queue_head {
+	bgp_dnssort_queue head;
+	bgp_dnssort_queue tail;
+} bgp_dnssort_queue_head;
+
+/* corresponding functions */
+static void bgp_dnssort_queue_init()
+{
+	bgp_dnssort_queue_head.head = NULL;
+	bgp_dnssort_queue_head.tail = NULL;
+}
+
+static bgp_dnssort_queue bgp_dnssort_queue_new()
+{
+	bgp_dnssort_queue e;
+	e = (bgp_dnssort_queue) 
+			XMALLOC(MTYPE_BGP_DNSSORT_QUEUE, sizeof(bgp_dnssort_queue_elm));
+	memset (e, 0, sizeof (bgp_dnssort_queue_elm));
+	return e;
+}
+
+static void bgp_dnssort_queue_free(bgp_dnssort_queue elm)
+{
+	XFREE(MTYPE_BGP_DNSSORT_QUEUE, elm);
+}
+
+static int bgp_dnssort_queue_get(bgp_dnssort_queue *n)
+{	
+	if ( n == NULL ) return -1;
+	
+	*n = bgp_dnssort_queue_head.head;
+	if ( *n == NULL ) {
+		zlog_debug("BGP DNSsort queue is empty");
+		return 0;
+	}
+
+	--bgp_dnssort_queue_cur;
+
+	/* unlink the current node */
+	bgp_dnssort_queue_head.head = (*n)->next;
+	if ( bgp_dnssort_queue_head.head == NULL ) {
+		bgp_dnssort_queue_head.tail = NULL;
+		return 0; /* queue empty, write thread does not need to be readded */
+	}
+	return 1;
+}
+
+static int bgp_dnssort_queue_add(char *b, int blen, struct sockaddr *to, int tlen)
+{
+	bgp_dnssort_queue n;
+	
+	n = bgp_dnssort_queue_new();
+	
+	n->buf = b;
+	n->blen = blen;
+	n->to = to;
+	n->tlen = tlen;
+
+	if (bgp_dnssort_queue_head.head == NULL) {
+		bgp_dnssort_queue_cur = 1; /* just to be sure */
+		bgp_dnssort_queue_head.head = n;
+		bgp_dnssort_queue_head.tail = n;
+		return 1; /* write thread needs to be readded */
+	}
+	
+	/* queue not empty */
+	bgp_dnssort_queue_head.tail->next = n;
+	bgp_dnssort_queue_head.tail = n;
+
+	if ( ++bgp_dnssort_queue_cur > bgp_dnssort_queue_max ) {
+		bgp_dnssort_queue t;
+		/* we know there was at least one element in the queue and
+			 therefor we do not need to readd the write thread */
+		
+		bgp_dnssort_pkg_drop_cnt++;
+
+		bgp_dnssort_queue_get(&t);
+		/* clean up */
+		XFREE(MTYPE_BGP_DNSSORT_MSG, t->buf);
+		XFREE(MTYPE_BGP_DNSSORT_MSG, t->to);
+		bgp_dnssort_queue_free(t);
+	}
+	return 0;
+}
+
+/********************   DNSsort cache functions *******************************/
+/* how do we do it:
+		Each chaching thread needs a context with the needed infos and the possibility
+		to store temporary data (e.g. rn and the tables).
+		We start with a table that has the same size as the one used befor or a defined
+		minimum. If needed we realloc the table.
+		The tree walk is only run a predefined times so that we do not block the process 
+		for to long. At the end the cache tables are switched in an atomic transaction
+		(changing the pointer of the installed table to the new table)
+ */
+int bgp_dnssort_timeout;
+
+/* To fetch and store as segment value. This is stolen from bgp_aspath.c */
+struct assegment
+{
+  u_char type;
+  u_char length;
+  as_t asval[1];
+};
+
+struct bgp_dnssort_IPv4_entry {
+	struct bgp_dnssort_IPv4_entry* next;
+	union {
+		u_char addr;       /* special access for compares with prefixes */
+		u_int32_t addr4;   /* address: IPv4 address */
+		struct in_addr prefix4;
+	} u;
+	int prefixlen;
+};
+
+struct bgp_dnssort_IPv4_table {
+	as_t asnum;
+	struct bgp_dnssort_IPv4_entry* entry;
+	u_int64_t hitcount;
+};
+
+struct bgp_dnssort_cache_ctx {
+	struct thread *thread;
+	struct route_node *rn;
+	int which; /* multiview index in bgp_dnssort_IPv4_cache */
+	struct bgp_dnssort_IPv4_table *table;
+	int size; /* size of table */
+#if 0
+	/* stuff for multiviews */
+	as_t as;
+	char* name;
+#endif
+} *bgp_dnssort_ctx;
+
+struct bgp_dnssort_IPv4_table *bgp_dnssort_IPv4_cache_main; /* XXX multiviews */
+struct bgp_dnssort_IPv4_table **bgp_dnssort_IPv4_cache = &bgp_dnssort_IPv4_cache_main;
+int bgp_dnssort_IPv4_cache_size_main; /* XXX multiviews */
+int *bgp_dnssort_IPv4_cache_size = &bgp_dnssort_IPv4_cache_size_main;
+
+/* XXX init */
+
+static void ctx_table_IPv4_init(struct bgp_dnssort_cache_ctx* ctx, struct route_node* rn)
+{
+	int i;
+	int j;
+	struct bgp_dnssort_IPv4_table* tab;
+	struct bgp_dnssort_IPv4_table* oe;
+	
+	ctx->rn = rn;
+	ctx->size = bgp_dnssort_IPv4_cache_size[ctx->which];
+	ctx->table = (struct bgp_dnssort_IPv4_table*)
+			XCALLOC(MTYPE_BGP_DNSSORT_CACHE, 
+					ctx->size, sizeof(struct bgp_dnssort_IPv4_table));
+	
+	if ( bgp_dnssort_IPv4_cache[ctx->which] == NULL ) {
+		/* this is only for the startup */
+		memset(ctx->table, 0, ctx->size * sizeof(struct bgp_dnssort_IPv4_table));
+		return;
+	}
+	
+	tab = ctx->table;
+	assert(ctx->which == 0);
+	for ( i = 0; i < ctx->size; i++ ) {
+		/* init the table: sorted by count (memmove is your friend) 
+			 We can assume that the old table is almost sorted.
+		 */
+		oe = bgp_dnssort_IPv4_cache[ctx->which];
+		oe = &oe[i];
+		for ( j = i; tab[j].hitcount > oe->hitcount && j > 0; j-- ) ;
+		if ( j < i ) {
+			/* move tab[j] till tab[i-1] to tab[j+1] till tab[i] */
+			memmove(&tab[j+1], &tab[j], (i-j) * sizeof(*tab) );
+		}
+		tab[j].hitcount = oe->hitcount;	
+		/* NOTE: the hitcount is stored now, so the hits done while we rebuild the cache are not counted */
+		tab[j].asnum = oe->asnum;
+		tab[j].entry = NULL;
+	}
+}
+
+static void ctx_table_IPv4_add(struct bgp_dnssort_cache_ctx* ctx, 
+		as_t asnum, struct in_addr addr, int prefixlen)
+{
+	int i;
+	struct bgp_dnssort_IPv4_entry* e;
+	
+	assert( ctx->table != NULL );
+	for( i = 0; i < ctx->size; i++) {
+		if ( asnum == ctx->table[i].asnum || ctx->table[i].asnum == 0 ) {
+			ctx->table[i].asnum = asnum;
+			e = (struct bgp_dnssort_IPv4_entry*)
+					XMALLOC(MTYPE_BGP_DNSSORT_CACHE, sizeof(struct bgp_dnssort_IPv4_entry));
+			e->u.prefix4 = addr;
+			e->prefixlen = prefixlen;
+			e->next = ctx->table[i].entry;
+			ctx->table[i].entry = e;
+			return;
+		}
+	}
+	ctx->table = (struct bgp_dnssort_IPv4_table*)
+			XREALLOC(MTYPE_BGP_DNSSORT_CACHE, (void *)ctx->table,
+			(ctx->size + 1)*sizeof(struct bgp_dnssort_IPv4_table));
+	ctx->table[ctx->size].asnum = asnum;
+	ctx->table[ctx->size].hitcount = 0;
+	e = (struct bgp_dnssort_IPv4_entry*)
+			XMALLOC(MTYPE_BGP_DNSSORT_CACHE, sizeof(struct bgp_dnssort_IPv4_entry));
+	e->u.prefix4 = addr;
+	e->prefixlen = prefixlen;
+	e->next = NULL;
+	ctx->table[ctx->size++].entry = e;
+	return;
+}
+
+static void ctx_table_IPv4_finish(struct bgp_dnssort_cache_ctx* ctx)
+{
+	struct bgp_dnssort_IPv4_table *old;
+	struct bgp_dnssort_IPv4_table	*tab;
+	struct bgp_dnssort_IPv4_entry* e;
+	struct bgp_dnssort_IPv4_entry* t;
+	int i;
+	int oldsize;
+	
+	old = bgp_dnssort_IPv4_cache[ctx->which];
+	oldsize = bgp_dnssort_IPv4_cache_size[ctx->which];
+	bgp_dnssort_IPv4_cache[ctx->which] = ctx->table;
+	bgp_dnssort_IPv4_cache_size[ctx->which] = ctx->size;
+	
+	/* print out the cache */
+#if 1
+	{
+	char buf[INET6_ADDRSTRLEN]; /* 46 Bytes */
+
+	zlog_debug("BGP DNSsort cache table for view %d", ctx->which);
+	zlog_debug("====================================================");
+	for ( i = 0; i < ctx->size; i++ ) {
+		tab = &ctx->table[i];
+		zlog_debug("AS %d (%qd hits)", ntohs(tab->asnum), tab->hitcount);
+		for ( e = tab->entry; e != NULL; e = e->next ) {
+			zlog_debug("\t%s/%d", inet_ntop(AF_INET, &e->u.addr, buf, INET6_ADDRSTRLEN), e->prefixlen);
+		}
+	}
+	
+	}
+#endif
+
+	/* free the old table */
+	for ( i = 0; i < oldsize && old != NULL; i++ ) {
+		tab = &old[i];
+		if ( tab == NULL ) continue;
+		for ( e = tab->entry; e != NULL; ) {
+			t = e;
+			e = e->next;
+			XFREE(MTYPE_BGP_DNSSORT_CACHE, t);
+		}
+	}
+	XFREE(MTYPE_BGP_DNSSORT_CACHE, old);
+	
+}
+
+static void ctx_table_IPv4_lookup(int which, as_t asnum, bgp_dnssort_val_IPv4* vect, u_int32_t num)
+{
+	char buf[INET6_ADDRSTRLEN]; /* 46 Bytes */
+	char buf2[INET6_ADDRSTRLEN]; /* 46 Bytes */
+	struct bgp_dnssort_IPv4_table	*tab;
+	struct bgp_dnssort_IPv4_entry* e;
+	u_int32_t i;
+	u_int32_t addr;
+	int size;
+	int shift;
+	
+	/* we need to ensure that which is a valid index */
+	tab = bgp_dnssort_IPv4_cache[which];
+	size = bgp_dnssort_IPv4_cache_size[which];
+	
+	e = NULL;
+	for ( i = 0; i < size; i++ )
+		if ( tab[i].asnum == asnum ) {
+			e = tab[i].entry;
+			tab[i].hitcount++;
+			break;
+		}
+	
+	if ( e == NULL ) {
+		zlog_debug("BGP DNSsort: asnum %d NOT found in cache!!!!!", ntohs(asnum));
+	}
+	while ( e != NULL ) {
+#if 0
+		zlog_debug("BGP DNSsort: possible candidate %s/%i",
+				inet_ntop(AF_INET, &e->u.addr, buf, INET6_ADDRSTRLEN),
+				e->prefixlen);
+#endif
+		for ( i = 1; i < num; i++ ) {
+			addr = ntohl(vect[i].u.addr4);
+			shift = 32 - e->prefixlen;
+			addr = htonl((addr >> shift) << shift);
+
+#if 0 			
+			zlog_debug("BGP DNSsort: comparing addr %#lx with cache entry %#lx\n", 
+					(unsigned long)ntohl(addr), (unsigned long)ntohl(e->u.addr));
+#endif
+			
+			if ( addr == e->u.addr4 ) {
+				/* match found */
+				zlog_debug("BGP DNSsort: %s does match in %s/%i, set prefix to %i",
+						inet_ntop(AF_INET, &vect[i].u.addr, buf2, INET6_ADDRSTRLEN),
+						inet_ntop(AF_INET, &e->u.addr, buf, INET6_ADDRSTRLEN),
+						e->prefixlen, e->prefixlen );
+				/* XXX preference is equal to prefix, so a W.X.Y.Z/24 is better
+					then A.B.C.D/16. This needs more testing */
+				vect[i].preference = htonl(e->prefixlen);
+			}
+		}
+		e = e->next;
+	}
+}
+
+void bgp_dnssort_cache_IPv4_init(int which)
+{
+	bgp_dnssort_ctx = (struct bgp_dnssort_cache_ctx*)
+					XMALLOC(MTYPE_BGP_DNSSORT_CACHE, sizeof(struct bgp_dnssort_cache_ctx));
+	bgp_dnssort_ctx->which = 0;
+	bgp_dnssort_IPv4_cache_size[0] = 8; /* XXX Default value */
+	bgp_dnssort_IPv4_cache[0] = NULL;
+}
+
+int bgp_dnssort_cache_IPv4(struct thread *thread);
+
+int bgp_dnssort_cache_IPv4_start(struct thread *thread)
+{
+	struct bgp *bgp;
+	struct route_node *rn;
+	struct route_table *table;
+	
+	/* XXX with multiviews we need to get one view after another.
+		 The cacher thread should be started on one view and this 
+		 needs to be stored. So there will be multiple cacher threads.
+	 */
+	bgp = bgp_get_default ();
+	if ( bgp == NULL ) {
+		zlog_warn("BGP DNSsort no BGP process is configured");
+		return -1;
+	}
+	
+	/* Lookup route node. */
+	table = bgp->rib[AFI_IP][SAFI_UNICAST];
+	rn = route_top(table);
+	
+	/* re-add thread and add bgp_dnssort_cache_IPv4 */
+	ctx_table_IPv4_init((struct bgp_dnssort_cache_ctx*)thread->arg, rn);
+	
+	return bgp_dnssort_cache_IPv4(thread);
+}
+
+#define MAX_CYCLE 4096 /* how much is enough or when is it to much ??? */
+int bgp_dnssort_cache_IPv4(struct thread *thread)
+{
+	struct route_node *rn;
+	struct bgp_info *ri;
+	as_t asnum;
+	int i;
+
+	rn = ((struct bgp_dnssort_cache_ctx *)thread->arg)->rn;
+
+	/* show ip bgp regexp ^[^[:space:]]$ || show ip bgp regexp ^$*/
+	for ( i = 0; rn != NULL && i++ < MAX_CYCLE ; rn = route_next(rn) ) {
+		if ( rn->info != NULL ) {
+			for ( ri = rn->info; ri; ri = ri->next ) {
+				if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)) {
+					if ( ri->attr->aspath->count == 1 || ri->attr->aspath->count == 0) {
+						/* found aspath entry */
+						/* store under the aspath, the prefix of this route node */
+						if ( ri->attr->aspath->count == 1 )
+							asnum = ((struct assegment *)ri->attr->aspath->data)->asval[0];
+						else if ( ri->attr->aspath->count == 0 )
+							asnum = 0;
+						else
+							assert(0);
+						
+						ctx_table_IPv4_add((struct bgp_dnssort_cache_ctx *)thread->arg, 
+								asnum, rn->p.u.prefix4, rn->p.prefixlen);
+						break;
+					}
+				}
+			}
+		}
+	}
+	if ( rn == NULL ) {
+		/* done */
+		/* finish up (change the table pointers) */
+
+		ctx_table_IPv4_finish((struct bgp_dnssort_cache_ctx *)thread->arg);
+		
+		((struct bgp_dnssort_cache_ctx *)thread->arg)->thread = 
+				thread_add_timer (master, bgp_dnssort_cache_IPv4_start, thread->arg, bgp_dnssort_timeout );
+		return 0;
+	}
+	
+	/* re-add thread */
+	((struct bgp_dnssort_cache_ctx *)thread->arg)->rn = route_next(rn);	
+	
+	((struct bgp_dnssort_cache_ctx *)thread->arg)->thread = 
+			thread_add_event (master, bgp_dnssort_cache_IPv4, thread->arg, 0);
+	
+	return 0;
+}
+
+/********************   DNSsort specific functions ****************************/
+
+/* DNSsort bind thread */
+struct thread *bgp_dnssort_bind_thread;
+
+/* DNSsort master read thread. */
+struct thread *bgp_dnssort_read_thread;
+
+/* DNSsort master write thread. */
+struct thread *bgp_dnssort_write_thread;
+
+/* DNSsort main socket */
+int bgp_dnssort_sock = -1;
+
+/* DNSsort listen address and port */
+struct in_addr bgp_dnssort_addr_v4;
+int bgp_dnssort_port;
+
+/* DNSsort fail counter */
+int bgp_dnssort_fail;
+
+/* DNSsort on/off indicator */
+int bgp_dnssort_running;
+
+
+/* DNSsort events and eventhandler */
+enum bgp_dnssort_event {BGP_DNSSORT_SCHEDULE, BGP_DNSSORT_BIND, 
+						BGP_DNSSORT_READ, BGP_DNSSORT_WRITE};
+
+static void bgp_dnssort_event(enum bgp_dnssort_event, int sock);
+void bgp_dnssort_stop();
+
+static int bgp_dnssort_lookup_IPv4_server(bgp_dnssort_val_IPv4 *vect, int l)
+{
+	char buf[INET6_ADDRSTRLEN]; /* 46 Bytes */
+	struct bgp *bgp;
+	struct bgp_info *ri;
+	struct prefix target;
+	struct route_node *rn;
+	struct route_table *table;
+	as_t asnum;
+	
+	/* show ip bgp targetip (vect[0].addr) */
+	bgp = bgp_get_default ();
+	if ( bgp == NULL ) {
+		zlog_warn("BGP DNSsort no BGP process is configured");
+		return -1;
+	}
+	
+	/* Convert bgp_dnssort_val_IPv4 to prefix. */
+	target.u.prefix4 = vect[0].u.prefix4;
+	target.family = AF_INET;
+	target.prefixlen = IPV4_MAX_BITLEN;
+	
+	/* Lookup route node. */
+	table = bgp->rib[AFI_IP][SAFI_UNICAST];
+	rn = route_node_match (table, &target);
+	if (rn == NULL) {
+		zlog_info("BGP DNSsort network %s not in table", 
+				inet_ntop(target.family, &target.u.prefix, buf, INET6_ADDRSTRLEN) );
+		return 1;
+	} 
+	
+	/* cycle through BGP route information to find best path */
+	for (ri = rn->info; ri; ri = ri->next) {
+		if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)) {
+			/* select best route */
+			if ( ri->attr->aspath->count != 0 ) 
+				asnum = ((struct assegment *)ri->attr->aspath->data)->asval[0];
+			else
+				asnum = 0;
+				
+			zlog_debug("BGP DNSsort best aspath for %s = %s", 
+					inet_ntop(target.family, &target.u.prefix, buf, INET6_ADDRSTRLEN),
+					ri->attr->aspath->str);
+			break;
+		}
+	}
+	if ( !ri ) {
+		zlog_info("BGP DNSsort no best path to %s", 
+				inet_ntop(target.family, &target.u.prefix, buf, INET6_ADDRSTRLEN) );
+		return 1;
+	}
+	/* Work is done, so unlock this node. */
+	route_unlock_node (rn);
+	
+	ctx_table_IPv4_lookup(0 /*default*/, asnum, vect, l);
+	return 0; /* all OK, send reply */
+}
+
+static int bgp_dnssort_lookup_IPv4(bgp_dnssort_msg *msg, int mlen)
+{
+	int l = ntohl(msg->numobjects);
+	int size = l * sizeof(bgp_dnssort_val_IPv4) + sizeof(bgp_dnssort_msg);
+	bgp_dnssort_val_IPv4 *addr_vec;
+	
+	if ( size > mlen ) {
+		zlog_warn("BGP DNSsort recieved truncated message");
+		return -1;
+	}
+	
+	addr_vec = (bgp_dnssort_val_IPv4 *)((char *)msg + sizeof(bgp_dnssort_msg));
+
+	switch ( msg->querytype ) {
+		case 1:
+			zlog_info("BGP DNSsort client lookups are not implemented");
+			return 1;
+		case 2:
+			return bgp_dnssort_lookup_IPv4_server(addr_vec, l);
+		default:
+			zlog_info("BGP DNSsort unknown querytype");
+			return -1;
+	}
+	return -1;
+}
+
+static int bgp_dnssort_lookup(char *b, int blen)
+{
+	bgp_dnssort_msg *msg;
+	
+	msg = (bgp_dnssort_msg*) b;
+/*	zlog_debug("msg:\tversion = %s type = %s (%s)\n\tid = %li RFU = %i #objects = %li\n",
+			ntohs(msg->version)==1?"IPv4   ":ntohs(msg->version)==2?"IPv6   ":"UNKNOWN",
+			msg->querytype==1?"client ":msg->querytype==2?"server ":"UNKNOWN",
+			msg->requesttype==1?"request":msg->requesttype==2?"answer":"UNKNOWN",
+			ntohl(msg->requestid),
+			ntohl(msg->RFU),
+			ntohl(msg->numobjects) );
+*/	
+	switch ( msg->requesttype ) {
+		case 1:
+			msg->requesttype = 2; /* answer */
+			break;
+		case 2:
+			zlog_info("BGP DNSsort answer recieved, dumping");
+			return 1;
+		default:
+			zlog_info("BGP DNSsort unknown requesttype");
+			return -1;
+	}
+	
+	switch ( ntohs(msg->version) ) {
+		case 1:
+			/* IPv4 Lookup */
+			return bgp_dnssort_lookup_IPv4(msg, blen);
+		case 2:
+			/* IPv6 Lookup */
+			zlog_info("BGP DNSsort IPv6 lookups not implemented");
+			return 1;
+		default:
+			zlog_info("BGP DNSsort unknown version");
+			return -1;
+	}
+	/* should never end here */
+	return -1;
+}
+
+static void bgp_dnssort_addpending(char *b, int blen, struct sockaddr *to, int tlen)
+{
+	if ( bgp_dnssort_queue_add(b, blen, to, tlen) ) {
+		/* re-add write thread to the thread queue */
+		bgp_dnssort_event(BGP_DNSSORT_WRITE, bgp_dnssort_sock);
+	}
+}
+
+int bgp_dnssort_read(struct thread *thread)
+{
+	int l = sizeof(struct sockaddr_in);
+	struct sockaddr *from = (struct sockaddr*) XMALLOC(MTYPE_BGP_DNSSORT_MSG, l);
+	int buflen = MAX_DNSSORTMSG_LEN;
+	char *buf = (char*) XMALLOC(MTYPE_BGP_DNSSORT_MSG, buflen);
+	
+	bgp_dnssort_read_thread = NULL;
+	/* first re-add the master read thread */
+	bgp_dnssort_event (BGP_DNSSORT_READ, bgp_dnssort_sock);
+			
+	buflen = recvfrom(bgp_dnssort_sock, buf, buflen, 0, from, &l);
+	if ( buflen == -1 ) {
+		zlog_warn ("[Error] BGP DNSsort socket readfrom failed (%s)", strerror (errno));
+		if ( bgp_dnssort_fail++ < BGP_DNSSORT_FAILURE ) {
+			zlog_err ("[Error] DNSsort to many failures, giving up");
+			bgp_dnssort_stop();
+		}
+		return -1;
+	}
+	bgp_dnssort_pkg_recv_cnt++;
+	
+	zlog_debug("BGP DNSsort recieved message from %s port %i", 
+			inet_ntoa( ((struct sockaddr_in*)from)->sin_addr), 
+			ntohs( ((struct sockaddr_in*)from)->sin_port) );
+
+	if ( buflen < sizeof(bgp_dnssort_msg) ) {
+		zlog_warn("BGP DNSsort recieved truncated message");
+		XFREE(MTYPE_BGP_DNSSORT_MSG, buf);
+		XFREE(MTYPE_BGP_DNSSORT_MSG, from);
+		return -1;
+	}			
+	/* lookup */
+	if ( bgp_dnssort_lookup(buf, buflen) == 0 ) {
+		bgp_dnssort_addpending(buf, buflen, from, l);
+		return 0;
+	} else {
+		XFREE(MTYPE_BGP_DNSSORT_MSG, buf);
+		XFREE(MTYPE_BGP_DNSSORT_MSG, from);
+		return 1;
+	}
+}
+
+int bgp_dnssort_write(struct thread *thread)
+{
+	bgp_dnssort_queue f;
+	int r;
+	
+	bgp_dnssort_write_thread = NULL;
+	/* first get first msg to send */
+	if ( bgp_dnssort_queue_get(&f) ) {
+		/* re-add thread to the write queue */
+		bgp_dnssort_event(BGP_DNSSORT_WRITE, bgp_dnssort_sock);
+	}
+	if ( !f ) return -1;
+	
+	bgp_dnssort_pkg_send_cnt++;
+	
+	/* send */
+	r = sendto(bgp_dnssort_sock, f->buf, f->blen, 0, f->to, f->tlen);
+	if ( r == -1 ) {
+		zlog_warn ("[Error] BGP DNSsort socket sendto failed (%s)", strerror (errno));
+		if ( bgp_dnssort_fail++ < BGP_DNSSORT_FAILURE ) {
+			zlog_err ("[Error] DNSsort to many failures, giving up");
+			bgp_dnssort_stop();
+		}
+		return -1;
+	}
+	
+	/* clean up */
+	XFREE(MTYPE_BGP_DNSSORT_MSG, f->buf);
+	XFREE(MTYPE_BGP_DNSSORT_MSG, f->to);
+	bgp_dnssort_queue_free(f);
+	return 0;
+}
+
+int bgp_dnssort_bind (struct thread *t)
+{
+	struct sockaddr_in addr;
+
+	bgp_dnssort_bind_thread = NULL;
+	
+	addr.sin_family = AF_INET;
+	addr.sin_port = htons(bgp_dnssort_port);
+	addr.sin_addr.s_addr = bgp_dnssort_addr_v4.s_addr;
+	
+	bgp_dnssort_sock = socket(AF_INET, SOCK_DGRAM, 0);
+	if (bgp_dnssort_sock == -1) {
+		zlog_err ("[Error] BGP DNSsort socket create failed (%s)", strerror (errno));
+		if ( bgp_dnssort_fail++ < BGP_DNSSORT_FAILURE )
+			bgp_dnssort_event(BGP_DNSSORT_BIND, 0);
+		return -1;
+	}
+	
+	if ( bind(bgp_dnssort_sock, (struct sockaddr*)&addr, sizeof(addr) ) < 0 ) {
+		zlog_err ("[Error] BGP DNSsort socket bind failed (%s)", strerror (errno)); 
+		if ( bgp_dnssort_fail++ < BGP_DNSSORT_FAILURE )
+			bgp_dnssort_event(BGP_DNSSORT_BIND, 0);
+		return -1;
+	}
+	
+	/* start main DNSsort read thread */
+	bgp_dnssort_event(BGP_DNSSORT_READ, bgp_dnssort_sock);
+	return 0;
+}
+
+static void bgp_dnssort_event(enum bgp_dnssort_event event, int sock)
+{
+	switch (event) {
+		case BGP_DNSSORT_SCHEDULE:
+			bgp_dnssort_cache_IPv4_init(0);
+			bgp_dnssort_ctx[0].thread = 
+					thread_add_event (master, bgp_dnssort_cache_IPv4_start, &bgp_dnssort_ctx[0], 0);
+			bgp_dnssort_event(BGP_DNSSORT_BIND, 0); /* this gives us 10 seconds to create the cache */
+			break;
+		case BGP_DNSSORT_BIND:
+			bgp_dnssort_bind_thread = thread_add_timer (master, bgp_dnssort_bind, NULL, 10);
+			break;
+		case BGP_DNSSORT_READ:
+			bgp_dnssort_read_thread = thread_add_read (master, bgp_dnssort_read, NULL, sock);
+			break;
+		case BGP_DNSSORT_WRITE:
+			bgp_dnssort_write_thread = thread_add_write (master, bgp_dnssort_write, NULL, sock);
+			break;
+		default:
+			break;
+	}
+}
+
+void bgp_dnssort_stop()
+{
+	if ( bgp_dnssort_read_thread )
+		thread_cancel (bgp_dnssort_read_thread);
+	if ( bgp_dnssort_bind_thread )
+		thread_cancel (bgp_dnssort_bind_thread);
+	if ( bgp_dnssort_write_thread )
+		thread_cancel (bgp_dnssort_write_thread);
+	/* XXX multiviews */
+	if ( bgp_dnssort_ctx[0].thread )
+		thread_cancel (bgp_dnssort_ctx[0].thread);
+
+	if ( bgp_dnssort_sock >= 0 ) {
+		close (bgp_dnssort_sock);
+		bgp_dnssort_sock = -1;
+	}
+	bgp_dnssort_running = 0;
+}
+
+void bgp_dnssort_start()
+{
+	/* let's start */
+	bgp_dnssort_fail = 0;
+	bgp_dnssort_running = 1;
+	bgp_dnssort_event (BGP_DNSSORT_SCHEDULE, 0);
+}	
+
+int bgp_dnssort_listencmd(struct vty *vty, char *ip_str, int afi, char *portnum)
+/* AFI not used until now */
+{
+	struct bgp *bgp;
+	int ret;
+	char *endptr = NULL;
+
+	bgp = vty->index;
+	if (bgp_dnssort_running == 1) {
+		vty_out (vty, "DNSsort already running, stop first%s", VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+	if ( ip_str ) {
+		ret = inet_aton (ip_str, &bgp_dnssort_addr_v4);
+		if (!ret) {
+			vty_out (vty, "Malformed bgp dnssort listen address%s", VTY_NEWLINE);
+			return CMD_WARNING;
+		}
+	} else {
+		bgp_dnssort_addr_v4.s_addr = htonl(INADDR_ANY);
+	}
+	
+	if ( portnum ) {
+		bgp_dnssort_port = strtoul (portnum, &endptr, 10);
+		if (bgp_dnssort_port == ULONG_MAX || *endptr != '\0' || bgp_dnssort_port < 1 || bgp_dnssort_port > 65535) {
+			vty_out (vty, "Port value error%s", VTY_NEWLINE);
+			return CMD_WARNING;
+		}
+	} else {
+		bgp_dnssort_port = BGP_DNSSORT_DEFAULTPORT;
+	}
+		
+	bgp->bgp_dnssort_port = bgp_dnssort_port;
+	bgp->bgp_dnssort_addr_v4 = bgp_dnssort_addr_v4;
+	/* let's start */
+	bgp_dnssort_start ();
+	SET_FLAG(bgp->config, BGP_CONFIG_DNSSORT);
+	return CMD_SUCCESS;
+}
+
+DEFUN (bgp_dnssort_listen_v4_port, bgp_dnssort_listen_v4_port_cmd,
+	"dnssort listen A.B.C.D port <1-65535>",
+	"DNSsort Protocol commands\n"
+	"DNSsort's listen address\n"
+	"IP address\n"
+	"DNSsort's listen port\n"
+	"UDP port number\n")
+{
+	return bgp_dnssort_listencmd(vty, argv[0], AFI_IP, argv[1]);
+}
+
+DEFUN (bgp_dnssort_listen_v4, bgp_dnssort_listen_v4_cmd,
+	"dnssort listen A.B.C.D",
+	"DNSsort Protocol commands\n"
+	"DNSsort's listen address\n"
+	"IP address\n")
+{
+	return bgp_dnssort_listencmd(vty, argv[0], AFI_IP, NULL);
+}
+
+DEFUN (bgp_dnssort_listen_v4_default, bgp_dnssort_listen_v4_default_cmd,
+	"dnssort listen",
+	"DNSsort Protocol commands\n"
+	"Enable DNSsort protocol\n")
+{
+	return bgp_dnssort_listencmd(vty, NULL, AFI_IP, NULL);
+}
+
+DEFUN (no_bgp_dnssort, no_bgp_dnssort_cmd,
+	"no dnssort listen",
+	NO_STR
+	"DNSsort Protocol commands\n"
+	"Disable DNSsort protocol\n")
+{
+	struct bgp *bgp;
+
+	bgp = vty->index;
+	bgp_dnssort_stop();
+	UNSET_FLAG(bgp->config, BGP_CONFIG_DNSSORT);
+	return CMD_SUCCESS;
+}
+
+DEFUN (bgp_dnssort_queue_length, bgp_dnssort_queue_cmd,
+	"dnssort queue <1-65535>",
+	"DNSsort Protocol commands\n"
+	"DNSsort work queue length\n"
+	"maximal queue length\n")
+{
+	char *endptr = NULL;
+	
+	bgp_dnssort_queue_max = strtoul (argv[0], &endptr, 10);
+	if (bgp_dnssort_queue_max == ULONG_MAX || *endptr != '\0' || 
+			bgp_dnssort_queue_max < 1 || bgp_dnssort_queue_max > 65535) {
+		vty_out (vty, "Queue value error%s", VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+	return CMD_SUCCESS;
+}
+
+DEFUN (bgp_dnssort_cache_timeout, bgp_dnssort_cache_timeout_cmd,
+	"dnssort cachetimeout <1-65635>",
+	"DNSsort Protocol commands\n"
+	"DNSsort cache rerun timeout\n"
+	SECONDS_STR)
+{
+	char *endptr = NULL;
+	
+	bgp_dnssort_timeout = strtoul (argv[0], &endptr, 10);
+	if (bgp_dnssort_timeout == ULONG_MAX || *endptr != '\0' || 
+			bgp_dnssort_timeout < 1 || bgp_dnssort_timeout > 65535) {
+		vty_out (vty, "Timeout value error%s", VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+	return CMD_SUCCESS;
+}
+
+DEFUN (bgp_dnssort_show_stats, bgp_dnssort_show_stats_cmd,
+	"show dnssort statistics",
+	SHOW_STR
+	"DNSsort Protocol commands\n"
+	"DNSsort statistics\n")
+{
+	struct bgp_dnssort_IPv4_table	*tab;
+	int i;
+	
+	vty_out(vty, "DNSsort statistics%s------------------%s", VTY_NEWLINE, VTY_NEWLINE);
+	vty_out(vty, "recieved packets: %qu%s", bgp_dnssort_pkg_recv_cnt, VTY_NEWLINE);
+	vty_out(vty, "sent packets:     %qu%s", bgp_dnssort_pkg_send_cnt, VTY_NEWLINE);
+	vty_out(vty, "dropped packets:  %qu%s", bgp_dnssort_pkg_drop_cnt, VTY_NEWLINE);
+	vty_out(vty, "currently queued: %u / %u%s", bgp_dnssort_queue_cur, bgp_dnssort_queue_max, VTY_NEWLINE);
+
+	vty_out(vty, "%sDNSsort cache table for view %5d%s", VTY_NEWLINE, 0, VTY_NEWLINE);
+	vty_out(vty, "----------------------------------%s", VTY_NEWLINE);
+	tab = bgp_dnssort_IPv4_cache[0];
+	for ( i = 0; i < bgp_dnssort_IPv4_cache_size[0]; i++ ) {
+		vty_out(vty, "AS %d: %qd hits%s", ntohs(tab[i].asnum), tab[i].hitcount, VTY_NEWLINE);
+	}
+
+	return CMD_SUCCESS;
+}
+
+
+DEFUN (bgp_dnssort_reset_stats, bgp_dnssort_reset_stats_cmd,
+	"clear dnssort statistsics",
+	CLEAR_STR
+	"DNSsort Protocol commands\n"
+	"Reset statistics counter\n")
+{
+	struct bgp_dnssort_IPv4_table	*tab;
+	int i;
+	
+	bgp_dnssort_pkg_send_cnt = 0;
+	bgp_dnssort_pkg_drop_cnt = 0;
+	
+	tab = bgp_dnssort_IPv4_cache[0];
+	for ( i = 0; i < bgp_dnssort_IPv4_cache_size[0]; i++ ) {
+		tab[i].hitcount = 0;
+	}
+	vty_out(vty, "Statistics counters have been reset.%s", VTY_NEWLINE);
+
+	return CMD_SUCCESS;
+}
+
+
+void bgp_dnssort_init()
+{
+	/* init */
+	bgp_dnssort_running = 0;
+	bgp_dnssort_pkg_recv_cnt = 0;
+	bgp_dnssort_pkg_send_cnt = 0;
+	bgp_dnssort_pkg_drop_cnt = 0;
+	bgp_dnssort_timeout = BGP_DNSSORT_TIMEOUT; /* 1 minute */
+	bgp_dnssort_queue_max = BGP_DNSSORT_QUEUE_LENGTH; /* same as in djbdns */
+	bgp_dnssort_queue_init ();
+
+	install_element(BGP_NODE, &bgp_dnssort_listen_v4_port_cmd);
+	install_element(BGP_NODE, &bgp_dnssort_listen_v4_cmd);
+	install_element(BGP_NODE, &bgp_dnssort_listen_v4_default_cmd);
+	install_element(BGP_NODE, &no_bgp_dnssort_cmd);
+	install_element(CONFIG_NODE, &bgp_dnssort_queue_cmd);
+	install_element(CONFIG_NODE, &bgp_dnssort_cache_timeout_cmd);
+	install_element(VIEW_NODE, &bgp_dnssort_show_stats_cmd);
+	install_element(ENABLE_NODE, &bgp_dnssort_show_stats_cmd);
+	install_element(CONFIG_NODE, &bgp_dnssort_show_stats_cmd);
+	install_element(BGP_NODE, &bgp_dnssort_show_stats_cmd);
+	install_element(ENABLE_NODE, &bgp_dnssort_reset_stats_cmd);
+}
+
+#endif /* BGP_DNSSORT */
Index: zebra/bgpd/bgp_dnssort.h
diff -u /dev/null zebra/bgpd/bgp_dnssort.h:1.2
--- /dev/null	Tue Jan 15 18:21:43 2002
+++ zebra/bgpd/bgp_dnssort.h	Wed Dec 19 09:36:31 2001
@@ -0,0 +1,37 @@
+/* Implemetation of the DNS sort protocol.
+ * Copyright (C) 2001 Claudio Jeker
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Zebra; see the file COPYING.  If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.  
+ */
+#ifdef BGP_DNSSORT
+#ifndef _ZEBRA_BGD_DNSSORT_H
+#define _ZEBRA_BGD_DNSSORT_H
+
+
+int bgp_dnssort_read(struct thread *thread);
+int bgp_dnssort_write(struct thread *thread);
+void bgp_dnssort_init();
+
+#define BGP_DNSSORT_TIMEOUT 60 /* 1 minute */
+#define BGP_DNSSORT_QUEUE_LENGTH 128 /* same as in djbdns */
+
+extern int bgp_dnssort_timeout;
+extern int bgp_dnssort_queue_max;
+
+#endif /* _ZEBRA_BGD_DNSSORT_H */
+#endif /* BGP_DNSSORT */
Index: zebra/bgpd/bgp_main.c
diff -u zebra/bgpd/bgp_main.c:1.1.2.1 zebra/bgpd/bgp_main.c:1.4
--- zebra/bgpd/bgp_main.c:1.1.2.1	Fri Dec  7 14:24:53 2001
+++ zebra/bgpd/bgp_main.c	Tue Dec 18 15:09:46 2001
@@ -36,6 +36,9 @@
 #include "bgpd/bgp_network.h"
 #include "bgpd/bgp_attr.h"
 #include "bgpd/bgp_mplsvpn.h"
+#ifdef BGP_DNSSORT
+#include "bgpd/bgp_dnssort.h"
+#endif
 
 /* bgpd options, we use GNU getopt library. */
 struct option longopts[] = 
@@ -248,6 +251,9 @@
   memory_init ();
   bgp_init ();
   bgp_mplsvpn_init ();
+#ifdef BGP_DNSSORT
+  bgp_dnssort_init ();
+#endif
   sort_node ();
 
   /* Parse config file. */
Index: zebra/bgpd/bgpd.c
diff -u zebra/bgpd/bgpd.c:1.1.2.1 zebra/bgpd/bgpd.c:1.5
--- zebra/bgpd/bgpd.c:1.1.2.1	Fri Dec  7 14:24:54 2001
+++ zebra/bgpd/bgpd.c	Wed Dec 19 09:36:31 2001
@@ -53,6 +53,9 @@
 #include "bgpd/bgp_nexthop.h"
 #include "bgpd/bgp_damp.h"
 #include "bgpd/bgp_mplsvpn.h"
+#ifdef BGP_DNSSORT
+#include "bgpd/bgp_dnssort.h"
+#endif
 
 #define min(a, b) ((a) < (b) ? (a) : (b))
 
@@ -8481,6 +8484,17 @@
       vty_out (vty, "!%s", VTY_NEWLINE);
     }
 
+  if (bgp_dnssort_timeout != BGP_DNSSORT_TIMEOUT)
+    {
+      vty_out (vty, "dnssort cachetimeout %d%s", bgp_dnssort_timeout, VTY_NEWLINE);
+    }
+  
+  if (bgp_dnssort_queue_max != BGP_DNSSORT_QUEUE_LENGTH)
+    {
+      vty_out (vty, "dnssort queue %d%s", bgp_dnssort_queue_max, VTY_NEWLINE);
+      vty_out (vty, "!%s", VTY_NEWLINE);
+    }
+  
   /* BGP configuration. */
   LIST_LOOP (bgp_list, bgp, nn)
     {
@@ -8566,6 +8580,14 @@
       /* BGP network import check. */
       if (CHECK_FLAG (bgp->config, BGP_CONFIG_IMPORT_CHECK))
 	vty_out (vty, " bgp network import-check%s", VTY_NEWLINE);
+
+#ifdef BGP_DNSSORT
+      /* BGP DNSsort. */
+      if (CHECK_FLAG (bgp->config, BGP_CONFIG_DNSSORT))
+	vty_out (vty, " dnssort listen %s port %lu%s",
+		inet_ntoa ( bgp->bgp_dnssort_addr_v4),
+		bgp->bgp_dnssort_port, VTY_NEWLINE);
+#endif
 
       /* BGP scan interval. */
       bgp_config_write_scan_time (vty);
Index: zebra/bgpd/bgpd.h
diff -u zebra/bgpd/bgpd.h:1.1.2.1 zebra/bgpd/bgpd.h:1.3
--- zebra/bgpd/bgpd.h:1.1.2.1	Fri Dec  7 14:24:55 2001
+++ zebra/bgpd/bgpd.h	Fri Dec  7 14:39:58 2001
@@ -69,6 +69,9 @@
 #define BGP_CONFIG_ASPATH_IGNORE          (1 << 11)
 #define BGP_CONFIG_DAMPENING              (1 << 12)
 #define BGP_CONFIG_IMPORT_CHECK           (1 << 13)
+#ifdef BGP_DNSSORT
+#define BGP_CONFIG_DNSSORT                (1 << 14)
+#endif
 
   /* BGP identifier. */
   struct in_addr id;
@@ -117,6 +120,11 @@
   /* BGP default timer.  */
   u_int32_t default_holdtime;
   u_int32_t default_keepalive;
+
+#ifdef BGP_DNSSORT
+  u_int32_t bgp_dnssort_port;
+  struct in_addr bgp_dnssort_addr_v4;
+#endif
 };
 
 /* BGP filter structure. */
Index: zebra/lib/memory.c
diff -u zebra/lib/memory.c:1.1.2.1 zebra/lib/memory.c:1.5
--- zebra/lib/memory.c:1.1.2.1	Fri Dec  7 14:24:56 2001
+++ zebra/lib/memory.c	Wed Dec 19 09:37:21 2001
@@ -287,6 +287,12 @@
   { 0,                        "---------------------\r\n" },
   { MTYPE_BGP_DISTANCE,       "BGP distance    : %ld\r\n" },
   { MTYPE_BGP_NEXTHOP_CACHE,  "BGP nexthop cache:%ld\r\n" },
+#ifdef BGP_DNSSORT
+  { 0,                        "---------------------\r\n" },
+  { MTYPE_BGP_DNSSORT_MSG,    "BGP DNSsort msg : %ld\r\n" },
+  { MTYPE_BGP_DNSSORT_QUEUE,  "BGP DNSsort queue:%ld\r\n" },
+  { MTYPE_BGP_DNSSORT_CACHE,  "BGP DNSsort cache:%ld\r\n" },
+#endif
   { -1, NULL }
 };
 
Index: zebra/lib/memory.h
diff -u zebra/lib/memory.h:1.1.2.1 zebra/lib/memory.h:1.4
--- zebra/lib/memory.h:1.1.2.1	Fri Dec  7 14:24:56 2001
+++ zebra/lib/memory.h	Tue Dec 18 10:08:44 2001
@@ -157,6 +157,11 @@
   MTYPE_BGP_ROUTE_QUEUE,
   MTYPE_OSPF_IF_INFO,
   MTYPE_OSPF_IF_PARAMS,
+#ifdef BGP_DNSSORT
+  MTYPE_BGP_DNSSORT_MSG,
+  MTYPE_BGP_DNSSORT_QUEUE,
+	MTYPE_BGP_DNSSORT_CACHE,
+#endif
   MTYPE_MAX
 };
 
