2 //=============================================================================
6 * Sample application demonstrating synchronous Snmp::trap API
7 * to send to an SNMP Version 1 trap listener app.
9 //=============================================================================
11 /*===================================================================
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
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
47 trapapp(const trapapp
&);
50 Pdu pdu_
; // construct a request Pdu
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 ());
71 int trapapp::valid() const
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
83 address_
= argv
[argc
- 1];
84 if ( !address_
.valid()) {
85 cout
<< "ERROR: Invalid IPv4 address or DNS hostname: " \
86 << argv
[argc
] << "\n";
90 ACE_Argv_Type_Converter
to_tchar (argc
, argv
);
91 ACE_Get_Opt
get_opt (argc
,
92 to_tchar
.get_TCHAR_argv (),
94 for (int c
; (c
= get_opt ()) != -1; )
97 case 'c': // community string
98 community_
= ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg());
99 target_
.set_read_community(community_
);
102 case 'e': // trap oid to send
103 ent
= ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg());
106 case 't': // trap oid
107 trap
= ACE_TEXT_ALWAYS_CHAR (get_opt
.opt_arg());
115 pdu_
.set_notify_enterprise( ent
); // set up the enterprise of the trap
117 pdu_
.set_notify_enterprise( def_ent_oid
);
120 pdu_
.set_notify_id( trap
); // set the id of the trap
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
);
129 pdu_
.get_notify_id(oid_
); // store for later use
133 void trapapp::usage()
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";
146 if ( snmp_
.valid() != SNMP_CLASS_SUCCESS
) {
147 cout
<< "\nASNMP:ERROR:Create session failed: "<<
148 snmp_
.error_string()<< "\n";
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
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";
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
;