From 3dcbd43ecd77c28762b0595475893ff052c0444a Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Mon, 14 Oct 2024 18:29:15 -0600 Subject: wip libnet rewrite --- libnet/CMakeLists.txt | 7 ++++++ libnet/include/libnet/libnet.h | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 libnet/CMakeLists.txt create mode 100644 libnet/include/libnet/libnet.h (limited to 'libnet') diff --git a/libnet/CMakeLists.txt b/libnet/CMakeLists.txt new file mode 100644 index 0000000..32047f0 --- /dev/null +++ b/libnet/CMakeLists.txt @@ -0,0 +1,7 @@ +# libnet/CMakeLists.txt - Build script for libnet support library +# +# Copyright (C) 2024 Luke T. Shumaker +# SPDX-Licence-Identifier: AGPL-3.0-or-later + +add_library(libnet INTERFACE) +target_include_directories(libnet SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) diff --git a/libnet/include/libnet/libnet.h b/libnet/include/libnet/libnet.h new file mode 100644 index 0000000..cef13d9 --- /dev/null +++ b/libnet/include/libnet/libnet.h @@ -0,0 +1,56 @@ +#ifndef _NETIO_H_ +#define _NETIO_H_ + +#include /* for bool */ +#include /* for size_t */ +#include /* for ssize_t */ + +struct libnet_conn; +struct libnet_listener; + +struct libnet_listener_vtable { + /** + * It is invalid to accept() a new connection if an existing + * connection is still open. + */ + struct libnet_conn *(*accept)(struct libnet_listener *self); +}; + +struct libnet_conn_vtable { + /** + * Return bytes-read on success, 0 on EOF, -errno on error; a + * short read is *not* an error. + */ + ssize_t (*read)(struct libnet_conn *self, void *buf, size_t count); + + /** + * Return `count` on success, -errno on error; a short write *is* an + * error. + * + * Writes are *not* guaranteed to be atomic (as this would be + * expensive to implement), so if you have concurrent writers then you + * should arrange for a mutex to protect the connection. + */ + ssize_t (*write)(struct libnet_conn *self, void *buf, size_t count); + + /** + * Return 0 on success, -errno on error. + */ + int (*close)(struct libnet_conn *self, bool rd, bool wr); +}; + +struct libnet_listener { + struct libnet_listener_vtable *vtable; + + /* This is where your implementation data goes. */ + char data[0]; +}; + +struct libnet_conn { + struct libnet_conn_vtable *vtable; + + /* This is where your implementation data goes. */ + char data[0]; +}; + +#endif /* _NETIO_H_ */ -- cgit v1.2.3-2-g168b