UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / drivers / snmplite / snmp.h
blob307ddb5ffd1d1bc7f0ca548ad42d2b590f3b0f73
1 /*
2 * snmp.h
4 * SNMP client interface
5 */
7 /*
8 * Copyright (C) 2009 Adam Kropelin
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General
12 * Public License as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA.
25 #ifndef __SNMP_H
26 #define __SNMP_H
28 #include "apc.h"
29 #include "astring.h"
30 #include "aarray.h"
31 #include "alist.h"
32 #include "asn.h"
34 namespace Snmp
36 // **************************************************************************
37 // Types
38 // **************************************************************************
39 struct Variable
41 Asn::Identifier type;
42 int i32;
43 unsigned int u32;
44 astring str;
45 alist<Variable> seq;
48 // **************************************************************************
49 // VarBind
50 // **************************************************************************
51 class VarBind
53 public:
54 VarBind(const Asn::ObjectId &oid, Variable *data = NULL);
55 VarBind(Asn::Sequence &seq);
56 ~VarBind();
58 bool Extract(Variable *data);
59 Asn::ObjectId &Oid() { return *_oid; }
61 Asn::Sequence *GetAsn();
63 private:
64 Asn::ObjectId *_oid;
65 Asn::Object *_data;
67 VarBind(const VarBind &rhs);
68 VarBind &operator=(const VarBind &rhs);
71 // **************************************************************************
72 // VarBindList
73 // **************************************************************************
74 class VarBindList
76 public:
77 VarBindList() {}
78 VarBindList(Asn::Sequence &seq);
79 ~VarBindList();
81 void Append(const Asn::ObjectId &oid, Variable *data = NULL);
83 unsigned int Size() const { return _vblist.size(); }
84 VarBind *operator[](unsigned int idx) { return _vblist[idx]; }
86 Asn::Sequence *GetAsn();
88 private:
89 aarray<VarBind *> _vblist;
91 VarBindList(const VarBindList &rhs);
92 VarBindList &operator=(const VarBindList &rhs);
95 // **************************************************************************
96 // Message
97 // **************************************************************************
98 class Message
100 public:
101 virtual ~Message() {}
103 Asn::Identifier Type() const { return _type; }
104 astring Community() const { return _community; }
106 static Message *Demarshal(unsigned char *&buffer, unsigned int &buflen);
107 bool Marshal(unsigned char *&buffer, unsigned int &buflen);
109 protected:
110 Message() {}
111 Message(Asn::Identifier type, const char *community) :
112 _type(type), _community(community) {}
113 static const int SNMP_VERSION_1 = 0;
114 virtual Asn::Sequence *GetAsn() = 0;
116 Asn::Identifier _type;
117 astring _community;
120 // **************************************************************************
121 // VbListMessage
122 // **************************************************************************
123 class VbListMessage: public Message
125 public:
126 VbListMessage(Asn::Identifier type, const char *community, int reqid);
127 virtual ~VbListMessage() { delete _vblist; }
129 int RequestId() const { return _reqid; }
130 int ErrorStatus() const { return _errstatus; }
131 int ErrorIndex() const { return _errindex; }
133 void Append(const Asn::ObjectId &oid, Variable *data = NULL);
134 unsigned int Size() const { return _vblist->Size(); }
135 VarBind &operator[](unsigned int idx) { return *((*_vblist)[idx]); }
137 static VbListMessage *CreateFromSequence(
138 Asn::Identifier type, const char *community, Asn::Sequence &seq);
140 protected:
141 VbListMessage(
142 Asn::Identifier type,
143 const char *community,
144 Asn::Sequence &seq);
146 virtual Asn::Sequence *GetAsn();
148 int _reqid;
149 int _errstatus;
150 int _errindex;
151 VarBindList *_vblist;
154 // **************************************************************************
155 // TrapMessage
156 // **************************************************************************
157 class TrapMessage: public Message
159 public:
160 virtual ~TrapMessage() { delete _vblist; }
162 int Generic() const { return _generic; }
163 int Specific() const { return _specific; }
164 unsigned int Timestamp() const { return _timestamp; }
166 static TrapMessage *CreateFromSequence(
167 Asn::Identifier type, const char *community, Asn::Sequence &seq);
169 protected:
170 TrapMessage(Asn::Identifier type, const char *community, Asn::Sequence &seq);
171 virtual Asn::Sequence *GetAsn() { return NULL; }
173 Asn::ObjectId *_enterprise;
174 int _generic;
175 int _specific;
176 unsigned int _timestamp;
177 VarBindList *_vblist;
180 // **************************************************************************
181 // SnmpEngine
182 // **************************************************************************
183 class SnmpEngine
185 public:
187 SnmpEngine();
188 ~SnmpEngine();
190 bool Open(const char *host, unsigned short port = SNMP_AGENT_PORT,
191 const char *comm = "public", bool trap = false);
192 void Close();
194 struct OidVar
196 const int *oid;
197 Variable data;
200 bool Get(const int oid[], Variable *data);
201 bool Get(alist<OidVar> &oids);
202 void GetSequence(const int oid[], Variable *data);
204 bool Set(const int oid[], Variable *data);
206 TrapMessage *TrapWait(unsigned int msec);
208 void SetCommunity(const char *comm) { _community = comm; }
210 private:
212 bool issue(Message *pdu);
213 Message *rspwait(unsigned int msec, bool trap = false);
214 VbListMessage *perform(VbListMessage *req);
216 static const unsigned short SNMP_TRAP_PORT = 162;
217 static const unsigned short SNMP_AGENT_PORT = 161;
219 int _socket;
220 int _trapsock;
221 int _reqid;
222 astring _community;
223 struct sockaddr_in _destaddr;
227 #endif