=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/plugin/oxmisc.c,v retrieving revision 1.1.1.1 retrieving revision 1.32 diff -u -p -r1.1.1.1 -r1.32 --- OpenXM/src/kan96xx/plugin/oxmisc.c 1999/10/08 02:12:05 1.1.1.1 +++ OpenXM/src/kan96xx/plugin/oxmisc.c 2020/10/06 11:33:47 1.32 @@ -1,4 +1,6 @@ +/* $OpenXM: OpenXM/src/kan96xx/plugin/oxmisc.c,v 1.31 2016/08/29 01:15:01 takayama Exp $ */ #include +#include #include #include #include @@ -10,7 +12,11 @@ #include #include #include -FILE *MyErrorOut = stdout; +#include +#include +#define SET_MYERROROUT { if (MyErrorOut == NULL) MyErrorOut=stdout; } +/* It is also defined in oxmisc2.c */ +FILE *MyErrorOut = NULL; /* Include files to understand object */ #include "../Kan/datatype.h" @@ -18,10 +24,13 @@ FILE *MyErrorOut = stdout; #include "../Kan/extern.h" #include "ox_kan.h" +#include "mysig.h" +void restoreLockCtrlCForOx(); // defined in Kan/stackmachine.c + #define READBUFSIZE 5000 -int OxVersion = 199909080; +int OxVersion = 200012030; int UseOXPacketSerial = 1; int SerialOX = 1; extern int Quiet; @@ -36,6 +45,7 @@ int readOneByte(int fd) /* blocking */ int ans; int watch = 1; + SET_MYERROROUT; if (fd < 0) { fprintf(MyErrorOut,"readOneByte fd < 0 ??? .\n"); return(-1); @@ -44,7 +54,7 @@ int readOneByte(int fd) /* blocking */ if (oxSocketSelect0(fd,-1)) { /* block */ size = read(fd,data,1); if (size == 0) { - fprintf(MyErrorOut,"oxSocketSelect0() returns 1, but there is no data. You peer may be killed.\n"); + fprintf(MyErrorOut,"oxSocketSelect0() returns 1, but there is no data. Your peer may be killed.\n"); return(-1); } return(data[0]); @@ -64,6 +74,7 @@ int readOneByte_org(int fd) /* blocking */ int ans; int watch = 1; + SET_MYERROROUT; if ((thisFd == -1) && (fd >= 0)) {thisFd = fd;} if (fd < 0) { fprintf(MyErrorOut,"readOneByte fd < 0 ??? .\n"); @@ -89,8 +100,8 @@ int readOneByte_org(int fd) /* blocking */ if (oxSocketSelect0(fd,-1)) { /* block */ size = read(fd,data,READBUFSIZE-1); if (size == 0) { - fprintf(MyErrorOut,"oxSocketSelect0() returns 1, but there is no data. You peer may be killed.\n"); - return(-1); + fprintf(MyErrorOut,"oxSocketSelect0() returns 1, but there is no data. Your peer may be killed.\n"); + return(-1); } from = 0; to = size; @@ -122,6 +133,7 @@ int oxGetInt32(ox_stream ostream) int oxGetCMOInt32(ox_stream ostream) { int id; + SET_MYERROROUT; id = oxGetInt32(ostream); if (id != CMO_INT32) { fprintf(MyErrorOut,"It is not CMO_INT32.\n"); @@ -134,6 +146,7 @@ char *oxGetCMOString(ox_stream ostream) { int size; char *r; int i; + SET_MYERROROUT; id = oxGetInt32(ostream); if (id != CMO_STRING) { fprintf(MyErrorOut,"It is not CMO_STRING.\n"); @@ -166,10 +179,31 @@ int oxWaitSyncBall(ox_stream ostream) { int sss; int mtag; + int com; while ((mtag = oxGetOXheader(ostream,&sss)) != OX_SYNC_BALL) { - fprintf(stderr,"Looking for the next message tag. mtag=%d\n",mtag); - /* or stdout */ + switch (mtag) { + case OX_COMMAND: + fprintf(stderr,"Waiting for command body: "); fflush(NULL); + com=oxGetInt32(ostream); + fprintf(stderr,"%d. Done\n",com); + break; + default: /* Todo, need support OX_DATA */ + fprintf(stderr,"Looking for the next message tag. Current unknown or unimplented mtag=%d\n",mtag); + if (UseOXPacketSerial) fprintf(stderr,"Note that we expect the OX message tag with a serial number.\n"); + if (UseOXPacketSerial && (sss == OX_SYNC_BALL)) { + /* dirty trick, it might cause a trouble. */ + fprintf(stderr,"We assume that the serial number is OX_SYNC_BALL\n"); + oxGetInt32(ostream); // discard the serial of OX_SYNC_BALL + goto aaa ; + } + break; + } + aaa: + /* or stdout */ fflush(NULL); + if (mtag == -1) { + fprintf(stderr,"Your peer seems to be dead.\n"); return 0; + } } } @@ -178,11 +212,12 @@ int oxWaitSyncBall_org(ox_stream ostream) int mtag; char data[4]; int c; + SET_MYERROROUT; data[0] = data[1] = data[2] = data[3] = 0xff; while (1) { /* This part should be revised so that this part understands the cmo format. - */ + */ if ((c = fp2fgetc(ostream)) < 0) { /* never use read directory. readOneByte() is buffers every thing. */ @@ -200,7 +235,7 @@ int oxWaitSyncBall_org(ox_stream ostream) return(mtag); } fprintf(stderr,"Looking for the next message tag.. %2x, mtag=%d\n",c,mtag); - /* or stdout */ + /* or stdout */ fflush(NULL); } } @@ -210,7 +245,7 @@ void oxfdSendCmoNull(int fd) { char data[4]; *((int *)&data[0]) = htonl(CMO_NULL); - write(fd,data,4); + {int r; r=write(fd,data,4);} fflush((FILE *)NULL); } void oxSendCmoNull(ox_stream os) @@ -242,7 +277,7 @@ void oxfdSendInt32(int fd,int k) { char data[4]; *((int *)&data[0]) = htonl(k); - write(fd,data,4); + {int r; r=write(fd,data,4);} fflush((FILE *)NULL); } void oxSendInt32(ox_stream os,int k) @@ -258,7 +293,7 @@ void oxfdSendCmoInt32(int fd,int k) char data[4*2]; *((int *)&data[0]) = htonl(CMO_INT32); *((int *)&data[4]) = htonl(k); - write(fd,data,4*2); + {int r; r=write(fd,data,4*2);} fflush((FILE *)NULL); } void oxSendCmoInt32(ox_stream os,int k) @@ -279,9 +314,9 @@ void oxfdSendCmoString(int fd,char *s) } *((int *)&data[0]) = htonl(CMO_STRING); *((int *)&data[4]) = htonl(n); - write(fd,data,4*2); + {int r; r=write(fd,data,4*2);} if (s != NULL) { - write(fd,s,n); + {int r; r=write(fd,s,n);} } fflush((FILE *)NULL); } @@ -317,7 +352,7 @@ void oxSendResultOfControlInt32(int fd,int i) oxfdSendOXheader(fd,OX_DATA,SerialOX++); *((int *)&data[0]) = htonl(CMO_INT32); *((int *)&data[4]) = htonl(i); - write(fd,data,4*2); + {int r; r=write(fd,data,4*2);} fflush((FILE *)NULL); } @@ -326,7 +361,7 @@ void oxSendResultOfControl(int fd) char data[4*1]; oxfdSendOXheader(fd,OX_DATA,SerialOX++); *((int *)&data[0]) = htonl(CMO_NULL); - write(fd,data,4*1); + {int r; r=write(fd,data,4*1);} fflush((FILE *)NULL); } @@ -334,8 +369,8 @@ void oxSendResultOfControl(int fd) void oxSendMathCap(ox_stream os,struct mathCap *mathcap) { - int i,n,infosize; - struct object mathinfo; + int i,n,infosize,ncmo; + struct object mathinfo = OINIT; /* printf("ox sending mathcap\n"); fflush(stdout); */ mathinfo = *((struct object *)(mathcap->infop)); infosize = getoaSize(mathinfo); @@ -363,23 +398,23 @@ void oxSendMathCap(ox_stream os,struct mathCap *mathca /* [2] */ oxSendInt32(os,CMO_LIST); - oxSendInt32(os,2); + n = mathcap->oxSize; + oxSendInt32(os,n); - /* first element */ oxSendInt32(os,CMO_LIST); - oxSendInt32(os,mathcap->oxSize); - n = mathcap->oxSize; + oxSendInt32(os,2); for (i=0; iox)[i]); + /* OX_DATA_xxx tags. In case of CMO, it is CMO tags. */ + oxSendInt32(os,CMO_LIST); + oxSendInt32(os,mathcap->n); + ncmo = mathcap->n; + for (i=0; icmo)[i]); + /* printf("i=%d %d, ",i,(mathcap->cmo)[i]); */ + } } - /* second element */ - oxSendInt32(os,CMO_LIST); - oxSendInt32(os,mathcap->n); - n = mathcap->n; - for (i=0; icmo)[i]); - /* printf("i=%d %d, ",i,(mathcap->cmo)[i]); */ - } /* printf("\n"); fflush(stdout); */ } @@ -436,7 +471,16 @@ void oxReqExecuteFunction(ox_stream os,char *s) fp2fflush(os); } +void oxReqExecuteFunctionWithOptionalArgument(ox_stream os,char *s) +{ + oxSendOXheader(os,OX_DATA,SerialOX++); + oxSendCmoString(os,s); + oxSendOXheader(os,OX_COMMAND,SerialOX++); + oxSendInt32(os,SM_executeFunctionWithOptionalArgument); + fp2fflush(os); +} + void oxReqPopString(ox_stream os) { oxSendOXheader(os,OX_COMMAND,SerialOX++); @@ -473,6 +517,7 @@ void oxReqPopCMO(ox_stream os) { int oxGetResultOfControlInt32(int fd) { int k; int sss; + SET_MYERROROUT; k = oxfdGetOXheader(fd,&sss); if (k != OX_DATA) { fprintf(MyErrorOut,"oxGetResultOfControlInt32: wrong header."); @@ -484,15 +529,15 @@ int oxGetResultOfControlInt32(int fd) { } int oxclientMultiSelect(oxclientp clients[],int dataready[], - int controlready[], int size, int t) + int controlready[], int size, int t) { int i, ddd; int fd; int humanfd = 0; fd_set readfds; struct timeval timeout; - extern int errno; + SET_MYERROROUT; /** printf("(1)"); fflush(NULL); */ FD_ZERO(&readfds); timeout.tv_sec = 0; @@ -507,28 +552,32 @@ int oxclientMultiSelect(oxclientp clients[],int datare fd = (fdcontrolfd?clients[i]->controlfd:fd); - FD_SET(clients[i]->controlfd,&readfds); - if (oxSocketSelect0(clients[i]->controlfd,0)) { - ddd = controlready[i] = 1; + if (clients[i]->controlport < 0) { /* For RFC_101 */ + controlready[i] = 0; }else{ - controlready[i] = 0; + fd = (fdcontrolfd?clients[i]->controlfd:fd); + FD_SET(clients[i]->controlfd,&readfds); + if (oxSocketSelect0(clients[i]->controlfd,0)) { + ddd = controlready[i] = 1; + }else{ + controlready[i] = 0; + } } if (clients[i]->datafp2 != NULL) { - fd = (fddatafp2->fd?clients[i]->datafp2->fd:fd); - FD_SET(clients[i]->datafp2->fd,&readfds); - if (fp2select(clients[i]->datafp2,0)) { - ddd = dataready[i] = 1; - }else{ - dataready[i] = 0; - } + fd = (fddatafp2->fd?clients[i]->datafp2->fd:fd); + FD_SET(clients[i]->datafp2->fd,&readfds); + if (fp2select(clients[i]->datafp2,0)) { + ddd = dataready[i] = 1; + }else{ + dataready[i] = 0; + } }else{ - dataready[i] = 0; + dataready[i] = 0; } } } @@ -553,7 +602,7 @@ int oxclientMultiSelect(oxclientp clients[],int datare } int oxGetControl(oxclientp client) -/* synchronized. */ + /* synchronized. */ { int ans; ox_stream os; @@ -584,6 +633,7 @@ int oxInitClient(oxclientp client) client->mathcapObjp = NULL; client->controlByteOrder = OX_BYTE_NETWORK_BYTE_ORDER; client->engineByteOrder = OX_BYTE_NETWORK_BYTE_ORDER; + client->engineID = -1; return(0); } @@ -594,10 +644,10 @@ int oxIsThereErrorClient(oxclientp client) { return(0); } -oxclientp oxCreateClient(char *sname,int portStream,int portControl) - /* you also need to change oxCreateClient2. */ +oxclientp oxCreateClient(char *sname,int portStream,int portControl, + char *passControl, char *passData) + /* you also need to change oxCreateClient2. */ { - static int clnum = 0; int v = 0; int fdControl = -1; int fdStream = -1; @@ -619,16 +669,33 @@ oxclientp oxCreateClient(char *sname,int portStream,in return(NULL); } + if (passControl != NULL) { + if (v) fprintf(stderr,"Sending password %s for the control channel.\n", + passControl); + if (write(fdControl,passControl,strlen(passControl)+1) < 0) { + fprintf(stderr,"oxCreateClient(): failed to send passControl.\n"); + return(NULL); + } + } + if (passData != NULL) { + if (v) fprintf(stderr,"Sending password %s for the data channel.\n", + passData); + if (write(fdStream,passData,strlen(passData)+1) < 0) { + fprintf(stderr,"oxCreateClient(): failed to send passData.\n"); + return(NULL); + } + } + controlByteOrder = oxSetByteOrder(fdControl); if (v) fprintf(stderr,"Byte order for control process is %s.\n", - (controlByteOrder == 0? "network byte order": - (controlByteOrder == 1? "little indican": - "big indian"))); + (controlByteOrder == 0? "network byte order": + (controlByteOrder == 1? "little indican": + "big indian"))); engineByteOrder = oxSetByteOrder(fdStream); if (v) fprintf(stderr,"Byte order for engine process is %s.\n", - (engineByteOrder == 0? "network byte order": - (engineByteOrder == 1? "little indican": - "big indian"))); + (engineByteOrder == 0? "network byte order": + (engineByteOrder == 1? "little indican": + "big indian"))); client = (oxclientp) mymalloc(sizeof(oxclient)); oxInitClient(client); @@ -640,7 +707,7 @@ oxclientp oxCreateClient(char *sname,int portStream,in client->dataport = portStream; client->controlport = portControl; client->controlfd = fdControl; - client->id = clnum; clnum++; + client->id = oxGetClientID(); client->type = CLIENT_SOCKET; /* socket */ client->engineByteOrder = engineByteOrder; client->controlByteOrder = controlByteOrder; @@ -707,7 +774,7 @@ oxclientp oxCreateClientFile(char *fname,char *mode,ch } void oxSendOXheader_generic(int type,int fd,ox_stream ox, - int k,int serial) + int k,int serial) { static int ss = 0; extern int UseOXPacketSerial; @@ -774,7 +841,7 @@ int oxGetOXheader(ox_stream ostream,int *sss) } -oxWritePortFile(int func,int port,char *fname) { +int oxWritePortFile(int func,int port,char *fname) { char name[1024]; FILE *fp; strcpy(name,fname); @@ -790,7 +857,7 @@ oxWritePortFile(int func,int port,char *fname) { fclose(fp); } } -oxReadPortFile(int func,char *fname) { +int oxReadPortFile(int func,char *fname) { int port = 0; char name[1024]; FILE *fp; @@ -798,12 +865,12 @@ oxReadPortFile(int func,char *fname) { if (func == 0) { strcat(name,".control"); fp = fopen(name,"r"); - fscanf(fp,"%d",&port); + {int r; r=fscanf(fp,"%d",&port);} fclose(fp); }else { strcat(name,".data"); fp = fopen(name,"r"); - fscanf(fp,"%d",&port); + {int r; r=fscanf(fp,"%d",&port);} fclose(fp); } return(port); @@ -829,6 +896,7 @@ char *oxGenPortFile(void) { int oxRemovePortFile(void) { char fname[1024]; FILE *fp; + SET_MYERROROUT; strcpy(fname,OxPortFileName); strcat(fname,".control"); if ((fp=fopen(fname,"r")) == NULL) { @@ -851,29 +919,43 @@ int oxRemovePortFile(void) { } char *oxGenPass(void) { - int seed; + static int seed = 0; long p; char *s; - seed = (int) time(NULL); - srandom((unsigned int) seed); - p = random(); + int i,n; + if (seed == 0) { + seed = (int) time(NULL) + (int) ((long) &p); + srandom((unsigned int) seed); + } s = (char *)malloc(128*sizeof(char)); + if (s == NULL) { fprintf(stderr,"No more memory.\n"); return(s); } + n = (((int)((long) s)) + (int) time(NULL)) % 100; + for (i=0; i < n ; i++) random(); + p = random(); sprintf(s,"%ld",p); return(s); } static void cancelConnection() { +#if defined(__CYGWIN__) + extern sigjmp_buf MyEnv_oxmisc; +#else extern jmp_buf MyEnv_oxmisc; - signal(SIGALRM,SIG_IGN); +#endif + mysignal(SIGALRM,SIG_IGN); fprintf(stderr,"Time out in TCP/IP connection.\n"); - longjmp(MyEnv_oxmisc,1); +#if defined(__CYGWIN__) + MYSIGLONGJMP(MyEnv_oxmisc,1); +#else + MYLONGJMP(MyEnv_oxmisc,1); +#endif } oxclientp oxCreateClient2(int fdstream,int portStream, - int fdcontrol,int portControl,int ipmask,char *pass) + int fdcontrol,int portControl,int ipmask, + char *passControl, char *passData) { - static int clnum = 0; int v = 0; int fdControl = -1; int fdStream = -1; @@ -881,16 +963,24 @@ oxclientp oxCreateClient2(int fdstream,int portStream, char *s; oxclientp client; - extern jmp_buf MyEnv_oxmisc ; +#if defined(__CYGWIN__) + extern sigjmp_buf MyEnv_oxmisc; +#else + extern jmp_buf MyEnv_oxmisc; +#endif int controlByteOrder, engineByteOrder; v = !Quiet; - if (setjmp(MyEnv_oxmisc)) { +#if defined(__CYGWIN__) + if (MYSIGSETJMP(MyEnv_oxmisc,1)) { +#else + if (MYSETJMP(MyEnv_oxmisc)) { +#endif return(NULL); }else{ } - alarm((unsigned int) 10); /* setup timeout. */ - signal(SIGALRM,cancelConnection); + alarm((unsigned int) 20); /* setup timeout. */ + mysignal(SIGALRM,cancelConnection); switch(ipmask) { case 0:/* only local */ @@ -906,37 +996,44 @@ oxclientp oxCreateClient2(int fdstream,int portStream, if (v) fprintf(stderr,"\nStream port %d : Connected.\n",portStream); if (fdStream == -1 || fdControl == -1) { - fprintf(stderr,"\nOpen error in oxCreateClient.\n"); + fprintf(stderr,"\nOpen error in oxCreateClient2.\n"); + fprintf(stderr,"fdStream=%d, fdControl=%d\n",fdStream,fdControl); return(NULL); } - /* Authentification by password. */ - m = strlen(pass); - s = (char *)mymalloc(sizeof(char)*(m+1)); - read(fdControl,s,m); s[m] = '\0'; - if (strcmp(s,pass) != 0) { - fprintf(stderr,"oxCreateClient2(): password authentification failed for control channel.\n"); - close(fdControl); - return(NULL); + /* Authentication by password. */ + m = strlen(passControl)+strlen(passData); + if (m > 0) { + s = (char *)mymalloc(sizeof(char)*(m+1)); + m = strlen(passControl); s[0] = 0; + {int r; r=read(fdControl,s,m+1);} s[m] = '\0'; + if (strcmp(s,passControl) != 0) { + fprintf(stderr,"s=%s, passControl=%s\n",s,passControl); + fprintf(stderr,"oxCreateClient2(): password authentication failed for control channel.\n"); + close(fdControl); + return(NULL); + } + m = strlen(passData); s[0] = 0; + {int r; r=read(fdStream,s,m+1);} s[m] = '\0'; + if (strcmp(s,passData) != 0) { + fprintf(stderr,"s=%s, passData=%s\n",s,passData); + fprintf(stderr,"oxCreateClient2(): password authentication failed for data channel.\n"); + close(fdStream); + return(NULL); + } } - read(fdStream,s,m); s[m] = '\0'; - if (strcmp(s,pass) != 0) { - fprintf(stderr,"oxCreateClient2(): password authentification failed for data channel.\n"); - close(fdStream); - return(NULL); - } - signal(SIGALRM,SIG_IGN); + mysignal(SIGALRM,SIG_IGN); controlByteOrder = oxSetByteOrder(fdControl); if (v) fprintf(stderr,"Byte order for control process is %s.\n", - (controlByteOrder == 0? "network byte order": - (controlByteOrder == 1? "little indican": - "big indian"))); + (controlByteOrder == 0? "network byte order": + (controlByteOrder == 1? "little indican": + "big indian"))); engineByteOrder = oxSetByteOrder(fdStream); if (v) fprintf(stderr,"Byte order for engine process is %s.\n", - (engineByteOrder == 0? "network byte order": - (engineByteOrder == 1? "little indican": - "big indian"))); + (engineByteOrder == 0? "network byte order": + (engineByteOrder == 1? "little indican": + "big indian"))); client = (oxclientp) mymalloc(sizeof(oxclient)); @@ -949,7 +1046,7 @@ oxclientp oxCreateClient2(int fdstream,int portStream, client->dataport = portStream; client->controlport = portControl; client->controlfd = fdControl; - client->id = clnum; clnum++; + client->id = oxGetClientID(); client->type = CLIENT_SOCKET; /* socket */ client->engineByteOrder = engineByteOrder; client->controlByteOrder = controlByteOrder; @@ -961,30 +1058,91 @@ int oxSetByteOrder(int fd) { int peertype; /* It is for client. read and next write. */ /* oxSocketSelect0(fd,10); wait. */ - read(fd,data,1); + {int r; r=read(fd,data,1);} peertype = (unsigned char) data[0]; /* We support only Network byte order */ data[0] = OX_BYTE_NETWORK_BYTE_ORDER; - write(fd,data,1); + {int r; r=write(fd,data,1);} return(OX_BYTE_NETWORK_BYTE_ORDER); } -int oxTellMyByteOrder(int fd) { +int oxTellMyByteOrder(int fdOut, int fdIn) { char data[1]; int peertype; /* It is for server. read and next write. */ /* We support only Network byte order */ data[0] = OX_BYTE_NETWORK_BYTE_ORDER; - write(fd,data,1); - fsync(fd); /* returns 0 if normal. Does it work for socket? */ + {int r; r=write(fdOut,data,1);} + fsync(fdOut); /* returns 0 if normal. Does it work for socket? */ - read(fd,data,1); + {int r; r=read(fdIn,data,1);} /* Read pear's byte order */ return(OX_BYTE_NETWORK_BYTE_ORDER); } +struct object OxClientList[MAX_N_OF_CLIENT]; +int OxClientListn = 0; +int oxGetClientID() { + extern struct object OxClientList[]; + extern int OxClientListn; + extern struct object Nobj; + int i; + for (i=0; i