1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "net.h"
void invalid_pkt(str from, str pkt)
{
// TODO: maybe inform peer about failure? not sure
// FIXME: hexdumping a gazillon bytes to stderr might be an issue
fprintf(stderr, "invalid pkt from %*s: ", PSTR(from));
for (size_t i = 0; i < pkt.len; i++)
fprintf(stderr, "%02x%c", (uint8_t) pkt.data[i], i+1 == pkt.len ? '\n' : ' ');
}
int socket_create(const char *host, const char *port, bool server)
{
struct addrinfo *info = NULL, hints = {0};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int err;
if ((err = getaddrinfo(host, port, &hints, &info))) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
return -1;
}
#define TRY(op, val) if ((val) < 0) { perror(op); close(fd); freeaddrinfo(info); return -1; }
int fd = -1;
TRY("socket", fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol))
int flag = 1;
TRY("setsockopt", setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &flag, sizeof flag))
if (server) {
TRY("bind", bind(fd, info->ai_addr, info->ai_addrlen))
TRY("listen", listen(fd, 3))
} else {
TRY("connect", connect(fd, info->ai_addr, info->ai_addrlen))
TRY("fcntl", SET_NONBLOCK(fd))
}
#undef TRY
freeaddrinfo(info);
return fd;
}
int socket_accept(int accept_fd)
{
int socket = accept(accept_fd, NULL, NULL); // TODO: save address
if (socket < 0) {
perror("accept");
return -1;
}
if (SET_NONBLOCK(socket) < 0) {
perror("fcntl");
close(socket);
return -1;
}
return socket;
}
void peer_init(peer *p, int socket)
{
p->socket = socket;
p->disco = false;
p->in.header = true;
p->in.len = 0;
p->in.promised = 0;
p->in.buffer = malloc(PEER_INBUFFER_SIZE);
p->out.cursor = 0;
p->out.avail = 0;
p->out.buffer = malloc(PEER_OUTBUFFER_SIZE);
}
void peer_free(peer *p)
{
close(p->socket);
free(p->in.buffer);
free(p->out.buffer);
}
// in
static void next_in(peer *p, bool header, size_t len)
{
p->in.header = header;
p->in.len = 0;
p->in.buffer = malloc(p->in.promised = len);
}
static bool peer_in_ready(peer *p)
{
errno = 0;
ssize_t got = read(p->socket, p->in.buffer + p->in.len, p->in.promised - p->in.len);
if (got <= 0) {
switch (errno) {
case 0: case ECONNRESET:
p->disco = true;
return true;
case EINTR:
return peer_in_ready(p); // retry
default:
perror("read");
return false;
}
}
p->in.len += got;
if (p->in.len == p->in.promised && p->in.len != 0) {
if (!p->in.header)
return true;
size_t len = *(pkt_header *) p->in.buffer;
if (len > PEER_INBUFFER_SIZE || len == 0)
// TODO: figure out what to do if packet too large (disconnect?)
next_in(p, true, sizeof(pkt_header));
else
next_in(p, false, len);
}
return false;
}
// out
static void send_raw(peer *p, void *data, size_t len)
{
memcpy(p->out.buffer + p->out.cursor + p->out.avail, data, len);
p->out.avail += len;
}
static bool out_space(peer *p, size_t len)
{
if (len + p->out.avail > PEER_OUTBUFFER_SIZE)
return false;
if (p->out.cursor + p->out.avail + len > PEER_OUTBUFFER_SIZE) {
memmove(p->out.buffer, p->out.buffer + p->out.cursor, p->out.avail);
p->out.cursor = 0;
}
return true;
}
bool peer_send(peer *p, void *data, size_t len)
{
if (len > PEER_INBUFFER_SIZE)
return false;
pkt_header hdr = (pkt_header) len;
if (!out_space(p, sizeof hdr + len))
return false;
send_raw(p, &hdr, sizeof hdr);
send_raw(p, data, len);
return true;
}
static bool peer_out_ready(peer *p)
{
ssize_t written = write(p->socket, p->out.buffer + p->out.cursor, p->out.avail);
if (written < 0) {
switch (errno) {
case ECONNRESET:
p->disco = true;
return true;
case EINTR:
return peer_out_ready(p);
default:
perror("write");
return false;
}
}
p->out.avail -= written;
if (p->out.avail == 0)
p->out.cursor = 0;
return false;
}
// poll
struct pollfd peer_prepare(peer *p)
{
struct pollfd pfd = {0};
pfd.fd = p->socket;
if (p->in.len == p->in.promised)
next_in(p, true, sizeof(pkt_header));
pfd.events = POLLIN | (p->out.avail ? POLLOUT : 0);
return pfd;
}
str peer_recv(peer *p, struct pollfd pfd)
{
if (pfd.revents & (POLLHUP | POLLERR))
p->disco = true;
if (!p->disco && pfd.revents & POLLOUT)
peer_out_ready(p);
if (!p->disco && pfd.revents & POLLIN)
if (peer_in_ready(p))
return (str) { p->in.len, (char *) p->in.buffer };
return NILS;
}
|