aboutsummaryrefslogtreecommitdiffstats
path: root/ncpd/link.cc
blob: a52e3ef7fea2942d828ab07c72323ad9d0373fdf (plain)
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
/*-*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 1999  Philip Proudman <philip.proudman@btinternet.com>
 *  Copyright (C) 1999-2002 Fritz Elfert <felfert@to.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stream.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
#include <plp_inttypes.h>

#include "link.h"
#include "packet.h"
#include "ncp.h"
#include "bufferstore.h"
#include "bufferarray.h"

extern "C" {
    static void *expire_check(void *arg)
    {
	Link *l = (Link *)arg;
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	while (1) {
	    usleep(l->retransTimeout * 500);
	    l->retransmit();
	}
    }
};

Link::Link(const char *fname, int baud, ncp *_ncp, unsigned short _verbose)
{
    p = new packet(fname, baud, this, _verbose);
    retransTimeout = ((unsigned long)baud * 1000 / 13200) + 200;
    theNCP = _ncp;
    verbose = _verbose;
    txSequence = 1;
    rxSequence = -1;
    failed = false;
    seqMask = 7;
    maxOutstanding = 1;
    for (int i = 0; i < 256; i++)
	xoff[i] = false;
    // generate magic number for sendCon()
    srandom(time(NULL));
    conMagic = random();

    pthread_mutex_init(&queueMutex, NULL);
    pthread_create(&checkthread, NULL, expire_check, this);

    // submit a link request
    bufferStore blank;
    transmit(blank);
}

Link::~Link()
{
    flush();
    pthread_cancel(checkthread);
    pthread_mutex_destroy(&queueMutex);
    delete p;
}

void Link::
reset() {
    txSequence = 1;
    rxSequence = -1;
    failed = false;

    pthread_mutex_lock(&queueMutex);
    ackWaitQueue.clear();
    holdQueue.clear();
    pthread_mutex_unlock(&queueMutex);
    for (int i = 0; i < 256; i++)
	xoff[i] = false;

    // submit a link request
    bufferStore blank;
    transmit(blank);
}

unsigned short Link::
getVerbose()
{
    return verbose;
}

void Link::
setVerbose(unsigned short _verbose)
{
    verbose = _verbose;
    p->setVerbose(verbose);
}

void Link::
send(const bufferStore & buff)
{
    if (buff.getLen() > 300) {
	failed = true;
    } else
	transmit(buff);
}

void Link::
purgeQueue(int channel)
{
    pthread_mutex_lock(&queueMutex);
    vector<ackWaitQueueElement>::iterator i;
    for (i = ackWaitQueue.begin(); i != ackWaitQueue.end(); i++)
	if (i->data.getByte(0) == channel) {
	    ackWaitQueue.erase(i);
	    i--;
	}
    vector<bufferStore>::iterator j;
    for (j = holdQueue.begin(); j != holdQueue.end(); j++)
	if (j->getByte(0) == channel) {
	    holdQueue.erase(j);
	    j--;
	}
    pthread_mutex_unlock(&queueMutex);
}

void Link::
sendAck(int seq)
{
    if (hasFailed())
	return;
    bufferStore tmp;
    if (verbose & LNK_DEBUG_LOG)
	cout << "Link: >> ack seq=" << seq << endl;
    if (seq > 7) {
	int hseq = seq >> 3;
	int lseq = (seq & 7) | 8;
	seq = (hseq << 8) + lseq;
	tmp.prependWord(seq);
    } else
	tmp.prependByte(seq);
    p->send(tmp);
}

void Link::
sendCon()
{
    if (hasFailed())
	return;
    bufferStore tmp;
    if (verbose & LNK_DEBUG_LOG)
	cout << "Link: >> con seq=4" << endl;
    tmp.addByte(0x24);
    tmp.addDWord(conMagic);
    ackWaitQueueElement e;
    e.seq = 0; // expected ACK is 0, _NOT_ 4!
    gettimeofday(&e.stamp, NULL);
    e.data = tmp;
    e.txcount = 4;
    pthread_mutex_lock(&queueMutex);
    ackWaitQueue.push_back(e);
    pthread_mutex_unlock(&queueMutex);
    p->send(tmp);
}

void Link::
receive(bufferStore buff)
{
    vector<ackWaitQueueElement>::iterator i;
    bool ackFound;
    bool conFound;
    int type = buff.getByte(0);
    int seq = type & 0x0f;

    type &= 0xf0;
    // Support for incoming extended sequence numbers
    if (seq & 8) {
	int tseq = buff.getByte(1);
	buff.discardFirstBytes(2);
	seq = (tseq << 3) + (seq & 0x07);
    } else
	buff.discardFirstBytes(1);

    switch (type) {
	case 0x30:
	    // Normal data
	    if (verbose & LNK_DEBUG_LOG) {
		cout << "Link: << dat seq=" << seq ;
		if (verbose & LNK_DEBUG_DUMP)
		    cout << " " << buff << endl;
		else
		    cout << " len=" << buff.getLen() << endl;
	    }
	    sendAck((rxSequence+1) & seqMask);

	    if (((rxSequence + 1) & seqMask) == seq) {
		rxSequence++;
		rxSequence &= seqMask;

		// Must check for XOFF/XON ncp frames HERE!
		if ((buff.getLen() == 3) && (buff.getByte(0) == 0)) {
		    switch (buff.getByte(2)) {
			case 1:
			    // XOFF
			    xoff[buff.getByte(1)] = true;
			    if (verbose & LNK_DEBUG_LOG)
				cout << "Link: got XOFF for channel "
				     << buff.getByte(1) << endl;
			    break;
			case 2:
			    // XON
			    xoff[buff.getByte(1)] = false;
			    if (verbose & LNK_DEBUG_LOG)
				cout << "Link: got XON for channel "
				     << buff.getByte(1) << endl;
			    // Transmit packets on hold queue
			    transmitHoldQueue(buff.getByte(1));
			    break;
			default:
			    theNCP->receive(buff);
		    }
		} else
		    theNCP->receive(buff);

	    } else {
		if (verbose & LNK_DEBUG_LOG)
		    cout << "Link: DUP\n";
	    }
	    break;

	case 0x00:
	    // Incoming ack
	    // Find corresponding packet in ackWaitQueue
	    ackFound = false;
	    struct timeval refstamp;
	    pthread_mutex_lock(&queueMutex);
	    for (i = ackWaitQueue.begin(); i != ackWaitQueue.end(); i++)
		if (i->seq == seq) {
		    ackFound = true;
		    refstamp = i->stamp;
		    ackWaitQueue.erase(i);
		    if (verbose & LNK_DEBUG_LOG) {
			cout << "Link: << ack seq=" << seq ;
			if (verbose & LNK_DEBUG_DUMP)
			    cout << " " << buff;
			cout << endl;
		    }
		    break;
		}
	    pthread_mutex_unlock(&queueMutex);
	    if (ackFound) {
		// Older packets implicitely ack'ed
		multiAck(refstamp);
		// Transmit waiting packets
		transmitWaitQueue();
	    } else {
		if (verbose & LNK_DEBUG_LOG) {
		    cout << "Link: << UNMATCHED ack seq=" << seq ;
		    if (verbose & LNK_DEBUG_DUMP)
			cout << " " << buff;
		    cout << endl;
		}
	    }
	    break;

	case 0x20:
	    // New link
	    conFound = false;
	    if (seq > 3) {
		// May be a link confirm packet (EPOC)
		pthread_mutex_lock(&queueMutex);
		for (i = ackWaitQueue.begin(); i != ackWaitQueue.end(); i++)
		    if ((i->seq > 0) && (i->seq < 4) &&
			(i->data.getByte(0) & 0xf0) == 0x20) {
			ackWaitQueue.erase(i);
			conFound = true;
			// EPOC can handle extended sequence numbers
			seqMask = 0x7ff;
			// EPOC can handle up to 8 unacknowledged packets
			maxOutstanding = 8;
			p->setEpoc(true);
			if (verbose & LNK_DEBUG_LOG) {
			    cout << "Link: << con seq=" << seq ;
			    if (verbose & LNK_DEBUG_DUMP)
				cout << " " << buff;
			    cout << endl;
			}
			break;
		    }
		pthread_mutex_unlock(&queueMutex);
	    }
	    if (conFound) {
		rxSequence = 0;
		txSequence = 1;
		sendAck(rxSequence);
	    } else {
		if (verbose & LNK_DEBUG_LOG) {
		    cout << "Link: << req seq=" << seq;
		    if (verbose & LNK_DEBUG_DUMP)
			cout << " " << buff;
		    cout << endl;
		}
		rxSequence = txSequence = 0;
		if (seq > 0) {
		    // EPOC can handle extended sequence numbers
		    seqMask = 0x7ff;
		    // EPOC can handle up to 8 unacknowledged packets
		    maxOutstanding = 8;
		    p->setEpoc(true);
		    sendCon();
		} else
		    sendAck(rxSequence);
	    }
	    break;

	case 0x10:
	    // Disconnect
	    if (verbose & LNK_DEBUG_LOG)
		cout << "Link: << DISC" << endl;
	    failed = true;
	    break;

	default:
	    cerr << "Link: FATAL: Unknown packet type " << type << endl;
    }
}

void Link::
transmitHoldQueue(int channel)
{
    vector<bufferStore> tmpQueue;
    vector<bufferStore>::iterator i;

    // First, move desired packets to a temporary queue
    pthread_mutex_lock(&queueMutex);
    for (i = holdQueue.begin(); i != holdQueue.end(); i++)
	if (i->getByte(0) == channel) {
	    tmpQueue.push_back(*i);
	    holdQueue.erase(i);
	    i--;
	}
    pthread_mutex_unlock(&queueMutex);

    // ... then transmit the moved packets
    for (i = tmpQueue.begin(); i != tmpQueue.end(); i++)
	transmit(*i);
}

void Link::
transmitWaitQueue()
{
    vector<bufferStore> tmpQueue;
    vector<bufferStore>::iterator i;

    // First, move desired packets to a temporary queue
    for (i = waitQueue.begin(); i != waitQueue.end(); i++)
	tmpQueue.push_back(*i);
    waitQueue.clear();
    // transmit the moved packets. If the backlock gets
    // full, they are put into waitQueue again.
    for (i = tmpQueue.begin(); i != tmpQueue.end(); i++)
	transmit(*i);
}

void Link::
transmit(bufferStore buf)
{
    if (hasFailed())
	return;

    int remoteChan = buf.getByte(0);
    if (xoff[remoteChan]) {
	pthread_mutex_lock(&queueMutex);
	holdQueue.push_back(buf);
	pthread_mutex_unlock(&queueMutex);
    } else {

	// If backlock is full, put on waitQueue
	int ql;
	pthread_mutex_lock(&queueMutex);
	ql = ackWaitQueue.size();
	pthread_mutex_unlock(&queueMutex);
	if (ql >= maxOutstanding) {
	    waitQueue.push_back(buf);
	    return;
	}

	ackWaitQueueElement e;
	e.seq = txSequence++;
	txSequence &= seqMask;
	gettimeofday(&e.stamp, NULL);
	// An empty buffer is considered a new link request
	if (buf.empty()) {
	    // Request for new link
	    e.txcount = 4;
	    if (verbose & LNK_DEBUG_LOG)
		cout << "Link: >> req seq=" << e.seq << endl;
	    buf.prependByte(0x20 + e.seq);
	} else {
	    e.txcount = 8;
	    if (verbose & LNK_DEBUG_LOG) {
		cout << "Link: >> dat seq=" << e.seq;
		if (verbose & LNK_DEBUG_DUMP)
		    cout << " " << buf;
		cout << endl;
	    }
	    if (e.seq > 7) {
		int hseq = e.seq >> 3;
		int lseq = 0x30 + ((e.seq & 7) | 8);
		int seq = (hseq << 8) + lseq;
		buf.prependWord(seq);
	    } else
		buf.prependByte(0x30 + e.seq);
	}
	e.data = buf;
	pthread_mutex_lock(&queueMutex);
	ackWaitQueue.push_back(e);
	pthread_mutex_unlock(&queueMutex);
	p->send(buf);
    }
}

static void
timesub(struct timeval *tv, unsigned long millisecs)
{
    uint64_t micros = tv->tv_sec;
    uint64_t sub = millisecs;

    micros <<= 32;
    micros += tv->tv_usec;
    micros -= (sub * 1000);
    tv->tv_usec = micros & 0xffffffff;
    tv->tv_sec  = (micros >>= 32) & 0xffffffff;
}

static bool
olderthan(struct timeval t1, struct timeval t2)
{
    uint64_t m1 = t1.tv_sec;
    uint64_t m2 = t2.tv_sec;
    m1 <<= 32;
    m2 <<= 32;
    m1 += t1.tv_usec;
    m2 += t2.tv_usec;
    return (m1 < m2);
}

void Link::
multiAck(struct timeval refstamp)
{
    vector<ackWaitQueueElement>::iterator i;
    pthread_mutex_lock(&queueMutex);
    for (i = ackWaitQueue.begin(); i != ackWaitQueue.end(); i++)
	if (olderthan(i->stamp, refstamp)) {
	    ackWaitQueue.erase(i);
	    i--;
	}
    pthread_mutex_unlock(&queueMutex);
}

void Link::
retransmit()
{

    if (hasFailed()) {
	pthread_mutex_lock(&queueMutex);
	ackWaitQueue.clear();
	holdQueue.clear();
	pthread_mutex_unlock(&queueMutex);
	return;
    }

    pthread_mutex_lock(&queueMutex);
    vector<ackWaitQueueElement>::iterator i;
    struct timeval now;
    gettimeofday(&now, NULL);
    struct timeval expired = now;
    timesub(&expired, retransTimeout);
    for (i = ackWaitQueue.begin(); i != ackWaitQueue.end(); i++)
	if (olderthan(i->stamp, expired)) {
	    if (i->txcount-- == 0) {
		// timeout, remove packet
		if (verbose & LNK_DEBUG_LOG)
		    cout << "Link: >> TRANSMIT timeout seq=" << i->seq << endl;
		ackWaitQueue.erase(i);
		i--;
	    } else {
		// retransmit it
		i->stamp = now;
		if (verbose & LNK_DEBUG_LOG)
		    cout << "Link: >> RETRANSMIT seq=" << i->seq << endl;
		p->send(i->data);
	    }
	}
    pthread_mutex_unlock(&queueMutex);
}

void Link::
flush() {
    while (stuffToSend())
	sleep(1);
}

bool Link::
stuffToSend()
{
    return ((!failed) && (!ackWaitQueue.empty()));
}

bool Link::
hasFailed()
{
    failed |= p->linkFailed();
    return failed;
}

/*
 * Local variables:
 * c-basic-offset: 4
 * End:
 */