Browse Source

Add support for LCN.

Georgi Makedonski 10 years ago
parent
commit
e38fb3bd29
12 changed files with 309 additions and 48 deletions
  1. 1
    1
      Makefile
  2. 32
    5
      config.c
  3. 17
    6
      config.h
  4. 3
    1
      data.c
  5. 4
    1
      data.h
  6. 1
    1
      input.c
  7. 4
    1
      mptsd.c
  8. 22
    0
      mptsd_channels.conf
  9. 0
    6
      mptsd_nit.conf
  10. 50
    26
      output_psi.c
  11. 143
    0
      udp_server.c
  12. 32
    0
      udp_server.h

+ 1
- 1
Makefile View File

@@ -26,7 +26,7 @@ mptsd_OBJS =  $(FUNCS_LIB) $(TSFUNCS_LIB) \
26 26
 	sleep.o network.o \
27 27
 	input.o \
28 28
 	output_psi.o output_mix.o output_write.o \
29
-	web_pages.o web_server.o \
29
+	web_pages.o web_server.o udp_server.o\
30 30
 	mptsd.o
31 31
 
32 32
 PROGS = mptsd

+ 32
- 5
config.c View File

@@ -110,6 +110,14 @@ int config_load_channels(CONFIG *conf) {
110 110
 	// Parse channels file
111 111
 	conf->provider_name = ini_get_string_copy(ini, NULL, "Global:provider_name");
112 112
 	conf->transport_stream_id = ini_get_int(ini, 0, "Global:transport_stream_id");
113
+	conf->frequency = ini_get_string_copy(ini, NULL, "Global:frequency");
114
+	conf->modulation = ini_get_string_copy(ini, NULL, "Global:modulation");
115
+	conf->symbol_rate = ini_get_string_copy(ini, NULL, "Global:symbol_rate");
116
+
117
+//	conf->epg_source = ini_get_string(ini, NULL, "EPG:source");
118
+
119
+
120
+
113 121
 	for (i=1;i<32;i++) {
114 122
 		CHANNEL *channel = NULL;
115 123
 		int service_id = ini_get_int(ini, 0, "Channel%d:service_id", i);
@@ -117,6 +125,9 @@ int config_load_channels(CONFIG *conf) {
117 125
 			continue;
118 126
 
119 127
 		int is_radio = ini_get_bool(ini, 0, "Channel%d:radio", i);
128
+		int lcn = ini_get_int(ini, 0, "Channel%d:lcn", i);
129
+		int is_lcn_visible = ini_get_bool(ini, 0, "Channel%d:lcn_visible", i);
130
+
120 131
 
121 132
 		char *id = ini_get_string(ini, NULL, "Channel%d:id", i);
122 133
 		if (!id) {
@@ -142,7 +153,7 @@ int config_load_channels(CONFIG *conf) {
142 153
 				}
143 154
 				// Init channel
144 155
 				if (channel == NULL) {
145
-					channel = channel_new(service_id, is_radio, id, name, source);
156
+					channel = channel_new(service_id, is_radio, id, name, source, lcn, is_lcn_visible);
146 157
 				} else {
147 158
 					chansrc_add(channel, source);
148 159
 				}
@@ -222,7 +233,7 @@ int config_load_channels(CONFIG *conf) {
222 233
 		if (r->cookie != cookie) {
223 234
 			proxy_log(r, "Remove");
224 235
 			/* Replace channel reference with real object and instruct free_restreamer to free it */
225
-			r->channel = channel_new(r->channel->service_id, r->channel->radio, r->channel->id, r->channel->name, r->channel->source);
236
+			r->channel = channel_new(r->channel->service_id, r->channel->radio, r->channel->id, r->channel->name, r->channel->source, r->channel->lcn, r->channel->lcn_visible);
226 237
 			r->freechannel = 1;
227 238
 			r->dienow = 1;
228 239
 		}
@@ -243,7 +254,7 @@ int config_load_global(CONFIG *conf) {
243 254
 		LOGf("CONFIG: Error loading global config (%s)\n", conf->global_conf);
244 255
 		return 0;
245 256
 	}
246
-	conf->network_id		= ini_get_int(ini, 0,    "Global:network_id");
257
+	conf->network_id				= ini_get_int(ini, 0,    "Global:network_id");
247 258
 	conf->timeouts.pat		= ini_get_int(ini, 100,  "Timeouts:pat");
248 259
 	conf->timeouts.pmt		= ini_get_int(ini, 200,  "Timeouts:pmt");
249 260
 	conf->timeouts.sdt		= ini_get_int(ini, 500,  "Timeouts:sdt");
@@ -413,6 +424,10 @@ static void show_usage() {
413 424
 	puts("Server settings:");
414 425
 	puts("\t-b addr\t\tLocal IP address to bind.   (default: 0.0.0.0)");
415 426
 	puts("\t-p port\t\tPort to listen.             (default: 0)");
427
+	puts("UDP Server settings:");
428
+	puts("\t-s addr\t\tLocal IP address to bind.   (default: 0.0.0.0)");
429
+	puts("\t-u port\t\tPort to listen.             (default: 2000)");
430
+
416 431
 	puts("\t-d pidfile\tDaemonize with pidfile");
417 432
 	puts("\t-l host\t\tSyslog host                 (default: disabled)");
418 433
 	puts("\t-L port\t\tSyslog port                 (default: 514)");
@@ -454,8 +469,10 @@ void config_load(CONFIG *conf, int argc, char **argv) {
454 469
 	conf->logport = 514;
455 470
 	conf->server_port = 0;
456 471
 	conf->server_socket = -1;
472
+	conf->udp_server_port = 2000;
473
+	conf->udp_server_socket = -1;
457 474
 
458
-	while ((j = getopt(argc, argv, "i:b:p:g:c:n:e:d:t:o:O:P:l:L:B:m:qDHhWE")) != -1) {
475
+	while ((j = getopt(argc, argv, "i:b:p:s:u:g:c:n:e:d:t:o:O:P:l:L:B:m:qDHhWE")) != -1) {
459 476
 		switch (j) {
460 477
 			case 'i':
461 478
 				conf->ident = strdup(optarg);
@@ -473,6 +490,12 @@ void config_load(CONFIG *conf, int argc, char **argv) {
473 490
 			case 'p':
474 491
 				conf->server_port = atoi(optarg);
475 492
 				break;
493
+			case 's':
494
+				conf->udp_server_addr = strdup(optarg);
495
+				break;
496
+			case 'u':
497
+				conf->udp_server_port = atoi(optarg);
498
+				break;
476 499
 			case 'd':
477 500
 				conf->pidfile = strdup(optarg);
478 501
 				break;
@@ -582,6 +605,9 @@ void config_load(CONFIG *conf, int argc, char **argv) {
582 605
 	if (conf->server_port)
583 606
 		init_server_socket(conf->server_addr, conf->server_port, &conf->server, &conf->server_socket);
584 607
 
608
+//	if (conf->udp_server_port)
609
+//		init_server_socket_udp(conf->server_addr, conf->udp_server_port, &conf->server, &conf->udp_server_socket);
610
+
585 611
 	if (!conf->quiet) {
586 612
 		printf("Configuration:\n");
587 613
 		printf("\tServer ident      : %s\n", conf->ident);
@@ -632,7 +658,8 @@ void config_load(CONFIG *conf, int argc, char **argv) {
632 658
 	if (!config_load_channels(conf))
633 659
 		goto ERR;
634 660
 
635
-	config_load_epg(conf);
661
+	if (!config_load_epg(conf))
662
+		LOGf("CONFIG: Run without EIT (%s)\n", conf->epg_conf);
636 663
 
637 664
 	return;
638 665
 

+ 17
- 6
config.h View File

@@ -35,13 +35,20 @@ typedef struct {
35 35
 	char			*loghost;
36 36
 	int				logport;
37 37
 
38
-	struct sockaddr_in	server;
39
-	char				*server_addr;
40
-	int					server_port;
41
-	int					server_socket;
42
-	pthread_t			server_thread;
38
+	struct sockaddr_in		server;
39
+	char			*server_addr;
40
+	int				server_port;
41
+	int				server_socket;
42
+	pthread_t		server_thread;
43
+
44
+	struct sockaddr_in		udp_server;
45
+	char			*udp_server_addr;
46
+	int				udp_server_port;
47
+	int				udp_server_socket;
48
+	pthread_t		udp_server_thread;
43 49
 
44 50
 	int				multicast_ttl;
51
+
45 52
 	struct in_addr	output_intf;
46 53
 
47 54
 	char			*global_conf;
@@ -61,6 +68,10 @@ typedef struct {
61 68
 
62 69
 	uint16_t		network_id;			// For NIT && SDT
63 70
 	uint16_t		transport_stream_id;// For NIT
71
+	char			*frequency;			// For NIT
72
+	char			*modulation;		// For NIT
73
+	char			*symbol_rate;		// For NIT
74
+
64 75
 	char 			*network_name;		// For NIT
65 76
 	char 			*provider_name;		// For SDT
66 77
 
@@ -94,7 +105,7 @@ void		config_free		(CONFIG **conf);
94 105
 void		config_load		(CONFIG *conf, int argc, char **argv);
95 106
 
96 107
 int			config_load_global		(CONFIG *conf);
97
-int			config_load_channels	(CONFIG *conf);
108
+int			config_load_channels		(CONFIG *conf);
98 109
 int			config_load_nit			(CONFIG *conf);
99 110
 int			config_load_epg			(CONFIG *conf);
100 111
 

+ 3
- 1
data.c View File

@@ -121,10 +121,12 @@ void chansrc_set(CHANNEL *c, uint8_t src_id) {
121 121
 
122 122
 
123 123
 
124
-CHANNEL *channel_new(int service_id, int is_radio, char *id, char *name, char *source) {
124
+CHANNEL *channel_new(int service_id, int is_radio, char *id, char *name, char *source, int lcn, int is_lcn_visible) {
125 125
 	CHANNEL *c = calloc(1, sizeof(CHANNEL));
126 126
 	c->service_id = service_id;
127 127
 	c->radio = is_radio;
128
+	c->lcn = lcn;
129
+	c->lcn_visible = is_lcn_visible;
128 130
 	c->base_pid = service_id * 32; // The first pid is saved for PMT
129 131
 	c->pmt_pid = c->base_pid; // The first pid is saved for PMT
130 132
 	c->id = strdup(id);

+ 4
- 1
data.h View File

@@ -63,6 +63,8 @@ typedef struct {
63 63
 	int			service_id;
64 64
 	int			pmt_pid;
65 65
 	int			radio;
66
+	int			lcn;
67
+	int			lcn_visible;
66 68
 	char *		id;
67 69
 	char *		name;
68 70
 	/* Sources */
@@ -219,7 +221,7 @@ EPG_ENTRY *	epg_new			(time_t start, int duration, char *encoding, char *event,
219 221
 void		epg_free		(EPG_ENTRY **e);
220 222
 int			epg_changed		(EPG_ENTRY *a, EPG_ENTRY *b);
221 223
 
222
-CHANNEL *	channel_new		(int service_id, int is_radio, char *id, char *name, char *source);
224
+CHANNEL *	channel_new		(int service_id, int is_radio, char *id, char *name, char *source, int lcn, int is_lcn_visible);
223 225
 void		channel_free	(CHANNEL **c);
224 226
 void		channel_free_epg(CHANNEL *c);
225 227
 
@@ -246,6 +248,7 @@ void		obuf_reset			(OBUF *ob);
246 248
 NIT *		nit_new			(uint16_t ts_id, char *freq, char *modulation, char *symbol_rate);
247 249
 void		nit_free		(NIT **nit);
248 250
 
251
+
249 252
 void		proxy_log		(INPUT *r, char *msg);
250 253
 void		proxy_close		(LIST *inputs, INPUT **input);
251 254
 

+ 1
- 1
input.c View File

@@ -404,7 +404,7 @@ void * input_stream(void *self) {
404 404
 				pid = ts_packet_get_pid(ts_packet);
405 405
 				// Kill incomming NIT, SDT, EIT, RST, TDT/TOT
406 406
 				if (pid == s->nit_pid || pid == 0x10 || pid == 0x11 || pid == 0x12 || pid == 0x13 || pid == 0x14 || pid == 0x1fff) {
407
-					// LOGf("INPUT: %-10s: Remove PID %03x\n", r->channel->id, pid);
407
+				//	 LOGf("INPUT: %-10s: Remove PID %03x\n", r->channel->id, pid);
408 408
 					continue;
409 409
 				}
410 410
 

+ 4
- 1
mptsd.c View File

@@ -31,6 +31,7 @@
31 31
 #include "input.h"
32 32
 #include "output.h"
33 33
 #include "web_server.h"
34
+#include "udp_server.h"
34 35
 
35 36
 #define PROGRAM_NAME "ux-mptsd"
36 37
 
@@ -64,7 +65,7 @@ void spawn_input_threads(CONFIG *conf) {
64 65
 			INPUT *nr = input_new(c->name, c);
65 66
 			if (nr) {
66 67
 				list_add(conf->inputs, nr);
67
-//				LOGf("SPAWN : %s thread.\n", c->name);
68
+				LOGf("SPAWN : %s thread.\n", c->name);
68 69
 				if (pthread_create(&nr->thread, NULL, &input_stream, nr) == 0) {
69 70
 					spawned++;
70 71
 					pthread_detach(nr->thread);
@@ -156,6 +157,7 @@ int main(int argc, char **argv) {
156 157
 
157 158
 	daemonize(config->pidfile);
158 159
 	web_server_start(config);
160
+	udp_server_start(config);
159 161
 	log_init(config->logident, config->syslog_active, config->pidfile == NULL, config->loghost, config->logport);
160 162
 	init_signals(config);
161 163
 
@@ -169,6 +171,7 @@ int main(int argc, char **argv) {
169 171
 
170 172
 	kill_threads(config);
171 173
 	web_server_stop(config);
174
+	udp_server_stop(config);
172 175
 
173 176
 	LOGf("SHUTDOWN: Signal %d | %s %s (%s)\n", rcvsig, server_sig, server_ver, config->ident);
174 177
 	config_free(&config);

+ 22
- 0
mptsd_channels.conf View File

@@ -1,6 +1,10 @@
1 1
 [Global]
2 2
 provider_name = Unix Solutions
3 3
 transport_stream_id = 1
4
+frequency           = 0658,0000
5
+modulation          = 64-QAM
6
+symbol_rate         = 006,8750
7
+
4 8
 
5 9
 [Channel1]
6 10
 service_id	= 1
@@ -12,42 +16,56 @@ source1		= http://signal-server/stb/btv.mpg
12 16
 #source4		= udp://239.0.0.1:5000/
13 17
 #source5        = rtp://239.78.78.2:5000/
14 18
 #worktime	= 14:18-14:19
19
+lcn		= 2
20
+lcn_visible	= yes
15 21
 
16 22
 [Channel2]
17 23
 service_id	= 2
18 24
 id			= kanal1
19 25
 name		= "Kanal 1"
20 26
 source		= http://signal-server/stb/kanal1.mpg
27
+lcn		= 2
28
+lcn_visible	= yes
21 29
 
22 30
 [Channel3]
23 31
 service_id	= 3
24 32
 id			= novatv
25 33
 name		= "Nova"
26 34
 source1		= http://signal-server/stb/novatv.mpg
35
+lcn		= 3
36
+lcn_visible	= yes
27 37
 
28 38
 [Channel4]
29 39
 service_id	= 4
30 40
 id			= tv2
31 41
 name		= "TV 2"
32 42
 source1		= http://signal-server/stb/tv2.mpg
43
+lcn		= 7
44
+lcn_visible	= no
33 45
 
34 46
 [Channel5]
35 47
 service_id	= 5
36 48
 id			= ngc
37 49
 name		= "NatGeo"
38 50
 source1		= http://signal-server/stb/ngc.mpg
51
+lcn		= 72
52
+lcn_visible	= yes
39 53
 
40 54
 [Channel6]
41 55
 service_id	= 6
42 56
 id			= tv1
43 57
 name		= "TV 1"
44 58
 source1		= http://signal-server/stb/tv1.mpg
59
+lcn		= 25
60
+lcn_visible	= yes
45 61
 
46 62
 [Channel7]
47 63
 service_id	= 7
48 64
 id			= planetahit
49 65
 name		= "Planeta HIT"
50 66
 source1		= http://signal-server/stb/planetahit.mpg
67
+lcn		= 33
68
+lcn_visible	= no
51 69
 
52 70
 [Channel8]
53 71
 service_id	= 8
@@ -55,6 +73,8 @@ id			= fresh
55 73
 name		= "Radio Fresh"
56 74
 radio		= yes
57 75
 source		= http://signal-server/stb/fresh.mpg
76
+lcn		= 201
77
+lcn_visible	= no
58 78
 
59 79
 [Channel9]
60 80
 service_id	= 9
@@ -62,3 +82,5 @@ id			= bgradio
62 82
 name		= "BG Radio"
63 83
 radio		= yes
64 84
 source1		= http://signal-server/stb/bgradio.mpg
85
+lcn		= 202
86
+lcn_visible	= yes

+ 0
- 6
mptsd_nit.conf View File

@@ -1,12 +1,6 @@
1 1
 [Global]
2 2
 network_name = "Unixsol DVB-C network"
3 3
 
4
-[Transponder1]
5
-transport_stream_id = 1
6
-frequency           = 0658,0000
7
-modulation          = 64-QAM
8
-symbol_rate         = 006,8750
9
-
10 4
 [Transponder2]
11 5
 transport_stream_id = 2
12 6
 frequency           = 0666,0000

+ 50
- 26
output_psi.c View File

@@ -29,6 +29,7 @@
29 29
 
30 30
 static void output_psi_init_pat(CONFIG *conf, OUTPUT *o) {
31 31
 	LNODE *lc, *lctmp;
32
+
32 33
 	o->pat = ts_pat_alloc_init(conf->transport_stream_id);
33 34
 	list_lock(conf->channels);
34 35
 	list_for_each(conf->channels, lc, lctmp) {
@@ -44,37 +45,60 @@ static void output_psi_init_nit(CONFIG *conf, OUTPUT *o) {
44 45
 
45 46
 	ts_nit_add_network_name_descriptor(nit, conf->network_name);
46 47
 
48
+	int num;
49
+	LNODE *lc, *lctmp;
50
+
51
+	uint32_t *svc_services = malloc(conf->channels->items * sizeof(uint32_t));
52
+	uint32_t *lcn_services = malloc(conf->channels->items * sizeof(uint32_t));
53
+
54
+	num = 0;
55
+
56
+	list_lock(conf->channels);
57
+
58
+	list_for_each(conf->channels, lc, lctmp) {
59
+			CHANNEL *c = lc->data;
60
+			uint32_t srv = 0;
61
+			srv  = (c->service_id &~ 0x00ff) << 24;
62
+			srv |= (c->service_id &~ 0xff00) << 16;
63
+			srv |= (c->lcn_visible ? 0x01 : 0x00 &~ 0xf0 ) << 15;
64
+			srv |= (0X00 &~ 0xf0 ) << 14;
65
+			srv |= (c->lcn &~ 0xc0ff);
66
+			srv |= (c->lcn &~ 0xff00);
67
+			lcn_services[num++] = srv;
68
+	}
69
+
70
+	list_unlock(conf->channels);
71
+
72
+	num = 0;
73
+
74
+	list_lock(conf->channels);
75
+    list_for_each(conf->channels, lc, lctmp) {
76
+             CHANNEL *c = lc->data;
77
+             uint32_t srv = 0;
78
+             srv  = (c->service_id &~ 0x00ff) << 16;
79
+             srv |= (c->service_id &~ 0xff00) << 8;
80
+             srv |= c->radio ? 0x02 : 0x01;
81
+             svc_services[num++] = srv;
82
+    }
83
+
84
+	list_unlock(conf->channels);
85
+
86
+    NIT *ts_ndata = nit_new(conf->transport_stream_id, conf->frequency, conf->modulation, conf->symbol_rate);
87
+
88
+	ts_nit_add_stream_descriptors(nit, ts_ndata->ts_id, conf->network_id, ts_ndata->_freq, ts_ndata->_modulation, ts_ndata->_symbol_rate, lcn_services, svc_services, num);
89
+
47 90
 	if (conf->nit->items < 64) {
48
-		int num;
49
-		LNODE *lc, *lctmp;
50
-		uint32_t *freqs = malloc(conf->nit->items * sizeof(uint32_t));
51
-		uint32_t *services = malloc(conf->channels->items * sizeof(uint32_t));
52
-		num = 0;
53 91
 		list_lock(conf->nit);
54
-		list_for_each(conf->nit, lc, lctmp) {
55
-			NIT *ndata = lc->data;
56
-			freqs[num++] = ndata->_freq;
57
-		}
58
-		ts_nit_add_frequency_list_descriptor_cable(nit, conf->transport_stream_id, conf->network_id, freqs, num);
59
-		list_for_each(conf->nit, lc, lctmp) {
92
+			list_for_each(conf->nit, lc, lctmp) {
60 93
 			NIT *ndata = lc->data;
61 94
 			ts_nit_add_cable_delivery_descriptor(nit, ndata->ts_id, conf->network_id, ndata->_freq, ndata->_modulation, ndata->_symbol_rate);
62 95
 		}
96
+
63 97
 		list_unlock(conf->nit);
64
-		num = 0;
65
-		list_lock(conf->channels);
66
-		list_for_each(conf->channels, lc, lctmp) {
67
-			CHANNEL *c = lc->data;
68
-			uint32_t srv = 0;
69
-			srv  = (c->service_id &~ 0x00ff) << 16;
70
-			srv |= (c->service_id &~ 0xff00) << 8;
71
-			srv |= c->radio ? 0x02 : 0x01;
72
-			services[num++] = srv;
73
-		}
74
-		list_unlock(conf->channels);
75
-		ts_nit_add_service_list_descriptor(nit, conf->transport_stream_id, conf->network_id, services, num);
76
-		free(freqs);
77
-		free(services);
98
+
99
+		free(lcn_services);
100
+		free(svc_services);
101
+
78 102
 	} else {
79 103
 		LOG("CONF  : Too much items in the NIT, maximum is 64! NIT not generated.\n");
80 104
 	}
@@ -192,7 +216,7 @@ static void __output_add_eit(OUTPUT *o, struct ts_eit *eit) {
192 216
 		o->pid_eit_cont += eit->section_header->num_packets;
193 217
 		cbuf_fill(o->psibuf, eit->section_header->packet_data, eit->section_header->num_packets * TS_PACKET_SIZE);
194 218
 	}
195
-//	ts_eit_dump(eit);
219
+	ts_eit_dump(eit);
196 220
 }
197 221
 
198 222
 static void output_add_eit(CONFIG *conf, OUTPUT *o) {

+ 143
- 0
udp_server.c View File

@@ -0,0 +1,143 @@
1
+/*
2
+ * mptsd internal web server
3
+ * Copyright (C) 2010-2011 Unix Solutions Ltd.
4
+ *
5
+ * This program is free software; you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License version 2
7
+ * as published by the Free Software Foundation.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+ * GNU General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17
+ */
18
+#include <stdlib.h>
19
+#include <regex.h>
20
+#include <errno.h>
21
+#include <string.h>
22
+#include <signal.h>
23
+#include <arpa/inet.h>
24
+#include <netinet/in.h>
25
+
26
+#include "libfuncs/libfuncs.h"
27
+
28
+#include "libfuncs/io.h"
29
+#include "libfuncs/log.h"
30
+
31
+#include "libtsfuncs/tsfuncs.h"
32
+
33
+#include "data.h"
34
+#include "config.h"
35
+#include "network.h"
36
+
37
+#include "udp_server.h"
38
+
39
+extern int keep_going;
40
+
41
+#define NEXT_CLIENT { FREE(path); FREE(buf); pthread_exit(0); }
42
+#define SHUTDOWN_CLIENT { FREE(path); FREE(buf); shutdown_fd(&clientsock); pthread_exit(0); }
43
+#define BUFSIZE 1316
44
+
45
+void *udp_server_thread(void *data) {
46
+	CONFIG *conf = data;
47
+
48
+	struct sockaddr_in client;					/* remote address */
49
+	socklen_t clientlen = sizeof(client);		/* length of addresses */
50
+	int clientsock; 							/* #bytes received */
51
+	char buf[FRAME_PACKET_SIZE];				/* receive buffer */
52
+
53
+	while (keep_going) {
54
+
55
+
56
+		clientsock = recvfrom(conf->udp_server_socket,(char*)buf, FRAME_PACKET_SIZE, 0, (struct sockaddr *)&client, &clientlen);
57
+
58
+		if (clientsock < 0) {
59
+			if (conf->udp_server_socket > -1)	// The server_socket is closed on exit, so do not report errors
60
+				LOGf("ERROR : Failed to accept client fd: %i err: %s\n", clientsock, strerror(errno));
61
+			if (errno==EMFILE || errno==ENFILE)
62
+				break;
63
+		} else {
64
+
65
+		//	LOGf("received %d bytes from %s with port %d \n", clientsock, inet_ntoa(client.sin_addr), ntohs( client.sin_port ) );
66
+
67
+		// TODO  Process buffer and add to output_mix
68
+
69
+		}
70
+
71
+	}
72
+
73
+	pthread_exit(0);
74
+
75
+
76
+}
77
+
78
+void udp_server_start(CONFIG *conf) {
79
+	if (conf->udp_server_port)
80
+		udp_server_init(conf->udp_server_addr, conf->udp_server_port, &conf->udp_server, &conf->udp_server_socket);
81
+
82
+	if (conf->udp_server_socket > -1) {
83
+		LOG("UDP_SERVER: Started \n");
84
+		pthread_create(&conf->udp_server_thread, NULL, &udp_server_thread, conf);
85
+		}
86
+}
87
+
88
+void udp_server_stop(CONFIG *conf) {
89
+	if (conf->udp_server_socket > -1) {
90
+		shutdown_fd(&conf->udp_server_socket);
91
+		pthread_join(conf->udp_server_thread, NULL);
92
+	}
93
+}
94
+
95
+
96
+void udp_server_init(char *bind_addr, int bind_port, struct sockaddr_in *server, int *server_socket) {
97
+
98
+	LOG("UDP_SERVER: Init.\n");
99
+
100
+	char *binded;
101
+
102
+	struct hostent *host_ptr;
103
+	*server_socket = socket(PF_INET, SOCK_DGRAM, 0);
104
+
105
+	if (*server_socket == -1) {
106
+		perror("socket(server_socket)");
107
+		exit(1);
108
+	}
109
+
110
+	int j = 1;
111
+	if (setsockopt(*server_socket, SOL_SOCKET, SO_REUSEADDR,(const char *) &j, sizeof(j))<0) {
112
+		perror("setsockopt(SO_REUSEADDR)");
113
+		exit(1);
114
+	}
115
+
116
+	memset(server, 0, sizeof(struct sockaddr_in));
117
+	if (!bind_addr) {
118
+		binded = "*";
119
+		server->sin_addr.s_addr = htonl(INADDR_ANY);
120
+	} else {
121
+		host_ptr = gethostbyname(bind_addr);
122
+		if (!host_ptr) {
123
+			fprintf(stderr,"Error can't resolve bind address: %s\n", bind_addr);
124
+			exit(1);
125
+		}
126
+		memcpy(&server->sin_addr, host_ptr->h_addr, sizeof(server->sin_addr));
127
+		binded = inet_ntoa(server->sin_addr);
128
+	}
129
+
130
+	/* Bind to server socket */
131
+	LOGf ("UDP_SERVER: Bind to : %s:%i\n", binded, bind_port);
132
+
133
+	server->sin_family = AF_INET;
134
+	server->sin_port   = htons(bind_port);
135
+
136
+	if (bind(*server_socket, (struct sockaddr *)server, sizeof(struct sockaddr_in)) < 0) {
137
+		perror("bind(server_socket)");
138
+		exit(1);
139
+	}
140
+
141
+	LOG("UDP_SERVER: UDP server Initialized.\n");
142
+
143
+}

+ 32
- 0
udp_server.h View File

@@ -0,0 +1,32 @@
1
+/*
2
+ * mptsd internal web server header file
3
+ * Copyright (C) 2010-2011 Unix Solutions Ltd.
4
+ *
5
+ * This program is free software; you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License version 2
7
+ * as published by the Free Software Foundation.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+ * GNU General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17
+ */
18
+#ifndef UDP_SERVER_H
19
+#define UDP_SERVER_H
20
+
21
+#include "config.h"
22
+
23
+void udp_server_start(CONFIG *conf);
24
+void udp_server_init(char *bind_addr, int bind_port, struct sockaddr_in *server, int *server_socket);
25
+void udp_server_stop(CONFIG *conf);
26
+
27
+
28
+
29
+
30
+
31
+
32
+#endif

Loading…
Cancel
Save