summary refs log tree commit diff
path: root/network.c
diff options
context:
space:
mode:
authorrubin <rubinowyblask@meow.li>2026-05-22 13:42:51 +0200
committerrubin <rubinowyblask@meow.li>2026-05-22 13:42:51 +0200
commit77dc79afe5fd909fb1fe0a3cc668db2763b82fb8 (patch)
treefbca4863a432f33822f8ee97d9fe39c15462b401 /network.c
import repository
Signed-off-by: rubinowy blask <rubinowyblask@encore.hosts.lumine>
Diffstat (limited to 'network.c')
-rw-r--r--network.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/network.c b/network.c
new file mode 100644
index 0000000..263e2cb
--- /dev/null
+++ b/network.c
@@ -0,0 +1,55 @@
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include "network.h"
+
+int
+net_listen4(uint32_t ip, uint16_t port, int backlog)
+{
+	struct sockaddr_in addr_in = { 0 };
+	int sock = 0;
+	int temp = 1;
+
+	if (ip != 0) {
+		return -ENOSYS;
+	}
+
+	if (port == 0) {
+		return -EINVAL;
+	}
+
+	addr_in.sin_port = htons(port);
+	sock = socket(AF_INET, SOCK_STREAM, 0);
+	if (errno != 0) {
+		perror("socket(3)");
+		return -errno;
+	}
+
+	temp = 1;
+	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &temp, sizeof(temp));
+	if (errno != 0) {
+		perror("setsockopt(3)");
+		close(sock);
+		return -errno;
+	}
+
+	bind(sock, (struct sockaddr *)&addr_in, sizeof(addr_in));
+	if (errno != 0) {
+		perror("bind(3)");
+		close(sock);
+		return -errno;
+	}
+
+	listen(sock, backlog);
+	if (errno != 0) {
+		perror("listen(3)");
+		close(sock);
+		return -errno;
+	}
+
+	return sock;
+}
+