=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/plugin/mytcpio.c,v retrieving revision 1.1.1.1 retrieving revision 1.13 diff -u -p -r1.1.1.1 -r1.13 --- OpenXM/src/kan96xx/plugin/mytcpio.c 1999/10/08 02:12:05 1.1.1.1 +++ OpenXM/src/kan96xx/plugin/mytcpio.c 2004/02/25 23:14:35 1.13 @@ -1,3 +1,4 @@ +/* $OpenXM: OpenXM/src/kan96xx/plugin/mytcpio.c,v 1.12 2003/09/16 02:57:39 takayama Exp $ */ #include #include #include @@ -5,6 +6,7 @@ #include #include #include +#include /* -lnsl -lsocket /usr/ucblib/libucb.a */ #include "ox_kan.h" /* @@ -14,15 +16,21 @@ #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; @@ -31,12 +39,11 @@ socketOpen(char *serverName,int portNumber) { static struct sockaddr_in me; static int s_waiting; static int on; - extern int errno; int tt; - + SET_TCPIOERROR; fprintf(TcpioError,"Hello from open. serverName is %s and portnumber is %d\n", - serverName,portNumber); + serverName,portNumber); if ((myhost = gethostbyname(serverName)) == NULL) { errorMsg1s("Bad server name."); return(-1); @@ -45,7 +52,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,7 +74,7 @@ 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); } @@ -80,11 +87,16 @@ socketOpen(char *serverName,int portNumber) { socketAccept(int snum) { int s, news; + SET_TCPIOERROR; s = snum; 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 (close(s) < 0) { @@ -99,13 +111,18 @@ socketAcceptLocal(int snum) { struct sockaddr peer; int len; int i; - + + SET_TCPIOERROR; s = snum; 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); @@ -117,9 +134,9 @@ socketAcceptLocal(int snum) { printf("\n"); if (peer.sa_data[2] == 0x7f && peer.sa_data[3] == 0 && peer.sa_data[4] == 0 && peer.sa_data[5] == 1) { - fprintf(stderr,"Authentification: localhost is allowed to be accepted.\n"); + fprintf(stderr,"Authentication: localhost is allowed to be accepted.\n"); }else{ - errorMsg1s("Authentification: The connection is not from the localhost."); + errorMsg1s("Authentication: The connection is not from the localhost."); close(s); fprintf(stderr,"The connection is refused."); return(-1); @@ -133,11 +150,51 @@ socketAcceptLocal(int snum) { return(news); } +/* It does not close the socket for listening. */ +socketAcceptLocal2(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); + len = sizeof(struct sockaddr); + if ((news = accept(s,&peer,&len)) < 0) { + errorMsg1s("Error in accept. Retrying (socketAcceptLocal2)."); + /* 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); + 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"); @@ -249,10 +307,13 @@ socketConnectWithPass(char *servername,int port,char * { 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); }