=================================================================== RCS file: /home/cvs/OpenXM/src/ox_math/Attic/ox.c,v retrieving revision 1.3 retrieving revision 1.5 diff -u -p -r1.3 -r1.5 --- OpenXM/src/ox_math/Attic/ox.c 1999/11/02 18:58:25 1.3 +++ OpenXM/src/ox_math/Attic/ox.c 1999/11/03 10:56:40 1.5 @@ -1,6 +1,5 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.2 1999/11/02 06:11:57 ohara Exp $ */ -/* $Id: ox.c,v 1.3 1999/11/02 18:58:25 ohara Exp $ */ +/* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.4 1999/11/02 21:15:02 ohara Exp $ */ /* 関数の名前付け規約(その2): @@ -45,20 +44,21 @@ static int cmolen_cmo_string(cmo_string* c); static int cmolen_cmo_zz(cmo_zz* c); static int cmolen_cmo_monomial32(cmo_monomial32* c); -static char* dump_cmo_int32(char* array, cmo_int32* m); -static char* dump_cmo_list(char* array, cmo_list* m); -static char* dump_cmo_mathcap(char* array, cmo_mathcap* m); -static char* dump_cmo_null(char* array, cmo_null* m); -static char* dump_cmo_string(char* array, cmo_string* m); -static char* dump_cmo_monomial32(char* array, cmo_monomial32* c); -static char* dump_cmo_zz(char* array, cmo_zz* c); -static char* dump_integer(char* array, int x); -static char* dump_mpz(char* array, mpz_ptr mpz); +static int dump_cmo_int32(cmo_int32* m); +static int dump_cmo_list(cmo_list* m); +static int dump_cmo_mathcap(cmo_mathcap* m); +static int dump_cmo_null(cmo_null* m); +static int dump_cmo_string(cmo_string* m); +static int dump_cmo_monomial32(cmo_monomial32* c); +static int dump_cmo_zz(cmo_zz* c); +static int dump_string(char *s, int len); +static int dump_integer(int x); +static int dump_mpz(mpz_ptr mpz); static int funcs(int cmo_type); /* CMO_xxx の値順にならべること(デバッグのため) */ -static int read_password(int fd, char* passwd); +static int login_with_otp(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); @@ -347,6 +347,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(); @@ -502,20 +509,6 @@ 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); @@ -560,7 +553,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 @@ -570,7 +585,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; @@ -590,14 +605,37 @@ 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); + /* 10マイクロ秒, 時間稼ぎする. */ + usleep(10); + sv->stream = mysocketOpen(host, portStream); + return sv; +} + void ox_reset(ox_file_t sv) { send_ox_command(sv->control, SM_control_reset_connection); @@ -708,121 +746,151 @@ int cmolen_cmo(cmo* c) return size; } -static char* dump_cmo_null(char* array, cmo_null* m) +static int d_ptr; +static char *d_buf; + +int init_dump_buffer(char *s) { - return array; + d_buf = s; + d_ptr = 0; } -static char* dump_cmo_int32(char* array, cmo_int32* m) +static int dump_cmo_null(cmo_null* m) { - return dump_integer(array, m->i); + return 0; } -static char* dump_cmo_string(char* array, cmo_string* m) +static int dump_cmo_int32(cmo_int32* m) { + dump_integer(m->i); +} + +static int dump_cmo_string(cmo_string* m) +{ int len = strlen(m->s); - array = dump_integer(array, len); - memcpy(array, m->s, len); - return array + len; + dump_integer(len); + dump_string(m->s, len); } -static char* dump_cmo_mathcap(char* array, cmo_mathcap* c) +static int dump_cmo_mathcap(cmo_mathcap* c) { - return dump_cmo(array, c->ob); + dump_cmo(c->ob); } -static char* dump_cmo_list(char* array, cmo_list* m) +static int dump_cmo_list(cmo_list* m) { cell* cp = m->head; int len = length_cmo_list(m); - array = dump_integer(array, len); + dump_integer(len); while(cp != NULL) { - array = dump_cmo(array, cp->cmo); + dump_cmo(cp->cmo); cp = cp->next; } - return array; } -static char* dump_cmo_monomial32(char* array, cmo_monomial32* c) +static int dump_cmo_monomial32(cmo_monomial32* c) { int i; int length = c->length; - array = dump_integer(array, c->length); + dump_integer(c->length); for(i=0; iexps[i]); + dump_integer(c->exps[i]); } - array = dump_cmo(array, c->coef); - return array; + dump_cmo(c->coef); } -static char* dump_cmo_zz(char* array, cmo_zz* c) +static int dump_cmo_zz(cmo_zz* c) { - return dump_mpz(array, c->mpz); + dump_mpz(c->mpz); } -static char* dump_cmo_distributed_polynomial(char* array, cmo_distributed_polynomial* m) +static int dump_cmo_distributed_polynomial(cmo_distributed_polynomial* m) { cell* cp = m->head; - int len = length_cmo_list(m); - array = dump_integer(array, len); - array = dump_cmo(array, m->ringdef); + int len = length_cmo_list((cmo_list *)m); + dump_integer(len); + dump_cmo(m->ringdef); while(cp != NULL) { - array = dump_cmo(array, cp->cmo); + dump_cmo(cp->cmo); cp = cp->next; } - return array; } /* タグを書き出してから、各関数を呼び出す */ -char* dump_cmo(char* array, cmo* m) +int dump_cmo(cmo* m) { - array = dump_integer(array, m->tag); + dump_integer(m->tag); switch(m->tag) { case CMO_NULL: case CMO_ZERO: case CMO_DMS_GENERIC: - return dump_cmo_null(array, m); + dump_cmo_null(m); + break; case CMO_INT32: - return dump_cmo_int32(array, (cmo_int32 *)m); + dump_cmo_int32((cmo_int32 *)m); + break; case CMO_STRING: - return dump_cmo_string(array, (cmo_string *)m); + dump_cmo_string((cmo_string *)m); + break; case CMO_MATHCAP: case CMO_RING_BY_NAME: case CMO_ERROR2: - return dump_cmo_mathcap(array, (cmo_mathcap *)m); + dump_cmo_mathcap((cmo_mathcap *)m); + break; case CMO_LIST: - return dump_cmo_list(array, (cmo_list *)m); + dump_cmo_list((cmo_list *)m); + break; case CMO_MONOMIAL32: - return dump_cmo_monomial32(array, (cmo_monomial32 *)m); + dump_cmo_monomial32((cmo_monomial32 *)m); + break; case CMO_ZZ: - return dump_cmo_zz(array, (cmo_zz *)m); + dump_cmo_zz((cmo_zz *)m); + break; case CMO_DISTRIBUTED_POLYNOMIAL: - return dump_cmo_distributed_polynomial(array, (cmo_distributed_polynomial *)m); + dump_cmo_distributed_polynomial((cmo_distributed_polynomial *)m); + break; default: - return NULL; } } -static char* dump_integer(char* array, int x) +static int dump_mpz(mpz_ptr mpz) { - int nx = htonl(x); - memcpy(array, &nx, sizeof(int)); - return array + sizeof(int); -} - -static char* dump_mpz(char* array, mpz_ptr mpz) -{ int i; int len = abs(mpz->_mp_size); - array = dump_integer(array, mpz->_mp_size); + dump_integer(mpz->_mp_size); for(i=0; i_mp_d[i]); + dump_integer(mpz->_mp_d[i]); } - return array; + return; } +static int dump_string(char *s, int len) +{ + memcpy(&d_buf[d_ptr], s, len); + d_ptr += len; +} +static int dump_integer(int x) +{ + int nx = htonl(x); + dump_string((char *)&nx, sizeof(int)); +} + +int dump_ox_data(ox_data* m) +{ + dump_integer(OX_DATA); + dump_integer(-1); + dump_cmo(m->cmo); +} + +int dump_ox_command(ox_command* m) +{ + dump_integer(OX_COMMAND); + dump_integer(-1); + dump_integer(m->command); +} + int send_ox(ox_file_t s, ox *m) { int tag = m->tag; @@ -959,20 +1027,6 @@ ox_command* new_ox_command(int sm_code) return m; } -char* dump_ox_data(char* array, ox_data* m) -{ - array = dump_integer(array, OX_DATA); - array = dump_integer(array, -1); - return dump_cmo(array, m->cmo); -} - -char* dump_ox_command(char* array, ox_command* m) -{ - array = dump_integer(array, OX_COMMAND); - array = dump_integer(array, -1); - return dump_integer(array, m->command); -} - #define MAX_TYPES 8 static int known_types[] = { -1, /* gate keeper */ @@ -1064,62 +1118,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"); /* まだ実装していません. */ @@ -1127,13 +1162,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;