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