All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pcap.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 #ifndef _FR_PCAP_H
17 #define _FR_PCAP_H
18 #ifdef HAVE_LIBPCAP
19 /**
20  * $Id: 15220776747c682f4f947d159e8362faa7736445 $
21  *
22  * @file include/pcap.h
23  * @brief Prototypes and constants for PCAP functions.
24  *
25  * @author Arran Cudbard-Bell <a.cudbardb@freeradius.org>
26  * @copyright 2013 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
27  */
28 RCSIDH(pcap_h, "$Id: 15220776747c682f4f947d159e8362faa7736445 $")
29 
30 #include <freeradius-devel/libradius.h>
31 #include <freeradius-devel/net.h>
32 
33 #include <sys/types.h>
34 #include <pcap.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #define SNAPLEN ETHER_HDR_LEN + IP_HDR_LEN + sizeof(struct udp_header) + MAX_RADIUS_LEN
41 #define PCAP_BUFFER_DEFAULT (10000)
42 /*
43  * It's unclear why this differs between platforms
44  */
45 #ifndef __linux__
46 # define PCAP_NONBLOCK_TIMEOUT (0)
47 #else
48 # define PCAP_NONBLOCK_TIMEOUT (-1)
49 #endif
50 
51 #ifndef BIOCIMMEDIATE
52 # define BIOCIMMEDIATE (2147762800)
53 #endif
54 
55 /*
56  * Older versions of libpcap don't define this
57  */
58 #ifndef PCAP_NETMASK_UNKNOWN
59 # define PCAP_NETMASK_UNKNOWN 0
60 #endif
61 
62 typedef enum {
63  PCAP_INVALID = 0,
64  PCAP_INTERFACE_IN,
65  PCAP_FILE_IN,
66  PCAP_STDIO_IN,
67  PCAP_INTERFACE_OUT,
68  PCAP_FILE_OUT,
69  PCAP_STDIO_OUT,
70  PCAP_INTERFACE_IN_OUT
71 } fr_pcap_type_t;
72 
73 /*
74  * Internal pcap structures
75  */
76 typedef struct fr_pcap fr_pcap_t;
77 struct fr_pcap {
78  char errbuf[PCAP_ERRBUF_SIZE]; //!< Last error on this interface.
79  fr_pcap_type_t type; //!< What type of handle this is.
80  char *name; //!< Name of file or interface.
81  uint8_t ether_addr[ETHER_ADDR_LEN]; //!< The MAC address of the interface
82 
83  bool promiscuous; //!< Whether the interface is in promiscuous mode.
84  //!< Only valid for live capture handles.
85  int buffer_pkts; //!< How big to make the PCAP ring buffer.
86  //!< Actual buffer size is SNAPLEN * buffer.
87  //!< Only valid for live capture handles.
88 
89  pcap_t *handle; //!< libpcap handle.
90  pcap_dumper_t *dumper; //!< libpcap dumper handle.
91 
92  int link_layer; //!< Link layer type.
93 
94  int fd; //!< Selectable file descriptor we feed to select.
95  struct pcap_stat pstats; //!< The last set of pcap stats for this handle.
96 
97  fr_pcap_t *next; //!< Next handle in collection.
98 };
99 
100 int fr_pcap_if_link_layer(char *errbuff, pcap_if_t *dev);
101 fr_pcap_t *fr_pcap_init(TALLOC_CTX *ctx, char const *name, fr_pcap_type_t type);
102 int fr_pcap_open(fr_pcap_t *handle);
103 int fr_pcap_apply_filter(fr_pcap_t *handle, char const *expression);
104 char *fr_pcap_device_names(TALLOC_CTX *ctx, fr_pcap_t *handle, char c);
105 int fr_pcap_mac_addr(uint8_t *macaddr, char *ifname);
106 #endif
107 
108 #ifdef __cplusplus
109 }
110 #endif
111 #endif /* _PCAP_H */
#define RCSIDH(h, id)
Definition: build.h:136
static char const * name
#define ETHER_ADDR_LEN
Definition: net.h:53