commit 68fe2af0e9be2c249c906ac3d24f2f15e340009d
parent e1e840b672e42d2545837d606b81a6fd365ed01d
Author: Francesco Saccone <francesco@francescosaccone.com>
Date: Mon, 31 Mar 2025 18:38:26 +0200
feat: add get_socket_size function
Signed-off-by: Francesco Saccone <francesco@francescosaccone.com>
Diffstat:
2 files changed, 19 insertions(+), 0 deletions(-)
diff --git a/socket.c b/socket.c
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
@@ -42,6 +43,17 @@ close_socket(int socket_fd) {
}
int
+get_socket_size(int socket_fd) {
+ int result;
+
+ if (ioctl(socket_fd, FIONREAD, &result) == -1) {
+ return -1;
+ };
+
+ return result;
+}
+
+int
accept_client(int server_socket_fd) {
struct sockaddr_in client_address;
socklen_t client_address_length = sizeof(client_address);
diff --git a/socket.h b/socket.h
@@ -16,6 +16,13 @@ create_socket(unsigned int port);
void
close_socket(int socket_fd);
+/*
+ * Returns -1 in case of an error and the length of a socket (actually a
+ * generic file descriptor) otherwise.
+ */
+int
+get_socket_size(int socket_fd);
+
/*
* Accepts a connection from the server socket and returns the client socket
* file descriptor.