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* tconfigDir)
59 if (threadIsActive()) return 1;
61 configDir = tconfigDir;
65 #define dsyslog(x) std::cout << x << std::endl;
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");
82 char* cfgLogFilename = config.getValueString("General", "Log file");
85 log.init(Log::DEBUG, cfgLogFilename);
86 delete[] cfgLogFilename;
87 log.log("Main", Log::INFO, "Logging started");
91 dsyslog("VOMP: Logging disabled");
94 // Work out a name for this server
98 // Try to get from vomp.conf
99 serverName = config.getValueString("General", "Server name");
100 if (!serverName) // If not, get the hostname
102 serverName = new char[1024];
103 if (gethostname(serverName, 1024)) // if not, just use "-"
105 strcpy(serverName, "-");
109 int udpSuccess = udpr.run(serverName);
115 log.log("Main", Log::CRIT, "Could not start UDP replier");
120 // Read config and start bootp and tftp as appropriate
123 int bootpEnabled = 0;
126 configString = config.getValueString("General", "Bootp server enabled");
127 if (configString && (!strcasecmp(configString, "yes"))) bootpEnabled = 1;
128 if (configString) delete[] configString;
130 configString = config.getValueString("General", "TFTP server enabled");
131 if (configString && (!strcasecmp(configString, "yes"))) tftpEnabled = 1;
132 if (configString) delete[] configString;
139 log.log("Main", Log::CRIT, "Could not start Bootpd");
146 log.log("Main", Log::INFO, "Not starting Bootpd");
151 char tftpPath[PATH_MAX];
153 configString = config.getValueString("General", "TFTP directory");
156 snprintf(tftpPath, PATH_MAX, "%s", configString);
158 // this will never happen.. surely.
159 if ((strlen(tftpPath) + 2) >= PATH_MAX)
161 delete[] configString;
162 log.log("Main", Log::CRIT, "Could not understand TFTP directory from config");
167 // if there isn't a / at the end of the dir, add one
168 if (tftpPath[strlen(tftpPath) - 1] != '/') strcat(tftpPath, "/");
170 delete[] configString;
174 snprintf(tftpPath, PATH_MAX, "%s/", configDir);
177 log.log("Main", Log::INFO, "TFTP path '%s'", tftpPath);
179 if (!tftpd.run(tftpPath))
181 log.log("Main", Log::CRIT, "Could not start TFTPd");
188 log.log("Main", Log::INFO, "Not starting TFTPd");
191 // Start mvprelay thread
194 log.log("Main", Log::CRIT, "Could not start MVPRelay");
200 log.log("Main", Log::INFO, "MVPRelay started");
206 log.log("Main", Log::CRIT, "Could not start MVPServer thread");
211 log.log("Main", Log::DEBUG, "MVPServer run success");
215 void MVPServer::threadMethod()
217 // I want to die as soon as I am cancelled because I'll be in accept()
218 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
219 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
221 struct sockaddr_in address;
222 address.sin_family = AF_INET;
223 address.sin_port = htons(3024);
224 address.sin_addr.s_addr = INADDR_ANY;
225 socklen_t length = sizeof(address);
227 listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
228 if (listeningSocket < 0)
230 log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
235 setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
237 if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
239 log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
240 close(listeningSocket);
244 listen(listeningSocket, 5);
250 clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
251 MVPClient* m = new MVPClient(&config, configDir, clientSocket);