win-pvdrivers

annotate copyconfig/copyconfig.c @ 460:dd42744a458e

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