Annotation of OpenXM_contrib2/asir2000/io/sio.c, Revision 1.1.1.1
1.1 noro 1: /* $OpenXM: OpenXM/src/asir99/io/sio.c,v 1.3 1999/11/18 09:00:38 noro Exp $ */
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_un s_un;
79: struct sockaddr *saddr;
80: int len;
81: int service;
82:
83: if ( use_unix ) {
84: service = socket(AF_UNIX, SOCK_STREAM, 0);
85: if (service < 0) {
86: perror("in socket");
87: return -1;
88: }
89: s_un.sun_family = AF_UNIX;
90: strcpy(s_un.sun_path,port_str);
91: #if defined(__FreeBSD__)
92: len = SUN_LEN(&s_un);
93: s_un.sun_len = len+1; /* XXX */
94: #else
95: len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
96: #endif
97: saddr = (struct sockaddr *)&s_un;
98: } else {
99: service = socket(AF_INET, SOCK_STREAM, 0);
100: if ( service < 0 ) {
101: perror("in socket");
102: return -1;
103: }
104: sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY;
105: sin.sin_port = htons(atoi(port_str));
106: len = sizeof(sin);
107: saddr = (struct sockaddr *)&sin;
108: }
109: if (bind(service, saddr, len) < 0) {
110: perror("in bind");
111: close(service);
112: return -1;
113: }
114: if (getsockname(service,saddr, &len) < 0) {
115: perror("in getsockname");
116: close(service);
117: return -1;
118: }
119: if (listen(service, SOCKQUEUELENGTH) < 0) {
120: perror("in listen");
121: close(service);
122: return -1;
123: }
124: return service;
125: }
126:
127: /*
128: try to accept a connection request
129:
130: Input
131: af_unix: s is UNIX domain socket if af_unix is nonzero
132: s: socket
133:
134: Output
135: c: an accepted socket which is newly created
136: -1: if failed to accept
137:
138: the original socket is always closed.
139: */
140:
141: int try_accept(af_unix,s)
142: int af_unix,s;
143: {
144: int len,c,i;
145: struct sockaddr_un s_un;
146: struct sockaddr_in sin;
147:
148: if ( af_unix ) {
149: len = sizeof(s_un);
150: for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )
151: c = accept(s, (struct sockaddr *) &s_un, &len);
152: } else {
153:
154: len = sizeof(sin);
155: for ( c = -1, i = 0; (c < 0)&&(i = 10) ; i++ )
156: c = accept(s, (struct sockaddr *) &sin, &len);
157: }
158: if ( i == 10 )
159: c = -1;
160: close(s);
161: return c;
162: }
163:
164: int try_connect(use_unix,host,port_str)
165: int use_unix;
166: char *host,*port_str;
167: {
168: struct sockaddr_in sin;
169: struct sockaddr_un s_un;
170: struct sockaddr *saddr;
171: struct hostent *hp;
172: int len,s,i;
173:
174: for ( i = 0; i < 10; i++ ) {
175: if ( use_unix ) {
176: if ( (s = socket(AF_UNIX,SOCK_STREAM,0)) < 0 ) {
177: perror("socket");
178: return -1;
179: }
180: bzero(&s_un,sizeof(s_un));
181: s_un.sun_family = AF_UNIX;
182: strcpy(s_un.sun_path,port_str);
183: #if defined(__FreeBSD__)
184: len = SUN_LEN(&s_un);
185: s_un.sun_len = len+1; /* XXX */
186: #else
187: len = strlen(s_un.sun_path)+sizeof(s_un.sun_family);
188: #endif
189: saddr = (struct sockaddr *)&s_un;
190: } else {
191: if ( (s = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
192: perror("socket");
193: return -1;
194: }
195: bzero(&sin,sizeof(sin));
196: sin.sin_port = htons(atoi(port_str));
197: sin.sin_addr.s_addr = inet_addr(host);
198: if ( sin.sin_addr.s_addr != -1 ) {
199: sin.sin_family = AF_INET;
200: } else {
201: hp = gethostbyname(host);
202: bcopy(hp->h_addr,&sin.sin_addr,hp->h_length);
203: sin.sin_family = hp->h_addrtype;
204: }
205: len = sizeof(sin);
206: saddr = (struct sockaddr *)&sin;
207: }
208: if ( connect(s,saddr,len) >= 0 )
209: break;
210: else {
211: close(s);
212: usleep(100000);
213: }
214: }
215: if ( i == 10 ) {
216: perror("connect");
217: return -1;
218: } else
219: return s;
220: }
221:
222: close_allconnections()
223: {
224: int s;
225:
226: #if defined(SIGPIPE)
227: signal(SIGPIPE,SIG_IGN);
228: #endif
229: for ( s = 0; s < MAXIOFP; s++ )
230: close_connection(s);
231: }
232:
233: close_connection(s)
234: int s;
235: {
236: struct IOFP *r;
237:
238: r = &iofp[s];
239: if ( r->in && r->out ) {
240: if ( check_sm_by_mc(s,SM_shutdown) )
241: ox_send_cmd(s,SM_shutdown);
242: free_iofp(s);
243: }
244: }
245:
246: free_iofp(s)
247: int s;
248: {
249: struct IOFP *r;
250:
251: r = &iofp[s];
252: r->in = r->out = 0; r->s = 0;
253: #if !defined(VISUAL)
254: if ( r->socket )
255: unlink(r->socket);
256: #endif
257: }
258:
259: #define LBUFSIZ BUFSIZ*10
260:
261: int get_iofp(s1,af_sock,is_server)
262: int s1;
263: char *af_sock;
264: int is_server;
265: {
266: int i;
267: unsigned char c,rc;
268:
269: for ( i = 0; i < MAXIOFP; i++ )
270: if ( !iofp[i].in )
271: break;
272: iofp[i].s = s1;
273: #if defined(VISUAL) || MPI
274: iofp[i].in = WSIO_open(s1,"r");
275: iofp[i].out = WSIO_open(s1,"w");
276: #else
277: iofp[i].in = fdopen(s1,"r");
278: iofp[i].out = fdopen(s1,"w");
279: setbuffer(iofp[i].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
280: setbuffer(iofp[i].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
281: #endif
282: #if MPI
283: iofp[i].conv = 0;
284: iofp[i].socket = 0;
285: #else
286: if ( little_endian )
287: c = 1;
288: else
289: c = 0xff;
290: if ( is_server ) {
291: /* server : write -> read */
292: write_char(iofp[i].out,&c); ox_flush_stream_force(i);
293: read_char(iofp[i].in,&rc);
294: } else {
295: /* client : read -> write */
296: read_char(iofp[i].in,&rc);
297: write_char(iofp[i].out,&c); ox_flush_stream_force(i);
298: }
299: iofp[i].conv = c == rc ? 0 : 1;
300: if ( af_sock && af_sock[0] ) {
301: iofp[i].socket = (char *)malloc(strlen(af_sock)+1);
302: strcpy(iofp[i].socket,af_sock);
303: } else
304: iofp[i].socket = 0;
305: #endif
306: return i;
307: }
308:
309: #if defined(VISUAL)
310: void init_socket()
311: {
312: static int socket_is_initialized;
313: WORD wVersionRequested;
314: WSADATA wsaData;
315: int err;
316: wVersionRequested = MAKEWORD(2,0);
317:
318: if ( socket_is_initialized )
319: return;
320: err = WSAStartup(wVersionRequested,&wsaData);
321: if ( err )
322: return;
323: }
324: #endif
325:
326: get_fd(index)
327: int index;
328: {
329: return iofp[index].s;
330: }
331:
332: get_index(fd)
333: int fd;
334: {
335: int i;
336:
337: for ( i = 0; i < MAXIOFP; i++ )
338: if ( iofp[i].s == fd )
339: return i;
340: return -1;
341: }
342: #endif /* INET */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>