Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports
Path: blob/main/japanese/FreeWnn-lib/files/patch-Wnn-jserver-de.c
18157 views
1
Index: Wnn/jserver/de.c
2
===================================================================
3
RCS file: /home/cvs/private/hrs/freewnn/Wnn/jserver/de.c,v
4
retrieving revision 1.1.1.1
5
retrieving revision 1.8
6
diff -u -p -r1.1.1.1 -r1.8
7
--- Wnn/jserver/de.c 20 Dec 2008 07:13:30 -0000 1.1.1.1
8
+++ Wnn/jserver/de.c 9 Sep 2014 16:18:01 -0000 1.8
9
@@ -1,7 +1,7 @@
10
/*
11
* FreeWnn is a network-extensible Kana-to-Kanji conversion system.
12
* This file is part of FreeWnn.
13
- *
14
+ *
15
* Copyright Kyoto University Research Institute for Mathematical Sciences
16
* 1987, 1988, 1989, 1990, 1991, 1992
17
* Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
18
@@ -102,54 +102,35 @@ static char rcs_id[] = "$Id: de.c,v 1.36
19
#endif
20
21
#ifndef INET6
22
-# define OPTIONARGS "Df:s:h:N:p:vu4"
23
+# define OPTIONARGS "a:Df:s:h:N:p:vu4"
24
#else
25
-# define OPTIONARGS "Df:s:h:N:p:vu46"
26
+# define OPTIONARGS "a:Df:s:h:N:p:vu46"
27
#endif /* INET6 */
28
29
/* Accept Socket */
30
-#ifdef INET6
31
-#define MAX_ACCEPTS 3
32
-#else
33
-#define MAX_ACCEPTS 2
34
-#endif
35
-#define UNIX_ACPT 0
36
-#define INET_ACPT 1
37
-#ifdef INET6
38
-#define INET6_ACPT 2
39
-#endif
40
+#define MAX_ACCEPTS 256
41
42
-#define PROTO_ALL 0x1
43
#define PROTO_UN 0x2
44
#define PROTO_INET 0x4
45
-#ifdef INET6
46
#define PROTO_INET6 0x8
47
-#endif
48
-static int listen_proto = PROTO_ALL;
49
+#define PROTO_ALL (PROTO_UN|PROTO_INET|PROTO_INET6)
50
+static int listen_proto;
51
52
jmp_buf client_dead;
53
54
static int port;
55
-static int serverNO = 0;
56
+static int serverNO;
57
58
-struct cmblk
59
-{
60
- int sd; /** �����åȤ�fd **/
61
+struct cmblk {
62
+ int domain;
63
+ int sd; /* socket fd */
64
};
65
-#define COMS_BLOCK struct cmblk
66
67
-static COMS_BLOCK *cblk;
68
- /** ���饤����Ȥ��ȤΥ����åȤ��������ơ��֥� **/
69
-
70
-static COMS_BLOCK accept_blk[MAX_ACCEPTS]; /*accept sock blocks */
71
-
72
-
73
-/* Communication Buffers */
74
-static char snd_buf[R_BUF_SIZ]; /** �����Хåե� **/
75
-static int sbp; /** �����Хåե��ݥ��� **/
76
-
77
-static int rbc; /** �����Хåե�counter **/
78
-static char rcv_buf[S_BUF_SIZ]; /** �����Хåե� **/
79
+/** ���饤����Ȥ��ȤΥ����åȤ��������ơ��֥� **/
80
+static struct cmblk *cblk;
81
+/*accept sock blocks */
82
+static struct cmblk accept_blk[MAX_ACCEPTS];
83
+static int bindex;
84
85
#if defined(EAGAIN)
86
# if defined(EWOULDBLOCK)
87
@@ -167,7 +148,6 @@ static char rcv_buf[S_BUF_SIZ];
88
89
/* Client Table */
90
int clientp; /** cblk��ͭ���ʥǡ����κǸ�򺹤��Ƥ��� **/
91
-
92
int cur_clp; /** ���ߤΥ��饤����Ȥ��ֹ� **/
93
94
static fd_set *all_socks; /** �ӥåȥѥ�����
95
@@ -181,27 +161,24 @@ struct msg_cat *wnn_msg_cat;
96
struct msg_cat *js_msg_cat;
97
98
/* function prototypes */
99
-static void daemon_main (void);
100
-static void socket_disc_init (void);
101
-static void sel_all (void);
102
-static int get_client (void);
103
-static void new_client (void);
104
-static void daemon_init (void);
105
-static void daemon_fin_un (int);
106
-static void daemon_fin_in (int);
107
-static int rcv_1_client (int);
108
-static void snd_1_client (int, int);
109
-static void socket_init_un (void);
110
-static void socket_init_in (void);
111
-static int socket_accept_un (void);
112
-static int socket_accept_in (int);
113
-static void xerror (char*);
114
-static void get_options (int, char **);
115
-static void usage (void);
116
-static void print_version (void);
117
-#ifdef DEBUG
118
-static void dmp (char*, int);
119
-#endif
120
+static void daemon_main(void);
121
+static void socket_disc_init(void);
122
+static int sel_all(void);
123
+static int get_client(void);
124
+static void new_client(void);
125
+static void daemon_init(void);
126
+static void daemon_fin_un(int);
127
+static void daemon_fin_in(int);
128
+static int rcv_1_client(int);
129
+static void snd_1_client(int, int);
130
+static void socket_init_un(int *);
131
+static void socket_init_in(int *);
132
+static int socket_accept(int);
133
+static void xerror(char*);
134
+static void get_options(int, char **);
135
+static void usage(void);
136
+static void print_version(void);
137
+static void dmp(char*, int);
138
139
static char cmd_name[16];
140
141
@@ -214,154 +191,136 @@ int deny_severity;
142
143
/* No arguments are used. Only options. */
144
int
145
-main (int argc, char *argv[])
146
+main(int argc, char *argv[])
147
{
148
- int tmpttyfd;
149
- char *cswidth_name;
150
- extern char *get_cswidth_name ();
151
- extern void set_cswidth ();
152
-
153
- char nlspath[64];
154
-
155
- strcpy (cmd_name, WNN_DAEMON_NAME);
156
- strcpy (lang_dir, LANG_NAME);
157
- strcpy (nlspath, LIBDIR);
158
- strcat (nlspath, "/%L/%N");
159
- js_msg_cat = msg_open (MESSAGE_FILE, nlspath, lang_dir);
160
- wnn_msg_cat = msg_open ("libwnn.msg", nlspath, lang_dir);
161
- if (wnn_msg_cat == NULL)
162
- {
163
- log_err ("cannot open message file libwnn.msg.");
164
- }
165
- if (cswidth_name = get_cswidth_name (LANG_NAME))
166
- set_cswidth (create_cswidth (cswidth_name));
167
+ int tmpttyfd;
168
+ pid_t pid;
169
+ int fd;
170
+ char *cswidth_name;
171
+ extern char *get_cswidth_name ();
172
+ extern void set_cswidth ();
173
+
174
+ char nlspath[64];
175
+
176
+ strcpy(cmd_name, WNN_DAEMON_NAME);
177
+ strcpy(lang_dir, LANG_NAME);
178
+ strcpy(nlspath, LIBDIR);
179
+ strcat(nlspath, "/%L/%N");
180
+ js_msg_cat = msg_open (MESSAGE_FILE, nlspath, lang_dir);
181
+ wnn_msg_cat = msg_open ("libwnn.msg", nlspath, lang_dir);
182
183
- port = -1;
184
- /* option default */
185
- option_flag = (OPT_FORK & ~OPT_VERBOSE);
186
-
187
- setuid (geteuid ());
188
- get_options (argc, argv);
189
- print_version();
190
- log_debug("invoked as %s.", argv[0]);
191
- if (option_flag & OPT_FORK)
192
- {
193
- if (fork ())
194
- {
195
- signal (SIGCHLD, _exit);
196
- signal (SIGHUP, SIG_IGN);
197
- signal (SIGINT, SIG_IGN);
198
- signal (SIGQUIT, SIG_IGN);
199
-#ifdef SIGTSTP
200
- signal (SIGTSTP, SIG_IGN);
201
-#endif
202
- signal (SIGTERM, _exit);
203
- pause ();
204
+ if (wnn_msg_cat == NULL) {
205
+ log_err ("cannot open message file libwnn.msg.");
206
}
207
- }
208
+ if (cswidth_name = get_cswidth_name (LANG_NAME))
209
+ set_cswidth (create_cswidth (cswidth_name));
210
211
+ port = -1;
212
+ /* option default */
213
+ option_flag = (OPT_FORK & ~OPT_VERBOSE);
214
+
215
+ setuid(geteuid());
216
+ get_options (argc, argv);
217
+ print_version();
218
+ log_debug("invoked as %s.", argv[0]);
219
#if defined(HAVE_LIBWRAP)
220
- allow_severity = LOG_INFO;
221
- deny_severity = LOG_WARNING;
222
- /* hosts_access_verbose = 2; */
223
+ allow_severity = LOG_INFO;
224
+ deny_severity = LOG_WARNING;
225
+ /* hosts_access_verbose = 2; */
226
#endif /* HAVE_LIBWRAP */
227
228
- signal (SIGHUP, signal_hand);
229
- signal (SIGINT, signal_hand);
230
- signal (SIGQUIT, signal_hand);
231
- signal (SIGTERM, terminate_hand);
232
- if (option_flag & OPT_FORK)
233
- {
234
-#ifdef SIGTSTP
235
- signal (SIGTSTP, SIG_IGN);
236
+ signal(SIGHUP, signal_hand);
237
+ signal(SIGQUIT, signal_hand);
238
+ signal(SIGTERM, terminate_hand);
239
+ if (option_flag & OPT_FORK) {
240
+ /* when -D is not specified, accept SIGINT */
241
+ signal(SIGINT, signal_hand);
242
+#ifdef SIGTSTP
243
+ signal(SIGTSTP, SIG_IGN);
244
#endif /* SIGTSTP */
245
- }
246
- read_default ();
247
- daemon_init ();
248
-
249
- env_init ();
250
- if (file_init () == NULL)
251
- {
252
- exit (1);
253
- }
254
- dic_init ();
255
- if (NULL == get_kaiseki_area (LENGTHCONV + 1)) /* �Ѵ���ǽʸ���� */
256
- {
257
- log_err ("get_kaiseki_area failed.");
258
- exit (1);
259
- }
260
- init_work_areas ();
261
- init_jmt ();
262
-
263
- read_default_files ();
264
-
265
- if (option_flag & OPT_FORK)
266
- {
267
- /* End of initialization, kill parent */
268
- kill (getppid (), SIGTERM);
269
- fclose (stdin);
270
- fclose (stdout);
271
- if (!(option_flag & OPT_VERBOSE))
272
- {
273
-#if !(defined(BSD) && (BSD >= 199306)) /* !4.4BSD-Lite by Taoka */
274
- fclose (stderr);
275
-#else /* 4.4BSD-Lite */
276
- int fd = open ("/dev/null", O_WRONLY);
277
- if (fd < 0)
278
- {
279
- xerror ("Cannot open /dev/null");
280
- }
281
- dup2 (fd, 2);
282
- close (fd);
283
-#endif /* 4.4BSD-Lite */
284
}
285
-
286
+ read_default();
287
+ daemon_init();
288
+ env_init();
289
+ if (file_init() == 0)
290
+ exit (1);
291
+ dic_init();
292
+ if (0 == get_kaiseki_area (LENGTHCONV + 1)) {
293
+ /* �Ѵ���ǽʸ���� */
294
+ log_err ("get_kaiseki_area failed.");
295
+ exit (1);
296
+ }
297
+ init_work_areas();
298
+ init_jmt();
299
+ read_default_files();
300
+ if (option_flag & OPT_FORK) {
301
+ pid = fork();
302
+ if (pid == -1) {
303
+ log_err ("cannot fork.");
304
+ exit(1);
305
+ }
306
+ if (pid > 0)
307
+ _exit(0);
308
+ setsid();
309
+ pid = fork();
310
+ if (pid == -1) {
311
+ log_err ("cannot fork.");
312
+ exit(1);
313
+ }
314
+ if (pid > 0)
315
+ _exit(0);
316
+ chdir("/");
317
+ umask(0);
318
+ close(STDIN_FILENO);
319
+ close(STDOUT_FILENO);
320
+ if (!(option_flag & OPT_VERBOSE)) {
321
+ close(STDERR_FILENO);
322
+ fd = open("/dev/null", O_WRONLY);
323
+ if (fd < 0) {
324
+ xerror ("Cannot open /dev/null");
325
+ }
326
+ dup2(fd, STDERR_FILENO);
327
+ close(fd);
328
+ }
329
#ifdef SETPGRP_VOID
330
- setpgrp ();
331
+ setpgrp();
332
#else /* !SETPGRP_VOID */
333
# if !defined(TIOCNOTTY) && defined(SVR4)
334
# define TIOCNOTTY _IO('t', 113)
335
# endif /* !defined(TIOCNOTTY) && defined(SVR4) */
336
#ifndef HITACHI
337
- if ((tmpttyfd = open ("/dev/tty", O_RDWR)) >= 0)
338
- {
339
- ioctl (tmpttyfd, TIOCNOTTY, 0);
340
- close (tmpttyfd);
341
- }
342
+ if ((tmpttyfd = open("/dev/tty", O_RDWR)) >= 0) {
343
+ ioctl(tmpttyfd, TIOCNOTTY, 0);
344
+ close(tmpttyfd);
345
+ }
346
#endif /* HITACHI */
347
#endif /* SETPGRP_VOID */
348
- }
349
-
350
- daemon_main ();
351
-
352
- daemon_fin ();
353
- return (0); /* it is not reached. only for avoiding compiler warning. */
354
+ }
355
+ daemon_main();
356
+ daemon_fin();
357
+ return(0); /* NOTREACHED */
358
}
359
360
static void
361
daemon_main (void)
362
{
363
- for (;;)
364
- {
365
- c_c = NULL; /* Added for logging: server section */
366
- sel_all ();
367
- new_client ();
368
- for (;;)
369
- {
370
- if (get_client () == -1)
371
- break;
372
- c_c = &client[cur_clp];
373
- rbc = 0;
374
- sbp = 0;
375
+ for (;;) {
376
+ c_c = NULL; /* Added for logging: server section */
377
+ sel_all();
378
+ new_client();
379
+
380
+ for (;;) {
381
+ if (get_client () == -1)
382
+ break;
383
+ c_c = &client[cur_clp];
384
/* if(rcv_1_client(cur_clp) == 0){ del_client(); continue; } */
385
- if (setjmp (client_dead))
386
- {
387
- del_client ();
388
- continue;
389
- }
390
- do_command (c_c);
391
- }
392
- }
393
+ if (setjmp(client_dead)) {
394
+ del_client ();
395
+ continue;
396
+ }
397
+ do_command(c_c);
398
+ }
399
+ }
400
}
401
402
/*
403
@@ -370,39 +329,36 @@ daemon_main (void)
404
static void
405
socket_disc_init (void)
406
{
407
- if (WNN_NFD <= FD_SETSIZE)
408
- {
409
- nofile = WNN_NFD;
410
- }
411
- else
412
- {
413
- nofile = FD_SETSIZE;
414
- }
415
- all_socks = (fd_set *) malloc (sizeof (fd_set));
416
- FD_ZERO (all_socks);
417
- ready_socks = (fd_set *) malloc (sizeof (fd_set));
418
- dummy1_socks = (fd_set *) malloc (sizeof (fd_set));
419
- dummy2_socks = (fd_set *) malloc (sizeof (fd_set));
420
+ nofile = MIN(WNN_NFD, FD_SETSIZE);
421
+
422
+ all_socks = (fd_set *)malloc(sizeof(fd_set));
423
+ ready_socks = (fd_set *)malloc(sizeof(fd_set));
424
+ dummy1_socks = (fd_set *)malloc(sizeof(fd_set));
425
+ dummy2_socks = (fd_set *)malloc(sizeof(fd_set));
426
+ FD_ZERO(all_socks);
427
+ FD_ZERO(ready_socks);
428
+ FD_ZERO(dummy1_socks);
429
+ FD_ZERO(dummy2_socks);
430
}
431
432
/** ���ƤΥ����åȤˤĤ����Ԥ� **/
433
-static void
434
-sel_all (void)
435
+static int
436
+sel_all(void)
437
{
438
- memcpy (ready_socks, all_socks, sizeof (fd_set));
439
- bzero (dummy1_socks, sizeof (fd_set));
440
- bzero (dummy2_socks, sizeof (fd_set));
441
+ int ns = 0;
442
+ memcpy(ready_socks, all_socks, sizeof(fd_set));
443
+ FD_ZERO(dummy1_socks);
444
+ FD_ZERO(dummy2_socks);
445
446
top:
447
- errno = 0;
448
- if ((no_of_ready_socks = select (nofile, ready_socks, dummy1_socks, dummy2_socks, NULL)) == -1)
449
- {
450
- if (errno == EINTR)
451
- goto top;
452
- xerror ("select error");
453
- }
454
+ errno = 0;
455
+ if ((no_of_ready_socks = select(nofile, ready_socks, dummy1_socks, dummy2_socks, NULL)) == -1) {
456
+ if (errno == EINTR)
457
+ goto top;
458
+ xerror ("select error");
459
+ }
460
#ifdef DEBUG
461
- log_debug ("select OK, ready_socks[0]=%02X, n-r-s=%x\n", ready_socks[0], no_of_ready_socks);
462
+ log_debug ("select OK, ready_socks[0]=%02X, n-r-s=%x\n", ready_socks[0], no_of_ready_socks);
463
#endif
464
}
465
466
@@ -412,142 +368,136 @@ top:
467
static int
468
get_client (void)
469
{
470
- int i;
471
-
472
- if (no_of_ready_socks == 0)
473
- return -1; /* no client waits service */
474
-
475
- for (i = cur_clp;;)
476
- {
477
- if (no_of_ready_socks == 0)
478
- return -1;
479
- i++;
480
- if (i >= clientp)
481
- i = 0;
482
- if (FD_ISSET (cblk[i].sd, ready_socks))
483
- {
484
- FD_CLR (cblk[i].sd, ready_socks);
485
- no_of_ready_socks--;
486
- return cur_clp = i;
487
- }
488
- }
489
+ int i;
490
+
491
+ if (no_of_ready_socks == 0)
492
+ return -1; /* no client waits service */
493
+
494
+ for (i = cur_clp;;)
495
+ {
496
+ if (no_of_ready_socks == 0)
497
+ return -1;
498
+ i++;
499
+ if (i >= clientp)
500
+ i = 0;
501
+ if (FD_ISSET(cblk[i].sd, ready_socks)) {
502
+ FD_CLR(cblk[i].sd, ready_socks);
503
+ no_of_ready_socks--;
504
+ return (cur_clp = i);
505
+ }
506
+ }
507
}
508
509
/** ���������饤����Ȥ���뤫�ݤ���Ĵ�٤�
510
�錄����cblk����Ͽ���� **/
511
static void
512
-new_client (void) /* NewClient */
513
+new_client(void)
514
{
515
- int sd;
516
- int full, i;
517
- FILE *f[3];
518
- char gomi[1024];
519
+ int sd = -1;
520
+ int full, i;
521
+ FILE *f[3];
522
+ char gomi[1024];
523
#ifdef HAVE_LIBWRAP
524
- int is_internet_socket;
525
- struct request_info tcpd_request;
526
+ int is_internet_socket;
527
+ struct request_info tcpd_request;
528
#endif /* HAVE_LIBWRAP */
529
-#ifdef AF_UNIX
530
- if ((serverNO == 0) &&
531
- (FD_ISSET (accept_blk[UNIX_ACPT].sd, ready_socks)))
532
- {
533
- FD_CLR (accept_blk[UNIX_ACPT].sd, ready_socks);
534
- no_of_ready_socks--;
535
- sd = socket_accept_un ();
536
+
537
+ log_debug("new client called");
538
+
539
+ for (i = 0; i < bindex && !FD_ISSET(accept_blk[i].sd, ready_socks); i++)
540
+ ;
541
+
542
+ if (i == bindex)
543
+ return;
544
+
545
+ FD_CLR(accept_blk[i].sd, ready_socks);
546
+ no_of_ready_socks--;
547
+ log_debug("new client: FDISSET(%d/%d) true, domain=%d",
548
+ i, bindex, accept_blk[i].domain);
549
+
550
+ switch (accept_blk[i].domain) {
551
+#ifdef AF_UNIX
552
+ case AF_UNIX:
553
+ sd = socket_accept(i);
554
#ifdef HAVE_LIBWRAP
555
- is_internet_socket = 0;
556
-#endif
557
- }
558
- else
559
+ is_internet_socket = 0;
560
+#endif /* HAVE_LIBWRAP */
561
+ break;
562
#endif
563
#ifdef INET6
564
- if (FD_ISSET (accept_blk[INET6_ACPT].sd, ready_socks))
565
- {
566
- FD_CLR (accept_blk[INET6_ACPT].sd, ready_socks);
567
- no_of_ready_socks--;
568
- sd = socket_accept_in (accept_blk[INET6_ACPT].sd);
569
-#ifdef HAVE_LIBWRAP
570
- is_internet_socket = 1;
571
-#endif
572
- }
573
- else
574
+ case AF_INET6:
575
#endif
576
- if (FD_ISSET (accept_blk[INET_ACPT].sd, ready_socks))
577
- {
578
- FD_CLR (accept_blk[INET_ACPT].sd, ready_socks);
579
- no_of_ready_socks--;
580
- sd = socket_accept_in (accept_blk[INET_ACPT].sd);
581
+ case AF_INET:
582
+ sd = socket_accept(i);
583
#ifdef HAVE_LIBWRAP
584
- is_internet_socket = 1;
585
-#endif
586
- }
587
- else
588
- {
589
- return;
590
- }
591
- log_debug ("new client: sd = %d", sd);
592
- /* reserve 2 fd */
593
- for (full = i = 0; i < 2; i++)
594
- {
595
- if (NULL == (f[i] = fopen ("/dev/null", "r")))
596
- {
597
- full = 1;
598
- }
599
- }
600
- for (i = 0; i < 2; i++)
601
- {
602
- if (NULL != f[i])
603
- fclose (f[i]);
604
- }
605
-
606
- if (full || sd >= nofile || clientp >= max_client)
607
- {
608
- log_err ("no more client.");
609
+ is_internet_socket = 1;
610
+#endif /* HAVE_LIBWRAP */
611
+ break;
612
+ default:
613
+ return;
614
+ }
615
+
616
+ log_debug("new client: sd = %d (type=%d)",
617
+ sd, accept_blk[i].domain);
618
+
619
+ /* reserve 2 fd */
620
+ for (full = i = 0; i < 2; i++) {
621
+ if (NULL == (f[i] = fopen ("/dev/null", "r"))) {
622
+ full = 1;
623
+ }
624
+ }
625
+
626
+ for (i = 0; i < 2; i++) {
627
+ if (NULL != f[i])
628
+ fclose (f[i]);
629
+ }
630
+
631
+ if (full || sd >= nofile || clientp >= max_client) {
632
+ log_err("no more client.");
633
#ifdef HAVE_RECV
634
- recv (sd, gomi, 1024, 0);
635
+ recv(sd, gomi, 1024, 0);
636
#else
637
- read (sd, gomi, 1024);
638
+ read(sd, gomi, 1024);
639
#endif
640
- shutdown (sd, 2);
641
+ shutdown(sd, 2);
642
#ifdef HAVE_CLOSESOCKET
643
- closesocket (sd);
644
+ closesocket(sd);
645
#else
646
- close (sd);
647
+ close(sd);
648
#endif
649
- return;
650
- }
651
-
652
+ return;
653
+ }
654
+
655
#ifdef HAVE_LIBWRAP
656
- if (is_internet_socket) {
657
- request_init (&tcpd_request,RQ_DAEMON, WNN_DAEMON_NAME,
658
- RQ_FILE, sd, NULL);
659
- fromhost (&tcpd_request);
660
- if (!hosts_access (&tcpd_request))
661
- {
662
- log_err ("reject client."); /* should be log_info? */
663
- /* should we log IP address / hostname? */
664
+ if (is_internet_socket) {
665
+ request_init (&tcpd_request,RQ_DAEMON, WNN_DAEMON_NAME,
666
+ RQ_FILE, sd, NULL);
667
+ fromhost (&tcpd_request);
668
+ if (!hosts_access (&tcpd_request)) {
669
+ log_err ("reject client."); /* should be log_info? */
670
+ /* should we log IP address / hostname? */
671
#ifdef HAVE_RECV
672
- recv (sd, gomi, 1024, 0);
673
+ recv(sd, gomi, 1024, 0);
674
#else
675
- read (sd, gomi, 1024);
676
+ read(sd, gomi, 1024);
677
#endif
678
- shutdown (sd, 2);
679
+ shutdown(sd, 2);
680
#ifdef HAVE_CLOSESOCKET
681
- closesocket (sd);
682
+ closesocket(sd);
683
#else
684
- close (sd);
685
+ close(sd);
686
#endif
687
- return;
688
- }
689
- }
690
+ return;
691
+ }
692
+ }
693
#endif /* HAVE_LIBWRAP */
694
-
695
- cblk[clientp].sd = sd;
696
- FD_SET (sd, all_socks);
697
- for (i = 0; i < WNN_MAX_ENV_OF_A_CLIENT; i++)
698
- {
699
- (client[clientp].env)[i] = -1;
700
- }
701
- clientp++;
702
+
703
+ cblk[clientp].sd = sd;
704
+ FD_SET(sd, all_socks);
705
+ for (i = 0; i < WNN_MAX_ENV_OF_A_CLIENT; i++) {
706
+ (client[clientp].env)[i] = -1;
707
+ }
708
+ clientp++;
709
}
710
711
/** ���饤����Ȥ�cblk���������� **/
712
@@ -555,746 +505,726 @@ new_client (void) /* NewCl
713
void
714
del_client (void)
715
{
716
- disconnect_all_env_of_client ();
717
- FD_CLR (cblk[cur_clp].sd, all_socks);
718
+ disconnect_all_env_of_client ();
719
+ FD_CLR(cblk[cur_clp].sd, all_socks);
720
#ifdef HAVE_CLOSESOCKET
721
- closesocket (cblk[cur_clp].sd);
722
+ closesocket(cblk[cur_clp].sd);
723
#else
724
- close (cblk[cur_clp].sd);
725
+ close(cblk[cur_clp].sd);
726
#endif
727
/* logging here because c_c (used in log_debug) will be broken after
728
following section */
729
- log_debug("Delete Client: cur_clp = %d\n", cur_clp);
730
- cblk[cur_clp] = cblk[clientp - 1];
731
- client[cur_clp] = client[clientp - 1];
732
- /* Clear host/user name with zero - needed for logging */
733
- client[clientp - 1].user_name[0] = '\0'; /* Should we use bzero()? */
734
- client[clientp - 1].host_name[0] = '\0';
735
- clientp--;
736
-}
737
+ log_debug("Delete Client: cur_clp = %d\n", cur_clp);
738
+ cblk[cur_clp] = cblk[clientp - 1];
739
+ client[cur_clp] = client[clientp - 1];
740
+ /* Clear host/user name with zero - needed for logging */
741
+
742
+ /* Should we use bzero()? */
743
+ client[clientp - 1].user_name[0] = '\0';
744
745
+ client[clientp - 1].host_name[0] = '\0';
746
+ clientp--;
747
+}
748
749
/** �����Ф򥤥˥���饤������ **/
750
static void
751
-daemon_init (void) /* initialize Daemon */
752
+daemon_init(void) /* initialize Daemon */
753
{
754
- /*
755
- signal (SIGHUP, SIG_IGN);
756
- signal (SIGINT, SIG_IGN);
757
- signal (SIGQUIT, SIG_IGN);
758
- */
759
+ /*
760
+ signal (SIGHUP, SIG_IGN);
761
+ signal (SIGINT, SIG_IGN);
762
+ signal (SIGQUIT, SIG_IGN);
763
+ */
764
765
+ if ((cblk = (struct cmblk *)malloc(max_client * sizeof(struct cmblk))) == NULL) {
766
+ xerror ("daemon_init: ");
767
+ }
768
+
769
+ if ((client = (CLIENT *)malloc(max_client * sizeof (CLIENT))) == NULL) {
770
+ xerror ("daemon_init: ");
771
+ }
772
+
773
+ SDRAND(time(NULL));
774
+ clientp = 0; /* V3.0 */
775
+ cur_clp = 0; /* V3.0 */
776
+ socket_disc_init();
777
778
- if ((cblk = (COMS_BLOCK *) malloc (max_client * sizeof (COMS_BLOCK))) == NULL)
779
- {
780
- xerror ("daemon_init: ");
781
- }
782
- if ((client = (CLIENT *) malloc (max_client * sizeof (CLIENT))) == NULL)
783
- {
784
- xerror ("daemon_init: ");
785
- }
786
- SDRAND (time (NULL));
787
- clientp = 0; /* V3.0 */
788
- cur_clp = 0; /* V3.0 */
789
- socket_disc_init ();
790
-#ifdef INET6
791
- if (listen_proto&(PROTO_ALL|PROTO_INET|PROTO_INET6))
792
- socket_init_in ();
793
-#else
794
- if (listen_proto&(PROTO_ALL|PROTO_INET))
795
- socket_init_in ();
796
-#endif
797
#ifdef AF_UNIX
798
- if (listen_proto&(PROTO_ALL|PROTO_UN))
799
- socket_init_un ();
800
-#endif /* AF_UNIX */
801
+ if (listen_proto & PROTO_UN)
802
+ socket_init_un(&bindex);
803
+#endif
804
+ if (listen_proto & (PROTO_INET|PROTO_INET6))
805
+ socket_init_in(&bindex);
806
}
807
808
/** �����Ф򽪤�� **/
809
-#ifdef AF_UNIX
810
+#ifdef AF_UNIX
811
static void
812
-daemon_fin_un (int sock_d_un)
813
+daemon_fin_un(int sock_d_un)
814
{
815
- int trueFlag = 1;
816
- struct sockaddr_un addr_un;
817
- socklen_t addrlen;
818
+ int trueFlag = 1;
819
+ struct sockaddr_un addr_un;
820
+ socklen_t addrlen;
821
822
- if (serverNO == 0)
823
- {
824
#ifndef SOLARIS
825
#if defined(FIONBIO)
826
- ioctl (sock_d_un, FIONBIO, &trueFlag);
827
+ ioctl (sock_d_un, FIONBIO, &trueFlag);
828
#endif
829
#else /* !SOLARIS */
830
- fcntl (sock_d_un, F_SETFL, F_UNLCK);
831
+ fcntl (sock_d_un, F_SETFL, F_UNLCK);
832
#endif /* !SOLARIS */
833
- for (;;)
834
- {
835
- addrlen = sizeof (addr_un);
836
- if (accept (sock_d_un, (struct sockaddr *) &addr_un, &addrlen) < 0)
837
- break;
838
- /* EWOULDBLOCK EXPECTED, but we don't check */
839
+ for (;;) {
840
+ addrlen = sizeof (addr_un);
841
+ if (accept(sock_d_un,
842
+ (struct sockaddr *)&addr_un,
843
+ &addrlen) < 0)
844
+ break;
845
+ /* EWOULDBLOCK EXPECTED, but we don't check */
846
}
847
- shutdown (sock_d_un, 2);
848
- close (sock_d_un);
849
- }
850
+ shutdown (sock_d_un, 2);
851
+ close (sock_d_un);
852
}
853
#endif /* AF_UNIX */
854
855
static void
856
-daemon_fin_in (int sock_d_in)
857
+daemon_fin_in(int sock_d_in)
858
{
859
- int trueFlag = 1;
860
- struct sockaddr_in addr_in;
861
- socklen_t addrlen;
862
+ int trueFlag = 1;
863
+ struct sockaddr_in addr_in;
864
+ socklen_t addrlen;
865
#ifdef USE_SETSOCKOPT
866
- int on = ~0;
867
+ int on = ~0;
868
#endif
869
870
#ifndef SOLARIS
871
#ifdef USE_SETSOCKOPT
872
- setsockopt (sock_d_in, SOL_SOCKET, SO_NONBLOCK, &on, sizeof (int));
873
+ setsockopt(sock_d_in, SOL_SOCKET, SO_NONBLOCK, &on, sizeof(int));
874
#else
875
#if defined(FIONBIO)
876
- ioctl (sock_d_in, FIONBIO, &trueFlag);
877
+ ioctl(sock_d_in, FIONBIO, &trueFlag);
878
#endif
879
#endif /* USE_SETSOCKOPT */
880
#else /* !SOLARIS */
881
- fcntl (sock_d_in, F_SETFL, F_UNLCK);
882
+ fcntl(sock_d_in, F_SETFL, F_UNLCK);
883
#endif /* !SOLARIS */
884
- for (;;)
885
- {
886
- addrlen = sizeof (addr_in);
887
- if (accept (sock_d_in, (struct sockaddr *) &addr_in, &addrlen) < 0)
888
- break;
889
- /* EWOULDBLOCK EXPECTED, but we don't check */
890
- }
891
- shutdown (sock_d_in, 2);
892
+ for (;;) {
893
+ addrlen = sizeof(addr_in);
894
+ if (accept(sock_d_in,
895
+ (struct sockaddr *)&addr_in,
896
+ &addrlen) < 0)
897
+ break;
898
+ /* EWOULDBLOCK EXPECTED, but we don't check */
899
+ }
900
+ shutdown (sock_d_in, 2);
901
#ifdef HAVE_CLOSESOCKET
902
- closesocket (sock_d_in);
903
+ closesocket (sock_d_in);
904
#else
905
- close (sock_d_in);
906
+ close (sock_d_in);
907
#endif
908
}
909
910
void
911
daemon_fin (void)
912
{
913
- int fd;
914
-#ifdef AF_UNIX
915
- int sock_d_un = accept_blk[UNIX_ACPT].sd;
916
-#endif /* AF_UNIX */
917
- int sock_d_in = accept_blk[INET_ACPT].sd;
918
-#ifdef INET6
919
- int sock_d_in6 = accept_blk[INET6_ACPT].sd;
920
-#endif
921
+ int i;
922
+ int fd;
923
924
- /*
925
- accept all pending connection from new clients,
926
- avoiding kernel hangup.
927
- */
928
+ for (i = 0; i < bindex; i++) {
929
+ if (FD_ISSET(accept_blk[i].sd, all_socks)) {
930
+ switch (accept_blk[i].domain) {
931
#ifdef AF_UNIX
932
- daemon_fin_un (sock_d_un);
933
-#endif
934
- daemon_fin_in (sock_d_in);
935
+ case AF_UNIX:
936
+ if (listen_proto & PROTO_UN)
937
+ daemon_fin_un(accept_blk[i].sd);
938
+ break;
939
+#endif
940
+ case AF_INET:
941
+ if (listen_proto & PROTO_INET)
942
+ daemon_fin_in(accept_blk[i].sd);
943
+ break;
944
#ifdef INET6
945
- daemon_fin_in (sock_d_in6);
946
-#endif
947
+ case AF_INET6:
948
+ if (listen_proto & PROTO_INET6)
949
+ daemon_fin_in(accept_blk[i].sd);
950
+ break;
951
+#endif
952
+ default:
953
+ break;
954
+ }
955
+ }
956
+ }
957
958
- for (fd = nofile - 1; fd >= 0; fd--)
959
- {
960
- if ((fd != sock_d_in) &&
961
-#ifdef INET6
962
- (fd != sock_d_in6) &&
963
-#endif
964
-#ifdef AF_UNIX
965
- (fd != sock_d_un) &&
966
-#endif /* AF_UNIX */
967
- FD_ISSET (fd, all_socks))
968
- {
969
- shutdown (fd, 2);
970
+ for (fd = nofile - 1; fd >= 0; fd--) {
971
+ if (FD_ISSET(fd, all_socks)) {
972
+ shutdown (fd, 2);
973
#ifdef HAVE_CLOSESOCKET
974
- closesocket (fd);
975
+ closesocket (fd);
976
#else
977
- close (fd);
978
+ close (fd);
979
#endif
980
- }
981
- }
982
+ }
983
+ }
984
}
985
986
-/*------*/
987
+static unsigned char snd_buf[S_BUF_SIZ]; /** �����Хåե� **/
988
+static unsigned char *sp = snd_buf;
989
+
990
+static unsigned char rcv_buf[R_BUF_SIZ]; /** �����Хåե� **/
991
+static unsigned char *rbp = rcv_buf;
992
+static unsigned char *rp = rcv_buf;
993
994
-/** **/
995
char *
996
-gets_cur (char *buffer, size_t buffer_size)
997
+gets_cur(char *buffer, size_t buffer_size)
998
{
999
- char *b;
1000
+ char *b;
1001
1002
- if (!buffer || !buffer_size)
1003
- return NULL;
1004
+ if (!buffer || !buffer_size)
1005
+ return NULL;
1006
1007
- b = buffer;
1008
+ b = buffer;
1009
1010
- while (--buffer_size && (*b = getc_cur ()) != '\0')
1011
- b++;
1012
+ while (--buffer_size && (*b = getc_cur()) != '\0')
1013
+ b++;
1014
1015
- if (!buffer_size)
1016
- {
1017
- *b = '\0';
1018
- while (getc_cur () != '\0')
1019
- ;
1020
- }
1021
+ if (!buffer_size) {
1022
+ *b = '\0';
1023
+ while (getc_cur() != '\0')
1024
+ ;
1025
+ }
1026
1027
- return buffer;
1028
+ return buffer;
1029
}
1030
1031
/** **/
1032
w_char *
1033
-getws_cur (w_char *buffer, size_t buffer_size)
1034
+getws_cur(w_char *buffer, size_t buffer_size)
1035
{
1036
- w_char *b;
1037
+ w_char *b;
1038
1039
- if (!buffer || !buffer_size)
1040
- return NULL;
1041
+ if (!buffer || !buffer_size)
1042
+ return NULL;
1043
1044
- b = buffer;
1045
+ b = buffer;
1046
1047
- while (--buffer_size && (*b = get2_cur ()) != 0)
1048
- b++;
1049
+ while (--buffer_size && (*b = get2_cur ()) != 0)
1050
+ b++;
1051
1052
- if (!buffer_size)
1053
- {
1054
- *b = 0;
1055
- while (getc_cur () != 0)
1056
- ;
1057
- }
1058
-
1059
- return buffer;
1060
+ if (!buffer_size) {
1061
+ *b = 0;
1062
+ while (getc_cur () != 0)
1063
+ ;
1064
+ }
1065
+ return buffer;
1066
}
1067
1068
/** �����ȡ����饤����Ȥ���2�Х��ȼ�� **/
1069
int
1070
get2_cur (void)
1071
{
1072
- int x;
1073
- x = getc_cur ();
1074
- return (x << 8) | getc_cur ();
1075
+ int h;
1076
+ h = getc_cur () << 8;
1077
+ h |= getc_cur ();
1078
+ return h;
1079
}
1080
1081
/** �����ȡ����饤����Ȥ���4�Х��ȼ�� **/
1082
int
1083
get4_cur (void)
1084
{
1085
- int x1, x2, x3;
1086
- x1 = getc_cur ();
1087
- x2 = getc_cur ();
1088
- x3 = getc_cur ();
1089
- return (x1 << (8 * 3)) | (x2 << (8 * 2)) | (x3 << (8 * 1)) | getc_cur ();
1090
+ int h;
1091
+ h = getc_cur() << (8*3);
1092
+ h |= getc_cur() << (8*2);
1093
+ h |= getc_cur() << (8*1);
1094
+ h |= getc_cur() << (8*0);
1095
+ return h;
1096
}
1097
1098
/** �����ȡ����饤����Ȥ���1�Х��ȼ�� **/
1099
int
1100
-getc_cur (void)
1101
+getc_cur(void)
1102
{
1103
- static int rbp;
1104
- if (rbc <= 0)
1105
- {
1106
- rbc = rcv_1_client (cur_clp);
1107
- rbp = 0;
1108
- }
1109
- rbc--;
1110
- return rcv_buf[rbp++] & 0xFF;
1111
+#if DEBUG_IO
1112
+ fprintf(stderr, "getc_cur: Enter\n");
1113
+#endif
1114
+ if (rp == rbp) {
1115
+ rcv_1_client(cur_clp);
1116
+ }
1117
+#if DEBUG_IO
1118
+ fprintf(stderr, "getc_cur: [%02x]\n", *rbp & 0xff);
1119
+#endif
1120
+ return *(rbp++) & 0xff;
1121
}
1122
1123
/** ���饤����Ȥ���1�ѥ��åȼ�� **/
1124
static int
1125
-rcv_1_client (int clp) /* clp=���饤������ֹ� */
1126
+rcv_1_client(int clp) /* clp=���饤������ֹ� */
1127
{
1128
- int cc = 0;
1129
- while (cc <= 0)
1130
- {
1131
- errno = 0;
1132
+ int n = 0;
1133
+
1134
+ if (rbp == rp) {
1135
+ rbp = rp = &rcv_buf[0];
1136
+ }
1137
+
1138
+ while (rbp == rp) {
1139
+ errno = 0;
1140
#ifdef HAVE_RECV
1141
- cc = recv (cblk[clp].sd, rcv_buf, S_BUF_SIZ, 0);
1142
+ n = recv(cblk[clp].sd, rcv_buf, sizeof(rcv_buf), 0);
1143
#else
1144
- cc = read (cblk[clp].sd, rcv_buf, S_BUF_SIZ);
1145
+ n = read(cblk[clp].sd, rcv_buf, sizeof(rcv_buf));
1146
#endif
1147
- if (cc <= 0)
1148
- {
1149
- if (ERRNO_CHECK (errno))
1150
- {
1151
- continue;
1152
- }
1153
- else if (cc == 0)
1154
- { /* client dead */
1155
- longjmp (client_dead, 666);
1156
- }
1157
- else
1158
- { /* cc == -1 */
1159
- if (errno != EINTR)
1160
- longjmp (client_dead, 666);
1161
- continue;
1162
- }
1163
- }
1164
- }
1165
-#ifdef DEBUG
1166
- log_debug ("rcv: clp = %d, sd = %d, cc = %d", clp, cblk[clp].sd, cc);
1167
- dmp (rcv_buf, cc);
1168
+ if (n <= 0) {
1169
+ if (ERRNO_CHECK (errno)) {
1170
+ continue;
1171
+ } else if (n == 0) {
1172
+ /* client dead */
1173
+ longjmp(client_dead, 666);
1174
+ } else {
1175
+ /* n == -1 */
1176
+ if (errno != EINTR)
1177
+ longjmp (client_dead, 666);
1178
+ continue;
1179
+ }
1180
+ }
1181
+ rp += n;
1182
+ log_debug ("rcv: clp=%d, sd=%d, n=%d\n", clp, cblk[clp].sd, n);
1183
+#if DEBUG_IO
1184
+ dmp(rbp, rp - rbp);
1185
#endif
1186
- return cc;
1187
+ }
1188
+
1189
+ return n;
1190
}
1191
1192
/** ���饤����Ȥ�1�ѥ��å����� **/
1193
static void
1194
-snd_1_client (int clp, /* clp: ���饤������ֹ� */
1195
- int n /* n : number of bytes to send */ )
1196
+snd_1_client(int clp, int dummy)
1197
{
1198
- int cc, x;
1199
-#ifdef DEBUG
1200
- log_debug ("snd: clp = %d, sd = %d", clp, cblk[clp].sd);
1201
- dmp (snd_buf, n);
1202
+ unsigned char *bp = snd_buf;
1203
+ int n;
1204
+ size_t total = sp - bp;
1205
+
1206
+#if DEBUG_IO
1207
+ fprintf(stderr, "snd: clp=%d, sd=%d\n", clp, cblk[clp].sd);
1208
+ dmp(snd_buf, sp - bp);
1209
#endif
1210
- for (cc = 0; cc < n;)
1211
- {
1212
- errno = 0;
1213
+
1214
+ while (0 < sp - bp && sp <= snd_buf + sizeof(snd_buf)) {
1215
+ errno =0;
1216
#ifdef HAVE_SEND
1217
- x = send (cblk[clp].sd, &snd_buf[cc], n - cc, 0);
1218
+ n = send(cblk[clp].sd, bp, sp - bp, 0);
1219
#else
1220
- x = write (cblk[clp].sd, &snd_buf[cc], n - cc);
1221
+ n = write(cblk[clp].sd, bp, sp - bp);
1222
#endif
1223
- if (x < 0)
1224
- {
1225
- if (ERRNO_CHECK (errno) || errno == EINTR)
1226
- {
1227
- errno = 0;
1228
- continue;
1229
- }
1230
- else
1231
- { /* client dead */
1232
- longjmp (client_dead, 666);
1233
- }
1234
- }
1235
- cc += x;
1236
- }
1237
+ if (n < 0) {
1238
+ if (ERRNO_CHECK (errno) || errno == EINTR) {
1239
+ continue;
1240
+ } else {
1241
+ /* client dead */
1242
+ longjmp (client_dead, 666);
1243
+ }
1244
+ }
1245
+ bp += n;
1246
+ }
1247
+ sp = snd_buf;
1248
}
1249
1250
-/** **/
1251
void
1252
-puts_cur (char *p)
1253
+puts_cur(char *p)
1254
{
1255
- int c;
1256
- while (c = *p++)
1257
- putc_cur (c);
1258
- putc_cur (0);
1259
+ int c;
1260
+ while(c = *p++)
1261
+ putc_cur(c);
1262
+ putc_cur(0x00);
1263
}
1264
1265
-/** **/
1266
void
1267
-puts_n_cur (char *p, int n)
1268
+puts_n_cur(char *p, int n)
1269
{
1270
- int c;
1271
- while ((c = *p++) && --n >= 0)
1272
- putc_cur (c);
1273
- putc_cur (0);
1274
+ int c;
1275
+ while ((c = *p++) && --n >= 0)
1276
+ putc_cur(c);
1277
+ putc_cur(0x00);
1278
}
1279
1280
-/** **/
1281
void
1282
-putws_cur (w_char *p)
1283
+putws_cur(w_char *p)
1284
{
1285
- int c;
1286
- while (c = *p++)
1287
- put2_cur (c);
1288
- put2_cur (0);
1289
+ int c;
1290
+ while (c = *p++)
1291
+ put2_cur(c);
1292
+ put2_cur(0x0000);
1293
}
1294
1295
-/** **/
1296
void
1297
-putnws_cur (w_char *p, int n)
1298
+putnws_cur(w_char *p, int n)
1299
{
1300
- int c;
1301
- for (; n > 0; n--)
1302
- {
1303
- if ((c = *p++) == 0)
1304
- break;
1305
- put2_cur (c);
1306
- }
1307
- put2_cur (0);
1308
+ unsigned int c;
1309
+ for (; n > 0; n--) {
1310
+ if ((c = *p++) == 0)
1311
+ break;
1312
+ put2_cur(c);
1313
+ }
1314
+ put2_cur(0x0000);
1315
}
1316
1317
-/** �����ȡ����饤����Ȥ�2�Х������� **/
1318
void
1319
-put2_cur (int c)
1320
+put2_cur(int c)
1321
{
1322
- putc_cur (c >> (8 * 1));
1323
- putc_cur (c);
1324
+ putc_cur(c >> (8 * 1));
1325
+ putc_cur(c);
1326
}
1327
1328
-/** �����ȡ����饤����Ȥ�4�Х������� **/
1329
void
1330
-put4_cur (int c)
1331
+put4_cur(int c)
1332
{
1333
- putc_cur (c >> (8 * 3));
1334
- putc_cur (c >> (8 * 2));
1335
- putc_cur (c >> (8 * 1));
1336
- putc_cur (c);
1337
+ putc_cur(c >> (8 * 3));
1338
+ putc_cur(c >> (8 * 2));
1339
+ putc_cur(c >> (8 * 1));
1340
+ putc_cur(c);
1341
}
1342
1343
-/** �����ȡ����饤����Ȥ�1�Х������� **/
1344
void
1345
-putc_cur (int c)
1346
+putc_cur(int c)
1347
{
1348
- snd_buf[sbp++] = c;
1349
- if (sbp >= R_BUF_SIZ)
1350
- {
1351
- snd_1_client (cur_clp, R_BUF_SIZ);
1352
- sbp = 0;
1353
- }
1354
+#if DEBUG_IO
1355
+ fprintf(stderr, "putc_cur: Enter\n");
1356
+#endif
1357
+ if (snd_buf + sizeof(snd_buf) <= sp)
1358
+ putc_purge();
1359
+
1360
+#if DEBUG_IO
1361
+ fprintf(stderr, "putc_cur: [%02x]\n", c & 0xff);
1362
+#endif
1363
+ *(sp++) = c & 0xff;
1364
}
1365
1366
-/** �����ȡ����饤����Ȥ������Хåե���ե�å��夹�� **/
1367
+/* flush send buffer */
1368
void
1369
-putc_purge (void)
1370
+putc_purge(void)
1371
{
1372
- if (sbp != 0)
1373
- {
1374
- snd_1_client (cur_clp, sbp);
1375
- sbp = 0;
1376
- }
1377
+ if (snd_buf < sp) {
1378
+ snd_1_client(cur_clp, 0);
1379
+ }
1380
}
1381
1382
-/*-----*/
1383
-
1384
-/** �����åȤΥ��˥���饤�� **/
1385
+/* initialize sockets */
1386
#ifdef AF_UNIX
1387
+#if !defined(SUN_LEN)
1388
+# define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
1389
+#endif
1390
+
1391
static void
1392
-socket_init_un (void)
1393
+socket_init_un(int *index)
1394
{
1395
- struct sockaddr_un saddr_un;
1396
- int sock_d_un;
1397
- if (serverNO == 0)
1398
- {
1399
- saddr_un.sun_family = AF_UNIX;
1400
- unlink (sockname);
1401
- strcpy (saddr_un.sun_path, sockname);
1402
- if ((sock_d_un = socket (AF_UNIX, SOCK_STREAM, 0)) == ERROR)
1403
- {
1404
- xerror ("could not create unix domain socket");
1405
- }
1406
- if (bind (sock_d_un, (struct sockaddr *) &saddr_un, strlen (saddr_un.sun_path) + 2) == ERROR)
1407
- {
1408
- shutdown (sock_d_un, 2);
1409
- xerror ("could not bind unix domain socket");
1410
+ struct sockaddr_un saddr_un;
1411
+ int sock_d_un;
1412
+
1413
+ saddr_un.sun_family = AF_UNIX;
1414
+ strncpy(saddr_un.sun_path, sockname, sizeof(saddr_un.sun_path) - 1);
1415
+ saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0';
1416
+
1417
+ unlink(saddr_un.sun_path);
1418
+
1419
+ if ((sock_d_un = socket(saddr_un.sun_family, SOCK_STREAM, 0)) == ERROR)
1420
+ xerror("could not create unix domain socket");
1421
+
1422
+ if (bind(sock_d_un,
1423
+ (struct sockaddr *)&saddr_un,
1424
+ SUN_LEN(&saddr_un)) == ERROR) {
1425
+ shutdown (sock_d_un, 2);
1426
+ xerror("could not bind unix domain socket");
1427
}
1428
- if (listen (sock_d_un, 5) == ERROR)
1429
- {
1430
- shutdown (sock_d_un, 2);
1431
- xerror ("could not listen unix domain socket");
1432
+
1433
+ if (listen(sock_d_un, 5) == ERROR) {
1434
+ shutdown (sock_d_un, 2);
1435
+ xerror("could not listen unix domain socket");
1436
}
1437
- chmod (sockname, 0777);
1438
- signal (SIGPIPE, SIG_IGN);
1439
-#ifdef DEBUG
1440
- log_debug ("sock_d_un = %d", sock_d_un);
1441
-#endif
1442
- accept_blk[UNIX_ACPT].sd = sock_d_un;
1443
- FD_SET (sock_d_un, all_socks);
1444
- }
1445
+
1446
+ chmod(sockname, 0777);
1447
+ signal(SIGPIPE, SIG_IGN);
1448
+ log_debug("sock_d_un = %d (bindex=%d)", sock_d_un, *index);
1449
+ accept_blk[(*index)].sd = sock_d_un;
1450
+ accept_blk[(*index)].domain = saddr_un.sun_family;
1451
+ (*index)++;
1452
+ FD_SET (sock_d_un, all_socks);
1453
}
1454
#endif /* AF_UNIX */
1455
1456
/* Inet V3.0 */
1457
static void
1458
-socket_init_in (void)
1459
+socket_init_in(int *index)
1460
{
1461
+ int i;
1462
#ifndef SOLARIS
1463
- int on = 1;
1464
+ int on = 1;
1465
#else /* SOLARIS */
1466
- int on = 0;
1467
+ int on = 0;
1468
#endif /* SOLARIS */
1469
- struct servent *sp;
1470
+
1471
+ struct servent *sp;
1472
#if !defined(SO_DONTLINGER) && defined(SO_LINGER)
1473
- struct linger linger;
1474
+ struct linger linger;
1475
#endif
1476
-#ifdef INET6
1477
- struct addrinfo hints, *res, *res0;
1478
- int error;
1479
- char sport[6];
1480
-#else
1481
- struct sockaddr_in saddr_in;
1482
-#endif
1483
- int sock_d_in;
1484
-
1485
- if (port < 0)
1486
- {
1487
- if ((sp = getservbyname (SERVERNAME, "tcp")) == NULL)
1488
- {
1489
- port = WNN_PORT_IN;
1490
- }
1491
- else
1492
- {
1493
- port = ntohs (sp->s_port);
1494
- }
1495
- }
1496
+ struct addrinfo hints, *res, *res0;
1497
+ struct sockaddr sa;
1498
+ int error;
1499
+ char hbuf[NI_MAXHOST];
1500
+ char sbuf[NI_MAXSERV];
1501
+ int sock_d_in;
1502
+
1503
+ memset(&sa, 0, sizeof(struct sockaddr));
1504
+ if (port < 0) {
1505
+ strncpy(sbuf, SERVERNAME, sizeof(sbuf) - 1);
1506
+ sbuf[sizeof(sbuf) - 1] = '\0';
1507
+ error = getnameinfo(&sa,
1508
+ sa.sa_len,
1509
+ NULL,
1510
+ 0,
1511
+ sbuf,
1512
+ sizeof(sbuf),
1513
+ NI_NUMERICSERV);
1514
+ if (error)
1515
+ sprintf(sbuf, "%d", WNN_PORT_IN);
1516
+ } else {
1517
+ sprintf(sbuf, "%d", port);
1518
+ }
1519
1520
- port += serverNO;
1521
+ port += serverNO;
1522
1523
-#if DEBUG
1524
- log_debug ("port=%x", port);
1525
-#endif
1526
-#ifdef INET6
1527
- memset(&hints, 0, sizeof(hints));
1528
- if (listen_proto&PROTO_INET && !(listen_proto&PROTO_INET6))
1529
- hints.ai_family = PF_INET;
1530
- else if (listen_proto&PROTO_INET6 && !(listen_proto&PROTO_INET))
1531
- hints.ai_family = PF_INET6;
1532
- else
1533
- hints.ai_family = PF_UNSPEC;
1534
- hints.ai_socktype = SOCK_STREAM;
1535
- hints.ai_flags = AI_PASSIVE;
1536
- sprintf(sport, "%d", port);
1537
- error = getaddrinfo(NULL, sport, &hints, &res0);
1538
- if (error)
1539
- {
1540
- xerror (gai_strerror(error));
1541
- }
1542
- for (res = res0; res; res = res->ai_next) {
1543
- if (res->ai_family == AF_INET || res->ai_family == AF_INET6){
1544
- if ((sock_d_in = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) == ERROR)
1545
-#else
1546
- saddr_in.sin_family = AF_INET;
1547
- saddr_in.sin_port = htons (port);
1548
- saddr_in.sin_addr.s_addr = htonl (INADDR_ANY);
1549
- if ((sock_d_in = socket (AF_INET, SOCK_STREAM, 0)) == ERROR)
1550
-#endif
1551
- {
1552
+ memset(&hints, 0, sizeof(hints));
1553
+ if (listen_proto & PROTO_INET
1554
+ || listen_proto & PROTO_INET6)
1555
+ hints.ai_family = PF_UNSPEC;
1556
+ hints.ai_socktype = SOCK_STREAM;
1557
+ hints.ai_flags = AI_PASSIVE;
1558
+
1559
+ for (i = 0; i < MAXLISTENADDR && listenaddr[i][0] != '\0'; i++) {
1560
+ log_debug("getaddrinfo: try %s",listenaddr[i]);
1561
+
1562
+ if (error = getaddrinfo(listenaddr[i], sbuf, &hints, &res0))
1563
+ xerror((char *)gai_strerror(error));
1564
+
1565
+ for (res = res0; res; res = res->ai_next) {
1566
+ log_debug("socket: try %s : %s (type=%d)",
1567
+ listenaddr[i], sbuf, res->ai_family);
1568
+
1569
+ switch (res->ai_family) {
1570
+ case AF_INET:
1571
+ if (!(listen_proto & PROTO_INET)) {
1572
+ log_debug("socket: ignore %s\n", listenaddr[i]);
1573
+ continue;
1574
+ }
1575
+ sock_d_in = socket(res->ai_family,
1576
+ res->ai_socktype,
1577
+ res->ai_protocol);
1578
+ if (sock_d_in == -1)
1579
+ xerror("could not create inet socket");
1580
+ break;
1581
#ifdef INET6
1582
- if (res->ai_family == AF_INET6)
1583
- xerror ("could not create inet6 socket");
1584
- else if (res->ai_family == AF_INET)
1585
+ case AF_INET6:
1586
+ if (!(listen_proto & PROTO_INET6)) {
1587
+ log_debug("socket: ignore %s\n", listenaddr[i]);
1588
+ continue;
1589
+ }
1590
+ sock_d_in = socket(res->ai_family,
1591
+ res->ai_socktype,
1592
+ res->ai_protocol);
1593
+ if (sock_d_in == -1)
1594
+ xerror("could not create inet6 socket");
1595
+#ifdef IPV6_V6ONLY
1596
+ setsockopt(sock_d_in, IPPROTO_IPV6, IPV6_V6ONLY, NULL, 0);
1597
#endif
1598
- xerror ("could not create inet socket");
1599
- }
1600
- setsockopt (sock_d_in, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (int));
1601
+#endif /* INET6 */
1602
+ default:
1603
+ continue;
1604
+ }
1605
+ setsockopt (sock_d_in, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (int));
1606
#ifdef SO_DONTLINGER
1607
- setsockopt (sock_d_in, SOL_SOCKET, SO_DONTLINGER, (char *) 0, 0);
1608
+ setsockopt (sock_d_in, SOL_SOCKET, SO_DONTLINGER, (char *) 0, 0);
1609
#else
1610
# ifdef SO_LINGER
1611
- linger.l_onoff = 0;
1612
- linger.l_linger = 0;
1613
- setsockopt (sock_d_in, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof linger);
1614
+ linger.l_onoff = 0;
1615
+ linger.l_linger = 0;
1616
+ setsockopt(sock_d_in, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof linger);
1617
# endif /* SO_LINGER */
1618
#endif /* SO_DONTLINGER */
1619
1620
-#ifdef INET6
1621
- if (bind (sock_d_in, res->ai_addr, res->ai_addrlen) == ERROR)
1622
-#else
1623
- if (bind (sock_d_in, (struct sockaddr *) &saddr_in, sizeof (saddr_in)) == ERROR)
1624
-#endif
1625
- {
1626
- shutdown (sock_d_in, 2);
1627
-#ifdef INET6
1628
- if (res->ai_family == AF_INET6)
1629
- xerror ("can't bind inet6 socket");
1630
- else if (res->ai_family == AF_INET)
1631
-#endif
1632
- xerror ("can't bind inet socket");
1633
- }
1634
- if (listen (sock_d_in, 5) == ERROR)
1635
- {
1636
- shutdown (sock_d_in, 2);
1637
-#ifdef INET6
1638
- if (res->ai_family == AF_INET6)
1639
- xerror ("can't listen inet6 socket");
1640
- else if (res->ai_family == AF_INET)
1641
-#endif
1642
- xerror ("can't listen inet socket");
1643
- }
1644
-#if DEBUG
1645
- log_debug ("sock_d_in = %d", sock_d_in);
1646
-#endif
1647
- FD_SET (sock_d_in, all_socks);
1648
-#ifdef INET6
1649
- if (res->ai_family == AF_INET)
1650
- accept_blk[INET_ACPT].sd = sock_d_in;
1651
- else if (res->ai_family == AF_INET6)
1652
- accept_blk[INET6_ACPT].sd = sock_d_in;
1653
- }
1654
- }
1655
- freeaddrinfo(res0);
1656
-#else
1657
- accept_blk[INET_ACPT].sd = sock_d_in;
1658
-#endif
1659
+ if (bind(sock_d_in, res->ai_addr, res->ai_addrlen) == ERROR) {
1660
+ shutdown (sock_d_in, 2);
1661
+ xerror("could not bind inet/inet6 socket");
1662
+ }
1663
+
1664
+ if (listen(sock_d_in, 5) == ERROR) {
1665
+ shutdown (sock_d_in, 2);
1666
+ xerror("could not listen inet/inet6 socket");
1667
+ }
1668
+
1669
+ log_debug("sock_d_in = %d (bindex=%d)",
1670
+ sock_d_in, *index);
1671
+ FD_SET(sock_d_in, all_socks);
1672
+ accept_blk[(*index)].sd = sock_d_in;
1673
+ accept_blk[(*index)].domain = res->ai_family;
1674
+ (*index)++;
1675
+ }
1676
+ }
1677
+ freeaddrinfo(res0);
1678
}
1679
1680
-
1681
/** accept new client socket **/
1682
-#ifdef AF_UNIX
1683
static int
1684
-socket_accept_un (void)
1685
+socket_accept(int index)
1686
{
1687
- struct sockaddr_un addr;
1688
- socklen_t addrlen;
1689
-
1690
- addrlen = sizeof (addr);
1691
- return accept (accept_blk[UNIX_ACPT].sd, (struct sockaddr *) &addr, &addrlen);
1692
-}
1693
-#endif /* AF_UNIX */
1694
-
1695
-static int
1696
-socket_accept_in (int fd)
1697
-{
1698
- struct sockaddr_in addr;
1699
- socklen_t addrlen;
1700
-
1701
- addrlen = sizeof (addr);
1702
- return accept (fd, (struct sockaddr *) &addr, &addrlen);
1703
+ return accept(accept_blk[index].sd, NULL, NULL);
1704
}
1705
1706
static void
1707
-xerror (char *s)
1708
+xerror(char *s)
1709
{
1710
- log_err ("%s (%s).", s, strerror(errno));
1711
- exit (1);
1712
+ log_err ("%s (%s).", s, strerror(errno));
1713
+ exit (1);
1714
}
1715
1716
-#if DEBUG
1717
static void
1718
-dmp (char *p, int n)
1719
+dmp(char *p, int n)
1720
{
1721
- int i, j;
1722
+ int i, j;
1723
1724
- for (i = 0; i < n; i += 16)
1725
- {
1726
- for (j = 0; j < 16; j++)
1727
- {
1728
- fprintf (stderr, "%02x ", p[i + j] & 0xFF);
1729
- }
1730
- fprintf (stderr, "n=%d\n", n);
1731
- }
1732
+ for (i = 0; i < n; i += 16) {
1733
+ for (j = 0; j < 16; j++) {
1734
+ fprintf (stderr, "%02x ", p[i + j] & 0xFF);
1735
+ }
1736
+ fprintf (stderr, "n=%d\n", n);
1737
+ }
1738
}
1739
-#endif
1740
1741
static void
1742
-get_options (int argc, char **argv)
1743
+get_options(int argc, char **argv)
1744
{
1745
- int c;
1746
- int digit_optind = 0;
1747
-
1748
- strcpy (jserverrcfile, LIBDIR); /* usr/local/lib/wnn */
1749
- strcat (jserverrcfile, SERVER_INIT_FILE); /* ja_JP/jserverrc */
1750
+ int c;
1751
+ int digit_optind = 0;
1752
+ int lindex = 0;
1753
1754
- while (1)
1755
- {
1756
- int this_option_optind = optind ? optind : 1;
1757
- int option_index = 0;
1758
- static struct option long_options[] =
1759
- {
1760
- {"baseport", 1, NULL, 'p'},
1761
- {"inet", 0, NULL, '4'},
1762
- {"inet6", 0, NULL, '6'},
1763
- {"jserverrc", 1, NULL, 'f'},
1764
- {"version", 0, NULL, 'v'},
1765
- {0, 0, 0, 0}
1766
- };
1767
-
1768
- c = getopt_long (argc, argv, OPTIONARGS,
1769
- long_options, &option_index);
1770
- if (c == -1)
1771
- break;
1772
+ strcpy (jserverrcfile, LIBDIR); /* usr/local/lib/wnn */
1773
+ strcat (jserverrcfile, SERVER_INIT_FILE); /* ja_JP/jserverrc */
1774
1775
- switch (c)
1776
+ while (1)
1777
{
1778
- case 'D': /* do not detach, not a daemon */
1779
- option_flag &= ~OPT_FORK;
1780
- break;
1781
-
1782
- case 'f': /* --jserverrc FILENAME */
1783
- strcpy (jserverrcfile, optarg);
1784
- break;
1785
-
1786
- case 's':
1787
- /* should nuke noisy someday */
1788
- noisy = 1; option_flag |= OPT_VERBOSE;
1789
- if (strcmp ("-", optarg) != 0)
1790
- {
1791
- /** maybe FILE wnnerr = stderr; or wnnerr = open(optarg...) is better? or freopen is normal method? */
1792
- /** take a look at daemon(3) */
1793
- if (freopen (optarg, "a", stderr) == NULL)
1794
- {
1795
- /** fprintf to stderr? */
1796
- printf ("Error in opening scriptfile %s.\n", optarg);
1797
- exit (1);
1798
- }
1799
- }
1800
- log_debug ("script started");
1801
- break;
1802
-
1803
- case 'h':
1804
- /* var hinsi_file_name polluted */
1805
- hinsi_file_name = optarg;
1806
- break;
1807
-
1808
- case 'N':
1809
- serverNO = atoi (optarg);
1810
- /* error handling needed */
1811
- break;
1812
-
1813
- case 'p':
1814
- port = atoi (optarg);
1815
- /* error handling needed */
1816
- break;
1817
-
1818
- case 'v':
1819
- print_version();
1820
- usage();
1821
- break;
1822
-
1823
- case 'u':
1824
- listen_proto &= ~PROTO_ALL;
1825
- listen_proto |= PROTO_UN;
1826
- break;
1827
-
1828
- case '4':
1829
- listen_proto &= ~PROTO_ALL;
1830
- listen_proto |= PROTO_INET;
1831
- break;
1832
+ int this_option_optind = optind ? optind : 1;
1833
+ int option_index = 0;
1834
+ static struct option long_options[] =
1835
+ {
1836
+ {"baseport", 1, NULL, 'p'},
1837
+ {"inet", 0, NULL, '4'},
1838
+ {"inet6", 0, NULL, '6'},
1839
+ {"jserverrc", 1, NULL, 'f'},
1840
+ {"listenaddr", 1, NULL, 'a'},
1841
+ {"unix", 0, NULL, 'u'},
1842
+ {"version", 0, NULL, 'v'},
1843
+ {0, 0, 0, 0}
1844
+ };
1845
+
1846
+ c = getopt_long (argc, argv, OPTIONARGS,
1847
+ long_options, &option_index);
1848
+ if (c == -1)
1849
+ break;
1850
+
1851
+ switch (c)
1852
+ {
1853
+ case 'D': /* do not detach, not a daemon */
1854
+ option_flag &= ~OPT_FORK;
1855
+ break;
1856
+
1857
+ case 'f': /* --jserverrc FILENAME */
1858
+ strncpy(jserverrcfile, optarg, sizeof(jserverrcfile) - 1);
1859
+ jserverrcfile[sizeof(jserverrcfile) - 1] = '\0';
1860
+ break;
1861
+
1862
+ case 'a': /* --listenaddr ADDR */
1863
+ strncpy(listenaddr[lindex], optarg, NI_MAXHOST - 1);
1864
+ listenaddr[lindex][NI_MAXHOST - 1] = '\0';
1865
+ lindex++;
1866
+ break;
1867
+
1868
+ case 's':
1869
+ /* should nuke noisy someday */
1870
+ noisy = 1; option_flag |= OPT_VERBOSE;
1871
+ if (strcmp ("-", optarg) != 0)
1872
+ {
1873
+ /** maybe FILE wnnerr = stderr; or wnnerr = open(optarg...) is better? or freopen is normal method? */
1874
+ /** take a look at daemon(3) */
1875
+ if (freopen (optarg, "a", stderr) == NULL)
1876
+ {
1877
+ /** fprintf to stderr? */
1878
+ printf ("Error in opening scriptfile %s.\n", optarg);
1879
+ exit (1);
1880
+ }
1881
+ }
1882
+ log_debug ("script started");
1883
+ break;
1884
+
1885
+ case 'h':
1886
+ /* var hinsi_file_name polluted */
1887
+ hinsi_file_name = optarg;
1888
+ break;
1889
+
1890
+ case 'N':
1891
+ serverNO = atoi (optarg);
1892
+ /* error handling needed */
1893
+ break;
1894
+
1895
+ case 'p':
1896
+ port = atoi (optarg);
1897
+ /* error handling needed */
1898
+ break;
1899
+
1900
+ case 'v':
1901
+ print_version();
1902
+ usage();
1903
+ break;
1904
+
1905
+ case 'u':
1906
+ listen_proto |= PROTO_UN;
1907
+ break;
1908
+
1909
+ case '4':
1910
+ listen_proto |= PROTO_INET;
1911
+ break;
1912
1913
#ifdef INET6
1914
- case '6':
1915
- listen_proto &= ~PROTO_ALL;
1916
- listen_proto |= PROTO_INET6;
1917
- break;
1918
+ case '6':
1919
+ listen_proto |= PROTO_INET6;
1920
+ break;
1921
#endif /* INET6 */
1922
-
1923
- default:
1924
- print_version();
1925
- usage();
1926
- break;
1927
+ default:
1928
+ print_version();
1929
+ usage();
1930
+ break;
1931
+ }
1932
+ }
1933
+ if (!listen_proto) {
1934
+ listen_proto = PROTO_ALL;
1935
}
1936
- }
1937
}
1938
1939
1940
-/*
1941
-*/
1942
void
1943
js_who (void)
1944
{
1945
- int i, j;
1946
+ int i, j;
1947
1948
- put4_cur (clientp);
1949
- for (i = 0; i < clientp; i++)
1950
- {
1951
- put4_cur (cblk[i].sd);
1952
- puts_cur (client[i].user_name);
1953
- puts_cur (client[i].host_name);
1954
- for (j = 0; j < WNN_MAX_ENV_OF_A_CLIENT; j++)
1955
- {
1956
- put4_cur ((client[i].env)[j]);
1957
- }
1958
+ put4_cur (clientp);
1959
+ for (i = 0; i < clientp; i++)
1960
+ {
1961
+ put4_cur (cblk[i].sd);
1962
+ puts_cur (client[i].user_name);
1963
+ puts_cur (client[i].host_name);
1964
+ for (j = 0; j < WNN_MAX_ENV_OF_A_CLIENT; j++)
1965
+ {
1966
+ put4_cur ((client[i].env)[j]);
1967
+ }
1968
1969
- }
1970
- putc_purge ();
1971
+ }
1972
+ putc_purge();
1973
}
1974
1975
void
1976
@@ -1302,25 +1232,25 @@ js_kill (void)
1977
{
1978
if (clientp == 1)
1979
{
1980
- put4_cur (0);
1981
- putc_purge ();
1982
+ put4_cur(0x00000000);
1983
+ putc_purge();
1984
terminate_hand ();
1985
}
1986
else
1987
{
1988
put4_cur (clientp - 1);
1989
- putc_purge ();
1990
+ putc_purge();
1991
}
1992
}
1993
1994
void
1995
usage (void)
1996
{
1997
- fprintf(stderr,
1998
+ fprintf(stderr,
1999
#ifdef INET6
2000
- "usage: %s [-Du46][-f <init_file> -s <log_file(\"-\" for stderr)> -h <pos_file> -N <serverNO> -p <port_base>]\n",
2001
+ "usage: %s [-Du46][-f <init_file> -a <listenaddr> -s <log_file(\"-\" for stderr)> -h <pos_file> -N <serverNO> -p <port_base>]\n",
2002
#else
2003
- "usage: %s [-Du4][-f <init_file> -s <log_file(\"-\" for stderr)> -h <pos_file> -N <serverNO> -p <port_base>]\n",
2004
+ "usage: %s [-Du4][-f <init_file> -a <listenaddr> -s <log_file(\"-\" for stderr)> -h <pos_file> -N <serverNO> -p <port_base>]\n",
2005
#endif
2006
cmd_name);
2007
fprintf(stderr,
2008
2009