~ubuntu-branches/ubuntu/feisty/basilisk2/feisty

« back to all changes in this revision

Viewing changes to src/Windows/util_windows.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2006-06-01 01:11:16 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060601011116-xjhegbgyfsxag5fl
Tags: 0.9.20060529-1
* New upstream CVS snapshot.
* Update local cdbs snippet copyright-check.mk:
  + Broaden scan to also look for "(c)" by default.
  + Make egrep options configurable.
  + Ignore auto-tools files.
* Bump up standards-version to 3.7.2 (no changes needed).
* Let dh_strip do the stripping (not the make install target).

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "util_windows.h"
25
25
#include "main.h"
26
26
 
 
27
#include <list>
 
28
using std::list;
 
29
 
 
30
#include <string>
 
31
using std::string;
 
32
 
27
33
BOOL exists( const char *path )
28
34
{
29
35
        HFILE h;
53
59
                        if(SetEndOfFile(h)) {
54
60
                                ok = true;
55
61
                                if(SetFilePointer( h, 0, NULL, FILE_BEGIN) != 0xFFFFFFFF) {
56
 
                                        DWORD written, zeroed_size = min(1024*1024,size);
 
62
                                        DWORD written;
 
63
                                        DWORD zeroed_size = size;
 
64
                                        if (zeroed_size > 1024*1024)
 
65
                                                zeroed_size = 1024*1024;
57
66
                                        char *b = (char *)malloc(zeroed_size);
58
67
                                        if(b) {
59
68
                                                memset( b, 0, zeroed_size );
135
144
 
136
145
        return true;
137
146
}
 
147
 
 
148
 
 
149
/*
 
150
 *  Network control panel helpers
 
151
 */
 
152
 
 
153
struct panel_reg {
 
154
        string name;
 
155
        string guid;
 
156
};
 
157
 
 
158
static list<panel_reg> network_registry;
 
159
typedef list<panel_reg>::const_iterator network_registry_iterator;
 
160
 
 
161
#define NETWORK_CONNECTIONS_KEY \
 
162
                "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
 
163
 
 
164
static void get_network_registry(void)
 
165
{
 
166
        LONG status;
 
167
        HKEY network_connections_key;
 
168
        DWORD len;
 
169
        int i = 0;
 
170
 
 
171
        if (network_registry.size() > 0)
 
172
                return;
 
173
 
 
174
        status = RegOpenKeyEx(
 
175
                HKEY_LOCAL_MACHINE,
 
176
                NETWORK_CONNECTIONS_KEY,
 
177
                0,
 
178
                KEY_READ,
 
179
                &network_connections_key);
 
180
 
 
181
        if (status != ERROR_SUCCESS)
 
182
                return;
 
183
 
 
184
        while (true) {
 
185
                char enum_name[256];
 
186
                char connection_string[256];
 
187
                HKEY connection_key;
 
188
                char name_data[256];
 
189
                DWORD name_type;
 
190
                const char name_string[] = "Name";
 
191
 
 
192
                len = sizeof (enum_name);
 
193
                status = RegEnumKeyEx(
 
194
                        network_connections_key,
 
195
                        i,
 
196
                        enum_name,
 
197
                        &len,
 
198
                        NULL,
 
199
                        NULL,
 
200
                        NULL,
 
201
                        NULL);
 
202
                if (status != ERROR_SUCCESS)
 
203
                        break;
 
204
 
 
205
                snprintf (connection_string, sizeof(connection_string),
 
206
                                  "%s\\%s\\Connection",
 
207
                                  NETWORK_CONNECTIONS_KEY, enum_name);
 
208
 
 
209
                status = RegOpenKeyEx(
 
210
                        HKEY_LOCAL_MACHINE,
 
211
                        connection_string,
 
212
                        0,
 
213
                        KEY_READ,
 
214
                        &connection_key);
 
215
 
 
216
                if (status == ERROR_SUCCESS) {
 
217
                        len = sizeof (name_data);
 
218
                        status = RegQueryValueEx(
 
219
                                connection_key,
 
220
                                name_string,
 
221
                                NULL,
 
222
                                &name_type,
 
223
                                (BYTE *)name_data,
 
224
                                &len);
 
225
 
 
226
                        if (status == ERROR_SUCCESS && name_type == REG_SZ) {
 
227
                                panel_reg pr;
 
228
                                pr.name = name_data;
 
229
                                pr.guid = enum_name;
 
230
                                network_registry.push_back(pr);
 
231
                        }
 
232
                        RegCloseKey (connection_key);
 
233
                }
 
234
                ++i;
 
235
    }
 
236
 
 
237
        RegCloseKey (network_connections_key);
 
238
}
 
239
 
 
240
const char *ether_name_to_guid(const char *name)
 
241
{
 
242
        get_network_registry();
 
243
 
 
244
        for (network_registry_iterator it = network_registry.begin(); it != network_registry.end(); it++) {
 
245
                if (strcmp((*it).name.c_str(), name) == 0)
 
246
                        return (*it).guid.c_str();
 
247
        }
 
248
 
 
249
        return NULL;
 
250
}
 
251
 
 
252
const char *ether_guid_to_name(const char *guid)
 
253
{
 
254
        get_network_registry();
 
255
 
 
256
        for (network_registry_iterator it = network_registry.begin(); it != network_registry.end(); it++) {
 
257
                if (strcmp((*it).guid.c_str(), guid) == 0)
 
258
                        return (*it).name.c_str();
 
259
        }
 
260
 
 
261
        return NULL;
 
262
}
 
263
 
 
264
 
 
265
/*
 
266
 *  Get TAP-Win32 adapters
 
267
 */
 
268
 
 
269
#define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
 
270
 
 
271
#define TAP_COMPONENT_ID "tap0801"
 
272
 
 
273
const char *ether_tap_devices(void)
 
274
{
 
275
        HKEY adapter_key;
 
276
        LONG status;
 
277
        DWORD len;
 
278
        int i = 0;
 
279
 
 
280
        status = RegOpenKeyEx(
 
281
                HKEY_LOCAL_MACHINE,
 
282
                ADAPTER_KEY,
 
283
                0,
 
284
                KEY_READ,
 
285
                &adapter_key);
 
286
 
 
287
        if (status != ERROR_SUCCESS)
 
288
                return NULL;
 
289
 
 
290
        list<string> devices;
 
291
 
 
292
        while (true) {
 
293
                char enum_name[256];
 
294
                char unit_string[256];
 
295
                HKEY unit_key;
 
296
                char component_id_string[] = "ComponentId";
 
297
                char component_id[256];
 
298
                char net_cfg_instance_id_string[] = "NetCfgInstanceId";
 
299
                char net_cfg_instance_id[256];
 
300
                DWORD data_type;
 
301
 
 
302
                len = sizeof (enum_name);
 
303
                status = RegEnumKeyEx(
 
304
                        adapter_key,
 
305
                        i,
 
306
                        enum_name,
 
307
                        &len,
 
308
                        NULL,
 
309
                        NULL,
 
310
                        NULL,
 
311
                        NULL);
 
312
                if (status != ERROR_SUCCESS)
 
313
                        break;
 
314
 
 
315
                snprintf (unit_string, sizeof(unit_string), "%s\\%s",
 
316
                                  ADAPTER_KEY, enum_name);
 
317
 
 
318
                status = RegOpenKeyEx(
 
319
                        HKEY_LOCAL_MACHINE,
 
320
                        unit_string,
 
321
                        0,
 
322
                        KEY_READ,
 
323
                        &unit_key);
 
324
 
 
325
                if (status == ERROR_SUCCESS) {
 
326
                        len = sizeof (component_id);
 
327
                        status = RegQueryValueEx(
 
328
                                unit_key,
 
329
                                component_id_string,
 
330
                                NULL,
 
331
                                &data_type,
 
332
                                (BYTE *)component_id,
 
333
                                &len);
 
334
 
 
335
                        if (status == ERROR_SUCCESS && data_type == REG_SZ) {
 
336
                                len = sizeof (net_cfg_instance_id);
 
337
                                status = RegQueryValueEx(
 
338
                                        unit_key,
 
339
                                        net_cfg_instance_id_string,
 
340
                                        NULL,
 
341
                                        &data_type,
 
342
                                        (BYTE *)net_cfg_instance_id,
 
343
                                        &len);
 
344
 
 
345
                                if (status == ERROR_SUCCESS && data_type == REG_SZ) {
 
346
                                        if (!strcmp (component_id, TAP_COMPONENT_ID))
 
347
                                                devices.push_back(net_cfg_instance_id);
 
348
                                }
 
349
                        }
 
350
                        RegCloseKey (unit_key);
 
351
                }
 
352
                ++i;
 
353
    }
 
354
 
 
355
        RegCloseKey (adapter_key);
 
356
 
 
357
        if (devices.empty())
 
358
                return NULL;
 
359
 
 
360
        // The result is a '\0' separated list of strings
 
361
        list<string>::const_iterator it;
 
362
        len = 0;
 
363
        for (it = devices.begin(); it != devices.end(); it++)
 
364
                len += (*it).length() + 1;
 
365
 
 
366
        char *names = (char *)malloc(len);
 
367
        if (names) {
 
368
                char *p = names;
 
369
                for (it = devices.begin(); it != devices.end(); it++) {
 
370
                        len = (*it).length();
 
371
                        strcpy(p, (*it).c_str());
 
372
                        p[len] = '\0';
 
373
                        p += len + 1;
 
374
                }
 
375
        }
 
376
 
 
377
        return names;
 
378
}