10 #include "stg/common.h"
11 #include "stg/ia_packets.h"
15 class HasIP : public std::unary_function<std::pair<uint32_t, USER>, bool> {
17 explicit HasIP(uint32_t i) : ip(i) {}
18 bool operator()(const std::pair<uint32_t, USER> & value) { return value.first == ip; }
23 PROTO::PROTO(const std::string & server,
31 uint32_t ip = inet_addr(server.c_str());
32 if (ip == INADDR_NONE)
34 struct hostent * hePtr = gethostbyname(server.c_str());
37 ip = *((uint32_t *)hePtr->h_addr_list[0]);
41 errorStr = "Unknown host: '";
44 printfd(__FILE__, "PROTO::PROTO() - %s\n", errorStr.c_str());
45 throw std::runtime_error(errorStr);
49 localAddr.sin_family = AF_INET;
50 localAddr.sin_port = htons(localPort);
51 localAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
53 serverAddr.sin_family = AF_INET;
54 serverAddr.sin_port = htons(port);
55 serverAddr.sin_addr.s_addr = ip;
57 unsigned char key[IA_PASSWD_LEN];
58 memset(key, 0, IA_PASSWD_LEN);
59 strncpy(reinterpret_cast<char *>(key), "pr7Hhen", 8);
60 Blowfish_Init(&ctx, key, IA_PASSWD_LEN);
62 processors["CONN_SYN_ACK"] = &PROTO::CONN_SYN_ACK_Proc;
63 processors["ALIVE_SYN"] = &PROTO::ALIVE_SYN_Proc;
64 processors["DISCONN_SYN_ACK"] = &PROTO::DISCONN_SYN_ACK_Proc;
65 processors["FIN"] = &PROTO::FIN_Proc;
66 processors["INFO"] = &PROTO::INFO_Proc;
67 // ERR_Proc will be handled explicitly
74 void * PROTO::Runner(void * data)
76 PROTO * protoPtr = static_cast<PROTO *>(data);
85 if (pthread_create(&tid, NULL, &Runner, NULL))
87 errorStr = "Failed to create listening thread: '";
88 errorStr += strerror(errno);
90 printfd(__FILE__, "PROTO::Start() - %s\n", errorStr.c_str());
100 while (!stopped && time < timeout)
102 struct timespec ts = {1, 0};
103 nanosleep(&ts, NULL);
108 errorStr = "Failed to stop listening thread - timed out";
109 printfd(__FILE__, "PROTO::Stop() - %s\n", errorStr.c_str());
112 if (pthread_join(tid, NULL))
114 errorStr = "Failed to join listening thread after stop: '";
115 errorStr += strerror(errno);
117 printfd(__FILE__, "PROTO::Stop() - %s\n", errorStr.c_str());
123 void PROTO::AddUser(const USER & user, bool connect)
125 users.push_back(std::make_pair(user.GetIP(), user));
127 pfd.fd = user.GetSocket();
130 pollFds.push_back(pfd);
132 users.back().second.InitNetwork();
136 RealConnect(&users.back().second);
140 bool PROTO::Connect(uint32_t ip)
142 std::vector<std::pair<uint32_t, USER> >::iterator it;
143 it = std::find_if(users.begin(), users.end(), HasIP(ip));
144 if (it == users.end())
149 return RealConnect(&it->second);
152 bool PROTO::Disconnect(uint32_t ip)
154 std::vector<std::pair<uint32_t, USER> >::iterator it;
155 it = std::find_if(users.begin(), users.end(), HasIP(ip));
156 if (it == users.end())
161 return RealDisconnect(&it->second);
168 int res = poll(&pollFds.front(), pollFds.size(), timeout);
180 bool PROTO::RecvPacket()
183 std::vector<struct pollfd>::iterator it;
184 std::vector<std::pair<uint32_t, USER> >::iterator userIt;
185 for (it = pollFds.begin(), userIt = users.begin(); it != pollFds.end() && userIt != users.end(); ++it, ++userIt)
190 assert(it->fd == userIt->second.GetSocket() && "File descriptors from poll fds and users must be syncked");
191 struct sockaddr_in addr;
192 socklen_t fromLen = sizeof(addr);
194 int res = recvfrom(userIt->second.GetSocket(), buffer, sizeof(buffer), 0, (struct sockaddr *)&addr, &fromLen);
202 result = result && HandlePacket(buffer, &(userIt->second));
209 bool PROTO::HandlePacket(const char * buffer, USER * user)
211 if (strcmp(buffer + 4 + sizeof(HDR_8), "ERR"))
213 return ERR_Proc(buffer, user);
216 std::string packetName(buffer + 12);
217 std::map<std::string, PacketProcessor>::const_iterator it;
218 it = processors.find(packetName);
219 if (it != processors.end())
220 return (this->*it->second)(buffer, user);
222 printfd(__FILE__, "PROTO::HandlePacket() - invalid packet signature: '%s'\n", packetName.c_str());
227 bool PROTO::CONN_SYN_ACK_Proc(const void * buffer, USER * user)
229 const CONN_SYN_ACK_8 * packet = static_cast<const CONN_SYN_ACK_8 *>(buffer);
231 uint32_t rnd = packet->rnd;
232 uint32_t userTimeout = packet->userTimeOut;
233 uint32_t aliveTimeout = packet->aliveDelay;
237 SwapBytes(userTimeout);
238 SwapBytes(aliveDelay);
243 if (user->GetPhase() != 2)
245 errorStr = "Unexpected CONN_SYN_ACK";
246 printfd(__FILE__, "PROTO::CONN_SYN_ACK_Proc() - wrong phase: %d\n", user->GetPhase());
250 user->SetAliveTimeout(aliveTimeout);
251 user->SetUserTimeout(userTimeout);
254 printfd(__FILE__, "PROTO::CONN_SYN_ACK_Proc() - user '%s' successfully logged in from IP %s\n", user->GetLogin().c_str(), inet_ntostring(user->GetIP()).c_str());
259 bool PROTO::ALIVE_SYN_Proc(const void * buffer, USER * user)
261 const ALIVE_SYN_8 * packet = static_cast<const ALIVE_SYN_8 *>(buffer);
263 uint32_t rnd = packet->rnd;
269 if (user->GetPhase() != 3)
271 errorStr = "Unexpected ALIVE_SYN";
272 printfd(__FILE__, "PROTO::ALIVE_SYN_Proc() - wrong phase: %d\n", user->GetPhase());
275 if (user->GetRnd() + 1 != rnd)
277 errorStr = "Wrong control value at ALIVE_SYN";
278 printfd(__FILE__, "PROTO::ALIVE_SYN_Proc() - wrong control value: %d, expected: %d\n", rnd, user->GetRnd() + 1);
284 Send_ALIVE_ACK(user);
289 bool PROTO::DISCONN_SYN_ACK_Proc(const void * buffer, USER * user)
291 const DISCONN_SYN_ACK_8 * packet = static_cast<const DISCONN_SYN_ACK_8 *>(buffer);
293 uint32_t rnd = packet->rnd;
299 if (user->GetPhase() != 4)
301 errorStr = "Unexpected DISCONN_SYN_ACK";
302 printfd(__FILE__, "PROTO::DISCONN_SYN_ACK_Proc() - wrong phase: %d\n", user->GetPhase());
305 if (user->GetRnd() + 1 != rnd)
307 errorStr = "Wrong control value at DISCONN_SYN_ACK";
308 printfd(__FILE__, "PROTO::DISCONN_SYN_ACK_Proc() - wrong control value: %d, expected: %d\n", rnd, user->GetRnd() + 1);
314 Send_DISCONN_ACK(user);
319 bool PROTO::FIN_Proc(const void * buffer, USER * user)
321 if (user->GetPhase() != 5)
323 errorStr = "Unexpected FIN";
324 printfd(__FILE__, "PROTO::FIN_Proc() - wrong phase: %d\n", user->GetPhase());
332 bool PROTO::INFO_Proc(const void * buffer, USER * user)
334 //const INFO_8 * packet = static_cast<const INFO_8 *>(buffer);
339 bool PROTO::ERR_Proc(const void * buffer, USER * user)
341 const ERR_8 * packet = static_cast<const ERR_8 *>(buffer);
342 const char * ptr = static_cast<const char *>(buffer);
344 for (size_t i = 0; i < sizeof(ERR_8) / 8; i++)
345 Blowfish_Decrypt(user->GetCtx(), (uint32_t *)(ptr + i * 8), (uint32_t *)(ptr + i * 8 + 4));
347 //uint32_t len = packet->len;
353 user->SetPhase(1); //TODO: Check
354 /*KOIToWin((const char*)err.text, &messageText);
355 if (pErrorCb != NULL)
356 pErrorCb(messageText, IA_SERVER_ERROR, errorCbData);
357 phaseTime = GetTickCount();
358 codeError = IA_SERVER_ERROR;*/
363 bool PROTO::Send_CONN_SYN(USER * user)
367 packet.len = sizeof(packet);
370 SwapBytes(packet.len);
373 strncpy((char *)packet.type, "CONN_SYN", sizeof(packet.type));
374 strncpy((char *)packet.login, user->GetLogin().c_str(), sizeof(packet.login));
375 packet.dirs = 0xFFffFFff;
377 return SendPacket(&packet, sizeof(packet), user);
380 bool PROTO::Send_CONN_ACK(USER * user)
384 packet.len = sizeof(packet);
385 packet.rnd = user->IncRnd();
388 SwapBytes(packet.len);
389 SwapBytes(packet.rnd);
392 strncpy((char *)packet.loginS, user->GetLogin().c_str(), sizeof(packet.loginS));
393 strncpy((char *)packet.type, "CONN_ACK", sizeof(packet.type));
395 return SendPacket(&packet, sizeof(packet), user);
398 bool PROTO::Send_ALIVE_ACK(USER * user)
402 packet.len = sizeof(packet);
403 packet.rnd = user->IncRnd();
406 SwapBytes(packet.len);
407 SwapBytes(packet.rnd);
410 strncpy((char *)packet.loginS, user->GetLogin().c_str(), sizeof(packet.loginS));
411 strncpy((char *)packet.type, "ALIVE_ACK", sizeof(packet.type));
413 return SendPacket(&packet, sizeof(packet), user);
416 bool PROTO::Send_DISCONN_SYN(USER * user)
418 DISCONN_SYN_8 packet;
420 packet.len = sizeof(packet);
423 SwapBytes(packet.len);
426 strncpy((char *)packet.loginS, user->GetLogin().c_str(), sizeof(packet.loginS));
427 strncpy((char *)packet.type, "DISCONN_SYN", sizeof(packet.type));
428 strncpy((char *)packet.login, user->GetLogin().c_str(), sizeof(packet.login));
430 return SendPacket(&packet, sizeof(packet), user);
433 bool PROTO::Send_DISCONN_ACK(USER * user)
435 DISCONN_ACK_8 packet;
437 packet.len = sizeof(packet);
438 packet.rnd = user->IncRnd();
441 SwapBytes(packet.len);
442 SwapBytes(packet.rnd);
445 strncpy((char *)packet.loginS, user->GetLogin().c_str(), sizeof(packet.loginS));
446 strncpy((char *)packet.type, "DISCONN_ACK", sizeof(packet.type));
448 return SendPacket(&packet, sizeof(packet), user);
451 bool PROTO::SendPacket(const void * packet, size_t length, USER * user)
455 assert(sizeof(hdr) + length < 2048 && "Packet length must not exceed 2048 bytes");
457 strncpy((char *)hdr.magic, IA_ID, 6);
459 hdr.protoVer[1] = 8; // IA_PROTO_VER
461 unsigned char buffer[2048];
462 memcpy(buffer, &hdr, sizeof(hdr));
463 memcpy(buffer + sizeof(hdr), packet, length);
465 size_t offset = sizeof(HDR_8);
466 for (size_t i = 0; i < IA_LOGIN_LEN / 8; i++)
468 Blowfish_Encrypt(&ctx,
469 (uint32_t *)(buffer + offset + i * 8),
470 (uint32_t *)(buffer + offset + i * 8 + 4));
473 offset += IA_LOGIN_LEN;
474 size_t encLen = (length - IA_LOGIN_LEN) / 8;
475 for (size_t i = 0; i < encLen; i++)
477 Blowfish_Encrypt(user->GetCtx(),
478 (uint32_t*)(buffer + offset + i * 8),
479 (uint32_t*)(buffer + offset + i * 8 + 4));
482 int res = sendto(user->GetSocket(), buffer, sizeof(buffer), 0, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
486 errorStr = "Failed to send packet: '";
487 errorStr += strerror(errno);
489 printfd(__FILE__, "PROTO::SendPacket() - %s, fd: %d\n", errorStr.c_str(), user->GetSocket());
493 if (res < sizeof(buffer))
495 errorStr = "Packet sent partially";
496 printfd(__FILE__, "PROTO::SendPacket() - %s\n", errorStr.c_str());
503 bool PROTO::RealConnect(USER * user)
505 if (user->GetPhase() != 1 &&
506 user->GetPhase() != 5)
508 errorStr = "Unexpected connect";
509 printfd(__FILE__, "PROTO::RealConnect() - wrong phase: %d\n", user->GetPhase());
513 return Send_CONN_SYN(user);
516 bool PROTO::RealDisconnect(USER * user)
518 if (user->GetPhase() != 3)
520 errorStr = "Unexpected disconnect";
521 printfd(__FILE__, "PROTO::RealDisconnect() - wrong phase: %d\n", user->GetPhase());
525 return Send_DISCONN_SYN(user);