1 | /* $Id: iphlpapi.cpp,v 1.5 2001-10-29 13:37:29 phaller Exp $ */
|
---|
2 | /*
|
---|
3 | * IPHLPAPI library
|
---|
4 | *
|
---|
5 | */
|
---|
6 |
|
---|
7 |
|
---|
8 | /****************************************************************************
|
---|
9 | * includes
|
---|
10 | ****************************************************************************/
|
---|
11 |
|
---|
12 | #include <odin.h>
|
---|
13 | #include <odinwrap.h>
|
---|
14 | #include <os2sel.h>
|
---|
15 |
|
---|
16 | #include <os2win.h>
|
---|
17 | #include <winversion.h>
|
---|
18 |
|
---|
19 | #include <string.h>
|
---|
20 |
|
---|
21 | #include "iphlpapi.h"
|
---|
22 |
|
---|
23 | ODINDEBUGCHANNEL(IPHLPAPI-IPHLPAPI)
|
---|
24 |
|
---|
25 |
|
---|
26 | /****************************************************************************
|
---|
27 | * module global variables
|
---|
28 | ****************************************************************************/
|
---|
29 |
|
---|
30 | static PIP_ADAPTER_INFO pipAdapters = NULL;
|
---|
31 |
|
---|
32 |
|
---|
33 | //******************************************************************************
|
---|
34 | //******************************************************************************
|
---|
35 | static void i_initializeAdapterInformation(void)
|
---|
36 | {
|
---|
37 | // @@@PH
|
---|
38 | // yet a fake to test some app
|
---|
39 | pipAdapters = (PIP_ADAPTER_INFO)malloc (sizeof (IP_ADAPTER_INFO) );
|
---|
40 |
|
---|
41 | memset(pipAdapters, 0, sizeof( IP_ADAPTER_INFO ));
|
---|
42 | pipAdapters->Next = NULL;
|
---|
43 | pipAdapters->ComboIndex = 1;
|
---|
44 | strcpy(pipAdapters->AdapterName, "ODIN IPHLPAPI Test Adapter");
|
---|
45 | strcpy(pipAdapters->Description, "ODIN IPHLPAPI Test Adapter (faked information)");
|
---|
46 | pipAdapters->AddressLength = 6; // MAX address
|
---|
47 | pipAdapters->Address[0] = 'V';
|
---|
48 | pipAdapters->Address[1] = 'P';
|
---|
49 | pipAdapters->Address[2] = 'C';
|
---|
50 | pipAdapters->Address[3] = 'O';
|
---|
51 | pipAdapters->Address[4] = 'S';
|
---|
52 | pipAdapters->Address[5] = '2';
|
---|
53 | pipAdapters->Index = 16777218;
|
---|
54 | pipAdapters->Type = 6;
|
---|
55 | pipAdapters->DhcpEnabled = 0;
|
---|
56 |
|
---|
57 | static IP_ADDR_STRING iasLocalhost;
|
---|
58 | iasLocalhost.Next = NULL;
|
---|
59 | strcpy((char*)&iasLocalhost.IpAddress,"127.0.0.1");
|
---|
60 | strcpy((char*)&iasLocalhost.IpMask, "255.0.0.0");
|
---|
61 | iasLocalhost.Context = 0;
|
---|
62 |
|
---|
63 | static IP_ADDR_STRING iasGateway;
|
---|
64 | iasGateway.Next = NULL;
|
---|
65 | strcpy((char*)&iasGateway.IpAddress,"192.168.1.1");
|
---|
66 | strcpy((char*)&iasGateway.IpMask, "255.255.255.0");
|
---|
67 | iasGateway.Context = 0;
|
---|
68 |
|
---|
69 |
|
---|
70 | memcpy((char*)&pipAdapters->IpAddressList, (char*)&iasLocalhost, sizeof(iasLocalhost));
|
---|
71 | pipAdapters->CurrentIpAddress = &pipAdapters->IpAddressList;
|
---|
72 | memcpy((char*)&pipAdapters->GatewayList, (char*)&iasGateway, sizeof(iasGateway));
|
---|
73 | memset((char*)&pipAdapters->DhcpServer, 0, sizeof( IP_ADDR_STRING ) );
|
---|
74 | pipAdapters->HaveWins = 0;
|
---|
75 | memset((char*)&pipAdapters->PrimaryWinsServer, 0, sizeof( IP_ADDR_STRING ) );
|
---|
76 | memset((char*)&pipAdapters->SecondaryWinsServer, 0, sizeof( IP_ADDR_STRING ) );
|
---|
77 | pipAdapters->LeaseObtained = 0;
|
---|
78 | pipAdapters->LeaseExpires = 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 | // copy over the whole list and advance the target pointer
|
---|
82 | static void i_copyIP_ADDRESS_STRING(PBYTE *ppTarget, PIP_ADDR_STRING pias)
|
---|
83 | {
|
---|
84 | while (pias)
|
---|
85 | {
|
---|
86 | memcpy(*ppTarget, pias, sizeof( IP_ADDR_STRING ) );
|
---|
87 | *ppTarget += sizeof ( IP_ADDR_STRING );
|
---|
88 | pias = pias->Next;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | static DWORD i_sizeOfIP_ADAPTER_INFO(PIP_ADAPTER_INFO piai)
|
---|
93 | {
|
---|
94 | PIP_ADDR_STRING pias;
|
---|
95 |
|
---|
96 | // check for sufficient space
|
---|
97 | DWORD dwRequired = sizeof( IP_ADAPTER_INFO );
|
---|
98 |
|
---|
99 | // follow the IP_ADDR_STRING lists
|
---|
100 | pias = &piai->IpAddressList;
|
---|
101 | while( pias )
|
---|
102 | {
|
---|
103 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
104 | pias = pias->Next;
|
---|
105 | }
|
---|
106 |
|
---|
107 | pias = &piai->GatewayList;
|
---|
108 | while( pias )
|
---|
109 | {
|
---|
110 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
111 | pias = pias->Next;
|
---|
112 | }
|
---|
113 |
|
---|
114 | pias = &piai->DhcpServer;
|
---|
115 | while( pias )
|
---|
116 | {
|
---|
117 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
118 | pias = pias->Next;
|
---|
119 | }
|
---|
120 |
|
---|
121 | pias = &piai->PrimaryWinsServer;
|
---|
122 | while( pias )
|
---|
123 | {
|
---|
124 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
125 | pias = pias->Next;
|
---|
126 | }
|
---|
127 |
|
---|
128 | pias = &piai->SecondaryWinsServer;
|
---|
129 | while( pias )
|
---|
130 | {
|
---|
131 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
132 | pias = pias->Next;
|
---|
133 | }
|
---|
134 |
|
---|
135 | return dwRequired;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | //******************************************************************************
|
---|
140 | //******************************************************************************
|
---|
141 |
|
---|
142 | // Note: returns error 50 under NT4 (NOT_SUPPORTED)
|
---|
143 | // so we better check out alternative ways, too.
|
---|
144 |
|
---|
145 | ODINFUNCTION2(DWORD, GetAdaptersInfo,
|
---|
146 | PIP_ADAPTER_INFO, pAdapterInfo,
|
---|
147 | PULONG, pOutBufLen)
|
---|
148 | {
|
---|
149 | dprintf(("GetAdaptersInfo incorrectly implemented"));
|
---|
150 |
|
---|
151 | if (NULL == pOutBufLen)
|
---|
152 | return ERROR_INVALID_PARAMETER;
|
---|
153 |
|
---|
154 | // verify first block of memory to write to
|
---|
155 | if (IsBadWritePtr(pAdapterInfo, 4))
|
---|
156 | return ERROR_INVALID_PARAMETER;
|
---|
157 |
|
---|
158 | if (NULL == pipAdapters)
|
---|
159 | {
|
---|
160 | // gather the information and save it
|
---|
161 | i_initializeAdapterInformation();
|
---|
162 |
|
---|
163 | // determine number of IP adapters (interfaces) in the system
|
---|
164 |
|
---|
165 | // os2_ioctl() SIOSTATIF42
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (NULL == pipAdapters)
|
---|
169 | return ERROR_NO_DATA;
|
---|
170 |
|
---|
171 | // OK, just copy over the information as far as possible
|
---|
172 | LONG lSpaceLeft = *pOutBufLen;
|
---|
173 | PBYTE pTarget = (PBYTE)pAdapterInfo;
|
---|
174 | PIP_ADAPTER_INFO pip;
|
---|
175 |
|
---|
176 | // calculate overall required buffer size
|
---|
177 | DWORD dwRequiredBuffer = 0;
|
---|
178 |
|
---|
179 | for( pip = pipAdapters ; pip ; pip = pip->Next )
|
---|
180 | {
|
---|
181 | // check for sufficient space
|
---|
182 | dwRequiredBuffer += i_sizeOfIP_ADAPTER_INFO(pip);
|
---|
183 | }
|
---|
184 |
|
---|
185 | for( pip = pipAdapters ; pip ; pip = pip->Next )
|
---|
186 | {
|
---|
187 | // check for sufficient space
|
---|
188 | DWORD dwRequired = i_sizeOfIP_ADAPTER_INFO(pip);
|
---|
189 |
|
---|
190 | if (lSpaceLeft - dwRequired > 0)
|
---|
191 | {
|
---|
192 | lSpaceLeft -= dwRequired;
|
---|
193 |
|
---|
194 | // copy over the whole structure hierarchy
|
---|
195 | memcpy(pTarget, pip, sizeof( IP_ADAPTER_INFO ));
|
---|
196 | pTarget += sizeof( IP_ADAPTER_INFO );
|
---|
197 |
|
---|
198 | // @@@PH shall point to somewhere within the current buffer
|
---|
199 | pip->CurrentIpAddress = (PIP_ADDR_STRING)pTarget;
|
---|
200 |
|
---|
201 | i_copyIP_ADDRESS_STRING(&pTarget, &pip->IpAddressList);
|
---|
202 | i_copyIP_ADDRESS_STRING(&pTarget, &pip->GatewayList);
|
---|
203 | i_copyIP_ADDRESS_STRING(&pTarget, &pip->DhcpServer);
|
---|
204 | i_copyIP_ADDRESS_STRING(&pTarget, &pip->PrimaryWinsServer);
|
---|
205 | i_copyIP_ADDRESS_STRING(&pTarget, &pip->SecondaryWinsServer);
|
---|
206 | }
|
---|
207 | else
|
---|
208 | {
|
---|
209 | // return overall size of required buffer
|
---|
210 | *pOutBufLen = dwRequiredBuffer;
|
---|
211 | return ERROR_BUFFER_OVERFLOW;
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | return ERROR_SUCCESS;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | //******************************************************************************
|
---|
220 | //******************************************************************************
|
---|
221 | ODINFUNCTION2(DWORD, GetNetworkParams,
|
---|
222 | PFIXED_INFO, pFixedInfo,
|
---|
223 | PULONG, pOutBufLen)
|
---|
224 | {
|
---|
225 | dprintf(("GetNetworkParams not implemented"));
|
---|
226 | return ERROR_NOT_SUPPORTED; //NT returns this
|
---|
227 | }
|
---|
228 | //******************************************************************************
|
---|
229 | //******************************************************************************
|
---|
230 |
|
---|
231 |
|
---|
232 | DWORD AddIPAddress(IPAddr Address, // IP address to add
|
---|
233 | IPMask IpMask, // subnet mask for IP address
|
---|
234 | DWORD IfIndex, // index of adapter
|
---|
235 | PULONG NTEContext, // Net Table Entry context
|
---|
236 | PULONG NTEInstance // Net Table Entry Instance
|
---|
237 | );
|
---|
238 | // SIOCAIFADDR
|
---|
239 |
|
---|
240 | DWORD DeleteIPAddress(
|
---|
241 | ULONG NTEContext // net table entry context
|
---|
242 | );
|
---|
243 | // SIOCDIFADDR
|
---|