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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include "peer.h"
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)
{
ssize_t got = read(p->socket, p->in.buffer + p->in.len, p->in.promised - p->in.len);
if (got < 0) {
switch (errno) {
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)
// 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, uint8_t *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, uint8_t *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, (uint8_t *) &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
short peer_prepare(peer *p)
{
if (p->in.len == p->in.promised)
next_in(p, true, sizeof(pkt_header));
return POLLIN | (p->out.avail ? POLLOUT : 0);
}
bool peer_ready(peer *p, short revents)
{
bool x = false;
if (revents & POLLIN)
x = x || peer_in_ready(p);
if (revents & POLLOUT)
x = x || peer_out_ready(p);
return x;
}
|