diff options
Diffstat (limited to 'libnet')
-rw-r--r-- | libnet/CMakeLists.txt | 7 | ||||
-rw-r--r-- | libnet/include/libnet/libnet.h | 56 |
2 files changed, 63 insertions, 0 deletions
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 <lukeshu@lukeshu.com> +# 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 <stdbool.h> /* for bool */ +#include <stddef.h> /* for size_t */ +#include <sys/types.h> /* 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_ */ |