-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrmc_pub_context.c
491 lines (379 loc) · 15 KB
/
rmc_pub_context.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright (C) 2018, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author: Magnus Feuer ([email protected])
#include "rmc_internal.h"
#include "rmc_log.h"
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <assert.h>
#include <signal.h>
// =============
// CONTEXT MANAGEMENT
// =============
int rmc_pub_init_context(rmc_pub_context_t** context_res,
// Used to avoid loopback dispatch of published packets
rmc_node_id_t node_id,
char* mcast_group_addr,
int multicast_port,
// Interface IP to bind mcast to. Default: "0.0.0.0" (IFADDR_ANY)
char* mcast_if_addr,
// IP address to listen to for incoming subscription
// connection from subscribers receiving multicast packets
// Default: "0.0.0.0" (IFADDR_ANY)
char* control_listen_if_addr,
int control_listen_port,
user_data_t user_data,
rmc_poll_add_cb_t poll_add,
rmc_poll_modify_cb_t poll_modify,
rmc_poll_remove_cb_t poll_remove,
uint32_t max_subscribers,
void (*payload_free)(void* payload,
payload_len_t payload_len,
user_data_t user_data))
{
struct in_addr addr;
unsigned int seed = rmc_usec_monotonic_timestamp() & 0xFFFFFFFF;
rmc_pub_context_t* ctx = 0;
uint8_t* conn_vec = 0;
if (!context_res || !mcast_group_addr)
return EINVAL;
ctx = (rmc_pub_context_t*) malloc(sizeof(rmc_pub_context_t));
if (!ctx) {
RMC_LOG_FATAL("Could not allocate %d bytes", (int) sizeof(rmc_pub_context_t));
exit(255);
}
*context_res = ctx;
conn_vec = (uint8_t*) malloc(sizeof(rmc_connection_t) * max_subscribers);
if (!conn_vec) {
RMC_LOG_FATAL("Could not allocate %d bytes for connections", (int) sizeof(rmc_connection_t) * max_subscribers);
exit(255);
}
signal(SIGPIPE, SIG_IGN);
// We can throw away seed result since we will only call rand here.
ctx->node_id = node_id?node_id:rand_r(&seed);
ctx->user_data = user_data;
rmc_conn_init_connection_vector(&ctx->conn_vec,
conn_vec,
max_subscribers,
ctx->user_data,
poll_add,
poll_modify,
poll_remove);
if (!control_listen_if_addr)
control_listen_if_addr = "0.0.0.0";
if (!mcast_if_addr)
mcast_if_addr = "0.0.0.0";
if (!inet_aton(mcast_group_addr, &addr)) {
RMC_LOG_WARNING("Could not resolve multicast address %s to IP address\n",
mcast_group_addr);
return EINVAL;
}
ctx->mcast_group_addr = ntohl(addr.s_addr);
if (!inet_aton(control_listen_if_addr, &addr)) {
RMC_LOG_WARNING("Could not resolve tcp listen address %s to IP address\n",
control_listen_if_addr);
return EINVAL;
}
ctx->control_listen_if_addr = ntohl(addr.s_addr);
if (!inet_aton(mcast_if_addr, &addr)) {
fprintf(stderr, "rmc_activate_context(multicast_interface_addr): Could not resolve %s to IP address\n",
mcast_if_addr);
return EINVAL;
}
ctx->mcast_if_addr = ntohl(addr.s_addr);
ctx->mcast_port = multicast_port;
ctx->control_listen_port = control_listen_port;
ctx->mcast_send_descriptor = -1;
ctx->listen_descriptor = -1;
ctx->user_data = user_data;
ctx->conn_vec.poll_add = poll_add;
ctx->conn_vec.poll_modify = poll_modify;
ctx->conn_vec.poll_remove = poll_remove;
ctx->payload_free = payload_free;
ctx->resend_timeout = RMC_DEFAULT_PACKET_TIMEOUT;
ctx->subscriber_connect_cb = 0;
ctx->subscriber_disconnect_cb = 0;
ctx->subscriber_connect_cb = 0;
ctx->announce_cb = 0;
ctx->announce_send_interval = 0;
ctx->announce_next_send_ts = 0;
ctx->traffic_suspend_threshold = 0;
ctx->traffic_resume_threshold = 0;
ctx->traffic_suspended = 0;
// outgoing_payload_free() will be called when
// pub_acket_ack() is called, which happens when a
// subscriber sends an ack back for the given pid.
// When all subscribers have acknowledged,
// payload_free() will be called is called to free the payload.
pub_init_context(&ctx->pub_ctx);
ctx->subscribers = malloc(sizeof(pub_subscriber_t) * max_subscribers);
RMC_LOG_DEBUG("Publisher Context init. node_id[0x%X] mcast_group[%s:%d] mcast_if[%s] user_data[0x%X]",
ctx->node_id, mcast_group_addr, multicast_port, mcast_if_addr, user_data.u32);
return 0;
}
int rmc_pub_activate_context(rmc_pub_context_t* ctx)
{
struct sockaddr_in sock_addr;
struct in_addr in_addr;
socklen_t sock_len = sizeof(struct sockaddr_in);
int on_flag = 1;
if (!ctx)
return EINVAL;
//
// Setup udp send descriptor to be received by multicast members.
//
ctx->mcast_send_descriptor = socket (AF_INET, SOCK_DGRAM, 0);
if (ctx->mcast_send_descriptor == -1) {
RMC_LOG_WARNING("socket(multicast): %s", strerror(errno));
goto error;
}
// Setup multicast group membership
memset((void*) &in_addr, 0, sizeof(in_addr));
in_addr.s_addr = htonl(ctx->mcast_if_addr);
if (setsockopt(ctx->mcast_send_descriptor,
IPPROTO_IP, IP_MULTICAST_IF, &in_addr, sizeof(in_addr)) < 0) {
perror("rmc_activate_context(multicast_recv): setsockopt(IP_MULTICAST_IF)");
goto error;
}
RMC_LOG_INFO("Bound sender to %X", ctx->mcast_if_addr);
// Setup TCP listen
// Did we specify a local interface address to bind to?
ctx->listen_descriptor = socket (AF_INET, SOCK_STREAM, 0);
if (ctx->listen_descriptor == -1) {
RMC_LOG_WARNING("rmc_init_pub_context(): socket(listen): %s", strerror(errno));
goto error;
}
if (setsockopt(ctx->listen_descriptor, SOL_SOCKET,
SO_REUSEADDR, &on_flag, sizeof(on_flag)) < 0) {
RMC_LOG_WARNING("rmc_init_pub_context(): setsockopt(REUSEADDR): %s", strerror(errno));
goto error;
}
if (setsockopt(ctx->listen_descriptor, SOL_SOCKET,
SO_REUSEPORT, &on_flag, sizeof(on_flag)) < 0) {
RMC_LOG_WARNING("rmc_init_pub_context(): setsockopt(SO_REUSEPORT): %s", strerror(errno));
goto error;
}
// Bind to local endpoint.
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = htonl(ctx->control_listen_if_addr);
sock_addr.sin_port = htons(ctx->control_listen_port);
if (bind(ctx->listen_descriptor,
(struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0) {
RMC_LOG_WARNING("rmc_init_pub_context(): bind(): %s", strerror(errno));
goto error;
}
if (listen(ctx->listen_descriptor, RMC_LISTEN_SOCKET_BACKLOG) != 0) {
RMC_LOG_WARNING("rmc_init_pub_context(): listen(): %s", strerror(errno));
goto error;
}
// If control_listen_port is 0, use getsockname() to grab the os-assigned ephereal port.
if (!ctx->control_listen_port) {
sock_len = sizeof(sock_addr);
if (getsockname(ctx->listen_descriptor, (struct sockaddr*) &sock_addr, &sock_len)) {
RMC_LOG_FATAL("getsockname(bind): %s", strerror(errno));
goto error;
}
ctx->control_listen_port = ntohs(sock_addr.sin_port);
RMC_LOG_INFO("Ephereal tcp port picked by OS to use as control port: %d", ctx->control_listen_port);
}
if (ctx->conn_vec.poll_add) {
(*ctx->conn_vec.poll_add)(ctx->user_data,
ctx->mcast_send_descriptor,
RMC_MULTICAST_INDEX,
RMC_POLLREAD);
(*ctx->conn_vec.poll_add)(ctx->user_data,
ctx->listen_descriptor,
RMC_LISTEN_INDEX,
RMC_POLLREAD);
}
return 0;
error:
if (ctx->mcast_send_descriptor != -1) {
close(ctx->mcast_send_descriptor);
ctx->mcast_send_descriptor = -1;
}
if (ctx->listen_descriptor != -1) {
close(ctx->listen_descriptor);
ctx->listen_descriptor = -1;
}
return errno;
}
int rmc_pub_deactivate_context(rmc_pub_context_t* ctx)
{
rmc_index_t ind = 0;
rmc_index_t max = 0;
if (!ctx)
return EINVAL;
// Callback to remnove listen and multicast descriptor
// from caller's poll vector.
if (ctx->conn_vec.poll_remove) {
(*ctx->conn_vec.poll_remove)(ctx->user_data,
ctx->mcast_send_descriptor,
RMC_MULTICAST_INDEX);
(*ctx->conn_vec.poll_remove)(ctx->user_data,
ctx->listen_descriptor,
RMC_LISTEN_INDEX);
}
rmc_conn_get_max_index_in_use(&ctx->conn_vec, &max);
if (max != RMC_NIL_INDEX) {
for(ind = 0; ind <= max; ++ind) {
rmc_connection_t* conn = rmc_conn_find_by_index(&ctx->conn_vec,
ind);
// Don't opereate on closed connections.
if (!conn)
continue;
rmc_pub_close_connection(ctx, ind);
}
}
close(ctx->mcast_send_descriptor);
close(ctx->listen_descriptor);
ctx->mcast_send_descriptor = -1;
ctx->listen_descriptor = -1;
return 0;
}
int rmc_pub_delete_context(rmc_pub_context_t* ctx)
{
if (!ctx)
return EINVAL;
rmc_pub_deactivate_context(ctx);
free(ctx->conn_vec.connections);
free(ctx);
return 0;
}
int rmc_pub_set_announce_interval(rmc_pub_context_t* ctx, uint32_t send_interval_usec)
{
if (!ctx)
return EINVAL;
// If not currently set, do setup a timestamp.
// If we already have a send timestamp, let it run its course per the old
// interval, and then use the new send inteval once it has expired.
if (send_interval_usec && !ctx->announce_next_send_ts)
ctx->announce_next_send_ts = rmc_usec_monotonic_timestamp() + send_interval_usec;
ctx->announce_send_interval = send_interval_usec;
// Wipe any pending announce we are waiting for.
if (!send_interval_usec)
ctx->announce_next_send_ts = 0;
return 0;
}
// Setup IP_MULTICAST_TTL for publishing multicast.
// Can be called once the context is activated.
int rmc_pub_set_multicast_ttl(rmc_pub_context_t* ctx, int hops)
{
if (ctx->mcast_send_descriptor == -1)
return ENOTCONN;
if (setsockopt(ctx->mcast_send_descriptor, IPPROTO_IP,
IP_MULTICAST_TTL, &hops, sizeof(hops)) < 0) {
RMC_LOG_WARNING("rmc_pub_set_multicast_ttl(): setsockopt(IP_MULTICAST_TTL): %s", strerror(errno));
return errno;
}
return 0;
}
int rmc_pub_set_announce_callback(rmc_pub_context_t* ctx,
uint8_t (*announce_cb)(struct rmc_pub_context* ctx,
void* payload,
payload_len_t max_payload_len,
payload_len_t* result_payload_len))
{
if (!ctx)
return EINVAL;
ctx->announce_cb = announce_cb;
return 0;
}
int rmc_pub_set_subscriber_connect_callback(rmc_pub_context_t* ctx,
uint8_t (*connect_cb)(struct rmc_pub_context* ctx,
uint32_t remote_ip,
uint16_t remote_port))
{
if (!ctx)
return EINVAL;
ctx->subscriber_connect_cb = connect_cb;
return 0;
}
int rmc_pub_set_subscriber_disconnect_callback(rmc_pub_context_t* ctx,
void (*disconnect_cb)(struct rmc_pub_context* ctx,
uint32_t remote_ip,
uint16_t remote_port))
{
if (!ctx)
return EINVAL;
ctx->subscriber_disconnect_cb = disconnect_cb;
return 0;
}
int rmc_pub_set_control_message_callback(rmc_pub_context_t* ctx,
void (*control_message_cb)(struct rmc_pub_context* ctx,
uint32_t publisher_address,
uint16_t publisher_port,
rmc_node_id_t node_id,
void* payload,
payload_len_t payload_len))
{
if (!ctx)
return EINVAL;
ctx->subscriber_control_message_cb = control_message_cb;
return 0;
}
user_data_t rmc_pub_user_data(rmc_pub_context_t* ctx)
{
if (!ctx)
return (user_data_t) { .u64 = 0 };
return ctx->user_data;
}
rmc_node_id_t rmc_pub_node_id(rmc_pub_context_t* ctx)
{
if (!ctx)
return 0;
return ctx->node_id;
}
int rmc_pub_set_user_data(rmc_pub_context_t* ctx, user_data_t user_data)
{
if (!ctx)
return EINVAL;
ctx->user_data = user_data;
return 0;
}
rmc_index_t rmc_pub_get_max_subscriber_count(rmc_pub_context_t* ctx)
{
rmc_index_t res = 0;
if (!ctx)
return 0;
rmc_conn_get_vector_size(&ctx->conn_vec, &res);
return res;
}
uint32_t rmc_pub_get_subscriber_count(rmc_pub_context_t* ctx)
{
rmc_index_t res = 0;
if (!ctx)
return 0;
rmc_conn_get_active_connection_count(&ctx->conn_vec, &res);
return res;
}
uint32_t rmc_pub_get_socket_count(rmc_pub_context_t* ctx)
{
if (!ctx)
return 0;
return rmc_pub_get_subscriber_count(ctx) +
((ctx->mcast_send_descriptor != -1)?1:0) +
((ctx->listen_descriptor != -1 )?1:0);
}
int rmc_pub_throttling(rmc_pub_context_t* ctx,
uint32_t suspend_threshold,
uint32_t resume_threshold)
{
if (!ctx)
return EINVAL;
if (suspend_threshold < resume_threshold) {
RMC_LOG_WARNING("Traffic throttling suspend threhold[%d] is less than the restart value[%d]. Ignored.",
suspend_threshold, resume_threshold);
return EINVAL;
}
ctx->traffic_suspend_threshold = suspend_threshold;
ctx->traffic_resume_threshold = resume_threshold;
return 0;
}