Annotation of OpenXM_contrib2/asir2000/io/sio.c, Revision 1.5
1.5 ! noro 1: /*
! 2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
! 3: * All rights reserved.
! 4: *
! 5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
! 6: * non-exclusive and royalty-free license to use, copy, modify and
! 7: * redistribute, solely for non-commercial and non-profit purposes, the
! 8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
! 9: * conditions of this Agreement. For the avoidance of doubt, you acquire
! 10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
! 11: * third party developer retains all rights, including but not limited to
! 12: * copyrights, in and to the SOFTWARE.
! 13: *
! 14: * (1) FLL does not grant you a license in any way for commercial
! 15: * purposes. You may use the SOFTWARE only for non-commercial and
! 16: * non-profit purposes only, such as academic, research and internal
! 17: * business use.
! 18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
! 19: * international copyright treaties. If you make copies of the SOFTWARE,
! 20: * with or without modification, as permitted hereunder, you shall affix
! 21: * to all such copies of the SOFTWARE the above copyright notice.
! 22: * (3) An explicit reference to this SOFTWARE and its copyright owner
! 23: * shall be made on your publication or presentation in any form of the
! 24: * results obtained by use of the SOFTWARE.
! 25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
! 26: * e-mail at risa-admin@flab.fujitsu.co.jp of the detailed specification
! 27: * for such modification or the source code of the modified part of the
! 28: * SOFTWARE.
! 29: *
! 30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
! 31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
! 32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
! 33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
! 34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
! 35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
! 36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
! 37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
! 38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
! 39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
! 40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
! 41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
! 42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
! 43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
! 44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
! 45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
! 46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
! 47: * $OpenXM: OpenXM_contrib2/asir2000/io/sio.c,v 1.4 2000/07/13 05:09:02 noro Exp $
! 48: */
1.1 noro 49: #if INET
50: #include "ca.h"
1.3 noro 51: #include "setjmp.h"
1.1 noro 52: #include "ox.h"
53: #if defined(VISUAL)
54: #include <winsock.h>
55: #else
56: #include <sys/time.h>
57: #include <sys/uio.h>
58: #include <sys/ioctl.h>
59: #include <sys/un.h>
60: #endif
61: #include<signal.h>
62:
63: #define SOCKQUEUELENGTH 5
64: #define ISIZ sizeof(int)
65:
66: extern int little_endian;
67:
68: struct IOFP iofp[MAXIOFP];
69:
70: #if !defined(_PA_RISC1_1)
71: #define RSH "rsh"
72: #else
73: #define RSH "remsh"
74: #endif
75:
76: void init_socket(void);
77:
78: int getremotesocket(s)
79: int s;
80: {
81: return iofp[s].s;
82: }
83:
84: void getremotename(s,name)
85: int s;
86: char *name;
87: {
88: struct sockaddr_in peer;
89: struct hostent *hp;
90: int peerlen;
91:
92: peerlen = sizeof(peer);
93: getpeername(getremotesocket(s),(struct sockaddr *)&peer,&peerlen);
94: hp = gethostbyaddr((char *)&peer.sin_addr,sizeof(struct in_addr),AF_INET);
95: if ( hp )
96: strcpy(name,hp->h_name);
97: else
98: strcpy(name,(char *)inet_ntoa(peer.sin_addr));
99: }
100:
101: int generate_port(use_unix,port_str)
102: int use_unix;
103: char *port_str;
104: {
105: double get_current_time();
106: unsigned long mt_genrand();
107: unsigned int port;
108: static int count=0;
109:
110: if ( use_unix ) {
111: sprintf(port_str,"/tmp/ox%02x.XXXXXX",count);
112: count++;
113: mktemp(port_str);
114: } else {
115: port = ((unsigned int)mt_genrand()+(unsigned int)get_current_time())
116: %(65536-1024)+1024;
117: sprintf(port_str,"%d",port);
118: }
119: }
120:
121: int try_bind_listen(use_unix,port_str)
122: int use_unix;
123: char *port_str;
124: {
125: struct sockaddr_in sin;
126: struct sockaddr *saddr;
127: int len;
128: int service;
1.2 noro 129: #if !defined(VISUAL)
130: struct sockaddr_un s_un;
1.1 noro 131:
132: if ( use_unix ) {
133: service = socket(AF_UNIX, SOCK_STREAM, 0);
134: if (service < 0) {
135: perror("in socket");
136: return -1;
137: }
138: s_un.sun_family = AF_UNIX;
139: strcpy(s_un.sun_path,port_str);
140: #if defined(__FreeBSD__)
141: len = SUN_LEN(&s_un);
142: s_un.sun_len = len+1; /* XXX */
143: #else
144: len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
145: #endif
146: saddr = (struct sockaddr *)&s_un;
1.2 noro 147: } else
148: #endif
149: {
1.1 noro 150: service = socket(AF_INET, SOCK_STREAM, 0);
151: if ( service < 0 ) {
152: perror("in socket");
153: return -1;
154: }
155: sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY;
156: sin.sin_port = htons(atoi(port_str));
157: len = sizeof(sin);
158: saddr = (struct sockaddr *)&sin;
159: }
160: if (bind(service, saddr, len) < 0) {
161: perror("in bind");
162: close(service);
163: return -1;
164: }
165: if (getsockname(service,saddr, &len) < 0) {
166: perror("in getsockname");
167: close(service);
168: return -1;
169: }
170: if (listen(service, SOCKQUEUELENGTH) < 0) {
171: perror("in listen");
172: close(service);
173: return -1;
174: }
175: return service;
176: }
177:
178: /*
179: try to accept a connection request
180:
181: Input
182: af_unix: s is UNIX domain socket if af_unix is nonzero
183: s: socket
184:
185: Output
186: c: an accepted socket which is newly created
187: -1: if failed to accept
188:
189: the original socket is always closed.
190: */
191:
192: int try_accept(af_unix,s)
193: int af_unix,s;
194: {
195: int len,c,i;
196: struct sockaddr_in sin;
197:
1.2 noro 198: #if !defined(VISUAL)
199: struct sockaddr_un s_un;
1.1 noro 200: if ( af_unix ) {
201: len = sizeof(s_un);
202: for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )
203: c = accept(s, (struct sockaddr *) &s_un, &len);
1.2 noro 204: } else
205: #endif
206: {
1.1 noro 207:
208: len = sizeof(sin);
209: for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )
210: c = accept(s, (struct sockaddr *) &sin, &len);
211: }
212: if ( i == 10 )
213: c = -1;
214: close(s);
215: return c;
216: }
217:
218: int try_connect(use_unix,host,port_str)
219: int use_unix;
220: char *host,*port_str;
221: {
222: struct sockaddr_in sin;
223: struct sockaddr *saddr;
224: struct hostent *hp;
225: int len,s,i;
1.2 noro 226: #if !defined(VISUAL)
227: struct sockaddr_un s_un;
228: #endif
1.1 noro 229:
230: for ( i = 0; i < 10; i++ ) {
1.2 noro 231: #if !defined(VISUAL)
1.1 noro 232: if ( use_unix ) {
233: if ( (s = socket(AF_UNIX,SOCK_STREAM,0)) < 0 ) {
234: perror("socket");
235: return -1;
236: }
237: bzero(&s_un,sizeof(s_un));
238: s_un.sun_family = AF_UNIX;
239: strcpy(s_un.sun_path,port_str);
240: #if defined(__FreeBSD__)
241: len = SUN_LEN(&s_un);
242: s_un.sun_len = len+1; /* XXX */
243: #else
244: len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
245: #endif
246: saddr = (struct sockaddr *)&s_un;
1.2 noro 247: } else
248: #endif /* VISUAL */
249: {
1.1 noro 250: if ( (s = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
251: perror("socket");
252: return -1;
253: }
254: bzero(&sin,sizeof(sin));
255: sin.sin_port = htons(atoi(port_str));
256: sin.sin_addr.s_addr = inet_addr(host);
257: if ( sin.sin_addr.s_addr != -1 ) {
258: sin.sin_family = AF_INET;
259: } else {
260: hp = gethostbyname(host);
261: bcopy(hp->h_addr,&sin.sin_addr,hp->h_length);
262: sin.sin_family = hp->h_addrtype;
263: }
264: len = sizeof(sin);
265: saddr = (struct sockaddr *)&sin;
266: }
267: if ( connect(s,saddr,len) >= 0 )
268: break;
269: else {
270: close(s);
1.2 noro 271: #if defined(VISUAL)
272: Sleep(100);
273: #else
1.1 noro 274: usleep(100000);
1.2 noro 275: #endif
1.1 noro 276: }
277: }
278: if ( i == 10 ) {
279: perror("connect");
280: return -1;
281: } else
282: return s;
283: }
284:
285: close_allconnections()
286: {
287: int s;
288:
289: #if defined(SIGPIPE)
290: signal(SIGPIPE,SIG_IGN);
291: #endif
292: for ( s = 0; s < MAXIOFP; s++ )
293: close_connection(s);
294: }
295:
296: close_connection(s)
297: int s;
298: {
299: struct IOFP *r;
300:
301: r = &iofp[s];
302: if ( r->in && r->out ) {
303: if ( check_sm_by_mc(s,SM_shutdown) )
304: ox_send_cmd(s,SM_shutdown);
305: free_iofp(s);
306: }
307: }
308:
309: free_iofp(s)
310: int s;
311: {
312: struct IOFP *r;
313:
314: r = &iofp[s];
315: r->in = r->out = 0; r->s = 0;
316: #if !defined(VISUAL)
317: if ( r->socket )
318: unlink(r->socket);
319: #endif
320: }
321:
322: #define LBUFSIZ BUFSIZ*10
323:
324: int get_iofp(s1,af_sock,is_server)
325: int s1;
326: char *af_sock;
327: int is_server;
328: {
329: int i;
330: unsigned char c,rc;
1.4 noro 331: extern int mpi_myid;
1.1 noro 332:
1.4 noro 333: #if MPI
334: iofp[s1].s = s1;
335: if ( mpi_myid == s1 ) {
336: iofp[s1].in = 0;
337: iofp[s1].out = 0;
338: } else {
339: iofp[s1].in = WSIO_open(s1,"r");
340: iofp[s1].out = WSIO_open(s1,"w");
341: }
342: iofp[s1].conv = 0;
343: iofp[s1].socket = 0;
344:
345: return s1;
346: #else
1.1 noro 347: for ( i = 0; i < MAXIOFP; i++ )
348: if ( !iofp[i].in )
349: break;
350: iofp[i].s = s1;
1.4 noro 351: #if defined(VISUAL)
1.1 noro 352: iofp[i].in = WSIO_open(s1,"r");
353: iofp[i].out = WSIO_open(s1,"w");
354: #else
355: iofp[i].in = fdopen(s1,"r");
356: iofp[i].out = fdopen(s1,"w");
357: setbuffer(iofp[i].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
358: setbuffer(iofp[i].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
359: #endif
360: if ( little_endian )
361: c = 1;
362: else
363: c = 0xff;
364: if ( is_server ) {
365: /* server : write -> read */
366: write_char(iofp[i].out,&c); ox_flush_stream_force(i);
367: read_char(iofp[i].in,&rc);
368: } else {
369: /* client : read -> write */
370: read_char(iofp[i].in,&rc);
371: write_char(iofp[i].out,&c); ox_flush_stream_force(i);
372: }
373: iofp[i].conv = c == rc ? 0 : 1;
374: if ( af_sock && af_sock[0] ) {
375: iofp[i].socket = (char *)malloc(strlen(af_sock)+1);
376: strcpy(iofp[i].socket,af_sock);
377: } else
378: iofp[i].socket = 0;
1.4 noro 379: return i;
1.1 noro 380: #endif
381: }
382:
383: #if defined(VISUAL)
384: void init_socket()
385: {
386: static int socket_is_initialized;
387: WORD wVersionRequested;
388: WSADATA wsaData;
389: int err;
390: wVersionRequested = MAKEWORD(2,0);
391:
392: if ( socket_is_initialized )
393: return;
394: err = WSAStartup(wVersionRequested,&wsaData);
395: if ( err )
396: return;
397: }
398: #endif
399:
400: get_fd(index)
401: int index;
402: {
403: return iofp[index].s;
404: }
405:
406: get_index(fd)
407: int fd;
408: {
409: int i;
410:
411: for ( i = 0; i < MAXIOFP; i++ )
412: if ( iofp[i].s == fd )
413: return i;
414: return -1;
415: }
416: #endif /* INET */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>