interface.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* $Id: interface.c 1845 2007-04-27 02:48:10Z 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. interface_list_t *
  68. get_interface_list(void)
  69. {
  70. interface_list_t *list_head, *list_ptr;
  71. char ebuf[PCAP_ERRBUF_SIZE];
  72. pcap_if_t *pcap_if, *pcap_if_ptr;
  73. int i = 0;
  74. #ifndef HAVE_WIN32
  75. /* Unix just has a warning about being root */
  76. if (geteuid() != 0)
  77. warn("May need to run as root to get complete list.");
  78. #endif
  79. if (pcap_findalldevs(&pcap_if, ebuf) < 0)
  80. errx(1, "Error: %s", ebuf);
  81. pcap_if_ptr = pcap_if;
  82. list_head = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  83. list_ptr = list_head;
  84. while (pcap_if_ptr != NULL) {
  85. strlcpy(list_ptr->name, pcap_if_ptr->name, sizeof(list_ptr->name));
  86. /* description is usually null under Unix */
  87. if (pcap_if_ptr->description != NULL)
  88. strlcpy(list_ptr->description, pcap_if_ptr->description, sizeof(list_ptr->description));
  89. sprintf(list_ptr->alias, "%%%d", i++);
  90. list_ptr->flags = pcap_if_ptr->flags;
  91. pcap_if_ptr = pcap_if_ptr->next;
  92. list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
  93. list_ptr = list_ptr->next;
  94. }
  95. free(list_ptr); /* free the last entry which was never used */
  96. pcap_freealldevs(pcap_if);
  97. return(list_head);
  98. }
  99. void
  100. list_interfaces(interface_list_t *list)
  101. {
  102. interface_list_t *ptr;
  103. if (list == NULL) {
  104. printf("No network interfaces available");
  105. return;
  106. }
  107. printf("Available network interfaces:\n");
  108. #ifdef HAVE_WIN32 /* Win32 has alias/name/description */
  109. printf("Alias\tName\tDescription\n");
  110. #endif
  111. ptr = list;
  112. do {
  113. if (! ptr->flags & PCAP_IF_LOOPBACK) {
  114. #ifdef HAVE_WIN32
  115. printf("%s\t%s\n\t%s\n", ptr->alias, ptr->name, ptr->description);
  116. #else
  117. printf("%s\n", ptr->name);
  118. #endif
  119. }
  120. ptr = ptr->next;
  121. } while (ptr != NULL);
  122. }