=================================================================== RCS file: /home/cvs/OpenXM/src/ox_math/Attic/ox.c,v retrieving revision 1.4 retrieving revision 1.9 diff -u -p -r1.4 -r1.9 --- OpenXM/src/ox_math/Attic/ox.c 1999/11/02 21:15:02 1.4 +++ OpenXM/src/ox_math/Attic/ox.c 1999/11/04 19:33:17 1.9 @@ -1,6 +1,5 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.3 1999/11/02 18:58:25 ohara Exp $ */ -/* $Id: ox.c,v 1.4 1999/11/02 21:15:02 ohara Exp $ */ +/* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.8 1999/11/04 18:13:47 ohara Exp $ */ /* 関数の名前付け規約(その2): @@ -58,16 +57,22 @@ static int dump_mpz(mpz_ptr mpz); static int funcs(int cmo_type); +static int login_with_otp(int fd, char* passwd); +static char *create_otp(); + /* CMO_xxx の値順にならべること(デバッグのため) */ -static int read_password(int fd, char* passwd); static cmo_null* receive_cmo_null(int fd); static cmo_int32* receive_cmo_int32(int fd); static cmo_string* receive_cmo_string(int fd); static cmo_mathcap* receive_cmo_mathcap(int fd); static cmo_list* receive_cmo_list(int fd); +static cmo_monomial32* receive_cmo_monomial32(int fd); static cmo_zz* receive_cmo_zz(int fd); static cmo_zero* receive_cmo_zero(int fd); -static cmo_dms_generic* receive_cmo_dms_generic(int fd); +static cmo_dms_generic* receive_cmo_dms_generic(int fd); +static cmo_ring_by_name* receive_cmo_ring_by_name(int fd); +static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd); + static cmo_error2* receive_cmo_error2(int fd); static void receive_mpz(int fd, mpz_ptr mpz); @@ -76,11 +81,12 @@ static int send_cmo_int32(int fd, cmo_int32* static int send_cmo_string(int fd, cmo_string* m); static int send_cmo_mathcap(int fd, cmo_mathcap* c); static int send_cmo_list(int fd, cmo_list* c); +static int send_cmo_monomial32(int fd, cmo_monomial32* c); static int send_cmo_zz(int fd, cmo_zz* c); static int send_cmo_error2(int fd, cmo_error2* c); static int send_mpz(int fd, mpz_ptr mpz); +static int send_cmo_distributed_polynomial(int fd, cmo_distributed_polynomial* c); - /* エラーハンドリングのため */ static int current_received_serial = 0; @@ -136,13 +142,12 @@ int send_ox_tag(int fd, int tag) return send_int32(fd, next_serial()); } - /* CMO_LIST 関係の関数群 */ -cell* new_cell(cmo* newcmo) +cell* new_cell() { cell* h = malloc(sizeof(cell)); h->next = NULL; - h->cmo = newcmo; + h->cmo = NULL; return h; } @@ -151,28 +156,28 @@ cell* next_cell(cell* this) return this->next; } -static cell* *tailp(cmo_list* this) { - cell **cp = &this->head; - while (*cp != NULL) { - cp = &((*cp)->next); +static cell *tail(cmo_list* this) { + cell *cp = this->head; + while (cp->next != NULL) { + cp = cp->next; } return cp; } -int length_cmo_list(cmo_list* this) -{ - return this->length; -} - int append_cmo_list(cmo_list* this, cmo* newcmo) { - /* リストの最後尾のNULLを保持している変数へのポインタ */ - cell **cp = tailp(this); - *cp = new_cell(newcmo); + cell *cp = tail(this); + cp->cmo = newcmo; + cp->next = new_cell(); this->length++; return 0; } +int length_cmo_list(cmo_list* this) +{ + return this->length; +} + /** receive_cmo_XXX 関数群 **/ static cmo_null* receive_cmo_null(int fd) { @@ -204,18 +209,31 @@ static cmo_mathcap* receive_cmo_mathcap(int fd) static cmo_list* receive_cmo_list(int fd) { - cmo* newcmo; + cmo* ob; cmo_list* c = new_cmo_list(); int len = receive_int32(fd); while (len>0) { - newcmo = receive_cmo(fd); - append_cmo_list(c, newcmo); + ob = receive_cmo(fd); + append_cmo_list(c, ob); len--; } return c; } +static cmo_monomial32* receive_cmo_monomial32(int fd) +{ + int i; + int len = receive_int32(fd); + cmo_monomial32* c = new_cmo_monomial32(len); + + for(i=0; iexps[i] = receive_int32(fd); + } + c->coef = receive_cmo(fd); + return c; +} + static cmo_zz* receive_cmo_zz(int fd) { cmo_zz* c = new_cmo_zz(); @@ -240,12 +258,82 @@ static cmo_ring_by_name* receive_cmo_ring_by_name(int return new_cmo_ring_by_name(ob); } +static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(int fd) +{ + cmo* ob; + cmo_distributed_polynomial* c = new_cmo_distributed_polynomial(); + int len = receive_int32(fd); + c->ringdef = receive_cmo(fd); + + while (len>0) { + ob = receive_cmo(fd); + append_cmo_list(c, ob); + len--; + } + return c; +} + static cmo_error2* receive_cmo_error2(int fd) { cmo* ob = receive_cmo(fd); return new_cmo_error2(ob); } +/* receive_ox_tag() == OX_DATA の後に呼び出される */ +/* 関数ポインタを使った方がきれいに書けるような気がする. */ +/* if (foo[tag] != NULL) foo[tag](fd); とか */ + +cmo* receive_cmo(int fd) +{ + cmo* m; + int tag; + tag = receive_int32(fd); + + switch(tag) { + case CMO_NULL: + m = receive_cmo_null(fd); + break; + case CMO_INT32: + m = (cmo *)receive_cmo_int32(fd); + break; + case CMO_STRING: + m = (cmo *)receive_cmo_string(fd); + break; + case CMO_MATHCAP: + m = (cmo *)receive_cmo_mathcap(fd); + break; + case CMO_LIST: + m = (cmo *)receive_cmo_list(fd); + break; + case CMO_MONOMIAL32: + m = (cmo *)receive_cmo_monomial32(fd); + break; + case CMO_ZZ: + m = (cmo *)receive_cmo_zz(fd); + break; + case CMO_ZERO: + m = (cmo *)receive_cmo_zero(fd); + break; + case CMO_DMS_GENERIC: + m = (cmo *)receive_cmo_dms_generic(fd); + break; + case CMO_RING_BY_NAME: + m = (cmo *)receive_cmo_ring_by_name(fd); + break; + case CMO_DISTRIBUTED_POLYNOMIAL: + m = (cmo *)receive_cmo_distributed_polynomial(fd); + break; + case CMO_ERROR2: + m = (cmo *)receive_cmo_error2(fd); + break; + case CMO_DATUM: + case CMO_QQ: + default: + fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag); + } + return m; +} + static void receive_mpz(int fd, mpz_ptr mpz) { int i; @@ -284,11 +372,13 @@ cmo_int32* new_cmo_int32(int i) cmo_string* new_cmo_string(char* s) { cmo_string* c = malloc(sizeof(cmo_string)); - char *s2 = malloc(strlen(s)+1); - strcpy(s2, s); - c->tag = CMO_STRING; - c->s = s2; + if (s != NULL) { + c->s = malloc(strlen(s)+1); + strcpy(c->s, s); + }else { + c->s = NULL; + } return c; } @@ -305,7 +395,7 @@ cmo_list* new_cmo_list() cmo_list* c = malloc(sizeof(cmo_list)); c->tag = CMO_LIST; c->length = 0; - c->head = NULL; + c->head->next = NULL; return c; } @@ -348,6 +438,13 @@ cmo_zz* new_cmo_zz_set_si(int i) return c; } +cmo_zz *new_cmo_zz_set_string(char *s) +{ + cmo_zz* c = new_cmo_zz_noinit(); + mpz_init_set_str(c->mpz, s, 10); + return c; +} + cmo_zz* new_cmo_zz_size(int size) { cmo_zz* c = new_cmo_zz(); @@ -390,7 +487,7 @@ cmo_distributed_polynomial* new_cmo_distributed_polyno cmo_distributed_polynomial* c = malloc(sizeof(cmo_distributed_polynomial)); c->tag = CMO_DISTRIBUTED_POLYNOMIAL; c->length = 0; - c->head = NULL; + c->head->next = NULL; c->ringdef = NULL; return c; } @@ -403,48 +500,6 @@ cmo_error2* new_cmo_error2(cmo* ob) return c; } -/* receive_ox_tag() == OX_DATA の後に呼び出される */ -/* 関数ポインタを使った方がきれいに書けるような気がする. */ -/* if (foo[tag] != NULL) foo[tag](fd); とか */ - -cmo* receive_cmo(int fd) -{ - cmo* m; - int tag; - tag = receive_int32(fd); - - switch(tag) { - case CMO_NULL: - m = receive_cmo_null(fd); - break; - case CMO_INT32: - m = (cmo *)receive_cmo_int32(fd); - break; - case CMO_STRING: - m = (cmo *)receive_cmo_string(fd); - break; - case CMO_MATHCAP: - m = (cmo *)receive_cmo_mathcap(fd); - break; - case CMO_LIST: - m = (cmo *)receive_cmo_list(fd); - break; - case CMO_ZZ: - m = (cmo *)receive_cmo_zz(fd); - break; - case CMO_ERROR2: - m = (cmo *)receive_cmo_error2(fd); - break; - case CMO_DATUM: - case CMO_QQ: - case CMO_ZERO: - case CMO_DMS: - default: - fprintf(stderr, "unknown cmo-type: tag = (%d)\n", m->tag); - } - return m; -} - void send_ox_command(int fd, int sm_command) { send_ox_tag(fd, OX_COMMAND); @@ -503,26 +558,12 @@ int print_cmo_string(cmo_string* c) fprintf(stderr, "cmo_string = (%s)\n", c->s); } -/* ox_start() から呼び出す */ -/* OneTimePassword の処理 */ -static int read_password(int fd, char* passwd) -{ - char buff[1024]; - int n; - if ((n = read(fd, buff, 1024)) != strlen(passwd)) { - fprintf(stderr, "Socket#%d: Login incorrect.\n", fd); - fprintf(stderr, "password = (%s), received = (%s).\n", passwd, buff); - fprintf(stderr, " = (%d), received = (%d).\n", strlen(passwd), n); - } - fflush(stderr); -} - void ox_close(ox_file_t sv) { send_ox_command(sv->control, SM_control_kill); #if DEBUG sleep(2); /* OpenXM server の終了を待つ. あまり意味はない. */ - fprintf(stderr, "I have closed an Open XM server.\n"); + fprintf(stderr, "I have closed the connection to an Open XM server.\n"); #endif } @@ -561,7 +602,29 @@ cmo* ox_pop_cmo(ox_file_t sv, int fd) return receive_cmo(fd); } +/* 手抜き. (後で改善しよう...) */ +static char *create_otp() +{ + static char otp[] = "otpasswd"; + return otp; +} + +/* OneTimePassword の処理 */ +static int login_with_otp(int fd, char* passwd) +{ + char buff[1024]; + int n = read(fd, buff, 1024); + int len = strlen(passwd)+1; + if (n != len) { + fprintf(stderr, "Socket#%d: Login incorrect.\n", fd); + fprintf(stderr, "password = (%s), length = (%d).\n", passwd, len); + fprintf(stderr, "received = (%d), length = (%d).\n", buff, n); + fflush(stderr); + } +} + /* + (-reverse 版の ox_start) ox_start は クライアントが呼び出すための関数である. サーバでは使われない. prog1 は コントロールサーバであり, -ox, -reverse, -data, -control, -pass, -host @@ -571,7 +634,7 @@ cmo* ox_pop_cmo(ox_file_t sv, int fd) ox_file_t ox_start(char* host, char* prog1, char* prog2) { - static char pass[] = "passwd"; + char *pass = create_otp(); char ctl[16], dat[16]; short portControl = 0; /* short であることに注意 */ short portStream = 0; @@ -591,14 +654,42 @@ ox_file_t ox_start(char* host, char* prog1, char* prog } sv->control = mysocketAccept(sv->control); - decideByteOrderWithReadPasswd(sv->control, sv->control, 0, pass); + login_with_otp(sv->control, pass); + decideByteOrderClient(sv->control, 0); + /* 10マイクロ秒, 時間稼ぎする. */ usleep(10); sv->stream = mysocketAccept(sv->stream); - decideByteOrderWithReadPasswd(sv->control, sv->control, 0, pass); + login_with_otp(sv->stream, pass); + decideByteOrderClient(sv->stream, 0); return sv; } +/* + (-insecure 版の ox_start) まだ、中身はありません。 + ox_start_insecure_nonreverse は クライアントが呼び出すための関数である. + 接続時には, sv->control を先にオープンする. + 既定値: + portControl = 1200 + portStream = 1300 +*/ + +ox_file_t ox_start_insecure_nonreverse(char* host, short portControl, short portStream) +{ + ox_file_t sv = malloc(sizeof(__ox_file_struct)); + + sv->control = mysocketOpen(host, portControl); +#if 0 + /* ox は insecure のとき byte order の決定が正しくできないようだ... */ + decideByteOrderClient(sv->control, 0); +#endif + /* 10マイクロ秒, 時間稼ぎする. */ + usleep(10); + sv->stream = mysocketOpen(host, portStream); + decideByteOrderClient(sv->stream, 0); + return sv; +} + void ox_reset(ox_file_t sv) { send_ox_command(sv->control, SM_control_reset_connection); @@ -643,10 +734,10 @@ static int cmolen_cmo_mathcap(cmo_mathcap* c) static int cmolen_cmo_list(cmo_list* c) { - int size = sizeof(c->head); + int size = sizeof(int); cell* cp = c->head; - while(cp != NULL) { + while(cp->next != NULL) { size += cmolen_cmo(cp->cmo); cp = cp->next; } @@ -689,6 +780,7 @@ int cmolen_cmo(cmo* c) break; case CMO_MATHCAP: case CMO_RING_BY_NAME: + case CMO_INDETERMINATE: case CMO_ERROR2: size += cmolen_cmo_mathcap((cmo_mathcap *)c); break; @@ -746,7 +838,7 @@ static int dump_cmo_list(cmo_list* m) int len = length_cmo_list(m); dump_integer(len); - while(cp != NULL) { + while(cp->next != NULL) { dump_cmo(cp->cmo); cp = cp->next; } @@ -798,6 +890,7 @@ int dump_cmo(cmo* m) break; case CMO_MATHCAP: case CMO_RING_BY_NAME: + case CMO_INDETERMINATE: case CMO_ERROR2: dump_cmo_mathcap((cmo_mathcap *)m); break; @@ -893,7 +986,7 @@ static int send_cmo_int32(int fd, cmo_int32* m) static int send_cmo_string(int fd, cmo_string* m) { - int len = strlen(m->s); + int len = (m->s != NULL)? strlen(m->s): 0; send_int32(fd, len); if (len > 0) { write(fd, m->s, len); @@ -913,16 +1006,43 @@ static int send_cmo_list(int fd, cmo_list* c) int len = length_cmo_list(c); send_int32(fd, len); - while(cp != NULL) { + while(cp->next != NULL) { send_cmo(fd, cp->cmo); cp = cp->next; } return 0; } +static int send_cmo_distributed_polynomial(int fd, cmo_distributed_polynomial* c) +{ + cell* cp = c->head; + int len = length_cmo_list((cmo_list *)c); + send_int32(fd, len); + send_cmo(fd, c->ringdef); + + while(cp->next != NULL) { + send_cmo(fd, cp->cmo); + cp = cp->next; + } + return 0; +} + +static int send_cmo_monomial32(int fd, cmo_monomial32* c) +{ + int i; + int len = c->length; + send_int32(fd, len); + for(i=0; iexps[i]); + } + send_cmo(fd, c->coef); + return 0; +} + static int send_cmo_zz(int fd, cmo_zz* c) { send_mpz(fd, c->mpz); + return 0; } static int send_cmo_error2(int fd, cmo_error2* c) @@ -939,6 +1059,8 @@ int send_cmo(int fd, cmo* c) send_int32(fd, tag); switch(tag) { case CMO_NULL: + case CMO_ZERO: + case CMO_DMS_GENERIC: send_cmo_null(fd, c); /* 空の関数 */ break; case CMO_INT32: @@ -948,16 +1070,22 @@ int send_cmo(int fd, cmo* c) send_cmo_string(fd, (cmo_string *)c); break; case CMO_MATHCAP: + case CMO_ERROR2: + case CMO_RING_BY_NAME: + case CMO_INDETERMINATE: send_cmo_mathcap(fd, (cmo_mathcap *)c); break; case CMO_LIST: send_cmo_list(fd, (cmo_list *)c); break; + case CMO_MONOMIAL32: + send_cmo_monomial32(fd, (cmo_monomial32 *)c); + break; case CMO_ZZ: send_cmo_zz(fd, (cmo_zz *)c); break; - case CMO_ERROR2: - send_cmo_error2(fd, (cmo_error2 *)c); + case CMO_DISTRIBUTED_POLYNOMIAL: + send_cmo_distributed_polynomial(fd, (cmo_distributed_polynomial *)c); break; default: } @@ -1057,6 +1185,7 @@ void setCmotypeDisable(int type) int i = funcs(type); known_types[i] = -1; } + #if 0 cmo* (*received_funcs[])(int fd) = { NULL, /* gate keeper */ @@ -1081,62 +1210,43 @@ cmo* receive_cmo2(int fd) /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */ /* 実際には order (0,1,or 0xFF)をみてはいない */ -int decideByteOrderWithReadPasswd(int fd_read, int fd_write, int order, char* passwd) +int decideByteOrderClient(oxfd fd, int order) { char zero = OX_BYTE_NETWORK_BYTE_ORDER; char dest; - read_password(fd_read, passwd); - write(fd_write, &zero, sizeof(char)); - read(fd_read, &dest, sizeof(char)); + read(fd, &dest, sizeof(char)); + write(fd, &zero, sizeof(char)); return 0; } -/* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */ -/* 実際には order (0,1,or 0xFF)をみてはいない */ -int decideByteOrder(int fd_read, int fd_write, int order) -{ - char zero = OX_BYTE_NETWORK_BYTE_ORDER; - char dest; - write(fd_write, &zero, sizeof(char)); - read(fd_read, &dest, sizeof(char)); - return 0; -} - /* Server 側ではこちらを用いる */ -int decideByteOrder2(int fd_read, int fd_write, int order) +/* いまの実装は dup されていることが前提になっている */ +int decideByteOrderServer(oxfd fd, int order) { char zero = OX_BYTE_NETWORK_BYTE_ORDER; char dest; - read(fd_read, &dest, sizeof(char)); - write(fd_write, &zero, sizeof(char)); + write(fd, &zero, sizeof(char)); + read(fd, &dest, sizeof(char)); return 0; } -/* cmo と string の変換関数群 */ - -cmo_zz *new_cmo_zz_set_string(char *s) +/* cmo と string (ここではC言語のstring) の変換関数群 */ +char *convert_zz_to_string(cmo_zz *c) { - cmo_zz* c = new_cmo_zz_noinit(); - mpz_init_set_str(c->mpz, s, 10); - return c; -} - -char *convert_zz_to_cstring(cmo_zz *c) -{ return mpz_get_str(NULL, 10, c->mpz); } -char *convert_cmo_to_cstring(cmo *m) +char *convert_cmo_to_string(cmo *m) { switch(m->tag) { case CMO_ZZ: - return convert_zz_to_cstring((cmo_zz *)m); + return convert_zz_to_string((cmo_zz *)m); case CMO_INT32: - return convert_int_to_cstring(((cmo_int32 *)m)->i); + return convert_int_to_string(((cmo_int32 *)m)->i); case CMO_STRING: return ((cmo_string *)m)->s; case CMO_NULL: - return convert_null_to_cstring(); + return convert_null_to_string(); default: fprintf(stderr, "sorry, not implemented CMO\n"); /* まだ実装していません. */ @@ -1144,13 +1254,13 @@ char *convert_cmo_to_cstring(cmo *m) } } -char *convert_null_to_cstring() +char *convert_null_to_string() { static char* null_string = ""; return null_string; } -char *convert_int_to_cstring(int integer) +char *convert_int_to_string(int integer) { char buff[1024]; char *s;