Use "pcap_findalldevs()" if present.
[obnox/wireshark/wip.git] / pcap-util.c
1 /* pcap-util.c
2  * Utility routines for packet capture
3  *
4  * $Id: pcap-util.c,v 1.18 2003/10/10 03:00:10 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #include <glib.h>
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
38 #endif
39
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
42 #endif
43
44 #include <pcap.h>
45
46 #include "pcap-util.h"
47 #include "pcap-util-int.h"
48
49 /*
50  * Get the data-link type for a libpcap device.
51  * This works around AIX 5.x's non-standard and incompatible-with-the-
52  * rest-of-the-universe libpcap.
53  */
54 int
55 get_pcap_linktype(pcap_t *pch, char *devname
56 #ifndef AIX
57         _U_
58 #endif
59 )
60 {
61         int linktype;
62 #ifdef AIX
63         char *ifacename;
64 #endif
65
66         linktype = pcap_datalink(pch);
67 #ifdef AIX
68
69         /*
70          * The libpcap that comes with AIX 5.x uses RFC 1573 ifType values
71          * rather than DLT_ values for link-layer types; the ifType values
72          * for LAN devices are:
73          *
74          *      Ethernet        6
75          *      802.3           7
76          *      Token Ring      9
77          *      FDDI            15
78          *
79          * and the ifType value for a loopback device is 24.
80          *
81          * The AIX names for LAN devices begin with:
82          *
83          *      Ethernet                en
84          *      802.3                   et
85          *      Token Ring              tr
86          *      FDDI                    fi
87          *
88          * and the AIX names for loopback devices begin with "lo".
89          *
90          * (The difference between "Ethernet" and "802.3" is presumably
91          * whether packets have an Ethernet header, with a packet type,
92          * or an 802.3 header, with a packet length, followed by an 802.2
93          * header and possibly a SNAP header.)
94          *
95          * If the device name matches "linktype" interpreted as an ifType
96          * value, rather than as a DLT_ value, we will assume this is AIX's
97          * non-standard, incompatible libpcap, rather than a standard libpcap,
98          * and will map the link-layer type to the standard DLT_ value for
99          * that link-layer type, as that's what the rest of Ethereal expects.
100          *
101          * (This means the capture files won't be readable by a tcpdump
102          * linked with AIX's non-standard libpcap, but so it goes.  They
103          * *will* be readable by standard versions of tcpdump, Ethereal,
104          * and so on.)
105          *
106          * XXX - if we conclude we're using AIX libpcap, should we also
107          * set a flag to cause us to assume the time stamps are in
108          * seconds-and-nanoseconds form, and to convert them to
109          * seconds-and-microseconds form before processing them and
110          * writing them out?
111          */
112
113         /*
114          * Find the last component of the device name, which is the
115          * interface name.
116          */
117         ifacename = strchr(devname, '/');
118         if (ifacename == NULL)
119                 ifacename = devnames;
120
121         /* See if it matches any of the LAN device names. */
122         if (strncmp(ifacename, "en", 2) == 0) {
123                 if (linktype == 6) {
124                         /*
125                          * That's the RFC 1573 value for Ethernet; map it
126                          * to DLT_EN10MB.
127                          */
128                         linktype = 1;
129                 }
130         } else if (strncmp(ifacename, "et", 2) == 0) {
131                 if (linktype == 7) {
132                         /*
133                          * That's the RFC 1573 value for 802.3; map it to
134                          * DLT_EN10MB.
135                          * (libpcap, tcpdump, Ethereal, etc. don't care if
136                          * it's Ethernet or 802.3.)
137                          */
138                         linktype = 1;
139                 }
140         } else if (strncmp(ifacename, "tr") == 0) {
141                 if (linktype == 9) {
142                         /*
143                          * That's the RFC 1573 value for 802.5 (Token Ring);
144                          * map it to DLT_IEEE802, which is what's used for
145                          * Token Ring.
146                          */
147                         linktype = 6;
148                 }
149         } else if (strncmp(ifacename, "fi") == 0) {
150                 if (linktype == 15) {
151                         /*
152                          * That's the RFC 1573 value for FDDI; map it to
153                          * DLT_FDDI.
154                          */
155                         linktype = 10;
156                 }
157         } else if (strncmp(ifacename, "lo") == 0) {
158                 if (linktype == 24) {
159                         /*
160                          * That's the RFC 1573 value for "software loopback"
161                          * devices; map it to DLT_NULL, which is what's used
162                          * for loopback devices on BSD.
163                          */
164                         linktype = 0;
165                 }
166         }
167 #endif
168
169         return linktype;
170 }
171
172 if_info_t *
173 if_info_new(char *name, char *description)
174 {
175         if_info_t *if_info;
176
177         if_info = g_malloc(sizeof (if_info_t));
178         if_info->name = g_strdup(name);
179         if (description == NULL)
180                 if_info->description = NULL;
181         else
182                 if_info->description = g_strdup(description);
183         return if_info;
184 }
185
186 #ifdef HAVE_PCAP_FINDALLDEVS
187 GList *
188 get_interface_list_findalldevs(int *err, char *err_str)
189 {
190         GList  *il = NULL;
191         pcap_if_t *alldevs, *dev;
192         if_info_t *if_info;
193
194         if (pcap_findalldevs(&alldevs, err_str) == -1) {
195                 *err = CANT_GET_INTERFACE_LIST;
196                 return NULL;
197         }
198
199         if (alldevs == NULL) {
200                 /*
201                  * No interfaces found.
202                  */
203                 *err = NO_INTERFACES_FOUND;
204                 return NULL;
205         }
206
207         for (dev = alldevs; dev != NULL; dev = dev->next) {
208                 if_info = if_info_new(dev->name, dev->description);
209                 il = g_list_append(il, if_info);
210         }
211         pcap_freealldevs(alldevs);
212
213         return il;
214 }
215 #endif /* HAVE_PCAP_FINDALLDEVS */
216
217 static void
218 free_if_cb(gpointer data, gpointer user_data _U_)
219 {
220         if_info_t *if_info = data;
221
222         g_free(if_info->name);
223         if (if_info->description != NULL)
224                 g_free(if_info->description);
225 }
226
227 void
228 free_interface_list(GList *if_list)
229 {
230         g_list_foreach(if_list, free_if_cb, NULL);
231         g_list_free(if_list);
232 }
233
234 #endif /* HAVE_LIBPCAP */