[BACK]Return to oxmisc.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/oxmisc.c, Revision 1.4

1.4     ! takayama    1: /*  $OpenXM: OpenXM/src/kan96xx/plugin/oxmisc.c,v 1.3 1999/11/03 08:29:40 takayama Exp $ */
1.1       maekawa     2: #include <stdio.h>
                      3: #include <sys/types.h>
                      4: #include <sys/stat.h>
                      5: #include <sys/socket.h>
                      6: #include <sys/time.h>
                      7: #include <netinet/in.h>
                      8: #include <netdb.h>
                      9: #include <fcntl.h>
                     10: #include <stdlib.h>
                     11: #include <unistd.h>
                     12: #include <signal.h>
                     13: #include <setjmp.h>
                     14: FILE *MyErrorOut = stdout;
                     15:
                     16: /* Include files to understand object */
                     17: #include "../Kan/datatype.h"
                     18: #include "../Kan/stackm.h"
                     19: #include "../Kan/extern.h"
                     20:
                     21: #include "ox_kan.h"
                     22:
                     23: #define READBUFSIZE 5000
                     24:
                     25: int OxVersion = 199909080;
                     26: int UseOXPacketSerial = 1;
                     27: int SerialOX = 1;
                     28: extern int Quiet;
                     29:
                     30: static char *OxPortFileName = ".ox.dummy";
                     31:
                     32:
                     33: int readOneByte(int fd)   /* blocking */
                     34: {
                     35:   static char data[1];
                     36:   int size;
                     37:   int ans;
                     38:   int watch = 1;
                     39:
                     40:   if (fd < 0) {
                     41:     fprintf(MyErrorOut,"readOneByte fd < 0 ??? .\n");
                     42:     return(-1);
                     43:   }
                     44:
                     45:   if (oxSocketSelect0(fd,-1)) { /* block */
                     46:     size = read(fd,data,1);
                     47:     if (size == 0) {
                     48:       fprintf(MyErrorOut,"oxSocketSelect0() returns 1, but there is no data. You peer may be killed.\n");
                     49:       return(-1);
                     50:     }
                     51:     return(data[0]);
                     52:   }else{
                     53:     fprintf(MyErrorOut,"readOneByte: select error in block mode. Retrying.\n");
                     54:     return(-1);
                     55:   }
                     56: }
                     57:
                     58: int readOneByte_org(int fd)   /* blocking */
                     59: {
                     60:   static char data[READBUFSIZE];
                     61:   static int thisFd = -1;
                     62:   static int from = 0;
                     63:   static int to = 0;
                     64:   int size;
                     65:   int ans;
                     66:   int watch = 1;
                     67:
                     68:   if ((thisFd == -1) && (fd >= 0)) {thisFd = fd;}
                     69:   if (fd < 0) {
                     70:     fprintf(MyErrorOut,"readOneByte fd < 0 ??? .\n");
                     71:     return(-1);
                     72:   }
                     73:
                     74:   if (fd != thisFd) {
                     75:     fprintf(MyErrorOut,"readOneByte can be used only for one fd.\n");
                     76:     fflush(NULL);
                     77:     return(-1);
                     78:   }
                     79:   if (to > from) {
                     80:     ans = data[from];
                     81:     from++;
                     82:     if (watch) {
                     83:       printf("%2x ",ans);
                     84:       fflush(stdout);
                     85:     }
                     86:     return(ans);
                     87:   }
                     88:
                     89:   while (1) {
                     90:     if (oxSocketSelect0(fd,-1)) { /* block */
                     91:       size = read(fd,data,READBUFSIZE-1);
                     92:       if (size == 0) {
                     93:        fprintf(MyErrorOut,"oxSocketSelect0() returns 1, but there is no data. You peer may be killed.\n");
                     94:        return(-1);
                     95:       }
                     96:       from = 0;
                     97:       to = size;
                     98:       return(readOneByte(fd));
                     99:     }else{
                    100:       fprintf(MyErrorOut,"readOneByte: select error in block mode. Retrying.\n");  }
                    101:   }
                    102: }
                    103:
                    104: int oxfdGetInt32(int fd)
                    105: {
                    106:   char d[4];
                    107:   int i;
                    108:   for (i=0; i<4; i++) {
                    109:     d[i] = readOneByte(fd);
                    110:   }
                    111:   return(ntohl(* ( (int *)d)));
                    112: }
                    113:
                    114: int oxGetInt32(ox_stream ostream)
                    115: {
                    116:   char d[4];
                    117:   int i;
                    118:   for (i=0; i<4; i++) {
                    119:     d[i] = fp2fgetc(ostream);
                    120:   }
                    121:   return(ntohl(* ( (int *)d)));
                    122: }
                    123: int oxGetCMOInt32(ox_stream ostream)
                    124: {
                    125:   int id;
                    126:   id = oxGetInt32(ostream);
                    127:   if (id != CMO_INT32) {
                    128:     fprintf(MyErrorOut,"It is not CMO_INT32.\n");
                    129:     return(0);
                    130:   }
                    131:   return(oxGetInt32(ostream));
                    132: }
                    133: char *oxGetCMOString(ox_stream ostream) {
                    134:   int id;
                    135:   int size;
                    136:   char *r;
                    137:   int i;
                    138:   id = oxGetInt32(ostream);
                    139:   if (id != CMO_STRING) {
                    140:     fprintf(MyErrorOut,"It is not CMO_STRING.\n");
                    141:     return(NULL);
                    142:   }
                    143:   size = oxGetInt32(ostream);
                    144:   if (size <0) {
                    145:     return(NULL);
                    146:   }else{
                    147:     r = (char *)mymalloc(size+1);
                    148:     for (i=0; i<size; i++) {
                    149:       r[i] = fp2fgetc(ostream);
                    150:     }
                    151:     r[size] = '\0';
                    152:     return(r);
                    153:   }
                    154: }
                    155:
                    156: void oxfdSendSyncBall(int fd)
                    157: {
                    158:   oxfdSendOXheader(fd,OX_SYNC_BALL,SerialOX++);
                    159: }
                    160:
                    161: void oxSendSyncBall(ox_stream os)
                    162: {
                    163:   oxSendOXheader(os,OX_SYNC_BALL,SerialOX++);
                    164: }
                    165:
                    166: int oxWaitSyncBall(ox_stream ostream)
                    167: {
                    168:   int sss;
                    169:   int mtag;
                    170:   while ((mtag = oxGetOXheader(ostream,&sss)) != OX_SYNC_BALL) {
                    171:     fprintf(stderr,"Looking for the next message tag. mtag=%d\n",mtag);
                    172:           /* or stdout */
                    173:     fflush(NULL);
                    174:   }
                    175: }
                    176:
                    177: int oxWaitSyncBall_org(ox_stream ostream)
                    178: {
                    179:   int mtag;
                    180:   char data[4];
                    181:   int c;
                    182:   data[0] = data[1] = data[2] = data[3] = 0xff;
                    183:   while (1) {
                    184:     /* This part should be revised so that this part understands
                    185:        the cmo format.
                    186:        */
                    187:
                    188:     if ((c = fp2fgetc(ostream)) < 0) {
                    189:       /* never use read directory. readOneByte() is buffers every thing. */
                    190:       fprintf(MyErrorOut,"End of file.\n");
                    191:       return(-1);
                    192:     }
                    193:     data[0] = data[1];
                    194:     data[1] = data[2];
                    195:     data[2] = data[3];
                    196:     data[3] = (char)c;
                    197:     mtag = ntohl(*((int *)(data)));
                    198:     if (mtag == OX_SYNC_BALL) {
                    199:       printf("Found the OX_SYNC_BALL. \n");
                    200:       fflush(NULL);
                    201:       return(mtag);
                    202:     }
                    203:     fprintf(stderr,"Looking for the next message tag.. %2x, mtag=%d\n",c,mtag);
                    204:           /* or stdout */
                    205:     fflush(NULL);
                    206:   }
                    207: }
                    208:
                    209:
                    210: void oxfdSendCmoNull(int fd)
                    211: {
                    212:   char data[4];
                    213:   *((int *)&data[0]) = htonl(CMO_NULL);
                    214:   write(fd,data,4);
                    215:   fflush((FILE *)NULL);
                    216: }
                    217: void oxSendCmoNull(ox_stream os)
                    218: {
                    219:   char data[4];
                    220:   *((int *)&data[0]) = htonl(CMO_NULL);
                    221:   fp2write(os,data,4);
                    222:   fp2fflush(os);
                    223: }
                    224:
                    225: void oxSendCmoError(ox_stream os)
                    226: {
                    227:   char data[4];
                    228:   *((int *)&data[0]) = htonl(CMO_ERROR);
                    229:   fp2write(os,data,4);
                    230:   fp2fflush(os);
                    231: }
                    232:
                    233: void oxSendCmoError2(ox_stream os,char *s)
                    234: {
                    235:   char data[4];
                    236:   *((int *)&data[0]) = htonl(CMO_ERROR2);
                    237:   fp2write(os,data,4);
                    238:   fp2fflush(os);
                    239:   oxSendCmoString(os,s);
                    240: }
                    241:
                    242: void oxfdSendInt32(int fd,int k)
                    243: {
                    244:   char data[4];
                    245:   *((int *)&data[0]) = htonl(k);
                    246:   write(fd,data,4);
                    247:   fflush((FILE *)NULL);
                    248: }
                    249: void oxSendInt32(ox_stream os,int k)
                    250: {
                    251:   char data[4];
                    252:   *((int *)&data[0]) = htonl(k);
                    253:   fp2write(os,data,4);
                    254:   fp2fflush(os);
                    255: }
                    256:
                    257: void oxfdSendCmoInt32(int fd,int k)
                    258: {
                    259:   char data[4*2];
                    260:   *((int *)&data[0]) = htonl(CMO_INT32);
                    261:   *((int *)&data[4]) = htonl(k);
                    262:   write(fd,data,4*2);
                    263:   fflush((FILE *)NULL);
                    264: }
                    265: void oxSendCmoInt32(ox_stream os,int k)
                    266: {
                    267:   char data[4*2];
                    268:   *((int *)&data[0]) = htonl(CMO_INT32);
                    269:   *((int *)&data[4]) = htonl(k);
                    270:   fp2write(os,data,4*2);
                    271:   fp2fflush(os);
                    272: }
                    273: void oxfdSendCmoString(int fd,char *s)
                    274: {
                    275:   char data[4*2];
                    276:   int n;
                    277:   if (s == NULL) n = 0;
                    278:   else {
                    279:     n = strlen(s);
                    280:   }
                    281:   *((int *)&data[0]) = htonl(CMO_STRING);
                    282:   *((int *)&data[4]) = htonl(n);
                    283:   write(fd,data,4*2);
                    284:   if (s != NULL) {
                    285:     write(fd,s,n);
                    286:   }
                    287:   fflush((FILE *)NULL);
                    288: }
                    289:
                    290: void oxSendCmoString(ox_stream os,char *s)
                    291: {
                    292:   char data[4*2];
                    293:   int n;
                    294:   if (s == NULL) n = 0;
                    295:   else {
                    296:     n = strlen(s);
                    297:   }
                    298:   *((int *)&data[0]) = htonl(CMO_STRING);
                    299:   *((int *)&data[4]) = htonl(n);
                    300:   fp2write(os,data,4*2);
                    301:   if (s != NULL) {
                    302:     fp2write(os,s,n);
                    303:   }
                    304:   fp2fflush(os);
                    305: }
                    306:
                    307:
                    308:
                    309: void oxReqPushString(ox_stream os, char *s)
                    310: {
                    311:   oxSendOXheader(os,OX_DATA,SerialOX++);
                    312:   oxSendCmoString(os,s);
                    313: }
                    314:
                    315: void oxSendResultOfControlInt32(int fd,int i)
                    316: {
                    317:   char data[4*2];
                    318:   oxfdSendOXheader(fd,OX_DATA,SerialOX++);
                    319:   *((int *)&data[0]) = htonl(CMO_INT32);
                    320:   *((int *)&data[4]) = htonl(i);
                    321:   write(fd,data,4*2);
                    322:   fflush((FILE *)NULL);
                    323: }
                    324:
                    325: void oxSendResultOfControl(int fd)
                    326: {
                    327:   char data[4*1];
                    328:   oxfdSendOXheader(fd,OX_DATA,SerialOX++);
                    329:   *((int *)&data[0]) = htonl(CMO_NULL);
                    330:   write(fd,data,4*1);
                    331:   fflush((FILE *)NULL);
                    332: }
                    333:
                    334:
                    335:
                    336: void oxSendMathCap(ox_stream os,struct mathCap *mathcap)
                    337: {
1.4     ! takayama  338:   int i,n,infosize,ncmo;
1.1       maekawa   339:   struct object mathinfo;
                    340:   /* printf("ox sending mathcap\n"); fflush(stdout); */
                    341:   mathinfo = *((struct object *)(mathcap->infop));
                    342:   infosize = getoaSize(mathinfo);
                    343:
                    344:   oxSendInt32(os,CMO_MATHCAP);
                    345:
                    346:   oxSendInt32(os,CMO_LIST);
                    347:   oxSendInt32(os,3);
                    348:
                    349:   /* [0] */
                    350:   oxSendInt32(os,CMO_LIST);
                    351:   oxSendInt32(os,infosize);
                    352:   oxSendCmoInt32(os,KopInteger(getoa(mathinfo,0)));
                    353:   for (i=1; i<infosize; i++) {
                    354:     oxSendCmoString(os,KopString(getoa(mathinfo,i)));
                    355:   }
                    356:
                    357:   /* [1]  */
                    358:   oxSendInt32(os,CMO_LIST);
                    359:   oxSendInt32(os,mathcap->smSize);
                    360:   n = mathcap->smSize;
                    361:   for (i=0; i<n; i++) {
                    362:     oxSendCmoInt32(os,(mathcap->sm)[i]);
                    363:   }
                    364:
                    365:   /* [2] */
                    366:   oxSendInt32(os,CMO_LIST);
1.4     ! takayama  367:   n = mathcap->oxSize;
        !           368:   oxSendInt32(os,n);
1.1       maekawa   369:
                    370:   oxSendInt32(os,CMO_LIST);
1.4     ! takayama  371:   oxSendInt32(os,2);
1.1       maekawa   372:   for (i=0; i<n; i++) {
1.4     ! takayama  373:     /* OX_DATA_xxx */
1.1       maekawa   374:     oxSendCmoInt32(os,(mathcap->ox)[i]);
1.4     ! takayama  375:     /* OX_DATA_xxx tags. In case of CMO, it is CMO tags. */
        !           376:     oxSendInt32(os,CMO_LIST);
        !           377:     oxSendInt32(os,mathcap->n);
        !           378:     ncmo = mathcap->n;
        !           379:     for (i=0; i<ncmo; i++) {
        !           380:       oxSendCmoInt32(os,(mathcap->cmo)[i]);
        !           381:       /* printf("i=%d %d, ",i,(mathcap->cmo)[i]); */
        !           382:     }
1.1       maekawa   383:   }
                    384:   /* printf("\n"); fflush(stdout); */
                    385: }
                    386:
                    387: void oxReqMathCap(ox_stream os) {
                    388:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    389:   oxSendInt32(os,SM_mathcap);
                    390:   fp2fflush(os);
                    391: }
                    392:
                    393: void oxReqSetMathCap(ox_stream os,struct mathCap *mathcap) {
                    394:   oxSendOXheader(os,OX_DATA,SerialOX++);
                    395:   oxSendMathCap(os,mathcap);
                    396:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    397:   oxSendInt32(os,SM_setMathCap);
                    398:   fp2fflush(os);
                    399: }
                    400: void oxReqPops(ox_stream os,int n) {
                    401:   oxSendOXheader(os,OX_DATA,SerialOX++);
                    402:   oxSendCmoInt32(os,n);
                    403:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    404:   oxSendInt32(os,SM_pops);
                    405:   fp2fflush(os);
                    406: }
                    407: void oxReqSetName(ox_stream os,char *s) {
                    408:   oxSendOXheader(os,OX_DATA,SerialOX++);
                    409:   oxSendCmoString(os,s);
                    410:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    411:   oxSendInt32(os,SM_setName);
                    412:   fp2fflush(os);
                    413: }
                    414: void oxReqEvalName(ox_stream os,char *s) {
                    415:   oxSendOXheader(os,OX_DATA,SerialOX++);
                    416:   oxSendCmoString(os,s);
                    417:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    418:   oxSendInt32(os,SM_evalName);
                    419:   fp2fflush(os);
                    420: }
                    421:
                    422: void oxReqExecuteStringByLocalParser(ox_stream os,char *s)
                    423: {
                    424:   oxSendOXheader(os,OX_DATA,SerialOX++);
                    425:   oxSendCmoString(os,s);
                    426:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    427:   oxSendInt32(os,SM_executeStringByLocalParser);
                    428:   fp2fflush(os);
                    429: }
                    430:
                    431: void oxReqExecuteFunction(ox_stream os,char *s)
                    432: {
                    433:   oxSendOXheader(os,OX_DATA,SerialOX++);
                    434:   oxSendCmoString(os,s);
                    435:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    436:   oxSendInt32(os,SM_executeFunction);
                    437:   fp2fflush(os);
                    438: }
                    439:
                    440:
                    441: void oxReqPopString(ox_stream os)
                    442: {
                    443:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    444:   oxSendInt32(os,SM_popString);
                    445:   fp2fflush(os);
                    446: }
                    447:
                    448: void oxReqSingleOperand(ox_stream os,int smtag)
                    449: {
                    450:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    451:   oxSendInt32(os,smtag);
                    452:   fp2fflush(os);
                    453: }
                    454:
                    455:
                    456: void oxReqControlResetConnection(int fd) {
                    457:   oxfdSendOXheader(fd,OX_COMMAND,SerialOX++);
                    458:   oxfdSendInt32(fd,SM_control_reset_connection);
                    459:   fflush(NULL);
                    460: }
                    461:
                    462: void oxReqControlKill(int fd) {
                    463:   oxfdSendOXheader(fd,OX_COMMAND,SerialOX++);
                    464:   oxfdSendInt32(fd,SM_control_kill);
                    465:   fflush(NULL);
                    466: }
                    467:
                    468: void oxReqPopCMO(ox_stream os) {
                    469:   oxSendOXheader(os,OX_COMMAND,SerialOX++);
                    470:   oxSendInt32(os,SM_popCMO);
                    471:   fp2fflush(os);
                    472: }
                    473:
                    474:
                    475: int oxGetResultOfControlInt32(int fd) {
                    476:   int k; int sss;
                    477:   k = oxfdGetOXheader(fd,&sss);
                    478:   if (k != OX_DATA) {
                    479:     fprintf(MyErrorOut,"oxGetResultOfControlInt32: wrong header.");
                    480:     return(-1);
                    481:   }
                    482:   k = oxfdGetInt32(fd); /* CMO_INT32 */
                    483:   k = oxfdGetInt32(fd);
                    484:   return(k);
                    485: }
                    486:
                    487: int oxclientMultiSelect(oxclientp clients[],int dataready[],
                    488:                        int controlready[], int size, int t)
                    489: {
                    490:   int i, ddd;
                    491:   int fd;
                    492:   int humanfd = 0;
                    493:   fd_set readfds;
                    494:   struct timeval timeout;
                    495:   extern int errno;
                    496:
                    497:   /** printf("(1)"); fflush(NULL); */
                    498:   FD_ZERO(&readfds);
                    499:   timeout.tv_sec = 0;
                    500:   timeout.tv_usec = (long) t;
                    501:
                    502:   ddd = 0;  fd = 0;
                    503:   for (i=0; i<size; i++) {
                    504:     dataready[i] = controlready[i] = 0;
                    505:   }
                    506:   for (i=0; i<size; i++) {
                    507:     if (clients[i]->humanio) {
                    508:       fd = (fd<humanfd?humanfd:fd);
                    509:       FD_SET(humanfd,&readfds);
                    510:       if (oxSocketSelect0(humanfd,0)) {
                    511:        ddd = dataready[i] = 1; controlready[i] = 0;
                    512:       }else{
                    513:        dataready[i] = 0; controlready[i] = 0;
                    514:       }
                    515:     }else{
                    516:       fd = (fd<clients[i]->controlfd?clients[i]->controlfd:fd);
                    517:       FD_SET(clients[i]->controlfd,&readfds);
                    518:       if (oxSocketSelect0(clients[i]->controlfd,0)) {
                    519:        ddd = controlready[i] = 1;
                    520:       }else{
                    521:        controlready[i] = 0;
                    522:       }
                    523:       if (clients[i]->datafp2 != NULL) {
                    524:        fd = (fd<clients[i]->datafp2->fd?clients[i]->datafp2->fd:fd);
                    525:        FD_SET(clients[i]->datafp2->fd,&readfds);
                    526:        if (fp2select(clients[i]->datafp2,0)) {
                    527:          ddd = dataready[i] = 1;
                    528:        }else{
                    529:          dataready[i] = 0;
                    530:        }
                    531:       }else{
                    532:        dataready[i] = 0;
                    533:       }
                    534:     }
                    535:   }
                    536:   if (t > 0 ) {
                    537:     if (ddd) return(1);
                    538:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    539:       fprintf(MyErrorOut,"error");
                    540:       return(-1);
                    541:     }
                    542:     return(oxclientMultiSelect(clients, dataready, controlready,size,0));
                    543:   }else if (t == 0) {
                    544:     return(ddd);
                    545:   }else {
                    546:     /** printf("(2)"); fflush(NULL); */
                    547:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    548:       fprintf(MyErrorOut,"error");
                    549:       return(-1);
                    550:     }
                    551:     /** printf("(3)"); fflush(NULL); */
                    552:     return(oxclientMultiSelect(clients, dataready, controlready,size,0));
                    553:   }
                    554: }
                    555:
                    556: int oxGetControl(oxclientp client)
                    557: /* synchronized. */
                    558: {
                    559:   int ans;
                    560:   ox_stream os;
                    561:   switch (client->cstate) {
                    562:   case 1:
                    563:     ans = oxGetResultOfControlInt32(client->controlfd);
                    564:     client->cstate = 0;
                    565:     return(ans);
                    566:   default:
                    567:     fprintf(MyErrorOut,"oxGet: unknown cstate.\n");
                    568:     client->cstate = -1;
                    569:     return(-1);
                    570:   }
                    571:
                    572:   return(-1);
                    573: }
                    574:
                    575: int oxInitClient(oxclientp client)
                    576: {
                    577:   client->datafp2 = NULL;
                    578:   client->dataport = 0;
                    579:   client->controlport = 0;
                    580:   client->controlfd = 0;
                    581:   client->humanio = 0;
                    582:   client->dstate = 0;
                    583:   client->cstate = 0;
                    584:   client->id = -1;
                    585:   client->mathcapObjp = NULL;
                    586:   client->controlByteOrder = OX_BYTE_NETWORK_BYTE_ORDER;
                    587:   client->engineByteOrder = OX_BYTE_NETWORK_BYTE_ORDER;
                    588:   return(0);
                    589: }
                    590:
                    591: int oxIsThereErrorClient(oxclientp client) {
                    592:   if (client == NULL) return(1);
                    593:   if (client->dstate == -1) return(1);
                    594:   if (client->cstate == -1) return(1);
                    595:   return(0);
                    596: }
                    597:
                    598: oxclientp oxCreateClient(char *sname,int portStream,int portControl)
                    599:   /* you also need to change oxCreateClient2. */
                    600: {
                    601:   static int clnum = 0;
                    602:   int v = 0;
                    603:   int fdControl = -1;
                    604:   int fdStream = -1;
                    605:   oxclientp client;
                    606:   int controlByteOrder, engineByteOrder;
                    607:   v = !Quiet;
                    608:   if (portControl != -1) {
                    609:     fdControl = socketConnect(sname,portControl);
                    610:     if (v) fprintf(stderr,"\nControl port %d : Connected.\n",portControl);
                    611:   }
                    612:   if (portStream != -1) {
                    613:     sleep(1); /* wait */
                    614:     fdStream = socketConnect(sname,portStream);
                    615:     if (v) fprintf(stderr,"\nStream port %d : Connected.\n",portStream);
                    616:   }
                    617:
                    618:   if (fdStream == -1 || fdControl == -1) {
                    619:     fprintf(stderr,"\nOpen error in oxCreateClient.\n");
                    620:     return(NULL);
                    621:   }
                    622:
                    623:   controlByteOrder = oxSetByteOrder(fdControl);
                    624:   if (v) fprintf(stderr,"Byte order for control process is %s.\n",
                    625:                 (controlByteOrder == 0? "network byte order":
                    626:                  (controlByteOrder == 1? "little indican":
                    627:                   "big indian")));
                    628:   engineByteOrder = oxSetByteOrder(fdStream);
                    629:   if (v) fprintf(stderr,"Byte order for engine process is %s.\n",
                    630:                 (engineByteOrder == 0? "network byte order":
                    631:                  (engineByteOrder == 1? "little indican":
                    632:                   "big indian")));
                    633:
                    634:   client = (oxclientp) mymalloc(sizeof(oxclient));
                    635:   oxInitClient(client);
                    636:   client->datafp2 = fp2open(fdStream);
                    637:   if (client->datafp2 == NULL) {
                    638:     fprintf(stderr,"oxCreateClient(): fp2open(fd) failed.\n");
                    639:     return(NULL);
                    640:   }
                    641:   client->dataport = portStream;
                    642:   client->controlport = portControl;
                    643:   client->controlfd = fdControl;
                    644:   client->id = clnum; clnum++;
                    645:   client->type = CLIENT_SOCKET; /* socket */
                    646:   client->engineByteOrder = engineByteOrder;
                    647:   client->controlByteOrder = controlByteOrder;
                    648:   return(client);
                    649: }
                    650:
                    651: oxclientp oxCreateClientFile(char *fname,char *mode,char *controlName,char *cmode)
                    652: {
                    653:   static int clnum = 0x8000;
                    654:   int v = 0;
                    655:   int fdControl = -1;
                    656:   int fdStream = -1;
                    657:   oxclientp client;
                    658:   v = 1;
                    659:   if (strcmp(mode,"w") == 0) {
                    660:     fdStream = creat(fname,S_IWRITE|S_IREAD|S_IRGRP|S_IROTH);
                    661:     if (fdStream < 0) {
                    662:       fprintf(stderr,"\nCreat failed for %s\n",fname); return(NULL);
                    663:     }
                    664:   }else if (strcmp(mode,"r")==0) {
                    665:     fdStream = open(fname,O_RDONLY);
                    666:     if (fdStream < 0) {
                    667:       fprintf(stderr,"\nOpen failed for %s\n",fname); return(NULL);
                    668:     }
                    669:   }else {
                    670:     fprintf(stderr,"\nThe mode %s is not supported.\n",mode); return(NULL);
                    671:   }
                    672:
                    673:   if (strcmp(cmode,"w") == 0) {
                    674:     fdControl = creat(controlName,S_IWRITE|S_IREAD|S_IRGRP|S_IROTH);
                    675:     if (fdControl < 0) {
                    676:       fprintf(stderr,"\nCreat failed for %s\n",controlName); return(NULL);
                    677:     }
                    678:   }else if (strcmp(cmode,"r")==0) {
                    679:     fdControl = open(controlName,O_RDONLY);
                    680:     if (fdControl < 0) {
                    681:       fprintf(stderr,"\nOpen failed for %s\n",controlName); return(NULL);
                    682:     }
                    683:   }else if (strcmp(cmode,"rw")==0) {
                    684:     fdControl = open(controlName,O_RDWR);
                    685:     if (fdControl < 0) {
                    686:       fprintf(stderr,"\nOpen failed for %s\n",controlName); return(NULL);
                    687:     }
                    688:   }else {
                    689:     fprintf(stderr,"\nThe mode %s is not supported.\n",cmode); return(NULL);
                    690:   }
                    691:
                    692:
                    693:   client = (oxclientp) mymalloc(sizeof(oxclient));
                    694:   oxInitClient(client);
                    695:   client->datafp2 = fp2open(fdStream);
                    696:   if (client->datafp2 == NULL) {
                    697:     fprintf(stderr,"oxCreateClientFile(): fp2open(fd) failed.\n");
                    698:     return(NULL);
                    699:   }
                    700:   client->dataport = 0;
                    701:   client->controlport = 0;
                    702:   client->controlfd = fdControl;
                    703:   client->id = clnum; clnum++;
                    704:   client->type = CLIENT_FILE;
                    705:   client->engineByteOrder = OX_BYTE_NETWORK_BYTE_ORDER;
                    706:   client->controlByteOrder = OX_BYTE_NETWORK_BYTE_ORDER;
                    707:   return(client);
                    708: }
                    709:
                    710: void oxSendOXheader_generic(int type,int fd,ox_stream ox,
                    711:                            int k,int serial)
                    712: {
                    713:   static int ss = 0;
                    714:   extern int UseOXPacketSerial;
                    715:   if (serial >= 0) ss = serial;
                    716:   else ss++;
                    717:   if (ss < 0) ss=0;
                    718:   if (type == 0) { /* fd */
                    719:     oxfdSendInt32(fd,k);
                    720:     if (UseOXPacketSerial) oxfdSendInt32(fd,ss);
                    721:   }else {
                    722:     oxSendInt32(ox,k);
                    723:     if (UseOXPacketSerial) oxSendInt32(ox,ss);
                    724:   }
                    725: }
                    726: void oxSendOXheader(ox_stream ostream,int k,int serial) {
                    727:   oxSendOXheader_generic(1,-1,ostream,k,serial);
                    728: }
                    729: void oxfdSendOXheader(int fd,int k,int serial) {
                    730:   oxSendOXheader_generic(0,fd,(ox_stream) NULL,k,serial);
                    731: }
                    732:
                    733: int oxfdGetOXheader(int fd,int *sss)
                    734: {
                    735:   char d[4];
                    736:   int i;
                    737:   int m;
                    738:
                    739:   for (i=0; i<4; i++) {
                    740:     d[i] = readOneByte(fd);
                    741:   }
                    742:   m = ntohl(* ( (int *)d));
                    743:   *sss = -1;
                    744:   if (UseOXPacketSerial) {
                    745:     for (i=0; i<4; i++) {
                    746:       d[i] = readOneByte(fd);
                    747:     }
                    748:     *sss = ntohl(* ( (int *)d));
                    749:   }
                    750:   return(m);
                    751: }
                    752:
                    753: int oxGetOXheader(ox_stream ostream,int *sss)
                    754: {
                    755:   char d[4];
                    756:   int m;
                    757:   int i;
                    758:
                    759:   unlockCtrlCForOx();  /* temporary unlock */
                    760:   while (fp2select(ostream,1000) == 0) ;
                    761:   restoreLockCtrlCForOx();
                    762:
                    763:   for (i=0; i<4; i++) {
                    764:     d[i] = fp2fgetc(ostream);
                    765:   }
                    766:   m = ntohl(* ( (int *)d));
                    767:   *sss = -1;
                    768:   if (UseOXPacketSerial) {
                    769:     for (i=0; i<4; i++) {
                    770:       d[i] = fp2fgetc(ostream);
                    771:     }
                    772:     *sss = ntohl(* ( (int *)d));
                    773:   }
                    774:   return(m);
                    775: }
                    776:
                    777:
                    778: oxWritePortFile(int func,int port,char *fname) {
                    779:   char name[1024];
                    780:   FILE *fp;
                    781:   strcpy(name,fname);
                    782:   if (func == 0) {
                    783:     strcat(name,".control");
                    784:     fp = fopen(name,"w");
                    785:     fprintf(fp,"%05d\n",port);
                    786:     fclose(fp);
                    787:   }else {
                    788:     strcat(name,".data");
                    789:     fp = fopen(name,"w");
                    790:     fprintf(fp,"%05d\n",port);
                    791:     fclose(fp);
                    792:   }
                    793: }
                    794: oxReadPortFile(int func,char *fname) {
                    795:   int port = 0;
                    796:   char name[1024];
                    797:   FILE *fp;
                    798:   strcpy(name,fname);
                    799:   if (func == 0) {
                    800:     strcat(name,".control");
                    801:     fp = fopen(name,"r");
                    802:     fscanf(fp,"%d",&port);
                    803:     fclose(fp);
                    804:   }else {
                    805:     strcat(name,".data");
                    806:     fp = fopen(name,"r");
                    807:     fscanf(fp,"%d",&port);
                    808:     fclose(fp);
                    809:   }
                    810:   return(port);
                    811: }
                    812: char *oxGenPortFile(void) {
                    813:   char *fname;
                    814:   time_t tt;
                    815:   char sstime[512];
                    816:
                    817:   fname = (char *)malloc(1024*sizeof(char));
                    818:   strcpy(fname,getenv("HOME"));
                    819:   strcat(fname,"/.ox.");
                    820:   tt = time(NULL);
                    821:   sprintf(sstime,"%ld",(long) tt);
                    822:   strcat(fname,sstime);
                    823:   if (fname[strlen(fname)-1] == '\n') {
                    824:     fname[strlen(fname)-1] = '\0';
                    825:   }
                    826:   /* fprintf(stderr,"OxPortFileName=%s\n",fname); */
                    827:   OxPortFileName = fname;
                    828:   return(fname);
                    829: }
                    830: int oxRemovePortFile(void) {
                    831:   char fname[1024];
                    832:   FILE *fp;
                    833:   strcpy(fname,OxPortFileName);
                    834:   strcat(fname,".control");
                    835:   if ((fp=fopen(fname,"r")) == NULL) {
                    836:   }{
                    837:     fclose(fp);
                    838:     if (unlink(fname)) {
                    839:       fprintf(MyErrorOut,"fail unlink.\n");
                    840:     }
                    841:
                    842:   }
                    843:   strcpy(fname,OxPortFileName);
                    844:   strcat(fname,".data");
                    845:   if ((fp=fopen(fname,"r")) == NULL) {
                    846:   }{
                    847:     fclose(fp);
                    848:     if (unlink(fname)) {
                    849:       fprintf(MyErrorOut,"fail unlink.\n");
                    850:     }
                    851:   }
                    852: }
                    853:
                    854: char *oxGenPass(void) {
1.3       takayama  855:   static int seed = 0;
1.1       maekawa   856:   long p;
                    857:   char *s;
1.3       takayama  858:   int i;
                    859:   if (seed == 0) {
                    860:     seed = (int) time(NULL) + (int) &p;
                    861:     srandom((unsigned int) seed);
                    862:   }
                    863:   for (i=0; i < ((int) &p) % 100 ; i++) random();
1.1       maekawa   864:   p = random();
                    865:   s = (char *)malloc(128*sizeof(char));
                    866:   sprintf(s,"%ld",p);
                    867:   return(s);
                    868: }
                    869:
                    870:
                    871: static void cancelConnection() {
                    872:   extern jmp_buf MyEnv_oxmisc;
                    873:   signal(SIGALRM,SIG_IGN);
                    874:   fprintf(stderr,"Time out in TCP/IP connection.\n");
                    875:   longjmp(MyEnv_oxmisc,1);
                    876: }
                    877:
                    878: oxclientp oxCreateClient2(int fdstream,int portStream,
                    879:                          int fdcontrol,int portControl,int ipmask,char *pass)
                    880: {
                    881:   static int clnum = 0;
                    882:   int v = 0;
                    883:   int fdControl = -1;
                    884:   int fdStream = -1;
                    885:   int m;
                    886:
                    887:   char *s;
                    888:   oxclientp client;
                    889:   extern jmp_buf MyEnv_oxmisc ;
                    890:   int controlByteOrder, engineByteOrder;
                    891:
                    892:   v = !Quiet;
                    893:   if (setjmp(MyEnv_oxmisc)) {
                    894:     return(NULL);
                    895:   }else{
                    896:   }
                    897:   alarm((unsigned int) 10);  /* setup timeout. */
                    898:   signal(SIGALRM,cancelConnection);
                    899:
                    900:   switch(ipmask) {
                    901:   case 0:/* only local */
                    902:     fdControl = socketAcceptLocal(fdcontrol);
                    903:     fdStream  = socketAcceptLocal(fdstream);
                    904:     break;
                    905:   default:/* any */
                    906:     fdControl = socketAccept(fdcontrol);
                    907:     fdStream  = socketAccept(fdstream);
                    908:     break;
                    909:   }
                    910:   if (v) fprintf(stderr,"\nControl port %d : Connected.\n",portControl);
                    911:   if (v) fprintf(stderr,"\nStream port %d : Connected.\n",portStream);
                    912:
                    913:   if (fdStream == -1 || fdControl == -1) {
                    914:     fprintf(stderr,"\nOpen error in oxCreateClient.\n");
                    915:     return(NULL);
                    916:   }
                    917:
                    918:   /* Authentification by password. */
                    919:   m = strlen(pass);
                    920:   s = (char *)mymalloc(sizeof(char)*(m+1));
1.2       takayama  921:   read(fdControl,s,m+1); s[m] = '\0';
1.1       maekawa   922:   if (strcmp(s,pass) != 0) {
                    923:     fprintf(stderr,"oxCreateClient2(): password authentification failed for control channel.\n");
                    924:     close(fdControl);
                    925:     return(NULL);
                    926:   }
1.2       takayama  927:   read(fdStream,s,m+1); s[m] = '\0';
1.1       maekawa   928:   if (strcmp(s,pass) != 0) {
                    929:     fprintf(stderr,"oxCreateClient2(): password authentification failed for data channel.\n");
                    930:     close(fdStream);
                    931:     return(NULL);
                    932:   }
                    933:   signal(SIGALRM,SIG_IGN);
                    934:
                    935:   controlByteOrder = oxSetByteOrder(fdControl);
                    936:   if (v) fprintf(stderr,"Byte order for control process is %s.\n",
                    937:                 (controlByteOrder == 0? "network byte order":
                    938:                  (controlByteOrder == 1? "little indican":
                    939:                   "big indian")));
                    940:   engineByteOrder = oxSetByteOrder(fdStream);
                    941:   if (v) fprintf(stderr,"Byte order for engine process is %s.\n",
                    942:                 (engineByteOrder == 0? "network byte order":
                    943:                  (engineByteOrder == 1? "little indican":
                    944:                   "big indian")));
                    945:
                    946:
                    947:   client = (oxclientp) mymalloc(sizeof(oxclient));
                    948:   oxInitClient(client);
                    949:   client->datafp2 = fp2open(fdStream);
                    950:   if (client->datafp2 == NULL) {
                    951:     fprintf(stderr,"oxCreateClient2(): fp2open(fd) failed.\n");
                    952:     return(NULL);
                    953:   }
                    954:   client->dataport = portStream;
                    955:   client->controlport = portControl;
                    956:   client->controlfd = fdControl;
                    957:   client->id = clnum; clnum++;
                    958:   client->type = CLIENT_SOCKET; /* socket */
                    959:   client->engineByteOrder = engineByteOrder;
                    960:   client->controlByteOrder = controlByteOrder;
                    961:   return(client);
                    962: }
                    963:
                    964: int oxSetByteOrder(int fd) {
                    965:   char data[1];
                    966:   int peertype;
                    967:   /* It is for client. read and next write. */
                    968:   /* oxSocketSelect0(fd,10);  wait. */
                    969:   read(fd,data,1);
                    970:   peertype = (unsigned char) data[0];
                    971:
                    972:   /* We support only Network byte order */
                    973:   data[0] = OX_BYTE_NETWORK_BYTE_ORDER;
                    974:   write(fd,data,1);
                    975:
                    976:   return(OX_BYTE_NETWORK_BYTE_ORDER);
                    977: }
                    978:
                    979: int oxTellMyByteOrder(int fd) {
                    980:   char data[1];
                    981:   int peertype;
                    982:   /* It is for server. read and next write. */
                    983:
                    984:   /* We support only Network byte order */
                    985:   data[0] = OX_BYTE_NETWORK_BYTE_ORDER;
                    986:   write(fd,data,1);
                    987:   fsync(fd);  /* returns 0 if normal. Does it work for socket? */
                    988:
                    989:   read(fd,data,1);
                    990:
                    991:   return(OX_BYTE_NETWORK_BYTE_ORDER);
                    992: }
                    993:
                    994:
                    995:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>