forked from gvanem/wsock-trace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlap.c
248 lines (216 loc) · 6.2 KB
/
overlap.c
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
/*
* overlap.c - Part of Wsock-Trace
*
* Functions for dealing with overlapped operations in WSA functions.
* All winsock function that this can apply to is:
* AcceptEx(), ConnectEx(), DisconnectEx(), TransmitFile(),
* TransmitPackets(), WSARecv(), WSARecvFrom(), WSARecvMsg(),
* WSASend(), WSASendMsg(), WSASendTo(), and WSAIoctl().
*
* Allthough Wsock-Trace does support only a few of these.
*/
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#include "init.h"
#include "smartlist.h"
#include "overlap.h"
#undef TRACE
#define TRACE(fmt, ...) \
do { \
if (g_cfg.trace_overlap >= 1 && \
g_cfg.trace_level >= g_cfg.trace_overlap) \
{ \
trace_indent (g_cfg.trace_indent+2); \
trace_printf ("overlap.c(%u): " fmt, __LINE__, \
## __VA_ARGS__); \
} \
} while (0)
func_WSAGetOverlappedResult p_WSAGetOverlappedResult = NULL;
/*
* Structure for remembering a socket and an overlapped structure.
* Used in overlapped send and receive to update the recv
* and transmit counters in 'WSAGetOverlappedResult()'.
*/
struct overlapped {
/*
* TRUE: this is an overlapped WSARecv() or WSARecvFrom().
* FALSE: this an overlapped WSASend() or WSASendTo().
*/
BOOL is_recv;
/* Number of bytes expected in WSARecv() / WSARecvFrom() or
* number of bytes given in WSASend() or WSASendTo().
*/
DWORD bytes;
/* The socket, event and overlapped pointer the call was issued with.
*/
SOCKET sock;
WSAEVENT event;
WSAOVERLAPPED *ov;
};
static smartlist_t *ov_list;
static DWORD num_overlaps;
void overlap_exit (void)
{
struct overlapped *ov;
int i, max = smartlist_len (ov_list);
if (max >= 1)
{
TRACE ("%d overlapped transfers not completed:\n", max);
for (i = 0; i < max; i++)
{
ov = smartlist_get (ov_list, i);
TRACE (" o: 0x%p, event: 0x%p, sock: %u, is_recv: %d, bytes: %lu\n",
ov->ov, ov->event, SOCKET_CAST(ov->sock), ov->is_recv, DWORD_CAST(ov->bytes));
}
}
else if (num_overlaps)
{
/* Print this only if we stored at least 1 overlap structure.
*/
TRACE ("All overlapped transfers completed.\n");
}
for (i = 0; i < max; i++)
{
ov = smartlist_get (ov_list, i);
free (ov);
}
smartlist_free (ov_list);
ov_list = NULL;
}
void overlap_init (void)
{
ov_list = smartlist_new();
}
static void overlap_trace (int i, const struct overlapped *ov)
{
if (i == -1)
{
int max = smartlist_len (ov_list);
for (i = 0; i < max; i++)
{
ov = smartlist_get (ov_list, i);
TRACE ("ov_list[%d]: is_recv: %d, event: 0x%p, sock: %u\n",
i, ov->is_recv, ov->event, (unsigned)ov->sock);
}
}
else
TRACE ("ov_list[%d]: is_recv: %d, event: 0x%p, sock: %u\n",
i, ov->is_recv, ov->event, SOCKET_CAST(ov->sock));
}
void overlap_store (SOCKET s, WSAOVERLAPPED *o, DWORD num_bytes, BOOL is_recv)
{
struct overlapped *ov;
int i, max = smartlist_len (ov_list);
BOOL modify = FALSE;
TRACE ("o: 0x%p, event: 0x%p, sock: %u\n",
o, o ? o->hEvent : NULL, SOCKET_CAST(s));
for (i = 0; i < max; i++)
{
ov = smartlist_get (ov_list, i);
if (ov->ov == o && ov->sock == s && ov->is_recv == is_recv)
{
modify = TRUE;
break;
}
}
if (!modify)
{
ov = malloc (sizeof(*ov));
if (!ov)
return;
ov->is_recv = is_recv;
ov->sock = s;
ov->ov = o;
smartlist_add (ov_list, ov);
num_overlaps++;
}
ov->event = o ? o->hEvent : NULL;
ov->bytes = num_bytes;
overlap_trace (-1, NULL);
}
/*
* Try to update all overlapped operations matching this event.
*/
void overlap_recall_all (const WSAEVENT *event)
{
int i;
for (i = 0; i < smartlist_len(ov_list); i++)
{
const struct overlapped *ov = smartlist_get (ov_list, i);
DWORD bytes = 0;
BOOL rc = FALSE;
if (p_WSAGetOverlappedResult && ov->event == event)
{
ENTER_CRIT();
rc = (*p_WSAGetOverlappedResult) (ov->sock, ov->ov, &bytes, 0, NULL);
LEAVE_CRIT();
if (rc)
{
/* This could reduce 'smartlist_len(ov_list)' by 1.
*/
overlap_recall (ov->sock, ov->ov, bytes);
}
}
if (i < smartlist_len(ov_list))
TRACE ("ov_list[%d]: event: 0x%p, is_recv: %d, rc: %d, got %lu bytes.\n",
i, ov->event, ov->is_recv, rc, DWORD_CAST(bytes));
else
TRACE ("ov_list[%d]: is_recv: %d, was recalled.\n", i, ov->is_recv);
}
}
/*
* Match the socket 's' and 'o' value against a previous overlapped
* 'WSARecvXX()' / 'WSASendXX()' and update the 'g_cfg.counts.recv_bytes'
* or 'g_cfg.counts.send_bytes' statistics with the 'bytes' value.
*/
void overlap_recall (SOCKET s, const WSAOVERLAPPED *o, DWORD bytes)
{
int i, max = smartlist_len (ov_list);
for (i = 0; i < max; i++)
{
struct overlapped *ov = smartlist_get (ov_list, i);
overlap_trace (i, ov);
if (ov->ov != o || ov->sock != s)
continue;
if (ov->is_recv)
{
g_cfg.counts.recv_bytes += bytes;
TRACE ("ov_list[%d]: room for %lu bytes, got %lu bytes.\n",
i, DWORD_CAST(ov->bytes), DWORD_CAST(bytes));
}
else
{
g_cfg.counts.send_bytes += bytes;
TRACE ("ov_list[%d]: sent %lu bytes, actual sent %lu bytes.\n",
i, DWORD_CAST(ov->bytes), DWORD_CAST(bytes));
}
free (ov);
smartlist_del (ov_list, i);
break;
}
}
/*
* Remove an overlap entry matching socket 's'.
*/
void overlap_remove (SOCKET s)
{
int i, max;
/* This if-test should not be needed. It would mean 'closesocket()' was called
* after 'wsock_trace_exit()'.
*/
if (!ov_list)
return;
max = smartlist_len (ov_list);
for (i = 0; i < max; i++)
{
struct overlapped *ov = smartlist_get (ov_list, i);
if (ov->sock != s)
continue;
free (ov);
smartlist_del (ov_list, i);
max--;
i--;
}
overlap_trace (-1, NULL);
}