usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / snmp / apps / snmpbulkget.c
blob47cc31fd09ccca763442f80c091cc1083ce89770
1 /*
2 * snmpbulkget.c - send SNMPv2 Bulk requests to a network entity.
4 */
5 /*********************************************************************
6 Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
8 All Rights Reserved
10 Permission to use, copy, modify, and distribute this software and its
11 documentation for any purpose and without fee is hereby granted,
12 provided that the above copyright notice appear in all copies and that
13 both that copyright notice and this permission notice appear in
14 supporting documentation, and that the name of CMU not be
15 used in advertising or publicity pertaining to distribution of the
16 software without specific, written prior permission.
18 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
19 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
20 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
21 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
23 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24 SOFTWARE.
25 **********************************************************************/
26 #include <net-snmp/net-snmp-config.h>
28 #if HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #if HAVE_STRING_H
35 #include <string.h>
36 #else
37 #include <strings.h>
38 #endif
39 #include <net-snmp/utilities.h>
40 #include <sys/types.h>
41 #if HAVE_NETINET_IN_H
42 #include <netinet/in.h>
43 #endif
44 #if TIME_WITH_SYS_TIME
45 # ifdef WIN32
46 # include <sys/timeb.h>
47 # else
48 # include <sys/time.h>
49 # endif
50 # include <time.h>
51 #else
52 # if HAVE_SYS_TIME_H
53 # include <sys/time.h>
54 # else
55 # include <time.h>
56 # endif
57 #endif
58 #if HAVE_SYS_SELECT_H
59 #include <sys/select.h>
60 #endif
61 #include <stdio.h>
62 #include <ctype.h>
63 #if HAVE_WINSOCK_H
64 #include <winsock.h>
65 #endif
66 #if HAVE_NETDB_H
67 #include <netdb.h>
68 #endif
69 #if HAVE_ARPA_INET_H
70 #include <arpa/inet.h>
71 #endif
73 #include <net-snmp/net-snmp-includes.h>
75 oid objid_mib[] = { 1, 3, 6, 1, 2, 1 };
76 int max_repetitions = 10;
77 int non_repeaters = 0;
78 struct nameStruct {
79 oid name[MAX_OID_LEN];
80 size_t name_len;
81 } *name, *namep;
82 int names;
84 void
85 usage(void)
87 fprintf(stderr, "USAGE: snmpbulkget ");
88 snmp_parse_args_usage(stderr);
89 fprintf(stderr, " OID [OID]...\n\n");
90 snmp_parse_args_descriptions(stderr);
91 fprintf(stderr,
92 " -C APPOPTS\t\tSet various application specific behaviours:\n");
93 fprintf(stderr, "\t\t\t n<NUM>: set non-repeaters to <NUM>\n");
94 fprintf(stderr, "\t\t\t r<NUM>: set max-repeaters to <NUM>\n");
97 static
98 void
99 optProc(int argc, char *const *argv, int opt)
101 char *endptr = NULL;
103 switch (opt) {
104 case 'C':
105 while (*optarg) {
106 switch (*optarg++) {
107 case 'n':
108 case 'r':
109 if (*(optarg - 1) == 'r') {
110 max_repetitions = strtol(optarg, &endptr, 0);
111 } else {
112 non_repeaters = strtol(optarg, &endptr, 0);
115 if (endptr == optarg) {
117 * No number given -- error.
119 usage();
120 exit(1);
121 } else {
122 optarg = endptr;
123 if (isspace(*optarg)) {
124 return;
127 break;
129 default:
130 fprintf(stderr, "Unknown flag passed to -C: %c\n",
131 optarg[-1]);
132 exit(1);
139 main(int argc, char *argv[])
141 netsnmp_session session, *ss;
142 netsnmp_pdu *pdu;
143 netsnmp_pdu *response;
144 netsnmp_variable_list *vars;
145 int arg;
146 int count;
147 int running;
148 int status;
149 int exitval = 0;
152 * get the common command line arguments
154 switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
155 case -2:
156 exit(0);
157 case -1:
158 usage();
159 exit(1);
160 default:
161 break;
164 names = argc - arg;
165 if (names < non_repeaters) {
166 fprintf(stderr, "snmpbulkget: need more objects than <nonrep>\n");
167 exit(1);
170 namep = name = (struct nameStruct *) calloc(names, sizeof(*name));
171 while (arg < argc) {
172 namep->name_len = MAX_OID_LEN;
173 if (snmp_parse_oid(argv[arg], namep->name, &namep->name_len) ==
174 NULL) {
175 snmp_perror(argv[arg]);
176 exit(1);
178 arg++;
179 namep++;
182 SOCK_STARTUP;
185 * open an SNMP session
187 ss = snmp_open(&session);
188 if (ss == NULL) {
190 * diagnose snmp_open errors with the input netsnmp_session pointer
192 snmp_sess_perror("snmpbulkget", &session);
193 SOCK_CLEANUP;
194 exit(1);
198 * create PDU for GETBULK request and add object name to request
200 pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
201 pdu->non_repeaters = non_repeaters;
202 pdu->max_repetitions = max_repetitions; /* fill the packet */
203 for (arg = 0; arg < names; arg++)
204 snmp_add_null_var(pdu, name[arg].name, name[arg].name_len);
207 * do the request
209 status = snmp_synch_response(ss, pdu, &response);
210 if (status == STAT_SUCCESS) {
211 if (response->errstat == SNMP_ERR_NOERROR) {
213 * check resulting variables
215 for (vars = response->variables; vars;
216 vars = vars->next_variable)
217 print_variable(vars->name, vars->name_length, vars);
218 } else {
220 * error in response, print it
222 running = 0;
223 if (response->errstat == SNMP_ERR_NOSUCHNAME) {
224 printf("End of MIB.\n");
225 } else {
226 fprintf(stderr, "Error in packet.\nReason: %s\n",
227 snmp_errstring(response->errstat));
228 if (response->errindex != 0) {
229 fprintf(stderr, "Failed object: ");
230 for (count = 1, vars = response->variables;
231 vars && (count != response->errindex);
232 vars = vars->next_variable, count++)
233 /*EMPTY*/;
234 if (vars)
235 fprint_objid(stderr, vars->name,
236 vars->name_length);
237 fprintf(stderr, "\n");
239 exitval = 2;
242 } else if (status == STAT_TIMEOUT) {
243 fprintf(stderr, "Timeout: No Response from %s\n",
244 session.peername);
245 running = 0;
246 exitval = 1;
247 } else { /* status == STAT_ERROR */
248 snmp_sess_perror("snmpbulkget", ss);
249 running = 0;
250 exitval = 1;
253 if (response)
254 snmp_free_pdu(response);
256 snmp_close(ss);
257 SOCK_CLEANUP;
258 return exitval;