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