$search
00001 /* 00002 * win_if_list - Display network interfaces with description (for Windows) 00003 * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation. 00008 * 00009 * Alternatively, this software may be distributed under the terms of BSD 00010 * license. 00011 * 00012 * See README and COPYING for more details. 00013 * 00014 * This small tool is for the Windows build to provide an easy way of fetching 00015 * a list of available network interfaces. 00016 */ 00017 00018 #include "includes.h" 00019 #include <stdio.h> 00020 #ifdef CONFIG_USE_NDISUIO 00021 #include <winsock2.h> 00022 #include <ntddndis.h> 00023 #else /* CONFIG_USE_NDISUIO */ 00024 #include "pcap.h" 00025 #include <winsock.h> 00026 #endif /* CONFIG_USE_NDISUIO */ 00027 00028 #ifdef CONFIG_USE_NDISUIO 00029 00030 /* from nuiouser.h */ 00031 #define FSCTL_NDISUIO_BASE FILE_DEVICE_NETWORK 00032 00033 #define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \ 00034 CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access) 00035 00036 #define IOCTL_NDISUIO_QUERY_BINDING \ 00037 _NDISUIO_CTL_CODE(0x203, METHOD_BUFFERED, \ 00038 FILE_READ_ACCESS | FILE_WRITE_ACCESS) 00039 00040 #define IOCTL_NDISUIO_BIND_WAIT \ 00041 _NDISUIO_CTL_CODE(0x204, METHOD_BUFFERED, \ 00042 FILE_READ_ACCESS | FILE_WRITE_ACCESS) 00043 00044 typedef struct _NDISUIO_QUERY_BINDING 00045 { 00046 ULONG BindingIndex; 00047 ULONG DeviceNameOffset; 00048 ULONG DeviceNameLength; 00049 ULONG DeviceDescrOffset; 00050 ULONG DeviceDescrLength; 00051 } NDISUIO_QUERY_BINDING, *PNDISUIO_QUERY_BINDING; 00052 00053 00054 static HANDLE ndisuio_open(void) 00055 { 00056 DWORD written; 00057 HANDLE h; 00058 00059 h = CreateFile(TEXT("\\\\.\\\\Ndisuio"), 00060 GENERIC_READ | GENERIC_WRITE, 0, NULL, 00061 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 00062 INVALID_HANDLE_VALUE); 00063 if (h == INVALID_HANDLE_VALUE) 00064 return h; 00065 00066 #ifndef _WIN32_WCE 00067 if (!DeviceIoControl(h, IOCTL_NDISUIO_BIND_WAIT, NULL, 0, NULL, 0, 00068 &written, NULL)) { 00069 printf("IOCTL_NDISUIO_BIND_WAIT failed: %d", 00070 (int) GetLastError()); 00071 CloseHandle(h); 00072 return INVALID_HANDLE_VALUE; 00073 } 00074 #endif /* _WIN32_WCE */ 00075 00076 return h; 00077 } 00078 00079 00080 static void ndisuio_query_bindings(HANDLE ndisuio) 00081 { 00082 NDISUIO_QUERY_BINDING *b; 00083 size_t blen = sizeof(*b) + 1024; 00084 int i, error; 00085 DWORD written; 00086 char name[256], desc[256]; 00087 WCHAR *pos; 00088 size_t j, len; 00089 00090 b = malloc(blen); 00091 if (b == NULL) 00092 return; 00093 00094 for (i = 0; ; i++) { 00095 memset(b, 0, blen); 00096 b->BindingIndex = i; 00097 if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_QUERY_BINDING, 00098 b, sizeof(NDISUIO_QUERY_BINDING), b, 00099 (DWORD) blen, &written, NULL)) { 00100 error = (int) GetLastError(); 00101 if (error == ERROR_NO_MORE_ITEMS) 00102 break; 00103 printf("IOCTL_NDISUIO_QUERY_BINDING failed: %d", 00104 error); 00105 break; 00106 } 00107 00108 pos = (WCHAR *) ((char *) b + b->DeviceNameOffset); 00109 len = b->DeviceNameLength; 00110 if (len >= sizeof(name)) 00111 len = sizeof(name) - 1; 00112 for (j = 0; j < len; j++) 00113 name[j] = (char) pos[j]; 00114 name[len] = '\0'; 00115 00116 pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset); 00117 len = b->DeviceDescrLength; 00118 if (len >= sizeof(desc)) 00119 len = sizeof(desc) - 1; 00120 for (j = 0; j < len; j++) 00121 desc[j] = (char) pos[j]; 00122 desc[len] = '\0'; 00123 00124 printf("ifname: %s\ndescription: %s\n\n", name, desc); 00125 } 00126 00127 free(b); 00128 } 00129 00130 00131 static void ndisuio_enum_bindings(void) 00132 { 00133 HANDLE ndisuio = ndisuio_open(); 00134 if (ndisuio == INVALID_HANDLE_VALUE) 00135 return; 00136 00137 ndisuio_query_bindings(ndisuio); 00138 CloseHandle(ndisuio); 00139 } 00140 00141 #else /* CONFIG_USE_NDISUIO */ 00142 00143 static void show_dev(pcap_if_t *dev) 00144 { 00145 printf("ifname: %s\ndescription: %s\n\n", 00146 dev->name, dev->description); 00147 } 00148 00149 00150 static void pcap_enum_devs(void) 00151 { 00152 pcap_if_t *devs, *dev; 00153 char err[PCAP_ERRBUF_SIZE + 1]; 00154 00155 if (pcap_findalldevs(&devs, err) < 0) { 00156 fprintf(stderr, "Error - pcap_findalldevs: %s\n", err); 00157 return; 00158 } 00159 00160 for (dev = devs; dev; dev = dev->next) { 00161 show_dev(dev); 00162 } 00163 00164 pcap_freealldevs(devs); 00165 } 00166 00167 #endif /* CONFIG_USE_NDISUIO */ 00168 00169 00170 int main(int argc, char *argv[]) 00171 { 00172 #ifdef CONFIG_USE_NDISUIO 00173 ndisuio_enum_bindings(); 00174 #else /* CONFIG_USE_NDISUIO */ 00175 pcap_enum_devs(); 00176 #endif /* CONFIG_USE_NDISUIO */ 00177 00178 return 0; 00179 }