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,
34 static const unsigned char UNIVERSAL
= 0x00;
35 static const unsigned char APPLICATION
= 0x40;
36 static const unsigned char CONTEXT
= 0x80;
37 static const unsigned char PRIVATE
= 0xC0;
39 // Primitive/Constructed field
40 static const unsigned char CONSTRUCTED
= 0x20;
41 static const unsigned char PRIMITIVE
= 0x00;
43 // Built-in types from ASN.1
44 typedef unsigned char Identifier
;
45 static const Identifier INTEGER
= UNIVERSAL
| PRIMITIVE
| 0x02;
46 static const Identifier BITSTRING
= UNIVERSAL
| PRIMITIVE
| 0x03;
47 static const Identifier OCTETSTRING
= UNIVERSAL
| PRIMITIVE
| 0x04;
48 static const Identifier NULLL
= UNIVERSAL
| PRIMITIVE
| 0x05;
49 static const Identifier OBJECTID
= UNIVERSAL
| PRIMITIVE
| 0x06;
50 static const Identifier SEQUENCE
= UNIVERSAL
| CONSTRUCTED
| 0x10;
52 // SNMP-specific types
53 static const Identifier IPADDRESS
= APPLICATION
| PRIMITIVE
| 0x00;
54 static const Identifier COUNTER
= APPLICATION
| PRIMITIVE
| 0x01;
55 static const Identifier GAUGE
= APPLICATION
| PRIMITIVE
| 0x02;
56 static const Identifier TIMETICKS
= APPLICATION
| PRIMITIVE
| 0x03;
57 static const Identifier GET_REQ_PDU
= CONTEXT
| CONSTRUCTED
| 0x00;
58 static const Identifier GETNEXT_REQ_PDU
= CONTEXT
| CONSTRUCTED
| 0x01;
59 static const Identifier GET_RSP_PDU
= CONTEXT
| CONSTRUCTED
| 0x02;
60 static const Identifier SET_REQ_PDU
= CONTEXT
| CONSTRUCTED
| 0x03;
61 static const Identifier TRAP_PDU
= CONTEXT
| CONSTRUCTED
| 0x04;
63 // **************************************************************************
64 // Forward declarations
65 // **************************************************************************
71 // **************************************************************************
73 // **************************************************************************
78 Object(Identifier type
): _type(type
) {}
81 static Object
*Demarshal(unsigned char *&buffer
, unsigned int &buflen
);
83 Identifier
Type() const { return _type
; }
85 Integer
*AsInteger() { return (Integer
*) this; }
86 ObjectId
*AsObjectId() { return (ObjectId
*) this; }
87 OctetString
*AsOctetString() { return (OctetString
*)this; }
88 Sequence
*AsSequence() { return (Sequence
*) this; }
90 virtual bool IsInteger() { return false; }
91 virtual bool IsObjectId() { return false; }
92 virtual bool IsOctetString() { return false; }
93 virtual bool IsSequence() { return false; }
96 unsigned char *&buffer
,
97 unsigned int &buflen
) const = 0;
99 virtual Object
*copy() const = 0;
103 virtual bool demarshal(unsigned char *&buffer
, unsigned int &buflen
) = 0;
104 bool marshalType(unsigned char *&buffer
, unsigned int &buflen
) const;
108 unsigned char *&buffer
,
109 unsigned int &buflen
) const;
111 bool demarshalLength(
112 unsigned char *&buffer
,
113 unsigned int &buflen
,
114 unsigned int &vallen
) const;
116 int numbits(unsigned int num
) const;
121 // **************************************************************************
123 // **************************************************************************
124 class Integer
: public Object
128 Integer(Identifier id
= INTEGER
) :
129 Object(id
), _value(0), _signed(false) {}
132 Object(INTEGER
), _value(value
), _signed(value
< 0) {}
134 virtual ~Integer() {}
136 unsigned int UintValue() const { return _value
; }
137 int IntValue() const { return (int)_value
; }
139 Integer
&operator=(int value
)
140 { _value
= value
; _signed
= value
< 0; return *this; }
141 Integer
&operator=(unsigned int value
)
142 { _value
= value
; _signed
= false; return *this; }
144 virtual Object
*copy() const { return new Integer(*this); }
145 virtual bool Marshal(unsigned char *&buffer
, unsigned int &buflen
) const;
146 virtual bool IsInteger() { return true; }
150 virtual bool demarshal(unsigned char *&buffer
, unsigned int &buflen
);
156 // **************************************************************************
158 // **************************************************************************
159 class OctetString
: public Object
163 OctetString() : Object(OCTETSTRING
), _data(NULL
), _len(0) {}
164 OctetString(const char *value
);
165 OctetString(const unsigned char *value
, unsigned int len
);
166 virtual ~OctetString() { delete [] _data
; }
168 OctetString(const OctetString
&rhs
);
169 OctetString
&operator=(const OctetString
&rhs
);
171 const unsigned char *Value() const { return _data
; }
172 const unsigned int Length() const { return _len
; }
174 operator const char *() const { return (const char *)_data
; }
176 virtual Object
*copy() const { return new OctetString(*this); }
177 virtual bool Marshal(unsigned char *&buffer
, unsigned int &buflen
) const;
178 virtual bool IsOctetString() { return true; }
182 virtual bool demarshal(unsigned char *&buffer
, unsigned int &buflen
);
184 void assign(const unsigned char *data
, unsigned int len
);
186 unsigned char *_data
;
190 // **************************************************************************
192 // **************************************************************************
193 class ObjectId
: public Object
197 ObjectId() : Object(OBJECTID
), _value(NULL
), _count(0) {}
198 ObjectId(const int oid
[]);
199 virtual ~ObjectId() { delete [] _value
; }
201 ObjectId(const ObjectId
&rhs
);
202 ObjectId
&operator=(const ObjectId
&rhs
);
203 ObjectId
&operator=(const int oid
[]);
205 bool operator==(const ObjectId
&rhs
) const;
206 bool operator==(const int oid
[]) const;
207 bool operator!=(const ObjectId
&rhs
) const { return !(*this == rhs
); }
208 bool operator!=(const int oid
[]) const { return !(*this == oid
); }
210 bool IsChildOf(const int oid
[]);
212 virtual Object
*copy() const { return new ObjectId(*this); }
213 virtual bool Marshal(unsigned char *&buffer
, unsigned int &buflen
) const;
214 virtual bool IsObjectId() { return true; }
218 virtual bool demarshal(unsigned char *&buffer
, unsigned int &buflen
);
219 void assign(const int oid
[]);
220 void assign(const int oid
[], unsigned int count
);
226 // **************************************************************************
228 // **************************************************************************
229 class Null
: public Object
233 Null() : Object(NULLL
) {}
236 virtual Object
*copy() const { return new Null(*this); }
237 virtual bool Marshal(unsigned char *&buffer
, unsigned int &buflen
) const;
241 virtual bool demarshal(unsigned char *&buffer
, unsigned int &buflen
);
244 // **************************************************************************
246 // **************************************************************************
247 class Sequence
: public Object
251 Sequence(Identifier type
= SEQUENCE
);
254 Sequence(const Sequence
&rhs
);
255 Sequence
&operator=(const Sequence
&rhs
);
257 unsigned int Size() { return _size
; }
258 Object
*operator[](unsigned int idx
);
259 void Append(Object
*obj
);
261 virtual Object
*copy() const { return new Sequence(*this); }
262 virtual bool Marshal(unsigned char *&buffer
, unsigned int &buflen
) const;
263 virtual bool IsSequence() { return true; }
267 virtual bool demarshal(unsigned char *&buffer
, unsigned int &buflen
);
268 void assign(const Sequence
&rhs
);