#include <ccan/oserver/oserver.h>
#include <ccan/read_write_all/read_write_all.h>
+#include <ccan/opt/opt.h>
+#include <ccan/tevent/tevent.h>
+#include <ccan/array_size/array_size.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
+#include <signal.h>
-void oserver_serve(int fd)
+enum state {
+ RECEIVING_USER_QUESTION,
+ SENDING_ANSWER,
+ FINISHED
+};
+
+static uint16_t state_flag_map[] = {
+ [RECEIVING_USER_QUESTION] = TEVENT_FD_READ,
+ [SENDING_ANSWER] = TEVENT_FD_WRITE,
+ [FINISHED] = 0
+};
+
+struct client {
+ /* What are we doing today, brain? */
+ enum state state;
+ /* Our event info, and the file descriptor. */
+ struct tevent_fd *fde;
+ int fd;
+ /* The question we read from client. */
+ char *question;
+ /* How many bytes of the reply we sent so far. */
+ size_t bytes_sent;
+ /* Our server. */
+ struct oserver *oserver;
+};
+
+struct oserver {
+ /* 5 clients should be enough for anybody! */
+ struct client *clients[5];
+ int fd;
+ struct tevent_fd *fde;
+};
+
+static ssize_t write_string(int fd, const char *str)
+{
+ return write(fd, str, strlen(str));
+}
+
+static ssize_t read_string(int fd, char **buf)
+{
+ ssize_t ret, len, maxlen;
+
+ len = strlen(*buf);
+ maxlen = talloc_array_length(*buf);
+
+ if (maxlen < len + 100) {
+ maxlen += 100;
+ *buf = talloc_realloc(NULL, *buf, char, maxlen);
+ }
+
+ ret = read(fd, *buf + len, maxlen - len - 1);
+ if (ret >= 0)
+ (*buf)[len + ret] = '\0';
+ return ret;
+}
+
+static bool input_finished(const char *str)
+{
+ return strchr(str, '\n');
+}
+
+/* Update state, and set our READ/WRITE flags appropriately. */
+static void set_state(struct client *c, enum state state)
+{
+ c->state = state;
+ tevent_fd_set_flags(c->fde, state_flag_map[state]);
+}
+
+/* Returns false on error, increments state on finishing string. */
+static bool send_string(struct client *c, const char *str)
+{
+ ssize_t len = write_string(c->fd, str + c->bytes_sent);
+ if (len < 0)
+ return false;
+ c->bytes_sent += len;
+ if (c->bytes_sent == strlen(str)) {
+ c->bytes_sent = 0;
+ set_state(c, c->state+1);
+ }
+ return true;
+}
+
+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)
{
- char buf[1024];
unsigned int i;
- for (i = 0; i < sizeof(buf)-1; i++) {
- if (read(fd, &buf[i], 1) != 1)
- errx(1, "Client disconnected");
- if (buf[i] == '\n' || buf[i] == '\r')
- break;
- buf[i] = toupper(buf[i]);
+ for (i = 0; i < ARRAY_SIZE(client->oserver->clients); i++) {
+ if (client->oserver->clients[i] == client) {
+ client->oserver->clients[i] = NULL;
+ tevent_fd_set_flags(client->oserver->fde,
+ TEVENT_FD_READ);
+ return 0;
+ }
}
- buf[i] = '\0';
+ abort();
+}
+
+static void add_client(struct tevent_context *ev,
+ struct tevent_fd *fde, uint16_t flags, void *_oserver)
+{
+ struct oserver *oserver = _oserver;
+ struct client *client;
+ unsigned int i;
+
+ client = talloc(oserver, struct client);
+ client->fd = accept(oserver->fd, 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->oserver = oserver;
+ 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; oserver->clients[i]; i++);
+ oserver->clients[i] = client;
+ talloc_set_destructor(client, cleanup_client);
- if (!write_all(fd, "Louder, like this: '",
- strlen("Louder, like this: '"))
- || !write_all(fd, buf, i)
- || !write_all(fd, "'\r\n", strlen("'\r\n")))
- err(1, "Write failed");
- exit(0);
+ /* Full? Stop listening... */
+ if (i == ARRAY_SIZE(oserver->clients)-1)
+ tevent_fd_set_flags(oserver->fde, 0);
}
-int oserver_setup(void)
+static void clear_clients(struct oserver *oserver)
{
- int sockfd;
+ memset(oserver->clients, 0,
+ ARRAY_SIZE(oserver->clients) * sizeof(oserver->clients[0]));
+}
+
+static int destroy_oserver(struct oserver *oserver)
+{
+ close(oserver->fd);
+ return 0;
+}
+
+struct oserver *oserver_setup(struct tevent_context *ev, unsigned short port)
+{
+ struct oserver *oserver;
int one = 1;
union {
struct sockaddr addr;
struct sockaddr_in in;
} u;
- sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sockfd < 0)
- err(1, "Creating TCP socket");
+ oserver = talloc(ev, struct oserver);
+ clear_clients(oserver);
+ oserver->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (oserver->fd < 0) {
+ talloc_free(oserver);
+ return NULL;
+ }
+
+ talloc_set_destructor(oserver, destroy_oserver);
- if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
+ if (setsockopt(oserver->fd, SOL_SOCKET, SO_REUSEADDR,
+ &one, sizeof(one)))
warn("Setting socket reuse");
u.in.sin_family = AF_INET;
- u.in.sin_port = htons(OSERVER_PORT);
+ u.in.sin_port = htons(port);
u.in.sin_addr.s_addr = INADDR_ANY;
- if (bind(sockfd, &u.addr, sizeof(u.in)) == -1)
- err(1, "Binding TCP socket");
+ if (bind(oserver->fd, &u.addr, sizeof(u.in)) == -1) {
+ talloc_free(oserver);
+ return NULL;
+ }
+
+ if (listen(oserver->fd, 0) != 0) {
+ talloc_free(oserver);
+ return NULL;
+ }
+
+ oserver->fde = tevent_add_fd(ev, oserver, oserver->fd,
+ TEVENT_FD_READ, add_client, oserver);
+ if (!oserver->fde) {
+ talloc_free(oserver);
+ return NULL;
+ }
+
+ /* Don't kill us if client dies. */
+ signal(SIGPIPE, SIG_IGN);
- if (listen(sockfd, 0) != 0)
- err(1, "Listening on TCP socket");
- return sockfd;
+ return oserver;
}