diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-10-23 15:21:09 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-10-23 15:21:09 -0600 |
commit | e7378b9f9b122e05baebce387fe9c3844c0736b6 (patch) | |
tree | ce921a21811cb4ded792f61f2d933cf4e8f87a96 /libmisc/include | |
parent | 9940877f4bb44bc69fecf7fb5afcf3cbab90f088 (diff) |
wip
Diffstat (limited to 'libmisc/include')
-rw-r--r-- | libmisc/include/libmisc/hash.h | 31 | ||||
-rw-r--r-- | libmisc/include/libmisc/net.h | 55 |
2 files changed, 72 insertions, 14 deletions
diff --git a/libmisc/include/libmisc/hash.h b/libmisc/include/libmisc/hash.h new file mode 100644 index 0000000..5940e64 --- /dev/null +++ b/libmisc/include/libmisc/hash.h @@ -0,0 +1,31 @@ +/* libmisc/hash.h - General-purpose hash utilities + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-Licence-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBMISC_HASH_H_ +#define _LIBMISC_HASH_H_ + +#include <stdint.h> /* for uint{n}_t */ +#include <stddef.h> /* for size_t */ + +/* djb2 hash */ +typedef uint32_t hash_t; +static inline void hash_init(hash_t *hash) { + *hash = 5381; +} +static inline void hash_write(hash_t *hash, void *dat, size_t len) { + for (size_t i = 0; i < len; i++) + *hash = (*hash * 33) + (hash_t)(((unsigned char *)dat)[i]); +} + +/* utilities */ +static inline hash_t hash(void *dat, size_t len) { + hash_t h; + hash_init(&h); + hash_write(&h, dat, len); + return h; +} + +#endif /* _LIBMISC_HASH_H_ */ diff --git a/libmisc/include/libmisc/net.h b/libmisc/include/libmisc/net.h index e5648f5..43c4a48 100644 --- a/libmisc/include/libmisc/net.h +++ b/libmisc/include/libmisc/net.h @@ -9,8 +9,17 @@ #include <stdbool.h> /* for bool */ #include <stddef.h> /* for size_t */ +#include <stdint.h> /* for uint{n}_t} */ #include <sys/types.h> /* for ssize_t */ +/* Errnos *********************************************************************/ + +#define NET_EOTHER 1 +#define NET_ETIMEDOUT 2 +#define NET_ETHREAD 3 + +/* Address types **************************************************************/ + struct net_ip4_addr { unsigned char octets[4]; }; @@ -22,23 +31,25 @@ struct net_eth_addr { unsigned char octets[6]; }; -struct net_conn; -struct net_listener; +/* Streams (e.g. TCP) *********************************************************/ -struct net_listener_vtable { +struct net_stream_listener; +struct net_stream_conn; + +struct net_stream_listener_vtable { /** * It is invalid to accept() a new connection if an existing * connection is still open. */ - struct net_conn *(*accept)(struct net_listener *self); + struct net_stream_conn *(*accept)(struct net_stream_listener *self); }; -struct net_conn_vtable { +struct net_stream_conn_vtable { /** * Return bytes-read on success, 0 on EOF, -errno on error; a * short read is *not* an error. */ - ssize_t (*read)(struct net_conn *self, void *buf, size_t count); + ssize_t (*read)(struct net_stream_conn *self, void *buf, size_t count); /** * Return `count` on success, -errno on error; a short write *is* an @@ -48,20 +59,36 @@ struct net_conn_vtable { * expensive to implement), so if you have concurrent writers then you * should arrange for a mutex to protect the connection. */ - ssize_t (*write)(struct net_conn *self, void *buf, size_t count); + ssize_t (*write)(struct net_stream_conn *self, void *buf, size_t count); /** * Return 0 on success, -errno on error. */ - int (*close)(struct net_conn *self, bool rd, bool wr); + int (*close)(struct net_stream_conn *self, bool rd, bool wr); }; -typedef struct net_listener { - struct net_listener_vtable *vtable; -} implements_net_listener; +typedef struct net_stream_listener { + struct net_stream_listener_vtable *vtable; +} implements_net_stream_listener; + +typedef struct net_stream_conn { + struct net_stream_conn_vtable *vtable; +} implements_net_stream_conn; + +/* Packets (e.g. UDP) *********************************************************/ + +struct net_packet_conn; + +struct net_packet_conn_vtable { + ssize_t (*sendto )(struct net_packet_conn *self, void *buf, size_t len, + struct net_ip4_addr node, uint16_t port); + ssize_t (*recvfrom)(struct net_packet_conn *self, void *buf, size_t len, + struct net_ip4_addr *ret_node, uint16_t *ret_port); + int (*close )(struct net_packet_conn *self); +}; -typedef struct net_conn { - struct net_conn_vtable *vtable; -} implements_net_conn; +typedef struct net_packet_conn { + struct net_packet_conn_vtable *vtable; +} implements_net_packet_conn; #endif /* _LIBMISC_NET_H_ */ |