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

  1 karl  1.3 //%2006////////////////////////////////////////////////////////////////////////
  2 yi.zhou 1.1 //
  3             // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4             // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5             // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6             // IBM Corp.; EMC Corporation, The Open Group.
  7             // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8             // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9             // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10             // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl    1.3 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12             // EMC Corporation; Symantec Corporation; The Open Group.
 13 yi.zhou 1.1 //
 14             // Permission is hereby granted, free of charge, to any person obtaining a copy
 15             // of this software and associated documentation files (the "Software"), to
 16             // deal in the Software without restriction, including without limitation the
 17             // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18             // sell copies of the Software, and to permit persons to whom the Software is
 19             // furnished to do so, subject to the following conditions:
 20             // 
 21             // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22             // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23             // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24             // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25             // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26             // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27             // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28             // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29             //
 30             //==============================================================================
 31             //
 32             //%/////////////////////////////////////////////////////////////////////////////
 33             
 34 yi.zhou 1.1 #include <Pegasus/Common/Packer.h>
 35             #include <Pegasus/Common/Tracer.h>
 36             #include "snmpDeliverTrap_netsnmp.h"
 37             
 38             PEGASUS_NAMESPACE_BEGIN
 39             
 40 yi.zhou 1.4 void snmpDeliverTrap_netsnmp::initialize()
 41             {
 42 kumpf   1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::initialize");
 43 yi.zhou 1.4  
 44 kumpf   1.10     // Defined default MIB modules (in net-snmp-config.h) do not need to be
 45 yi.zhou 1.4      // loaded and loading them can cause some stderr;
 46                  // use environment variable MIBS to override the default MIB modules.
 47 kumpf   1.10     // If there is no MIBS environment variable, add it in.
 48                  char* envVar;
 49 yi.zhou 1.4      envVar = getenv("MIBS");
 50              
 51                  if (envVar == NULL)
 52                  {
 53                      putenv("MIBS=");
 54                  }
 55              
 56                  // Initialize the mib reader
 57                  netsnmp_set_mib_directory("");
 58                  init_mib();
 59 kumpf   1.10 
 60 yi.zhou 1.4      // Initializes the SNMP library
 61                  init_snmp("snmpIndicationHandler");
 62              
 63 yi.zhou 1.20     // don't load/save persistent file
 64              #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
 65                  netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
 66                      NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
 67              #endif
 68              
 69 yi.zhou 1.4      // windows32 specific initialization (is a NOOP on unix)
 70                  SOCK_STARTUP;
 71              
 72 kumpf   1.10     PEG_METHOD_EXIT();
 73 yi.zhou 1.4  }
 74              
 75              void snmpDeliverTrap_netsnmp::terminate()
 76              {
 77 kumpf   1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::terminate");
 78 yi.zhou 1.4  
 79 yi.zhou 1.13     // Shuts down the application, and appropriate clean up
 80                  snmp_shutdown("snmpIndicationHandler");
 81                 
 82 yi.zhou 1.4      SOCK_CLEANUP;
 83              
 84 kumpf   1.10     PEG_METHOD_EXIT();
 85 yi.zhou 1.4  }
 86              
 87 yi.zhou 1.1  void snmpDeliverTrap_netsnmp::deliverTrap(
 88 kumpf   1.10     const String& trapOid,
 89                  const String& securityName,
 90                  const String& targetHost,
 91                  const Uint16& targetHostFormat,
 92                  const String& otherTargetHostFormat,
 93                  const Uint32& portNumber,
 94                  const Uint16& snmpVersion,
 95                  const String& engineID,
 96                  const Array<String>& vbOids,
 97                  const Array<String>& vbTypes,
 98                  const Array<String>& vbValues)
 99 yi.zhou 1.1  {
100              
101 kumpf   1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::deliverTrap");
102 yi.zhou 1.1  
103 kumpf   1.10     void* sessionHandle;
104                  struct snmp_session* sessionPtr;
105 yi.zhou 1.1  
106 kumpf   1.10     struct snmp_pdu* snmpPdu;
107 yi.zhou 1.1  
108                  // Creates a SNMP session
109 yi.zhou 1.16     _createSession(targetHost, targetHostFormat, portNumber, securityName,
110 yi.zhou 1.1                     sessionHandle, sessionPtr);
111              
112                  try
113                  {
114                      _createPdu(snmpVersion, trapOid, sessionPtr, snmpPdu);
115                  }
116                  catch (...)
117                  {
118                      _destroySession(sessionHandle);
119              
120 kumpf   1.10         PEG_METHOD_EXIT();
121 yi.zhou 1.1          throw;
122                  }
123              
124                  // Pack OIDs into the PDU
125                  try
126                  {
127                      _packOidsIntoPdu(vbOids, vbTypes, vbValues, snmpPdu);
128                  }
129                  catch (Exception& e)
130                  {
131 kumpf   1.18         Logger::put_l(Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
132                          MessageLoaderParms(
133                              _MSG_PACK_CIM_PROPERTY_TO_PDU_FAILED_KEY,
134                              _MSG_PACK_CIM_PROPERTY_TO_PDU_FAILED,
135                              e.getMessage()));
136 yi.zhou 1.1      }
137                  catch (...)
138                  {
139 marek   1.14         PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
140 kumpf   1.10             "Snmp Indication Handler failed to pack a CIM "
141                              "Property into the SNMP PDU: Unknown exception.");
142 yi.zhou 1.1      }
143              
144                  // Send the trap to the destination
145                  if (snmp_sess_send(sessionHandle, snmpPdu) == 0)
146                  {
147                      Sint32 libErr, sysErr;
148 kumpf   1.10         char* errStr;
149 yi.zhou 1.1  
150                      // snmp_sess_send failed
151                      // get library, system errno
152                      snmp_sess_error(sessionHandle, &libErr, &sysErr, &errStr);
153              
154                      String exceptionStr = _MSG_SESSION_SEND_FAILED;
155 kumpf   1.10         exceptionStr.append(errStr);
156 yi.zhou 1.1  
157                      free(errStr);
158              
159 yi.zhou 1.19         snmp_free_pdu(snmpPdu);
160              
161 yi.zhou 1.1          _destroySession(sessionHandle);
162              
163 kumpf   1.10         PEG_METHOD_EXIT();
164 yi.zhou 1.1  
165 kumpf   1.10         throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
166                          MessageLoaderParms(_MSG_SESSION_SEND_FAILED_KEY, exceptionStr));
167 yi.zhou 1.1      }
168              
169                  _destroySession(sessionHandle);
170              
171 kumpf   1.10     PEG_METHOD_EXIT();
172 yi.zhou 1.1  }
173              
174              // Creates a SNMP session
175              void snmpDeliverTrap_netsnmp::_createSession(
176 kumpf   1.10     const String& targetHost,
177 yi.zhou 1.16     Uint16 targetHostFormat,
178 yi.zhou 1.1      Uint32 portNumber,
179 kumpf   1.10     const String& securityName,
180                  void*& sessionHandle,
181                  snmp_session*& sessionPtr)
182 yi.zhou 1.1  {
183 kumpf   1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
184 yi.zhou 1.1          "snmpDeliverTrap_netsnmp::_createSession");
185              
186                  Sint32 libErr, sysErr;
187 kumpf   1.10     char* errStr;
188 yi.zhou 1.1      String exceptionStr;
189 kumpf   1.10 
190 yi.zhou 1.4      struct snmp_session snmpSession;
191 yi.zhou 1.1  
192 yi.zhou 1.2      {
193 kumpf   1.10         AutoMutex autoMut(_sessionInitMutex);
194                      snmp_sess_init(&snmpSession);
195 yi.zhou 1.1  
196 kumpf   1.10         CString targetHostCStr = targetHost.getCString();
197 yi.zhou 1.1  
198 kumpf   1.10         // peername has format: targetHost:portNumber
199                      snmpSession.peername =
200                          (char*)malloc((size_t)(strlen(targetHostCStr) + 1 + 32));
201 yi.zhou 1.16 
202                      if (targetHostFormat == _IPV6_ADDRESS)
203                      {
204                          sprintf(snmpSession.peername, "udp6:[%s]:%u",
205                              (const char*)targetHostCStr,
206                              portNumber);
207                      }
208                      else
209                      {
210                          sprintf(snmpSession.peername, "%s:%u",
211                              (const char*)targetHostCStr,
212                              portNumber);
213                      }
214 yi.zhou 1.4  
215 kumpf   1.10         sessionHandle = snmp_sess_open(&snmpSession);
216 yi.zhou 1.4      }
217 yi.zhou 1.1  
218                  if (sessionHandle == NULL)
219                  {
220                      exceptionStr = _MSG_SESSION_OPEN_FAILED;
221              
222                      // Get library, system errno
223 yi.zhou 1.5          snmp_error(&snmpSession, &libErr, &sysErr, &errStr);
224 yi.zhou 1.1  
225                      exceptionStr.append(errStr);
226              
227                      free(errStr);
228              
229 yi.zhou 1.19         free(snmpSession.peername);
230              
231 kumpf   1.10         PEG_METHOD_EXIT();
232 yi.zhou 1.1  
233 kumpf   1.10         throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
234                          MessageLoaderParms(_MSG_SESSION_OPEN_FAILED_KEY, exceptionStr));
235 yi.zhou 1.1      }
236              
237                  try
238                  {
239                      // get the snmp_session pointer
240                      sessionPtr = snmp_sess_session(sessionHandle);
241                      if (sessionPtr == NULL)
242                      {
243                          exceptionStr = _MSG_GET_SESSION_POINT_FAILED;
244              
245                          // Get library, system errno
246                          snmp_sess_error(&snmpSession, &libErr, &sysErr, &errStr);
247              
248                          exceptionStr.append(errStr);
249              
250                          free(errStr);
251              
252 yi.zhou 1.19             free(snmpSession.peername);
253              
254 kumpf   1.10             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
255                              _MSG_GET_SESSION_POINTER_FAILED_KEY,
256                              exceptionStr));
257 yi.zhou 1.1          }
258              
259                      // Community Name, default is public
260                      String communityName;
261                      if (securityName.size() == 0)
262                      {
263                          communityName.assign("public");
264                      }
265                      else
266                      {
267                          communityName = securityName;
268                      }
269              
270 yi.zhou 1.7          free(snmpSession.peername);
271 yi.zhou 1.1  
272 yi.zhou 1.7          free(sessionPtr->community);
273 yi.zhou 1.1  
274                      CString communityNameCStr = communityName.getCString();
275                      size_t communityNameLen = strlen(communityNameCStr);
276              
277                      sessionPtr->community = (u_char*)malloc(communityNameLen);
278              
279 kumpf   1.10         memcpy(sessionPtr->community, (const char*)communityNameCStr,
280 yi.zhou 1.1                 communityNameLen);
281                      sessionPtr->community_len = communityNameLen;
282                  }
283                  catch (...)
284                  {
285                      _destroySession(sessionHandle);
286              
287 kumpf   1.10         PEG_METHOD_EXIT();
288 yi.zhou 1.1          throw;
289                  }
290              
291 kumpf   1.10     PEG_METHOD_EXIT();
292 yi.zhou 1.1  }
293              
294              // Creates a SNMP session
295              void snmpDeliverTrap_netsnmp::_destroySession(
296 kumpf   1.10     void* sessionHandle)
297 yi.zhou 1.1  {
298 kumpf   1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
299 yi.zhou 1.1          "snmpDeliverTrap_netsnmp::_destroySession");
300              
301                  snmp_sess_close(sessionHandle);
302              
303 kumpf   1.10     PEG_METHOD_EXIT();
304 yi.zhou 1.1  }
305              
306 kumpf   1.10 // Creates a SNMP TRAP PDU
307 yi.zhou 1.1  void snmpDeliverTrap_netsnmp::_createPdu(
308                  Uint16 snmpVersion,
309                  const String& trapOid,
310 kumpf   1.10     snmp_session*& sessionPtr,
311                  snmp_pdu*& snmpPdu)
312 yi.zhou 1.1  {
313 kumpf   1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::_createPdu");
314 yi.zhou 1.1  
315                  oid _SYSTEM_UP_TIME_OID [] = {1,3,6,1,2,1,1,3,0};
316                  oid _SNMPTRAP_OID [] = {1,3,6,1,6,3,1,1,4,1,0};
317              
318 kumpf   1.10     in_addr_t* pduInAddr;
319 yi.zhou 1.1  
320                  switch (snmpVersion)
321                  {
322                      case _SNMPv1_TRAP:
323                      {
324              
325                          sessionPtr->version = SNMP_VERSION_1;
326              
327                          // Create the PDU
328 kumpf   1.10             snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP);
329              
330 yi.zhou 1.1              // Failed to create pdu
331                          if (!snmpPdu)
332                          {
333 kumpf   1.10                 PEG_METHOD_EXIT();
334                              throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
335                                  MessageLoaderParms(
336                                      _MSG_PDU_CREATE_FAILED_KEY,
337                                      _MSG_PDU_CREATE_FAILED));
338 yi.zhou 1.1              }
339              
340                          // Make sure that the v1 trap PDU includes the local IP address
341                          pduInAddr = (in_addr_t*) snmpPdu->agent_addr;
342                          *pduInAddr = get_myaddr();
343              
344                          // get system up time
345                          snmpPdu->time = get_uptime();
346              
347                          // Pack trap information into the PDU
348                          try
349                          {
350                              _packTrapInfoIntoPdu(trapOid, snmpPdu);
351                          }
352                          catch (CIMException& e)
353                          {
354 kumpf   1.10                 Logger::put_l(
355                                  Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
356 kumpf   1.18                     MessageLoaderParms(
357                                      _MSG_PACK_TRAP_INFO_INTO_PDU_FAILED_KEY,
358                                      _MSG_PACK_TRAP_INFO_INTO_PDU_FAILED,
359                                      e.getMessage()));
360 yi.zhou 1.1              }
361              
362                          break;
363                      }
364                      case _SNMPv2C_TRAP:
365                      {
366                          sessionPtr->version = SNMP_VERSION_2c;
367              
368                          // Create the PDU
369 kumpf   1.10             snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP2);
370              
371 yi.zhou 1.1              // Failed to create pdu
372                          if (!snmpPdu)
373                          {
374 kumpf   1.10                 PEG_METHOD_EXIT();
375 yi.zhou 1.1  
376 kumpf   1.10                 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
377                                  MessageLoaderParms(
378                                      _MSG_PDU_CREATE_FAILED_KEY,
379                                      _MSG_PDU_CREATE_FAILED));
380 yi.zhou 1.1              }
381              
382                          // Add sysUpTime to the PDU
383                          char sysUpTime[32];
384                          sprintf(sysUpTime, "%ld", get_uptime());
385 kumpf   1.10 
386 yi.zhou 1.1              Sint32 retCode;
387 kumpf   1.10             retCode = snmp_add_var(
388                              snmpPdu,
389                              _SYSTEM_UP_TIME_OID,
390                              OID_LENGTH(_SYSTEM_UP_TIME_OID),
391                              't',
392                              sysUpTime);
393 yi.zhou 1.1  
394                          // Failed to add sysUpTime to the pdu
395                          if (retCode != 0)
396                          {
397                              String errMsg = snmp_api_errstring(retCode);
398              
399 kumpf   1.10                 Logger::put_l(
400                                  Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
401 kumpf   1.18                     MessageLoaderParms(
402                                      _MSG_ADD_SYSUPTIME_TO_PDU_FAILED_KEY,
403                                      _MSG_ADD_SYSUPTIME_TO_PDU_FAILED,
404                                      errMsg));
405 yi.zhou 1.1              }
406              
407                          // Add snmp trap to the PDU
408 kumpf   1.10             retCode = snmp_add_var(
409                              snmpPdu,
410                              _SNMPTRAP_OID,
411                              OID_LENGTH(_SNMPTRAP_OID),
412                              'o',
413                              trapOid.getCString());
414 yi.zhou 1.1  
415                          // Failed to add snmp trap to the pdu
416                          if (retCode != 0)
417                          {
418                              String errMsg = snmp_api_errstring(retCode);
419              
420 kumpf   1.10                 Logger::put_l(
421                                  Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
422 kumpf   1.18                     MessageLoaderParms(
423                                      _MSG_ADD_SNMP_TRAP_TO_PDU_FAILED_KEY,
424                                      _MSG_ADD_SNMP_TRAP_TO_PDU_FAILED,
425                                      errMsg));
426 yi.zhou 1.1              }
427              
428                          break;
429                      }
430                      default:
431                      {
432 kumpf   1.10             PEG_METHOD_EXIT();
433 yi.zhou 1.1  
434                          throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED,
435 kumpf   1.10                 MessageLoaderParms(
436                                  _MSG_VERSION_NOT_SUPPORTED_KEY,
437                                  _MSG_VERSION_NOT_SUPPORTED));
438 yi.zhou 1.1          }
439                  }
440              
441 kumpf   1.10     PEG_METHOD_EXIT();
442 yi.zhou 1.1  }
443              
444              
445              // Pack trap information into the PDU
446              void snmpDeliverTrap_netsnmp::_packTrapInfoIntoPdu(
447 kumpf   1.10     const String& trapOid,
448                  snmp_pdu* snmpPdu)
449 yi.zhou 1.1  {
450 kumpf   1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
451 yi.zhou 1.1          "snmpDeliverTrap_netsnmp::_packTrapInfoIntoPdu");
452              
453                  oid enterpriseOid[MAX_OID_LEN];
454 kumpf   1.10     size_t enterpriseOidLength;
455 yi.zhou 1.1  
456                  Array<String> standard_traps;
457              
458                  standard_traps.append(String("1.3.6.1.6.3.1.1.5.1"));
459                  standard_traps.append(String("1.3.6.1.6.3.1.1.5.2"));
460                  standard_traps.append(String("1.3.6.1.6.3.1.1.5.3"));
461                  standard_traps.append(String("1.3.6.1.6.3.1.1.5.4"));
462                  standard_traps.append(String("1.3.6.1.6.3.1.1.5.5"));
463                  standard_traps.append(String("1.3.6.1.6.3.1.1.5.6"));
464              
465 kumpf   1.10     Array<const char*> oidSubIdentifiers;
466 yi.zhou 1.1  
467                  CString trapOidCStr = trapOid.getCString();
468              
469 kumpf   1.10     char* trapOidCopy = strdup(trapOidCStr);
470                  char* numericEntOid = (char*) malloc(strlen(trapOidCStr));
471 yi.zhou 1.9  
472                  try
473                  {
474 yi.zhou 1.1  
475 a.dunfey 1.12 #if !defined(PEGASUS_OS_TYPE_WINDOWS) && !defined(PEGASUS_OS_ZOS)
476 kumpf    1.10         char* last;
477                       for (const char* p = strtok_r(trapOidCopy, ".", &last); p;
478 yi.zhou  1.9              p=strtok_r(NULL, ".", &last))
479 yi.zhou  1.1  #else
480 yi.zhou  1.9          for (const char* p = strtok(trapOidCopy, "."); p; p=strtok(NULL, "."))
481 yi.zhou  1.1  #endif
482 yi.zhou  1.9          {
483                           oidSubIdentifiers.append(p);
484                       }
485 yi.zhou  1.1  
486 yi.zhou  1.9          long genTrap = 0;
487                       long specTrap = 0;
488 yi.zhou  1.1  
489 yi.zhou  1.9          enterpriseOidLength = MAX_OID_LEN;
490 yi.zhou  1.1  
491 yi.zhou  1.9          if (Contains(standard_traps, trapOid))
492                       {
493                           //
494                           // if the trapOid is one of the standard traps,
495                           // then the SNMPV1 enterprise parameter must be set
496                           // to the value of the trapOid, the generic-trap
497                           // parameter must be set to one of (0 - 5), and the
498                           // specific-trap parameter must be set to 0
499                           //
500               
501                           // Convert trapOid from numeric form to a list of subidentifiers
502 kumpf    1.10             if (read_objid((const char*)trapOidCStr, enterpriseOid,
503 yi.zhou  1.9                  &enterpriseOidLength) == 0)
504                           {
505 kumpf    1.10                 // Failed to parse trapOid
506 yi.zhou  1.9  
507 kumpf    1.10                 PEG_METHOD_EXIT();
508                               throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
509 yi.zhou  1.9                      MessageLoaderParms(_MSG_READ_OBJID_FAILED_KEY,
510                                                      _MSG_READ_OBJID_FAILED,
511                                                      trapOid));
512                           }
513               
514                           // the generic trap is last sub-identifier of the
515                           // trapOid minus 1
516 kumpf    1.10             snmpPdu->trap_type =
517 yi.zhou  1.9                  atoi(oidSubIdentifiers[oidSubIdentifiers.size() - 1]) - 1;
518                           snmpPdu->specific_type = 0;
519 yi.zhou  1.1          }
520 yi.zhou  1.9          else
521 yi.zhou  1.1          {
522 yi.zhou  1.9              //
523                           // if the trapOid is not one of the standard traps:
524                           // then 1) the generic-trap parameter must be set to 6,
525                           // 2) if the next-to-last sub-identifier of the
526                           // trapOid is zero, then the SNMPV1 enterprise
527                           // parameter is the trapOid with the last 2
528                           // sub-identifiers removed, otherwise, the
529                           // SNMPV1 enterprise parameter is the trapOid
530                           // with the last sub-identifier removed;
531                           // 3) the SNMPv1 specific-trap parameter is the last
532                           // sub-identifier of the trapOid;
533                           //
534               
535                           snmpPdu->trap_type = 6;
536               
537 kumpf    1.10             snmpPdu->specific_type =
538 yi.zhou  1.9                  atoi(oidSubIdentifiers[oidSubIdentifiers.size()-1]);
539               
540                           strcpy(numericEntOid, oidSubIdentifiers[0]);
541                           for (Uint32 i = 1; i < oidSubIdentifiers.size()-2; i++)
542                           {
543                               strcat(numericEntOid, ".");
544                               strcat(numericEntOid, oidSubIdentifiers[i]);
545                           }
546               
547                           if (strcmp(oidSubIdentifiers[oidSubIdentifiers.size()-2], "0") != 0)
548                           {
549                               strcat(numericEntOid, ".");
550 kumpf    1.10                 strcat(numericEntOid,
551 yi.zhou  1.9                     oidSubIdentifiers[oidSubIdentifiers.size()-2]);
552                           }
553 yi.zhou  1.1  
554 kumpf    1.10             // Convert ent from numeric form to a list of subidentifiers
555                           if (read_objid(numericEntOid, enterpriseOid,
556 yi.zhou  1.9                  &enterpriseOidLength) == 0)
557                           {
558                               // Failed to parse numericEntOid
559 yi.zhou  1.1  
560 kumpf    1.10                 PEG_METHOD_EXIT();
561 yi.zhou  1.1  
562 kumpf    1.10                 throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
563 yi.zhou  1.9                      MessageLoaderParms(_MSG_READ_ENTOID_FAILED_KEY,
564                                                      _MSG_READ_ENTOID_FAILED,
565                                                      String(numericEntOid)));
566                           }
567 yi.zhou  1.1  
568                       }
569               
570 yi.zhou  1.9          snmpPdu->enterprise = (oid*) malloc(enterpriseOidLength * sizeof(oid));
571 kumpf    1.10         memcpy(snmpPdu->enterprise, enterpriseOid,
572                           enterpriseOidLength * sizeof(oid));
573 yi.zhou  1.9  
574                       snmpPdu->enterprise_length = enterpriseOidLength;
575 yi.zhou  1.1      }
576 yi.zhou  1.9      catch (...)
577                   {
578                       free(trapOidCopy);
579                       free(numericEntOid);
580 yi.zhou  1.1  
581 yi.zhou  1.9          PEG_METHOD_EXIT();
582                       throw;
583                   }
584 yi.zhou  1.1  
585                   free(trapOidCopy);
586                   free(numericEntOid);
587               
588 kumpf    1.10     PEG_METHOD_EXIT();
589 yi.zhou  1.1  }
590               
591               // Pack oids into the PDU
592               void snmpDeliverTrap_netsnmp::_packOidsIntoPdu(
593                   const Array<String>& vbOids,
594                   const Array<String>& vbTypes,
595                   const Array<String>& vbValues,
596 kumpf    1.10     snmp_pdu* snmpPdu)
597 yi.zhou  1.1  {
598               
599 kumpf    1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
600 yi.zhou  1.1          "snmpDeliverTrap_netsnmp::_packOidsIntoPdu");
601               
602                   char dataType;
603                   oid vbOid[MAX_OID_LEN];
604                   size_t vbOidLength = MAX_OID_LEN;
605               
606                   for (Uint32 i=0; i < vbOids.size(); i++)
607                   {
608                       if (vbTypes[i] == "OctetString")
609                       {
610                           dataType = 's';
611                       }
612                       else if (vbTypes[i] == "Integer")
613                       {
614                           dataType = 'i';
615 kumpf    1.10         }
616 yi.zhou  1.1          else if (vbTypes[i] == "OID")
617                       {
618                           dataType = 'o';
619                       }
620                       else
621                       {
622 kumpf    1.10             // Integer, OctetString, and OID are supported SNMP Data Types
623 yi.zhou  1.1              // for the CIM Property
624               
625 kumpf    1.10             PEG_METHOD_EXIT();
626 yi.zhou  1.1  
627 kumpf    1.10             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
628 yi.zhou  1.1                  MessageLoaderParms(_MSG_UNSUPPORTED_SNMP_DATA_TYPE_KEY,
629                                                  _MSG_UNSUPPORTED_SNMP_DATA_TYPE,
630                                                  vbTypes[i]));
631               
632                       }
633               
634 kumpf    1.10         // Convert oid of a CIM property from numeric form to a list of
635                       // subidentifiers
636                       if (read_objid((const char*)vbOids[i].getCString(), vbOid,
637 yi.zhou  1.1              &vbOidLength) == 0)
638                       {
639                           // Failed to parse vbOids
640               
641 kumpf    1.10             PEG_METHOD_EXIT();
642 yi.zhou  1.1  
643 kumpf    1.10             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
644 yi.zhou  1.1                  MessageLoaderParms(_MSG_PARSE_CIM_PROPERTY_OID_FAILED_KEY,
645                                                  _MSG_PARSE_CIM_PROPERTY_OID_FAILED,
646                                                  vbOids[i]));
647                       }
648               
649                       Sint32 retCode;
650 kumpf    1.10         retCode = snmp_add_var(snmpPdu, vbOid, vbOidLength, dataType,
651 yi.zhou  1.1                                 vbValues[i].getCString());
652               
653                       // Failed to add vbOid to the pdu
654                       if (retCode != 0)
655                       {
656 kumpf    1.10             PEG_METHOD_EXIT();
657 yi.zhou  1.1  
658 kumpf    1.10             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
659 yi.zhou  1.1                  MessageLoaderParms(_MSG_ADD_VAR_TO_PDU_FAILED_KEY,
660                                                  _MSG_ADD_VAR_TO_PDU_FAILED,
661                                                  vbOids[i],
662 kumpf    1.10                                    String(snmp_api_errstring(retCode))));
663 yi.zhou  1.1  
664                       }
665                   }
666               
667 kumpf    1.10     PEG_METHOD_EXIT();
668 yi.zhou  1.1  }
669               
670               PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2