summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--bounce.c73
-rw-r--r--bounce.h2
-rw-r--r--client.c16
3 files changed, 45 insertions, 46 deletions
diff --git a/bounce.c b/bounce.c
index 07ed4ae..33d4e9d 100644
--- a/bounce.c
+++ b/bounce.c
@@ -28,30 +28,30 @@
 #include "bounce.h"
 
 static struct {
-	size_t cap, len;
 	struct pollfd *fds;
 	struct Client **clients;
-} loop;
-
-static void loopAdd(int fd, struct Client *client) {
-	if (loop.len == loop.cap) {
-		loop.cap = (loop.cap ? loop.cap * 2 : 4);
-		loop.fds = realloc(loop.fds, sizeof(struct pollfd) * loop.cap);
-		loop.clients = realloc(loop.clients, sizeof(struct Client *) * loop.cap);
-		if (!loop.fds || !loop.clients) err(EX_OSERR, "realloc");
+	size_t cap, len;
+} event;
+
+static void eventAdd(int fd, struct Client *client) {
+	if (event.len == event.cap) {
+		event.cap = (event.cap ? event.cap * 2 : 8);
+		event.fds = realloc(event.fds, sizeof(*event.fds) * event.cap);
+		if (!event.fds) err(EX_OSERR, "realloc");
+		event.clients = realloc(
+			event.clients, sizeof(*event.clients) * event.cap
+		);
+		if (!event.clients) err(EX_OSERR, "realloc");
 	}
-
-	loop.fds[loop.len].fd = fd;
-	loop.fds[loop.len].events = POLLIN;
-	loop.fds[loop.len].revents = 0;
-	loop.clients[loop.len] = client;
-	loop.len++;
+	event.fds[event.len] = (struct pollfd) { .fd = fd, .events = POLLIN };
+	event.clients[event.len] = client;
+	event.len++;
 }
 
-static void loopRemove(size_t i) {
-	loop.len--;
-	loop.fds[i] = loop.fds[loop.len];
-	loop.clients[i] = loop.clients[loop.len];
+static void eventRemove(size_t i) {
+	event.len--;
+	event.fds[i] = event.fds[event.len];
+	event.clients[i] = event.clients[event.len];
 }
 
 static char *censor(char *arg) {
@@ -115,9 +115,8 @@ int main(int argc, char *argv[]) {
 
 	listenConfig(certPath, privPath);
 
-	enum { BindCap = 8 };
-	int bind[BindCap];
-	size_t bindLen = listenBind(bind, BindCap, localHost, localPort);
+	int bind[8];
+	size_t binds = listenBind(bind, 8, localHost, localPort);
 
 	int server = serverConnect(host, port);
 	serverLogin(pass, auth, nick, user, real);
@@ -127,29 +126,29 @@ int main(int argc, char *argv[]) {
 	}
 	if (join) serverJoin(join);
 
-	for (size_t i = 0; i < bindLen; ++i) {
+	for (size_t i = 0; i < binds; ++i) {
 		int error = listen(bind[i], 1);
 		if (error) err(EX_IOERR, "listen");
-		loopAdd(bind[i], NULL);
+		eventAdd(bind[i], NULL);
 	}
-	loopAdd(server, NULL);
+	eventAdd(server, NULL);
 
-	while (0 < poll(loop.fds, loop.len, -1)) {
-		for (size_t i = 0; i < loop.len; ++i) {
-			if (!loop.fds[i].revents) continue;
-			if (i < bindLen) {
+	while (0 < poll(event.fds, event.len, -1)) {
+		for (size_t i = 0; i < event.len; ++i) {
+			if (!event.fds[i].revents) continue;
+			if (i < binds) {
 				struct tls *tls;
-				int fd = listenAccept(&tls, loop.fds[i].fd);
-				loopAdd(fd, clientAlloc(tls));
-			} else if (!loop.clients[i]) {
+				int fd = listenAccept(&tls, event.fds[i].fd);
+				eventAdd(fd, clientAlloc(tls));
+			} else if (!event.clients[i]) {
 				serverRecv();
 			} else {
-				struct Client *client = loop.clients[i];
-				if (loop.fds[i].revents & POLLIN) clientRecv(client);
-				if (loop.fds[i].revents & ~POLLIN || clientClose(client)) {
+				struct Client *client = event.clients[i];
+				if (event.fds[i].revents & POLLIN) clientRecv(client);
+				if (event.fds[i].revents & ~POLLIN || clientError(client)) {
 					clientFree(client);
-					close(loop.fds[i].fd);
-					loopRemove(i);
+					close(event.fds[i].fd);
+					eventRemove(i);
 					break;
 				}
 			}
diff --git a/bounce.h b/bounce.h
index 7b9f4f8..1c47094 100644
--- a/bounce.h
+++ b/bounce.h
@@ -69,7 +69,7 @@ void serverRecv(void);
 char *clientPass;
 struct Client *clientAlloc(struct tls *tls);
 void clientFree(struct Client *client);
-bool clientClose(const struct Client *client);
+bool clientError(const struct Client *client);
 void clientRecv(struct Client *client);
 void clientSend(struct Client *client, const char *ptr, size_t len);
 
diff --git a/client.c b/client.c
index 185a277..3e3684f 100644
--- a/client.c
+++ b/client.c
@@ -34,7 +34,7 @@ enum Need {
 };
 
 struct Client {
-	bool close;
+	bool error;
 	struct tls *tls;
 	enum Need need;
 	char buf[4096];
@@ -45,7 +45,7 @@ struct Client *clientAlloc(struct tls *tls) {
 	struct Client *client = malloc(sizeof(*client));
 	if (!client) err(EX_OSERR, "malloc");
 
-	client->close = false;
+	client->error = false;
 	client->tls = tls;
 	client->need = NeedNick | NeedUser | (clientPass ? NeedPass : 0);
 	client->len = 0;
@@ -59,8 +59,8 @@ void clientFree(struct Client *client) {
 	free(client);
 }
 
-bool clientClose(const struct Client *client) {
-	return client->close;
+bool clientError(const struct Client *client) {
+	return client->error;
 }
 
 void clientSend(struct Client *client, const char *ptr, size_t len) {
@@ -71,7 +71,7 @@ void clientSend(struct Client *client, const char *ptr, size_t len) {
 		if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue;
 		if (ret < 0) {
 			warnx("tls_write: %s", tls_error(client->tls));
-			client->close = true;
+			client->error = true;
 			return;
 		}
 		ptr += ret;
@@ -95,7 +95,7 @@ static void passRequired(struct Client *client) {
 		":invalid 464 * :Password incorrect\r\n"
 		"ERROR :Password incorrect\r\n"
 	);
-	client->close = true;
+	client->error = true;
 }
 
 typedef void Handler(struct Client *client, struct Message msg);
@@ -143,7 +143,7 @@ static void clientParse(struct Client *client, char *line) {
 	if (!msg.cmd) {
 		// FIXME: Identify client in message.
 		warnx("no command");
-		client->close = true;
+		client->error = true;
 		return;
 	}
 	for (size_t i = 0; i < ARRAY_LEN(Handlers); ++i) {
@@ -161,7 +161,7 @@ void clientRecv(struct Client *client) {
 	if (read == TLS_WANT_POLLIN || read == TLS_WANT_POLLOUT) return;
 	if (read < 0) warnx("tls_read: %s", tls_error(client->tls));
 	if (read < 1) {
-		client->close = true;
+		client->error = true;
 		return;
 	}
 	client->len += read;