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");
51 if (threadIsActive()) return 1;
54 const char* configDir = cPlugin::ConfigDirectory();
57 dsyslog("VOMP: Could not get config dir from VDR");
61 char configFileName[PATH_MAX];
62 snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
63 if (config.init(configFileName))
65 dsyslog("VOMP: Config file found");
69 dsyslog("VOMP: Config file not found");
75 char* cfgLogFilename = config.getValueString("General", "Log file");
78 log.init(Log::DEBUG, cfgLogFilename);
79 delete[] cfgLogFilename;
80 log.log("Main", Log::INFO, "Logging started");
84 dsyslog("VOMP: Logging disabled");
87 // Work out a name for this server
91 // Try to get from vomp.conf
92 serverName = config.getValueString("General", "Server name");
93 if (!serverName) // If not, get the hostname
95 serverName = new char[1024];
96 if (gethostname(serverName, 1024)) // if not, just use "-"
98 strcpy(serverName, "-");
102 int udpSuccess = udpr.run(serverName);
108 log.log("Main", Log::CRIT, "Could not start UDP replier");
113 // Read config and start bootp and tftp as appropriate
116 int bootpEnabled = 0;
119 configString = config.getValueString("General", "Bootp server enabled");
120 if (configString && (!strcasecmp(configString, "yes"))) bootpEnabled = 1;
121 if (configString) delete[] configString;
123 configString = config.getValueString("General", "TFTP server enabled");
124 if (configString && (!strcasecmp(configString, "yes"))) tftpEnabled = 1;
125 if (configString) delete[] configString;
132 log.log("Main", Log::CRIT, "Could not start Bootpd");
139 log.log("Main", Log::INFO, "Not starting Bootpd");
144 char tftpPath[PATH_MAX];
145 // snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
147 configString = config.getValueString("General", "TFTP directory");
150 snprintf(tftpPath, PATH_MAX, "%s", configString);
152 // this will never happen.. surely.
153 if ((strlen(tftpPath) + 2) >= PATH_MAX)
155 delete[] configString;
156 log.log("Main", Log::CRIT, "Could not understand TFTP directory from config");
161 // if there isn't a / at the end of the dir, add one
162 if (tftpPath[strlen(tftpPath) - 1] != '/') strcat(tftpPath, "/");
164 delete[] configString;
168 snprintf(tftpPath, PATH_MAX, "%s/", configDir);
171 log.log("Main", Log::INFO, "TFTP path '%s'", tftpPath);
173 if (!tftpd.run(tftpPath))
175 log.log("Main", Log::CRIT, "Could not start TFTPd");
182 log.log("Main", Log::INFO, "Not starting TFTPd");
189 log.log("Main", Log::CRIT, "Could not start MVPServer thread");
194 log.log("Main", Log::DEBUG, "MVPServer run success");
198 void MVPServer::threadMethod()
200 // I want to die as soon as I am cancelled because I'll be in accept()
201 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
202 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
204 struct sockaddr_in address;
205 address.sin_family = AF_INET;
206 address.sin_port = htons(3024);
207 address.sin_addr.s_addr = INADDR_ANY;
208 socklen_t length = sizeof(address);
210 listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
211 if (listeningSocket < 0)
213 log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
218 setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
220 if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
222 log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
223 close(listeningSocket);
227 listen(listeningSocket, 5);
233 clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
234 MVPClient* m = new MVPClient(clientSocket);