-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp-balancer.c
312 lines (252 loc) · 6.47 KB
/
udp-balancer.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* udp-balancer.c - Very simple UDP/GELF round robin load balancer
*
* Copyright (c) 2017 - Jo-Philipp Wich <[email protected]>
*
* This file falls under the MIT License.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <inttypes.h>
#define DEFAULT_CONFIG "/etc/udp-balancer.conf"
#define BUFFER_SIZE 65536
static uint64_t seqnr = 0;
static struct {
bool handle_gelf;
unsigned int send_buffer;
unsigned int recv_buffer;
uint8_t nremotes;
struct sockaddr_in local;
struct sockaddr_in remotes[256];
} conf;
/*
* Parse addr:port notation into a struct sockaddr_in.
*
* Modifies the given string buffer, expects IPv4.
*/
static int parse_addr(char *addr, struct sockaddr_in *si)
{
char *ptr, *e;
unsigned int n;
ptr = strtok(addr, ":");
if (!ptr)
return -1;
if (!inet_pton(AF_INET, ptr, &si->sin_addr))
return -1;
ptr = strtok(NULL, " \t\n");
if (!ptr)
return -1;
n = strtoul(ptr, &e, 10);
if (e == ptr || *e || n > 65535)
return -1;
si->sin_port = htons(n);
si->sin_family = AF_INET;
return 0;
}
/*
* Parse given configuration file and fill local structs.
*
* A configuration file consists of one "listen ipaddr:port" directive and
* a series of "upstream ipaddr:port" directives in arbitrary order.
*
* Additionally supported directives are:
*
* handle-gelf Enables special handling of GELF chunk messages
* send-buffer # Override the size of the socket send buffer
* recv-buffer # Override the size of the socket recv buffer
*/
static int parse_config(const char *filename)
{
char line[128], *ptr, *e;
int ln = 0;
FILE *fp;
fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Unable to open file \"%s\": %s\n",
filename, strerror(errno));
return -1;
}
while (fgets(line, sizeof(line), fp))
{
ln++;
ptr = strtok(line, " \t\n");
if (!ptr)
continue;
if (!strcmp(ptr, "listen"))
{
ptr = strtok(NULL, " \t\n");
if (!ptr || parse_addr(ptr, &conf.local)) {
fprintf(stderr, "Invalid listen directive at line %d\n", ln);
return -1;
}
}
else if (!strcmp(ptr, "upstream"))
{
ptr = strtok(NULL, " \t\n");
if (!ptr || parse_addr(ptr, &conf.remotes[conf.nremotes++])) {
fprintf(stderr, "Invalid upstream directive at line %d\n", ln);
return -1;
}
}
else if (!strcmp(ptr, "handle-gelf"))
{
conf.handle_gelf = true;
}
else if (!strcmp(ptr, "send-buffer"))
{
ptr = strtok(NULL, " \t\n");
conf.send_buffer = ptr ? strtoul(ptr, &e, 0) : 0;
if (!ptr || e == ptr || *e || !conf.send_buffer) {
fprintf(stderr, "Invalid send buffer value at line %d\n", ln);
return -1;
}
}
else if (!strcmp(ptr, "recv-buffer"))
{
ptr = strtok(NULL, " \t\n");
conf.recv_buffer = ptr ? strtoul(ptr, &e, 0) : 0;
if (!ptr || e == ptr || *e || !conf.recv_buffer) {
fprintf(stderr, "Invalid recv buffer value at line %d\n", ln);
return -1;
}
}
else {
fprintf(stderr, "Unknown keyword \"%s\" at line %d\n", ptr, ln);
return -1;
}
}
fclose(fp);
return 0;
}
/*
* Simple CRC8 implementation.
*
* Polynomial is 0x81
*/
static uint8_t crc8(const char *msg, ssize_t len)
{
uint8_t crc = 0;
uint8_t bit;
while (len--) {
crc ^= (uint8_t)*msg++;
for (bit = 8; bit > 0; bit--) {
crc = (crc << 1);
if (crc & 0x80)
crc ^= 0x81;
}
}
return crc;
}
/*
* Select upstream address to relay to.
*
* If the payload carries a GELF chunk magic, then calculate CRC8 over the
* message ID (bytes 3-10) modulo the number of upstreams to ensure that
* all chunks end up on the same backend.
*
* For all other payloads simply forward the packet to the next upstream
* in a round robin fashion by taking the sent message counter modulo the
* number of upstreams.
*/
static struct sockaddr *
toaddr(const char *payload)
{
uint8_t remote;
if (conf.handle_gelf && !memcmp(payload, "\036\017", 2))
remote = crc8(payload + 2, 8) % conf.nremotes;
else
remote = seqnr++ % conf.nremotes;
return (struct sockaddr *)&conf.remotes[remote];
}
/*
* Format a sockaddr struct into a human readable notation using a static
* fixed buffer.
*/
static char *addstr(const void *ptr)
{
static char buf[INET6_ADDRSTRLEN + sizeof(":65535")];
const struct sockaddr_in *sa = ptr;
memset(buf, 0, sizeof(buf));
inet_ntop(sa->sin_family, &sa->sin_addr, buf, sizeof(buf));
sprintf(buf + strlen(buf), ":%u", ntohs(sa->sin_port));
return buf;
}
int main(int argc, char *argv[])
{
int sock;
socklen_t fromlen;
char pkt[BUFFER_SIZE];
struct sockaddr_in from;
ssize_t recvlen, sendlen;
struct sockaddr *sa;
const char *config = DEFAULT_CONFIG;
if (argc > 1)
config = argv[1];
if (parse_config(config))
{
fprintf(stderr, "Failed to parse configuration\n");
return -1;
}
if (!conf.local.sin_family) {
fprintf(stderr, "No listen address defined\n");
return -1;
}
if (!conf.nremotes) {
fprintf(stderr, "No upstream addresses defined\n");
return -1;
}
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
fprintf(stderr, "socket(): %s (%d)\n", strerror(errno), errno);
return -1;
}
if (conf.send_buffer &&
setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
&conf.send_buffer, sizeof(conf.send_buffer))) {
fprintf(stderr, "setsockopt(SO_SNDBUF): %s (%d)\n",
strerror(errno), errno);
}
if (conf.recv_buffer &&
setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
&conf.recv_buffer, sizeof(conf.recv_buffer))) {
fprintf(stderr, "setsockopt(SO_RCVBUF): %s (%d)\n",
strerror(errno), errno);
}
if (bind(sock, (struct sockaddr *)&conf.local, sizeof(conf.local)) < 0) {
fprintf(stderr, "bind(%s): %s (%d)\n",
addstr(&conf.local), strerror(errno), errno);
close(sock);
return -1;
}
while (1) {
fromlen = sizeof(from);
recvlen = recvfrom(sock, pkt, sizeof(pkt), 0,
(struct sockaddr *)&from, &fromlen);
if (recvlen < 0) {
fprintf(stderr, "recvfrom(%s): %s (%d)\n",
addstr(&from), strerror(errno), errno);
break;
}
if (recvlen < 12) {
fprintf(stderr, "recvfrom(%s): bad packet\n", addstr(&from));
continue;
}
sa = toaddr(pkt);
sendlen = sendto(sock, pkt, recvlen, 0, sa, sizeof(*sa));
if (sendlen != recvlen) {
fprintf(stderr, "sendto(%s): %s (%d)\n",
addstr(sa), strerror(errno), errno);
continue;
}
}
close(sock);
return 0;
}