]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testsnmp.c
Import CUPS 1.4svn-r7356.
[thirdparty/cups.git] / cups / testsnmp.c
1 /*
2 * "$Id$"
3 *
4 * SNMP test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2008 by Apple Inc.
7 *
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/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 *
16 * Contents:
17 *
18 * main() - Main entry.
19 * scan_oid() - Scan an OID value.
20 * show_oid() - Show the specified OID.
21 * usage() - Show program usage and exit.
22 */
23
24 /*
25 * Include necessary headers...
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include "string.h"
32 #include "snmp.h"
33
34
35 /*
36 * Local functions...
37 */
38
39 static void print_packet(cups_snmp_t *packet, void *data);
40 static int *scan_oid(const char *s, int *oid, int oidsize);
41 static int show_oid(int fd, const char *community,
42 http_addr_t *addr, const char *s, int walk);
43 static void usage(void);
44
45
46 /*
47 * 'main()' - Main entry.
48 */
49
50 int /* O - Exit status */
51 main(int argc, /* I - Number of command-line args */
52 char *argv[]) /* I - Command-line arguments */
53 {
54 int i; /* Looping var */
55 int fd = -1; /* SNMP socket */
56 http_addrlist_t *host = NULL; /* Address of host */
57 int walk = 0; /* Walk OIDs? */
58 char *oid = NULL; /* Last OID shown */
59 const char *community; /* Community name */
60
61
62 fputs("cupsSNMPDefaultCommunity: ", stdout);
63
64 if ((community = cupsSNMPDefaultCommunity()) == NULL)
65 {
66 puts("FAIL (NULL community name)");
67 return (1);
68 }
69
70 printf("PASS (%s)\n", community);
71
72 /*
73 * Query OIDs from the command-line...
74 */
75
76 for (i = 1; i < argc; i ++)
77 if (!strcmp(argv[i], "-c"))
78 {
79 i ++;
80
81 if (i >= argc)
82 usage();
83 else
84 community = argv[i];
85 }
86 else if (!strcmp(argv[i], "-d"))
87 cupsSNMPSetDebug(10);
88 else if (!strcmp(argv[i], "-w"))
89 walk = 1;
90 else if (!host)
91 {
92 if ((host = httpAddrGetList(argv[i], AF_UNSPEC, "161")) == NULL)
93 {
94 printf("testsnmp: Unable to find \"%s\"!\n", argv[1]);
95 return (1);
96 }
97
98 if (fd < 0)
99 {
100 fputs("cupsSNMPOpen: ", stdout);
101
102 if ((fd = cupsSNMPOpen(host->addr.addr.sa_family)) < 0)
103 {
104 printf("FAIL (%s)\n", strerror(errno));
105 return (1);
106 }
107
108 puts("PASS");
109 }
110 }
111 else if (!show_oid(fd, community, &(host->addr), argv[i], walk))
112 return (1);
113 else
114 oid = argv[i];
115
116 if (!host)
117 usage();
118
119 if (!oid)
120 {
121 if (!show_oid(fd, community, &(host->addr),
122 walk ? "1.3.6.1.2.1.43" :
123 "1.3.6.1.2.1.43.10.2.1.4.1.1", walk))
124 return (1);
125 }
126
127 return (0);
128 }
129
130
131 /*
132 * 'print_packet()' - Print the contents of the response packet.
133 */
134
135 static void
136 print_packet(cups_snmp_t *packet, /* I - SNMP response packet */
137 void *data) /* I - User data pointer (not used) */
138 {
139 int i; /* Looping var */
140
141
142 (void)data;
143
144 printf("%d", packet->object_name[0]);
145 for (i = 1; packet->object_name[i] >= 0; i ++)
146 printf(".%d", packet->object_name[i]);
147 fputs(" = ", stdout);
148
149 switch (packet->object_type)
150 {
151 case CUPS_ASN1_BOOLEAN :
152 printf("BOOLEAN %s\n",
153 packet->object_value.boolean ? "TRUE" : "FALSE");
154 break;
155
156 case CUPS_ASN1_INTEGER :
157 printf("INTEGER %d\n", packet->object_value.integer);
158 break;
159
160 case CUPS_ASN1_BIT_STRING :
161 printf("BIT-STRING \"%s\"\n", packet->object_value.string);
162 break;
163
164 case CUPS_ASN1_OCTET_STRING :
165 printf("OCTET-STRING \"%s\"\n", packet->object_value.string);
166 break;
167
168 case CUPS_ASN1_NULL_VALUE :
169 puts("NULL-VALUE");
170 break;
171
172 case CUPS_ASN1_OID :
173 printf("OID %d", packet->object_value.oid[0]);
174 for (i = 1; packet->object_value.oid[i] >= 0; i ++)
175 printf(".%d", packet->object_value.oid[i]);
176 putchar('\n');
177 break;
178
179 case CUPS_ASN1_HEX_STRING :
180 fputs("Hex-STRING", stdout);
181 for (i = 0; i < packet->object_value.hex_string.num_bytes; i ++)
182 printf(" %02X", packet->object_value.hex_string.bytes[i]);
183 putchar('\n');
184 break;
185
186 case CUPS_ASN1_COUNTER :
187 printf("Counter %d\n", packet->object_value.counter);
188 break;
189
190 case CUPS_ASN1_GAUGE :
191 printf("Gauge %u\n", packet->object_value.gauge);
192 break;
193
194 case CUPS_ASN1_TIMETICKS :
195 printf("Timeticks %u days, %u:%02u:%02u.%02u\n",
196 packet->object_value.timeticks / 8640000,
197 (packet->object_value.timeticks / 360000) % 24,
198 (packet->object_value.timeticks / 6000) % 60,
199 (packet->object_value.timeticks / 100) % 60,
200 packet->object_value.timeticks % 100);
201 break;
202
203 default :
204 printf("Unknown-%X\n", packet->object_type);
205 break;
206 }
207 }
208
209
210 /*
211 * 'scan_oid()' - Scan an OID value.
212 */
213
214 static int * /* O - OID or NULL on error */
215 scan_oid(const char *s, /* I - OID string */
216 int *oid, /* I - OID array */
217 int oidsize) /* I - Size of OID array in integers */
218 {
219 int i; /* Index into OID array */
220 char *ptr; /* Pointer into string */
221
222
223 for (ptr = (char *)s, i = 0, oidsize --; ptr && *ptr && i < oidsize; i ++)
224 {
225 if (!isdigit(*ptr & 255))
226 return (NULL);
227
228 oid[i] = strtol(ptr, &ptr, 10);
229 if (ptr && *ptr == '.')
230 ptr ++;
231 }
232
233 if (i >= oidsize)
234 return (NULL);
235
236 oid[i] = -1;
237
238 return (oid);
239 }
240
241
242 /*
243 * 'show_oid()' - Show the specified OID.
244 */
245
246 static int /* O - 1 on success, 0 on error */
247 show_oid(int fd, /* I - SNMP socket */
248 const char *community, /* I - Community name */
249 http_addr_t *addr, /* I - Address to query */
250 const char *s, /* I - OID to query */
251 int walk) /* I - Walk OIDs? */
252 {
253 int i; /* Looping var */
254 int oid[CUPS_SNMP_MAX_OID]; /* OID */
255 cups_snmp_t packet; /* SNMP packet */
256
257
258 if (!scan_oid(s, oid, sizeof(oid) / sizeof(oid[0])))
259 {
260 puts("testsnmp: Bad OID");
261 return (0);
262 }
263
264 if (walk)
265 {
266 printf("cupsSNMPWalk(%d", oid[0]);
267 for (i = 1; oid[i] >= 0; i ++)
268 printf(".%d", oid[i]);
269 puts("):");
270
271 if (cupsSNMPWalk(fd, addr, CUPS_SNMP_VERSION_1, community, oid, 5.0,
272 print_packet, NULL) < 0)
273 {
274 printf("FAIL (%s)\n", strerror(errno));
275 return (0);
276 }
277 }
278 else
279 {
280 printf("cupsSNMPWrite(%d", oid[0]);
281 for (i = 1; oid[i] >= 0; i ++)
282 printf(".%d", oid[i]);
283 fputs("): ", stdout);
284
285 if (!cupsSNMPWrite(fd, addr, CUPS_SNMP_VERSION_1, community,
286 CUPS_ASN1_GET_REQUEST, 1, oid))
287 {
288 printf("FAIL (%s)\n", strerror(errno));
289 return (0);
290 }
291
292 puts("PASS");
293
294 fputs("cupsSNMPRead(5.0): ", stdout);
295
296 if (!cupsSNMPRead(fd, &packet, 5.0))
297 {
298 puts("FAIL (timeout)");
299 return (0);
300 }
301
302 if (!cupsSNMPIsOID(&packet, oid))
303 {
304 printf("FAIL (bad OID %d", packet.object_name[0]);
305 for (i = 1; packet.object_name[i] >= 0; i ++)
306 printf(".%d", packet.object_name[i]);
307 puts(")");
308 return (0);
309 }
310
311 if (packet.error)
312 {
313 printf("FAIL (%s)\n", packet.error);
314 return (0);
315 }
316
317 puts("PASS");
318
319 print_packet(&packet, NULL);
320 }
321
322 return (1);
323 }
324
325
326 /*
327 * 'usage()' - Show program usage and exit.
328 */
329
330 static void
331 usage(void)
332 {
333 puts("Usage: testsnmp [options] host-or-ip [oid ...]");
334 puts("");
335 puts("Options:");
336 puts("");
337 puts(" -c community Set community name");
338 puts(" -d Enable debugging");
339 puts(" -w Walk all OIDs under the specified one");
340
341 exit (1);
342 }
343
344
345 /*
346 * End of "$Id$".
347 */