commit 857c3f91214120e7a54062f12c303e41972250b2
Author: Joris Vink <joris@coders.se>
Date: Wed, 17 Apr 2013 22:34:27 +0200
first commit
Diffstat:
Makefile | | | 21 | +++++++++++++++++++++ |
includes/.kore.h.swp | | | 0 | |
includes/kore.h | | | 59 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
src/kore.c | | | 191 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
src/utils.c | | | 102 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 373 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,21 @@
+# Kore Makefile
+
+CC=clang
+BIN=kore
+
+S_SRC= src/kore.c src/utils.c
+S_OBJS= $(S_SRC:.c=.o)
+
+CFLAGS+=-Wall -Wstrict-prototypes -Wmissing-prototypes
+CFLAGS+=-Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-qual
+CFLAGS+=-Wsign-compare -Iincludes -g
+LDFLAGS=
+
+light: $(S_OBJS)
+ $(CC) $(CFLAGS) $(LDFLAGS) $(S_OBJS) -o $(BIN)
+
+.c.o: $<
+ $(CC) $(CFLAGS) -c $< -o $@
+
+clean:
+ rm -f src/*.o $(BIN)
diff --git a/includes/.kore.h.swp b/includes/.kore.h.swp
Binary files differ.
diff --git a/includes/kore.h b/includes/kore.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2013 Joris Vink <joris@coders.se>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef __H_KORE_H
+#define __H_KORE_H
+
+#define KORE_RESULT_ERROR 0
+#define KORE_RESULT_OK 1
+
+#define errno_s strerror(errno)
+#define kore_log(fmt, ...) \
+ kore_log_internal(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
+
+struct netbuf {
+ u_int8_t *data;
+ u_int32_t offset;
+ u_int32_t length;
+ void *owner;
+ int (*cb)(struct netbuf *);
+
+ TAILQ_ENTRY(netbuf) list;
+};
+
+struct listener {
+ int fd;
+ struct sockaddr_in sin;
+};
+
+struct connection {
+ int fd;
+ struct sockaddr_in sin;
+ void *owner;
+
+ TAILQ_HEAD(, netbuf) send_queue;
+ TAILQ_HEAD(, netbuf) recv_queue;
+};
+
+void *kore_malloc(size_t);
+void *kore_calloc(size_t, size_t);
+void *kore_realloc(void *, size_t);
+char *kore_strdup(const char *);
+
+void fatal(const char *, ...);
+void kore_log_internal(char *, int, const char *, ...);
+
+#endif /* !__H_KORE_H */
diff --git a/src/kore.c b/src/kore.c
@@ -0,0 +1,191 @@
+/*
+ * Copyright (c) 2013 Joris Vink <joris@coders.se>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/queue.h>
+#include <sys/epoll.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "kore.h"
+
+#define EPOLL_EVENTS 500
+
+static int efd = -1;
+
+static int kore_server_bind(struct listener *, const char *, int);
+static int kore_server_accept(struct listener *);
+static int kore_connection_handle(struct connection *, int);
+static int kore_socket_nonblock(int);
+static void kore_event(int, int, void *);
+
+int
+main(int argc, char *argv[])
+{
+ struct connection *c;
+ struct listener server;
+ struct epoll_event *events;
+ int n, i, *fd;
+
+ if (argc != 3)
+ fatal("Usage: kore [ip] [port]");
+
+ if (!kore_server_bind(&server, argv[1], atoi(argv[2])))
+ fatal("cannot bind to %s:%s", argv[1], argv[2]);
+
+ if ((efd = epoll_create(1000)) == -1)
+ fatal("epoll_create(): %s", errno_s);
+
+ kore_event(server.fd, EPOLLIN, &server);
+ events = kore_calloc(EPOLL_EVENTS, sizeof(struct epoll_event));
+ for (;;) {
+ n = epoll_wait(efd, events, EPOLL_EVENTS, -1);
+ if (n == -1)
+ fatal("epoll_wait(): %s", errno_s);
+
+ for (i = 0; i < n; i++) {
+ fd = (int *)events[i].data.ptr;
+
+ if (events[i].events & EPOLLERR ||
+ events[i].events & EPOLLHUP) {
+ if (*fd == server.fd)
+ fatal("error on server socket");
+
+ c = (struct connection *)events[i].data.ptr;
+ continue;
+ }
+
+ if (*fd == server.fd) {
+ kore_server_accept(&server);
+ } else {
+ c = (struct connection *)events[i].data.ptr;
+ if (!kore_connection_handle(c, events[i].events))
+ /* Disconnect. */;
+ }
+ }
+ }
+
+ close(server.fd);
+ return (0);
+}
+
+static int
+kore_server_bind(struct listener *l, const char *ip, int port)
+{
+ if ((l->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+ kore_log("socket(): %s", errno_s);
+ return (KORE_RESULT_ERROR);
+ }
+
+ if (!kore_socket_nonblock(l->fd)) {
+ close(l->fd);
+ return (KORE_RESULT_ERROR);
+ }
+
+ memset(&(l->sin), 0, sizeof(l->sin));
+ l->sin.sin_family = AF_INET;
+ l->sin.sin_port = htons(port);
+ l->sin.sin_addr.s_addr = inet_addr(ip);
+
+ if (bind(l->fd, (struct sockaddr *)&(l->sin), sizeof(l->sin)) == -1) {
+ close(l->fd);
+ kore_log("bind(): %s", errno_s);
+ return (KORE_RESULT_ERROR);
+ }
+
+ if (listen(l->fd, 50) == -1) {
+ close(l->fd);
+ kore_log("listen(): %s", errno_s);
+ return (KORE_RESULT_ERROR);
+ }
+
+ return (KORE_RESULT_OK);
+}
+
+static int
+kore_server_accept(struct listener *l)
+{
+ socklen_t len;
+ struct connection *c;
+
+ len = sizeof(struct sockaddr_in);
+ c = (struct connection *)kore_malloc(sizeof(*c));
+ if ((c->fd = accept(l->fd, (struct sockaddr *)&(c->sin), &len)) == -1) {
+ free(c);
+ kore_log("accept(): %s", errno_s);
+ return (KORE_RESULT_ERROR);
+ }
+
+ if (!kore_socket_nonblock(c->fd)) {
+ close(c->fd);
+ free(c);
+ return (KORE_RESULT_ERROR);
+ }
+
+ c->owner = l;
+ TAILQ_INIT(&(c->send_queue));
+ TAILQ_INIT(&(c->recv_queue));
+ kore_event(c->fd, EPOLLIN | EPOLLET, c);
+ kore_log("new connection from %s", inet_ntoa(c->sin.sin_addr));
+
+ return (KORE_RESULT_OK);
+}
+
+static int
+kore_connection_handle(struct connection *c, int flags)
+{
+ return (KORE_RESULT_OK);
+}
+
+static int
+kore_socket_nonblock(int fd)
+{
+ int flags;
+
+ if ((flags = fcntl(fd, F_GETFL, 0)) == -1) {
+ kore_log("fcntl(): F_GETFL %s", errno_s);
+ return (KORE_RESULT_ERROR);
+ }
+
+ flags |= O_NONBLOCK;
+ if (fcntl(fd, F_SETFL, flags) == -1) {
+ kore_log("fcntl(): F_SETFL %s", errno_s);
+ return (KORE_RESULT_ERROR);
+ }
+
+ return (KORE_RESULT_OK);
+}
+
+static void
+kore_event(int fd, int flags, void *udata)
+{
+ struct epoll_event evt;
+
+ evt.events = flags;
+ evt.data.ptr = udata;
+ if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &evt) == -1)
+ fatal("epoll_ctl(): %s", errno_s);
+}
diff --git a/src/utils.c b/src/utils.c
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2013 Joris Vink <joris@coders.se>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/queue.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "kore.h"
+
+void *
+kore_malloc(size_t len)
+{
+ void *ptr;
+
+ if ((ptr = malloc(len)) == NULL)
+ fatal("kore_malloc(%d): %d", len, errno);
+
+ return (ptr);
+}
+
+void *
+kore_realloc(void *ptr, size_t len)
+{
+ void *nptr;
+
+ if ((nptr = realloc(ptr, len)) == NULL)
+ fatal("kore_realloc(%p, %d): %d", ptr, len, errno);
+
+ return (ptr);
+}
+
+void *
+kore_calloc(size_t memb, size_t len)
+{
+ void *ptr;
+
+ if ((ptr = calloc(memb, len)) == NULL)
+ fatal("kore_calloc(%d, %d): %d", memb, len, errno);
+
+ return (ptr);
+}
+
+char *
+kore_strdup(const char *str)
+{
+ char *nstr;
+
+ if ((nstr = strdup(str)) == NULL)
+ fatal("kore_strdup(): %d", errno);
+
+ return (nstr);
+}
+
+void
+kore_log_internal(char *file, int line, const char *fmt, ...)
+{
+ va_list args;
+ char buf[2048];
+
+ va_start(args, fmt);
+ vsnprintf(buf, sizeof(buf), fmt, args);
+ va_end(args);
+
+ printf("%s:%d - %s\n", file, line, buf);
+}
+
+void
+fatal(const char *fmt, ...)
+{
+ va_list args;
+ char buf[2048];
+
+ va_start(args, fmt);
+ vsnprintf(buf, sizeof(buf), fmt, args);
+ va_end(args);
+
+ printf("error: %s\n", buf);
+ exit(1);
+}