+static void service_client(struct tevent_context *ev,
+ struct tevent_fd *fde, uint16_t flags, void *_c)
+{
+ struct client *c = _c;
+ ssize_t len;
+
+ switch (c->state) {
+ case RECEIVING_USER_QUESTION:
+ len = read_string(c->fd, &c->question);
+ if (len <= 0)
+ goto fail;
+ if (input_finished(c->question)) {
+ unsigned int i;
+
+ for (i = 0; c->question[i]; i++)
+ c->question[i] = toupper(c->question[i]);
+ set_state(c, SENDING_ANSWER);
+ }
+ break;
+ case SENDING_ANSWER:
+ if (!send_string(c, c->question))
+ goto fail;
+ break;
+ default:
+ goto fail;
+ }
+
+ if (c->state != FINISHED)
+ return;
+
+fail:
+ talloc_free(c);
+}
+
+static int cleanup_client(struct client *client)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(clients); i++) {
+ if (clients[i] == client) {
+ clients[i] = NULL;
+ tevent_fd_set_flags(sfde, TEVENT_FD_READ);
+ return 0;
+ }
+ }
+ abort();
+}
+
+static void add_client(struct tevent_context *ev,
+ struct tevent_fd *fde, uint16_t flags, void *unused)
+{
+ struct client *client;
+ unsigned int i;
+
+ client = talloc(sfde, struct client);
+ client->fd = accept(sfd, NULL, 0);
+ if (client->fd < 0)
+ err(1, "Accepting client connection");
+
+ client->state = RECEIVING_USER_QUESTION;
+ client->bytes_sent = 0;
+ client->question = talloc_strdup(client, "");
+ client->fde = tevent_add_fd(ev, client, client->fd,
+ state_flag_map[client->state],
+ service_client, client);
+ tevent_fd_set_auto_close(client->fde);
+
+ /* Find empty slot in array for this client. */
+ for (i = 0; clients[i]; i++);
+ clients[i] = client;
+ talloc_set_destructor(client, cleanup_client);
+
+ /* Full? Stop listening... */
+ if (i == ARRAY_SIZE(clients)-1)
+ tevent_fd_set_flags(sfde, 0);
+}
+
+void *oserver_setup(struct tevent_context *ev, unsigned short port)