summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/net.h18
1 files changed, 15 insertions, 3 deletions
diff --git a/include/net.h b/include/net.h
index fcb12a4..ce2eb02 100644
--- a/include/net.h
+++ b/include/net.h
@@ -5,13 +5,16 @@
#ifndef NET_H
#define NET_H
+#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
+#include <stdlib.h>
#include <poll.h>
#include <fcntl.h>
-
#include "str.h"
+#include "ser.h"
+#include "content.h"
#define SET_NONBLOCK(X) fcntl((X), F_SETFL, fcntl((X), F_GETFL, 0) | O_NONBLOCK)
@@ -23,6 +26,7 @@ typedef uint32_t pkt_header;
typedef struct {
int socket;
bool disco;
+ str *name;
struct {
bool header;
size_t len;
@@ -37,12 +41,20 @@ typedef struct {
} out;
} peer;
-void invalid_pkt(str from, str pkt);
+#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);
+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);