Set the svn:eol-style property on all text files to "native", so that
[obnox/wireshark/wip.git] / pcap-util.c
1 /* pcap-util.c
2  * Utility routines for packet capture
3  *
4  * $Id$
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 <pcap.h>
32
33 #include <glib.h>
34
35 #include <stdlib.h>
36 #include <limits.h>
37 #include <string.h>
38
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
45 #endif
46
47 #include <wtap.h>
48 #include <wtap-capture.h>
49
50 #include "pcap-util.h"
51 #include "pcap-util-int.h"
52
53 #ifndef WIN32
54 #include <netinet/in.h>
55 #endif
56
57
58 /*
59  * Get the data-link type for a libpcap device.
60  * This works around AIX 5.x's non-standard and incompatible-with-the-
61  * rest-of-the-universe libpcap.
62  */
63 int
64 get_pcap_linktype(pcap_t *pch, char *devname
65 #ifndef _AIX
66         _U_
67 #endif
68 )
69 {
70         int linktype;
71 #ifdef _AIX
72         char *ifacename;
73 #endif
74
75         linktype = pcap_datalink(pch);
76 #ifdef _AIX
77
78         /*
79          * The libpcap that comes with AIX 5.x uses RFC 1573 ifType values
80          * rather than DLT_ values for link-layer types; the ifType values
81          * for LAN devices are:
82          *
83          *      Ethernet        6
84          *      802.3           7
85          *      Token Ring      9
86          *      FDDI            15
87          *
88          * and the ifType value for a loopback device is 24.
89          *
90          * The AIX names for LAN devices begin with:
91          *
92          *      Ethernet                en
93          *      802.3                   et
94          *      Token Ring              tr
95          *      FDDI                    fi
96          *
97          * and the AIX names for loopback devices begin with "lo".
98          *
99          * (The difference between "Ethernet" and "802.3" is presumably
100          * whether packets have an Ethernet header, with a packet type,
101          * or an 802.3 header, with a packet length, followed by an 802.2
102          * header and possibly a SNAP header.)
103          *
104          * If the device name matches "linktype" interpreted as an ifType
105          * value, rather than as a DLT_ value, we will assume this is AIX's
106          * non-standard, incompatible libpcap, rather than a standard libpcap,
107          * and will map the link-layer type to the standard DLT_ value for
108          * that link-layer type, as that's what the rest of Ethereal expects.
109          *
110          * (This means the capture files won't be readable by a tcpdump
111          * linked with AIX's non-standard libpcap, but so it goes.  They
112          * *will* be readable by standard versions of tcpdump, Ethereal,
113          * and so on.)
114          *
115          * XXX - if we conclude we're using AIX libpcap, should we also
116          * set a flag to cause us to assume the time stamps are in
117          * seconds-and-nanoseconds form, and to convert them to
118          * seconds-and-microseconds form before processing them and
119          * writing them out?
120          */
121
122         /*
123          * Find the last component of the device name, which is the
124          * interface name.
125          */
126         ifacename = strchr(devname, '/');
127         if (ifacename == NULL)
128                 ifacename = devname;
129
130         /* See if it matches any of the LAN device names. */
131         if (strncmp(ifacename, "en", 2) == 0) {
132                 if (linktype == 6) {
133                         /*
134                          * That's the RFC 1573 value for Ethernet; map it
135                          * to DLT_EN10MB.
136                          */
137                         linktype = 1;
138                 }
139         } else if (strncmp(ifacename, "et", 2) == 0) {
140                 if (linktype == 7) {
141                         /*
142                          * That's the RFC 1573 value for 802.3; map it to
143                          * DLT_EN10MB.
144                          * (libpcap, tcpdump, Ethereal, etc. don't care if
145                          * it's Ethernet or 802.3.)
146                          */
147                         linktype = 1;
148                 }
149         } else if (strncmp(ifacename, "tr", 2) == 0) {
150                 if (linktype == 9) {
151                         /*
152                          * That's the RFC 1573 value for 802.5 (Token Ring);
153                          * map it to DLT_IEEE802, which is what's used for
154                          * Token Ring.
155                          */
156                         linktype = 6;
157                 }
158         } else if (strncmp(ifacename, "fi", 2) == 0) {
159                 if (linktype == 15) {
160                         /*
161                          * That's the RFC 1573 value for FDDI; map it to
162                          * DLT_FDDI.
163                          */
164                         linktype = 10;
165                 }
166         } else if (strncmp(ifacename, "lo", 2) == 0) {
167                 if (linktype == 24) {
168                         /*
169                          * That's the RFC 1573 value for "software loopback"
170                          * devices; map it to DLT_NULL, which is what's used
171                          * for loopback devices on BSD.
172                          */
173                         linktype = 0;
174                 }
175         }
176 #endif
177
178         return linktype;
179 }
180
181 if_info_t *
182 if_info_new(char *name, char *description)
183 {
184         if_info_t *if_info;
185
186         if_info = g_malloc(sizeof (if_info_t));
187         if_info->name = g_strdup(name);
188         if (description == NULL)
189                 if_info->description = NULL;
190         else
191                 if_info->description = g_strdup(description);
192     if_info->ip_addr = NULL;
193     if_info->loopback = FALSE;
194         return if_info;
195 }
196
197
198 /* get all ip address information from the given interface */
199 static void if_info_ip(if_info_t *if_info, pcap_if_t *d)
200 {
201   pcap_addr_t *a;
202   guint32       *ip_addr;
203
204   /* Loopback interface */
205   if_info->loopback = (d->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE;
206
207   /* All addresses */
208   for(a=d->addresses;a;a=a->next) {
209     switch(a->addr->sa_family)
210     {
211       /* IPv4 address */
212       case AF_INET:
213         if (a->addr) {
214               struct sockaddr_in *ai = ((struct sockaddr_in *)(a->addr));
215           ip_addr = g_malloc(sizeof(*ip_addr));
216           *ip_addr = *((guint32 *)&(ai->sin_addr.s_addr));
217           if_info->ip_addr = g_slist_append(if_info->ip_addr, ip_addr);
218         }
219         break;
220       default:
221         break;
222     }
223   }
224 }
225
226
227 #ifdef HAVE_PCAP_FINDALLDEVS
228 GList *
229 get_interface_list_findalldevs(int *err, char *err_str)
230 {
231         GList  *il = NULL;
232         pcap_if_t *alldevs, *dev;
233         if_info_t *if_info;
234
235         if (pcap_findalldevs(&alldevs, err_str) == -1) {
236                 *err = CANT_GET_INTERFACE_LIST;
237                 return NULL;
238         }
239
240         if (alldevs == NULL) {
241                 /*
242                  * No interfaces found.
243                  */
244                 *err = NO_INTERFACES_FOUND;
245                 return NULL;
246         }
247
248         for (dev = alldevs; dev != NULL; dev = dev->next) {
249                 if_info = if_info_new(dev->name, dev->description);
250                 il = g_list_append(il, if_info);
251                 if_info_ip(if_info, dev);
252         }
253         pcap_freealldevs(alldevs);
254
255         return il;
256 }
257 #endif /* HAVE_PCAP_FINDALLDEVS */
258
259 static void
260 free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
261 {
262     g_free(addr);
263 }
264
265 static void
266 free_if_cb(gpointer data, gpointer user_data _U_)
267 {
268         if_info_t *if_info = data;
269
270         g_free(if_info->name);
271         if (if_info->description != NULL)
272                 g_free(if_info->description);
273
274     g_slist_foreach(if_info->ip_addr, free_if_info_addr_cb, NULL);
275     g_slist_free(if_info->ip_addr);
276 }
277
278 void
279 free_interface_list(GList *if_list)
280 {
281         g_list_foreach(if_list, free_if_cb, NULL);
282         g_list_free(if_list);
283 }
284
285 /*
286  * Get the data-link types available for a libpcap device.
287  */
288 static data_link_info_t *
289 create_data_link_info(int dlt)
290 {
291         data_link_info_t *data_link_info;
292 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
293         const char *typename;
294 #endif
295         int wtap_encap;
296
297         data_link_info = g_malloc(sizeof (data_link_info_t));
298         data_link_info->dlt = dlt;
299 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
300         typename = pcap_datalink_val_to_name(dlt);
301         if (typename != NULL)
302                 data_link_info->name = g_strdup(typename);
303         else
304 #endif
305                 data_link_info->name = g_strdup_printf("DLT %d", dlt);
306         wtap_encap = wtap_pcap_encap_to_wtap_encap(dlt);
307         if (wtap_encap == WTAP_ENCAP_UNKNOWN) {
308                 /*
309                  * We don't support this in Wiretap.
310                  * However, we should, so you can capture on it.
311                  * Put in an entry for it, with no description.
312                  */
313                 data_link_info->description = NULL;
314         } else {
315                 /*
316                  * If this is null, that's a bug in
317                  * "wtap_pcap_encap_to_wtap_encap()" - it should always
318                  * return a valid encapsulation type - so we assume it's
319                  * not null.
320                  */
321                 data_link_info->description =
322                     g_strdup(wtap_encap_string(wtap_encap));
323         }
324         return data_link_info;
325 }
326
327 GList *
328 get_pcap_linktype_list(char *devname, char *err_buf)
329 {
330         GList *linktype_list = NULL;
331         pcap_t *pch;
332         int deflt;
333 #ifdef HAVE_PCAP_SET_DATALINK
334         int *linktypes;
335         int i, nlt;
336 #endif
337         data_link_info_t *data_link_info;
338
339         pch = pcap_open_live(devname, MIN_PACKET_SIZE, 0, 0, err_buf);
340         if (pch == NULL)
341                 return NULL;
342         err_buf[0] = '\0';      /* an empty list doesn't mean an error */
343         deflt = get_pcap_linktype(pch, devname);
344 #ifdef HAVE_PCAP_LIST_DATALINKS
345         nlt = pcap_list_datalinks(pch, &linktypes);
346         if (nlt == 0 || linktypes == NULL)
347                 return NULL;
348         for (i = 0; i < nlt; i++) {
349                 data_link_info = create_data_link_info(linktypes[i]);
350
351                 /*
352                  * XXX - for 802.11, make the most detailed 802.11
353                  * version the default, rather than the one the
354                  * device has as the default?
355                  */
356                 if (linktypes[i] == deflt)
357                         linktype_list = g_list_prepend(linktype_list,
358                             data_link_info);
359                 else
360                         linktype_list = g_list_append(linktype_list,
361                             data_link_info);
362         }
363         free(linktypes);
364 #else
365         data_link_info = create_data_link_info(deflt);
366         linktype_list = g_list_append(linktype_list, data_link_info);
367 #endif
368
369         pcap_close(pch);
370         return linktype_list;
371 }
372
373 static void
374 free_linktype_cb(gpointer data, gpointer user_data _U_)
375 {
376         data_link_info_t *linktype_info = data;
377
378         g_free(linktype_info->name);
379         if (linktype_info->description != NULL)
380                 g_free(linktype_info->description);
381 }
382
383 void
384 free_pcap_linktype_list(GList *linktype_list)
385 {
386         g_list_foreach(linktype_list, free_linktype_cb, NULL);
387         g_list_free(linktype_list);
388 }
389
390 /* Set the data link type on a pcap. */
391 const char *
392 set_pcap_linktype(pcap_t *pch, char *devname
393 #ifdef HAVE_PCAP_SET_DATALINK
394         _U_
395 #endif
396         , int dlt)
397 {
398 #ifdef HAVE_PCAP_SET_DATALINK
399         if (pcap_set_datalink(pch, dlt) == 0)
400                 return NULL;    /* no error */
401         return pcap_geterr(pch);
402 #else
403         /* Let them set it to the type it is; reject any other request. */
404         if (get_pcap_linktype(pch, devname) == dlt)
405                 return NULL;    /* no error */
406         return "That DLT is not one of the DLTs supported by this device";
407 #endif
408 }
409
410 #endif /* HAVE_LIBPCAP */