]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/testsnmp.c
Return kDNSServiceErr_ServiceNotRunning when Bonjour for Windows not installed.
[thirdparty/cups.git] / cups / testsnmp.c
index eb47254a1d686079024f816080929874004d28d7..8071847a780353131dee099d2e5575c48293ffc2 100644 (file)
@@ -1,35 +1,17 @@
 /*
- * "$Id$"
+ * SNMP test program for CUPS.
  *
- *   SNMP test program for the Common UNIX Printing System (CUPS).
+ * Copyright 2008-2014 by Apple Inc.
  *
- *   Copyright 2008 by Apple Inc.
- *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   main()     - Main entry.
- *   scan_oid() - Scan an OID value.
- *   show_oid() - Show the specified OID.
- *   usage()    - Show program usage and exit.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
  * Include necessary headers...
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include "string.h"
-#include "snmp.h"
+#include "cups-private.h"
+#include "snmp-private.h"
 
 
 /*
  */
 
 static void    print_packet(cups_snmp_t *packet, void *data);
-static int     *scan_oid(const char *s, int *oid, int oidsize);
 static int     show_oid(int fd, const char *community,
                         http_addr_t *addr, const char *s, int walk);
-static void    usage(void);
+static void    usage(void) _CUPS_NORETURN;
 
 
 /*
@@ -59,9 +40,9 @@ main(int  argc,                               /* I - Number of command-line args */
   const char           *community;     /* Community name */
 
 
-  fputs("cupsSNMPDefaultCommunity: ", stdout);
+  fputs("_cupsSNMPDefaultCommunity: ", stdout);
 
-  if ((community = cupsSNMPDefaultCommunity()) == NULL)
+  if ((community = _cupsSNMPDefaultCommunity()) == NULL)
   {
     puts("FAIL (NULL community name)");
     return (1);
@@ -84,7 +65,7 @@ main(int  argc,                               /* I - Number of command-line args */
         community = argv[i];
     }
     else if (!strcmp(argv[i], "-d"))
-      cupsSNMPSetDebug(10);
+      _cupsSNMPSetDebug(10);
     else if (!strcmp(argv[i], "-w"))
       walk = 1;
     else if (!host)
@@ -97,9 +78,9 @@ main(int  argc,                               /* I - Number of command-line args */
 
       if (fd < 0)
       {
-       fputs("cupsSNMPOpen: ", stdout);
+       fputs("_cupsSNMPOpen: ", stdout);
 
-       if ((fd = cupsSNMPOpen(host->addr.addr.sa_family)) < 0)
+       if ((fd = _cupsSNMPOpen(host->addr.addr.sa_family)) < 0)
        {
          printf("FAIL (%s)\n", strerror(errno));
          return (1);
@@ -119,11 +100,11 @@ main(int  argc,                           /* I - Number of command-line args */
   if (!oid)
   {
     if (!show_oid(fd, community,  &(host->addr),
-                  walk ? "1.3.6.1.2.1.43" :
-                        "1.3.6.1.2.1.43.10.2.1.4.1.1", walk))
+                  walk ? ".1.3.6.1.2.1.43" :
+                        ".1.3.6.1.2.1.43.10.2.1.4.1.1", walk))
       return (1);
   }
-  
+
   return (0);
 }
 
@@ -136,15 +117,13 @@ static void
 print_packet(cups_snmp_t *packet,      /* I - SNMP response packet */
              void        *data)                /* I - User data pointer (not used) */
 {
-  int  i;                              /* Looping var */
+  unsigned     i;                      /* Looping var */
+  char         temp[1024];             /* Temporary OID string */
 
 
   (void)data;
 
-  printf("%d", packet->object_name[0]);
-  for (i = 1; packet->object_name[i] >= 0; i ++)
-    printf(".%d", packet->object_name[i]);
-  fputs(" = ", stdout);
+  printf("%s = ", _cupsSNMPOIDToString(packet->object_name, temp, sizeof(temp)));
 
   switch (packet->object_type)
   {
@@ -158,11 +137,13 @@ print_packet(cups_snmp_t *packet, /* I - SNMP response packet */
        break;
 
     case CUPS_ASN1_BIT_STRING :
-       printf("BIT-STRING \"%s\"\n", packet->object_value.string);
+       printf("BIT-STRING \"%s\"\n",
+              (char *)packet->object_value.string.bytes);
        break;
 
     case CUPS_ASN1_OCTET_STRING :
-       printf("OCTET-STRING \"%s\"\n", packet->object_value.string);
+       printf("OCTET-STRING \"%s\"\n",
+              (char *)packet->object_value.string.bytes);
        break;
 
     case CUPS_ASN1_NULL_VALUE :
@@ -170,16 +151,14 @@ print_packet(cups_snmp_t *packet, /* I - SNMP response packet */
        break;
 
     case CUPS_ASN1_OID :
-       printf("OID %d", packet->object_value.oid[0]);
-       for (i = 1; packet->object_value.oid[i] >= 0; i ++)
-         printf(".%d", packet->object_value.oid[i]);
-       putchar('\n');
+       printf("OID %s\n", _cupsSNMPOIDToString(packet->object_value.oid,
+                                               temp, sizeof(temp)));
        break;
 
     case CUPS_ASN1_HEX_STRING :
        fputs("Hex-STRING", stdout);
-       for (i = 0; i < packet->object_value.hex_string.num_bytes; i ++)
-         printf(" %02X", packet->object_value.hex_string.bytes[i]);
+       for (i = 0; i < packet->object_value.string.num_bytes; i ++)
+         printf(" %02X", packet->object_value.string.bytes[i]);
        putchar('\n');
        break;
 
@@ -207,38 +186,6 @@ print_packet(cups_snmp_t *packet,  /* I - SNMP response packet */
 }
 
 
-/*
- * 'scan_oid()' - Scan an OID value.
- */
-
-static int *                           /* O - OID or NULL on error */
-scan_oid(const char *s,                        /* I - OID string */
-         int        *oid,              /* I - OID array */
-        int        oidsize)            /* I - Size of OID array in integers */
-{
-  int  i;                              /* Index into OID array */
-  char *ptr;                           /* Pointer into string */
-
-
-  for (ptr = (char *)s, i = 0, oidsize --; ptr && *ptr && i < oidsize; i ++)
-  {
-    if (!isdigit(*ptr & 255))
-      return (NULL);
-
-    oid[i] = strtol(ptr, &ptr, 10);
-    if (ptr && *ptr == '.')
-      ptr ++;
-  }
-
-  if (i >= oidsize)
-    return (NULL);
-
-  oid[i] = -1;
-
-  return (oid);
-}
-
-
 /*
  * 'show_oid()' - Show the specified OID.
  */
@@ -253,9 +200,10 @@ show_oid(int         fd,           /* I - SNMP socket */
   int          i;                      /* Looping var */
   int          oid[CUPS_SNMP_MAX_OID]; /* OID */
   cups_snmp_t  packet;                 /* SNMP packet */
+  char         temp[1024];             /* Temporary OID string */
 
 
-  if (!scan_oid(s, oid, sizeof(oid) / sizeof(oid[0])))
+  if (!_cupsSNMPStringToOID(s, oid, sizeof(oid) / sizeof(oid[0])))
   {
     puts("testsnmp: Bad OID");
     return (0);
@@ -263,12 +211,9 @@ show_oid(int         fd,           /* I - SNMP socket */
 
   if (walk)
   {
-    printf("cupsSNMPWalk(%d", oid[0]);
-    for (i = 1; oid[i] >= 0; i ++)
-      printf(".%d", oid[i]);
-    puts("):");
+    printf("_cupsSNMPWalk(%s): ", _cupsSNMPOIDToString(oid, temp, sizeof(temp)));
 
-    if (cupsSNMPWalk(fd, addr, CUPS_SNMP_VERSION_1, community, oid, 5.0,
+    if (_cupsSNMPWalk(fd, addr, CUPS_SNMP_VERSION_1, community, oid, 5.0,
                      print_packet, NULL) < 0)
     {
       printf("FAIL (%s)\n", strerror(errno));
@@ -277,12 +222,9 @@ show_oid(int         fd,           /* I - SNMP socket */
   }
   else
   {
-    printf("cupsSNMPWrite(%d", oid[0]);
-    for (i = 1; oid[i] >= 0; i ++)
-      printf(".%d", oid[i]);
-    fputs("): ", stdout);
+    printf("_cupsSNMPWrite(%s): ", _cupsSNMPOIDToString(oid, temp, sizeof(temp)));
 
-    if (!cupsSNMPWrite(fd, addr, CUPS_SNMP_VERSION_1, community,
+    if (!_cupsSNMPWrite(fd, addr, CUPS_SNMP_VERSION_1, community,
                       CUPS_ASN1_GET_REQUEST, 1, oid))
     {
       printf("FAIL (%s)\n", strerror(errno));
@@ -291,15 +233,15 @@ show_oid(int         fd,          /* I - SNMP socket */
 
     puts("PASS");
 
-    fputs("cupsSNMPRead(5.0): ", stdout);
+    fputs("_cupsSNMPRead(5.0): ", stdout);
 
-    if (!cupsSNMPRead(fd, &packet, 5.0))
+    if (!_cupsSNMPRead(fd, &packet, 5.0))
     {
       puts("FAIL (timeout)");
       return (0);
     }
 
-    if (!cupsSNMPIsOID(&packet, oid))
+    if (!_cupsSNMPIsOID(&packet, oid))
     {
       printf("FAIL (bad OID %d", packet.object_name[0]);
       for (i = 1; packet.object_name[i] >= 0; i ++)
@@ -340,8 +282,3 @@ usage(void)
 
   exit (1);
 }
-
-
-/*
- * End of "$Id$".
- */