From 591232eba1809643df1f4a6075ff9581b8bb2f7a Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Sat, 13 Jul 2024 12:51:05 +0200 Subject: move headers to src Signed-off-by: Lizzy Fleckenstein --- src/net.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/net.h (limited to 'src/net.h') diff --git a/src/net.h b/src/net.h new file mode 100644 index 0000000..08c566a --- /dev/null +++ b/src/net.h @@ -0,0 +1,63 @@ +// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#ifndef NET_H +#define NET_H + +#include +#include +#include +#include +#include +#include +#include +#include "str.h" +#include "ser.h" +#include "content.h" + +#define SET_NONBLOCK(X) fcntl((X), F_SETFL, fcntl((X), F_GETFL, 0) | O_NONBLOCK) + +#define PEER_INBUFFER_SIZE 0x100000 // 1MB +#define PEER_OUTBUFFER_SIZE 0x200000 // 2MB + +typedef uint32_t pkt_header; + +typedef struct { + int socket; + bool disco; + str *name; + struct { + bool header; + size_t len; + size_t promised; + uint8_t *buffer; + } in; + // TODO: ring buffer + struct { + size_t avail; + size_t cursor; + uint8_t *buffer; + } out; +} peer; + +#define SEND_PKT(CONN, TYPE, ...) \ + { strbuf pkt = NILSBUF; \ + ser_pkt_type(&pkt, TYPE); \ + __VA_ARGS__ \ + if (!peer_send(&(CONN), pkt.data, pkt.len)) \ + fprintf(stderr, "failed to send " #TYPE " to %.*s\n", PSTR(*(CONN).name)); \ + free(pkt.data); } + +void invalid_pkt(peer *p, str pkt); + +int socket_create(const char *host, const char *port, bool server); +int socket_accept(int accept_fd); + +void peer_init(peer *p, int socket, str *name); +void peer_free(peer *p); +struct pollfd peer_prepare(peer *p); +str peer_recv(peer *p, struct pollfd pfd); +bool peer_send(peer *p, void *data, size_t len); + +#endif -- cgit v1.2.3