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