You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

win_if_list.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * win_if_list - Display network interfaces with description (for Windows)
  3. * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. *
  14. * This small tool is for the Windows build to provide an easy way of fetching
  15. * a list of available network interfaces.
  16. */
  17. #include "includes.h"
  18. #include <stdio.h>
  19. #ifdef CONFIG_USE_NDISUIO
  20. #include <winsock2.h>
  21. #include <ntddndis.h>
  22. #else /* CONFIG_USE_NDISUIO */
  23. #include "pcap.h"
  24. #include <winsock.h>
  25. #endif /* CONFIG_USE_NDISUIO */
  26. #ifdef CONFIG_USE_NDISUIO
  27. /* from nuiouser.h */
  28. #define FSCTL_NDISUIO_BASE FILE_DEVICE_NETWORK
  29. #define _NDISUIO_CTL_CODE(_Function, _Method, _Access) \
  30. CTL_CODE(FSCTL_NDISUIO_BASE, _Function, _Method, _Access)
  31. #define IOCTL_NDISUIO_QUERY_BINDING \
  32. _NDISUIO_CTL_CODE(0x203, METHOD_BUFFERED, \
  33. FILE_READ_ACCESS | FILE_WRITE_ACCESS)
  34. #define IOCTL_NDISUIO_BIND_WAIT \
  35. _NDISUIO_CTL_CODE(0x204, METHOD_BUFFERED, \
  36. FILE_READ_ACCESS | FILE_WRITE_ACCESS)
  37. typedef struct _NDISUIO_QUERY_BINDING
  38. {
  39. ULONG BindingIndex;
  40. ULONG DeviceNameOffset;
  41. ULONG DeviceNameLength;
  42. ULONG DeviceDescrOffset;
  43. ULONG DeviceDescrLength;
  44. } NDISUIO_QUERY_BINDING, *PNDISUIO_QUERY_BINDING;
  45. static HANDLE ndisuio_open(void)
  46. {
  47. DWORD written;
  48. HANDLE h;
  49. h = CreateFile(TEXT("\\\\.\\\\Ndisuio"),
  50. GENERIC_READ | GENERIC_WRITE, 0, NULL,
  51. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  52. INVALID_HANDLE_VALUE);
  53. if (h == INVALID_HANDLE_VALUE)
  54. return h;
  55. #ifndef _WIN32_WCE
  56. if (!DeviceIoControl(h, IOCTL_NDISUIO_BIND_WAIT, NULL, 0, NULL, 0,
  57. &written, NULL)) {
  58. printf("IOCTL_NDISUIO_BIND_WAIT failed: %d",
  59. (int) GetLastError());
  60. CloseHandle(h);
  61. return INVALID_HANDLE_VALUE;
  62. }
  63. #endif /* _WIN32_WCE */
  64. return h;
  65. }
  66. static void ndisuio_query_bindings(HANDLE ndisuio)
  67. {
  68. NDISUIO_QUERY_BINDING *b;
  69. size_t blen = sizeof(*b) + 1024;
  70. int i, error;
  71. DWORD written;
  72. char name[256], desc[256];
  73. WCHAR *pos;
  74. size_t j, len;
  75. b = malloc(blen);
  76. if (b == NULL)
  77. return;
  78. for (i = 0; ; i++) {
  79. memset(b, 0, blen);
  80. b->BindingIndex = i;
  81. if (!DeviceIoControl(ndisuio, IOCTL_NDISUIO_QUERY_BINDING,
  82. b, sizeof(NDISUIO_QUERY_BINDING), b,
  83. (DWORD) blen, &written, NULL)) {
  84. error = (int) GetLastError();
  85. if (error == ERROR_NO_MORE_ITEMS)
  86. break;
  87. printf("IOCTL_NDISUIO_QUERY_BINDING failed: %d",
  88. error);
  89. break;
  90. }
  91. pos = (WCHAR *) ((char *) b + b->DeviceNameOffset);
  92. len = b->DeviceNameLength;
  93. if (len >= sizeof(name))
  94. len = sizeof(name) - 1;
  95. for (j = 0; j < len; j++)
  96. name[j] = (char) pos[j];
  97. name[len] = '\0';
  98. pos = (WCHAR *) ((char *) b + b->DeviceDescrOffset);
  99. len = b->DeviceDescrLength;
  100. if (len >= sizeof(desc))
  101. len = sizeof(desc) - 1;
  102. for (j = 0; j < len; j++)
  103. desc[j] = (char) pos[j];
  104. desc[len] = '\0';
  105. printf("ifname: %s\ndescription: %s\n\n", name, desc);
  106. }
  107. free(b);
  108. }
  109. static void ndisuio_enum_bindings(void)
  110. {
  111. HANDLE ndisuio = ndisuio_open();
  112. if (ndisuio == INVALID_HANDLE_VALUE)
  113. return;
  114. ndisuio_query_bindings(ndisuio);
  115. CloseHandle(ndisuio);
  116. }
  117. #else /* CONFIG_USE_NDISUIO */
  118. static void show_dev(pcap_if_t *dev)
  119. {
  120. printf("ifname: %s\ndescription: %s\n\n",
  121. dev->name, dev->description);
  122. }
  123. static void pcap_enum_devs(void)
  124. {
  125. pcap_if_t *devs, *dev;
  126. char err[PCAP_ERRBUF_SIZE + 1];
  127. if (pcap_findalldevs(&devs, err) < 0) {
  128. fprintf(stderr, "Error - pcap_findalldevs: %s\n", err);
  129. return;
  130. }
  131. for (dev = devs; dev; dev = dev->next) {
  132. show_dev(dev);
  133. }
  134. pcap_freealldevs(devs);
  135. }
  136. #endif /* CONFIG_USE_NDISUIO */
  137. int main(int argc, char *argv[])
  138. {
  139. #ifdef CONFIG_USE_NDISUIO
  140. ndisuio_enum_bindings();
  141. #else /* CONFIG_USE_NDISUIO */
  142. pcap_enum_devs();
  143. #endif /* CONFIG_USE_NDISUIO */
  144. return 0;
  145. }