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);
41 log.log("Main", Log::INFO, "Stopped main server thread");
49 int MVPServer::run(char* tconfigDirExtra)
51 if (threadIsActive()) return 1;
53 configDirExtra = tconfigDirExtra;
56 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
59 dsyslog("VOMP: Could not get config dir from VDR");
63 char configFileName[PATH_MAX];
64 snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
66 if (config.init(configFileName))
68 dsyslog("VOMP: Config file found");
72 dsyslog("VOMP: Config file not found");
78 char* cfgLogFilename = config.getValueString("General", "Log file");
81 log.init(Log::DEBUG, cfgLogFilename);
82 delete[] cfgLogFilename;
83 log.log("Main", Log::INFO, "Logging started");
87 dsyslog("VOMP: Logging disabled");
90 // Work out a name for this server
94 // Try to get from vomp.conf
95 serverName = config.getValueString("General", "Server name");
96 if (!serverName) // If not, get the hostname
98 serverName = new char[1024];
99 if (gethostname(serverName, 1024)) // if not, just use "-"
101 strcpy(serverName, "-");
105 int udpSuccess = udpr.run(serverName);
111 log.log("Main", Log::CRIT, "Could not start UDP replier");
116 // Read config and start bootp and tftp as appropriate
119 int bootpEnabled = 0;
122 configString = config.getValueString("General", "Bootp server enabled");
123 if (configString && (!strcasecmp(configString, "yes"))) bootpEnabled = 1;
124 if (configString) delete[] configString;
126 configString = config.getValueString("General", "TFTP server enabled");
127 if (configString && (!strcasecmp(configString, "yes"))) tftpEnabled = 1;
128 if (configString) delete[] configString;
135 log.log("Main", Log::CRIT, "Could not start Bootpd");
142 log.log("Main", Log::INFO, "Not starting Bootpd");
147 char tftpPath[PATH_MAX];
148 // snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
150 configString = config.getValueString("General", "TFTP directory");
153 snprintf(tftpPath, PATH_MAX, "%s", configString);
155 // this will never happen.. surely.
156 if ((strlen(tftpPath) + 2) >= PATH_MAX)
158 delete[] configString;
159 log.log("Main", Log::CRIT, "Could not understand TFTP directory from config");
164 // if there isn't a / at the end of the dir, add one
165 if (tftpPath[strlen(tftpPath) - 1] != '/') strcat(tftpPath, "/");
167 delete[] configString;
171 snprintf(tftpPath, PATH_MAX, "%s/", configDir);
174 log.log("Main", Log::INFO, "TFTP path '%s'", tftpPath);
176 if (!tftpd.run(tftpPath))
178 log.log("Main", Log::CRIT, "Could not start TFTPd");
185 log.log("Main", Log::INFO, "Not starting TFTPd");
192 log.log("Main", Log::CRIT, "Could not start MVPServer thread");
197 log.log("Main", Log::DEBUG, "MVPServer run success");
201 void MVPServer::threadMethod()
203 // I want to die as soon as I am cancelled because I'll be in accept()
204 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
205 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
207 struct sockaddr_in address;
208 address.sin_family = AF_INET;
209 address.sin_port = htons(3024);
210 address.sin_addr.s_addr = INADDR_ANY;
211 socklen_t length = sizeof(address);
213 listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
214 if (listeningSocket < 0)
216 log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
221 setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
223 if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
225 log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
226 close(listeningSocket);
230 listen(listeningSocket, 5);
236 clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
237 MVPClient* m = new MVPClient(configDirExtra, clientSocket);