-
Notifications
You must be signed in to change notification settings - Fork 2
/
tshost.c
599 lines (538 loc) · 10.8 KB
/
tshost.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* TwinSock - "Troy's Windows Sockets"
*
* Copyright (C) 1994 Troy Rollo <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the license in the file LICENSE.TXT included
* with the TwinSock distribution.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <ctype.h>
#include <netinet/in.h>
#include <errno.h>
#ifdef NEED_SELECT_H
#include <sys/select.h>
#endif
#include "twinsock.h"
#include "tx.h"
#include "sockinfo.h"
extern enum Encoding eLine;
extern void PacketTransmitData(void *pvData, int iDataLen, int iStream);
#define BUFFER_SIZE 1024
static fd_set fdsActive;
static fd_set fdsListener;
static fd_set fdsWriting;
static int nLargestFD;
static char achBuffer[BUFFER_SIZE];
static int bFlushing = 0;
void FlushInput(void);
int GetNextExpiry(struct timeval *ptvValue);
void CheckTimers(void);
void WriteSocketData(tws_sockinfo *psi);
extern char *sys_errlist[];
static int nHosts = 0;
static char achDomain[256] = "";
static char aachHosts[4][30];
#define TIMER_ID_SEND 0
#define TIMER_ID_RECEIVE 1
#define TIMER_ID_FLUSH 2
int BumpLargestFD(int iValue)
{
if (iValue > nLargestFD)
nLargestFD = iValue;
FD_SET(iValue, &fdsActive);
};
int SetListener(int iValue)
{
FD_SET(iValue, &fdsListener);
}
int SetWriter(int iValue)
{
if (iValue > nLargestFD)
nLargestFD = iValue;
FD_SET(iValue, &fdsWriting);
}
int SetClosed(int iValue)
{
FD_CLR(iValue, &fdsListener);
FD_CLR(iValue, &fdsActive);
FD_CLR(iValue, &fdsWriting);
}
void
read_resolver(void)
{
FILE *fp;
char achInput[80];
char *pchIn, *pchOut;
fp = fopen("/etc/resolv.conf", "r");
if (!fp)
{
fp = fopen("/usr/etc/resolv.conf", "r");
if (!fp)
return;
}
while (fgets(achInput, 80, fp))
{
if (achInput[strlen(achInput) - 1] == '\n')
achInput[strlen(achInput) - 1] = 0;
pchIn = achInput;
while (isspace(*pchIn))
pchIn++;
if (!strncmp(pchIn, "domain", 6))
{
pchIn += 6;
while (isspace(*pchIn))
pchIn++;
pchOut = achDomain;
while (*pchIn && !isspace(*pchIn))
*pchOut++ = *pchIn++;
*pchOut = 0;
}
else if (!strncmp(pchIn, "nameserver", 10) && nHosts < 4)
{
pchIn += 10;
while (isspace(*pchIn))
pchIn++;
pchOut = aachHosts[nHosts];
while (*pchIn && !isspace(*pchIn))
*pchOut++ = *pchIn++;
*pchOut = 0;
nHosts++;
}
}
fclose(fp);
}
static char achHostName[256];
main(int argc, char **argv)
{
fd_set fdsRead, fdsWrite, fdsExcept, fdsDummy;
int nRead;
int i;
int iResult;
struct sockaddr_in saSource;
int nSourceLen;
int s;
struct timeval tvZero;
struct timeval tv;
while (argc-- > 1)
{
argv++;
if (**argv =='-')
{
while (*++*argv)
{
switch(**argv)
{
case '8':
eLine = E_8Bit;
break;
case 'n':
eLine = E_8NoCtrl;
break;
case 'N':
eLine = E_8NoHiCtrl;
break;
case 'x':
eLine = E_8NoX;
break;
case 'X':
eLine = E_8NoHiX;
break;
case 'e':
eLine = E_Explicit;
break;
}
}
}
}
read_resolver();
fprintf(stderr, "TwinSock Host 2.0\n");
fprintf(stderr, "Copyright 1994-1995 Troy Rollo\n");
fprintf(stderr, "This program is free software\n");
fprintf(stderr, "See the file COPYING for details\n");
fprintf(stderr, "\nStart your TwinSock client now\n");
if (isatty(0))
InitTerm();
InitProtocol();
*achHostName = 0;
gethostname(achHostName, 256);
fprintf(stdout, "!@$TSStart%d:%s", (int) eLine, achHostName);
fprintf(stdout, "#%s", achDomain);
for ( i = 0; i < nHosts; i++)
fprintf(stdout, "*%s", aachHosts[i]);
fprintf(stdout, "$@");
fflush(stdout);
nLargestFD = 0;
FD_ZERO(&fdsActive);
FD_ZERO(&fdsExcept);
FD_ZERO(&fdsWriting);
FD_SET(0, &fdsActive);
while(1)
{
fdsRead = fdsActive;
fdsWrite = fdsWriting;
iResult = select(nLargestFD + 1,
&fdsRead,
&fdsWrite,
&fdsExcept,
GetNextExpiry(&tv) ? &tv : 0);
CheckTimers();
if (iResult <= 0)
continue;
if (FD_ISSET(0, &fdsRead))
{
nRead = read(0, achBuffer, BUFFER_SIZE);
if (nRead > 0)
{
if (bFlushing)
FlushInput();
else
PacketReceiveData(achBuffer, nRead);
}
else if (nRead == 0 && !isatty(0))
{
exit(0);
}
}
for (i = 3; i <= nLargestFD; i++)
{
if (FD_ISSET(i, &fdsWrite))
{
tws_sockinfo *psi;
FD_CLR(i, &fdsWriting);
psi = FindSocketEntry(i);
if (!psi)
continue;
if (psi->ptxrConnect)
FinishConnect(psi);
WriteSocketData(psi);
}
if (FD_ISSET(i, &fdsRead))
{
FD_ZERO(&fdsDummy);
FD_SET(i, &fdsDummy);
tvZero.tv_sec = tvZero.tv_usec = 0;
if (select(i + 1,
&fdsDummy,
&fdsWrite,
&fdsExcept,
&tvZero) != 1)
continue; /* Select lied */
nSourceLen = sizeof(saSource);
if (FD_ISSET(i, &fdsListener))
{
int iClient;
s = accept(i,
&saSource,
&nSourceLen);
if (s == -1)
continue;
iClient = GetServerSocket();
AddSocketEntry(iClient, s);
BumpLargestFD(s);
s = htonl(iClient);
SendSocketData(GetClientFromServer(i),
&s,
sizeof(s),
&saSource,
nSourceLen,
FN_Accept);
}
else
{
nRead = recvfrom(i,
achBuffer,
BUFFER_SIZE,
0,
&saSource,
&nSourceLen);
if (nRead < 0 &&
errno == ENOTCONN)
{
/* Was a datagram socket */
FD_CLR(i, &fdsActive);
continue;
}
if (nRead >= 0)
SendSocketData(GetClientFromServer(i),
achBuffer,
nRead,
&saSource,
nSourceLen,
FN_Data);
if (nRead == 0)
SetClosed(i);
}
}
}
}
}
void
SetTransmitTimeout(void)
{
KillTimer(TIMER_ID_SEND);
SetTimer(TIMER_ID_SEND, 10000);
}
void
KillTransmitTimeout(void)
{
KillTimer(TIMER_ID_SEND);
}
void SetReceiveTimeout(void)
{
KillTimer(TIMER_ID_RECEIVE);
SetTimer(TIMER_ID_RECEIVE, 1500);
}
void KillReceiveTimeout(void)
{
KillTimer(TIMER_ID_RECEIVE);
}
static struct timeval atvTimers[3];
static int iTimersOn;
static int iTimerRunning;
static int iInTimer = 0;
int FireTimer(int iTimer)
{
switch(iTimer)
{
case TIMER_ID_SEND:
TimeoutReceived();
break;
case TIMER_ID_RECEIVE:
PacketReceiveData(0, 0);
break;
case TIMER_ID_FLUSH:
bFlushing = 0;
break;
}
}
int GetNextExpiry(struct timeval *ptvValue)
{
struct timeval tvNow;
struct timezone tzDummy;
struct timeval tvGo;
int i;
if (!iTimersOn)
return 0;
tvGo.tv_sec = 0x7fffffff;
tvGo.tv_usec = 0;
for (i = 0; i < 3; i++)
{
if (!(iTimersOn & (1 << i)))
continue;
if (atvTimers[i].tv_sec < tvGo.tv_sec ||
(atvTimers[i].tv_sec == tvGo.tv_sec &&
atvTimers[i].tv_usec < tvGo.tv_usec))
{
tvGo = atvTimers[i];
iTimerRunning = i;
}
}
gettimeofday(&tvNow, &tzDummy);
ptvValue->tv_sec = tvGo.tv_sec - tvNow.tv_sec;
ptvValue->tv_usec = tvGo.tv_usec - tvNow.tv_usec;
while (ptvValue->tv_usec < 0)
{
ptvValue->tv_usec += 1000000l;
ptvValue->tv_sec--;
}
while (ptvValue->tv_usec >= 1000000l)
{
ptvValue->tv_usec -= 1000000l;
ptvValue->tv_sec++;
}
if (ptvValue->tv_sec < 0)
{
ptvValue->tv_sec = 0;
ptvValue->tv_usec = 100;
}
if (!ptvValue->tv_sec &&
ptvValue->tv_usec < 100)
ptvValue->tv_usec = 100;
return 1;
}
void CheckTimers(void)
{
struct timeval tvNow;
struct timezone tzDummy;
int i;
gettimeofday(&tvNow, &tzDummy);
for (i = 0; i < 3; i++)
{
if (!(iTimersOn & (1 << i)))
continue;
if (atvTimers[i].tv_sec < tvNow.tv_sec ||
(atvTimers[i].tv_sec == tvNow.tv_sec &&
atvTimers[i].tv_usec < tvNow.tv_usec))
{
KillTimer(i);
FireTimer(i);
}
}
}
int SetTimer(int idTimer, int iTime)
{
struct timeval tvNow;
struct timezone tzDummy;
gettimeofday(&tvNow, &tzDummy);
atvTimers[idTimer] = tvNow;
atvTimers[idTimer].tv_usec += (long) iTime % 1000l * 1000l;
atvTimers[idTimer].tv_sec += iTime / 1000;
while (atvTimers[idTimer].tv_usec > 1000000l)
{
atvTimers[idTimer].tv_sec++;
atvTimers[idTimer].tv_usec -= 1000000l;
}
iTimersOn |= (1 << idTimer);
}
int KillTimer(int idTimer)
{
iTimersOn &= ~(1 << idTimer);
}
void Shutdown(void)
{
if (isatty(0))
UnInitTerm();
fprintf(stderr, "\nTwinSock Host Finished\n");
exit(0);
}
int
SendData(void *pvData, int iDataLen)
{
int iLen;
int nWritten;
if (bFlushing)
return iDataLen; /* Lie */
iLen = iDataLen;
while (iLen > 0)
{
nWritten = write(1, pvData, iDataLen);
if (nWritten > 0)
{
pvData = (char *) pvData + nWritten;
iLen -= nWritten;
}
}
return iDataLen;
}
void
FlushInput(void)
{
bFlushing = 1;
SetTimer(TIMER_ID_FLUSH, 1500);
}
static void
SendInitResponse(void)
{
struct tx_request txr;
txr.iType = htons(FN_Init);
txr.nArgs = 0;
txr.nLen = htons(10);
txr.id = -1;
txr.nError = 0;
PacketTransmitData(&txr, 10, -2);
}
void
DataReceived(void *pvData, int iLen)
{
static struct tx_request *ptxr = 0;
static struct tx_request txrHeader;
static int nBytes = 0;
short nPktLen;
enum Functions ft;
int nCopy;
while (iLen)
{
if (nBytes < 10)
{
nCopy = 10 - nBytes;
if (nCopy > iLen)
nCopy = iLen;
memcpy((char *) &txrHeader + nBytes, pvData, nCopy);
nBytes += nCopy;
pvData = (char *) pvData + nCopy;
iLen -= nCopy;
if (nBytes == 10)
{
nPktLen = ntohs(txrHeader.nLen);
ptxr = (struct tx_request *) malloc(sizeof(struct tx_request) + nPktLen - 10);
memcpy(ptxr, &txrHeader, 10);
}
}
if (nBytes >= 10)
{
nPktLen = ntohs(txrHeader.nLen);
ft = (enum Functions) ntohs(txrHeader.iType);
nCopy = nPktLen - nBytes;
if (nCopy > iLen)
nCopy = iLen;
if (nCopy)
{
memcpy((char *) ptxr + nBytes, pvData, nCopy);
nBytes += nCopy;
pvData = (char *) pvData + nCopy;
iLen -= nCopy;
}
if (nBytes == nPktLen)
{
if (ft == FN_Init)
SendInitResponse();
else
ResponseReceived(ptxr);
free(ptxr);
ptxr = 0;
nBytes = 0;
}
}
}
}
void
WriteSocketData(tws_sockinfo *psi)
{
int iResult;
if (!psi->pdata)
return;
if (psi->pdata->bTo)
{
iResult = sendto(psi->iServerSocket,
psi->pdata->pchData + psi->pdata->iLoc,
psi->pdata->nBytes - psi->pdata->iLoc,
psi->pdata->iFlags,
(struct sockaddr *) &psi->pdata->sinDest,
sizeof(struct sockaddr_in));
}
else
{
iResult = sendto(psi->iServerSocket,
psi->pdata->pchData + psi->pdata->iLoc,
psi->pdata->nBytes - psi->pdata->iLoc,
psi->pdata->iFlags);
}
if (iResult >= 0)
{
tws_data *pdata = psi->pdata;
pdata->iLoc += iResult;
if (pdata->iLoc == pdata->nBytes)
{
psi->pdata = pdata->pdataNext;
free(pdata->pchData);
free(pdata);
}
}
else
{
if (errno != EWOULDBLOCK &&
errno != EINTR &&
errno != EAGAIN)
return; /* The socket is dead */
}
if (psi->pdata)
FD_SET(psi->iServerSocket, &fdsWriting);
}