interface.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* $Id: interface.c 1928 2007-10-26 17:21:35Z aturner $ */
  2. /*
  3. * Copyright (c) 2007 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. char *
  48. get_interface(interface_list_t *list, const char *alias)
  49. {
  50. interface_list_t *ptr;
  51. assert(alias);
  52. if (list != NULL) {
  53. ptr = list;
  54. do {
  55. /* check both the alias & name fields */
  56. if (strcmp(alias, ptr->alias) == 0)
  57. return(ptr->name);
  58. if (strcmp(alias, ptr->name) == 0)
  59. return(ptr->name);
  60. ptr = ptr->next;
  61. } while (ptr != NULL);
  62. } else {
  63. return(alias);
  64. }
  65. return(NULL);
  66. }
  67. /**
  68. * Get all available interfaces as an interface_list *
  69. */
  70. interface_list_t *
  71. get_interface_list(void)
  72. {
  73. interface_list_t *list_head, *list_ptr;
  74. char ebuf[PCAP_ERRBUF_SIZE];
  75. pcap_if_t *pcap_if, *pcap_if_ptr;
  76. int i = 0;
  77. #ifndef HAVE_WIN32
  78. /* Unix just has a warning about being root */
  79. if (geteuid() != 0)
  80. warn("May need to run as root to get complete list.");
  81. #endif
  82. if (pcap_findalldevs(&pcap_if, ebuf) < 0)
  83. errx(1, "Error: %s", ebuf);
  84. pcap_if_ptr = pcap_if;
  85. list_head = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  86. list_ptr = list_head;
  87. while (pcap_if_ptr != NULL) {
  88. if (i > 0) {
  89. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  90. list_ptr = list_ptr->next;
  91. }
  92. strlcpy(list_ptr->name, pcap_if_ptr->name, sizeof(list_ptr->name));
  93. /* description is usually null under Unix */
  94. if (pcap_if_ptr->description != NULL)
  95. strlcpy(list_ptr->description, pcap_if_ptr->description, sizeof(list_ptr->description));
  96. sprintf(list_ptr->alias, "%%%d", i++);
  97. list_ptr->flags = pcap_if_ptr->flags;
  98. pcap_if_ptr = pcap_if_ptr->next;
  99. }
  100. pcap_freealldevs(pcap_if);
  101. return(list_head);
  102. }
  103. /**
  104. * Prints all the available interfaces found by get_interface_list()
  105. */
  106. void
  107. list_interfaces(interface_list_t *list)
  108. {
  109. interface_list_t *ptr;
  110. if (list == NULL) {
  111. printf("No network interfaces available");
  112. return;
  113. }
  114. printf("Available network interfaces:\n");
  115. #ifdef HAVE_WIN32 /* Win32 has alias/name/description */
  116. printf("Alias\tName\tDescription\n");
  117. #endif
  118. ptr = list;
  119. do {
  120. if (! ptr->flags & PCAP_IF_LOOPBACK) {
  121. #ifdef HAVE_WIN32
  122. printf("%s\t%s\n\t%s\n", ptr->alias, ptr->name, ptr->description);
  123. #else
  124. printf("%s\n", ptr->name);
  125. #endif
  126. }
  127. ptr = ptr->next;
  128. } while (ptr != NULL);
  129. }