0
|
1 /*
|
|
2 * $Id: do_socket.c,v 1.2 2001/06/14 18:16:14 ura Exp $
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
|
|
7 * This file is part of FreeWnn.
|
|
8 *
|
|
9 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
|
|
10 * Copyright 1991, 1992 by Massachusetts Institute of Technology
|
|
11 *
|
|
12 * Author: OMRON SOFTWARE Co., Ltd. <freewnn@rd.kyoto.omronsoft.co.jp>
|
|
13 *
|
|
14 * This program is free software; you can redistribute it and/or modify
|
|
15 * it under the terms of the GNU General Public License as published by
|
|
16 * the Free Software Foundation; either version 2, or (at your option)
|
|
17 * any later version.
|
|
18 *
|
|
19 * This program is distributed in the hope that it will be useful,
|
|
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22 * GNU General Public License for more details.
|
|
23 *
|
|
24 * You should have received a copy of the GNU General Public License
|
|
25 * along with GNU Emacs; see the file COPYING. If not, write to the
|
|
26 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
27 *
|
|
28 * Commentary:
|
|
29 *
|
|
30 * Change log:
|
|
31 *
|
|
32 * Last modified date: 8,Feb.1999
|
|
33 *
|
|
34 * Code:
|
|
35 *
|
|
36 */
|
|
37
|
|
38 #include <stdio.h>
|
|
39 #include <errno.h>
|
|
40 #include <X11/Xlib.h>
|
|
41 #ifdef X11R4
|
|
42 #define NEED_EVENTS 1
|
|
43 #include <X11/Xlibos.h>
|
|
44 #else /* X11R4 */
|
|
45 #include "Xlibnet.h"
|
|
46
|
|
47 #ifndef X11R5
|
|
48 /* followings were included in Xlibnet.h, but none in X11R6 */
|
|
49 #include <netinet/in.h>
|
|
50 #include <sys/ioctl.h>
|
|
51 #include <netdb.h>
|
|
52 /* end */
|
|
53 #endif /* !X11R5 */
|
|
54
|
|
55 #endif /* X11R4 */
|
|
56 #include <X11/Xos.h>
|
|
57 #if defined(TCPCONN) || defined(UNIXCONN)
|
|
58 #include <sys/socket.h>
|
|
59 #endif /* defined(TCPCONN) || defined(UNIXCONN) */
|
|
60 #ifdef TCPCONN
|
|
61 #define XIM_PORT_IN 0x9495
|
|
62 #endif /* TCPCONN */
|
|
63
|
|
64 #ifdef UNIXCONN
|
|
65 #include <sys/un.h>
|
|
66 #ifndef XIM_UNIX_PATH
|
|
67 #define XIM_UNIX_PATH "/tmp/.X11-unix/XIM"
|
|
68 #endif /* XIM_UNIX_PATH */
|
|
69 #endif /* UNIXCONN */
|
|
70
|
|
71 #include "sdefine.h"
|
|
72 #include "commonhd.h"
|
|
73 #include "xim.h"
|
|
74 #include "proto.h"
|
|
75 #include "ext.h"
|
|
76
|
|
77 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
78 #define MAX_ACCEPT 5
|
|
79 #else
|
|
80 #define MAX_ACCEPT 3
|
|
81 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
82
|
|
83 #define UNIX_ACPT 0
|
|
84 #define INET_ACPT 1
|
|
85 #define SERVER_ACPT 2
|
|
86
|
|
87 char my_byteOrder;
|
|
88
|
|
89 XIMCmblk accept_blk[MAX_ACCEPT];
|
|
90 XIMCmblk *cblk;
|
|
91 XIMCmblk *cur_cblk = NULL;
|
|
92
|
|
93 int *all_socks;
|
|
94 int *ready_socks;
|
|
95 static int *dummy1_socks, *dummy2_socks;
|
|
96 static int sel_width;
|
|
97
|
|
98 static int nofile;
|
|
99 int max_client;
|
|
100 int cur_sock = -1;
|
|
101 static int rest_length = 0;
|
|
102
|
|
103 #ifndef SEND_BUF_SZ
|
|
104 #define SEND_BUF_SZ 8192
|
|
105 #endif
|
|
106 #ifndef RECV_BUF_SZ
|
|
107 #define RECV_BUF_SZ 8192
|
|
108 #endif
|
|
109
|
|
110 static char send_buf[SEND_BUF_SZ];
|
|
111 static int sp = 0;
|
|
112 static char recv_buf[RECV_BUF_SZ];
|
|
113 static int rp = 0;
|
|
114 static int rc = 0;
|
|
115
|
|
116 #define BINTSIZE (sizeof(int)*8)
|
|
117 #define sock_set(array,pos) (array[pos/BINTSIZE] |= (1<<(pos%BINTSIZE)))
|
|
118 #define sock_clr(array,pos) (array[pos/BINTSIZE] &= ~(1<<(pos%BINTSIZE)))
|
|
119 #define sock_tst(array,pos) (array[pos/BINTSIZE] & (1<<(pos%BINTSIZE)))
|
|
120
|
|
121 extern int read (), write ();
|
|
122
|
|
123 #define _Read(fd, data, size) read((fd), (data), (size))
|
|
124 #define _Write(fd, data, size) write((fd), (data), (size))
|
|
125
|
|
126 static void
|
|
127 _Error ()
|
|
128 {
|
|
129 #ifndef X11R5
|
|
130 XimError (cur_sock);
|
|
131 #endif
|
|
132 close_socket (cur_sock);
|
|
133 }
|
|
134
|
|
135 static int
|
|
136 _Writen (num)
|
|
137 register int num;
|
|
138 {
|
|
139 register int i, ret;
|
|
140
|
|
141 if (cur_sock < 0)
|
|
142 return (-1);
|
|
143 for (i = 0; i < num;)
|
|
144 {
|
|
145 ret = _Write (cur_sock, &send_buf[i], num - i);
|
|
146 if (ret <= 0)
|
|
147 {
|
|
148 _Error ();
|
|
149 return (-1);
|
|
150 }
|
|
151 i += ret;
|
|
152 }
|
|
153 return (0);
|
|
154 }
|
|
155
|
|
156 int
|
|
157 _Send_Flush ()
|
|
158 {
|
|
159 if (sp == 0)
|
|
160 {
|
|
161 return (0);
|
|
162 }
|
|
163 if (_Writen (sp) == -1)
|
|
164 {
|
|
165 sp = 0;
|
|
166 return (-1);
|
|
167 }
|
|
168 sp = 0;
|
|
169 return (0);
|
|
170 }
|
|
171
|
|
172 #define my_bcopy(src, dst, size) {register int i; \
|
|
173 register unsigned char *ppp, *qqq;\
|
|
174 for(i=size,ppp=(unsigned char*)src,qqq=(unsigned char*)dst;i>0;i--) \
|
|
175 *qqq++=*ppp++; \
|
|
176 }
|
|
177
|
|
178 int
|
|
179 _WriteToClient (p, num)
|
|
180 register char *p;
|
|
181 register int num;
|
|
182 {
|
|
183 if (num <= 0)
|
|
184 return (0);
|
|
185 for (;;)
|
|
186 {
|
|
187 if ((sp + num) > SEND_BUF_SZ)
|
|
188 {
|
|
189 my_bcopy (p, &send_buf[sp], SEND_BUF_SZ - sp);
|
|
190 if (_Writen (SEND_BUF_SZ) == -1)
|
|
191 {
|
|
192 sp = 0;
|
|
193 return (-1);
|
|
194 }
|
|
195 num -= (SEND_BUF_SZ - sp);
|
|
196 p += (SEND_BUF_SZ - sp);
|
|
197 sp = 0;
|
|
198 }
|
|
199 else
|
|
200 {
|
|
201 my_bcopy (p, &send_buf[sp], num);
|
|
202 sp += num;
|
|
203 return (0);
|
|
204 }
|
|
205 }
|
|
206 }
|
|
207
|
|
208 int
|
|
209 _ReadFromClient (p, num)
|
|
210 register char *p;
|
|
211 register int num;
|
|
212 {
|
|
213 register char *x = p;
|
|
214
|
|
215 if (num <= 0)
|
|
216 return (0);
|
|
217 for (;;)
|
|
218 {
|
|
219 if (num > rc)
|
|
220 {
|
|
221 if (rc > 0)
|
|
222 {
|
|
223 my_bcopy (&recv_buf[rp], x, rc);
|
|
224 x += rc;
|
|
225 num -= rc;
|
|
226 rc = 0;
|
|
227 }
|
|
228 rc = _Read (cur_sock, recv_buf, RECV_BUF_SZ);
|
|
229 if (rc <= 0)
|
|
230 {
|
|
231 _Error ();
|
|
232 rp = 0;
|
|
233 rc = 0;
|
|
234 return (-1);
|
|
235 }
|
|
236 rp = 0;
|
|
237 }
|
|
238 else
|
|
239 {
|
|
240 my_bcopy (&recv_buf[rp], x, num);
|
|
241 rc -= num;
|
|
242 rp += num;
|
|
243 return (0);
|
|
244 }
|
|
245 }
|
|
246 }
|
|
247
|
|
248 static int
|
|
249 init_net_area ()
|
|
250 {
|
|
251 register int sel_w;
|
|
252 register int sel_bwidth;
|
|
253 register char *p;
|
|
254
|
|
255 nofile = OPEN_MAX;
|
|
256 sel_w = (nofile - 1) / BINTSIZE + 1;
|
|
257 sel_width = sel_w * sizeof (int);
|
|
258 sel_bwidth = sel_width * 8;
|
|
259 max_client = OPEN_MAX - MAX_ACCEPT;
|
|
260
|
|
261 p = (char *) Malloc ((4 * sel_width) + (max_client * sizeof (XIMCmblk)));
|
|
262 if (!p)
|
|
263 {
|
|
264 malloc_error ("allocation of dates for socket");
|
|
265 return (-1);
|
|
266 }
|
|
267 all_socks = (int *) p;
|
|
268 p += sel_width;
|
|
269 ready_socks = (int *) p;
|
|
270 p += sel_width;
|
|
271 dummy1_socks = (int *) p;
|
|
272 p += sel_width;
|
|
273 dummy2_socks = (int *) p;
|
|
274 p += sel_width;
|
|
275 bzero (all_socks, sel_width);
|
|
276 bzero (ready_socks, sel_width);
|
|
277
|
|
278 cblk = (XIMCmblk *) p;
|
|
279 bzero ((char *) cblk, max_client * sizeof (XIMCmblk));
|
|
280 bzero ((char *) accept_blk, MAX_ACCEPT * sizeof (XIMCmblk));
|
|
281 #ifdef UNIXCONN
|
|
282 accept_blk[UNIX_ACPT].sd = -1;
|
|
283 #endif /* UNIXCONN */
|
|
284 #ifdef TCPCONN
|
|
285 accept_blk[INET_ACPT].sd = -1;
|
|
286 #endif /* TCPCONN */
|
|
287 return (0);
|
|
288 }
|
|
289
|
|
290 #ifdef UNIXCONN
|
|
291 static int
|
|
292 init_net_un ()
|
|
293 {
|
|
294 int so;
|
|
295 struct sockaddr_un saddr_un;
|
|
296 extern int unlink (), socket (), bind (), shutdown (), listen ();
|
|
297
|
|
298 unlink (XIM_UNIX_PATH);
|
|
299 saddr_un.sun_family = AF_UNIX;
|
|
300 strcpy (saddr_un.sun_path, XIM_UNIX_PATH);
|
|
301
|
|
302 if ((so = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
|
|
303 {
|
|
304 return (-1);
|
|
305 }
|
|
306 if (bind (so, (struct sockaddr *) &saddr_un, strlen (saddr_un.sun_path) + 2) < 0)
|
|
307 {
|
|
308 shutdown (so, 2);
|
|
309 return (-1);
|
|
310 }
|
|
311 if (listen (so, 5) < 0)
|
|
312 {
|
|
313 shutdown (so, 2);
|
|
314 return (-1);
|
|
315 }
|
|
316 accept_blk[UNIX_ACPT].sd = so;
|
|
317 sock_set (all_socks, so);
|
|
318 return (0);
|
|
319 }
|
|
320 #endif /* UNIXCONN */
|
|
321
|
|
322 #ifdef TCPCONN
|
|
323 static short
|
|
324 init_net_in ()
|
|
325 {
|
|
326 int so;
|
|
327 struct servent *sp;
|
|
328 struct sockaddr_in saddr_in;
|
|
329 unsigned short port;
|
|
330 #ifndef SOLARIS
|
|
331 int on = 1;
|
|
332 #else /* SOLARIS */
|
|
333 int on = 0;
|
|
334 #endif /* SOLARIS */
|
|
335 int i, ok = 0;
|
|
336 extern int setsockopt ();
|
|
337
|
|
338 if ((sp = getservbyname ("xim", "tcp")) == NULL)
|
|
339 {
|
|
340 port = XIM_PORT_IN;
|
|
341 }
|
|
342 else
|
|
343 {
|
|
344 port = ntohs (sp->s_port);
|
|
345 }
|
|
346 saddr_in.sin_family = AF_INET;
|
|
347 saddr_in.sin_port = htons (port);
|
|
348 saddr_in.sin_addr.s_addr = htonl (INADDR_ANY);
|
|
349
|
|
350 for (i = 0; i < MAX_PORT_NUMBER; i++)
|
|
351 {
|
|
352 if ((so = socket (AF_INET, SOCK_STREAM, 0)) < 0)
|
|
353 {
|
|
354 return ((short) -1);
|
|
355 }
|
|
356 setsockopt (so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (int));
|
|
357 if (bind (so, (struct sockaddr *) &saddr_in, sizeof (struct sockaddr_in)) == 0)
|
|
358 {
|
|
359 ok = 1;
|
|
360 break;
|
|
361 }
|
|
362 shutdown (so, 2);
|
|
363 saddr_in.sin_port = htons (++port);
|
|
364 }
|
|
365 if (ok == 0)
|
|
366 {
|
|
367 return ((short) -1);
|
|
368 }
|
|
369 if (listen (so, 5) < 0)
|
|
370 {
|
|
371 shutdown (so, 2);
|
|
372 return ((short) -1);
|
|
373 }
|
|
374 accept_blk[INET_ACPT].sd = so;
|
|
375 sock_set (all_socks, so);
|
|
376 return ((short) port);
|
|
377 }
|
|
378 #endif /* TCPCONN */
|
|
379
|
|
380 short
|
|
381 init_net (fd, displayname)
|
|
382 int fd;
|
|
383 char *displayname;
|
|
384 {
|
|
385 short port = -1;
|
|
386 int indian = 1;
|
|
387
|
|
388 if (*(char *) &indian)
|
|
389 {
|
|
390 my_byteOrder = 'l';
|
|
391 }
|
|
392 else
|
|
393 {
|
|
394 my_byteOrder = 'B';
|
|
395 }
|
|
396 if (init_net_area () < 0)
|
|
397 return (port);
|
|
398 #ifdef UNIXCONN
|
|
399 if (!strncmp (displayname, "unix:", 5) || !strncmp (displayname, ":", 1))
|
|
400 {
|
|
401 if (init_net_un () == (short) -1)
|
|
402 {
|
|
403 print_out ("Could not open UNIX domain socket.");
|
|
404 }
|
|
405 }
|
|
406 #endif /* UNIXCONN */
|
|
407 #ifdef TCPCONN
|
|
408 if ((port = init_net_in ()) == (short) -1)
|
|
409 {
|
|
410 print_out ("Could not open INET domain socket.");
|
|
411 return (port);
|
|
412 }
|
|
413 accept_blk[SERVER_ACPT].sd = fd;
|
|
414 sock_set (all_socks, fd);
|
|
415 #endif /* TCPCONN */
|
|
416 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
417 XJp_init_net ();
|
|
418 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
419 return (port);
|
|
420 }
|
|
421
|
|
422 void
|
|
423 close_net ()
|
|
424 {
|
|
425 int i;
|
|
426 int trueFlag = 1;
|
|
427 #ifdef UNIXCONN
|
|
428 struct sockaddr_un addr_un;
|
|
429 #endif /* UNIXCONN */
|
|
430 #ifdef TCPCONN
|
|
431 struct sockaddr_in addr_in;
|
|
432 #endif /* TCPCONN */
|
|
433 #if defined(UNIXCONN) || defined(TCPCONN)
|
|
434 int addrlen;
|
|
435 extern int accept (), close ();
|
|
436 #endif
|
|
437
|
|
438 #ifdef UNIXCONN
|
|
439 if (accept_blk[UNIX_ACPT].sd != -1)
|
|
440 {
|
|
441 #ifndef SOLARIS
|
|
442 #if defined(FIONBIO)
|
|
443 ioctl (accept_blk[UNIX_ACPT].sd, FIONBIO, &trueFlag);
|
|
444 #endif
|
|
445 #else /* !SOLARIS */
|
|
446 fcntl (accept_blk[UNIX_ACPT].sd, F_SETFL, F_UNLCK);
|
|
447 #endif /* !SOLARIS */
|
|
448 for (;;)
|
|
449 {
|
|
450 addrlen = sizeof (addr_un);
|
|
451 if (accept (accept_blk[UNIX_ACPT].sd, (struct sockaddr *) &addr_un, &addrlen) < 0)
|
|
452 break;
|
|
453 }
|
|
454 shutdown (accept_blk[UNIX_ACPT].sd, 2);
|
|
455 close (accept_blk[UNIX_ACPT].sd);
|
|
456 }
|
|
457 #endif /* UNIXCONN */
|
|
458
|
|
459 #ifdef TCPCONN
|
|
460 if (accept_blk[INET_ACPT].sd != -1)
|
|
461 {
|
|
462 #ifndef SOLARIS
|
|
463 #if defined(FIONBIO)
|
|
464 ioctl (accept_blk[INET_ACPT].sd, FIONBIO, &trueFlag);
|
|
465 #endif
|
|
466 #else /* !SOLARIS */
|
|
467 fcntl (accept_blk[INET_ACPT].sd, F_SETFL, F_UNLCK);
|
|
468 #endif /* !SOLARIS */
|
|
469 for (;;)
|
|
470 {
|
|
471 addrlen = sizeof (addr_in);
|
|
472 if (accept (accept_blk[INET_ACPT].sd, (struct sockaddr *) &addr_in, &addrlen) < 0)
|
|
473 break;
|
|
474 }
|
|
475 shutdown (accept_blk[INET_ACPT].sd, 2);
|
|
476 close (accept_blk[INET_ACPT].sd);
|
|
477 }
|
|
478 #endif /* TCPCONN */
|
|
479 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
480 XJp_close_net ();
|
|
481 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
482
|
|
483 for (i = 0; i < max_client; i++)
|
|
484 {
|
|
485 if (cblk[i].use)
|
|
486 {
|
|
487 shutdown (cblk[i].sd, 2);
|
|
488 close (cblk[i].sd);
|
|
489 }
|
|
490 }
|
|
491 }
|
|
492
|
|
493 #if defined(UNIXCONN) || defined(TCPCONN)
|
|
494 static int
|
|
495 get_screen_number (p)
|
|
496 char *p;
|
|
497 {
|
|
498 register char *s;
|
|
499
|
|
500 if (!(s = index (p, ':')))
|
|
501 return (-1);
|
|
502 if (!(s = index (s, '.')))
|
|
503 return (-1);
|
|
504 if (!*(++s))
|
|
505 return (-1);
|
|
506 return (atoi (s));
|
|
507 }
|
|
508
|
|
509 #if defined(X11R5) || defined(BC_X11R5)
|
|
510 static void
|
|
511 new_cl_sock (b)
|
|
512 XIMCmblk *b;
|
|
513 {
|
|
514 char displaybuf[256];
|
|
515 ximConnClient client;
|
|
516 ximNormalReply reply;
|
|
517 int len;
|
|
518
|
|
519 if (_ReadFromClient (&client, sz_ximConnClient) == -1)
|
|
520 return;
|
|
521 if (my_byteOrder != client.byteOrder)
|
|
522 {
|
|
523 b->byteOrder = True;
|
|
524 }
|
|
525 else
|
|
526 {
|
|
527 b->byteOrder = False;
|
|
528 }
|
|
529 if (need_byteswap () == True)
|
|
530 {
|
|
531 client.length = byteswap_s (client.length);
|
|
532 }
|
|
533 if (_ReadFromClient (displaybuf, client.length) == -1)
|
|
534 return;
|
|
535 displaybuf[client.length] = '\0';
|
|
536 if ((len = strlen (displaybuf)) > 0)
|
|
537 {
|
|
538 b->screen = get_screen_number (displaybuf);
|
|
539 }
|
|
540 else
|
|
541 {
|
|
542 b->screen = -1;
|
|
543 }
|
|
544 reply.state = 0;
|
|
545 if (need_byteswap () == True)
|
|
546 {
|
|
547 byteswap_NormalReply (&reply);
|
|
548 }
|
|
549 if (_WriteToClient (&reply, sz_ximNormalReply) == -1)
|
|
550 return;
|
|
551 if (_Send_Flush () == -1)
|
|
552 return;
|
|
553 }
|
|
554 #endif /* defined(X11R5) || defined(BC_X11R5) */
|
|
555
|
|
556 #ifndef X11R5
|
|
557 static void
|
|
558 xim_new_cl_sock (b)
|
|
559 XIMCmblk *b;
|
|
560 {
|
|
561 #ifdef BC_X11R5
|
|
562 extern int ximr5_client;
|
|
563 b->ximr5 = ximr5_client;
|
|
564 if (b->ximr5)
|
|
565 {
|
|
566 new_cl_sock (b);
|
|
567 return;
|
|
568 }
|
|
569 ximr5_client = 1;
|
|
570 #endif /* BC_X11R5 */
|
|
571 /* nothing to do, all are done in XimPreConnect() */
|
|
572 }
|
|
573 #endif /* !X11R5 */
|
|
574
|
|
575 #endif /* defined(UNIXCONN) || defined(TCPCONN) */
|
|
576
|
|
577 static int
|
|
578 new_client_acpt (fd)
|
|
579 int fd;
|
|
580 {
|
|
581 #ifdef UNIXCONN
|
|
582 struct sockaddr_un addr_un;
|
|
583 #endif /* UNIXCONN */
|
|
584 #ifdef TCPCONN
|
|
585 struct sockaddr_in addr_in;
|
|
586 #endif /* TCPCONN */
|
|
587 #if defined(UNIXCONN) || defined(TCPCONN)
|
|
588 register int i;
|
|
589 int addrlen;
|
|
590 #endif
|
|
591
|
|
592 #ifdef UNIXCONN
|
|
593 if (accept_blk[UNIX_ACPT].sd != -1)
|
|
594 {
|
|
595 if (fd == accept_blk[UNIX_ACPT].sd)
|
|
596 {
|
|
597 for (i = 0; i < max_client; i++)
|
|
598 {
|
|
599 if (cblk[i].use == 0)
|
|
600 {
|
|
601 addrlen = sizeof (addr_un);
|
|
602 if ((cblk[i].sd = accept (fd, (struct sockaddr *) &addr_un, &addrlen)) < 0)
|
|
603 {
|
|
604 return (-1);
|
|
605 }
|
|
606 cur_cblk = &cblk[i];
|
|
607 cur_cblk->use = 1;
|
|
608 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
609 cur_cblk->xjp = 0;
|
|
610 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
611 cur_sock = cur_cblk->sd;
|
|
612 sock_set (all_socks, cur_sock);
|
|
613 #ifdef X11R5
|
|
614 new_cl_sock (cur_cblk);
|
|
615 #else
|
|
616 xim_new_cl_sock (cur_cblk);
|
|
617 #endif /* X11R5 */
|
|
618 return (0);
|
|
619 }
|
|
620 }
|
|
621 }
|
|
622 }
|
|
623 #endif /* UNIXCONN */
|
|
624 #ifdef TCPCONN
|
|
625 if (fd == accept_blk[INET_ACPT].sd)
|
|
626 {
|
|
627 for (i = 0; i < max_client; i++)
|
|
628 {
|
|
629 if (cblk[i].use == 0)
|
|
630 {
|
|
631 addrlen = sizeof (addr_in);
|
|
632 if ((cblk[i].sd = accept (fd, (struct sockaddr *) &addr_in, &addrlen)) < 0)
|
|
633 {
|
|
634 return (-1);
|
|
635 }
|
|
636 cur_cblk = &cblk[i];
|
|
637 cur_cblk->use = 1;
|
|
638 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
639 cur_cblk->xjp = 0;
|
|
640 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
641 cur_sock = cur_cblk->sd;
|
|
642 sock_set (all_socks, cur_sock);
|
|
643 #ifdef X11R5
|
|
644 new_cl_sock (cur_cblk);
|
|
645 #else
|
|
646 xim_new_cl_sock (cur_cblk);
|
|
647 #endif /* X11R5 */
|
|
648 break;
|
|
649 }
|
|
650 }
|
|
651 }
|
|
652 #endif /* TCPCONN */
|
|
653 return (0);
|
|
654 }
|
|
655
|
|
656 int
|
|
657 wait_for_socket ()
|
|
658 {
|
|
659 register int i;
|
|
660 int n, ret;
|
|
661 extern int errno;
|
|
662 extern int select ();
|
|
663
|
|
664 /*
|
|
665 * If recv_buf has some data, return with as cur_sock get data
|
|
666 */
|
|
667 if (cur_sock != -1 && rc > 0)
|
|
668 {
|
|
669 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
670 if (cur_cblk && cur_cblk->xjp)
|
|
671 return (XJP_DIRECT_TYPE);
|
|
672 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
673 #ifdef X11R5
|
|
674 return (REQUEST_TYPE);
|
|
675 #else
|
|
676 #ifdef BC_X11R5
|
|
677 if (cur_cblk && cur_cblk->ximr5)
|
|
678 return REQUEST_TYPE;
|
|
679 else
|
|
680 #endif /* BC_X11R5 */
|
|
681 return REQUEST_TYPE_XIM;
|
|
682 #endif /* X11R5 */
|
|
683 }
|
|
684 if (!*ready_socks)
|
|
685 {
|
|
686 my_bcopy (all_socks, ready_socks, sel_width);
|
|
687 bzero (dummy1_socks, sel_width);
|
|
688 bzero (dummy2_socks, sel_width);
|
|
689
|
|
690 if ((n = select (nofile, ready_socks, dummy1_socks, dummy2_socks, (struct timeval *) NULL)) == -1)
|
|
691 {
|
|
692 if (errno != EINTR)
|
|
693 do_end ();
|
|
694 else
|
|
695 {
|
|
696 bzero (ready_socks, sel_width);
|
|
697 return (0);
|
|
698 }
|
|
699 }
|
|
700 }
|
|
701 #ifdef UNIXCONN
|
|
702 if (accept_blk[UNIX_ACPT].sd != -1)
|
|
703 {
|
|
704 if (sock_tst (ready_socks, accept_blk[UNIX_ACPT].sd))
|
|
705 {
|
|
706 sock_clr (ready_socks, accept_blk[UNIX_ACPT].sd);
|
|
707 cur_cblk = NULL;
|
|
708 cur_sock = -1;
|
|
709 new_client_acpt (accept_blk[UNIX_ACPT].sd);
|
|
710 return (0);
|
|
711 }
|
|
712 }
|
|
713 #endif /* UNIXCONN */
|
|
714 #ifdef TCPCONN
|
|
715 if (accept_blk[INET_ACPT].sd != -1)
|
|
716 {
|
|
717 if (sock_tst (ready_socks, accept_blk[INET_ACPT].sd))
|
|
718 {
|
|
719 sock_clr (ready_socks, accept_blk[INET_ACPT].sd);
|
|
720 cur_cblk = NULL;
|
|
721 cur_sock = -1;
|
|
722 new_client_acpt (accept_blk[INET_ACPT].sd);
|
|
723 return (0);
|
|
724 }
|
|
725 }
|
|
726 #endif /* TCPCONN */
|
|
727 if (sock_tst (ready_socks, accept_blk[SERVER_ACPT].sd))
|
|
728 {
|
|
729 sock_clr (ready_socks, accept_blk[SERVER_ACPT].sd);
|
|
730 cur_cblk = NULL;
|
|
731 cur_sock = -1;
|
|
732 return (XEVENT_TYPE);
|
|
733 }
|
|
734 else
|
|
735 {
|
|
736 if (cur_sock != -1 && sock_tst (ready_socks, cur_sock))
|
|
737 {
|
|
738 sock_clr (ready_socks, cur_sock);
|
|
739 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
740 if (cur_cblk && cur_cblk->xjp)
|
|
741 return (XJP_DIRECT_TYPE);
|
|
742 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
743 #ifdef X11R5
|
|
744 return (REQUEST_TYPE);
|
|
745 #else
|
|
746 #ifdef BC_X11R5
|
|
747 if (cur_cblk && cur_cblk->ximr5)
|
|
748 return REQUEST_TYPE;
|
|
749 else
|
|
750 #endif /* BC_X11R5 */
|
|
751 return REQUEST_TYPE_XIM;
|
|
752 #endif /* X11R5 */
|
|
753 }
|
|
754 cur_cblk = NULL;
|
|
755 cur_sock = -1;
|
|
756 for (i = 0; i < max_client; i++)
|
|
757 {
|
|
758 if (cblk[i].use && sock_tst (ready_socks, cblk[i].sd))
|
|
759 {
|
|
760 sock_clr (ready_socks, cblk[i].sd);
|
|
761 cur_cblk = &cblk[i];
|
|
762 cur_sock = cur_cblk->sd;
|
|
763 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
764 if (cur_cblk && cur_cblk->xjp)
|
|
765 return (XJP_DIRECT_TYPE);
|
|
766 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
767 #ifdef X11R5
|
|
768 return (REQUEST_TYPE);
|
|
769 #else
|
|
770 #ifdef BC_X11R5
|
|
771 if (cur_cblk && cur_cblk->ximr5)
|
|
772 return REQUEST_TYPE;
|
|
773 else
|
|
774 #endif /* BC_X11R5 */
|
|
775 return REQUEST_TYPE_XIM;
|
|
776 #endif /* X11R5 */
|
|
777 }
|
|
778 }
|
|
779 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
780 if (ret = XJp_wait_for_socket ())
|
|
781 return (0);
|
|
782 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
783 return (0);
|
|
784 }
|
|
785 }
|
|
786
|
|
787 void
|
|
788 close_socket (fd)
|
|
789 int fd;
|
|
790 {
|
|
791 int i;
|
|
792 for (i = 0; i < max_client; i++)
|
|
793 {
|
|
794 if (fd == cblk[i].sd && cblk[i].use == 1)
|
|
795 {
|
|
796 cblk[i].use = 0;
|
|
797 shutdown (cblk[i].sd, 2);
|
|
798 sock_clr (all_socks, cblk[i].sd);
|
|
799 close (cblk[i].sd);
|
|
800 #if defined(XJPLIB) && defined(XJPLIB_DIRECT)
|
|
801 if (cblk[i].xjp)
|
|
802 XJp_destroy_for_sock (cblk[i].sd);
|
|
803 else
|
|
804 #endif /* defined(XJPLIB) && defined(XJPLIB_DIRECT) */
|
|
805 destroy_for_sock (cblk[i].sd);
|
|
806 if (cur_sock == fd)
|
|
807 cur_sock = -1;
|
|
808 return;
|
|
809 }
|
|
810 }
|
|
811 }
|
|
812
|
|
813 int
|
|
814 get_cur_sock ()
|
|
815 {
|
|
816 return (cur_sock);
|
|
817 }
|
|
818
|
|
819 int
|
|
820 get_rest_len ()
|
|
821 {
|
|
822 return (rest_length);
|
|
823 }
|
|
824
|
|
825 int
|
|
826 read_requestheader ()
|
|
827 {
|
|
828 ximRequestHeader request_header;
|
|
829
|
|
830 if (_ReadFromClient (&request_header, sz_ximRequestHeader) == -1)
|
|
831 {
|
|
832 return (-1);
|
|
833 }
|
|
834 else
|
|
835 {
|
|
836 if (need_byteswap () == True)
|
|
837 request_header.length = byteswap_s (request_header.length);
|
|
838 rest_length = request_header.length - sz_ximRequestHeader;
|
|
839 return (request_header.reqType);
|
|
840 }
|
|
841 }
|
|
842
|
|
843 int
|
|
844 read_strings (p, p_len, r_len)
|
|
845 char **p;
|
|
846 int *p_len, r_len;
|
|
847 {
|
|
848 register int len;
|
|
849 register char *ptr;
|
|
850 char tmp;
|
|
851
|
|
852 if (r_len > 0)
|
|
853 {
|
|
854 if (*p_len <= r_len)
|
|
855 {
|
|
856 len = r_len + 1;
|
|
857 if (!(ptr = Malloc (len)))
|
|
858 {
|
|
859 malloc_error ("allocation of client data");
|
|
860 for (; r_len > 0; r_len--)
|
|
861 { /* read data in dummy */
|
|
862 if (_ReadFromClient (&tmp, 1) == 1)
|
|
863 return (-1);
|
|
864 }
|
|
865 return (-2);
|
|
866 }
|
|
867 if (*p)
|
|
868 Free (*p);
|
|
869 *p = ptr;
|
|
870 *p_len = len;
|
|
871 }
|
|
872 if (_ReadFromClient (*p, r_len) == 1)
|
|
873 return (-1);
|
|
874 p[0][r_len] = '\0';
|
|
875 }
|
|
876 else
|
|
877 {
|
|
878 if (*p_len > 0)
|
|
879 **p = '\0';
|
|
880 }
|
|
881 return (0);
|
|
882 }
|
|
883
|
|
884 /* *INDENT-OFF* */
|
|
885 Bool
|
|
886 need_byteswap ()
|
|
887 /* *INDENT-ON* */
|
|
888
|
|
889 {
|
|
890 if (!cur_cblk)
|
|
891 return (False);
|
|
892 return (cur_cblk->byteOrder);
|
|
893 }
|
|
894
|
|
895 int
|
|
896 get_client_screen ()
|
|
897 {
|
|
898 if (!cur_cblk)
|
|
899 return (-1);
|
|
900 return (cur_cblk->screen);
|
|
901 }
|