(file) Return to snmpDeliverTrap_netsnmp.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Handler / snmpIndicationHandler

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2