2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 #if defined(__sgi) && (IRIX > 602)
7 # include <sys/ptimers.h>
11 #if !defined(__SVR4) && !defined(__GNUC__)
14 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/ioctl.h>
21 #include <sys/param.h>
22 #include <netinet/in.h>
23 #include <netinet/in_systm.h>
25 #include <netinet/ip_var.h>
27 #include <netinet/ip.h>
28 #include <netinet/tcp.h>
30 #include "ip_compat.h"
31 #include <netinet/tcpip.h>
38 static const char rcsid
[] = "@(#)$Id: ipft_pc.c,v 2.2.2.5 2002/12/06 11:40:25 darrenr Exp $";
43 int lc_sz
; /* LLC header length */
44 int lc_to
; /* LLC Type offset */
45 int lc_tl
; /* LLC Type length */
49 * While many of these maybe the same, some do have different header formats
50 * which make this useful.
53 static struct llc llcs
[] = {
54 { DLT_NULL
, 0, 0, 0 },
55 { DLT_EN10MB
, 14, 12, 2 },
56 { DLT_EN3MB
, 0, 0, 0 },
57 { DLT_AX25
, 0, 0, 0 },
58 { DLT_PRONET
, 0, 0, 0 },
59 { DLT_CHAOS
, 0, 0, 0 },
60 { DLT_IEEE802
, 0, 0, 0 },
61 { DLT_ARCNET
, 0, 0, 0 },
62 { DLT_SLIP
, 0, 0, 0 },
64 { DLT_FDDI
, 0, 0, 0 },
66 { DLT_ATMRFC1483
, 0, 0, 0 },
73 { DLT_SLIP_BSDOS
, 0, 0, 0 },
76 { DLT_PPP_BSDOS
, 0, 0, 0 },
79 { DLT_HIPPI
, 0, 0, 0 },
82 { DLT_HDLC
, 0, 0, 0 },
85 { DLT_PPP_SERIAL
, 4, 4, 0 },
88 { DLT_PPP_ETHER
, 8, 8, 0 },
91 { DLT_ECONET
, 0, 0, 0 },
96 static int pcap_open
__P((char *));
97 static int pcap_close
__P((void));
98 static int pcap_readip
__P((char *, int, char **, int *));
99 static void swap_hdr
__P((pcaphdr_t
*));
100 static int pcap_read_rec
__P((struct pcap_pkthdr
*));
102 static int pfd
= -1, s_type
= -1, swapped
= 0;
103 static struct llc
*llcp
= NULL
;
105 struct ipread pcap
= { pcap_open
, pcap_close
, pcap_readip
};
107 #define SWAPLONG(y) \
108 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
109 #define SWAPSHORT(y) \
110 ( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
112 static void swap_hdr(p
)
115 p
->pc_v_maj
= SWAPSHORT(p
->pc_v_maj
);
116 p
->pc_v_min
= SWAPSHORT(p
->pc_v_min
);
117 p
->pc_zone
= SWAPLONG(p
->pc_zone
);
118 p
->pc_sigfigs
= SWAPLONG(p
->pc_sigfigs
);
119 p
->pc_slen
= SWAPLONG(p
->pc_slen
);
120 p
->pc_type
= SWAPLONG(p
->pc_type
);
123 static int pcap_open(fname
)
132 if (!strcmp(fname
, "-"))
134 else if ((fd
= open(fname
, O_RDONLY
)) == -1)
137 if (read(fd
, (char *)&ph
, sizeof(ph
)) != sizeof(ph
))
140 if (ph
.pc_id
!= TCPDUMP_MAGIC
) {
141 if (SWAPLONG(ph
.pc_id
) != TCPDUMP_MAGIC
) {
149 if (ph
.pc_v_maj
!= PCAP_VERSION_MAJ
) {
154 for (i
= 0; llcs
[i
].lc_type
!= -1; i
++)
155 if (llcs
[i
].lc_type
== ph
.pc_type
) {
167 printf("opened pcap file %s:\n", fname
);
168 printf("\tid: %08x version: %d.%d type: %d snap %d\n",
169 ph
.pc_id
, ph
.pc_v_maj
, ph
.pc_v_min
, ph
.pc_type
, ph
.pc_slen
);
175 static int pcap_close()
182 * read in the header (and validate) which should be the first record
185 static int pcap_read_rec(rec
)
186 struct pcap_pkthdr
*rec
;
190 if (read(pfd
, (char *)rec
, sizeof(*rec
)) != sizeof(*rec
))
194 rec
->ph_clen
= SWAPLONG(rec
->ph_clen
);
195 rec
->ph_len
= SWAPLONG(rec
->ph_len
);
196 rec
->ph_ts
.tv_sec
= SWAPLONG(rec
->ph_ts
.tv_sec
);
197 rec
->ph_ts
.tv_usec
= SWAPLONG(rec
->ph_ts
.tv_usec
);
200 n
= MIN(p
, rec
->ph_len
);
210 * read an entire pcap packet record. only the data part is copied into
211 * the available buffer, with the number of bytes copied returned.
213 static int pcap_read(buf
, cnt
)
217 struct pcap_pkthdr rec
;
218 static char *bufp
= NULL
;
221 if ((i
= pcap_read_rec(&rec
)) <= 0)
227 bufp
= realloc(bufp
, i
);
229 if (read(pfd
, bufp
, i
) != i
)
240 * return only an IP packet read into buf
242 static int pcap_readip(buf
, cnt
, ifn
, dir
)
246 static char *bufp
= NULL
;
247 struct pcap_pkthdr rec
;
255 if ((i
= pcap_read_rec(&rec
)) <= 0)
261 bufp
= realloc(bufp
, i
);
264 if (read(pfd
, s
, i
) != i
)
269 bcopy(s
, ty
, l
->lc_tl
);
271 /* } while (ty[0] != 0x8 && ty[1] != 0); */