1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | #ifdef HAVE_PCAP |
---|
28 | #include "config.h" |
---|
29 | |
---|
30 | #ifndef HAVE_PCAP_NEXT_EX |
---|
31 | #include <stdio.h> |
---|
32 | #include <pcap.h> |
---|
33 | #ifdef HAVE_PCAP_INT_H |
---|
34 | # include <pcap-int.h> |
---|
35 | #endif |
---|
36 | #include <string.h> |
---|
37 | #include <libtrace.h> |
---|
38 | #include <stdlib.h> |
---|
39 | |
---|
40 | /* Custom implementation of pcap_next_ex as some versions of PCAP do not have |
---|
41 | * it */ |
---|
42 | |
---|
43 | struct pcap_data_t { |
---|
44 | struct pcap_pkthdr *header; |
---|
45 | u_char *payload; |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | static struct pcap_data_t pcap_data; |
---|
50 | |
---|
51 | static void trace_pcap_handler(u_char *user, const struct pcap_pkthdr *pcaphdr,const u_char *pcappkt) { |
---|
52 | struct pcap_data_t *packet = (struct pcap_data_t *)user; |
---|
53 | |
---|
54 | /* pcaphdr and pcappkt don't seem to persist for particularly long |
---|
55 | * so we need to memcpy them. Obviously, this spoils the whole |
---|
56 | * zero-copy concept but if you're using outdated pcap libraries |
---|
57 | * you deserve everything you get |
---|
58 | */ |
---|
59 | memcpy(packet->header, pcaphdr, sizeof(struct pcap_pkthdr)); |
---|
60 | memcpy(packet->payload, pcappkt, packet->header->len); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, |
---|
65 | const u_char **pkt_data) { |
---|
66 | |
---|
67 | int pcapbytes = 0; |
---|
68 | |
---|
69 | pcap_data.header = *pkt_header; |
---|
70 | pcap_data.payload = *pkt_data; |
---|
71 | |
---|
72 | pcapbytes = pcap_dispatch(p, 1, &trace_pcap_handler, |
---|
73 | (u_char *) &pcap_data); |
---|
74 | |
---|
75 | if (pcapbytes == -1) |
---|
76 | return -1; |
---|
77 | |
---|
78 | if (pcapbytes == 0 && pcap_file(p) != NULL) |
---|
79 | return -2; |
---|
80 | |
---|
81 | if (pcapbytes == 0) |
---|
82 | return 0; |
---|
83 | |
---|
84 | return 1; |
---|
85 | } |
---|
86 | #endif |
---|
87 | |
---|
88 | #endif //HAVE_PCAP |
---|