usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / snmp / apps / snmpset.c
blob32b306fdbae54ddadf757845612fa4cf51165dcc
1 /*
2 * snmpset.c - send snmp SET 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 <sys/types.h>
40 #if HAVE_NETINET_IN_H
41 #include <netinet/in.h>
42 #endif
43 #include <stdio.h>
44 #include <ctype.h>
45 #if TIME_WITH_SYS_TIME
46 # ifdef WIN32
47 # include <sys/timeb.h>
48 # else
49 # include <sys/time.h>
50 # endif
51 # include <time.h>
52 #else
53 # if HAVE_SYS_TIME_H
54 # include <sys/time.h>
55 # else
56 # include <time.h>
57 # endif
58 #endif
59 #if HAVE_SYS_SELECT_H
60 #include <sys/select.h>
61 #endif
62 #if HAVE_WINSOCK_H
63 #include <winsock.h>
64 #endif
65 #if HAVE_NETDB_H
66 #include <netdb.h>
67 #endif
68 #if HAVE_ARPA_INET_H
69 #include <arpa/inet.h>
70 #endif
72 #include <net-snmp/net-snmp-includes.h>
74 int failures = 0;
76 void
77 usage(void)
79 fprintf(stderr, "USAGE: snmpset ");
80 snmp_parse_args_usage(stderr);
81 fprintf(stderr, " OID TYPE VALUE [OID TYPE VALUE]...\n\n");
82 snmp_parse_args_descriptions(stderr);
83 fprintf(stderr,
84 " -C APPOPTS\t\tSet various application specific behaviours:\n");
85 fprintf(stderr, "\t\t\t q: don't print results on success\n");
86 fprintf(stderr, "\n TYPE: one of i, u, t, a, o, s, x, d, b, n\n");
87 fprintf(stderr,
88 "\ti: INTEGER, u: unsigned INTEGER, t: TIMETICKS, a: IPADDRESS\n");
89 fprintf(stderr,
90 "\to: OBJID, s: STRING, x: HEX STRING, d: DECIMAL STRING, b: BITS\n");
91 #ifdef OPAQUE_SPECIAL_TYPES
92 fprintf(stderr,
93 "\tU: unsigned int64, I: signed int64, F: float, D: double\n");
94 #endif /* OPAQUE_SPECIAL_TYPES */
98 static int quiet = 0;
100 static
101 void
102 optProc(int argc, char *const *argv, int opt)
104 switch (opt) {
105 case 'C':
106 while (*optarg) {
107 switch (*optarg++) {
108 case 'q':
109 quiet = 1;
110 break;
112 default:
113 fprintf(stderr, "Unknown flag passed to -C: %c\n",
114 optarg[-1]);
115 exit(1);
122 main(int argc, char *argv[])
124 netsnmp_session session, *ss;
125 netsnmp_pdu *pdu, *response = NULL;
126 netsnmp_variable_list *vars;
127 int arg;
128 int count;
129 int current_name = 0;
130 int current_type = 0;
131 int current_value = 0;
132 char *names[128];
133 char types[128];
134 char *values[128];
135 oid name[MAX_OID_LEN];
136 size_t name_length;
137 int status;
138 int exitval = 0;
140 putenv(strdup("POSIXLY_CORRECT=1"));
143 * get the common command line arguments
145 switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
146 case -2:
147 exit(0);
148 case -1:
149 usage();
150 exit(1);
151 default:
152 break;
155 if (arg >= argc) {
156 fprintf(stderr, "Missing object name\n");
157 usage();
158 exit(1);
162 * get object names, types, and values
164 for (; arg < argc; arg++) {
165 DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %s %s %s\n",
166 arg,argv[arg], arg+1 < argc ? argv[arg+1] : NULL,
167 arg+2 < argc ? argv[arg+2] : NULL));
168 names[current_name++] = argv[arg++];
169 if (arg < argc) {
170 switch (*argv[arg]) {
171 case '=':
172 case 'i':
173 case 'u':
174 case 't':
175 case 'a':
176 case 'o':
177 case 's':
178 case 'x':
179 case 'd':
180 case 'b':
181 #ifdef OPAQUE_SPECIAL_TYPES
182 case 'I':
183 case 'U':
184 case 'F':
185 case 'D':
186 #endif /* OPAQUE_SPECIAL_TYPES */
187 types[current_type++] = *argv[arg++];
188 break;
189 default:
190 fprintf(stderr, "%s: Bad object type: %c\n", argv[arg - 1],
191 *argv[arg]);
192 exit(1);
194 } else {
195 fprintf(stderr, "%s: Needs type and value\n", argv[arg - 1]);
196 exit(1);
198 if (arg < argc)
199 values[current_value++] = argv[arg];
200 else {
201 fprintf(stderr, "%s: Needs value\n", argv[arg - 2]);
202 exit(1);
206 SOCK_STARTUP;
209 * open an SNMP session
211 ss = snmp_open(&session);
212 if (ss == NULL) {
214 * diagnose snmp_open errors with the input netsnmp_session pointer
216 snmp_sess_perror("snmpset", &session);
217 SOCK_CLEANUP;
218 exit(1);
222 * create PDU for SET request and add object names and values to request
224 pdu = snmp_pdu_create(SNMP_MSG_SET);
225 for (count = 0; count < current_name; count++) {
226 name_length = MAX_OID_LEN;
227 if (snmp_parse_oid(names[count], name, &name_length) == NULL) {
228 snmp_perror(names[count]);
229 failures++;
230 } else
231 if (snmp_add_var
232 (pdu, name, name_length, types[count], values[count])) {
233 snmp_perror(names[count]);
234 failures++;
238 if (failures) {
239 SOCK_CLEANUP;
240 exit(1);
244 * do the request
246 status = snmp_synch_response(ss, pdu, &response);
247 if (status == STAT_SUCCESS) {
248 if (response->errstat == SNMP_ERR_NOERROR) {
249 if (!quiet) {
250 for (vars = response->variables; vars;
251 vars = vars->next_variable)
252 print_variable(vars->name, vars->name_length, vars);
254 } else {
255 fprintf(stderr, "Error in packet.\nReason: %s\n",
256 snmp_errstring(response->errstat));
257 if (response->errindex != 0) {
258 fprintf(stderr, "Failed object: ");
259 for (count = 1, vars = response->variables;
260 vars && (count != response->errindex);
261 vars = vars->next_variable, count++);
262 if (vars)
263 fprint_objid(stderr, vars->name, vars->name_length);
264 fprintf(stderr, "\n");
266 exitval = 2;
268 } else if (status == STAT_TIMEOUT) {
269 fprintf(stderr, "Timeout: No Response from %s\n",
270 session.peername);
271 exitval = 1;
272 } else { /* status == STAT_ERROR */
273 snmp_sess_perror("snmpset", ss);
274 exitval = 1;
277 if (response)
278 snmp_free_pdu(response);
279 snmp_close(ss);
280 SOCK_CLEANUP;
281 return exitval;