]> git.ozlabs.org Git - ccan-lca-2011.git/blob - ccan/oserver/oserver.c
lca2011: turn it into a true Usenet Oracle.
[ccan-lca-2011.git] / ccan / oserver / oserver.c
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>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <netinet/tcp.h>
10 #include <err.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <assert.h>
19
20 enum state {
21         SENDING_GREETING,
22         RECEIVING_USER_QUESTION,
23         AWAITING_A_SUBCLIENT,
24         SENDING_OTHER_QUESTION_PREFIX,
25         SENDING_OTHER_QUESTION,
26         RECEIVING_OTHER_ANSWER,
27         AWAITING_OUR_ORACLE,
28         SENDING_ANSWER_PREFIX,
29         SENDING_ANSWER,
30         FINISHED
31 };
32
33 static uint16_t state_flag_map[] = {
34         [SENDING_GREETING]              = TEVENT_FD_WRITE,
35         [RECEIVING_USER_QUESTION]       = TEVENT_FD_READ,
36         [AWAITING_A_SUBCLIENT]          = 0,
37         [SENDING_OTHER_QUESTION_PREFIX] = TEVENT_FD_WRITE,
38         [SENDING_OTHER_QUESTION]        = TEVENT_FD_WRITE,
39         [RECEIVING_OTHER_ANSWER]        = TEVENT_FD_READ,
40         [AWAITING_OUR_ORACLE]           = 0,
41         [SENDING_ANSWER_PREFIX]         = TEVENT_FD_WRITE,
42         [SENDING_ANSWER]                = TEVENT_FD_WRITE,
43         [FINISHED]                      = 0
44 };
45
46 struct client {
47         /* What are we doing today, brain? */
48         enum state state;
49         /* Our event info, and the file descriptor. */
50         struct tevent_fd *fde;
51         int fd;
52         /* The question we read from client. */
53         char *question;
54         /* The answer to the client. */
55         char *answer;
56         /* How many bytes of the reply we sent so far. */
57         size_t bytes_sent;
58         /* Our server. */
59         struct oserver *oserver;
60         /* Whose question this client is answering. */
61         struct client *subclient;
62         /* Who is answering our question. */
63         struct client *oracle;
64 };
65
66 struct oserver {
67         /* 5 clients should be enough for anybody! */
68         struct client *clients[5];
69         int fd;
70         struct tevent_fd *fde;
71 };
72
73 static ssize_t write_string(int fd, const char *str)
74 {
75         return write(fd, str, strlen(str));
76 }
77
78 static ssize_t read_string(int fd, char **buf)
79 {
80         ssize_t ret, len, maxlen;
81
82         len = strlen(*buf);
83         maxlen = talloc_array_length(*buf);
84
85         if (maxlen < len + 100) {
86                 maxlen += 100;
87                 *buf = talloc_realloc(NULL, *buf, char, maxlen);
88         }
89
90         ret = read(fd, *buf + len, maxlen - len - 1);
91         if (ret >= 0)
92                 (*buf)[len + ret] = '\0';
93         return ret;
94 }
95
96 static bool input_finished(const char *str)
97 {
98         return strchr(str, '\n');
99 }
100
101 /* Update state, and set our READ/WRITE flags appropriately. */
102 static void set_state(struct client *c, enum state state)
103 {
104         c->state = state;
105         tevent_fd_set_flags(c->fde, state_flag_map[state]);
106 }
107
108 /* Returns false on error, increments state on finishing string. */
109 static bool send_string(struct client *c, const char *str)
110 {
111         ssize_t len = write_string(c->fd, str + c->bytes_sent);
112         if (len < 0)
113                 return false;
114         c->bytes_sent += len;
115         if (c->bytes_sent == strlen(str)) {
116                 c->bytes_sent = 0;
117                 set_state(c, c->state+1);
118         }
119         return true;
120 }
121
122 static bool get_subclient(struct client *me)
123 {
124         unsigned int i;
125
126         for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
127                 struct client *c = me->oserver->clients[i];
128                 if (!c || c == me)
129                         continue;
130                 if (c->oracle == NULL && input_finished(c->question)) {
131                         me->subclient = c;
132                         c->oracle = me;
133                         return true;
134                 }
135         }
136         return false;
137 }
138
139 static bool get_oracle(struct client *me)
140 {
141         unsigned int i;
142
143         for (i = 0; i < ARRAY_SIZE(me->oserver->clients); i++) {
144                 struct client *c = me->oserver->clients[i];
145                 if (!c || c == me)
146                         continue;
147                 if (c->subclient == NULL && input_finished(c->question)) {
148                         me->oracle = c;
149                         c->subclient = me;
150                         return true;
151                 }
152         }
153         return false;
154 }
155
156 static void service_client(struct tevent_context *ev,
157                            struct tevent_fd *fde, uint16_t flags, void *_c)
158 {
159         struct client *c = _c;
160         ssize_t len;
161
162         switch (c->state) {
163         case SENDING_GREETING:
164                 if (!send_string(c, "Welcome.  Please ask your question.\n"))
165                         goto fail;
166                 break;
167         case RECEIVING_USER_QUESTION:
168                 len = read_string(c->fd, &c->question);
169                 if (len <= 0)
170                         goto fail;
171                 if (input_finished(c->question)) {
172                         /* Look for someone to be oracle to. */
173                         if (get_subclient(c)) {
174                                 set_state(c, SENDING_OTHER_QUESTION_PREFIX);
175                         } else {
176                                 /* We sit here until someone find_oracles us */
177                                 set_state(c, AWAITING_A_SUBCLIENT);
178                         }
179
180                         /* Look for an oracle for ourselves. */
181                         if (get_oracle(c)) {
182                                 assert(c->oracle->state
183                                        == AWAITING_A_SUBCLIENT);
184                                 set_state(c->oracle,
185                                           SENDING_OTHER_QUESTION_PREFIX);
186                         }
187                 }
188                 break;
189         case SENDING_OTHER_QUESTION_PREFIX:
190                 if (!send_string(c, "While the Oracle ponders,"
191                                  " please answer the following question:\n"))
192                         goto fail;
193                 break;
194         case SENDING_OTHER_QUESTION:
195                 if (!send_string(c, c->subclient->question))
196                         goto fail;
197                 break;
198         case RECEIVING_OTHER_ANSWER:
199                 len = read_string(c->fd, &c->subclient->answer);
200                 if (len <= 0)
201                         goto fail;
202                 if (input_finished(c->subclient->answer)) {
203                         /* Did our oracle answer for us already? */
204                         if (input_finished(c->answer))
205                                 set_state(c, SENDING_ANSWER_PREFIX);
206                         else
207                                 set_state(c, AWAITING_OUR_ORACLE);
208
209                         /* If they were waiting for an answer, move them. */
210                         if (c->subclient->state == AWAITING_OUR_ORACLE)
211                                 set_state(c->subclient, SENDING_ANSWER_PREFIX);
212                 }
213                 break;
214         case SENDING_ANSWER_PREFIX:
215                 if (!send_string(c, "The Oracle spake thus:\n"))
216                         goto fail;
217                 break;
218         case SENDING_ANSWER:
219                 if (!send_string(c, c->answer))
220                         goto fail;
221                 break;
222         default:
223                 goto fail;
224         }
225
226         if (c->state != FINISHED)
227                 return;
228
229 fail:
230         talloc_free(c);
231 }
232
233 static int cleanup_client(struct client *client)
234 {
235         unsigned int i;
236
237         for (i = 0; i < ARRAY_SIZE(client->oserver->clients); i++) {
238                 if (client->oserver->clients[i] == client) {
239                         client->oserver->clients[i] = NULL;
240                         tevent_fd_set_flags(client->oserver->fde,
241                                             TEVENT_FD_READ);
242                         return 0;
243                 }
244         }
245         abort();
246 }
247
248 static void add_client(struct tevent_context *ev,
249                        struct tevent_fd *fde, uint16_t flags, void *_oserver)
250 {
251         struct oserver *oserver = _oserver;
252         struct client *client;
253         unsigned int i;
254
255         client = talloc(oserver, struct client);
256         client->fd = accept(oserver->fd, NULL, 0);
257         if (client->fd < 0)
258                 err(1, "Accepting client connection");
259
260         client->state = SENDING_GREETING;
261         client->bytes_sent = 0;
262         client->question = talloc_strdup(client, "");
263         client->oserver = oserver;
264         client->oracle = NULL;
265         client->subclient = NULL;
266         client->answer = talloc_strdup(client, "");
267         client->fde = tevent_add_fd(ev, client, client->fd,
268                                     state_flag_map[client->state],
269                                     service_client, client);
270         tevent_fd_set_auto_close(client->fde);
271
272         /* Find empty slot in array for this client. */
273         for (i = 0; oserver->clients[i]; i++);
274         oserver->clients[i] = client;
275         talloc_set_destructor(client, cleanup_client);
276
277         /* Full?  Stop listening... */
278         if (i == ARRAY_SIZE(oserver->clients)-1)
279                 tevent_fd_set_flags(oserver->fde, 0);
280 }
281
282 static void clear_clients(struct oserver *oserver)
283 {
284         memset(oserver->clients, 0,
285                ARRAY_SIZE(oserver->clients) * sizeof(oserver->clients[0]));
286 }
287
288 static int destroy_oserver(struct oserver *oserver)
289 {
290         close(oserver->fd);
291         return 0;
292 }
293
294 struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port)
295 {
296         struct oserver *oserver;
297         int one = 1;
298         union {
299                 struct sockaddr addr;
300                 struct sockaddr_in in;
301         } u;
302
303         oserver = talloc(ev, struct oserver);
304         clear_clients(oserver);
305         oserver->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
306         if (oserver->fd < 0) {
307                 talloc_free(oserver);
308                 return NULL;
309         }
310
311         talloc_set_destructor(oserver, destroy_oserver);
312
313         if (setsockopt(oserver->fd, SOL_SOCKET, SO_REUSEADDR,
314                        &one, sizeof(one)))
315                 warn("Setting socket reuse");
316
317         u.in.sin_family = AF_INET;
318         u.in.sin_port = htons(port);
319         u.in.sin_addr.s_addr = INADDR_ANY;
320         if (bind(oserver->fd, &u.addr, sizeof(u.in)) == -1) {
321                 talloc_free(oserver);
322                 return NULL;
323         }
324
325         if (listen(oserver->fd, 0) != 0) {
326                 talloc_free(oserver);
327                 return NULL;
328         }
329
330         oserver->fde = tevent_add_fd(ev, oserver, oserver->fd,
331                                      TEVENT_FD_READ, add_client, oserver);
332         if (!oserver->fde) {
333                 talloc_free(oserver);
334                 return NULL;
335         }
336
337         /* Don't kill us if client dies. */
338         signal(SIGPIPE, SIG_IGN);
339
340         return oserver;
341 }