Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / ASNMP / examples / trap / trap.cpp
blobdb841ee58898540c9b422a6f9e05f79efd7788bc
2 //=============================================================================
3 /**
4 * @file trap.cpp
6 * Sample application demonstrating synchronous Snmp::trap API
7 * to send to an SNMP Version 1 trap listener app.
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 #define DEFINE_TRAP_CONSTANTS_
29 #include "asnmp/enttraps.h" // enterprise standard traps
30 #include "ace/Argv_Type_Converter.h"
31 #include "ace/Get_Opt.h"
33 // FUZZ: disable check_for_streams_include
34 #include "ace/streams.h"
37 // SNMPv1 Trap Application
39 class trapapp {
40 public:
41 trapapp(int argc, char **argv); // process command line args
42 int valid() const; // verify transaction can proceed
43 int run(); // issue transaction
44 static void usage(); // operator help message
46 private:
47 trapapp(const trapapp&);
49 UdpAddress address_;
50 Pdu pdu_; // construct a request Pdu
51 Oid oid_;
52 OctetStr community_;
53 Snmp snmp_;
54 UdpTarget target_;
55 int valid_;
59 // main entry point
60 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
62 ACE_Argv_Type_Converter atc (argc, argv);
63 trapapp get (atc.get_argc (), atc.get_ASCII_argv ());
64 if (get.valid())
65 return get.run();
66 else
67 trapapp::usage();
68 return 1;
71 int trapapp::valid() const
73 return valid_;
75 trapapp::trapapp(int argc, char *argv[]): valid_(0)
77 Oid def_ent_oid("1.3.6.1.2.1.1.1.2.0.1"); // def enterprise oid
78 Oid ent, trap; // user specified values
80 if ( argc < 2) // hostname mandatory
81 return;
83 address_ = argv[argc - 1];
84 if ( !address_.valid()) {
85 cout << "ERROR: Invalid IPv4 address or DNS hostname: " \
86 << argv[argc] << "\n";
87 return;
90 ACE_Argv_Type_Converter to_tchar (argc, argv);
91 ACE_Get_Opt get_opt (argc,
92 to_tchar.get_TCHAR_argv (),
93 ACE_TEXT ("c:e:t:"));
94 for (int c; (c = get_opt ()) != -1; )
95 switch (c)
97 case 'c': // community string
98 community_ = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
99 target_.set_read_community(community_);
100 break;
102 case 'e': // trap oid to send
103 ent = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
104 break;
106 case 't': // trap oid
107 trap = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
108 break;;
110 default:
111 break;
114 if (ent.valid())
115 pdu_.set_notify_enterprise( ent); // set up the enterprise of the trap
116 else
117 pdu_.set_notify_enterprise( def_ent_oid);
119 if (trap.valid())
120 pdu_.set_notify_id( trap); // set the id of the trap
121 else
122 pdu_.set_notify_id( coldStart); // set the id of the trap
124 Oid detail_oid("1.3.6.1.4.1.11.2.16.2");
125 OctetStr detail_value("SNMP++ Trap Send Test");
126 Vb vb(detail_oid, detail_value);
127 pdu_ += vb;
129 pdu_.get_notify_id(oid_); // store for later use
130 valid_ = 1;
133 void trapapp::usage()
135 cout << "Usage:\n";
136 cout << "trap [options] dotted-quad | DNSName[:port]\n";
137 cout << " -c Community_name, default is 'public'\n";
138 cout << " -r N retries default is N = 1 retry\n";
139 cout << " -t N timeout in seconds default is 1 second" << endl;
140 cout << " -e oid enterprise oid default is 1.3.6.1.2.1.1.1.2.0.1\n";
141 cout << " -O oid trap id default is coldStart 1.3.6.1.6.3.1.1.5.1\n";
144 int trapapp::run()
146 if ( snmp_.valid() != SNMP_CLASS_SUCCESS) {
147 cout << "\nASNMP:ERROR:Create session failed: "<<
148 snmp_.error_string()<< "\n";
149 return 1;
152 if (address_.get_port() == 0)
153 address_.set_port(DEF_TRAP_PORT);
154 target_.set_address( address_); // make a target using the address
156 //-------[ issue the request, blocked mode ]-----------------------------
157 cout << "\nASNMP:INFO:SNMP Version " << (target_.get_version()+ 1) << \
158 " TRAP GENERATOR SAMPLE PROGRAM \nOID: " << oid_.to_string() << "\n";
159 target_.get_address(address_); // target updates port used
160 int rc;
161 const char *name = address_.resolve_hostname(rc);
163 cout << "Device: " << address_ << " ";
165 //FUZZ: disable check_for_lack_ACE_OS
166 cout << (rc ? "<< did not resolve via gethostbyname() >>" : name) << "\n";
167 //FUZZ: enable check_for_lack_ACE_OS
169 cout << "[ Community=" << community_.to_string() << " ]"<< endl;
171 if (snmp_.trap( pdu_, target_) == SNMP_CLASS_SUCCESS) {
172 cout << "Trap was written to network...\n";
174 else {
175 const char *ptr = snmp_.error_string();
176 cout << "ASNMP:ERROR: trap command failed reason: " << ptr << endl;
179 cout << "ASNMP:INFO:command completed normally.\n"<< endl;
180 return 0;