2 * Utility routines for packet capture
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
49 #include "capture_ifinfo.h"
50 #include "capture-pcap-util.h"
51 #include "capture-pcap-util-int.h"
54 #include <netinet/in.h>
58 if_info_new(char *name, char *description)
62 if_info = (if_info_t *)g_malloc(sizeof (if_info_t));
63 if_info->name = g_strdup(name);
64 if (description == NULL)
65 if_info->description = NULL;
67 if_info->description = g_strdup(description);
68 if_info->addrs = NULL;
69 if_info->loopback = FALSE;
74 if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
77 struct sockaddr_in *ai;
79 struct sockaddr_in6 *ai6;
82 switch (addr->sa_family) {
85 ai = (struct sockaddr_in *)addr;
86 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
87 if_addr->ifat_type = IF_AT_IPv4;
88 if_addr->addr.ip4_addr =
89 *((guint32 *)&(ai->sin_addr.s_addr));
90 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
95 ai6 = (struct sockaddr_in6 *)addr;
96 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
97 if_addr->ifat_type = IF_AT_IPv6;
98 memcpy((void *)&if_addr->addr.ip6_addr,
99 (void *)&ai6->sin6_addr.s6_addr,
100 sizeof if_addr->addr.ip6_addr);
101 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
107 #ifdef HAVE_PCAP_FINDALLDEVS
109 * Get all IP address information, and the loopback flag, for the given
113 if_info_ip(if_info_t *if_info, pcap_if_t *d)
118 if_info->loopback = (d->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE;
121 for (a = d->addresses; a != NULL; a = a->next) {
123 if_info_add_address(if_info, a->addr);
127 #ifdef HAVE_PCAP_REMOTE
129 get_interface_list_findalldevs_ex(const char *source,
130 struct pcap_rmtauth *auth,
131 int *err, char **err_str)
134 pcap_if_t *alldevs, *dev;
136 char errbuf[PCAP_ERRBUF_SIZE];
138 if (pcap_findalldevs_ex((char *)source, auth, &alldevs, errbuf) == -1) {
139 *err = CANT_GET_INTERFACE_LIST;
141 *err_str = cant_get_if_list_error_message(errbuf);
145 if (alldevs == NULL) {
147 * No interfaces found.
149 *err = NO_INTERFACES_FOUND;
155 for (dev = alldevs; dev != NULL; dev = dev->next) {
156 if_info = if_info_new(dev->name, dev->description);
157 il = g_list_append(il, if_info);
158 if_info_ip(if_info, dev);
160 pcap_freealldevs(alldevs);
167 get_interface_list_findalldevs(int *err, char **err_str)
170 pcap_if_t *alldevs, *dev;
172 char errbuf[PCAP_ERRBUF_SIZE];
174 if (pcap_findalldevs(&alldevs, errbuf) == -1) {
175 *err = CANT_GET_INTERFACE_LIST;
177 *err_str = cant_get_if_list_error_message(errbuf);
181 if (alldevs == NULL) {
183 * No interfaces found.
185 *err = NO_INTERFACES_FOUND;
191 for (dev = alldevs; dev != NULL; dev = dev->next) {
192 if_info = if_info_new(dev->name, dev->description);
193 il = g_list_append(il, if_info);
194 if_info_ip(if_info, dev);
196 pcap_freealldevs(alldevs);
200 #endif /* HAVE_PCAP_FINDALLDEVS */
203 free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
209 free_if_cb(gpointer data, gpointer user_data _U_)
211 if_info_t *if_info = (if_info_t *)data;
213 g_free(if_info->name);
214 g_free(if_info->description);
216 g_slist_foreach(if_info->addrs, free_if_info_addr_cb, NULL);
217 g_slist_free(if_info->addrs);
222 free_interface_list(GList *if_list)
224 g_list_foreach(if_list, free_if_cb, NULL);
225 g_list_free(if_list);
228 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
231 const char *description;
235 #define DLT_CHOICE(code, description) { #code, description, code }
236 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
238 static struct dlt_choice dlt_choices[] = {
239 DLT_CHOICE(DLT_NULL, "BSD loopback"),
240 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
241 DLT_CHOICE(DLT_IEEE802, "Token ring"),
242 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
243 DLT_CHOICE(DLT_SLIP, "SLIP"),
244 DLT_CHOICE(DLT_PPP, "PPP"),
245 DLT_CHOICE(DLT_FDDI, "FDDI"),
246 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
247 DLT_CHOICE(DLT_RAW, "Raw IP"),
248 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
249 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
250 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
251 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
252 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
253 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
254 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
255 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
256 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
257 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
258 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
259 DLT_CHOICE(DLT_LTALK, "Localtalk"),
260 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
261 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
262 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
263 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
264 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
265 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
266 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
267 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
268 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
272 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL)
274 pcap_datalink_name_to_val(const char *name)
278 for (i = 0; dlt_choices[i].name != NULL; i++) {
279 if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
281 return (dlt_choices[i].dlt);
285 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) */
287 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
289 pcap_datalink_val_to_name(int dlt)
293 for (i = 0; dlt_choices[i].name != NULL; i++) {
294 if (dlt_choices[i].dlt == dlt)
295 return (dlt_choices[i].name + sizeof("DLT_") - 1);
299 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
301 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
303 pcap_datalink_val_to_description(int dlt)
307 for (i = 0; dlt_choices[i].name != NULL; i++) {
308 if (dlt_choices[i].dlt == dlt)
309 return (dlt_choices[i].description);
313 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
315 #endif /* !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
318 free_linktype_cb(gpointer data, gpointer user_data _U_)
320 data_link_info_t *linktype_info = (data_link_info_t *)data;
322 g_free(linktype_info->name);
323 g_free(linktype_info->description);
327 free_if_capabilities(if_capabilities_t *caps)
329 g_list_foreach(caps->data_link_types, free_linktype_cb, NULL);
330 g_list_free(caps->data_link_types);
335 linktype_val_to_name(int dlt)
337 return pcap_datalink_val_to_name(dlt);
340 int linktype_name_to_val(const char *linktype)
342 return pcap_datalink_name_to_val(linktype);
345 #endif /* HAVE_LIBPCAP */