]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testsnmp.c
4 * SNMP test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2008 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * main() - Main entry.
22 * Include necessary headers...
36 static int *scan_oid(char *s
, int *oid
, int oidsize
);
37 static int show_oid(int fd
, char *s
, http_addr_t
*addr
);
41 * 'main()' - Main entry.
44 int /* O - Exit status */
45 main(int argc
, /* I - Number of command-line args */
46 char *argv
[]) /* I - Command-line arguments */
48 int i
; /* Looping var */
49 int fd
; /* SNMP socket */
50 http_addrlist_t
*host
; /* Address of host */
55 puts("Usage: ./testsnmp host-or-ip");
59 if ((host
= httpAddrGetList(argv
[1], AF_UNSPEC
, "161")) == NULL
)
61 printf("Unable to find \"%s\"!\n", argv
[1]);
65 fputs("cupsSNMPOpen: ", stdout
);
67 if ((fd
= cupsSNMPOpen()) < 0)
69 printf("FAIL (%s)\n", strerror(errno
));
78 * Query OIDs from the command-line...
81 for (i
= 2; i
< argc
; i
++)
82 if (!show_oid(fd
, argv
[i
], &(host
->addr
)))
85 else if (!show_oid(fd
, (char *)"1.3.6.1.2.1.43.10.2.1.4.1.1", &(host
->addr
)))
93 * 'scan_oid()' - Scan an OID value.
96 static int * /* O - OID or NULL on error */
97 scan_oid(char *s
, /* I - OID string */
98 int *oid
, /* I - OID array */
99 int oidsize
) /* I - Size of OID array in integers */
101 int i
; /* Index into OID array */
102 char *ptr
; /* Pointer into string */
105 for (ptr
= s
, i
= 0, oidsize
--; ptr
&& *ptr
&& i
< oidsize
; i
++)
107 if (!isdigit(*ptr
& 255))
110 oid
[i
] = strtol(ptr
, &ptr
, 10);
125 * 'show_oid()' - Show the specified OID.
128 static int /* O - 1 on success, 0 on error */
129 show_oid(int fd
, /* I - SNMP socket */
130 char *s
, /* I - OID to query */
131 http_addr_t
*addr
) /* I - Address to query */
133 int i
; /* Looping var */
134 int oid
[255]; /* OID */
135 cups_snmp_t packet
; /* SNMP packet */
138 printf("cupsSNMPWrite(%s): ", s
);
140 if (!scan_oid(s
, oid
, sizeof(oid
) / sizeof(oid
[0])))
142 puts("FAIL (bad OID)");
146 if (!cupsSNMPWrite(fd
, addr
, CUPS_SNMP_VERSION_1
, "public",
147 CUPS_ASN1_GET_REQUEST
, 1, oid
))
155 fputs("cupsSNMPRead(5000): ", stdout
);
157 if (!cupsSNMPRead(fd
, &packet
, 5000))
159 puts("FAIL (timeout)");
163 if (!cupsSNMPIsOID(&packet
, oid
))
165 puts("FAIL (bad OID)");
171 printf("FAIL (%s)\n", packet
.error
);
175 switch (packet
.object_type
)
177 case CUPS_ASN1_BOOLEAN
:
178 printf("PASS (BOOLEAN %s)\n",
179 packet
.object_value
.boolean
? "TRUE" : "FALSE");
182 case CUPS_ASN1_INTEGER
:
183 printf("PASS (INTEGER %d)\n", packet
.object_value
.integer
);
186 case CUPS_ASN1_BIT_STRING
:
187 printf("PASS (BIT-STRING \"%s\")\n", packet
.object_value
.string
);
190 case CUPS_ASN1_OCTET_STRING
:
191 printf("PASS (OCTET-STRING \"%s\")\n", packet
.object_value
.string
);
194 case CUPS_ASN1_NULL_VALUE
:
195 puts("PASS (NULL-VALUE)");
199 printf("PASS (OID %d", packet
.object_value
.oid
[0]);
200 for (i
= 1; packet
.object_value
.oid
[i
]; i
++)
201 printf(".%d", packet
.object_value
.oid
[i
]);
205 case CUPS_ASN1_COUNTER
:
206 printf("PASS (Counter %d)\n", packet
.object_value
.counter
);
209 case CUPS_ASN1_GAUGE
:
210 printf("PASS (Gauge %u)\n", packet
.object_value
.gauge
);
214 printf("PASS (Unknown-%X)\n", packet
.object_type
);