From 857c3f91214120e7a54062f12c303e41972250b2 Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Wed, 17 Apr 2013 22:34:27 +0200 Subject: [PATCH] first commit --- Makefile | 21 +++++ includes/.kore.h.swp | Bin 0 -> 12288 bytes includes/kore.h | 59 +++++++++++++ src/kore.c | 191 +++++++++++++++++++++++++++++++++++++++++++ src/utils.c | 102 +++++++++++++++++++++++ 5 files changed, 373 insertions(+) create mode 100644 Makefile create mode 100644 includes/.kore.h.swp create mode 100644 includes/kore.h create mode 100644 src/kore.c create mode 100644 src/utils.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..adf26b1 --- /dev/null +++ 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 new file mode 100644 index 0000000000000000000000000000000000000000..a640ce5b41291b36336ed46b7728f09da83b0c5e GIT binary patch literal 12288 zcmeI2OOM-B6vuBXfok5b1?*0nO(u$xc_7iMfN&Gf#MH58uALc;sAOgAn~7=04%g09 z1EOLNqF;dp8_E*cgG8xRi4On^*s&@_{QwC5*GZ;t6?FyZM*2<0_nv$1IX^!ZGu~O; zw7c|LV};@K9Ah^wryC~^9%q-IX6)TeCb=4a$R`(aU&zI!hlH_T1=9|fntks0{XA8kMz&<3;tZ9p5)2DAZfKpW5ov;l2E8~C3X z=*Nuxc#N?t1{%Wu|JC0B{(P3PYv3xl2wnw$Jj2*^@EJGy_Irs>?15SaH;AQYDdi({bUcUifgRg-8Xam}SHlPh?1KNN#@NYLzsfaXA z2Gp$4@i6R!XYW4txD|G&)?{oyOJc)NZ&`+;Y&T5OLddi~sz$@Uq*|RYP^TzM^Md9> zjYebP^lagvU#Q^w%rg~k7_~BWUdVVdz9a51#WX^S{%Dj%RaKALH&5OdVR2VbG>w_g zJs@W9_86>ZAD(7LVA{?`*s;uZwN9xhb|!-bLyBlWd~YHq;z1EPG|nTKF;!*MvJ0t@ zSc0Y|Q9*eYo$tr7ggYZoQcS>^VHkt>G@FIMPb#7!$x`}YTF^gVJ4*6GV8i*!X)Bf+ z@a^in6BUC_XB$^*(au73w!<`pYA|t60~2++SC&ImI}+(`F+|L5W7%Mk3*?=Som649 z80Q-!_g*hyxN19A7}hBa9osE`8|>lQsZ_#n(+o|%=7-?|tHffEq=M!ds%4P6q>OWi zlS0Zg3v)yuYOiZ1}Ta` zy|_W9LrmQgR3nd*?NZBgU8@z?o=aPH(4nAXQO~#3?7GEQJ>E6l77kZ2WBcTLtHGAZ zt%k~APS;@$Spts3I=R-GW3O4|8crV4aSA++4SN1;L7hx)`?kAAUN1mqm1?VF2EJ!) zB6`}dnqK8zmmJUc)%GZ=PHi(V%hI^FY6pHD>#TAMbYi+?P3|`N>sHG)9faE)z17j@ z{5Q0PT77Gyhl>j_$k;X4EbPFMV0(V3GIcH9>X>d|p0OP}*v535u&HbLenXjaJwm;1 zKz_$`oKl?G3pz-2#v;cy&o~yrU)h~9%dIt&w;|(BlStKadTrION@I7|9ouRXBy((T ztI|qC>cq_L6VAjs_crY|B2Sn;j!%^Jsi0XAZQE}-rrm{gq`j36^{A4fY?GksU`?sPj O7BX+-;!W19vA+QWq9zdl literal 0 HcmV?d00001 diff --git a/includes/kore.h b/includes/kore.h new file mode 100644 index 0000000..d0fd8fa --- /dev/null +++ b/includes/kore.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2013 Joris Vink + * + * 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 new file mode 100644 index 0000000..2080762 --- /dev/null +++ b/src/kore.c @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2013 Joris Vink + * + * 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 +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#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 new file mode 100644 index 0000000..42f46d1 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2013 Joris Vink + * + * 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 +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#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); +}