Add write support for ipForwarding and ipDefaultTTL objects

Tushar Gohad <[email protected]>
Newsgroups gmane.network.net-snmp.devel,gmane.network.net-snmp.user
Message-ID <[email protected]>
Hello folks,

The attached patch adds write support for the ipForwarding and
ipDefaultTTL objects to net-snmp. The patch should be applied on top of 
the latest net-snmp SVN tree.

Any feedback is welcome.

Thanks,
- Tushar Gohad

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/

_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders
(unnamed) (text/x-patch, 10.7 KB)
Index: net-snmp/README.agent-mibs
===================================================================
--- net-snmp.orig/README.agent-mibs
+++ net-snmp/README.agent-mibs
@@ -182,6 +182,9 @@ IP-MIB
  ipv6RouterAdvertSpinLock.0   ---
  ipv6ScopeZoneIndexTable      ---
 
+ ipForwarding.0               L          5.4    S ip-mib/ip_scalars.c
+ ipDefaultTTL.0               L          5.4    S ip-mib/ip_scalars.c
+
 ------------------------------------------------------------------------------
 IPV6-MIB
  ipv6MIBObjects.?.0           U         4.1     O mibII/ipv6.c
Index: net-snmp/agent/mibgroup/ip-mib/data_access/scalars_linux.c
===================================================================
--- net-snmp.orig/agent/mibgroup/ip-mib/data_access/scalars_linux.c
+++ net-snmp/agent/mibgroup/ip-mib/data_access/scalars_linux.c
@@ -8,10 +8,11 @@
 
 #include <net-snmp/data_access/ip_scalars.h>
 
-const char *ipfw_name = "/proc/sys/net/ipv6/conf/all/forwarding";
+const char *ipfw_name = "/proc/sys/net/ipv4/conf/all/forwarding";
+const char *ipfw6_name = "/proc/sys/net/ipv6/conf/all/forwarding";
 
 int
-netsnmp_arch_ip_scalars_ipv6IpForwarding_get(u_long *value)
+netsnmp_arch_ip_scalars_ipForwarding_get(u_long *value)
 {
     FILE *filep;
     int rc;
@@ -22,7 +23,7 @@ netsnmp_arch_ip_scalars_ipv6IpForwarding
 
     filep = fopen(ipfw_name, "r");
     if (NULL == filep) {
-        DEBUGMSGTL(("access:ipv6IpForwarding", "could not open %s\n",
+        DEBUGMSGTL(("access:ipForwarding", "could not open %s\n",
                     ipfw_name));
         return -2;
     }
@@ -30,13 +31,13 @@ netsnmp_arch_ip_scalars_ipv6IpForwarding
     rc = fscanf(filep, "%ld", value);
     fclose(filep);
     if (1 != rc) {
-        DEBUGMSGTL(("access:ipv6IpForwarding", "could not read %s\n",
+        DEBUGMSGTL(("access:ipForwarding", "could not read %s\n",
                     ipfw_name));
         return -3;
     }
 
     if ((0 != *value) && (1 != *value)) {
-        DEBUGMSGTL(("access:ipv6IpForwarding", "unexpected value %ld in %s\n",
+        DEBUGMSGTL(("access:ipForwarding", "unexpected value %ld in %s\n",
                     *value, ipfw_name));
         return -4;
     }
@@ -45,6 +46,74 @@ netsnmp_arch_ip_scalars_ipv6IpForwarding
 }
 
 int
+netsnmp_arch_ip_scalars_ipForwarding_set(u_long value)
+{
+    FILE *filep;
+    int rc;
+
+    if (1 == value)
+        ;
+    else if (2 == value)
+        value = 0;
+    else {
+        DEBUGMSGTL(("access:ipForwarding", "bad value %ld for %s\n",
+                    value));
+        return SNMP_ERR_WRONGVALUE;
+    }
+
+    filep = fopen(ipfw_name, "w");
+    if (NULL == filep) {
+        DEBUGMSGTL(("access:ipForwarding", "could not open %s\n",
+                    ipfw_name));
+        return SNMP_ERR_RESOURCEUNAVAILABLE;
+    }
+
+    rc = fprintf(filep, "%ld", value);
+    fclose(filep);
+    if (1 != rc) {
+        DEBUGMSGTL(("access:ipForwarding", "could not write %s\n",
+                    ipfw_name));
+        return SNMP_ERR_GENERR;
+    }
+
+    return 0;
+}
+
+int
+netsnmp_arch_ip_scalars_ipv6IpForwarding_get(u_long *value)
+{
+    FILE *filep;
+    int rc;
+
+    if (NULL == value)
+        return -1;
+
+
+    filep = fopen(ipfw6_name, "r");
+    if (NULL == filep) {
+        DEBUGMSGTL(("access:ipv6IpForwarding", "could not open %s\n",
+                    ipfw6_name));
+        return -2;
+    }
+
+    rc = fscanf(filep, "%ld", value);
+    fclose(filep);
+    if (1 != rc) {
+        DEBUGMSGTL(("access:ipv6IpForwarding", "could not read %s\n",
+                    ipfw6_name));
+        return -3;
+    }
+
+    if ((0 != *value) && (1 != *value)) {
+        DEBUGMSGTL(("access:ipv6IpForwarding", "unexpected value %ld in %s\n",
+                    *value, ipfw6_name));
+        return -4;
+    }
+
+    return 0;
+}
+
+int
 netsnmp_arch_ip_scalars_ipv6IpForwarding_set(u_long value)
 {
     FILE *filep;
@@ -60,10 +129,10 @@ netsnmp_arch_ip_scalars_ipv6IpForwarding
         return SNMP_ERR_WRONGVALUE;
     }
 
-    filep = fopen(ipfw_name, "w");
+    filep = fopen(ipfw6_name, "w");
     if (NULL == filep) {
         DEBUGMSGTL(("access:ipv6IpForwarding", "could not open %s\n",
-                    ipfw_name));
+                    ipfw6_name));
         return SNMP_ERR_RESOURCEUNAVAILABLE;
     }
 
@@ -71,7 +140,7 @@ netsnmp_arch_ip_scalars_ipv6IpForwarding
     fclose(filep);
     if (1 != rc) {
         DEBUGMSGTL(("access:ipv6IpForwarding", "could not write %s\n",
-                    ipfw_name));
+                    ipfw6_name));
         return SNMP_ERR_GENERR;
     }
 
Index: net-snmp/agent/mibgroup/ip-mib/ip_scalars.c
===================================================================
--- net-snmp.orig/agent/mibgroup/ip-mib/ip_scalars.c
+++ net-snmp/agent/mibgroup/ip-mib/ip_scalars.c
@@ -12,6 +12,18 @@
 #include "ip_scalars.h"
 
 int
+handle_ipForwarding(netsnmp_mib_handler *handler,
+                    netsnmp_handler_registration *reginfo,
+                    netsnmp_agent_request_info *reqinfo,
+                    netsnmp_request_info *requests);
+
+int
+handle_ipDefaultTTL(netsnmp_mib_handler *handler,
+                          netsnmp_handler_registration *reginfo,
+                          netsnmp_agent_request_info *reqinfo,
+                          netsnmp_request_info *requests);
+
+int
 handle_ipv6IpForwarding(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *reqinfo,
@@ -21,6 +33,8 @@ handle_ipv6IpForwarding(netsnmp_mib_hand
 void
 init_ip_scalars(void)
 {
+    static oid 	    ipForwarding_oid[] = { 1, 3, 6, 1, 2, 1, 4, 1 };
+    static oid 	    ipDefaultTTL_oid[] = { 1, 3, 6, 1, 2, 1, 4, 2, 0 };
     static oid      ipReasmTimeout_oid[] = { 1, 3, 6, 1, 2, 1, 4, 13, 0 };
     static oid      ipv6IpForwarding_oid[] = { 1, 3, 6, 1, 2, 1, 4, 25 };
     static oid      ipv6IpDefaultHopLimit_oid[] =
@@ -35,6 +49,12 @@ init_ip_scalars(void)
          HANDLER_CAN_RONLY, NULL, NULL);
                                        
     netsnmp_register_scalar(netsnmp_create_handler_registration
+                             ("ipForwarding", handle_ipForwarding,
+                              ipForwarding_oid,
+                              OID_LENGTH(ipForwarding_oid),
+                              HANDLER_CAN_RWRITE));
+
+    netsnmp_register_scalar(netsnmp_create_handler_registration
                             ("ipv6IpForwarding", handle_ipv6IpForwarding,
                              ipv6IpForwarding_oid,
                              OID_LENGTH(ipv6IpForwarding_oid),
@@ -45,10 +65,116 @@ init_ip_scalars(void)
          ipv6IpDefaultHopLimit_oid, OID_LENGTH(ipv6IpDefaultHopLimit_oid),
          "/proc/sys/net/ipv6/conf/default/hop_limit", ASN_INTEGER,
          HANDLER_CAN_RWRITE, NULL, NULL);
+
+     netsnmp_register_num_file_instance
+        ("ipDefaultTTL",
+         ipDefaultTTL_oid, OID_LENGTH(ipDefaultTTL_oid),
+         "/proc/sys/net/ipv4/ip_default_ttl", ASN_INTEGER,
+         HANDLER_CAN_RWRITE, NULL, NULL);
                                        
 }
 
 int
+handle_ipForwarding(netsnmp_mib_handler *handler,
+                          netsnmp_handler_registration *reginfo,
+                          netsnmp_agent_request_info   *reqinfo,
+                          netsnmp_request_info         *requests)
+{
+    int      rc;
+    u_long   value;
+
+    int ret;
+    /* We are never called for a GETNEXT if it's registered as a
+       "instance", as it's "magically" handled for us.  */
+
+    /* 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. */
+
+    switch(reqinfo->mode) {
+
+        case MODE_GET:
+            rc = netsnmp_arch_ip_scalars_ipForwarding_get(&value);
+            if (rc != 0) {
+                netsnmp_set_request_error(reqinfo, requests,
+                                      SNMP_NOSUCHINSTANCE);
+            }
+            else {
+                value = value ? 1 : 2;
+                snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
+                                     (u_char *)&value, sizeof(value));
+            }
+            break;
+
+        /*
+         * SET REQUEST
+         *
+         * multiple states in the transaction.  See:
+         * http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg
+         */
+        case MODE_SET_RESERVE1:
+            break;
+
+        case MODE_SET_RESERVE2:
+            /*
+             * store old info for undo later
+             */
+            rc = netsnmp_arch_ip_scalars_ipForwarding_get(&value);
+            if (rc < 0) {
+                netsnmp_set_request_error(reqinfo, requests,
+                                          SNMP_ERR_NOCREATION);
+            } else {
+                u_long *value_save;
+                memdup((u_char **) & value_save, (u_char *) &value,
+                       sizeof(value));
+                if ( NULL == value_save )
+                    netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_RESOURCEUNAVAILABLE);
+                else
+                    netsnmp_request_add_list_data(requests,
+                                                  netsnmp_create_data_list
+                                                  ("ipfw", value_save,
+                                                  free));
+	    }
+            break;
+
+        case MODE_SET_FREE:
+            /* XXX: free resources allocated in RESERVE1 and/or
+               RESERVE2.  Something failed somewhere, and the states
+               below won't be called. */
+            break;
+
+        case MODE_SET_ACTION:
+            /* XXX: perform the value change here */
+            value =  *(requests->requestvb->val.integer);
+            rc = netsnmp_arch_ip_scalars_ipForwarding_set(value);
+            if ( 0 != rc ) {
+                netsnmp_set_request_error(reqinfo, requests, rc);
+            }
+            break;
+
+        case MODE_SET_COMMIT:
+            break;
+
+        case MODE_SET_UNDO:
+             value =
+                 *((u_long *) netsnmp_request_get_list_data(requests,
+                                                            "ipfw"));
+             rc = netsnmp_arch_ip_scalars_ipForwarding_set(value);
+             if ( 0 != rc ) {
+                 netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_UNDOFAILED);
+             }
+             break;
+
+        default:
+            /* we should never get here, so this is a really bad error */
+            snmp_log(LOG_ERR, "unknown mode (%d) in handle_ipForwarding\n", reqinfo->mode );
+            return SNMP_ERR_GENERR;
+    }
+
+    return SNMP_ERR_NOERROR;
+}
+
+
+int
 handle_ipv6IpForwarding(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *reqinfo,
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.