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 MVPServer::MVPServer()
27 MVPServer::~MVPServer()
34 if (threadIsActive()) threadCancel();
35 close(listeningSocket);
42 log.log("Main", Log::INFO, "Stopped main server thread");
50 int MVPServer::run(char* tconfigDirExtra)
52 if (threadIsActive()) return 1;
54 configDirExtra = tconfigDirExtra;
57 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
60 dsyslog("VOMP: Could not get config dir from VDR");
64 char configFileName[PATH_MAX];
65 snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
67 if (config.init(configFileName))
69 dsyslog("VOMP: Config file found");
73 dsyslog("VOMP: Config file not found");
79 char* cfgLogFilename = config.getValueString("General", "Log file");
82 log.init(Log::DEBUG, cfgLogFilename);
83 delete[] cfgLogFilename;
84 log.log("Main", Log::INFO, "Logging started");
88 dsyslog("VOMP: Logging disabled");
91 // Work out a name for this server
95 // Try to get from vomp.conf
96 serverName = config.getValueString("General", "Server name");
97 if (!serverName) // If not, get the hostname
99 serverName = new char[1024];
100 if (gethostname(serverName, 1024)) // if not, just use "-"
102 strcpy(serverName, "-");
106 int udpSuccess = udpr.run(serverName);
112 log.log("Main", Log::CRIT, "Could not start UDP replier");
117 // Read config and start bootp and tftp as appropriate
120 int bootpEnabled = 0;
123 configString = config.getValueString("General", "Bootp server enabled");
124 if (configString && (!strcasecmp(configString, "yes"))) bootpEnabled = 1;
125 if (configString) delete[] configString;
127 configString = config.getValueString("General", "TFTP server enabled");
128 if (configString && (!strcasecmp(configString, "yes"))) tftpEnabled = 1;
129 if (configString) delete[] configString;
136 log.log("Main", Log::CRIT, "Could not start Bootpd");
143 log.log("Main", Log::INFO, "Not starting Bootpd");
148 char tftpPath[PATH_MAX];
149 // snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
151 configString = config.getValueString("General", "TFTP directory");
154 snprintf(tftpPath, PATH_MAX, "%s", configString);
156 // this will never happen.. surely.
157 if ((strlen(tftpPath) + 2) >= PATH_MAX)
159 delete[] configString;
160 log.log("Main", Log::CRIT, "Could not understand TFTP directory from config");
165 // if there isn't a / at the end of the dir, add one
166 if (tftpPath[strlen(tftpPath) - 1] != '/') strcat(tftpPath, "/");
168 delete[] configString;
172 snprintf(tftpPath, PATH_MAX, "%s/", configDir);
175 log.log("Main", Log::INFO, "TFTP path '%s'", tftpPath);
177 if (!tftpd.run(tftpPath))
179 log.log("Main", Log::CRIT, "Could not start TFTPd");
186 log.log("Main", Log::INFO, "Not starting TFTPd");
189 // Start mvprelay thread
192 log.log("Main", Log::CRIT, "Could not start MVPRelay");
198 log.log("Main", Log::INFO, "MVPRelay started");
204 log.log("Main", Log::CRIT, "Could not start MVPServer thread");
209 log.log("Main", Log::DEBUG, "MVPServer run success");
213 void MVPServer::threadMethod()
215 // I want to die as soon as I am cancelled because I'll be in accept()
216 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
217 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
219 struct sockaddr_in address;
220 address.sin_family = AF_INET;
221 address.sin_port = htons(3024);
222 address.sin_addr.s_addr = INADDR_ANY;
223 socklen_t length = sizeof(address);
225 listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
226 if (listeningSocket < 0)
228 log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
233 setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
235 if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
237 log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
238 close(listeningSocket);
242 listen(listeningSocket, 5);
248 clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
249 MVPClient* m = new MVPClient(configDirExtra, clientSocket);