Annotation of OpenXM/src/kan96xx/plugin/Old/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: FILE *MyErrorOut = stdout;
10: #include "ox_kan.h"
11:
12: #define READBUFSIZE 5000
13:
14: int UseOXPacketSerial = 0;
15:
16:
17: int readOneByte(int fd) /* blocking */
18: {
19: static char data[1];
20: int size;
21: int ans;
22: int watch = 1;
23:
24: if (fd < 0) {
25: fprintf(MyErrorOut,"readOneByte fd < 0 ??? .\n");
26: return(-1);
27: }
28:
29: if (oxSocketSelect0(fd,-1)) { /* block */
30: size = read(fd,data,1);
31: if (size == 0) {
32: fprintf(MyErrorOut,"oxSocketSelect0() returns 1, but there is no data. You peer may be killed.\n");
33: return(-1);
34: }
35: return(data[0]);
36: }else{
37: fprintf(MyErrorOut,"readOneByte: select error in block mode. Retrying.\n");
38: return(-1);
39: }
40: }
41:
42: int readOneByte_org(int fd) /* blocking */
43: {
44: static char data[READBUFSIZE];
45: static int thisFd = -1;
46: static int from = 0;
47: static int to = 0;
48: int size;
49: int ans;
50: int watch = 1;
51:
52: if ((thisFd == -1) && (fd >= 0)) {thisFd = fd;}
53: if (fd < 0) {
54: fprintf(MyErrorOut,"readOneByte fd < 0 ??? .\n");
55: return(-1);
56: }
57:
58: if (fd != thisFd) {
59: fprintf(MyErrorOut,"readOneByte can be used only for one fd.\n");
60: fflush(NULL);
61: return(-1);
62: }
63: if (to > from) {
64: ans = data[from];
65: from++;
66: if (watch) {
67: printf("%2x ",ans);
68: fflush(stdout);
69: }
70: return(ans);
71: }
72:
73: while (1) {
74: if (oxSocketSelect0(fd,-1)) { /* block */
75: size = read(fd,data,READBUFSIZE-1);
76: if (size == 0) {
77: fprintf(MyErrorOut,"oxSocketSelect0() returns 1, but there is no data. You peer may be killed.\n");
78: return(-1);
79: }
80: from = 0;
81: to = size;
82: return(readOneByte(fd));
83: }else{
84: fprintf(MyErrorOut,"readOneByte: select error in block mode. Retrying.\n"); }
85: }
86: }
87:
88: int oxfdGetInt32(int fd)
89: {
90: char d[4];
91: int i;
92: for (i=0; i<4; i++) {
93: d[i] = readOneByte(fd);
94: }
95: return(ntohl(* ( (int *)d)));
96: }
97:
98: int oxGetInt32(ox_stream ostream)
99: {
100: char d[4];
101: int i;
102: for (i=0; i<4; i++) {
103: d[i] = fp2fgetc(ostream);
104: }
105: return(ntohl(* ( (int *)d)));
106: }
107: int oxGetCMOInt32(ox_stream ostream)
108: {
109: int id;
110: id = oxGetInt32(ostream);
111: if (id != CMO_INT32) {
112: fprintf(MyErrorOut,"It is not CMO_INT32.\n");
113: return(0);
114: }
115: return(oxGetInt32(ostream));
116: }
117: char *oxGetCMOString(ox_stream ostream) {
118: int id;
119: int size;
120: char *r;
121: int i;
122: id = oxGetInt32(ostream);
123: if (id != CMO_STRING) {
124: fprintf(MyErrorOut,"It is not CMO_STRING.\n");
125: return(NULL);
126: }
127: size = oxGetInt32(ostream);
128: if (size <0) {
129: return(NULL);
130: }else{
131: r = (char *)mymalloc(size+1);
132: for (i=0; i<size; i++) {
133: r[i] = fp2fgetc(ostream);
134: }
135: r[size] = '\0';
136: return(r);
137: }
138: }
139:
140: void oxfdSendSyncBall(int fd)
141: {
142: oxfdSendOXheader(fd,OX_SYNC_BALL,SerialOX++);
143: /*
144: char data[4*2];
145: *((int *)&data[0]) = htonl(OX_SYNC_BALL);
146: write(fd,data,4);
147: fflush((FILE *)NULL);
148: */
149: }
150:
151: void oxSendSyncBall(ox_stream os)
152: {
153: char data[4*2];
154: *((int *)&data[0]) = htonl(OX_SYNC_BALL);
155: fp2write(os,data,4);
156: fp2fflush(os);
157: }
158:
159: int oxWaitSyncBall(ox_stream ostream)
160: {
161: int mtag;
162: char data[4];
163: int c;
164: data[0] = data[1] = data[2] = data[3] = 0xff;
165: while (1) {
166: /* This part should be revised so that this part understands
167: the cmo format.
168: */
169:
170: if ((c = fp2fgetc(ostream)) < 0) {
171: /* never use read directory. readOneByte() is buffers every thing. */
172: fprintf(MyErrorOut,"End of file.\n");
173: return(-1);
174: }
175: data[0] = data[1];
176: data[1] = data[2];
177: data[2] = data[3];
178: data[3] = (char)c;
179: mtag = ntohl(*((int *)(data)));
180: if (mtag == OX_SYNC_BALL) {
181: printf("Found the OX_SYNC_BALL. \n");
182: fflush(NULL);
183: return(mtag);
184: }
185: fprintf(stderr,"Looking for the next message tag.. %2x, mtag=%d\n",c,mtag);
186: /* or stdout */
187: fflush(NULL);
188: }
189: }
190:
191:
192: void oxfdSendCmoNull(int fd)
193: {
194: char data[4];
195: *((int *)&data[0]) = htonl(CMO_NULL);
196: write(fd,data,4);
197: fflush((FILE *)NULL);
198: }
199: void oxSendCmoNull(ox_stream os)
200: {
201: char data[4];
202: *((int *)&data[0]) = htonl(CMO_NULL);
203: fp2write(os,data,4);
204: fp2fflush(os);
205: }
206:
207: void oxSendCmoError(ox_stream os)
208: {
209: char data[4];
210: *((int *)&data[0]) = htonl(CMO_ERROR);
211: fp2write(os,data,4);
212: fp2fflush(os);
213: }
214:
215: void oxSendCmoError2(ox_stream os,char *s)
216: {
217: char data[4];
218: *((int *)&data[0]) = htonl(CMO_ERROR2);
219: fp2write(os,data,4);
220: fp2fflush(os);
221: oxSendCmoString(os,s);
222: }
223:
224: void oxfdSendInt32(int fd,int k)
225: {
226: char data[4];
227: *((int *)&data[0]) = htonl(k);
228: write(fd,data,4);
229: fflush((FILE *)NULL);
230: }
231: void oxSendInt32(ox_stream os,int k)
232: {
233: char data[4];
234: *((int *)&data[0]) = htonl(k);
235: fp2write(os,data,4);
236: fp2fflush(os);
237: }
238:
239: void oxfdSendCmoInt32(int fd,int k)
240: {
241: char data[4*2];
242: *((int *)&data[0]) = htonl(CMO_INT32);
243: *((int *)&data[4]) = htonl(k);
244: write(fd,data,4*2);
245: fflush((FILE *)NULL);
246: }
247: void oxSendCmoInt32(ox_stream os,int k)
248: {
249: char data[4*2];
250: *((int *)&data[0]) = htonl(CMO_INT32);
251: *((int *)&data[4]) = htonl(k);
252: fp2write(os,data,4*2);
253: fp2fflush(os);
254: }
255: void oxfdSendCmoString(int fd,char *s)
256: {
257: char data[4*2];
258: int n;
259: if (s == NULL) n = 0;
260: else {
261: n = strlen(s);
262: }
263: *((int *)&data[0]) = htonl(CMO_STRING);
264: *((int *)&data[4]) = htonl(n);
265: write(fd,data,4*2);
266: if (s != NULL) {
267: write(fd,s,n);
268: }
269: fflush((FILE *)NULL);
270: }
271:
272: void oxSendCmoString(ox_stream os,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: fp2write(os,data,4*2);
283: if (s != NULL) {
284: fp2write(os,s,n);
285: }
286: fp2fflush(os);
287: }
288:
289: void oxReqPushString(ox_stream os, char *s)
290: {
291: oxSendInt32(os,OX_DATA);
292: oxSendCmoString(os,s);
293: }
294:
295: void oxSendResultOfControlInt32(int fd,int i)
296: {
297: char data[4*3];
298: *((int *)&data[0]) = htonl(OX_DATA);
299: *((int *)&data[4]) = htonl(CMO_INT32);
300: *((int *)&data[8]) = htonl(i);
301: write(fd,data,4*3);
302: fflush((FILE *)NULL);
303: }
304:
305: void oxSendResultOfControl(int fd)
306: {
307: char data[4*2];
308: *((int *)&data[0]) = htonl(OX_DATA);
309: *((int *)&data[4]) = htonl(CMO_NULL);
310: write(fd,data,4*2);
311: fflush((FILE *)NULL);
312: }
313:
314: void oxReqMathCap(ox_stream os) {
315: oxSendInt32(os,OX_COMMAND);
316: oxSendInt32(os,SM_mathcap);
317: fp2fflush(os);
318: }
319: void oxReqPops(ox_stream os,int n) {
320: oxSendInt32(os,OX_DATA);
321: oxSendCmoInt32(os,n);
322: oxSendInt32(os,OX_COMMAND);
323: oxSendInt32(os,SM_pops);
324: fp2fflush(os);
325: }
326: void oxReqSetName(ox_stream os,char *s) {
327: oxSendInt32(os,OX_DATA);
328: oxSendCmoString(os,s);
329: oxSendInt32(os,OX_COMMAND);
330: oxSendInt32(os,SM_setName);
331: fp2fflush(os);
332: }
333: void oxReqEvalName(ox_stream os,char *s) {
334: oxSendInt32(os,OX_DATA);
335: oxSendCmoString(os,s);
336: oxSendInt32(os,OX_COMMAND);
337: oxSendInt32(os,SM_evalName);
338: fp2fflush(os);
339: }
340:
341: void oxReqExecuteStringByLocalParser(ox_stream os,char *s)
342: {
343: oxSendInt32(os,OX_DATA);
344: oxSendCmoString(os,s);
345: oxSendInt32(os,OX_COMMAND);
346: oxSendInt32(os,SM_executeStringByLocalParser);
347: fp2fflush(os);
348: }
349:
350: void oxReqExecuteFunction(ox_stream os,char *s)
351: {
352: oxSendInt32(os,OX_DATA);
353: oxSendCmoString(os,s);
354: oxSendInt32(os,OX_COMMAND);
355: oxSendInt32(os,SM_executeFunction);
356: fp2fflush(os);
357: }
358:
359:
360: void oxReqPopString(ox_stream os)
361: {
362: oxSendInt32(os,OX_COMMAND);
363: oxSendInt32(os,SM_popString);
364: fp2fflush(os);
365: }
366:
367:
368: void oxReqControlResetConnection(int fd) {
369: oxfdSendInt32(fd,OX_COMMAND);
370: oxfdSendInt32(fd,SM_control_reset_connection);
371: fflush(NULL);
372: }
373:
374: void oxReqControlKill(int fd) {
375: oxfdSendInt32(fd,OX_COMMAND);
376: oxfdSendInt32(fd,SM_control_kill);
377: fflush(NULL);
378: }
379:
380: void oxReqPopCMO(ox_stream os) {
381: oxSendInt32(os,OX_COMMAND);
382: oxSendInt32(os,SM_popCMO);
383: fp2fflush(os);
384: }
385:
386:
387: int oxGetResultOfControlInt32(int fd) {
388: int k;
389: k = oxfdGetInt32(fd);
390: if (k != OX_DATA) {
391: fprintf(MyErrorOut,"oxGetResultOfControlInt32: wrong header.");
392: return(-1);
393: }
394: k = oxfdGetInt32(fd); /* CMO_INT32 */
395: k = oxfdGetInt32(fd);
396: return(k);
397: }
398:
399: int oxclientMultiSelect(oxclientp clients[],int dataready[],
400: int controlready[], int size, int t)
401: {
402: int i, ddd;
403: int fd;
404: int humanfd = 0;
405: fd_set readfds;
406: struct timeval timeout;
407: extern int errno;
408:
409: /** printf("(1)"); fflush(NULL); */
410: FD_ZERO(&readfds);
411: timeout.tv_sec = 0;
412: timeout.tv_usec = (long) t;
413:
414: ddd = 0; fd = 0;
415: for (i=0; i<size; i++) {
416: dataready[i] = controlready[i] = 0;
417: }
418: for (i=0; i<size; i++) {
419: if (clients[i]->humanio) {
420: fd = (fd<humanfd?humanfd:fd);
421: FD_SET(humanfd,&readfds);
422: if (oxSocketSelect0(humanfd,0)) {
423: ddd = dataready[i] = 1; controlready[i] = 0;
424: }else{
425: dataready[i] = 0; controlready[i] = 0;
426: }
427: }else{
428: fd = (fd<clients[i]->controlfd?clients[i]->controlfd:fd);
429: FD_SET(clients[i]->controlfd,&readfds);
430: if (oxSocketSelect0(clients[i]->controlfd,0)) {
431: ddd = controlready[i] = 1;
432: }else{
433: controlready[i] = 0;
434: }
435: if (clients[i]->datafp2 != NULL) {
436: fd = (fd<clients[i]->datafp2->fd?clients[i]->datafp2->fd:fd);
437: FD_SET(clients[i]->datafp2->fd,&readfds);
438: if (fp2select(clients[i]->datafp2,0)) {
439: ddd = dataready[i] = 1;
440: }else{
441: dataready[i] = 0;
442: }
443: }else{
444: dataready[i] = 0;
445: }
446: }
447: }
448: if (t > 0 ) {
449: if (ddd) return(1);
450: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
451: fprintf(MyErrorOut,"error");
452: return(-1);
453: }
454: return(oxclientMultiSelect(clients, dataready, controlready,size,0));
455: }else if (t == 0) {
456: return(ddd);
457: }else {
458: /** printf("(2)"); fflush(NULL); */
459: if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
460: fprintf(MyErrorOut,"error");
461: return(-1);
462: }
463: /** printf("(3)"); fflush(NULL); */
464: return(oxclientMultiSelect(clients, dataready, controlready,size,0));
465: }
466: }
467:
468: int oxGetControl(oxclientp client)
469: /* synchronized. */
470: {
471: int ans;
472: ox_stream os;
473: switch (client->cstate) {
474: case 1:
475: ans = oxGetResultOfControlInt32(client->controlfd);
476: client->cstate = 0;
477: return(ans);
478: default:
479: fprintf(MyErrorOut,"oxGet: unknown cstate.\n");
480: client->cstate = -1;
481: return(-1);
482: }
483:
484: return(-1);
485: }
486:
487: int oxInitClient(oxclientp client)
488: {
489: client->datafp2 = NULL;
490: client->dataport = 0;
491: client->controlport = 0;
492: client->controlfd = 0;
493: client->humanio = 0;
494: client->dstate = 0;
495: client->cstate = 0;
496: client->id = -1;
497: return(0);
498: }
499:
500: int oxIsThereErrorClient(oxclientp client) {
501: if (client == NULL) return(1);
502: if (client->dstate == -1) return(1);
503: if (client->cstate == -1) return(1);
504: return(0);
505: }
506:
507: oxclientp oxCreateClient(char *sname,int portStream,int portControl)
508: {
509: static int clnum = 0;
510: int v = 0;
511: int fdControl = -1;
512: int fdStream = -1;
513: oxclientp client;
514: v = 1;
515: if (portControl != -1) {
516: fdControl = socketConnect(sname,portControl);
517: if (v) fprintf(stderr,"\nControl port %d : Connected.\n",portControl);
518: }
519: if (portStream != -1) {
520: sleep(1); /* wait */
521: fdStream = socketConnect(sname,portStream);
522: if (v) fprintf(stderr,"\nStream port %d : Connected.\n",portStream);
523: }
524:
525: if (fdStream == -1 || fdControl == -1) {
526: fprintf(stderr,"\nOpen error in oxCreateClient.\n");
527: return(NULL);
528: }
529: client = (oxclientp) mymalloc(sizeof(oxclient));
530: oxInitClient(client);
531: client->datafp2 = fp2open(fdStream);
532: if (client->datafp2 == NULL) {
533: fprintf(stderr,"oxCreateClient(): fp2open(fd) failed.\n");
534: return(NULL);
535: }
536: client->dataport = portStream;
537: client->controlport = portControl;
538: client->controlfd = fdControl;
539: client->id = clnum; clnum++;
540: client->type = CLIENT_SOCKET; /* socket */
541: return(client);
542: }
543:
544: oxclientp oxCreateClientFile(char *fname,char *mode,char *controlName,char *cmode)
545: {
546: static int clnum = 0x8000;
547: int v = 0;
548: int fdControl = -1;
549: int fdStream = -1;
550: oxclientp client;
551: v = 1;
552: if (strcmp(mode,"w") == 0) {
553: fdStream = creat(fname,S_IWRITE|S_IREAD|S_IRGRP|S_IROTH);
554: if (fdStream < 0) {
555: fprintf(stderr,"\nCreat failed for %s\n",fname); return(NULL);
556: }
557: }else if (strcmp(mode,"r")==0) {
558: fdStream = open(fname,O_RDONLY);
559: if (fdStream < 0) {
560: fprintf(stderr,"\nOpen failed for %s\n",fname); return(NULL);
561: }
562: }else {
563: fprintf(stderr,"\nThe mode %s is not supported.\n",mode); return(NULL);
564: }
565:
566: if (strcmp(cmode,"w") == 0) {
567: fdControl = creat(controlName,S_IWRITE|S_IREAD|S_IRGRP|S_IROTH);
568: if (fdControl < 0) {
569: fprintf(stderr,"\nCreat failed for %s\n",controlName); return(NULL);
570: }
571: }else if (strcmp(cmode,"r")==0) {
572: fdControl = open(controlName,O_RDONLY);
573: if (fdControl < 0) {
574: fprintf(stderr,"\nOpen failed for %s\n",controlName); return(NULL);
575: }
576: }else if (strcmp(cmode,"rw")==0) {
577: fdControl = open(controlName,O_RDWR);
578: if (fdControl < 0) {
579: fprintf(stderr,"\nOpen failed for %s\n",controlName); return(NULL);
580: }
581: }else {
582: fprintf(stderr,"\nThe mode %s is not supported.\n",cmode); return(NULL);
583: }
584:
585:
586: client = (oxclientp) mymalloc(sizeof(oxclient));
587: oxInitClient(client);
588: client->datafp2 = fp2open(fdStream);
589: if (client->datafp2 == NULL) {
590: fprintf(stderr,"oxCreateClientFile(): fp2open(fd) failed.\n");
591: return(NULL);
592: }
593: client->dataport = 0;
594: client->controlport = 0;
595: client->controlfd = fdControl;
596: client->id = clnum; clnum++;
597: client->type = CLIENT_FILE;
598: return(client);
599: }
600:
601: void oxSendOXheader_generic(int type,int fd,ox_stream ox,
602: int k,int serial)
603: {
604: static int ss = 0;
605: extern int UseOXPacketSerial;
606: if (serial >= 0) ss = serial;
607: else ss++;
608: if (ss < 0) ss=0;
609: if (type == 0) { /* fd */
610: oxfdSendInt32(fd,k);
611: if (UseOXPacketSerial) oxfdSendInt32(fd,ss);
612: }else {
613: oxSendInt32(ox,k);
614: if (UseOXPacketSerial) oxSendInt32(ox,ss);
615: }
616: }
617: void oxSendOXheader(ox_stream ostream,int k,int serial) {
618: oxSendOXheader_generic(1,-1,ostream,k,serial);
619: }
620: void oxfdSendOXheader(int fd,int k,int serial) {
621: oxSendOXheader_generic(0,fd,(ox_stream) NULL,k,serial);
622: }
623:
624:
625:
626:
627:
628:
629:
630:
631:
632:
633:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>