Andrew Jeffery | cad4730 | 2021-08-20 21:37:57 +0930 | [diff] [blame] | 1 | #include "utils/mctp-capture.h" |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <sys/time.h> |
| 5 | |
| 6 | int capture_init(void) |
| 7 | { |
| 8 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 9 | int rc; |
| 10 | |
| 11 | if ((rc = pcap_init(PCAP_CHAR_ENC_UTF_8, errbuf)) == -1) { |
| 12 | fprintf(stderr, "pcap_init: %s\n", errbuf); |
| 13 | return -1; |
| 14 | } |
| 15 | |
| 16 | return 0; |
| 17 | } |
| 18 | |
| 19 | int capture_prepare(struct capture *cap) |
| 20 | { |
| 21 | int rc; |
| 22 | |
| 23 | if (cap->linktype < CAPTURE_LINKTYPE_FIRST || |
| 24 | cap->linktype > CAPTURE_LINKTYPE_LAST) { |
| 25 | fprintf(stderr, |
| 26 | "Invalid private linktype value %d: see https://www.tcpdump.org/linktypes.html\n", |
| 27 | cap->linktype); |
| 28 | return -1; |
| 29 | } |
| 30 | |
| 31 | if (!(cap->pcap = pcap_open_dead(cap->linktype, UINT16_MAX))) { |
| 32 | fprintf(stderr, "pcap_open_dead: failed\n"); |
| 33 | return -1; |
| 34 | } |
| 35 | |
| 36 | if (!(cap->dumper = pcap_dump_open(cap->pcap, cap->path))) { |
| 37 | fprintf(stderr, "pcap_dump_open: failed\n"); |
| 38 | return -1; |
| 39 | } |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | void capture_close(struct capture *cap) |
| 45 | { |
| 46 | pcap_dump_close(cap->dumper); |
| 47 | |
| 48 | pcap_close(cap->pcap); |
| 49 | } |
| 50 | |
| 51 | void capture_binding(struct mctp_pktbuf *pkt, void *user) |
| 52 | { |
| 53 | pcap_dumper_t *dumper = user; |
| 54 | struct pcap_pkthdr hdr; |
| 55 | int rc; |
| 56 | |
| 57 | if ((rc = gettimeofday(&hdr.ts, NULL)) == -1) |
| 58 | return; |
| 59 | |
| 60 | hdr.caplen = mctp_pktbuf_size(pkt); |
| 61 | hdr.len = mctp_pktbuf_size(pkt); |
| 62 | |
| 63 | pcap_dump((u_char *)dumper, &hdr, (const u_char *)mctp_pktbuf_hdr(pkt)); |
| 64 | } |
| 65 | |
| 66 | void capture_socket(pcap_dumper_t *dumper, const void *buf, size_t len) |
| 67 | { |
| 68 | struct pcap_pkthdr hdr; |
| 69 | int rc; |
| 70 | |
| 71 | if ((rc = gettimeofday(&hdr.ts, NULL)) == -1) |
| 72 | return; |
| 73 | |
| 74 | hdr.caplen = len; |
| 75 | hdr.len = len; |
| 76 | |
| 77 | pcap_dump((u_char *)dumper, &hdr, buf); |
| 78 | } |