[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.10

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.10    ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/oxf_old.c,v 1.9 2015/08/27 03:03:34 ohara 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.10    ! ohara     115: #if !defined(_MSC_VER)
1.2       ohara     116: void ox_exec_local(char* ctl_prog, char* dat_prog, int portControl, int portStream, char *passwd)
1.1       ohara     117: {
                    118:     char  ctl[128], dat[128];
1.2       ohara     119:     char localhost[MAXHOSTNAMELEN];
                    120:
1.1       ohara     121:     sprintf(ctl, "%d", portControl);
                    122:     sprintf(dat, "%d", portStream);
                    123:
                    124:     set_OpenXM_HOME();
                    125:     ctl_prog = concat_openxm_home_bin(ctl_prog);
                    126:     dat_prog = concat_openxm_home_bin(dat_prog);
                    127:
1.2       ohara     128:     if (gethostname(localhost, MAXHOSTNAMELEN)==0) {
                    129:                if (fork() == 0) {
                    130:                        execlp("oxlog", "oxlog", "xterm", "-icon", "-e", ctl_prog,
                    131:                                   "-reverse", "-ox", dat_prog,
                    132:                                   "-data", dat, "-control", ctl, "-pass", passwd,
                    133:                                   "-host", localhost, NULL);
                    134:                        exit(1);
                    135:                }
                    136:        }
1.1       ohara     137: }
                    138:
                    139: /*
                    140:    (-reverse 版の ox_start)
                    141:    ox_start は クライアントが呼び出すための関数である.
                    142:    サーバでは使われない.  ctl_prog はコントロールサーバであり,
                    143:    -ox, -reverse, -data, -control, -pass, -host
                    144:    というオプションを理解することを仮定する. dat_prog は計算サーバである.
                    145:    接続時には, ct を先にオープンする.
                    146: */
                    147:
                    148: OXFILE *ox_start(char* host, char* ctl_prog, char* dat_prog)
                    149: {
                    150:     OXFILE *st, *ct;
                    151:     char *passwd = generate_otp();
                    152:     int   listen[2];
1.7       ohara     153:     int   ports[2] = {0};
1.1       ohara     154:
1.2       ohara     155:        /* host を無視 */
                    156:     listen[0] = oxf_listen(&ports[0]);
                    157:     listen[1] = oxf_listen(&ports[1]);
1.1       ohara     158:
1.2       ohara     159:     ox_exec_local(ctl_prog, dat_prog, ports[0], ports[1], passwd);
1.1       ohara     160:     ct = mysocketAccept2(listen[0], passwd);
                    161:     if (ct != NULL) {
                    162:         usleep(10);   /* zzz... */
                    163:         st = mysocketAccept2(listen[1], passwd);
                    164:         if (st != NULL) {
                    165:             return oxf_control_set(st, ct);
                    166:         }
                    167:     }
                    168:     return NULL;
                    169: }
                    170:
                    171: /*
                    172:    (-insecure 版の ox_start)  まだ、中身はありません。
                    173:    ox_start_insecure は クライアントが呼び出すための関数である.
                    174:    接続時には, ct を先にオープンする.
                    175:    既定値:
                    176:    portControl = 1200
                    177:    portStream  = 1300
                    178: */
                    179:
                    180: OXFILE *ox_start_insecure(char* host, short portControl, short portStream)
                    181: {
                    182:     OXFILE *ct, *st;
                    183:     ct = oxf_connect_active(host, portControl);
                    184:     /* ox は insecure のとき byte order の決定が正しくできないようだ... */
                    185:     /* oxf_determine_byteorder_client(ct); */
                    186:     /* wainting 10 micro second. */
                    187:     usleep(10);
                    188:     st = oxf_connect_active(host, portStream);
                    189:     oxf_determine_byteorder_client(st);
                    190:     return oxf_control_set(st, ct);
                    191: }
                    192:
                    193: /* ssh -f host oxlog xterm -e ox -ox ox_asir ... */
                    194: void ssh_ox_server(char *remote_host, char *ctl_prog, char *dat_prog, short portControl, short portStream)
                    195: {
1.8       iwane     196:     char buf[2][10];
                    197: /*
1.1       ohara     198:     ctl_prog = concat_openxm_home_bin(ctl_prog);
                    199:     dat_prog = concat_openxm_home_bin(dat_prog);
1.8       iwane     200: */
1.1       ohara     201:
                    202:     if (fork() == 0) {
1.8       iwane     203:
                    204:         sprintf(buf[0], "%hd", portStream);
                    205:         sprintf(buf[1], "%hd", portControl);
                    206:
1.1       ohara     207:         execlp("ssh", "ssh", "-f", remote_host, "oxlog", "xterm", "-icon",
                    208:               "-e", ctl_prog, "-insecure", "-ox", dat_prog,
1.8       iwane     209:               "-data", buf[0], "-control", buf[1],
1.1       ohara     210:               "-host", remote_host, NULL);
                    211:         exit(1);
                    212:     }
1.8       iwane     213:
                    214:     sleep(1); /* wait ssh */
1.1       ohara     215: }
                    216:
                    217: OXFILE *ox_start_remote_with_ssh(char *dat_prog, char* remote_host)
                    218: {
                    219:     ssh_ox_server(remote_host, "ox", dat_prog, 1200, 1300);
                    220:     return ox_start_insecure(remote_host, 1200, 1300);
                    221: }
1.10    ! ohara     222: #endif

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