FreeRDP/libfreerdp/utils/pcap.c

266 lines
5.4 KiB
C
Raw Normal View History

/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
* 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>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/wtypes.h>
2022-06-22 13:06:54 +03:00
#include <winpr/assert.h>
#include <winpr/file.h>
#include <winpr/crt.h>
#include <winpr/sysinfo.h>
#include <freerdp/types.h>
#include <freerdp/utils/pcap.h>
2019-11-06 17:24:51 +03:00
#define PCAP_MAGIC 0xA1B2C3D4
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;
};
static BOOL pcap_read_header(rdpPcap* pcap, pcap_header* header)
{
2022-06-22 13:06:54 +03:00
WINPR_ASSERT(pcap);
WINPR_ASSERT(header);
return fread(header, sizeof(pcap_header), 1, pcap->fp) == 1;
}
2022-06-22 13:06:54 +03:00
static BOOL pcap_write_header(rdpPcap* pcap, const pcap_header* header)
{
2022-06-22 13:06:54 +03:00
WINPR_ASSERT(pcap);
WINPR_ASSERT(header);
return fwrite(header, sizeof(pcap_header), 1, pcap->fp) == 1;
}
static BOOL pcap_read_record_header(rdpPcap* pcap, pcap_record_header* record)
{
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;
}
2022-06-22 13:06:54 +03:00
static BOOL pcap_write_record_header(rdpPcap* pcap, const pcap_record_header* record)
{
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;
}
static BOOL pcap_read_record(rdpPcap* pcap, pcap_record* record)
{
2022-06-22 13:06:54 +03:00
WINPR_ASSERT(pcap);
WINPR_ASSERT(record);
if (!pcap_read_record_header(pcap, &record->header))
return FALSE;
record->length = record->header.incl_len;
record->data = malloc(record->length);
if (!record->data)
return FALSE;
2015-06-22 19:52:13 +03:00
if (fread(record->data, record->length, 1, pcap->fp) != 1)
{
free(record->data);
record->data = NULL;
return FALSE;
}
return TRUE;
}
2022-06-22 13:06:54 +03:00
static BOOL pcap_write_record(rdpPcap* pcap, const pcap_record* record)
{
2022-06-22 13:06:54 +03:00
WINPR_ASSERT(pcap);
WINPR_ASSERT(record);
return pcap_write_record_header(pcap, &record->header) &&
(fwrite(record->cdata, record->length, 1, pcap->fp) == 1);
}
2022-06-22 13:06:54 +03:00
BOOL pcap_add_record(rdpPcap* pcap, const void* data, size_t length)
{
2022-06-22 13:06:54 +03:00
WINPR_ASSERT(pcap);
WINPR_ASSERT(data || (length == 0));
WINPR_ASSERT(length <= UINT32_MAX);
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;
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
if (pcap->tail == NULL)
{
2022-06-22 13:06:54 +03:00
pcap->tail = record;
if (!pcap->tail)
return FALSE;
pcap->head = pcap->tail;
}
else
{
2022-06-22 13:06:54 +03:00
record->next = pcap->tail;
pcap->tail = record;
}
if (pcap->record == NULL)
pcap->record = record;
return TRUE;
}
2022-06-22 13:06:54 +03:00
BOOL pcap_has_next_record(const rdpPcap* pcap)
{
2022-06-22 13:06:54 +03:00
WINPR_ASSERT(pcap);
if (pcap->file_size - (_ftelli64(pcap->fp)) <= 16)
return FALSE;
return TRUE;
}
BOOL pcap_get_next_record_header(rdpPcap* pcap, pcap_record* record)
{
2022-06-22 13:06:54 +03:00
WINPR_ASSERT(pcap);
WINPR_ASSERT(record);
if (pcap_has_next_record(pcap) != TRUE)
return FALSE;
pcap_read_record_header(pcap, &record->header);
record->length = record->header.incl_len;
return TRUE;
}
BOOL pcap_get_next_record_content(rdpPcap* pcap, pcap_record* record)
{
2022-06-22 13:06:54 +03:00
WINPR_ASSERT(pcap);
WINPR_ASSERT(record);
return fread(record->data, record->length, 1, pcap->fp) == 1;
}
BOOL pcap_get_next_record(rdpPcap* pcap, pcap_record* record)
{
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);
}
2020-11-18 09:51:45 +03:00
rdpPcap* pcap_open(const char* name, BOOL write)
{
rdpPcap* pcap = NULL;
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));
if (!pcap)
2020-05-25 15:03:35 +03:00
goto fail;
2020-11-18 09:51:45 +03:00
pcap->name = _strdup(name);
pcap->write = write;
pcap->record_count = 0;
pcap->fp = winpr_fopen(name, write ? "w+b" : "rb");
2020-05-25 15:03:35 +03:00
if (pcap->fp == NULL)
goto fail;
if (write)
{
2022-06-22 13:06:54 +03:00
pcap->header.magic_number = PCAP_MAGIC;
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;
pcap->header.network = 0;
if (!pcap_write_header(pcap, &pcap->header))
goto fail;
}
else
{
(void)_fseeki64(pcap->fp, 0, SEEK_END);
pcap->file_size = _ftelli64(pcap->fp);
(void)_fseeki64(pcap->fp, 0, SEEK_SET);
if (!pcap_read_header(pcap, &pcap->header))
goto fail;
}
return pcap;
fail:
2020-05-25 15:03:35 +03:00
pcap_close(pcap);
return NULL;
}
void pcap_flush(rdpPcap* pcap)
{
2022-06-22 13:06:54 +03:00
WINPR_ASSERT(pcap);
while (pcap->record != NULL)
{
pcap_write_record(pcap, pcap->record);
pcap->record = pcap->record->next;
}
if (pcap->fp != NULL)
(void)fflush(pcap->fp);
}
void pcap_close(rdpPcap* pcap)
{
2020-05-25 15:03:35 +03:00
if (!pcap)
return;
pcap_flush(pcap);
if (pcap->fp != NULL)
(void)fclose(pcap->fp);
2020-11-18 09:51:45 +03:00
free(pcap->name);
free(pcap);
}