[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.1.1.1

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

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