]>
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.
19 * scan_oid() - Scan an OID value.
20 * show_oid() - Show the specified OID.
21 * usage() - Show program usage and exit.
25 * Include necessary headers...
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);
47 * 'main()' - Main entry.
50 int /* O - Exit status */
51 main(int argc
, /* I - Number of command-line args */
52 char *argv
[]) /* I - Command-line arguments */
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 */
62 fputs("cupsSNMPDefaultCommunity: ", stdout
);
64 if ((community
= cupsSNMPDefaultCommunity()) == NULL
)
66 puts("FAIL (NULL community name)");
70 printf("PASS (%s)\n", community
);
73 * Query OIDs from the command-line...
76 for (i
= 1; i
< argc
; i
++)
77 if (!strcmp(argv
[i
], "-c"))
86 else if (!strcmp(argv
[i
], "-d"))
88 else if (!strcmp(argv
[i
], "-w"))
92 if ((host
= httpAddrGetList(argv
[i
], AF_UNSPEC
, "161")) == NULL
)
94 printf("testsnmp: Unable to find \"%s\"!\n", argv
[1]);
100 fputs("cupsSNMPOpen: ", stdout
);
102 if ((fd
= cupsSNMPOpen(host
->addr
.addr
.sa_family
)) < 0)
104 printf("FAIL (%s)\n", strerror(errno
));
111 else if (!show_oid(fd
, community
, &(host
->addr
), argv
[i
], walk
))
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
))
132 * 'print_packet()' - Print the contents of the response packet.
136 print_packet(cups_snmp_t
*packet
, /* I - SNMP response packet */
137 void *data
) /* I - User data pointer (not used) */
139 int i
; /* Looping var */
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
);
149 switch (packet
->object_type
)
151 case CUPS_ASN1_BOOLEAN
:
152 printf("BOOLEAN %s\n",
153 packet
->object_value
.boolean
? "TRUE" : "FALSE");
156 case CUPS_ASN1_INTEGER
:
157 printf("INTEGER %d\n", packet
->object_value
.integer
);
160 case CUPS_ASN1_BIT_STRING
:
161 printf("BIT-STRING \"%s\"\n", packet
->object_value
.string
);
164 case CUPS_ASN1_OCTET_STRING
:
165 printf("OCTET-STRING \"%s\"\n", packet
->object_value
.string
);
168 case CUPS_ASN1_NULL_VALUE
:
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
]);
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
]);
186 case CUPS_ASN1_COUNTER
:
187 printf("Counter %d\n", packet
->object_value
.counter
);
190 case CUPS_ASN1_GAUGE
:
191 printf("Gauge %u\n", packet
->object_value
.gauge
);
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);
204 printf("Unknown-%X\n", packet
->object_type
);
211 * 'scan_oid()' - Scan an OID value.
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 */
219 int i
; /* Index into OID array */
220 char *ptr
; /* Pointer into string */
223 for (ptr
= (char *)s
, i
= 0, oidsize
--; ptr
&& *ptr
&& i
< oidsize
; i
++)
225 if (!isdigit(*ptr
& 255))
228 oid
[i
] = strtol(ptr
, &ptr
, 10);
229 if (ptr
&& *ptr
== '.')
243 * 'show_oid()' - Show the specified OID.
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? */
253 int i
; /* Looping var */
254 int oid
[CUPS_SNMP_MAX_OID
]; /* OID */
255 cups_snmp_t packet
; /* SNMP packet */
258 if (!scan_oid(s
, oid
, sizeof(oid
) / sizeof(oid
[0])))
260 puts("testsnmp: Bad OID");
266 printf("cupsSNMPWalk(%d", oid
[0]);
267 for (i
= 1; oid
[i
] >= 0; i
++)
268 printf(".%d", oid
[i
]);
271 if (cupsSNMPWalk(fd
, addr
, CUPS_SNMP_VERSION_1
, community
, oid
, 5.0,
272 print_packet
, NULL
) < 0)
274 printf("FAIL (%s)\n", strerror(errno
));
280 printf("cupsSNMPWrite(%d", oid
[0]);
281 for (i
= 1; oid
[i
] >= 0; i
++)
282 printf(".%d", oid
[i
]);
283 fputs("): ", stdout
);
285 if (!cupsSNMPWrite(fd
, addr
, CUPS_SNMP_VERSION_1
, community
,
286 CUPS_ASN1_GET_REQUEST
, 1, oid
))
288 printf("FAIL (%s)\n", strerror(errno
));
294 fputs("cupsSNMPRead(5.0): ", stdout
);
296 if (!cupsSNMPRead(fd
, &packet
, 5.0))
298 puts("FAIL (timeout)");
302 if (!cupsSNMPIsOID(&packet
, oid
))
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
]);
313 printf("FAIL (%s)\n", packet
.error
);
319 print_packet(&packet
, NULL
);
327 * 'usage()' - Show program usage and exit.
333 puts("Usage: testsnmp [options] host-or-ip [oid ...]");
337 puts(" -c community Set community name");
338 puts(" -d Enable debugging");
339 puts(" -w Walk all OIDs under the specified one");