2011-08-27 02:36:35 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-27 02:36:35 +04:00
|
|
|
* pcap File Format Utils
|
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
#include <stdio.h>
|
2012-10-09 07:21:26 +04:00
|
|
|
#include <stdlib.h>
|
2011-08-27 02:36:35 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-08-29 10:09:38 +03:00
|
|
|
#include <winpr/wtypes.h>
|
2022-06-22 13:06:54 +03:00
|
|
|
#include <winpr/assert.h>
|
2021-05-31 12:42:03 +03:00
|
|
|
#include <winpr/file.h>
|
2012-11-22 04:22:41 +04:00
|
|
|
#include <winpr/crt.h>
|
2024-03-08 11:34:04 +03:00
|
|
|
#include <winpr/sysinfo.h>
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
#include <freerdp/types.h>
|
|
|
|
#include <freerdp/utils/pcap.h>
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define PCAP_MAGIC 0xA1B2C3D4
|
2011-08-27 02:36:35 +04:00
|
|
|
|
2022-06-22 13:06:54 +03:00
|
|
|
struct rdp_pcap
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
char* name;
|
|
|
|
BOOL write;
|
|
|
|
INT64 file_size;
|
|
|
|
size_t record_count;
|
|
|
|
pcap_header header;
|
|
|
|
pcap_record* head;
|
|
|
|
pcap_record* tail;
|
|
|
|
pcap_record* record;
|
|
|
|
};
|
|
|
|
|
2015-06-26 16:58:01 +03:00
|
|
|
static BOOL pcap_read_header(rdpPcap* pcap, pcap_header* header)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(header);
|
|
|
|
|
|
|
|
return fread(header, sizeof(pcap_header), 1, pcap->fp) == 1;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2022-06-22 13:06:54 +03:00
|
|
|
static BOOL pcap_write_header(rdpPcap* pcap, const pcap_header* header)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(header);
|
|
|
|
|
|
|
|
return fwrite(header, sizeof(pcap_header), 1, pcap->fp) == 1;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2015-06-26 16:58:01 +03:00
|
|
|
static BOOL pcap_read_record_header(rdpPcap* pcap, pcap_record_header* record)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(record);
|
|
|
|
|
|
|
|
return fread(record, sizeof(pcap_record_header), 1, pcap->fp) == 1;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2022-06-22 13:06:54 +03:00
|
|
|
static BOOL pcap_write_record_header(rdpPcap* pcap, const pcap_record_header* record)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(record);
|
|
|
|
|
|
|
|
return fwrite(record, sizeof(pcap_record_header), 1, pcap->fp) == 1;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2015-06-26 16:58:01 +03:00
|
|
|
static BOOL pcap_read_record(rdpPcap* pcap, pcap_record* record)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(record);
|
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!pcap_read_record_header(pcap, &record->header))
|
|
|
|
return FALSE;
|
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
record->length = record->header.incl_len;
|
2012-10-09 07:21:26 +04:00
|
|
|
record->data = malloc(record->length);
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!record->data)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-06-22 19:52:13 +03:00
|
|
|
if (fread(record->data, record->length, 1, pcap->fp) != 1)
|
2015-06-16 16:42:07 +03:00
|
|
|
{
|
|
|
|
free(record->data);
|
|
|
|
record->data = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2022-06-22 13:06:54 +03:00
|
|
|
static BOOL pcap_write_record(rdpPcap* pcap, const pcap_record* record)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(record);
|
|
|
|
|
2015-06-26 16:58:01 +03:00
|
|
|
return pcap_write_record_header(pcap, &record->header) &&
|
2021-01-18 11:25:03 +03:00
|
|
|
(fwrite(record->cdata, record->length, 1, pcap->fp) == 1);
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2022-06-22 13:06:54 +03:00
|
|
|
BOOL pcap_add_record(rdpPcap* pcap, const void* data, size_t length)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(data || (length == 0));
|
|
|
|
WINPR_ASSERT(length <= UINT32_MAX);
|
|
|
|
|
2024-03-08 11:34:04 +03:00
|
|
|
pcap_record* record = (pcap_record*)calloc(1, sizeof(pcap_record));
|
2022-06-22 13:06:54 +03:00
|
|
|
if (!record)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
record->cdata = data;
|
|
|
|
record->length = length;
|
|
|
|
record->header.incl_len = (UINT32)length;
|
|
|
|
record->header.orig_len = (UINT32)length;
|
|
|
|
|
2024-03-08 11:34:04 +03:00
|
|
|
const UINT64 ns = winpr_GetUnixTimeNS();
|
|
|
|
|
|
|
|
record->header.ts_sec = WINPR_TIME_NS_TO_S(ns);
|
|
|
|
record->header.ts_usec = WINPR_TIME_NS_REM_US(ns);
|
2022-06-22 13:06:54 +03:00
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
if (pcap->tail == NULL)
|
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
pcap->tail = record;
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!pcap->tail)
|
|
|
|
return FALSE;
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
pcap->head = pcap->tail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
record->next = pcap->tail;
|
2011-08-27 02:36:35 +04:00
|
|
|
pcap->tail = record;
|
|
|
|
}
|
|
|
|
|
2011-08-27 04:26:40 +04:00
|
|
|
if (pcap->record == NULL)
|
|
|
|
pcap->record = record;
|
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
return TRUE;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2022-06-22 13:06:54 +03:00
|
|
|
BOOL pcap_has_next_record(const rdpPcap* pcap)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
|
2017-08-11 11:07:46 +03:00
|
|
|
if (pcap->file_size - (_ftelli64(pcap->fp)) <= 16)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-27 02:36:35 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL pcap_get_next_record_header(rdpPcap* pcap, pcap_record* record)
|
2011-08-27 05:44:37 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(record);
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (pcap_has_next_record(pcap) != TRUE)
|
|
|
|
return FALSE;
|
2011-08-27 05:44:37 +04:00
|
|
|
|
|
|
|
pcap_read_record_header(pcap, &record->header);
|
|
|
|
record->length = record->header.incl_len;
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-27 05:44:37 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL pcap_get_next_record_content(rdpPcap* pcap, pcap_record* record)
|
2011-08-27 05:44:37 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(record);
|
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
return fread(record->data, record->length, 1, pcap->fp) == 1;
|
2011-08-27 05:44:37 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL pcap_get_next_record(rdpPcap* pcap, pcap_record* record)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
WINPR_ASSERT(record);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return pcap_has_next_record(pcap) && pcap_read_record(pcap, record);
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2020-11-18 09:51:45 +03:00
|
|
|
rdpPcap* pcap_open(const char* name, BOOL write)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpPcap* pcap = NULL;
|
2011-08-27 02:36:35 +04:00
|
|
|
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(name);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pcap = (rdpPcap*)calloc(1, sizeof(rdpPcap));
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!pcap)
|
2020-05-25 15:03:35 +03:00
|
|
|
goto fail;
|
2015-06-16 16:42:07 +03:00
|
|
|
|
2020-11-18 09:51:45 +03:00
|
|
|
pcap->name = _strdup(name);
|
2015-06-16 16:42:07 +03:00
|
|
|
pcap->write = write;
|
|
|
|
pcap->record_count = 0;
|
2021-05-31 12:42:03 +03:00
|
|
|
pcap->fp = winpr_fopen(name, write ? "w+b" : "rb");
|
2020-05-25 15:03:35 +03:00
|
|
|
|
|
|
|
if (pcap->fp == NULL)
|
|
|
|
goto fail;
|
2011-08-27 02:36:35 +04:00
|
|
|
|
2015-06-16 16:42:07 +03:00
|
|
|
if (write)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
pcap->header.magic_number = PCAP_MAGIC;
|
2015-06-16 16:42:07 +03:00
|
|
|
pcap->header.version_major = 2;
|
|
|
|
pcap->header.version_minor = 4;
|
|
|
|
pcap->header.thiszone = 0;
|
|
|
|
pcap->header.sigfigs = 0;
|
2022-06-22 13:06:54 +03:00
|
|
|
pcap->header.snaplen = UINT32_MAX;
|
2015-06-16 16:42:07 +03:00
|
|
|
pcap->header.network = 0;
|
|
|
|
if (!pcap_write_header(pcap, &pcap->header))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-09-14 21:55:48 +03:00
|
|
|
(void)_fseeki64(pcap->fp, 0, SEEK_END);
|
2017-08-11 11:07:46 +03:00
|
|
|
pcap->file_size = _ftelli64(pcap->fp);
|
2024-09-14 21:55:48 +03:00
|
|
|
(void)_fseeki64(pcap->fp, 0, SEEK_SET);
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!pcap_read_header(pcap, &pcap->header))
|
|
|
|
goto fail;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pcap;
|
2015-06-16 16:42:07 +03:00
|
|
|
|
|
|
|
fail:
|
2020-05-25 15:03:35 +03:00
|
|
|
pcap_close(pcap);
|
2015-06-16 16:42:07 +03:00
|
|
|
return NULL;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_flush(rdpPcap* pcap)
|
|
|
|
{
|
2022-06-22 13:06:54 +03:00
|
|
|
WINPR_ASSERT(pcap);
|
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
while (pcap->record != NULL)
|
|
|
|
{
|
|
|
|
pcap_write_record(pcap, pcap->record);
|
|
|
|
pcap->record = pcap->record->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pcap->fp != NULL)
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fflush(pcap->fp);
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_close(rdpPcap* pcap)
|
|
|
|
{
|
2020-05-25 15:03:35 +03:00
|
|
|
if (!pcap)
|
|
|
|
return;
|
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
pcap_flush(pcap);
|
|
|
|
|
|
|
|
if (pcap->fp != NULL)
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fclose(pcap->fp);
|
2013-08-29 13:07:19 +04:00
|
|
|
|
2020-11-18 09:51:45 +03:00
|
|
|
free(pcap->name);
|
2013-08-29 13:07:19 +04:00
|
|
|
free(pcap);
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|