Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / ASNMP / examples / walk / walk.cpp
blob555340358f6dbac40baa84b94fbe331d73c00dbb
2 //=============================================================================
3 /**
4 * @file walk.cpp
6 * Sample application demonstrating synchronous Snmp::get, get_next API
7 * to access an SNMP Version 1 agent.
8 */
9 //=============================================================================
11 /*===================================================================
12 Copyright (c) 1996
13 Hewlett-Packard Company
15 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
16 Permission to use, copy, modify, distribute and/or sell this software
17 and/or its documentation is hereby granted without fee. User agrees
18 to display the above copyright notice and this license notice in all
19 copies of the software and any documentation of the software. User
20 agrees to assume all liability for the use of the software; Hewlett-Packard
21 makes no representations about the suitability of this software for any
22 purpose. It is provided "AS-IS without warranty of any kind,either express
23 or implied. User hereby grants a royalty-free license to any and all
24 derivatives based upon this software code base.
25 =====================================================================*/
27 #include "asnmp/snmp.h"
28 #include "ace/Argv_Type_Converter.h"
29 #include "ace/Get_Opt.h"
30 // FUZZ: disable check_for_streams_include
31 #include "ace/streams.h"
34 // SNMPv1 Walk Mib Application
36 class walkapp {
37 public:
38 walkapp(int argc, char **argv); // process command line args
39 int valid() const; // verify transaction can proceed
40 int run(); // issue transaction
41 static void usage(); // operator help message
43 private:
44 walkapp(const walkapp&);
46 UdpAddress address_;
47 Pdu pdu_; // construct a request Pdu
48 Oid oid_;
49 OctetStr community_;
50 Snmp snmp_;
51 UdpTarget target_;
52 int valid_;
56 // main entry point
57 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
59 ACE_Argv_Type_Converter atc (argc, argv);
60 walkapp get (atc.get_argc (), atc.get_ASCII_argv ());
61 if (get.valid())
62 return get.run();
63 else
64 walkapp::usage();
65 return 1;
68 int walkapp::valid() const
70 return valid_;
73 walkapp::walkapp(int argc, char *argv[]): valid_(0)
75 Oid req, def_oid("1.3.6.1.2.1.1.1.0"); // default begin walk with MIBII
76 if ( argc < 2)
77 return;
79 address_ = argv[argc - 1];
80 if ( !address_.valid()) {
81 cout << "ERROR: Invalid IPv4 address or DNS hostname: " \
82 << argv[argc] << "\n";
83 return;
86 ACE_Argv_Type_Converter to_tchar (argc, argv);
87 ACE_Get_Opt get_opt (argc,
88 to_tchar.get_TCHAR_argv (),
89 ACE_TEXT ("o:c:r:t:"));
90 for (int c; (c = get_opt ()) != -1; )
91 switch (c)
93 case 'o':
94 req = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
95 if (req.valid() == 0)
96 cout << "ERROR: oid value: "
97 << ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg())
98 << "is not valid. using default.\n";
99 break;
101 case 'c':
102 community_ = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
103 target_.set_read_community(community_);
104 break;
106 case 'r':
107 target_.set_retry(ACE_OS::atoi (get_opt.opt_arg()));
108 break;
110 case 't':
111 target_.set_timeout(ACE_OS::atoi (get_opt.opt_arg()));
112 break;
114 default:
115 break;
118 Vb vb; // construct a Vb object
119 if (req.valid())
120 vb.set_oid( req); // set the Oid portion of the Vb
121 else {
122 vb.set_oid( def_oid); // set the Oid portion of the Vb
124 pdu_ += vb;
125 vb.get_oid(oid_); // store for later use
126 valid_ = 1;
129 void walkapp::usage()
131 cout << "Usage:\n";
132 cout << "walk [options] dotted-quad | DNSName[:port]\n";
133 cout << " -o OID starts with oid after 1.3.6.1.2.1.1.1.0 (mibII sysDescr.0)\n";
134 cout << " -c Community_name, default is 'public'\n";
135 cout << " -r N retries default is N = 1 retry\n";
136 cout << " -t N timeout in seconds default is 1 second" << endl;
141 // simple mib iterator class
143 class MibIter {
144 public:
145 // Pdu must contain initial oid to begin with
146 MibIter(Snmp *snmp, Pdu& pdu, UdpTarget* target);
147 int next(Vb& vb, char *&err_reason); // return next oid in mib
149 private:
150 Snmp *snmp_;
151 UdpTarget *target_;
152 Pdu pdu_;
153 Vb vb_;
154 int first_; // flag to obtain first entry
155 int valid_; // flag to obtain first entry
158 MibIter::MibIter(Snmp* snmp, Pdu& pdu, UdpTarget *target):
159 snmp_(snmp), target_(target), pdu_(pdu), first_(0),
160 valid_(0)
162 // verify we have a valid oid to begin iterating with
163 Oid oid;
164 Vb vb;
165 pdu.get_vb(vb, 0);
166 vb.get_oid(oid);
167 if (oid.valid())
168 valid_ = 1;
171 // return vb of next oid in agent tree, return 1 else return 0, reason set
172 int MibIter::next(Vb& vb, char *& reason)
174 int rc;
176 if (valid_ == 0) // not valid object
177 return -1;
179 // 1. poll for value
180 if (first_ == 0) {
181 rc = snmp_->get( pdu_, *target_);
182 first_++;
184 else {
185 rc = snmp_->get_next( pdu_, *target_);
188 if (rc != SNMP_CLASS_SUCCESS) {
189 reason = const_cast <char*> (snmp_->error_string());
190 return 0;
193 // 2. check for problems
194 if (pdu_.get_error_status()) {
195 reason = const_cast <char*> (pdu_.agent_error_reason());
196 return 0;
199 // 3. return vb to caller
200 pdu_.get_vb(vb, 0);
201 Oid nextoid;
202 vb.get_oid(nextoid); // and setup next oid to get
203 Vb nextvb(nextoid);
204 pdu_.delete_all_vbs();
205 pdu_ += nextvb; // can't do set_vb as there are no entries to replace
207 return 1; // ok
210 int walkapp::run()
212 //----------[ create a ASNMP session ]-----------------------------------
213 if ( snmp_.valid() != SNMP_CLASS_SUCCESS) {
214 cout << "\nASNMP:ERROR:Create session failed: "<<
215 snmp_.error_string()<< "\n";
216 return 1;
219 //--------[ build up ASNMP object needed ]-------------------------------
220 if (address_.get_port() == 0)
221 address_.set_port(DEF_AGENT_PORT);
222 target_.set_address( address_); // make a target using the address
224 //-------[ issue the request, blocked mode ]-----------------------------
225 cout << "\nASNMP:INFO:SNMP Version " << (target_.get_version()+ 1) << \
226 " WALK SAMPLE PROGRAM \nOID: " << oid_.to_string() << "\n";
227 target_.get_address(address_); // target updates port used
228 int rc;
229 const char *name = address_.resolve_hostname(rc);
231 cout << "Device: " << address_ << " ";
233 //FUZZ: disable check_for_lack_ACE_OS
234 cout << (rc ? "<< did not resolve via gethostbyname() >>" : name) << "\n";
235 //FUZZ: enable check_for_lack_ACE_OS
237 cout << "[ Retries=" << target_.get_retry() << " \
238 Timeout=" << target_.get_timeout() <<" ms " << "Community=" << \
239 community_.to_string() << " ]"<< endl;
241 MibIter iter(&snmp_, pdu_, &target_);
242 char *err_str = 0;
243 Vb vb;
244 unsigned ctr = 0;
245 while (iter.next(vb, err_str)) {
246 cout << "\tOid = " << vb.to_string_oid() << "\n";
247 cout << "\tValue = " << vb.to_string_value() << "\n";
248 ctr++;
251 if (!err_str) {
252 cout << "ERROR: walk: " << err_str << endl;
253 return 0;
256 cout << "ASNMP:INFO:command completed normally. ACE Rocks...\n"<< endl;
257 return 0;