Project

General

Profile

Download (30.5 KB) Statistics
| Branch: | Revision:
/*
* wconsd.c - serial port server service for Windows NT
*
* Copyright (c) 2003 Benjamin Schweizer <gopher at h07 dot org>
* 1998 Stephen Early <Stephen.Early@cl.cam.ac.uk>
*
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/* Note: winsock2.h MUST be included before windows.h */

#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <winsvc.h>
#include <stdio.h>
#include <stdlib.h>

#define VERSION "0.1.2"

/* Size of buffers for send and receive */
#define BUFSIZE 1024
#define MAXLEN 1024

/* Sockets for listening and communicating */
SOCKET ls=INVALID_SOCKET,cs=INVALID_SOCKET;

/* Event objects */
HANDLE stopEvent, connectionCloseEvent, threadTermEvent;
HANDLE readEvent, writeEvent;
WSAEVENT listenSocketEvent;

/* COM port */
HANDLE hCom;
DCB dcb;
COMMTIMEOUTS timeouts;

/* Port default settings are here */
int com_port=1;
DWORD com_speed=9600;
BYTE com_data=8;
BYTE com_parity=NOPARITY;
BYTE com_stop=ONESTOPBIT;
BOOL com_autoclose=TRUE;
BOOL com_state=FALSE; // FALSE=closed,TRUE=open

int default_tcpport = 23;

/* Service status: our current status, and handle on service manager */
SERVICE_STATUS wconsd_status;
SERVICE_STATUS_HANDLE wconsd_statusHandle;

int debug_mode = 0;

#define MAXCONNECTIONS 8

int next_connection_id = 0; /* lifetime unique connection id */
struct connection {
int active; /* an active entry cannot be reused */
int id; /* connection identifier */
int menuactive; /* dont run the serial pump on a menu */
HANDLE menuThread;
SOCKET net;
HANDLE netThread;
HANDLE serial;
HANDLE serialThread;
int option_echo; /* will we echo chars recieved? */
int net_bytes_rx;
int net_bytes_tx;
};
struct connection connection[MAXCONNECTIONS];

/*
* output from OutputDebugStringA can be seen using sysinternals debugview
* http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
*/

/*
* log a debug message
*/
int dprintf_level = 1;
int dprintf(unsigned char severity, const char *fmt, ...) {
va_list args;
char buf[MAXLEN];
int i;

if (severity > dprintf_level)
return 0;

va_start(args,fmt);
i=vsnprintf(buf,sizeof(buf),fmt,args);
va_end(args);

if (debug_mode) {
printf("%s",buf);
} else {
OutputDebugStringA(buf);
}

return i;
}

/*
* format a string and send it to a net connection
*/
int netprintf(struct connection *conn, const char *fmt, ...) {
va_list args;
char buf[MAXLEN];
int i;
int bytes;

va_start(args,fmt);
i=vsnprintf(buf,sizeof(buf),fmt,args);
va_end(args);

bytes = send(conn->net,buf,(i>MAXLEN)?MAXLEN-1:i,0);

if (bytes==-1) {
dprintf(1,"wconsd[%i]: netprintf: send error %i\n",conn->id,GetLastError());
} else {
conn->net_bytes_tx += bytes;
}

return i;
}

/* open the com port */
DWORD open_com_port(DWORD *specificError) {
/* Open the COM port */
char portstr[12];
sprintf(portstr, "\\\\.\\COM%d", com_port);
hCom = CreateFile(portstr,
GENERIC_READ | GENERIC_WRITE,
0, // Exclusive access
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (hCom == INVALID_HANDLE_VALUE) {
*specificError=GetLastError();
return 13;
}

if (!GetCommState(hCom, &dcb)) {
*specificError=GetLastError();
return 14;
}

// Fill in the device control block
dcb.BaudRate=com_speed;
dcb.ByteSize=com_data;
dcb.Parity=com_parity; // NOPARITY, ODDPARITY, EVENPARITY
dcb.StopBits=com_stop; // ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS
dcb.fBinary=TRUE;
dcb.fOutxCtsFlow=FALSE;
dcb.fOutxDsrFlow=FALSE;
dcb.fDtrControl=DTR_CONTROL_ENABLE; // Always on
dcb.fDsrSensitivity=FALSE;
dcb.fTXContinueOnXoff=FALSE;
dcb.fOutX=FALSE;
dcb.fInX=FALSE;
dcb.fErrorChar=FALSE;
dcb.fNull=FALSE;
dcb.fRtsControl=RTS_CONTROL_ENABLE; // Always on
dcb.fAbortOnError=FALSE;

if (!SetCommState(hCom, &dcb)) {
*specificError=GetLastError();
return 15;
}

timeouts.ReadIntervalTimeout=20;
timeouts.ReadTotalTimeoutMultiplier=0;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=0;
timeouts.WriteTotalTimeoutConstant=0;
if (!SetCommTimeouts(hCom, &timeouts)) {
*specificError=GetLastError();
return 16;
}
com_state=TRUE;
return 0;
}

/* close the com port */
void close_com_port() {
CloseHandle(hCom);
com_state=FALSE;
}


/* Initialise wconsd: open a listening socket and the COM port, and
* create lots of event objects. */
DWORD wconsd_init(DWORD argc, LPSTR *argv, DWORD *specificError)
{
struct sockaddr_in sin;
WORD wVersionRequested;
WSADATA wsaData;
int err;

/* Start up sockets */
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
*specificError=err;
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 1;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return 2;
}

/* The WinSock DLL is acceptable. Proceed. */

// Create the event object used to signal service shutdown
stopEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if (stopEvent==NULL) {
*specificError=GetLastError();
return 3;
}
// Create the event object used to signal connection close
connectionCloseEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if (connectionCloseEvent==NULL) {
*specificError=GetLastError();
return 4;
}
threadTermEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if (threadTermEvent==NULL) {
*specificError=GetLastError();
return 5;
}
// Event objects for overlapped IO
readEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if (readEvent==NULL) {
*specificError=GetLastError();
return 6;
}
writeEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if (writeEvent==NULL) {
*specificError=GetLastError();
return 7;
}
// Create the event object for socket operations
listenSocketEvent = WSACreateEvent();
if (listenSocketEvent==WSA_INVALID_EVENT) {
*specificError=WSAGetLastError();
return 8;
}

/* Create a socket to listen for connections. */
memset(&sin,0,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_port=htons(default_tcpport);
ls=socket(AF_INET,SOCK_STREAM,0);
if (ls==INVALID_SOCKET) {
*specificError=WSAGetLastError();
return 9;
}
#ifndef MS_WINDOWS
{
int one=1;
setsockopt(ls,SOL_SOCKET,SO_REUSEADDR,(void*)&one,sizeof(one));
}
#endif
if (bind(ls,(struct sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR) {
*specificError=WSAGetLastError();
dprintf(1,"wconsd: wconsd_init: failed to bind socket\n");
return 10;
}
if (listen(ls,1)==SOCKET_ERROR) {
*specificError=WSAGetLastError();
return 11;
}

/* Mark the socket as non-blocking */
if (WSAEventSelect(ls,listenSocketEvent,FD_ACCEPT)==SOCKET_ERROR) {
*specificError=WSAGetLastError();
return 12;
}
return 0;
}

DWORD WINAPI wconsd_net_to_com(LPVOID lpParam)
{
unsigned char buf[BUFSIZE];
DWORD size,wsize;
unsigned long zero=0;
fd_set s;
OVERLAPPED o={0};

dprintf(1,"wconsd: start wconsd_net_to_com\n");

o.hEvent = writeEvent;
while (WaitForSingleObject(threadTermEvent,0)!=WAIT_OBJECT_0) {
/* There's a bug in some versions of Windows which leads
* to recv() returning -1 and indicating error WSAEWOULDBLOCK,
* even on a blocking socket. This select() is here to work
* around that bug. */
FD_SET(cs,&s);
select(0,&s,NULL,NULL,NULL);
size=recv(cs,(void*)&buf,BUFSIZE,0);
if (size==0) {
SetEvent(connectionCloseEvent);
return 0;
}
if (size==SOCKET_ERROR) {
/* General paranoia about blocking sockets */
ioctlsocket(cs,FIONBIO,&zero);
}
if (size!=SOCKET_ERROR) {
if (!WriteFile(hCom,buf,size,&wsize,&o)) {
if (GetLastError()==ERROR_IO_PENDING) {
// Wait for it...
if (!GetOverlappedResult(hCom,&o,&wsize,TRUE)) {
dprintf(1,"wconsd: Error %d (overlapped) writing to COM port\n",GetLastError());
}
} else {
dprintf(1,"wconsd: Error %d writing to COM port\n",GetLastError());
}
}
if (wsize!=size) {
dprintf(1,"wconsd: Eeek! WriteFile: wrote %d of %d\n",wsize,size);
}
}
}
return 0;
}

DWORD WINAPI wconsd_com_to_net(LPVOID lpParam)
{
unsigned char buf[BUFSIZE];
DWORD size;
OVERLAPPED o={0};

o.hEvent=readEvent;

dprintf(1,"wconsd: start wconsd_com_to_net\n");

while (WaitForSingleObject(threadTermEvent,0)!=WAIT_OBJECT_0) {
if (!ReadFile(hCom,buf,BUFSIZE,&size,&o)) {
if (GetLastError()==ERROR_IO_PENDING) {
// Wait for overlapped operation to complete
if (!GetOverlappedResult(hCom,&o,&size,TRUE)) {
dprintf(1,"wconsd: Error %d (overlapped) reading from COM port\n",GetLastError());
}
} else {
dprintf(1,"wconsd: Error %d reading from COM port\n",GetLastError());
SetEvent(connectionCloseEvent);
return 0;
}
}
if (size>0) {
send(cs,(void*)&buf,size,0);
}
}
return 0;
}

void send_help(struct connection *conn) {
netprintf(conn,"available commands:\r\n\n"
" port, speed, data, parity, stop\r\n"
" help, status, copyright\r\n"
" open, close, autoclose\r\n"
" quit\r\n");
}

void show_status(struct connection* conn) {
/* print the status to the net connection */

netprintf(conn, "status:\r\n\n"
" port=%d speed=%d data=%d parity=%d stop=%d\r\n\n",
com_port, com_speed, com_data, com_parity, com_stop);

if(com_state) {
netprintf(conn, " state=open autoclose=%d\r\n\n", com_autoclose);
} else {
netprintf(conn, " state=closed autoclose=%d\r\n\n", com_autoclose);
}
}

/*
* I thought that I would need this a lot, but it turns out that there
* was a lot of duplicated code
*/
int check_atoi(char *p,int old_value,struct connection *conn,char *error) {
if (!p) {
netprintf(conn,error);
return old_value;
}

return atoi(p);
}

int process_menu_line(struct connection*conn, char *line) {
DWORD errcode;
char *command;
char *parameter1;

/*
* FIXME - non re-entrant code
*
* based on winelib, I am unsure if windows has strtok_r
* and thus I am running these two strtok as close as possible.
* I am definitely not going to re-invent the wheel with my own
* code.
*/
command = strtok(line," ");
parameter1 = strtok(NULL," ");

if (!strcmp(command, "help") || !strcmp(command, "?")) {
// help
send_help(conn);
} else if (!strcmp(command, "status")) {
// status
show_status(conn);
} else if (!strcmp(command, "copyright")) { // copyright
netprintf(conn,
" Copyright (c) 2008 by Hamish Coleman <hamish@zot.org>\r\n"
" 2003 by Benjamin Schweizer <gopher at h07 dot org>\r\n"
" 1998 by Stephen Early <Stephen.Early@cl.cam.ac.uk>\r\n"
"\r\n"
"\r\n"
" This program is free software; you can redistribute it and/or modify\r\n"
" it under the terms of the GNU General Public License as published by\r\n"
" the Free Software Foundation; either version 2 of the License, or\r\n"
" (at your option) any later version.\r\n"
"\r\n"
" This program is distributed in the hope that it will be useful,\r\n"
" but WITHOUT ANY WARRANTY; without even the implied warranty of\r\n"
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r\n"
" GNU General Public License for more details.\r\n"
"\r\n"
" You should have received a copy of the GNU General Public License\r\n"
" along with this program; if not, write to the Free Software\r\n"
" Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r\n"
"\n");
} else if (!strcmp(command, "port")) { // port
int new = check_atoi(parameter1,com_port,conn,"must specify a port\r\n");

if (new >= 1 && new <= 16) {
com_port=new;
}
} else if (!strcmp(command, "speed")) { // speed
com_speed = check_atoi(parameter1,com_port,conn,"must specify a speed\r\n");
} else if (!strcmp(command, "data")) { // data
if (!strcmp(parameter1, "5")) {
com_data=5;
} else if (!strcmp(parameter1, "6")) {
com_data=6;
} else if (!strcmp(parameter1, "7")) {
com_data=7;
} else if (!strcmp(parameter1, "8")) {
com_data=8;
}
show_status(conn);
} else if (!strcmp(command, "parity")) { // parity
if (!strcmp(parameter1, "no") || !strcmp(parameter1, "0")) {
com_parity=NOPARITY;
} else if (!strcmp(parameter1, "even") || !strcmp(parameter1, "2")) {
com_parity=EVENPARITY;
} else if (!strcmp(parameter1, "odd") || !strcmp(parameter1, "1")) {
com_parity=ODDPARITY;
} else if (!strcmp(parameter1, "mark")) {
com_parity=MARKPARITY;
} else if (!strcmp(parameter1, "space")) {
com_parity=SPACEPARITY;
}
show_status(conn);
} else if (!strcmp(command, "stop")) {
if (!strcmp(parameter1, "one") || !strcmp(parameter1, "1")) {
com_stop=ONESTOPBIT;
} else if (!strcmp(parameter1, "one5") || !strcmp(parameter1, "1.5")) {
com_stop=ONE5STOPBITS;
} else if (!strcmp(parameter1, "two") || !strcmp(parameter1, "2")) {
com_stop=TWOSTOPBITS;
}
show_status(conn);
} else if (!strcmp(command, "open")) { // open
int new = check_atoi(parameter1,com_port,conn,"open default port\r\n");

if (new >= 1 && new <= 16) {
com_port=new;
}
if (com_state) {
/* port ist still open */
netprintf(conn,"\r\n\n");
/* signal to quit the menu */
conn->menuactive=0;
/* and return still running */
return 1;
}

if (!open_com_port(&errcode)) {
netprintf(conn,"\r\n\n");
// signal to quit the menu
conn->menuactive=0;
/* and return still running */
return 1;
} else {
netprintf(conn,"error: cannot open port\r\n\n");
}
} else if (!strcmp(command, "close")) { // close
close_com_port();
netprintf(conn,"info: actual com port closed\r\n\n");
} else if (!strcmp(command, "autoclose")) { // autoclose
if (!strcmp(parameter1, "true") || !strcmp(parameter1, "1") || !strcmp(parameter1, "yes")) {
com_autoclose=TRUE;
} else if (!strcmp(parameter1, "false") || !strcmp(parameter1, "0") || !strcmp(parameter1, "no")) {
com_autoclose=FALSE;
}
show_status(conn);
} else if (!strcmp(command, "quit")) {
// quit the connection
SetEvent(connectionCloseEvent);
conn->menuactive=0;
/* and return not running */
return 0;
} else if (!strcmp(command, "show_conn_table")) {
int i;
netprintf(conn,
"slot A id M mTh net netTh serial serialTh E netrx nettx\r\n");
netprintf(conn,
"---- - -- - --- --- ----- ------ -------- - ----- -----\r\n");
for (i=0;i<MAXCONNECTIONS;i++) {
netprintf(conn,
"%-4i %i %2i %i %3i %3i %5i %6i %8i %i %5i %5i\r\n",
i, connection[i].active, connection[i].id,
connection[i].menuactive, connection[i].menuThread,
connection[i].net, connection[i].netThread,
connection[i].serial, connection[i].serialThread,
connection[i].option_echo,
connection[i].net_bytes_rx, connection[i].net_bytes_tx
);
}
} else {
/* other, unknown commands */
netprintf(conn,"debug: line='%s', command='%s'\r\n\n",line,command);
}
/* return still running */
return 1;
}

int process_telnet_option(struct connection*conn, unsigned char *buf) {

switch (buf[1]) {
case 0xf0: /* suboption end */
case 241: /* NOP */
case 242: /* Data Mark */
dprintf(1,"wconsd[%i]: option IAC %i\n",conn->id,buf[1]);
return 1;
case 243: /* Break */
/* TODO */
dprintf(1,"wconsd[%i]: break not supported\n",conn->id);
return 1;
case 244: /* suspend */
case 245: /* abort output */
dprintf(1,"wconsd[%i]: option IAC %i\n",conn->id,buf[1]);
return 1;
case 246: /* are you there */
dprintf(1,"wconsd[%i]: option IAC AYT\n",conn->id);
netprintf(conn,"yes\r\n");
return 1;
case 247: /* erase character */
case 248: /* erase line */
case 249: /* go ahead */
dprintf(1,"wconsd[%i]: option IAC %i\n",conn->id,buf[1]);
return 1;
case 0xfa: /* suboption begin */
if (buf[3]==0) {
/*
* I dont expect us to get any IS statements ...
* and if we do, I'm going to need to rewrite the
* way chars are absorbed
*/
dprintf(1,"wconsd[%i]: option IAC SB %i IS\n",conn->id,buf[2]);
return 3;
} else if (buf[3]>1) {
dprintf(1,"wconsd[%i]: option IAC SB %i error\n",conn->id,buf[2]);
return 3;
}
dprintf(1,"wconsd[%i]: option IAC SB %i SEND\n",conn->id,buf[2]);
switch (buf[2]) {
case 5: /* STATUS */
netprintf(conn,"%s%c%s%s%s",
"\xff\xfa\x05",
0,
"\xfb\x05",
conn->option_echo?"\xfb\x01":"",
"\xff\xf0");
}
return 5;
case 0xfb: /* WILL */
dprintf(1,"wconsd[%i]: option IAC WILL %i\n",conn->id,buf[2]);
return 2;
case 0xfc: /* WONT */
dprintf(1,"wconsd[%i]: option IAC WONT %i\n",conn->id,buf[2]);
return 2;
case 0xfd: /* DO */
switch (buf[2]) {
case 0x01: /* ECHO */
dprintf(1,"wconsd[%i]: DO ECHO\n",conn->id);
conn->option_echo=1;
break;
case 0x03: /* suppress go ahead */
dprintf(1,"wconsd[%i]: DO suppress go ahead\n",conn->id);
break;
}
return 2;
case 0xfe: /* DONT */
dprintf(1,"wconsd[%i]: option IAC DONT %i\n",conn->id,buf[2]);
return 2;
case 0xff: /* send ff */
/* TODO - work out how to support this */
dprintf(1,"wconsd[%i]: does not support quoted 0xff\n",conn->id);
return 1;
default:
dprintf(1,"wconsd[%i]: option IAC %i %i\n",conn->id,buf[1],buf[2]);
}

/* not normally reached */
return 2;
}

int run_menu(struct connection * conn) {
unsigned char buf[BUFSIZE+5]; /* ensure there is room for our kludge telnet options */
unsigned char line[MAXLEN];
DWORD size, linelen=0;
WORD i;

unsigned long zero=0;

/* IAC WILL ECHO */
/* IAC WILL suppress go ahead */
/* IAC WILL status */
netprintf(conn,"\xff\xfb\x01\xff\xfb\x03\xff\xfb\x05");

netprintf(conn,"wconsd " VERSION " a serial port server\r\n\r\n");
send_help(conn);
netprintf(conn,"> ");

while (conn->menuactive) {
size=recv(conn->net,(void*)&buf,BUFSIZE,0);

if (size==0) {
closesocket(conn->net);
conn->net=INVALID_SOCKET;
SetEvent(connectionCloseEvent);
return 0; /* signal running=0 */
}
if (size==SOCKET_ERROR) {
dprintf(1,"wconsd[%i]: socket error\n",conn->id);
/* General paranoia about blocking sockets */
ioctlsocket(conn->net,FIONBIO,&zero);
continue;
}
conn->net_bytes_rx+=size;

for (i = 0; i < size; i++) {
if (buf[i]==0) {
// strange, I'm not expecting nul bytes !
continue;
} else if (buf[i] == 127 || buf[i]==8) {
// backspace
if (linelen > 0) {
netprintf(conn,"\x08 \x08");
linelen--;
} else {
/* if the linebuf is empty, ring the bell */
netprintf(conn,"\x07");
}
continue;
} else if (buf[i] == 0x0d || buf[i]==0x0a) {
// detected cr or lf

if (i+1<size && (buf[i+1]==0x0a || buf[i+1]==0)) {
i++;
}

if (conn->option_echo)
/* echo the endofline */
netprintf(conn,"\r\n");

if (linelen!=0) {
int running;
line[linelen]=0; // ensure string is terminated

running = process_menu_line(conn,(char*)line);
if (!conn->menuactive) {
/* exiting the menu.. */
return running;
}
}

netprintf(conn,"> ");
linelen=0;
continue;
} else if (buf[i] <0x20) {
/* ignore other ctrl chars */
continue;
} else if (buf[i]==0xff) {
// telnet option packet

/*
* Note that we avoid accessing outside the buffer
* by ensuring that the buffer is larger than
* the largest recv
*
* this doesnt help us if there is a split buffer from
* the client
*/
i+=process_telnet_option(conn,&buf[i]);
continue;
} else {
// other chars

if (linelen < MAXLEN - 1) {
line[linelen] = buf[i];
linelen++;
if (conn->option_echo) {
netprintf(conn,"%c",buf[i]); /* echo */
}
} else {
netprintf(conn,"\x07"); /* linebuf full bell */
}
continue;
}
}
}

/* not reached */
return 0;
}

DWORD WINAPI thread_new_connection(LPVOID lpParam) {
struct connection * conn = (struct connection*)lpParam;
HANDLE netThread=NULL, comThread=NULL;
int running=1;

while(running) {
dprintf(1,"wconsd[%i]: top of menu thread running loop\n",conn->id);
if (conn->menuactive) {
/* run the menu to ask the user questions */
running = run_menu(conn);
} else {
/* they must have opened the com port, so start the threads */
PurgeComm(hCom,PURGE_RXCLEAR|PURGE_RXABORT);
netThread=CreateThread(NULL,0,wconsd_net_to_com,NULL,0,NULL);
comThread=CreateThread(NULL,0,wconsd_com_to_net,NULL,0,NULL);

WaitForSingleObject(netThread,INFINITE);
WaitForSingleObject(comThread,INFINITE);

/*
* since I wait for the threads and there currently
* is no escape from the threads, we finish here
*/
running = 0;
}
}

/* cleanup */
dprintf(1,"wconsd[%i]: connection closing\n",conn->id);

/* TODO print bytecounts */
/* maybe close file descriptors? */
shutdown(conn->net,SD_BOTH);
closesocket(conn->net);
conn->active=0;

return 0;
}

static void wconsd_main(void)
{
HANDLE wait_array[3];
BOOL run=TRUE;
DWORD o;
SOCKET as;
HANDLE netThread=NULL, comThread=NULL;
unsigned long zero=0;

struct sockaddr_in sa;
int salen;
char buf[MAXLEN];

int i;

/* clear out any bogus data in the connections table */
for (i=0;i<MAXCONNECTIONS;i++) {
connection[i].active = 0;
}

/* Main loop: wait for a connection, service it, repeat
* until signalled that the service is terminating */
wait_array[0]=stopEvent;
wait_array[1]=listenSocketEvent;
wait_array[2]=connectionCloseEvent;

while (run) {
dprintf(1,"wconsd: top of wconsd_main run loop\n");

o=WaitForMultipleObjects(3,wait_array,FALSE,INFINITE);

switch (o-WAIT_OBJECT_0) {
case 0: /* stopEvent */
run=FALSE;
ResetEvent(stopEvent);
break;
case 1: /* listenSocketEvent */
/* There is an incoming connection */
WSAResetEvent(listenSocketEvent);
salen = sizeof(sa);
as=accept(ls,(struct sockaddr*)&sa,&salen);

if (as==INVALID_SOCKET) {
break;
}

if (!getnameinfo((struct sockaddr*)&sa,salen,buf,sizeof(buf),NULL,0,0)) {
dprintf(1,"wconsd: new connection from %08x\n",
htonl(sa.sin_addr.s_addr));
} else {
dprintf(1,"wconsd: new connection from %s\n",
&buf);
}

/* search for an empty connection slot */
i=0;
while(connection[i].active && i<MAXCONNECTIONS) {
i++;
}
if (i==MAXCONNECTIONS) {
dprintf(1,"wconsd: connection table overflow\n");
/* FIXME - properly reject the incoming connection */
/* for now, just close the socket */
closesocket(as);
break;
}
connection[i].active=1; /* mark this entry busy */
connection[i].id = next_connection_id++;
connection[i].menuactive=1; /* start in the menu */
connection[i].menuThread=NULL;
connection[i].net=as;
connection[i].netThread=NULL;
connection[i].serial=INVALID_HANDLE_VALUE;
connection[i].serialThread=NULL;
connection[i].option_echo=0;
connection[i].net_bytes_rx=0;
connection[i].net_bytes_tx=0;

dprintf(1,"wconsd[%i]: accepted new connection\n",connection[i].id);


/* we successfully accepted the connection */

if (cs!=INVALID_SOCKET) {
/* There is an existing connected socket */
/* Close down the existing connection and let the new one through */
SetEvent(threadTermEvent);
shutdown(cs,SD_BOTH);
WaitForSingleObject(netThread,INFINITE);
WaitForSingleObject(comThread,INFINITE);
CloseHandle(netThread);
CloseHandle(comThread);
closesocket(cs);
ResetEvent(connectionCloseEvent);
ResetEvent(threadTermEvent);
}
cs=as;
ioctlsocket(cs,FIONBIO,&zero);

connection[i].menuThread = CreateThread(NULL,0,thread_new_connection,&connection[i],0,NULL);

break;
case 2: /* connectionCloseEvent*/
/* The data connection has been broken */
dprintf(1,"wconsd: connectionCloseEvent\n");
SetEvent(threadTermEvent);
WaitForSingleObject(netThread,INFINITE);
WaitForSingleObject(comThread,INFINITE);
CloseHandle(netThread);
CloseHandle(comThread);
netThread=NULL;
comThread=NULL;
if (cs!=INVALID_SOCKET) {
shutdown(cs,SD_BOTH);
closesocket(cs);
cs=INVALID_SOCKET;
}
ResetEvent(connectionCloseEvent);
ResetEvent(threadTermEvent);
if (com_autoclose) {
close_com_port();
}
break;
default:
run=FALSE; // Stop the service - I want to get off!
break;
}
}

if (cs!=INVALID_SOCKET) {
shutdown(cs,SD_BOTH);
closesocket(cs);
}

if (netThread || comThread) {
SetEvent(threadTermEvent);
WaitForSingleObject(netThread,INFINITE);
WaitForSingleObject(comThread,INFINITE);
CloseHandle(netThread);
CloseHandle(comThread);
}

CloseHandle(hCom);
closesocket(ls);
WSACleanup();
}

VOID WINAPI MyServiceCtrlHandler(DWORD opcode)
{
DWORD status;

switch(opcode) {
case SERVICE_CONTROL_STOP:
wconsd_status.dwWin32ExitCode = 0;
wconsd_status.dwCurrentState = SERVICE_STOP_PENDING;
wconsd_status.dwCheckPoint = 0;
wconsd_status.dwWaitHint = 0;

if (!SetServiceStatus(wconsd_statusHandle, &wconsd_status)) {
status = GetLastError();
dprintf(1,"wconsd: SetServiceStatus error %ld\n",status);
}

SetEvent(stopEvent);
break;

case SERVICE_CONTROL_INTERROGATE:
// fall through to send current status
break;

default:
dprintf(1,"wconsd: unrecognised opcode %ld\n",opcode);
break;
}

// Send current status
if (!SetServiceStatus(wconsd_statusHandle, &wconsd_status)) {
status = GetLastError();
dprintf(1,"wconsd: SetServiceStatus error %ld\n",status);
}
return;
}

VOID WINAPI ServiceStart(DWORD argc, LPSTR *argv)
{
DWORD status;
DWORD specificError;

wconsd_status.dwServiceType = SERVICE_WIN32;
wconsd_status.dwCurrentState = SERVICE_START_PENDING;
wconsd_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
wconsd_status.dwWin32ExitCode = 0;
wconsd_status.dwServiceSpecificExitCode = 0;
wconsd_status.dwCheckPoint = 0;
wconsd_status.dwWaitHint = 0;
wconsd_statusHandle = RegisterServiceCtrlHandler(TEXT("wconsd"),MyServiceCtrlHandler);

if (wconsd_statusHandle == (SERVICE_STATUS_HANDLE)0) {
dprintf(1,"wconsd: RegisterServiceCtrlHandler failed %d\n", GetLastError());
return;
}

status = wconsd_init(argc, argv, &specificError);

if (status != NO_ERROR) {
wconsd_status.dwCurrentState = SERVICE_STOPPED;
wconsd_status.dwCheckPoint = 0;
wconsd_status.dwWaitHint = 0;
wconsd_status.dwWin32ExitCode = status;
wconsd_status.dwServiceSpecificExitCode = specificError;

SetServiceStatus(wconsd_statusHandle, &wconsd_status);
return;
}

/* Initialisation complete - report running status */
wconsd_status.dwCurrentState = SERVICE_RUNNING;
wconsd_status.dwCheckPoint = 0;
wconsd_status.dwWaitHint = 0;

if (!SetServiceStatus(wconsd_statusHandle, &wconsd_status)) {
status = GetLastError();
dprintf(1,"wconsd: SetServiceStatus error %ld\n",status);
}

wconsd_main();

wconsd_status.dwCurrentState = SERVICE_STOPPED;
wconsd_status.dwCheckPoint = 0;
wconsd_status.dwWaitHint = 0;
wconsd_status.dwWin32ExitCode = 0;
wconsd_status.dwServiceSpecificExitCode = 0;

SetServiceStatus(wconsd_statusHandle, &wconsd_status);

return;
}

static void RegisterService(LPSTR path)
{
SC_HANDLE schSCManager, schService;

schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

schService = CreateService(
schSCManager,
TEXT("wconsd"),
TEXT("wconsd - a serial port server"),
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
path,
NULL, NULL, NULL, NULL, NULL);

if (schService == NULL) {
printf("CreateService failed\n");
} else {
printf("Created service 'wconsd', binary path %s\n",path);
printf("You should now start the service using the service manager.\n");
}
CloseServiceHandle(schService);
}

static void RemoveService(void)
{
SC_HANDLE schSCManager, schService;

schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL) {
printf("Couldn't open service manager\n");
return;
}

schService = OpenService(schSCManager, TEXT("wconsd"), DELETE);

if (schService == NULL) {
printf("Couldn't open wconsd service\n");
return;
}

if (!DeleteService(schService)) {
printf("Couldn't delete wconsd service\n");
return;
}

printf("Deleted service 'wconsd'\n");

CloseServiceHandle(schService);
}

static void usage(void)
{
printf("Usage: wconsd [-i pathname | -r | -d]\n");
printf(" -i pathname install service 'wconsd'; pathname\n");
printf(" must be the full path to the binary\n");
printf(" -r remove service 'wconsd'\n");
printf(" -d run wconsd in debug mode (in the foreground)\n");
}

int main(int argc, char **argv)
{
DWORD err;
int console_application=0;
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ "wconsd", ServiceStart },
{ NULL, NULL }
};

// debug info for when I test this as a service
dprintf(1,"wconsd: started with argc==%i\n",argc);

if (argc==1 || argc==0) {

// assume that our messages are going to the debug log
debug_mode=0;

// start by trying to run as a service
if (StartServiceCtrlDispatcher(DispatchTable)==0) {
err = GetLastError();
dprintf(1,"wconsd: StartServiceCtrlDispatcher error = %d\n", err);

if (err != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
// any other error, assume fatal
return 1;
}
}

// fall through and try running as a command-line application
console_application=1;
}

// We are running in debug mode (or any other command-line mode)
debug_mode=1;

dprintf(1,"wconsd: Serial Console server\n");

if (argc>1) {
if (strcmp(argv[1],"-i")==0) {
// request service installation
if (argc!=3) {
usage();
return 1;
}
RegisterService(argv[2]);
return 0;
} else if (strcmp(argv[1],"-r")==0) {
// request service removal
RemoveService();
return 0;
} else if (strcmp(argv[1],"-p")==0) {
console_application=1;
default_tcpport = atoi(argv[2]);
} else if (strcmp(argv[1],"-d")==0) {
console_application=1;
} else {
usage();
return 1;
}
}

dprintf(1,"wconsd: listen on port %i\n",default_tcpport);

// if we have decided to run as a console app..
if (console_application) {
int r;
printf("wconsd: Console Application Mode (version %s)\n",VERSION);
r=wconsd_init(argc,argv,&err);
if (r!=0) {
printf("wconsd: wconsd_init failed, return code %d [%d]\n",r, err);
return 1;
}
wconsd_main();
}

return 0;
}

(3-3/3)