This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.
--
On Wed, 28 Nov 2001, James Rich wrote:

> On Wed, 28 Nov 2001, Brendan Bispham wrote:
>
> > I expected there would be more problems :) but if that's the only thing 
>left,
> > take a look at this page and tell me if you can see the same code there...
> > it's the same on all the web manuals, but it's possible there's a
> > browser-ccsid type thing that's cocking it up:
> >
> > 
>http://as400bks.rochester.ibm.com/pubs/html/as400/v5r1/ic2924/info/apis/snmpexmp.htm
>
> Sure enough, it is screwed up in the sample code.  I guess I'll go ahead
> and actually read the code now to see if I can figure out what is supposed
> to be going on here...

Alright, I think I have some code that should do what the sample code
intends.  Reading from the comments it looks like you are supposed to make
up some return values.  So I did.  I also cleaned it up a bit and took off
some comments that I thought hindered more than help.  It is attached.
For people on the mailing list if you want the attachment let me know and
I'll send it to you directly.

James Rich
james@eaerich.com

--
Content-Description: 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <qtomeapi.h>


#define UNKNOWN_PDU_TYPE -1
#define MAX_EXCEEDED -2
#define UNKNOWN_ASN_TYPE -3


typedef union
{
  int *int_val;
  char *str_val;
}
value;                          /* Value typedef.         */


int
AddVarbind (snmppdu ** pdu,
            char *oid,
            value v,
            unsigned char pdu_type,
            unsigned char asn_type);


int
main ()
{
  snmppdu *pdu;                 /* PDU pointer.           */
  value v;                      /* Value container.       */
  int rc;                       /* Return code.           */
  char community_name[120];     /* Community container.   */


  pdu = NULL;

  /* Add varbind with OID, value, type of PDU this is for, ASN */
  /* type.  PDU pointer is set to non-null. */
  rc = AddVarbind (&pdu,
                   "1.3.6.1.2.1.1.1.0",
                   v,
                   GET_PDU_TYPE,
                   0);
  if (rc < 0)
    {
      printf ("Error: %d\n", rc);
      exit (1);
    }

  /* Add second varbind.  PDU pointer is now non-null after 1st */
  /* invocation of AddVarbind. */
  rc = AddVarbind (&pdu,
                   "1.3.6.1.2.1.1.1.1",
                   v,
                   GET_PDU_TYPE,
                   0);
  if (rc < 0)
    {
      printf ("Error: %d\n", rc);
      exit (1);
    }

  /* Set community name. */
  strcpy (community_name, "public");

  rc = snmpGet (pdu,            /* Invoke operation.      */
                "system_name_of_snmp_agent_system",     /* Hostname.     */
                10,             /* Time-out value.        */
                community_name, /* Pointer to community name. */
                6);             /* Correct length of      */
}                               /* community name.        */





void
FreePdu (snmppdu * pdu)         /* Pass in pointer to PDU.              */
{
  varBind *vb, *t;              /* Define pointers to varbinds.         */

  vb = pdu->varbind;            /* Set first varbind pointer.           */
  while (vb != NULL)
    {                           /* Loop as long as varbinds exist.      */
      t = vb;                   /* Save current varbind pointer.        */
      vb = vb->next;            /* Pointer to next varbind.             */
      free (t->oid);            /* Free storage allocated for OID.      */
      free (t->val.str_val);    /* Free storage allocated for value.    */
      free (t);                 /* Free storage allocated for temporary 
varbind. */
    }
  free (pdu);                   /* Free storage allocated for PDU.      */
}


int
AddVarbind (snmppdu ** pdu,
            char *oid,
            value v,
            unsigned char pdu_type,
            unsigned char asn_type)
{
  varBind *t;                   /* Varbind pointer.       */
  int str_len, i;

  /* Check for a valid PDU type */
  switch (pdu_type)
    {
    case GET_PDU_TYPE:
    case SET_PDU_TYPE:
    case GETNEXT_PDU_TYPE:
      break;
    default:
      return (UNKNOWN_PDU_TYPE);
      break;
    }


  if (pdu[0] == NULL || pdu[0] == 0 || pdu[0] == '\0')
    {                           /* Check if PDU is null (meaning new PDU). */
      pdu[0] = (snmppdu *) malloc (sizeof (snmppdu));
      memset ((snmppdu *) pdu[0], 0, sizeof (pdu[0]));
      pdu[0]->pdu_type = pdu_type;
      pdu[0]->varbind = (varBind *) malloc (sizeof (varBind));
      str_len = strlen (oid);

      if (str_len > API_MAX_OID_SIZE)
        {
          return (MAX_EXCEEDED);
        }

      pdu[0]->varbind->oid = (char *) malloc (API_MAX_OID_SIZE + 1);
      strcpy (pdu[0]->varbind->oid, oid);       /* Copy the
                                                   OID. */
      pdu[0]->varbind->oid[str_len] = '\0';
      pdu[0]->varbind->next = NULL;     /* Nullify next
                                           pointer. */
      /* This signifies last varbind. */
      t = pdu[0]->varbind;      /* Set temporary pointer to
                                   varbind. */
      t->val.str_val = (char *) malloc (API_MAX_VALUE_SIZE + 1);

      /*********************************************************************/
      /* Note:  This sample code shows a malloc of the maximum value size  */
      /* plus 1 for null termination.  It would be in your best interest   */
      /* to allocate only the amount of the actual value, plus 1.  This    */
      /* reduces the amount of space allocated on each PDU.                */
      /*********************************************************************/
    }
  else
    {
      if (pdu[0]->pdu_type != pdu_type)
        {
          return (UNKNOWN_PDU_TYPE);
        }

      /* If this is not the initial call to      */
      /* add a varbind, then check to make       */
      /* sure the PDU type of this call          */
      /* matches the original.                   */
      t = pdu[0]->varbind;
      /* Store temporary pointer to this varbind. */
      i = 0;                    /* Initialize loop variable.               */

      while (t->next != NULL)   /* Loop until you locate last varbind. */
        {
          t = t->next;
          i++;
        }

      if (i > 100 /* MAX_NUM... */ )
        {
          return (MAX_EXCEEDED);
        }

      t->next = (varBind *) malloc (sizeof (varBind));
      t = t->next;              /* Set new temporary varbind pointer.      */
      str_len = strlen (oid);   /* Set length of OID.                  */

      if (str_len > API_MAX_OID_SIZE)
        {
          return (MAX_EXCEEDED);
        }

      t->oid = (char *) malloc (API_MAX_OID_SIZE + 1);
      strcpy (t->oid, oid);
      t->oid[str_len] = '\0';
      t->val.str_val = (char *) malloc (API_MAX_VALUE_SIZE + 1);
      t->val_len = API_MAX_VALUE_SIZE + 1;

      /**********************************************************************/
      /* Note:  This sample code shows a malloc of the maximum value size   */
      /* plus 1 for null termination.  It would be in your best interest    */
      /* to allocate only the amount of the actual value, plus 1.  This     */
      /* reduces the amount of space allocated on each PDU.                 */
      /**********************************************************************/
      t->next = NULL;
      /* Nullify next varbind pointer        */
    }                           /* signifying the last varbind.        */

  if (pdu_type == SET_PDU_TYPE) /* For sets only         */
    {
      t->asn_type = asn_type;   /* Save ASN type         */

      switch (asn_type)
        {
        case API_ASN_OCTET_STRING:
        case API_ASN_OBJECT_IDENTIFIER:
        case API_ASN_IpAddress:
        case API_ASN_Opaque:
          str_len = strlen (v.str_val);
          strcpy (t->val.str_val, v.str_val);
          t->val.str_val[str_len] = '\0';
          t->val_len = str_len;
          break;
        case API_ASN_INTEGER:
        case API_ASN_Counter:
        case API_ASN_Gauge:
        case API_ASN_TimeTicks:
          *t->val.int_val = *v.int_val;
          t->val_len = sizeof (int);
          break;
        default:
          return (UNKNOWN_ASN_TYPE);
        }
    }
  return (API_RC_OK);
}


This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available here. If you have questions about this, please contact [javascript protected email address].