Fetch an indication of whether the interface supports capturing in
[obnox/wireshark/wip.git] / capture-pcap-util.c
1 /* capture-pcap-util.c
2  * Utility routines for packet capture
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 <stdio.h>
35 #include <limits.h>
36 #include <string.h>
37
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
44 #endif
45
46 #include <wtap.h>
47 #include <libpcap.h>
48
49 #include "capture_ifinfo.h"
50 #include "capture-pcap-util.h"
51 #include "capture-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, const char *devname
65 #ifndef _AIX
66         _U_
67 #endif
68 )
69 {
70         int linktype;
71 #ifdef _AIX
72         const 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 Wireshark 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, Wireshark,
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, Wireshark, 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 = (if_info_t *)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->addrs = NULL;
193         if_info->loopback = FALSE;
194         return if_info;
195 }
196
197 void
198 if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
199 {
200         if_addr_t *if_addr;
201         struct sockaddr_in *ai;
202 #ifdef INET6
203         struct sockaddr_in6 *ai6;
204 #endif
205
206         switch (addr->sa_family) {
207
208         case AF_INET:
209                 ai = (struct sockaddr_in *)addr;
210                 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
211                 if_addr->ifat_type = IF_AT_IPv4;
212                 if_addr->addr.ip4_addr =
213                     *((guint32 *)&(ai->sin_addr.s_addr));
214                 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
215                 break;
216
217 #ifdef INET6
218         case AF_INET6:
219                 ai6 = (struct sockaddr_in6 *)addr;
220                 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
221                 if_addr->ifat_type = IF_AT_IPv6;
222                 memcpy((void *)&if_addr->addr.ip6_addr,
223                     (void *)&ai6->sin6_addr.s6_addr,
224                     sizeof if_addr->addr.ip6_addr);
225                 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
226                 break;
227 #endif
228         }
229 }
230
231 #ifdef HAVE_PCAP_FINDALLDEVS
232 /*
233  * Get all IP address information, and the loopback flag, for the given
234  * interface.
235  */
236 static void
237 if_info_ip(if_info_t *if_info, pcap_if_t *d)
238 {
239         pcap_addr_t *a;
240
241         /* Loopback flag */
242         if_info->loopback = (d->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE;
243
244         /* All addresses */
245         for (a = d->addresses; a != NULL; a = a->next) {
246                 if (a->addr != NULL)
247                         if_info_add_address(if_info, a->addr);
248         }
249 }
250
251 #ifdef HAVE_PCAP_REMOTE
252 GList *
253 get_interface_list_findalldevs_ex(const char *source,
254                                   struct pcap_rmtauth *auth,
255                                   int *err, char **err_str)
256 {
257         GList  *il = NULL;
258         pcap_if_t *alldevs, *dev;
259         if_info_t *if_info;
260         char errbuf[PCAP_ERRBUF_SIZE];
261
262         if (pcap_findalldevs_ex((char *)source, auth, &alldevs, errbuf) == -1) {
263                 *err = CANT_GET_INTERFACE_LIST;
264                 if (err_str != NULL)
265                         *err_str = cant_get_if_list_error_message(errbuf);
266                 return NULL;
267         }
268
269         if (alldevs == NULL) {
270                 /*
271                  * No interfaces found.
272                  */
273                 *err = NO_INTERFACES_FOUND;
274                 if (err_str != NULL)
275                         *err_str = NULL;
276                 return NULL;
277         }
278
279         for (dev = alldevs; dev != NULL; dev = dev->next) {
280                 if_info = if_info_new(dev->name, dev->description);
281                 il = g_list_append(il, if_info);
282                 if_info_ip(if_info, dev);
283         }
284         pcap_freealldevs(alldevs);
285
286         return il;
287 }
288 #endif
289
290 GList *
291 get_interface_list_findalldevs(int *err, char **err_str)
292 {
293         GList  *il = NULL;
294         pcap_if_t *alldevs, *dev;
295         if_info_t *if_info;
296         char errbuf[PCAP_ERRBUF_SIZE];
297
298         if (pcap_findalldevs(&alldevs, errbuf) == -1) {
299                 *err = CANT_GET_INTERFACE_LIST;
300                 if (err_str != NULL)
301                         *err_str = cant_get_if_list_error_message(errbuf);
302                 return NULL;
303         }
304
305         if (alldevs == NULL) {
306                 /*
307                  * No interfaces found.
308                  */
309                 *err = NO_INTERFACES_FOUND;
310                 if (err_str != NULL)
311                         *err_str = NULL;
312                 return NULL;
313         }
314
315         for (dev = alldevs; dev != NULL; dev = dev->next) {
316                 if_info = if_info_new(dev->name, dev->description);
317                 il = g_list_append(il, if_info);
318                 if_info_ip(if_info, dev);
319         }
320         pcap_freealldevs(alldevs);
321
322         return il;
323 }
324 #endif /* HAVE_PCAP_FINDALLDEVS */
325
326 static void
327 free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
328 {
329         g_free(addr);
330 }
331
332 static void
333 free_if_cb(gpointer data, gpointer user_data _U_)
334 {
335         if_info_t *if_info = (if_info_t *)data;
336
337         g_free(if_info->name);
338         g_free(if_info->description);
339
340         g_slist_foreach(if_info->addrs, free_if_info_addr_cb, NULL);
341         g_slist_free(if_info->addrs);
342         g_free(if_info);
343 }
344
345 void
346 free_interface_list(GList *if_list)
347 {
348         g_list_foreach(if_list, free_if_cb, NULL);
349         g_list_free(if_list);
350 }
351
352 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
353 struct dlt_choice {
354         const char *name;
355         const char *description;
356         int     dlt;
357 };
358
359 #define DLT_CHOICE(code, description) { #code, description, code }
360 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
361
362 static struct dlt_choice dlt_choices[] = {
363         DLT_CHOICE(DLT_NULL, "BSD loopback"),
364         DLT_CHOICE(DLT_EN10MB, "Ethernet"),
365         DLT_CHOICE(DLT_IEEE802, "Token ring"),
366         DLT_CHOICE(DLT_ARCNET, "ARCNET"),
367         DLT_CHOICE(DLT_SLIP, "SLIP"),
368         DLT_CHOICE(DLT_PPP, "PPP"),
369         DLT_CHOICE(DLT_FDDI, "FDDI"),
370         DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
371         DLT_CHOICE(DLT_RAW, "Raw IP"),
372         DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
373         DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
374         DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
375         DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
376         DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
377         DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
378         DLT_CHOICE(DLT_IEEE802_11, "802.11"),
379         DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
380         DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
381         DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
382         DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
383         DLT_CHOICE(DLT_LTALK, "Localtalk"),
384         DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
385         DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
386         DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
387         DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
388         DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
389         DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
390         DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
391         DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
392         DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
393         DLT_CHOICE_SENTINEL
394 };
395
396 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL)
397 static int
398 pcap_datalink_name_to_val(const char *name)
399 {
400         int i;
401
402         for (i = 0; dlt_choices[i].name != NULL; i++) {
403                 if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
404                     name) == 0)
405                         return (dlt_choices[i].dlt);
406         }
407         return (-1);
408 }
409 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) */
410
411 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
412 static const char *
413 pcap_datalink_val_to_name(int dlt)
414 {
415         int i;
416
417         for (i = 0; dlt_choices[i].name != NULL; i++) {
418                 if (dlt_choices[i].dlt == dlt)
419                         return (dlt_choices[i].name + sizeof("DLT_") - 1);
420         }
421         return (NULL);
422 }
423 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
424
425 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
426 const char *
427 pcap_datalink_val_to_description(int dlt)
428 {
429         int i;
430
431         for (i = 0; dlt_choices[i].name != NULL; i++) {
432                 if (dlt_choices[i].dlt == dlt)
433                         return (dlt_choices[i].description);
434         }
435         return (NULL);
436 }
437 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
438
439 #endif /* !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
440
441 static void
442 free_linktype_cb(gpointer data, gpointer user_data _U_)
443 {
444         data_link_info_t *linktype_info = (data_link_info_t *)data;
445
446         g_free(linktype_info->name);
447         g_free(linktype_info->description);
448 }
449
450 void
451 free_if_capabilities(if_capabilities_t *caps)
452 {
453         g_list_foreach(caps->data_link_types, free_linktype_cb, NULL);
454         g_list_free(caps->data_link_types);
455         g_free(caps);
456 }
457
458 const char *
459 linktype_val_to_name(int dlt)
460 {
461     return pcap_datalink_val_to_name(dlt);
462 }
463
464 int linktype_name_to_val(const char *linktype)
465 {
466     return pcap_datalink_name_to_val(linktype);
467 }
468
469 #endif /* HAVE_LIBPCAP */