title: C NET-SNMP Get and Set specifically via MIB Name, Not OID
body: <p>I have written and am testing software for a generic SNMP client module in C as well as an implementation using this generic module.  I am having trouble getting a get request to work by passing in a MIB name(e.g. sysDescr) instead of an OID(e.g. 1.3.6.1.2.1.1.1).</p>
  
  <p>I am successful when I pass in a character array containing the OID to _snmp_parse_oid()_ but not the name.</p>
  
  <p>I have checked the MIB file to make sure I am using the correct name. When I run the command line SNMP translate on the name it gives me the OID listed above:</p>
  
  <pre><code>$ snmptranslate -m +&lt;MIB File&gt; -IR -On &lt;MIB Name&gt;
  .#.#.#.#.#.#.#####.#.#.#.#.#.#
  </code></pre>
  
  <p><em>(In the above command I replaced my actual mib file with <code>&lt;MIB File&gt;</code>, mib name with <code>&lt;MIB Name&gt;</code>, and OID numbers returned from the command with <code>#</code> characters)</em></p>
  
  <p>The following is my code for my generic SNMP get function, assume returned values are #define numbers and I have removed some error handling for brevity:</p>
  
  <pre><code>/// @Synopsis        Function to send out get request since the 
  ///                  SNMPOidData object has been setup
  ///
  /// @Param oid_name  String containing the OID to set
  /// @Param value     Value to set
  ///
  /// @Returns         Error
  int snmpGet(SNMPAgent *this, char const * const oid_name, SNMPOidData * value)
  {
      netsnmp_pdu           *pdu;
      netsnmp_pdu           *response;
      netsnmp_variable_list *vars;
      oid                   *retrieved_oid;
      oidStruct             oid_to_get;
      int                   status   = 0;
      int                   result   = ERROR_SUCCESS;
  
      // Create the PDU for the data for our request
      pdu = snmp_pdu_create(SNMP_MSG_GET);
  
      oid_to_get.OidLen = MAX_OID_LEN; // Set max length
  
      // Send out the request(s)
      retrieved_oid = snmp_parse_oid(oid_name, oid_to_get.Oid, &amp;oid_to_get.OidLen);
  
      // Set the data
      snmp_add_null_var(pdu, oid_to_get.Oid, oid_to_get.OidLen))
  
      // Send the request out
      status = snmp_synch_response(this-&gt;port.snmp_session_handle, pdu, &amp;response);
      if (STAT_SUCCESS == status)
      {
          if (SNMP_ERR_NOERROR == response-&gt;errstat)
          {
              vars = response-&gt;variables;
              value-&gt;type = vars-&gt;type;
  
              if (vars-&gt;next_variable != NULL)
              {
                  // There are more values, set return type to null
                  value-&gt;type = ASN_NULL;
              }
              else if (!(CHECK_END(vars-&gt;type))) // Exception
              {
                  result = ERROR_NOT_PRESENT;
                  fprintf(stderr, "Warning: OID=%s gets snmp exception %d \n",
                      oid_name, vars-&gt;type);
              }
              else if ((vars-&gt;type == ASN_INTEGER)
                    || (vars-&gt;type == ASN_COUNTER)
                    || (vars-&gt;type == ASN_UNSIGNED))
              {
                  value-&gt;integer = *(vars-&gt;val.integer);
                  value-&gt;str_len = sizeof(value-&gt;integer);
              }
              else
              {
                  value-&gt;str_len = vars-&gt;val_len;
  
                  if (value-&gt;str_len &gt;= MAX_ASN_STR_LEN)
                      value-&gt;str_len = MAX_ASN_STR_LEN;
  
                  if (value-&gt;str_len &gt; 0)
                      memcpy(value-&gt;string, vars-&gt;val.string, value-&gt;str_len);
  
                  // guarantee NULL terminated string
                  value-&gt;string[value-&gt;str_len] = '\0';
              }
          }
      }
  
      this-&gt;freePDU(response); // Clean up: free the response
  
      return result;
  }
  </code></pre>
  
  <p>The error I am getting:</p>
  
  <pre><code>oid_name: Unknown Object Identifier (Sub-id not found: (top) -&gt; &lt;MIB Name&gt;)
  </code></pre>
  
  <p>Which comes from the following call:</p>
  
  <pre><code>retrieved_oid = snmp_parse_oid(oid_name, oid_to_get.Oid, &amp;oid_to_get.OidLen);
  </code></pre>
  
  <p>I have made sure that the MIB files are on the machine in the configured place (snmptranslate wouldn't work if this weren't the case).</p>
  
  <p>I have spent a good amount of time on Google results as well as directly searching here on Stack Overflow.
  The following is a good tutorial but does not address my issue (they directly reference the OID they want to get the value of):
  <a href="http://www.net-snmp.org/wiki/index.php/TUT:Simple_Application" rel="nofollow noreferrer">http://www.net-snmp.org/wiki/index.php/TUT:Simple_Application</a></p>
  
  <p>Any help or insight would be much appreciated.</p>
  
  <p>Some other info I can think of is that this is being compiled to run on an armv5tejl target running Linux communicating with an external device via ethernet.</p>
  
  <p>Thanks,</p>