2 //=============================================================================
6 * The basic SNMPv1 API - blocking version using UDP/IPv4 transport
9 * @author Peter E Mellquist class design/orig codeMichael R MacFaden mrm@cisco.com / rework API
10 * @author use ACE facilities
12 //=============================================================================
14 /*===================================================================
16 Hewlett-Packard Company
18 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
19 Permission to use, copy, modify, distribute and/or sell this software
20 and/or its documentation is hereby granted without fee. User agrees
21 to display the above copyright notice and this license notice in all
22 copies of the software and any documentation of the software. User
23 agrees to assume all liability for the use of the software; Hewlett-Packard
24 makes no representations about the suitability of this software for any
25 purpose. It is provided "AS-IS without warranty of any kind,either express
26 or implied. User hereby grants a royalty-free license to any and all
27 derivatives based upon this software code base.
28 =====================================================================*/
31 #define INCLUDE_SNMP_ERR_STRINGS_
33 //----[ snmp++ includes ]----------------------------------------------
34 #include "asnmp/snmp.h" // class def for this module
35 #include "asnmp/oid.h" // class def for oids
36 #include "asnmp/enttraps.h" // class def for well known trap oids
37 #include "asnmp/vb.h" // class def for vbs
38 #include "asnmp/address.h" // class def for addresses
39 #include "asnmp/wpdu.h" // adapter to cmu_library / wire protocol
40 #include "asnmp/transaction.h" // synchronous transaction processor
41 #include "ace/OS_NS_string.h"
42 #include "ace/OS_NS_unistd.h"
43 #include "ace/OS_NS_time.h"
45 const coldStartOid coldStart
;
46 const warmStartOid warmStart
;
47 const linkDownOid linkDown
;
48 const linkUpOid linkUp
;
49 const authenticationFailureOid authenticationFailure
;
50 const egpNeighborLossOid egpNeighborLoss
;
51 const snmpTrapEnterpriseOid snmpTrapEnterprise
;
53 char Snmp::host_name_
[MAXHOSTNAMELEN
] = "";
55 Snmp::Snmp(unsigned short port
): result_(0), construct_status_(SNMP_CLASS_ERROR
), last_transaction_status_(0)
57 ACE_TRACE("Snmp::Snmp");
59 ACE_INET_Addr
addr(port
); // any port,address is ok
60 if (iv_snmp_session_
.open(addr
) < 0) {
61 last_transaction_status_
= errno
; // open udp/ipv4 socket
62 ACE_DEBUG((LM_DEBUG
, "Snmp::snmp::open port %d failed", port
));
66 // initial request id randomly generated then monotonically incremented
67 req_id_
= (unsigned) ACE_OS::time(0);
69 construct_status_
= SNMP_CLASS_SUCCESS
;
75 ACE_TRACE("Snmp::~Snmp");
76 iv_snmp_session_
.close();
79 int Snmp::valid() const
81 return construct_status_
;
84 // given error code, return string definition
87 const char * Snmp::error_string(int last_transaction_status_
)
89 ACE_TRACE("Snmp::error_string");
91 return ((last_transaction_status_
< 0) ?
92 ((last_transaction_status_
< MAX_NEG_ERROR
)?
93 nErrs
[ abs(MAX_NEG_ERROR
) + 1] : nErrs
[abs(last_transaction_status_
)]):
94 ((last_transaction_status_
> MAX_POS_ERROR
)?
95 pErrs
[MAX_POS_ERROR
+1] : pErrs
[last_transaction_status_
]));
99 const char * Snmp::error_string()
101 return Snmp::error_string(last_transaction_status_
);
104 void Snmp::check_default_port(UdpTarget
& target
, unsigned short port
)
107 target
.get_address(tmp
);
108 if (tmp
.get_port() == 0) {
110 target
.set_address(tmp
);
114 int Snmp::run_transaction(Pdu
& pdu
, UdpTarget
& target
)
118 // 1. set unique id to match this packet on return
119 size_t hold_req_id
= req_id_
++;
120 set_request_id(&pdu
, hold_req_id
);
122 // 2. write request to agent
123 transaction
trans(pdu
, target
, iv_snmp_session_
);
125 // this call blocks while it attempts to retrieve agent response
127 if ((rc
= trans
.run()) < 0) {
128 last_transaction_status_
= rc
;
133 // verify this is the pdu we are after
134 if (pdu
.get_request_id() == hold_req_id
)
141 int Snmp::run_transaction(Pdu
& pdu
, UdpTarget
& target
, Snmp_Result
* cb
)
144 return run_transaction(pdu
, target
);
146 // 1. set unique id to match this packet on return
147 hold_req_id_
= req_id_
++;
148 set_request_id(&pdu
, hold_req_id_
);
152 // 2. write request to agent
153 transaction
* trans
= new transaction(pdu
, target
, iv_snmp_session_
);
154 return trans
->run(this);
157 void Snmp::result(transaction
*t
, int rc
)
160 // verify this is the pdu we are after
161 if (pdu_
->get_request_id() == hold_req_id_
)
163 last_transaction_status_
= rc
;
165 result_
->result(this, rc
);
173 result_
->result(this, rc
);
178 int Snmp::validate_args(const Pdu
& pdu
, const UdpTarget
& target
) const
180 // 0. check object status
181 if (construct_status_
!= SNMP_CLASS_SUCCESS
)
182 return construct_status_
;
184 // 1. check args passed
185 if ( !pdu
.valid() || !target
.valid() )
186 return SNMP_INVALID_ARGS
;
190 // SYNC API: write request to wire then wait for reply or timeout
191 int Snmp::get( Pdu
&pdu
, UdpTarget
&target
, Snmp_Result
* cb
)
193 ACE_TRACE("Snmp::get");
195 if ((rc
= validate_args(pdu
, target
)) != 0)
198 pdu
.set_type( sNMP_PDU_GET
);
199 check_default_port(target
);
200 return run_transaction(pdu
, target
, cb
);
203 int Snmp::get_next( Pdu
&pdu
, UdpTarget
&target
, Snmp_Result
* cb
)
205 ACE_TRACE("Snmp::get_next");
207 if ((rc
= validate_args(pdu
, target
)) != 0)
210 pdu
.set_type( sNMP_PDU_GETNEXT
);
211 check_default_port(target
);
212 return run_transaction(pdu
, target
, cb
);
215 int Snmp::set( Pdu
&pdu
, UdpTarget
&target
, Snmp_Result
* cb
)
217 ACE_TRACE("Snmp::set");
219 if ((rc
= validate_args(pdu
, target
)) != 0)
222 pdu
.set_type( sNMP_PDU_SET
);
223 check_default_port(target
);
224 return run_transaction(pdu
, target
, cb
);
227 // one way, best of luck, non-confirmed alert
228 int Snmp::trap( Pdu
&pdu
, UdpTarget
&target
)
230 ACE_TRACE("Snmp::trap");
232 if ((rc
= validate_args(pdu
, target
)) != 0)
235 pdu
.set_type( sNMP_PDU_V1TRAP
);
236 check_default_port(target
, DEF_TRAP_PORT
);
238 // 2. write request to agent
239 transaction
trans(pdu
, target
, iv_snmp_session_
);
240 if (trans
.send() > 0) // expect number of bytes sent on
243 last_transaction_status_
= SNMP_CLASS_INTERNAL_ERROR
;
247 // Allow host name to be overriden. Supplying a null pointer or zero
248 // length string removes the override.
249 void Snmp::override_host_name(const char* name
)
253 ACE_OS::strncpy(host_name_
, name
, MAXHOSTNAMELEN
);
254 host_name_
[MAXHOSTNAMELEN
-1] = 0;
261 // Returns the current host name in the supplied string.
262 void Snmp::get_host_name(char* name
, int len
)
266 if (ACE_OS::strlen(host_name_
) > 0)
268 ACE_OS::strncpy(name
, host_name_
, len
);
273 if (ACE_OS::hostname(name
, len
-1) == -1)
279 Snmp_Result::~Snmp_Result()
283 Snmp_Result::Snmp_Result()