This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvpserver.c
359 lines (292 loc) · 8.68 KB
/
mvpserver.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
/*
Copyright 2004-2019 Chris Tallon
This file is part of VOMP.
VOMP 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 2 of the License, or
(at your option) any later version.
VOMP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VOMP. If not, see <https://www.gnu.org/licenses/>.
*/
#include "mvpserver.h"
#ifdef VOMPSTANDALONE
#include <iostream>
#endif
extern pthread_mutex_t threadClientMutex;
#define UDP6PORT 51056
MVPServer::MVPServer()
{
// MH in case anbody has a better position :-)
pthread_mutex_init(&threadClientMutex, NULL);
tcpServerPort = 0;
logoDir = NULL;
resourceDir = NULL;
imageDir = NULL;
cacheDir = NULL;
}
MVPServer::~MVPServer()
{
if (logoDir) delete[] logoDir;
if (resourceDir) delete[] resourceDir;
if (imageDir) delete[] imageDir;
if (cacheDir) delete[] cacheDir;
stop();
}
int MVPServer::stop()
{
if (threadIsActive()) threadCancel();
close(listeningSocket);
udpr.shutdown();
udpr6.shutdown();
bootpd.shutdown();
tftpd.shutdown();
mvprelay.shutdown();
log.log("Main", Log::INFO, "Stopped main server thread");
log.shutdown();
config.shutdown();
return 1;
}
int MVPServer::run(char* tconfigDir)
{
if (threadIsActive()) return 1;
configDir = tconfigDir;
// Start config
#ifdef VOMPSTANDALONE
#define dsyslog(x) std::cout << x << std::endl;
#endif
char configFileName[PATH_MAX];
snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
if (config.init(configFileName))
{
dsyslog("VOMP: Config file found");
}
else
{
dsyslog("VOMP: Config file not found");
}
// Start logging
char* cfgLogFilename = config.getValueString("General", "Log file");
if (cfgLogFilename)
{
log.init(Log::DEBUG, cfgLogFilename);
delete[] cfgLogFilename;
log.log("Main", Log::INFO, "Logging started");
}
else
{
dsyslog("VOMP: Logging disabled");
}
const char *bigresdir = cPlugin::ResourceDirectory();
const char *bigcachedir = cPlugin::CacheDirectory();
// get logo directory
logoDir = config.getValueString("General", "Channel logo directory");
if (logoDir)
{
log.log("Main", Log::INFO, "LogoDir set %s", logoDir);
} else {
if (bigresdir) {
logoDir = new char[strlen(bigresdir)+1+7];
sprintf(logoDir,"%s/logos/",bigresdir);
log.log("Main", Log::INFO, "No LogoDir set, default %s",logoDir);
} else {
log.log("Main", Log::INFO, "No LogoDir set, no res dir");
}
}
// get epg Image directory
imageDir = config.getValueString("General", "Epg image directory");
if (imageDir)
{
log.log("Main", Log::INFO, "ImageDir set %s", imageDir);
} else {
if (bigcachedir) {
imageDir = new char[strlen(bigcachedir)+1+11+3];
sprintf(imageDir,"%s/../epgimages/",bigcachedir);
log.log("Main", Log::INFO, "No ImageDir set, default %s",imageDir);
} else {
log.log("Main", Log::INFO, "No ImageDir set, no cache dir");
}
}
if (bigresdir) {
resourceDir = new char[strlen(bigresdir)+1];
strcpy(resourceDir,bigresdir);
log.log("Main", Log::INFO, "Resource directory is %s",bigresdir);
} else {
log.log("Main", Log::INFO, "Resource directory is not set");
}
if (bigcachedir) {
cacheDir = new char[strlen(bigcachedir)+1];
strcpy(cacheDir,bigcachedir);
log.log("Main", Log::INFO, "Cache directory is %s",bigcachedir);
} else {
log.log("Main", Log::INFO, "Cache directory is not set");
}
// Get UDP port number for discovery service
int fail = 1;
int udpport = config.getValueLong("General", "UDP port", &fail);
if (fail) udpport = 51051;
// Work out a name for this server
char* serverName;
// Try to get from vomp.conf
serverName = config.getValueString("General", "Server name");
if (!serverName) // If not, get the hostname
{
serverName = new char[1024];
if (gethostname(serverName, 1024)) // if not, set default
{
strcpy(serverName, "VOMP Server");
}
}
// Get VOMP server TCP port to give to UDP replier to put in packets
fail = 1;
tcpServerPort = config.getValueLong("General", "TCP port", &fail);
if (fail) tcpServerPort = 3024;
int udpSuccess = udpr.run(udpport, serverName, tcpServerPort);
int udp6Success = udpr6.run(UDP6PORT, serverName, tcpServerPort);
delete[] serverName;
if (!udpSuccess)
{
log.log("Main", Log::CRIT, "Could not start UDP replier");
stop();
return 0;
}
if (!udp6Success)
{
log.log("Main", Log::CRIT, "Could not start UDP6 replier");
stop();
return 0;
}
// Read config and start bootp and tftp as appropriate
char* configString;
int bootpEnabled = 0;
int tftpEnabled = 0;
int mvprelayEnabled = 1;
configString = config.getValueString("General", "Bootp server enabled");
if (configString && (!strcasecmp(configString, "yes"))) bootpEnabled = 1;
if (configString) delete[] configString;
configString = config.getValueString("General", "TFTP server enabled");
if (configString && (!strcasecmp(configString, "yes"))) tftpEnabled = 1;
if (configString) delete[] configString;
configString = config.getValueString("General", "MVPRelay enabled");
if (configString && (strcasecmp(configString, "yes"))) mvprelayEnabled = 0;
if (configString) delete[] configString;
if (bootpEnabled)
{
if (!bootpd.run(configDir))
{
log.log("Main", Log::CRIT, "Could not start Bootpd");
stop();
return 0;
}
}
else
{
log.log("Main", Log::INFO, "Not starting Bootpd");
}
if (tftpEnabled)
{
char tftpPath[PATH_MAX];
configString = config.getValueString("General", "TFTP directory");
if (configString)
{
snprintf(tftpPath, PATH_MAX, "%s", configString);
// this will never happen.. surely.
if ((strlen(tftpPath) + 2) >= PATH_MAX)
{
delete[] configString;
log.log("Main", Log::CRIT, "Could not understand TFTP directory from config");
stop();
return 0;
}
// if there isn't a / at the end of the dir, add one
if (tftpPath[strlen(tftpPath) - 1] != '/') strcat(tftpPath, "/");
delete[] configString;
}
else
{
snprintf(tftpPath, PATH_MAX, "%s/", configDir);
}
log.log("Main", Log::INFO, "TFTP path '%s'", tftpPath);
if (!tftpd.run(tftpPath))
{
log.log("Main", Log::CRIT, "Could not start TFTPd");
stop();
return 0;
}
}
else
{
log.log("Main", Log::INFO, "Not starting TFTPd");
}
// Start mvprelay thread
if (mvprelayEnabled)
{
if (!mvprelay.run())
{
log.log("Main", Log::CRIT, "Could not start MVPRelay");
stop();
return 0;
}
else
{
log.log("Main", Log::INFO, "MVPRelay started");
}
}
else
{
log.log("Main", Log::INFO, "Not starting MVPRelay");
}
// start thread here
if (!threadStart())
{
log.log("Main", Log::CRIT, "Could not start MVPServer thread");
stop();
return 0;
}
log.log("Main", Log::DEBUG, "MVPServer run success");
return 1;
}
void MVPServer::threadMethod()
{
// I want to die as soon as I am cancelled because I'll be in accept()
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
/*
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(tcpServerPort);
address.sin_addr.s_addr = INADDR_ANY;
socklen_t length = sizeof(address);
listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
*/
struct sockaddr_in6 address;
address.sin6_family = AF_INET6;
address.sin6_port = htons(tcpServerPort);
address.sin6_addr = in6addr_any;
socklen_t length = sizeof(address);
listeningSocket = socket(AF_INET6, SOCK_STREAM, 0);
if (listeningSocket < 0)
{
log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
return;
}
int value=1;
setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
{
log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
close(listeningSocket);
return;
}
listen(listeningSocket, 5);
int clientSocket;
while(1)
{
clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
VompClient* m = new VompClient(&config, configDir, logoDir, resourceDir, imageDir, cacheDir, clientSocket);
m->run();
}
}