summaryrefslogtreecommitdiffabout
path: root/shared-code/SNMPExtDll.h
authorMichael Krelin <hacker@klever.net>2012-12-08 21:19:17 (UTC)
committer Michael Krelin <hacker@klever.net>2012-12-11 21:59:29 (UTC)
commit8808689fe340bec6e90ab13dd502292b0579cf1f (patch) (unidiff)
tree45b7c863151341f687b74e40bffcbd7ab5468783 /shared-code/SNMPExtDll.h
parent6e7e413ca364d79673e523c09767c18e7cff1bec (diff)
downloadpumpkin-osx/0.0.zip
pumpkin-osx/0.0.tar.gz
pumpkin-osx/0.0.tar.bz2
initial osx portosx/0.0
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'shared-code/SNMPExtDll.h') (more/less context) (ignore whitespace changes)
-rw-r--r--shared-code/SNMPExtDll.h252
1 files changed, 0 insertions, 252 deletions
diff --git a/shared-code/SNMPExtDll.h b/shared-code/SNMPExtDll.h
deleted file mode 100644
index 14c920d..0000000
--- a/shared-code/SNMPExtDll.h
+++ b/dev/null
@@ -1,252 +0,0 @@
1 #ifndef__SNMPEXTDLL_H
2#define __SNMPEXTDLL_H
3
4#include "snmpeer.h"
5
6#include <snmp.h>
7
8namespace Klever {
9
10 class CSNMPExtDLL : public CSNMPeer{
11public:
12 HINSTANCE m_hInstance;
13 HANDLE m_hEvent;
14 AsnObjectIdentifier m_OID;
15 BOOL (SNMP_FUNC_TYPE *m_extInit)(DWORD dw,HANDLE h,AsnObjectIdentifier* aoid);
16 BOOL (SNMP_FUNC_TYPE *m_extQuery)(BYTE b,RFC1157VarBindList* rvbl,AsnInteger* ai1,AsnInteger* ai2);
17 BOOL (SNMP_FUNC_TYPE *m_extTrap)(AsnObjectIdentifier*,AsnNetworkAddress*,AsnInteger*,AsnInteger*,AsnTimeticks*,RFC1157VarBindList*);
18
19 HINSTANCE m_hSNMPAPI;
20 void (SNMP_FUNC_TYPE *m_snmpOIDFree)(AsnObjectIdentifier*);
21 LPVOID (SNMP_FUNC_TYPE *m_snmpAlloc)(UINT);
22 void (SNMP_FUNC_TYPE *m_snmpFree)(LPVOID);
23 void (SNMP_FUNC_TYPE *m_snmpVBLFree)(RFC1157VarBindList* vbl);
24 void InitSNMP() {
25 m_hSNMPAPI = ::LoadLibraryEx("SNMPAPI",NULL,0);
26 if(!m_hSNMPAPI)
27 return;
28 *(FARPROC*)&m_snmpOIDFree = ::GetProcAddress(m_hSNMPAPI,"SnmpUtilOidFree");
29 *(FARPROC*)&m_snmpAlloc = ::GetProcAddress(m_hSNMPAPI,"SnmpUtilMemAlloc");
30 *(FARPROC*)&m_snmpFree = ::GetProcAddress(m_hSNMPAPI,"SnmpUtilMemFree");
31 *(FARPROC*)&m_snmpVBLFree = ::GetProcAddress(m_hSNMPAPI,"SnmpUtilVarBindListFree");
32 if(
33 (m_snmpFree && !m_snmpAlloc) ||
34 (m_snmpAlloc && !m_snmpFree)
35 )
36 DeinitSNMP();
37 }
38 void DeinitSNMP() {
39 if(!m_hSNMPAPI)
40 return;
41 ::FreeLibrary(m_hSNMPAPI);
42 m_hSNMPAPI=NULL;
43 }
44 void SNMPFreeOID(AsnObjectIdentifier* oid) {
45 if(m_hSNMPAPI && m_snmpOIDFree)
46 (*m_snmpOIDFree)(oid);
47 else
48 ::GlobalFree((HGLOBAL)oid->ids);
49 }
50 LPVOID SNMPAlloc(UINT size) {
51 if(m_hSNMPAPI && m_snmpAlloc)
52 return (*m_snmpAlloc)(size);
53 else
54 return ::GlobalAlloc(GMEM_FIXED,size);
55 }
56 void SNMPFree(LPVOID addr) {
57 if(m_hSNMPAPI && m_snmpFree)
58 (*m_snmpFree)(addr);
59 else
60 :: GlobalFree((HGLOBAL)addr);
61 }
62 void SNMPFreeVBL(RFC1157VarBindList& vbl) {
63 if(m_hSNMPAPI && m_snmpVBLFree)
64 (*m_snmpVBLFree)(&vbl);
65 else{
66 for(UINT tmp=0;tmp<vbl.len;tmp++) {
67 SNMPFree(vbl.list[tmp].name.ids);
68 switch(vbl.list[tmp].value.asnType){
69 case ASN_OCTETSTRING:
70 case ASN_SEQUENCE:
71 case ASN_RFC1155_IPADDRESS:
72 case ASN_RFC1155_OPAQUE:
73 // case ASN_RFC1213_DISPSTRING:
74 if(vbl.list[tmp].value.asnValue.arbitrary.dynamic)
75 SNMPFree(vbl.list[tmp].value.asnValue.arbitrary.stream);
76 break;
77 case ASN_OBJECTIDENTIFIER:
78 SNMPFree(vbl.list[tmp].value.asnValue.object.ids);
79 break;
80 default:
81 break;
82 }
83 }
84 SNMPFree(vbl.list);
85 }
86 }
87
88 BOOL SNMPBuildVBL(RFC1157VarBindList& vbl,CSNMPVarBindList& in) {
89 vbl.len = in.GetCount();
90 vbl.list = (RFC1157VarBind*)SNMPAlloc(sizeof(RFC1157VarBind)*vbl.len);
91 POSITION p = in.GetHeadPosition();
92 UINT ptr = 0;
93 while(p){
94 CSNMPVarBind& vb = in.GetNext(p);
95 ASSERT(ptr<vbl.len);
96 SNMPBuildVB(vbl.list[ptr++],vb);
97 }
98 return TRUE;
99 }
100 BOOL SNMPBuildVB(RFC1157VarBind& vb,CSNMPVarBind& in) {
101 ASSERT(in.name.type==CASNAny::typeASNOID);
102 return SNMPBuildOID(vb.name,in.name.value.oid) && SNMPBuildAA(vb.value,in.value);
103 }
104 BOOL SNMPBuildAA(AsnAny& aa,CASNAny& in) {
105 aa.asnType=in.type;
106 switch(in.type) {
107 case CASNAny::typeASNInteger:
108 aa.asnValue.number=in.value.number; break;
109 case CASNAny::typeASNOctetString:
110 // case CASNAny::typeASNDispString:
111 SNMPBuildOS(aa.asnValue.string,in.value.string); break;
112 case CASNAny::typeASNNull:
113 break;
114 case CASNAny::typeASNOID:
115 SNMPBuildOID(aa.asnValue.object,in.value.oid); break;
116 case CASNAny::typeASNSequence:
117 // case CASNAny::typeASNSequenceOf:
118 SNMPBuildOS(aa.asnValue.sequence,in.value.sequence); break;
119 case CASNAny::typeASNIP:
120 SNMPBuildFLOS(aa.asnValue.address,(LPBYTE)&in.value.ip,sizeof(in.value.ip)); break;
121 case CASNAny::typeASNCounter:
122 aa.asnValue.counter = in.value.counter; break;
123 case CASNAny::typeASNGauge:
124 aa.asnValue.gauge = in.value.gauge; break;
125 case CASNAny::typeASNTicks:
126 aa.asnValue.ticks = in.value.ticks; break;
127 case CASNAny::typeASNOpaque:
128 ASSERT(in.storeType==CASNAny::storeDynamic);
129 SNMPBuildOS(aa.asnValue.arbitrary,in.value.data); break;
130 break;
131 default:
132 ASSERT(FALSE);
133 return FALSE;
134 }
135 return TRUE;
136 }
137 BOOL SNMPBuildOS(AsnOctetString& os,CASNAny::asnOctetString& in) {
138 return SNMPBuildFLOS(os,in.data,in.size);
139 }
140 BOOL SNMPBuildOID(AsnObjectIdentifier& oid,CASNAny::asnOID& in) {
141 oid.idLength = in.size/sizeof(UINT);
142 ASSERT(!(in.size%sizeof(UINT)));
143 VERIFY(oid.ids = (UINT*)SNMPAlloc(in.size));
144 memmove(oid.ids,in.data,in.size);
145 return TRUE;
146 }
147 BOOL SNMPBuildFLOS(AsnOctetString& os,LPBYTE data,UINT size) {
148 os.length = size;
149 VERIFY(os.stream = (BYTE*)SNMPAlloc(os.length));
150 os.dynamic=TRUE;
151 memmove(os.stream,data,size);
152 return TRUE;
153 }
154 BOOL SNMPParseVBL(RFC1157VarBindList& vbl,CSNMPVarBindList& ou) {
155 for(UINT tmp=0;tmp<vbl.len;tmp++){
156 CSNMPVarBind vb;
157 SNMPParseVB(vbl.list[tmp],vb);
158 ou.AddTail(vb);
159 }
160 return TRUE;
161 }
162 BOOL SNMPParseVB(RFC1157VarBind& vb,CSNMPVarBind& ou) {
163 ou.name.Set(CASNAny::typeASNOID,(LPBYTE)vb.name.ids,vb.name.idLength*sizeof(UINT));
164 return SNMPParseAA(vb.value,ou.value);
165 }
166 BOOL SNMPParseAA(AsnAny& aa,CASNAny& ou) {
167 switch(aa.asnType){
168 case ASN_INTEGER:
169 ou.Set(CASNAny::typeASNInteger,aa.asnValue.number); break;
170 case ASN_OCTETSTRING:
171 // case ASN_RFC1213_DISPSTRING:
172 ou.Set(CASNAny::typeASNOctetString,aa.asnValue.string.stream,aa.asnValue.string.length); break;
173 case ASN_OBJECTIDENTIFIER:
174 ou.Set(CASNAny::typeASNOID,(LPBYTE)aa.asnValue.object.ids,aa.asnValue.object.idLength);
175 SNMPParseOID(aa.asnValue.object,ou.value.oid); break;
176 case ASN_SEQUENCE:
177 ou.Set(CASNAny::typeASNSequence,aa.asnValue.sequence.stream,aa.asnValue.sequence.length); break;
178 case ASN_RFC1155_IPADDRESS:
179 SNMPParseIP(aa.asnValue.address,ou); break;
180 case ASN_RFC1155_COUNTER:
181 ou.Set(CASNAny::typeASNCounter,(LONG)aa.asnValue.counter); break;
182 case ASN_RFC1155_GAUGE:
183 ou.Set(CASNAny::typeASNGauge,(LONG)aa.asnValue.gauge); break;
184 case ASN_RFC1155_TIMETICKS:
185 ou.Set(CASNAny::typeASNTicks,(LONG)aa.asnValue.ticks); break;
186 case ASN_RFC1155_OPAQUE:
187 ou.Set(CASNAny::typeASNOpaque,aa.asnValue.arbitrary.stream,aa.asnValue.arbitrary.length); break;
188 case ASN_NULL:
189 ou.Free(); break;
190 default:
191 ASSERT(FALSE);
192 return FALSE;
193 }
194 return TRUE;
195 }
196 BOOL SNMPParseOID(AsnObjectIdentifier& oid,CASNAny::asnOID& ou) {
197 ASSERT(ou.size==(oid.idLength*sizeof(UINT)));
198 memmove(ou.data,oid.ids,ou.size);
199 return TRUE;
200 }
201 BOOL SNMPParseIP(AsnIPAddress& ip,CASNAny& ou) {
202 in_addr i;
203 if(ip.length>sizeof(i))
204 return FALSE;
205 i.s_addr=0;
206 memmove(&i,ip.stream,ip.length);
207 ou.Set(i);
208 return TRUE;
209 }
210
211
212 CSNMPExtDLL(LPCTSTR dllName) : m_hInstance(NULL) { InitSNMP(); Init(dllName); }
213 ~CSNMPExtDLL() { Deinit(); DeinitSNMP(); }
214
215 BOOL Init(LPCTSTR dllName) {
216 Deinit();
217 m_hInstance = ::LoadLibraryEx(dllName,NULL,0);
218 if(!m_hInstance)
219 return FALSE;
220 *(FARPROC*)&m_extInit = ::GetProcAddress(m_hInstance,"SnmpExtensionInit");
221 *(FARPROC*)&m_extQuery = ::GetProcAddress(m_hInstance,"SnmpExtensionQuery");
222 *(FARPROC*)&m_extTrap = ::GetProcAddress(m_hInstance,"SnmpExtensionTrap");
223 if(!(m_extInit && m_extQuery && m_extTrap)){
224 Deinit();
225 return FALSE;
226 }
227 if(!((*m_extInit)(GetCurrentTime(),&m_hEvent,&m_OID))){
228 Deinit();
229 return FALSE;
230 }
231 return TRUE;
232 }
233 void Deinit() {
234 if(!m_hInstance)
235 return;
236 ::FreeLibrary(m_hInstance);
237 }
238 virtual BOOL Request(BYTE type,CSNMPVarBindList& in,CSNMPVarBindList& ou) {
239 RFC1157VarBindList vbl;
240 SNMPBuildVBL(vbl,in);
241 AsnInteger errorStatus, errorIndex;
242 (*m_extQuery)(type,&vbl,&errorStatus,&errorIndex);
243 ou.RemoveAll();
244 SNMPParseVBL(vbl,ou);
245 SNMPFreeVBL(vbl);
246 return TRUE;
247 }
248};
249
250};
251
252 #endif// __SNMPEXTDLL_H