1 martin 1.21 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.22 //
|
3 martin 1.21 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
|
9 martin 1.22 //
|
10 martin 1.21 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
|
16 martin 1.22 //
|
17 martin 1.21 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.22 //
|
20 martin 1.21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 martin 1.22 //
|
28 martin 1.21 //////////////////////////////////////////////////////////////////////////
|
29 yi.zhou 1.1 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
|
32 thilo.boehm 1.24 #include <Pegasus/Repository/Packer.h>
|
33 yi.zhou 1.1 #include <Pegasus/Common/Tracer.h>
34 #include "snmpDeliverTrap_netsnmp.h"
35
36 PEGASUS_NAMESPACE_BEGIN
37
|
38 yi.zhou 1.4 void snmpDeliverTrap_netsnmp::initialize()
39 {
|
40 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::initialize");
|
41 yi.zhou 1.4
|
42 kumpf 1.10 // Defined default MIB modules (in net-snmp-config.h) do not need to be
|
43 yi.zhou 1.4 // loaded and loading them can cause some stderr;
44 // use environment variable MIBS to override the default MIB modules.
|
45 kumpf 1.10 // If there is no MIBS environment variable, add it in.
46 char* envVar;
|
47 yi.zhou 1.4 envVar = getenv("MIBS");
48
49 if (envVar == NULL)
50 {
51 putenv("MIBS=");
52 }
53
54 // Initialize the mib reader
55 netsnmp_set_mib_directory("");
56 init_mib();
|
57 kumpf 1.10
|
58 yi.zhou 1.4 // Initializes the SNMP library
59 init_snmp("snmpIndicationHandler");
60
|
61 yi.zhou 1.20 // don't load/save persistent file
62 #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
63 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
64 NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
65 #endif
66
|
67 yi.zhou 1.4 // windows32 specific initialization (is a NOOP on unix)
68 SOCK_STARTUP;
69
|
70 kumpf 1.10 PEG_METHOD_EXIT();
|
71 yi.zhou 1.4 }
72
73 void snmpDeliverTrap_netsnmp::terminate()
74 {
|
75 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::terminate");
|
76 yi.zhou 1.4
|
77 yi.zhou 1.13 // Shuts down the application, and appropriate clean up
78 snmp_shutdown("snmpIndicationHandler");
|
79 kumpf 1.23
|
80 yi.zhou 1.4 SOCK_CLEANUP;
81
|
82 kumpf 1.10 PEG_METHOD_EXIT();
|
83 yi.zhou 1.4 }
84
|
85 yi.zhou 1.1 void snmpDeliverTrap_netsnmp::deliverTrap(
|
86 kumpf 1.10 const String& trapOid,
87 const String& securityName,
88 const String& targetHost,
89 const Uint16& targetHostFormat,
90 const String& otherTargetHostFormat,
91 const Uint32& portNumber,
92 const Uint16& snmpVersion,
93 const String& engineID,
|
94 sahana.prabhakar 1.25 const Uint8& snmpSecLevel,
95 const Uint8& snmpSecAuthProto,
96 const Array<Uint8>& snmpSecAuthKey,
97 const Uint8& snmpSecPrivProto,
98 const Array<Uint8>& snmpSecPrivKey,
|
99 kumpf 1.10 const Array<String>& vbOids,
100 const Array<String>& vbTypes,
101 const Array<String>& vbValues)
|
102 yi.zhou 1.1 {
103
|
104 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::deliverTrap");
|
105 yi.zhou 1.1
|
106 kumpf 1.10 void* sessionHandle;
107 struct snmp_session* sessionPtr;
|
108 yi.zhou 1.1
|
109 kumpf 1.10 struct snmp_pdu* snmpPdu;
|
110 yi.zhou 1.1
111 // Creates a SNMP session
|
112 sahana.prabhakar 1.25 _createSession(
113 targetHost,
114 targetHostFormat,
115 portNumber,
116 securityName,
117 snmpVersion,
118 engineID,
119 snmpSecLevel,
120 snmpSecAuthProto,
121 snmpSecAuthKey,
122 snmpSecPrivProto,
123 snmpSecPrivKey,
124 sessionHandle,
125 sessionPtr);
|
126 yi.zhou 1.1
127 try
128 {
129 _createPdu(snmpVersion, trapOid, sessionPtr, snmpPdu);
130 }
131 catch (...)
132 {
133 _destroySession(sessionHandle);
134
|
135 kumpf 1.10 PEG_METHOD_EXIT();
|
136 yi.zhou 1.1 throw;
137 }
138
139 // Pack OIDs into the PDU
140 try
141 {
142 _packOidsIntoPdu(vbOids, vbTypes, vbValues, snmpPdu);
143 }
144 catch (Exception& e)
145 {
|
146 kumpf 1.18 Logger::put_l(Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
147 MessageLoaderParms(
148 _MSG_PACK_CIM_PROPERTY_TO_PDU_FAILED_KEY,
149 _MSG_PACK_CIM_PROPERTY_TO_PDU_FAILED,
150 e.getMessage()));
|
151 yi.zhou 1.1 }
152 catch (...)
153 {
|
154 marek 1.14 PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
|
155 kumpf 1.10 "Snmp Indication Handler failed to pack a CIM "
156 "Property into the SNMP PDU: Unknown exception.");
|
157 yi.zhou 1.1 }
158
159 // Send the trap to the destination
160 if (snmp_sess_send(sessionHandle, snmpPdu) == 0)
161 {
162 Sint32 libErr, sysErr;
|
163 kumpf 1.10 char* errStr;
|
164 yi.zhou 1.1
165 // snmp_sess_send failed
166 // get library, system errno
167 snmp_sess_error(sessionHandle, &libErr, &sysErr, &errStr);
168
169 String exceptionStr = _MSG_SESSION_SEND_FAILED;
|
170 kumpf 1.10 exceptionStr.append(errStr);
|
171 yi.zhou 1.1
172 free(errStr);
173
|
174 yi.zhou 1.19 snmp_free_pdu(snmpPdu);
175
|
176 yi.zhou 1.1 _destroySession(sessionHandle);
177
|
178 kumpf 1.10 PEG_METHOD_EXIT();
|
179 yi.zhou 1.1
|
180 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
181 MessageLoaderParms(_MSG_SESSION_SEND_FAILED_KEY, exceptionStr));
|
182 yi.zhou 1.1 }
183
184 _destroySession(sessionHandle);
185
|
186 kumpf 1.10 PEG_METHOD_EXIT();
|
187 yi.zhou 1.1 }
188
189 // Creates a SNMP session
190 void snmpDeliverTrap_netsnmp::_createSession(
|
191 kumpf 1.10 const String& targetHost,
|
192 yi.zhou 1.16 Uint16 targetHostFormat,
|
193 yi.zhou 1.1 Uint32 portNumber,
|
194 kumpf 1.10 const String& securityName,
|
195 sahana.prabhakar 1.25 Uint16 snmpVersion,
196 const String& engineID,
197 const Uint8& snmpSecLevel,
198 const Uint8& snmpSecAuthProto,
199 const Array<Uint8>& snmpSecAuthKey,
200 const Uint8& snmpSecPrivProto,
201 const Array<Uint8>& snmpSecPrivKey,
|
202 kumpf 1.10 void*& sessionHandle,
203 snmp_session*& sessionPtr)
|
204 yi.zhou 1.1 {
|
205 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER,
|
206 yi.zhou 1.1 "snmpDeliverTrap_netsnmp::_createSession");
207
208 Sint32 libErr, sysErr;
|
209 kumpf 1.10 char* errStr;
|
210 yi.zhou 1.1 String exceptionStr;
|
211 kumpf 1.10
|
212 yi.zhou 1.4 struct snmp_session snmpSession;
|
213 yi.zhou 1.1
|
214 yi.zhou 1.2 {
|
215 kumpf 1.10 AutoMutex autoMut(_sessionInitMutex);
216 snmp_sess_init(&snmpSession);
|
217 yi.zhou 1.1
|
218 kumpf 1.10 CString targetHostCStr = targetHost.getCString();
|
219 yi.zhou 1.1
|
220 kumpf 1.10 // peername has format: targetHost:portNumber
221 snmpSession.peername =
|
222 sahana.prabhakar 1.25 (char*)calloc(1,strlen(targetHostCStr) + 1 + 32);
|
223 yi.zhou 1.16
224 if (targetHostFormat == _IPV6_ADDRESS)
225 {
226 sprintf(snmpSession.peername, "udp6:[%s]:%u",
227 (const char*)targetHostCStr,
228 portNumber);
229 }
230 else
231 {
232 sprintf(snmpSession.peername, "%s:%u",
233 (const char*)targetHostCStr,
234 portNumber);
235 }
|
236 yi.zhou 1.4
|
237 kumpf 1.10 sessionHandle = snmp_sess_open(&snmpSession);
|
238 yi.zhou 1.4 }
|
239 yi.zhou 1.1
240 if (sessionHandle == NULL)
241 {
242 exceptionStr = _MSG_SESSION_OPEN_FAILED;
243
244 // Get library, system errno
|
245 yi.zhou 1.5 snmp_error(&snmpSession, &libErr, &sysErr, &errStr);
|
246 yi.zhou 1.1
247 exceptionStr.append(errStr);
248
249 free(errStr);
250
|
251 yi.zhou 1.19 free(snmpSession.peername);
252
|
253 kumpf 1.10 PEG_METHOD_EXIT();
|
254 yi.zhou 1.1
|
255 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
256 MessageLoaderParms(_MSG_SESSION_OPEN_FAILED_KEY, exceptionStr));
|
257 yi.zhou 1.1 }
258
259 try
260 {
261 // get the snmp_session pointer
262 sessionPtr = snmp_sess_session(sessionHandle);
263 if (sessionPtr == NULL)
264 {
265 exceptionStr = _MSG_GET_SESSION_POINT_FAILED;
266
267 // Get library, system errno
268 snmp_sess_error(&snmpSession, &libErr, &sysErr, &errStr);
269
270 exceptionStr.append(errStr);
271
272 free(errStr);
273
|
274 yi.zhou 1.19 free(snmpSession.peername);
275
|
276 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
277 _MSG_GET_SESSION_POINTER_FAILED_KEY,
278 exceptionStr));
|
279 yi.zhou 1.1 }
280
|
281 sahana.prabhakar 1.25 free(snmpSession.peername);
282
283 switch (snmpVersion)
|
284 yi.zhou 1.1 {
|
285 sahana.prabhakar 1.25 case _SNMPv1_TRAP:
286 {
287 sessionPtr->version = SNMP_VERSION_1;
288 _addCommunity(sessionPtr,securityName);
289 break;
290
291 }
292 case _SNMPv2C_TRAP:
293 {
294 sessionPtr->version = SNMP_VERSION_2c;
295 _addCommunity(sessionPtr,securityName);
296 break;
297 }
|
298 sahana.prabhakar 1.26 #ifdef PEGASUS_ENABLE_NET_SNMPV3
|
299 sahana.prabhakar 1.25 case _SNMPv3_TRAP:
300 {
301 sessionPtr->version = SNMP_VERSION_3;
302 CString securityNameCStr = securityName.getCString();
303 size_t securityNameLen = strlen(securityNameCStr);
304 SNMP_FREE(sessionPtr->securityName);
305 sessionPtr->securityName = (char *)calloc(1,securityNameLen+1);
306 sessionPtr->securityNameLen = securityNameLen;
307 memcpy(sessionPtr->securityName, (const char*)securityNameCStr,
308 securityNameLen);
309
310 CString engineIdCStr = engineID.getCString();
311 size_t engineIdHexLen = strlen(engineIdCStr);
312 size_t engineIdBinLen = 0;
313 u_char *engineIdBin = (u_char *)calloc(1,engineIdHexLen);
314 free(sessionPtr->securityEngineID);
315 if(!snmp_hex_to_binary(&engineIdBin, &engineIdHexLen,
316 &engineIdBinLen, 1,engineIdCStr))
317 {
318 PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
319 "Snmp Indication Handler failed to generate binary"
320 sahana.prabhakar 1.25 " engine ID for sending the SNMPv3 trap.");
321 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
322 MessageLoaderParms(
323 "Handler.snmpIndicationHandler."
324 "snmpIndicationHandler."
325 "FAILED_TO_DELIVER_TRAP",
326 "Failed to deliver trap."));
327 }
328 sessionPtr->securityEngineIDLen = engineIdBinLen;
329 sessionPtr->securityEngineID = engineIdBin;
330
331 switch(snmpSecLevel)
332 {
333 case 1:
334 sessionPtr->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
335 break;
336 case 2:
337 sessionPtr->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
338 break;
339 case 3:
340 sessionPtr->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
341 sahana.prabhakar 1.25 break;
342 default:
343 //use the dedault in the net-snmp conf file.
344 break;
345 }
346
347 SNMP_FREE(sessionPtr->securityAuthProto);
348 if(snmpSecAuthProto == 1) // MD5
349 {
350 sessionPtr->securityAuthProto = snmp_duplicate_objid(
351 usmHMACMD5AuthProtocol,
352 USM_AUTH_PROTO_MD5_LEN);
353 sessionPtr->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
354 }
355 else if(snmpSecAuthProto == 2)// SHA
356 {
357 sessionPtr->securityAuthProto = snmp_duplicate_objid(
358 usmHMACSHA1AuthProtocol,
359 USM_AUTH_PROTO_SHA_LEN);
360 sessionPtr->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
361 }
362 sahana.prabhakar 1.25 // use the default in net-snmp conf files.
363
364 if(snmpSecAuthKey.size() > 0)
365 {
366 for(Uint32 i=0;i<snmpSecAuthKey.size();i++)
367 {
368 sessionPtr->securityAuthKey[i] = snmpSecAuthKey[i];
369 }
370 sessionPtr->securityAuthKeyLen = snmpSecAuthKey.size();
371 }
372
373 SNMP_FREE(sessionPtr->securityPrivProto);
374 //Privacy
375 if(snmpSecPrivProto == 1) //DES
376 {
377 sessionPtr->securityPrivProto = snmp_duplicate_objid(
378 usmDESPrivProtocol,
379 USM_PRIV_PROTO_DES_LEN);
380 sessionPtr->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
381 }
382 else if(snmpSecPrivProto == 2) // AES
383 sahana.prabhakar 1.25 {
384 sessionPtr->securityPrivProto = snmp_duplicate_objid(
385 usmAESPrivProtocol,
386 USM_PRIV_PROTO_AES_LEN);
387 sessionPtr->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
388 }
389 // use the defaults in net-snmp conf files
390
391 // Privacy Key
392 if(snmpSecPrivKey.size() > 0)
393 {
394 for(Uint32 j=0;j<snmpSecPrivKey.size();j++)
395 {
396 sessionPtr->securityPrivKey[j] = snmpSecPrivKey[j];
397 }
398 sessionPtr->securityPrivKeyLen = snmpSecPrivKey.size();
399 }
400 break;
401 }
|
402 sahana.prabhakar 1.26 #endif // ifdef PEGASUS_ENABLE_NET_SNMPV3
|
403 sahana.prabhakar 1.25 default:
404 {
405 PEG_METHOD_EXIT();
406 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED,
407 MessageLoaderParms(
408 _MSG_VERSION_NOT_SUPPORTED_KEY,
409 _MSG_VERSION_NOT_SUPPORTED));
410 break;
411 }
|
412 yi.zhou 1.1 }
413
414 }
415 catch (...)
416 {
417 _destroySession(sessionHandle);
418
|
419 kumpf 1.10 PEG_METHOD_EXIT();
|
420 yi.zhou 1.1 throw;
421 }
422
|
423 kumpf 1.10 PEG_METHOD_EXIT();
|
424 yi.zhou 1.1 }
425
|
426 sahana.prabhakar 1.25 void snmpDeliverTrap_netsnmp::_addCommunity(
427 struct snmp_session*& sessionPtr,
428 const String& securityName)
429 {
430 PEG_METHOD_ENTER(TRC_IND_HANDLER,
431 "snmpDeliverTrap_netsnmp::_addCommunity");
432
433 // Community Name, default is public
434 String communityName;
435 if (securityName.size() == 0)
436 {
437 communityName.assign("public");
438 }
439 else
440 {
441 communityName = securityName;
442 }
443
444 free(sessionPtr->community);
445
446 CString communityNameCStr = communityName.getCString();
447 sahana.prabhakar 1.25 size_t communityNameLen = strlen(communityNameCStr);
448
449 sessionPtr->community = (u_char*)calloc(1,communityNameLen+1);
450
451 memcpy(sessionPtr->community, (const char*)communityNameCStr,
452 communityNameLen);
453
454 sessionPtr->community_len = communityNameLen;
455 PEG_METHOD_EXIT();
456 }
457
|
458 yi.zhou 1.1 // Creates a SNMP session
459 void snmpDeliverTrap_netsnmp::_destroySession(
|
460 kumpf 1.10 void* sessionHandle)
|
461 yi.zhou 1.1 {
|
462 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER,
|
463 yi.zhou 1.1 "snmpDeliverTrap_netsnmp::_destroySession");
464
465 snmp_sess_close(sessionHandle);
466
|
467 kumpf 1.10 PEG_METHOD_EXIT();
|
468 yi.zhou 1.1 }
469
|
470 kumpf 1.10 // Creates a SNMP TRAP PDU
|
471 yi.zhou 1.1 void snmpDeliverTrap_netsnmp::_createPdu(
472 Uint16 snmpVersion,
473 const String& trapOid,
|
474 kumpf 1.10 snmp_session*& sessionPtr,
475 snmp_pdu*& snmpPdu)
|
476 yi.zhou 1.1 {
|
477 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::_createPdu");
|
478 yi.zhou 1.1
479 oid _SYSTEM_UP_TIME_OID [] = {1,3,6,1,2,1,1,3,0};
480 oid _SNMPTRAP_OID [] = {1,3,6,1,6,3,1,1,4,1,0};
481
|
482 kumpf 1.10 in_addr_t* pduInAddr;
|
483 yi.zhou 1.1
484 switch (snmpVersion)
485 {
486 case _SNMPv1_TRAP:
487 {
488 // Create the PDU
|
489 kumpf 1.10 snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP);
490
|
491 yi.zhou 1.1 // Failed to create pdu
492 if (!snmpPdu)
493 {
|
494 kumpf 1.10 PEG_METHOD_EXIT();
495 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
496 MessageLoaderParms(
497 _MSG_PDU_CREATE_FAILED_KEY,
498 _MSG_PDU_CREATE_FAILED));
|
499 yi.zhou 1.1 }
500
501 // Make sure that the v1 trap PDU includes the local IP address
502 pduInAddr = (in_addr_t*) snmpPdu->agent_addr;
503 *pduInAddr = get_myaddr();
504
505 // get system up time
506 snmpPdu->time = get_uptime();
507
508 // Pack trap information into the PDU
509 try
510 {
511 _packTrapInfoIntoPdu(trapOid, snmpPdu);
512 }
513 catch (CIMException& e)
514 {
|
515 kumpf 1.10 Logger::put_l(
516 Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
|
517 kumpf 1.18 MessageLoaderParms(
518 _MSG_PACK_TRAP_INFO_INTO_PDU_FAILED_KEY,
519 _MSG_PACK_TRAP_INFO_INTO_PDU_FAILED,
520 e.getMessage()));
|
521 yi.zhou 1.1 }
522
523 break;
524 }
525 case _SNMPv2C_TRAP:
|
526 sahana.prabhakar 1.25 case _SNMPv3_TRAP:
|
527 yi.zhou 1.1 {
528 // Create the PDU
|
529 kumpf 1.10 snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP2);
530
|
531 yi.zhou 1.1 // Failed to create pdu
532 if (!snmpPdu)
533 {
|
534 kumpf 1.10 PEG_METHOD_EXIT();
|
535 yi.zhou 1.1
|
536 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
537 MessageLoaderParms(
538 _MSG_PDU_CREATE_FAILED_KEY,
539 _MSG_PDU_CREATE_FAILED));
|
540 yi.zhou 1.1 }
541
542 // Add sysUpTime to the PDU
543 char sysUpTime[32];
544 sprintf(sysUpTime, "%ld", get_uptime());
|
545 kumpf 1.10
|
546 yi.zhou 1.1 Sint32 retCode;
|
547 kumpf 1.10 retCode = snmp_add_var(
548 snmpPdu,
549 _SYSTEM_UP_TIME_OID,
550 OID_LENGTH(_SYSTEM_UP_TIME_OID),
551 't',
552 sysUpTime);
|
553 yi.zhou 1.1
554 // Failed to add sysUpTime to the pdu
555 if (retCode != 0)
556 {
557 String errMsg = snmp_api_errstring(retCode);
558
|
559 kumpf 1.10 Logger::put_l(
560 Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
|
561 kumpf 1.18 MessageLoaderParms(
562 _MSG_ADD_SYSUPTIME_TO_PDU_FAILED_KEY,
563 _MSG_ADD_SYSUPTIME_TO_PDU_FAILED,
564 errMsg));
|
565 yi.zhou 1.1 }
566
567 // Add snmp trap to the PDU
|
568 kumpf 1.10 retCode = snmp_add_var(
569 snmpPdu,
570 _SNMPTRAP_OID,
571 OID_LENGTH(_SNMPTRAP_OID),
572 'o',
573 trapOid.getCString());
|
574 yi.zhou 1.1
575 // Failed to add snmp trap to the pdu
576 if (retCode != 0)
577 {
578 String errMsg = snmp_api_errstring(retCode);
579
|
580 kumpf 1.10 Logger::put_l(
581 Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
|
582 kumpf 1.18 MessageLoaderParms(
583 _MSG_ADD_SNMP_TRAP_TO_PDU_FAILED_KEY,
584 _MSG_ADD_SNMP_TRAP_TO_PDU_FAILED,
585 errMsg));
|
586 yi.zhou 1.1 }
587
588 break;
589 }
590 default:
591 {
|
592 kumpf 1.10 PEG_METHOD_EXIT();
|
593 yi.zhou 1.1
594 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED,
|
595 kumpf 1.10 MessageLoaderParms(
596 _MSG_VERSION_NOT_SUPPORTED_KEY,
597 _MSG_VERSION_NOT_SUPPORTED));
|
598 yi.zhou 1.1 }
599 }
600
|
601 kumpf 1.10 PEG_METHOD_EXIT();
|
602 yi.zhou 1.1 }
603
604
605 // Pack trap information into the PDU
606 void snmpDeliverTrap_netsnmp::_packTrapInfoIntoPdu(
|
607 kumpf 1.10 const String& trapOid,
608 snmp_pdu* snmpPdu)
|
609 yi.zhou 1.1 {
|
610 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER,
|
611 yi.zhou 1.1 "snmpDeliverTrap_netsnmp::_packTrapInfoIntoPdu");
612
613 oid enterpriseOid[MAX_OID_LEN];
|
614 kumpf 1.10 size_t enterpriseOidLength;
|
615 yi.zhou 1.1
616 Array<String> standard_traps;
617
618 standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
619 standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
620 standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
621 standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
622 standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
623 standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
624
|
625 kumpf 1.10 Array<const char*> oidSubIdentifiers;
|
626 yi.zhou 1.1
627 CString trapOidCStr = trapOid.getCString();
628
|
629 kumpf 1.10 char* trapOidCopy = strdup(trapOidCStr);
|
630 sahana.prabhakar 1.25 char* numericEntOid = (char*) malloc(strlen(trapOidCStr)+1);
|
631 yi.zhou 1.9
632 try
633 {
|
634 yi.zhou 1.1
|
635 a.dunfey 1.12 #if !defined(PEGASUS_OS_TYPE_WINDOWS) && !defined(PEGASUS_OS_ZOS)
|
636 kumpf 1.10 char* last;
637 for (const char* p = strtok_r(trapOidCopy, ".", &last); p;
|
638 yi.zhou 1.9 p=strtok_r(NULL, ".", &last))
|
639 yi.zhou 1.1 #else
|
640 yi.zhou 1.9 for (const char* p = strtok(trapOidCopy, "."); p; p=strtok(NULL, "."))
|
641 yi.zhou 1.1 #endif
|
642 yi.zhou 1.9 {
643 oidSubIdentifiers.append(p);
644 }
|
645 yi.zhou 1.1
|
646 yi.zhou 1.9 long genTrap = 0;
647 long specTrap = 0;
|
648 yi.zhou 1.1
|
649 yi.zhou 1.9 enterpriseOidLength = MAX_OID_LEN;
|
650 yi.zhou 1.1
|
651 yi.zhou 1.9 if (Contains(standard_traps, trapOid))
652 {
653 //
654 // if the trapOid is one of the standard traps,
655 // then the SNMPV1 enterprise parameter must be set
656 // to the value of the trapOid, the generic-trap
657 // parameter must be set to one of (0 - 5), and the
658 // specific-trap parameter must be set to 0
659 //
660
661 // Convert trapOid from numeric form to a list of subidentifiers
|
662 kumpf 1.10 if (read_objid((const char*)trapOidCStr, enterpriseOid,
|
663 yi.zhou 1.9 &enterpriseOidLength) == 0)
664 {
|
665 kumpf 1.10 // Failed to parse trapOid
|
666 yi.zhou 1.9
|
667 kumpf 1.10 PEG_METHOD_EXIT();
668 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
669 yi.zhou 1.9 MessageLoaderParms(_MSG_READ_OBJID_FAILED_KEY,
670 _MSG_READ_OBJID_FAILED,
671 trapOid));
672 }
673
674 // the generic trap is last sub-identifier of the
675 // trapOid minus 1
|
676 kumpf 1.10 snmpPdu->trap_type =
|
677 yi.zhou 1.9 atoi(oidSubIdentifiers[oidSubIdentifiers.size() - 1]) - 1;
678 snmpPdu->specific_type = 0;
|
679 yi.zhou 1.1 }
|
680 yi.zhou 1.9 else
|
681 yi.zhou 1.1 {
|
682 yi.zhou 1.9 //
683 // if the trapOid is not one of the standard traps:
684 // then 1) the generic-trap parameter must be set to 6,
685 // 2) if the next-to-last sub-identifier of the
686 // trapOid is zero, then the SNMPV1 enterprise
687 // parameter is the trapOid with the last 2
688 // sub-identifiers removed, otherwise, the
689 // SNMPV1 enterprise parameter is the trapOid
690 // with the last sub-identifier removed;
691 // 3) the SNMPv1 specific-trap parameter is the last
692 // sub-identifier of the trapOid;
693 //
694
695 snmpPdu->trap_type = 6;
696
|
697 kumpf 1.10 snmpPdu->specific_type =
|
698 yi.zhou 1.9 atoi(oidSubIdentifiers[oidSubIdentifiers.size()-1]);
699
700 strcpy(numericEntOid, oidSubIdentifiers[0]);
701 for (Uint32 i = 1; i < oidSubIdentifiers.size()-2; i++)
702 {
703 strcat(numericEntOid, ".");
704 strcat(numericEntOid, oidSubIdentifiers[i]);
705 }
706
707 if (strcmp(oidSubIdentifiers[oidSubIdentifiers.size()-2], "0") != 0)
708 {
709 strcat(numericEntOid, ".");
|
710 kumpf 1.10 strcat(numericEntOid,
|
711 yi.zhou 1.9 oidSubIdentifiers[oidSubIdentifiers.size()-2]);
712 }
|
713 yi.zhou 1.1
|
714 kumpf 1.10 // Convert ent from numeric form to a list of subidentifiers
715 if (read_objid(numericEntOid, enterpriseOid,
|
716 yi.zhou 1.9 &enterpriseOidLength) == 0)
717 {
718 // Failed to parse numericEntOid
|
719 yi.zhou 1.1
|
720 kumpf 1.10 PEG_METHOD_EXIT();
|
721 yi.zhou 1.1
|
722 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
723 yi.zhou 1.9 MessageLoaderParms(_MSG_READ_ENTOID_FAILED_KEY,
724 _MSG_READ_ENTOID_FAILED,
725 String(numericEntOid)));
726 }
|
727 yi.zhou 1.1
728 }
729
|
730 sahana.prabhakar 1.25 SNMP_FREE(snmpPdu->enterprise);
|
731 yi.zhou 1.9 snmpPdu->enterprise = (oid*) malloc(enterpriseOidLength * sizeof(oid));
|
732 sahana.prabhakar 1.25 memcpy(snmpPdu->enterprise, enterpriseOid,
|
733 kumpf 1.10 enterpriseOidLength * sizeof(oid));
|
734 yi.zhou 1.9
735 snmpPdu->enterprise_length = enterpriseOidLength;
|
736 yi.zhou 1.1 }
|
737 yi.zhou 1.9 catch (...)
738 {
739 free(trapOidCopy);
740 free(numericEntOid);
|
741 yi.zhou 1.1
|
742 yi.zhou 1.9 PEG_METHOD_EXIT();
743 throw;
744 }
|
745 yi.zhou 1.1
746 free(trapOidCopy);
747 free(numericEntOid);
748
|
749 kumpf 1.10 PEG_METHOD_EXIT();
|
750 yi.zhou 1.1 }
751
752 // Pack oids into the PDU
753 void snmpDeliverTrap_netsnmp::_packOidsIntoPdu(
754 const Array<String>& vbOids,
755 const Array<String>& vbTypes,
756 const Array<String>& vbValues,
|
757 kumpf 1.10 snmp_pdu* snmpPdu)
|
758 yi.zhou 1.1 {
759
|
760 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER,
|
761 yi.zhou 1.1 "snmpDeliverTrap_netsnmp::_packOidsIntoPdu");
762
763 char dataType;
764 oid vbOid[MAX_OID_LEN];
765 size_t vbOidLength = MAX_OID_LEN;
766
767 for (Uint32 i=0; i < vbOids.size(); i++)
768 {
769 if (vbTypes[i] == "OctetString")
770 {
771 dataType = 's';
772 }
773 else if (vbTypes[i] == "Integer")
774 {
775 dataType = 'i';
|
776 kumpf 1.10 }
|
777 yi.zhou 1.1 else if (vbTypes[i] == "OID")
778 {
779 dataType = 'o';
780 }
781 else
782 {
|
783 kumpf 1.10 // Integer, OctetString, and OID are supported SNMP Data Types
|
784 yi.zhou 1.1 // for the CIM Property
785
|
786 kumpf 1.10 PEG_METHOD_EXIT();
|
787 yi.zhou 1.1
|
788 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
789 yi.zhou 1.1 MessageLoaderParms(_MSG_UNSUPPORTED_SNMP_DATA_TYPE_KEY,
790 _MSG_UNSUPPORTED_SNMP_DATA_TYPE,
791 vbTypes[i]));
792
793 }
794
|
795 kumpf 1.10 // Convert oid of a CIM property from numeric form to a list of
796 // subidentifiers
797 if (read_objid((const char*)vbOids[i].getCString(), vbOid,
|
798 yi.zhou 1.1 &vbOidLength) == 0)
799 {
800 // Failed to parse vbOids
801
|
802 kumpf 1.10 PEG_METHOD_EXIT();
|
803 yi.zhou 1.1
|
804 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
805 yi.zhou 1.1 MessageLoaderParms(_MSG_PARSE_CIM_PROPERTY_OID_FAILED_KEY,
806 _MSG_PARSE_CIM_PROPERTY_OID_FAILED,
807 vbOids[i]));
808 }
809
810 Sint32 retCode;
|
811 kumpf 1.10 retCode = snmp_add_var(snmpPdu, vbOid, vbOidLength, dataType,
|
812 yi.zhou 1.1 vbValues[i].getCString());
813
814 // Failed to add vbOid to the pdu
815 if (retCode != 0)
816 {
|
817 kumpf 1.10 PEG_METHOD_EXIT();
|
818 yi.zhou 1.1
|
819 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
820 yi.zhou 1.1 MessageLoaderParms(_MSG_ADD_VAR_TO_PDU_FAILED_KEY,
821 _MSG_ADD_VAR_TO_PDU_FAILED,
822 vbOids[i],
|
823 kumpf 1.10 String(snmp_api_errstring(retCode))));
|
824 yi.zhou 1.1
825 }
826 }
827
|
828 kumpf 1.10 PEG_METHOD_EXIT();
|
829 yi.zhou 1.1 }
830
831 PEGASUS_NAMESPACE_END
|