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

Annotation of OpenXM/src/ox_toolkit/oxf.c, Revision 1.1

1.1     ! ohara       1: /* -*- mode: C; coding: euc-japan -*- */
        !             2: /* $OpenXM$ */
        !             3:
        !             4: /*
        !             5:    This module includes functions for sending/receiveng CMO's.
        !             6:    Some commnets is written in Japanese by the EUC-JP coded
        !             7:    character set.
        !             8: */
        !             9:
        !            10: #include <stdio.h>
        !            11: #include <stdlib.h>
        !            12: #include <string.h>
        !            13: #include <unistd.h>
        !            14: #include <errno.h>
        !            15: #include <fcntl.h>
        !            16: #include <sys/file.h>
        !            17: #include <sys/param.h>
        !            18: #include <time.h>
        !            19:
        !            20: #include "mysocket.h"
        !            21: #include "ox_toolkit.h"
        !            22:
        !            23: OXFILE *oxf_open(int fd)
        !            24: {
        !            25:     OXFILE *oxfp = (OXFILE *)malloc(sizeof(OXFILE));
        !            26:     oxfp->fd = fd;
        !            27:     oxfp->send_int32    = send_int32_nbo;
        !            28:     oxfp->receive_int32 = receive_int32_nbo;
        !            29:        oxfp->control = NULL;
        !            30:     return oxfp;
        !            31:     /* oxfp->fp = fdopen(fd, "a+"); */
        !            32:     /* return (oxfp->fp != NULL)? oxfp: NULL; */
        !            33: }
        !            34:
        !            35: OXFILE *oxf_control(OXFILE *oxfp)
        !            36: {
        !            37:        return oxfp->control;
        !            38: }
        !            39:
        !            40: /* The function determines a byte order of integer on the OpenXM
        !            41:    connection `oxfp'. */
        !            42: /* REMARKS:
        !            43:    we request the byte order of macine integer on a local machine by
        !            44:    (*(char *)&offer).  The fact depends on OX_BYTE_LITTLE_ENDIAN==1. */
        !            45: void oxf_determine_byteorder_client(OXFILE *oxfp)
        !            46: {
        !            47:     int  offer = OX_BYTE_LITTLE_ENDIAN;
        !            48:     char receiv;
        !            49:     int  mode;
        !            50:
        !            51:     oxf_read(&receiv, 1, 1, oxfp);
        !            52:     oxf_write(&offer, 1, 1, oxfp);
        !            53:     mode = (receiv == *(char *)&offer);
        !            54:     oxf_setopt(oxfp, mode);
        !            55: }
        !            56:
        !            57: /* Server 側ではこちらを用いる */
        !            58: /* いまの実装は dup されていることが前提になっている */
        !            59: void oxf_determine_byteorder_server(OXFILE *oxfp)
        !            60: {
        !            61:     int  offer = OX_BYTE_LITTLE_ENDIAN;
        !            62:     char receiv;
        !            63:     int  mode;
        !            64:
        !            65:     oxf_write(&offer, 1, 1, oxfp);
        !            66:     oxf_read(&receiv, 1, 1, oxfp);
        !            67:     mode = (receiv == *(char *)&offer);
        !            68:     oxf_setopt(oxfp, mode);
        !            69: }
        !            70:
        !            71: void oxf_flush(OXFILE *oxfp)
        !            72: {
        !            73:     /* fflush(oxfp->fp); */
        !            74: }
        !            75:
        !            76: void oxf_close(OXFILE *oxfp)
        !            77: {
        !            78:     close(oxfp->fd);
        !            79:     /* fclose(oxfp->fp); */
        !            80: }
        !            81:
        !            82: #define OXF_SETOPT_NBO  0
        !            83: #define OXF_SETOPT_LBO  1
        !            84:
        !            85: void oxf_setopt(OXFILE *oxfp, int mode)
        !            86: {
        !            87:     if (mode == OXF_SETOPT_LBO) {
        !            88:         oxfp->send_int32    = send_int32_lbo;
        !            89:         oxfp->receive_int32 = receive_int32_lbo;
        !            90:     }else if (mode == OXF_SETOPT_NBO) {
        !            91:         oxfp->send_int32    = send_int32_nbo;
        !            92:         oxfp->receive_int32 = receive_int32_nbo;
        !            93:     }
        !            94: }
        !            95:
        !            96: OXFILE *oxf_connect_active(char *hostname, short port)
        !            97: {
        !            98:     int fd = mysocketOpen(hostname, port);
        !            99:     return oxf_open(fd);
        !           100: }
        !           101:
        !           102: OXFILE *oxf_connect_passive(int listened)
        !           103: {
        !           104:     int fd = mysocketAccept(listened);
        !           105:     return oxf_open(fd);
        !           106: }
        !           107:
        !           108: #define LENGTH_OF_ONETIME_PASSWORD 64
        !           109:
        !           110: /* a password generator. */
        !           111: char *generate_otp()
        !           112: {
        !           113:        static char crypto[] = "%.,^_+-=/@0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        !           114:     static char otp[LENGTH_OF_ONETIME_PASSWORD+1] = {0};
        !           115:     int i;
        !           116:
        !           117:     srandom(time(NULL));
        !           118:     for(i=0; i<LENGTH_OF_ONETIME_PASSWORD; i++) {
        !           119:         otp[i] = crypto[random() % (sizeof(crypto)-1)];
        !           120:     }
        !           121:     return otp;
        !           122: }
        !           123:
        !           124: /* proceeding a one time password. */
        !           125: /* if the password is right,
        !           126:    then the function returns 1, if otherwise, then 0. */
        !           127: int oxf_confirm_client(OXFILE *oxfp, char *passwd)
        !           128: {
        !           129:     int len = strlen(passwd)+1;
        !           130:     char *buf = alloca(len);
        !           131:
        !           132:     oxf_read(buf, 1, len, oxfp);
        !           133:     return !strcmp(passwd, buf);
        !           134: }
        !           135:
        !           136: int oxf_confirm_server(OXFILE *oxfp, char *passwd)
        !           137: {
        !           138:     return oxf_write(passwd, 1, strlen(passwd)+1, oxfp);
        !           139: }
        !           140:
        !           141: /* example: which("xterm", getenv("PATH")); */
        !           142: char *which(char *exe, const char *env)
        !           143: {
        !           144:     char *tok;
        !           145:     char *path;
        !           146:     char delim[] = ":";
        !           147:     char *e = alloca(strlen(env)+1);
        !           148:     strcpy(e, env);
        !           149:     tok = strtok(e, delim);
        !           150:     while (tok != NULL) {
        !           151:         path = malloc(strlen(tok)+strlen(exe)+2);
        !           152:         sprintf(path, "%s/%s", tok, exe);
        !           153:         if (access(path, X_OK&R_OK) == 0) {
        !           154:             return path;
        !           155:         }
        !           156:         free(path);
        !           157:         tok = strtok(NULL, delim);
        !           158:     }
        !           159:     return NULL;
        !           160: }
        !           161:
        !           162: /* Remarks: ssh determines remote host by his name, i.e. by arg[0]. */
        !           163: int oxc_start(char *remote_host, short port, char *passwd)
        !           164: {
        !           165:     char localhost[MAXHOSTNAMELEN];
        !           166:     char ports[128];
        !           167:     int pid = 0;
        !           168:     char *cmd = "echo";
        !           169: /*  char *cmd = "oxc"; */
        !           170:
        !           171:     if (gethostname(localhost, MAXHOSTNAMELEN)==0) {
        !           172:         if ((pid = fork()) == 0) {
        !           173:             sprintf(ports, "%d", port);
        !           174: #ifdef DEBUG
        !           175:             fprintf(stderr, "oxf.c:: oxc_start() does %s(ssh) -f %s -h %s -p %s -c %s\n", remote_host, cmd, localhost, ports, passwd);
        !           176: #endif
        !           177:             execlp("ssh", remote_host, "-f", cmd,
        !           178:                   "-h", localhost, "-p", ports,"-c", passwd, NULL);
        !           179:         }
        !           180:     }
        !           181:     return pid;
        !           182: }

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