/* main.c www.eit.se/hcpips Copyright 2008 Henrik Björkman www.eit.se May be redistributed as under Gnu General Public License version 3. http://www.gnu.org/licenses/licenses.html#GPL You may modify this code as long as you document the modifications you have made in the History section below and do not remove this copyright notice and the file history section. It should work with both Windows and UNIX (such as Solaris & Linux). But the latest version is only tested under Win32. History: Created 1995-02-01 by Henrik Bjorkman. Ported to win32s 2000-04-18 by Henrik and Bengt Englund. Added some command lins switches. Henrik 2003-07-30 050709 Improved comments and renamed some function to make things more clear. Henrik 2008-09-11 Added features to connect to a serial port. 2008-09-23 Added feature local host only. Henrik 2008-10-19 Special handling if second socket in a connection is closed. Henrik */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include #include #include #include #ifdef WIN32 #include #include /* http://predef.sourceforge.net/precomp.html */ #if (!((defined(_MSC_VER)) && (_MSC_VER < 1300))) #include #endif #include #else #include #include #endif #include "version.h" #include "ClientStateAndData.h" #include "ClientServerMapper.h" //#include "hca.h" #include "hcb.h" //#define DEBUG /* Macro for debug printouts */ #ifdef DEBUG #define D(x) x #else #define D(x) #endif //char ogm[MaxData]; //int ogm_len=0; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* Same as standard fopen but execution is terminated on error. */ /* FILE *h_fopen(char* name,char* mode) { FILE *fp=fopen(name,mode); if (fp==NULL) { perror("fopen"); fprintf(stderr,"Could not open file %s\n",name); Hcb::cleanUp(); exit(2); } return(fp); } */ /* To write a buffer in hex, but only first 16 bytes */ #ifdef DEBUG static void HexPrint(const char *prefix, unsigned char *ptr, int len, const char *postfix) { printf("%s",prefix); if (len!=0) { for(;;) { printf("%02x",*ptr++); len--; if (len==0) { break; } //printf(" "); } } printf("%s",postfix); } /* To write all graphic characters in a buffer */ static void GraphPrint(char *ptr, int len) { D(printf("GraphPrint %2d: ",len);) if (len>64) len=64; while(len-->0) { if (isgraph(*ptr)) {putc(*ptr,stdout);} else {putc('.',stdout);} ptr++; } /*printf("\n");*/ fflush(stdout); } #endif /* static const char* findEoln(const char *str, int n) { while (n>0) { if (!isprint(*str)) { return str; } str++; n--; } return NULL; } static void movemem(char *dst, const char* src, int n) { while (n) { *dst=*src; dst++; src++; n--; } } */ int parseArgsInCommandLine(const char *cmd, char *buffer, int bufferSize, char **argv_p, int maxArgs) { int i=0; int bufferIndex=0; for(;;) { //int j=0; // First skip leading space while ((*cmd!=0) && (!isgraph(*cmd))) { cmd++; } if (*cmd==0) { break; } assert(i TCP/IP port number to use\n"); printf("-w Set expected identifier to \n"); //printf("-l Localhost only, dont accept only connections from other computers\n"); printf("-a accept connections from other computers\n"); //printf("-b Batch mode (no console input expected)\n"); printf("\nexample:\n"); printf("%s -p9998 -a -wbanana\n", arg0); printf("This will tell server to use port 9998\n"); printf("Allow connections from any computer\n"); printf("Set password to banana\n"); printf("\nwww.eit.se\n"); Hcb::cleanUp(); exit(1); } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* main */ int main(int argc,char *argv[]) { //char *file_name=NULL; int tcp_port=9999; int server_socket; //int latest_fd=HCB_NO_FD; //int err; //int baudrate=0; /*#if WIN32 char *serial_dev="COM1"; #else char *serial_dev="/dev/ttyS0"; #endif*/ int argp=1; //Hcb *standard_input_hca=NULL; //Hcb *serialHca=NULL; //bool batchMode=false; bool only_localhost=true; while ((argp=MaxData) break; ogm[ogm_len]=ch; ogm_len++; } fclose(fp); printf("File loaded. %d bytes\n",ogm_len); }*/ Hcb::init(); server_socket=Hcb::server(tcp_port); if (server_socket!=HCB_NO_FD) { printf("server socket %d connected to tcpip port %d\n",server_socket, tcp_port); } else { fprintf(stderr, "failed to open server socket on port %d\n", tcp_port); Hcb::cleanUp(); exit(1); } /* Use this to open a serial port */ /* Currently we open only one serial port, it would not be to hard to open more */ /*if (baudrate!=0) { serialHca=Hcb::open_serial_port(serial_dev, baudrate); if (serialHca==NULL) { fprintf(stderr, "Failed to open serial port %s at %d\n", serial_dev, baudrate); } else { clientServerMapper.newClient(serialHca); } }*/ #ifdef WIN32 if (!batchMode) { standard_input_hca=Hcb::open_standard_input(); if (standard_input_hca==NULL) { fprintf(stderr, "not using standard input\n"); } else { clientServerMapper.newClient(standard_input_hca, 1); } } #endif for(;;) { //char buf[200]={0}; //int len; // Wait for input from some socket Hcb::wait(10000); { Hcb *hcb=Hcb::accept(server_socket, only_localhost); // Check if a new client has connected. if (hcb!=NULL) { // yes a new client has connected. D(printf("client connected\n");) clientServerMapper.newClient(hcb, 0); } } if (clientServerMapper.process()==-2) { break; }; } Hcb::cleanUp(); return 0; }