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