📄 savefile.c
字号:
* Controller Area Network (CAN) v. 2.0B packets. * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>. * Used to dump CAN packets coming from a CAN Vector board. * More documentation on the CAN v2.0B frames can be found at * http://www.can-cia.org/downloads/?269 */#define LINKTYPE_CAN20B 190static struct linktype_map { int dlt; int linktype;} map[] = { /* * These DLT_* codes have LINKTYPE_* codes with values identical * to the values of the corresponding DLT_* code. */ { DLT_NULL, LINKTYPE_NULL }, { DLT_EN10MB, LINKTYPE_ETHERNET }, { DLT_EN3MB, LINKTYPE_EXP_ETHERNET }, { DLT_AX25, LINKTYPE_AX25 }, { DLT_PRONET, LINKTYPE_PRONET }, { DLT_CHAOS, LINKTYPE_CHAOS }, { DLT_IEEE802, LINKTYPE_TOKEN_RING }, { DLT_ARCNET, LINKTYPE_ARCNET }, { DLT_SLIP, LINKTYPE_SLIP }, { DLT_PPP, LINKTYPE_PPP }, { DLT_FDDI, LINKTYPE_FDDI }, /* * These DLT_* codes have different values on different * platforms; we map them to LINKTYPE_* codes that * have values that should never be equal to any DLT_* * code. */#ifdef DLT_FR /* BSD/OS Frame Relay */ { DLT_FR, LINKTYPE_FRELAY },#endif { DLT_SYMANTEC_FIREWALL, LINKTYPE_SYMANTEC_FIREWALL }, { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 }, { DLT_RAW, LINKTYPE_RAW }, { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS }, { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS }, /* BSD/OS Cisco HDLC */ { DLT_C_HDLC, LINKTYPE_C_HDLC }, /* * These DLT_* codes are not on all platforms, but, so far, * there don't appear to be any platforms that define * other codes with those values; we map them to * different LINKTYPE_* values anyway, just in case. */ /* Linux ATM Classical IP */ { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP }, /* NetBSD sync/async serial PPP (or Cisco HDLC) */ { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC }, /* NetBSD PPP over Ethernet */ { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER }, /* IEEE 802.11 wireless */ { DLT_IEEE802_11, LINKTYPE_IEEE802_11 }, /* Frame Relay */ { DLT_FRELAY, LINKTYPE_FRELAY }, /* OpenBSD loopback */ { DLT_LOOP, LINKTYPE_LOOP }, /* Linux cooked socket capture */ { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL }, /* Apple LocalTalk hardware */ { DLT_LTALK, LINKTYPE_LTALK }, /* Acorn Econet */ { DLT_ECONET, LINKTYPE_ECONET }, /* OpenBSD DLT_PFLOG */ { DLT_PFLOG, LINKTYPE_PFLOG }, /* For Cisco-internal use */ { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS }, /* Prism II monitor-mode header plus 802.11 header */ { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER }, /* FreeBSD Aironet driver stuff */ { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER }, /* Siemens HiPath HDLC */ { DLT_HHDLC, LINKTYPE_HHDLC }, /* RFC 2625 IP-over-Fibre Channel */ { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC }, /* Solaris+SunATM */ { DLT_SUNATM, LINKTYPE_SUNATM }, /* RapidIO */ { DLT_RIO, LINKTYPE_RIO }, /* PCI Express */ { DLT_PCI_EXP, LINKTYPE_PCI_EXP }, /* Xilinx Aurora link layer */ { DLT_AURORA, LINKTYPE_AURORA }, /* 802.11 plus BSD radio header */ { DLT_IEEE802_11_RADIO, LINKTYPE_IEEE802_11_RADIO }, /* Tazmen Sniffer Protocol */ { DLT_TZSP, LINKTYPE_TZSP }, /* Arcnet with Linux-style link-layer headers */ { DLT_ARCNET_LINUX, LINKTYPE_ARCNET_LINUX }, /* Juniper-internal chassis encapsulation */ { DLT_JUNIPER_MLPPP, LINKTYPE_JUNIPER_MLPPP }, { DLT_JUNIPER_MLFR, LINKTYPE_JUNIPER_MLFR }, { DLT_JUNIPER_ES, LINKTYPE_JUNIPER_ES }, { DLT_JUNIPER_GGSN, LINKTYPE_JUNIPER_GGSN }, { DLT_JUNIPER_MFR, LINKTYPE_JUNIPER_MFR }, { DLT_JUNIPER_ATM2, LINKTYPE_JUNIPER_ATM2 }, { DLT_JUNIPER_SERVICES, LINKTYPE_JUNIPER_SERVICES }, { DLT_JUNIPER_ATM1, LINKTYPE_JUNIPER_ATM1 }, /* Apple IP-over-IEEE 1394 cooked header */ { DLT_APPLE_IP_OVER_IEEE1394, LINKTYPE_APPLE_IP_OVER_IEEE1394 }, /* SS7 */ { DLT_MTP2_WITH_PHDR, LINKTYPE_MTP2_WITH_PHDR }, { DLT_MTP2, LINKTYPE_MTP2 }, { DLT_MTP3, LINKTYPE_MTP3 }, { DLT_SCCP, LINKTYPE_SCCP }, /* DOCSIS MAC frames */ { DLT_DOCSIS, LINKTYPE_DOCSIS }, /* IrDA IrLAP packets + Linux-cooked header */ { DLT_LINUX_IRDA, LINKTYPE_LINUX_IRDA }, /* IBM SP and Next Federation switches */ { DLT_IBM_SP, LINKTYPE_IBM_SP }, { DLT_IBM_SN, LINKTYPE_IBM_SN }, /* 802.11 plus AVS radio header */ { DLT_IEEE802_11_RADIO_AVS, LINKTYPE_IEEE802_11_RADIO_AVS }, /* * Any platform that defines additional DLT_* codes should: * * request a LINKTYPE_* code and value from tcpdump.org, * as per the above; * * add, in their version of libpcap, an entry to map * those DLT_* codes to the corresponding LINKTYPE_* * code; * * redefine, in their "net/bpf.h", any DLT_* values * that collide with the values used by their additional * DLT_* codes, to remove those collisions (but without * making them collide with any of the LINKTYPE_* * values equal to 50 or above; they should also avoid * defining DLT_* values that collide with those * LINKTYPE_* values, either). */ /* Juniper-internal chassis encapsulation */ { DLT_JUNIPER_MONITOR, LINKTYPE_JUNIPER_MONITOR }, /* BACnet MS/TP */ { DLT_BACNET_MS_TP, LINKTYPE_BACNET_MS_TP }, /* PPP for pppd, with direction flag in the PPP header */ { DLT_PPP_PPPD, LINKTYPE_PPP_PPPD}, /* Juniper-internal chassis encapsulation */ { DLT_JUNIPER_PPPOE, LINKTYPE_JUNIPER_PPPOE }, { DLT_JUNIPER_PPPOE_ATM,LINKTYPE_JUNIPER_PPPOE_ATM }, /* GPRS LLC */ { DLT_GPRS_LLC, LINKTYPE_GPRS_LLC }, /* Transparent Generic Framing Procedure (ITU-T G.7041/Y.1303) */ { DLT_GPF_T, LINKTYPE_GPF_T }, /* Framed Generic Framing Procedure (ITU-T G.7041/Y.1303) */ { DLT_GPF_F, LINKTYPE_GPF_F }, { DLT_GCOM_T1E1, LINKTYPE_GCOM_T1E1 }, { DLT_GCOM_SERIAL, LINKTYPE_GCOM_SERIAL }, /* Juniper-internal chassis encapsulation */ { DLT_JUNIPER_PIC_PEER, LINKTYPE_JUNIPER_PIC_PEER }, /* Endace types */ { DLT_ERF_ETH, LINKTYPE_ERF_ETH }, { DLT_ERF_POS, LINKTYPE_ERF_POS }, /* viSDN LAPD */ { DLT_LINUX_LAPD, LINKTYPE_LINUX_LAPD }, /* Juniper meta-information before Ether, PPP, Frame Relay, C-HDLC Frames */ { DLT_JUNIPER_ETHER, LINKTYPE_JUNIPER_ETHER }, { DLT_JUNIPER_PPP, LINKTYPE_JUNIPER_PPP }, { DLT_JUNIPER_FRELAY, LINKTYPE_JUNIPER_FRELAY }, { DLT_JUNIPER_CHDLC, LINKTYPE_JUNIPER_CHDLC }, /* Multi Link Frame Relay (FRF.16) */ { DLT_MFR, LINKTYPE_MFR }, /* Juniper Voice PIC */ { DLT_JUNIPER_VP, LINKTYPE_JUNIPER_VP }, /* Controller Area Network (CAN) v2.0B */ { DLT_A429, LINKTYPE_A429 }, /* Controller Area Network (CAN) v2.0B */ { DLT_CAN20B, LINKTYPE_CAN20B }, /* Arinc 653 Interpartition Communication messages */ { DLT_A653_ICM, LINKTYPE_A653_ICM }, { -1, -1 }};static intdlt_to_linktype(int dlt){ int i; for (i = 0; map[i].dlt != -1; i++) { if (map[i].dlt == dlt) return (map[i].linktype); } /* * If we don't have a mapping for this DLT_ code, return an * error; that means that the table above needs to have an * entry added. */ return (-1);}static intlinktype_to_dlt(int linktype){ int i; for (i = 0; map[i].linktype != -1; i++) { if (map[i].linktype == linktype) return (map[i].dlt); } /* * If we don't have an entry for this link type, return * the link type value; it may be a DLT_ value from an * older version of libpcap. */ return linktype;}static intsf_write_header(FILE *fp, int linktype, int thiszone, int snaplen){ struct pcap_file_header hdr; hdr.magic = TCPDUMP_MAGIC; hdr.version_major = PCAP_VERSION_MAJOR; hdr.version_minor = PCAP_VERSION_MINOR; hdr.thiszone = thiszone; hdr.snaplen = snaplen; hdr.sigfigs = 0; hdr.linktype = linktype; if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1) return (-1); return (0);}static voidswap_hdr(struct pcap_file_header *hp){ hp->version_major = SWAPSHORT(hp->version_major); hp->version_minor = SWAPSHORT(hp->version_minor); hp->thiszone = SWAPLONG(hp->thiszone); hp->sigfigs = SWAPLONG(hp->sigfigs); hp->snaplen = SWAPLONG(hp->snaplen); hp->linktype = SWAPLONG(hp->linktype);}static intsf_getnonblock(pcap_t *p, char *errbuf){ /* * This is a savefile, not a live capture file, so never say * it's in non-blocking mode. */ return (0);}static intsf_setnonblock(pcap_t *p, int nonblock, char *errbuf){ /* * This is a savefile, not a live capture file, so ignore * requests to put it in non-blocking mode. */ return (0);}static intsf_stats(pcap_t *p, struct pcap_stat *ps){ snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Statistics aren't available from savefiles"); return (-1);}static intsf_inject(pcap_t *p, const void *buf _U_, size_t size _U_){ strlcpy(p->errbuf, "Sending packets isn't supported on savefiles", PCAP_ERRBUF_SIZE); return (-1);}/* * Set direction flag: Which packets do we accept on a forwarding * single device? IN, OUT or both? */static intsf_setdirection(pcap_t *p, pcap_direction_t d){ snprintf(p->errbuf, sizeof(p->errbuf), "Setting direction is not supported on savefiles"); return (-1);}static voidsf_close(pcap_t *p){ if (p->sf.rfile != stdin) (void)fclose(p->sf.rfile); if (p->sf.base != NULL) free(p->sf.base);}pcap_t *pcap_open_offline(const char *fname, char *errbuf){ FILE *fp; pcap_t *p; if (fname[0] == '-' && fname[1] == '\0') { fp = stdin;#if defined(WIN32) || defined(MSDOS) /* * We're reading from the standard input, so put it in binary * mode, as savefiles are binary files. */ SET_BINMODE(fp);#endif } else {#if !defined(WIN32) && !defined(MSDOS) fp = fopen(fname, "r");#else fp = fopen(fname, "rb");#endif if (fp == NULL) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname, pcap_strerror(errno)); return (NULL); } } p = pcap_fopen_offline(fp, errbuf); if (p == NULL) { if (fp != stdin) fclose(fp); } return (p);}pcap_t *pcap_fopen_offline(FILE *fp, char *errbuf){ register pcap_t *p; struct pcap_file_header hdr; size_t amt_read; bpf_u_int32 magic; int linklen; p = (pcap_t *)malloc(sizeof(*p)); if (p == NULL) { strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE); return (NULL); } memset((char *)p, 0, sizeof(*p)); amt_read = fread((char *)&hdr, 1, sizeof(hdr), fp); if (amt_read != sizeof(hdr)) { if (ferror(fp)) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "error reading dump file: %s", pcap_strerror(errno)); } else { snprintf(errbuf, PCAP_ERRBUF_SIZE, "truncated dump file; tried to read %lu file header bytes, only got %lu", (unsigned long)sizeof(hdr), (unsigned long)amt_read); } goto bad; } magic = hdr.magic; if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) { magic = SWAPLONG(magic); if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "bad dump file format"); goto bad; } p->sf.swapped = 1; swap_hdr(&hdr); } if (magic == KUZNETZOV_TCPDUMP_MAGIC) { /* * XXX - the patch that's in some versions of libpcap * changes the packet header but not the magic number, * and some other versions with this magic number have * some extra debugging information in the packet header; * we'd have to use some hacks^H^H^H^H^Hheuristics to * detect those variants. * * Ethereal does that, but it does so by trying to read * the first two packets of the file with each of the * record header formats. That currently means it seeks * backwards and retries the reads, which doesn't work * on pipes. We want to be able to read from a pipe, so * that strategy won't work; we'd have to buffer some * data ourselves and read from that buffer in order to * make that work. */ p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr); } else p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr); if (hdr.version_major < PCAP_VERSION_MAJOR) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format"); goto bad; } p->tzoff = hdr.thiszone; p->snapshot = hdr.snaplen; p->linktype = linktype_to_dlt(hdr.linktype);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -