]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/testsnmp.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / cups / testsnmp.c
CommitLineData
91c84a35 1/*
7e86f2f6 2 * SNMP test program for CUPS.
91c84a35 3 *
7e86f2f6 4 * Copyright 2008-2014 by Apple Inc.
91c84a35 5 *
e3101897 6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
91c84a35
MS
7 */
8
9/*
10 * Include necessary headers...
11 */
12
71e16022 13#include "cups-private.h"
7a14d768 14#include "snmp-private.h"
91c84a35
MS
15
16
17/*
18 * Local functions...
19 */
20
ac884b6a 21static void print_packet(cups_snmp_t *packet, void *data);
ac884b6a
MS
22static int show_oid(int fd, const char *community,
23 http_addr_t *addr, const char *s, int walk);
85dda01c 24static void usage(void) __attribute__((noreturn));
91c84a35
MS
25
26
27/*
28 * 'main()' - Main entry.
29 */
30
31int /* O - Exit status */
32main(int argc, /* I - Number of command-line args */
33 char *argv[]) /* I - Command-line arguments */
34{
35 int i; /* Looping var */
ac884b6a
MS
36 int fd = -1; /* SNMP socket */
37 http_addrlist_t *host = NULL; /* Address of host */
38 int walk = 0; /* Walk OIDs? */
39 char *oid = NULL; /* Last OID shown */
40 const char *community; /* Community name */
91c84a35
MS
41
42
7a14d768 43 fputs("_cupsSNMPDefaultCommunity: ", stdout);
ac884b6a 44
7a14d768 45 if ((community = _cupsSNMPDefaultCommunity()) == NULL)
91c84a35 46 {
ac884b6a 47 puts("FAIL (NULL community name)");
91c84a35
MS
48 return (1);
49 }
50
ac884b6a
MS
51 printf("PASS (%s)\n", community);
52
53 /*
54 * Query OIDs from the command-line...
55 */
56
57 for (i = 1; i < argc; i ++)
58 if (!strcmp(argv[i], "-c"))
59 {
60 i ++;
61
62 if (i >= argc)
63 usage();
64 else
65 community = argv[i];
66 }
67 else if (!strcmp(argv[i], "-d"))
7a14d768 68 _cupsSNMPSetDebug(10);
ac884b6a
MS
69 else if (!strcmp(argv[i], "-w"))
70 walk = 1;
71 else if (!host)
72 {
73 if ((host = httpAddrGetList(argv[i], AF_UNSPEC, "161")) == NULL)
74 {
75 printf("testsnmp: Unable to find \"%s\"!\n", argv[1]);
76 return (1);
77 }
78
79 if (fd < 0)
80 {
7a14d768 81 fputs("_cupsSNMPOpen: ", stdout);
ac884b6a 82
7a14d768 83 if ((fd = _cupsSNMPOpen(host->addr.addr.sa_family)) < 0)
ac884b6a
MS
84 {
85 printf("FAIL (%s)\n", strerror(errno));
86 return (1);
87 }
88
89 puts("PASS");
90 }
91 }
92 else if (!show_oid(fd, community, &(host->addr), argv[i], walk))
93 return (1);
94 else
95 oid = argv[i];
96
97 if (!host)
98 usage();
99
100 if (!oid)
91c84a35 101 {
ac884b6a 102 if (!show_oid(fd, community, &(host->addr),
20fbc903
MS
103 walk ? ".1.3.6.1.2.1.43" :
104 ".1.3.6.1.2.1.43.10.2.1.4.1.1", walk))
ac884b6a 105 return (1);
91c84a35 106 }
85dda01c 107
ac884b6a
MS
108 return (0);
109}
91c84a35 110
91c84a35 111
ac884b6a
MS
112/*
113 * 'print_packet()' - Print the contents of the response packet.
114 */
91c84a35 115
ac884b6a
MS
116static void
117print_packet(cups_snmp_t *packet, /* I - SNMP response packet */
118 void *data) /* I - User data pointer (not used) */
119{
7e86f2f6
MS
120 unsigned i; /* Looping var */
121 char temp[1024]; /* Temporary OID string */
91c84a35 122
ac884b6a
MS
123
124 (void)data;
125
20fbc903 126 printf("%s = ", _cupsSNMPOIDToString(packet->object_name, temp, sizeof(temp)));
ac884b6a
MS
127
128 switch (packet->object_type)
91c84a35 129 {
ac884b6a
MS
130 case CUPS_ASN1_BOOLEAN :
131 printf("BOOLEAN %s\n",
132 packet->object_value.boolean ? "TRUE" : "FALSE");
133 break;
91c84a35 134
ac884b6a
MS
135 case CUPS_ASN1_INTEGER :
136 printf("INTEGER %d\n", packet->object_value.integer);
137 break;
138
139 case CUPS_ASN1_BIT_STRING :
d1c13e16
MS
140 printf("BIT-STRING \"%s\"\n",
141 (char *)packet->object_value.string.bytes);
ac884b6a
MS
142 break;
143
144 case CUPS_ASN1_OCTET_STRING :
d1c13e16
MS
145 printf("OCTET-STRING \"%s\"\n",
146 (char *)packet->object_value.string.bytes);
ac884b6a
MS
147 break;
148
149 case CUPS_ASN1_NULL_VALUE :
150 puts("NULL-VALUE");
151 break;
152
153 case CUPS_ASN1_OID :
20fbc903
MS
154 printf("OID %s\n", _cupsSNMPOIDToString(packet->object_value.oid,
155 temp, sizeof(temp)));
ac884b6a
MS
156 break;
157
158 case CUPS_ASN1_HEX_STRING :
159 fputs("Hex-STRING", stdout);
d1c13e16
MS
160 for (i = 0; i < packet->object_value.string.num_bytes; i ++)
161 printf(" %02X", packet->object_value.string.bytes[i]);
ac884b6a
MS
162 putchar('\n');
163 break;
164
165 case CUPS_ASN1_COUNTER :
166 printf("Counter %d\n", packet->object_value.counter);
167 break;
168
169 case CUPS_ASN1_GAUGE :
170 printf("Gauge %u\n", packet->object_value.gauge);
171 break;
172
173 case CUPS_ASN1_TIMETICKS :
174 printf("Timeticks %u days, %u:%02u:%02u.%02u\n",
175 packet->object_value.timeticks / 8640000,
176 (packet->object_value.timeticks / 360000) % 24,
177 (packet->object_value.timeticks / 6000) % 60,
178 (packet->object_value.timeticks / 100) % 60,
179 packet->object_value.timeticks % 100);
180 break;
181
182 default :
183 printf("Unknown-%X\n", packet->object_type);
184 break;
91c84a35 185 }
91c84a35
MS
186}
187
188
91c84a35
MS
189/*
190 * 'show_oid()' - Show the specified OID.
191 */
192
193static int /* O - 1 on success, 0 on error */
194show_oid(int fd, /* I - SNMP socket */
ac884b6a
MS
195 const char *community, /* I - Community name */
196 http_addr_t *addr, /* I - Address to query */
197 const char *s, /* I - OID to query */
198 int walk) /* I - Walk OIDs? */
91c84a35
MS
199{
200 int i; /* Looping var */
ac884b6a 201 int oid[CUPS_SNMP_MAX_OID]; /* OID */
91c84a35 202 cups_snmp_t packet; /* SNMP packet */
20fbc903 203 char temp[1024]; /* Temporary OID string */
91c84a35
MS
204
205
20fbc903 206 if (!_cupsSNMPStringToOID(s, oid, sizeof(oid) / sizeof(oid[0])))
91c84a35 207 {
ac884b6a 208 puts("testsnmp: Bad OID");
91c84a35
MS
209 return (0);
210 }
211
ac884b6a 212 if (walk)
91c84a35 213 {
20fbc903 214 printf("_cupsSNMPWalk(%s): ", _cupsSNMPOIDToString(oid, temp, sizeof(temp)));
ac884b6a 215
7a14d768 216 if (_cupsSNMPWalk(fd, addr, CUPS_SNMP_VERSION_1, community, oid, 5.0,
ac884b6a
MS
217 print_packet, NULL) < 0)
218 {
219 printf("FAIL (%s)\n", strerror(errno));
220 return (0);
221 }
91c84a35 222 }
ac884b6a 223 else
91c84a35 224 {
20fbc903 225 printf("_cupsSNMPWrite(%s): ", _cupsSNMPOIDToString(oid, temp, sizeof(temp)));
ac884b6a 226
7a14d768 227 if (!_cupsSNMPWrite(fd, addr, CUPS_SNMP_VERSION_1, community,
ac884b6a
MS
228 CUPS_ASN1_GET_REQUEST, 1, oid))
229 {
230 printf("FAIL (%s)\n", strerror(errno));
231 return (0);
232 }
233
234 puts("PASS");
235
7a14d768 236 fputs("_cupsSNMPRead(5.0): ", stdout);
ac884b6a 237
7a14d768 238 if (!_cupsSNMPRead(fd, &packet, 5.0))
ac884b6a
MS
239 {
240 puts("FAIL (timeout)");
241 return (0);
242 }
243
7a14d768 244 if (!_cupsSNMPIsOID(&packet, oid))
ac884b6a
MS
245 {
246 printf("FAIL (bad OID %d", packet.object_name[0]);
247 for (i = 1; packet.object_name[i] >= 0; i ++)
248 printf(".%d", packet.object_name[i]);
249 puts(")");
250 return (0);
251 }
252
253 if (packet.error)
254 {
255 printf("FAIL (%s)\n", packet.error);
256 return (0);
257 }
258
259 puts("PASS");
260
261 print_packet(&packet, NULL);
91c84a35
MS
262 }
263
ac884b6a
MS
264 return (1);
265}
91c84a35 266
91c84a35 267
ac884b6a
MS
268/*
269 * 'usage()' - Show program usage and exit.
270 */
91c84a35 271
ac884b6a
MS
272static void
273usage(void)
274{
275 puts("Usage: testsnmp [options] host-or-ip [oid ...]");
276 puts("");
277 puts("Options:");
278 puts("");
279 puts(" -c community Set community name");
280 puts(" -d Enable debugging");
281 puts(" -w Walk all OIDs under the specified one");
282
283 exit (1);
91c84a35 284}