From e6582246160542a078a13e35d74e6ea36a4cf6f0 Mon Sep 17 00:00:00 2001
From: Michal Wasiak <michal.wasiak@gmail.com>
Date: Thu, 8 Oct 2020 15:05:46 +0200
Subject: [PATCH] snmp/bridge_mib: update dot1dBase

Signed-off-by: Michal Wasiak <michal.wasiak@gmail.com>
---
 userspace/snmpd/bridge_mib/Makefile          |  1 +
 userspace/snmpd/bridge_mib/dot1dBase.c       | 37 ++++++++++++++------
 userspace/snmpd/bridge_mib/dot1dBase.h       |  2 ++
 userspace/snmpd/bridge_mib/init_bridge_mib.c |  2 ++
 4 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/userspace/snmpd/bridge_mib/Makefile b/userspace/snmpd/bridge_mib/Makefile
index 6b4ab7b44..9549d1c82 100644
--- a/userspace/snmpd/bridge_mib/Makefile
+++ b/userspace/snmpd/bridge_mib/Makefile
@@ -13,6 +13,7 @@ include $(BRIDGE_INCLUDE_DIRS)
 CFLAGS += -I$(BRIDGE_MIB_DIR)
 
 BRIDGE_MIB_SRCS += \
+	dot1dBase.c \
 	init_bridge_mib.c
 
 BRIDGE_MIB_SRCS:=$(addprefix $(BRIDGE_MIB_DIR)/,$(BRIDGE_MIB_SRCS))
diff --git a/userspace/snmpd/bridge_mib/dot1dBase.c b/userspace/snmpd/bridge_mib/dot1dBase.c
index 523c2aeb7..246c631e7 100644
--- a/userspace/snmpd/bridge_mib/dot1dBase.c
+++ b/userspace/snmpd/bridge_mib/dot1dBase.c
@@ -8,6 +8,8 @@
 #include <net-snmp/net-snmp-includes.h>
 #include <net-snmp/agent/net-snmp-agent-includes.h>
 #include "dot1dBase.h"
+#include "wrsSnmp.h"
+#include "snmp_shmem.h"
 
 /** Initializes the dot1dBase module */
 void
@@ -47,15 +49,25 @@ handle_dot1dBaseBridgeAddress(netsnmp_mib_handler *handler,
 
     /* a instance handler also only hands us one request at a time, so
        we don't need to loop over a list of requests; we'll only get one. */
-    
+
+    int i;
+
     switch(reqinfo->mode) {
 
         case MODE_GET:
-            snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
-                                     /* XXX: a pointer to the scalar's data */,
-                                     /* XXX: the length of the data in bytes */);
-            break;
-
+            /* look for a first port */
+            for (i = 0; i < hal_nports_local; i++) {
+                if (!strcmp(hal_ports[i].name, FIRST_PORT_NAME)) {
+                    /* First port found */
+                    snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
+                                     hal_ports[i].hw_addr,
+                                     ETH_ALEN);
+                    /* no need for more checks */
+                    return SNMP_ERR_NOERROR;
+                }
+            }
+            snmp_log(LOG_ERR, "Port name (%s) not found for handle_dot1dBaseBridgeAddress!\n", FIRST_PORT_NAME);
+            return SNMP_ERR_GENERR;
 
         default:
             /* we should never get here, so this is a really bad error */
@@ -76,13 +88,15 @@ handle_dot1dBaseNumPorts(netsnmp_mib_handler *handler,
 
     /* a instance handler also only hands us one request at a time, so
        we don't need to loop over a list of requests; we'll only get one. */
-    
+
+    /* Get the port number from HAL */
+    int port_num = hal_nports_local;
     switch(reqinfo->mode) {
 
         case MODE_GET:
             snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
-                                     /* XXX: a pointer to the scalar's data */,
-                                     /* XXX: the length of the data in bytes */);
+                                     &port_num,
+                                     sizeof(port_num));
             break;
 
 
@@ -106,12 +120,13 @@ handle_dot1dBaseType(netsnmp_mib_handler *handler,
     /* a instance handler also only hands us one request at a time, so
        we don't need to loop over a list of requests; we'll only get one. */
     
+    int baseType = BASETYPE_TRANSPARENT_ONLY;
     switch(reqinfo->mode) {
 
         case MODE_GET:
             snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
-                                     /* XXX: a pointer to the scalar's data */,
-                                     /* XXX: the length of the data in bytes */);
+                                     &baseType,
+                                     sizeof(baseType));
             break;
 
 
diff --git a/userspace/snmpd/bridge_mib/dot1dBase.h b/userspace/snmpd/bridge_mib/dot1dBase.h
index 8c4da0644..bedd96a12 100644
--- a/userspace/snmpd/bridge_mib/dot1dBase.h
+++ b/userspace/snmpd/bridge_mib/dot1dBase.h
@@ -11,6 +11,8 @@
 #define BASETYPE_SOURCEROUTE_ONLY   3
 #define BASETYPE_SRT                4
 
+#define FIRST_PORT_NAME "wri1"
+
 /* function declarations */
 void init_dot1dBase(void);
 Netsnmp_Node_Handler handle_dot1dBaseBridgeAddress;
diff --git a/userspace/snmpd/bridge_mib/init_bridge_mib.c b/userspace/snmpd/bridge_mib/init_bridge_mib.c
index 1b6f276a5..6a370ee94 100644
--- a/userspace/snmpd/bridge_mib/init_bridge_mib.c
+++ b/userspace/snmpd/bridge_mib/init_bridge_mib.c
@@ -8,11 +8,13 @@
 /* The sub-init functions */
 #include "wrsSnmp.h"
 #include "snmp_shmem.h"
+#include "dot1dBase.h"
 #include "dot1dTpFdbTable/dot1dTpFdbTable.h"
 #include "dot1qFdbTable/dot1qFdbTable.h"
 
 void init_bridge_mib(void)
 {
+    init_dot1dBase();
     init_dot1dTpFdbTable();
     init_dot1qFdbTable();
 }
-- 
GitLab