source: lib/pcap_next_ex.c @ 25024fd

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 25024fd was 5551415, checked in by Daniel Lawson <dlawson@…>, 15 years ago
  • wrapping lib/pcap*c with #ifdef HAVE_PCAP so they don't build if we don't have pcap
  • changed some types and defines in linktypes.c to use libtrace internal representations rather than libpcap's, in case we don't have pcap.
  • Property mode set to 100644
File size: 1.3 KB
Line 
1#ifdef HAVE_PCAP
2#include "config.h"
3
4#ifndef HAVE_PCAP_NEXT_EX
5#include <stdio.h>
6#include <pcap.h>
7#ifdef HAVE_PCAP_INT_H
8# include <pcap-int.h>
9#endif
10#include <string.h>
11#include <libtrace.h>
12#include <stdlib.h>
13
14struct pcap_data_t {
15        struct pcap_pkthdr *header;
16        u_char *payload;
17};
18
19
20static struct pcap_data_t pcap_data;
21
22static void trace_pcap_handler(u_char *user, const struct pcap_pkthdr *pcaphdr,const u_char *pcappkt) {
23        struct pcap_data_t *packet = (struct pcap_data_t *)user;
24
25        /* pcaphdr and pcappkt don't seem to persist for particularly long
26         * so we need to memcpy them. Obviously, this spoils the whole
27         * zero-copy concept but if you're using outdated pcap libraries
28         * you deserve everything you get
29         */
30        memcpy(packet->header, pcaphdr, sizeof(struct pcap_pkthdr));
31        memcpy(packet->payload, pcappkt, packet->header->len);
32}
33
34
35int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
36                        const u_char **pkt_data) {
37
38        int pcapbytes = 0;
39
40        pcap_data.header = *pkt_header;
41        pcap_data.payload = *pkt_data;
42
43        pcapbytes = pcap_dispatch(p, 1, &trace_pcap_handler,
44                       (u_char *) &pcap_data);
45
46        if (pcapbytes == -1)
47                return -1;
48
49        if (pcapbytes == 0 && pcap_file(p) != NULL)
50                return -2;
51
52        if (pcapbytes == 0)
53                return 0;
54
55        return 1;
56}
57#endif
58
59#endif //HAVE_PCAP
Note: See TracBrowser for help on using the repository browser.