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 }
|
411 yi.zhou 1.1 }
412
413 }
414 catch (...)
415 {
416 _destroySession(sessionHandle);
417
|
418 kumpf 1.10 PEG_METHOD_EXIT();
|
419 yi.zhou 1.1 throw;
420 }
421
|
422 kumpf 1.10 PEG_METHOD_EXIT();
|
423 yi.zhou 1.1 }
424
|
425 sahana.prabhakar 1.25 void snmpDeliverTrap_netsnmp::_addCommunity(
426 struct snmp_session*& sessionPtr,
427 const String& securityName)
428 {
429 PEG_METHOD_ENTER(TRC_IND_HANDLER,
430 "snmpDeliverTrap_netsnmp::_addCommunity");
431
432 // Community Name, default is public
433 String communityName;
434 if (securityName.size() == 0)
435 {
436 communityName.assign("public");
437 }
438 else
439 {
440 communityName = securityName;
441 }
442
443 free(sessionPtr->community);
444
445 CString communityNameCStr = communityName.getCString();
446 sahana.prabhakar 1.25 size_t communityNameLen = strlen(communityNameCStr);
447
448 sessionPtr->community = (u_char*)calloc(1,communityNameLen+1);
449
450 memcpy(sessionPtr->community, (const char*)communityNameCStr,
451 communityNameLen);
452
453 sessionPtr->community_len = communityNameLen;
454 PEG_METHOD_EXIT();
455 }
456
|
457 yi.zhou 1.1 // Creates a SNMP session
458 void snmpDeliverTrap_netsnmp::_destroySession(
|
459 kumpf 1.10 void* sessionHandle)
|
460 yi.zhou 1.1 {
|
461 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER,
|
462 yi.zhou 1.1 "snmpDeliverTrap_netsnmp::_destroySession");
463
464 snmp_sess_close(sessionHandle);
465
|
466 kumpf 1.10 PEG_METHOD_EXIT();
|
467 yi.zhou 1.1 }
468
|
469 kumpf 1.10 // Creates a SNMP TRAP PDU
|
470 yi.zhou 1.1 void snmpDeliverTrap_netsnmp::_createPdu(
471 Uint16 snmpVersion,
472 const String& trapOid,
|
473 kumpf 1.10 snmp_session*& sessionPtr,
474 snmp_pdu*& snmpPdu)
|
475 yi.zhou 1.1 {
|
476 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::_createPdu");
|
477 yi.zhou 1.1
478 oid _SYSTEM_UP_TIME_OID [] = {1,3,6,1,2,1,1,3,0};
479 oid _SNMPTRAP_OID [] = {1,3,6,1,6,3,1,1,4,1,0};
480
|
481 kumpf 1.10 in_addr_t* pduInAddr;
|
482 yi.zhou 1.1
483 switch (snmpVersion)
484 {
485 case _SNMPv1_TRAP:
486 {
487 // Create the PDU
|
488 kumpf 1.10 snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP);
489
|
490 yi.zhou 1.1 // Failed to create pdu
491 if (!snmpPdu)
492 {
|
493 kumpf 1.10 PEG_METHOD_EXIT();
494 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
495 MessageLoaderParms(
496 _MSG_PDU_CREATE_FAILED_KEY,
497 _MSG_PDU_CREATE_FAILED));
|
498 yi.zhou 1.1 }
499
500 // Make sure that the v1 trap PDU includes the local IP address
501 pduInAddr = (in_addr_t*) snmpPdu->agent_addr;
502 *pduInAddr = get_myaddr();
503
504 // get system up time
505 snmpPdu->time = get_uptime();
506
507 // Pack trap information into the PDU
508 try
509 {
510 _packTrapInfoIntoPdu(trapOid, snmpPdu);
511 }
512 catch (CIMException& e)
513 {
|
514 kumpf 1.10 Logger::put_l(
515 Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
|
516 kumpf 1.18 MessageLoaderParms(
517 _MSG_PACK_TRAP_INFO_INTO_PDU_FAILED_KEY,
518 _MSG_PACK_TRAP_INFO_INTO_PDU_FAILED,
519 e.getMessage()));
|
520 yi.zhou 1.1 }
521
522 break;
523 }
524 case _SNMPv2C_TRAP:
|
525 sahana.prabhakar 1.25 case _SNMPv3_TRAP:
|
526 yi.zhou 1.1 {
527 // Create the PDU
|
528 kumpf 1.10 snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP2);
529
|
530 yi.zhou 1.1 // Failed to create pdu
531 if (!snmpPdu)
532 {
|
533 kumpf 1.10 PEG_METHOD_EXIT();
|
534 yi.zhou 1.1
|
535 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
536 MessageLoaderParms(
537 _MSG_PDU_CREATE_FAILED_KEY,
538 _MSG_PDU_CREATE_FAILED));
|
539 yi.zhou 1.1 }
540
541 // Add sysUpTime to the PDU
542 char sysUpTime[32];
543 sprintf(sysUpTime, "%ld", get_uptime());
|
544 kumpf 1.10
|
545 yi.zhou 1.1 Sint32 retCode;
|
546 kumpf 1.10 retCode = snmp_add_var(
547 snmpPdu,
548 _SYSTEM_UP_TIME_OID,
549 OID_LENGTH(_SYSTEM_UP_TIME_OID),
550 't',
551 sysUpTime);
|
552 yi.zhou 1.1
553 // Failed to add sysUpTime to the pdu
554 if (retCode != 0)
555 {
556 String errMsg = snmp_api_errstring(retCode);
557
|
558 kumpf 1.10 Logger::put_l(
559 Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
|
560 kumpf 1.18 MessageLoaderParms(
561 _MSG_ADD_SYSUPTIME_TO_PDU_FAILED_KEY,
562 _MSG_ADD_SYSUPTIME_TO_PDU_FAILED,
563 errMsg));
|
564 yi.zhou 1.1 }
565
566 // Add snmp trap to the PDU
|
567 kumpf 1.10 retCode = snmp_add_var(
568 snmpPdu,
569 _SNMPTRAP_OID,
570 OID_LENGTH(_SNMPTRAP_OID),
571 'o',
572 trapOid.getCString());
|
573 yi.zhou 1.1
574 // Failed to add snmp trap to the pdu
575 if (retCode != 0)
576 {
577 String errMsg = snmp_api_errstring(retCode);
578
|
579 kumpf 1.10 Logger::put_l(
580 Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
|
581 kumpf 1.18 MessageLoaderParms(
582 _MSG_ADD_SNMP_TRAP_TO_PDU_FAILED_KEY,
583 _MSG_ADD_SNMP_TRAP_TO_PDU_FAILED,
584 errMsg));
|
585 yi.zhou 1.1 }
586
587 break;
588 }
589 default:
590 {
|
591 kumpf 1.10 PEG_METHOD_EXIT();
|
592 yi.zhou 1.1
593 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED,
|
594 kumpf 1.10 MessageLoaderParms(
595 _MSG_VERSION_NOT_SUPPORTED_KEY,
596 _MSG_VERSION_NOT_SUPPORTED));
|
597 yi.zhou 1.1 }
598 }
599
|
600 kumpf 1.10 PEG_METHOD_EXIT();
|
601 yi.zhou 1.1 }
602
603
604 // Pack trap information into the PDU
605 void snmpDeliverTrap_netsnmp::_packTrapInfoIntoPdu(
|
606 kumpf 1.10 const String& trapOid,
607 snmp_pdu* snmpPdu)
|
608 yi.zhou 1.1 {
|
609 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER,
|
610 yi.zhou 1.1 "snmpDeliverTrap_netsnmp::_packTrapInfoIntoPdu");
611
612 oid enterpriseOid[MAX_OID_LEN];
|
613 kumpf 1.10 size_t enterpriseOidLength;
|
614 yi.zhou 1.1
615 Array<String> standard_traps;
616
617 standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
618 standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
619 standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
620 standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
621 standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
622 standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
623
|
624 kumpf 1.10 Array<const char*> oidSubIdentifiers;
|
625 yi.zhou 1.1
626 CString trapOidCStr = trapOid.getCString();
627
|
628 kumpf 1.10 char* trapOidCopy = strdup(trapOidCStr);
|
629 sahana.prabhakar 1.25 char* numericEntOid = (char*) malloc(strlen(trapOidCStr)+1);
|
630 yi.zhou 1.9
631 try
632 {
|
633 yi.zhou 1.1
|
634 a.dunfey 1.12 #if !defined(PEGASUS_OS_TYPE_WINDOWS) && !defined(PEGASUS_OS_ZOS)
|
635 kumpf 1.10 char* last;
636 for (const char* p = strtok_r(trapOidCopy, ".", &last); p;
|
637 yi.zhou 1.9 p=strtok_r(NULL, ".", &last))
|
638 yi.zhou 1.1 #else
|
639 yi.zhou 1.9 for (const char* p = strtok(trapOidCopy, "."); p; p=strtok(NULL, "."))
|
640 yi.zhou 1.1 #endif
|
641 yi.zhou 1.9 {
642 oidSubIdentifiers.append(p);
643 }
|
644 yi.zhou 1.1
|
645 yi.zhou 1.9 long genTrap = 0;
646 long specTrap = 0;
|
647 yi.zhou 1.1
|
648 yi.zhou 1.9 enterpriseOidLength = MAX_OID_LEN;
|
649 yi.zhou 1.1
|
650 yi.zhou 1.9 if (Contains(standard_traps, trapOid))
651 {
652 //
653 // if the trapOid is one of the standard traps,
654 // then the SNMPV1 enterprise parameter must be set
655 // to the value of the trapOid, the generic-trap
656 // parameter must be set to one of (0 - 5), and the
657 // specific-trap parameter must be set to 0
658 //
659
660 // Convert trapOid from numeric form to a list of subidentifiers
|
661 kumpf 1.10 if (read_objid((const char*)trapOidCStr, enterpriseOid,
|
662 yi.zhou 1.9 &enterpriseOidLength) == 0)
663 {
|
664 kumpf 1.10 // Failed to parse trapOid
|
665 yi.zhou 1.9
|
666 kumpf 1.10 PEG_METHOD_EXIT();
667 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
668 yi.zhou 1.9 MessageLoaderParms(_MSG_READ_OBJID_FAILED_KEY,
669 _MSG_READ_OBJID_FAILED,
670 trapOid));
671 }
672
673 // the generic trap is last sub-identifier of the
674 // trapOid minus 1
|
675 kumpf 1.10 snmpPdu->trap_type =
|
676 yi.zhou 1.9 atoi(oidSubIdentifiers[oidSubIdentifiers.size() - 1]) - 1;
677 snmpPdu->specific_type = 0;
|
678 yi.zhou 1.1 }
|
679 yi.zhou 1.9 else
|
680 yi.zhou 1.1 {
|
681 yi.zhou 1.9 //
682 // if the trapOid is not one of the standard traps:
683 // then 1) the generic-trap parameter must be set to 6,
684 // 2) if the next-to-last sub-identifier of the
685 // trapOid is zero, then the SNMPV1 enterprise
686 // parameter is the trapOid with the last 2
687 // sub-identifiers removed, otherwise, the
688 // SNMPV1 enterprise parameter is the trapOid
689 // with the last sub-identifier removed;
690 // 3) the SNMPv1 specific-trap parameter is the last
691 // sub-identifier of the trapOid;
692 //
693
694 snmpPdu->trap_type = 6;
695
|
696 kumpf 1.10 snmpPdu->specific_type =
|
697 yi.zhou 1.9 atoi(oidSubIdentifiers[oidSubIdentifiers.size()-1]);
698
699 strcpy(numericEntOid, oidSubIdentifiers[0]);
700 for (Uint32 i = 1; i < oidSubIdentifiers.size()-2; i++)
701 {
702 strcat(numericEntOid, ".");
703 strcat(numericEntOid, oidSubIdentifiers[i]);
704 }
705
706 if (strcmp(oidSubIdentifiers[oidSubIdentifiers.size()-2], "0") != 0)
707 {
708 strcat(numericEntOid, ".");
|
709 kumpf 1.10 strcat(numericEntOid,
|
710 yi.zhou 1.9 oidSubIdentifiers[oidSubIdentifiers.size()-2]);
711 }
|
712 yi.zhou 1.1
|
713 kumpf 1.10 // Convert ent from numeric form to a list of subidentifiers
714 if (read_objid(numericEntOid, enterpriseOid,
|
715 yi.zhou 1.9 &enterpriseOidLength) == 0)
716 {
717 // Failed to parse numericEntOid
|
718 yi.zhou 1.1
|
719 kumpf 1.10 PEG_METHOD_EXIT();
|
720 yi.zhou 1.1
|
721 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
722 yi.zhou 1.9 MessageLoaderParms(_MSG_READ_ENTOID_FAILED_KEY,
723 _MSG_READ_ENTOID_FAILED,
724 String(numericEntOid)));
725 }
|
726 yi.zhou 1.1
727 }
728
|
729 sahana.prabhakar 1.25 SNMP_FREE(snmpPdu->enterprise);
|
730 yi.zhou 1.9 snmpPdu->enterprise = (oid*) malloc(enterpriseOidLength * sizeof(oid));
|
731 sahana.prabhakar 1.25 memcpy(snmpPdu->enterprise, enterpriseOid,
|
732 kumpf 1.10 enterpriseOidLength * sizeof(oid));
|
733 yi.zhou 1.9
734 snmpPdu->enterprise_length = enterpriseOidLength;
|
735 yi.zhou 1.1 }
|
736 yi.zhou 1.9 catch (...)
737 {
738 free(trapOidCopy);
739 free(numericEntOid);
|
740 yi.zhou 1.1
|
741 yi.zhou 1.9 PEG_METHOD_EXIT();
742 throw;
743 }
|
744 yi.zhou 1.1
745 free(trapOidCopy);
746 free(numericEntOid);
747
|
748 kumpf 1.10 PEG_METHOD_EXIT();
|
749 yi.zhou 1.1 }
750
751 // Pack oids into the PDU
752 void snmpDeliverTrap_netsnmp::_packOidsIntoPdu(
753 const Array<String>& vbOids,
754 const Array<String>& vbTypes,
755 const Array<String>& vbValues,
|
756 kumpf 1.10 snmp_pdu* snmpPdu)
|
757 yi.zhou 1.1 {
758
|
759 kumpf 1.10 PEG_METHOD_ENTER(TRC_IND_HANDLER,
|
760 yi.zhou 1.1 "snmpDeliverTrap_netsnmp::_packOidsIntoPdu");
761
762 char dataType;
763 oid vbOid[MAX_OID_LEN];
764 size_t vbOidLength = MAX_OID_LEN;
765
766 for (Uint32 i=0; i < vbOids.size(); i++)
767 {
768 if (vbTypes[i] == "OctetString")
769 {
770 dataType = 's';
771 }
772 else if (vbTypes[i] == "Integer")
773 {
774 dataType = 'i';
|
775 kumpf 1.10 }
|
776 yi.zhou 1.1 else if (vbTypes[i] == "OID")
777 {
778 dataType = 'o';
779 }
780 else
781 {
|
782 kumpf 1.10 // Integer, OctetString, and OID are supported SNMP Data Types
|
783 yi.zhou 1.1 // for the CIM Property
784
|
785 kumpf 1.10 PEG_METHOD_EXIT();
|
786 yi.zhou 1.1
|
787 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
788 yi.zhou 1.1 MessageLoaderParms(_MSG_UNSUPPORTED_SNMP_DATA_TYPE_KEY,
789 _MSG_UNSUPPORTED_SNMP_DATA_TYPE,
790 vbTypes[i]));
791
792 }
793
|
794 kumpf 1.10 // Convert oid of a CIM property from numeric form to a list of
795 // subidentifiers
796 if (read_objid((const char*)vbOids[i].getCString(), vbOid,
|
797 yi.zhou 1.1 &vbOidLength) == 0)
798 {
799 // Failed to parse vbOids
800
|
801 kumpf 1.10 PEG_METHOD_EXIT();
|
802 yi.zhou 1.1
|
803 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
804 yi.zhou 1.1 MessageLoaderParms(_MSG_PARSE_CIM_PROPERTY_OID_FAILED_KEY,
805 _MSG_PARSE_CIM_PROPERTY_OID_FAILED,
806 vbOids[i]));
807 }
808
809 Sint32 retCode;
|
810 kumpf 1.10 retCode = snmp_add_var(snmpPdu, vbOid, vbOidLength, dataType,
|
811 yi.zhou 1.1 vbValues[i].getCString());
812
813 // Failed to add vbOid to the pdu
814 if (retCode != 0)
815 {
|
816 kumpf 1.10 PEG_METHOD_EXIT();
|
817 yi.zhou 1.1
|
818 kumpf 1.10 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
|
819 yi.zhou 1.1 MessageLoaderParms(_MSG_ADD_VAR_TO_PDU_FAILED_KEY,
820 _MSG_ADD_VAR_TO_PDU_FAILED,
821 vbOids[i],
|
822 kumpf 1.10 String(snmp_api_errstring(retCode))));
|
823 yi.zhou 1.1
824 }
825 }
826
|
827 kumpf 1.10 PEG_METHOD_EXIT();
|
828 yi.zhou 1.1 }
829
830 PEGASUS_NAMESPACE_END
|