Merge remote branch 'vincentbernat/feature/agentx'
diff --git a/babeld/Makefile.am b/babeld/Makefile.am
index 8703de0..af1201a 100644
--- a/babeld/Makefile.am
+++ b/babeld/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
diff --git a/bgpd/Makefile.am b/bgpd/Makefile.am
index 3051555..9928734 100644
--- a/bgpd/Makefile.am
+++ b/bgpd/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
diff --git a/bgpd/bgp_snmp.c b/bgpd/bgp_snmp.c
index 86cc087..c8f2aa5 100644
--- a/bgpd/bgp_snmp.c
+++ b/bgpd/bgp_snmp.c
@@ -21,14 +21,8 @@
 #include <zebra.h>
 
 #ifdef HAVE_SNMP
-#ifdef HAVE_NETSNMP
 #include <net-snmp/net-snmp-config.h>
 #include <net-snmp/net-snmp-includes.h>
-#else
-#include <asn1.h>
-#include <snmp.h>
-#include <snmp_impl.h>
-#endif
 
 #include "if.h"
 #include "log.h"
@@ -124,6 +118,7 @@
 
 /* BGP-MIB instances. */
 oid bgp_oid [] = { BGP4MIB };
+oid bgp_trap_oid [] = { BGP4MIB, 0 };
 
 /* IP address 0.0.0.0. */
 static struct in_addr bgp_empty_addr = {0};
@@ -460,7 +455,9 @@
   static struct in_addr addr;
   struct peer *peer;
 
-  *write_method = NULL;
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
   memset (&addr, 0, sizeof (struct in_addr));
 
   peer = bgpPeerTable_lookup (v, name, length, &addr, exact);
@@ -770,6 +767,9 @@
   if (! bgp)
     return NULL;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
   memset (&addr, 0, sizeof (struct prefix_ipv4));
 
   binfo = bgp4PathAttrLookup (v, name, length, bgp, &addr, exact);
@@ -839,8 +839,8 @@
 /* BGP Traps. */
 struct trap_object bgpTrapList[] =
 {
-  {bgpPeerTable, 3, {3, 1, BGPPEERLASTERROR}},
-  {bgpPeerTable, 3, {3, 1, BGPPEERSTATE}}
+  {3, {3, 1, BGPPEERLASTERROR}},
+  {3, {3, 1, BGPPEERSTATE}}
 };
 
 void
@@ -856,10 +856,12 @@
 
   oid_copy_addr (index, &addr, IN_ADDR_SIZE);
 
-  smux_trap (bgp_oid, sizeof bgp_oid / sizeof (oid),
+  smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
+	     bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
+	     bgp_oid, sizeof bgp_oid / sizeof (oid),
 	     index, IN_ADDR_SIZE,
 	     bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
-	     bm->start_time - bgp_clock (), BGPESTABLISHED);
+	     BGPESTABLISHED);
 }
 
 void
@@ -875,10 +877,12 @@
 
   oid_copy_addr (index, &addr, IN_ADDR_SIZE);
 
-  smux_trap (bgp_oid, sizeof bgp_oid / sizeof (oid),
+  smux_trap (bgp_variables, sizeof bgp_variables / sizeof (struct variable),
+	     bgp_trap_oid, sizeof bgp_trap_oid / sizeof (oid),
+	     bgp_oid, sizeof bgp_oid / sizeof (oid),
 	     index, IN_ADDR_SIZE,
 	     bgpTrapList, sizeof bgpTrapList / sizeof (struct trap_object),
-	     bm->start_time - bgp_clock (), BGPBACKWARDTRANSITION);
+	     BGPBACKWARDTRANSITION);
 }
 
 void
diff --git a/configure.ac b/configure.ac
index 80f65b9..277bad5 100755
--- a/configure.ac
+++ b/configure.ac
@@ -221,10 +221,8 @@
 [  --enable-netlink        force to use Linux netlink interface])
 AC_ARG_ENABLE(broken-aliases,
 [  --enable-broken-aliases enable aliases as distinct interfaces for Linux 2.2.X])
-AC_ARG_WITH(crypto,
-[  --without-crypto        do not use libcrypto in SNMP])
 AC_ARG_ENABLE(snmp,
-[  --enable-snmp           enable SNMP support])
+[  --enable-snmp=ARG       enable SNMP support (smux or agentx)])
 AC_ARG_WITH(libpam,
 [  --with-libpam           use libpam for PAM support in vtysh])
 AC_ARG_ENABLE(tcp-zebra,
@@ -1365,21 +1363,39 @@
 dnl ------------------
 dnl check Net-SNMP library
 dnl ------------------
-if test "${enable_snmp}" = "yes"; then
-    if test "$with_crypto" != "no"; then
-        LIBS="${LIBS} -lcrypto";
-    fi
-    AC_CHECK_LIB(netsnmp, asn_parse_int,
-    	[AC_DEFINE(HAVE_NETSNMP,,Net SNMP) 
-    	 AC_DEFINE(HAVE_SNMP,,SNMP)
-    	 LIBS="${LIBS} -lnetsnmp"],
-    	[AC_MSG_ERROR([--enable-snmp given, but cannot find support for SNMP])])
-
-    AC_CHECK_HEADER([net-snmp/net-snmp-config.h],
-        [],
-        [AC_MSG_ERROR([--enable-snmp given, but cannot find net-snmp-config.h])],
-	QUAGGA_INCLUDES)
-    AC_SUBST(SNMP_INCLUDES)
+if test "${enable_snmp}" != ""; then
+   AC_PATH_TOOL([NETSNMP_CONFIG], [net-snmp-config], [no])
+   if test x"$NETSNMP_CONFIG" = x"no"; then
+      AC_MSG_ERROR([--enable-snmp given but unable to find net-snmp-config])
+   fi
+   LIBS="$LIBS `${NETSNMP_CONFIG} --agent-libs`"
+   CFLAGS="`${NETSNMP_CONFIG} --base-cflags` $CFLAGS"
+   AC_MSG_CHECKING([whether we can link to Net-SNMP])
+   AC_LINK_IFELSE([AC_LANG_PROGRAM([
+int main(void);
+],
+[
+{
+  return 0;
+}
+])],[AC_MSG_RESULT(yes)],[
+     AC_MSG_RESULT(no)
+     AC_MSG_ERROR([--enable-snmp given but not usable])])
+   AC_DEFINE(HAVE_SNMP,,SNMP)
+   case "${enable_snmp}" in
+     yes)
+      SNMP_METHOD=agentx
+      ;;
+     smux|agentx)
+      SNMP_METHOD="${enable_snmp}"
+      ;;
+     *)
+      AC_MSG_ERROR([--enable-snmp given with an unknown method (${enable_snmp}). Use smux or agentx])
+      ;;
+   esac
+   AH_TEMPLATE([SNMP_SMUX], [Use SNMP SMUX to interface with snmpd])
+   AH_TEMPLATE([SNMP_AGENTX], [Use SNMP AgentX to interface with snmpd])
+   AC_DEFINE_UNQUOTED(AS_TR_CPP(SNMP_${SNMP_METHOD}),,SNMP method to interface with snmpd)
 fi
 
 dnl ---------------------------
@@ -1625,7 +1641,7 @@
 compiler                : ${CC}
 compiler flags          : ${CFLAGS}
 make                    : ${MAKE-make}
-includes                : ${INCLUDES} ${SNMP_INCLUDES}
+includes                : ${INCLUDES}
 linker flags            : ${LDFLAGS} ${LIBS} ${LIBCAP} ${LIBREADLINE} ${LIBM}
 state file directory    : ${quagga_statedir}
 config file directory   : `eval echo \`echo ${sysconfdir}\``
diff --git a/doc/overview.texi b/doc/overview.texi
index 435834b..7acfc0e 100644
--- a/doc/overview.texi
+++ b/doc/overview.texi
@@ -7,7 +7,7 @@
 as RIPv1, RIPv2, RIPng, OSPFv2, OSPFv3, BGP-4, and BGP-4+ (@pxref{Supported
 RFCs}). Quagga also supports special BGP Route Reflector and Route Server
 behavior.  In addition to traditional IPv4 routing protocols, Quagga also
-supports IPv6 routing protocols.  With SNMP daemon which supports SMUX
+supports IPv6 routing protocols.  With SNMP daemon which supports SMUX and AgentX
 protocol, Quagga provides routing protocol MIBs (@pxref{SNMP Support}).
 
   Quagga uses an advanced software architecture to provide you with a high
@@ -238,6 +238,9 @@
 @cite{OSPF Version 2 Management Information Base. F. Baker, R. Coltun.
 November 1995.}
 
+@item @asis{RFC2741}
+@cite{Agent Extensibility (AgentX) Protocol. M. Daniele, B. Wijnen. January 2000.}
+
 @end table
 
 @node How to get Quagga
diff --git a/doc/snmp.texi b/doc/snmp.texi
index 3f80cc5..0918a46 100644
--- a/doc/snmp.texi
+++ b/doc/snmp.texi
@@ -5,11 +5,12 @@
 feature for collecting network information from router and/or host.
 Quagga itself does not support SNMP agent (server daemon) functionality
 but is able to connect to a SNMP agent using the SMUX protocol
-(@cite{RFC1227}) and make the routing protocol MIBs available through
-it.
+(@cite{RFC1227}) or the AgentX protocol (@cite{RFC2741}) and make the
+routing protocol MIBs available through it.
 
 @menu
 * Getting and installing an SNMP agent::
+* AgentX configuration::
 * SMUX configuration::
 * MIB and command reference::
 * Handling SNMP Traps::
@@ -18,20 +19,82 @@
 @node Getting and installing an SNMP agent
 @section Getting and installing an SNMP agent
 
-There are several SNMP agent which support SMUX. We recommend to use the latest
+There are several SNMP agent which support SMUX or AgentX. We recommend to use the latest
 version of @code{net-snmp} which was formerly known as @code{ucd-snmp}.
 It is free and open software and available at @uref{http://www.net-snmp.org/}
 and as binary package for most Linux distributions.
-@code{net-snmp} has to be compiled with @code{--with-mib-modules=smux} to
-be able to accept connections from Quagga.
+@code{net-snmp} has to be compiled with @code{--with-mib-modules=agentx} to
+be able to accept connections from Quagga using AgentX protocol or with
+@code{--with-mib-modules=smux} to use SMUX protocol.
+
+Nowadays, SMUX is a legacy protocol. The AgentX protocol should be
+preferred for any new deployment. Both protocols have the same coverage.
+
+@node AgentX configuration
+@section AgentX configuration
+
+To enable AgentX protocol support, Quagga must have been build with the
+@code{--enable-snmp} or @code{--enable-snmp=agentx} option. Both the
+master SNMP agent (snmpd) and each of the Quagga daemons must be
+configured. In @code{/etc/snmp/snmpd.conf}, @code{master agentx}
+directive should be added. In each of the Quagga daemons, @code{agentx}
+command will enable AgentX support.
+
+@example
+/etc/snmp/snmpd.conf:
+	#
+	# example access restrictions setup
+	#
+	com2sec readonly default public
+	group MyROGroup v1 readonly
+	view all included .1 80
+	access MyROGroup "" any noauth exact all none none
+	#
+	# enable master agent for AgentX subagents
+	#
+	master agentx
+
+/etc/quagga/ospfd.conf:
+	! ... the rest of ospfd.conf has been omitted for clarity ...
+	!
+	agentx
+	!
+@end example
+
+Upon successful connection, you should get something like this in the
+log of each Quagga daemons:
+
+@example
+2012/05/25 11:39:08 ZEBRA: snmp[info]: NET-SNMP version 5.4.3 AgentX subagent connected
+@end example
+
+Then, you can use the following command to check everything works as expected:
+
+@example
+# snmpwalk -c public -v1 localhost .1.3.6.1.2.1.14.1.1
+OSPF-MIB::ospfRouterId.0 = IpAddress: 192.168.42.109
+[...]
+@end example
+
+The AgentX protocol can be transported over a Unix socket or using TCP
+or UDP. It usually defaults to a Unix socket and depends on how NetSNMP
+was built. If need to configure Quagga to use another transport, you can
+configure it through @code{/etc/snmp/quagga.conf}:
+
+@example
+/etc/snmp/quagga.conf:
+	[snmpd]
+	# Use a remote master agent
+	agentXSocket tcp:192.168.15.12:705
+@end example
 
 @node SMUX configuration
 @section SMUX configuration
 
 To enable SMUX protocol support, Quagga must have been build with the
-@code{--enable-snmp} option.
+@code{--enable-snmp=smux} option.
 
-A separate connection has then to be established between between the
+A separate connection has then to be established between the
 SNMP agent (snmpd) and each of the Quagga daemons. This connections
 each use different OID numbers and passwords. Be aware that this OID
 number is not the one that is used in queries by clients, it is solely
@@ -85,7 +148,7 @@
 @section MIB and command reference
 
 The following OID numbers are used for the interprocess communication of snmpd and
-the Quagga daemons. Sadly, SNMP has not been implemented in all daemons yet.
+the Quagga daemons with SMUX only.
 @example
             (OIDs below .iso.org.dod.internet.private.enterprises)
 zebra	.1.3.6.1.4.1.3317.1.2.1 .gnome.gnomeProducts.zebra.zserv
@@ -95,7 +158,8 @@
 ospf6d	.1.3.6.1.4.1.3317.1.2.6 .gnome.gnomeProducts.zebra.ospf6d
 @end example
 
-The following OID numbers are used for querying the SNMP daemon by a client:
+Sadly, SNMP has not been implemented in all daemons yet. The following
+OID numbers are used for querying the SNMP daemon by a client:
 @example
 zebra	.1.3.6.1.2.1.4.24   .iso.org.dot.internet.mgmt.mib-2.ip.ipForward
 ospfd	.1.3.6.1.2.1.14	    .iso.org.dot.internet.mgmt.mib-2.ospf
@@ -104,7 +168,7 @@
 ospf6d	.1.3.6.1.3.102	    .iso.org.dod.internet.experimental.ospfv3
 @end example
 
-The following syntax is understood by the Quagga daemons for configuring SNMP:
+The following syntax is understood by the Quagga daemons for configuring SNMP using SMUX:
 @deffn {Command} {smux peer @var{oid}} {}
 @deffnx {Command} {no smux peer @var{oid}} {}
 @end deffn
@@ -113,4 +177,9 @@
 @deffnx {Command} {no smux peer @var{oid} @var{password}} {}
 @end deffn
 
+Here is the syntax for using AgentX:
+@deffn {Command} {agentx} {}
+@deffnx {Command} {no agentx} {}
+@end deffn
+
 @include snmptrap.texi
diff --git a/guile/Makefile.am b/guile/Makefile.am
index 5beb71c..8d7008e 100644
--- a/guile/Makefile.am
+++ b/guile/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with Automake to create Makefile.in
 
-INCLUDES = @GUILE_CFLAGS@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib
+INCLUDES = @GUILE_CFLAGS@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -I. -I$(srcdir)
 
 AM_CFLAGS = $(PICFLAGS)
diff --git a/isisd/Makefile.am b/isisd/Makefile.am
index 26b8ee7..4e9b244 100644
--- a/isisd/Makefile.am
+++ b/isisd/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib \
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib \
 	   @ISIS_TOPOLOGY_INCLUDES@
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
diff --git a/isisd/topology/Makefile.am b/isisd/topology/Makefile.am
index b25497c..ccd2e71 100644
--- a/isisd/topology/Makefile.am
+++ b/isisd/topology/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 
 AM_CFLAGS = $(PICFLAGS)
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 890cc5c..e00ad54 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 
 lib_LTLIBRARIES = libzebra.la
@@ -11,7 +11,7 @@
 	checksum.c vector.c linklist.c vty.c command.c \
 	sockunion.c prefix.c thread.c if.c memory.c buffer.c table.c hash.c \
 	filter.c routemap.c distribute.c stream.c str.c log.c plist.c \
-	zclient.c sockopt.c smux.c md5.c if_rmap.c keychain.c privs.c \
+	zclient.c sockopt.c smux.c agentx.c snmp.c md5.c if_rmap.c keychain.c privs.c \
 	sigevent.c pqueue.c jhash.c memtypes.c workqueue.c
 
 BUILT_SOURCES = memtypes.h route_types.h
diff --git a/lib/agentx.c b/lib/agentx.c
new file mode 100644
index 0000000..be6b432
--- /dev/null
+++ b/lib/agentx.c
@@ -0,0 +1,213 @@
+/* SNMP support
+ * Copyright (C) 2012 Vincent Bernat <bernat@luffy.cx>
+ *
+ * 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>
+
+#if defined HAVE_SNMP && defined SNMP_AGENTX
+#include <net-snmp/net-snmp-config.h>
+#include <net-snmp/net-snmp-includes.h>
+
+#include "command.h"
+#include "smux.h"
+
+int agentx_enabled = 0;
+
+/* AgentX node. */
+static struct cmd_node agentx_node =
+{
+  SMUX_NODE,
+  ""                            /* AgentX has no interface. */
+};
+
+/* Logging NetSNMP messages */
+static int
+agentx_log_callback(int major, int minor,
+		    void *serverarg, void *clientarg)
+{
+  struct snmp_log_message *slm = (struct snmp_log_message *)serverarg;
+  char *msg = strdup (slm->msg);
+  if (msg) msg[strlen(msg)-1] = '\0';
+  switch (slm->priority)
+    {
+    case LOG_EMERG:   zlog_err   ("snmp[emerg]: %s",   msg?msg:slm->msg); break;
+    case LOG_ALERT:   zlog_err   ("snmp[alert]: %s",   msg?msg:slm->msg); break;
+    case LOG_CRIT:    zlog_err   ("snmp[crit]: %s",    msg?msg:slm->msg); break;
+    case LOG_ERR:     zlog_err   ("snmp[err]: %s",     msg?msg:slm->msg); break;
+    case LOG_WARNING: zlog_warn  ("snmp[warning]: %s", msg?msg:slm->msg); break;
+    case LOG_NOTICE:  zlog_notice("snmp[notice]: %s",  msg?msg:slm->msg); break;
+    case LOG_INFO:    zlog_info  ("snmp[info]: %s",    msg?msg:slm->msg); break;
+    case LOG_DEBUG:   zlog_debug ("snmp[debug]: %s",   msg?msg:slm->msg); break;
+    }
+  free(msg);
+  return SNMP_ERR_NOERROR;
+}
+
+static int
+config_write_agentx (struct vty *vty)
+{
+  if (agentx_enabled)
+      vty_out (vty, "agentx%s", VTY_NEWLINE);
+  return 0;
+}
+
+DEFUN (agentx_enable,
+       agentx_enable_cmd,
+       "agentx",
+       "SNMP AgentX protocol settings\n"
+       "SNMP AgentX settings\n")
+{
+  if (!agentx_enabled)
+    {
+      init_snmp("quagga");
+      agentx_enabled = 1;
+      return CMD_SUCCESS;
+    }
+  vty_out (vty, "SNMP AgentX already enabled%s", VTY_NEWLINE);
+  return CMD_WARNING;
+}
+
+DEFUN (no_agentx,
+       no_agentx_cmd,
+       "no agentx",
+       NO_STR
+       "SNMP AgentX protocol settings\n"
+       "SNMP AgentX settings\n")
+{
+  if (!agentx_enabled) return CMD_SUCCESS;
+  vty_out (vty, "SNMP AgentX support cannot be disabled once enabled%s", VTY_NEWLINE);
+  return CMD_WARNING;
+}
+
+void
+smux_init (struct thread_master *tm)
+{
+  netsnmp_enable_subagent ();
+  snmp_disable_log ();
+  snmp_enable_calllog ();
+  snmp_register_callback (SNMP_CALLBACK_LIBRARY,
+			  SNMP_CALLBACK_LOGGING,
+			  agentx_log_callback,
+			  NULL);
+  init_agent ("quagga");
+
+  install_node (&agentx_node, config_write_agentx);
+  install_element (CONFIG_NODE, &agentx_enable_cmd);
+  install_element (CONFIG_NODE, &no_agentx_cmd);
+}
+
+void
+smux_register_mib (const char *descr, struct variable *var, 
+		   size_t width, int num,
+		   oid name[], size_t namelen)
+{
+  register_mib (descr, var, width, num, name, namelen);
+}
+
+int
+smux_trap (struct variable *vp, size_t vp_len,
+	   const oid *ename, size_t enamelen,
+	   const oid *name, size_t namelen,
+	   const oid *iname, size_t inamelen,
+	   const struct trap_object *trapobj, size_t trapobjlen,
+	   u_char sptrap)
+{
+  oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
+  size_t objid_snmptrap_len = sizeof objid_snmptrap / sizeof (oid);
+  oid notification_oid[MAX_OID_LEN];
+  size_t notification_oid_len;
+  unsigned int i;
+
+  netsnmp_variable_list *notification_vars = NULL;
+  if (!agentx_enabled) return 0;
+
+  /* snmpTrapOID */
+  oid_copy (notification_oid, ename, enamelen);
+  notification_oid[enamelen] = sptrap;
+  notification_oid_len = enamelen + 1;
+  snmp_varlist_add_variable (&notification_vars,
+			     objid_snmptrap, objid_snmptrap_len,
+			     ASN_OBJECT_ID,
+			     (u_char *) notification_oid,
+			     notification_oid_len * sizeof(oid));
+
+  /* Provided bindings */
+  for (i = 0; i < trapobjlen; i++)
+    {
+      unsigned int j;
+      oid oid[MAX_OID_LEN];
+      size_t oid_len, onamelen;
+      u_char *val;
+      size_t val_len;
+      WriteMethod *wm = NULL;
+      struct variable cvp;
+
+      /* Make OID. */
+      if (trapobj[i].namelen > 0)
+        {
+	  /* Columnar object */
+	  onamelen = trapobj[i].namelen;
+	  oid_copy (oid, name, namelen);
+	  oid_copy (oid + namelen, trapobj[i].name, onamelen);
+	  oid_copy (oid + namelen + onamelen, iname, inamelen);
+	  oid_len = namelen + onamelen + inamelen;
+        }
+      else
+        {
+	  /* Scalar object */
+	  onamelen = trapobj[i].namelen * (-1);
+	  oid_copy (oid, name, namelen);
+	  oid_copy (oid + namelen, trapobj[i].name, onamelen);
+	  oid[onamelen + namelen] = 0;
+	  oid_len = namelen + onamelen + 1;
+        }
+
+      /* Locate the appropriate function and type in the MIB registry. */
+      for (j = 0; j < vp_len; j++)
+	{
+	  if (oid_compare (trapobj[i].name, onamelen, vp[j].name, vp[j].namelen) != 0)
+	    continue;
+	  /* We found the appropriate variable in the MIB registry. */
+	  oid_copy(cvp.name, name, namelen);
+	  oid_copy(cvp.name + namelen, vp[j].name, vp[j].namelen);
+	  cvp.namelen = namelen + vp[j].namelen;
+	  cvp.type = vp[j].type;
+	  cvp.magic = vp[j].magic;
+	  cvp.acl = vp[j].acl;
+	  cvp.findVar = vp[j].findVar;
+	  /* Grab the result. */
+	  val = cvp.findVar (&cvp, oid, &oid_len, 1, &val_len, &wm);
+	  if (!val) break;
+	  snmp_varlist_add_variable (&notification_vars,
+				     oid, oid_len,
+				     vp[j].type,
+				     val,
+				     val_len);
+	  break;
+	}
+    }
+
+
+  send_v2trap (notification_vars);
+  snmp_free_varbind (notification_vars);
+  return 1;
+}
+
+#endif /* HAVE_SNMP */
diff --git a/lib/smux.c b/lib/smux.c
index b7cd18d..0746640 100644
--- a/lib/smux.c
+++ b/lib/smux.c
@@ -21,15 +21,9 @@
 
 #include <zebra.h>
 
-#ifdef HAVE_SNMP
-#ifdef HAVE_NETSNMP
+#if defined HAVE_SNMP && defined SNMP_SMUX
 #include <net-snmp/net-snmp-config.h>
 #include <net-snmp/net-snmp-includes.h>
-#else
-#include <asn1.h>
-#include <snmp.h>
-#include <snmp_impl.h>
-#endif
 
 #include "log.h"
 #include "thread.h"
@@ -40,6 +34,45 @@
 #include "sockunion.h"
 #include "smux.h"
 
+#define SMUX_PORT_DEFAULT 199
+
+#define SMUXMAXPKTSIZE    1500
+#define SMUXMAXSTRLEN      256
+
+#define SMUX_OPEN       (ASN_APPLICATION | ASN_CONSTRUCTOR | 0)
+#define SMUX_CLOSE      (ASN_APPLICATION | ASN_PRIMITIVE | 1)
+#define SMUX_RREQ       (ASN_APPLICATION | ASN_CONSTRUCTOR | 2)
+#define SMUX_RRSP       (ASN_APPLICATION | ASN_PRIMITIVE | 3)
+#define SMUX_SOUT       (ASN_APPLICATION | ASN_PRIMITIVE | 4)
+
+#define SMUX_GET        (ASN_CONTEXT | ASN_CONSTRUCTOR | 0)
+#define SMUX_GETNEXT    (ASN_CONTEXT | ASN_CONSTRUCTOR | 1)
+#define SMUX_GETRSP     (ASN_CONTEXT | ASN_CONSTRUCTOR | 2)
+#define SMUX_SET	(ASN_CONTEXT | ASN_CONSTRUCTOR | 3)
+#define SMUX_TRAP	(ASN_CONTEXT | ASN_CONSTRUCTOR | 4)
+
+#define SMUX_MAX_FAILURE 3
+
+/* SNMP tree. */
+struct subtree
+{
+  /* Tree's oid. */
+  oid name[MAX_OID_LEN];
+  u_char name_len;
+
+  /* List of the variables. */
+  struct variable *variables;
+
+  /* Length of the variables list. */
+  int variables_num;
+
+  /* Width of the variables list. */
+  int variables_width;
+
+  /* Registered flag. */
+  int registered;
+};
+
 #define min(A,B) ((A) < (B) ? (A) : (B))
 
 enum smux_event {SMUX_SCHEDULE, SMUX_CONNECT, SMUX_READ};
@@ -82,62 +115,6 @@
 /* thread master */
 static struct thread_master *master;
 
-void *
-oid_copy (void *dest, const void *src, size_t size)
-{
-  return memcpy (dest, src, size * sizeof (oid));
-}
-
-void
-oid2in_addr (oid oid[], int len, struct in_addr *addr)
-{
-  int i;
-  u_char *pnt;
-  
-  if (len == 0)
-    return;
-
-  pnt = (u_char *) addr;
-
-  for (i = 0; i < len; i++)
-    *pnt++ = oid[i];
-}
-
-void
-oid_copy_addr (oid oid[], struct in_addr *addr, int len)
-{
-  int i;
-  u_char *pnt;
-  
-  if (len == 0)
-    return;
-
-  pnt = (u_char *) addr;
-
-  for (i = 0; i < len; i++)
-    oid[i] = *pnt++;
-}
-
-int
-oid_compare (oid *o1, int o1_len, oid *o2, int o2_len)
-{
-  int i;
-
-  for (i = 0; i < min (o1_len, o2_len); i++)
-    {
-      if (o1[i] < o2[i])
-	return -1;
-      else if (o1[i] > o2[i])
-	return 1;
-    }
-  if (o1_len < o2_len)
-    return -1;
-  if (o1_len > o2_len)
-    return 1;
-
-  return 0;
-}
-
 static int
 oid_compare_part (oid *o1, int o1_len, oid *o2, int o2_len)
 {
@@ -479,7 +456,7 @@
                   if (write_method)
                     {
                       return (*write_method)(action, val, val_type, val_len,
-					     statP, suffix, suffix_len, v);
+					     statP, suffix, suffix_len);
                     }
                   else
                     {
@@ -991,11 +968,18 @@
   return send (sock, buf, (ptr - buf), 0);
 }
 
+/* `ename` is ignored. Instead of using the provided enterprise OID,
+   the SMUX peer is used. This keep compatibility with the previous
+   versions of Quagga.
+
+   All other fields are used as they are intended. */
 int
-smux_trap (const oid *name, size_t namelen,
+smux_trap (struct variable *vp, size_t vp_len,
+	   const oid *ename, size_t enamelen,
+	   const oid *name, size_t namelen,
 	   const oid *iname, size_t inamelen,
 	   const struct trap_object *trapobj, size_t trapobjlen,
-	   unsigned int tick, u_char sptrap)
+	   u_char sptrap)
 {
   unsigned int i;
   u_char buf[BUFSIZ];
@@ -1360,32 +1344,6 @@
   return 0;
 }
 
-int
-smux_header_generic (struct variable *v, oid *name, size_t *length, int exact,
-		     size_t *var_len, WriteMethod **write_method)
-{
-  oid fulloid[MAX_OID_LEN];
-  int ret;
-
-  oid_copy (fulloid, v->name, v->namelen);
-  fulloid[v->namelen] = 0;
-  /* Check against full instance. */
-  ret = oid_compare (name, *length, fulloid, v->namelen + 1);
-
-  /* Check single instance. */
-  if ((exact && (ret != 0)) || (!exact && (ret >= 0)))
-	return MATCH_FAILED;
-
-  /* In case of getnext, fill in full instance. */
-  memcpy (name, fulloid, (v->namelen + 1) * sizeof (oid));
-  *length = v->namelen + 1;
-
-  *write_method = 0;
-  *var_len = sizeof(long);    /* default to 'long' results */
-
-  return MATCH_SUCCEEDED;
-}
-
 static int
 smux_peer_default (void)
 {
diff --git a/lib/smux.h b/lib/smux.h
index f5754ed..72b4eaf 100644
--- a/lib/smux.h
+++ b/lib/smux.h
@@ -22,24 +22,8 @@
 #ifndef _ZEBRA_SNMP_H
 #define _ZEBRA_SNMP_H
 
-#define SMUX_PORT_DEFAULT 199
-
-#define SMUXMAXPKTSIZE    1500
-#define SMUXMAXSTRLEN      256
-
-#define SMUX_OPEN       (ASN_APPLICATION | ASN_CONSTRUCTOR | 0)
-#define SMUX_CLOSE      (ASN_APPLICATION | ASN_PRIMITIVE | 1)
-#define SMUX_RREQ       (ASN_APPLICATION | ASN_CONSTRUCTOR | 2)
-#define SMUX_RRSP       (ASN_APPLICATION | ASN_PRIMITIVE | 3)
-#define SMUX_SOUT       (ASN_APPLICATION | ASN_PRIMITIVE | 4)
-
-#define SMUX_GET        (ASN_CONTEXT | ASN_CONSTRUCTOR | 0)
-#define SMUX_GETNEXT    (ASN_CONTEXT | ASN_CONSTRUCTOR | 1)
-#define SMUX_GETRSP     (ASN_CONTEXT | ASN_CONSTRUCTOR | 2)
-#define SMUX_SET	(ASN_CONTEXT | ASN_CONSTRUCTOR | 3)
-#define SMUX_TRAP	(ASN_CONTEXT | ASN_CONSTRUCTOR | 4)
-
-#define SMUX_MAX_FAILURE 3
+#include <net-snmp/agent/net-snmp-agent-includes.h>
+#include <net-snmp/agent/snmp_vars.h>
 
 /* Structures here are mostly compatible with UCD SNMP 4.1.1 */
 #define MATCH_FAILED     (-1)
@@ -55,73 +39,15 @@
 
 #define IN_ADDR_SIZE sizeof(struct in_addr)
 
-struct variable;
-
+#undef REGISTER_MIB
 #define REGISTER_MIB(descr, var, vartype, theoid)		\
     smux_register_mib(descr, (struct variable *)var, sizeof(struct vartype), \
     sizeof(var)/sizeof(struct vartype),			\
     theoid, sizeof(theoid)/sizeof(oid))
 
-typedef int (WriteMethod)(int action,
-			  u_char  *var_val,
-			  u_char   var_val_type,
-			  size_t   var_val_len,
-			  u_char  *statP,
-			  oid     *name,
-			  size_t   length,
-			  struct variable *v);
-
-typedef u_char *(FindVarMethod)(struct variable *v,
-				oid     *name,
-				size_t  *length,
-				int      exact,
-				size_t  *var_len,
-				WriteMethod   **write_method);
-
-/* SNMP variable */
-struct variable
-{
-  /* Index of the MIB.*/
-  u_char magic;
-
-  /* Type of variable. */
-  char type;
-
-  /* Access control list. */
-  u_short acl;
-
-  /* Callback function. */
-  FindVarMethod *findVar;
-
-  /* Suffix of the MIB. */
-  int namelen;
-  oid name[MAX_OID_LEN];
-};
-
-/* SNMP tree. */
-struct subtree
-{
-  /* Tree's oid. */
-  oid name[MAX_OID_LEN];
-  u_char name_len;
-
-  /* List of the variables. */
-  struct variable *variables;
-
-  /* Length of the variables list. */
-  int variables_num;
-
-  /* Width of the variables list. */
-  int variables_width;
-
-  /* Registered flag. */
-  int registered;
-};
-
 struct trap_object
 {
-  FindVarMethod *findVar;
-  int namelen;
+  int namelen; /* Negative if the object is not indexed */
   oid name[MAX_OID_LEN];
 };
 
@@ -145,14 +71,41 @@
   )
 
 extern void smux_init (struct thread_master *tm);
-extern void smux_start (void);
 extern void smux_register_mib(const char *, struct variable *, 
                               size_t, int, oid [], size_t);
 extern int smux_header_generic (struct variable *, oid [], size_t *, 
                                 int, size_t *, WriteMethod **);
-extern int smux_trap (const oid *, size_t, const oid *, size_t, 
-		      const struct trap_object *, 
-                      size_t, unsigned int, u_char);
+extern int smux_header_table (struct variable *, oid *, size_t *, 
+			      int, size_t *, WriteMethod **);
+
+/* For traps, three OID are provided:
+
+ 1. The enterprise OID to use (the last argument will be appended to
+    it to form the SNMP trap OID)
+
+ 2. The base OID for objects to be sent in traps.
+
+ 3. The index OID for objects to be sent in traps. This index is used
+    to designate a particular instance of a column.
+
+ The provided trap object contains the bindings to be sent with the
+ trap. The base OID will be prefixed to the provided OID and, if the
+ length is positive, the requested OID is assumed to be a columnar
+ object and the index OID will be appended.
+
+ The two first arguments are the MIB registry used to locate the trap
+ objects.
+
+ The use of the arguments may differ depending on the implementation
+ used.
+*/
+extern int smux_trap (struct variable *, size_t,
+		      const oid *, size_t,
+		      const oid *, size_t,
+		      const oid *, size_t,
+		      const struct trap_object *, size_t,
+		      u_char);
+
 extern int oid_compare (oid *, int, oid *, int);
 extern void oid2in_addr (oid [], int, struct in_addr *);
 extern void *oid_copy (void *, const void *, size_t);
diff --git a/lib/snmp.c b/lib/snmp.c
new file mode 100644
index 0000000..79595a1
--- /dev/null
+++ b/lib/snmp.c
@@ -0,0 +1,133 @@
+/* SNMP support
+ * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
+ *
+ * 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 HAVE_SNMP
+#include <net-snmp/net-snmp-config.h>
+#include <net-snmp/net-snmp-includes.h>
+
+#include "smux.h"
+
+#define min(A,B) ((A) < (B) ? (A) : (B))
+
+int
+oid_compare (oid *o1, int o1_len, oid *o2, int o2_len)
+{
+  int i;
+
+  for (i = 0; i < min (o1_len, o2_len); i++)
+    {
+      if (o1[i] < o2[i])
+	return -1;
+      else if (o1[i] > o2[i])
+	return 1;
+    }
+  if (o1_len < o2_len)
+    return -1;
+  if (o1_len > o2_len)
+    return 1;
+
+  return 0;
+}
+
+void *
+oid_copy (void *dest, const void *src, size_t size)
+{
+  return memcpy (dest, src, size * sizeof (oid));
+}
+
+void
+oid2in_addr (oid oid[], int len, struct in_addr *addr)
+{
+  int i;
+  u_char *pnt;
+  
+  if (len == 0)
+    return;
+
+  pnt = (u_char *) addr;
+
+  for (i = 0; i < len; i++)
+    *pnt++ = oid[i];
+}
+
+void
+oid_copy_addr (oid oid[], struct in_addr *addr, int len)
+{
+  int i;
+  u_char *pnt;
+  
+  if (len == 0)
+    return;
+
+  pnt = (u_char *) addr;
+
+  for (i = 0; i < len; i++)
+    oid[i] = *pnt++;
+}
+
+int
+smux_header_generic (struct variable *v, oid *name, size_t *length, int exact,
+		     size_t *var_len, WriteMethod **write_method)
+{
+  oid fulloid[MAX_OID_LEN];
+  int ret;
+
+  oid_copy (fulloid, v->name, v->namelen);
+  fulloid[v->namelen] = 0;
+  /* Check against full instance. */
+  ret = oid_compare (name, *length, fulloid, v->namelen + 1);
+
+  /* Check single instance. */
+  if ((exact && (ret != 0)) || (!exact && (ret >= 0)))
+	return MATCH_FAILED;
+
+  /* In case of getnext, fill in full instance. */
+  memcpy (name, fulloid, (v->namelen + 1) * sizeof (oid));
+  *length = v->namelen + 1;
+
+  *write_method = 0;
+  *var_len = sizeof(long);    /* default to 'long' results */
+
+  return MATCH_SUCCEEDED;
+}
+
+int
+smux_header_table (struct variable *v, oid *name, size_t *length, int exact,
+		   size_t *var_len, WriteMethod **write_method)
+{
+  /* If the requested OID name is less than OID prefix we
+     handle, adjust it to our prefix. */
+  if ((oid_compare (name, *length, v->name, v->namelen)) < 0)
+    {
+      if (exact)
+	return MATCH_FAILED;
+      oid_copy(name, v->name, v->namelen);
+      *length = v->namelen;
+    }
+
+  *write_method = 0;
+  *var_len = sizeof(long);
+
+  return MATCH_SUCCEEDED;
+}
+#endif /* HAVE_SNMP */
diff --git a/lib/thread.c b/lib/thread.c
index 86d0ff8..6341dfd 100644
--- a/lib/thread.c
+++ b/lib/thread.c
@@ -29,6 +29,16 @@
 #include "hash.h"
 #include "command.h"
 #include "sigevent.h"
+
+#if defined HAVE_SNMP && defined SNMP_AGENTX
+#include <net-snmp/net-snmp-config.h>
+#include <net-snmp/net-snmp-includes.h>
+#include <net-snmp/agent/net-snmp-agent-includes.h>
+#include <net-snmp/agent/snmp_vars.h>
+
+extern int agentx_enabled;
+#endif
+
 
 /* Recent absolute time of day */
 struct timeval recent_time;
@@ -1030,6 +1040,11 @@
   while (1)
     {
       int num = 0;
+#if defined HAVE_SNMP && defined SNMP_AGENTX
+      struct timeval snmp_timer_wait;
+      int snmpblock = 0;
+      int fdsetsize;
+#endif
       
       /* Signals pre-empt everything */
       quagga_sigevent_process ();
@@ -1065,6 +1080,26 @@
             timer_wait = timer_wait_bg;
         }
       
+#if defined HAVE_SNMP && defined SNMP_AGENTX
+      /* When SNMP is enabled, we may have to select() on additional
+	 FD. snmp_select_info() will add them to `readfd'. The trick
+	 with this function is its last argument. We need to set it to
+	 0 if timer_wait is not NULL and we need to use the provided
+	 new timer only if it is still set to 0. */
+      if (agentx_enabled)
+        {
+          fdsetsize = FD_SETSIZE;
+          snmpblock = 1;
+          if (timer_wait)
+            {
+              snmpblock = 0;
+              memcpy(&snmp_timer_wait, timer_wait, sizeof(struct timeval));
+            }
+          snmp_select_info(&fdsetsize, &readfd, &snmp_timer_wait, &snmpblock);
+          if (snmpblock == 0)
+            timer_wait = &snmp_timer_wait;
+        }
+#endif
       num = select (FD_SETSIZE, &readfd, &writefd, &exceptfd, timer_wait);
       
       /* Signals should get quick treatment */
@@ -1076,6 +1111,20 @@
             return NULL;
         }
 
+#if defined HAVE_SNMP && defined SNMP_AGENTX
+      if (agentx_enabled)
+        {
+          if (num > 0)
+            snmp_read(&readfd);
+          else if (num == 0)
+            {
+              snmp_timeout();
+              run_alarms();
+            }
+          netsnmp_check_outstanding_agent_requests();
+        }
+#endif
+
       /* Check foreground timers.  Historically, they have had higher
          priority than I/O threads, so let's push them onto the ready
 	 list in front of the I/O threads. */
diff --git a/ospf6d/Makefile.am b/ospf6d/Makefile.am
index 01bc6fe..726ce54 100644
--- a/ospf6d/Makefile.am
+++ b/ospf6d/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
diff --git a/ospf6d/ospf6_snmp.c b/ospf6d/ospf6_snmp.c
index 5ac7846..d252f54 100644
--- a/ospf6d/ospf6_snmp.c
+++ b/ospf6d/ospf6_snmp.c
@@ -23,14 +23,8 @@
 
 #ifdef HAVE_SNMP
 
-#ifdef HAVE_NETSNMP
 #include <net-snmp/net-snmp-config.h>
 #include <net-snmp/net-snmp-includes.h>
-#else
-#include <asn1.h>
-#include <snmp.h>
-#include <snmp_impl.h>
-#endif
 
 #include "log.h"
 #include "vty.h"
@@ -312,6 +306,10 @@
   if (ospf6 == NULL)
     return NULL;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   len = *length - v->namelen;
   len = (len >= sizeof (u_int32_t) ? sizeof (u_int32_t) : 0);
   if (exact && len != sizeof (u_int32_t))
@@ -378,6 +376,10 @@
   struct ospf6_area *oa;
   struct listnode *node;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   memset (&area_id, 0, sizeof (struct in_addr));
   type = 0;
   memset (&id, 0, sizeof (struct in_addr));
diff --git a/ospfclient/Makefile.am b/ospfclient/Makefile.am
index b8aae84..607bbed 100644
--- a/ospfclient/Makefile.am
+++ b/ospfclient/Makefile.am
@@ -1,6 +1,6 @@
 ## Automake.am for OSPF API client
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 
 lib_LTLIBRARIES = libospfapiclient.la
 libospfapiclient_la_LDFLAGS = -version 0:0:0
diff --git a/ospfd/Makefile.am b/ospfd/Makefile.am
index 2e4d5c8..f968d7d 100644
--- a/ospfd/Makefile.am
+++ b/ospfd/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ $(LOCAL_OPTS) -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
diff --git a/ospfd/ospf_snmp.c b/ospfd/ospf_snmp.c
index 9c9dd47..1daf0d6 100644
--- a/ospfd/ospf_snmp.c
+++ b/ospfd/ospf_snmp.c
@@ -25,14 +25,8 @@
 #include <zebra.h>
 
 #ifdef HAVE_SNMP
-#ifdef HAVE_NETSNMP
 #include <net-snmp/net-snmp-config.h>
 #include <net-snmp/net-snmp-includes.h>
-#else
-#include <asn1.h>
-#include <snmp.h>
-#include <snmp_impl.h>
-#endif
 
 #include "if.h"
 #include "log.h"
@@ -216,6 +210,7 @@
 
 /* OSPF-MIB instances. */
 oid ospf_oid [] = { OSPF2MIB };
+oid ospf_trap_oid [] = { OSPF2MIB, 16, 2 }; /* Not reverse mappable! */
 
 /* IP address 0.0.0.0. */
 static struct in_addr ospf_empty_addr = {0};
@@ -709,6 +704,10 @@
   struct ospf_area *area;
   struct in_addr addr;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   memset (&addr, 0, sizeof (struct in_addr));
 
   area = ospfAreaLookup (v, name, length, &addr, exact);
@@ -852,6 +851,10 @@
   struct ospf_area *area;
   struct in_addr addr;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   memset (&addr, 0, sizeof (struct in_addr));
 
   area = ospfStubAreaLookup (v, name, length, &addr, exact);
@@ -1083,6 +1086,10 @@
   struct in_addr router_id;
   struct ospf *ospf;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   /* INDEX { ospfLsdbAreaId, ospfLsdbType,
      ospfLsdbLsid, ospfLsdbRouterId } */
 
@@ -1245,6 +1252,10 @@
   struct in_addr mask;
   struct ospf *ospf;
   
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   /* Check OSPF instance. */
   ospf = ospf_lookup ();
   if (ospf == NULL)
@@ -1349,6 +1360,10 @@
   struct in_addr addr;
   struct ospf *ospf;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   /* Check OSPF instance. */
   ospf = ospf_lookup ();
   if (ospf == NULL)
@@ -1684,6 +1699,10 @@
   struct ospf_interface *oi;
   struct ospf *ospf;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   ifindex = 0;
   memset (&ifaddr, 0, sizeof (struct in_addr));
 
@@ -1852,6 +1871,10 @@
   struct ospf_interface *oi;
   struct ospf *ospf;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   ifindex = 0;
   memset (&ifaddr, 0, sizeof (struct in_addr));
 
@@ -2044,6 +2067,10 @@
   struct in_addr area_id;
   struct in_addr neighbor;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   memset (&area_id, 0, sizeof (struct in_addr));
   memset (&neighbor, 0, sizeof (struct in_addr));
 
@@ -2277,6 +2304,10 @@
   struct ospf_neighbor *nbr;
   struct ospf_interface *oi;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   memset (&nbr_addr, 0, sizeof (struct in_addr));
   ifindex = 0;
   
@@ -2339,6 +2370,10 @@
   struct in_addr neighbor;
   struct ospf *ospf;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   memset (&area_id, 0, sizeof (struct in_addr));
   memset (&neighbor, 0, sizeof (struct in_addr));
 
@@ -2487,6 +2522,10 @@
   struct in_addr router_id;
   struct ospf *ospf;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   type = OSPF_AS_EXTERNAL_LSA;
   memset (&ls_id, 0, sizeof (struct in_addr));
   memset (&router_id, 0, sizeof (struct in_addr));
@@ -2538,6 +2577,10 @@
 ospfAreaAggregateEntry (struct variable *v, oid *name, size_t *length,
 			int exact, size_t *var_len, WriteMethod **write_method)
 {
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   /* Return the current value of the variable */
   switch (v->magic) 
     {
@@ -2574,35 +2617,35 @@
 
 struct trap_object ospfNbrTrapList[] =
 {
-  {ospfGeneralGroup, -2, {1, OSPFROUTERID}},
-  {ospfNbrEntry, 3, {10, 1, OSPFNBRIPADDR}},
-  {ospfNbrEntry, 3, {10, 1, OSPFNBRRTRID}},
-  {ospfNbrEntry, 3, {10, 1, OSPFNBRSTATE}}
+  {-2, {1, OSPFROUTERID}},
+  {3, {10, 1, OSPFNBRIPADDR}},
+  {3, {10, 1, OSPFNBRRTRID}},
+  {3, {10, 1, OSPFNBRSTATE}}
 };
 
 
 struct trap_object ospfVirtNbrTrapList[] =
 {
-  {ospfGeneralGroup, -2, {1, 1}},
-  {ospfVirtNbrEntry, 3, {11, 1, OSPFVIRTNBRAREA}},
-  {ospfVirtNbrEntry, 3, {11, 1, OSPFVIRTNBRRTRID}},
-  {ospfVirtNbrEntry, 3, {11, 1, OSPFVIRTNBRSTATE}}
+  {-2, {1, 1}},
+  {3, {11, 1, OSPFVIRTNBRAREA}},
+  {3, {11, 1, OSPFVIRTNBRRTRID}},
+  {3, {11, 1, OSPFVIRTNBRSTATE}}
 };
 
 struct trap_object ospfIfTrapList[] =
 {
-  {ospfGeneralGroup, -2, {1, OSPFROUTERID}},
-  {ospfIfEntry, 3, {7, 1, OSPFIFIPADDRESS}},
-  {ospfIfEntry, 3, {7, 1, OSPFADDRESSLESSIF}},
-  {ospfIfEntry, 3, {7, 1, OSPFIFSTATE}}
+  {-2, {1, OSPFROUTERID}},
+  {3, {7, 1, OSPFIFIPADDRESS}},
+  {3, {7, 1, OSPFADDRESSLESSIF}},
+  {3, {7, 1, OSPFIFSTATE}}
 };
 
 struct trap_object ospfVirtIfTrapList[] =
 {
-  {ospfGeneralGroup, -2, {1, OSPFROUTERID}},
-  {ospfVirtIfEntry, 3, {9, 1, OSPFVIRTIFAREAID}},
-  {ospfVirtIfEntry, 3, {9, 1, OSPFVIRTIFNEIGHBOR}},
-  {ospfVirtIfEntry, 3, {9, 1, OSPFVIRTIFSTATE}}
+  {-2, {1, OSPFROUTERID}},
+  {3, {9, 1, OSPFVIRTIFAREAID}},
+  {3, {9, 1, OSPFVIRTIFNEIGHBOR}},
+  {3, {9, 1, OSPFVIRTIFSTATE}}
 };
 
 void
@@ -2618,11 +2661,13 @@
   oid_copy_addr (index, &(on->address.u.prefix4), IN_ADDR_SIZE);
   index[IN_ADDR_SIZE] = 0;
 
-  smux_trap (ospf_oid, sizeof ospf_oid / sizeof (oid),
+  smux_trap (ospf_variables, sizeof ospf_variables / sizeof (struct variable),
+	     ospf_trap_oid, sizeof ospf_trap_oid / sizeof (oid),
+	     ospf_oid, sizeof ospf_oid / sizeof (oid),
              index,  IN_ADDR_SIZE + 1,
              ospfNbrTrapList, 
              sizeof ospfNbrTrapList / sizeof (struct trap_object),
-             time (NULL), NBRSTATECHANGE);
+             NBRSTATECHANGE);
 }
 
 void
@@ -2635,11 +2680,13 @@
   oid_copy_addr (index, &(on->address.u.prefix4), IN_ADDR_SIZE);
   index[IN_ADDR_SIZE] = 0;
 
-  smux_trap (ospf_oid, sizeof ospf_oid / sizeof (oid),
+  smux_trap (ospf_variables, sizeof ospf_variables / sizeof (struct variable),
+	     ospf_trap_oid, sizeof ospf_trap_oid / sizeof (oid),
+	     ospf_oid, sizeof ospf_oid / sizeof (oid),
              index,  IN_ADDR_SIZE + 1,
              ospfVirtNbrTrapList, 
              sizeof ospfVirtNbrTrapList / sizeof (struct trap_object),
-             time (NULL), VIRTNBRSTATECHANGE);
+             VIRTNBRSTATECHANGE);
 }
 
 void
@@ -2654,11 +2701,13 @@
   oid_copy_addr (index, &(oi->address->u.prefix4), IN_ADDR_SIZE);
   index[IN_ADDR_SIZE] = 0;
 
-  smux_trap (ospf_oid, sizeof ospf_oid / sizeof (oid),
+  smux_trap (ospf_variables, sizeof ospf_variables / sizeof (struct variable),
+	     ospf_trap_oid, sizeof ospf_trap_oid / sizeof (oid),
+	     ospf_oid, sizeof ospf_oid / sizeof (oid),
              index, IN_ADDR_SIZE + 1,
              ospfIfTrapList, 
              sizeof ospfIfTrapList / sizeof (struct trap_object),
-             time (NULL), IFSTATECHANGE);
+             IFSTATECHANGE);
 }
 
 void
@@ -2671,11 +2720,13 @@
   oid_copy_addr (index, &(oi->address->u.prefix4), IN_ADDR_SIZE);
   index[IN_ADDR_SIZE] = 0;
 
-  smux_trap (ospf_oid, sizeof ospf_oid / sizeof (oid),
+  smux_trap (ospf_variables, sizeof ospf_variables / sizeof (struct variable),
+	     ospf_trap_oid, sizeof ospf_trap_oid / sizeof (oid),
+	     ospf_oid, sizeof ospf_oid / sizeof (oid),
              index, IN_ADDR_SIZE + 1,
              ospfVirtIfTrapList,
              sizeof ospfVirtIfTrapList / sizeof (struct trap_object),
-             time (NULL), VIRTIFSTATECHANGE);
+             VIRTIFSTATECHANGE);
 }
 /* Register OSPF2-MIB. */
 void
diff --git a/ripd/Makefile.am b/ripd/Makefile.am
index 2fa2665..b0bc7a8 100644
--- a/ripd/Makefile.am
+++ b/ripd/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
diff --git a/ripd/rip_snmp.c b/ripd/rip_snmp.c
index 61c47c7..090ebfa 100644
--- a/ripd/rip_snmp.c
+++ b/ripd/rip_snmp.c
@@ -22,14 +22,8 @@
 #include <zebra.h>
 
 #ifdef HAVE_SNMP
-#ifdef HAVE_NETSNMP
 #include <net-snmp/net-snmp-config.h>
 #include <net-snmp/net-snmp-includes.h>
-#else
-#include <asn1.h>
-#include <snmp.h>
-#include <snmp_impl.h>
-#endif
 
 #include "if.h"
 #include "log.h"
@@ -351,6 +345,10 @@
   static struct in_addr addr;
   static long valid = SNMP_VALID;
 
+  if (smux_header_table(v, name, length, exact, var_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   memset (&addr, 0, sizeof (struct in_addr));
   
   /* Lookup interface. */
@@ -454,6 +452,10 @@
   struct interface *ifp;
   struct rip_interface *ri;
 
+  if (smux_header_table(v, name, length, exact, val_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   memset (&addr, 0, sizeof (struct in_addr));
   
   /* Lookup interface. */
@@ -524,6 +526,10 @@
 
   struct rip_peer *peer;
 
+  if (smux_header_table(v, name, length, exact, val_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   memset (&addr, 0, sizeof (struct in_addr));
   
   /* Lookup interface. */
diff --git a/ripngd/Makefile.am b/ripngd/Makefile.am
index c6bd486..de5beba 100644
--- a/ripngd/Makefile.am
+++ b/ripngd/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 INSTALL_SDATA=@INSTALL@ -m 600
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2e98ff7..0c262a4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,4 +1,4 @@
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ $(LOCAL_OPTS) -DSYSCONFDIR=\"$(sysconfdir)/\"
 
 AM_CFLAGS = $(PICFLAGS)
diff --git a/vtysh/Makefile.am b/vtysh/Makefile.am
index 0fd2f14..7550173 100644
--- a/vtysh/Makefile.am
+++ b/vtysh/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with Automake to create Makefile.in
 
-INCLUDES = @INCLUDES@ -I$(top_srcdir) -I$(top_srcdir)/lib
+INCLUDES = @INCLUDES@ -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
 
 LIBS = @LIBS@ @CURSES@ @LIBPAM@
diff --git a/vtysh/extract.pl.in b/vtysh/extract.pl.in
index 61b2d2a..2dbaf0a 100755
--- a/vtysh/extract.pl.in
+++ b/vtysh/extract.pl.in
@@ -63,7 +63,7 @@
 foreach (@ARGV) {
     $file = $_;
 
-    open (FH, "cpp -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -DHAVE_IPV6 -I@top_builddir@ -I@srcdir@/ -I@srcdir@/.. -I@top_srcdir@/lib -I@top_srcdir@/isisd/topology @SNMP_INCLUDES@ @CPPFLAGS@ $file |");
+    open (FH, "cpp -DHAVE_CONFIG_H -DVTYSH_EXTRACT_PL -DHAVE_IPV6 -I@top_builddir@ -I@srcdir@/ -I@srcdir@/.. -I@top_srcdir@/lib -I@top_builddir@/lib -I@top_srcdir@/isisd/topology @CPPFLAGS@ $file |");
     local $/; undef $/;
     $line = <FH>;
     close (FH);
diff --git a/watchquagga/Makefile.am b/watchquagga/Makefile.am
index a49f62e..badaa5b 100644
--- a/watchquagga/Makefile.am
+++ b/watchquagga/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with Automake to create Makefile.in
 
-INCLUDES = @INCLUDES@ -I$(top_srcdir) -I$(top_srcdir)/lib
+INCLUDES = @INCLUDES@ -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSTATEDIR=\"$(localstatedir)/\"
 
 AM_CFLAGS = $(PICFLAGS)
diff --git a/zebra/Makefile.am b/zebra/Makefile.am
index 542f36f..9ac90f8 100644
--- a/zebra/Makefile.am
+++ b/zebra/Makefile.am
@@ -1,6 +1,6 @@
 ## Process this file with automake to produce Makefile.in.
 
-INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@
+INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib
 DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\" -DMULTIPATH_NUM=@MULTIPATH_NUM@
 INSTALL_SDATA=@INSTALL@ -m 600
 
diff --git a/zebra/zebra_snmp.c b/zebra/zebra_snmp.c
index 0fde4bb..f52bbcb 100644
--- a/zebra/zebra_snmp.c
+++ b/zebra/zebra_snmp.c
@@ -22,14 +22,8 @@
 #include <zebra.h>
 
 #ifdef HAVE_SNMP
-#ifdef HAVE_NETSNMP
 #include <net-snmp/net-snmp-config.h>
 #include <net-snmp/net-snmp-includes.h>
-#else
-#include <asn1.h>
-#include <snmp.h>
-#include <snmp_impl.h>
-#endif
 
 #include "if.h"
 #include "log.h"
@@ -457,6 +451,10 @@
   static struct in_addr netmask;
   struct nexthop *nexthop;
 
+  if (smux_header_table(v, objid, objid_len, exact, val_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   get_fwtable_route_node(v, objid, objid_len, exact, &np, &rib);
   if (!np)
     return NULL;
@@ -555,6 +553,10 @@
 ipCidrTable (struct variable *v, oid objid[], size_t *objid_len,
 	     int exact, size_t *val_len, WriteMethod **write_method)
 {
+  if (smux_header_table(v, objid, objid_len, exact, val_len, write_method)
+      == MATCH_FAILED)
+    return NULL;
+
   switch (v->magic)
     {
     case IPCIDRROUTEDEST: