1 mike 1.2 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 kumpf 1.10 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
|
5 mike 1.2 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 kumpf 1.10 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10 mike 1.2 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
|
13 kumpf 1.10 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
14 mike 1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
16 kumpf 1.10 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
19 mike 1.2 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 //==============================================================================
23 //
24 // Author: Nitin Upasani, Hewlett-Packard Company (Nitin_Upasani@hp.com)
25 //
|
26 kumpf 1.12 // Modified By: Yi Zhou, Hewlett-Packard Company (yi_zhou@hp.com)
|
27 mike 1.2 //
28 //%/////////////////////////////////////////////////////////////////////////////
29
|
30 kumpf 1.3 #include <Pegasus/Common/System.h>
|
31 kumpf 1.12 #include <Pegasus/Common/ArrayInternal.h>
|
32 kumpf 1.8 #include <Pegasus/Common/Destroyer.h>
|
33 kumpf 1.12 #include <Pegasus/Common/InternalException.h>
|
34 mike 1.2 #include "snmpDeliverTrap_emanate.h"
|
35 kumpf 1.12
36 // EMANATE specific declarations and entry points - MUST be in begining
37 // and outside NAMESPACE.
38
39 // master agent needs these two declarations for communication with sub-agent.
40 // Following two declarations must be in the CODE.
41
42 #include <prnt_lib.h> // MUST be at the end in include list.
|
43 mike 1.2
|
44 kumpf 1.3 static char *sr_filename = __FILE__;
45
46 IPCFunctionP IPCfp; /* IPC functions pointer */
47
|
48 kumpf 1.12 // This code would normally be generated by Emanate from defined MIB objects.
49 // Since we do not have MIB objects defined, just defined here to load
50 // subagent as library. OidList[] provides objects for entry point to master agent.
|
51 kumpf 1.3
52 // The objects internal to the agent
53 ObjectInfo OidList[] =
54 {
55 { { 0, NULL },
56 #ifndef LIGHT
57 NULL,
58 #endif /* LIGHT */
59 0, 0, 0, 0,
60 NULL, NULL }
61 };
62
|
63 kumpf 1.12 // This code would normally be generated by Emanate in k_* routine from defined MIB objects.
64 // Since we do not have MIB objects defined, just define here to pass compile and
65 // enable entry point for master agent to start communication with library.
|
66 kumpf 1.3
67 // Called by the master agent during initialization */
68 int k_initialize()
69 {
70 return 1;
71 }
|
72 kumpf 1.9
|
73 kumpf 1.12 // END EMANATE specific declarations.
74
|
75 kumpf 1.9 PEGASUS_NAMESPACE_BEGIN
76
77 PEGASUS_USING_STD;
|
78 kumpf 1.3
79 snmpDeliverTrap_emanate::snmpDeliverTrap_emanate()
80 {
81
82 }
83
84 snmpDeliverTrap_emanate::~snmpDeliverTrap_emanate()
85 {
86
87 }
88
|
89 kumpf 1.12
90 // initialize sub-agent
91
92 // This also defines the communication protocol to be used between master
93 // and sub-agent.
94
|
95 kumpf 1.3 void snmpDeliverTrap_emanate::initialize()
96 {
97 #ifndef SR_UDS_IPC
98 InitIPCArrayTCP(&IPCfp);
99 #else /* SR_UDS_IPC */
100 InitIPCArrayUDS(&IPCfp);
101 #endif /* SR_UDS_IPC */
102
103 if(InitSubagent() == -1)
104 {
|
105 kumpf 1.12 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
106 _MSG_INITSUBAGENT_FAILED);
|
107 kumpf 1.3 }
108 }
109
110 void snmpDeliverTrap_emanate::deliverTrap(
|
111 kumpf 1.12 const String& trapOid,
112 const String& securityName,
113 const String& targetHost,
114 const Uint16& targetHostFormat,
115 const String& otherTargetHostFormat,
116 const Uint32& portNumber,
117 const Uint16& snmpVersion,
118 const String& engineID,
119 Array<String>& vbOids,
120 Array<String>& vbTypes,
121 Array<String>& vbValues)
|
122 mike 1.2 {
|
123 kumpf 1.12 VarBind *vbhead = NULL;
|
124 kumpf 1.3 VarBind *vb = NULL;
|
125 kumpf 1.12 VarBind *vblast = NULL;
|
126 kumpf 1.3
127 OID *object = NULL;
128
|
129 kumpf 1.12 // Initialize with Master agent
130
131 initialize();
132
133 // Translate a string into an OID
134 OID *sendtrapOid = MakeOIDFromDot(trapOid.getCString());
135
136 if (sendtrapOid == NULL)
137 {
138 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
139 _MSG_INVALID_TRAPOID);
140 }
141
142 // Destination : convert targetHost into Transport
|
143 kumpf 1.5
|
144 kumpf 1.12 CString trap_dest = targetHost.getCString();
|
145 kumpf 1.5
146 TransportInfo global_ti;
147 global_ti.type = SR_IP_TRANSPORT;
148
|
149 kumpf 1.12 switch (targetHostFormat)
150 {
151 case _HOST_NAME:
152 {
153 char * ipAddr = _getIPAddress(trap_dest);
154
155 if (ipAddr == NULL)
156 {
157 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
158 _MSG_DESTINATION_NOT_FOUND);
159 }
160 global_ti.t_ipAddr = inet_addr(trap_dest);
161 break;
162 }
163 case _IPV4_ADDRESS:
164 {
165 global_ti.t_ipAddr = inet_addr(trap_dest);
166 break;
167 }
168 default:
169 {
170 kumpf 1.12 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_NOT_SUPPORTED,
171 _MSG_TARGETHOSTFORMAT_NOT_SUPPORTED);
172 break;
173 }
174 }
175
176 global_ti.t_ipPort = htons((unsigned short)portNumber);
177
178 // Community Name, default is public
179 CString _community;
180 if (securityName.size() == 0)
181 {
182 String community;
183 community.assign("public");
184 _community = community.getCString();
185 }
186 else
187 {
188 _community = securityName.getCString();
189 }
190
191 kumpf 1.12 OctetString* community_name = MakeOctetStringFromText(_community);
192
193 if (community_name == NULL)
194 {
195 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
196 _MSG_INVALID_SECURITY_NAME);
197 }
|
198 kumpf 1.5
|
199 kumpf 1.3 // getting IP address of the host
|
200 kumpf 1.12
201 CString hostname = System::getHostName().getCString();
202 char* IP_string = _getIPAddress(hostname);
203
|
204 kumpf 1.3 // formatting agent(host) address into OctetString format
|
205 kumpf 1.12
|
206 kumpf 1.3 OctetString* agent_addr;
207
208 SR_INT32 s1, s2, s3, s4;
209 SR_UINT32 ipaddr;
210
211 // pull out each of the 4 octet values from IP address
|
212 kumpf 1.12
|
213 kumpf 1.3 sscanf(IP_string,"%d.%d.%d.%d", &s1, &s2, &s3, &s4);
214
|
215 kumpf 1.12 // validate the values for s1, s2, s3, and s4 to make sure values are
|
216 kumpf 1.3 // between 0 and 255
|
217 kumpf 1.12 if (!_isValidOctet(s1) || !_isValidOctet(s2) ||
218 !_isValidOctet(s3) || !_isValidOctet(s4))
219 {
220 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
221 _MSG_INVALID_OCTET_VALUE);
222 }
|
223 kumpf 1.3
224 // create an empty 4 length OctetString
|
225 kumpf 1.12
|
226 kumpf 1.3 agent_addr = MakeOctetString(NULL,4);
|
227 kumpf 1.12
228 if (agent_addr == NULL)
229 {
230 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
231 _MSG_CREATE_OCTET_FAILED);
232 }
|
233 kumpf 1.3
234 // fill in values for OctetString
|
235 kumpf 1.12
|
236 kumpf 1.3 agent_addr->octet_ptr[0] = (unsigned char)s1;
237 agent_addr->octet_ptr[1] = (unsigned char)s2;
238 agent_addr->octet_ptr[2] = (unsigned char)s3;
239 agent_addr->octet_ptr[3] = (unsigned char)s4;
240
|
241 kumpf 1.4 // specTrap from trapOid.
|
242 kumpf 1.12
|
243 kumpf 1.5 SR_INT32 genTrap = 0;
244 SR_INT32 specTrap = 0;
|
245 kumpf 1.3
|
246 kumpf 1.5 OID* enterpriseOid ;
247
248 Array<String> standard_traps;
249
250 standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
251 standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
252 standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
253 standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
254 standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
255 standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
256
257 Array<String> oids;
258 String tmpoid = trapOid;
259
260 while(tmpoid.find(".") != PEG_NOT_FOUND)
261 {
262 oids.append(tmpoid.subString(0, tmpoid.find(".")));
263 tmpoid = tmpoid.subString(tmpoid.find(".") + 1);
264 }
|
265 kumpf 1.12
|
266 kumpf 1.5 oids.append(tmpoid);
267
268 String ent;
269 if (Contains(standard_traps, trapOid))
270 {
|
271 kumpf 1.12 //
272 // if the trapOid is one of the standard traps,
273 // then the SNMPV1 enterprise parameter must be set
274 // to the value of the trapOid, the generic-trap
275 // parameter must be set to one of (0 - 5), and the
276 // specific-trap parameter must be set to 0
277 //
|
278 kumpf 1.5
279 enterpriseOid = sendtrapOid;
|
280 kumpf 1.12
281 // the generic trap is last sub-identifier of the
282 // trapOid minus 1
283 genTrap = atoi(oids[oids.size() - 1].getCString()) - 1;
284 specTrap = 0;
|
285 kumpf 1.5 }
286 else
287 {
|
288 kumpf 1.12 //
289 // if the trapOid is not one of the standard traps:
290 // then 1) the generic-trap parameter must be set to 6,
291 // 2) if the next-to-last sub-identifier of the
292 // trapOid is zero, then the SNMPV1 enterprise
293 // parameter is the trapOid with the last 2
294 // sub-identifiers removed, otherwise, the
295 // SNMPV1 enterprise parameter is the trapOid
296 // with the last sub-identifier removed;
297 // 3) the SNMPv1 specific-trap parameter is the last
298 // sub-identifier of the trapOid;
299 //
300
|
301 kumpf 1.5 genTrap = 6;
|
302 mike 1.2
|
303 kumpf 1.11 specTrap = atoi(oids[oids.size()-1].getCString());
|
304 mike 1.2
|
305 kumpf 1.12 ent = oids[0];
306 for (Uint8 i = 1; i < oids.size()-2; i++)
307 {
308 ent = ent + "." + oids[i];
309 }
310
311 if (oids[oids.size()-2] != "0")
312 {
313 ent = ent + "." + oids[oids.size()-2];
314 }
315
316 enterpriseOid = MakeOIDFromDot(ent.getCString());
317
318 if (enterpriseOid == NULL)
319 {
320 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
321 _MSG_INVALID_ENTERPRISEOID);
322 }
|
323 kumpf 1.5 }
324
|
325 kumpf 1.12 // creates VarBind
|
326 kumpf 1.4 for(Uint32 i = 0; i < vbOids.size(); i++)
|
327 mike 1.2 {
|
328 kumpf 1.12 CString _vbOid = vbOids[i].getCString();
329 CString _vbValue = vbValues[i].getCString();
330
331 if ((object = MakeOIDFromDot(_vbOid)) == NULL)
|
332 kumpf 1.3 {
|
333 kumpf 1.12 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
334 _MSG_INVALID_PROPERTYOID);
|
335 kumpf 1.3 }
336
|
337 kumpf 1.12 if (String::equalNoCase(vbTypes[i], "OctetString"))
338 {
339 OctetString* value;
340
341 value = CloneOctetString(MakeOctetStringFromText(_vbValue));
342 if (value == NULL)
343 {
344 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
345 _MSG_INVALID_PROPERTYVALUE);
346 }
347
348 if ((vb = MakeVarBindWithValue(object,
349 (OID *) NULL,
350 OCTET_PRIM_TYPE,
351 value)) == NULL)
352 {
353 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
354 _MSG_MAKE_VARBIND_FAILED_FOR_OCTET_PRIM_TYPE);
355 }
356 }
357 else if (String::equalNoCase(vbTypes[i], "OID"))
358 kumpf 1.12 {
359 void* value = MakeOIDFromDot(_vbValue);
360 if (value == NULL)
|
361 kumpf 1.4 {
|
362 kumpf 1.12 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
363 _MSG_INVALID_PROPERTYVALUE);
|
364 kumpf 1.4 }
|
365 kumpf 1.12
366 if ((vb = MakeVarBindWithValue(object,
367 (OID *) NULL,
368 OBJECT_ID_TYPE,
369 value)) == NULL)
|
370 kumpf 1.3 {
|
371 kumpf 1.12 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
372 _MSG_MAKE_VARBIND_FAILED_FOR_OBJECT_ID_TYPE);
|
373 kumpf 1.3 }
|
374 kumpf 1.12 }
375 else
376 {
377 int vbvalue = atoi(_vbValue);
|
378 kumpf 1.6 void* value = &vbvalue;
|
379 kumpf 1.3
|
380 kumpf 1.12 if (value == NULL)
|
381 kumpf 1.4 {
|
382 kumpf 1.12 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
383 _MSG_INVALID_PROPERTYVALUE);
|
384 kumpf 1.4 }
|
385 kumpf 1.12
386 if ((vb = MakeVarBindWithValue(object,
387 (OID *) NULL,
388 INTEGER_TYPE,
389 value)) == NULL)
|
390 mike 1.2 {
|
391 kumpf 1.12 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED,
392 _MSG_MAKE_VARBIND_FAILED_FOR_INTEGER_TYPE);
|
393 mike 1.2 }
|
394 kumpf 1.12 }
395
|
396 kumpf 1.4 if (i == 0)
397 {
|
398 kumpf 1.12 vbhead = vb;
399 vblast = vb;
|
400 kumpf 1.4 }
401 else
402 {
|
403 kumpf 1.12 vblast->next_var = vb;
404 vblast = vblast->next_var;
|
405 kumpf 1.4 }
406
|
407 kumpf 1.3 }
|
408 kumpf 1.5
|
409 kumpf 1.12 vblast->next_var = NULL;
|
410 kumpf 1.3
|
411 kumpf 1.12 // Now send the trap
412 switch (snmpVersion)
|
413 kumpf 1.3 {
|
414 kumpf 1.12 case _SNMPv1_TRAP:
415 {
416 SendNotificationToDestSMIv1Params(
417 1, // notifyType - TRAP
|
418 kumpf 1.5 genTrap, // genTrap
419 specTrap, // specTrap
|
420 kumpf 1.3 enterpriseOid, // enterprise
421 agent_addr, // agent_addr
|
422 kumpf 1.12 vbhead, // vb
|
423 kumpf 1.3 NULL, // contextName
424 1, // retryCount
425 1, // timeout
|
426 kumpf 1.5 community_name, // securityName,
|
427 kumpf 1.3 SR_SECURITY_LEVEL_NOAUTH, // securityLevel
428 SR_SECURITY_MODEL_V1, // securityModel
429 &global_ti, // Transport Info
|
430 kumpf 1.12 0); // cfg_chk
431 break;
432 }
433 case _SNMPv2C_TRAP:
434 {
435 SendNotificationToDestSMIv2Params(
436 (SR_INT32)SNMPv2_TRAP_TYPE, // notifyType - NOTIFICATION
|
437 kumpf 1.3 sendtrapOid, // snmpTrapOID
438 agent_addr, // agent_addr
|
439 kumpf 1.12 vbhead, // vb
|
440 kumpf 1.3 NULL, // contextName
441 1, // retryCount
442 100, // timeout
|
443 kumpf 1.5 community_name, // securityName or community
|
444 kumpf 1.3 SR_SECURITY_LEVEL_NOAUTH, // securityLevel
|
445 kumpf 1.12 SR_SECURITY_MODEL_V2C, // securityModel
|
446 kumpf 1.3 &global_ti, // TransportInfo
447 0); // cfg_chk
|
448 kumpf 1.12 break;
449 }
450 default:
451 {
452 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_NOT_SUPPORTED,
453 _MSG_VERSION_NOT_SUPPORTED);
454 break;
455 }
456 }
457
458 // Free OID built by calls MakeOIDFromDot()
459 FreeOID(sendtrapOid);
460 FreeOID(enterpriseOid);
461 FreeOID(object);
462
463 // Free the data structures allocated and built by calls
464 // MakeOctetString() and MakeOctetStringFrom Text()
465 FreeOctetString(community_name);
466 FreeOctetString(agent_addr);
467
468 // Free the VarBind data structures allocated and built
469 kumpf 1.12 // by calls MakeVarBindWithValue()
470 FreeVarBindList(vbhead);
471 FreeVarBindList(vb);
472 FreeVarBindList(vblast);
473 }
474
475 // get the IP address of a host
476 char * snmpDeliverTrap_emanate::_getIPAddress(const CString& hostName)
477 {
478 struct hostent *targetHostInfo;
479 struct in_addr in;
480
481 targetHostInfo = gethostbyname(hostName);
482
483 if (targetHostInfo == NULL)
484 {
485 return(NULL);
486 }
487
488 char ** networkAddr;
489 networkAddr = targetHostInfo->h_addr_list;
490 kumpf 1.12 (void)memcpy(&in.s_addr, *networkAddr, sizeof(in.s_addr));
491 char * ipAddr = inet_ntoa(in);
492 return(ipAddr);
493 }
494
495 // check the value of each part of an IP address which should be
496 // between 0 and 255
497 Boolean snmpDeliverTrap_emanate::_isValidOctet(const Uint32& octetValue)
498 {
499 if (octetValue > 0 && octetValue < 255)
500 {
501 return true;
|
502 kumpf 1.3 }
503 else
504 {
|
505 kumpf 1.12 return false;
|
506 mike 1.2 }
507 }
508
509 PEGASUS_NAMESPACE_END
|