aboutsummaryrefslogtreecommitdiffstats
path: root/ncpd/main.cc
blob: 2c0fd3d8bbb58c627f60b9b7ca402703134f2ee6 (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
// $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

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

#include "defs.h"
#include "ncp.h"
#include "bufferstore.h"
#include "ppsocket.h"
#include "socketchan.h"
#include "iowatch.h"
#include "linkchan.h"
#include "link.h"
#include "packet.h"
#include "log.h"

static bool verbose = false;

void
checkForNewSocketConnection(ppsocket & skt, int &numScp, socketChan ** scp, ncp * a, IOWatch & iow)
{
	char peer[201];
	ppsocket *next = skt.accept(peer, 200);
	if (next != NULL) {
		// New connect
		if (verbose)
			cout << "New socket connection from " << peer << endl;
		if ((numScp == 7) || (!a->gotLinkChannel())) {
			bufferStore a;
			a.addStringT("No Psion Connected\n");
			next->sendBufferStore(a);
			next->closeSocket();
		} else
			scp[numScp++] = new socketChan(next, a, iow);
	}
}

void
pollSocketConnections(int &numScp, socketChan ** scp)
{
	for (int i = 0; i < numScp; i++) {
		scp[i]->socketPoll();
		if (scp[i]->terminate()) {
			// Requested channel termination
			delete scp[i];
			numScp--;
			for (int j = i; j < numScp; j++)
				scp[j] = scp[j + 1];
			i--;
		}
	}
}

void
usage()
{
	cerr << "Usage : ncpd [-V] [-v logclass] [-d] [-e] [-p <port>] [-s <device>] [-b <baudrate>]\n";
	exit(1);
}

int
main(int argc, char **argv)
{
	ppsocket skt;
	IOWatch iow;
	int pid;
	bool dofork = true;
	bool autoexit = false;

	// Command line parameter processing
	int sockNum = DPORT;
	int baudRate = DSPEED;
	const char *serialDevice = DDEV;
	short int nverbose = 0;
	short int pverbose = 0;
	short int lverbose = 0;

	signal(SIGPIPE, SIG_IGN);
	for (int i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-p") && i + 1 < argc) {
			sockNum = atoi(argv[++i]);
		} else if (!strcmp(argv[i], "-s") && i + 1 < argc) {
			serialDevice = argv[++i];
		} else if (!strcmp(argv[i], "-v") && i + 1 < argc) {
			i++;
			if (!strcmp(argv[i], "nl"))
				nverbose |= NCP_DEBUG_LOG;
			if (!strcmp(argv[i], "nd"))
				nverbose |= NCP_DEBUG_DUMP;
			if (!strcmp(argv[i], "ll"))
				lverbose |= LNK_DEBUG_LOG;
			if (!strcmp(argv[i], "ld"))
				lverbose |= LNK_DEBUG_DUMP;
			if (!strcmp(argv[i], "pl"))
				pverbose |= PKT_DEBUG_LOG;
			if (!strcmp(argv[i], "pd"))
				pverbose |= PKT_DEBUG_DUMP;
			if (!strcmp(argv[i], "ph"))
				pverbose |= PKT_DEBUG_HANDSHAKE;
			if (!strcmp(argv[i], "m"))
				verbose = true;
			if (!strcmp(argv[i], "all")) {
				nverbose = NCP_DEBUG_LOG | NCP_DEBUG_DUMP;
				lverbose = LNK_DEBUG_LOG | LNK_DEBUG_DUMP;
				pverbose = PKT_DEBUG_LOG | PKT_DEBUG_DUMP;
				verbose = true;
			}
		} else if (!strcmp(argv[i], "-b") && i + 1 < argc) {
			baudRate = atoi(argv[++i]);
		} else if (!strcmp(argv[i], "-d")) {
			dofork = 0;
		} else if (!strcmp(argv[i], "-e")) {
			autoexit = true;
		} else if (!strcmp(argv[i], "-V")) {
			cout << "ncpd version " << VERSION << endl;
			exit(0);
		} else
			usage();
	}

	if (dofork)
		pid = fork();
	else
		pid = 0;
	switch (pid) {
		case 0:
			if (!skt.listen("127.0.0.1", sockNum))
				cerr << "listen on port " << sockNum << ": " << strerror(errno) << endl;
			else {
				if (dofork) {
					logbuf dlog(LOG_DEBUG);
					logbuf elog(LOG_ERR);
					ostream lout(&dlog);
					ostream lerr(&elog);
					cout = lout;
					cerr = lerr;
					openlog("ncpd", LOG_CONS|LOG_PID, LOG_DAEMON);
				}
				ncp *a = new ncp(serialDevice, baudRate, iow);
				int numScp = 0;
				socketChan *scp[MAX_CHANNEL+1];

				a->setVerbose(nverbose);
				a->setLinkVerbose(lverbose);
				a->setPktVerbose(pverbose);
				iow.addIO(skt.socket());
				while (true) {
					// sockets
					pollSocketConnections(numScp, scp);
					checkForNewSocketConnection(skt, numScp, scp, a, iow);

					// psion
					a->poll();

					if (a->stuffToSend())
						iow.watch(0, 100000);
					else
						iow.watch(1, 0);

					if (a->hasFailed()) {
						if (autoexit)
							break;

						iow.watch(5, 0);
						if (verbose)
							cout << "ncp: restarting\n";
						a->reset();
					}
				}
				delete a;
			}
			skt.closeSocket();
			break;
		case -1:
			cerr << "fork: " << strerror(errno) << endl;
			break;
		default:
			exit(0);
	}
}