pcap.h 4.6 KB

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