Merge pull request #2254 from esohns/fstat_size_64_bits
[ACE_TAO.git] / ACE / ASNMP / examples / get / get_async.cpp
blobac843df2192af386e3345a40212e2ebfe151ecc9
1 //=============================================================================
2 /**
3 * @file get_async.cpp
5 * Sample application demonstrating synchronous Snmp::get API
6 * to access an SNMP Version 1 agent.
7 */
8 //=============================================================================
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"
31 // FUZZ: disable check_for_streams_include
32 #include "ace/streams.h"
34 // SNMPv1 Get Application
35 class getapp : public Snmp_Result
37 public:
38 getapp(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 virtual void result(Snmp * r, int rc);
45 private:
46 getapp(const getapp&);
48 UdpAddress address_;
49 Pdu pdu_; // construct a request Pdu
50 Oid oid_;
51 OctetStr community_;
52 Snmp snmp_;
53 UdpTarget target_;
54 int valid_;
58 // main entry point
59 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
61 ACE_Argv_Type_Converter atc (argc, argv);
62 getapp get (atc.get_argc (), atc.get_ASCII_argv ());
63 if (get.valid())
64 return get.run();
65 else
66 getapp::usage();
67 return 1;
70 int getapp::valid() const
72 return valid_;
74 getapp::getapp(int argc, char *argv[]): valid_(0)
76 Oid req, def_oid("1.3.6.1.2.1.1.1.0"); // default is sysDescr
77 if ( argc < 2)
78 return;
80 address_ = argv[argc - 1];
81 if ( !address_.valid()) {
82 cout << "ERROR: Invalid IPv4 address or DNS hostname: " \
83 << argv[argc] << "\n";
84 return;
87 ACE_Argv_Type_Converter to_tchar (argc, argv);
88 ACE_Get_Opt get_opt (argc,
89 to_tchar.get_TCHAR_argv (),
90 ACE_TEXT ("o:c:r:t:p:"));
91 for (int c; (c = get_opt ()) != -1; )
92 switch (c)
94 case 'o':
95 req = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
96 if (req.valid() == 0)
97 cout << "ERROR: oid value: "
98 << ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg())
99 << "is not valid. using default.\n";
100 break;
102 case 'c':
103 community_ = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
104 target_.set_read_community(community_);
105 break;
107 case 'r':
108 target_.set_retry(ACE_OS::atoi (get_opt.opt_arg()));
109 break;
111 case 't':
112 target_.set_timeout(ACE_OS::atoi (get_opt.opt_arg()));
113 break;
115 default:
116 break;
119 Vb vb; // construct a Vb object
120 if (req.valid())
121 vb.set_oid( req); // set the Oid portion of the Vb
122 else {
123 vb.set_oid( def_oid); // set the Oid portion of the Vb
125 pdu_ += vb;
126 vb.get_oid(oid_); // store for later use
127 valid_ = 1;
130 void getapp::usage()
132 cout << "Usage:\n";
133 cout << "get [options] dotted-quad | DNSName[:port]\n";
134 cout << " -o OID defaults to 1.3.6.1.2.1.1.1.0 (mibII sysDescr.0)\n";
135 cout << " -c Community_name, default is 'public'\n";
136 cout << " -r N retries default is N = 1 retry\n";
137 cout << " -t N timeout in seconds default is 1 second" << endl;
141 int getapp::run()
143 //----------[ create a ASNMP session ]-----------------------------------
144 if ( snmp_.valid() != SNMP_CLASS_SUCCESS) {
145 cout << "\nASNMP:ERROR:Create session failed: "<<
146 snmp_.error_string()<< "\n";
147 return 1;
150 //--------[ build up ASNMP object needed ]-------------------------------
151 if (address_.get_port() == 0)
152 address_.set_port(DEF_AGENT_PORT);
153 target_.set_address( address_); // make a target using the address
155 //-------[ issue the request, blocked mode ]-----------------------------
156 cout << "\nASNMP:INFO:SNMP Version " << (target_.get_version()+ 1) << \
157 " GET SAMPLE PROGRAM \nOID: " << oid_.to_string() << "\n";
158 target_.get_address(address_); // target updates port used
159 int rc;
160 const char *name = address_.resolve_hostname(rc);
162 cout << "Device: " << address_ << " ";
164 //FUZZ: disable check_for_lack_ACE_OS
165 cout << (rc ? "<< did not resolve via gethostbyname() >>" : name) << "\n";
166 //FUZZ: enable check_for_lack_ACE_OS
168 cout << "[ Retries=" << target_.get_retry() << " \
169 Timeout=" << target_.get_timeout() <<" ms " << "Community=" << \
170 community_.to_string() << " ]"<< endl;
172 if (snmp_.get( pdu_, target_, this) != SNMP_CLASS_SUCCESS) {
173 const char *ptr = snmp_.error_string();
174 cout << "ASNMP:ERROR: get command failed reason: " << ptr << endl;
175 } else {
176 ACE_Reactor::instance()->run_reactor_event_loop();
178 return 0;
181 void getapp::result(Snmp *, int rc)
183 Vb vb;
184 if (rc < 0)
186 const char *ptr = snmp_.error_string();
187 cout << "ASNMP:ERROR: get command failed reason: " << ptr << endl;
189 else
191 // check to see if there are any errors
192 if (pdu_.get_error_status())
194 cout << "ERROR: agent replied as follows\n";
195 cout << pdu_.agent_error_reason() << endl;
197 else
199 VbIter iter(pdu_);
200 while (iter.next(vb))
202 cout << "\tOid = " << vb.to_string_oid() << "\n";
203 cout << "\tValue = " << vb.to_string_value() << "\n";
207 cout << "\nASNMP:INFO: command completed normally.\n"<< endl;
208 ACE_Reactor::instance()->end_reactor_event_loop();