Main Page | Class Hierarchy | Class List | File List | Class Members

snmp_module.h

00001 // tacppd store end send/receive snmp information
00002 // (c) Copyright 2000-2004 by tacppd team and contributors
00003 // see http://tacppd.org for more information
00004 
00005 //  This program is free software; you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // all comments which start from /**<CR> - this is comments for KDoc
00020 //  for classes
00021 //    @short [short description of the class]
00022 //    @author [class author]
00023 //    @version [class version]
00024 //    @see [references to other classes or methods]
00025 //  for methods
00026 //    @see [references]
00027 //    @return [sentence describing the return value]
00028 //    @exception [list the exeptions that could be thrown]
00029 //    @param [name id] [description] - can be multiple
00030 
00031 #ifndef __SNMP_MODULE_H__
00032 #define __SNMP_MODULE_H__
00033 
00034 #ifndef _PTHREADS
00035 #define   _PTHREADS   // enable STL pthread-safe code
00036 #endif //_PTHREADS
00037 
00038 #ifndef _REENTRANT
00039 #define   _REENTRANT  // Pthread safe stuff in the stdlibs
00040 #endif //_REENTRANT   // or you should compile with -pthread key
00041 
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <unistd.h>
00045 
00046 #ifdef SOLARIS
00047  #include <strings.h>
00048 #endif
00049 
00050 #include <string>
00051 #include <list>
00052 
00053 using std::list;
00054 using std::string;
00055 
00056 #define SNMP_ERROR_MSG_LEN   200
00057 
00058 #ifndef USERNAME_LEN
00059 #define USERNAME_LEN    32
00060 #endif //USERNAME_LEN
00061 
00062 #ifndef PORT_LEN
00063 #define PORT_LEN        32
00064 #endif //PORT_LEN
00065 
00066 #ifndef IP_LEN
00067 #define IP_LEN          16
00068 #endif //IP_LEN
00069 
00070 #ifndef REM_ADDR_LEN
00071 #define REM_ADDR_LEN    64
00072 #endif //REM_ADDR_LEN
00073 
00074 #ifndef MAX_SNMP_OUT
00075 #define MAX_SNMP_OUT    3000
00076 #endif //MAX_SNMP_OUT
00077 
00078 // common interface OIDs
00079 #define SNMP_ifIndex            ".1.3.6.1.2.1.2.2.1.1"
00080 #define SNMP_ifName             ".1.3.6.1.2.1.31.1.1.1.1"
00081 #define SNMP_ifAdminStatus      ".1.3.6.1.2.1.2.2.1.7"
00082 #define SNMP_ifOperStatus       ".1.3.6.1.2.1.2.2.1.8"
00083 
00084 // OIDs for traffic
00085 #define SNMP_ifInOctets         ".1.3.6.1.2.1.2.2.1.10"
00086 #define SNMP_ifOutOctets        ".1.3.6.1.2.1.2.2.1.16"
00087 
00093 class snmp_d {
00096   pthread_mutex_t mutex_;
00099   void lock() {
00100     pthread_mutex_lock(&mutex_);
00101   }
00104   void unlock() {
00105     pthread_mutex_unlock(&mutex_);
00106   }
00107  public:
00110   string res;
00113   int idx;
00116   int idx1;
00119   snmp_d(string o) {
00120     pthread_mutex_init(&mutex_,0);
00121     lock();
00122     res = o;
00123     idx = 0;
00124     idx1=0;
00125     unlock();
00126   }
00129   snmp_d(string o,int i) {
00130     pthread_mutex_init(&mutex_,0);
00131     lock();
00132     res = o;
00133     idx = i;
00134     idx1 = 0;
00135     unlock();
00136   }
00139   snmp_d(string o,int i,int i1) {
00140     pthread_mutex_init(&mutex_,0);
00141     lock();
00142     res = o;
00143     idx = 1;
00144     idx1 = i1;
00145     unlock();
00146   }
00149   ~snmp_d() {
00150     pthread_mutex_destroy(&mutex_);
00151   }
00152 };
00153 
00159 class SnmpD {
00162   pthread_mutex_t mutex_;
00165   void lock() {
00166     pthread_mutex_lock(&mutex_);
00167   }
00170   void unlock() {
00171     pthread_mutex_unlock(&mutex_);
00172   }
00175   list<snmp_d*> sd_;
00176  public:
00179   SnmpD() {
00180     pthread_mutex_init(&mutex_,0);
00181   }
00184   ~SnmpD() {
00185     lock();
00186     snmp_d *tmp = 0;
00187     for(list<snmp_d*>::iterator i=sd_.begin();i!=sd_.end() && !sd_.empty();) {
00188       tmp = *i;
00189       i = sd_.erase(i);
00190       delete tmp;
00191     }
00192     sd_.clear();
00193     unlock();
00194     pthread_mutex_destroy(&mutex_);
00195   }
00198   void add(snmp_d *s) {
00199     lock();
00200     sd_.push_back(s);
00201     unlock();
00202   }
00206   snmp_d *get(int n) {
00207     snmp_d *tmp;
00208     lock();
00209     tmp = 0;
00210     if(sd_.size() > 0 && (int)sd_.size() > n) {
00211       int count = 0;
00212       for(list<snmp_d*>::const_iterator i=sd_.begin();i!=sd_.end();i++,count++) {
00213         if(count == n) {
00214           tmp = *i;
00215           break;
00216         }
00217       }
00218     }
00219     unlock();
00220     return tmp;
00221   }
00222 };
00223 
00229 class SnmpOut {
00232   pthread_mutex_t mutex_;
00235   void lock() {
00236     pthread_mutex_lock(&mutex_);
00237   }
00240   void unlock() {
00241     pthread_mutex_unlock(&mutex_);
00242   }
00245   string user_;
00248   string port_;
00251   unsigned long int ifOutOctets_;
00254   unsigned long int ifInOctets_;
00255  public:
00258   string getUser() {
00259     string ret;
00260     lock();
00261     ret = user_;
00262     unlock();
00263     return ret;
00264   }
00267   void setUser(string d) {
00268     lock();
00269     user_ = d;
00270     unlock();
00271   }
00274   string getPort() {
00275     string ret;
00276     lock();
00277     ret = port_;
00278     unlock();
00279     return ret;
00280   }
00283   void setPort(string d) {
00284     lock();
00285     port_ = d;
00286     unlock();
00287   }
00290   unsigned long int getIfOO() {
00291     unsigned long int ret;
00292     lock();
00293     ret = ifOutOctets_;
00294     unlock();
00295     return ret;
00296   }
00299   void setIfOO(unsigned long int d) {
00300     lock();
00301     ifOutOctets_ = d;
00302     unlock();
00303   }
00306   unsigned long int getIfIO() {
00307     unsigned long int ret;
00308     lock();
00309     ret = ifInOctets_;
00310     unlock();
00311     return ret;
00312   }
00315   void setIfIO(unsigned long int d) {
00316     lock();
00317     ifInOctets_ = d;
00318     unlock();
00319   }
00322   SnmpOut() {
00323     pthread_mutex_init(&mutex_,0);
00324     lock();
00325     ifOutOctets_ = 0;
00326     ifInOctets_ = 0;
00327     unlock();
00328   }
00331   ~SnmpOut() {
00332     pthread_mutex_destroy(&mutex_);
00333   }
00334 };
00335 
00341 class SnmpDevice {
00344   pthread_mutex_t mutex_;
00347   void lock() {
00348     pthread_mutex_lock(&mutex_);
00349   }
00352   void unlock() {
00353     pthread_mutex_unlock(&mutex_);
00354   }
00357   list<SnmpOut*> s_;
00360   string device_;
00363   string commun_;
00364  public:
00367   int size() {
00368     int ret;
00369     lock();
00370     ret = s_.size();
00371     unlock();
00372     return ret;
00373   }
00376   string getDevice() {
00377     string ret;
00378     lock();
00379     ret = device_;
00380     unlock();
00381     return ret;
00382   }
00385   void setDevice(string d) {
00386     lock();
00387     device_ = d;
00388     unlock();
00389   }
00392   string getCommun() {
00393     string ret;
00394     lock();
00395     ret = commun_;
00396     unlock();
00397     return ret;
00398   }
00401   void setCommun(string d) {
00402     lock();
00403     commun_ = d;
00404     unlock();
00405   }
00408   SnmpDevice(char *d,char *c) {
00409     pthread_mutex_init(&mutex_,0);
00410     lock();
00411     device_ = d;
00412     commun_ = c;
00413     unlock();
00414   }
00417   ~SnmpDevice() {
00418     SnmpOut *tmp;
00419     tmp = 0;
00420     lock();
00421     for(list<SnmpOut*>::iterator i=s_.begin();i!=s_.end() && !s_.empty();) {
00422       tmp = *i;
00423       i = s_.erase(i);
00424       delete tmp;
00425     }
00426     s_.clear();
00427     unlock();
00428     pthread_mutex_destroy(&mutex_);
00429   }
00432   SnmpOut *get(int n) {
00433     SnmpOut *tmp;
00434     tmp = 0;
00435     lock();
00436     if(s_.size() > 0 && (int)s_.size() > n) {
00437       int count = 0;
00438       for(list<SnmpOut*>::const_iterator i=s_.begin();i!=s_.end(); i++,count++) {
00439         if(count == n) {
00440           tmp = *i;
00441           break;
00442         }
00443       }
00444     }
00445     unlock();
00446     return tmp;
00447   }
00450   void add(SnmpOut *so) {
00451     lock();
00452     s_.push_back(so);
00453     unlock();
00454   }
00455 };
00456 
00457 #endif //__SNMP_MODULE_H__

Generated on Thu Jul 21 23:09:45 2005 for tacppd.kdevelop by doxygen 1.3.5