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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
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>
|
|
|
|
|
2012-11-22 04:22:41 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2011-10-22 00:45:35 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/time.h>
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/timeb.h>
|
2012-09-19 05:07:47 +04:00
|
|
|
#include <winpr/windows.h>
|
2011-10-22 00:45:35 +04:00
|
|
|
|
|
|
|
int gettimeofday(struct timeval* tp, void* tz)
|
|
|
|
{
|
|
|
|
struct _timeb timebuffer;
|
2012-09-19 05:07:47 +04:00
|
|
|
_ftime(&timebuffer);
|
2011-10-22 00:45:35 +04:00
|
|
|
tp->tv_sec = (long) timebuffer.time;
|
|
|
|
tp->tv_usec = timebuffer.millitm * 1000;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
#include <freerdp/types.h>
|
|
|
|
#include <freerdp/utils/pcap.h>
|
|
|
|
|
|
|
|
#define PCAP_MAGIC 0xA1B2C3D4
|
|
|
|
|
|
|
|
void pcap_read_header(rdpPcap* pcap, pcap_header* header)
|
|
|
|
{
|
|
|
|
fread((void*) header, sizeof(pcap_header), 1, pcap->fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_write_header(rdpPcap* pcap, pcap_header* header)
|
|
|
|
{
|
|
|
|
fwrite((void*) header, sizeof(pcap_header), 1, pcap->fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_read_record_header(rdpPcap* pcap, pcap_record_header* record)
|
|
|
|
{
|
|
|
|
fread((void*) record, sizeof(pcap_record_header), 1, pcap->fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_write_record_header(rdpPcap* pcap, pcap_record_header* record)
|
|
|
|
{
|
|
|
|
fwrite((void*) record, sizeof(pcap_record_header), 1, pcap->fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_read_record(rdpPcap* pcap, pcap_record* record)
|
|
|
|
{
|
|
|
|
pcap_read_record_header(pcap, &record->header);
|
|
|
|
record->length = record->header.incl_len;
|
2012-10-09 07:21:26 +04:00
|
|
|
record->data = malloc(record->length);
|
2011-08-27 02:36:35 +04:00
|
|
|
fread(record->data, record->length, 1, pcap->fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_write_record(rdpPcap* pcap, pcap_record* record)
|
|
|
|
{
|
|
|
|
pcap_write_record_header(pcap, &record->header);
|
|
|
|
fwrite(record->data, record->length, 1, pcap->fp);
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
void pcap_add_record(rdpPcap* pcap, void* data, UINT32 length)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
|
|
|
pcap_record* record;
|
2011-10-18 20:36:34 +04:00
|
|
|
struct timeval tp;
|
2011-08-27 02:36:35 +04:00
|
|
|
|
|
|
|
if (pcap->tail == NULL)
|
|
|
|
{
|
2012-11-22 04:22:41 +04:00
|
|
|
pcap->tail = (pcap_record*) malloc(sizeof(pcap_record));
|
|
|
|
ZeroMemory(pcap->tail, sizeof(pcap_record));
|
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
pcap->head = pcap->tail;
|
|
|
|
pcap->record = pcap->head;
|
|
|
|
record = pcap->tail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-22 04:22:41 +04:00
|
|
|
record = (pcap_record*) malloc(sizeof(pcap_record));
|
|
|
|
ZeroMemory(record, sizeof(pcap_record));
|
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
pcap->tail->next = record;
|
|
|
|
pcap->tail = record;
|
|
|
|
}
|
|
|
|
|
2011-08-27 04:26:40 +04:00
|
|
|
if (pcap->record == NULL)
|
|
|
|
pcap->record = record;
|
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
record->data = data;
|
|
|
|
record->length = length;
|
|
|
|
record->header.incl_len = length;
|
|
|
|
record->header.orig_len = length;
|
|
|
|
|
2011-10-18 20:36:34 +04:00
|
|
|
gettimeofday(&tp, 0);
|
|
|
|
record->header.ts_sec = tp.tv_sec;
|
|
|
|
record->header.ts_usec = tp.tv_usec;
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL pcap_has_next_record(rdpPcap* pcap)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
|
|
|
if (pcap->file_size - (ftell(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
|
|
|
{
|
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
|
|
|
{
|
|
|
|
fread(record->data, record->length, 1, pcap->fp);
|
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(rdpPcap* pcap, pcap_record* record)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
if (pcap_has_next_record(pcap) != TRUE)
|
|
|
|
return FALSE;
|
2011-08-27 02:36:35 +04:00
|
|
|
|
|
|
|
pcap_read_record(pcap, record);
|
|
|
|
|
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
|
|
|
rdpPcap* pcap_open(char* name, BOOL write)
|
2011-08-27 02:36:35 +04:00
|
|
|
{
|
|
|
|
rdpPcap* pcap;
|
|
|
|
|
2013-06-25 19:11:36 +04:00
|
|
|
FILE* pcap_fp = fopen(name, write ? "w+b" : "rb");
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2011-10-20 16:25:25 +04:00
|
|
|
if (pcap_fp == NULL)
|
|
|
|
{
|
|
|
|
perror("opening pcap dump");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-22 04:22:41 +04:00
|
|
|
pcap = (rdpPcap*) malloc(sizeof(rdpPcap));
|
2011-08-27 02:36:35 +04:00
|
|
|
|
|
|
|
if (pcap != NULL)
|
|
|
|
{
|
2012-11-22 04:22:41 +04:00
|
|
|
ZeroMemory(pcap, sizeof(rdpPcap));
|
|
|
|
|
2011-08-27 02:36:35 +04:00
|
|
|
pcap->name = name;
|
|
|
|
pcap->write = write;
|
|
|
|
pcap->record_count = 0;
|
2011-10-20 16:25:25 +04:00
|
|
|
pcap->fp = pcap_fp;
|
2011-08-27 02:36:35 +04:00
|
|
|
|
|
|
|
if (write)
|
|
|
|
{
|
|
|
|
pcap->header.magic_number = 0xA1B2C3D4;
|
|
|
|
pcap->header.version_major = 2;
|
|
|
|
pcap->header.version_minor = 4;
|
|
|
|
pcap->header.thiszone = 0;
|
|
|
|
pcap->header.sigfigs = 0;
|
2011-08-28 23:12:33 +04:00
|
|
|
pcap->header.snaplen = 0xFFFFFFFF;
|
2011-08-27 02:36:35 +04:00
|
|
|
pcap->header.network = 0;
|
|
|
|
pcap_write_header(pcap, &pcap->header);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fseek(pcap->fp, 0, SEEK_END);
|
|
|
|
pcap->file_size = (int) ftell(pcap->fp);
|
|
|
|
fseek(pcap->fp, 0, SEEK_SET);
|
|
|
|
pcap_read_header(pcap, &pcap->header);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pcap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_flush(rdpPcap* pcap)
|
|
|
|
{
|
|
|
|
while (pcap->record != NULL)
|
|
|
|
{
|
|
|
|
pcap_write_record(pcap, pcap->record);
|
|
|
|
pcap->record = pcap->record->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pcap->fp != NULL)
|
|
|
|
fflush(pcap->fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_close(rdpPcap* pcap)
|
|
|
|
{
|
|
|
|
pcap_flush(pcap);
|
|
|
|
|
|
|
|
if (pcap->fp != NULL)
|
|
|
|
fclose(pcap->fp);
|
2013-08-29 13:07:19 +04:00
|
|
|
|
|
|
|
free(pcap);
|
2011-08-27 02:36:35 +04:00
|
|
|
}
|