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()
29 if (threadIsActive()) stop();
34 if (!threadIsActive()) return 0;
37 log.log("MVPServer", Log::INFO, "Stopped MVPServer thread");
42 close(listeningSocket);
49 if (threadIsActive()) return 1;
51 log.init(Log::DEBUG, "/tmp/vompserver.log", 1);
53 char serverName[1024];
54 bool nameSuccess = false;
56 // Try to get a server name from vomp.conf
57 const char* configDir = cPlugin::ConfigDirectory();
60 log.log("Client", Log::DEBUG, "No config dir!");
64 char configFileName[PATH_MAX];
65 snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
68 if (c.init(configFileName))
70 char* fc = c.getValueString("General", "Server name");
73 strncpy(serverName, fc, 1024);
83 if (gethostname(serverName, 1024)) // if fail
85 strcpy(serverName, "-");
89 serverName[1023] = '\0';
91 if (!udpr.run(serverName))
93 log.log("MVPServer", Log::CRIT, "Could not start UDP replier");
101 log.log("MVPServer", Log::CRIT, "Could not start MVPServer thread");
107 log.log("MVPServer", Log::DEBUG, "MVPServer run success");
111 void MVPServer::threadMethod()
113 // I want to die as soon as I am cancelled because I'll be in accept()
114 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
115 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
117 struct sockaddr_in address;
118 address.sin_family = AF_INET;
119 address.sin_port = htons(3024);
120 address.sin_addr.s_addr = INADDR_ANY;
121 socklen_t length = sizeof(address);
123 listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
124 if (listeningSocket < 0)
126 log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
131 setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
133 if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
135 log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
136 close(listeningSocket);
140 listen(listeningSocket, 5);
146 clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
147 MVPClient* m = new MVPClient(clientSocket);
153 ULLONG ntohll(ULLONG a)
158 ULLONG htonll(ULLONG a)
160 #if BYTE_ORDER == BIG_ENDIAN
165 b = ((a << 56) & 0xFF00000000000000ULL)
166 | ((a << 40) & 0x00FF000000000000ULL)
167 | ((a << 24) & 0x0000FF0000000000ULL)
168 | ((a << 8) & 0x000000FF00000000ULL)
169 | ((a >> 8) & 0x00000000FF000000ULL)
170 | ((a >> 24) & 0x0000000000FF0000ULL)
171 | ((a >> 40) & 0x000000000000FF00ULL)
172 | ((a >> 56) & 0x00000000000000FFULL) ;