4 * SNMP client interface
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,
36 // **************************************************************************
38 // **************************************************************************
48 // **************************************************************************
50 // **************************************************************************
54 VarBind(const Asn::ObjectId
&oid
, Variable
*data
= NULL
);
55 VarBind(Asn::Sequence
&seq
);
58 bool Extract(Variable
*data
);
59 Asn::ObjectId
&Oid() { return *_oid
; }
61 Asn::Sequence
*GetAsn();
67 VarBind(const VarBind
&rhs
);
68 VarBind
&operator=(const VarBind
&rhs
);
71 // **************************************************************************
73 // **************************************************************************
78 VarBindList(Asn::Sequence
&seq
);
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();
89 aarray
<VarBind
*> _vblist
;
91 VarBindList(const VarBindList
&rhs
);
92 VarBindList
&operator=(const VarBindList
&rhs
);
95 // **************************************************************************
97 // **************************************************************************
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
);
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
;
120 // **************************************************************************
122 // **************************************************************************
123 class VbListMessage
: public Message
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
);
142 Asn::Identifier type
,
143 const char *community
,
146 virtual Asn::Sequence
*GetAsn();
151 VarBindList
*_vblist
;
154 // **************************************************************************
156 // **************************************************************************
157 class TrapMessage
: public Message
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
);
170 TrapMessage(Asn::Identifier type
, const char *community
, Asn::Sequence
&seq
);
171 virtual Asn::Sequence
*GetAsn() { return NULL
; }
173 Asn::ObjectId
*_enterprise
;
176 unsigned int _timestamp
;
177 VarBindList
*_vblist
;
180 // **************************************************************************
182 // **************************************************************************
190 bool Open(const char *host
, unsigned short port
= SNMP_AGENT_PORT
,
191 const char *comm
= "public", bool trap
= false);
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
; }
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;
223 struct sockaddr_in _destaddr
;