2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1995-03-27 08:34:30 quinn
8 * Added dynamic server functionality.
9 * Released bindings to session.c (is now redundant)
11 * Revision 1.8 1995/03/20 09:46:26 quinn
14 * Revision 1.7 1995/03/16 13:29:04 quinn
17 * Revision 1.6 1995/03/15 15:18:52 quinn
18 * Little changes to better support nonblocking I/O
21 * Revision 1.5 1995/03/15 08:37:45 quinn
22 * Now we're pretty much set for nonblocking I/O.
24 * Revision 1.4 1995/03/14 16:59:48 quinn
27 * Revision 1.3 1995/03/14 11:30:15 quinn
30 * Revision 1.2 1995/03/14 10:28:03 quinn
31 * More work on demo server.
33 * Revision 1.1 1995/03/10 18:22:45 quinn
34 * The rudiments of an asynchronous server.
39 * Simple, static server. I wouldn't advise a static server unless you
40 * really have to, but it's great for debugging memory management. :)
60 static int dynamic = 0; /* fork on incoming connection */
62 #define DEFAULT_LISTENER "tcp:localhost:9999"
65 * handle incoming connect requests.
66 * The dynamic mode is a bit tricky mostly because we want to avoid
67 * doing all of the listening and accepting in the parent - it's
70 static void listener(IOCHAN h, int event)
72 COMSTACK line = (COMSTACK) iochan_getdata(h);
78 if (event == EVENT_INPUT)
80 if (dynamic && !child)
89 if ((res = fork()) < 0)
94 else if (res == 0) /* child */
102 /* wait for child to take the call */
108 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
110 perror("handshake read");
116 fprintf(stderr, "P: Child has taken the call\n");
121 if ((res = cs_listen(line, 0, 0)) < 0)
123 fprintf(stderr, "cs_listen failed.\n");
128 iochan_setevent(h, EVENT_OUTPUT);
129 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
131 /* in dynamic mode, only the child ever comes down here */
132 else if (event == EVENT_OUTPUT)
137 if (!(new_line = cs_accept(line)))
139 fprintf(stderr, "Accept failed.\n");
140 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
146 /* close our half of the listener sockets */
147 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
149 COMSTACK l = iochan_getdata(pp);
154 fprintf(stderr, "Releasing parent\n");
156 fprintf(stderr, "New fd is %d\n", cs_fileno(new_line));
159 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
161 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
164 fprintf(stderr, "Failed to create iochan\n");
167 if (!(newas = create_association(new_chan, new_line)))
169 fprintf(stderr, "Failed to create new assoc.\n");
172 iochan_setdata(new_chan, newas);
176 fprintf(stderr, "Bad event on listener.\n");
182 * Set up a listening endpoint, and give it to the event-handler.
184 static void add_listener(char *where, int what)
188 char mode[100], addr[100];
192 fprintf(stderr, "Adding %s listener on %s\n",
193 what == PROTO_SR ? "SR" : "Z3950", where);
194 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
196 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
200 if (!strcmp(mode, "tcp"))
202 if (!(ap = tcpip_strtoaddr(addr)))
204 fprintf(stderr, "Address resolution failed for TCP.\n");
209 else if (!strcmp(mode, "osi"))
211 if (!(ap = mosi_strtoaddr(addr)))
213 fprintf(stderr, "Address resolution failed for TCP.\n");
220 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
223 if (!(l = cs_create(type, 0, what)))
225 fprintf(stderr, "Failed to create listener\n");
228 if (cs_bind(l, ap, CS_SERVER) < 0)
230 fprintf(stderr, "Failed to bind.\n");
234 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
237 fprintf(stderr, "Failed to create IOCHAN-type\n");
240 iochan_setdata(lst, l);
243 static void catchchld(int num)
245 while (waitpid(-1, 0, WNOHANG) > 0);
246 signal(SIGCHLD, catchchld);
249 int statserv_main(int argc, char **argv)
251 int ret, listeners = 0;
253 int protocol = CS_Z3950;;
256 while ((ret = options("szdl:", argv, argc, &arg)) != -2)
260 add_listener(arg, protocol);
263 case 'z': protocol = CS_Z3950; break;
264 case 's': protocol = CS_SR; break;
265 case 'd': dynamic = 1; break;
267 fprintf(stderr, "Usage: %s [ -zsd <listener-addr> ... ]\n", me);
271 signal(SIGCHLD, catchchld);
273 add_listener(DEFAULT_LISTENER, protocol);