Add "(BE)" to the big endian representations of the ICMP identifier and
[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 if_info_t *
58 if_info_new(char *name, char *description)
59 {
60         if_info_t *if_info;
61
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;
66         else
67                 if_info->description = g_strdup(description);
68         if_info->addrs = NULL;
69         if_info->loopback = FALSE;
70         return if_info;
71 }
72
73 void
74 if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
75 {
76         if_addr_t *if_addr;
77         struct sockaddr_in *ai;
78 #ifdef INET6
79         struct sockaddr_in6 *ai6;
80 #endif
81
82         switch (addr->sa_family) {
83
84         case AF_INET:
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);
91                 break;
92
93 #ifdef INET6
94         case AF_INET6:
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);
102                 break;
103 #endif
104         }
105 }
106
107 #ifdef HAVE_PCAP_FINDALLDEVS
108 /*
109  * Get all IP address information, and the loopback flag, for the given
110  * interface.
111  */
112 static void
113 if_info_ip(if_info_t *if_info, pcap_if_t *d)
114 {
115         pcap_addr_t *a;
116
117         /* Loopback flag */
118         if_info->loopback = (d->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE;
119
120         /* All addresses */
121         for (a = d->addresses; a != NULL; a = a->next) {
122                 if (a->addr != NULL)
123                         if_info_add_address(if_info, a->addr);
124         }
125 }
126
127 #ifdef HAVE_PCAP_REMOTE
128 GList *
129 get_interface_list_findalldevs_ex(const char *source,
130                                   struct pcap_rmtauth *auth,
131                                   int *err, char **err_str)
132 {
133         GList  *il = NULL;
134         pcap_if_t *alldevs, *dev;
135         if_info_t *if_info;
136         char errbuf[PCAP_ERRBUF_SIZE];
137
138         if (pcap_findalldevs_ex((char *)source, auth, &alldevs, errbuf) == -1) {
139                 *err = CANT_GET_INTERFACE_LIST;
140                 if (err_str != NULL)
141                         *err_str = cant_get_if_list_error_message(errbuf);
142                 return NULL;
143         }
144
145         if (alldevs == NULL) {
146                 /*
147                  * No interfaces found.
148                  */
149                 *err = NO_INTERFACES_FOUND;
150                 if (err_str != NULL)
151                         *err_str = NULL;
152                 return NULL;
153         }
154
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);
159         }
160         pcap_freealldevs(alldevs);
161
162         return il;
163 }
164 #endif
165
166 GList *
167 get_interface_list_findalldevs(int *err, char **err_str)
168 {
169         GList  *il = NULL;
170         pcap_if_t *alldevs, *dev;
171         if_info_t *if_info;
172         char errbuf[PCAP_ERRBUF_SIZE];
173
174         if (pcap_findalldevs(&alldevs, errbuf) == -1) {
175                 *err = CANT_GET_INTERFACE_LIST;
176                 if (err_str != NULL)
177                         *err_str = cant_get_if_list_error_message(errbuf);
178                 return NULL;
179         }
180
181         if (alldevs == NULL) {
182                 /*
183                  * No interfaces found.
184                  */
185                 *err = NO_INTERFACES_FOUND;
186                 if (err_str != NULL)
187                         *err_str = NULL;
188                 return NULL;
189         }
190
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);
195         }
196         pcap_freealldevs(alldevs);
197
198         return il;
199 }
200 #endif /* HAVE_PCAP_FINDALLDEVS */
201
202 static void
203 free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
204 {
205         g_free(addr);
206 }
207
208 static void
209 free_if_cb(gpointer data, gpointer user_data _U_)
210 {
211         if_info_t *if_info = (if_info_t *)data;
212
213         g_free(if_info->name);
214         g_free(if_info->description);
215
216         g_slist_foreach(if_info->addrs, free_if_info_addr_cb, NULL);
217         g_slist_free(if_info->addrs);
218         g_free(if_info);
219 }
220
221 void
222 free_interface_list(GList *if_list)
223 {
224         g_list_foreach(if_list, free_if_cb, NULL);
225         g_list_free(if_list);
226 }
227
228 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
229 struct dlt_choice {
230         const char *name;
231         const char *description;
232         int     dlt;
233 };
234
235 #define DLT_CHOICE(code, description) { #code, description, code }
236 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
237
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"),
269         DLT_CHOICE_SENTINEL
270 };
271
272 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL)
273 static int
274 pcap_datalink_name_to_val(const char *name)
275 {
276         int i;
277
278         for (i = 0; dlt_choices[i].name != NULL; i++) {
279                 if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
280                     name) == 0)
281                         return (dlt_choices[i].dlt);
282         }
283         return (-1);
284 }
285 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) */
286
287 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
288 static const char *
289 pcap_datalink_val_to_name(int dlt)
290 {
291         int i;
292
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);
296         }
297         return (NULL);
298 }
299 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
300
301 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
302 const char *
303 pcap_datalink_val_to_description(int dlt)
304 {
305         int i;
306
307         for (i = 0; dlt_choices[i].name != NULL; i++) {
308                 if (dlt_choices[i].dlt == dlt)
309                         return (dlt_choices[i].description);
310         }
311         return (NULL);
312 }
313 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
314
315 #endif /* !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
316
317 static void
318 free_linktype_cb(gpointer data, gpointer user_data _U_)
319 {
320         data_link_info_t *linktype_info = (data_link_info_t *)data;
321
322         g_free(linktype_info->name);
323         g_free(linktype_info->description);
324 }
325
326 void
327 free_if_capabilities(if_capabilities_t *caps)
328 {
329         g_list_foreach(caps->data_link_types, free_linktype_cb, NULL);
330         g_list_free(caps->data_link_types);
331         g_free(caps);
332 }
333
334 const char *
335 linktype_val_to_name(int dlt)
336 {
337     return pcap_datalink_val_to_name(dlt);
338 }
339
340 int linktype_name_to_val(const char *linktype)
341 {
342     return pcap_datalink_name_to_val(linktype);
343 }
344
345 #endif /* HAVE_LIBPCAP */