interface.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2022 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "interface.h"
  20. #include "config.h"
  21. #include "common.h"
  22. #include <dirent.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/ioctl.h>
  26. #include <unistd.h>
  27. /**
  28. * Method takes a user specified device name and returns
  29. * the canonical name for that device. This allows me to
  30. * create named interface aliases on platforms like Windows
  31. * which use horrifically long interface names
  32. *
  33. * Returns NULL on error
  34. */
  35. char *
  36. get_interface(interface_list_t *list, const char *alias)
  37. {
  38. interface_list_t *ptr;
  39. assert(alias);
  40. ptr = list;
  41. while (ptr) {
  42. /* check both the alias & name fields */
  43. if (strcmp(alias, ptr->alias) == 0 || strcmp(alias, ptr->name) == 0)
  44. return (ptr->name);
  45. ptr = ptr->next;
  46. }
  47. return (NULL);
  48. }
  49. /**
  50. * Get all available interfaces as an interface_list *
  51. */
  52. interface_list_t *
  53. get_interface_list(void)
  54. {
  55. interface_list_t *list_head, *list_ptr;
  56. char ebuf[PCAP_ERRBUF_SIZE], testnic[320];
  57. pcap_if_t *pcap_if, *pcap_if_ptr;
  58. int i = 0;
  59. DIR *dir;
  60. struct dirent *dirdata;
  61. #ifdef HAVE_NETMAP
  62. nmreq_t nmr;
  63. #endif
  64. #ifdef HAVE_NETMAP
  65. int netmap_version;
  66. #endif
  67. #ifndef HAVE_WIN32
  68. /* Unix just has a warning about being root */
  69. if (geteuid() != 0)
  70. warn("May need to run as root to get access to all network interfaces.");
  71. #endif
  72. #ifdef HAVE_NETMAP
  73. netmap_version = get_netmap_version();
  74. #endif
  75. if (pcap_findalldevs(&pcap_if, ebuf) < 0)
  76. errx(-1, "Error: %s", ebuf);
  77. pcap_if_ptr = pcap_if;
  78. list_head = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  79. list_ptr = list_head;
  80. while (pcap_if_ptr != NULL) {
  81. if (i > 0) {
  82. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  83. list_ptr = list_ptr->next;
  84. }
  85. strlcpy(list_ptr->name, pcap_if_ptr->name, sizeof(list_ptr->name));
  86. dbgx(3, "Adding %s to interface list", list_ptr->name);
  87. /* description is usually null under Unix */
  88. if (pcap_if_ptr->description != NULL)
  89. strlcpy(list_ptr->description, pcap_if_ptr->description, sizeof(list_ptr->description));
  90. sprintf(list_ptr->alias, "%%%d", i++);
  91. list_ptr->flags = pcap_if_ptr->flags;
  92. #ifdef HAVE_LIBPCAP_NETMAP
  93. /*
  94. * add the syntaxes supported by netmap-libpcap
  95. *
  96. * available at http://code.google.com/p/netmap-libpcap/
  97. */
  98. if (!(pcap_if_ptr->flags & PCAP_IF_LOOPBACK) && strcmp("any", pcap_if_ptr->name)) {
  99. #endif
  100. #ifdef HAVE_NETMAP
  101. int fd = -1;
  102. if (netmap_version != -1 && (fd = open("/dev/netmap", O_RDWR)) < 0)
  103. continue;
  104. bzero(&nmr, sizeof(nmr));
  105. strncpy(nmr.nr_name, pcap_if_ptr->name, sizeof(nmr.nr_name));
  106. nmr.nr_version = netmap_version;
  107. if (fd > 0 && ioctl(fd, NIOCGINFO, &nmr) == 0) {
  108. #endif /* HAVE_NETMAP */
  109. #if defined HAVE_LIBPCAP_NETMAP || defined HAVE_NETMAP
  110. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  111. list_ptr = list_ptr->next;
  112. snprintf(list_ptr->name, sizeof(list_ptr->name), "vale:%s", pcap_if_ptr->name);
  113. sprintf(list_ptr->alias, "%%%d", i++);
  114. list_ptr->flags = pcap_if_ptr->flags;
  115. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  116. list_ptr = list_ptr->next;
  117. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s", pcap_if_ptr->name);
  118. sprintf(list_ptr->alias, "%%%d", i++);
  119. list_ptr->flags = pcap_if_ptr->flags;
  120. #endif /* HAVE_LIBPCAP_NETMAP || HAVE_NETMAP */
  121. #ifdef HAVE_NETMAP
  122. if (netmap_version >= 10) {
  123. int x;
  124. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  125. list_ptr = list_ptr->next;
  126. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s!", pcap_if_ptr->name);
  127. sprintf(list_ptr->alias, "%%%d", i++);
  128. list_ptr->flags = pcap_if_ptr->flags;
  129. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  130. list_ptr = list_ptr->next;
  131. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s*", pcap_if_ptr->name);
  132. sprintf(list_ptr->alias, "%%%d", i++);
  133. list_ptr->flags = pcap_if_ptr->flags;
  134. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  135. list_ptr = list_ptr->next;
  136. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s^", pcap_if_ptr->name);
  137. sprintf(list_ptr->alias, "%%%d", i++);
  138. list_ptr->flags = pcap_if_ptr->flags;
  139. for (x = 0; x < nmr.nr_rx_rings; ++x) {
  140. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  141. list_ptr = list_ptr->next;
  142. snprintf(list_ptr->name, sizeof(list_ptr->name), "netmap:%s-%d", pcap_if_ptr->name, x);
  143. sprintf(list_ptr->alias, "%%%d", i++);
  144. list_ptr->flags = pcap_if_ptr->flags;
  145. }
  146. }
  147. }
  148. close(fd);
  149. #endif /* HAVE_NETMAP */
  150. #ifdef HAVE_LIBPCAP_NETMAP
  151. }
  152. #endif /* HAVE_LIBPCAP_NETMAP */
  153. #ifdef HAVE_PF_RING_PCAP
  154. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  155. list_ptr = list_ptr->next;
  156. snprintf(list_ptr->name, sizeof(list_ptr->name), "zc:%s", pcap_if_ptr->name);
  157. sprintf(list_ptr->alias, "%%%d", i++);
  158. list_ptr->flags = pcap_if_ptr->flags;
  159. #endif
  160. pcap_if_ptr = pcap_if_ptr->next;
  161. }
  162. pcap_freealldevs(pcap_if);
  163. /* look for khial device: https://github.com/boundary/khial */
  164. if ((dir = opendir("/dev/char")) != NULL) {
  165. while ((dirdata = readdir(dir)) != NULL) {
  166. if (strncmp(dirdata->d_name, "testpackets", strlen("testpackets")) == 0) {
  167. if (i > 0) {
  168. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  169. list_ptr = list_ptr->next;
  170. }
  171. dbgx(3, "Adding %s to interface list", dirdata->d_name);
  172. snprintf(testnic, sizeof(testnic), "/dev/char/%s", dirdata->d_name);
  173. strlcpy(list_ptr->name, testnic, sizeof(list_ptr->name));
  174. snprintf(testnic, sizeof(testnic), "khial pseudo-nic: %s", dirdata->d_name);
  175. strlcpy(list_ptr->description, testnic, sizeof(list_ptr->description));
  176. strlcpy(list_ptr->alias, dirdata->d_name, sizeof(list_ptr->alias));
  177. i += 1;
  178. }
  179. }
  180. closedir(dir);
  181. }
  182. dbg(1, "xxx get_interface_list end");
  183. return (list_head);
  184. }
  185. /**
  186. * Prints all the available interfaces found by get_interface_list()
  187. */
  188. void
  189. list_interfaces(interface_list_t *list)
  190. {
  191. interface_list_t *ptr;
  192. if (list == NULL) {
  193. printf("No network interfaces available");
  194. return;
  195. }
  196. printf("Available network interfaces:\n");
  197. #ifdef HAVE_WIN32 /* Win32 has alias/name/description */
  198. printf("Alias\tName\tDescription\n");
  199. #endif
  200. ptr = list;
  201. do {
  202. if (!(ptr->flags & PCAP_IF_LOOPBACK)) {
  203. #ifdef HAVE_WIN32
  204. printf("%s\t%s\n\t%s\n", ptr->alias, ptr->name, ptr->description);
  205. #else
  206. printf("%s\n", ptr->name);
  207. #endif
  208. }
  209. ptr = ptr->next;
  210. } while (ptr != NULL);
  211. }