]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testsnmp.c
4 * SNMP test program for CUPS.
6 * Copyright 2008-2014 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 * Include necessary headers...
21 #include "cups-private.h"
22 #include "snmp-private.h"
29 static void print_packet(cups_snmp_t
*packet
, void *data
);
30 static int show_oid(int fd
, const char *community
,
31 http_addr_t
*addr
, const char *s
, int walk
);
32 static void usage(void) __attribute__((noreturn
));
36 * 'main()' - Main entry.
39 int /* O - Exit status */
40 main(int argc
, /* I - Number of command-line args */
41 char *argv
[]) /* I - Command-line arguments */
43 int i
; /* Looping var */
44 int fd
= -1; /* SNMP socket */
45 http_addrlist_t
*host
= NULL
; /* Address of host */
46 int walk
= 0; /* Walk OIDs? */
47 char *oid
= NULL
; /* Last OID shown */
48 const char *community
; /* Community name */
51 fputs("_cupsSNMPDefaultCommunity: ", stdout
);
53 if ((community
= _cupsSNMPDefaultCommunity()) == NULL
)
55 puts("FAIL (NULL community name)");
59 printf("PASS (%s)\n", community
);
62 * Query OIDs from the command-line...
65 for (i
= 1; i
< argc
; i
++)
66 if (!strcmp(argv
[i
], "-c"))
75 else if (!strcmp(argv
[i
], "-d"))
76 _cupsSNMPSetDebug(10);
77 else if (!strcmp(argv
[i
], "-w"))
81 if ((host
= httpAddrGetList(argv
[i
], AF_UNSPEC
, "161")) == NULL
)
83 printf("testsnmp: Unable to find \"%s\"!\n", argv
[1]);
89 fputs("_cupsSNMPOpen: ", stdout
);
91 if ((fd
= _cupsSNMPOpen(host
->addr
.addr
.sa_family
)) < 0)
93 printf("FAIL (%s)\n", strerror(errno
));
100 else if (!show_oid(fd
, community
, &(host
->addr
), argv
[i
], walk
))
110 if (!show_oid(fd
, community
, &(host
->addr
),
111 walk
? ".1.3.6.1.2.1.43" :
112 ".1.3.6.1.2.1.43.10.2.1.4.1.1", walk
))
121 * 'print_packet()' - Print the contents of the response packet.
125 print_packet(cups_snmp_t
*packet
, /* I - SNMP response packet */
126 void *data
) /* I - User data pointer (not used) */
128 unsigned i
; /* Looping var */
129 char temp
[1024]; /* Temporary OID string */
134 printf("%s = ", _cupsSNMPOIDToString(packet
->object_name
, temp
, sizeof(temp
)));
136 switch (packet
->object_type
)
138 case CUPS_ASN1_BOOLEAN
:
139 printf("BOOLEAN %s\n",
140 packet
->object_value
.boolean
? "TRUE" : "FALSE");
143 case CUPS_ASN1_INTEGER
:
144 printf("INTEGER %d\n", packet
->object_value
.integer
);
147 case CUPS_ASN1_BIT_STRING
:
148 printf("BIT-STRING \"%s\"\n",
149 (char *)packet
->object_value
.string
.bytes
);
152 case CUPS_ASN1_OCTET_STRING
:
153 printf("OCTET-STRING \"%s\"\n",
154 (char *)packet
->object_value
.string
.bytes
);
157 case CUPS_ASN1_NULL_VALUE
:
162 printf("OID %s\n", _cupsSNMPOIDToString(packet
->object_value
.oid
,
163 temp
, sizeof(temp
)));
166 case CUPS_ASN1_HEX_STRING
:
167 fputs("Hex-STRING", stdout
);
168 for (i
= 0; i
< packet
->object_value
.string
.num_bytes
; i
++)
169 printf(" %02X", packet
->object_value
.string
.bytes
[i
]);
173 case CUPS_ASN1_COUNTER
:
174 printf("Counter %d\n", packet
->object_value
.counter
);
177 case CUPS_ASN1_GAUGE
:
178 printf("Gauge %u\n", packet
->object_value
.gauge
);
181 case CUPS_ASN1_TIMETICKS
:
182 printf("Timeticks %u days, %u:%02u:%02u.%02u\n",
183 packet
->object_value
.timeticks
/ 8640000,
184 (packet
->object_value
.timeticks
/ 360000) % 24,
185 (packet
->object_value
.timeticks
/ 6000) % 60,
186 (packet
->object_value
.timeticks
/ 100) % 60,
187 packet
->object_value
.timeticks
% 100);
191 printf("Unknown-%X\n", packet
->object_type
);
198 * 'show_oid()' - Show the specified OID.
201 static int /* O - 1 on success, 0 on error */
202 show_oid(int fd
, /* I - SNMP socket */
203 const char *community
, /* I - Community name */
204 http_addr_t
*addr
, /* I - Address to query */
205 const char *s
, /* I - OID to query */
206 int walk
) /* I - Walk OIDs? */
208 int i
; /* Looping var */
209 int oid
[CUPS_SNMP_MAX_OID
]; /* OID */
210 cups_snmp_t packet
; /* SNMP packet */
211 char temp
[1024]; /* Temporary OID string */
214 if (!_cupsSNMPStringToOID(s
, oid
, sizeof(oid
) / sizeof(oid
[0])))
216 puts("testsnmp: Bad OID");
222 printf("_cupsSNMPWalk(%s): ", _cupsSNMPOIDToString(oid
, temp
, sizeof(temp
)));
224 if (_cupsSNMPWalk(fd
, addr
, CUPS_SNMP_VERSION_1
, community
, oid
, 5.0,
225 print_packet
, NULL
) < 0)
227 printf("FAIL (%s)\n", strerror(errno
));
233 printf("_cupsSNMPWrite(%s): ", _cupsSNMPOIDToString(oid
, temp
, sizeof(temp
)));
235 if (!_cupsSNMPWrite(fd
, addr
, CUPS_SNMP_VERSION_1
, community
,
236 CUPS_ASN1_GET_REQUEST
, 1, oid
))
238 printf("FAIL (%s)\n", strerror(errno
));
244 fputs("_cupsSNMPRead(5.0): ", stdout
);
246 if (!_cupsSNMPRead(fd
, &packet
, 5.0))
248 puts("FAIL (timeout)");
252 if (!_cupsSNMPIsOID(&packet
, oid
))
254 printf("FAIL (bad OID %d", packet
.object_name
[0]);
255 for (i
= 1; packet
.object_name
[i
] >= 0; i
++)
256 printf(".%d", packet
.object_name
[i
]);
263 printf("FAIL (%s)\n", packet
.error
);
269 print_packet(&packet
, NULL
);
277 * 'usage()' - Show program usage and exit.
283 puts("Usage: testsnmp [options] host-or-ip [oid ...]");
287 puts(" -c community Set community name");
288 puts(" -d Enable debugging");
289 puts(" -w Walk all OIDs under the specified one");