1 /*_############################################################################
6 _## -----------------------------------------------
7 _## Copyright (c) 2001-2010 Jochen Katz, Frank Fock
9 _## This software is based on SNMP++2.6 from Hewlett Packard:
11 _## Copyright (c) 1996
12 _## Hewlett-Packard Company
14 _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15 _## Permission to use, copy, modify, distribute and/or sell this software
16 _## and/or its documentation is hereby granted without fee. User agrees
17 _## to display the above copyright notice and this license notice in all
18 _## copies of the software and any documentation of the software. User
19 _## agrees to assume all liability for the use of the software;
20 _## Hewlett-Packard and Jochen Katz make no representations about the
21 _## suitability of this software for any purpose. It is provided
22 _## "AS-IS" without warranty of any kind, either express or implied. User
23 _## hereby grants a royalty-free license to any and all derivatives based
24 _## upon this software code base.
26 _## Stuttgart, Germany, Thu Sep 2 00:07:47 CEST 2010
28 _##########################################################################*/
29 /*===================================================================
32 Hewlett-Packard Company
34 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
35 Permission to use, copy, modify, distribute and/or sell this software
36 and/or its documentation is hereby granted without fee. User agrees
37 to display the above copyright notice and this license notice in all
38 copies of the software and any documentation of the software. User
39 agrees to assume all liability for the use of the software; Hewlett-Packard
40 makes no representations about the suitability of this software for any
41 purpose. It is provided "AS-IS" without warranty of any kind,either express
42 or implied. User hereby grants a royalty-free license to any and all
43 derivatives based upon this software code base.
47 DESIGN + AUTHOR: Peter E Mellquist
49 DESCRIPTION: Target class defines target SNMP agents.
51 =====================================================================*/
52 char target_cpp_version[]="#(@) SNMP++ $Id: target.cpp 1686 2009-11-24 13:47:44Z katz $";
53 #include "snmp_pp/target.h"
54 #include "snmp_pp/v3.h"
56 #ifdef SNMP_PP_NAMESPACE
60 #define PUBLIC "public"
62 // class variables for default behavior control
63 unsigned long SnmpTarget::default_timeout = 100;
64 int SnmpTarget::default_retries = 1;
66 //----------------------------------------------------------------------
67 //--------[ Abstract SnmpTarget Member Functions ]----------------------
68 //----------------------------------------------------------------------
71 int SnmpTarget::get_address(GenAddress &address) const
73 if (validity == false) return FALSE;
80 int SnmpTarget::set_address(const Address &address)
83 if ( my_address.valid())
91 SnmpTarget* SnmpTarget::clone() const
93 GenAddress addr = my_address;
94 SnmpTarget* res = new SnmpTarget;
95 res->set_timeout(timeout);
96 res->set_retry(retries);
97 res->set_address(addr);
98 res->set_version(version);
102 //=============[ int operator == SnmpTarget, SnmpTarget ]======================
103 // equivlence operator overloaded
104 int SnmpTarget::operator==( const SnmpTarget &rhs) const
106 if (my_address != rhs.my_address) return 0;
107 if (version != rhs.version) return 0;
108 if (timeout != rhs.timeout) return 0;
109 if (retries != rhs.retries) return 0;
110 return 1; // they are equal
114 void SnmpTarget::clear()
117 timeout = default_timeout;
118 retries = default_retries;
124 //----------------------------------------------------------------------
125 //--------[ CTarget Member Functions ]----------------------------------
126 //----------------------------------------------------------------------
128 //---------[ CTarget::CTarget( void) ]----------------------------------
129 // CTarget constructor no args
130 CTarget::CTarget( void)
131 : read_community(PUBLIC), write_community(PUBLIC)
133 ttype = type_ctarget; // overwrite value set in SnmpTarget()
136 //-----------[ CTarget:: CTarget ]-------------------------------------
137 // CTarget constructor with args
138 CTarget::CTarget(const Address &address,
139 const char *read_community_name,
140 const char *write_community_name)
141 : SnmpTarget(address),
142 read_community(read_community_name), write_community(write_community_name)
144 ttype = type_ctarget; // overwrite value set in SnmpTarget()
147 //-----------[ CTarget:: CTarget ]-----------------------------------
148 // CTarget constructor with args
149 CTarget::CTarget(const Address &address,
150 const OctetStr& read_community_name,
151 const OctetStr& write_community_name)
152 : SnmpTarget(address),
153 read_community(read_community_name), write_community(write_community_name)
155 ttype = type_ctarget; // overwrite value set in SnmpTarget()
159 //-----------[ CTarget:: CTarget( Address &address) ]--------------
160 // CTarget constructor with args
161 CTarget::CTarget(const Address &address)
162 : SnmpTarget(address), read_community(PUBLIC), write_community(PUBLIC)
164 ttype = type_ctarget; // overwrite value set in SnmpTarget()
167 //-----------[ CTarget:: CTarget( const CTarget &target) ]-------
168 // CTarget constructor with args
169 CTarget::CTarget( const CTarget &target)
171 read_community = target.read_community;
172 write_community = target.write_community;
173 my_address = target.my_address;
174 timeout = target.timeout;
175 retries = target.retries;
176 version = target.version;
177 validity = target.validity;
178 ttype = type_ctarget;
181 //----------[ CTarget::resolve_to_V1 ]---------------------------------
183 // common interface for Community based targets
184 int CTarget::resolve_to_C ( OctetStr &read_comm,
185 OctetStr &write_comm,
189 unsigned char &v) const
191 // if the target is invalid then return false
195 read_comm = read_community;
196 write_comm = write_community;
197 address = my_address;
206 // overloaded assignment
207 CTarget& CTarget::operator=( const CTarget& target)
209 if (this == &target) return *this; // check for self assignment
211 timeout = target.timeout;
212 retries = target.retries;
213 read_community = target.read_community;
214 write_community = target.write_community;
215 validity = target.validity;
216 my_address = target.my_address;
217 version = target.version;
221 //=============[ int operator == CTarget, CTarget ]==========================
222 // equivlence operator overloaded
223 int CTarget::operator==( const CTarget &rhs) const
225 if (SnmpTarget::operator==(rhs) == 0) return 0;
226 // need to compare all the members of a CTarget
227 if (read_community != rhs.read_community) return 0;
228 if (write_community != rhs.write_community) return 0;
233 void CTarget::clear()
236 read_community.clear();
237 write_community.clear();
238 ttype = type_ctarget; // overwrite value set in SnmpTarget::clear()
241 //----------------------------------------------------------------------
242 //--------[ UTarget Member Functions ]----------------------------------
243 //----------------------------------------------------------------------
245 //---------[ UTarget::UTarget( void) ]----------------------------------
246 // UTarget constructor no args
248 : security_name(INITIAL_USER),
250 security_model(SNMP_SECURITY_MODEL_USM), engine_id("")
252 security_model(SNMP_SECURITY_MODEL_V1)
258 ttype = type_utarget;
261 //-----------[ UTarget:: UTarget ]-------------------------------------
262 // UTarget constructor with args
263 UTarget::UTarget( const Address &address,
264 const char *sec_name,
266 : SnmpTarget(address), security_name(sec_name), security_model(sec_model)
274 ttype = type_utarget;
277 //-----------[ UTarget:: UTarget ]-----------------------------------
278 // UTarget constructor with args
279 UTarget::UTarget( const Address &address,
280 const OctetStr &sec_name,
282 : SnmpTarget(address), security_name(sec_name), security_model(sec_model)
290 ttype = type_utarget;
293 //-----------[ UTarget:: UTarget( Address &address) ]--------------
294 // UTarget constructor with args
295 UTarget::UTarget( const Address &address)
296 : SnmpTarget(address), security_name(INITIAL_USER),
298 security_model(SNMP_SECURITY_MODEL_USM), engine_id("")
300 security_model(SNMP_SECURITY_MODEL_V1)
306 ttype = type_utarget;
309 //-----------[ UTarget:: UTarget( const UTarget &target) ]-------
310 // UTarget constructor with args
311 UTarget::UTarget( const UTarget &target)
314 engine_id = target.engine_id;
316 security_name = target.security_name;
317 security_model = target.security_model;
318 my_address = target.my_address;
319 timeout = target.timeout;
320 retries = target.retries;
321 version = target.version;
322 validity = target.validity;
323 ttype = type_utarget;
328 int UTarget::set_address(const Address &address)
331 engine_id = ""; // delete engine_id
333 return SnmpTarget::set_address(address);
336 int UTarget::resolve_to_U( OctetStr& sec_name,
341 unsigned char &v) const
343 // if the target is invalid then return false
347 sec_name = security_name;
348 sec_model = security_model;
349 address = my_address;
358 // overloaded assignment
359 UTarget& UTarget::operator=(const UTarget& target)
361 if (this == &target) return *this; // check for self assignment
363 timeout = target.timeout;
364 retries = target.retries;
367 engine_id = target.engine_id;
369 security_name = target.security_name;
370 security_model = target.security_model;
371 version = target.version;
373 validity = target.validity;
374 my_address = target.my_address;
379 //=============[ int operator == UTarget, UTarget ]==========================
380 // equivlence operator overloaded
381 int UTarget::operator==(const UTarget &rhs) const
383 if (SnmpTarget::operator==(rhs) == 0) return 0;
384 // need to compare all the members of a UTarget
385 // but don`t compare engine_id
386 if (security_name != rhs.security_name) return 0;
387 if (security_model != rhs.security_model) return 0;
388 return 1; // they are equal
392 void UTarget::clear()
395 security_name = INITIAL_USER;
397 security_model = SNMP_SECURITY_MODEL_USM;
401 security_model = SNMP_SECURITY_MODEL_V1;
403 ttype = type_utarget;
406 #ifdef SNMP_PP_NAMESPACE
407 }; // end of namespace Snmp_pp