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