]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testsnmp.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / cups / testsnmp.c
1 /*
2 * "$Id$"
3 *
4 * SNMP test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2008 by Apple Inc.
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 *
18 * main() - Main entry.
19 */
20
21 /*
22 * Include necessary headers...
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include "string.h"
29 #include "snmp.h"
30
31
32 /*
33 * Local functions...
34 */
35
36 static int *scan_oid(char *s, int *oid, int oidsize);
37 static int show_oid(int fd, char *s, http_addr_t *addr);
38
39
40 /*
41 * 'main()' - Main entry.
42 */
43
44 int /* O - Exit status */
45 main(int argc, /* I - Number of command-line args */
46 char *argv[]) /* I - Command-line arguments */
47 {
48 int i; /* Looping var */
49 int fd; /* SNMP socket */
50 http_addrlist_t *host; /* Address of host */
51
52
53 if (argc < 2)
54 {
55 puts("Usage: ./testsnmp host-or-ip");
56 return (1);
57 }
58
59 if ((host = httpAddrGetList(argv[1], AF_UNSPEC, "161")) == NULL)
60 {
61 printf("Unable to find \"%s\"!\n", argv[1]);
62 return (1);
63 }
64
65 fputs("cupsSNMPOpen: ", stdout);
66
67 if ((fd = cupsSNMPOpen()) < 0)
68 {
69 printf("FAIL (%s)\n", strerror(errno));
70 return (1);
71 }
72
73 puts("PASS");
74
75 if (argc > 2)
76 {
77 /*
78 * Query OIDs from the command-line...
79 */
80
81 for (i = 2; i < argc; i ++)
82 if (!show_oid(fd, argv[i], &(host->addr)))
83 return (1);
84 }
85 else if (!show_oid(fd, (char *)"1.3.6.1.2.1.43.10.2.1.4.1.1", &(host->addr)))
86 return (1);
87
88 return (0);
89 }
90
91
92 /*
93 * 'scan_oid()' - Scan an OID value.
94 */
95
96 static int * /* O - OID or NULL on error */
97 scan_oid(char *s, /* I - OID string */
98 int *oid, /* I - OID array */
99 int oidsize) /* I - Size of OID array in integers */
100 {
101 int i; /* Index into OID array */
102 char *ptr; /* Pointer into string */
103
104
105 for (ptr = s, i = 0, oidsize --; ptr && *ptr && i < oidsize; i ++)
106 {
107 if (!isdigit(*ptr & 255))
108 return (NULL);
109
110 oid[i] = strtol(ptr, &ptr, 10);
111 if (*ptr == '.')
112 ptr ++;
113 }
114
115 if (i >= oidsize)
116 return (NULL);
117
118 oid[i] = 0;
119
120 return (oid);
121 }
122
123
124 /*
125 * 'show_oid()' - Show the specified OID.
126 */
127
128 static int /* O - 1 on success, 0 on error */
129 show_oid(int fd, /* I - SNMP socket */
130 char *s, /* I - OID to query */
131 http_addr_t *addr) /* I - Address to query */
132 {
133 int i; /* Looping var */
134 int oid[255]; /* OID */
135 cups_snmp_t packet; /* SNMP packet */
136
137
138 printf("cupsSNMPWrite(%s): ", s);
139
140 if (!scan_oid(s, oid, sizeof(oid) / sizeof(oid[0])))
141 {
142 puts("FAIL (bad OID)");
143 return (0);
144 }
145
146 if (!cupsSNMPWrite(fd, addr, CUPS_SNMP_VERSION_1, "public",
147 CUPS_ASN1_GET_REQUEST, 1, oid))
148 {
149 puts("FAIL");
150 return (0);
151 }
152
153 puts("PASS");
154
155 fputs("cupsSNMPRead(5000): ", stdout);
156
157 if (!cupsSNMPRead(fd, &packet, 5000))
158 {
159 puts("FAIL (timeout)");
160 return (0);
161 }
162
163 if (!cupsSNMPIsOID(&packet, oid))
164 {
165 puts("FAIL (bad OID)");
166 return (0);
167 }
168
169 if (packet.error)
170 {
171 printf("FAIL (%s)\n", packet.error);
172 return (0);
173 }
174
175 switch (packet.object_type)
176 {
177 case CUPS_ASN1_BOOLEAN :
178 printf("PASS (BOOLEAN %s)\n",
179 packet.object_value.boolean ? "TRUE" : "FALSE");
180 break;
181
182 case CUPS_ASN1_INTEGER :
183 printf("PASS (INTEGER %d)\n", packet.object_value.integer);
184 break;
185
186 case CUPS_ASN1_BIT_STRING :
187 printf("PASS (BIT-STRING \"%s\")\n", packet.object_value.string);
188 break;
189
190 case CUPS_ASN1_OCTET_STRING :
191 printf("PASS (OCTET-STRING \"%s\")\n", packet.object_value.string);
192 break;
193
194 case CUPS_ASN1_NULL_VALUE :
195 puts("PASS (NULL-VALUE)");
196 break;
197
198 case CUPS_ASN1_OID :
199 printf("PASS (OID %d", packet.object_value.oid[0]);
200 for (i = 1; packet.object_value.oid[i]; i ++)
201 printf(".%d", packet.object_value.oid[i]);
202 puts(")");
203 break;
204
205 case CUPS_ASN1_COUNTER :
206 printf("PASS (Counter %d)\n", packet.object_value.counter);
207 break;
208
209 case CUPS_ASN1_GAUGE:
210 printf("PASS (Gauge %u)\n", packet.object_value.gauge);
211 break;
212
213 default :
214 printf("PASS (Unknown-%X)\n", packet.object_type);
215 break;
216 }
217
218 return (1);
219 }
220
221
222 /*
223 * End of "$Id$".
224 */