mp_v3.cpp

来自「JdonFramework need above jdk 1.4.0 This」· C++ 代码 · 共 1,081 行 · 第 1/2 页

CPP
1,081
字号
/*_############################################################################  _##   _##  mp_v3.cpp    _##  _##  SNMP++v3.2.21a  _##  -----------------------------------------------  _##  Copyright (c) 2001-2006 Jochen Katz, Frank Fock  _##  _##  This software is based on SNMP++2.6 from Hewlett Packard:  _##    _##    Copyright (c) 1996  _##    Hewlett-Packard Company  _##    _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.  _##  Permission to use, copy, modify, distribute and/or sell this software   _##  and/or its documentation is hereby granted without fee. User agrees   _##  to display the above copyright notice and this license notice in all   _##  copies of the software and any documentation of the software. User   _##  agrees to assume all liability for the use of the software;   _##  Hewlett-Packard and Jochen Katz make no representations about the   _##  suitability of this software for any purpose. It is provided   _##  "AS-IS" without warranty of any kind, either express or implied. User   _##  hereby grants a royalty-free license to any and all derivatives based  _##  upon this software code base.   _##    _##  Stuttgart, Germany, Tue Nov 21 22:12:16 CET 2006   _##    _##########################################################################*/char mp_v3_cpp_version[]="@(#) SNMP++ $Id: mp_v3.cpp,v 1.22 2006/06/08 22:24:58 fock Exp $";#include <stdlib.h>#include "snmp_pp/config_snmp_pp.h"#ifdef _SNMPv3#include "snmp_pp/v3.h"#include "snmp_pp/mp_v3.h"#include "snmp_pp/usm_v3.h"#include "snmp_pp/notifyqueue.h"#include "snmp_pp/snmpmsg.h"#include "snmp_pp/uxsnmp.h"#include "snmp_pp/eventlistholder.h"#include "snmp_pp/asn1.h"#include "snmp_pp/vb.h"#include "snmp_pp/log.h"#ifdef SNMP_PP_NAMESPACEnamespace Snmp_pp {#endif#define MAX_MPMSGID 2147483647#define ASN_UNI_PRIV (ASN_UNIVERSAL | ASN_PRIMITIVE)#define ASN_SEQ_CON (ASN_SEQUENCE | ASN_CONSTRUCTOR)#define CACHE_LOCAL_REQ true#define CACHE_REMOTE_REQ falsev3MP *v3MP::I = 0;// Use locking on access methods in an multithreading enviroment.#ifdef _THREADS#define BEGIN_REENTRANT_CODE_BLOCK SnmpSynchronize auto_lock(lock)#define BEGIN_REENTRANT_CODE_BLOCK_CONST  \          SnmpSynchronize auto_lock(*(PP_CONST_CAST(SnmpSynchronized*, &lock)))#else#define BEGIN_REENTRANT_CODE_BLOCK#define BEGIN_REENTRANT_CODE_BLOCK_CONST#endif// ========================[ Engine id table ]=============================// Construct engine id tablev3MP::EngineIdTable::EngineIdTable(int initial_size){  if (initial_size < 1)    initial_size = 10;  if (!initialize_table(initial_size))  {    LOG_BEGIN(ERROR_LOG | 0);    LOG("v3MP::EngineIdTable: Error creating empty table.");    LOG_END;  }}// Denstruct enigne id tablev3MP::EngineIdTable::~EngineIdTable(){  if (table)    delete [] table;  table = 0;}// Add an entry to the table.int v3MP::EngineIdTable::add_entry(const OctetStr &engine_id,                                   const OctetStr &host, int port){  if (!table)    return SNMPv3_MP_NOT_INITIALIZED;  LOG_BEGIN(INFO_LOG | 9);  LOG("v3MP::EngineIdTable: adding new entry (id) (host) (port)");  LOG(engine_id.get_printable());  LOG(host.get_printable());  LOG(port);  LOG_END;  BEGIN_REENTRANT_CODE_BLOCK;  for (int i = 0; i < entries; i++)    if ((table[i].port == port) &&        (table[i].host == host))    {      // make sure the engine_id is right      table[i].engine_id = engine_id;      return SNMPv3_MP_OK;         // host is in table    }  table[entries].engine_id = engine_id;  table[entries].host = host;  table[entries].port = port;  entries++;  if (entries == max_entries)  {    // resize Table    struct Entry_T *tmp;    tmp = new struct Entry_T[2 * max_entries];    if (!tmp)    {      entries--;      return SNMPv3_MP_ERROR;    }    for (int i = 0; i < entries; i++)      tmp[i] = table[i];    delete [] table;    table = tmp;    max_entries *= 2;  }  return SNMPv3_MP_OK;}// Get the engine_id of the SNMP entity at the given host/port.int v3MP::EngineIdTable::get_entry(OctetStr &engine_id,                                   const OctetStr &hostport) const{  int port;  char host[MAX_HOST_NAME_LENGTH + 1];  char *ptr;  /* Check length */  if (hostport.len() > MAX_HOST_NAME_LENGTH)    return SNMPv3_MP_ERROR;  /* split up port from hostport */  strcpy(host, hostport.get_printable());  ptr = strstr((char*)host,"/");  if (!ptr)    return SNMPv3_MP_ERROR;  *ptr = '\0';  port = atol(ptr + 1);  /* Check for IPv6 address with [] */  if (host[0] == '[')  {    if (*(ptr -1) == ']')    {      *(ptr-1) = '\0';      return get_entry(engine_id, &(host[1]), port);    }    else      return SNMPv3_MP_ERROR;  }  return get_entry(engine_id, host, port);}// Get the engineID of the SNMP entity at the given host/port.int v3MP::EngineIdTable::get_entry(OctetStr &engine_id,                                   const OctetStr &host, int port) const{  if (!table)    return SNMPv3_MP_NOT_INITIALIZED;  BEGIN_REENTRANT_CODE_BLOCK_CONST;  int i, found = 0;  for (i = 0; i < entries; i++)    if ((table[i].port == port) &&        (table[i].host == host))    {      found=1;      break;    }  if (!found)  {    LOG_BEGIN(INFO_LOG | 4);    LOG("v3MP::EngineIdTable: Dont know engine id for (host) (port)");    LOG(host.get_printable());    LOG(port);    LOG_END;    return SNMPv3_MP_ERROR;  }  engine_id = table[i].engine_id;  return SNMPv3_MP_OK;}// Remove all entries from the engine id table.int v3MP::EngineIdTable::reset(){  if (!table)    return SNMPv3_MP_NOT_INITIALIZED;  LOG_BEGIN(INFO_LOG | 1);  LOG("v3MP::EngineIdTable: Resetting table.");  LOG_END;  BEGIN_REENTRANT_CODE_BLOCK;  entries = 0;  return SNMPv3_MP_OK;}// Remove the given engine id from the table.int v3MP::EngineIdTable::delete_entry(const OctetStr &engine_id){  if (!table)    return SNMPv3_MP_NOT_INITIALIZED;  int i, found = 0;  BEGIN_REENTRANT_CODE_BLOCK;  for (i = 0; i < entries; i++)    if (table[i].engine_id == engine_id)    {      found=1;      break;    }  if (!found)  {    LOG_BEGIN(WARNING_LOG | 4);    LOG("v3MP::EngineIdTable: cannot remove nonexisting entry (engine id)");    LOG(engine_id.get_printable());    LOG_END;    return SNMPv3_MP_ERROR;  }  /* i is the entry to remove */  if (i != entries - 1)    table[i] = table[entries-1];  entries--;  return SNMPv3_MP_OK;}// Remove the entry for the given address/port from the table.int v3MP::EngineIdTable::delete_entry(const OctetStr &host, int port){  if (!table)    return SNMPv3_MP_NOT_INITIALIZED;  int i, found = 0;  BEGIN_REENTRANT_CODE_BLOCK;  for (i = 0; i < entries; i++)    if ((table[i].port == port) &&        (table[i].host == host))    {      found=1;      break;    }  if (!found)  {    LOG_BEGIN(WARNING_LOG | 4);    LOG("v3MP::EngineIdTable: cannot remove nonexisting entry (host) (port)");    LOG(host.get_printable());    LOG(port);    LOG_END;    return SNMPv3_MP_ERROR;  }  /* i is the entry to remove */  if (i != entries - 1)    table[i] = table[entries-1];  entries--;  return SNMPv3_MP_OK;}int v3MP::EngineIdTable::initialize_table(const int size){  table = new struct Entry_T[size];  entries = 0;  if (!table)  {    max_entries = 0;    return FALSE;  }  max_entries = size;  return TRUE;}// ===============================[ Cache ]==================================v3MP::Cache::Cache(){  // init cache  table = new struct Entry_T[5];  if (!table)  {    LOG_BEGIN(ERROR_LOG | 1);    LOG("v3MP::Cache: could not create empty table.");    LOG_END;    max_entries = 0;  }  else    max_entries = 5;  entries = 0;}v3MP::Cache::~Cache(){  if (table)  {    for (int i = 0; i < entries; i++)      usm->delete_sec_state_reference(table[i].sec_state_ref);    entries = 0;    delete [] table;    table = 0;    max_entries = 0;  }}// Add an entry to the cache.int v3MP::Cache::add_entry(int msg_id, unsigned long req_id,                           const OctetStr &sec_engine_id,                           int sec_model,                           const OctetStr &sec_name,                           int sec_level,                           const OctetStr &context_engine_id,                           const OctetStr &context_name,                           struct SecurityStateReference *sec_state_ref,                           int error_code, bool local_request){  if (!table)    return SNMPv3_MP_ERROR;  LOG_BEGIN(INFO_LOG | 8);  LOG("v3MP::Cache: adding new entry (n) (msg id) (req id) (type)");  LOG(entries);  LOG(msg_id);  LOG(req_id);  LOG(local_request ? "local" : "remote");  LOG_END;  BEGIN_REENTRANT_CODE_BLOCK;  for (int i = 0; i < entries; i++)    if ((table[i].msg_id == msg_id) &&        (table[i].req_id == req_id) &&        (table[i].local_request == local_request) &&        (table[i].sec_engine_id == sec_engine_id) &&        (table[i].sec_model == sec_model) &&        (table[i].sec_name == sec_name) &&        (table[i].sec_level == sec_level) &&        (table[i].context_engine_id == context_engine_id) &&        (table[i].context_name == context_name))    {      LOG_BEGIN(WARNING_LOG | 3);      LOG("v3MP::Cache: Dont add doubled entry (msg id) (req id)");      LOG(msg_id);      LOG(req_id);      LOG_END;      return SNMPv3_MP_DOUBLED_MESSAGE;    }  table[entries].msg_id            = msg_id;  table[entries].req_id            = req_id;  table[entries].local_request     = local_request;  table[entries].sec_engine_id     = sec_engine_id;  table[entries].sec_model         = sec_model;  table[entries].sec_name          = sec_name;  table[entries].sec_level         = sec_level;  table[entries].context_engine_id = context_engine_id;  table[entries].context_name      = context_name;  table[entries].sec_state_ref     = sec_state_ref;  table[entries].error_code        = error_code;  entries++;  if (entries == max_entries)  {    // resize Table    struct Entry_T *tmp;    tmp = new struct Entry_T[2 * max_entries];    if (!tmp)    {      entries--;      return SNMPv3_MP_ERROR;    }    for (int i=0; i<entries;i++)      tmp[i] = table[i];    delete [] table;    table = tmp;    max_entries *= 2;  }  return SNMPv3_MP_OK;}// Search the cache for a message id, return the error code andint v3MP::Cache::get_entry(int msg_id, bool local_request, int *error_code,                           struct SecurityStateReference **sec_state_ref){  if (!table) return SNMPv3_MP_ERROR;  BEGIN_REENTRANT_CODE_BLOCK;  for (int i=0; i < entries; i++)  {    if ((msg_id == table[i].msg_id) &&        (local_request == table[i].local_request))    {      *error_code = table[i].error_code;      *sec_state_ref = table[i].sec_state_ref;      entries--;      LOG_BEGIN(INFO_LOG | 8);      LOG("v3MP::Cache: Found entry (n) (msg id) (type)");      LOG(i);      LOG(msg_id);      LOG(local_request ? "local" : "remote");      LOG_END;      if (entries > i)      {        table[i] = table[entries];	LOG_BEGIN(INFO_LOG | 10);	LOG("v3MP::Cache: Moving entry (from) (to)");	LOG(entries);	LOG(i);	LOG_END;      }      return SNMPv3_MP_OK;    }  }  LOG_BEGIN(WARNING_LOG | 5);  LOG("v3MP::Cache: Entry not found (msg id) (type)");  LOG(msg_id);  LOG(local_request ? "local" : "remote");  LOG_END;  return SNMPv3_MP_ERROR;}// Delete the entry with the given request id from the cache.void v3MP::Cache::delete_entry(unsigned long req_id, bool local_request){  if (!table) return;  BEGIN_REENTRANT_CODE_BLOCK;  for (int i=0; i<entries; i++)    if ((table[i].req_id == req_id) &&        (table[i].local_request == local_request))    {      LOG_BEGIN(INFO_LOG | 8);      LOG("v3MP::Cache: Delete unprocessed entry (n) (req id) (type)");      LOG(i);      LOG(req_id);      LOG(local_request ? "local" : "remote");      LOG_END;      usm->delete_sec_state_reference(table[i].sec_state_ref);      entries--;      if (entries > i)      {        table[i] = table[entries];	LOG_BEGIN(INFO_LOG | 10);	LOG("v3MP::Cache: Moving entry (from) (to)");	LOG(entries);	LOG(i);	LOG_END;      }      return;    }  LOG_BEGIN(INFO_LOG | 8);  LOG("v3MP::Cache: Entry to delete not found (req id) (type)");  LOG(req_id);  LOG(local_request ? "local" : "remote");  LOG_END;  return;}// Delete the entry with the given request ans message id from the cache.void v3MP::Cache::delete_entry(unsigned long req_id, int msg_id,			       bool local_request){  if (!table) return;  BEGIN_REENTRANT_CODE_BLOCK;  for (int i=0; i<entries; i++)    if ((table[i].req_id == req_id) &&	(table[i].msg_id == msg_id) &&        (table[i].local_request == local_request))    {      LOG_BEGIN(INFO_LOG | 8);      LOG("v3MP::Cache: Delete unprocessed entry (n) (req id) (msg id) (type)");      LOG(i);      LOG(req_id);      LOG(msg_id);      LOG(local_request ? "local" : "remote");      LOG_END;      usm->delete_sec_state_reference(table[i].sec_state_ref);      entries--;      if (entries > i)      {        table[i] = table[entries];

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?