8 #include "../../../users.h"
12 BASE_PLUGIN * GetPlugin()
14 return new USERSTAT();
21 xmlParser = XML_ParserCreate(NULL);
22 pthread_mutex_init(&mutex, NULL);
27 XML_ParserFree(xmlParser);
30 int USERSTAT::ParseSettings()
32 vector<PARAM_VALUE>::iterator i;
35 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
38 transform(s.begin(), s.end(), s.begin(), USERSTAT::ToLower());
41 if (str2x<uint16_t>(*(i->value.begin()), port))
43 errorStr = "'Port' parameter must be a numeric value";
47 if (s == "maxthreads")
49 if (str2x<unsigned>(*(i->value.begin()), maxThreads))
51 errorStr = "'MaxThreads' parameter must be a numeric value";
60 int USERSTAT::Prepare()
62 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
66 errorStr = "Create USERSTAT socket failed.";
70 printfd(__FILE__, "USERSTAT::Prepare() socket - ok\n");
72 listenAddr.sin_family = PF_INET;
73 listenAddr.sin_port = htons(port);
74 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
78 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
80 errorStr = "Setsockopt failed. " + string(strerror(errno));
84 printfd(__FILE__, "USERSTAT::Prepare() setsockopt - ok\n");
86 int res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
90 errorStr = "Bind USERSTAT socket failed";
94 printfd(__FILE__, "USERSTAT::Prepare() bind - ok port: %d\n", port);
96 res = listen(listenSocket, 0);
99 errorStr = "Listen USERSTAT socket failed";
102 printfd(__FILE__, "USERSTAT::Prepare() listen - ok\n");
108 int USERSTAT::Finalize()
110 return close(listenSocket);
113 int USERSTAT::Start()
120 if (pthread_create(&thread, NULL, Run, this))
122 errorStr = "Cannot create thread";
132 if (pthread_kill(thread, SIGTERM))
134 errorStr = "Cannot send signal to thread";
137 for (int i = 0; i < 25; i++)
146 errorStr = "Cannot stop thread";
152 void * USERSTAT::Run(void * t)
154 USERSTAT * us = reinterpret_cast<USERSTAT *>(t);
157 struct sockaddr_in outerAddr;
158 socklen_t outerAddrLen;
161 us->isRunning = true;
164 outerSocket = accept(us->listenSocket, (struct sockaddr *)&outerAddr, &outerAddrLen);
167 std::vector<THREAD_INFO>::iterator it;
168 us->pool.erase(remove_if(us->pool.begin(), us->pool.end(), USERSTAT::IsDone()), us->pool.end());
170 while (us->pool.size() >= us->maxThreads)
173 info.users = us->users;
174 info.store = us->store;
175 info.outerSocket = outerSocket;
177 us->pool.push_back(info);
181 if (pthread_create(&thread, NULL, Operate, &(*it)))
183 us->errorStr = "Cannot create thread";
184 printfd(__FILE__, "Cannot create thread\n");
189 us->isRunning = false;
193 void * USERSTAT::Operate(void * i)
195 THREAD_INFO * info = reinterpret_cast<THREAD_INFO *>(i);
200 int res = read(info->outerSocket, &size, sizeof(size));
201 if (res != sizeof(size))
203 printfd(__FILE__, "Reading stream size failed! Wanted %d bytes, got %d bytes.\n", sizeof(size), res);
208 printfd(__FILE__, "USERSTAT::Operate() size = %d\n", size);
211 printfd(__FILE__, "USERSTAT::Operate() Invalid data size.\n");
216 login = new char[size];
218 res = read(info->outerSocket, login, size);
221 printfd(__FILE__, "Reading login failed! Wanted %d bytes, got %d bytes.\n", 32, res);
227 l.assign(login, size);
229 res = read(info->outerSocket, &size, sizeof(size));
230 if (res != sizeof(size))
232 printfd(__FILE__, "Reading stream size failed! Wanted %d bytes, got %d bytes.\n", sizeof(size), res);
237 printfd(__FILE__, "USERSTAT::Operate() size = %d\n", size);
240 printfd(__FILE__, "USERSTAT::Operate() Invalid data size.\n");
245 buf = new unsigned char[size];
246 res = read(info->outerSocket, buf, size);
249 printfd(__FILE__, "Reading stream failed! Wanted %d bytes, got %d bytes.\n", size, res);
254 printfd(__FILE__, "Received data: %s\n", buf);
257 if (info->users->FindByName(l, &it))
259 printfd(__FILE__, "User '%s' not found.\n", login);
264 std::string password = it->property.password;
266 printfd(__FILE__, "USERSTAT::Operate() Requested user: '%s'\n", login);
267 printfd(__FILE__, "USERSTAT::Operate() Encription init using password: '%s'\n", password.c_str());
270 char * key = new char[password.length()];
271 strncpy(key, password.c_str(), password.length());
274 reinterpret_cast<unsigned char *>(key),
277 for (int i = 0; i < size / 8; ++i) {
280 a = n2l(buf + i * 8);
281 b = n2l(buf + i * 8 + 4);
282 Blowfish_Decrypt(&ctx,
286 l2n(b, buf + i * 8 + 4);
291 printfd(__FILE__, "Received XML: %s\n", buf);