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"
26 extern pthread_mutex_t threadClientMutex;
28 MVPServer::MVPServer()
30 // MH in case anbody has a better position :-)
31 pthread_mutex_init(&threadClientMutex, NULL);
34 MVPServer::~MVPServer()
41 if (threadIsActive()) threadCancel();
42 close(listeningSocket);
49 log.log("Main", Log::INFO, "Stopped main server thread");
57 int MVPServer::run(char* tconfigDirExtra)
59 if (threadIsActive()) return 1;
61 configDirExtra = tconfigDirExtra;
64 #ifndef VOMPSTANDALONE
65 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
67 const char* configDir = ".";
68 #define dsyslog(x) std::cout << x << std::endl;
72 dsyslog("VOMP: Could not get config dir from VDR");
76 char configFileName[PATH_MAX];
77 snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
79 if (config.init(configFileName))
81 dsyslog("VOMP: Config file found");
85 dsyslog("VOMP: Config file not found");
91 char* cfgLogFilename = config.getValueString("General", "Log file");
94 log.init(Log::DEBUG, cfgLogFilename);
95 delete[] cfgLogFilename;
96 log.log("Main", Log::INFO, "Logging started");
100 dsyslog("VOMP: Logging disabled");
103 // Work out a name for this server
107 // Try to get from vomp.conf
108 serverName = config.getValueString("General", "Server name");
109 if (!serverName) // If not, get the hostname
111 serverName = new char[1024];
112 if (gethostname(serverName, 1024)) // if not, just use "-"
114 strcpy(serverName, "-");
118 int udpSuccess = udpr.run(serverName);
124 log.log("Main", Log::CRIT, "Could not start UDP replier");
129 // Read config and start bootp and tftp as appropriate
132 int bootpEnabled = 0;
135 configString = config.getValueString("General", "Bootp server enabled");
136 if (configString && (!strcasecmp(configString, "yes"))) bootpEnabled = 1;
137 if (configString) delete[] configString;
139 configString = config.getValueString("General", "TFTP server enabled");
140 if (configString && (!strcasecmp(configString, "yes"))) tftpEnabled = 1;
141 if (configString) delete[] configString;
148 log.log("Main", Log::CRIT, "Could not start Bootpd");
155 log.log("Main", Log::INFO, "Not starting Bootpd");
160 char tftpPath[PATH_MAX];
161 // snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
163 configString = config.getValueString("General", "TFTP directory");
166 snprintf(tftpPath, PATH_MAX, "%s", configString);
168 // this will never happen.. surely.
169 if ((strlen(tftpPath) + 2) >= PATH_MAX)
171 delete[] configString;
172 log.log("Main", Log::CRIT, "Could not understand TFTP directory from config");
177 // if there isn't a / at the end of the dir, add one
178 if (tftpPath[strlen(tftpPath) - 1] != '/') strcat(tftpPath, "/");
180 delete[] configString;
184 snprintf(tftpPath, PATH_MAX, "%s/", configDir);
187 log.log("Main", Log::INFO, "TFTP path '%s'", tftpPath);
189 if (!tftpd.run(tftpPath))
191 log.log("Main", Log::CRIT, "Could not start TFTPd");
198 log.log("Main", Log::INFO, "Not starting TFTPd");
201 // Start mvprelay thread
204 log.log("Main", Log::CRIT, "Could not start MVPRelay");
210 log.log("Main", Log::INFO, "MVPRelay started");
216 log.log("Main", Log::CRIT, "Could not start MVPServer thread");
221 log.log("Main", Log::DEBUG, "MVPServer run success");
225 void MVPServer::threadMethod()
227 // I want to die as soon as I am cancelled because I'll be in accept()
228 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
229 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
231 struct sockaddr_in address;
232 address.sin_family = AF_INET;
233 address.sin_port = htons(3024);
234 address.sin_addr.s_addr = INADDR_ANY;
235 socklen_t length = sizeof(address);
237 listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
238 if (listeningSocket < 0)
240 log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
245 setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
247 if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
249 log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
250 close(listeningSocket);
254 listen(listeningSocket, 5);
260 clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
261 MVPClient* m = new MVPClient(&config, configDirExtra, clientSocket);