[BACK]Return to sio.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/sio.c, Revision 1.2

1.2     ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/io/sio.c,v 1.1.1.1 1999/12/03 07:39:11 noro Exp $ */
1.1       noro        2: #if INET
                      3: #include "ca.h"
                      4: #include "ox.h"
                      5: #if defined(VISUAL)
                      6: #include <winsock.h>
                      7: #else
                      8: #include <sys/time.h>
                      9: #include <sys/uio.h>
                     10: #include <sys/ioctl.h>
                     11: #include <sys/un.h>
                     12: #endif
                     13: #include<signal.h>
                     14:
                     15: #define SOCKQUEUELENGTH 5
                     16: #define ISIZ sizeof(int)
                     17:
                     18: extern int little_endian;
                     19:
                     20: struct IOFP iofp[MAXIOFP];
                     21:
                     22: #if !defined(_PA_RISC1_1)
                     23: #define RSH "rsh"
                     24: #else
                     25: #define RSH "remsh"
                     26: #endif
                     27:
                     28: void init_socket(void);
                     29:
                     30: int getremotesocket(s)
                     31: int s;
                     32: {
                     33:        return iofp[s].s;
                     34: }
                     35:
                     36: void getremotename(s,name)
                     37: int s;
                     38: char *name;
                     39: {
                     40:        struct sockaddr_in peer;
                     41:        struct hostent *hp;
                     42:        int peerlen;
                     43:
                     44:        peerlen = sizeof(peer);
                     45:        getpeername(getremotesocket(s),(struct sockaddr *)&peer,&peerlen);
                     46:        hp = gethostbyaddr((char *)&peer.sin_addr,sizeof(struct in_addr),AF_INET);
                     47:        if ( hp )
                     48:                strcpy(name,hp->h_name);
                     49:        else
                     50:                strcpy(name,(char *)inet_ntoa(peer.sin_addr));
                     51: }
                     52:
                     53: int generate_port(use_unix,port_str)
                     54: int use_unix;
                     55: char *port_str;
                     56: {
                     57:        double get_current_time();
                     58:        unsigned long mt_genrand();
                     59:        unsigned int port;
                     60:        static int count=0;
                     61:
                     62:        if ( use_unix ) {
                     63:                sprintf(port_str,"/tmp/ox%02x.XXXXXX",count);
                     64:                count++;
                     65:                mktemp(port_str);
                     66:        } else {
                     67:                port = ((unsigned int)mt_genrand()+(unsigned int)get_current_time())
                     68:                        %(65536-1024)+1024;
                     69:                sprintf(port_str,"%d",port);
                     70:        }
                     71: }
                     72:
                     73: int try_bind_listen(use_unix,port_str)
                     74: int use_unix;
                     75: char *port_str;
                     76: {
                     77:        struct sockaddr_in sin;
                     78:        struct sockaddr *saddr;
                     79:        int len;
                     80:        int service;
1.2     ! noro       81: #if !defined(VISUAL)
        !            82:        struct sockaddr_un s_un;
1.1       noro       83:
                     84:        if ( use_unix ) {
                     85:                service = socket(AF_UNIX, SOCK_STREAM, 0);
                     86:                if (service < 0) {
                     87:                        perror("in socket");
                     88:                        return -1;
                     89:                }
                     90:                s_un.sun_family = AF_UNIX;
                     91:                strcpy(s_un.sun_path,port_str);
                     92: #if defined(__FreeBSD__)
                     93:                len = SUN_LEN(&s_un);
                     94:                s_un.sun_len = len+1; /* XXX */
                     95: #else
                     96:                len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
                     97: #endif
                     98:                saddr = (struct sockaddr *)&s_un;
1.2     ! noro       99:        } else
        !           100: #endif
        !           101:        {
1.1       noro      102:                service = socket(AF_INET, SOCK_STREAM, 0);
                    103:                if ( service < 0 ) {
                    104:                        perror("in socket");
                    105:                        return -1;
                    106:                }
                    107:                sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY;
                    108:                sin.sin_port = htons(atoi(port_str));
                    109:                len = sizeof(sin);
                    110:                saddr = (struct sockaddr *)&sin;
                    111:        }
                    112:        if (bind(service, saddr, len) < 0) {
                    113:                perror("in bind");
                    114:                close(service);
                    115:                return -1;
                    116:        }
                    117:        if (getsockname(service,saddr, &len) < 0) {
                    118:            perror("in getsockname");
                    119:            close(service);
                    120:            return -1;
                    121:        }
                    122:        if (listen(service, SOCKQUEUELENGTH) < 0) {
                    123:                perror("in listen");
                    124:                close(service);
                    125:                return -1;
                    126:        }
                    127:        return service;
                    128: }
                    129:
                    130: /*
                    131:   try to accept a connection request
                    132:
                    133:   Input
                    134:     af_unix: s is UNIX domain socket if af_unix is nonzero
                    135:     s: socket
                    136:
                    137:   Output
                    138:     c: an accepted socket which is newly created
                    139:     -1: if failed to accept
                    140:
                    141:   the original socket is always closed.
                    142: */
                    143:
                    144: int try_accept(af_unix,s)
                    145: int af_unix,s;
                    146: {
                    147:        int len,c,i;
                    148:        struct sockaddr_in sin;
                    149:
1.2     ! noro      150: #if !defined(VISUAL)
        !           151:        struct sockaddr_un s_un;
1.1       noro      152:        if ( af_unix ) {
                    153:                len = sizeof(s_un);
                    154:                for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )
                    155:                        c = accept(s, (struct sockaddr *) &s_un, &len);
1.2     ! noro      156:        } else
        !           157: #endif
        !           158:        {
1.1       noro      159:
                    160:                len = sizeof(sin);
                    161:                for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )
                    162:                        c = accept(s, (struct sockaddr *) &sin, &len);
                    163:        }
                    164:        if ( i == 10 )
                    165:                c = -1;
                    166:        close(s);
                    167:        return c;
                    168: }
                    169:
                    170: int try_connect(use_unix,host,port_str)
                    171: int use_unix;
                    172: char *host,*port_str;
                    173: {
                    174:        struct sockaddr_in sin;
                    175:        struct sockaddr *saddr;
                    176:        struct hostent *hp;
                    177:        int len,s,i;
1.2     ! noro      178: #if !defined(VISUAL)
        !           179:        struct sockaddr_un s_un;
        !           180: #endif
1.1       noro      181:
                    182:        for ( i = 0; i < 10; i++ ) {
1.2     ! noro      183: #if !defined(VISUAL)
1.1       noro      184:                if ( use_unix ) {
                    185:                        if ( (s = socket(AF_UNIX,SOCK_STREAM,0)) < 0 ) {
                    186:                                perror("socket");
                    187:                                return -1;
                    188:                        }
                    189:                        bzero(&s_un,sizeof(s_un));
                    190:                        s_un.sun_family = AF_UNIX;
                    191:                        strcpy(s_un.sun_path,port_str);
                    192: #if defined(__FreeBSD__)
                    193:                        len = SUN_LEN(&s_un);
                    194:                        s_un.sun_len = len+1; /* XXX */
                    195: #else
                    196:                        len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
                    197: #endif
                    198:                        saddr = (struct sockaddr *)&s_un;
1.2     ! noro      199:                } else
        !           200: #endif /* VISUAL */
        !           201:                {
1.1       noro      202:                        if ( (s = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
                    203:                                perror("socket");
                    204:                                return -1;
                    205:                        }
                    206:                        bzero(&sin,sizeof(sin));
                    207:                        sin.sin_port = htons(atoi(port_str));
                    208:                        sin.sin_addr.s_addr = inet_addr(host);
                    209:                        if ( sin.sin_addr.s_addr != -1 ) {
                    210:                                sin.sin_family = AF_INET;
                    211:                        } else {
                    212:                                hp = gethostbyname(host);
                    213:                                bcopy(hp->h_addr,&sin.sin_addr,hp->h_length);
                    214:                                sin.sin_family = hp->h_addrtype;
                    215:                        }
                    216:                        len = sizeof(sin);
                    217:                        saddr = (struct sockaddr *)&sin;
                    218:                }
                    219:                if ( connect(s,saddr,len) >= 0 )
                    220:                        break;
                    221:                else {
                    222:                        close(s);
1.2     ! noro      223: #if defined(VISUAL)
        !           224:                        Sleep(100);
        !           225: #else
1.1       noro      226:                        usleep(100000);
1.2     ! noro      227: #endif
1.1       noro      228:                }
                    229:        }
                    230:        if ( i == 10 ) {
                    231:                perror("connect");
                    232:                return -1;
                    233:        } else
                    234:                return s;
                    235: }
                    236:
                    237: close_allconnections()
                    238: {
                    239:        int s;
                    240:
                    241: #if defined(SIGPIPE)
                    242:        signal(SIGPIPE,SIG_IGN);
                    243: #endif
                    244:        for ( s = 0; s < MAXIOFP; s++ )
                    245:                close_connection(s);
                    246: }
                    247:
                    248: close_connection(s)
                    249: int s;
                    250: {
                    251:        struct IOFP *r;
                    252:
                    253:        r = &iofp[s];
                    254:        if ( r->in && r->out ) {
                    255:                if ( check_sm_by_mc(s,SM_shutdown) )
                    256:                        ox_send_cmd(s,SM_shutdown);
                    257:                free_iofp(s);
                    258:        }
                    259: }
                    260:
                    261: free_iofp(s)
                    262: int s;
                    263: {
                    264:        struct IOFP *r;
                    265:
                    266:        r = &iofp[s];
                    267:        r->in = r->out = 0; r->s = 0;
                    268: #if !defined(VISUAL)
                    269:        if ( r->socket )
                    270:                unlink(r->socket);
                    271: #endif
                    272: }
                    273:
                    274: #define LBUFSIZ BUFSIZ*10
                    275:
                    276: int get_iofp(s1,af_sock,is_server)
                    277: int s1;
                    278: char *af_sock;
                    279: int is_server;
                    280: {
                    281:        int i;
                    282:        unsigned char c,rc;
                    283:
                    284:        for ( i = 0; i < MAXIOFP; i++ )
                    285:                if ( !iofp[i].in )
                    286:                        break;
                    287:        iofp[i].s = s1;
                    288: #if defined(VISUAL) || MPI
                    289:        iofp[i].in = WSIO_open(s1,"r");
                    290:        iofp[i].out = WSIO_open(s1,"w");
                    291: #else
                    292:        iofp[i].in = fdopen(s1,"r");
                    293:        iofp[i].out = fdopen(s1,"w");
                    294:        setbuffer(iofp[i].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
                    295:        setbuffer(iofp[i].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
                    296: #endif
                    297: #if MPI
                    298:        iofp[i].conv = 0;
                    299:        iofp[i].socket = 0;
                    300: #else
                    301:        if ( little_endian )
                    302:                c = 1;
                    303:        else
                    304:                c = 0xff;
                    305:        if ( is_server ) {
                    306:                /* server : write -> read */
                    307:                write_char(iofp[i].out,&c); ox_flush_stream_force(i);
                    308:                read_char(iofp[i].in,&rc);
                    309:        } else {
                    310:                /* client : read -> write */
                    311:                read_char(iofp[i].in,&rc);
                    312:                write_char(iofp[i].out,&c); ox_flush_stream_force(i);
                    313:        }
                    314:        iofp[i].conv = c == rc ? 0 : 1;
                    315:        if ( af_sock && af_sock[0] ) {
                    316:                iofp[i].socket = (char *)malloc(strlen(af_sock)+1);
                    317:                strcpy(iofp[i].socket,af_sock);
                    318:        } else
                    319:                iofp[i].socket = 0;
                    320: #endif
                    321:        return i;
                    322: }
                    323:
                    324: #if defined(VISUAL)
                    325: void init_socket()
                    326: {
                    327:        static int socket_is_initialized;
                    328:        WORD wVersionRequested;
                    329:        WSADATA wsaData;
                    330:        int err;
                    331:        wVersionRequested = MAKEWORD(2,0);
                    332:
                    333:        if ( socket_is_initialized )
                    334:                return;
                    335:        err = WSAStartup(wVersionRequested,&wsaData);
                    336:        if ( err )
                    337:                return;
                    338: }
                    339: #endif
                    340:
                    341: get_fd(index)
                    342: int index;
                    343: {
                    344:        return iofp[index].s;
                    345: }
                    346:
                    347: get_index(fd)
                    348: int fd;
                    349: {
                    350:        int i;
                    351:
                    352:        for ( i = 0; i < MAXIOFP; i++ )
                    353:                if ( iofp[i].s == fd )
                    354:                        return i;
                    355:        return -1;
                    356: }
                    357: #endif /* INET */

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