[BACK]Return to oxf_old.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Annotation of OpenXM/src/ox_toolkit/oxf_old.c, Revision 1.9

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.9     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/oxf_old.c,v 1.8 2009/03/20 18:41:16 iwane Exp $ */
1.1       ohara       3:
                      4: /* このモジュールは互換性のためのものです。*/
                      5:
                      6: #include <stdio.h>
                      7: #include <stdlib.h>
                      8: #include <string.h>
1.9     ! ohara       9: #include <fcntl.h>
        !            10: #include <time.h>
        !            11:
        !            12: #if defined(_MSC_VER) || defined(__MINGW32__)
        !            13: #include <io.h>
        !            14: #define MAXHOSTNAMELEN 256
        !            15: #else
1.1       ohara      16: #include <unistd.h>
                     17: #include <sys/file.h>
                     18: #include <sys/param.h>
1.9     ! ohara      19: #endif
1.4       ohara      20:
                     21: #if defined(__sun__)
                     22: #include <netdb.h>
                     23: #include <sys/types.h>
                     24: #include <netinet/in.h>
                     25: #endif
1.1       ohara      26:
                     27: #include "mysocket.h"
                     28: #include "ox_toolkit.h"
                     29:
                     30: OXFILE *oxf_control_set(OXFILE *oxfp, OXFILE *ctl)
                     31: {
                     32:        oxfp->control = ctl;
                     33:        return oxfp;
                     34: }
                     35:
1.5       ohara      36: static char *OpenXM_HOME = "/usr/local/OpenXM";
1.1       ohara      37: static char *concat_openxm_home_bin(char *s);
                     38:
                     39: OXFILE *         ox_start(char* host, char* prog1, char* prog2);
                     40: OXFILE *         ox_start_insecure_nonreverse(char* host, short portControl, short portStream);
                     41: OXFILE *         ox_start_remote_with_ssh(char *dat_prog, char* host);
                     42:
                     43: #if 0
                     44: /* 将来計画: start 関数は次のような設計であるべきだろう */
                     45:
                     46: OXFILE *oxf_connect_client()
                     47: {
                     48:     short port   = 0; /* Listen() に決めさせる. */
                     49:     int listened = Listen(&port);
                     50:
                     51:     execute_server(port);
                     52:     return oxf_connect_passive(listened); /* ここで、認証, バイトオーダの決定 */
                     53: }
                     54:
                     55: OXFILE *ox_start_calc(char *cserver)
                     56: {
                     57:     short port   = 0; /* Listen() に決めさせる. */
                     58:     int listened;
                     59:
                     60:     listened = Listen(&port);
                     61:     oxc_open(oxfp_ctl, port, cserver); /* ここでpasswordを送る手もあるが... */
                     62:     return oxf_connect_passive(listened); /* 認証しない. バイトオーダは決定する. */
                     63:
                     64: }
                     65:
                     66: int ox_start()
                     67: {
                     68:     OXFILE *oxfp_ctl, *oxfp_calc;
                     69:
                     70:     oxfp_ctl = oxf_connect_client();
                     71:     /* mathcap の交換 */
                     72:     oxfp_calc = ox_start_calc("ox_sm1");
                     73:     return oxf_control_set(oxfp_calc, oxfp_ctl);
                     74: }
                     75: #endif
                     76:
                     77: static OXFILE *mysocketAccept2(int listened, char *passwd)
                     78: {
                     79:     OXFILE *oxfp = oxf_connect_passive(listened);
                     80:     if(oxf_confirm_client(oxfp, passwd)) {
                     81:         oxf_determine_byteorder_client(oxfp);
                     82:         return oxfp;
                     83:     }
                     84:     oxf_close(oxfp);
                     85:     return NULL;
                     86: }
                     87:
                     88: /* The environment variable OpenXM_HOME must be defined. */
                     89: static char *concat_openxm_home_bin(char *s)
                     90: {
                     91:     char *path;
                     92:
                     93:     /* if s includes '/' then it is not concaticated. */
                     94:     if (strchr(s, '/') != NULL) {
                     95:         return s;
                     96:     }
                     97:
1.6       ohara      98:     path = MALLOC(strlen(OpenXM_HOME)+6+strlen(s));
1.5       ohara      99:     sprintf(path, "%s/bin/%s", OpenXM_HOME, s);
1.1       ohara     100:     return path;
                    101: }
                    102:
                    103: void set_OpenXM_HOME()
                    104: {
1.5       ohara     105:        char *e;
                    106:        if ((e = getenv("OpenXM_HOME")) != NULL
                    107: #if defined(__CYGWIN__)
                    108:                || (e = getenv("OPENXM_HOME")) != NULL
                    109: #endif
                    110:                ) {
                    111:                OpenXM_HOME = e;
1.1       ohara     112:     }
                    113: }
                    114:
1.2       ohara     115: void ox_exec_local(char* ctl_prog, char* dat_prog, int portControl, int portStream, char *passwd)
1.1       ohara     116: {
                    117:     char  ctl[128], dat[128];
1.2       ohara     118:     char localhost[MAXHOSTNAMELEN];
                    119:
1.1       ohara     120:     sprintf(ctl, "%d", portControl);
                    121:     sprintf(dat, "%d", portStream);
                    122:
                    123:     set_OpenXM_HOME();
                    124:     ctl_prog = concat_openxm_home_bin(ctl_prog);
                    125:     dat_prog = concat_openxm_home_bin(dat_prog);
                    126:
1.2       ohara     127:     if (gethostname(localhost, MAXHOSTNAMELEN)==0) {
                    128:                if (fork() == 0) {
                    129:                        execlp("oxlog", "oxlog", "xterm", "-icon", "-e", ctl_prog,
                    130:                                   "-reverse", "-ox", dat_prog,
                    131:                                   "-data", dat, "-control", ctl, "-pass", passwd,
                    132:                                   "-host", localhost, NULL);
                    133:                        exit(1);
                    134:                }
                    135:        }
1.1       ohara     136: }
                    137:
                    138: /*
                    139:    (-reverse 版の ox_start)
                    140:    ox_start は クライアントが呼び出すための関数である.
                    141:    サーバでは使われない.  ctl_prog はコントロールサーバであり,
                    142:    -ox, -reverse, -data, -control, -pass, -host
                    143:    というオプションを理解することを仮定する. dat_prog は計算サーバである.
                    144:    接続時には, ct を先にオープンする.
                    145: */
                    146:
                    147: OXFILE *ox_start(char* host, char* ctl_prog, char* dat_prog)
                    148: {
                    149:     OXFILE *st, *ct;
                    150:     char *passwd = generate_otp();
                    151:     int   listen[2];
1.7       ohara     152:     int   ports[2] = {0};
1.1       ohara     153:
1.2       ohara     154:        /* host を無視 */
                    155:     listen[0] = oxf_listen(&ports[0]);
                    156:     listen[1] = oxf_listen(&ports[1]);
1.1       ohara     157:
1.2       ohara     158:     ox_exec_local(ctl_prog, dat_prog, ports[0], ports[1], passwd);
1.1       ohara     159:     ct = mysocketAccept2(listen[0], passwd);
                    160:     if (ct != NULL) {
                    161:         usleep(10);   /* zzz... */
                    162:         st = mysocketAccept2(listen[1], passwd);
                    163:         if (st != NULL) {
                    164:             return oxf_control_set(st, ct);
                    165:         }
                    166:     }
                    167:     return NULL;
                    168: }
                    169:
                    170: /*
                    171:    (-insecure 版の ox_start)  まだ、中身はありません。
                    172:    ox_start_insecure は クライアントが呼び出すための関数である.
                    173:    接続時には, ct を先にオープンする.
                    174:    既定値:
                    175:    portControl = 1200
                    176:    portStream  = 1300
                    177: */
                    178:
                    179: OXFILE *ox_start_insecure(char* host, short portControl, short portStream)
                    180: {
                    181:     OXFILE *ct, *st;
                    182:     ct = oxf_connect_active(host, portControl);
                    183:     /* ox は insecure のとき byte order の決定が正しくできないようだ... */
                    184:     /* oxf_determine_byteorder_client(ct); */
                    185:     /* wainting 10 micro second. */
                    186:     usleep(10);
                    187:     st = oxf_connect_active(host, portStream);
                    188:     oxf_determine_byteorder_client(st);
                    189:     return oxf_control_set(st, ct);
                    190: }
                    191:
                    192: /* ssh -f host oxlog xterm -e ox -ox ox_asir ... */
                    193: void ssh_ox_server(char *remote_host, char *ctl_prog, char *dat_prog, short portControl, short portStream)
                    194: {
1.8       iwane     195:     char buf[2][10];
                    196: /*
1.1       ohara     197:     ctl_prog = concat_openxm_home_bin(ctl_prog);
                    198:     dat_prog = concat_openxm_home_bin(dat_prog);
1.8       iwane     199: */
1.1       ohara     200:
                    201:     if (fork() == 0) {
1.8       iwane     202:
                    203:         sprintf(buf[0], "%hd", portStream);
                    204:         sprintf(buf[1], "%hd", portControl);
                    205:
1.1       ohara     206:         execlp("ssh", "ssh", "-f", remote_host, "oxlog", "xterm", "-icon",
                    207:               "-e", ctl_prog, "-insecure", "-ox", dat_prog,
1.8       iwane     208:               "-data", buf[0], "-control", buf[1],
1.1       ohara     209:               "-host", remote_host, NULL);
                    210:         exit(1);
                    211:     }
1.8       iwane     212:
                    213:     sleep(1); /* wait ssh */
1.1       ohara     214: }
                    215:
                    216: OXFILE *ox_start_remote_with_ssh(char *dat_prog, char* remote_host)
                    217: {
                    218:     ssh_ox_server(remote_host, "ox", dat_prog, 1200, 1300);
                    219:     return ox_start_insecure(remote_host, 1200, 1300);
                    220: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>