1 /* 2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #pragma once 7 8 #include <stdio.h> 9 #include "esp_err.h" 10 11 #ifdef __cplusplus 12 extern "C" { 13 #endif 14 15 #define PCAP_DEFAULT_VERSION_MAJOR 0x02 /*!< Major Version */ 16 #define PCAP_DEFAULT_VERSION_MINOR 0x04 /*!< Minor Version */ 17 #define PCAP_DEFAULT_TIME_ZONE_GMT 0x00 /*!< Time Zone */ 18 19 /** 20 * @brief Type of pcap file handle 21 * 22 */ 23 typedef struct pcap_file_t *pcap_file_handle_t; 24 25 /** 26 * @brief Link layer Type Definition, used for Pcap reader to decode payload 27 * 28 */ 29 typedef enum { 30 PCAP_LINK_TYPE_LOOPBACK = 0, /*!< Loopback devices, except for later OpenBSD */ 31 PCAP_LINK_TYPE_ETHERNET = 1, /*!< Ethernet, and Linux loopback devices */ 32 PCAP_LINK_TYPE_TOKEN_RING = 6, /*!< 802.5 Token Ring */ 33 PCAP_LINK_TYPE_ARCNET = 7, /*!< ARCnet */ 34 PCAP_LINK_TYPE_SLIP = 8, /*!< SLIP */ 35 PCAP_LINK_TYPE_PPP = 9, /*!< PPP */ 36 PCAP_LINK_TYPE_FDDI = 10, /*!< FDDI */ 37 PCAP_LINK_TYPE_ATM = 100, /*!< LLC/SNAP encapsulated ATM */ 38 PCAP_LINK_TYPE_RAW_IP = 101, /*!< Raw IP, without link */ 39 PCAP_LINK_TYPE_BSD_SLIP = 102, /*!< BSD/OS SLIP */ 40 PCAP_LINK_TYPE_BSD_PPP = 103, /*!< BSD/OS PPP */ 41 PCAP_LINK_TYPE_CISCO_HDLC = 104, /*!< Cisco HDLC */ 42 PCAP_LINK_TYPE_802_11 = 105, /*!< 802.11 */ 43 PCAP_LINK_TYPE_BSD_LOOPBACK = 108, /*!< OpenBSD loopback devices(with AF_value in network byte order) */ 44 PCAP_LINK_TYPE_LOCAL_TALK = 114 /*!< LocalTalk */ 45 } pcap_link_type_t; 46 47 /** 48 * @brief Pcap configuration Type Definition 49 * 50 */ 51 typedef struct { 52 FILE *fp; /*!< Pointer to a standard file handle */ 53 unsigned int major_version; /*!< Pcap version: major */ 54 unsigned int minor_version; /*!< Pcap version: minor */ 55 unsigned int time_zone; /*!< Pcap timezone code */ 56 struct { 57 unsigned int little_endian: 1; /*!< Whether the pcap file is recored in little endian format */ 58 } flags; 59 } pcap_config_t; 60 61 /** 62 * @brief Create a new pcap session, and returns pcap file handle 63 * 64 * @note This function won't create the low level FILE* object, the user should take care of the creation of the File Stream. 65 * 66 * @param[in] config pcap file configuration 67 * @param[out] ret_pcap Returned pcap file handle 68 * @return 69 * - ESP_OK: Create pcap file successfully 70 * - ESP_ERR_INVALID_ARG: Create pcap file failed because of invalid argument 71 * - ESP_ERR_NO_MEM: Create pcap file failed because out of memory 72 * - ESP_FAIL: Create pcap file failed 73 */ 74 esp_err_t pcap_new_session(const pcap_config_t *config, pcap_file_handle_t *ret_pcap); 75 76 /** 77 * @brief Delete the pcap session, and close the File Stream 78 * 79 * @param[in] pcap pcap file handle created by `pcap_new_session()` 80 * @return 81 * - ESP_OK: Delete pcap session successfully 82 * - ESP_ERR_INVALID_ARG: Delete pcap session failed because of invalid argument 83 * - ESP_FAIL: Delete pcap session failed 84 */ 85 esp_err_t pcap_del_session(pcap_file_handle_t pcap); 86 87 /** 88 * @brief Write pcap file header 89 * 90 * @param[in] pcap pcap file handle created by `pcap_new_session()` 91 * @param[in] link_type Network link layer type 92 * @return 93 * - ESP_OK: Write pcap file header successfully 94 * - ESP_ERR_INVALID_ARG: Write pcap file header failed because of invalid argument 95 * - ESP_FAIL: Write pcap file header failed 96 */ 97 esp_err_t pcap_write_header(pcap_file_handle_t pcap, pcap_link_type_t link_type); 98 99 /** 100 * @brief Capture one packet into pcap file 101 * 102 * @param[in] pcap pcap file handle created by `pcap_new_session()` 103 * @param[in] payload pointer of the captured data buffer 104 * @param[in] length length of captured data buffer 105 * @param[in] seconds second of capture time 106 * @param[in] microseconds microsecond of capture time 107 * @return 108 * - ESP_OK: Write network packet into pcap file successfully 109 * - ESP_ERR_INVALID_ARG: Write network packet into pcap file failed because of invalid argument 110 * - ESP_FAIL: Write network packet into pcap file failed 111 */ 112 esp_err_t pcap_capture_packet(pcap_file_handle_t pcap, void *payload, uint32_t length, uint32_t seconds, uint32_t microseconds); 113 114 /** 115 * @brief Print the summary of pcap file into stream 116 * 117 * @param[in] pcap pcap file handle created by `pcap_new_session()` 118 * @param[in] print_file the file stream to save the summary 119 * @return 120 * - ESP_OK: Print pcap file summary successfully 121 * - ESP_ERR_INVALID_ARG: Print pcap file summary failed because of invalid argument 122 * - ESP_FAIL: Print pcap file summary failed 123 */ 124 esp_err_t pcap_print_summary(pcap_file_handle_t pcap, FILE *print_file); 125 126 #ifdef __cplusplus 127 } 128 #endif 129