PTLib
Version 2.10.4
|
00001 /* 00002 * psnmp.h 00003 * 00004 * Simple Network Management Protocol classes. 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (c) 1993-2002 Equivalence Pty. Ltd. 00009 * 00010 * The contents of this file are subject to the Mozilla Public License 00011 * Version 1.0 (the "License"); you may not use this file except in 00012 * compliance with the License. You may obtain a copy of the License at 00013 * http://www.mozilla.org/MPL/ 00014 * 00015 * Software distributed under the License is distributed on an "AS IS" 00016 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00017 * the License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * The Original Code is Portable Windows Library. 00021 * 00022 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00023 * 00024 * Contributor(s): ______________________________________. 00025 * 00026 * $Revision: 26773 $ 00027 * $Author: rjongbloed $ 00028 * $Date: 2011-12-07 00:43:33 -0600 (Wed, 07 Dec 2011) $ 00029 */ 00030 00031 #ifndef PTLIB_PSNMP_H 00032 #define PTLIB_PSNMP_H 00033 00034 #ifdef P_USE_PRAGMA 00035 #pragma interface 00036 #endif 00037 00038 #ifdef P_SNMP 00039 00040 #include <ptlib/sockets.h> 00041 #include <ptclib/snmp.h> 00042 #include <ptclib/pasn.h> 00043 00044 #include <list> 00045 #include <vector> 00046 00048 00051 class PSNMPVarBindingList : public PObject 00052 { 00053 PCLASSINFO(PSNMPVarBindingList, PObject) 00054 public: 00055 00056 void Append(const PString & objectID); 00057 void Append(const PString & objectID, PASNObject * obj); 00058 void AppendString(const PString & objectID, const PString & str); 00059 00060 void RemoveAll(); 00061 00062 PINDEX GetSize() const; 00063 00064 PINDEX GetIndex(const PString & objectID) const; 00065 PString GetObjectID(PINDEX idx) const; 00066 PASNObject & operator[](PINDEX idx) const; 00067 00068 void PrintOn(ostream & strm) const; 00069 00070 protected: 00071 PStringArray objectIds; 00072 PASNObjectArray values; 00073 }; 00074 00076 00079 class PSNMP : public PIndirectChannel 00080 { 00081 PCLASSINFO(PSNMP, PIndirectChannel) 00082 public: 00083 enum ErrorType { 00084 // Standard RFC1157 errors 00085 NoError = 0, 00086 TooBig = 1, 00087 NoSuchName = 2, 00088 BadValue = 3, 00089 ReadOnly = 4, 00090 GenErr = 5, 00091 00092 // Additional errors 00093 NoResponse, 00094 MalformedResponse, 00095 SendFailed, 00096 RxBufferTooSmall, 00097 TxDataTooBig, 00098 NumErrors 00099 }; 00100 00101 enum RequestType { 00102 GetRequest = 0, 00103 GetNextRequest = 1, 00104 GetResponse = 2, 00105 SetRequest = 3, 00106 Trap = 4, 00107 }; 00108 00109 enum { TrapPort = 162 }; 00110 00111 enum TrapType { 00112 ColdStart = 0, 00113 WarmStart = 1, 00114 LinkDown = 2, 00115 LinkUp = 3, 00116 AuthenticationFailure = 4, 00117 EGPNeighbourLoss = 5, 00118 EnterpriseSpecific = 6, 00119 NumTrapTypes 00120 }; 00121 00122 static PString GetErrorText(ErrorType err); 00123 00124 static PString GetTrapTypeText(PINDEX code); 00125 00126 static void SendEnterpriseTrap ( 00127 const PIPSocket::Address & addr, 00128 const PString & community, 00129 const PString & enterprise, 00130 PINDEX specificTrap, 00131 PASNUnsigned timeTicks, 00132 WORD sendPort = TrapPort); 00133 00134 static void SendEnterpriseTrap ( 00135 const PIPSocket::Address & addr, 00136 const PString & community, 00137 const PString & enterprise, 00138 PINDEX specificTrap, 00139 PASNUnsigned timeTicks, 00140 const PSNMPVarBindingList & vars, 00141 WORD sendPort = TrapPort); 00142 00143 static void SendTrap ( 00144 const PIPSocket::Address & addr, 00145 PSNMP::TrapType trapType, 00146 const PString & community, 00147 const PString & enterprise, 00148 PINDEX specificTrap, 00149 PASNUnsigned timeTicks, 00150 const PSNMPVarBindingList & vars, 00151 WORD sendPort = TrapPort); 00152 00153 static void SendTrap ( 00154 const PIPSocket::Address & addr, 00155 PSNMP::TrapType trapType, 00156 const PString & community, 00157 const PString & enterprise, 00158 PINDEX specificTrap, 00159 PASNUnsigned timeTicks, 00160 const PSNMPVarBindingList & vars, 00161 const PIPSocket::Address & agentAddress, 00162 WORD sendPort = TrapPort); 00163 00164 static void WriteTrap ( PChannel & channel, 00165 PSNMP::TrapType trapType, 00166 const PString & community, 00167 const PString & enterprise, 00168 PINDEX specificTrap, 00169 PASNUnsigned timeTicks, 00170 const PSNMPVarBindingList & vars, 00171 const PIPSocket::Address & agentAddress); 00172 00173 /* 00174 static PBoolean DecodeTrap(const PBYTEArray & readBuffer, 00175 PINDEX & version, 00176 PString & community, 00177 PString & enterprise, 00178 PIPSocket::Address & address, 00179 PINDEX & genericTrapType, 00180 PINDEX & specificTrapType, 00181 PASNUnsigned & timeTicks, 00182 PSNMPVarBindingList & varsOut); 00183 */ 00184 00185 typedef list<pair<PString,PRFC1155_ObjectSyntax> > BindingList; 00186 }; 00187 00188 00190 00193 class PSNMPClient : public PSNMP 00194 { 00195 PCLASSINFO(PSNMPClient, PSNMP) 00196 public: 00197 PSNMPClient(const PString & host, 00198 PINDEX retryMax = 5, 00199 PINDEX timeoutMax = 5, 00200 PINDEX rxBufferSize = 1500, 00201 PINDEX txSize = 484); 00202 00203 PSNMPClient(PINDEX retryMax = 5, 00204 PINDEX timeoutMax = 5, 00205 PINDEX rxBufferSize = 1500, 00206 PINDEX txSize = 484); 00207 00208 void SetVersion(PASNInt version); 00209 PASNInt GetVersion() const; 00210 00211 void SetCommunity(const PString & str); 00212 PString GetCommunity() const; 00213 00214 void SetRequestID(PASNInt requestID); 00215 PASNInt GetRequestID() const; 00216 00217 PBoolean WriteGetRequest (PSNMPVarBindingList & varsIn, 00218 PSNMPVarBindingList & varsOut); 00219 00220 PBoolean WriteGetNextRequest (PSNMPVarBindingList & varsIn, 00221 PSNMPVarBindingList & varsOut); 00222 00223 PBoolean WriteSetRequest (PSNMPVarBindingList & varsIn, 00224 PSNMPVarBindingList & varsOut); 00225 00226 ErrorType GetLastErrorCode() const; 00227 PINDEX GetLastErrorIndex() const; 00228 PString GetLastErrorText() const; 00229 00230 protected: 00231 PBoolean WriteRequest (PASNInt requestCode, 00232 PSNMPVarBindingList & varsIn, 00233 PSNMPVarBindingList & varsOut); 00234 00235 00236 PBoolean ReadRequest(PBYTEArray & readBuffer); 00237 00238 PString hostName; 00239 PString community; 00240 PASNInt requestId; 00241 PASNInt version; 00242 PINDEX retryMax; 00243 PINDEX lastErrorIndex; 00244 ErrorType lastErrorCode; 00245 PBYTEArray readBuffer; 00246 PINDEX maxRxSize; 00247 PINDEX maxTxSize; 00248 }; 00249 00250 00252 00255 class PSNMPServer : public PSNMP 00256 { 00257 PCLASSINFO(PSNMPServer, PSNMP) 00258 public: 00259 00260 PSNMPServer(PIPSocket::Address binding = PIPSocket::GetDefaultIpAny(), 00261 WORD localPort = 161, 00262 PINDEX timeout = 5000, 00263 PINDEX rxSize = 10000, 00264 PINDEX txSize = 10000); 00265 00266 ~PSNMPServer(); 00267 00268 void Main(); 00269 00270 void SetVersion(PASNInt newVersion); 00271 PBoolean HandleChannel(); 00272 PBoolean ProcessPDU(const PBYTEArray & readBuffer, PBYTEArray & writeBuffer); 00273 00274 virtual PBoolean Authorise(const PIPSocket::Address & received); 00275 virtual PBoolean ConfirmVersion(PASN_Integer vers); 00276 virtual PBoolean ConfirmCommunity(PASN_OctetString & community); 00277 00278 virtual PBoolean MIB_LocalMatch(PSNMP_PDU & pdu); 00279 00280 virtual PBoolean OnGetRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode); 00281 virtual PBoolean OnGetNextRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode); 00282 virtual PBoolean OnSetRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode); 00283 00284 PSNMP::ErrorType SendGetResponse (PSNMPVarBindingList & vars); 00285 00286 protected: 00287 PThreadObj<PSNMPServer> m_thread; 00288 PString community; 00289 PASN_Integer version; 00290 PINDEX lastErrorIndex; 00291 ErrorType lastErrorCode; 00292 PBYTEArray readBuffer; 00293 PINDEX maxRxSize; 00294 PINDEX maxTxSize; 00295 PUDPSocket *baseSocket; 00296 PDictionary<PRFC1155_ObjectName, PRFC1155_ObjectSyntax> objList; 00297 }; 00298 00299 #endif // P_SNMP 00300 00301 #endif // PTLIB_PSNMP_H 00302 00303 00304 // End Of File ///////////////////////////////////////////////////////////////