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