summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-10-20 14:49:04 -0400
committerJune McEnroe <june@causal.agency>2019-10-20 14:49:04 -0400
commit48ee1e045709b5d2a08335f700c8b0a94566cf30 (patch)
tree22f356ca60e233fd85875ab0a9c575d03cadd85e
parentMove listen code to listen.c (diff)
downloadpounce-48ee1e045709b5d2a08335f700c8b0a94566cf30.tar.gz
pounce-48ee1e045709b5d2a08335f700c8b0a94566cf30.zip
Remove pollfd from listen interface
-rw-r--r--bouncer.c25
-rw-r--r--bouncer.h6
-rw-r--r--listen.c28
3 files changed, 28 insertions, 31 deletions
diff --git a/bouncer.c b/bouncer.c
index 3bf44cd..0d1d551 100644
--- a/bouncer.c
+++ b/bouncer.c
@@ -29,7 +29,8 @@
 static char *censor(char *arg) {
 	char *dup = strdup(arg);
 	if (!dup) err(EX_OSERR, "strdup");
-	memset(arg, '*', strlen(dup));
+	memset(arg, '\0', strlen(dup));
+	arg[0] = '*';
 	return dup;
 }
 
@@ -84,16 +85,24 @@ int main(int argc, char *argv[]) {
 	if (!user) user = nick;
 	if (!real) real = nick;
 
-	enum { PollCap = 64 };
-	struct pollfd fds[PollCap];
-
 	listenConfig(certPath, privPath);
-	size_t binds = listenBind(fds, PollCap, localHost, localPort);
 
-	while (0 < poll(fds, binds, -1)) {
-		for (size_t i = 0; i < binds; ++i) {
+	enum { BindCap = 8 };
+	int bind[BindCap];
+	size_t bindLen = listenBind(bind, BindCap, localHost, localPort);
+
+	// Wishing for struct-of-arrays...
+	struct pollfd fds[BindCap];
+	for (size_t i = 0; i < bindLen; ++i) {
+		fds[i].fd = bind[i];
+		fds[i].events = POLLIN;
+	}
+
+	while (0 < poll(fds, bindLen, -1)) {
+		for (size_t i = 0; i < bindLen; ++i) {
 			if (!fds[i].revents) continue;
-			struct tls *client = listenAccept(&fds[binds], fds[i].fd);
+			struct tls *client;
+			int fd = listenAccept(&client, fds[i].fd);
 		}
 	}
 }
diff --git a/bouncer.h b/bouncer.h
index b8e6362..13656ad 100644
--- a/bouncer.h
+++ b/bouncer.h
@@ -14,7 +14,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <poll.h>
 #include <stdlib.h>
 #include <tls.h>
 
@@ -27,6 +26,5 @@
 #endif
 
 void listenConfig(const char *cert, const char *priv);
-size_t
-listenBind(struct pollfd fds[], size_t cap, const char *host, const char *port);
-struct tls *listenAccept(struct pollfd *poll, int fd);
+size_t listenBind(int fds[], size_t cap, const char *host, const char *port);
+int listenAccept(struct tls **client, int fd);
diff --git a/listen.c b/listen.c
index 4ecf5d3..cb6b164 100644
--- a/listen.c
+++ b/listen.c
@@ -17,7 +17,6 @@
 #include <err.h>
 #include <netdb.h>
 #include <netinet/in.h>
-#include <poll.h>
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <sysexits.h>
@@ -48,11 +47,7 @@ void listenConfig(const char *cert, const char *priv) {
 	tls_config_free(config);
 }
 
-size_t
-listenBind(
-	struct pollfd fds[], size_t cap,
-	const char *host, const char *port
-) {
+size_t listenBind(int fds[], size_t cap, const char *host, const char *port) {
 	struct addrinfo *head;
 	struct addrinfo hints = {
 		.ai_family = AF_UNSPEC,
@@ -64,21 +59,19 @@ listenBind(
 
 	size_t len = 0;
 	for (struct addrinfo *ai = head; ai && len < cap; ai = ai->ai_next) {
-		int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
-		if (sock < 0) err(EX_OSERR, "socket");
+		fds[len] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+		if (fds[len] < 0) err(EX_OSERR, "socket");
 
-		error = bind(sock, ai->ai_addr, ai->ai_addrlen);
+		error = bind(fds[len], ai->ai_addr, ai->ai_addrlen);
 		if (error) {
 			warn("%s:%s", host, port);
-			close(sock);
+			close(fds[len]);
 			continue;
 		}
 
-		error = listen(sock, 1);
+		error = listen(fds[len], 1);
 		if (error) err(EX_IOERR, "listen");
 
-		fds[len].fd = sock;
-		fds[len].events = POLLIN;
 		len++;
 	}
 	freeaddrinfo(head);
@@ -87,7 +80,7 @@ listenBind(
 	return len;
 }
 
-struct tls *listenAccept(struct pollfd *poll, int fd) {
+int listenAccept(struct tls **client, int fd) {
 	int sock = accept(fd, NULL, NULL);
 	if (sock < 0) err(EX_IOERR, "accept");
 
@@ -95,11 +88,8 @@ struct tls *listenAccept(struct pollfd *poll, int fd) {
 	int error = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes));
 	if (error) err(EX_OSERR, "setsockopt");
 
-	struct tls *client;
-	error = tls_accept_socket(server, &client, sock);
+	error = tls_accept_socket(server, client, sock);
 	if (error) errx(EX_SOFTWARE, "tls_accept_socket: %s", tls_error(server));
 
-	poll->fd = sock;
-	poll->events = POLLIN;
-	return client;
+	return sock;
 }