aboutsummaryrefslogtreecommitdiffhomepage
path: root/backends/sacn.c
blob: 7fc7a592ca9b4131ae96b24f08ec41d3887c093c (plain)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <netinet/in.h>

#include "sacn.h"
//upper limit imposed by using the fd index as 16-bit part of the instance id
#define MAX_FDS 4096
#define BACKEND_NAME "sacn"

static struct /*_sacn_global_config*/ {
	uint8_t source_name[64];
	uint8_t cid[16];
	size_t fds;
	sacn_fd* fd;
	uint64_t last_announce;
} global_cfg = {
	.source_name = "MIDIMonster",
	.cid = {'M', 'I', 'D', 'I', 'M', 'o', 'n', 's', 't', 'e', 'r'},
	.fds = 0,
	.fd = NULL,
	.last_announce = 0
};

int init(){
	backend sacn = {
		.name = BACKEND_NAME,
		.conf = sacn_configure,
		.create = sacn_instance,
		.conf_instance = sacn_configure_instance,
		.channel = sacn_channel,
		.handle = sacn_set,
		.process = sacn_handle,
		.start = sacn_start,
		.shutdown = sacn_shutdown
	};

	//register the backend
	if(mm_backend_register(sacn)){
		fprintf(stderr, "Failed to register sACN backend\n");
		return 1;
	}

	return 0;
}

static int sacn_listener(char* host, char* port, uint8_t fd_flags){
	int fd = -1, status, yes = 1, flags;
	struct addrinfo hints = {
		.ai_family = AF_UNSPEC,
		.ai_socktype = SOCK_DGRAM,
		.ai_flags = AI_PASSIVE
	};
	struct addrinfo* info;
	struct addrinfo* addr_it;

	if(global_cfg.fds >= MAX_FDS){
		fprintf(stderr, "sACN backend descriptor limit reached\n");
		return -1;
	}

	status = getaddrinfo(host, port, &hints, &info);
	if(status){
		fprintf(stderr, "Failed to get socket info for %s port %s: %s\n", host, port, gai_strerror(status));
		return -1;
	}

	for(addr_it = info; addr_it != NULL; addr_it = addr_it->ai_next){
		fd = socket(addr_it->ai_family, addr_it->ai_socktype, addr_it->ai_protocol);
		if(fd < 0){
			continue;
		}

		yes = 1;
		if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0){
			fprintf(stderr, "Failed to set SO_REUSEADDR on socket\n");
		}

		yes = 1;
		if(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void*)&yes, sizeof(yes)) < 0){
			fprintf(stderr, "Failed to set SO_BROADCAST on socket\n");
		}

		yes = 0;
		if(setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void*)&yes, sizeof(yes)) < 0){
			fprintf(stderr, "Failed to unset IP_MULTICAST_LOOP option: %s\n", strerror(errno));
		}

		status = bind(fd, addr_it->ai_addr, addr_it->ai_addrlen);
		if(status < 0){
			close(fd);
			continue;
		}

		break;
	}

	freeaddrinfo(info);

	if(!addr_it){
		fprintf(stderr, "Failed to create listening socket for %s port %s\n", host, port);
		return -1;
	}

	//set nonblocking
	flags = fcntl(fd, F_GETFL, 0);
	if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0){
		fprintf(stderr, "Failed to set sACN descriptor nonblocking\n");
		close(fd);
		return -1;
	}

	//store fd
	global_cfg.fd = realloc(global_cfg.fd, (global_cfg.fds + 1) * sizeof(sacn_fd));
	if(!global_cfg.fd){
		close(fd);
		fprintf(stderr, "Failed to allocate memory\n");
		return -1;
	}

	fprintf(stderr, "sACN backend interface %zu bound to %s port %s\n", global_cfg.fds, host, port);
	global_cfg.fd[global_cfg.fds].fd = fd;
	global_cfg.fd[global_cfg.fds].flags = fd_flags;
	global_cfg.fd[global_cfg.fds].universes = 0;
	global_cfg.fd[global_cfg.fds].universe = NULL;
	global_cfg.fd[global_cfg.fds].last_frame = NULL;
	global_cfg.fds++;
	return 0;
}

static int sacn_parse_addr(char* host, char* port, struct sockaddr_storage* addr, socklen_t* len){
	struct addrinfo* head;
	struct addrinfo hints = {
		.ai_family = AF_UNSPEC,
		.ai_socktype = SOCK_DGRAM
	};

	int error = getaddrinfo(host, port, &hints, &head);
	if(error || !head){
		fprintf(stderr, "Failed to parse address %s port %s: %s\n", host, port, gai_strerror(error));
		return 1;
	}

	memcpy(addr, head->ai_addr, head->ai_addrlen);
	*len = head->ai_addrlen;

	freeaddrinfo(head);
	return 0;
}

static int sacn_parse_hostspec(char* in, char** host, char** port, uint8_t* flags){
	size_t u;

	if(!in || !host || !port){
		return 1;
	}

	for(u = 0; in[u] && !isspace(in[u]); u++){
	}

	//guess
	*host = in;

	if(in[u]){
		in[u] = 0;
		*port = in + u + 1;
	}
	else{
		//no port given
		*port = SACN_PORT;
	}

	if(flags){
		//TODO parse hostspec trailing data for options
		*flags = 0;
	}
	return 0;
}

static int sacn_configure(char* option, char* value){
	char* host = NULL, *port = NULL, *next = NULL;
	uint8_t flags = 0;
	size_t u;

	if(!strcmp(option, "name")){
		if(strlen(value) > 63){
			fprintf(stderr, "Invalid sACN source name %s, limit is 63 characters\n", value);
			return 1;
		}

		memset(global_cfg.source_name, 0, sizeof(global_cfg.source_name));
		memcpy(global_cfg.source_name, value, strlen(value));
		return 0;
	}
	else if(!strcmp(option, "cid")){
		next = value;
		for(u = 0; u < sizeof(global_cfg.cid); u++){
			global_cfg.cid[u] = (strtoul(next, &next, 0) & 0xFF);
		}
	}
	else if(!strcmp(option, "bind")){
		if(sacn_parse_hostspec(value, &host, &port, &flags)){
			fprintf(stderr, "Not a valid sACN bind address: %s\n", value);
			return 1;
		}

		if(sacn_listener(host, port, flags)){
			fprintf(stderr, "Failed to bind sACN descriptor: %s\n", value);
			return 1;
		}
		return 0;
	}

	fprintf(stderr, "Unknown sACN backend option %s\n", option);
	return 1;
}

static int sacn_configure_instance(instance* inst, char* option, char* value){
	sacn_instance_data* data = (sacn_instance_data*) inst->impl;
	char* host = NULL, *port = NULL, *next = NULL;
	size_t u;

	if(!strcmp(option, "universe")){
		data->uni = strtoul(value, NULL, 10);
		return 0;
	}
	else if(!strcmp(option, "interface")){
		data->fd_index = strtoul(value, NULL, 10);

		if(data->fd_index >= global_cfg.fds){
			fprintf(stderr, "Configured sACN interface index is out of range on instance %s\n", inst->name);
			return 1;
		}
		return 0;
	}
	else if(!strcmp(option, "priority")){
		data->xmit_prio = strtoul(value, NULL, 10);
		return 0;
	}
	else if(!strcmp(option, "destination")){
		if(sacn_parse_hostspec(value, &host, &port, NULL)){
			fprintf(stderr, "Not a valid sACN destination for instance %s: %s\n", inst->name, value);
			return 1;
		}

		return sacn_parse_addr(host, port, &data->dest_addr, &data->dest_len);
	}
	else if(!strcmp(option, "from")){
		next = value;
		data->filter_enabled = 1;
		for(u = 0; u < sizeof(data->cid_filter); u++){
			data->cid_filter[u] = (strtoul(next, &next, 0) & 0xFF);
		}
		fprintf(stderr, "Enabled source CID filter for instance %s\n", inst->name);
		return 0;
	}
	else if(!strcmp(option, "unicast")){
		data->unicast_input = strtoul(value, NULL, 10);
	}

	fprintf(stderr, "Unknown configuration option %s for sACN backend\n", option);
	return 1;
}

static instance* sacn_instance(){
	instance* inst = mm_instance();
	if(!inst){
		return NULL;
	}

	inst->impl = calloc(1, sizeof(sacn_instance_data));
	if(!inst->impl){
		fprintf(stderr, "Failed to allocate memory");
		return NULL;
	}

	return inst;
}

static channel* sacn_channel(instance* inst, char* spec){
	sacn_instance_data* data = (sacn_instance_data*) inst->impl;
	char* spec_next = spec;

	unsigned chan_a = strtoul(spec, &spec_next, 10), chan_b = 0;
	
	//range check
	if(!chan_a || chan_a > 512){
		fprintf(stderr, "sACN channel out of range on instance %s: %s\n", inst->name, spec);
		return NULL;
	}
	chan_a--;

	//if wide channel, mark fine
	if(*spec_next == '+'){
		chan_b = strtoul(spec_next + 1, NULL, 10);
		if(!chan_b || chan_b > 512){
			fprintf(stderr, "Invalid wide-channel spec on instance %s: %s\n", inst->name, spec);
			return NULL;
		}
		chan_b--;

		//if already mapped, bail
		if(IS_ACTIVE(data->data.map[chan_b]) && data->data.map[chan_b] != (MAP_FINE | chan_a)){
			fprintf(stderr, "Fine channel %u already mapped on instance %s\n", chan_b, inst->name);
			return NULL;
		}

		data->data.map[chan_b] = MAP_FINE | chan_a;
	}

	//if already active, assert that nothing changes
	if(IS_ACTIVE(data->data.map[chan_a])){
		if((*spec_next == '+' && data->data.map[chan_a] != (MAP_COARSE | chan_b))
				|| (*spec_next != '+' && data->data.map[chan_a] != (MAP_SINGLE | chan_a))){
			fprintf(stderr, "Primary sACN channel %u already mapped in another mode on instance %s\n", chan_a, inst->name);
			return NULL;
		}
	}

	data->data.map[chan_a] = (*spec_next == '+') ? (MAP_COARSE | chan_b) : (MAP_SINGLE | chan_a);
	return mm_channel(inst, chan_a, 1);
}

static int sacn_transmit(instance* inst){
	size_t u;
	sacn_instance_data* data = (sacn_instance_data*) inst->impl;
	sacn_data_pdu pdu = {
		.root = {
			.preamble_size = htobe16(0x10),
			.postamble_size = 0,
			.magic = { 0 }, //memcpy'd
			.flags = htobe16(0x7000 | 0x026e),
			.vector = htobe32(ROOT_E131_DATA),
			.sender_cid = { 0 }, //memcpy'd
			.frame_flags = htobe16(0x7000 | 0x0258),
			.frame_vector = htobe32(FRAME_E131_DATA)
		},
		.data = {
			.source_name = "", //memcpy'd
			.priority = data->xmit_prio,
			.sync_addr = 0,
			.sequence = data->data.last_seq++,
			.options = 0,
			.universe = htobe16(data->uni),
			.flags = htobe16(0x7000 | 0x0205),
			.vector = DMP_SET_PROPERTY,
			.format = 0xA1,
			.startcode_offset = 0,
			.address_increment = htobe16(1),
			.channels = htobe16(513),
			.data = { 0 } //memcpy'd
		}
	};

	memcpy(pdu.root.magic, SACN_PDU_MAGIC, sizeof(pdu.root.magic));
	memcpy(pdu.root.sender_cid, global_cfg.cid, sizeof(pdu.root.sender_cid));
	memcpy(pdu.data.source_name, global_cfg.source_name, sizeof(pdu.data.source_name));
	memcpy((((uint8_t*)pdu.data.data) + 1), data->data.out, 512);

	if(sendto(global_cfg.fd[data->fd_index].fd, &pdu, sizeof(pdu), 0, (struct sockaddr*) &data->dest_addr, data->dest_len) < 0){
		fprintf(stderr, "Failed to output sACN frame for instance %s: %s\n", inst->name, strerror(errno));
	}

	//update last transmit timestamp
	for(u = 0; u < global_cfg.fd[data->fd_index].universes; u++){
		if(global_cfg.fd[data->fd_index].universe[u] == data->uni){
			global_cfg.fd[data->fd_index].last_frame[u] = mm_timestamp();
		}
	}
	return 0;
}

static int sacn_set(instance* inst, size_t num, channel** c, channel_value* v){
	size_t u, mark = 0;
	sacn_instance_data* data = (sacn_instance_data*) inst->impl;

	if(!num){
		return 0;
	}

	if(!data->xmit_prio){
		fprintf(stderr, "sACN instance %s not enabled for output (%zu channel events)\n", inst->name, num);
		return 0;
	}

	for(u = 0; u < num; u++){
		if(IS_WIDE(data->data.map[c[u]->ident])){
			uint32_t val = v[u].normalised * ((double) 0xFFFF);

			if(data->data.out[c[u]->ident] != ((val >> 8) & 0xFF)){
				mark = 1;
				data->data.out[c[u]->ident] = (val >> 8) & 0xFF;
			}

			if(data->data.out[MAPPED_CHANNEL(data->data.map[c[u]->ident])] != (val & 0xFF)){
				mark = 1;
				data->data.out[MAPPED_CHANNEL(data->data.map[c[u]->ident])] = val & 0xFF;
			}
		}
		else if(data->data.out[c[u]->ident] != (v[u].normalised * 255.0)){
			mark = 1;
			data->data.out[c[u]->ident] = v[u].normalised * 255.0;
		}
	}

	//send packet if required
	if(mark){
		sacn_transmit(inst);
	}

	return 0;
}

static int sacn_process_frame(instance* inst, sacn_frame_root* frame, sacn_frame_data* data){
	size_t u, max_mark = 0;
	channel* chan = NULL;
	channel_value val;
	sacn_instance_data* inst_data = (sacn_instance_data*) inst->impl;

	//source filtering
	if(inst_data->filter_enabled && memcmp(inst_data->cid_filter, frame->sender_cid, 16)){
		return 0;
	}

	if(data->format != 0xa1
			|| data->startcode_offset
			|| be16toh(data->address_increment) != 1){
		fprintf(stderr, "sACN framing not supported\n");
		return 1;
	}

	if(be16toh(data->channels) > 513){
		fprintf(stderr, "Invalid sACN frame channel count\n");
		return 1;
	}

	//handle source priority (currently a 1-bit counter)
	if(inst_data->data.last_priority > data->priority){
		inst_data->data.last_priority = data->priority;
		return 0;
	}
	inst_data->data.last_priority = data->priority;

	//read data (except start code), mark changed channels
	for(u = 1; u < be16toh(data->channels); u++){
		if(IS_ACTIVE(inst_data->data.map[u - 1])
				&& data->data[u] != inst_data->data.in[u - 1]){
			inst_data->data.in[u - 1] = data->data[u];
			inst_data->data.map[u - 1] |= MAP_MARK;
			max_mark = u - 1;
		}
	}

	//generate events
	for(u = 0; u <= max_mark; u++){
		if(inst_data->data.map[u] & MAP_MARK){
			//unmark and get channel
			inst_data->data.map[u] &= ~MAP_MARK;
			if(inst_data->data.map[u] & MAP_FINE){
				chan = mm_channel(inst, MAPPED_CHANNEL(inst_data->data.map[u]), 0);
			}
			else{
				chan = mm_channel(inst, u, 0);
			}

			if(!chan){
				fprintf(stderr, "Active channel %zu on %s not known to core", u, inst->name);
				return 1;
			}

			//generate value
			if(IS_WIDE(inst_data->data.map[u])){
				inst_data->data.map[MAPPED_CHANNEL(inst_data->data.map[u])] &= ~MAP_MARK;
				val.raw.u64 = inst_data->data.in[u] << ((inst_data->data.map[u] & MAP_COARSE) ? 8 : 0);
				val.raw.u64 |= inst_data->data.in[MAPPED_CHANNEL(inst_data->data.map[u])] << ((inst_data->data.map[u] & MAP_COARSE) ? 0 : 8);
				val.normalised = (double) val.raw.u64 / (double) 0xFFFF;
			}
			else{
				val.raw.u64 = inst_data->data.in[u];
				val.normalised = (double) val.raw.u64 / 255.0;
			}

			if(mm_channel_event(chan, val)){
				fprintf(stderr, "Failed to push sACN channel event to core\n");
				return 1;
			}
		}
	}
	return 0;
}

static void sacn_discovery(size_t fd){
	size_t page = 0, pages = (global_cfg.fd[fd].universes / 512) + 1, universes;
	struct sockaddr_in discovery_dest = {
		.sin_family = AF_INET,
		.sin_port = htobe16(SACN_PORT),
		.sin_addr.s_addr = htobe32(((uint32_t) 0xefff0000) | 64214)
	};

	sacn_discovery_pdu pdu = {
		.root = {
			.preamble_size = htobe16(0x10),
			.postamble_size = 0,
			.magic = { 0 }, //memcpy'd
			.flags = 0, //filled later
			.vector = htobe32(ROOT_E131_EXTENDED),
			.sender_cid = { 0 }, //memcpy'd
			.frame_flags = 0, //filled later
			.frame_vector = htobe32(FRAME_E131_DISCOVERY)
		},
		.data = {
			.source_name = "", //memcpy'd
			.flags = 0, //filled later
			.vector = htobe32(DISCOVERY_UNIVERSE_LIST),
			.page = 0, //filled later
			.max_page = pages - 1,
			.data = { 0 } //memcpy'd
		}
	};

	memcpy(pdu.root.magic, SACN_PDU_MAGIC, sizeof(pdu.root.magic));
	memcpy(pdu.root.sender_cid, global_cfg.cid, sizeof(pdu.root.sender_cid));
	memcpy(pdu.data.source_name, global_cfg.source_name, sizeof(pdu.data.source_name));

	for(; page < pages; page++){
		universes = (global_cfg.fd[fd].universes - page * 512 >= 512) ? 512 : (global_cfg.fd[fd].universes % 512);
		pdu.root.flags = htobe16(0x7000 | (104 + universes * sizeof(uint16_t)));
		pdu.root.frame_flags = htobe16(0x7000 | (82 + universes * sizeof(uint16_t)));
		pdu.data.flags = htobe16(0x7000 | (8 + universes * sizeof(uint16_t)));

		pdu.data.page = page;
		memcpy(pdu.data.data, global_cfg.fd[fd].universe + page * 512, universes * sizeof(uint16_t));

		if(sendto(global_cfg.fd[fd].fd, &pdu, sizeof(pdu) - (512 - universes) * sizeof(uint16_t), 0, (struct sockaddr*) &discovery_dest, sizeof(discovery_dest)) < 0){
			fprintf(stderr, "Failed to output sACN universe discovery frame for interface %zu: %s\n", fd, strerror(errno));
		}
	}
}

static int sacn_handle(size_t num, managed_fd* fds){
	size_t u, c;
	uint64_t timestamp = mm_timestamp();
	ssize_t bytes_read;
	char recv_buf[SACN_RECV_BUF];
	instance* inst = NULL;
	sacn_instance_id instance_id = {
		.label = 0
	};
	sacn_frame_root* frame = (sacn_frame_root*) recv_buf;
	sacn_frame_data* data = (sacn_frame_data*) (recv_buf + sizeof(sacn_frame_root));

	if(mm_timestamp() - global_cfg.last_announce > SACN_DISCOVERY_TIMEOUT){
		//send universe discovery pdu
		for(u = 0; u < global_cfg.fds; u++){
			if(global_cfg.fd[u].universes){
				sacn_discovery(u);
			}
		}
		global_cfg.last_announce = timestamp;
	}

	//check for keepalive frames
	for(u = 0; u < global_cfg.fds; u++){
		for(c = 0; c < global_cfg.fd[u].universes; c++){
			if(timestamp - global_cfg.fd[u].last_frame[c] >= SACN_KEEPALIVE_INTERVAL){
				instance_id.fields.fd_index = u;
				instance_id.fields.uni = global_cfg.fd[u].universe[c];
				inst = mm_instance_find(BACKEND_NAME, instance_id.label);
				if(inst){
					sacn_transmit(inst);
				}
			}
		}
	}

	//early exit
	if(!num){
		return 0;
	}

	for(u = 0; u < num; u++){
		do{
			bytes_read = recv(fds[u].fd, recv_buf, sizeof(recv_buf), 0);
			if(bytes_read > 0 && bytes_read > sizeof(sacn_frame_root)){
				if(!memcmp(frame->magic, SACN_PDU_MAGIC, 12)
						&& be16toh(frame->preamble_size) == 0x10
						&& frame->postamble_size == 0
						&& be32toh(frame->vector) == ROOT_E131_DATA
						&& be32toh(frame->frame_vector) == FRAME_E131_DATA
						&& data->vector == DMP_SET_PROPERTY){
					instance_id.fields.fd_index = ((uint64_t) fds[u].impl) & 0xFFFF;
					instance_id.fields.uni = be16toh(data->universe);
					inst = mm_instance_find(BACKEND_NAME, instance_id.label);
					if(inst && sacn_process_frame(inst, frame, data)){
						fprintf(stderr, "Failed to process sACN frame\n");
					}
				}
			}
		} while(bytes_read > 0);

		if(bytes_read < 0 && errno != EAGAIN){
			fprintf(stderr, "sACN failed to receive data: %s\n", strerror(errno));
		}

		if(bytes_read == 0){
			fprintf(stderr, "sACN listener closed\n");
			return 1;
		}
	}

	return 0;
}

static int sacn_start(){
	size_t n, u, p;
	int rv = 1;
	instance** inst = NULL;
	sacn_instance_data* data = NULL;
	sacn_instance_id id = {
		.label = 0
	};
	struct ip_mreq mcast_req = {
		.imr_interface = { INADDR_ANY }
	};
	struct sockaddr_in* dest_v4 = NULL;

	//fetch all instances
	if(mm_backend_instances(BACKEND_NAME, &n, &inst)){
		fprintf(stderr, "Failed to fetch instance list\n");
		return 1;
	}

	if(!n){
		free(inst);
		return 0;
	}

	if(!global_cfg.fds){
		fprintf(stderr, "Failed to start sACN backend: no descriptors bound\n");
		free(inst);
		return 1;
	}

	//update instance identifiers, join multicast groups
	for(u = 0; u < n; u++){
		data = (sacn_instance_data*) inst[u]->impl;
		id.fields.fd_index = data->fd_index;
		id.fields.uni = data->uni;
		inst[u]->ident = id.label;

		if(!data->uni){
			fprintf(stderr, "Please specify a universe on instance %s\n", inst[u]->name);
			goto bail;
		}

		//find duplicates
		for(p = 0; p < u; p++){
			if(inst[u]->ident == inst[p]->ident){
				fprintf(stderr, "Colliding sACN instances, use one: %s - %s\n", inst[u]->name, inst[p]->name);
				goto bail;
			}
		}

		if(!data->unicast_input){
			mcast_req.imr_multiaddr.s_addr = htobe32(((uint32_t) 0xefff0000) | ((uint32_t) data->uni));
			if(setsockopt(global_cfg.fd[data->fd_index].fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcast_req, sizeof(mcast_req))){
				fprintf(stderr, "Failed to join Multicast group for sACN universe %u on instance %s: %s\n", data->uni, inst[u]->name, strerror(errno));
			}
		}

		if(data->xmit_prio){
			//add to list of advertised universes for this fd
			global_cfg.fd[data->fd_index].universe = realloc(global_cfg.fd[data->fd_index].universe, (global_cfg.fd[data->fd_index].universes + 1) * sizeof(uint16_t));
			if(!global_cfg.fd[data->fd_index].universe){
				fprintf(stderr, "Failed to allocate memory\n");
				goto bail;
			}

			global_cfg.fd[data->fd_index].universe[global_cfg.fd[data->fd_index].universes] = data->uni;
			global_cfg.fd[data->fd_index].universes++;

			//generate multicast destination address if none set
			if(!data->dest_len){
				data->dest_len = sizeof(struct sockaddr_in);
				dest_v4 = (struct sockaddr_in*) (&data->dest_addr);
				dest_v4->sin_family = AF_INET;
				dest_v4->sin_port = htobe16(strtoul(SACN_PORT, NULL, 10));
				dest_v4->sin_addr = mcast_req.imr_multiaddr;
			}
		}
	}

	fprintf(stderr, "sACN backend registering %zu descriptors to core\n", global_cfg.fds);
	for(u = 0; u < global_cfg.fds; u++){
		//allocate memory for storing last frame transmission timestamp
		global_cfg.fd[u].last_frame = calloc(global_cfg.fd[u].universes, sizeof(uint64_t));
		if(!global_cfg.fd[u].last_frame){
			fprintf(stderr, "Failed to allocate memory\n");
			goto bail;
		}
		if(mm_manage_fd(global_cfg.fd[u].fd, BACKEND_NAME, 1, (void*) u)){
			goto bail;
		}
	}

	rv = 0;
bail:
	free(inst);
	return rv;
}

static int sacn_shutdown(){
	size_t n, p;
	instance** inst = NULL;

	if(mm_backend_instances(BACKEND_NAME, &n, &inst)){
		fprintf(stderr, "Failed to fetch instance list\n");
		return 1;
	}

	for(p = 0; p < n; p++){
		free(inst[p]->impl);
	}
	free(inst);

	for(p = 0; p < global_cfg.fds; p++){
		close(global_cfg.fd[p].fd);
		free(global_cfg.fd[p].universe);
		free(global_cfg.fd[p].last_frame);
	}
	free(global_cfg.fd);
	fprintf(stderr, "sACN backend shut down\n");
	return 0;
}