1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/**
* 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.
*/
#include <freerdp/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/wtypes.h>
#include <winpr/assert.h>
#include <winpr/file.h>
#include <winpr/crt.h>
#include <freerdp/log.h>
#define TAG FREERDP_TAG("utils")
#ifndef _WIN32
#include <sys/time.h>
#else
#include <time.h>
#include <sys/timeb.h>
#include <winpr/windows.h>
int gettimeofday(struct timeval* tp, void* tz)
{
struct _timeb timebuffer;
_ftime(&timebuffer);
tp->tv_sec = (long)timebuffer.time;
tp->tv_usec = timebuffer.millitm * 1000;
return 0;
}
#endif
#include <freerdp/types.h>
#include <freerdp/utils/pcap.h>
#define PCAP_MAGIC 0xA1B2C3D4
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)
{
WINPR_ASSERT(pcap);
WINPR_ASSERT(header);
return fread(header, sizeof(pcap_header), 1, pcap->fp) == 1;
}
static BOOL pcap_write_header(rdpPcap* pcap, const pcap_header* header)
{
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)
{
WINPR_ASSERT(pcap);
WINPR_ASSERT(record);
return fread(record, sizeof(pcap_record_header), 1, pcap->fp) == 1;
}
static BOOL pcap_write_record_header(rdpPcap* pcap, const pcap_record_header* record)
{
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)
{
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;
if (fread(record->data, record->length, 1, pcap->fp) != 1)
{
free(record->data);
record->data = NULL;
return FALSE;
}
return TRUE;
}
static BOOL pcap_write_record(rdpPcap* pcap, const pcap_record* record)
{
WINPR_ASSERT(pcap);
WINPR_ASSERT(record);
return pcap_write_record_header(pcap, &record->header) &&
(fwrite(record->cdata, record->length, 1, pcap->fp) == 1);
}
BOOL pcap_add_record(rdpPcap* pcap, const void* data, size_t length)
{
pcap_record* record = NULL;
struct timeval tp;
WINPR_ASSERT(pcap);
WINPR_ASSERT(data || (length == 0));
WINPR_ASSERT(length <= UINT32_MAX);
record = (pcap_record*)calloc(1, sizeof(pcap_record));
if (!record)
return FALSE;
record->cdata = data;
record->length = length;
record->header.incl_len = (UINT32)length;
record->header.orig_len = (UINT32)length;
gettimeofday(&tp, 0);
record->header.ts_sec = (UINT32)tp.tv_sec;
record->header.ts_usec = (UINT32)tp.tv_usec;
if (pcap->tail == NULL)
{
pcap->tail = record;
if (!pcap->tail)
return FALSE;
pcap->head = pcap->tail;
}
else
{
record->next = pcap->tail;
pcap->tail = record;
}
if (pcap->record == NULL)
pcap->record = record;
return TRUE;
}
BOOL pcap_has_next_record(const rdpPcap* pcap)
{
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)
{
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)
{
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)
{
WINPR_ASSERT(pcap);
WINPR_ASSERT(record);
return pcap_has_next_record(pcap) && pcap_read_record(pcap, record);
}
rdpPcap* pcap_open(const char* name, BOOL write)
{
rdpPcap* pcap = NULL;
WINPR_ASSERT(name);
pcap = (rdpPcap*)calloc(1, sizeof(rdpPcap));
if (!pcap)
goto fail;
pcap->name = _strdup(name);
pcap->write = write;
pcap->record_count = 0;
pcap->fp = winpr_fopen(name, write ? "w+b" : "rb");
if (pcap->fp == NULL)
goto fail;
if (write)
{
pcap->header.magic_number = PCAP_MAGIC;
pcap->header.version_major = 2;
pcap->header.version_minor = 4;
pcap->header.thiszone = 0;
pcap->header.sigfigs = 0;
pcap->header.snaplen = UINT32_MAX;
pcap->header.network = 0;
if (!pcap_write_header(pcap, &pcap->header))
goto fail;
}
else
{
_fseeki64(pcap->fp, 0, SEEK_END);
pcap->file_size = _ftelli64(pcap->fp);
_fseeki64(pcap->fp, 0, SEEK_SET);
if (!pcap_read_header(pcap, &pcap->header))
goto fail;
}
return pcap;
fail:
pcap_close(pcap);
return NULL;
}
void pcap_flush(rdpPcap* pcap)
{
WINPR_ASSERT(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)
{
if (!pcap)
return;
pcap_flush(pcap);
if (pcap->fp != NULL)
fclose(pcap->fp);
free(pcap->name);
free(pcap);
}
|