win-pvdrivers

view copyconfig/copyconfig.c @ 592:d1754b0e1ead

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