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
|
#include "dvbipstream.h"
#include "iface.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifndef WIN32
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#ifndef APPLE
#define _LINUX_IN_H
#include <linux/mroute.h>
#endif
#else
#include <direct.h>
#ifdef __MINGW32__
#include <getopt.h>
#endif
#endif
#include "streamer.h"
#define PORT_MAX 6
#ifndef WIN32
// Local function Prototypes
static void signalHandler(int);
// Global vars...
static int sighandled = 0;
#define GOT_SIGINT 0x01
#define GOT_SIGHUP 0x02
#define GOT_SIGTERM 0x04
#endif
channel_t *channels=NULL;
int channel_num=0;
int channel_max_num=0;
int channel_use_eit = 0;
int channel_use_sdt = 0;
int portnum = 12345;
int table = 0;
int quiet = 0;
/*-------------------------------------------------------------------------*/
channel_t * read_channel_list(char *filename, char *dirname)
{
FILE *cf;
FILE *pf;
char buf[512];
cf=fopen(filename,"r");
if (!cf) {
printf("Can't read %s: %s\n",filename,strerror(errno));
return NULL;
}
if (dirname) {
#ifdef WIN32
if (((dirname[0]>='A'&&dirname[0]<='Z') ||
(dirname[0]>='a'&&dirname[0]<='z')) && dirname[1]==':') {
if (_chdrive((dirname[0]&0xdf)-'A'+1)) {
printf("Can't access %s: %s\n", dirname,
strerror(errno));
fclose(cf);
return NULL;
}
}
#endif
if (chdir(dirname)) {
printf("Can't access %s: %s\n", dirname,
strerror(errno));
fclose(cf);
return NULL;
}
}
pf =fopen("channels.m3u", "w");
if (!pf) {
printf("Can't create %s: %s\n", "channels.m3u",
strerror(errno));
fclose(cf);
return NULL;
}
fprintf(pf, "#EXTM3U\n");
while(fgets(buf,sizeof(buf),cf)) {
if (channel_num==channel_max_num) {
channel_max_num+=200;
channels=(channel_t*)realloc(channels,
channel_max_num*sizeof(channel_t));
if (!channels) {
printf("out of memory\n");
fclose(pf);
fclose(cf);
return NULL;
}
}
if (ParseLine(buf,&channels[channel_num])) {
int ip1 = (channel_num+1)/256;
int ip2 = (channel_num) - (ip1*256) + 1;
if (!quiet)
printf("%i: udp://@239.255.%i.%i:%i - %s \n",
channel_num+1, ip1, ip2, portnum,
channels[channel_num].name);
fprintf(pf, "#EXTINF: %i,%s\n", channel_num+1,
channels[channel_num].name);
fprintf(pf, "udp://@239.255.%i.%i:%i\n", ip1,ip2,
portnum);
if (channel_use_eit)
{
channels[channel_num].NumEitpids = 1;
channels[channel_num].eitpids[0] = 0x12;
}
if (channel_use_sdt)
{
channels[channel_num].NumSdtpids = 1;
channels[channel_num].sdtpids[0] = 0x11;
}
channel_num++;
}
}
printf("Read %i channels, M3U playlist file \"channels.m3u\" "
"generated.\n",channel_num);
fclose(pf);
fclose(cf);
return channels;
}
/*-------------------------------------------------------------------------*/
int get_channel_num(void)
{
return channel_num;
}
/*-------------------------------------------------------------------------*/
int get_channel_name(int n, char *str, int maxlen)
{
#ifndef WIN32
snprintf(str,maxlen,"%04i_%s.ts",n+1,channels[n].name);
#else
sprintf(str,"%04i_%s.ts",n+1,channels[n].name);
#endif
while(*str) {
char c=*str;
if (c=='/' || c=='\\' || c==':' || c=='?' || c=='*' ||
!isprint(c))
*str='_';
str++;
}
return 0;
}
/*-------------------------------------------------------------------------*/
channel_t *get_channel_data(int n)
{
return &channels[n];
}
extern cmdline_t cmd;
void usage (void)
{
printf("Usage: netcv2dvbip "
"[-b <multicast interface>] "
"[-p <port>] "
"[-i <netceiver interface>] "
"[-c <channels.conf>] "
"[-e activate EIT PID (EPG)] "
"[-s activate SDT PID (may be required for EPG)] "
#ifdef MRT_TABLE
"[-t <routing table number> (requires "
"CONFIG_IP_MROUTE_MULTIPLE_TABLES)] "
#endif
"[-q be more quiet on the screen] "
"[-o <output directory>]\n");
exit(1);
}
/*-------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
char c;
char *dirname = NULL;
char channels[_POSIX_PATH_MAX];
strcpy(channels, "channels.conf");
char bindiface[IFNAMSIZ];
iface_t iflist[MAXIF];
#ifndef WIN32
uid_t uid;
struct sigaction sa;
uid = getuid();
if(uid != 0)
{
printf("You must be root! Current uid: %d.\n",uid);
return(1);
}
sa.sa_handler = signalHandler;
sa.sa_flags = 0; /* Interrupt system calls */
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
#else
bool IsAdmin = false;
IsUserAdmin(&IsAdmin);
if( IsAdmin == false)
{
printf("You must have administrative privileges!\n");
return(1);
}
#endif
memset(bindiface, 0, sizeof(bindiface));
while(1)
{
int ret = getopt(argc,argv, "i:hc:b:p:esqo:"
#ifdef MRT_TABLE
"t:"
#endif
);
if (ret==-1)
break;
c=(char)ret;
switch (c) {
case 'i':
strncpy(cmd.iface, optarg, IFNAMSIZ-1);
cmd.iface[IFNAMSIZ-1]=0;
break;
case 'c':
strncpy(channels, optarg, _POSIX_PATH_MAX-1);
channels[_POSIX_PATH_MAX-1]=0;
break;
case 'b':
strncpy(bindiface, optarg, IFNAMSIZ-1);
bindiface[IFNAMSIZ-1] = 0;
break;
case 'p':
portnum = atoi(optarg);
break;
#ifdef MRT_TABLE
case 't':
table = atoi(optarg);
break;
#endif
case 'h':
usage();
return(0);
case 'e':
channel_use_eit = 1;
break;
case 's':
channel_use_sdt = 1;
break;
case 'q':
quiet = 1;
break;
case 'o':
dirname = optarg;
break;
}
}
memset( iflist, 0, sizeof(iflist));
int num_ifaces=discover_interfaces( (iface_t*) &iflist );
if ( !num_ifaces )
exit(-1);
int ifindex = 0;
if ( strlen(bindiface) > 0 )
{
int found = false;
while ( ifindex < num_ifaces )
{
if (!strncmp(iflist[ifindex].name, bindiface, IFNAMSIZ))
{
found = true;
break;
}
ifindex++;
}
if (!found)
{
printf("Cannot find interface %s. Exiting...\n",
bindiface);
exit(-1);
}
}
printf("Starting netcv2dvbip. Streams will be sent to port: %d\n",
portnum);
if (!read_channel_list(channels, dirname))
exit(-1);
mcli_startup();
#ifdef WIN32
if ( !IsVistaOrHigher() )
{
printf("Windows version does not support MLDv2, enabling "
"internal MLDv2 reporter.\n");
mld_client_init(cmd.iface);
}
#endif
printf("\n");
#ifdef WIN32
WSADATA ws;
WSAStartup(MAKEWORD (2, 2),&ws);
#endif
cStreamer streamer;
struct sockaddr_in listenaddr;
listenaddr.sin_addr.s_addr = iflist[ifindex].ipaddr;
printf("Listening on interface %s (%s)\n\n\n", iflist[ifindex].name,
inet_ntoa(listenaddr.sin_addr));
streamer.SetBindIf(iflist[ifindex]);
streamer.SetStreamPort(portnum);
streamer.SetTable(table);
streamer.SetNumGroups(get_channel_num());
streamer.Run();
#ifdef WIN32
printf("\nPlease press enter to stop.\n\n\n");
getchar();
printf("Stopping...please wait...\n");
#else
while (1)
{
// Process signaling...
if (sighandled) {
if (sighandled & GOT_SIGINT) {
sighandled &= ~GOT_SIGINT;
printf("\nGot SIGINT signal. Exiting.\n");
break;
}
if (sighandled & GOT_SIGTERM) {
sighandled &= ~GOT_SIGTERM;
printf("Got SIGTERM signal. Exiting.\n");
break;
}
}
usleep(10000);
}
#endif
streamer.Stop();
#ifdef WIN32
if ( !IsVistaOrHigher() )
{
mld_client_exit();
}
#endif
printf("netcv2dvbip stopped.\n");
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
/*-------------------------------------------------------------------------*/
#ifndef WIN32
/*
* Signal handler. Take note of the fact that the signal arrived
* so that the main loop can take care of it.
*/
static void signalHandler(int sig) {
switch (sig) {
case SIGINT:
sighandled |= GOT_SIGINT;
break;
case SIGTERM:
sighandled |= GOT_SIGTERM;
break;
/* XXX: Not in use.
case SIGHUP:
sighandled |= GOT_SIGHUP;
break;
*/
}
}
#endif
|