2 //=============================================================================
6 * Target class defines target SNMP agents.
8 * @author Peter E MellquistMichael R MacFaden mrm@cisco.com - rework & ACE port
10 //=============================================================================
12 /*===================================================================
14 Hewlett-Packard Company
16 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
17 Permission to use, copy, modify, distribute and/or sell this software
18 and/or its documentation is hereby granted without fee. User agrees
19 to display the above copyright notice and this license notice in all
20 copies of the software and any documentation of the software. User
21 agrees to assume all liability for the use of the software; Hewlett-Packard
22 makes no representations about the suitability of this software for any
23 purpose. It is provided "AS-IS without warranty of any kind,either express
24 or implied. User hereby grants a royalty-free license to any and all
25 derivatives based upon this software code base.
26 =====================================================================*/
28 #include "asnmp/target.h"
29 #include "ace/OS_NS_stdio.h"
31 //----------------------------------------------------------------------
32 //--------[ Class default values ]----------------------
33 //----------------------------------------------------------------------
34 u_long
SnmpTarget::default_timeout_
= DEF_TIMEOUT
;
35 int SnmpTarget::default_retries_
= DEF_RETRIES
;
36 u_long
SnmpTarget::default_max_pdu_size_
= DEF_MAX_SNMP_PACKET
;
37 snmp_version
SnmpTarget::default_version_
= version1
;
39 OctetStr
UdpTarget::default_rd_community_(READ_COMM_STR
);
40 OctetStr
UdpTarget::default_wr_community_(WRITE_COMM_STR
);
42 //----------------------------------------------------------------------
43 //--------[ Abstract SnmpTarget Member Functions ]----------------------
44 //----------------------------------------------------------------------
45 SnmpTarget::SnmpTarget():
46 validity_(0), timeout_(default_timeout_
), retries_(default_retries_
),
47 max_pdu_size_(default_max_pdu_size_
), version_(default_version_
)
51 // return validity of target
52 int SnmpTarget::valid() const
57 // allow destruction of derived classes
58 SnmpTarget::~SnmpTarget()
63 void SnmpTarget::set_timeout( const u_long t
)
68 // change the default timeout
69 void SnmpTarget::set_default_timeout( const u_long t
)
74 // set the retry value
75 void SnmpTarget::set_retry( const int r
)
80 // change the default retries
81 void SnmpTarget::set_default_retry( const int r
)
86 void SnmpTarget:: set_max_pdu_size(const u_long max_pdu_sz
)
88 max_pdu_size_
= max_pdu_sz
;
92 void SnmpTarget::set_default_max_pdu_size(const u_long max_pdu_sz
)
94 default_max_pdu_size_
= max_pdu_sz
;
97 void SnmpTarget::set_version( const snmp_version v
)
102 void SnmpTarget::set_default_version( const snmp_version v
)
104 default_version_
= v
;
107 snmp_version
SnmpTarget::get_version() const
112 snmp_version
SnmpTarget::get_default_version() const
114 return default_version_
;
117 u_long
SnmpTarget::get_max_pdu_size() const
119 return max_pdu_size_
;
122 // get the retry value
123 int SnmpTarget::get_retry() const
128 // get the retry value
129 int SnmpTarget::get_default_retry() const
131 return default_retries_
;
135 u_long
SnmpTarget::get_timeout() const
140 SnmpTarget
& SnmpTarget::operator=(const SnmpTarget
& lhs
)
145 validity_
= lhs
.validity_
;
146 timeout_
= lhs
.timeout_
;
147 retries_
= lhs
.retries_
;
148 max_pdu_size_
=lhs
.max_pdu_size_
;
149 version_
= lhs
.version_
;
153 bool operator==(const SnmpTarget
& lhs
, const SnmpTarget
& rhs
)
155 if (lhs
.timeout_
!= rhs
.timeout_
)
158 if (lhs
.retries_
!= rhs
.retries_
)
161 if (lhs
.max_pdu_size_
!= rhs
.max_pdu_size_
)
164 if (lhs
.version_
!= rhs
.version_
)
171 //----------------------------------------------------------------------
172 //--------[ IpTarget Member Functions ]----------------------------------
173 //----------------------------------------------------------------------
176 //---------[ UdpTarget::UdpTarget() ]----------------------------------
178 UdpTarget::UdpTarget()
181 read_community_
= default_rd_community_
; // assign by init fails
182 write_community_
= default_wr_community_
;
185 UdpTarget::UdpTarget(const UdpAddress
& udp
) : udp_address_(udp
)
187 if (udp_address_
.valid())
189 read_community_
= default_rd_community_
;
190 write_community_
= default_wr_community_
;
193 // - copy constructor can be synthesised by compiler
195 //-----------[ UdpTarget::~UdpTarget() ]--------------------------------
196 UdpTarget::~UdpTarget()
200 //-----------[ UdpTarget::clone() ]--------------------------------
201 SnmpTarget
* UdpTarget::clone() const
203 return (SnmpTarget
*) new UdpTarget(*this);
207 void UdpTarget::set_default_read_community(const OctetStr
& rd_community
)
209 default_rd_community_
= rd_community
;
212 void UdpTarget::set_default_write_community(const OctetStr
& wr_community
)
214 default_wr_community_
= wr_community
;
218 // get the read community name as an u_char and len
219 void UdpTarget::get_read_community( OctetStr
& read_community_oct
) const
221 read_community_oct
= read_community_
;
224 //---------[ UdpTarget::set_getcommunity ]---------------------------------
225 // set the read community name
226 void UdpTarget::set_read_community( const OctetStr
& new_read_community
)
228 read_community_
= new_read_community
;
231 //---------[ UdpTarget::get_writecommunity ]----------------------------
232 // get the write community
233 void UdpTarget::get_write_community( OctetStr
&write_community_oct
) const
235 write_community_oct
= write_community_
;
238 //-----------[ UdpTarget::set_writecommunity ]---------------------------
239 // set the write community
240 void UdpTarget::set_write_community( const OctetStr
& write_community_oct
)
242 write_community_
= write_community_oct
;
245 //------------[ Address& UdpTarget::get_address() ]---------------------
247 void UdpTarget::get_address( UdpAddress
&address
) const
249 address
= udp_address_
;
253 //-------------[ UdpTarget::set_address ]--------------------------------
255 int UdpTarget::set_address( UdpAddress
&udp_address
)
257 udp_address_
= udp_address
;
258 if ( udp_address_
.valid())
266 // overloaded assignment
267 UdpTarget
& UdpTarget::operator=( const UdpTarget
& lhs
)
273 *((SnmpTarget
*) this) = *((SnmpTarget
*)&lhs
);
275 udp_address_
= lhs
.udp_address_
;
276 read_community_
= lhs
.read_community_
;
277 write_community_
= lhs
.write_community_
;
278 validity_
= lhs
.validity_
;
283 const char *UdpTarget::to_string()
285 ACE_OS::sprintf(output_buffer_
,"UdpTarget: [ valid: %d addr: %s rd: %s wr: %s \
286 ver: %d, timeout: %d, retries: %d max_pdu_size: %d]",
287 valid(), udp_address_
.to_string(), read_community_
.to_string(),
288 write_community_
.to_string(), version_
, (int) timeout_
,
289 retries_
, max_pdu_size_
);
291 return output_buffer_
;
294 //=============[ int operator == UdpTarget, UdpTarget ]===============
295 // equivlence operator overloaded
296 bool operator==( const UdpTarget
&lhs
,const UdpTarget
&rhs
)
298 // need to compare all the members of a UdpTarget
299 if ( lhs
.read_community_
!= rhs
.read_community_
)
302 if ( lhs
.write_community_
!= rhs
.write_community_
)
305 if ( lhs
.udp_address_
!= rhs
.udp_address_
)
308 if ( lhs
.timeout_
!= rhs
.timeout_
)
311 if ( lhs
.retries_
!= rhs
.retries_
)
314 return true; // they are equal