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