forked from fsphil/tsmerge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpush.c
390 lines (327 loc) · 9.18 KB
/
push.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
/* tspush - Relay TS data to tsmerger */
/*=======================================================================*/
/* Copyright (C)2016 Philip Heron <[email protected]> */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <signal.h>
#include "ts/ts.h"
typedef enum {
MODE_MX,
MODE_TS,
} _mode_t;
typedef struct {
/* Input file descriptor */
FILE *input_fd;
/* Output UDP Socket */
int output_socket;
/* Output Mode (MX/TS) */
_mode_t output_mode;
/* Output packet counter */
uint32_t output_counter;
/* TS Packet Data buffer */
uint8_t data[0x10 + TS_PACKET_SIZE];
} _push_t;
_push_t tspush;
static int _open_socket(char *host, char *port, int ai_family)
{
int r;
int sock;
struct addrinfo hints;
struct addrinfo *re, *rp;
char s[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = ai_family;
hints.ai_socktype = SOCK_DGRAM;
r = getaddrinfo(host, port, &hints, &re);
if(r != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(r));
return(-1);
}
/* Try IPv6 first */
for(sock = -1, rp = re; sock == -1 && rp != NULL; rp = rp->ai_next)
{
if(rp->ai_addr->sa_family != AF_INET6) continue;
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) rp->ai_addr)->sin6_addr), s, INET6_ADDRSTRLEN);
printf("Sending to [%s]:%d\n", s, ntohs((((struct sockaddr_in6 *) rp->ai_addr)->sin6_port)));
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sock == -1)
{
perror("socket");
continue;
}
if(connect(sock, rp->ai_addr, rp->ai_addrlen) == -1)
{
perror("connect");
close(sock);
sock = -1;
}
}
/* Try IPv4 next */
for(rp = re; sock == -1 && rp != NULL; rp = rp->ai_next)
{
if(rp->ai_addr->sa_family != AF_INET) continue;
inet_ntop(AF_INET, &(((struct sockaddr_in *) rp->ai_addr)->sin_addr), s, INET6_ADDRSTRLEN);
printf("Sending to %s:%d\n", s, ntohs((((struct sockaddr_in *) rp->ai_addr)->sin_port)));
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sock == -1)
{
perror("socket");
continue;
}
if(connect(sock, rp->ai_addr, rp->ai_addrlen) == -1)
{
perror("connect");
close(sock);
sock = -1;
}
}
freeaddrinfo(re);
return(sock);
}
void _print_usage(void)
{
printf(
"\n"
"Usage: tspush [options] INPUT\n"
"\n"
" -h, --host <name> Set the hostname to send data to. Default: localhost\n"
" -p, --port <number> Set the port number to send data to. Default: 5678\n"
" -4, --ipv4 Force IPv4 only.\n"
" -6, --ipv6 Force IPv6 only.\n"
" -m, --mode <ts|mx> Send raw TS packets, or MX packets for TS merger.\n"
" Default: mx\n"
" -c, --callsign <id> Set the station callsign, up to 10 characters.\n"
" Required for mx mode. Not used by ts mode.\n"
" -b, --bitrate <number> Set the desired output bitrate (accurate for 204-byte input only)\n"
"\n"
);
}
static uint8_t _send_packet(void)
{
int c;
ts_header_t ts;
if(fread(&tspush.data[0x10], 1, TS_PACKET_SIZE, tspush.input_fd) == TS_PACKET_SIZE)
{
if(tspush.data[0x10] != TS_HEADER_SYNC)
{
/* Re-align input to the TS sync byte */
uint8_t *p = memchr(&tspush.data[0x10], TS_HEADER_SYNC, TS_PACKET_SIZE);
if(p == NULL) return 1;
c = p - &tspush.data[0x10];
memmove(&tspush.data[0x10], p, TS_PACKET_SIZE - c);
if(fread(&tspush.data[0x10 + TS_PACKET_SIZE - c], 1, c, tspush.input_fd) != (size_t)c)
{
return 0;
}
}
if(ts_parse_header(&ts, &tspush.data[0x10]) != TS_OK)
{
/* Don't transmit packets with invalid headers */
printf("TS_INVALID\n");
return 1;
}
/* We don't transmit NULL/padding packets */
if(ts.pid == TS_NULL_PID) return 1;
/* Counter (4 bytes little-endian) */
tspush.data[0x05] = (tspush.output_counter & 0xFF000000) >> 24;
tspush.data[0x04] = (tspush.output_counter & 0x00FF0000) >> 16;
tspush.data[0x03] = (tspush.output_counter & 0x0000FF00) >> 8;
tspush.data[0x02] = (tspush.output_counter & 0x000000FF) >> 0;
if(tspush.output_mode == MODE_MX)
{
/* Send the full MX packet */
send(tspush.output_socket, tspush.data, sizeof(tspush.data), 0);
}
else if(tspush.output_mode == MODE_TS)
{
/* Send just the TS packet */
send(tspush.output_socket, &tspush.data[0x10], TS_PACKET_SIZE, 0);
}
tspush.output_counter++;
return 1;
}
else
{
return 0;
}
}
static void _handle_alarm(int sig)
{
(void)sig;
/* Send 1 data packet */
if(_send_packet())
{
/* Successful, se re-enable alarm handler */
signal(SIGALRM, _handle_alarm);
}
else
{
/* Failed, likely EOF, so close sockets */
if(tspush.input_fd != stdin)
{
fclose(tspush.input_fd);
}
close(tspush.output_socket);
/* Main function will trigger on sock !> 0 and terminate */
tspush.output_socket = 0;
}
}
int main(int argc, char *argv[])
{
int opt;
int c;
char *host = "localhost";
char *port = "5678";
char *callsign = NULL;
int ai_family = AF_UNSPEC;
int bitrate = 0;
int packet_delay_us;
tspush.input_fd = stdin;
tspush.output_mode = MODE_MX;
tspush.output_counter = 0;
static const struct option long_options[] = {
{ "host", required_argument, 0, 'h' },
{ "port", required_argument, 0, 'p' },
{ "ipv6", no_argument, 0, '6' },
{ "ipv4", no_argument, 0, '4' },
{ "callsign", required_argument, 0, 'c' },
{ "mode", required_argument, 0, 'm' },
{ "bitrate", required_argument, 0, 'b' },
{ 0, 0, 0, 0 }
};
opterr = 0;
while((c = getopt_long(argc, argv, "h:p:64c:b:", long_options, &opt)) != -1)
{
switch(c)
{
case 'h': /* --host <name> */
host = optarg;
break;
case 'p': /* --port <number> */
port = optarg;
break;
case '6': /* --ipv6 */
ai_family = AF_INET6;
break;
case '4': /* --ipv4 */
ai_family = AF_INET;
break;
case 'm': /* --mode <ts|mx> */
if(strcmp(optarg, "ts") == 0)
{
tspush.output_mode = MODE_TS;
}
else if(strcmp(optarg, "mx") == 0)
{
tspush.output_mode = MODE_MX;
}
else
{
printf("Error: Unrecognised mode '%s'\n", optarg);
_print_usage();
return(-1);
}
break;
case 'c': /* --callsign <id> */
callsign = optarg;
break;
case 'b': /* --bitrate <number> */
bitrate = atoi(optarg);
break;
case '?':
_print_usage();
return(0);
}
}
/* If output mode is specified */
if(tspush.output_mode == MODE_MX)
{
if(callsign == NULL)
{
printf("Error: A callsign is required in mx mode\n");
_print_usage();
return(-1);
}
else if(strlen(callsign) > 10)
{
printf("Error: Callsign cannot be longer than 10 characters\n");
_print_usage();
return(-1);
}
}
/* If input file is specified */
if(argc - optind == 1)
{
tspush.input_fd = fopen(argv[optind], "rb");
if(!tspush.input_fd)
{
perror("fopen");
return(-1);
}
}
else if(argc - optind > 1)
{
printf("Error: More than one input file specified\n");
_print_usage();
return(0);
}
/* Open the outgoing socket */
tspush.output_socket = _open_socket(host, port, ai_family);
if(tspush.output_socket == -1)
{
printf("Failed to resolve %s\n", host);
return(-1);
}
/* Initialise the header */
memset(tspush.data, 0, sizeof(tspush.data));
/* Packet ID / type */
tspush.data[0x00] = 0xA1;
tspush.data[0x01] = 0x55;
/* Station ID (10 bytes, UTF-8) */
if(callsign != NULL)
{
strncpy((char *) &tspush.data[0x06], callsign, 10);
}
if(bitrate == 0)
{
/* Stream the data until fails, likely due to EOF */
while(_send_packet()) {};
/* then close sockets */
if(tspush.input_fd != stdin)
{
fclose(tspush.input_fd);
}
close(tspush.output_socket);
}
else
{
/* Assume 204-byte input packets */
packet_delay_us = 1000000 / (bitrate/(TS_PACKET_SIZE*8));
/* On SIGALRM, run _send_packet() */
signal(SIGALRM, _handle_alarm);
/* Send a SIGALRM after packet_delay_us microseconds, and
* at intervals of every packet_delay_us microseconds thereafter */
ualarm (packet_delay_us, packet_delay_us);
/* Loop main function until output socket is closed by signal handler */
while(tspush.output_socket > 0)
{
sleep(10);
}
}
return(0);
}