]> git.ozlabs.org Git - ccan-lca-2011.git/blob - ccan/oserver/oserver.c
4503aa49610f1a13d55649d37dbfa89c0d9f24ac
[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 <ccan/noerr/noerr.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <netinet/tcp.h>
11 #include <err.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <signal.h>
19
20 enum state {
21         RECEIVING_USER_QUESTION,
22         SENDING_ANSWER,
23         FINISHED
24 };
25
26 static uint16_t state_flag_map[] = {
27         [RECEIVING_USER_QUESTION]       = TEVENT_FD_READ,
28         [SENDING_ANSWER]                = TEVENT_FD_WRITE,
29         [FINISHED]                      = 0
30 };
31
32 struct client {
33         /* What are we doing today, brain? */
34         enum state state;
35         /* Our event info, and the file descriptor. */
36         struct tevent_fd *fde;
37         int fd;
38         /* The question we read from client. */
39         char *question;
40         /* How many bytes of the reply we sent so far. */
41         size_t bytes_sent;
42 };
43
44 /* 5 clients should be enough for anybody! */
45 static struct client *clients[5];
46 static int sfd;
47 static struct tevent_fd *sfde;
48
49 static ssize_t write_string(int fd, const char *str)
50 {
51         return write(fd, str, strlen(str));
52 }
53
54 static ssize_t read_string(int fd, char **buf)
55 {
56         ssize_t ret, len, maxlen;
57
58         len = strlen(*buf);
59         maxlen = talloc_array_length(*buf);
60
61         if (maxlen < len + 100) {
62                 maxlen += 100;
63                 *buf = talloc_realloc(NULL, *buf, char, maxlen);
64         }
65
66         ret = read(fd, *buf + len, maxlen - len - 1);
67         if (ret >= 0)
68                 (*buf)[len + ret] = '\0';
69         return ret;
70 }
71
72 static bool input_finished(const char *str)
73 {
74         return strchr(str, '\n');
75 }
76
77 /* Update state, and set our READ/WRITE flags appropriately. */
78 static void set_state(struct client *c, enum state state)
79 {
80         c->state = state;
81         tevent_fd_set_flags(c->fde, state_flag_map[state]);
82 }
83
84 /* Returns false on error, increments state on finishing string. */
85 static bool send_string(struct client *c, const char *str)
86 {
87         ssize_t len = write_string(c->fd, str + c->bytes_sent);
88         if (len < 0)
89                 return false;
90         c->bytes_sent += len;
91         if (c->bytes_sent == strlen(str)) {
92                 c->bytes_sent = 0;
93                 set_state(c, c->state+1);
94         }
95         return true;
96 }
97
98 static void service_client(struct tevent_context *ev,
99                            struct tevent_fd *fde, uint16_t flags, void *_c)
100 {
101         struct client *c = _c;
102         ssize_t len;
103
104         switch (c->state) {
105         case RECEIVING_USER_QUESTION:
106                 len = read_string(c->fd, &c->question);
107                 if (len <= 0)
108                         goto fail;
109                 if (input_finished(c->question)) {
110                         unsigned int i;
111
112                         for (i = 0; c->question[i]; i++)
113                                 c->question[i] = toupper(c->question[i]);
114                         set_state(c, SENDING_ANSWER);
115                 }
116                 break;
117         case SENDING_ANSWER:
118                 if (!send_string(c, c->question))
119                         goto fail;
120                 break;
121         default:
122                 goto fail;
123         }
124
125         if (c->state != FINISHED)
126                 return;
127
128 fail:
129         talloc_free(c);
130 }
131
132 static int cleanup_client(struct client *client)
133 {
134         unsigned int i;
135
136         for (i = 0; i < ARRAY_SIZE(clients); i++) {
137                 if (clients[i] == client) {
138                         clients[i] = NULL;
139                         tevent_fd_set_flags(sfde, TEVENT_FD_READ);
140                         return 0;
141                 }
142         }
143         abort();
144 }
145
146 static void add_client(struct tevent_context *ev,
147                        struct tevent_fd *fde, uint16_t flags, void *unused)
148 {
149         struct client *client;
150         unsigned int i;
151
152         client = talloc(sfde, struct client);
153         client->fd = accept(sfd, NULL, 0);
154         if (client->fd < 0)
155                 err(1, "Accepting client connection");
156
157         client->state = RECEIVING_USER_QUESTION;
158         client->bytes_sent = 0;
159         client->question = talloc_strdup(client, "");
160         client->fde = tevent_add_fd(ev, client, client->fd,
161                                     state_flag_map[client->state],
162                                     service_client, client);
163         tevent_fd_set_auto_close(client->fde);
164
165         /* Find empty slot in array for this client. */
166         for (i = 0; clients[i]; i++);
167         clients[i] = client;
168         talloc_set_destructor(client, cleanup_client);
169
170         /* Full?  Stop listening... */
171         if (i == ARRAY_SIZE(clients)-1)
172                 tevent_fd_set_flags(sfde, 0);
173 }
174
175 void *oserver_setup(struct tevent_context *ev, unsigned short port)
176 {
177         int one = 1;
178         union {
179                 struct sockaddr addr;
180                 struct sockaddr_in in;
181         } u;
182
183         sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
184         if (sfd < 0)
185                 return false;
186
187         if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
188                 warn("Setting socket reuse");
189
190         u.in.sin_family = AF_INET;
191         u.in.sin_port = htons(port);
192         u.in.sin_addr.s_addr = INADDR_ANY;
193         if (bind(sfd, &u.addr, sizeof(u.in)) == -1) {
194                 close_noerr(sfd);
195                 return NULL;
196         }
197
198         if (listen(sfd, 0) != 0) {
199                 close_noerr(sfd);
200                 return NULL;
201         }
202
203         sfde = tevent_add_fd(ev, ev, sfd, TEVENT_FD_READ, add_client, NULL);
204         tevent_fd_set_auto_close(sfde);
205
206         /* Don't kill us if client dies. */
207         signal(SIGPIPE, SIG_IGN);
208
209         return sfde;
210 }