author | Michael Krelin <hacker@klever.net> | 2004-07-05 01:53:09 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2004-07-05 01:53:09 (UTC) |
commit | 885f8cc426a8840ae61023b75f3f0e4a1e268082 (patch) (unidiff) | |
tree | 5e942d450c97ca0bf0f9cfb80aa0fefb486535d9 /shared-code/SNMPeer.h | |
download | kinsole-885f8cc426a8840ae61023b75f3f0e4a1e268082.zip kinsole-885f8cc426a8840ae61023b75f3f0e4a1e268082.tar.gz kinsole-885f8cc426a8840ae61023b75f3f0e4a1e268082.tar.bz2 |
initial commit into svn repository
git-svn-id: http://svn.klever.net/kin/kinsole/trunk@1 fe716a7a-6dde-0310-88d9-d003556173a8
-rw-r--r-- | shared-code/SNMPeer.h | 286 |
1 files changed, 286 insertions, 0 deletions
diff --git a/shared-code/SNMPeer.h b/shared-code/SNMPeer.h new file mode 100644 index 0000000..68f2efe --- a/dev/null +++ b/shared-code/SNMPeer.h | |||
@@ -0,0 +1,286 @@ | |||
1 | #ifndef__SNMPEER_H | ||
2 | #define __SNMPEER_H | ||
3 | |||
4 | namespace Klever { | ||
5 | |||
6 | class CASNAny{ | ||
7 | public: | ||
8 | enum{ | ||
9 | asnCls = 0xC0, | ||
10 | asnClsUniversal = 0x00, | ||
11 | asnClsApplication = 0x40, | ||
12 | asnClsContextSpecific = 0x80, | ||
13 | asnClsPrivate = 0xC0, | ||
14 | asnConstructed = 0x20, | ||
15 | asnPrimitive = 0x00, | ||
16 | asnTag = 0x1F, | ||
17 | // ASN.1 Primitive Tags | ||
18 | asnTagInteger = 0x02, | ||
19 | asnTagOctetString = 0x04, | ||
20 | asnTagNull = 0x05, | ||
21 | asnTagOID = 0x06, | ||
22 | // ASN.1 Constructed Tags | ||
23 | asnTagSequence = 0x10, | ||
24 | // RFC1155 Primitive Tags | ||
25 | asnTagIP = 0x00, | ||
26 | asnTagCounter = 0x01, | ||
27 | asnTagGauge = 0x02, | ||
28 | asnTagTicks = 0x03, | ||
29 | asnTagOpaque = 0x04, | ||
30 | // RFC1213 alias | ||
31 | asnTagDispString = 0x04,// (ASN.1 Octet string) | ||
32 | // RFC1157 Constructed Tags | ||
33 | asnTagGetRequest = 0x00, | ||
34 | asnTagGetNextRequest = 0x01, | ||
35 | asnTagGetResponse = 0x02, | ||
36 | asnTagSetRequest = 0x03, | ||
37 | asnTagTrap = 0x04 | ||
38 | }; | ||
39 | enum{ | ||
40 | typeASNInteger = (asnClsUniversal|asnPrimitive|asnTagInteger), | ||
41 | typeASNOctetString = (asnClsUniversal|asnPrimitive|asnTagOctetString), | ||
42 | typeASNNull = (asnClsUniversal|asnPrimitive|asnTagNull), | ||
43 | typeASNOID = (asnClsUniversal|asnPrimitive|asnTagOID), | ||
44 | |||
45 | typeASNSequence = (asnClsUniversal|asnConstructed|asnTagSequence), | ||
46 | typeASNSequenceOf = (asnClsUniversal|asnConstructed|asnTagSequence), | ||
47 | |||
48 | typeASNIP = (asnClsApplication|asnPrimitive|asnTagIP), | ||
49 | typeASNCounter = (asnClsApplication|asnPrimitive|asnTagCounter), | ||
50 | typeASNGauge = (asnClsApplication|asnPrimitive|asnTagGauge), | ||
51 | typeASNTicks = (asnClsApplication|asnPrimitive|asnTagTicks), | ||
52 | typeASNOpaque = (asnClsApplication|asnPrimitive|asnTagOpaque), | ||
53 | typeASNDispString = (asnClsUniversal|asnPrimitive|asnTagOctetString), | ||
54 | |||
55 | typeASNGetRequest = (asnClsContextSpecific|asnConstructed|asnTagGetRequest), | ||
56 | typeASNGetNextRequest = (asnClsContextSpecific|asnConstructed|asnTagGetNextRequest), | ||
57 | typeASNGetResponse = (asnClsContextSpecific|asnConstructed|asnTagGetResponse), | ||
58 | typeASNSetRequest = (asnClsContextSpecific|asnConstructed|asnTagSetRequest), | ||
59 | typeASNTrap = (asnClsContextSpecific|asnConstructed|asnTagTrap) | ||
60 | }; | ||
61 | |||
62 | typedefLONG asnInteger; | ||
63 | typedef LARGE_INTEGER asnInteger64; | ||
64 | typedefDWORD asnCounter; | ||
65 | typedef ULARGE_INTEGER asnCounter64; | ||
66 | typedef DWORD asnGauge; | ||
67 | typedef ULARGE_INTEGER asnGauge64; | ||
68 | typedefDWORD asnTicks; | ||
69 | typedef ULARGE_INTEGER asnTicks64; | ||
70 | struct asnDynamic{ | ||
71 | UINT size; | ||
72 | LPBYTE data; | ||
73 | BOOL Allocate(UINT size) { | ||
74 | BOOL rv = Free(); | ||
75 | if(size) | ||
76 | rv=rv&&(data=new BYTE[size]); | ||
77 | if(rv) | ||
78 | asnDynamic::size=size; | ||
79 | return rv; | ||
80 | } | ||
81 | BOOL Set(LPBYTE data,UINT size) { | ||
82 | BOOL rv = Allocate(size); | ||
83 | if(rv && size) | ||
84 | memmove(asnDynamic::data,data,size); | ||
85 | return rv; | ||
86 | } | ||
87 | BOOL Free() { | ||
88 | if(!size) | ||
89 | return TRUE; | ||
90 | delete data; | ||
91 | size=0; | ||
92 | data=0; | ||
93 | return TRUE; | ||
94 | } | ||
95 | void Clean() { | ||
96 | size=0; | ||
97 | data=0; | ||
98 | } | ||
99 | BOOL Copy(asnDynamic& src) { | ||
100 | BOOL rv = Free(); | ||
101 | if(rv){ | ||
102 | if(src.size) | ||
103 | rv=rv&&(data = new BYTE[src.size]); | ||
104 | if(rv){ | ||
105 | if(size=src.size) | ||
106 | memmove(data,src.data,size); | ||
107 | } | ||
108 | } | ||
109 | return rv; | ||
110 | } | ||
111 | }; | ||
112 | typedefasnDynamic asnOctetString; | ||
113 | typedef asnDynamic asnOID; | ||
114 | typedef in_addr asnIP; | ||
115 | typedef asnDynamic asnSequence; | ||
116 | |||
117 | BYTE type; | ||
118 | enum _storeType{ | ||
119 | storeDynamic, | ||
120 | storeStatic | ||
121 | }storeType; | ||
122 | union{ | ||
123 | asnInteger number; | ||
124 | asnInteger64 number64; | ||
125 | asnOctetString string; | ||
126 | asnOID oid; | ||
127 | asnSequence sequence; | ||
128 | asnIP ip; | ||
129 | asnCounter counter; | ||
130 | asnCounter64 counter64; | ||
131 | asnGauge gauge; | ||
132 | asnGauge64 gauge64; | ||
133 | asnTicks ticks; | ||
134 | asnTicks64 ticks64; | ||
135 | asnDynamic data; | ||
136 | } value; | ||
137 | |||
138 | CASNAny() : type(typeASNNull), storeType(storeStatic) { value.data.Clean(); } | ||
139 | CASNAny(CASNAny& src) : type(typeASNNull), storeType(storeStatic) { value.data.Clean();Copy(src); } | ||
140 | CASNAny(BYTE type) : type(type), storeType(storeStatic) { value.data.Clean(); } | ||
141 | CASNAny(BYTE type,LONG number) : type(typeASNNull), storeType(storeStatic) { value.data.Clean();Set(type,number); } | ||
142 | CASNAny(BYTE type,LONGLONG number) : type(typeASNNull), storeType(storeStatic) { value.data.Clean();Set(type,number); } | ||
143 | CASNAny(BYTE type,LPCTSTR string) : type(typeASNNull), storeType(storeStatic) { value.data.Clean();Set(type,string); } | ||
144 | CASNAny(BYTE type,LPBYTE data,UINT length) : type(typeASNNull), storeType(storeStatic) { value.data.Clean();Set(type,data,length); } | ||
145 | CASNAny(BYTE type,UINT* data,UINT size) : type(typeASNNull), storeType(storeStatic) { value.data.Clean();Set(type,(LPBYTE)data,size); } | ||
146 | CASNAny(in_addr& ip) : type(typeASNNull), storeType(storeStatic) { value.data.Clean();Set(ip); } | ||
147 | ~CASNAny() { Free(); } | ||
148 | |||
149 | BOOL Set(BYTE type) { | ||
150 | BOOL rv = Free(); | ||
151 | CASNAny::type=type; | ||
152 | return rv; | ||
153 | } | ||
154 | BOOL Set(BYTE type,LONG number) { | ||
155 | BOOL rv = Free(); | ||
156 | CASNAny::type=type; | ||
157 | value.number=number; | ||
158 | storeType=storeStatic; | ||
159 | return rv; | ||
160 | } | ||
161 | BOOL Set(BYTE type,LONGLONG number) { | ||
162 | BOOL rv = Free(); | ||
163 | CASNAny::type=type; | ||
164 | value.number64.QuadPart = number; | ||
165 | storeType=storeStatic; | ||
166 | return rv; | ||
167 | } | ||
168 | BOOL Set(BYTE type,LPCTSTR string) { | ||
169 | BOOL rv = Free(); | ||
170 | CASNAny::type=type; | ||
171 | rv=rv&&value.string.Set((LPBYTE)string,strlen(string)+1); | ||
172 | if(rv){ | ||
173 | value.string.size--; | ||
174 | storeType=storeDynamic; | ||
175 | } | ||
176 | return rv; | ||
177 | } | ||
178 | BOOL Set(BYTE type,LPBYTE data,UINT length) { | ||
179 | BOOL rv = Free(); | ||
180 | CASNAny::type=type; | ||
181 | rv=rv&&value.data.Set(data,length); | ||
182 | if(rv) | ||
183 | storeType=storeDynamic; | ||
184 | return rv; | ||
185 | } | ||
186 | BOOL Set(in_addr& ip) { | ||
187 | BOOL rv = Free(); | ||
188 | memmove(&value.ip,&ip,sizeof(value.ip)); | ||
189 | type=typeASNIP; | ||
190 | storeType=storeStatic; | ||
191 | return rv; | ||
192 | } | ||
193 | BOOL Free() { | ||
194 | if(storeType==storeDynamic) | ||
195 | value.data.Free(); | ||
196 | else{ | ||
197 | memset(&value,0,sizeof(value)); | ||
198 | value.data.Clean(); | ||
199 | } | ||
200 | storeType=storeStatic; | ||
201 | type=typeASNNull; | ||
202 | return TRUE; | ||
203 | } | ||
204 | BOOL Copy(CASNAny& src) { | ||
205 | BOOL rv = Free(); | ||
206 | if(src.storeType==storeDynamic){ | ||
207 | rv=rv&&value.data.Copy(src.value.data); | ||
208 | if(rv){ | ||
209 | type=src.type; | ||
210 | storeType=src.storeType; | ||
211 | } | ||
212 | }else{ | ||
213 | memmove(this,&src,sizeof(*this)); | ||
214 | } | ||
215 | return rv; | ||
216 | } | ||
217 | CASNAny& operator=(CASNAny& src) { | ||
218 | VERIFY(Copy(src)); | ||
219 | return *this; | ||
220 | } | ||
221 | |||
222 | // High Level | ||
223 | CString GetString() { | ||
224 | ASSERT(storeType==storeDynamic); | ||
225 | CString rv; | ||
226 | LPTSTR b = rv.GetBuffer(value.data.size+1); | ||
227 | ASSERT(b); | ||
228 | b[value.data.size]=0; | ||
229 | memmove(b,value.data.data,value.data.size); | ||
230 | rv.ReleaseBuffer(); | ||
231 | return rv; | ||
232 | } | ||
233 | }; | ||
234 | |||
235 | |||
236 | class CSNMPVarBind{ | ||
237 | public: | ||
238 | CASNAny name; | ||
239 | CASNAny value; | ||
240 | |||
241 | CSNMPVarBind() {} | ||
242 | CSNMPVarBind(CASNAny& name,CASNAny& value) : name(name), value(value) {} | ||
243 | CSNMPVarBind(CASNAny& name) : name(name) {} | ||
244 | CSNMPVarBind(CSNMPVarBind& src) { Copy(src); } | ||
245 | BOOL Copy(CSNMPVarBind& src) { | ||
246 | name.Copy(src.name); | ||
247 | value.Copy(src.value); | ||
248 | return TRUE; | ||
249 | } | ||
250 | CSNMPVarBind& operator=(CSNMPVarBind& src) { | ||
251 | Copy(src); | ||
252 | return *this; | ||
253 | } | ||
254 | // High level | ||
255 | BOOL IsName(UINT* prefix,UINT prefixSize,BOOL bExact=FALSE) { | ||
256 | if(name.type!=CASNAny::typeASNOID) | ||
257 | return FALSE; | ||
258 | if(name.value.oid.size<prefixSize) | ||
259 | return FALSE; | ||
260 | if(bExact && (name.value.oid.size!=prefixSize)) | ||
261 | return FALSE; | ||
262 | return !memcmp(prefix,name.value.oid.data,prefixSize); | ||
263 | } | ||
264 | }; | ||
265 | |||
266 | class CSNMPVarBindList: public CList<CSNMPVarBind,CSNMPVarBind&> { | ||
267 | public: | ||
268 | CSNMPVarBind* GetVarBind(UINT* prefix,UINT prefixSize,BOOL bExact=FALSE) { | ||
269 | POSITION p = GetHeadPosition(); | ||
270 | while(p){ | ||
271 | CSNMPVarBind& vb = GetNext(p); | ||
272 | if(vb.IsName(prefix,prefixSize,bExact)) | ||
273 | return &vb; | ||
274 | } | ||
275 | return NULL; | ||
276 | } | ||
277 | }; | ||
278 | |||
279 | class CSNMPeer{ | ||
280 | public: | ||
281 | virtual BOOL Request(BYTE type,CSNMPVarBindList& in,CSNMPVarBindList& ou) = 0; | ||
282 | }; | ||
283 | |||
284 | }; | ||
285 | |||
286 | #endif// __SNMPEER_H | ||