2005-09-29 Alain Ritoux <alain.ritoux@6wind.com>

    * lib/smux.[ch]: allow to retreive global OID (identified by <0
      namelen).
    * ospf_ism.c: generate SNMP traps on Interface state change
    * ospf_nsm.c: generate SNMP traps on Neighbour state change
    * ospf_snmp.[ch]: support for SNMP traps for interface and neighbours.
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 2a8256f..fd3feff 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,8 @@
+2005-09-29 Alain Ritoux <alain.ritoux@6wind.com>
+
+	* smux.[ch]: allow to retreive global OID (identified by <0
+	  namelen).
+
 2005-09-29 Paul Jakma <paul.jakma@sun.com>
 
 	* zebra.h: Solaris capabilities requires priv.h to be included.
diff --git a/lib/smux.c b/lib/smux.c
index 28f402d..c9b7a88 100644
--- a/lib/smux.c
+++ b/lib/smux.c
@@ -1065,13 +1065,34 @@
       u_char val_type;
 
       /* Make OID. */
-      oid_copy (oid, name, namelen);
-      oid_copy (oid + namelen, trapobj[i].name, trapobj[i].namelen);
-      oid_copy (oid + namelen + trapobj[i].namelen, iname, inamelen);
-      oid_len = namelen + trapobj[i].namelen + inamelen;
+      if (trapobj[i].namelen > 0) 
+        {
+          oid_copy (oid, name, namelen);
+          oid_copy (oid + namelen, trapobj[i].name, trapobj[i].namelen);
+          oid_copy (oid + namelen + trapobj[i].namelen, iname, inamelen);
+          oid_len = namelen + trapobj[i].namelen + inamelen;
+        }
+      else 
+        {
+          oid_copy (oid, name, namelen);
+          oid_copy (oid + namelen, trapobj[i].name, trapobj[i].namelen * (-1));
+          oid_len = namelen + trapobj[i].namelen * (-1) ;
+        }
 
-      if (debug_smux)
-	smux_oid_dump ("Trap", oid, oid_len);
+      if (debug_smux) 
+        {
+          smux_oid_dump ("Trap", name, namelen);
+          if (trapobj[i].namelen < 0)
+            smux_oid_dump ("Trap", 
+                           trapobj[i].name, (- 1) * (trapobj[i].namelen));
+          else 
+            {
+              smux_oid_dump ("Trap", trapobj[i].name, (trapobj[i].namelen));
+              smux_oid_dump ("Trap", iname, inamelen);
+            }
+          smux_oid_dump ("Trap", oid, oid_len);
+          zlog_info ("BUFSIZ: %d // oid_len: %d", BUFSIZ, oid_len);
+      }
 
       ret = smux_get (oid, &oid_len, 1, &val_type, &val, &val_len);
 
diff --git a/lib/smux.h b/lib/smux.h
index a4f1718..79d23e7 100644
--- a/lib/smux.h
+++ b/lib/smux.h
@@ -94,7 +94,7 @@
   FindVarMethod *findVar;
 
   /* Suffix of the MIB. */
-  u_char namelen;
+  int namelen;
   oid name[MAX_OID_LEN];
 };
 
@@ -121,7 +121,7 @@
 struct trap_object
 {
   FindVarMethod *findVar;
-  u_char namelen;
+  int namelen;
   oid name[MAX_OID_LEN];
 };
 
diff --git a/ospfd/ChangeLog b/ospfd/ChangeLog
index 2cc9a4f..be1e0f9 100644
--- a/ospfd/ChangeLog
+++ b/ospfd/ChangeLog
@@ -1,5 +1,11 @@
 2005-09-29 Alain Ritoux <alain.ritoux@6wind.com>
 
+	* ospf_ism.c: generate SNMP traps on Interface state change
+	* ospf_nsm.c: generate SNMP traps on Neighbour state change
+	* ospf_snmp.[ch]: support for SNMP traps for interface and neighbours.
+
+2005-09-29 Alain Ritoux <alain.ritoux@6wind.com>
+
 	* ospf_vty.c: forece default route LSA to be re_issued whenever
 	  cost is changed ( [no] ip ospf area XXX default-cost YYY)
 	  Support ignore-mtu option
diff --git a/ospfd/ospf_ism.c b/ospfd/ospf_ism.c
index b251e59..ab91e8d 100644
--- a/ospfd/ospf_ism.c
+++ b/ospfd/ospf_ism.c
@@ -43,6 +43,7 @@
 #include "ospfd/ospf_packet.h"
 #include "ospfd/ospf_flood.h"
 #include "ospfd/ospf_abr.h"
+#include "ospfd/ospf_snmp.h"
 
 /* elect DR and BDR. Refer to RFC2319 section 9.4 */
 static struct ospf_neighbor *
@@ -552,6 +553,20 @@
   oi->state = state;
   oi->state_change++;
 
+#ifdef HAVE_SNMP
+  /* Terminal state or regression */ 
+  if ((state == ISM_DR) || (state == ISM_Backup) || (state == ISM_DROther) ||
+      (state == ISM_PointToPoint) || (state < old_state))
+    {
+      /* ospfVirtIfStateChange */
+      if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
+        ospfTrapVirtIfStateChange (oi);
+      /* ospfIfStateChange */
+      else
+        ospfTrapIfStateChange (oi);
+    }
+#endif
+
   /* Set multicast memberships appropriately for new state. */
   ospf_if_set_multicast(oi);
 
diff --git a/ospfd/ospf_nsm.c b/ospfd/ospf_nsm.c
index 2af4fc1..1d10084 100644
--- a/ospfd/ospf_nsm.c
+++ b/ospfd/ospf_nsm.c
@@ -47,6 +47,7 @@
 #include "ospfd/ospf_dump.h"
 #include "ospfd/ospf_flood.h"
 #include "ospfd/ospf_abr.h"
+#include "ospfd/ospf_snmp.h"
 
 void nsm_reset_nbr (struct ospf_neighbor *);
 
@@ -692,6 +693,22 @@
   if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
     vl_area = ospf_area_lookup_by_area_id (oi->ospf, oi->vl_data->vl_area_id);
   
+#ifdef HAVE_SNMP
+  /* Terminal state or regression */ 
+  if ((state == NSM_Full) || (state == NSM_TwoWay) || (state < old_state))
+    {
+      /* ospfVirtNbrStateChange */
+      if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
+        ospfTrapVirtNbrStateChange(nbr);
+      /* ospfNbrStateChange trap  */
+      else
+        /* To/From FULL, only managed by DR */
+        if (((state != NSM_Full) && (old_state != NSM_Full)) ||
+            (oi->state == ISM_DR))
+          ospfTrapNbrStateChange(nbr);
+    }
+#endif
+
   /* One of the neighboring routers changes to/from the FULL state. */
   if ((old_state != NSM_Full && state == NSM_Full) ||
       (old_state == NSM_Full && state != NSM_Full))
diff --git a/ospfd/ospf_snmp.c b/ospfd/ospf_snmp.c
index bceb962..05e4de8 100644
--- a/ospfd/ospf_snmp.c
+++ b/ospfd/ospf_snmp.c
@@ -2064,7 +2064,9 @@
       for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
 	if ((nbr = rn->info) != NULL
 	    && nbr != oi->nbr_self
+/* If EXACT match is needed, provide ALL entry found
 	    && nbr->state != NSM_Down
+ */
 	    && nbr->src.s_addr != 0)
 	  {
 	    if (IPV4_ADDR_SAME (&nbr->src, nbr_addr))
@@ -2475,6 +2477,112 @@
   return NULL;
 }
 
+/* OSPF Traps. */
+#define IFSTATECHANGE      16
+#define VIRTIFSTATECHANGE   1
+#define NBRSTATECHANGE      2
+#define VIRTNBRSTATECHANGE  3
+
+struct trap_object ospfNbrTrapList[] =
+{
+  {ospfGeneralGroup, -2, {1, OSPFROUTERID}},
+  {ospfNbrEntry, 3, {10, 1, OSPFNBRIPADDR}},
+  {ospfNbrEntry, 3, {10, 1, OSPFNBRRTRID}},
+  {ospfNbrEntry, 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}}
+};
+
+struct trap_object ospfIfTrapList[] =
+{
+  {ospfGeneralGroup, -2, {1, OSPFROUTERID}},
+  {ospfIfEntry, 3, {7, 1, OSPFIFIPADDRESS}},
+  {ospfIfEntry, 3, {7, 1, OSPFADDRESSLESSIF}},
+  {ospfIfEntry, 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}}
+};
+
+void
+ospfTrapNbrStateChange (struct ospf_neighbor *on)
+{
+  oid index[sizeof (oid) * (IN_ADDR_SIZE + 1)];
+  
+  zlog (NULL, LOG_INFO, "ospfTrapNbrStateChange trap sent");
+
+  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),
+             index,  IN_ADDR_SIZE + 1,
+             ospfNbrTrapList, 
+             sizeof ospfNbrTrapList / sizeof (struct trap_object),
+             time (NULL), NBRSTATECHANGE);
+}
+
+void
+ospfTrapVirtNbrStateChange (struct ospf_neighbor *on)
+{
+  oid index[sizeof (oid) * (IN_ADDR_SIZE + 1)];
+  
+  zlog (NULL, LOG_INFO, "ospfTrapVirtNbrStateChange trap sent");
+
+  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),
+             index,  IN_ADDR_SIZE + 1,
+             ospfVirtNbrTrapList, 
+             sizeof ospfVirtNbrTrapList / sizeof (struct trap_object),
+             time (NULL), VIRTNBRSTATECHANGE);
+}
+
+void
+ospfTrapIfStateChange (struct ospf_interface *oi)
+{
+  oid index[sizeof (oid) * (IN_ADDR_SIZE + 1)];
+
+  zlog (NULL, LOG_INFO, "ospfTrapIfStateChange trap sent");
+  
+  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),
+             index, IN_ADDR_SIZE + 1,
+             ospfIfTrapList, 
+             sizeof ospfIfTrapList / sizeof (struct trap_object),
+             time (NULL), IFSTATECHANGE);
+}
+
+void
+ospfTrapVirtIfStateChange (struct ospf_interface *oi)
+{
+  oid index[sizeof (oid) * (IN_ADDR_SIZE + 1)];
+
+  zlog (NULL, LOG_INFO, "ospfTrapVirtIfStateChange trap sent");
+  
+  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),
+             index, IN_ADDR_SIZE + 1,
+             ospfVirtIfTrapList,
+             sizeof ospfVirtIfTrapList / sizeof (struct trap_object),
+             time (NULL), VIRTIFSTATECHANGE);
+}
 /* Register OSPF2-MIB. */
 void
 ospf_snmp_init ()
diff --git a/ospfd/ospf_snmp.h b/ospfd/ospf_snmp.h
index 8b561b0..413d1d7 100644
--- a/ospfd/ospf_snmp.h
+++ b/ospfd/ospf_snmp.h
@@ -30,4 +30,9 @@
 extern void ospf_snmp_vl_add (struct ospf_vl_data *);
 extern void ospf_snmp_vl_delete (struct ospf_vl_data *);
 
+extern void ospfTrapIfStateChange (struct ospf_interface *);
+extern void ospfTrapVirtIfStateChange (struct ospf_interface *);
+extern void ospfTrapNbrStateChange (struct ospf_neighbor *);
+extern void ospfTrapVirtNbrStateChange (struct ospf_neighbor *);
+
 #endif /* _ZEBRA_OSPF_SNMP_H */