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

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

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