123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /* $Id: interface.c 1928 2007-10-26 17:21:35Z aturner $ */
- /*
- * Copyright (c) 2007 Aaron Turner.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the names of the copyright owners nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
- * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include "config.h"
- #include "defines.h"
- #include "common.h"
- #include "interface.h"
- /**
- * Method takes a user specified device name and returns
- * the canonical name for that device. This allows me to
- * create named interface aliases on platforms like Windows
- * which use horrifically long interface names
- *
- * Returns NULL on error
- */
- char *
- get_interface(interface_list_t *list, const char *alias)
- {
- interface_list_t *ptr;
-
- assert(alias);
-
- if (list != NULL) {
- ptr = list;
-
- do {
- /* check both the alias & name fields */
- if (strcmp(alias, ptr->alias) == 0)
- return(ptr->name);
-
- if (strcmp(alias, ptr->name) == 0)
- return(ptr->name);
-
- ptr = ptr->next;
- } while (ptr != NULL);
- } else {
- return(alias);
- }
-
- return(NULL);
- }
- /**
- * Get all available interfaces as an interface_list *
- */
- interface_list_t *
- get_interface_list(void)
- {
- interface_list_t *list_head, *list_ptr;
- char ebuf[PCAP_ERRBUF_SIZE];
- pcap_if_t *pcap_if, *pcap_if_ptr;
- int i = 0;
-
- #ifndef HAVE_WIN32
- /* Unix just has a warning about being root */
- if (geteuid() != 0)
- warn("May need to run as root to get complete list.");
- #endif
- if (pcap_findalldevs(&pcap_if, ebuf) < 0)
- errx(1, "Error: %s", ebuf);
-
- pcap_if_ptr = pcap_if;
- list_head = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
- list_ptr = list_head;
-
- while (pcap_if_ptr != NULL) {
- if (i > 0) {
- list_ptr->next = (interface_list_t *)safe_malloc(sizeof(interface_list_t));
- list_ptr = list_ptr->next;
- }
- strlcpy(list_ptr->name, pcap_if_ptr->name, sizeof(list_ptr->name));
-
- /* description is usually null under Unix */
- if (pcap_if_ptr->description != NULL)
- strlcpy(list_ptr->description, pcap_if_ptr->description, sizeof(list_ptr->description));
-
- sprintf(list_ptr->alias, "%%%d", i++);
- list_ptr->flags = pcap_if_ptr->flags;
- pcap_if_ptr = pcap_if_ptr->next;
- }
- pcap_freealldevs(pcap_if);
- return(list_head);
- }
- /**
- * Prints all the available interfaces found by get_interface_list()
- */
- void
- list_interfaces(interface_list_t *list)
- {
- interface_list_t *ptr;
- if (list == NULL) {
- printf("No network interfaces available");
- return;
- }
- printf("Available network interfaces:\n");
-
- #ifdef HAVE_WIN32 /* Win32 has alias/name/description */
- printf("Alias\tName\tDescription\n");
- #endif
-
-
- ptr = list;
- do {
- if (! ptr->flags & PCAP_IF_LOOPBACK) {
- #ifdef HAVE_WIN32
- printf("%s\t%s\n\t%s\n", ptr->alias, ptr->name, ptr->description);
- #else
- printf("%s\n", ptr->name);
- #endif
- }
- ptr = ptr->next;
- } while (ptr != NULL);
- }
|