⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _snmp.c

📁 This directory contains source code for tcpdump, a tool for network monitoring and data acquisition
💻 C
📖 第 1 页 / 共 4 页
字号:
         /* XXX */
         break;

         /* case SMI_BASETYPE_INTEGER64: SMIng */
         /* case SMI_BASETYPE_FLOAT32: SMIng */
         /* case SMI_BASETYPE_FLOAT64: SMIng */
         /* case SMI_BASETYPE_FLOAT128: SMIng */

    case SMI_BASETYPE_ENUM:
    case SMI_BASETYPE_BITS:
    case SMI_BASETYPE_UNKNOWN:
         ok = 1;
         break;
  }

  return ok;
}

static int smi_check_range (SmiType * smiType, struct be *elem)
{
  SmiRange *smiRange;
  int ok = 1;

  for (smiRange = smiGetFirstRange (smiType->module, smiType->name);
       smiRange;
       smiRange = smiGetNextRange (smiRange))
  {
    ok = smi_check_a_range (smiType, smiRange, elem);
    if (ok)
    {
      smiFreeRange (smiRange);
      break;
    }
  }

  if (ok && smiType->parentmodule && smiType->parentname)
  {
    SmiType *parentType = smiGetType (smiType->parentmodule, smiType->parentname);
    if (parentType)
    {
      ok = smi_check_range (parentType, elem);
      smiFreeType (parentType);
    }
  }
  return ok;
}

static SmiNode *smi_print_variable (struct be *elem)
{
  unsigned int oid[128], oidlen;
  SmiNode *smiNode = NULL;
  int i;

  smi_decode_oid (elem, oid, &oidlen);
  smiNode = smiGetNodeByOID (oidlen, oid);
  if (!smiNode)
  {
    asn1_print (elem);
    return NULL;
  }
  if (vflag)
  {
    PUTS (smiNode->module);
    PUTS ("::");
  }
  PUTS (smiNode->name);
  if (smiNode->oidlen < oidlen)
     for (i = smiNode->oidlen; i < oidlen; i++)
         PRINTF (".%u", oid[i]);
  return smiNode;
}

static void smi_print_value (SmiNode * smiNode, u_char pduid, struct be *elem)
{
  unsigned int oid[128], oidlen;
  SmiType *smiType;
  SmiNamedNumber *nn;
  int i, done = 0;

  if (!smiNode || !(smiNode->nodekind & (SMI_NODEKIND_SCALAR | SMI_NODEKIND_COLUMN)))
  {
    asn1_print (elem);
    return;
  }

  if (NOTIFY_CLASS (pduid) && smiNode->access < SMI_ACCESS_NOTIFY)
     PUTS ("[notNotifyable]");

  if (READ_CLASS (pduid) && smiNode->access < SMI_ACCESS_READ_ONLY)
     PUTS ("[notReadable]");

  if (WRITE_CLASS (pduid) && smiNode->access < SMI_ACCESS_READ_WRITE)
     PUTS ("[notWritable]");

  if (RESPONSE_CLASS (pduid) && smiNode->access == SMI_ACCESS_NOT_ACCESSIBLE)
     PUTS ("[noAccess]");

  if (!smi_check_type (smiNode->basetype, elem->type))
     PUTS ("[wrongType]");

  smiType = smiGetType (smiNode->typemodule, smiNode->typename);
  if (!smiType)
  {
    asn1_print (elem);
    return;
  }

  if (!smi_check_range (smiType, elem))
     PUTS ("[wrongLength]");

  /* resolve bits to named bits */

  /* check whether instance identifier is valid */

  /* apply display hints (integer, octetstring) */

  /* convert instance identifier to index type values */

  switch (elem->type)
  {
    case BE_OID:
         if (smiNode->basetype == SMI_BASETYPE_BITS &&
             smiNode->typemodule && smiNode->typename)
         {
           /* print bit labels */
         }
         else
         {
           smi_decode_oid (elem, oid, &oidlen);
           smiNode = smiGetNodeByOID (oidlen, oid);
           if (smiNode)
           {
             if (vflag)
             {
               PUTS (smiNode->module);
               PUTS ("::");
             }
             PUTS (smiNode->name);
             if (smiNode->oidlen < oidlen)
             {
               for (i = smiNode->oidlen; i < oidlen; i++)
                  PRINTF (".%u", oid[i]);
             }
             done++;
           }
         }
         break;

    case BE_INT:
         if (smiNode->basetype == SMI_BASETYPE_ENUM && smiNode->typemodule && smiNode->typename)
         {
           for (nn = smiGetFirstNamedNumber (smiNode->typemodule,
                                             smiNode->typename);
                nn; nn = smiGetNextNamedNumber (nn))
           {
             if (nn->value.value.integer32 == elem->data.integer)
             {
               PUTS (nn->name);
               PRINTF ("(%d)", elem->data.integer);
               done++;
               break;
             }
           }
         }
         break;
  }

  if (!done)
     asn1_print (elem);

  if (smiType)
     smiFreeType (smiType);
}
#endif

/*
 * General SNMP header
 *      SEQUENCE {
 *              version INTEGER {version-1(0)},
 *              community OCTET STRING,
 *              data ANY        -- PDUs
 *      }
 * PDUs for all but Trap: (see rfc1157 from page 15 on)
 *      SEQUENCE {
 *              request-id INTEGER,
 *              error-status INTEGER,
 *              error-index INTEGER,
 *              varbindlist SEQUENCE OF
 *                      SEQUENCE {
 *                              name ObjectName,
 *                              value ObjectValue
 *                      }
 *      }
 * PDU for Trap:
 *      SEQUENCE {
 *              enterprise OBJECT IDENTIFIER,
 *              agent-addr NetworkAddress,
 *              generic-trap INTEGER,
 *              specific-trap INTEGER,
 *              time-stamp TimeTicks,
 *              varbindlist SEQUENCE OF
 *                      SEQUENCE {
 *                              name ObjectName,
 *                              value ObjectValue
 *                      }
 *      }
 */

/*
 * Decode SNMP varBind
 */
static void varbind_print (u_char pduid, const u_char * np, u_int length)
{
  struct be elem;
  int count = 0, ind;

#ifdef LIBSMI
  SmiNode *smiNode = NULL;
#endif

  /* Sequence of varBind */
  if ((count = asn1_parse (np, length, &elem)) < 0)
     return;

  if (elem.type != BE_SEQ)
  {
    PUTS ("[!SEQ of varbind]");
    asn1_print (&elem);
    return;
  }
  if (count < length)
     PRINTF ("[%d extra after SEQ of varbind]", length - count);

  /* descend */
  length = elem.asnlen;
  np = (u_char *) elem.data.raw;

  for (ind = 1; length > 0; ind++)
  {
    const u_char *vbend;
    u_int vblength;

    PUTCHAR (' ');

    /* Sequence */
    if ((count = asn1_parse (np, length, &elem)) < 0)
       return;

    if (elem.type != BE_SEQ)
    {
      PUTS ("[!varbind]");
      asn1_print (&elem);
      return;
    }
    vbend = np + count;
    vblength = length - count;

    /* descend */
    length = elem.asnlen;
    np = (u_char *) elem.data.raw;

    /* objName (OID) */
    if ((count = asn1_parse (np, length, &elem)) < 0)
      return;
    if (elem.type != BE_OID)
    {
      PUTS ("[objName!=OID]");
      asn1_print (&elem);
      return;
    }
#ifdef LIBSMI
    smiNode = smi_print_variable (&elem);
#else
    asn1_print (&elem);
#endif

    length -= count;
    np += count;

    if (pduid != GETREQ && pduid != GETNEXTREQ && pduid != GETBULKREQ)
       PUTCHAR ('=');

    /* objVal (ANY) */
    if ((count = asn1_parse (np, length, &elem)) < 0)
       return;

    if (pduid == GETREQ || pduid == GETNEXTREQ || pduid == GETBULKREQ)
    {
      if (elem.type != BE_NULL)
      {
        PUTS ("[objVal!=NULL]");
        asn1_print (&elem);
      }
    }
    else
    {
      if (elem.type != BE_NULL)
      {
#ifdef LIBSMI
        smi_print_value (smiNode, pduid, &elem);
        smiFreeNode (smiNode);
#else
        asn1_print (&elem);
#endif
      }
    }
    length = vblength;
    np = vbend;
  }
}

/*
 * Decode SNMP PDUs: GetRequest, GetNextRequest, GetResponse, SetRequest,
 * GetBulk, Inform, V2Trap, and Report
 */
static void snmppdu_print (u_char pduid, const u_char * np, u_int length)
{
  struct be elem;
  int count = 0, error;

  /* reqId (Integer) */
  if ((count = asn1_parse (np, length, &elem)) < 0)
     return;

  if (elem.type != BE_INT)
  {
    PUTS ("[reqId!=INT]");
    asn1_print (&elem);
    return;
  }

  /* ignore the reqId */
  length -= count;
  np += count;

  /* errorStatus (Integer) */
  if ((count = asn1_parse (np, length, &elem)) < 0)
     return;

  if (elem.type != BE_INT)
  {
    PUTS ("[errorStatus!=INT]");
    asn1_print (&elem);
    return;
  }
  error = 0;
  if ((pduid == GETREQ || pduid == GETNEXTREQ || pduid == SETREQ ||
       pduid == INFORMREQ || pduid == V2TRAP || pduid == REPORT) &&
       elem.data.integer != 0)
  {
    char errbuf[10];

    PRINTF ("[errorStatus(%s)!=0]", DECODE_ErrorStatus (elem.data.integer));
  }
  else if (pduid == GETBULKREQ)
  {
    PRINTF (" N=%d", elem.data.integer);
  }
  else if (elem.data.integer != 0)
  {
    char errbuf[10];

    PRINTF (" %s", DECODE_ErrorStatus (elem.data.integer));
    error = elem.data.integer;
  }
  length -= count;
  np += count;

  /* errorIndex (Integer) */
  if ((count = asn1_parse (np, length, &elem)) < 0)
     return;

  if (elem.type != BE_INT)
  {
    PUTS ("[errorIndex!=INT]");
    asn1_print (&elem);
    return;
  }

  if ((pduid == GETREQ || pduid == GETNEXTREQ || pduid == SETREQ ||
       pduid == INFORMREQ || pduid == V2TRAP || pduid == REPORT) &&
       elem.data.integer != 0)
    PRINTF ("[errorIndex(%d)!=0]", elem.data.integer);

  else if (pduid == GETBULKREQ)
    PRINTF (" M=%d", elem.data.integer);

  else if (elem.data.integer != 0)
  {
    if (!error)
      PRINTF ("[errorIndex(%d) w/o errorStatus]", elem.data.integer);
    else
    {
      PRINTF ("@%d", elem.data.integer);
      error = elem.data.integer;
    }
  }

  else if (error)
  {
    PUTS ("[errorIndex==0]");
    error = 0;
  }
  length -= count;
  np += count;

  varbind_print (pduid, np, length);
  return;
}

/*
 * Decode SNMP Trap PDU
 */
static void trappdu_print (const u_char * np, u_int length)
{
  struct be elem;
  int count = 0, generic;

  PUTCHAR (' ');

  /* enterprise (oid) */
  if ((count = asn1_parse (np, length, &elem)) < 0)
     return;

  if (elem.type != BE_OID)
  {
    PUTS ("[enterprise!=OID]");
    asn1_print (&elem);
    return;
  }
  asn1_print (&elem);
  length -= count;
  np += count;

  PUTCHAR (' ');

  /* agent-addr (inetaddr) */
  if ((count = asn1_parse (np, length, &elem)) < 0)
     return;

  if (elem.type != BE_INETADDR)
  {
    PUTS ("[agent-addr!=INETADDR]");
    asn1_print (&elem);
    return;
  }
  asn1_print (&elem);
  length -= count;
  np += count;

  /* generic-trap (Integer) */
  if ((count = asn1_parse (np, length, &elem)) < 0)
    return;
  if (elem.type != BE_INT)
  {
    PUTS ("[generic-trap!=INT]");
    asn1_print (&elem);
    return;
  }
  generic = elem.data.integer;
  {
    char buf[10];
    PRINTF (" %s", DECODE_GenericTrap (generic));
  }
  length -= count;
  np += count;

  /* specific-trap (Integer) */
  if ((count = asn1_parse (np, length, &elem)) < 0)
     return;

  if (elem.type != BE_INT)
  {
    PUTS ("[specific-trap!=INT]");
    asn1_print (&elem);
    return;
  }
  if (generic != GT_ENTERPRISE)
  {
    if (elem.data.integer != 0)
      PRINTF ("[specific-trap(%d)!=0]", elem.data.integer);
  }
  else
    PRINTF (" s=%d", elem.data.integer);

  length -= count;
  np += count;

  PUTCHAR (' ');

  /* time-stamp (TimeTicks) */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -