PTLib  Version 2.10.11
psnmp.h
Go to the documentation of this file.
1 /*
2  * psnmp.h
3  *
4  * Simple Network Management Protocol classes.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 26773 $
27  * $Author: rjongbloed $
28  * $Date: 2011-12-07 00:43:33 -0600 (Wed, 07 Dec 2011) $
29  */
30 
31 #ifndef PTLIB_PSNMP_H
32 #define PTLIB_PSNMP_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #ifdef P_SNMP
39 
40 #include <ptlib/sockets.h>
41 #include <ptclib/snmp.h>
42 #include <ptclib/pasn.h>
43 
44 #include <list>
45 #include <vector>
46 
48 
52 {
54  public:
55 
56  void Append(const PString & objectID);
57  void Append(const PString & objectID, PASNObject * obj);
58  void AppendString(const PString & objectID, const PString & str);
59 
60  void RemoveAll();
61 
62  PINDEX GetSize() const;
63 
64  PINDEX GetIndex(const PString & objectID) const;
65  PString GetObjectID(PINDEX idx) const;
66  PASNObject & operator[](PINDEX idx) const;
67 
68  void PrintOn(ostream & strm) const;
69 
70  protected:
72  PASNObjectArray values;
73 };
74 
76 
79 class PSNMP : public PIndirectChannel
80 {
82  public:
83  enum ErrorType {
84  // Standard RFC1157 errors
85  NoError = 0,
86  TooBig = 1,
87  NoSuchName = 2,
88  BadValue = 3,
89  ReadOnly = 4,
90  GenErr = 5,
91 
92  // Additional errors
98  NumErrors
99  };
100 
101  enum RequestType {
102  GetRequest = 0,
103  GetNextRequest = 1,
104  GetResponse = 2,
105  SetRequest = 3,
106  Trap = 4,
107  };
108 
109  enum { TrapPort = 162 };
110 
111  enum TrapType {
112  ColdStart = 0,
113  WarmStart = 1,
114  LinkDown = 2,
115  LinkUp = 3,
116  AuthenticationFailure = 4,
117  EGPNeighbourLoss = 5,
118  EnterpriseSpecific = 6,
119  NumTrapTypes
120  };
121 
122  static PString GetErrorText(ErrorType err);
123 
124  static PString GetTrapTypeText(PINDEX code);
125 
126  static void SendEnterpriseTrap (
127  const PIPSocket::Address & addr,
128  const PString & community,
129  const PString & enterprise,
130  PINDEX specificTrap,
131  PASNUnsigned timeTicks,
132  WORD sendPort = TrapPort);
133 
134  static void SendEnterpriseTrap (
135  const PIPSocket::Address & addr,
136  const PString & community,
137  const PString & enterprise,
138  PINDEX specificTrap,
139  PASNUnsigned timeTicks,
140  const PSNMPVarBindingList & vars,
141  WORD sendPort = TrapPort);
142 
143  static void SendTrap (
144  const PIPSocket::Address & addr,
145  PSNMP::TrapType trapType,
146  const PString & community,
147  const PString & enterprise,
148  PINDEX specificTrap,
149  PASNUnsigned timeTicks,
150  const PSNMPVarBindingList & vars,
151  WORD sendPort = TrapPort);
152 
153  static void SendTrap (
154  const PIPSocket::Address & addr,
155  PSNMP::TrapType trapType,
156  const PString & community,
157  const PString & enterprise,
158  PINDEX specificTrap,
159  PASNUnsigned timeTicks,
160  const PSNMPVarBindingList & vars,
161  const PIPSocket::Address & agentAddress,
162  WORD sendPort = TrapPort);
163 
164  static void WriteTrap ( PChannel & channel,
165  PSNMP::TrapType trapType,
166  const PString & community,
167  const PString & enterprise,
168  PINDEX specificTrap,
169  PASNUnsigned timeTicks,
170  const PSNMPVarBindingList & vars,
171  const PIPSocket::Address & agentAddress);
172 
173 /*
174  static PBoolean DecodeTrap(const PBYTEArray & readBuffer,
175  PINDEX & version,
176  PString & community,
177  PString & enterprise,
178  PIPSocket::Address & address,
179  PINDEX & genericTrapType,
180  PINDEX & specificTrapType,
181  PASNUnsigned & timeTicks,
182  PSNMPVarBindingList & varsOut);
183 */
184 
185  typedef list<pair<PString,PRFC1155_ObjectSyntax> > BindingList;
186 };
187 
188 
190 
193 class PSNMPClient : public PSNMP
194 {
196  public:
197  PSNMPClient(const PString & host,
198  PINDEX retryMax = 5,
199  PINDEX timeoutMax = 5,
200  PINDEX rxBufferSize = 1500,
201  PINDEX txSize = 484);
202 
203  PSNMPClient(PINDEX retryMax = 5,
204  PINDEX timeoutMax = 5,
205  PINDEX rxBufferSize = 1500,
206  PINDEX txSize = 484);
207 
208  void SetVersion(PASNInt version);
209  PASNInt GetVersion() const;
210 
211  void SetCommunity(const PString & str);
212  PString GetCommunity() const;
213 
214  void SetRequestID(PASNInt requestID);
215  PASNInt GetRequestID() const;
216 
217  PBoolean WriteGetRequest (PSNMPVarBindingList & varsIn,
218  PSNMPVarBindingList & varsOut);
219 
220  PBoolean WriteGetNextRequest (PSNMPVarBindingList & varsIn,
221  PSNMPVarBindingList & varsOut);
222 
223  PBoolean WriteSetRequest (PSNMPVarBindingList & varsIn,
224  PSNMPVarBindingList & varsOut);
225 
226  ErrorType GetLastErrorCode() const;
227  PINDEX GetLastErrorIndex() const;
228  PString GetLastErrorText() const;
229 
230  protected:
231  PBoolean WriteRequest (PASNInt requestCode,
232  PSNMPVarBindingList & varsIn,
233  PSNMPVarBindingList & varsOut);
234 
235 
236  PBoolean ReadRequest(PBYTEArray & readBuffer);
237 
242  PINDEX retryMax;
246  PINDEX maxRxSize;
247  PINDEX maxTxSize;
248 };
249 
250 
252 
255 class PSNMPServer : public PSNMP
256 {
258  public:
259 
261  WORD localPort = 161,
262  PINDEX timeout = 5000,
263  PINDEX rxSize = 10000,
264  PINDEX txSize = 10000);
265 
266  ~PSNMPServer();
267 
268  void Main();
269 
270  void SetVersion(PASNInt newVersion);
271  PBoolean HandleChannel();
272  PBoolean ProcessPDU(const PBYTEArray & readBuffer, PBYTEArray & writeBuffer);
273 
274  virtual PBoolean Authorise(const PIPSocket::Address & received);
275  virtual PBoolean ConfirmVersion(PASN_Integer vers);
276  virtual PBoolean ConfirmCommunity(PASN_OctetString & community);
277 
278  virtual PBoolean MIB_LocalMatch(PSNMP_PDU & pdu);
279 
280  virtual PBoolean OnGetRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
281  virtual PBoolean OnGetNextRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
282  virtual PBoolean OnSetRequest (PINDEX reqID, PSNMP::BindingList & vars, PSNMP::ErrorType & errCode);
283 
284  PSNMP::ErrorType SendGetResponse (PSNMPVarBindingList & vars);
285 
286  protected:
293  PINDEX maxRxSize;
294  PINDEX maxTxSize;
297 };
298 
299 #endif // P_SNMP
300 
301 #endif // PTLIB_PSNMP_H
302 
303 
304 // End Of File ///////////////////////////////////////////////////////////////
PBYTEArray readBuffer
Definition: psnmp.h:245
list< pair< PString, PRFC1155_ObjectSyntax > > BindingList
Definition: psnmp.h:185
PString GetObjectID(PINDEX idx) const
PASN_Integer version
Definition: psnmp.h:289
PINDEX GetSize() const
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PBYTEArray readBuffer
Definition: psnmp.h:292
PINDEX retryMax
Definition: psnmp.h:242
PString community
Definition: psnmp.h:288
Definition: psnmp.h:95
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:47
A descendant of PUDPSocket which can perform SNMP calls.
Definition: psnmp.h:79
PString community
Definition: psnmp.h:239
This is a channel that operates indirectly through another channel(s).
Definition: indchan.h:49
TrapType
Definition: psnmp.h:111
PStringArray objectIds
Definition: psnmp.h:71
This is an array collection class of PString objects.
Definition: pstring.h:2024
PINDEX lastErrorIndex
Definition: psnmp.h:290
PInt32 PASNInt
Definition: pasn.h:43
PINDEX lastErrorIndex
Definition: psnmp.h:243
This class defines the common behviour of all ASN objects.
Definition: pasn.h:64
Class which gets SNMP data.
Definition: psnmp.h:193
PINDEX maxTxSize
Definition: psnmp.h:294
BOOL PBoolean
Definition: object.h:102
void AppendString(const PString &objectID, const PString &str)
Array of unsigned characters.
Definition: array.h:670
PString hostName
Definition: psnmp.h:238
Definition: psnmp.h:96
ErrorType
Definition: psnmp.h:83
PThreadObj< PSNMPServer > m_thread
Definition: psnmp.h:287
PASNInt requestId
Definition: psnmp.h:240
Definition: psnmp.h:97
PINDEX maxRxSize
Definition: psnmp.h:246
Abstract class defining I/O channel semantics.
Definition: channel.h:107
PASNObjectArray values
Definition: psnmp.h:72
PINDEX maxTxSize
Definition: psnmp.h:247
The character string class.
Definition: pstring.h:108
Definition: psnmp.h:93
PASNInt version
Definition: psnmp.h:241
A list of object IDs and their values.
Definition: psnmp.h:51
PASNObject & operator[](PINDEX idx) const
static PIPSocket::Address GetDefaultIpAny()
void Append(const PString &objectID)
ErrorType lastErrorCode
Definition: psnmp.h:244
A class describing an IP address.
Definition: ipsock.h:75
PINDEX GetIndex(const PString &objectID) const
PUDPSocket * baseSocket
Definition: psnmp.h:295
void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
Class for ASN Octet String type.
Definition: asner.h:458
RequestType
Definition: psnmp.h:101
Class for ASN Integer type.
Definition: asner.h:234
Definition: snmp.h:135
Class which supplies SNMP data.
Definition: psnmp.h:255
PDictionary< PRFC1155_ObjectName, PRFC1155_ObjectSyntax > objList
Definition: psnmp.h:296
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PINDEX maxRxSize
Definition: psnmp.h:293
DWORD PASNUnsigned
Definition: pasn.h:44
Definition: psnmp.h:94
ErrorType lastErrorCode
Definition: psnmp.h:291