interface.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* $Id: interface.c 2423 2010-03-13 07:09:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2007-2010 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <string.h>
  32. #include <sys/types.h>
  33. #include <unistd.h>
  34. #include <stdlib.h>
  35. #include "config.h"
  36. #include "defines.h"
  37. #include "common.h"
  38. #include "interface.h"
  39. /**
  40. * Method takes a user specified device name and returns
  41. * the canonical name for that device. This allows me to
  42. * create named interface aliases on platforms like Windows
  43. * which use horrifically long interface names
  44. *
  45. * Returns NULL on error
  46. *
  47. * On success, it *may* malloc() memory equal to the length of *alias.
  48. */
  49. char *
  50. get_interface(interface_list_t *list, const char *alias)
  51. {
  52. interface_list_t *ptr;
  53. char *name;
  54. assert(alias);
  55. if (list != NULL) {
  56. ptr = list;
  57. do {
  58. /* check both the alias & name fields */
  59. if (strcmp(alias, ptr->alias) == 0)
  60. return(ptr->name);
  61. if (strcmp(alias, ptr->name) == 0)
  62. return(ptr->name);
  63. ptr = ptr->next;
  64. } while (ptr != NULL);
  65. } else {
  66. name = (char *)safe_malloc(strlen(alias) + 1);
  67. strlcpy(name, alias, sizeof(name));
  68. return(name);
  69. }
  70. return(NULL);
  71. }
  72. /**
  73. * Get all available interfaces as an interface_list *
  74. */
  75. interface_list_t *
  76. get_interface_list(void)
  77. {
  78. interface_list_t *list_head, *list_ptr;
  79. char ebuf[PCAP_ERRBUF_SIZE];
  80. pcap_if_t *pcap_if, *pcap_if_ptr;
  81. int i = 0;
  82. #ifndef HAVE_WIN32
  83. /* Unix just has a warning about being root */
  84. if (geteuid() != 0)
  85. warn("May need to run as root to get complete list.");
  86. #endif
  87. if (pcap_findalldevs(&pcap_if, ebuf) < 0)
  88. errx(-1, "Error: %s", ebuf);
  89. pcap_if_ptr = pcap_if;
  90. list_head = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  91. list_ptr = list_head;
  92. while (pcap_if_ptr != NULL) {
  93. if (i > 0) {
  94. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  95. list_ptr = list_ptr->next;
  96. }
  97. strlcpy(list_ptr->name, pcap_if_ptr->name, sizeof(list_ptr->name));
  98. /* description is usually null under Unix */
  99. if (pcap_if_ptr->description != NULL)
  100. strlcpy(list_ptr->description, pcap_if_ptr->description, sizeof(list_ptr->description));
  101. sprintf(list_ptr->alias, "%%%d", i++);
  102. list_ptr->flags = pcap_if_ptr->flags;
  103. pcap_if_ptr = pcap_if_ptr->next;
  104. }
  105. pcap_freealldevs(pcap_if);
  106. return(list_head);
  107. }
  108. /**
  109. * Prints all the available interfaces found by get_interface_list()
  110. */
  111. void
  112. list_interfaces(interface_list_t *list)
  113. {
  114. interface_list_t *ptr;
  115. if (list == NULL) {
  116. printf("No network interfaces available");
  117. return;
  118. }
  119. printf("Available network interfaces:\n");
  120. #ifdef HAVE_WIN32 /* Win32 has alias/name/description */
  121. printf("Alias\tName\tDescription\n");
  122. #endif
  123. ptr = list;
  124. do {
  125. if (! ptr->flags & PCAP_IF_LOOPBACK) {
  126. #ifdef HAVE_WIN32
  127. printf("%s\t%s\n\t%s\n", ptr->alias, ptr->name, ptr->description);
  128. #else
  129. printf("%s\n", ptr->name);
  130. #endif
  131. }
  132. ptr = ptr->next;
  133. } while (ptr != NULL);
  134. }