win-pvdrivers

view copyconfig/copyconfig.c @ 427:599061042271

updates to add copyconfig to the installer
author James Harper <james.harper@bendigoit.com.au>
date Mon Sep 08 22:10:30 2008 +1000 (2008-09-08)
parents 37ed25854efa
children dd42744a458e
line source
1 #include <basetyps.h>
2 #include <stdlib.h>
3 #include <wtypes.h>
4 #include <initguid.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <winioctl.h>
8 #include <ntddndis.h>
10 #define ADAPTER_TYPE_XEN
11 #define ADAPTER_TYPE_OTHER
13 typedef struct adapter_details
14 {
15 struct adapter_details *next;
16 BYTE mac_address[6];
17 CHAR xen_IpConfig_key_name[1024];
18 CHAR other_IpConfig_key_name[1024];
19 } adapter_details_t;
21 int __cdecl
22 main(
23 // __in ULONG argc,
24 // __in_ecount(argc) PCHAR argv[]
25 )
26 {
27 HKEY key_handle;
28 HKEY adapter_key_handle;
29 LONG status;
30 CHAR adapter_key_name[256];
31 BYTE buf[1024];
32 DWORD buf_len;
33 CHAR *keyptr;
34 HANDLE handle;
35 CHAR filename[256];
36 NDIS_STATISTICS_VALUE oid_req;
37 int i;
38 adapter_details_t *list_head = NULL, *prev;
39 adapter_details_t *adapter;
40 CHAR value_name[256];
41 DWORD value_name_len;
42 DWORD value_type;
43 CHAR value_data[1024];
44 DWORD value_data_len;
45 HKEY src_key_handle;
46 HKEY dst_key_handle;
48 // Enumerate keys in \\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters
49 status = RegOpenKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters", &key_handle);
50 if (status != ERROR_SUCCESS)
51 {
52 fprintf(stderr, "Cannot read registry - status = %08x\n", status);
53 return 1;
54 }
55 printf("Enumerating adapters\n");
56 buf_len = 39;
57 for (i = 0; (status = RegEnumKeyEx(key_handle, i, adapter_key_name, &buf_len, NULL, NULL, NULL, NULL)) != ERROR_NO_MORE_ITEMS; buf_len = 39, i++)
58 {
59 if (status == ERROR_MORE_DATA)
60 continue; /* if the key is longer than a GUID then we aren't interested in it anyway */
61 if (status != ERROR_SUCCESS)
62 break;
63 //printf("buf_len = %d, buf = %s\n", buf_len, buf);
64 if (buf_len != 38)
65 continue;
66 /* check that the name looks like a guid */
67 sprintf(filename, "\\\\.\\%s", adapter_key_name);
68 handle = CreateFile(filename, FILE_GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
69 if (!handle)
70 continue;
71 oid_req.Oid = OID_802_3_PERMANENT_ADDRESS;
72 oid_req.DataLength = 10;
73 status = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid_req, sizeof(NDIS_STATISTICS_VALUE), buf, 256, &buf_len, NULL);
74 if (!status || buf_len != 6)
75 continue;
76 if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 0 && buf[4] == 0 && buf[5] == 0)
77 continue;
78 printf("Found Adapter:\n MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
79 buf[0], buf[1], buf[2],
80 buf[3], buf[4], buf[5]);
81 for (adapter = list_head, prev = NULL; adapter != NULL; prev = adapter, adapter = adapter->next)
82 {
83 if (memcmp(adapter->mac_address, buf, 6) == 0)
84 break;
85 }
86 if (!adapter)
87 {
88 adapter = malloc(sizeof(adapter_details_t));
89 if (prev == NULL)
90 list_head = adapter;
91 else
92 prev->next = adapter;
93 adapter->next = NULL;
94 memcpy(adapter->mac_address, buf, 6);
95 adapter->xen_IpConfig_key_name[0] = 0;
96 adapter->other_IpConfig_key_name[0] = 0;
97 }
99 oid_req.Oid = OID_GEN_VENDOR_DESCRIPTION;
100 oid_req.DataLength = 256;
101 status = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid_req, sizeof(NDIS_STATISTICS_VALUE), buf, 256, &buf_len, NULL);
102 if (!status)
103 {
104 printf(" Error opening. Ignoring\n");
105 continue;
106 }
107 printf(" Description = %s\n", buf);
108 if (strstr(buf, "Xen"))
109 {
110 printf(" Type = Xen\n");
111 keyptr = adapter->xen_IpConfig_key_name;
112 }
113 else
114 {
115 printf(" Type = Other\n");
116 keyptr = adapter->other_IpConfig_key_name;
117 }
118 if (keyptr[0])
119 {
120 printf(" Multiple Other or multiple Xen adapters exist with the same mac. Ignoring.\n");
121 continue;
122 }
123 buf_len = 1024;
125 status = RegOpenKey(key_handle, adapter_key_name, &adapter_key_handle);
126 if (status != ERROR_SUCCESS)
127 {
128 printf(" Failed to read registry (%08x). Ignoring.\n", status);
129 continue;
130 }
131 status = RegQueryValueEx(adapter_key_handle, "IpConfig", NULL, NULL, keyptr, &buf_len);
132 if (status != ERROR_SUCCESS)
133 {
134 printf(" Failed to read registry (%08x). Ignoring.\n", status);
135 continue;
136 }
137 }
139 printf("\nCloning IP Configurations\n");
140 for (adapter = list_head; adapter != NULL; adapter = adapter->next)
141 {
142 printf("Considering MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
143 adapter->mac_address[0], adapter->mac_address[1], adapter->mac_address[2],
144 adapter->mac_address[3], adapter->mac_address[4], adapter->mac_address[5]);
145 if (adapter->xen_IpConfig_key_name[0])
146 printf(" Xen adapter present\n");
147 else
148 printf(" Xen adapter not present\n");
149 if (adapter->other_IpConfig_key_name[0])
150 printf(" Other adapter present\n");
151 else
152 printf(" Other adapter not present\n");
154 if (adapter->xen_IpConfig_key_name[0] && adapter->other_IpConfig_key_name[0])
155 {
156 // open HKLM\SYSTEM\Services\%s
157 sprintf(buf, "SYSTEM\\CurrentControlSet\\Services\\%s", adapter->xen_IpConfig_key_name);
158 status = RegOpenKey(HKEY_LOCAL_MACHINE, buf, &dst_key_handle);
159 if (status != ERROR_SUCCESS)
160 {
161 printf(" Cannot open Xen adapter config key. Skipping.\n");
162 continue;
163 }
164 sprintf(buf, "SYSTEM\\CurrentControlSet\\Services\\%s", adapter->other_IpConfig_key_name);
165 status = RegOpenKey(HKEY_LOCAL_MACHINE, buf, &src_key_handle);
166 if (status != ERROR_SUCCESS)
167 {
168 printf(" Cannot open Other adapter config key. Skipping.\n");
169 continue;
170 }
171 value_name_len = 256;
172 value_data_len = 1024;
173 while ((status = RegEnumValue(dst_key_handle, 0, value_name, &value_name_len, NULL, &value_type, value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
174 {
175 RegDeleteValue(dst_key_handle, value_name);
176 value_name_len = 256;
177 value_data_len = 1024;
178 }
179 i = 0;
180 value_name_len = 256;
181 value_data_len = 1024;
182 while ((status = RegEnumValue(src_key_handle, i, value_name, &value_name_len, NULL, &value_type, value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
183 {
184 RegSetValueEx(dst_key_handle, value_name, 0, value_type, value_data, value_data_len);
185 value_name_len = 256;
186 value_data_len = 1024;
187 i++;
188 }
189 printf(" Copied\n", buf);
190 }
191 }
193 // loop through key names that look like GUIDs
194 // get OID_GEN_VENDOR_DESCRIPTION
195 // if description does not contain "RTL8139" then continue to next key name
196 // get OID_802_3_PERMANENT_ADDRESS
197 // get IpConfig path value registry
198 // again loop through key names that look like GUIDs
199 // if description does not contain "xen" then continue to next key name
200 // get IpConfig path value from registry
201 // delete contents of above path
202 // copy contents of RTL8139 IpConfig to Xen IpConfig (use RegCopyTree)
203 }