Annotation of OpenXM/src/ox_math/ox.c, Revision 1.9
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.9 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.8 1999/11/04 18:13:47 ohara Exp $ */
1.1 ohara 3:
4: /*
5: 関数の名前付け規約(その2):
6: (1) receive_cmo 関数はCMOタグとデータ本体を受信する. この関数は CMOタグの
7: 値が事前に分からないときに使用する. 返り値として、cmo へのポインタを返す.
8: (2) receive_cmo_XXX 関数は, CMOタグを親の関数で受信してから呼び出される関
9: 数で、データ本体のみを受信し、cmo_XXX へのポインタを返す. しかも、
10: 関数内部で new_cmo_XXX 関数を呼び出す.
11: (3) send_cmo 関数はCMOタグとデータ本体を送信する.
12: (4) send_cmo_XXX 関数はCMOタグを親の関数で送信してから呼び出される関数で、
13: データ本体のみを送信する.
14:
15: ----
16: (5) receive_ox_XXX 関数は存在しない(作らない). receive_cmo を利用する.
17: (6) send_ox_XXX 関数は OX タグを含めて送信する.
18: (7) ox_XXX 関数は一連の送受信を含むより抽象的な操作を表現する.
19: ox_XXX 関数は、第一引数として、ox_file_t型の変数 sv をとる.
20:
21: (8) YYY_cmo 関数と YYY_cmo_XXX 関数の関係は次の通り:
22: まず YYY_cmo 関数で cmo のタグを処理し、タグを除いた残りの部分を
23: YYY_cmo_XXX 関数が処理する. cmo の内部に cmo_ZZZ へのポインタが
24: あるときには、その種類によらずに YYY_cmo 関数を呼び出す.
25: */
26:
27: #include <stdio.h>
28: #include <stdlib.h>
29: #include <string.h>
30: #include <unistd.h>
31: #include <errno.h>
32: #include <fcntl.h>
33: #include <gmp.h>
34:
35: #include "mysocket.h"
36: #include "ox.h"
37: #include "parse.h"
38:
39: static int cmolen_cmo_int32(cmo_int32* c);
40: static int cmolen_cmo_list(cmo_list* c);
41: static int cmolen_cmo_mathcap(cmo_mathcap* c);
42: static int cmolen_cmo_null(cmo_null* c);
43: static int cmolen_cmo_string(cmo_string* c);
44: static int cmolen_cmo_zz(cmo_zz* c);
1.2 ohara 45: static int cmolen_cmo_monomial32(cmo_monomial32* c);
1.1 ohara 46:
1.4 ohara 47: static int dump_cmo_int32(cmo_int32* m);
48: static int dump_cmo_list(cmo_list* m);
49: static int dump_cmo_mathcap(cmo_mathcap* m);
50: static int dump_cmo_null(cmo_null* m);
51: static int dump_cmo_string(cmo_string* m);
52: static int dump_cmo_monomial32(cmo_monomial32* c);
53: static int dump_cmo_zz(cmo_zz* c);
54: static int dump_string(char *s, int len);
55: static int dump_integer(int x);
56: static int dump_mpz(mpz_ptr mpz);
1.1 ohara 57:
58: static int funcs(int cmo_type);
59:
1.7 ohara 60: static int login_with_otp(int fd, char* passwd);
61: static char *create_otp();
62:
1.2 ohara 63: /* CMO_xxx の値順にならべること(デバッグのため) */
64: static cmo_null* receive_cmo_null(int fd);
1.1 ohara 65: static cmo_int32* receive_cmo_int32(int fd);
1.2 ohara 66: static cmo_string* receive_cmo_string(int fd);
67: static cmo_mathcap* receive_cmo_mathcap(int fd);
1.1 ohara 68: static cmo_list* receive_cmo_list(int fd);
1.7 ohara 69: static cmo_monomial32* receive_cmo_monomial32(int fd);
1.1 ohara 70: static cmo_zz* receive_cmo_zz(int fd);
1.2 ohara 71: static cmo_zero* receive_cmo_zero(int fd);
1.7 ohara 72: static cmo_dms_generic* receive_cmo_dms_generic(int fd);
73: static cmo_ring_by_name* receive_cmo_ring_by_name(int fd);
74: static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd);
75:
1.2 ohara 76: static cmo_error2* receive_cmo_error2(int fd);
1.1 ohara 77: static void receive_mpz(int fd, mpz_ptr mpz);
78:
1.2 ohara 79: static int send_cmo_null(int fd, cmo_null* c);
1.1 ohara 80: static int send_cmo_int32(int fd, cmo_int32* m);
1.2 ohara 81: static int send_cmo_string(int fd, cmo_string* m);
82: static int send_cmo_mathcap(int fd, cmo_mathcap* c);
1.1 ohara 83: static int send_cmo_list(int fd, cmo_list* c);
1.7 ohara 84: static int send_cmo_monomial32(int fd, cmo_monomial32* c);
1.1 ohara 85: static int send_cmo_zz(int fd, cmo_zz* c);
1.2 ohara 86: static int send_cmo_error2(int fd, cmo_error2* c);
1.1 ohara 87: static int send_mpz(int fd, mpz_ptr mpz);
1.7 ohara 88: static int send_cmo_distributed_polynomial(int fd, cmo_distributed_polynomial* c);
1.1 ohara 89:
90: /* エラーハンドリングのため */
91: static int current_received_serial = 0;
92:
93: /* エラーを起こしたときはサーバは次のようにすればよい. */
1.2 ohara 94: cmo_error2* gen_error_object(int err_code)
1.1 ohara 95: {
96: cmo_list* li = new_cmo_list();
97: append_cmo_list(li, (cmo *)new_cmo_int32(current_received_serial));
98: append_cmo_list(li, (cmo *)new_cmo_int32(err_code));
99: /* 他の情報を加えるならココ */
1.2 ohara 100: return new_cmo_error2((cmo *)li);
1.1 ohara 101: }
102:
103: /* add at Mon Sep 7 15:51:28 JST 1998 */
104: #define DEFAULT_SERIAL_NUMBER 0x0000ffff
105: #define receive_serial_number(x) (receive_int32(x))
106:
107: /* 新しいシリアル番号を得る */
108: int next_serial()
109: {
110: static int serial_number = DEFAULT_SERIAL_NUMBER;
111: return serial_number++;
112: }
113:
114: /* int32 型のオブジェクトを送信する. */
115: int send_int32(int fd, int int32)
116: {
117: int32 = htonl(int32);
118: return write(fd, &int32, sizeof(int));
119: }
120:
121: /* int32 型のオブジェクトを受信する. */
122: int receive_int32(int fd)
123: {
124: int tag;
125: read(fd, &tag, sizeof(int));
126: return ntohl(tag);
127: }
128:
129: /* (OX_tag, serial number) を受信する. */
130: int receive_ox_tag(int fd)
131: {
132: int serial;
133: int tag = receive_int32(fd);
134: current_received_serial = receive_serial_number(fd);
135: return tag;
136: }
137:
138: /* (OX_tag, serial number) を送信する. */
139: int send_ox_tag(int fd, int tag)
140: {
141: send_int32(fd, tag);
142: return send_int32(fd, next_serial());
143: }
144:
145: /* CMO_LIST 関係の関数群 */
1.8 ohara 146: cell* new_cell()
1.1 ohara 147: {
148: cell* h = malloc(sizeof(cell));
149: h->next = NULL;
1.8 ohara 150: h->cmo = NULL;
1.1 ohara 151: return h;
152: }
153:
1.2 ohara 154: cell* next_cell(cell* this)
155: {
156: return this->next;
157: }
158:
1.8 ohara 159: static cell *tail(cmo_list* this) {
160: cell *cp = this->head;
161: while (cp->next != NULL) {
162: cp = cp->next;
1.1 ohara 163: }
164: return cp;
165: }
166:
1.8 ohara 167: int append_cmo_list(cmo_list* this, cmo* newcmo)
1.1 ohara 168: {
1.8 ohara 169: cell *cp = tail(this);
170: cp->cmo = newcmo;
171: cp->next = new_cell();
172: this->length++;
173: return 0;
1.1 ohara 174: }
175:
1.8 ohara 176: int length_cmo_list(cmo_list* this)
1.1 ohara 177: {
1.8 ohara 178: return this->length;
1.1 ohara 179: }
180:
181: /** receive_cmo_XXX 関数群 **/
1.2 ohara 182: static cmo_null* receive_cmo_null(int fd)
1.1 ohara 183: {
1.2 ohara 184: return new_cmo_null();
1.1 ohara 185: }
186:
187: static cmo_int32* receive_cmo_int32(int fd)
188: {
189: int i = receive_int32(fd);
190: return new_cmo_int32(i);
191: }
192:
1.2 ohara 193: static cmo_string* receive_cmo_string(int fd)
194: {
195: int len = receive_int32(fd);
196: char* s = malloc(len+1);
197: memset(s, '\0', len+1);
198: if (len > 0) {
199: read(fd, s, len);
200: }
201: return new_cmo_string(s);
202: }
203:
204: static cmo_mathcap* receive_cmo_mathcap(int fd)
205: {
206: cmo* ob = receive_cmo(fd);
207: return new_cmo_mathcap(ob);
208: }
209:
1.1 ohara 210: static cmo_list* receive_cmo_list(int fd)
211: {
1.7 ohara 212: cmo* ob;
1.1 ohara 213: cmo_list* c = new_cmo_list();
214: int len = receive_int32(fd);
215:
216: while (len>0) {
1.7 ohara 217: ob = receive_cmo(fd);
218: append_cmo_list(c, ob);
1.1 ohara 219: len--;
220: }
221: return c;
222: }
223:
1.7 ohara 224: static cmo_monomial32* receive_cmo_monomial32(int fd)
225: {
226: int i;
227: int len = receive_int32(fd);
228: cmo_monomial32* c = new_cmo_monomial32(len);
229:
230: for(i=0; i<len; i++) {
231: c->exps[i] = receive_int32(fd);
232: }
233: c->coef = receive_cmo(fd);
234: return c;
235: }
236:
1.2 ohara 237: static cmo_zz* receive_cmo_zz(int fd)
238: {
239: cmo_zz* c = new_cmo_zz();
240: receive_mpz(fd, c->mpz);
241: return c;
242: }
243:
244: static cmo_zero* receive_cmo_zero(int fd)
1.1 ohara 245: {
1.2 ohara 246: return new_cmo_zero();
1.1 ohara 247: }
248:
1.2 ohara 249: static cmo_dms_generic* receive_cmo_dms_generic(int fd)
1.1 ohara 250: {
1.2 ohara 251: return new_cmo_dms_generic();
1.1 ohara 252: }
253:
1.2 ohara 254: static cmo_ring_by_name* receive_cmo_ring_by_name(int fd)
1.1 ohara 255: {
1.2 ohara 256: cmo* ob = receive_cmo(fd);
257: /* 意味的チェックが必要 */
258: return new_cmo_ring_by_name(ob);
1.1 ohara 259: }
260:
1.7 ohara 261: static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd)
262: {
263: cmo* ob;
264: cmo_distributed_polynomial* c = new_cmo_distributed_polynomial();
265: int len = receive_int32(fd);
266: c->ringdef = receive_cmo(fd);
267:
268: while (len>0) {
269: ob = receive_cmo(fd);
270: append_cmo_list(c, ob);
271: len--;
272: }
273: return c;
274: }
275:
1.2 ohara 276: static cmo_error2* receive_cmo_error2(int fd)
1.1 ohara 277: {
1.2 ohara 278: cmo* ob = receive_cmo(fd);
279: return new_cmo_error2(ob);
1.1 ohara 280: }
281:
1.7 ohara 282: /* receive_ox_tag() == OX_DATA の後に呼び出される */
283: /* 関数ポインタを使った方がきれいに書けるような気がする. */
284: /* if (foo[tag] != NULL) foo[tag](fd); とか */
285:
286: cmo* receive_cmo(int fd)
287: {
288: cmo* m;
289: int tag;
290: tag = receive_int32(fd);
291:
292: switch(tag) {
293: case CMO_NULL:
294: m = receive_cmo_null(fd);
295: break;
296: case CMO_INT32:
297: m = (cmo *)receive_cmo_int32(fd);
298: break;
299: case CMO_STRING:
300: m = (cmo *)receive_cmo_string(fd);
301: break;
302: case CMO_MATHCAP:
303: m = (cmo *)receive_cmo_mathcap(fd);
304: break;
305: case CMO_LIST:
306: m = (cmo *)receive_cmo_list(fd);
307: break;
308: case CMO_MONOMIAL32:
309: m = (cmo *)receive_cmo_monomial32(fd);
310: break;
311: case CMO_ZZ:
312: m = (cmo *)receive_cmo_zz(fd);
313: break;
314: case CMO_ZERO:
315: m = (cmo *)receive_cmo_zero(fd);
316: break;
317: case CMO_DMS_GENERIC:
318: m = (cmo *)receive_cmo_dms_generic(fd);
319: break;
320: case CMO_RING_BY_NAME:
321: m = (cmo *)receive_cmo_ring_by_name(fd);
322: break;
323: case CMO_DISTRIBUTED_POLYNOMIAL:
324: m = (cmo *)receive_cmo_distributed_polynomial(fd);
325: break;
326: case CMO_ERROR2:
327: m = (cmo *)receive_cmo_error2(fd);
328: break;
329: case CMO_DATUM:
330: case CMO_QQ:
331: default:
332: fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag);
333: }
334: return m;
335: }
336:
1.1 ohara 337: static void receive_mpz(int fd, mpz_ptr mpz)
338: {
339: int i;
340: int size = receive_int32(fd);
341: int len = abs(size);
342: resize_mpz(mpz, size);
343:
344: for(i=0; i<len; i++) {
345: mpz->_mp_d[i] = receive_int32(fd);
346: }
347: }
348:
349: void resize_mpz(mpz_ptr mpz, int size)
350: {
351: _mpz_realloc(mpz, abs(size));
352: mpz->_mp_size = size;
353: }
354:
355: /** new_cmo_XXX 関数群 **/
1.2 ohara 356: cmo_null* new_cmo_null()
1.1 ohara 357: {
1.2 ohara 358: cmo_null* m = malloc(sizeof(cmo_null));
359: m->tag = CMO_NULL;
360: return m;
1.1 ohara 361: }
362:
363: cmo_int32* new_cmo_int32(int i)
364: {
365: cmo_int32* c;
366: c = malloc(sizeof(cmo_int32));
367: c->tag = CMO_INT32;
368: c->i = i;
369: return c;
370: }
371:
1.2 ohara 372: cmo_string* new_cmo_string(char* s)
373: {
374: cmo_string* c = malloc(sizeof(cmo_string));
375: c->tag = CMO_STRING;
1.6 ohara 376: if (s != NULL) {
377: c->s = malloc(strlen(s)+1);
378: strcpy(c->s, s);
379: }else {
380: c->s = NULL;
381: }
1.2 ohara 382: return c;
383: }
384:
385: cmo_mathcap* new_cmo_mathcap(cmo* ob)
386: {
387: cmo_mathcap* c = malloc(sizeof(cmo_mathcap));
388: c->tag = CMO_MATHCAP;
389: c->ob = ob;
390: return c;
391: }
392:
1.1 ohara 393: cmo_list* new_cmo_list()
394: {
395: cmo_list* c = malloc(sizeof(cmo_list));
396: c->tag = CMO_LIST;
397: c->length = 0;
1.8 ohara 398: c->head->next = NULL;
1.1 ohara 399: return c;
400: }
401:
1.2 ohara 402: cmo_monomial32* new_cmo_monomial32()
1.1 ohara 403: {
1.2 ohara 404: cmo_monomial32* c = malloc(sizeof(cmo_monomial32));
405: c->tag = CMO_MONOMIAL32;
1.1 ohara 406: return c;
407: }
408:
1.2 ohara 409: cmo_monomial32* new_cmo_monomial32_size(int size)
1.1 ohara 410: {
1.2 ohara 411: cmo_monomial32* c = new_cmo_monomial32();
412: if (size>0) {
413: c->length = size;
414: c->exps = malloc(sizeof(int)*size);
415: }
1.1 ohara 416: return c;
417: }
418:
419: cmo_zz* new_cmo_zz()
420: {
421: cmo_zz* c = malloc(sizeof(cmo_zz));
422: c->tag = CMO_ZZ;
423: mpz_init(c->mpz);
424: return c;
425: }
426:
427: cmo_zz* new_cmo_zz_noinit()
428: {
429: cmo_zz* c = malloc(sizeof(cmo_zz));
430: c->tag = CMO_ZZ;
431: return c;
432: }
433:
434: cmo_zz* new_cmo_zz_set_si(int i)
435: {
436: cmo_zz* c = new_cmo_zz();
437: mpz_set_si(c->mpz, i);
438: return c;
439: }
440:
1.5 ohara 441: cmo_zz *new_cmo_zz_set_string(char *s)
442: {
443: cmo_zz* c = new_cmo_zz_noinit();
444: mpz_init_set_str(c->mpz, s, 10);
445: return c;
446: }
447:
1.1 ohara 448: cmo_zz* new_cmo_zz_size(int size)
449: {
450: cmo_zz* c = new_cmo_zz();
451: resize_mpz(c->mpz, size);
452: return c;
453: }
454:
1.2 ohara 455: cmo_zero* new_cmo_zero()
456: {
457: cmo_zero* m = malloc(sizeof(cmo_zero));
458: m->tag = CMO_ZERO;
459: return m;
460: }
461:
462: cmo_dms_generic* new_cmo_dms_generic()
463: {
464: cmo_dms_generic* m = malloc(sizeof(cmo_dms_generic));
465: m->tag = CMO_DMS_GENERIC;
466: return m;
467: }
468:
469: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
470: {
471: cmo_ring_by_name* c = malloc(sizeof(cmo_ring_by_name));
472: c->tag = CMO_RING_BY_NAME;
473: c->ob = ob;
474: return c;
475: }
476:
477: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
478: {
479: cmo_indeterminate* c = malloc(sizeof(cmo_indeterminate));
480: c->tag = CMO_INDETERMINATE;
481: c->ob = ob;
482: return c;
483: }
484:
1.3 ohara 485: cmo_distributed_polynomial* new_cmo_distributed_polynomial()
486: {
487: cmo_distributed_polynomial* c = malloc(sizeof(cmo_distributed_polynomial));
488: c->tag = CMO_DISTRIBUTED_POLYNOMIAL;
489: c->length = 0;
1.8 ohara 490: c->head->next = NULL;
1.3 ohara 491: c->ringdef = NULL;
492: return c;
493: }
494:
1.2 ohara 495: cmo_error2* new_cmo_error2(cmo* ob)
496: {
497: cmo_error2* c = malloc(sizeof(cmo_error2));
498: c->tag = CMO_ERROR2;
499: c->ob = ob;
500: return c;
501: }
502:
1.1 ohara 503: void send_ox_command(int fd, int sm_command)
504: {
505: send_ox_tag(fd, OX_COMMAND);
506: send_int32(fd, sm_command);
507: }
508:
509: int print_cmo(cmo* c)
510: {
511: int tag = c->tag;
512: fprintf(stderr, "local::tag = (%d): ", tag);
513: switch(tag) {
514: case CMO_LIST:
515: print_cmo_list((cmo_list *)c);
516: break;
517: case CMO_INT32:
518: print_cmo_int32((cmo_int32 *)c);
519: break;
520: case CMO_MATHCAP:
521: print_cmo_mathcap((cmo_mathcap *)c);
522: break;
523: case CMO_STRING:
524: print_cmo_string((cmo_string *)c);
525: break;
526: case CMO_NULL:
527: fprintf(stderr, "\n");
528: break;
529: default:
530: fprintf(stderr, "print_cmo() does not know how to print.\n");
531: }
532: }
533:
534: int print_cmo_int32(cmo_int32* c)
535: {
536: fprintf(stderr, "cmo_int32 = (%d)\n", c->i);
537: }
538:
539: int print_cmo_list(cmo_list* li)
540: {
541: cell* cp = li->head;
542: fprintf(stderr, "length = (%d)\nlist:\n", li->length);
543: while(cp != NULL) {
544: print_cmo(cp->cmo);
545: cp=cp->next;
546: }
547: fprintf(stderr, "end of list\n");
548: }
549:
550: int print_cmo_mathcap(cmo_mathcap* c)
551: {
552: fprintf(stderr, "\n");
1.2 ohara 553: print_cmo(c->ob);
1.1 ohara 554: }
555:
556: int print_cmo_string(cmo_string* c)
557: {
558: fprintf(stderr, "cmo_string = (%s)\n", c->s);
559: }
560:
561: void ox_close(ox_file_t sv)
562: {
563: send_ox_command(sv->control, SM_control_kill);
564: #if DEBUG
565: sleep(2); /* OpenXM server の終了を待つ. あまり意味はない. */
1.8 ohara 566: fprintf(stderr, "I have closed the connection to an Open XM server.\n");
1.1 ohara 567: #endif
568: }
569:
570: void ox_executeStringByLocalParser(ox_file_t sv, char* s)
571: {
572: /* 文字列ををスタックにプッシュ. */
573: send_ox_cmo(sv->stream, (cmo *)new_cmo_string(s));
574:
575: /* サーバに実行させる. */
576: send_ox_command(sv->stream, SM_executeStringByLocalParser);
577: }
578:
579: /* ox_mathcap() をコールする. */
580: cmo_mathcap* ox_mathcap(ox_file_t sv)
581: {
582: send_ox_command(sv->stream, SM_mathcap);
583: send_ox_command(sv->stream, SM_popCMO);
584: receive_ox_tag(sv->stream); /* OX_DATA */
585: return (cmo_mathcap *)receive_cmo(sv->stream);
586: }
587:
588: char* ox_popString(ox_file_t sv, int fd)
589: {
590: cmo_string* m = NULL;
591:
592: send_ox_command(fd, SM_popString);
593: receive_ox_tag(fd); /* OX_DATA */
594: m = (cmo_string *)receive_cmo(fd);
595: return m->s;
596: }
597:
598: cmo* ox_pop_cmo(ox_file_t sv, int fd)
599: {
600: send_ox_command(fd, SM_popCMO);
601: receive_ox_tag(fd); /* OX_DATA */
602: return receive_cmo(fd);
603: }
604:
1.5 ohara 605: /* 手抜き. (後で改善しよう...) */
606: static char *create_otp()
607: {
608: static char otp[] = "otpasswd";
609: return otp;
610: }
611:
612: /* OneTimePassword の処理 */
613: static int login_with_otp(int fd, char* passwd)
614: {
615: char buff[1024];
616: int n = read(fd, buff, 1024);
617: int len = strlen(passwd)+1;
618: if (n != len) {
619: fprintf(stderr, "Socket#%d: Login incorrect.\n", fd);
620: fprintf(stderr, "password = (%s), length = (%d).\n", passwd, len);
621: fprintf(stderr, "received = (%d), length = (%d).\n", buff, n);
622: fflush(stderr);
623: }
624: }
625:
1.1 ohara 626: /*
1.5 ohara 627: (-reverse 版の ox_start)
1.1 ohara 628: ox_start は クライアントが呼び出すための関数である.
629: サーバでは使われない. prog1 は コントロールサーバであり,
630: -ox, -reverse, -data, -control, -pass, -host
631: というオプションを理解することを仮定する. prog2 は計算サーバである.
632: 接続時には, sv->control を先にオープンする.
633: */
634:
635: ox_file_t ox_start(char* host, char* prog1, char* prog2)
636: {
1.5 ohara 637: char *pass = create_otp();
1.1 ohara 638: char ctl[16], dat[16];
639: short portControl = 0; /* short であることに注意 */
640: short portStream = 0;
641: ox_file_t sv = malloc(sizeof(__ox_file_struct));
642:
643: sv->control = mysocketListen(host, &portControl);
644: sv->stream = mysocketListen(host, &portStream);
645: sprintf(ctl, "%d", portControl);
646: sprintf(dat, "%d", portStream);
647:
648: if (fork() == 0) {
649: dup2(2, 1);
650: dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2);
651: execl(prog1, prog1, "-reverse", "-ox", prog2,
652: "-data", dat, "-control", ctl, "-pass", pass,
653: "-host", host, NULL);
654: }
655:
656: sv->control = mysocketAccept(sv->control);
1.5 ohara 657: login_with_otp(sv->control, pass);
658: decideByteOrderClient(sv->control, 0);
659: /* 10マイクロ秒, 時間稼ぎする. */
1.1 ohara 660: usleep(10);
661: sv->stream = mysocketAccept(sv->stream);
1.5 ohara 662: login_with_otp(sv->stream, pass);
663: decideByteOrderClient(sv->stream, 0);
1.1 ohara 664:
665: return sv;
666: }
667:
1.5 ohara 668: /*
669: (-insecure 版の ox_start) まだ、中身はありません。
670: ox_start_insecure_nonreverse は クライアントが呼び出すための関数である.
671: 接続時には, sv->control を先にオープンする.
672: 既定値:
673: portControl = 1200
674: portStream = 1300
675: */
676:
677: ox_file_t ox_start_insecure_nonreverse(char* host, short portControl, short portStream)
678: {
679: ox_file_t sv = malloc(sizeof(__ox_file_struct));
680:
681: sv->control = mysocketOpen(host, portControl);
1.9 ! ohara 682: #if 0
! 683: /* ox は insecure のとき byte order の決定が正しくできないようだ... */
! 684: decideByteOrderClient(sv->control, 0);
! 685: #endif
1.5 ohara 686: /* 10マイクロ秒, 時間稼ぎする. */
687: usleep(10);
688: sv->stream = mysocketOpen(host, portStream);
1.9 ! ohara 689: decideByteOrderClient(sv->stream, 0);
1.5 ohara 690: return sv;
691: }
692:
1.1 ohara 693: void ox_reset(ox_file_t sv)
694: {
695: send_ox_command(sv->control, SM_control_reset_connection);
696:
697: receive_ox_tag(sv->control); /* OX_DATA */
698: receive_cmo(sv->control); /* (CMO_INT32, 0) */
699:
700: while(receive_ox_tag(sv->stream) != OX_SYNC_BALL) {
701: receive_cmo(sv->stream); /* skipping a message. */
702: }
703:
704: send_ox_tag(sv->stream, OX_SYNC_BALL);
705: #if DEBUG
706: fprintf(stderr, "I have reset an Open XM server.\n");
707: #endif
708: }
709:
1.2 ohara 710: /* 以下は bconv.c で必要とする関数群である. */
711:
712: /* cmolen 関数は cmo の(送信時の)バイト長を返す. */
713: /* cmolen_XXX 関数は cmo_XXX の tag を除いたバイト長を返す. */
714:
715: static int cmolen_cmo_null(cmo_null* c)
716: {
717: return 0;
718: }
1.1 ohara 719:
720: static int cmolen_cmo_int32(cmo_int32* c)
721: {
722: return sizeof(int);
723: }
724:
1.2 ohara 725: static int cmolen_cmo_string(cmo_string* c)
726: {
727: return sizeof(int)+strlen(c->s);
728: }
729:
730: static int cmolen_cmo_mathcap(cmo_mathcap* c)
731: {
732: return cmolen_cmo(c->ob);
733: }
734:
1.1 ohara 735: static int cmolen_cmo_list(cmo_list* c)
736: {
1.8 ohara 737: int size = sizeof(int);
1.1 ohara 738: cell* cp = c->head;
739:
1.8 ohara 740: while(cp->next != NULL) {
1.1 ohara 741: size += cmolen_cmo(cp->cmo);
742: cp = cp->next;
743: }
744: return size;
745: }
746:
1.2 ohara 747: static int cmolen_cmo_monomial32(cmo_monomial32* c)
1.1 ohara 748: {
1.2 ohara 749: int len = (c->length + 1)*sizeof(int);
750: return len + cmolen_cmo(c->coef);
1.1 ohara 751: }
752:
753: static int cmolen_cmo_zz(cmo_zz* c)
754: {
755: int len = abs(c->mpz->_mp_size);
756: return sizeof(len) + len*sizeof(int);
757: }
758:
1.3 ohara 759: static int cmolen_cmo_distributed_polynomial(cmo_distributed_polynomial* c)
760: {
761: return cmolen_cmo_list((cmo_list *)c) + cmolen_cmo(c->ringdef);
762: }
763:
1.1 ohara 764: /* CMO がバイトエンコードされた場合のバイト列の長さを求める */
765: int cmolen_cmo(cmo* c)
766: {
767: int size = sizeof(int);
768:
769: switch(c->tag) {
770: case CMO_NULL:
1.2 ohara 771: case CMO_ZERO:
772: case CMO_DMS_GENERIC:
1.1 ohara 773: size += cmolen_cmo_null(c);
774: break;
775: case CMO_INT32:
776: size += cmolen_cmo_int32((cmo_int32 *)c);
777: break;
778: case CMO_STRING:
779: size += cmolen_cmo_string((cmo_string *)c);
780: break;
1.2 ohara 781: case CMO_MATHCAP:
782: case CMO_RING_BY_NAME:
1.8 ohara 783: case CMO_INDETERMINATE:
1.2 ohara 784: case CMO_ERROR2:
785: size += cmolen_cmo_mathcap((cmo_mathcap *)c);
786: break;
1.1 ohara 787: case CMO_LIST:
788: size += cmolen_cmo_list((cmo_list *)c);
789: break;
1.2 ohara 790: case CMO_MONOMIAL32:
791: size += cmolen_cmo_monomial32((cmo_monomial32 *)c);
792: break;
1.1 ohara 793: case CMO_ZZ:
794: size += cmolen_cmo_zz((cmo_zz *)c);
795: break;
1.3 ohara 796: case CMO_DISTRIBUTED_POLYNOMIAL:
797: size += cmolen_cmo_distributed_polynomial((cmo_distributed_polynomial *)c);
798: break;
1.1 ohara 799: default:
800: }
801: return size;
802: }
803:
1.4 ohara 804: static int d_ptr;
805: static char *d_buf;
806:
807: int init_dump_buffer(char *s)
1.2 ohara 808: {
1.4 ohara 809: d_buf = s;
810: d_ptr = 0;
1.2 ohara 811: }
812:
1.4 ohara 813: static int dump_cmo_null(cmo_null* m)
1.1 ohara 814: {
1.4 ohara 815: return 0;
1.1 ohara 816: }
817:
1.4 ohara 818: static int dump_cmo_int32(cmo_int32* m)
819: {
820: dump_integer(m->i);
821: }
822:
823: static int dump_cmo_string(cmo_string* m)
1.2 ohara 824: {
825: int len = strlen(m->s);
1.4 ohara 826: dump_integer(len);
827: dump_string(m->s, len);
1.2 ohara 828: }
829:
1.4 ohara 830: static int dump_cmo_mathcap(cmo_mathcap* c)
1.2 ohara 831: {
1.4 ohara 832: dump_cmo(c->ob);
1.2 ohara 833: }
834:
1.4 ohara 835: static int dump_cmo_list(cmo_list* m)
1.1 ohara 836: {
837: cell* cp = m->head;
838: int len = length_cmo_list(m);
1.4 ohara 839: dump_integer(len);
1.1 ohara 840:
1.8 ohara 841: while(cp->next != NULL) {
1.4 ohara 842: dump_cmo(cp->cmo);
1.1 ohara 843: cp = cp->next;
844: }
845: }
846:
1.4 ohara 847: static int dump_cmo_monomial32(cmo_monomial32* c)
1.1 ohara 848: {
1.2 ohara 849: int i;
850: int length = c->length;
1.4 ohara 851: dump_integer(c->length);
1.2 ohara 852: for(i=0; i<length; i++) {
1.4 ohara 853: dump_integer(c->exps[i]);
1.2 ohara 854: }
1.4 ohara 855: dump_cmo(c->coef);
1.1 ohara 856: }
857:
1.4 ohara 858: static int dump_cmo_zz(cmo_zz* c)
1.1 ohara 859: {
1.4 ohara 860: dump_mpz(c->mpz);
1.1 ohara 861: }
862:
1.4 ohara 863: static int dump_cmo_distributed_polynomial(cmo_distributed_polynomial* m)
1.3 ohara 864: {
865: cell* cp = m->head;
1.4 ohara 866: int len = length_cmo_list((cmo_list *)m);
867: dump_integer(len);
868: dump_cmo(m->ringdef);
1.3 ohara 869: while(cp != NULL) {
1.4 ohara 870: dump_cmo(cp->cmo);
1.3 ohara 871: cp = cp->next;
872: }
873: }
874:
1.1 ohara 875: /* タグを書き出してから、各関数を呼び出す */
1.4 ohara 876: int dump_cmo(cmo* m)
1.1 ohara 877: {
1.4 ohara 878: dump_integer(m->tag);
1.1 ohara 879: switch(m->tag) {
880: case CMO_NULL:
1.2 ohara 881: case CMO_ZERO:
882: case CMO_DMS_GENERIC:
1.4 ohara 883: dump_cmo_null(m);
884: break;
1.1 ohara 885: case CMO_INT32:
1.4 ohara 886: dump_cmo_int32((cmo_int32 *)m);
887: break;
1.1 ohara 888: case CMO_STRING:
1.4 ohara 889: dump_cmo_string((cmo_string *)m);
890: break;
1.2 ohara 891: case CMO_MATHCAP:
892: case CMO_RING_BY_NAME:
1.8 ohara 893: case CMO_INDETERMINATE:
1.2 ohara 894: case CMO_ERROR2:
1.4 ohara 895: dump_cmo_mathcap((cmo_mathcap *)m);
896: break;
1.1 ohara 897: case CMO_LIST:
1.4 ohara 898: dump_cmo_list((cmo_list *)m);
899: break;
1.2 ohara 900: case CMO_MONOMIAL32:
1.4 ohara 901: dump_cmo_monomial32((cmo_monomial32 *)m);
902: break;
1.1 ohara 903: case CMO_ZZ:
1.4 ohara 904: dump_cmo_zz((cmo_zz *)m);
905: break;
1.3 ohara 906: case CMO_DISTRIBUTED_POLYNOMIAL:
1.4 ohara 907: dump_cmo_distributed_polynomial((cmo_distributed_polynomial *)m);
908: break;
1.1 ohara 909: default:
910: }
911: }
912:
1.4 ohara 913: static int dump_mpz(mpz_ptr mpz)
914: {
915: int i;
916: int len = abs(mpz->_mp_size);
917: dump_integer(mpz->_mp_size);
918: for(i=0; i<len; i++) {
919: dump_integer(mpz->_mp_d[i]);
920: }
921: return;
922: }
923:
924: static int dump_string(char *s, int len)
925: {
926: memcpy(&d_buf[d_ptr], s, len);
927: d_ptr += len;
928: }
929:
930: static int dump_integer(int x)
1.1 ohara 931: {
932: int nx = htonl(x);
1.4 ohara 933: dump_string((char *)&nx, sizeof(int));
1.1 ohara 934: }
935:
1.4 ohara 936: int dump_ox_data(ox_data* m)
1.1 ohara 937: {
1.4 ohara 938: dump_integer(OX_DATA);
939: dump_integer(-1);
940: dump_cmo(m->cmo);
1.1 ohara 941: }
942:
1.4 ohara 943: int dump_ox_command(ox_command* m)
944: {
945: dump_integer(OX_COMMAND);
946: dump_integer(-1);
947: dump_integer(m->command);
948: }
1.1 ohara 949:
950: int send_ox(ox_file_t s, ox *m)
951: {
952: int tag = m->tag;
953: int code;
954: if (tag == OX_DATA) {
955: send_ox_cmo(s->stream, ((ox_data *)m)->cmo);
956: }else if (tag == OX_COMMAND) {
957: code = ((ox_command *)m)->command;
958: if (code >= 1024) {
959: /* control command */
960: send_ox_command(s->control, code);
961: }else {
962: send_ox_command(s->stream, code);
963: }
964: }else {
965: /* CMO?? */
966: send_ox_cmo(s->stream, (cmo *)m);
967: }
968: }
969:
970: int send_ox_cmo(int fd, cmo* m)
971: {
972: send_ox_tag(fd, OX_DATA);
973: send_cmo(fd, m);
974: }
975:
1.2 ohara 976: /* send_cmo_xxx 関数群 */
977: static int send_cmo_null(int fd, cmo_null* c)
978: {
979: return 0;
980: }
1.1 ohara 981:
1.2 ohara 982: static int send_cmo_int32(int fd, cmo_int32* m)
1.1 ohara 983: {
1.2 ohara 984: send_int32(fd, m->i);
1.1 ohara 985: }
986:
1.2 ohara 987: static int send_cmo_string(int fd, cmo_string* m)
1.1 ohara 988: {
1.6 ohara 989: int len = (m->s != NULL)? strlen(m->s): 0;
1.2 ohara 990: send_int32(fd, len);
991: if (len > 0) {
992: write(fd, m->s, len);
993: }
1.1 ohara 994: return 0;
995: }
996:
1.2 ohara 997: static int send_cmo_mathcap(int fd, cmo_mathcap* c)
1.1 ohara 998: {
1.2 ohara 999: send_cmo(fd, c->ob);
1000: return 0;
1.1 ohara 1001: }
1002:
1003: static int send_cmo_list(int fd, cmo_list* c)
1004: {
1005: cell* cp = c->head;
1006: int len = length_cmo_list(c);
1007: send_int32(fd, len);
1008:
1.8 ohara 1009: while(cp->next != NULL) {
1.1 ohara 1010: send_cmo(fd, cp->cmo);
1011: cp = cp->next;
1012: }
1013: return 0;
1014: }
1015:
1.7 ohara 1016: static int send_cmo_distributed_polynomial(int fd, cmo_distributed_polynomial* c)
1017: {
1018: cell* cp = c->head;
1019: int len = length_cmo_list((cmo_list *)c);
1020: send_int32(fd, len);
1021: send_cmo(fd, c->ringdef);
1022:
1.8 ohara 1023: while(cp->next != NULL) {
1.7 ohara 1024: send_cmo(fd, cp->cmo);
1025: cp = cp->next;
1026: }
1027: return 0;
1028: }
1029:
1030: static int send_cmo_monomial32(int fd, cmo_monomial32* c)
1031: {
1032: int i;
1033: int len = c->length;
1034: send_int32(fd, len);
1035: for(i=0; i<len; i++) {
1036: send_int32(fd, c->exps[i]);
1037: }
1038: send_cmo(fd, c->coef);
1039: return 0;
1040: }
1041:
1.2 ohara 1042: static int send_cmo_zz(int fd, cmo_zz* c)
1.1 ohara 1043: {
1.2 ohara 1044: send_mpz(fd, c->mpz);
1.7 ohara 1045: return 0;
1.1 ohara 1046: }
1047:
1.2 ohara 1048: static int send_cmo_error2(int fd, cmo_error2* c)
1.1 ohara 1049: {
1.2 ohara 1050: send_cmo(fd, c->ob);
1.1 ohara 1051: return 0;
1052: }
1053:
1054: /* CMOを送る. OX_tag は送信済*/
1055: int send_cmo(int fd, cmo* c)
1056: {
1057: int tag = c->tag;
1058:
1059: send_int32(fd, tag);
1060: switch(tag) {
1061: case CMO_NULL:
1.7 ohara 1062: case CMO_ZERO:
1063: case CMO_DMS_GENERIC:
1.1 ohara 1064: send_cmo_null(fd, c); /* 空の関数 */
1065: break;
1066: case CMO_INT32:
1067: send_cmo_int32(fd, (cmo_int32 *)c);
1068: break;
1069: case CMO_STRING:
1070: send_cmo_string(fd, (cmo_string *)c);
1071: break;
1072: case CMO_MATHCAP:
1.7 ohara 1073: case CMO_ERROR2:
1074: case CMO_RING_BY_NAME:
1075: case CMO_INDETERMINATE:
1.1 ohara 1076: send_cmo_mathcap(fd, (cmo_mathcap *)c);
1077: break;
1078: case CMO_LIST:
1079: send_cmo_list(fd, (cmo_list *)c);
1080: break;
1.7 ohara 1081: case CMO_MONOMIAL32:
1082: send_cmo_monomial32(fd, (cmo_monomial32 *)c);
1083: break;
1.1 ohara 1084: case CMO_ZZ:
1085: send_cmo_zz(fd, (cmo_zz *)c);
1086: break;
1.7 ohara 1087: case CMO_DISTRIBUTED_POLYNOMIAL:
1088: send_cmo_distributed_polynomial(fd, (cmo_distributed_polynomial *)c);
1.1 ohara 1089: break;
1090: default:
1091: }
1092: }
1093:
1094: static int send_mpz(int fd, mpz_ptr mpz)
1095: {
1096: int i;
1097: int len = abs(mpz->_mp_size);
1098: send_int32(fd, mpz->_mp_size);
1099: for(i=0; i<len; i++) {
1100: send_int32(fd, mpz->_mp_d[i]);
1101: }
1102: return 0;
1103: }
1104:
1105: ox_data* new_ox_data(cmo* c)
1106: {
1107: ox_data* m = malloc(sizeof(ox_data));
1108: m->tag = OX_DATA;
1109: m->cmo = c;
1110: return m;
1111: }
1112:
1113: ox_command* new_ox_command(int sm_code)
1114: {
1115: ox_command* m = malloc(sizeof(ox_command));
1116: m->tag = OX_COMMAND;
1117: m->command = sm_code;
1118: return m;
1119: }
1120:
1121: #define MAX_TYPES 8
1122: static int known_types[] = {
1123: -1, /* gate keeper */
1124: CMO_NULL,
1125: CMO_INT32,
1126: CMO_STRING,
1127: CMO_MATHCAP,
1128: CMO_LIST,
1129: CMO_ZZ,
1130: CMO_ERROR2,
1131: };
1132:
1.2 ohara 1133: #define ID_TEMP "(CMO_MATHCAP, (CMO_LIST, (CMO_LIST, (CMO_INT32, %d), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\"), (CMO_STRING, \"%s\")), (CMO_LIST, (CMO_INT32, 1), (CMO_INT32, 2), (CMO_INT32, 4), (CMO_INT32, 5), (CMO_INT32, 17), (CMO_INT32, 20), (CMO_INT32, 2130706434))))\n"
1.1 ohara 1134:
1.2 ohara 1135: cmo* make_mathcap_object2(int ver, char* ver_s, char* sysname)
1.1 ohara 1136: {
1.2 ohara 1137: cmo *cap;
1138: char buff[8192];
1.1 ohara 1139:
1140: setgetc(mygetc);
1.2 ohara 1141: sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE"));
1142: setmode_mygetc(buff, 8192);
1143: cap = parse();
1.1 ohara 1144: resetgetc();
1145:
1.2 ohara 1146: return cap;
1.1 ohara 1147: }
1148:
1149: cmo* make_mathcap_object(int version, char* id_string)
1150: {
1151: cmo_list *li_ver, *li_cmo, *li;
1152:
1153: int i;
1154: li_ver = new_cmo_list();
1155: append_cmo_list(li_ver, (cmo *)new_cmo_int32(version));
1156: append_cmo_list(li_ver, (cmo *)new_cmo_string(id_string));
1157:
1158: li_cmo = new_cmo_list();
1159: for(i=0; i<MAX_TYPES; i++) {
1160: if (known_types[i] != -1) {
1161: append_cmo_list(li_cmo, (cmo *)new_cmo_int32(known_types[i]));
1162: }
1163: }
1164:
1165: li = new_cmo_list();
1166: append_cmo_list(li, (cmo *)li_ver);
1167: append_cmo_list(li, (cmo *)li_cmo);
1168:
1.2 ohara 1169: return (cmo *)new_cmo_mathcap((cmo *)li);
1.1 ohara 1170: }
1171:
1172: static int funcs(int cmo_type)
1173: {
1174: int i;
1175: for(i=0; i<MAX_TYPES; i++) {
1176: if (known_types[i] == cmo_type) {
1177: return i;
1178: }
1179: }
1180: return 0;
1181: }
1182:
1183: void setCmotypeDisable(int type)
1184: {
1185: int i = funcs(type);
1186: known_types[i] = -1;
1187: }
1.8 ohara 1188:
1.1 ohara 1189: #if 0
1190: cmo* (*received_funcs[])(int fd) = {
1191: NULL, /* gate keeper */
1192: receive_cmo_null,
1193: receive_cmo_int32,
1194: receive_cmo_string,
1195: receive_cmo_mathcap,
1196: receive_cmo_list,
1197: receive_cmo_zz,
1.2 ohara 1198: receive_cmo_error2
1.1 ohara 1199: };
1200:
1201: cmo* receive_cmo2(int fd)
1202: {
1203: int tag;
1204: cmo* (*foo)() = received_funcs[funcs(tag)];
1205: if (foo != NULL) {
1206: return foo(fd);
1207: }
1208: }
1.3 ohara 1209: #endif
1210:
1211: /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */
1212: /* 実際には order (0,1,or 0xFF)をみてはいない */
1.5 ohara 1213: int decideByteOrderClient(oxfd fd, int order)
1.1 ohara 1214: {
1.3 ohara 1215: char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1 ohara 1216: char dest;
1.5 ohara 1217: read(fd, &dest, sizeof(char));
1218: write(fd, &zero, sizeof(char));
1.1 ohara 1219: return 0;
1220: }
1221:
1222: /* Server 側ではこちらを用いる */
1.5 ohara 1223: /* いまの実装は dup されていることが前提になっている */
1224: int decideByteOrderServer(oxfd fd, int order)
1.1 ohara 1225: {
1.3 ohara 1226: char zero = OX_BYTE_NETWORK_BYTE_ORDER;
1.1 ohara 1227: char dest;
1.5 ohara 1228: write(fd, &zero, sizeof(char));
1229: read(fd, &dest, sizeof(char));
1.1 ohara 1230: return 0;
1231: }
1232:
1.5 ohara 1233: /* cmo と string (ここではC言語のstring) の変換関数群 */
1234: char *convert_zz_to_string(cmo_zz *c)
1.1 ohara 1235: {
1236: return mpz_get_str(NULL, 10, c->mpz);
1237: }
1238:
1.5 ohara 1239: char *convert_cmo_to_string(cmo *m)
1.1 ohara 1240: {
1241: switch(m->tag) {
1242: case CMO_ZZ:
1.5 ohara 1243: return convert_zz_to_string((cmo_zz *)m);
1.1 ohara 1244: case CMO_INT32:
1.5 ohara 1245: return convert_int_to_string(((cmo_int32 *)m)->i);
1.1 ohara 1246: case CMO_STRING:
1247: return ((cmo_string *)m)->s;
1248: case CMO_NULL:
1.5 ohara 1249: return convert_null_to_string();
1.1 ohara 1250: default:
1251: fprintf(stderr, "sorry, not implemented CMO\n");
1252: /* まだ実装していません. */
1253: return NULL;
1254: }
1255: }
1256:
1.5 ohara 1257: char *convert_null_to_string()
1.1 ohara 1258: {
1259: static char* null_string = "";
1260: return null_string;
1261: }
1262:
1.5 ohara 1263: char *convert_int_to_string(int integer)
1.1 ohara 1264: {
1265: char buff[1024];
1266: char *s;
1267:
1268: sprintf(buff, "%d", integer);
1269: s = malloc(strlen(buff)+1);
1270: strcpy(s, buff);
1271:
1272: return s;
1273: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>