aboutsummaryrefslogtreecommitdiffstats
path: root/ncpd
diff options
context:
space:
mode:
authorFritz Elfert <felfert@to.com>2002-02-16 17:35:42 +0000
committerFritz Elfert <felfert@to.com>2002-02-16 17:35:42 +0000
commit12241e0ff5ea242891fc3a8c177e8f55e897f2a3 (patch)
treedd2ad24b8d0a0faf4e8e6548ff3f7297d5d3cf73 /ncpd
parentb32cfed12f9c9aff06ef3f7c6fb046c38ada987a (diff)
downloadplptools-12241e0ff5ea242891fc3a8c177e8f55e897f2a3.tar.gz
plptools-12241e0ff5ea242891fc3a8c177e8f55e897f2a3.tar.bz2
plptools-12241e0ff5ea242891fc3a8c177e8f55e897f2a3.zip
- Applied patches from Luke Diamand, Marc Spoorendonk and Chris Halls
Diffstat (limited to 'ncpd')
-rw-r--r--ncpd/link.cc6
-rw-r--r--ncpd/link.h2
-rw-r--r--ncpd/main.cc369
-rw-r--r--ncpd/ncp.cc10
-rw-r--r--ncpd/ncp.h2
-rw-r--r--ncpd/packet.cc19
-rw-r--r--ncpd/packet.h5
-rw-r--r--ncpd/socketchan.cc1
8 files changed, 223 insertions, 191 deletions
diff --git a/ncpd/link.cc b/ncpd/link.cc
index 6e3be36..b7d5fb7 100644
--- a/ncpd/link.cc
+++ b/ncpd/link.cc
@@ -34,7 +34,7 @@
#include "bufferstore.h"
#include "bufferarray.h"
-link::link(const char *fname, int baud, IOWatch & iow, unsigned short _verbose)
+link::link(const char *fname, int baud, IOWatch *iow, unsigned short _verbose)
{
p = new packet(fname, baud, iow);
verbose = _verbose;
@@ -44,7 +44,7 @@ link::link(const char *fname, int baud, IOWatch & iow, unsigned short _verbose)
somethingToSend = false;
timesSent = 0;
failed = false;
- for (int i; i < 256; i++)
+ for (int i = 0; i < 256; i++)
xoff[i] = false;
}
@@ -62,7 +62,7 @@ reset() {
somethingToSend = false;
timesSent = 0;
failed = false;
- for (int i; i < 256; i++)
+ for (int i = 0; i < 256; i++)
xoff[i] = false;
}
diff --git a/ncpd/link.h b/ncpd/link.h
index b6700e9..4fbd391 100644
--- a/ncpd/link.h
+++ b/ncpd/link.h
@@ -38,7 +38,7 @@ class IOWatch;
class link {
public:
- link(const char *fname, int baud, IOWatch &iow, unsigned short _verbose = 0);
+ link(const char *fname, int baud, IOWatch *iow, unsigned short _verbose = 0);
~link();
void send(const bufferStore &buff);
bufferArray poll();
diff --git a/ncpd/main.cc b/ncpd/main.cc
index 465c003..b2f550f 100644
--- a/ncpd/main.cc
+++ b/ncpd/main.cc
@@ -51,218 +51,239 @@ static bool active = true;
static RETSIGTYPE
term_handler(int)
{
- cout << "Got SIGTERM" << endl;
- signal(SIGTERM, term_handler);
- active = false;
+ cout << "Got SIGTERM" << endl;
+ signal(SIGTERM, term_handler);
+ active = false;
};
static RETSIGTYPE
int_handler(int)
{
- cout << "Got SIGINT" << endl;
- signal(SIGINT, int_handler);
- active = false;
+ cout << "Got SIGINT" << endl;
+ signal(SIGINT, int_handler);
+ active = false;
};
void
-checkForNewSocketConnection(ppsocket & skt, int &numScp, socketChan ** scp, ncp * a, IOWatch &iow)
+checkForNewSocketConnection(ppsocket & skt, int &numScp, socketChan ** scp, ncp * a)
{
- string peer;
- ppsocket *next = skt.accept(&peer);
- if (next != NULL) {
- // New connect
- if (verbose)
- cout << "New socket connection from " << peer << endl;
- if ((numScp >= a->maxLinks()) || (!a->gotLinkChannel())) {
- bufferStore a;
+ string peer;
+ ppsocket *next = skt.accept(&peer);
+ if (next != NULL) {
+ // New connect
+ if (verbose)
+ cout << "New socket connection from " << peer << endl;
+ if ((numScp >= a->maxLinks()) || (!a->gotLinkChannel())) {
+ bufferStore a;
- // Give the client time to send it's version request.
- next->dataToGet(1, 0);
- next->getBufferStore(a, false);
+ // Give the client time to send it's version request.
+ next->dataToGet(1, 0);
+ next->getBufferStore(a, false);
- a.init();
- a.addStringT("No Psion Connected\n");
- next->sendBufferStore(a);
- next->closeSocket();
- if (verbose)
- cout << "rejected" << endl;
- } else
- scp[numScp++] = new socketChan(next, a);
- }
+ a.init();
+ a.addStringT("No Psion Connected\n");
+ next->sendBufferStore(a);
+ next->closeSocket();
+ if (verbose)
+ cout << "rejected" << endl;
+ } else
+ scp[numScp++] = new socketChan(next, a);
+ }
}
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--;
- }
+ 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);
+ 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;
+ ppsocket skt;
+ IOWatch iow;
+ int pid;
+ bool dofork = true;
+ bool autoexit = false;
- int sockNum = DPORT;
- int baudRate = DSPEED;
- const char *serialDevice = NULL;
- short int nverbose = 0;
- short int pverbose = 0;
- short int lverbose = 0;
+ int sockNum = DPORT;
+ int baudRate = DSPEED;
+ const char *host = "127.0.0.1";
+ const char *serialDevice = NULL;
+ short int nverbose = 0;
+ short int pverbose = 0;
+ short int lverbose = 0;
- struct servent *se = getservbyname("psion", "tcp");
- endservent();
- if (se != 0L)
- sockNum = ntohs(se->s_port);
+ struct servent *se = getservbyname("psion", "tcp");
+ endservent();
+ if (se != 0L)
+ sockNum = ntohs(se->s_port);
- // Command line parameter processing
- 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 |
- PKT_DEBUG_HANDSHAKE;
- 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);
+ // Command line parameter processing
+ for (int i = 1; i < argc; i++) {
+ if (!strcmp(argv[i], "-p") && i + 1 < argc) {
+ // parse port argument
+ i++;
+ char *pp = strchr(argv[i], ':');
+ if (pp != NULL) {
+ // host.domain:400
+ // 10.0.0.1:400
+ *pp ++= '\0';
+ host = argv[i];
+ } else {
+ // 400
+ // host.domain
+ // host
+ // 10.0.0.1
+ if (strchr(argv[i], '.') || !isdigit(argv[i][0])) {
+ host = argv[i];
+ pp = NULL;
} else
- usage();
- }
-
- if (serialDevice == NULL) {
- // If started with -e, assume being started from mgetty and
- // use the tty opened by mgetty instead of the builtin default.
- if (autoexit)
- serialDevice = ttyname(0);
- else
- serialDevice = DDEV;
- }
+ pp = argv[i];
+ }
+ if (pp != NULL)
+ sockNum = atoi(pp);
+ } 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 |
+ PKT_DEBUG_HANDSHAKE;
+ 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();
+ if (serialDevice == NULL) {
+ // If started with -e, assume being started from mgetty and
+ // use the tty opened by mgetty instead of the builtin default.
+ if (autoexit)
+ serialDevice = ttyname(0);
else
- pid = 0;
- switch (pid) {
- case 0:
- signal(SIGTERM, term_handler);
- signal(SIGINT, int_handler);
- skt.setWatch(&iow);
- if (!skt.listen("127.0.0.1", sockNum))
- cerr << "listen on port " << sockNum << ": " << strerror(errno) << endl;
- else {
- if (dofork || autoexit) {
- 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);
- syslog(LOG_INFO,
- "daemon started. Listening at 127.0.0.1:%d, using device %s\n",
- sockNum, serialDevice);
- setsid();
- chdir("/");
- int devnull =
- open("/dev/null", O_RDWR, 0);
- if (devnull != -1) {
- dup2(devnull, STDIN_FILENO);
- dup2(devnull, STDOUT_FILENO);
- dup2(devnull, STDERR_FILENO);
- if (devnull > 2)
- close(devnull);
- }
- }
- ncp *a = new ncp(serialDevice, baudRate, iow);
- int numScp = 0;
- socketChan *scp[257]; // MAX_CHANNELS_PSION + 1
+ serialDevice = DDEV;
+ }
- a->setVerbose(nverbose);
- a->setLinkVerbose(lverbose);
- a->setPktVerbose(pverbose);
- while (active) {
- // sockets
- pollSocketConnections(numScp, scp);
- checkForNewSocketConnection(skt, numScp, scp, a, iow);
+ if (dofork)
+ pid = fork();
+ else
+ pid = 0;
+ switch (pid) {
+ case 0:
+ signal(SIGTERM, term_handler);
+ signal(SIGINT, int_handler);
+ skt.setWatch(&iow);
+ if (!skt.listen(host, sockNum))
+ cerr << "listen on " << host << ":" << sockNum << ": " << strerror(errno) << endl;
+ else {
+ if (dofork || autoexit) {
+ 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);
+ syslog(LOG_INFO,
+ "daemon started. Listening at %s:%d, using device %s\n",
+ host, sockNum, serialDevice);
+ setsid();
+ chdir("/");
+ int devnull =
+ open("/dev/null", O_RDWR, 0);
+ if (devnull != -1) {
+ dup2(devnull, STDIN_FILENO);
+ dup2(devnull, STDOUT_FILENO);
+ dup2(devnull, STDERR_FILENO);
+ if (devnull > 2)
+ close(devnull);
+ }
+ }
+ ncp *a = new ncp(serialDevice, baudRate, &iow);
+ int numScp = 0;
+ socketChan *scp[257]; // MAX_CHANNELS_PSION + 1
- // psion
- a->poll();
+ a->setVerbose(nverbose);
+ a->setLinkVerbose(lverbose);
+ a->setPktVerbose(pverbose);
+ while (active) {
+ // sockets
+ pollSocketConnections(numScp, scp);
+ checkForNewSocketConnection(skt, numScp, scp, a);
- if (a->stuffToSend())
- iow.watch(0, 100000);
- else
- iow.watch(1, 0);
+ // psion
+ a->poll();
- if (a->hasFailed()) {
- if (autoexit)
- break;
+ if (a->stuffToSend())
+ iow.watch(0, 100000);
+ else
+ iow.watch(1, 0);
- 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);
- }
+ 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);
+ }
}
/*
diff --git a/ncpd/ncp.cc b/ncpd/ncp.cc
index cead4a7..031487f 100644
--- a/ncpd/ncp.cc
+++ b/ncpd/ncp.cc
@@ -39,7 +39,7 @@
#define MAX_CHANNELS_SIBO 8
#define NCP_SENDLEN 250
-ncp::ncp(const char *fname, int baud, IOWatch & iow)
+ncp::ncp(const char *fname, int baud, IOWatch *iow)
{
channelPtr = new channel*[MAX_CHANNELS_PSION + 1];
messageList = new bufferStore[MAX_CHANNELS_PSION + 1];
@@ -71,8 +71,9 @@ ncp::~ncp()
}
controlChannel(0, NCON_MSG_NCP_END, b);
delete l;
- delete channelPtr;
- delete remoteChanList;
+ delete [] channelPtr;
+ delete [] remoteChanList;
+ delete [] messageList;
}
int ncp::
@@ -251,7 +252,8 @@ decodeControlMessage(bufferStore & buff)
} else {
if (verbose & NCP_DEBUG_LOG)
cout << "Unknown " << (int) buff.getByte(1) << endl;
- channelPtr[forChan]->ncpConnectNak();
+ if (channelPtr[forChan])
+ channelPtr[forChan]->ncpConnectNak();
}
break;
diff --git a/ncpd/ncp.h b/ncpd/ncp.h
index abbf08e..c0c5abe 100644
--- a/ncpd/ncp.h
+++ b/ncpd/ncp.h
@@ -38,7 +38,7 @@ class IOWatch;
class ncp {
public:
- ncp(const char *fname, int baud, IOWatch &iow);
+ ncp(const char *fname, int baud, IOWatch *iow);
~ncp();
int connect(channel *c); // returns channel, or -1 if failure
diff --git a/ncpd/packet.cc b/ncpd/packet.cc
index 81b8894..fc0ce11 100644
--- a/ncpd/packet.cc
+++ b/ncpd/packet.cc
@@ -46,7 +46,7 @@ extern "C" {
#define BUFFERLEN 2000
-packet::packet(const char *fname, int _baud, IOWatch & _iow, short int _verbose = 0):
+packet::packet(const char *fname, int _baud, IOWatch *_iow, short int _verbose = 0):
iow(_iow)
{
verbose = _verbose;
@@ -58,14 +58,19 @@ iow(_iow)
foundSync = 0;
esc = false;
lastFatal = false;
+ iowLocal = false;
serialStatus = -1;
crcIn = crcOut = 0;
fd = init_serial(devname, baud, 0);
+ if (!_iow) {
+ iow = new IOWatch();
+ iowLocal = true;
+ }
if (fd == -1)
lastFatal = true;
- else
- iow.addIO(fd);
+ else
+ iow->addIO(fd);
}
void packet::reset()
@@ -73,7 +78,7 @@ void packet::reset()
if (verbose & PKT_DEBUG_LOG)
cout << "resetting serial connection" << endl;
if (fd != -1) {
- iow.remIO(fd);
+ iow->remIO(fd);
ser_exit(fd);
}
usleep(100000);
@@ -87,7 +92,7 @@ void packet::reset()
crcIn = crcOut = 0;
fd = init_serial(devname, baud, 0);
if (fd != -1) {
- iow.addIO(fd);
+ iow->addIO(fd);
lastFatal = false;
}
if (verbose & PKT_DEBUG_LOG)
@@ -110,13 +115,15 @@ setVerbose(short int _verbose)
packet::~packet()
{
if (fd != -1) {
- iow.remIO(fd);
+ iow->remIO(fd);
ser_exit(fd);
}
usleep(100000);
delete[]inBuffer;
delete[]outBuffer;
free(devname);
+ if (iowLocal)
+ delete iow;
}
void packet::
diff --git a/ncpd/packet.h b/ncpd/packet.h
index a21412e..f80e4c9 100644
--- a/ncpd/packet.h
+++ b/ncpd/packet.h
@@ -38,7 +38,7 @@ class IOWatch;
class packet {
public:
- packet(const char *fname, int baud, IOWatch &iow, short int verbose = 0);
+ packet(const char *fname, int baud, IOWatch *iow, short int verbose = 0);
~packet();
void send(unsigned char type, const bufferStore &b);
bool get(unsigned char &type, bufferStore &b);
@@ -70,9 +70,10 @@ class packet {
short int verbose;
bool esc;
bool lastFatal;
+ bool iowLocal;
char *devname;
int baud;
- IOWatch &iow;
+ IOWatch *iow;
};
#endif
diff --git a/ncpd/socketchan.cc b/ncpd/socketchan.cc
index 32830af..83d0695 100644
--- a/ncpd/socketchan.cc
+++ b/ncpd/socketchan.cc
@@ -46,6 +46,7 @@ socketChan::~socketChan()
{
skt->closeSocket();
delete skt;
+ skt = 0;
if (registerName)
free(registerName);
}