1 #include <ccan/oserver/oserver.h>
2 #include <ccan/read_write_all/read_write_all.h>
3 #include <ccan/opt/opt.h>
4 #include <ccan/tevent/tevent.h>
5 #include <ccan/array_size/array_size.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <netinet/tcp.h>
22 RECEIVING_USER_QUESTION,
23 SENDING_OTHER_QUESTION_PREFIX,
24 SENDING_OTHER_QUESTION,
25 RECEIVING_OTHER_ANSWER,
26 SENDING_ANSWER_PREFIX,
31 static uint16_t state_flag_map[] = {
32 [SENDING_GREETING] = TEVENT_FD_WRITE,
33 [RECEIVING_USER_QUESTION] = TEVENT_FD_READ,
34 [SENDING_OTHER_QUESTION_PREFIX] = TEVENT_FD_WRITE,
35 [SENDING_OTHER_QUESTION] = TEVENT_FD_WRITE,
36 [RECEIVING_OTHER_ANSWER] = TEVENT_FD_READ,
37 [SENDING_ANSWER_PREFIX] = TEVENT_FD_WRITE,
38 [SENDING_ANSWER] = TEVENT_FD_WRITE,
43 /* What are we doing today, brain? */
45 /* Our event info, and the file descriptor. */
46 struct tevent_fd *fde;
48 /* The question we read from client. */
50 /* The answer to the client. */
52 /* How many bytes of the reply we sent so far. */
55 struct oserver *oserver;
56 /* Whose question this client is answering. */
57 struct client *subclient;
58 /* Who is answering our question. */
59 struct client *oracle;
63 /* 5 clients should be enough for anybody! */
64 struct client *clients[5];
66 struct tevent_fd *fde;
69 static ssize_t write_string(int fd, const char *str)
71 return write(fd, str, strlen(str));
74 static ssize_t read_string(int fd, char **buf)
76 ssize_t ret, len, maxlen;
79 maxlen = talloc_array_length(*buf);
81 if (maxlen < len + 100) {
83 *buf = talloc_realloc(NULL, *buf, char, maxlen);
86 ret = read(fd, *buf + len, maxlen - len - 1);
88 (*buf)[len + ret] = '\0';
92 static bool input_finished(const char *str)
94 return strchr(str, '\n');
97 /* Update state, and set our READ/WRITE flags appropriately. */
98 static void set_state(struct client *c, enum state state)
101 tevent_fd_set_flags(c->fde, state_flag_map[state]);
104 /* Returns false on error, increments state on finishing string. */
105 static bool send_string(struct client *c, const char *str)
107 ssize_t len = write_string(c->fd, str + c->bytes_sent);
110 c->bytes_sent += len;
111 if (c->bytes_sent == strlen(str)) {
113 set_state(c, c->state+1);
118 static bool get_subclient(struct client *me)
122 for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
123 struct client *c = me->oserver->clients[i];
126 if (c->oracle == NULL && input_finished(c->question)) {
135 static bool get_oracle(struct client *me)
139 for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
140 struct client *c = me->oserver->clients[i];
143 if (c->subclient == NULL && input_finished(c->question)) {
152 static void wakeup(struct client *c)
154 tevent_fd_set_flags(c->fde, state_flag_map[c->state]);
157 static void service_client(struct tevent_context *ev,
158 struct tevent_fd *fde, uint16_t flags, void *_c)
160 struct client *c = _c;
164 case SENDING_GREETING:
165 if (!send_string(c, "Welcome. Please ask your question.\n"))
168 case RECEIVING_USER_QUESTION:
169 len = read_string(c->fd, &c->question);
172 if (input_finished(c->question))
173 set_state(c, SENDING_OTHER_QUESTION_PREFIX);
175 case SENDING_OTHER_QUESTION_PREFIX:
178 if (!send_string(c, "While the Oracle ponders,"
179 " please answer the following question:\n"))
182 case SENDING_OTHER_QUESTION:
185 if (!send_string(c, c->subclient->question))
188 case RECEIVING_OTHER_ANSWER:
191 len = read_string(c->fd, &c->subclient->answer);
194 if (input_finished(c->subclient->answer)) {
195 set_state(c, SENDING_ANSWER_PREFIX);
196 wakeup(c->subclient);
199 case SENDING_ANSWER_PREFIX:
200 if (!input_finished(c->answer))
202 if (!send_string(c, "The Oracle spake thus:\n"))
206 if (!send_string(c, c->answer))
213 if (c->state != FINISHED)
220 if (!get_subclient(c)) {
221 /* We can't get one: go to sleep until someone find_oracle() */
222 tevent_fd_set_flags(c->fde, 0);
224 /* In case they are waiting... */
225 wakeup(c->subclient);
229 /* If we don't have an oracle and find one, that's OK. */
230 if (!c->oracle && get_oracle(c)) {
231 /* In case they are waiting... */
236 /* Either our oracle is not finished, or we don't have one: sleep. */
237 tevent_fd_set_flags(c->fde, 0);
240 static int cleanup_client(struct client *client)
244 /* We were an oracle? */
245 if (client->subclient)
246 client->subclient->oracle = NULL;
248 /* We had an oracle? */
250 client->oracle->subclient = NULL;
252 for (i = 0; i < ARRAY_SIZE(client->oserver->clients); i++) {
253 if (client->oserver->clients[i] == client) {
254 client->oserver->clients[i] = NULL;
255 tevent_fd_set_flags(client->oserver->fde,
263 static void add_client(struct tevent_context *ev,
264 struct tevent_fd *fde, uint16_t flags, void *_oserver)
266 struct oserver *oserver = _oserver;
267 struct client *client;
270 client = talloc(oserver, struct client);
271 client->fd = accept(oserver->fd, NULL, 0);
273 err(1, "Accepting client connection");
275 client->state = SENDING_GREETING;
276 client->bytes_sent = 0;
277 client->question = talloc_strdup(client, "");
278 client->oserver = oserver;
279 client->oracle = NULL;
280 client->subclient = NULL;
281 client->answer = talloc_strdup(client, "");
282 client->fde = tevent_add_fd(ev, client, client->fd,
283 state_flag_map[client->state],
284 service_client, client);
285 tevent_fd_set_auto_close(client->fde);
287 /* Find empty slot in array for this client. */
288 for (i = 0; oserver->clients[i]; i++);
289 oserver->clients[i] = client;
290 talloc_set_destructor(client, cleanup_client);
292 /* Full? Stop listening... */
293 if (i == ARRAY_SIZE(oserver->clients)-1)
294 tevent_fd_set_flags(oserver->fde, 0);
297 static void clear_clients(struct oserver *oserver)
299 memset(oserver->clients, 0,
300 ARRAY_SIZE(oserver->clients) * sizeof(oserver->clients[0]));
303 static int destroy_oserver(struct oserver *oserver)
309 struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port)
311 struct oserver *oserver;
314 struct sockaddr addr;
315 struct sockaddr_in in;
318 oserver = talloc(ev, struct oserver);
319 clear_clients(oserver);
320 oserver->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
321 if (oserver->fd < 0) {
322 talloc_free(oserver);
326 talloc_set_destructor(oserver, destroy_oserver);
328 if (setsockopt(oserver->fd, SOL_SOCKET, SO_REUSEADDR,
330 warn("Setting socket reuse");
332 u.in.sin_family = AF_INET;
333 u.in.sin_port = htons(port);
334 u.in.sin_addr.s_addr = INADDR_ANY;
335 if (bind(oserver->fd, &u.addr, sizeof(u.in)) == -1) {
336 talloc_free(oserver);
340 if (listen(oserver->fd, 0) != 0) {
341 talloc_free(oserver);
345 oserver->fde = tevent_add_fd(ev, oserver, oserver->fd,
346 TEVENT_FD_READ, add_client, oserver);
348 talloc_free(oserver);
352 /* Don't kill us if client dies. */
353 signal(SIGPIPE, SIG_IGN);