2019-01-25 04:03:01 -05:00
|
|
|
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-09-26 23:24:12 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include "esp_types.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "pcap.h"
|
|
|
|
|
2019-01-25 04:03:01 -05:00
|
|
|
static const char *PCAP_TAG = "pcap";
|
2021-05-12 13:52:28 -04:00
|
|
|
|
2019-01-25 04:03:01 -05:00
|
|
|
#define PCAP_CHECK(a, str, goto_tag, ...) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (!(a)) \
|
|
|
|
{ \
|
|
|
|
ESP_LOGE(PCAP_TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
|
|
|
goto goto_tag; \
|
|
|
|
} \
|
2018-09-26 23:24:12 -04:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/**
|
2019-01-25 04:03:01 -05:00
|
|
|
* @brief Pcap File Header
|
2018-09-26 23:24:12 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
uint32_t magic; /*!< Magic Number */
|
|
|
|
uint16_t major; /*!< Major Version */
|
|
|
|
uint16_t minor; /*!< Minor Version */
|
|
|
|
uint32_t zone; /*!< Time Zone Offset */
|
|
|
|
uint32_t sigfigs; /*!< Timestamp Accuracy */
|
|
|
|
uint32_t snaplen; /*!< Max Length to Capture */
|
|
|
|
uint32_t link_type; /*!< Link Layer Type */
|
|
|
|
} pcap_file_header_t;
|
|
|
|
|
|
|
|
/**
|
2019-01-25 04:03:01 -05:00
|
|
|
* @brief Pcap Packet Header
|
2018-09-26 23:24:12 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
uint32_t seconds; /*!< Number of seconds since January 1st, 1970, 00:00:00 GMT */
|
2019-01-25 04:03:01 -05:00
|
|
|
uint32_t microseconds; /*!< Number of microseconds when the packet was captured (offset from seconds) */
|
|
|
|
uint32_t capture_length; /*!< Number of bytes of captured data, no longer than packet_length */
|
2018-09-26 23:24:12 -04:00
|
|
|
uint32_t packet_length; /*!< Actual length of current packet */
|
|
|
|
} pcap_packet_header_t;
|
|
|
|
|
2019-01-25 04:03:01 -05:00
|
|
|
/**
|
|
|
|
* @brief Pcap Runtime Handle
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
FILE *file; /*!< File handle */
|
|
|
|
} pcap_runtime_t;
|
2018-09-26 23:24:12 -04:00
|
|
|
|
2019-01-25 04:03:01 -05:00
|
|
|
esp_err_t pcap_capture_packet(pcap_handle_t handle, void *payload, uint32_t length, uint32_t seconds, uint32_t microseconds)
|
2018-09-26 23:24:12 -04:00
|
|
|
{
|
2019-01-25 04:03:01 -05:00
|
|
|
PCAP_CHECK(handle, "pcap handle is NULL", err);
|
|
|
|
pcap_runtime_t *pcap_rt = (pcap_runtime_t *)handle;
|
|
|
|
PCAP_CHECK(pcap_rt->file, "pcap file is NULL", err);
|
2018-09-26 23:24:12 -04:00
|
|
|
size_t real_write = 0;
|
|
|
|
pcap_packet_header_t header = {
|
|
|
|
.seconds = seconds,
|
|
|
|
.microseconds = microseconds,
|
|
|
|
.capture_length = length,
|
|
|
|
.packet_length = length
|
|
|
|
};
|
2019-01-25 04:03:01 -05:00
|
|
|
real_write = fwrite(&header, sizeof(header), 1, pcap_rt->file);
|
|
|
|
PCAP_CHECK(real_write == 1, "write packet header error", err);
|
|
|
|
real_write = fwrite(payload, sizeof(uint8_t), length, pcap_rt->file);
|
|
|
|
PCAP_CHECK(real_write == length, "write packet payload error", err);
|
2018-09-26 23:24:12 -04:00
|
|
|
/* Flush content in the buffer into device */
|
2019-01-25 04:03:01 -05:00
|
|
|
fflush(pcap_rt->file);
|
2018-09-26 23:24:12 -04:00
|
|
|
return ESP_OK;
|
2019-01-25 04:03:01 -05:00
|
|
|
err:
|
|
|
|
return ESP_FAIL;
|
2018-09-26 23:24:12 -04:00
|
|
|
}
|
|
|
|
|
2019-01-25 04:03:01 -05:00
|
|
|
esp_err_t pcap_deinit(pcap_handle_t handle)
|
2018-09-26 23:24:12 -04:00
|
|
|
{
|
2019-01-25 04:03:01 -05:00
|
|
|
PCAP_CHECK(handle, "pcap handle is NULL", err);
|
|
|
|
pcap_runtime_t *pcap_rt = (pcap_runtime_t *)handle;
|
2021-05-12 13:52:28 -04:00
|
|
|
|
|
|
|
if (pcap_rt->file != NULL) {
|
|
|
|
fclose(pcap_rt->file);
|
|
|
|
pcap_rt->file = NULL;
|
|
|
|
}
|
2019-01-25 04:03:01 -05:00
|
|
|
free(pcap_rt);
|
|
|
|
ESP_LOGD(PCAP_TAG, "pcap deinit OK");
|
2018-09-26 23:24:12 -04:00
|
|
|
return ESP_OK;
|
2019-01-25 04:03:01 -05:00
|
|
|
err:
|
|
|
|
ESP_LOGW(PCAP_TAG, "pcap deinit failed");
|
|
|
|
return ESP_FAIL;
|
2018-09-26 23:24:12 -04:00
|
|
|
}
|
|
|
|
|
2019-01-25 04:03:01 -05:00
|
|
|
esp_err_t pcap_init(pcap_config_t *config, pcap_handle_t *handle)
|
2018-09-26 23:24:12 -04:00
|
|
|
{
|
2019-01-25 04:03:01 -05:00
|
|
|
PCAP_CHECK(config, "config is NULL", err);
|
|
|
|
PCAP_CHECK(handle, "pcap handle is NULL", err);
|
2021-05-12 13:52:28 -04:00
|
|
|
pcap_runtime_t *pcap_rt = calloc(1, sizeof(pcap_runtime_t));
|
2019-01-25 04:03:01 -05:00
|
|
|
PCAP_CHECK(pcap_rt, "calloc pcap runtime failed", err);
|
|
|
|
pcap_rt->file = config->fp;
|
2018-09-26 23:24:12 -04:00
|
|
|
/* Write Pcap File header */
|
|
|
|
pcap_file_header_t header = {
|
|
|
|
.magic = PCAP_MAGIC_BIG_ENDIAN,
|
|
|
|
.major = PCAP_VERSION_MAJOR,
|
|
|
|
.minor = PCAP_VERSION_MINOR,
|
|
|
|
.zone = PCAP_TIME_ZONE_GMT,
|
|
|
|
.sigfigs = 0,
|
|
|
|
.snaplen = 0x40000,
|
|
|
|
.link_type = config->link_type
|
|
|
|
};
|
2019-01-25 04:03:01 -05:00
|
|
|
size_t real_write = fwrite(&header, sizeof(header), 1, pcap_rt->file);
|
|
|
|
PCAP_CHECK(real_write == 1, "write pcap file header failed", err_write);
|
2018-09-26 23:24:12 -04:00
|
|
|
/* Flush content in the buffer into device */
|
2019-01-25 04:03:01 -05:00
|
|
|
fflush(pcap_rt->file);
|
|
|
|
*handle = (pcap_handle_t)pcap_rt;
|
|
|
|
ESP_LOGD(PCAP_TAG, "pcap init OK");
|
2018-09-26 23:24:12 -04:00
|
|
|
return ESP_OK;
|
|
|
|
/* Error Handling */
|
|
|
|
err_write:
|
2019-01-25 04:03:01 -05:00
|
|
|
fclose(pcap_rt->file);
|
|
|
|
pcap_rt->file = NULL;
|
|
|
|
free(pcap_rt);
|
|
|
|
err:
|
|
|
|
ESP_LOGW(PCAP_TAG, "pcap init failed");
|
2018-09-26 23:24:12 -04:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|