PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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: 25507 $
27  * $Author: rjongbloed $
28  * $Date: 2011-04-06 15:02:42 +1000 (Wed, 06 Apr 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,
88  BadValue = 3,
89  ReadOnly = 4,
90  GenErr = 5,
91 
92  // Additional errors
99  };
100 
101  enum RequestType {
106  Trap = 4,
107  };
108 
109  enum { TrapPort = 162 };
110 
111  enum TrapType {
114  LinkDown = 2,
115  LinkUp = 3,
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 
218  PSNMPVarBindingList & varsOut);
219 
221  PSNMPVarBindingList & varsOut);
222 
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 
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);
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 
285 
286  protected:
289  PASN_Integer version;
293  PINDEX maxRxSize;
294  PINDEX maxTxSize;
297 };
298 
299 #endif // P_SNMP
300 
301 #endif // PTLIB_PSNMP_H
302 
303 
304 // End Of File ///////////////////////////////////////////////////////////////