2 * Copyright (C) 1993-1998 by Darren Reed.
4 * Redistribution and use in source and binary forms are permitted
5 * provided that this notice is preserved and due credit is given
6 * to the original author and the contributors.
10 #if !defined(__SVR4) && !defined(__GNUC__)
13 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/ioctl.h>
20 #include <sys/param.h>
21 #include <netinet/in.h>
22 #include <netinet/in_systm.h>
24 #include <netinet/ip_var.h>
26 #include <netinet/ip.h>
27 #include <netinet/tcp.h>
29 #include "ip_compat.h"
30 #include <netinet/tcpip.h>
36 static const char rcsid[] = "@(#)$Id: ipft_pc.c,v 2.1 1999/08/04 17:30:03 darrenr Exp $";
40 int lc_sz; /* LLC header length */
41 int lc_to; /* LLC Type offset */
42 int lc_tl; /* LLC Type length */
46 * While many of these maybe the same, some do have different header formats
47 * which make this useful.
51 static struct llc llcs[DLT_MAX+1] = {
52 { 0, 0, 0 }, /* DLT_NULL */
53 { 14, 12, 2 }, /* DLT_E10MB */
54 { 0, 0, 0 }, /* DLT_EN3MB */
55 { 0, 0, 0 }, /* DLT_AX25 */
56 { 0, 0, 0 }, /* DLT_PRONET */
57 { 0, 0, 0 }, /* DLT_CHAOS */
58 { 0, 0, 0 }, /* DLT_IEEE802 */
59 { 0, 0, 0 }, /* DLT_ARCNET */
60 { 0, 0, 0 }, /* DLT_SLIP */
61 { 0, 0, 0 }, /* DLT_PPP */
62 { 0, 0, 0 } /* DLT_FDDI */
65 static int pcap_open __P((char *));
66 static int pcap_close __P((void));
67 static int pcap_readip __P((char *, int, char **, int *));
68 static void swap_hdr __P((pcaphdr_t *));
69 static int pcap_read_rec __P((struct pcap_pkthdr *));
71 static int pfd = -1, s_type = -1, swapped = 0;
73 struct ipread pcap = { pcap_open, pcap_close, pcap_readip };
76 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
77 #define SWAPSHORT(y) \
78 ( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
80 static void swap_hdr(p)
83 p->pc_v_maj = SWAPSHORT(p->pc_v_maj);
84 p->pc_v_min = SWAPSHORT(p->pc_v_min);
85 p->pc_zone = SWAPLONG(p->pc_zone);
86 p->pc_sigfigs = SWAPLONG(p->pc_sigfigs);
87 p->pc_slen = SWAPLONG(p->pc_slen);
88 p->pc_type = SWAPLONG(p->pc_type);
91 static int pcap_open(fname)
100 if (!strcmp(fname, "-"))
102 else if ((fd = open(fname, O_RDONLY)) == -1)
105 if (read(fd, (char *)&ph, sizeof(ph)) != sizeof(ph))
108 if (ph.pc_id != TCPDUMP_MAGIC) {
109 if (SWAPLONG(ph.pc_id) != TCPDUMP_MAGIC) {
117 if (ph.pc_v_maj != PCAP_VERSION_MAJ || ph.pc_type > DLT_MAX) {
124 printf("opened pcap file %s:\n", fname);
125 printf("\tid: %08x version: %d.%d type: %d snap %d\n",
126 ph.pc_id, ph.pc_v_maj, ph.pc_v_min, ph.pc_type, ph.pc_slen);
132 static int pcap_close()
139 * read in the header (and validate) which should be the first record
142 static int pcap_read_rec(rec)
143 struct pcap_pkthdr *rec;
147 if (read(pfd, (char *)rec, sizeof(*rec)) != sizeof(*rec))
151 rec->ph_clen = SWAPLONG(rec->ph_clen);
152 rec->ph_len = SWAPLONG(rec->ph_len);
153 rec->ph_ts.tv_sec = SWAPLONG(rec->ph_ts.tv_sec);
154 rec->ph_ts.tv_usec = SWAPLONG(rec->ph_ts.tv_usec);
157 n = MIN(p, rec->ph_len);
167 * read an entire pcap packet record. only the data part is copied into
168 * the available buffer, with the number of bytes copied returned.
170 static int pcap_read(buf, cnt)
174 struct pcap_pkthdr rec;
175 static char *bufp = NULL;
178 if ((i = pcap_read_rec(&rec)) <= 0)
184 bufp = realloc(bufp, i);
186 if (read(pfd, bufp, i) != i)
197 * return only an IP packet read into buf
199 static int pcap_readip(buf, cnt, ifn, dir)
203 static char *bufp = NULL;
204 struct pcap_pkthdr rec;
210 if ((i = pcap_read_rec(&rec)) <= 0)
216 bufp = realloc(bufp, i);
219 if (read(pfd, s, i) != i)
225 bcopy(s, ty, l->lc_tl);
227 } while (ty[0] != 0x8 && ty[1] != 0);