Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ASNMP / examples / set / set.cpp
bloba2a83387fc9d0386f419a8719a1915490378974b
2 //=============================================================================
3 /**
4 * @file set.cpp
6 * Sample application demonstrating synchronous Snmp::set API
7 * to update an oid in 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"
31 // FUZZ: disable check_for_streams_include
32 #include "ace/streams.h"
35 // SNMPv1 Set Application
37 class set {
38 public:
39 set(int argc, char **argv); // process command line args
40 int valid() const; // verify transaction can proceed
41 int run(); // issue transaction
42 static void usage(); // operator help message
44 private:
45 set(const set&);
47 UdpAddress address_;
48 Pdu pdu_; // construct a request Pdu
49 Oid oid_;
50 OctetStr community_;
51 Snmp snmp_;
52 UdpTarget target_;
53 int valid_;
57 // main entry point
58 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
60 ACE_Argv_Type_Converter atc (argc, argv);
61 set get (atc.get_argc (), atc.get_ASCII_argv ());
62 if (get.valid())
63 return get.run();
64 else
65 set::usage();
66 return 1;
69 int
70 set::valid() const
72 return valid_;
75 set::set(int argc, char *argv[]): valid_(0)
77 Vb vb; // construct a Vb object
78 Oid req;
79 if ( argc < 2)
80 return;
81 target_.get_write_community(community_);
82 address_ = argv[argc - 1];
83 if ( !address_.valid()) {
84 cout << "ERROR: Invalid IPv4 address or DNS hostname: " \
85 << argv[argc] << "\n";
86 return;
89 ACE_Argv_Type_Converter to_tchar (argc, argv);
90 ACE_Get_Opt get_opt (argc,
91 to_tchar.get_TCHAR_argv (),
92 ACE_TEXT ("o:c:r:t:I:U:C:G:T:O:S:P:"));
93 for (int c; (c = get_opt ()) != -1; )
94 switch (c)
96 case 'o':
97 req = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
98 if (req.valid() == 0)
99 cout << "ERROR: oid value: "
100 << ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg())
101 << "is not valid. using default.\n";
102 break;
104 case 'c':
105 community_ = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
106 target_.set_write_community(community_);
107 break;
109 case 'r':
110 target_.set_retry(ACE_OS::atoi (get_opt.opt_arg()));
111 break;
113 case 't':
114 target_.set_timeout(ACE_OS::atoi (get_opt.opt_arg()));
115 break;
117 case 'I': // Integer32
119 SnmpInt32 o(ACE_OS::atoi(get_opt.opt_arg()));
120 vb.set_value(o);
121 pdu_ += vb;
123 break;
125 case 'U': // Unsigned32
127 SnmpUInt32 o(ACE_OS::atoi(get_opt.opt_arg()));
128 vb.set_value(o);
129 pdu_ += vb;
131 break;
133 case 'C': // Counter32
135 Counter32 o(ACE_OS::atoi(get_opt.opt_arg()));
136 vb.set_value(o);
137 pdu_ += vb;
139 break;
141 case 'G': // Gauge32
143 Gauge32 o(ACE_OS::atoi(get_opt.opt_arg()));
144 vb.set_value(o);
145 pdu_ += vb;
147 break;
149 case 'T': // TimeTicks
151 TimeTicks o(ACE_OS::atoi(get_opt.opt_arg()));
152 vb.set_value(o);
153 pdu_ += vb;
155 break;
157 case 'O': // Oid as a variable identifier
159 oid_ = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
160 vb.set_oid(oid_); // when value is set, pdu updated
162 break;
164 case 'S': // Octet String
166 OctetStr o(ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg()));
167 vb.set_value(o); // set the Oid portion of the Vb
168 pdu_ += vb;
170 break;
172 case 'P': // Oid String as a value
174 Oid o(ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg()));
175 vb.set_value(o); // set the Oid portion of the Vb
176 pdu_ += vb;
178 break;
180 default:
181 break;
184 // if user didn't set anything use defaults
185 if (pdu_.get_vb_count() == 0) {
186 Oid def_oid("1.3.6.1.2.1.1.4.0"); // defualt is sysName
187 OctetStr def_value("sysName.0 updated by ASNMP set command");
188 vb.set_oid(def_oid);
189 vb.set_value(def_value);
190 pdu_ += vb;
191 cout << "INFO: using defaults, setting sysName to : " << \
192 def_value.to_string() << endl;
195 valid_ = 1;
198 void set::usage()
200 cout << "Usage:\n";
201 cout << "next [options] dotted-quad | DNSName[:port]\n";
202 cout << " -o OID starts with oid after 1.3.6.1.2.1.1.1.0 (mibII sysDescr.0)\n";
203 cout << " -c Community_name, default is 'private'\n";
204 cout << " -r N retries default is N = 1 retry\n";
205 cout << " -t N timeout in seconds default is 1 second\n";
206 cout << " -O oid_to_set -{I,U,G,S,P} value\n";
207 cout << " where I=int32, U=uint32, G=gauge32, S=octet, P=oid" << endl;
211 int set::run()
213 //----------[ create a ASNMP session ]-----------------------------------
214 if ( snmp_.valid() != SNMP_CLASS_SUCCESS) {
215 cout << "\nASNMP:ERROR:Create session failed: "<<
216 snmp_.error_string()<< "\n";
217 return 1;
220 //--------[ build up ASNMP object needed ]-------------------------------
221 if (address_.get_port() == 0)
222 address_.set_port(DEF_AGENT_PORT);
223 target_.set_address( address_); // make a target using the address
225 //-------[ issue the request, blocked mode ]-----------------------------
226 cout << "\nASNMP:INFO:SNMP Version " << (target_.get_version()+ 1) << \
227 " SET SAMPLE PROGRAM \nOID: " << oid_.to_string() << "\n";
228 target_.get_address(address_); // target updates port used
229 int rc;
230 const char *name = address_.resolve_hostname(rc);
232 cout << "Device: " << address_ << " ";
234 //FUZZ: disable check_for_lack_ACE_OS
235 cout << (rc ? "<< did not resolve via gethostbyname() >>" : name) << "\n";
236 //FUZZ: enable check_for_lack_ACE_OS
238 cout << "[ Retries=" << target_.get_retry() << " \
239 Timeout=" << target_.get_timeout() <<" ms " << "Community=" << \
240 community_.to_string() << " ]"<< endl;
242 if (snmp_.set( pdu_, target_) == SNMP_CLASS_SUCCESS) {
243 Vb vb;
244 // check to see if there are any errors
245 if (pdu_.get_error_status()) {
246 cout << "ERROR: agent replied as follows\n";
247 cout << pdu_.agent_error_reason() << endl;
249 else {
250 VbIter iter(pdu_);
251 while (iter.next(vb)) {
252 cout << "\tOid = " << vb.to_string_oid() << "\n";
253 cout << "\tValue = " << vb.to_string_value() << "\n";
257 else {
258 const char *ptr = snmp_.error_string();
259 cout << "ASNMP:ERROR: set command failed reason: " << ptr << endl;
261 cout << "ASNMP:INFO:command completed normally.\n"<< endl;
262 return 0;