1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "esp_log.h"
11 #include "esp_check.h"
12 #include "pcap.h"
13
14 static const char *TAG = "pcap";
15
16 #define PCAP_MAGIC_BIG_ENDIAN 0xA1B2C3D4 /*!< Big-Endian */
17 #define PCAP_MAGIC_LITTLE_ENDIAN 0xD4C3B2A1 /*!< Little-Endian */
18
19 typedef struct pcap_file_t pcap_file_t;
20
21 /**
22 * @brief Pcap File Header
23 *
24 */
25 typedef struct {
26 uint32_t magic; /*!< Magic Number */
27 uint16_t major; /*!< Major Version */
28 uint16_t minor; /*!< Minor Version */
29 uint32_t zone; /*!< Time Zone Offset */
30 uint32_t sigfigs; /*!< Timestamp Accuracy */
31 uint32_t snaplen; /*!< Max Length to Capture */
32 uint32_t link_type; /*!< Link Layer Type */
33 } pcap_file_header_t;
34
35 /**
36 * @brief Pcap Packet Header
37 *
38 */
39 typedef struct {
40 uint32_t seconds; /*!< Number of seconds since January 1st, 1970, 00:00:00 GMT */
41 uint32_t microseconds; /*!< Number of microseconds when the packet was captured (offset from seconds) */
42 uint32_t capture_length; /*!< Number of bytes of captured data, no longer than packet_length */
43 uint32_t packet_length; /*!< Actual length of current packet */
44 } pcap_packet_header_t;
45
46 /**
47 * @brief Pcap Runtime Handle
48 *
49 */
50 struct pcap_file_t {
51 FILE *file; /*!< File handle */
52 pcap_link_type_t link_type; /*!< Pcap Link Type */
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 uint32_t endian_magic; /*!< Magic value related to endian format */
57 };
58
pcap_new_session(const pcap_config_t * config,pcap_file_handle_t * ret_pcap)59 esp_err_t pcap_new_session(const pcap_config_t *config, pcap_file_handle_t *ret_pcap)
60 {
61 esp_err_t ret = ESP_OK;
62 pcap_file_t *pcap = NULL;
63 ESP_GOTO_ON_FALSE(config && ret_pcap, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
64 ESP_GOTO_ON_FALSE(config->fp, ESP_ERR_INVALID_ARG, err, TAG, "pcap file handle can't be NULL");
65 pcap = calloc(1, sizeof(pcap_file_t));
66 ESP_GOTO_ON_FALSE(pcap, ESP_ERR_NO_MEM, err, TAG, "no mem for pcap file object");
67 pcap->file = config->fp;
68 pcap->major_version = config->major_version;
69 pcap->minor_version = config->minor_version;
70 pcap->endian_magic = config->flags.little_endian ? PCAP_MAGIC_LITTLE_ENDIAN : PCAP_MAGIC_BIG_ENDIAN;
71 pcap->time_zone = config->time_zone;
72 *ret_pcap = pcap;
73 return ret;
74 err:
75 if (pcap) {
76 free(pcap);
77 }
78 return ret;
79 }
80
pcap_del_session(pcap_file_handle_t pcap)81 esp_err_t pcap_del_session(pcap_file_handle_t pcap)
82 {
83 ESP_RETURN_ON_FALSE(pcap, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
84 if (pcap->file) {
85 fclose(pcap->file);
86 pcap->file = NULL;
87 }
88 free(pcap);
89 return ESP_OK;
90 }
91
pcap_write_header(pcap_file_handle_t pcap,pcap_link_type_t link_type)92 esp_err_t pcap_write_header(pcap_file_handle_t pcap, pcap_link_type_t link_type)
93 {
94 ESP_RETURN_ON_FALSE(pcap, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
95 /* Write Pcap File header */
96 pcap_file_header_t header = {
97 .magic = pcap->endian_magic,
98 .major = pcap->major_version,
99 .minor = pcap->minor_version,
100 .zone = pcap->time_zone,
101 .sigfigs = 0,
102 .snaplen = 0x40000,
103 .link_type = link_type,
104 };
105 size_t real_write = fwrite(&header, sizeof(header), 1, pcap->file);
106 ESP_RETURN_ON_FALSE(real_write == 1, ESP_FAIL, TAG, "write pcap file header failed");
107 /* Save the link type to pcap file object */
108 pcap->link_type = link_type;
109 /* Flush content in the buffer into device */
110 fflush(pcap->file);
111 return ESP_OK;
112 }
113
pcap_capture_packet(pcap_file_handle_t pcap,void * payload,uint32_t length,uint32_t seconds,uint32_t microseconds)114 esp_err_t pcap_capture_packet(pcap_file_handle_t pcap, void *payload, uint32_t length, uint32_t seconds, uint32_t microseconds)
115 {
116 ESP_RETURN_ON_FALSE(pcap && payload, ESP_ERR_INVALID_ARG, TAG, "invalid argumnet");
117 size_t real_write = 0;
118 pcap_packet_header_t header = {
119 .seconds = seconds,
120 .microseconds = microseconds,
121 .capture_length = length,
122 .packet_length = length
123 };
124 real_write = fwrite(&header, sizeof(header), 1, pcap->file);
125 ESP_RETURN_ON_FALSE(real_write == 1, ESP_FAIL, TAG, "write packet header failed");
126 real_write = fwrite(payload, sizeof(uint8_t), length, pcap->file);
127 ESP_RETURN_ON_FALSE(real_write == length, ESP_FAIL, TAG, "write packet payload failed");
128 /* Flush content in the buffer into device */
129 fflush(pcap->file);
130 return ESP_OK;
131 }
132
pcap_print_summary(pcap_file_handle_t pcap,FILE * print_file)133 esp_err_t pcap_print_summary(pcap_file_handle_t pcap, FILE *print_file)
134 {
135 esp_err_t ret = ESP_OK;
136 long size = 0;
137 char *packet_payload = NULL;
138 ESP_RETURN_ON_FALSE(pcap && print_file, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
139 // get file size
140 fseek(pcap->file, 0L, SEEK_END);
141 size = ftell(pcap->file);
142 fseek(pcap->file, 0L, SEEK_SET);
143 // file empty is allowed, so return ESP_OK
144 ESP_RETURN_ON_FALSE(size, ESP_OK, TAG, "pcap file is empty");
145 // packet index (by bytes)
146 uint32_t index = 0;
147 pcap_file_header_t file_header;
148 size_t real_read = fread(&file_header, sizeof(pcap_file_header_t), 1, pcap->file);
149 ESP_RETURN_ON_FALSE(real_read == 1, ESP_FAIL, TAG, "read pcap file header failed");
150 index += sizeof(pcap_file_header_t);
151 //print pcap header information
152 fprintf(print_file, "------------------------------------------------------------------------\n");
153 fprintf(print_file, "Pcap packet Head:\n");
154 fprintf(print_file, "------------------------------------------------------------------------\n");
155 fprintf(print_file, "Magic Number: %x\n", file_header.magic);
156 fprintf(print_file, "Major Version: %d\n", file_header.major);
157 fprintf(print_file, "Minor Version: %d\n", file_header.minor);
158 fprintf(print_file, "SnapLen: %d\n", file_header.snaplen);
159 fprintf(print_file, "LinkType: %d\n", file_header.link_type);
160 fprintf(print_file, "------------------------------------------------------------------------\n");
161 uint32_t packet_num = 0;
162 pcap_packet_header_t packet_header;
163 while (index < size) {
164 real_read = fread(&packet_header, sizeof(pcap_packet_header_t), 1, pcap->file);
165 ESP_GOTO_ON_FALSE(real_read == 1, ESP_FAIL, err, TAG, "read pcap packet header failed");
166 // print packet header information
167 fprintf(print_file, "Packet %d:\n", packet_num);
168 fprintf(print_file, "Timestamp (Seconds): %d\n", packet_header.seconds);
169 fprintf(print_file, "Timestamp (Microseconds): %d\n", packet_header.microseconds);
170 fprintf(print_file, "Capture Length: %d\n", packet_header.capture_length);
171 fprintf(print_file, "Packet Length: %d\n", packet_header.packet_length);
172 size_t payload_length = packet_header.capture_length;
173 packet_payload = malloc(payload_length);
174 ESP_GOTO_ON_FALSE(packet_payload, ESP_ERR_NO_MEM, err, TAG, "no mem to save packet payload");
175 real_read = fread(packet_payload, payload_length, 1, pcap->file);
176 ESP_GOTO_ON_FALSE(real_read == 1, ESP_FAIL, err, TAG, "read payload error");
177 // print packet information
178 if (file_header.link_type == PCAP_LINK_TYPE_802_11) {
179 // Frame Control Field is coded as LSB first
180 fprintf(print_file, "Frame Type: %2x\n", (packet_payload[0] >> 2) & 0x03);
181 fprintf(print_file, "Frame Subtype: %2x\n", (packet_payload[0] >> 4) & 0x0F);
182 fprintf(print_file, "Destination: ");
183 for (int j = 0; j < 5; j++) {
184 fprintf(print_file, "%2x ", packet_payload[4 + j]);
185 }
186 fprintf(print_file, "%2x\n", packet_payload[9]);
187 fprintf(print_file, "Source: ");
188 for (int j = 0; j < 5; j++) {
189 fprintf(print_file, "%2x ", packet_payload[10 + j]);
190 }
191 fprintf(print_file, "%2x\n", packet_payload[15]);
192 fprintf(print_file, "------------------------------------------------------------------------\n");
193 } else if (file_header.link_type == PCAP_LINK_TYPE_ETHERNET){
194 fprintf(print_file, "Destination: ");
195 for (int j = 0; j < 5; j++) {
196 fprintf(print_file, "%2x ", packet_payload[j]);
197 }
198 fprintf(print_file, "%2x\n", packet_payload[5]);
199 fprintf(print_file, "Source: ");
200 for (int j = 0; j < 5; j++) {
201 fprintf(print_file, "%2x ", packet_payload[6 + j]);
202 }
203 fprintf(print_file, "%2x\n", packet_payload[11]);
204 fprintf(print_file, "Type: 0x%x\n", packet_payload[13] | (packet_payload[12] << 8));
205 fprintf(print_file, "------------------------------------------------------------------------\n");
206 }
207 else {
208 fprintf(print_file, "Unknown link type:%d\n", file_header.link_type);
209 fprintf(print_file, "------------------------------------------------------------------------\n");
210 }
211 free(packet_payload);
212 packet_payload = NULL;
213 index += packet_header.capture_length + sizeof(pcap_packet_header_t);
214 packet_num ++;
215 }
216 fprintf(print_file, "Pcap packet Number: %d\n", packet_num);
217 fprintf(print_file, "------------------------------------------------------------------------\n");
218 return ret;
219 err:
220 if (packet_payload) {
221 free(packet_payload);
222 }
223 return ret;
224 }
225