1 #include <ccan/oserver/oserver.h>
2 #include <ccan/oserver/oserver_types.h>
3 #include <ccan/oserver/oserver_cdump.h>
4 #include <ccan/read_write_all/read_write_all.h>
5 #include <ccan/opt/opt.h>
6 #include <ccan/tevent/tevent.h>
7 #include <ccan/array_size/array_size.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netinet/tcp.h>
21 #include <sys/types.h>
25 static uint16_t state_flag_map[] = {
26 [SENDING_GREETING] = TEVENT_FD_WRITE,
27 [RECEIVING_USER_QUESTION] = TEVENT_FD_READ,
28 [SENDING_OTHER_QUESTION_PREFIX] = TEVENT_FD_WRITE,
29 [SENDING_OTHER_QUESTION] = TEVENT_FD_WRITE,
30 [RECEIVING_OTHER_ANSWER] = TEVENT_FD_READ,
31 [SENDING_ANSWER_PREFIX] = TEVENT_FD_WRITE,
32 [SENDING_ANSWER] = TEVENT_FD_WRITE,
36 static ssize_t write_string(int fd, const char *str)
38 return write(fd, str, strlen(str));
41 static ssize_t read_string(int fd, char **buf)
43 ssize_t ret, len, maxlen;
46 maxlen = talloc_array_length(*buf);
48 if (maxlen < len + 100) {
50 *buf = talloc_realloc(NULL, *buf, char, maxlen);
53 ret = read(fd, *buf + len, maxlen - len - 1);
55 (*buf)[len + ret] = '\0';
59 static bool input_finished(const char *str)
61 return strchr(str, '\n');
64 /* Update state, and set our READ/WRITE flags appropriately. */
65 static void set_state(struct client *c, enum state state)
68 tevent_fd_set_flags(c->fde, state_flag_map[state]);
71 /* Returns false on error, increments state on finishing string. */
72 static bool send_string(struct client *c, const char *str)
74 ssize_t len = write_string(c->fd, str + c->bytes_sent);
78 if (c->bytes_sent == strlen(str)) {
80 set_state(c, c->state+1);
85 static bool get_subclient(struct client *me)
89 for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
90 struct client *c = me->oserver->clients[i];
93 if (c->oracle == -1 && input_finished(c->question)) {
94 me->subclient = c->id;
102 static bool get_oracle(struct client *me)
106 for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
107 struct client *c = me->oserver->clients[i];
110 if (c->subclient == -1 && input_finished(c->question)) {
112 c->subclient = me->id;
119 static void wakeup(struct client *c)
121 tevent_fd_set_flags(c->fde, state_flag_map[c->state]);
124 static void service_client(struct tevent_context *ev,
125 struct tevent_fd *fde, uint16_t flags, void *_c)
127 struct client *c = _c;
131 case SENDING_GREETING:
132 if (!send_string(c, "Welcome. Please ask your question.\n"))
135 case RECEIVING_USER_QUESTION:
136 len = read_string(c->fd, &c->question);
139 if (input_finished(c->question))
140 set_state(c, SENDING_OTHER_QUESTION_PREFIX);
142 case SENDING_OTHER_QUESTION_PREFIX:
143 if (c->subclient == -1)
145 if (!send_string(c, "While the Oracle ponders,"
146 " please answer the following question:\n"))
149 case SENDING_OTHER_QUESTION:
150 if (c->subclient == -1)
153 c->oserver->clients[c->subclient]->question))
156 case RECEIVING_OTHER_ANSWER:
157 if (c->subclient == -1)
159 len = read_string(c->fd,
160 &c->oserver->clients[c->subclient]->answer);
163 if (input_finished(c->oserver->clients[c->subclient]->answer)) {
164 set_state(c, SENDING_ANSWER_PREFIX);
165 wakeup(c->oserver->clients[c->subclient]);
168 case SENDING_ANSWER_PREFIX:
169 if (!input_finished(c->answer))
171 if (!send_string(c, "The Oracle spake thus:\n"))
175 if (!send_string(c, c->answer))
182 if (c->state != FINISHED)
189 if (!get_subclient(c)) {
190 /* We can't get one: go to sleep until someone find_oracle() */
191 tevent_fd_set_flags(c->fde, 0);
193 /* In case they are waiting... */
194 wakeup(c->oserver->clients[c->subclient]);
198 /* If we don't have an oracle and find one, that's OK. */
199 if (c->oracle == -1 && get_oracle(c)) {
200 /* In case they are waiting... */
201 wakeup(c->oserver->clients[c->oracle]);
205 /* Either our oracle is not finished, or we don't have one: sleep. */
206 tevent_fd_set_flags(c->fde, 0);
209 static int cleanup_client(struct client *client)
212 /* We were an oracle? */
213 if (client->subclient >= 0)
214 client->oserver->clients[client->subclient]->oracle = -1;
216 /* We had an oracle? */
217 if (client->oracle >= 0)
218 client->oserver->clients[client->oracle]->subclient = -1;
220 assert(client->oserver->clients[client->id] == client);
221 client->oserver->clients[client->id] = NULL;
225 static void add_client(struct tevent_context *ev,
226 struct tevent_fd *fde, uint16_t flags, void *_oserver)
228 struct oserver *oserver = _oserver;
229 struct client *client;
231 client = talloc(oserver, struct client);
232 client->fd = accept(oserver->fd, NULL, 0);
234 err(1, "Accepting client connection");
236 client->state = SENDING_GREETING;
237 client->bytes_sent = 0;
238 client->question = talloc_strdup(client, "");
239 client->oserver = oserver;
241 client->subclient = -1;
242 client->answer = talloc_strdup(client, "");
243 client->fde = tevent_add_fd(ev, client, client->fd,
244 state_flag_map[client->state],
245 service_client, client);
246 tevent_fd_set_auto_close(client->fde);
248 /* Find empty slot in array for this client. */
249 for (client->id = 0; oserver->clients[client->id]; client->id++);
250 oserver->clients[client->id] = client;
251 talloc_set_destructor(client, cleanup_client);
253 /* Full? Stop listening... */
254 if (client->id == ARRAY_SIZE(oserver->clients)-1)
255 tevent_fd_set_flags(oserver->fde, 0);
258 static void clear_clients(struct oserver *oserver)
260 memset(oserver->clients, 0,
261 ARRAY_SIZE(oserver->clients) * sizeof(oserver->clients[0]));
264 static int destroy_oserver(struct oserver *oserver)
270 static void talloc_dump(struct tevent_context *ev,
271 struct tevent_signal *se,
277 struct oserver *oserver = _oserver;
280 /* Fork off a child for the report, so we aren't stopped. */
282 f = fopen("/var/run/oserver/talloc.dump", "w");
284 talloc_report_full(oserver, f);
291 static void dump(struct tevent_context *ev,
292 struct tevent_signal *se,
298 struct oserver *oserver = _oserver;
302 str = cdump_bundle(ev, cdump_struct_oserver, oserver);
303 fd = open(oserver->dumpfile, O_CREAT|O_TRUNC|O_WRONLY, 0600);
304 write(fd, str, strlen(str));
309 struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port,
310 const char *dumpfile)
312 struct oserver *oserver;
315 struct sockaddr addr;
316 struct sockaddr_in in;
319 oserver = talloc(ev, struct oserver);
320 clear_clients(oserver);
321 oserver->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
322 if (oserver->fd < 0) {
323 talloc_free(oserver);
327 talloc_set_destructor(oserver, destroy_oserver);
329 if (setsockopt(oserver->fd, SOL_SOCKET, SO_REUSEADDR,
331 warn("Setting socket reuse");
333 u.in.sin_family = AF_INET;
334 u.in.sin_port = htons(port);
335 u.in.sin_addr.s_addr = INADDR_ANY;
336 if (bind(oserver->fd, &u.addr, sizeof(u.in)) == -1) {
337 talloc_free(oserver);
341 if (listen(oserver->fd, 0) != 0) {
342 talloc_free(oserver);
346 oserver->fde = tevent_add_fd(ev, oserver, oserver->fd,
347 TEVENT_FD_READ, add_client, oserver);
349 talloc_free(oserver);
353 oserver->dumpfile = dumpfile;
354 if (oserver->dumpfile) {
355 tevent_add_signal(ev, oserver, SIGHUP, SA_RESTART,
359 /* Don't kill us if client dies. */
360 signal(SIGPIPE, SIG_IGN);
362 /* Show talloc tree on SIGUSR1. */
363 tevent_add_signal(ev, oserver, SIGUSR1, SA_RESTART,
364 talloc_dump, oserver);