This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 145
/
tcp.c
498 lines (411 loc) · 14.4 KB
/
tcp.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*!
* @file tcp.c
* @brief Definitions for functionality that handles TCP client operations.
*/
#include "precomp.h"
#include "tcp.h"
/*!
* @brief Writes data from the remote half of the channel to the established connection.
* @param channel Pointer to the channel to write to.
* @param request Pointer to the request packet.
* @param context Pointer to the channel's context.
* @param buffer Buffer containing the data to write to the channel.
* @param bufferSize Size of the buffer indicating how many bytes to write.
* @param bytesWritten Pointer that receives the number of bytes written to the \c channel.
* @returns Indication of success or failure.
* @retval ERROR_SUCCESS writing the data completed successfully.
*/
DWORD tcp_channel_client_write(Channel *channel, Packet *request, LPVOID context, LPVOID buffer, DWORD bufferSize, LPDWORD bytesWritten)
{
DWORD dwResult = ERROR_SUCCESS;
TcpClientContext * ctx = NULL;
LONG written = 0;
do
{
dprintf("[TCP] tcp_channel_client_write. channel=0x%08X, buffsize=%d", channel, bufferSize);
ctx = (TcpClientContext *)context;
if (!ctx)
{
BREAK_WITH_ERROR("[TCP] tcp_channel_client_write. ctx == NULL", ERROR_INVALID_HANDLE);
}
written = send(ctx->fd, buffer, bufferSize, 0);
if (written == SOCKET_ERROR)
{
dwResult = WSAGetLastError();
if (dwResult == WSAEWOULDBLOCK)
{
struct timeval tv = { 0 };
fd_set set = { 0 };
DWORD res = 0;
dprintf("[TCP] tcp_channel_client_write. send returned WSAEWOULDBLOCK, waiting until we can send again...");
while (TRUE)
{
tv.tv_sec = 0;
tv.tv_usec = 1000;
FD_ZERO(&set);
FD_SET(ctx->fd, &set);
res = select(0, NULL, &set, NULL, &tv);
if (res > 0)
{
dwResult = ERROR_SUCCESS;
break;
}
else if (res == SOCKET_ERROR)
{
dwResult = WSAGetLastError();
break;
}
Sleep(100);
}
if (dwResult == ERROR_SUCCESS)
{
continue;
}
else
{
dprintf("[TCP] tcp_channel_client_write. select == SOCKET_ERROR. dwResult=%d", dwResult);
}
}
written = 0;
dprintf("[TCP] tcp_channel_client_write. written == SOCKET_ERROR. dwResult=%d", dwResult);
}
if (bytesWritten)
{
*bytesWritten = written;
}
} while (0);
dprintf("[TCP] tcp_channel_client_write. finished. dwResult=%d, written=%d", dwResult, written);
return dwResult;
}
/*!
* @brief Closes the established connection and cleans up stale state.
* @param channel Pointer to the channel to be closed.
* @param request Pointer to the request packet.
* @param context Pointer to the channel's context.
* @returns indication of success or failure.
* @retval ERROR_SUCCESS the channel was closed successfully.
*/
DWORD tcp_channel_client_close(Channel *channel, Packet *request, LPVOID context)
{
TcpClientContext *ctx = (TcpClientContext *)context;
dprintf( "[TCP] tcp_channel_client_close. channel=0x%08X, ctx=0x%08X", channel, ctx );
if (ctx)
{
// Set the context channel to NULL so we don't try to close the
// channel (since it's already being closed)
ctx->channel = NULL;
// Free the context
free_tcp_client_context(ctx);
// Set the native channel operations context to NULL
channel_set_native_io_context(channel, NULL);
}
return ERROR_SUCCESS;
}
/*!
* @brief Callback for when there is data available on the local side of the TCP client connection.
* @param remote Pointer to the remote that will receive the data.
* @param ctx Pointer to the TCP client context.
* @returns Indication of success or failure.
* @retval ERROR_SUCCESS This value is always returned.
*/
DWORD tcp_channel_client_local_notify(Remote * remote, TcpClientContext * ctx)
{
struct timeval tv = { 0 };
fd_set set = { 0 };
UCHAR buf[16384] = { 0 };
LONG dwBytesRead = 0;
// We select in a loop with a zero second timeout because it's possible
// that we could get a recv notification and a close notification at once,
// so we need some way to make sure that we see them both, otherwise the
// event handle wont get re set to notify us.
do
{
// Reset the notification event
ResetEvent(ctx->notify);
FD_ZERO(&set);
FD_SET(ctx->fd, &set);
tv.tv_sec = 0;
tv.tv_usec = 0;
// Read data from the client connection
dwBytesRead = recv(ctx->fd, buf, sizeof(buf), 0);
if (dwBytesRead == SOCKET_ERROR)
{
DWORD dwError = WSAGetLastError();
// WSAECONNRESET: The connection was forcibly closed by the remote host.
// WSAECONNABORTED: The connection was terminated due to a time-out or other failure.
if (dwError == WSAECONNRESET || dwError == WSAECONNABORTED)
{
dprintf("[TCP] tcp_channel_client_local_notify. [error] closing down channel gracefully. WSAGetLastError=%d", dwError);
// By setting bytesRead to zero, we can ensure we close down the channel gracefully...
dwBytesRead = 0;
}
else if (dwError == WSAEWOULDBLOCK)
{
dprintf("[TCP] tcp_channel_client_local_notify. channel=0x%08X. recv generated a WSAEWOULDBLOCK", ctx->channel);
// break and let the scheduler notify us again if needed.
break;
}
else
{
dprintf("[TCP] tcp_channel_client_local_notify. [error] channel=0x%08X read=0x%.8x (ignored). WSAGetLastError=%d", ctx->channel, dwBytesRead, dwError);
// we loop again because bytesRead is -1.
}
}
if (dwBytesRead == 0)
{
dprintf("[TCP] tcp_channel_client_local_notify. [closed] channel=0x%08X read=0x%.8x", ctx->channel, dwBytesRead);
// Set the native channel operations context to NULL
channel_set_native_io_context(ctx->channel, NULL);
// Sleep for a quarter second
Sleep(250);
// Free the context
free_tcp_client_context(ctx);
// Stop processing
break;
}
else if (dwBytesRead > 0)
{
if (ctx->channel)
{
dprintf("[TCP] tcp_channel_client_local_notify. [data] channel=0x%08X read=%d", ctx->channel, dwBytesRead);
channel_write(ctx->channel, ctx->remote, NULL, 0, buf, dwBytesRead, 0);
}
else
{
dprintf("[TCP] tcp_channel_client_local_notify. [data] channel=<invalid> read=0x%.8x", dwBytesRead);
}
}
} while (select(1, &set, NULL, NULL, &tv) > 0);
return ERROR_SUCCESS;
}
/*!
* @brief Allocates a streaming TCP channel.
* @param remote Pointer to the remote instance.
* @param packet Pointer to the request packet.
* @returns Indication of success or failure.
* @retval ERROR_SUCCESS Opening of the channel succeeded.
* @remarks The request packet needs to contain:
* - \c TLV_TYPE_HOST_NAME - Host to connnect to.
* - \c TLV_TYPE_PORT - Port to connnect to.
*/
DWORD request_net_tcp_client_channel_open(Remote *remote, Packet *packet)
{
Channel *channel = NULL;
Packet *response = packet_create_response(packet);
DWORD result = ERROR_SUCCESS;
LPCSTR host;
DWORD port;
do
{
// No response packet?
if (!response)
{
break;
}
// Extract the hostname and port that we are to connect to
host = packet_get_tlv_value_string(packet, TLV_TYPE_PEER_HOST);
port = packet_get_tlv_value_uint(packet, TLV_TYPE_PEER_PORT);
// Open the TCP channel
if ((result = create_tcp_client_channel(remote, host, (USHORT)(port & 0xffff), &channel)) != ERROR_SUCCESS)
{
break;
}
// Set the channel's identifier on the response
packet_add_tlv_uint(response, TLV_TYPE_CHANNEL_ID, channel_get_id(channel));
} while (0);
// Transmit the response
packet_transmit_response(result, remote, response);
return ERROR_SUCCESS;
}
/*!
* @brief Creates a connection to a remote host and builds a logical channel to represent it.
* @param remote Pointer to the remote instance.
* @param remoteHost The remote host to connect to.
* @param remoteHost The remote port to connect to.
* @param outChannel Pointer that will receive the newly created channel.
* @returns Indication of success or failure.
* @retval ERROR_SUCCESS Creation of the TCP client was successful.
*/
DWORD create_tcp_client_channel(Remote *remote, LPCSTR remoteHost, USHORT remotePort, Channel **outChannel)
{
StreamChannelOps chops;
TcpClientContext *ctx = NULL;
DWORD result = ERROR_SUCCESS;
Channel *channel = NULL;
struct sockaddr_in s;
SOCKET clientFd = 0;
if (outChannel)
{
*outChannel = NULL;
}
dprintf("[TCP] create_tcp_client_channel. host=%s, port=%d", remoteHost, remotePort);
do
{
// Allocate a client socket
if ((clientFd = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, 0)) == INVALID_SOCKET)
{
clientFd = 0;
result = GetLastError();
break;
}
s.sin_family = AF_INET;
s.sin_port = htons(remotePort);
s.sin_addr.s_addr = inet_addr(remoteHost);
// Resolve the host name locally
if (s.sin_addr.s_addr == (DWORD)-1)
{
struct hostent *h;
if (!(h = gethostbyname(remoteHost)))
{
result = GetLastError();
break;
}
memcpy(&s.sin_addr.s_addr, h->h_addr, h->h_length);
}
dprintf("[TCP] create_tcp_client_channel. host=%s, port=%d connecting...", remoteHost, remotePort);
// Try to connect to the host/port
if (connect(clientFd, (struct sockaddr *)&s, sizeof(s)) == SOCKET_ERROR)
{
#ifdef _WIN32
result = WSAGetLastError();
#else
result = errno;
#endif
dprintf("[TCP] create client failed host=%s, port=%d error=%u 0x%x", remoteHost, remotePort, result, result);
break;
}
dprintf("[TCP] create_tcp_client_channel. host=%s, port=%d connected!", remoteHost, remotePort);
// Allocate the client context for tracking the connection
if (!(ctx = (TcpClientContext *)malloc(sizeof(TcpClientContext))))
{
result = ERROR_NOT_ENOUGH_MEMORY;
break;
}
// Initialize the context attributes
memset(ctx, 0, sizeof(TcpClientContext));
ctx->remote = remote;
ctx->fd = clientFd;
// Initialize the channel operations structure
memset(&chops, 0, sizeof(chops));
chops.native.context = ctx;
chops.native.write = tcp_channel_client_write;
chops.native.close = tcp_channel_client_close;
dprintf("[TCP] create_tcp_client_channel. host=%s, port=%d creating the channel", remoteHost, remotePort);
// Allocate an uninitialized channel for associated with this connection
if (!(channel = channel_create_stream(0, 0, &chops)))
{
result = ERROR_NOT_ENOUGH_MEMORY;
break;
}
// Save the channel context association
ctx->channel = channel;
// Finally, create a waitable event and insert it into the scheduler's
// waitable list
dprintf("[TCP] create_tcp_client_channel. host=%s, port=%d creating the notify", remoteHost, remotePort);
if ((ctx->notify = WSACreateEvent()))
{
WSAEventSelect(ctx->fd, ctx->notify, FD_READ | FD_CLOSE);
dprintf("[TCP] create_tcp_client_channel. host=%s, port=%d created the notify %.8x", remoteHost, remotePort, ctx->notify);
scheduler_insert_waitable(ctx->notify, ctx, NULL, (WaitableNotifyRoutine)tcp_channel_client_local_notify, NULL);
}
} while (0);
dprintf("[TCP] create_tcp_client_channel. host=%s, port=%d all done", remoteHost, remotePort);
// Clean up on failure
if (result != ERROR_SUCCESS)
{
dprintf("[TCP] create_tcp_client_channel. host=%s, port=%d cleaning up failed connection", remoteHost, remotePort);
if (ctx)
{
free_tcp_client_context(ctx);
}
if (clientFd)
{
closesocket(clientFd);
}
channel = NULL;
}
if (outChannel)
{
*outChannel = channel;
}
return result;
}
/*!
* @brief Deallocates and cleans up the attributes of a socket context.
* @ctx Pointer to the socket context to free.
*/
VOID free_socket_context(SocketContext *ctx)
{
dprintf("[TCP] free_socket_context. ctx=0x%08X", ctx);
// Close the socket and notification handle
if (ctx->fd)
{
closesocket(ctx->fd);
ctx->fd = 0;
}
if (ctx->channel)
{
channel_close(ctx->channel, ctx->remote, NULL, 0, NULL);
ctx->channel = NULL;
}
if (ctx->notify)
{
dprintf("[TCP] free_socket_context. remove_waitable ctx=0x%08X notify=0x%08X", ctx, ctx->notify);
// The scheduler calls CloseHandle on our WSACreateEvent() for us
scheduler_signal_waitable(ctx->notify, Stop);
ctx->notify = NULL;
}
// Free the context
free(ctx);
}
/*!
* @brief Shuts the socket down for either reading or writing.
* @param remote Pointer to the remote instance.
* @param packet Pointer to the packet.
* @remark The contents of the \c packet indicate whether to stop reading or writing.
* @returns Indication of success or failure.
* @retval ERROR_SUCCESS This value is always returned.
*/
DWORD request_net_socket_tcp_shutdown(Remote *remote, Packet *packet)
{
DWORD dwResult = ERROR_SUCCESS;
Packet * response = NULL;
SocketContext * ctx = NULL;
Channel * channel = NULL;
DWORD cid = 0;
DWORD how = 0;
do
{
dprintf("[TCP] entering request_net_socket_tcp_shutdown");
response = packet_create_response(packet);
if (!response)
{
BREAK_WITH_ERROR("[TCP] request_net_socket_tcp_shutdown. response == NULL", ERROR_NOT_ENOUGH_MEMORY);
}
cid = packet_get_tlv_value_uint(packet, TLV_TYPE_CHANNEL_ID);
how = packet_get_tlv_value_uint(packet, TLV_TYPE_SHUTDOWN_HOW);
channel = channel_find_by_id(cid);
if (!response)
{
BREAK_WITH_ERROR("[TCP] request_net_socket_tcp_shutdown. channel == NULL", ERROR_INVALID_HANDLE);
}
dprintf("[TCP] request_net_socket_tcp_shutdown. channel=0x%08X, cid=%d", channel, cid);
ctx = channel_get_native_io_context(channel);
if (!ctx)
{
BREAK_WITH_ERROR("[TCP] request_net_socket_tcp_shutdown. ctx == NULL", ERROR_INVALID_HANDLE);
}
if (shutdown(ctx->fd, how) == SOCKET_ERROR)
{
BREAK_ON_WSAERROR("[TCP] request_net_socket_tcp_shutdown. shutdown failed");
}
// sf: we dont seem to need to call this here, as the channels tcp_channel_client_local_notify() will
// catch the socket closure and call free_socket_context() for us, due the the FD_READ|FD_CLOSE flags
// being passed to WSAEventSelect for the notify event in create_tcp_client_channel().
// This avoids a double call (from two different threads) and subsequent access violation in some edge cases.
//free_socket_context( ctx );
} while (0);
packet_transmit_response(dwResult, remote, response);
dprintf("[TCP] leaving request_net_socket_tcp_shutdown");
return ERROR_SUCCESS;
}