version 1.4, 1999/12/15 05:57:35 |
version 1.7, 2000/01/17 19:55:55 |
|
|
/* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
/* $OpenXM: OpenXM/src/ox_toolkit/ox.c,v 1.3 1999/12/14 09:29:13 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/ox_toolkit/ox.c,v 1.6 1999/12/22 11:26:37 ohara Exp $ */ |
|
|
/* |
/* |
関数の名前付け規約(その2): |
関数の名前付け規約(その2): |
Line 30 YYY_cmo_XXX 関数が処理する. cmo の内部に cmo_ZZZ への |
|
Line 30 YYY_cmo_XXX 関数が処理する. cmo の内部に cmo_ZZZ への |
|
#include <unistd.h> |
#include <unistd.h> |
#include <errno.h> |
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#include <gmp.h> |
|
#include <unistd.h> |
|
#include <sys/file.h> |
#include <sys/file.h> |
|
#include <gmp.h> |
|
|
#include "mysocket.h" |
#include "mysocket.h" |
#include "ox.h" |
#include "ox.h" |
Line 135 static cmo *call_hook_after_send_cmo(int fd, cmo *c) |
|
Line 134 static cmo *call_hook_after_send_cmo(int fd, cmo *c) |
|
return c; |
return c; |
} |
} |
|
|
/* エラーハンドリングのため */ |
/* Handling an error. */ |
static int current_received_serial = 0; |
static int current_received_serial = 0; |
|
|
/* エラーを起こしたときにサーバは次を呼び出す. */ |
/* If an error object be needed, then a server call the following function. */ |
cmo_error2* make_error_object(int err_code, cmo *ob) |
cmo_error2* make_error_object(int err_code, cmo *ob) |
{ |
{ |
cmo_list* li = new_cmo_list(); |
cmo_list* li = new_cmo_list(); |
Line 153 cmo_error2* make_error_object(int err_code, cmo *ob) |
|
Line 152 cmo_error2* make_error_object(int err_code, cmo *ob) |
|
#define DEFAULT_SERIAL_NUMBER 0x0000ffff |
#define DEFAULT_SERIAL_NUMBER 0x0000ffff |
#define receive_serial_number(x) (receive_int32(x)) |
#define receive_serial_number(x) (receive_int32(x)) |
|
|
/* 新しいシリアル番号を得る */ |
/* getting a next serial number. */ |
int next_serial() |
int next_serial() |
{ |
{ |
static int serial_number = DEFAULT_SERIAL_NUMBER; |
static int serial_number = DEFAULT_SERIAL_NUMBER; |
return serial_number++; |
return serial_number++; |
} |
} |
|
|
/* int32 型のオブジェクトを送信する. */ |
/* sending an object of int32 type. (not equal to cmo_int32 type) */ |
int send_int32(int fd, int int32) |
int send_int32(int fd, int int32) |
{ |
{ |
int32 = htonl(int32); |
int32 = htonl(int32); |
return write(fd, &int32, sizeof(int)); |
return write(fd, &int32, sizeof(int)); |
} |
} |
|
|
/* int32 型のオブジェクトを受信する. */ |
/* receiving an object of int32 type. (not equal to cmo_int32 type) */ |
int receive_int32(int fd) |
int receive_int32(int fd) |
{ |
{ |
int tag; |
int tag; |
Line 175 int receive_int32(int fd) |
|
Line 174 int receive_int32(int fd) |
|
return ntohl(tag); |
return ntohl(tag); |
} |
} |
|
|
/* (OX_tag, serial number) を受信する. */ |
/* receiving an (OX_tag, serial number) */ |
int receive_ox_tag(int fd) |
int receive_ox_tag(int fd) |
{ |
{ |
int serial; |
int serial; |
Line 184 int receive_ox_tag(int fd) |
|
Line 183 int receive_ox_tag(int fd) |
|
return tag; |
return tag; |
} |
} |
|
|
/* (OX_tag, serial number) を送信する. */ |
/* sending an (OX_tag, serial number) */ |
int send_ox_tag(int fd, int tag) |
int send_ox_tag(int fd, int tag) |
{ |
{ |
send_int32(fd, tag); |
send_int32(fd, tag); |
return send_int32(fd, next_serial()); |
return send_int32(fd, next_serial()); |
} |
} |
|
|
/* CMO_LIST 関係の関数群 */ |
/* functions for a cmo_list */ |
cell* new_cell() |
cell* new_cell() |
{ |
{ |
cell* h = malloc(sizeof(cell)); |
cell* h = malloc(sizeof(cell)); |
Line 239 int length_cmo_list(cmo_list* this) |
|
Line 238 int length_cmo_list(cmo_list* this) |
|
return this->length; |
return this->length; |
} |
} |
|
|
/** receive_cmo_XXX 関数群 **/ |
/* functions named receive_cmo_*. */ |
static cmo_null* receive_cmo_null(int fd) |
static cmo_null* receive_cmo_null(int fd) |
{ |
{ |
return new_cmo_null(); |
return new_cmo_null(); |
Line 315 static cmo_dms_generic* receive_cmo_dms_generic(int fd |
|
Line 314 static cmo_dms_generic* receive_cmo_dms_generic(int fd |
|
static cmo_ring_by_name* receive_cmo_ring_by_name(int fd) |
static cmo_ring_by_name* receive_cmo_ring_by_name(int fd) |
{ |
{ |
cmo* ob = receive_cmo(fd); |
cmo* ob = receive_cmo(fd); |
/* 意味的チェックが必要 */ |
/* We need to check semantics but yet ... */ |
return new_cmo_ring_by_name(ob); |
return new_cmo_ring_by_name(ob); |
} |
} |
|
|
Line 413 static void resize_mpz(mpz_ptr mpz, int size) |
|
Line 412 static void resize_mpz(mpz_ptr mpz, int size) |
|
mpz->_mp_size = size; |
mpz->_mp_size = size; |
} |
} |
|
|
/** new_cmo_XXX 関数群 **/ |
/* functions named new_cmo_*. */ |
cmo_null* new_cmo_null() |
cmo_null* new_cmo_null() |
{ |
{ |
cmo_null* m = malloc(sizeof(cmo_null)); |
cmo_null* m = malloc(sizeof(cmo_null)); |
Line 499 cmo_zz* new_cmo_zz_set_si(int i) |
|
Line 498 cmo_zz* new_cmo_zz_set_si(int i) |
|
return c; |
return c; |
} |
} |
|
|
|
cmo_zz* new_cmo_zz_set_mpz(mpz_ptr z) |
|
{ |
|
cmo_zz* c = new_cmo_zz(); |
|
mpz_set(c->mpz, z); |
|
return c; |
|
} |
|
|
cmo_zz *new_cmo_zz_set_string(char *s) |
cmo_zz *new_cmo_zz_set_string(char *s) |
{ |
{ |
cmo_zz* c = new_cmo_zz_noinit(); |
cmo_zz* c = new_cmo_zz_noinit(); |
Line 636 void ox_close(ox_file_t sv) |
|
Line 642 void ox_close(ox_file_t sv) |
|
{ |
{ |
send_ox_command(sv->control, SM_control_kill); |
send_ox_command(sv->control, SM_control_kill); |
#ifdef DEBUG |
#ifdef DEBUG |
sleep(2); /* OpenXM server の終了を待つ. あまり意味はない. */ |
sleep(2); |
|
/* We wait thar an OpenXM server terminates. */ |
fprintf(stderr, "I have closed the connection to an Open XM server.\n"); |
fprintf(stderr, "I have closed the connection to an Open XM server.\n"); |
#endif |
#endif |
} |
} |
|
|
void ox_shutdown(ox_file_t sv) |
void ox_shutdown(ox_file_t sv) |
{ |
{ |
/* 後で SM_shutdown を用いるものに書き換える予定. */ |
/* We need to use SM_shutdown but yet ... */ |
ox_close(sv); |
ox_close(sv); |
} |
} |
|
|
Line 669 void ox_push_cmd(ox_file_t sv, int sm_code) |
|
Line 676 void ox_push_cmd(ox_file_t sv, int sm_code) |
|
send_ox_command(sv->stream, sm_code); |
send_ox_command(sv->stream, sm_code); |
} |
} |
|
|
/* ox_mathcap() をコールする. */ |
|
cmo_mathcap* ox_mathcap(ox_file_t sv) |
cmo_mathcap* ox_mathcap(ox_file_t sv) |
{ |
{ |
send_ox_command(sv->stream, SM_mathcap); |
send_ox_command(sv->stream, SM_mathcap); |
Line 706 void ox_push_cmo(ox_file_t sv, cmo *c) |
|
Line 712 void ox_push_cmo(ox_file_t sv, cmo *c) |
|
send_ox_cmo(sv->stream, c); |
send_ox_cmo(sv->stream, c); |
} |
} |
|
|
/* バッファのフラッシュの振りをする. */ |
/* a dummy function for flushing a connection. */ |
int ox_flush(ox_file_t sv) |
int ox_flush(ox_file_t sv) |
{ |
{ |
return 1; |
return 1; |
} |
} |
|
|
/* 手抜き. (後で改善しよう...) */ |
/* a dummy password function. */ |
static char *create_otp() |
static char *create_otp() |
{ |
{ |
static char otp[] = "otpasswd"; |
static char otp[] = "otpasswd"; |
return otp; |
return otp; |
} |
} |
|
|
/* OneTimePassword の処理 */ |
/* proceeding an one time password. */ |
static int login_with_otp(int fd, char* passwd) |
static int login_with_otp(int fd, char* passwd) |
{ |
{ |
int len = strlen(passwd)+1; |
int len = strlen(passwd)+1; |
Line 741 static int login_with_otp(int fd, char* passwd) |
|
Line 747 static int login_with_otp(int fd, char* passwd) |
|
return ret; |
return ret; |
} |
} |
|
|
static int exists_ox(char *dir, char *prog) |
static int chdir_openxm_home_bin() |
{ |
{ |
char *path = alloca(strlen(dir)+strlen(prog)+6); |
char *dir; |
sprintf(path, "%s/%s", dir, prog); |
char *base = getenv("OpenXM_HOME"); |
return access(path, X_OK|R_OK); |
|
|
if (base != NULL) { |
|
dir = alloca(strlen(base)+5); |
|
sprintf(dir, "%s/bin", base); |
|
}else { |
|
dir = "/usr/local/OpenXM/bin"; |
|
} |
|
return chdir(dir); |
} |
} |
|
|
static char *search_ox(char *prog) |
/* example: which("xterm", getenv("PATH")); */ |
|
static char *which(char *prog, char *path_env) |
{ |
{ |
char *env = getenv("OpenXM_HOME"); |
char *tok; |
char *dir; |
char *path; |
if (env != NULL) { |
char delim[] = ":"; |
dir = malloc(strlen(env)+5); |
char *e = alloca(strlen(path_env)+1); |
sprintf(dir, "%s/bin", env); |
strcpy(e, path_env); |
if (exists_ox(dir, prog) == 0) { |
tok = strtok(e, delim); |
return dir; |
while (tok != NULL) { |
} |
char *path = malloc(strlen(tok)+strlen(prog)+2); |
free(dir); |
sprintf(path, "%s/%s", tok, prog); |
} |
if (access(path, X_OK&R_OK) == 0) { |
dir = "/usr/local/OpenXM/bin"; |
return path; |
if (exists_ox(dir, prog) == 0) { |
} |
return dir; |
free(path); |
} |
tok = strtok(NULL, delim); |
dir = "."; |
} |
if (exists_ox(dir, prog) == 0) { |
return NULL; |
return dir; |
|
} |
|
return NULL; |
|
} |
} |
|
|
static int mysocketAccept2(int fd, char *pass) |
static int mysocketAccept2(int fd, char *pass) |
Line 782 static int mysocketAccept2(int fd, char *pass) |
|
Line 793 static int mysocketAccept2(int fd, char *pass) |
|
return -1; |
return -1; |
} |
} |
|
|
|
/* 0 でなければ、oxlog を介して ox を起動する。*/ |
|
static int flag_ox_start_with_oxlog = 1; |
|
|
/* |
/* |
(-reverse 版の ox_start) |
(-reverse 版の ox_start) |
ox_start は クライアントが呼び出すための関数である. |
ox_start は クライアントが呼び出すための関数である. |
Line 793 static int mysocketAccept2(int fd, char *pass) |
|
Line 807 static int mysocketAccept2(int fd, char *pass) |
|
|
|
ox_file_t ox_start(char* host, char* ctl_prog, char* dat_prog) |
ox_file_t ox_start(char* host, char* ctl_prog, char* dat_prog) |
{ |
{ |
|
ox_file_t sv = NULL; |
char *pass; |
char *pass; |
char ctl[16], dat[16]; |
char ctl[128], dat[128]; |
short portControl = 0; /* short であることに注意 */ |
short portControl = 0; /* short であることに注意 */ |
short portStream = 0; |
short portStream = 0; |
ox_file_t sv = NULL; |
|
char *dir; |
char *dir; |
char *oxlog = "oxlog"; |
char *oxlog = "oxlog"; |
|
|
if ((dir = search_ox(ctl_prog)) == NULL) { |
|
fprintf(stderr, "client:: %s not found.\n", ctl_prog); |
|
return NULL; |
|
} |
|
|
|
sv = malloc(sizeof(__ox_file_struct)); |
sv = malloc(sizeof(__ox_file_struct)); |
sv->control = mysocketListen(host, &portControl); |
sv->control = mysocketListen(host, &portControl); |
sv->stream = mysocketListen(host, &portStream); |
sv->stream = mysocketListen(host, &portStream); |
Line 815 ox_file_t ox_start(char* host, char* ctl_prog, char* d |
|
Line 824 ox_file_t ox_start(char* host, char* ctl_prog, char* d |
|
pass = create_otp(); |
pass = create_otp(); |
|
|
if (fork() == 0) { |
if (fork() == 0) { |
dup2(2, 1); |
chdir_openxm_home_bin(); |
dup2(open(DEFAULT_LOGFILE, O_RDWR|O_CREAT|O_TRUNC, 0644), 2); |
if (flag_ox_start_with_oxlog) { |
chdir(dir); |
execl(oxlog, oxlog, "xterm", "-icon", "-e", ctl_prog, |
execl(oxlog, oxlog, "/usr/X11R6/bin/xterm", "-icon", "-e", |
"-reverse", "-ox", dat_prog, |
ctl_prog, "-reverse", "-ox", dat_prog, |
"-data", dat, "-control", ctl, "-pass", pass, |
"-data", dat, "-control", ctl, "-pass", pass, |
"-host", host, NULL); |
"-host", host, NULL); |
}else { |
|
dup2(2, 1); |
|
dup2(open(tempnam("/tmp", "ox."), O_RDWR|O_CREAT|O_TRUNC, 0644), 2); |
|
execl(ctl_prog, ctl_prog, "-reverse", "-ox", dat_prog, |
|
"-data", dat, "-control", ctl, "-pass", pass, |
|
"-host", host, NULL); |
|
} |
|
exit(1); |
} |
} |
|
|
if ((sv->control = mysocketAccept2(sv->control, pass)) == -1) { |
if ((sv->control = mysocketAccept2(sv->control, pass)) == -1) { |
Line 836 ox_file_t ox_start(char* host, char* ctl_prog, char* d |
|
Line 852 ox_file_t ox_start(char* host, char* ctl_prog, char* d |
|
return sv; |
return sv; |
} |
} |
|
|
|
/* ssh -f host oxlog xterm -e ox -ox ox_asir ... */ |
|
int ssh_ox_server(char *host, char *ctl_prog, char *dat_prog, short portControl, short portStream) |
|
{ |
|
if (fork() == 0) { |
|
execl("ssh", "ssh", "-f", host, "oxlog", "xterm", "-icon", |
|
"-e", ctl_prog, "-insecure", "-ox", dat_prog, |
|
"-data", portStream, "-control", portControl, "-host", host, NULL); |
|
exit(1); |
|
} |
|
} |
|
|
/* |
/* |
(-insecure 版の ox_start) まだ、中身はありません。 |
(-insecure 版の ox_start) まだ、中身はありません。 |
ox_start_insecure_nonreverse は クライアントが呼び出すための関数である. |
ox_start_insecure_nonreverse は クライアントが呼び出すための関数である. |
Line 861 ox_file_t ox_start_insecure_nonreverse(char* host, sho |
|
Line 888 ox_file_t ox_start_insecure_nonreverse(char* host, sho |
|
return sv; |
return sv; |
} |
} |
|
|
|
ox_file_t ox_start_insecure_nonreverse2(char* host, char *ctl_prog, char *dat_prog) |
|
{ |
|
short portControl= 1200; /* 自動生成させよう... */ |
|
short portStream = 1300; |
|
|
|
ssh_ox_server(host, ctl_prog, dat_prog, portControl, portStream); |
|
return ox_start_insecure_nonreverse(host, portControl, portStream); |
|
} |
|
|
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 1304 static cmo_list* make_list_of_id(int ver, char* ver_s, |
|
Line 1340 static cmo_list* make_list_of_id(int ver, char* ver_s, |
|
cmo_list *cap; |
cmo_list *cap; |
char buff[512]; |
char buff[512]; |
|
|
setgetc(mygetc); |
|
sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE")); |
sprintf(buff, ID_TEMP, ver, sysname, ver_s, getenv("HOSTTYPE")); |
setmode_mygetc(buff, 512); |
init_parser(buff); |
cap = (cmo_list *)parse(); |
cap = (cmo_list *)parse(); |
resetgetc(); |
|
|
|
return cap; |
return cap; |
} |
} |