aboutsummaryrefslogtreecommitdiffstats
path: root/ncpd/ncp.cc
blob: 76f4d635ee2cdd7ca2984f9fd6475e86ede4806d (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
/*-*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 1999  Philip Proudman <philip.proudman@btinternet.com>
 *  Copyright (C) 1999-2001 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 <string>
#include <time.h>

#include "ncp.h"
#include "linkchan.h"
#include <bufferstore.h>
#include "link.h"
#include <bufferarray.h>

#define MAX_CHANNELS_PSION 256
#define MAX_CHANNELS_SIBO  8
#define NCP_SENDLEN 250

ncp::ncp(const char *fname, int baud, unsigned short _verbose)
{
    channelPtr = new channel*[MAX_CHANNELS_PSION + 1];
    assert(channelPtr);
    messageList = new bufferStore[MAX_CHANNELS_PSION + 1];
    assert(messageList);
    remoteChanList = new int[MAX_CHANNELS_PSION + 1];
    assert(remoteChanList);

    failed = false;
    verbose = _verbose;

    // until detected on receipt of INFO we use these.
    maxChannels = MAX_CHANNELS_SIBO;
    protocolVersion = PV_SERIES_5;

    // init channels
    for (int i = 0; i < MAX_CHANNELS_PSION; i++)
	channelPtr[i] = NULL;

    l = new Link(fname, baud, this, verbose);
    assert(l);
}

ncp::~ncp()
{
    bufferStore b;
    for (int i = 0; i < maxLinks(); i++) {
	if (isValidChannel(i)) {
	    bufferStore b2;
	    b2.addByte(remoteChanList[i]);
	    controlChannel(i, NCON_MSG_CHANNEL_DISCONNECT, b2);
	}
	channelPtr[i] = NULL;
    }
    controlChannel(0, NCON_MSG_NCP_END, b);
    delete l;
    delete [] channelPtr;
    delete [] remoteChanList;
    delete [] messageList;
}

int ncp::
maxLinks() {
    return maxChannels;
}

void ncp::
reset() {
    for (int i = 0; i < maxLinks(); i++) {
	if (isValidChannel(i))
	    channelPtr[i]->terminateWhenAsked();
	channelPtr[i] = NULL;
    }
    failed = false;
    lChan = NULL;
    protocolVersion = PV_SERIES_5; // until detected on receipt of INFO
    l->reset();
}

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

void ncp::
setVerbose(unsigned short _verbose)
{
    verbose = _verbose;
    l->setVerbose(verbose);
}

short int ncp::
getProtocolVersion()
{
    return protocolVersion;
}

void ncp::
receive(bufferStore s) {
    if (s.getLen() > 1) {
	int channel = s.getByte(0);
	s.discardFirstBytes(1);
	if (channel == 0) {
	    decodeControlMessage(s);
	} else {
	    int allData = s.getByte(1);
	    s.discardFirstBytes(2);
	    if (!isValidChannel(channel)) {
		cerr << "ncp: Got message for unknown channel\n";
	    } else {
		messageList[channel].addBuff(s);
		if (allData == LAST_MESS) {
		    channelPtr[channel]->ncpDataCallback(messageList[channel]);
		    messageList[channel].init();
		} else if (allData != NOT_LAST_MESS) {
		    cerr << "ncp: bizarre third byte!\n";
		}
	    }
	}
    } else
	cerr << "Got null message\n";
}

void ncp::
controlChannel(int chan, enum interControllerMessageType t, bufferStore & command)
{
    bufferStore open;
    open.addByte(0);	// control

    open.addByte(chan);
    open.addByte(t);
    open.addBuff(command);
    if (verbose & NCP_DEBUG_LOG)
	cout << "ncp: >> " << ctrlMsgName(t) << " " << chan << endl;
    l->send(open);
}

void ncp::
decodeControlMessage(bufferStore & buff)
{
    int remoteChan = buff.getByte(0);

    interControllerMessageType imt = (interControllerMessageType)buff.getByte(1);
    buff.discardFirstBytes(2);
    if (verbose & NCP_DEBUG_LOG)
	cout << "ncp: << " << ctrlMsgName(imt) << " " << remoteChan;

    bufferStore b;

    switch (imt) {
	case NCON_MSG_CONNECT_TO_SERVER:
	    if (verbose & NCP_DEBUG_LOG) {
		if (verbose & NCP_DEBUG_DUMP)
		    cout << " [" << buff << "]";
		cout << endl;
	    }

	    int localChan;

	    // Ack with connect response
	    localChan = getFirstUnusedChan();
	    b.addByte(remoteChan);
	    b.addByte(0);
	    controlChannel(localChan, NCON_MSG_CONNECT_RESPONSE, b);

	    // NOTE: we don't allow connections from the
	    // Psion to any local "processes" other than
	    // LINK.* - Matt might need to change this for
	    // his NCP<->TCP/IP bridge code...

	    if (!strcmp(buff.getString(0), "LINK.*")) {
		if (lChan)
		    failed = true;
		if (verbose & NCP_DEBUG_LOG)
		    cout << "ncp: Link UP" << endl;
		channelPtr[localChan] = lChan = new linkChan(this, localChan);
		lChan->setVerbose(verbose);
		lChan->ncpConnectAck();
	    } else {
		if (verbose & NCP_DEBUG_LOG)
		    cout << "ncp: REJECT connect" << endl;
		bufferStore b;
		b.addByte(remoteChan);
		controlChannel(0, NCON_MSG_CHANNEL_DISCONNECT, b);
	    }
	    break;

	case NCON_MSG_CONNECT_RESPONSE:

	    int forChan;

	    forChan = buff.getByte(0);
	    if (verbose & NCP_DEBUG_LOG)
		cout << " ch=" << forChan << " stat=";
	    if (buff.getByte(1) == 0) {
		if (verbose & NCP_DEBUG_LOG)
		    cout << "OK" << endl;
		if (isValidChannel(forChan)) {
		    remoteChanList[forChan] = remoteChan;
		    channelPtr[forChan]->ncpConnectAck();
		} else {
		    if (verbose & NCP_DEBUG_LOG)
			cout << "ncp: message for unknown channel" << endl;
		}
	    } else {
		if (verbose & NCP_DEBUG_LOG)
		    cout << "Unknown " << (int) buff.getByte(1) << endl;
		if (isValidChannel(forChan))
		    channelPtr[forChan]->ncpConnectNak();
	    }
	    break;

	case NCON_MSG_NCP_INFO:

	    int ver;

	    ver = buff.getByte(0);
	    // Series 3c returns '3', as does mclink. PsiWin 1.1
	    // returns version 2. We return whatever version we're
	    // sent, which is rather crude, but works for Series 3
	    // and probably 5. If Symbian have changed EPOC Connect
	    // for the Series 5mx/7, this may need to change.
	    //
	    if (ver == PV_SERIES_5 || ver == PV_SERIES_3) {
		bufferStore b;
		protocolVersion = ver;
		if (verbose & NCP_DEBUG_LOG) {
		    if (verbose & NCP_DEBUG_DUMP)
			cout << " [" << buff << "]";
		    cout << endl;
		}
		// Fake NCP version 2 for a Series 3 (behave like PsiWin 1.1)
		if (ver == PV_SERIES_3) {
		    ver = 2;
		} else {
		    // Series 5 supports more channels
		    maxChannels = MAX_CHANNELS_PSION;
		}
		b.addByte(ver);
		// Do we send a time of 0 or a real time?
		// The Psion uses this to determine whether to
		// restart. (See protocol docs for details)
		// b.addDWord(0);
		b.addDWord(time(NULL));
		controlChannel(0, NCON_MSG_NCP_INFO, b);
	    } else
		cout << "ALERT!!!! Unexpected Protocol Version!! (No Series 5/3?)!" << endl;
	    break;

	case NCON_MSG_CHANNEL_DISCONNECT:
	    if (verbose & NCP_DEBUG_LOG)
		cout << " ch=" << (int) buff.getByte(0) << endl;
	    disconnect(buff.getByte(0));
	    l->purgeQueue(remoteChan);
	    break;

	case NCON_MSG_DATA_XOFF:
	case NCON_MSG_DATA_XON:
	case NCON_MSG_CHANNEL_CLOSED:
	case NCON_MSG_NCP_END:
	default:
	    if (verbose & NCP_DEBUG_LOG) {
		if (verbose & NCP_DEBUG_DUMP)
		    cout << " [" << buff << "]";
		cout << endl;
	    }

    }
}

int ncp::
getFirstUnusedChan()
{
    for (int cNum = 1; cNum < maxLinks(); cNum++) {
	if (channelPtr[cNum] == NULL) {
	    if (verbose & NCP_DEBUG_LOG)
		cout << "ncp: getFirstUnusedChan=" << cNum << endl;
	    channelPtr[cNum] = (channel *)0xdeadbeef;
	    return cNum;
	}
    }
    return 0;
}

bool ncp::
isValidChannel(int channel)
{
    return (channelPtr[channel] && ((int)channelPtr[channel] != 0xdeadbeef));
}

void ncp::
RegisterAck(int chan, const char *name)
{
    if (verbose & NCP_DEBUG_LOG)
	cout << "ncp: RegisterAck: chan=" << chan << endl;
    for (int cNum = 1; cNum < maxLinks(); cNum++) {
	channel *ch = channelPtr[cNum];
	if (ch && ch->getNcpChannel() == chan) {
	    ch->setNcpConnectName(name);
	    ch->ncpRegisterAck();
	    return;
	}
    }
    cerr << "ncp: RegisterAck: no channel to deliver" << endl;
}

void ncp::
Register(channel * ch)
{
    if (lChan) {
	int cNum = ch->getNcpChannel();
	if (cNum == 0)
	    cNum = getFirstUnusedChan();
	if (cNum > 0) {
	    channelPtr[cNum] = ch;
	    ch->setNcpChannel(cNum);
	    lChan->Register(ch);
	} else
	    cerr << "ncp: Out of channels in register" << endl;
    } else
	cerr << "ncp: Register without established lChan" << endl;
}

int ncp::
connect(channel * ch)
{
    // look for first unused chan

    int cNum = ch->getNcpChannel();
    if (cNum == 0)
	cNum = getFirstUnusedChan();
    if (cNum > 0) {
	channelPtr[cNum] = ch;
	ch->setNcpChannel(cNum);
	bufferStore b;
	if (ch->getNcpConnectName())
	    b.addString(ch->getNcpConnectName());
	else {
	    b.addString(ch->getNcpRegisterName());
	    b.addString(".*");
	}
	b.addByte(0);
	controlChannel(cNum, NCON_MSG_CONNECT_TO_SERVER, b);
	return cNum;
    }
    return -1;
}

void ncp::
send(int channel, bufferStore & a)
{
    bool last;
    do {
	last = true;

	if (a.getLen() > NCP_SENDLEN)
	    last = false;

	bufferStore out;
	out.addByte(remoteChanList[channel]);
	out.addByte(channel);

	if (last) {
	    out.addByte(LAST_MESS);
	} else {
	    out.addByte(NOT_LAST_MESS);
	}

	out.addBuff(a, NCP_SENDLEN);
	a.discardFirstBytes(NCP_SENDLEN);
	l->send(out);
    } while (!last);
}

void ncp::
disconnect(int channel)
{
    if (!isValidChannel(channel)) {
	cerr << "ncp: Ignored disconnect for unknown channel #" << channel << endl;
	return;
    }
    channelPtr[channel]->terminateWhenAsked();
    if (verbose & NCP_DEBUG_LOG)
	cout << "ncp: disconnect: channel=" << channel << endl;
    channelPtr[channel] = NULL;
    bufferStore b;
    b.addByte(remoteChanList[channel]);
    controlChannel(channel, NCON_MSG_CHANNEL_DISCONNECT, b);
}

bool ncp::
stuffToSend()
{
    return l->stuffToSend();
}

bool ncp::
hasFailed()
{
    if (failed)
	return true;
    return l->hasFailed();
}

bool ncp::
gotLinkChannel()
{
    return (lChan != NULL);
}

char *ncp::
ctrlMsgName(unsigned char msgType)
{
    switch (msgType) {
	case NCON_MSG_DATA_XOFF:
	    return "NCON_MSG_DATA_XOFF";
	case NCON_MSG_DATA_XON:
	    return "NCON_MSG_DATA_XON";
	case NCON_MSG_CONNECT_TO_SERVER:
	    return "NCON_MSG_CONNECT_TO_SERVER";
	case NCON_MSG_CONNECT_RESPONSE:
	    return "NCON_MSG_CONNECT_RESPONSE";
	case NCON_MSG_CHANNEL_CLOSED:
	    return "NCON_MSG_CHANNEL_CLOSED";
	case NCON_MSG_NCP_INFO:
	    return "NCON_MSG_NCP_INFO";
	case NCON_MSG_CHANNEL_DISCONNECT:
	    return "NCON_MSG_CHANNEL_DISCONNECT";
	case NCON_MSG_NCP_END:
	    return "NCON_MSG_NCP_END";
    }
    return "NCON_MSG_UNKNOWN";
}

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