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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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);
35 MVPServer::~MVPServer()
42 if (threadIsActive()) threadCancel();
43 close(listeningSocket);
50 log.log("Main", Log::INFO, "Stopped main server thread");
58 int MVPServer::run(char* tconfigDir)
60 if (threadIsActive()) return 1;
62 configDir = tconfigDir;
66 #define dsyslog(x) std::cout << x << std::endl;
69 char configFileName[PATH_MAX];
70 snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
72 if (config.init(configFileName))
74 dsyslog("VOMP: Config file found");
78 dsyslog("VOMP: Config file not found");
83 char* cfgLogFilename = config.getValueString("General", "Log file");
86 log.init(Log::DEBUG, cfgLogFilename);
87 delete[] cfgLogFilename;
88 log.log("Main", Log::INFO, "Logging started");
92 dsyslog("VOMP: Logging disabled");
95 // Get UDP port number for discovery service
98 int udpport = config.getValueLong("General", "UDP port", &fail);
99 if (fail) udpport = 51051;
101 // Work out a name for this server
105 // Try to get from vomp.conf
106 serverName = config.getValueString("General", "Server name");
107 if (!serverName) // If not, get the hostname
109 serverName = new char[1024];
110 if (gethostname(serverName, 1024)) // if not, set default
112 strcpy(serverName, "VOMP Server");
116 // Get VOMP server TCP port to give to UDP replier to put in packets
118 tcpServerPort = config.getValueLong("General", "TCP port", &fail);
119 if (fail) tcpServerPort = 3024;
121 int udpSuccess = udpr.run(udpport, serverName, tcpServerPort);
127 log.log("Main", Log::CRIT, "Could not start UDP replier");
132 // Read config and start bootp and tftp as appropriate
135 int bootpEnabled = 0;
137 int mvprelayEnabled = 1;
139 configString = config.getValueString("General", "Bootp server enabled");
140 if (configString && (!strcasecmp(configString, "yes"))) bootpEnabled = 1;
141 if (configString) delete[] configString;
143 configString = config.getValueString("General", "TFTP server enabled");
144 if (configString && (!strcasecmp(configString, "yes"))) tftpEnabled = 1;
145 if (configString) delete[] configString;
147 configString = config.getValueString("General", "MVPRelay enabled");
148 if (configString && (strcasecmp(configString, "yes"))) mvprelayEnabled = 0;
149 if (configString) delete[] configString;
154 if (!bootpd.run(configDir))
156 log.log("Main", Log::CRIT, "Could not start Bootpd");
163 log.log("Main", Log::INFO, "Not starting Bootpd");
168 char tftpPath[PATH_MAX];
170 configString = config.getValueString("General", "TFTP directory");
173 snprintf(tftpPath, PATH_MAX, "%s", configString);
175 // this will never happen.. surely.
176 if ((strlen(tftpPath) + 2) >= PATH_MAX)
178 delete[] configString;
179 log.log("Main", Log::CRIT, "Could not understand TFTP directory from config");
184 // if there isn't a / at the end of the dir, add one
185 if (tftpPath[strlen(tftpPath) - 1] != '/') strcat(tftpPath, "/");
187 delete[] configString;
191 snprintf(tftpPath, PATH_MAX, "%s/", configDir);
194 log.log("Main", Log::INFO, "TFTP path '%s'", tftpPath);
196 if (!tftpd.run(tftpPath))
198 log.log("Main", Log::CRIT, "Could not start TFTPd");
205 log.log("Main", Log::INFO, "Not starting TFTPd");
208 // Start mvprelay thread
213 log.log("Main", Log::CRIT, "Could not start MVPRelay");
219 log.log("Main", Log::INFO, "MVPRelay started");
224 log.log("Main", Log::INFO, "Not starting MVPRelay");
230 log.log("Main", Log::CRIT, "Could not start MVPServer thread");
235 log.log("Main", Log::DEBUG, "MVPServer run success");
239 void MVPServer::threadMethod()
241 // I want to die as soon as I am cancelled because I'll be in accept()
242 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
243 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
245 struct sockaddr_in address;
246 address.sin_family = AF_INET;
247 address.sin_port = htons(tcpServerPort);
248 address.sin_addr.s_addr = INADDR_ANY;
249 socklen_t length = sizeof(address);
251 listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
252 if (listeningSocket < 0)
254 log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
259 setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
261 if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
263 log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
264 close(listeningSocket);
268 listen(listeningSocket, 5);
274 clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
275 VompClient* m = new VompClient(&config, configDir, clientSocket);