ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Windows/router/mib/mibaccess.cpp
Revision: 1.1
Committed: 2004-12-05T16:48:36Z (19 years, 11 months ago) by gbeauche
Branch: MAIN
Log Message:
import NAT-Router code from original Basilisk II for Windows

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * MibAccess.cpp
3     *
4     * The original code by Stas Khirman modified by Lauri Pesonen, December, 2000:
5     *
6     * SnmpUtilVarBindFree(), SnmpUtilOidNCmp() and SnmpUtilOidCpy() now loaded from
7     * "snmpapi.dll" dynamically instead of linking statically.
8     *
9     * MibII ctor now takes a parameter whether to load Winsock or not.
10     * WSAStartup maintains an internal reference counter so it would have been ok
11     * to let it load always.
12     *
13     * Fixed a bug where the return value of LoadLibrary() was compared against
14     * HINSTANCE_ERROR instead of NULL.
15     *
16     * Removed some type conversion warnings by casting.
17     *
18     * Added a check in MibExtLoad ctor that the function entry points were found.
19     *
20     * Added a check in GetIPMask() and GetIPAddress() that the library was loaded
21     * before accessing the functions.
22     *
23     * Changed the return type of GetIPAddress() and GetIPMask() from BOOL to void
24     * as they always returned TRUE.
25     *
26     */
27    
28     /************************************************************************/
29     /* Copyright (C) Stas Khirman 1998. All rights reserved. */
30     /* Written by Stas Khirman (staskh@rocketmail.com). */
31     /* and */
32     /* Raz Galili (razgalili@hotmail.com) */
33     /* */
34     /* Free software: no warranty; use anywhere is ok; spread the */
35     /* sources; note any modifications; share variations and */
36     /* derivatives (including sending to staskh@rocketmail.com). */
37     /* */
38     /************************************************************************/
39    
40     /*
41     * MibAccess.cpp - ip router
42     *
43     * Basilisk II (C) 1997-2001 Christian Bauer
44     *
45     * Windows platform specific code copyright (C) Lauri Pesonen
46     *
47     * This program is free software; you can redistribute it and/or modify
48     * it under the terms of the GNU General Public License as published by
49     * the Free Software Foundation; either version 2 of the License, or
50     * (at your option) any later version.
51     *
52     * This program is distributed in the hope that it will be useful,
53     * but WITHOUT ANY WARRANTY; without even the implied warranty of
54     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55     * GNU General Public License for more details.
56     *
57     * You should have received a copy of the GNU General Public License
58     * along with this program; if not, write to the Free Software
59     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
60     */
61    
62    
63     #include "sysdeps.h"
64     #include "mibaccess.h"
65     #include "..\dynsockets.h"
66     #include "..\dump.h"
67    
68     #if DEBUG
69     #pragma optimize("",off)
70     #endif
71    
72     #include "debug.h"
73    
74     MibExtLoad::MibExtLoad( LPSTR MibDllName, LPSTR SnmpDllName )
75     {
76    
77     m_Init = NULL;
78    
79     m_InitEx = NULL;
80     m_Query = NULL;
81     m_Trap = NULL;
82    
83     m_hInst_snmputil = NULL;
84    
85     m_SnmpUtilVarBindFree = NULL;
86     m_SnmpUtilOidNCmp = NULL;
87     m_SnmpUtilOidCpy = NULL;
88    
89     m_hInst = LoadLibrary( MibDllName );
90     if(!m_hInst) {
91     D(bug("MIB: library %s could not be loaded.\r\n", MibDllName));
92     return;
93     }
94     D(bug("MIB: library %s loaded ok.\r\n", MibDllName));
95    
96     m_Init = (pSnmpExtensionInit)GetProcAddress(m_hInst ,"SnmpExtensionInit");
97     m_InitEx= (pSnmpExtensionInitEx)GetProcAddress(m_hInst ,"SnmpExtensionInitEx");
98     m_Query = (pSnmpExtensionQuery)GetProcAddress(m_hInst ,"SnmpExtensionQuery");
99     m_Trap = (pSnmpExtensionTrap)GetProcAddress(m_hInst ,"SnmpExtensionTrap");
100    
101     if( !m_Init || !m_InitEx || !m_Query || !m_Trap )
102     {
103     D(bug("MIB: required entry points not found in library %s.\r\n", MibDllName));
104     FreeLibrary( m_hInst );
105     m_hInst = NULL;
106     }
107    
108     m_hInst_snmputil = LoadLibrary( SnmpDllName );
109     if(!m_hInst_snmputil){
110     D(bug("MIB: library %s could not be loaded.\r\n", SnmpDllName));
111     FreeLibrary( m_hInst );
112     m_hInst = NULL;
113     return;
114     }
115     D(bug("MIB: library %s loaded ok.\r\n", SnmpDllName));
116    
117     m_SnmpUtilVarBindFree = (VOID (SNMP_FUNC_TYPE *)(SnmpVarBind *))GetProcAddress( m_hInst_snmputil, "SnmpUtilVarBindFree" );
118     m_SnmpUtilOidNCmp = (SNMPAPI (SNMP_FUNC_TYPE *)(AsnObjectIdentifier *, AsnObjectIdentifier *, UINT))GetProcAddress( m_hInst_snmputil, "SnmpUtilOidNCmp" );
119     m_SnmpUtilOidCpy = (SNMPAPI (SNMP_FUNC_TYPE *)(AsnObjectIdentifier *, AsnObjectIdentifier *))GetProcAddress( m_hInst_snmputil, "SnmpUtilOidCpy" );
120    
121     if( !m_SnmpUtilVarBindFree || !m_SnmpUtilOidNCmp || !m_SnmpUtilOidCpy )
122     {
123     D(bug("MIB: required entry points not found in library %s.\r\n", SnmpDllName));
124     FreeLibrary( m_hInst );
125     FreeLibrary( m_hInst_snmputil );
126     m_hInst = NULL;
127     m_hInst_snmputil = NULL;
128     }
129    
130     #undef SNMP_FreeVarBind
131     #undef SNMP_oidncmp
132     #undef SNMP_oidcpy
133    
134     #define SNMP_FreeVarBind m_SnmpUtilVarBindFree
135     #define SNMP_oidncmp m_SnmpUtilOidNCmp
136     #define SNMP_oidcpy m_SnmpUtilOidCpy
137     }
138    
139     MibExtLoad::~MibExtLoad()
140     {
141     if( m_hInst ) {
142     FreeLibrary( m_hInst );
143     m_hInst = NULL;
144     }
145     if( m_hInst_snmputil ) {
146     FreeLibrary( m_hInst_snmputil );
147     m_hInst_snmputil = NULL;
148     }
149     }
150    
151     BOOL MibExtLoad::Init(DWORD dwTimeZeroReference,HANDLE *hPollForTrapEvent,AsnObjectIdentifier *supportedView)
152     {
153     if(m_hInst && m_Init)
154     return m_Init(dwTimeZeroReference,hPollForTrapEvent,supportedView);
155     return FALSE;
156     }
157     BOOL MibExtLoad::InitEx(AsnObjectIdentifier *supportedView)
158     {
159     if(m_hInst && m_InitEx)
160     return m_InitEx(supportedView);
161    
162     return FALSE;
163     }
164    
165     BOOL MibExtLoad::Query(BYTE requestType,OUT RFC1157VarBindList *variableBindings,
166     AsnInteger *errorStatus,AsnInteger *errorIndex)
167     {
168     if(m_hInst && m_Query)
169     return m_Query(requestType,variableBindings,errorStatus,errorIndex);
170    
171     return FALSE;
172     }
173    
174     BOOL MibExtLoad::Trap(AsnObjectIdentifier *enterprise, AsnInteger *genericTrap,
175     AsnInteger *specificTrap, AsnTimeticks *timeStamp,
176     RFC1157VarBindList *variableBindings)
177     {
178     if(m_hInst && m_Trap)
179     return m_Trap(enterprise, genericTrap,specificTrap, timeStamp, variableBindings);
180    
181     return FALSE;
182     }
183    
184     MibII::MibII( bool load_winsock ):MibExtLoad("inetmib1.dll","snmpapi.dll")
185     {
186     WSADATA wsa;
187     m_load_winsock = load_winsock;
188     if(load_winsock) {
189     int err = _WSAStartup( 0x0101, &wsa );
190     }
191     }
192    
193     MibII::~MibII()
194     {
195     if(m_load_winsock) _WSACleanup();
196     }
197    
198     BOOL MibII::Init()
199     {
200     HANDLE PollForTrapEvent;
201     AsnObjectIdentifier SupportedView;
202    
203     return MibExtLoad::Init(GetTickCount(),&PollForTrapEvent,&SupportedView);
204    
205     }
206    
207    
208     void MibII::GetIPAddress( UINT IpArray[], UINT &IpArraySize )
209     {
210     if(!m_hInst) {
211     IpArraySize = 0;
212     return;
213     }
214    
215     UINT OID_ipAdEntAddr[] = { 1, 3, 6, 1, 2, 1, 4 , 20, 1 ,1 };
216     AsnObjectIdentifier MIB_ipAdEntAddr = { sizeof(OID_ipAdEntAddr)/sizeof(UINT), OID_ipAdEntAddr };
217     RFC1157VarBindList varBindList;
218     RFC1157VarBind varBind[1];
219     AsnInteger errorStatus;
220     AsnInteger errorIndex;
221     AsnObjectIdentifier MIB_NULL = {0,0};
222     BOOL Exit;
223     int ret;
224     int IpCount=0;
225     DWORD dtmp;
226    
227     varBindList.list = varBind;
228     varBindList.len = 1;
229     varBind[0].name = MIB_NULL;
230     SNMP_oidcpy(&varBind[0].name,&MIB_ipAdEntAddr);
231     Exit = FALSE;
232    
233     IpCount=0;
234     while(!Exit){
235     ret = Query(ASN_RFC1157_GETNEXTREQUEST,&varBindList,&errorStatus,&errorIndex);
236    
237     if(!ret)
238     Exit=TRUE;
239     else{
240     ret = SNMP_oidncmp(&varBind[0].name,&MIB_ipAdEntAddr,MIB_ipAdEntAddr.idLength);
241     if(ret!=0){
242     Exit=TRUE;
243     }
244     else{
245     dtmp = *((DWORD *)varBind[0].value.asnValue.address.stream);
246     IpArray[IpCount] = dtmp;
247     IpCount++;
248     if(IpCount>=(int)IpArraySize)
249     Exit = TRUE;
250     }
251     }
252     }
253    
254     IpArraySize = IpCount;
255    
256     SNMP_FreeVarBind(&varBind[0]);
257     }
258    
259     void MibII::GetIPMask( UINT IpArray[], UINT &IpArraySize )
260     {
261     if(!m_hInst) {
262     IpArraySize = 0;
263     return;
264     }
265    
266     UINT OID_ipAdEntMask[] = { 1, 3, 6, 1, 2, 1, 4 , 20, 1 ,3 };
267     AsnObjectIdentifier MIB_ipAdEntMask = { sizeof(OID_ipAdEntMask)/sizeof(UINT), OID_ipAdEntMask };
268     RFC1157VarBindList varBindList;
269     RFC1157VarBind varBind[1];
270     AsnInteger errorStatus;
271     AsnInteger errorIndex;
272     AsnObjectIdentifier MIB_NULL = {0,0};
273     BOOL Exit;
274     int ret;
275     int IpCount=0;
276     DWORD dtmp;
277    
278     varBindList.list = varBind;
279     varBindList.len = 1;
280     varBind[0].name = MIB_NULL;
281     SNMP_oidcpy(&varBind[0].name,&MIB_ipAdEntMask);
282     Exit = FALSE;
283    
284     IpCount=0;
285     while(!Exit){
286     ret = Query(ASN_RFC1157_GETNEXTREQUEST,&varBindList,&errorStatus,&errorIndex);
287    
288     if(!ret)
289     Exit=TRUE;
290     else{
291     ret = SNMP_oidncmp(&varBind[0].name,&MIB_ipAdEntMask,MIB_ipAdEntMask.idLength);
292     if(ret!=0){
293     Exit=TRUE;
294     }
295     else{
296     dtmp = *((DWORD *)varBind[0].value.asnValue.address.stream);
297     IpArray[IpCount] = dtmp;
298     IpCount++;
299     if(IpCount>=(int)IpArraySize)
300     Exit = TRUE;
301     }
302     }
303     }
304    
305     IpArraySize = IpCount;
306    
307     SNMP_FreeVarBind(&varBind[0]);
308     }