=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/plugin/mytcpio.c,v retrieving revision 1.1.1.1 retrieving revision 1.17 diff -u -p -r1.1.1.1 -r1.17 --- OpenXM/src/kan96xx/plugin/mytcpio.c 1999/10/08 02:12:05 1.1.1.1 +++ OpenXM/src/kan96xx/plugin/mytcpio.c 2020/10/06 11:33:47 1.17 @@ -1,10 +1,14 @@ +/* $OpenXM: OpenXM/src/kan96xx/plugin/mytcpio.c,v 1.16 2005/07/03 11:08:54 ohara Exp $ */ #include +#include #include #include #include #include #include #include +#include +#include /* -lnsl -lsocket /usr/ucblib/libucb.a */ #include "ox_kan.h" /* @@ -14,29 +18,34 @@ #if defined(sun) int MyEnv_oxmisc[2000]; #else +#if defined(__CYGWIN__) +sigjmp_buf MyEnv_oxmisc; /* may cause a trouble in Solaris. */ +#else jmp_buf MyEnv_oxmisc; /* may cause a trouble in Solaris. */ #endif +#endif #define READBUFSIZE 10000 +#define MAX_LISTEN_QUEUE 3 static void errorMsg1s(char *s) { fprintf(stderr,"%s\n",s); } -FILE *TcpioError = stdout; +#define SET_TCPIOERROR { if (TcpioError == NULL) TcpioError = stdout; } +FILE *TcpioError = NULL; int OpenedSocket = 0; extern int Quiet; -socketOpen(char *serverName,int portNumber) { +int socketOpen(char *serverName,int portNumber) { static struct hostent *myhost; static struct sockaddr_in me; static int s_waiting; static int on; - extern int errno; int tt; - - fprintf(TcpioError,"Hello from open. serverName is %s and portnumber is %d\n", - serverName,portNumber); + SET_TCPIOERROR; + if (!Quiet) fprintf(TcpioError,"Hello from open. serverName is %s and portnumber is %d\n", + serverName,portNumber); if ((myhost = gethostbyname(serverName)) == NULL) { errorMsg1s("Bad server name."); return(-1); @@ -45,7 +54,7 @@ socketOpen(char *serverName,int portNumber) { me.sin_family = AF_INET; me.sin_port = htons(portNumber); bcopy(myhost->h_addr, - &me.sin_addr,myhost->h_length); + &me.sin_addr,myhost->h_length); if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) { errorMsg1s("Socket allocation is failed."); @@ -67,26 +76,31 @@ socketOpen(char *serverName,int portNumber) { return(-1); } - if (listen(s_waiting,1) < 0) { + if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) { errorMsg1s("Listen failed"); return(-1); } - fprintf(TcpioError,"Done the initialization. port =%d\n",ntohs(me.sin_port)); + if (!Quiet) fprintf(TcpioError,"Done the initialization. port =%d\n",ntohs(me.sin_port)); OpenedSocket = ntohs(me.sin_port); return(s_waiting); } -socketAccept(int snum) { +int socketAccept(int snum) { int s, news; + SET_TCPIOERROR; s = snum; - fprintf(TcpioError,"Trying to accept... "); fflush(TcpioError); + if (!Quiet) {fprintf(TcpioError,"Trying to accept... "); fflush(TcpioError);} if ((news = accept(s,NULL,NULL)) < 0) { - errorMsg1s("Error in accept."); - return(-1); + errorMsg1s("Error in accept. Retrying (socketAccept)."); + /* Code added for strange behavior on cygwin. */ + if ((news = accept(s,NULL,NULL)) < 0) { + errorMsg1s("Error in accept. Retry failed."); + return (-1); + } } - fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError); + if (!Quiet) {fprintf(TcpioError,"Accepted.\n"); fflush(TcpioError);} if (close(s) < 0) { errorMsg1s("Error in closing the old socket."); return(-1); @@ -94,38 +108,43 @@ socketAccept(int snum) { return(news); } -socketAcceptLocal(int snum) { +int socketAcceptLocal(int snum) { int s, news; struct sockaddr peer; int len; int i; - + + SET_TCPIOERROR; s = snum; - fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError); + if (!Quiet) {fprintf(TcpioError,"Trying to accept from localhost... "); fflush(TcpioError);} len = sizeof(struct sockaddr); if ((news = accept(s,&peer,&len)) < 0) { - errorMsg1s("Error in accept."); - return(-1); + errorMsg1s("Error in accept. Retrying"); + /* Code added for strange behavior on cygwin. */ + if ((news = accept(s,&peer,&len)) < 0) { + errorMsg1s("Error in accept. Retry failed."); + return (-1); + } } len = sizeof(struct sockaddr); getpeername(news,&peer,&len); - printf("len= %d\n",len); + if (!Quiet) printf("len= %d\n",len); for (i=0; ih_addr, - (char *)&server.sin_addr,servhost->h_length); + (char *)&server.sin_addr,servhost->h_length); if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) { errorMsg1s("socket allocation is failed.\n"); @@ -235,7 +295,7 @@ socketConnect(char *serverName,int portNumber) { } /* on=1; setsockopt(socketid,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); */ if (!Quiet) { - fprintf(TcpioError,"Trying to connect port %d, ip=%x\n",ntohs(server.sin_port),server.sin_addr); + fprintf(TcpioError,"Trying to connect port %d, ip=%lx\n",ntohs(server.sin_port),(long) server.sin_addr.s_addr); } if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) { errorMsg1s("cannot connect"); @@ -245,14 +305,17 @@ socketConnect(char *serverName,int portNumber) { return(socketid); } -socketConnectWithPass(char *servername,int port,char *pass) +int socketConnectWithPass(char *servername,int port,char *pass) { int fd; int m; + SET_TCPIOERROR; fd = socketConnect(servername,port); + if ((pass == NULL) && (fd >= 0)) return fd; + if ((pass == NULL) && (fd < 0)) return -1; if (fd >= 0) { - m = write(fd,pass,strlen(pass)); - if (m != strlen(pass)) { + m = write(fd,pass,strlen(pass)+1); + if (m != strlen(pass)+1) { fprintf(TcpioError,"Fail to send password to fd=%d.\n",fd); return(-1); }