(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                      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                                   case _SNMPv3_TRAP:
299                                   {
300                                       sessionPtr->version = SNMP_VERSION_3;
301                                       CString securityNameCStr = securityName.getCString();
302                                       size_t securityNameLen = strlen(securityNameCStr);
303                                       SNMP_FREE(sessionPtr->securityName);
304                                       sessionPtr->securityName = (char *)calloc(1,securityNameLen+1);
305                                       sessionPtr->securityNameLen = securityNameLen;
306 sahana.prabhakar 1.25                 memcpy(sessionPtr->securityName, (const char*)securityNameCStr,
307                                           securityNameLen);
308                       
309                                       CString engineIdCStr = engineID.getCString();
310                                       size_t engineIdHexLen = strlen(engineIdCStr);
311                                       size_t engineIdBinLen = 0;
312                                       u_char *engineIdBin = (u_char *)calloc(1,engineIdHexLen);
313                                       free(sessionPtr->securityEngineID);
314                                       if(!snmp_hex_to_binary(&engineIdBin, &engineIdHexLen, 
315                                           &engineIdBinLen, 1,engineIdCStr))
316                                       {
317                                           PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
318                                               "Snmp Indication Handler failed to generate binary"
319                                                   " engine ID for sending the SNMPv3 trap.");
320                                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, 
321                                               MessageLoaderParms(
322                                                   "Handler.snmpIndicationHandler."
323                                                       "snmpIndicationHandler."
324                                                   "FAILED_TO_DELIVER_TRAP",
325                                               "Failed to deliver trap."));
326                                       }
327 sahana.prabhakar 1.25                 sessionPtr->securityEngineIDLen = engineIdBinLen;
328                                       sessionPtr->securityEngineID = engineIdBin; 
329                       
330                                       switch(snmpSecLevel)
331                                       {
332                                           case 1:
333                                               sessionPtr->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
334                                               break;
335                                           case 2:
336                                               sessionPtr->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
337                                               break;
338                                           case 3:
339                                               sessionPtr->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
340                                               break;
341                                           default:
342                                               //use the dedault in the net-snmp conf file.
343                                               break;
344                                       }
345                        
346                                       SNMP_FREE(sessionPtr->securityAuthProto);
347                                       if(snmpSecAuthProto == 1) // MD5
348 sahana.prabhakar 1.25                 {
349                                           sessionPtr->securityAuthProto = snmp_duplicate_objid(
350                                               usmHMACMD5AuthProtocol,
351                                               USM_AUTH_PROTO_MD5_LEN);
352                                           sessionPtr->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
353                                       }
354                                       else if(snmpSecAuthProto == 2)// SHA
355                                       {
356                                           sessionPtr->securityAuthProto = snmp_duplicate_objid(
357                                               usmHMACSHA1AuthProtocol,
358                                               USM_AUTH_PROTO_SHA_LEN);
359                                           sessionPtr->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
360                                       }
361                                       // use the default in net-snmp conf files.
362                       
363                                       if(snmpSecAuthKey.size() > 0)
364                                       {
365                                           for(Uint32 i=0;i<snmpSecAuthKey.size();i++)
366                                           {
367                                               sessionPtr->securityAuthKey[i] = snmpSecAuthKey[i];
368                                           }
369 sahana.prabhakar 1.25                     sessionPtr->securityAuthKeyLen = snmpSecAuthKey.size();
370                                       }
371                       
372                                       SNMP_FREE(sessionPtr->securityPrivProto);
373                                       //Privacy
374                                       if(snmpSecPrivProto == 1) //DES
375                                       {
376                                           sessionPtr->securityPrivProto = snmp_duplicate_objid(
377                                               usmDESPrivProtocol,
378                                               USM_PRIV_PROTO_DES_LEN);
379                                           sessionPtr->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
380                                       }
381                                       else if(snmpSecPrivProto == 2) // AES
382                                       {
383                                           sessionPtr->securityPrivProto = snmp_duplicate_objid(
384                                               usmAESPrivProtocol,
385                                               USM_PRIV_PROTO_AES_LEN);
386                                           sessionPtr->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
387                                       }
388                                       // use the defaults in net-snmp conf files
389                        
390 sahana.prabhakar 1.25                 // Privacy Key
391                                       if(snmpSecPrivKey.size() > 0)
392                                       {
393                                           for(Uint32 j=0;j<snmpSecPrivKey.size();j++)
394                                           {
395                                               sessionPtr->securityPrivKey[j] = snmpSecPrivKey[j];
396                                           }
397                                           sessionPtr->securityPrivKeyLen = snmpSecPrivKey.size();
398                                       }
399                                       break;
400                                   }
401                                   default:
402                                   {
403                                       PEG_METHOD_EXIT();
404                                       throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED,
405                                           MessageLoaderParms(
406                                               _MSG_VERSION_NOT_SUPPORTED_KEY,
407                                               _MSG_VERSION_NOT_SUPPORTED));
408                                       break;
409                                   }
410 yi.zhou          1.1          }
411                       
412                           }
413                           catch (...)
414                           {
415                               _destroySession(sessionHandle);
416                       
417 kumpf            1.10         PEG_METHOD_EXIT();
418 yi.zhou          1.1          throw;
419                           }
420                       
421 kumpf            1.10     PEG_METHOD_EXIT();
422 yi.zhou          1.1  }
423                       
424 sahana.prabhakar 1.25 void snmpDeliverTrap_netsnmp::_addCommunity(
425                           struct snmp_session*& sessionPtr,
426                           const String& securityName)
427                       {
428                           PEG_METHOD_ENTER(TRC_IND_HANDLER,
429                               "snmpDeliverTrap_netsnmp::_addCommunity");
430                       
431                           // Community Name, default is public
432                           String communityName;
433                           if (securityName.size() == 0)
434                           {
435                               communityName.assign("public");
436                           }
437                           else
438                           {
439                               communityName = securityName;
440                           }
441                       
442                           free(sessionPtr->community);
443                                     
444                           CString communityNameCStr = communityName.getCString();
445 sahana.prabhakar 1.25     size_t communityNameLen = strlen(communityNameCStr);
446                       
447                           sessionPtr->community = (u_char*)calloc(1,communityNameLen+1);
448                       
449                           memcpy(sessionPtr->community, (const char*)communityNameCStr,
450                               communityNameLen);
451                       
452                           sessionPtr->community_len = communityNameLen;
453                           PEG_METHOD_EXIT();
454                       } 
455                       
456 yi.zhou          1.1  // Creates a SNMP session
457                       void snmpDeliverTrap_netsnmp::_destroySession(
458 kumpf            1.10     void* sessionHandle)
459 yi.zhou          1.1  {
460 kumpf            1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
461 yi.zhou          1.1          "snmpDeliverTrap_netsnmp::_destroySession");
462                       
463                           snmp_sess_close(sessionHandle);
464                       
465 kumpf            1.10     PEG_METHOD_EXIT();
466 yi.zhou          1.1  }
467                       
468 kumpf            1.10 // Creates a SNMP TRAP PDU
469 yi.zhou          1.1  void snmpDeliverTrap_netsnmp::_createPdu(
470                           Uint16 snmpVersion,
471                           const String& trapOid,
472 kumpf            1.10     snmp_session*& sessionPtr,
473                           snmp_pdu*& snmpPdu)
474 yi.zhou          1.1  {
475 kumpf            1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::_createPdu");
476 yi.zhou          1.1  
477                           oid _SYSTEM_UP_TIME_OID [] = {1,3,6,1,2,1,1,3,0};
478                           oid _SNMPTRAP_OID [] = {1,3,6,1,6,3,1,1,4,1,0};
479                       
480 kumpf            1.10     in_addr_t* pduInAddr;
481 yi.zhou          1.1  
482                           switch (snmpVersion)
483                           {
484                               case _SNMPv1_TRAP:
485                               {
486                                   // Create the PDU
487 kumpf            1.10             snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP);
488                       
489 yi.zhou          1.1              // Failed to create pdu
490                                   if (!snmpPdu)
491                                   {
492 kumpf            1.10                 PEG_METHOD_EXIT();
493                                       throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
494                                           MessageLoaderParms(
495                                               _MSG_PDU_CREATE_FAILED_KEY,
496                                               _MSG_PDU_CREATE_FAILED));
497 yi.zhou          1.1              }
498                       
499                                   // Make sure that the v1 trap PDU includes the local IP address
500                                   pduInAddr = (in_addr_t*) snmpPdu->agent_addr;
501                                   *pduInAddr = get_myaddr();
502                       
503                                   // get system up time
504                                   snmpPdu->time = get_uptime();
505                       
506                                   // Pack trap information into the PDU
507                                   try
508                                   {
509                                       _packTrapInfoIntoPdu(trapOid, snmpPdu);
510                                   }
511                                   catch (CIMException& e)
512                                   {
513 kumpf            1.10                 Logger::put_l(
514                                           Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
515 kumpf            1.18                     MessageLoaderParms(
516                                               _MSG_PACK_TRAP_INFO_INTO_PDU_FAILED_KEY,
517                                               _MSG_PACK_TRAP_INFO_INTO_PDU_FAILED,
518                                               e.getMessage()));
519 yi.zhou          1.1              }
520                       
521                                   break;
522                               }
523                               case _SNMPv2C_TRAP:
524 sahana.prabhakar 1.25         case _SNMPv3_TRAP:
525 yi.zhou          1.1          {
526                                   // Create the PDU
527 kumpf            1.10             snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP2);
528                       
529 yi.zhou          1.1              // Failed to create pdu
530                                   if (!snmpPdu)
531                                   {
532 kumpf            1.10                 PEG_METHOD_EXIT();
533 yi.zhou          1.1  
534 kumpf            1.10                 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
535                                           MessageLoaderParms(
536                                               _MSG_PDU_CREATE_FAILED_KEY,
537                                               _MSG_PDU_CREATE_FAILED));
538 yi.zhou          1.1              }
539                       
540                                   // Add sysUpTime to the PDU
541                                   char sysUpTime[32];
542                                   sprintf(sysUpTime, "%ld", get_uptime());
543 kumpf            1.10 
544 yi.zhou          1.1              Sint32 retCode;
545 kumpf            1.10             retCode = snmp_add_var(
546                                       snmpPdu,
547                                       _SYSTEM_UP_TIME_OID,
548                                       OID_LENGTH(_SYSTEM_UP_TIME_OID),
549                                       't',
550                                       sysUpTime);
551 yi.zhou          1.1  
552                                   // Failed to add sysUpTime to the pdu
553                                   if (retCode != 0)
554                                   {
555                                       String errMsg = snmp_api_errstring(retCode);
556                       
557 kumpf            1.10                 Logger::put_l(
558                                           Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
559 kumpf            1.18                     MessageLoaderParms(
560                                               _MSG_ADD_SYSUPTIME_TO_PDU_FAILED_KEY,
561                                               _MSG_ADD_SYSUPTIME_TO_PDU_FAILED,
562                                               errMsg));
563 yi.zhou          1.1              }
564                       
565                                   // Add snmp trap to the PDU
566 kumpf            1.10             retCode = snmp_add_var(
567                                       snmpPdu,
568                                       _SNMPTRAP_OID,
569                                       OID_LENGTH(_SNMPTRAP_OID),
570                                       'o',
571                                       trapOid.getCString());
572 yi.zhou          1.1  
573                                   // Failed to add snmp trap to the pdu
574                                   if (retCode != 0)
575                                   {
576                                       String errMsg = snmp_api_errstring(retCode);
577                       
578 kumpf            1.10                 Logger::put_l(
579                                           Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
580 kumpf            1.18                     MessageLoaderParms(
581                                               _MSG_ADD_SNMP_TRAP_TO_PDU_FAILED_KEY,
582                                               _MSG_ADD_SNMP_TRAP_TO_PDU_FAILED,
583                                               errMsg));
584 yi.zhou          1.1              }
585                       
586                                   break;
587                               }
588                               default:
589                               {
590 kumpf            1.10             PEG_METHOD_EXIT();
591 yi.zhou          1.1  
592                                   throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED,
593 kumpf            1.10                 MessageLoaderParms(
594                                           _MSG_VERSION_NOT_SUPPORTED_KEY,
595                                           _MSG_VERSION_NOT_SUPPORTED));
596 yi.zhou          1.1          }
597                           }
598                       
599 kumpf            1.10     PEG_METHOD_EXIT();
600 yi.zhou          1.1  }
601                       
602                       
603                       // Pack trap information into the PDU
604                       void snmpDeliverTrap_netsnmp::_packTrapInfoIntoPdu(
605 kumpf            1.10     const String& trapOid,
606                           snmp_pdu* snmpPdu)
607 yi.zhou          1.1  {
608 kumpf            1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
609 yi.zhou          1.1          "snmpDeliverTrap_netsnmp::_packTrapInfoIntoPdu");
610                       
611                           oid enterpriseOid[MAX_OID_LEN];
612 kumpf            1.10     size_t enterpriseOidLength;
613 yi.zhou          1.1  
614                           Array<String> standard_traps;
615                       
616                           standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
617                           standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
618                           standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
619                           standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
620                           standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
621                           standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
622                       
623 kumpf            1.10     Array<const char*> oidSubIdentifiers;
624 yi.zhou          1.1  
625                           CString trapOidCStr = trapOid.getCString();
626                       
627 kumpf            1.10     char* trapOidCopy = strdup(trapOidCStr);
628 sahana.prabhakar 1.25     char* numericEntOid = (char*) malloc(strlen(trapOidCStr)+1);
629 yi.zhou          1.9  
630                           try
631                           {
632 yi.zhou          1.1  
633 a.dunfey         1.12 #if !defined(PEGASUS_OS_TYPE_WINDOWS) && !defined(PEGASUS_OS_ZOS)
634 kumpf            1.10         char* last;
635                               for (const char* p = strtok_r(trapOidCopy, ".", &last); p;
636 yi.zhou          1.9              p=strtok_r(NULL, ".", &last))
637 yi.zhou          1.1  #else
638 yi.zhou          1.9          for (const char* p = strtok(trapOidCopy, "."); p; p=strtok(NULL, "."))
639 yi.zhou          1.1  #endif
640 yi.zhou          1.9          {
641                                   oidSubIdentifiers.append(p);
642                               }
643 yi.zhou          1.1  
644 yi.zhou          1.9          long genTrap = 0;
645                               long specTrap = 0;
646 yi.zhou          1.1  
647 yi.zhou          1.9          enterpriseOidLength = MAX_OID_LEN;
648 yi.zhou          1.1  
649 yi.zhou          1.9          if (Contains(standard_traps, trapOid))
650                               {
651                                   //
652                                   // if the trapOid is one of the standard traps,
653                                   // then the SNMPV1 enterprise parameter must be set
654                                   // to the value of the trapOid, the generic-trap
655                                   // parameter must be set to one of (0 - 5), and the
656                                   // specific-trap parameter must be set to 0
657                                   //
658                       
659                                   // Convert trapOid from numeric form to a list of subidentifiers
660 kumpf            1.10             if (read_objid((const char*)trapOidCStr, enterpriseOid,
661 yi.zhou          1.9                  &enterpriseOidLength) == 0)
662                                   {
663 kumpf            1.10                 // Failed to parse trapOid
664 yi.zhou          1.9  
665 kumpf            1.10                 PEG_METHOD_EXIT();
666                                       throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
667 yi.zhou          1.9                      MessageLoaderParms(_MSG_READ_OBJID_FAILED_KEY,
668                                                              _MSG_READ_OBJID_FAILED,
669                                                              trapOid));
670                                   }
671                       
672                                   // the generic trap is last sub-identifier of the
673                                   // trapOid minus 1
674 kumpf            1.10             snmpPdu->trap_type =
675 yi.zhou          1.9                  atoi(oidSubIdentifiers[oidSubIdentifiers.size() - 1]) - 1;
676                                   snmpPdu->specific_type = 0;
677 yi.zhou          1.1          }
678 yi.zhou          1.9          else
679 yi.zhou          1.1          {
680 yi.zhou          1.9              //
681                                   // if the trapOid is not one of the standard traps:
682                                   // then 1) the generic-trap parameter must be set to 6,
683                                   // 2) if the next-to-last sub-identifier of the
684                                   // trapOid is zero, then the SNMPV1 enterprise
685                                   // parameter is the trapOid with the last 2
686                                   // sub-identifiers removed, otherwise, the
687                                   // SNMPV1 enterprise parameter is the trapOid
688                                   // with the last sub-identifier removed;
689                                   // 3) the SNMPv1 specific-trap parameter is the last
690                                   // sub-identifier of the trapOid;
691                                   //
692                       
693                                   snmpPdu->trap_type = 6;
694                       
695 kumpf            1.10             snmpPdu->specific_type =
696 yi.zhou          1.9                  atoi(oidSubIdentifiers[oidSubIdentifiers.size()-1]);
697                       
698                                   strcpy(numericEntOid, oidSubIdentifiers[0]);
699                                   for (Uint32 i = 1; i < oidSubIdentifiers.size()-2; i++)
700                                   {
701                                       strcat(numericEntOid, ".");
702                                       strcat(numericEntOid, oidSubIdentifiers[i]);
703                                   }
704                       
705                                   if (strcmp(oidSubIdentifiers[oidSubIdentifiers.size()-2], "0") != 0)
706                                   {
707                                       strcat(numericEntOid, ".");
708 kumpf            1.10                 strcat(numericEntOid,
709 yi.zhou          1.9                     oidSubIdentifiers[oidSubIdentifiers.size()-2]);
710                                   }
711 yi.zhou          1.1  
712 kumpf            1.10             // Convert ent from numeric form to a list of subidentifiers
713                                   if (read_objid(numericEntOid, enterpriseOid,
714 yi.zhou          1.9                  &enterpriseOidLength) == 0)
715                                   {
716                                       // Failed to parse numericEntOid
717 yi.zhou          1.1  
718 kumpf            1.10                 PEG_METHOD_EXIT();
719 yi.zhou          1.1  
720 kumpf            1.10                 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
721 yi.zhou          1.9                      MessageLoaderParms(_MSG_READ_ENTOID_FAILED_KEY,
722                                                              _MSG_READ_ENTOID_FAILED,
723                                                              String(numericEntOid)));
724                                   }
725 yi.zhou          1.1  
726                               }
727                       
728 sahana.prabhakar 1.25         SNMP_FREE(snmpPdu->enterprise);
729 yi.zhou          1.9          snmpPdu->enterprise = (oid*) malloc(enterpriseOidLength * sizeof(oid));
730 sahana.prabhakar 1.25         memcpy(snmpPdu->enterprise, enterpriseOid, 
731 kumpf            1.10             enterpriseOidLength * sizeof(oid));
732 yi.zhou          1.9  
733                               snmpPdu->enterprise_length = enterpriseOidLength;
734 yi.zhou          1.1      }
735 yi.zhou          1.9      catch (...)
736                           {
737                               free(trapOidCopy);
738                               free(numericEntOid);
739 yi.zhou          1.1  
740 yi.zhou          1.9          PEG_METHOD_EXIT();
741                               throw;
742                           }
743 yi.zhou          1.1  
744                           free(trapOidCopy);
745                           free(numericEntOid);
746                       
747 kumpf            1.10     PEG_METHOD_EXIT();
748 yi.zhou          1.1  }
749                       
750                       // Pack oids into the PDU
751                       void snmpDeliverTrap_netsnmp::_packOidsIntoPdu(
752                           const Array<String>& vbOids,
753                           const Array<String>& vbTypes,
754                           const Array<String>& vbValues,
755 kumpf            1.10     snmp_pdu* snmpPdu)
756 yi.zhou          1.1  {
757                       
758 kumpf            1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
759 yi.zhou          1.1          "snmpDeliverTrap_netsnmp::_packOidsIntoPdu");
760                       
761                           char dataType;
762                           oid vbOid[MAX_OID_LEN];
763                           size_t vbOidLength = MAX_OID_LEN;
764                       
765                           for (Uint32 i=0; i < vbOids.size(); i++)
766                           {
767                               if (vbTypes[i] == "OctetString")
768                               {
769                                   dataType = 's';
770                               }
771                               else if (vbTypes[i] == "Integer")
772                               {
773                                   dataType = 'i';
774 kumpf            1.10         }
775 yi.zhou          1.1          else if (vbTypes[i] == "OID")
776                               {
777                                   dataType = 'o';
778                               }
779                               else
780                               {
781 kumpf            1.10             // Integer, OctetString, and OID are supported SNMP Data Types
782 yi.zhou          1.1              // for the CIM Property
783                       
784 kumpf            1.10             PEG_METHOD_EXIT();
785 yi.zhou          1.1  
786 kumpf            1.10             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
787 yi.zhou          1.1                  MessageLoaderParms(_MSG_UNSUPPORTED_SNMP_DATA_TYPE_KEY,
788                                                          _MSG_UNSUPPORTED_SNMP_DATA_TYPE,
789                                                          vbTypes[i]));
790                       
791                               }
792                       
793 kumpf            1.10         // Convert oid of a CIM property from numeric form to a list of
794                               // subidentifiers
795                               if (read_objid((const char*)vbOids[i].getCString(), vbOid,
796 yi.zhou          1.1              &vbOidLength) == 0)
797                               {
798                                   // Failed to parse vbOids
799                       
800 kumpf            1.10             PEG_METHOD_EXIT();
801 yi.zhou          1.1  
802 kumpf            1.10             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
803 yi.zhou          1.1                  MessageLoaderParms(_MSG_PARSE_CIM_PROPERTY_OID_FAILED_KEY,
804                                                          _MSG_PARSE_CIM_PROPERTY_OID_FAILED,
805                                                          vbOids[i]));
806                               }
807                       
808                               Sint32 retCode;
809 kumpf            1.10         retCode = snmp_add_var(snmpPdu, vbOid, vbOidLength, dataType,
810 yi.zhou          1.1                                 vbValues[i].getCString());
811                       
812                               // Failed to add vbOid to the pdu
813                               if (retCode != 0)
814                               {
815 kumpf            1.10             PEG_METHOD_EXIT();
816 yi.zhou          1.1  
817 kumpf            1.10             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
818 yi.zhou          1.1                  MessageLoaderParms(_MSG_ADD_VAR_TO_PDU_FAILED_KEY,
819                                                          _MSG_ADD_VAR_TO_PDU_FAILED,
820                                                          vbOids[i],
821 kumpf            1.10                                    String(snmp_api_errstring(retCode))));
822 yi.zhou          1.1  
823                               }
824                           }
825                       
826 kumpf            1.10     PEG_METHOD_EXIT();
827 yi.zhou          1.1  }
828                       
829                       PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2