| version 1.4, 1999/11/02 21:15:02 |
version 1.6, 1999/11/04 03:05:50 |
|
|
| /* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
| /* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.3 1999/11/02 18:58:25 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/ox_math/ox.c,v 1.5 1999/11/03 10:56:40 ohara Exp $ */ |
| /* $Id$ */ |
|
| |
|
| /* |
/* |
| 関数の名前付け規約(その2): |
関数の名前付け規約(その2): |
| Line 59 static int dump_mpz(mpz_ptr mpz); |
|
| Line 58 static int dump_mpz(mpz_ptr mpz); |
|
| static int funcs(int cmo_type); |
static int funcs(int cmo_type); |
| |
|
| /* CMO_xxx の値順にならべること(デバッグのため) */ |
/* 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_null* receive_cmo_null(int fd); |
| static cmo_int32* receive_cmo_int32(int fd); |
static cmo_int32* receive_cmo_int32(int fd); |
| static cmo_string* receive_cmo_string(int fd); |
static cmo_string* receive_cmo_string(int fd); |
| Line 284 cmo_int32* new_cmo_int32(int i) |
|
| Line 283 cmo_int32* new_cmo_int32(int i) |
|
| cmo_string* new_cmo_string(char* s) |
cmo_string* new_cmo_string(char* s) |
| { |
{ |
| cmo_string* c = malloc(sizeof(cmo_string)); |
cmo_string* c = malloc(sizeof(cmo_string)); |
| char *s2 = malloc(strlen(s)+1); |
|
| strcpy(s2, s); |
|
| |
|
| c->tag = CMO_STRING; |
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; |
return c; |
| } |
} |
| |
|
| Line 348 cmo_zz* new_cmo_zz_set_si(int i) |
|
| Line 349 cmo_zz* new_cmo_zz_set_si(int i) |
|
| return c; |
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* new_cmo_zz_size(int size) |
| { |
{ |
| cmo_zz* c = new_cmo_zz(); |
cmo_zz* c = new_cmo_zz(); |
| Line 503 int print_cmo_string(cmo_string* c) |
|
| Line 511 int print_cmo_string(cmo_string* c) |
|
| fprintf(stderr, "cmo_string = (%s)\n", c->s); |
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) |
void ox_close(ox_file_t sv) |
| { |
{ |
| send_ox_command(sv->control, SM_control_kill); |
send_ox_command(sv->control, SM_control_kill); |
| Line 561 cmo* ox_pop_cmo(ox_file_t sv, int fd) |
|
| Line 555 cmo* ox_pop_cmo(ox_file_t sv, int fd) |
|
| return receive_cmo(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 は クライアントが呼び出すための関数である. |
ox_start は クライアントが呼び出すための関数である. |
| サーバでは使われない. prog1 は コントロールサーバであり, |
サーバでは使われない. prog1 は コントロールサーバであり, |
| -ox, -reverse, -data, -control, -pass, -host |
-ox, -reverse, -data, -control, -pass, -host |
| Line 571 cmo* ox_pop_cmo(ox_file_t sv, int fd) |
|
| Line 587 cmo* ox_pop_cmo(ox_file_t sv, int fd) |
|
| |
|
| ox_file_t ox_start(char* host, char* prog1, char* prog2) |
ox_file_t ox_start(char* host, char* prog1, char* prog2) |
| { |
{ |
| static char pass[] = "passwd"; |
char *pass = create_otp(); |
| char ctl[16], dat[16]; |
char ctl[16], dat[16]; |
| short portControl = 0; /* short であることに注意 */ |
short portControl = 0; /* short であることに注意 */ |
| short portStream = 0; |
short portStream = 0; |
| Line 591 ox_file_t ox_start(char* host, char* prog1, char* prog |
|
| Line 607 ox_file_t ox_start(char* host, char* prog1, char* prog |
|
| } |
} |
| |
|
| sv->control = mysocketAccept(sv->control); |
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); |
usleep(10); |
| sv->stream = mysocketAccept(sv->stream); |
sv->stream = mysocketAccept(sv->stream); |
| decideByteOrderWithReadPasswd(sv->control, sv->control, 0, pass); |
login_with_otp(sv->stream, pass); |
| |
decideByteOrderClient(sv->stream, 0); |
| |
|
| return sv; |
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) |
void ox_reset(ox_file_t sv) |
| { |
{ |
| send_ox_command(sv->control, SM_control_reset_connection); |
send_ox_command(sv->control, SM_control_reset_connection); |
| Line 893 static int send_cmo_int32(int fd, cmo_int32* m) |
|
| Line 932 static int send_cmo_int32(int fd, cmo_int32* m) |
|
| |
|
| static int send_cmo_string(int fd, cmo_string* 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); |
send_int32(fd, len); |
| if (len > 0) { |
if (len > 0) { |
| write(fd, m->s, len); |
write(fd, m->s, len); |
| Line 1081 cmo* receive_cmo2(int fd) |
|
| Line 1120 cmo* receive_cmo2(int fd) |
|
| |
|
| /* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */ |
/* ファイルディスクリプタ fd の通信路での integer の byte order を決定する */ |
| /* 実際には order (0,1,or 0xFF)をみてはいない */ |
/* 実際には 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 zero = OX_BYTE_NETWORK_BYTE_ORDER; |
| char dest; |
char dest; |
| read_password(fd_read, passwd); |
read(fd, &dest, sizeof(char)); |
| write(fd_write, &zero, sizeof(char)); |
write(fd, &zero, sizeof(char)); |
| read(fd_read, &dest, sizeof(char)); |
|
| return 0; |
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 側ではこちらを用いる */ |
/* 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 zero = OX_BYTE_NETWORK_BYTE_ORDER; |
| char dest; |
char dest; |
| read(fd_read, &dest, sizeof(char)); |
write(fd, &zero, sizeof(char)); |
| write(fd_write, &zero, sizeof(char)); |
read(fd, &dest, sizeof(char)); |
| return 0; |
return 0; |
| } |
} |
| |
|
| /* cmo と string の変換関数群 */ |
/* cmo と string (ここではC言語のstring) の変換関数群 */ |
| |
char *convert_zz_to_string(cmo_zz *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; |
|
| } |
|
| |
|
| char *convert_zz_to_cstring(cmo_zz *c) |
|
| { |
|
| return mpz_get_str(NULL, 10, c->mpz); |
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) { |
switch(m->tag) { |
| case CMO_ZZ: |
case CMO_ZZ: |
| return convert_zz_to_cstring((cmo_zz *)m); |
return convert_zz_to_string((cmo_zz *)m); |
| case CMO_INT32: |
case CMO_INT32: |
| return convert_int_to_cstring(((cmo_int32 *)m)->i); |
return convert_int_to_string(((cmo_int32 *)m)->i); |
| case CMO_STRING: |
case CMO_STRING: |
| return ((cmo_string *)m)->s; |
return ((cmo_string *)m)->s; |
| case CMO_NULL: |
case CMO_NULL: |
| return convert_null_to_cstring(); |
return convert_null_to_string(); |
| default: |
default: |
| fprintf(stderr, "sorry, not implemented CMO\n"); |
fprintf(stderr, "sorry, not implemented CMO\n"); |
| /* まだ実装していません. */ |
/* まだ実装していません. */ |
| Line 1144 char *convert_cmo_to_cstring(cmo *m) |
|
| Line 1164 char *convert_cmo_to_cstring(cmo *m) |
|
| } |
} |
| } |
} |
| |
|
| char *convert_null_to_cstring() |
char *convert_null_to_string() |
| { |
{ |
| static char* null_string = ""; |
static char* null_string = ""; |
| return null_string; |
return null_string; |
| } |
} |
| |
|
| char *convert_int_to_cstring(int integer) |
char *convert_int_to_string(int integer) |
| { |
{ |
| char buff[1024]; |
char buff[1024]; |
| char *s; |
char *s; |