win-pvdrivers

annotate copyconfig/copyconfig.c @ 424:37ed25854efa

Added 'copyconfig' utility to copy rtl8139 ip configuration to xen driver
author James Harper <james.harper@bendigoit.com.au>
date Thu Sep 04 22:26:18 2008 +1000 (2008-09-04)
parents
children 599061042271
rev   line source
james@424 1 #include <basetyps.h>
james@424 2 #include <stdlib.h>
james@424 3 #include <wtypes.h>
james@424 4 #include <initguid.h>
james@424 5 #include <stdio.h>
james@424 6 #include <string.h>
james@424 7 #include <winioctl.h>
james@424 8 #include <ntddndis.h>
james@424 9
james@424 10 /*
james@424 11 1. Get an adapter id. You can get it from registry,
james@424 12 \\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters
james@424 13 - they are specified in this key.
james@424 14
james@424 15 2. For each adapter id you have to open it using ZwCreateFile to obtain it's
james@424 16 handle to make ioctl's on,
james@424 17
james@424 18 status = ZwOpenFile(&adapter, GENERIC_READ|GENERIC_WRITE, &oa, &ioStatus,
james@424 19 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_NON_DIRECTORY_FILE);
james@424 20
james@424 21 Don't forget to prepend \\??\\ at the begning of the adapter id string.
james@424 22
james@424 23 3. Once you obtained valid apadter handle, you can query
james@424 24 OID_802_3_PERMANENT_ADDRESS:
james@424 25
james@424 26 args = OID_802_3_PERMANENT_ADDRESS;
james@424 27 status = ZwDeviceIoControlFile(adapter, 0, 0, 0, &ioStatus,
james@424 28 IOCTL_NDIS_QUERY_GLOBAL_STATS, &args, sizeof(ULONG), controlbuff,
james@424 29 sizeofcontrolbuf);
james@424 30
james@424 31 Make sure, that you can obtain adapter mac address while the device is up.
james@424 32 If it is down - imagine wi-fi apadter on notebook when it is switched off -
james@424 33 you're out of luck.
james@424 34 */
james@424 35
james@424 36 #define ADAPTER_TYPE_XEN
james@424 37 #define ADAPTER_TYPE_OTHER
james@424 38
james@424 39 typedef struct adapter_details
james@424 40 {
james@424 41 struct adapter_details *next;
james@424 42 BYTE mac_address[6];
james@424 43 CHAR xen_IpConfig_key_name[1024];
james@424 44 CHAR other_IpConfig_key_name[1024];
james@424 45 } adapter_details_t;
james@424 46
james@424 47 int __cdecl
james@424 48 main(
james@424 49 // __in ULONG argc,
james@424 50 // __in_ecount(argc) PCHAR argv[]
james@424 51 )
james@424 52 {
james@424 53 HKEY key_handle;
james@424 54 HKEY adapter_key_handle;
james@424 55 LONG status;
james@424 56 CHAR adapter_key_name[256];
james@424 57 BYTE buf[1024];
james@424 58 DWORD buf_len;
james@424 59 CHAR *keyptr;
james@424 60 HANDLE handle;
james@424 61 CHAR filename[256];
james@424 62 NDIS_STATISTICS_VALUE oid_req;
james@424 63 int i;
james@424 64 adapter_details_t *list_head = NULL, *prev;
james@424 65 adapter_details_t *adapter;
james@424 66 CHAR value_name[256];
james@424 67 DWORD value_name_len;
james@424 68 DWORD value_type;
james@424 69 CHAR value_data[1024];
james@424 70 DWORD value_data_len;
james@424 71 HKEY src_key_handle;
james@424 72 HKEY dst_key_handle;
james@424 73
james@424 74 // Enumerate keys in \\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters
james@424 75 status = RegOpenKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Adapters", &key_handle);
james@424 76 if (status != ERROR_SUCCESS)
james@424 77 {
james@424 78 fprintf(stderr, "Cannot read registry - status = %08x\n", status);
james@424 79 return 1;
james@424 80 }
james@424 81 printf("Enumerating adapters\n");
james@424 82 buf_len = 39;
james@424 83 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++)
james@424 84 {
james@424 85 if (status == ERROR_MORE_DATA)
james@424 86 continue; /* if the key is longer than a GUID then we aren't interested in it anyway */
james@424 87 if (status != ERROR_SUCCESS)
james@424 88 break;
james@424 89 //printf("buf_len = %d, buf = %s\n", buf_len, buf);
james@424 90 if (buf_len != 38)
james@424 91 continue;
james@424 92 /* check that the name looks like a guid */
james@424 93 sprintf(filename, "\\\\.\\%s", adapter_key_name);
james@424 94 handle = CreateFile(filename, FILE_GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
james@424 95 if (!handle)
james@424 96 continue;
james@424 97 oid_req.Oid = OID_802_3_PERMANENT_ADDRESS;
james@424 98 oid_req.DataLength = 10;
james@424 99 status = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid_req, sizeof(NDIS_STATISTICS_VALUE), buf, 256, &buf_len, NULL);
james@424 100 if (!status || buf_len != 6)
james@424 101 continue;
james@424 102 if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 0 && buf[4] == 0 && buf[5] == 0)
james@424 103 continue;
james@424 104 printf("Found Adapter:\n MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
james@424 105 buf[0], buf[1], buf[2],
james@424 106 buf[3], buf[4], buf[5]);
james@424 107 for (adapter = list_head, prev = NULL; adapter != NULL; prev = adapter, adapter = adapter->next)
james@424 108 {
james@424 109 if (memcmp(adapter->mac_address, buf, 6) == 0)
james@424 110 break;
james@424 111 }
james@424 112 if (!adapter)
james@424 113 {
james@424 114 adapter = malloc(sizeof(adapter_details_t));
james@424 115 if (prev == NULL)
james@424 116 list_head = adapter;
james@424 117 else
james@424 118 prev->next = adapter;
james@424 119 adapter->next = NULL;
james@424 120 memcpy(adapter->mac_address, buf, 6);
james@424 121 adapter->xen_IpConfig_key_name[0] = 0;
james@424 122 adapter->other_IpConfig_key_name[0] = 0;
james@424 123 }
james@424 124
james@424 125 oid_req.Oid = OID_GEN_VENDOR_DESCRIPTION;
james@424 126 oid_req.DataLength = 256;
james@424 127 status = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid_req, sizeof(NDIS_STATISTICS_VALUE), buf, 256, &buf_len, NULL);
james@424 128 if (!status)
james@424 129 {
james@424 130 printf(" Error opening. Ignoring\n");
james@424 131 continue;
james@424 132 }
james@424 133 printf(" Description = %s\n", buf);
james@424 134 if (strstr(buf, "Xen"))
james@424 135 {
james@424 136 printf(" Type = Xen\n");
james@424 137 keyptr = adapter->xen_IpConfig_key_name;
james@424 138 }
james@424 139 else
james@424 140 {
james@424 141 printf(" Type = Other\n");
james@424 142 keyptr = adapter->other_IpConfig_key_name;
james@424 143 }
james@424 144 if (keyptr[0])
james@424 145 {
james@424 146 printf(" Multiple Other or multiple Xen adapters exist with the same mac. Ignoring.\n");
james@424 147 continue;
james@424 148 }
james@424 149 buf_len = 1024;
james@424 150
james@424 151 status = RegOpenKey(key_handle, adapter_key_name, &adapter_key_handle);
james@424 152 if (status != ERROR_SUCCESS)
james@424 153 {
james@424 154 printf(" Failed to read registry (%08x). Ignoring.\n", status);
james@424 155 continue;
james@424 156 }
james@424 157 status = RegQueryValueEx(adapter_key_handle, "IpConfig", NULL, NULL, keyptr, &buf_len);
james@424 158 if (status != ERROR_SUCCESS)
james@424 159 {
james@424 160 printf(" Failed to read registry (%08x). Ignoring.\n", status);
james@424 161 continue;
james@424 162 }
james@424 163 }
james@424 164
james@424 165 printf("\nCloning IP Configurations\n");
james@424 166 for (adapter = list_head; adapter != NULL; adapter = adapter->next)
james@424 167 {
james@424 168 printf("Considering MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
james@424 169 adapter->mac_address[0], adapter->mac_address[1], adapter->mac_address[2],
james@424 170 adapter->mac_address[3], adapter->mac_address[4], adapter->mac_address[5]);
james@424 171 if (adapter->xen_IpConfig_key_name[0])
james@424 172 printf(" Xen adapter present\n");
james@424 173 else
james@424 174 printf(" Xen adapter not present\n");
james@424 175 if (adapter->other_IpConfig_key_name[0])
james@424 176 printf(" Other adapter present\n");
james@424 177 else
james@424 178 printf(" Other adapter not present\n");
james@424 179
james@424 180 if (adapter->xen_IpConfig_key_name[0] && adapter->other_IpConfig_key_name[0])
james@424 181 {
james@424 182 // open HKLM\SYSTEM\Services\%s
james@424 183 sprintf(buf, "SYSTEM\\CurrentControlSet\\Services\\%s", adapter->xen_IpConfig_key_name);
james@424 184 status = RegOpenKey(HKEY_LOCAL_MACHINE, buf, &dst_key_handle);
james@424 185 if (status != ERROR_SUCCESS)
james@424 186 {
james@424 187 printf(" Cannot open Xen adapter config key. Skipping.\n");
james@424 188 continue;
james@424 189 }
james@424 190 sprintf(buf, "SYSTEM\\CurrentControlSet\\Services\\%s", adapter->other_IpConfig_key_name);
james@424 191 status = RegOpenKey(HKEY_LOCAL_MACHINE, buf, &src_key_handle);
james@424 192 if (status != ERROR_SUCCESS)
james@424 193 {
james@424 194 printf(" Cannot open Other adapter config key. Skipping.\n");
james@424 195 continue;
james@424 196 }
james@424 197 value_name_len = 256;
james@424 198 value_data_len = 1024;
james@424 199 while ((status = RegEnumValue(dst_key_handle, 0, value_name, &value_name_len, NULL, &value_type, value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
james@424 200 {
james@424 201 RegDeleteValue(dst_key_handle, value_name);
james@424 202 value_name_len = 256;
james@424 203 value_data_len = 1024;
james@424 204 }
james@424 205 i = 0;
james@424 206 value_name_len = 256;
james@424 207 value_data_len = 1024;
james@424 208 while ((status = RegEnumValue(src_key_handle, i, value_name, &value_name_len, NULL, &value_type, value_data, &value_data_len)) != ERROR_NO_MORE_ITEMS)
james@424 209 {
james@424 210 RegSetValueEx(dst_key_handle, value_name, 0, value_type, value_data, value_data_len);
james@424 211 value_name_len = 256;
james@424 212 value_data_len = 1024;
james@424 213 i++;
james@424 214 }
james@424 215 printf(" Copied\n", buf);
james@424 216 }
james@424 217 }
james@424 218
james@424 219 // loop through key names that look like GUIDs
james@424 220 // get OID_GEN_VENDOR_DESCRIPTION
james@424 221 // if description does not contain "RTL8139" then continue to next key name
james@424 222 // get OID_802_3_PERMANENT_ADDRESS
james@424 223 // get IpConfig path value registry
james@424 224 // again loop through key names that look like GUIDs
james@424 225 // if description does not contain "xen" then continue to next key name
james@424 226 // get IpConfig path value from registry
james@424 227 // delete contents of above path
james@424 228 // copy contents of RTL8139 IpConfig to Xen IpConfig (use RegCopyTree)
james@424 229 }
james@424 230