2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * $Id: ipft_pc.c,v 1.10 2004/02/07 18:17:40 darrenr Exp $
13 static const char rcsid
[] = "@(#)$Id: ipft_pc.c,v 1.10 2004/02/07 18:17:40 darrenr Exp $";
17 int lc_sz
; /* LLC header length */
18 int lc_to
; /* LLC Type offset */
19 int lc_tl
; /* LLC Type length */
23 * While many of these maybe the same, some do have different header formats
24 * which make this useful.
27 static struct llc llcs
[] = {
28 { DLT_NULL
, 0, 0, 0 },
29 { DLT_EN10MB
, 14, 12, 2 },
30 { DLT_EN3MB
, 0, 0, 0 },
31 { DLT_AX25
, 0, 0, 0 },
32 { DLT_PRONET
, 0, 0, 0 },
33 { DLT_CHAOS
, 0, 0, 0 },
34 { DLT_IEEE802
, 0, 0, 0 },
35 { DLT_ARCNET
, 0, 0, 0 },
36 { DLT_SLIP
, 0, 0, 0 },
38 { DLT_FDDI
, 0, 0, 0 },
40 { DLT_ATMRFC1483
, 0, 0, 0 },
47 { DLT_SLIP_BSDOS
, 0, 0, 0 },
50 { DLT_PPP_BSDOS
, 0, 0, 0 },
53 { DLT_HIPPI
, 0, 0, 0 },
56 { DLT_HDLC
, 0, 0, 0 },
59 { DLT_PPP_SERIAL
, 4, 4, 0 },
62 { DLT_PPP_ETHER
, 8, 8, 0 },
65 { DLT_ECONET
, 0, 0, 0 },
70 static int pcap_open
__P((char *));
71 static int pcap_close
__P((void));
72 static int pcap_readip
__P((char *, int, char **, int *));
73 static void swap_hdr
__P((pcaphdr_t
*));
74 static int pcap_read_rec
__P((struct pcap_pkthdr
*));
76 static int pfd
= -1, swapped
= 0;
77 static struct llc
*llcp
= NULL
;
79 struct ipread pcap
= { pcap_open
, pcap_close
, pcap_readip
, 0 };
82 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
83 #define SWAPSHORT(y) \
84 ( (((y)&0xff)<<8) | (((y)&0xff00)>>8) )
86 static void swap_hdr(p
)
89 p
->pc_v_maj
= SWAPSHORT(p
->pc_v_maj
);
90 p
->pc_v_min
= SWAPSHORT(p
->pc_v_min
);
91 p
->pc_zone
= SWAPLONG(p
->pc_zone
);
92 p
->pc_sigfigs
= SWAPLONG(p
->pc_sigfigs
);
93 p
->pc_slen
= SWAPLONG(p
->pc_slen
);
94 p
->pc_type
= SWAPLONG(p
->pc_type
);
97 static int pcap_open(fname
)
106 if (!strcmp(fname
, "-"))
108 else if ((fd
= open(fname
, O_RDONLY
)) == -1)
111 if (read(fd
, (char *)&ph
, sizeof(ph
)) != sizeof(ph
))
114 if (ph
.pc_id
!= TCPDUMP_MAGIC
) {
115 if (SWAPLONG(ph
.pc_id
) != TCPDUMP_MAGIC
) {
123 if (ph
.pc_v_maj
!= PCAP_VERSION_MAJ
) {
128 for (i
= 0; llcs
[i
].lc_type
!= -1; i
++)
129 if (llcs
[i
].lc_type
== ph
.pc_type
) {
140 printf("opened pcap file %s:\n", fname
);
141 printf("\tid: %08x version: %d.%d type: %d snap %d\n",
142 ph
.pc_id
, ph
.pc_v_maj
, ph
.pc_v_min
, ph
.pc_type
, ph
.pc_slen
);
148 static int pcap_close()
155 * read in the header (and validate) which should be the first record
158 static int pcap_read_rec(rec
)
159 struct pcap_pkthdr
*rec
;
163 if (read(pfd
, (char *)rec
, sizeof(*rec
)) != sizeof(*rec
))
167 rec
->ph_clen
= SWAPLONG(rec
->ph_clen
);
168 rec
->ph_len
= SWAPLONG(rec
->ph_len
);
169 rec
->ph_ts
.tv_sec
= SWAPLONG(rec
->ph_ts
.tv_sec
);
170 rec
->ph_ts
.tv_usec
= SWAPLONG(rec
->ph_ts
.tv_usec
);
173 n
= MIN(p
, rec
->ph_len
);
183 * read an entire pcap packet record. only the data part is copied into
184 * the available buffer, with the number of bytes copied returned.
186 static int pcap_read(buf
, cnt
)
190 struct pcap_pkthdr rec
;
191 static char *bufp
= NULL
;
194 if ((i
= pcap_read_rec(&rec
)) <= 0)
200 bufp
= realloc(bufp
, i
);
202 if (read(pfd
, bufp
, i
) != i
)
213 * return only an IP packet read into buf
215 static int pcap_readip(buf
, cnt
, ifn
, dir
)
219 static char *bufp
= NULL
;
220 struct pcap_pkthdr rec
;
228 if ((i
= pcap_read_rec(&rec
)) <= 0)
234 bufp
= realloc(bufp
, i
);
237 if (read(pfd
, s
, i
) != i
)
242 bcopy(s
, ty
, l
->lc_tl
);
244 /* } while (ty[0] != 0x8 && ty[1] != 0); */