blob: 5161b750862c1127d0dc7f47e1ca3af1a91524b3 [file] [log] [blame]
Wes Hardakerfca311a1995-03-09 00:08:14 +00001/*
2 * snmpgetnext.c - send snmp GETNEXT requests to a network entity.
3 *
4 */
5/***********************************************************************
6 Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
7
8 All Rights Reserved
9
Wes Hardaker84bacb01997-11-07 23:28:59 +000010Permission to use, copy, modify, and distribute this software and its
11documentation for any purpose and without fee is hereby granted,
Wes Hardakerfca311a1995-03-09 00:08:14 +000012provided that the above copyright notice appear in all copies and that
Wes Hardaker84bacb01997-11-07 23:28:59 +000013both that copyright notice and this permission notice appear in
Wes Hardakerfca311a1995-03-09 00:08:14 +000014supporting documentation, and that the name of CMU not be
15used in advertising or publicity pertaining to distribution of the
Wes Hardaker84bacb01997-11-07 23:28:59 +000016software without specific, written prior permission.
Wes Hardakerfca311a1995-03-09 00:08:14 +000017
18CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
19ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
20CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
21ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
23ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24SOFTWARE.
25******************************************************************/
Wes Hardakerf86050d1995-09-29 20:44:13 +000026#include <config.h>
27
Wes Hardaker238b7ba1997-01-20 14:09:25 +000028#if HAVE_STDLIB_H
29#include <stdlib.h>
30#endif
31#if HAVE_UNISTD_H
32#include <unistd.h>
33#endif
Wes Hardakera1a822e1998-03-26 15:22:30 +000034#if HAVE_STRING_H
Wes Hardaker238b7ba1997-01-20 14:09:25 +000035#include <string.h>
Wes Hardakera1a822e1998-03-26 15:22:30 +000036#else
37#include <strings.h>
Wes Hardaker238b7ba1997-01-20 14:09:25 +000038#endif
Wes Hardakerfca311a1995-03-09 00:08:14 +000039#include <sys/types.h>
Wes Hardakerf86050d1995-09-29 20:44:13 +000040#if HAVE_NETINET_IN_H
Wes Hardakerfca311a1995-03-09 00:08:14 +000041#include <netinet/in.h>
Wes Hardakerf86050d1995-09-29 20:44:13 +000042#endif
Wes Hardakerfca311a1995-03-09 00:08:14 +000043#include <stdio.h>
44#include <ctype.h>
Wes Hardakerf86050d1995-09-29 20:44:13 +000045#if TIME_WITH_SYS_TIME
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +000046# ifdef WIN32
47# include <sys/timeb.h>
48# else
49# include <sys/time.h>
50# endif
Wes Hardakerf86050d1995-09-29 20:44:13 +000051# 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
Wes Hardaker7a2cb581997-08-14 14:38:17 +000059#if HAVE_SYS_SELECT_H
60#include <sys/select.h>
61#endif
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +000062#if HAVE_WINSOCK_H
63#include <winsock.h>
Dave Shield9a2b5571999-05-18 16:23:50 +000064#endif
65#if HAVE_NETDB_H
Wes Hardakerfca311a1995-03-09 00:08:14 +000066#include <netdb.h>
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +000067#endif
Wes Hardaker238b7ba1997-01-20 14:09:25 +000068#if HAVE_ARPA_INET_H
69#include <arpa/inet.h>
70#endif
Wes Hardakerfca311a1995-03-09 00:08:14 +000071
Michael Slifcak829bf952000-04-18 18:08:02 +000072#include <getopt.h>
73
Wes Hardakerfca311a1995-03-09 00:08:14 +000074#include "asn1.h"
Wes Hardakerfca311a1995-03-09 00:08:14 +000075#include "snmp_api.h"
Wes Hardaker0310a931998-03-06 22:01:52 +000076#include "snmp_impl.h"
Wes Hardakerfca311a1995-03-09 00:08:14 +000077#include "snmp_client.h"
Wes Hardaker522909f1997-03-05 14:06:41 +000078#include "snmp.h"
79#include "mib.h"
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +000080#include "system.h"
Wes Hardaker191b2c81997-06-30 12:53:57 +000081#include "snmp_parse_args.h"
Wes Hardaker09188962000-04-12 16:13:08 +000082#include "default_store.h"
Wes Hardakerfca311a1995-03-09 00:08:14 +000083
Wes Hardaker4bc17ac2000-04-14 22:08:48 +000084#define DS_APP_DONT_FIX_PDUS 0
85
86static void optProc(int argc, char *const *argv, int opt)
87{
88 switch (opt) {
89 case 'C':
90 while (*optarg) {
91 switch (*optarg++) {
92 case 'f':
93 ds_toggle_boolean(DS_APPLICATION_ID, DS_APP_DONT_FIX_PDUS);
94 break;
95 }
96 }
97 break;
98 }
99}
Wes Hardakerfca311a1995-03-09 00:08:14 +0000100
Wes Hardaker40b2bf11999-01-18 18:47:36 +0000101void usage(void)
Wes Hardaker522909f1997-03-05 14:06:41 +0000102{
Wes Hardaker4bc17ac2000-04-14 22:08:48 +0000103 fprintf(stderr,"Usage: snmpgetnext [-Cf]");
Wes Hardaker4d0e99d1997-07-01 13:02:57 +0000104 snmp_parse_args_usage(stderr);
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000105 fprintf(stderr," [<objectID> ...]\n\n");
Wes Hardaker4d0e99d1997-07-01 13:02:57 +0000106 snmp_parse_args_descriptions(stderr);
Wes Hardaker4bc17ac2000-04-14 22:08:48 +0000107 fprintf(stderr, "snmpgetnext specific options\n");
108 fprintf(stderr, " -Cf\t\tDon't fix errors and retry the request.\n");
Wes Hardakerfca311a1995-03-09 00:08:14 +0000109}
110
Wes Hardaker40b2bf11999-01-18 18:47:36 +0000111int main(int argc, char *argv[])
Wes Hardakerfca311a1995-03-09 00:08:14 +0000112{
113 struct snmp_session session, *ss;
114 struct snmp_pdu *pdu, *response;
115 struct variable_list *vars;
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000116 int arg;
117 int count;
118 int current_name = 0;
119 char *names[128];
Wes Hardakerf95d33b1999-02-12 00:10:39 +0000120 oid name[MAX_OID_LEN];
Niels Baggesen0045e2f1999-05-03 22:40:11 +0000121 size_t name_length;
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000122 int status;
123 int failures = 0;
Wes Hardakerfca311a1995-03-09 00:08:14 +0000124
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000125 /* get the common command line arguments */
Wes Hardakerf74f8d12000-05-22 23:21:37 +0000126 if ((arg = snmp_parse_args(argc, argv, &session, "C:", &optProc)) < 0) {
127 usage();
128 exit(1);
129 }
Wes Hardakerfca311a1995-03-09 00:08:14 +0000130
Wes Hardakerecef0271999-08-19 23:09:54 +0000131 if (arg >= argc) {
132 fprintf(stderr, "Missing object name\n");
133 usage();
134 exit(1);
135 }
136
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000137 /* get the object names */
Wes Hardaker191b2c81997-06-30 12:53:57 +0000138 for(; arg < argc; arg++)
139 names[current_name++] = argv[arg];
Wes Hardakerfca311a1995-03-09 00:08:14 +0000140
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000141 SOCK_STARTUP;
142
143 /* open an SNMP session */
Wes Hardakerfca311a1995-03-09 00:08:14 +0000144 ss = snmp_open(&session);
145 if (ss == NULL){
Michael Slifcak16c2b161999-10-21 17:41:38 +0000146 /* diagnose snmp_open errors with the input struct snmp_session pointer */
Michael Slifcakc1a4cd71999-07-27 16:33:44 +0000147 snmp_sess_perror("snmpgetnext", &session);
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000148 SOCK_CLEANUP;
149 exit(1);
Wes Hardakerfca311a1995-03-09 00:08:14 +0000150 }
151
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000152 /* create PDU for GET request and add object names to request */
Wes Hardaker3b0cab31998-03-11 16:06:17 +0000153 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
Wes Hardakerfca311a1995-03-09 00:08:14 +0000154
155 for(count = 0; count < current_name; count++){
Wes Hardakerf95d33b1999-02-12 00:10:39 +0000156 name_length = MAX_OID_LEN;
Wes Hardaker7f4bb2f1997-12-05 19:58:52 +0000157 if (snmp_parse_oid(names[count], name, &name_length) == NULL) {
Michael Slifcakc1a4cd71999-07-27 16:33:44 +0000158 snmp_perror(names[count]);
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000159 failures++;
160 } else
161 snmp_add_null_var(pdu, name, name_length);
162 }
163 if (failures) {
164 SOCK_CLEANUP;
165 exit(1);
Wes Hardakerfca311a1995-03-09 00:08:14 +0000166 }
167
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000168 /* do the request */
Wes Hardakerfca311a1995-03-09 00:08:14 +0000169retry:
170 status = snmp_synch_response(ss, pdu, &response);
171 if (status == STAT_SUCCESS){
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000172 if (response->errstat == SNMP_ERR_NOERROR){
173 for(vars = response->variables; vars; vars = vars->next_variable)
174 print_variable(vars->name, vars->name_length, vars);
175 } else {
Wes Hardaker4e5278b1997-10-29 19:20:54 +0000176 fprintf(stderr, "Error in packet.\nReason: %s\n",
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000177 snmp_errstring(response->errstat));
178 if (response->errstat == SNMP_ERR_NOSUCHNAME){
Wes Hardaker4e5278b1997-10-29 19:20:54 +0000179 fprintf(stderr, "This name doesn't exist: ");
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000180 for(count = 1, vars = response->variables;
181 vars && count != response->errindex;
182 vars = vars->next_variable, count++)
183 ;
184 if (vars)
Wes Hardaker4e5278b1997-10-29 19:20:54 +0000185 fprint_objid(stderr, vars->name, vars->name_length);
186 fprintf(stderr, "\n");
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000187 }
Michael Slifcak55211d21999-04-14 23:56:37 +0000188
189 /* retry if the errored variable was successfully removed */
Wes Hardaker4bc17ac2000-04-14 22:08:48 +0000190 if (!ds_get_boolean(DS_APPLICATION_ID, DS_APP_DONT_FIX_PDUS)) {
Wes Hardaker09188962000-04-12 16:13:08 +0000191 pdu = snmp_fix_pdu(response, SNMP_MSG_GETNEXT);
192 snmp_free_pdu(response);
193 response = NULL;
194 if (pdu != NULL)
195 goto retry;
196 }
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000197 }
Wes Hardakerfca311a1995-03-09 00:08:14 +0000198 } else if (status == STAT_TIMEOUT){
Wes Hardakerd4728021998-04-27 18:00:44 +0000199 fprintf(stderr, "Timeout: No Response from %s.\n", session.peername);
Wes Hardakerfca311a1995-03-09 00:08:14 +0000200 } else { /* status == STAT_ERROR */
Wes Hardakerca7eabd1999-07-23 22:22:02 +0000201 snmp_sess_perror("snmpgetnext", ss);
Wes Hardakerfca311a1995-03-09 00:08:14 +0000202 }
203
204 if (response)
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000205 snmp_free_pdu(response);
Wes Hardakerfca311a1995-03-09 00:08:14 +0000206 snmp_close(ss);
Wes Hardakerb5a5a3c1997-10-28 00:26:57 +0000207 SOCK_CLEANUP;
Niels Baggesendf5cd021999-11-10 23:29:18 +0000208 return 0;
Wes Hardakerfca311a1995-03-09 00:08:14 +0000209}