-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpipereader.cpp
More file actions
423 lines (371 loc) · 13.6 KB
/
pipereader.cpp
File metadata and controls
423 lines (371 loc) · 13.6 KB
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
/*
* Copyright (c) 2014 Citrix Systems, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "stdafx.h"
#include <winsock2.h>
#include <windows.h>
#include "plj_utils.h"
#include "spinlock.h"
#include "circle.h"
#include "threadcontrol.h"
#include "logger.h"
#include "pipereader.h"
PipeReader::PipeReader()
{
pipe = INVALID_HANDLE_VALUE;
}
PipeReader::~PipeReader()
{
if (pipe != INVALID_HANDLE_VALUE)
{
CloseHandle(pipe);
pipe = INVALID_HANDLE_VALUE;
}
}
bool PipeReader::Initialize(char * PipeName, Logger * Logger, class Connectoid * Sock, bool StartThread, bool Client, char * Tag)
{
Connectoid::Initialize(Logger, Sock, Tag);
client = Client;
pipeName = PipeName;
receiveBuffer.Initialize(PUMP_BUFFER_SIZE);
//
// If we are spinning a thread for this pipe, spin it.
//
if (StartThread != false)
{
if (!Start())
{
die("failed to initialize/start pipe reader thread.");
}
}
return true;
}
int PipeReader::Send(Circle * buffer)
{
BOOL success;
DWORD written;
DWORD previouslyWritten = 0;
if (!receiveDataSeen)
{
//
// Drop incoming data on floor.
//
buffer->Drain();
return 0;
}
int lastError = 0;
if (write.Pending())
{
//
// Check result from previous write.
//
success = GetOverlappedResult(pipe, write.Overlapped(), &previouslyWritten, false);
log->Log(TAG_XMIT_DBG, tag, "write: GetOverlapResult returned %d, oh = %p", success, write.GetHandle());
if (!success)
{
lastError = GetLastError();
if (lastError == ERROR_IO_PENDING)
{
//
// The previous write is not yet complete. We can't
// initiate another until it is.
//
log->Log(TAG_XMIT_DBG, tag, "write: GetOverlapResult returned ERROR_IO_PENDING, we shouldn't have come here!!!");
return 0;
}
else
{
die("pipe write failed, error %d\n", lastError);
}
}
write.SetPending(false);
// log->Log(TAG_SENT, tag, previouslyWritten, NULL);
{
char * bufftmp;
DWORD lentmp;
lentmp = buffer->QueryContiguousRetrievable(&bufftmp);
ASSERT(lentmp >= previouslyWritten);
log->Log(TAG_SENT, tag, previouslyWritten, bufftmp);
}
buffer->CommitRetrieved(previouslyWritten);
}
//
// Initiate a write.
//
char * buff;
int length = buffer->QueryContiguousRetrievable(&buff);
if (length == 0)
{
//
// Nothing available to be written just yet.
//
log->Log(TAG_XMIT_DBG, tag, "write: nothing to write");
return 0;
}
log->Log(TAG_SEND, tag, length, NULL);
log->Log(TAG_XMIT_DBG, tag, "initiate %d byte write oh = %p", length, write.GetHandle());
success = WriteFile(pipe, buff, length, &written, write.Overlapped());
if (success)
{
// log->Log(TAG_SENT, tag, written, NULL);
log->Log(TAG_SENT, tag, written, buff);
log->Log(TAG_XMIT_DBG, tag, "write: instant gratification %d bytes on oh = %p", written, write.GetHandle());
return buffer->CommitRetrieved(written) + previouslyWritten;
}
lastError = GetLastError();
if (lastError == ERROR_IO_PENDING)
{
write.SetPending(true);
sender->SetWriteWait(true);
log->Log(TAG_XMIT_DBG, tag, "write: initiated send %d bytes, new state == pending, oh = %p", length, write.GetHandle());
return previouslyWritten;
}
g_shutdown = true;
die("pipe write failed, error %d\n", lastError);
}
int PipeReader::Receive(char * buffer, int length)
{
BOOL success;
DWORD received = 0;
if (read.Pending())
{
//
// Check result from previous read.
//
success = GetOverlappedResult(pipe, read.Overlapped(), &received, false);
log->Log(TAG_READ_DBG, tag, "read: GetOverlappedResult status %d, count %d, oh = %p", success, received, read.GetHandle());
if (!success)
{
int lastError = GetLastError();
if (lastError == ERROR_IO_PENDING)
{
//
// The previous read is not yet complete. We can't
// initiate another until it is.
//
log->Log(TAG_READ_DBG, tag, "read: still pending, this shouldn't have happpened *****************");
return 0;
}
if (lastError == ERROR_IO_INCOMPLETE)
{
log->Log(TAG_READ_DBG, tag, "read: ERROR_IO_INCOMPLETE, this shouldn't have happened ***************");
fprintf(stderr, "E_IO_INCOMPLETE read from %.3s -- this shouldn't happen, ignoring.\n", tag);
return 0;
}
else
{
die("pipe read failed, error %d\n", lastError);
}
}
read.SetPending(false);
log->Log(TAG_READ_DBG, tag, "read: delayed read complete, received %d bytes.", received);
return received;
}
log->Log(TAG_READ_DBG, tag, "read: initiating read %d bytes (max) oh %p", length, read.GetHandle());
success = ReadFile(pipe, buffer, length, &received, read.Overlapped());
if (!success)
{
int lastError = GetLastError();
if (lastError == ERROR_IO_PENDING)
{
log->Log(TAG_READ_DBG, tag, 0, "read: pending.");
read.SetPending(true);
SetReadWait(true); // ick, needs cleaning.
}
else if (lastError == ERROR_PIPE_LISTENING)
{
Sleep(500);
log->Log(TAG_READ_DBG, tag, "read: nobody is listening.");
read.SetOverlappedEvent(); // make sure the big wait will not wait for ever
}
else
{
fprintf(stderr, "read pipe failed %d\n", lastError);
// For now, get us out of here.
g_shutdown = true;
my_exit(0);
}
return 0;
}
log->Log(TAG_READ_DBG, tag, "read: instantly returning %d bytes", received);
return received;
}
void PipeReader::Pump()
{
char buf[PUMP_BUFFER_SIZE];
bool pljtmpOnce = 0;
connected = true;
while (g_shutdown == false)
{
int received = 0;
int sent = 0;
if (!wait.read)
{
//
// If there's room in the recieve buffer, attempt to receive
// some.
int receivable = receiveBuffer.QueryInsertable();
if (receivable != 0)
{
if ((received = Receive(buf, receivable)) != 0)
{
if (receiveDataSeen == false)
{
printf("pipe connected.\n");
log->Log(TAG_READ_DBG, tag, "Recieve data from debugger.\n");
receiveDataSeen = true;
}
log->Log(TAG_READ, tag, received, buf);
if (sender->Connected() == false)
{
// Ignore all incoming data until there is something
// connected to send it to.
printf("pipereader: dropping %d bytes because socket is not connected.\n",
received);
log->Log(TAG_READ_DBG, tag,
"dropping %d bytes because socket is not connected.\n",
received);
received = 0;
}
if (resetAckExpected)
{
// Ignore all incoming data until the other side tells
// us a reset ack has been seen.
log->Log(TAG_READ_DBG, tag,
"dropping %d bytes, waiting for reset ack from target.\n",
received);
received = 0;
}
int count = CheckForResetPacket(buf, received);
if (count != 0)
{
log->Log(TAG_READ_DBG, tag, "RESET packet received.\n");
if (!resetAckExpected)
{
printf("debugger sent RESET.\n");
}
// Truncate received data to end of reset packet. We
// discard all remaining data until we see a reset ack
// from the other end. Note: On send the reset packet
// is usually followed by 8 bytes of zero, the target
// seems to depend on it ... and being as we truncated
// and we don't know we got them all yet anyway and we
// will drop all further data until we see something,
// better send the zeros along.
receiveBuffer.Insert(buf, count);
int z = 0;
receiveBuffer.Insert((char *)&z, sizeof(z));
receiveBuffer.Insert((char *)&z, sizeof(z));
received = 0;
SetResetAckExpected(true);
}
receiveBuffer.Insert(buf, received);
}
}
}
if (!wait.write)
{
//
// Attempt to send.
//
// Note: The Send routine will take care of the no data case.
//
sent = sender->Send(&receiveBuffer);
}
if (!(sent | received))
{
//
// Nothing sent, nothing received, wait until something happens
// on one end or the other.
//
Wait();
}
}
connected = false;
}
void PipeReader::ThreadEntry(void * inContext)
{
if (client == false)
{
//
// Create pipe for server (that other end will connect to).
//
pipe = CreateNamedPipe(pipeName,
PIPE_ACCESS_DUPLEX
// | FILE_FLAG_FIRST_PIPE_INSTANCE
// | FILE_FLAG_WRITE_THROUGH
| FILE_FLAG_OVERLAPPED
,
PIPE_TYPE_BYTE
// | PIPE_WAIT
,
1,
4096,
4096,
INFINITE,
NULL);
if (pipe == INVALID_HANDLE_VALUE)
{
die("failed to open pipe, err = %d.", GetLastError());
}
}
else
{
//
// Create pipe for client. Use CreateFile because, oddly, CreateNamedPipe
// seems to fail if you use the \\servername\pipe\pipename format. Sadly,
// this too will fail if the other end doesn't exist so we need to do it
// in the spun thread. We did spin a thread right?
//
bool first = TRUE;
do
{
pipe = CreateFile(pipeName, // pipe name
GENERIC_READ | GENERIC_WRITE, // read and write access
0, // no sharing
NULL, // default security
OPEN_ALWAYS, // sample says open existing
FILE_FLAG_WRITE_THROUGH // push the data to the other end
| FILE_FLAG_OVERLAPPED // don't wait
,
NULL); // why would i want a template?
if (pipe == INVALID_HANDLE_VALUE)
{
DWORD err = GetLastError();
if (err != ERROR_FILE_NOT_FOUND)
{
die("failed to open pipe, err = %d.", err);
}
if (first)
{
printf("pipe client waiting for remote pipe to become available.\n");
first = false;
}
Sleep(1700);
}
} while (pipe == INVALID_HANDLE_VALUE);
}
printf("hello from the pipe reader.\n");
//
// Now pass data back and forth across the connections.
//
Pump();
}