notifyqueue.cpp
来自「JdonFramework need above jdk 1.4.0 This」· C++ 代码 · 共 617 行 · 第 1/2 页
CPP
617 行
/*_############################################################################ _## _## notifyqueue.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 _## _##########################################################################*//*=================================================================== Copyright (c) 1999 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 makes 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. N O T I F Y Q U E U E . C P P CNotifyEventQueue CLASS DEFINITION COPYRIGHT HEWLETT PACKARD COMPANY 1999 INFORMATION NETWORKS DIVISION NETWORK MANAGEMENT SECTION DESIGN + AUTHOR: Tom Murray LANGUAGE: ANSI C++ DESCRIPTION: Queue for holding callback associated with user defined timeouts=====================================================================*/char notifyqueue_version[]="#(@) SNMP++ $Id: notifyqueue.cpp,v 1.18 2006/04/05 19:28:09 katz Exp $";//-----[ includes ]----------------------------------------------------#ifdef WIN32#include <winsock.h>#endif#include <errno.h>//----[ snmp++ includes ]----------------------------------------------#include "snmp_pp/config_snmp_pp.h"#include "snmp_pp/v3.h"#include "snmp_pp/notifyqueue.h" // queue for holding sessions waiting for async notifications#include "snmp_pp/eventlistholder.h"#include "snmp_pp/uxsnmp.h"#include "snmp_pp/snmperrs.h"#include "snmp_pp/pdu.h"#if defined (CPU) && CPU == PPC603#include <sockLib.h> #endif#ifdef SNMP_PP_NAMESPACEnamespace Snmp_pp {#endif//--------[ externs ]---------------------------------------------------extern int receive_snmp_notification(SnmpSocket sock, Snmp &snmp_session, Pdu &pdu, SnmpTarget **target);//-----[ macros ]------------------------------------------------------// should be in snmp.h...#define SNMP_PORT 161 // standard port # for SNMP#define SNMP_TRAP_PORT 162 // standard port # for SNMP traps#ifdef WIN32#define close closesocket#elif _AIX#include <unistd.h>#endif//----[ CNotifyEvent class ]------------------------------------------------CNotifyEvent::CNotifyEvent(Snmp *snmp, const OidCollection &trapids, const TargetCollection &targets, const AddressCollection &addresses) : m_snmp(snmp){ // create new collections using parms passed in notify_ids = new OidCollection(trapids); notify_targets = new TargetCollection(targets); notify_addresses = new AddressCollection(addresses);}CNotifyEvent::~CNotifyEvent(){ // free up local collections if (notify_ids) { delete notify_ids; notify_ids = 0; } if (notify_targets) { delete notify_targets; notify_targets = 0; } if (notify_addresses) { delete notify_addresses; notify_addresses = 0; }}int CNotifyEvent::notify_filter(const Oid &trapid, SnmpTarget &target) const{ int target_count, has_target = FALSE, target_matches = FALSE; int trapid_count, has_trapid = FALSE, trapid_matches = FALSE; GenAddress targetaddr, tmpaddr; // figure out how many targets, handle empty case as all targets if ((notify_targets) && (target_count = notify_targets->size())) { SnmpTarget *tmptarget = 0; has_target = TRUE; target.get_address(targetaddr); if (targetaddr.valid()) { // loop through all targets in the collection SnmpTarget::target_type target_type = target.get_type(); SnmpTarget::target_type tmptarget_type; for ( int x = 0; x < target_count; x++) // for all targets { if (notify_targets->get_element(tmptarget, x)) continue; tmptarget->get_address(tmpaddr); if ((tmpaddr.valid())) { int addr_equal = 0; /* check for types of Address */ if ((tmpaddr.get_type() == Address::type_ip) && (targetaddr.get_type() == Address::type_udp)) { /* special case that works for UdpAddress == IpAddress */ IpAddress ip1(targetaddr); IpAddress ip2(tmpaddr); addr_equal = (ip1.valid() && ip2.valid() && (ip1 == ip2)); } else { addr_equal = (targetaddr == tmpaddr); } if (addr_equal) { tmptarget_type = tmptarget->get_type(); if (target_type == SnmpTarget::type_utarget) { // target is a UTarget if (tmptarget_type == SnmpTarget::type_utarget) { // both are UTarget if ((((UTarget*)(&target))->get_security_name() == ((UTarget*)tmptarget)->get_security_name()) && (((UTarget*)(&target))->get_security_model() == ((UTarget*)tmptarget)->get_security_model())) { target_matches = TRUE; break; } } else if (tmptarget_type == SnmpTarget::type_ctarget) // in case utarget is used with v1 or v2: if ((tmptarget->get_version() == target.get_version()) && (((UTarget*)(&target))->get_security_name() == OctetStr(((CTarget*)tmptarget)-> get_readcommunity()))) { target_matches = TRUE; break; } } else { if (target_type == SnmpTarget::type_ctarget) { // target is a CTarget if (tmptarget_type == SnmpTarget::type_ctarget) { // both are CTarget if (!strcmp(((CTarget*)(&target))->get_readcommunity(), ((CTarget*)tmptarget)->get_readcommunity())) { target_matches = TRUE; break; } } else if (tmptarget_type == SnmpTarget::type_utarget) { if ((tmptarget->get_version() == target.get_version()) && (OctetStr(((CTarget*)(&target))->get_readcommunity()) == ((UTarget*)tmptarget)->get_security_name())) { target_matches = TRUE; break; } } } } } // end if (add_equal) } // end if tmpaddr.valid()... } } } // else no targets means all targets // figure out how many trapids, handle empty case as all trapids if ((notify_ids) && (trapid_count = notify_ids->size())) { Oid tmpoid; has_trapid = TRUE; // loop through all trapids in the collection for (int y=0; y < trapid_count; y++) // for all trapids { if (notify_ids->get_element(tmpoid, y)) continue; if (trapid == tmpoid) { trapid_matches = TRUE; break; } } } // else no trapids means all traps // Make the callback if the trap passed the filters if ((has_target && !target_matches) || (has_trapid && !trapid_matches)) return FALSE; return TRUE;}int CNotifyEvent::Callback(SnmpTarget &target, Pdu &pdu, SnmpSocket fd, int status){ Oid trapid; pdu.get_notify_id(trapid); // Make the callback if the trap passed the filters if ((m_snmp) && (notify_filter(trapid, target))) { int reason; if (SNMP_CLASS_TL_FAILED == status) reason = SNMP_CLASS_TL_FAILED; else reason = SNMP_CLASS_NOTIFICATION; //------[ call into the callback function ]------------------------- if (m_snmp->get_notify_callback()) (m_snmp->get_notify_callback())( reason, m_snmp, // snmp++ session who owns the req pdu, // trap pdu target, // target m_snmp->get_notify_callback_data()); // callback data } return SNMP_CLASS_SUCCESS;}//----[ CNotifyEventQueueElt class ]--------------------------------------CNotifyEventQueue::CNotifyEventQueueElt::CNotifyEventQueueElt( CNotifyEvent *notifyevent, CNotifyEventQueueElt *next, CNotifyEventQueueElt *previous) : m_notifyevent(notifyevent), m_Next(next), m_previous(previous){ /* Finish insertion into doubly linked list */ if (m_Next) m_Next->m_previous = this; if (m_previous) m_previous->m_Next = this;}CNotifyEventQueue::CNotifyEventQueueElt::~CNotifyEventQueueElt(){ /* Do deletion form doubly linked list */ if (m_Next) m_Next->m_previous = m_previous; if (m_previous) m_previous->m_Next = m_Next; if (m_notifyevent) delete m_notifyevent;}CNotifyEvent *CNotifyEventQueue::CNotifyEventQueueElt::TestId(Snmp *snmp){ if (m_notifyevent && (m_notifyevent->GetId() == snmp)) return m_notifyevent; return 0;}//----[ CNotifyEventQueue class ]--------------------------------------CNotifyEventQueue::CNotifyEventQueue(EventListHolder *holder, Snmp *session) : m_head(NULL,NULL,NULL), m_msgCount(0), m_notify_fds(0), m_notify_fd_count(0), m_listen_port(SNMP_TRAP_PORT),
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?