示例#1
0
int					/* O - Number of OIDs found or -1 on error */
_cupsSNMPWalk(int            fd,	/* I - SNMP socket */
              http_addr_t    *address,	/* I - Address to query */
	      int            version,	/* I - SNMP version */
	      const char     *community,/* I - Community name */
              const int      *prefix,	/* I - OID prefix */
	      double         timeout,	/* I - Timeout for each response in seconds */
	      cups_snmp_cb_t cb,	/* I - Function to call for each response */
	      void           *data)	/* I - User data pointer that is passed to the callback function */
{
  int		count = 0;		/* Number of OIDs found */
  int		request_id = 0;		/* Current request ID */
  cups_snmp_t	packet;			/* Current response packet */
  int		lastoid[CUPS_SNMP_MAX_OID];
					/* Last OID we got */


 /*
  * Range check input...
  */

  DEBUG_printf(("4_cupsSNMPWalk(fd=%d, address=%p, version=%d, "
                "community=\"%s\", prefix=%p, timeout=%.1f, cb=%p, data=%p)",
		fd, address, version, community, prefix, timeout, cb, data));

  if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community ||
      !prefix || !cb)
  {
    DEBUG_puts("5_cupsSNMPWalk: Returning -1");

    return (-1);
  }

 /*
  * Copy the OID prefix and then loop until we have no more OIDs...
  */

  _cupsSNMPCopyOID(packet.object_name, prefix, CUPS_SNMP_MAX_OID);
  lastoid[0] = -1;

  for (;;)
  {
    request_id ++;

    if (!_cupsSNMPWrite(fd, address, version, community,
                        CUPS_ASN1_GET_NEXT_REQUEST, request_id,
		        packet.object_name))
    {
      DEBUG_puts("5_cupsSNMPWalk: Returning -1");

      return (-1);
    }

    if (!_cupsSNMPRead(fd, &packet, timeout))
    {
      DEBUG_puts("5_cupsSNMPWalk: Returning -1");

      return (-1);
    }

    if (!_cupsSNMPIsOIDPrefixed(&packet, prefix) ||
        _cupsSNMPIsOID(&packet, lastoid))
    {
      DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count));

      return (count);
    }

    if (packet.error || packet.error_status)
    {
      DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count > 0 ? count : -1));

      return (count > 0 ? count : -1);
    }

    _cupsSNMPCopyOID(lastoid, packet.object_name, CUPS_SNMP_MAX_OID);

    count ++;

    (*cb)(&packet, data);
  }
}
示例#2
0
文件: testsnmp.c 项目: zdohnal/cups
static int				/* O - 1 on success, 0 on error */
show_oid(int         fd,		/* I - SNMP socket */
         const char  *community,	/* I - Community name */
	 http_addr_t *addr,		/* I - Address to query */
         const char  *s,		/* I - OID to query */
	 int         walk)		/* I - Walk OIDs? */
{
  int		i;			/* Looping var */
  int		oid[CUPS_SNMP_MAX_OID];	/* OID */
  cups_snmp_t	packet;			/* SNMP packet */
  char		temp[1024];		/* Temporary OID string */


  if (!_cupsSNMPStringToOID(s, oid, sizeof(oid) / sizeof(oid[0])))
  {
    puts("testsnmp: Bad OID");
    return (0);
  }

  if (walk)
  {
    printf("_cupsSNMPWalk(%s): ", _cupsSNMPOIDToString(oid, temp, sizeof(temp)));

    if (_cupsSNMPWalk(fd, addr, CUPS_SNMP_VERSION_1, community, oid, 5.0,
                     print_packet, NULL) < 0)
    {
      printf("FAIL (%s)\n", strerror(errno));
      return (0);
    }
  }
  else
  {
    printf("_cupsSNMPWrite(%s): ", _cupsSNMPOIDToString(oid, temp, sizeof(temp)));

    if (!_cupsSNMPWrite(fd, addr, CUPS_SNMP_VERSION_1, community,
		       CUPS_ASN1_GET_REQUEST, 1, oid))
    {
      printf("FAIL (%s)\n", strerror(errno));
      return (0);
    }

    puts("PASS");

    fputs("_cupsSNMPRead(5.0): ", stdout);

    if (!_cupsSNMPRead(fd, &packet, 5.0))
    {
      puts("FAIL (timeout)");
      return (0);
    }

    if (!_cupsSNMPIsOID(&packet, oid))
    {
      printf("FAIL (bad OID %d", packet.object_name[0]);
      for (i = 1; packet.object_name[i] >= 0; i ++)
	printf(".%d", packet.object_name[i]);
      puts(")");
      return (0);
    }

    if (packet.error)
    {
      printf("FAIL (%s)\n", packet.error);
      return (0);
    }

    puts("PASS");

    print_packet(&packet, NULL);
  }

  return (1);
}