]> jfr.im git - irc/rqf/shadowircd.git/blob - servlink/servlink.c
[svn] - the new plan:
[irc/rqf/shadowircd.git] / servlink / servlink.c
1 /************************************************************************
2 * IRC - Internet Relay Chat, servlink/servlink.c
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 1, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * $Id: servlink.c 6 2005-09-10 01:02:21Z nenolod $
19 */
20
21 #include "setup.h"
22
23 #include <sys/types.h>
24 #include <sys/time.h>
25 #include <sys/socket.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31
32 #ifdef HAVE_LIBZ
33 #include <zlib.h>
34 #endif
35
36 #include "servlink.h"
37 #include "io.h"
38 #include "control.h"
39
40 static void usage(void);
41
42 struct slink_state in_state;
43 struct slink_state out_state;
44
45 struct fd_table fds[3] = {
46 {0, read_ctrl, NULL}, /* ctrl */
47 {0, NULL, NULL}, /* data */
48 {0, NULL, NULL}, /* net */
49 };
50
51 /* usage();
52 *
53 * Display usage message
54 */
55 static void
56 usage(void)
57 {
58 fprintf(stderr, "ircd-ratbox server link v1.2\n");
59 fprintf(stderr, "2004-03-02\n");
60 fprintf(stderr, "\n");
61 fprintf(stderr, "This program is called by the ircd-ratbox ircd.\n");
62 fprintf(stderr, "It cannot be used on its own.\n");
63 exit(1);
64 }
65
66 int
67 main(int argc, char *argv[])
68 {
69 int max_fd = 0;
70 int i, x;
71 #ifdef SERVLINK_DEBUG
72 int GDBAttached = 0;
73
74 while (!GDBAttached)
75 sleep(1);
76 #endif
77
78 /* Make sure we are running under ircd.. */
79
80 if(argc != 4 || strcmp(argv[0], "-slink"))
81 usage(); /* exits */
82
83
84 for (i = 0; i < 3; i++)
85 {
86 fds[i].fd = atoi(argv[i + 1]);
87 if(fds[i].fd < 0)
88 exit(1);
89 }
90
91 for (i = 0; i < 3; i++)
92 {
93 /* XXX: Hack alert...we need to do dup2() here for some dumb
94 * platforms (Solaris) that don't like select using fds > 255
95 */
96
97 if(fds[i].fd >= 255)
98 {
99 for(x = 0; x < 255; x++)
100 {
101 if(x != fds[0].fd && x != fds[1].fd && x != fds[2].fd)
102 {
103 if(dup2(fds[i].fd, x) < 0)
104 exit(1);
105 close(fds[i].fd);
106 fds[i].fd = x;
107 break;
108 }
109 }
110 }
111 fcntl(fds[i].fd, F_SETFL, O_NONBLOCK);
112 if(fds[i].fd > max_fd)
113 max_fd = fds[i].fd;
114 }
115
116 /* enter io loop */
117 io_loop(max_fd + 1);
118
119 /* NOTREACHED */
120 return (0);
121 } /* main() */