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");
114 if (config.getValueString("General", "Bootp server"))
118 log.log("Main", Log::CRIT, "Could not start Bootpd");
125 log.log("Main", Log::INFO, "Not starting Bootpd");
129 if (config.getValueString("General", "TFTP server"))
133 log.log("Main", Log::CRIT, "Could not start TFTPd");
140 log.log("Main", Log::INFO, "Not starting TFTPd");
146 log.log("Main", Log::CRIT, "Could not start MVPServer thread");
151 log.log("Main", Log::DEBUG, "MVPServer run success");
155 void MVPServer::threadMethod()
157 // I want to die as soon as I am cancelled because I'll be in accept()
158 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
159 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
161 struct sockaddr_in address;
162 address.sin_family = AF_INET;
163 address.sin_port = htons(3024);
164 address.sin_addr.s_addr = INADDR_ANY;
165 socklen_t length = sizeof(address);
167 listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
168 if (listeningSocket < 0)
170 log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
175 setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
177 if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
179 log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
180 close(listeningSocket);
184 listen(listeningSocket, 5);
190 clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
191 MVPClient* m = new MVPClient(clientSocket);