]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/src/target.cpp
Initial adding
[ssmd.git] / 3rdparty / snmp++ / src / target.cpp
1 /*_############################################################################
2   _## 
3   _##  target.cpp  
4   _##
5   _##  SNMP++v3.2.25
6   _##  -----------------------------------------------
7   _##  Copyright (c) 2001-2010 Jochen Katz, Frank Fock
8   _##
9   _##  This software is based on SNMP++2.6 from Hewlett Packard:
10   _##  
11   _##    Copyright (c) 1996
12   _##    Hewlett-Packard Company
13   _##  
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. 
25   _##  
26   _##  Stuttgart, Germany, Thu Sep  2 00:07:47 CEST 2010 
27   _##  
28   _##########################################################################*/
29 /*===================================================================
30
31   Copyright (c) 1999
32   Hewlett-Packard Company
33
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.
44
45   T A R G E T . C P P
46
47   DESIGN + AUTHOR:  Peter E Mellquist
48
49   DESCRIPTION:      Target class defines target SNMP agents.
50
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"
55
56 #ifdef SNMP_PP_NAMESPACE
57 namespace Snmp_pp {
58 #endif
59
60 #define PUBLIC "public"
61
62 // class variables for default behavior control
63 unsigned long SnmpTarget::default_timeout = 100;
64 int SnmpTarget::default_retries = 1;
65
66 //----------------------------------------------------------------------
67 //--------[ Abstract SnmpTarget Member Functions ]----------------------
68 //----------------------------------------------------------------------
69
70 // get the address
71 int SnmpTarget::get_address(GenAddress &address) const
72 {
73   if (validity == false) return FALSE;
74
75   address = my_address;
76   return TRUE;
77 }
78
79 // set the address
80 int SnmpTarget::set_address(const Address &address)
81 {
82    my_address = address;
83    if ( my_address.valid())
84       validity = true;
85    else
86       validity = false;
87
88    return validity;
89 }
90
91 SnmpTarget* SnmpTarget::clone() const
92 {
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);
99   return res;
100 }
101
102 //=============[ int operator == SnmpTarget, SnmpTarget ]======================
103 // equivlence operator overloaded
104 int SnmpTarget::operator==( const SnmpTarget &rhs) const
105 {
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
111 }
112
113 // reset the object
114 void SnmpTarget::clear()
115 {
116   validity = false;
117   timeout  = default_timeout;
118   retries  = default_retries;
119   version  = version1;
120   ttype    = type_base;
121   my_address.clear();
122 }
123
124 //----------------------------------------------------------------------
125 //--------[ CTarget Member Functions ]----------------------------------
126 //----------------------------------------------------------------------
127
128 //---------[ CTarget::CTarget( void) ]----------------------------------
129 // CTarget constructor no args
130 CTarget::CTarget( void)
131   : read_community(PUBLIC), write_community(PUBLIC)
132 {
133   ttype = type_ctarget; // overwrite value set in SnmpTarget()
134 }
135
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)
143 {
144   ttype = type_ctarget; // overwrite value set in SnmpTarget()
145 }
146
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)
154 {
155   ttype = type_ctarget; // overwrite value set in SnmpTarget()
156 }
157
158
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)
163 {
164   ttype = type_ctarget; // overwrite value set in SnmpTarget()
165 }
166
167 //-----------[ CTarget:: CTarget( const CTarget &target) ]-------
168 // CTarget constructor with args
169 CTarget::CTarget( const CTarget &target)
170 {
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;
179 }
180
181 //----------[ CTarget::resolve_to_V1 ]---------------------------------
182 // resolve entity
183 // common interface for Community based targets
184 int CTarget::resolve_to_C ( OctetStr &read_comm,
185                             OctetStr &write_comm,
186                             GenAddress &address,
187                             unsigned long &t,
188                             int &r,
189                             unsigned char &v) const
190 {
191    // if the target is invalid then return false
192    if ( !validity)
193      return FALSE;
194
195    read_comm = read_community;
196    write_comm = write_community;
197    address = my_address;
198
199    t = timeout;
200    r = retries;
201    v = version;
202
203    return TRUE;
204 }
205
206 // overloaded assignment
207 CTarget& CTarget::operator=( const CTarget& target)
208 {
209   if (this == &target) return *this;  // check for self assignment
210
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;
218   return *this;
219 }
220
221 //=============[ int operator == CTarget, CTarget ]==========================
222 // equivlence operator overloaded
223 int CTarget::operator==( const CTarget &rhs) const
224 {
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;
229   return 1; // equal
230 }
231
232 // reset the object
233 void CTarget::clear()
234 {
235   SnmpTarget::clear();
236   read_community.clear();
237   write_community.clear();
238   ttype = type_ctarget; // overwrite value set in SnmpTarget::clear()
239 }
240
241 //----------------------------------------------------------------------
242 //--------[ UTarget Member Functions ]----------------------------------
243 //----------------------------------------------------------------------
244
245 //---------[ UTarget::UTarget( void) ]----------------------------------
246 // UTarget constructor no args
247 UTarget::UTarget()
248   : security_name(INITIAL_USER),
249 #ifdef _SNMPv3
250   security_model(SNMP_SECURITY_MODEL_USM), engine_id("")
251 #else
252   security_model(SNMP_SECURITY_MODEL_V1)
253 #endif
254 {
255 #ifdef _SNMPv3
256   version = version3;
257 #endif
258   ttype = type_utarget;
259 }
260
261 //-----------[ UTarget:: UTarget ]-------------------------------------
262 // UTarget constructor with args
263 UTarget::UTarget( const Address &address,
264                   const char *sec_name,
265                   const int sec_model)
266   : SnmpTarget(address), security_name(sec_name), security_model(sec_model)
267 #ifdef _SNMPv3
268     ,engine_id("")
269 #endif
270 {
271 #ifdef _SNMPv3
272   version = version3;
273 #endif
274   ttype = type_utarget;
275 }
276
277 //-----------[ UTarget:: UTarget ]-----------------------------------
278 // UTarget constructor with args
279 UTarget::UTarget( const Address &address,
280                   const OctetStr &sec_name,
281                   const int sec_model)
282   : SnmpTarget(address), security_name(sec_name), security_model(sec_model)
283 #ifdef _SNMPv3
284     ,engine_id("")
285 #endif
286 {
287 #ifdef _SNMPv3
288   version = version3;
289 #endif
290   ttype = type_utarget;
291 }
292
293 //-----------[ UTarget:: UTarget( Address &address) ]--------------
294 // UTarget constructor with args
295 UTarget::UTarget( const Address &address)
296   : SnmpTarget(address), security_name(INITIAL_USER),
297 #ifdef _SNMPv3
298     security_model(SNMP_SECURITY_MODEL_USM), engine_id("")
299 #else
300     security_model(SNMP_SECURITY_MODEL_V1)
301 #endif
302 {
303 #ifdef _SNMPv3
304   version = version3;
305 #endif
306   ttype = type_utarget;
307 }
308
309 //-----------[ UTarget:: UTarget( const UTarget &target) ]-------
310 // UTarget constructor with args
311 UTarget::UTarget( const UTarget &target)
312 {
313 #ifdef _SNMPv3
314   engine_id = target.engine_id;
315 #endif
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;
324 }
325
326
327 // set the address
328 int UTarget::set_address(const Address &address)
329 {
330 #ifdef _SNMPv3
331    engine_id = ""; // delete engine_id
332 #endif
333    return SnmpTarget::set_address(address);
334 }
335
336 int UTarget::resolve_to_U( OctetStr&  sec_name,
337                            int &sec_model,
338                            GenAddress &address,
339                            unsigned long &t,
340                            int &r,
341                            unsigned char &v) const
342 {
343   // if the target is invalid then return false
344   if ( !validity)
345     return FALSE;
346
347   sec_name = security_name;
348   sec_model = security_model;
349   address = my_address;
350
351   t = timeout;
352   r = retries;
353   v = version;
354
355   return TRUE;
356 }
357
358 // overloaded assignment
359 UTarget& UTarget::operator=(const UTarget& target)
360 {
361   if (this == &target) return *this;  // check for self assignment
362
363   timeout = target.timeout;
364   retries = target.retries;
365
366 #ifdef _SNMPv3
367   engine_id = target.engine_id;
368 #endif
369   security_name = target.security_name;
370   security_model = target.security_model;
371   version = target.version;
372
373   validity = target.validity;
374   my_address = target.my_address;
375
376   return *this;
377 }
378
379 //=============[ int operator == UTarget, UTarget ]==========================
380 // equivlence operator overloaded
381 int UTarget::operator==(const UTarget &rhs) const
382 {
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
389 }
390
391 // reset the object
392 void UTarget::clear()
393 {
394   SnmpTarget::clear();
395   security_name = INITIAL_USER;
396 #ifdef _SNMPv3
397   security_model = SNMP_SECURITY_MODEL_USM;
398   engine_id.clear();
399   version = version3;
400 #else
401   security_model = SNMP_SECURITY_MODEL_V1;
402 #endif
403   ttype = type_utarget;
404 }
405
406 #ifdef SNMP_PP_NAMESPACE
407 }; // end of namespace Snmp_pp
408 #endif