aboutsummaryrefslogtreecommitdiffstats
path: root/ncpd/link.cc
blob: 90a745117dca5373f65c469403efe413c456ce07 (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
// $Id$
//
//  PLP - An implementation of the PSION link protocol
//
//  Copyright (C) 1999  Philip Proudman
//  Modifications for plptools:
//    Copyright (C) 1999 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
//
//  e-mail philip.proudman@btinternet.com

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stream.h>
#include <stdlib.h>
#include <stdio.h>

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

link::link(const char *fname, int baud, IOWatch & iow, unsigned short _verbose)
{
	p = new packet(fname, baud, iow);
	verbose = _verbose;
	idSent = 0;
	idLastGot = -1;
	newLink = true;
	somethingToSend = false;
	timesSent = 0;
	failed = false;
}

link::~link()
{
	flush();
	delete p;
}

void link::
reset() {
	idSent = 0;
	idLastGot = -1;
	newLink = true;
	somethingToSend = false;
	timesSent = 0;
	failed = false;
	// p->reset();
}

short int link::
getVerbose()
{
	return verbose;
}

void link::
setVerbose(short int _verbose)
{
	verbose = _verbose;
}

short int link::
getPktVerbose()
{
	return p->getVerbose();
}

void link::
setPktVerbose(short int _verbose)
{
	p->setVerbose(_verbose);
}

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

bufferArray link::
poll()
{
	bufferArray ret;
	bufferStore buff;
	unsigned char type;

	while (p->get(type, buff)) {
		int seq = type & 0x0f;
		bufferStore blank;
		type &= 0xf0;
		switch (type) {
			case 0x30:
				if (verbose & LNK_DEBUG_LOG) {
					cout << "link: << dat seq=" << seq ;
					if (verbose & LNK_DEBUG_DUMP)
						cout << " " << buff << endl;
					else
						cout << " len=" << buff.getLen() << endl;
				}
				// Send ack
				if (idLastGot != seq) {
					idLastGot = seq;
					ret += buff;
				} else {
					if (verbose & LNK_DEBUG_LOG)
						cout << "link: DUP\n";
				}
				if (verbose & LNK_DEBUG_LOG)
					cout << "link: >> ack seq=" << seq << endl;
				blank.init();
				p->send(seq, blank);
				break;
			case 0x00:
				// Ack
				if (seq == idSent) {
					if (verbose & LNK_DEBUG_LOG) {
						cout << "link: << ack seq=" << seq ;
						if (verbose & LNK_DEBUG_DUMP)
							cout << " " << buff;
						cout << endl;
					}
					somethingToSend = false;
					timesSent = 0;
				}
				break;
			case 0x20:
				// New link
				if (verbose & LNK_DEBUG_LOG) {
					cout << "link: << lrq seq=" << seq;
					if (verbose & LNK_DEBUG_DUMP)
						cout << " " << buff;
					cout << endl;
				}
				idLastGot = 0;
				if (verbose & LNK_DEBUG_LOG)
					cout << "link: >> lack seq=" << seq << endl;
				somethingToSend = false;
				blank.init();
				p->send(idLastGot, blank);
				break;
			case 0x10:
				// Disconnect
				if (verbose & LNK_DEBUG_LOG)
					cout << "link: << DISC" << endl;
				failed = true;
				return ret;
		}
	}
	if (p->linkFailed()) {
		failed = true;
		return ret;
	}

	if (!somethingToSend) {
		countToResend = 0;
		if (newLink) {
			somethingToSend = true;
			toSend.init();
			newLink = false;
			idSent = 0;
		} else {
			if (!sendQueue.empty()) {
				somethingToSend = true;
				toSend = sendQueue.pop();
				idSent++;
				if (idSent > 7)
					idSent = 0;
			}
		}
	}
	if (somethingToSend) {
		if (countToResend == 0) {
			timesSent++;
			if (timesSent == 5) {
				failed = true;
			} else {
				if (toSend.empty()) {
					// Request for new link
					if (verbose & LNK_DEBUG_LOG)
						cout << "link: >> lrq seq=" << idSent << endl;
					p->send(0x20 + idSent, toSend);
				} else {
					if (verbose & LNK_DEBUG_LOG) {
						cout << "link: >> data seq=" << idSent;
						if (verbose & LNK_DEBUG_DUMP)
							cout << " " << toSend;
						cout << endl;
					}
					p->send(0x30 + idSent, toSend);
				}
				countToResend = 5;
			}
		} else
			countToResend--;
	}
	return ret;
}

void link::
flush() {
	while ((!failed) && stuffToSend())
		poll();
}

bool link::
stuffToSend()
{
	return (somethingToSend || !sendQueue.empty());
}

bool link::
hasFailed()
{
	return failed;
}