/* Read packets from a capture file, and call the callback for each packet. If cnt > 0, return after 'cnt' packets, otherwise continue until eof.  */
int pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
/* Read packets from a capture file, and call the callback for each packet. If cnt > 0, return after 'cnt' packets, otherwise continue until eof.  */
int pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
{
	struct bpf_insn *fcode;
	int status = 0;
	int n = 0;
	u_char *data;
	while (status == 0) {
		struct pcap_pkthdr h;
		if (p->break_loop) {
			if (n == 0) {
				p->break_loop = 0;
				return (-2);
			} else
				return (n);
		}
		status = p->next_packet_op(p, &h, &data);
		if (status) {
			if (status == 1)
				return (0);
			return (status);
		}
		if ((fcode = p->fcode.bf_insns) == NULL ||
		    bpf_filter(fcode, data, h.len, h.caplen)) {
			(*callback)(user, &h, data);
			if (++n >= cnt && cnt > 0)
				break;
		}
	}
	return (n);
}