1 /* A cheap knock-off of the pcap library to remove that dependency. */
9 ofp_pcap_open(struct ofp_pcap *p, const char *fname, char *errbuf)
12 struct pcap_file_header hdr;
15 fp = fopen(fname, "r");
17 memset((char *)p, 0, sizeof(*p));
19 amt_read = fread((char *)&hdr, 1, sizeof(hdr), fp);
20 if (amt_read != sizeof(hdr)) {
21 snprintf(errbuf, OFP_PCAP_ERRBUF_SIZE, "error reading dump file");
25 if (hdr.magic != TCPDUMP_MAGIC) {
26 hdr.magic = SWAPLONG(hdr.magic);
27 hdr.version_major = SWAPSHORT(hdr.version_major);
28 hdr.version_minor = SWAPSHORT(hdr.version_minor);
29 hdr.thiszone = SWAPLONG(hdr.thiszone);
30 hdr.sigfigs = SWAPLONG(hdr.sigfigs);
31 hdr.snaplen = SWAPLONG(hdr.snaplen);
32 hdr.linktype = SWAPLONG(hdr.linktype);
39 p->bufsize = hdr.snaplen+sizeof(struct pcap_pkthdr);
40 p->buf = malloc(p->bufsize);
42 snprintf(errbuf, OFP_PCAP_ERRBUF_SIZE, "error allocating buffer");
46 if (hdr.version_major < OFP_PCAP_VERSION_MAJOR) {
47 snprintf(errbuf, OFP_PCAP_ERRBUF_SIZE, "archaic file format");
60 ofp_pcap_next(struct ofp_pcap *p, struct pcap_pkthdr *hdr)
64 amt_read = fread(hdr, 1, sizeof(*hdr), p->fp);
65 if (amt_read != sizeof(*hdr)) {
66 snprintf(p->errbuf, OFP_PCAP_ERRBUF_SIZE, "error reading dump file");
71 hdr->caplen = SWAPLONG(hdr->caplen);
72 hdr->len = SWAPLONG(hdr->len);
73 hdr->ts.tv_sec = SWAPLONG(hdr->ts.tv_sec);
74 hdr->ts.tv_usec = SWAPLONG(hdr->ts.tv_usec);
77 if (hdr->caplen > p->bufsize) {
78 snprintf(p->errbuf, OFP_PCAP_ERRBUF_SIZE, "error reading dump file");
82 amt_read = fread((char *)p->buf, 1, hdr->caplen, p->fp);
83 if (amt_read != hdr->caplen){
84 snprintf(p->errbuf, OFP_PCAP_ERRBUF_SIZE, "error reading dump file");
92 ofp_pcap_close(struct ofp_pcap *p)