2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mvpserver.h"
23 extern pthread_mutex_t threadClientMutex;
25 MVPServer::MVPServer()
27 // MH in case anbody has a better position :-)
28 pthread_mutex_init(&threadClientMutex, NULL);
31 MVPServer::~MVPServer()
38 if (threadIsActive()) threadCancel();
39 close(listeningSocket);
46 log.log("Main", Log::INFO, "Stopped main server thread");
54 int MVPServer::run(char* tconfigDirExtra)
56 if (threadIsActive()) return 1;
58 configDirExtra = tconfigDirExtra;
61 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
64 dsyslog("VOMP: Could not get config dir from VDR");
68 char configFileName[PATH_MAX];
69 snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
71 if (config.init(configFileName))
73 dsyslog("VOMP: Config file found");
77 dsyslog("VOMP: Config file not found");
83 char* cfgLogFilename = config.getValueString("General", "Log file");
86 log.init(Log::DEBUG, cfgLogFilename);
87 delete[] cfgLogFilename;
88 log.log("Main", Log::INFO, "Logging started");
92 dsyslog("VOMP: Logging disabled");
95 // Work out a name for this server
99 // Try to get from vomp.conf
100 serverName = config.getValueString("General", "Server name");
101 if (!serverName) // If not, get the hostname
103 serverName = new char[1024];
104 if (gethostname(serverName, 1024)) // if not, just use "-"
106 strcpy(serverName, "-");
110 int udpSuccess = udpr.run(serverName);
116 log.log("Main", Log::CRIT, "Could not start UDP replier");
121 // Read config and start bootp and tftp as appropriate
124 int bootpEnabled = 0;
127 configString = config.getValueString("General", "Bootp server enabled");
128 if (configString && (!strcasecmp(configString, "yes"))) bootpEnabled = 1;
129 if (configString) delete[] configString;
131 configString = config.getValueString("General", "TFTP server enabled");
132 if (configString && (!strcasecmp(configString, "yes"))) tftpEnabled = 1;
133 if (configString) delete[] configString;
140 log.log("Main", Log::CRIT, "Could not start Bootpd");
147 log.log("Main", Log::INFO, "Not starting Bootpd");
152 char tftpPath[PATH_MAX];
153 // snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
155 configString = config.getValueString("General", "TFTP directory");
158 snprintf(tftpPath, PATH_MAX, "%s", configString);
160 // this will never happen.. surely.
161 if ((strlen(tftpPath) + 2) >= PATH_MAX)
163 delete[] configString;
164 log.log("Main", Log::CRIT, "Could not understand TFTP directory from config");
169 // if there isn't a / at the end of the dir, add one
170 if (tftpPath[strlen(tftpPath) - 1] != '/') strcat(tftpPath, "/");
172 delete[] configString;
176 snprintf(tftpPath, PATH_MAX, "%s/", configDir);
179 log.log("Main", Log::INFO, "TFTP path '%s'", tftpPath);
181 if (!tftpd.run(tftpPath))
183 log.log("Main", Log::CRIT, "Could not start TFTPd");
190 log.log("Main", Log::INFO, "Not starting TFTPd");
193 // Start mvprelay thread
196 log.log("Main", Log::CRIT, "Could not start MVPRelay");
202 log.log("Main", Log::INFO, "MVPRelay started");
208 log.log("Main", Log::CRIT, "Could not start MVPServer thread");
213 log.log("Main", Log::DEBUG, "MVPServer run success");
217 void MVPServer::threadMethod()
219 // I want to die as soon as I am cancelled because I'll be in accept()
220 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
221 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
223 struct sockaddr_in address;
224 address.sin_family = AF_INET;
225 address.sin_port = htons(3024);
226 address.sin_addr.s_addr = INADDR_ANY;
227 socklen_t length = sizeof(address);
229 listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
230 if (listeningSocket < 0)
232 log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
237 setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
239 if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
241 log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
242 close(listeningSocket);
246 listen(listeningSocket, 5);
252 clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
253 MVPClient* m = new MVPClient(configDirExtra, clientSocket);