9 #include "stg/common.h"
10 #include "stg/ia_packets.h"
14 PROTO::PROTO(const std::string & server,
22 uint32_t ip = inet_addr(server.c_str());
23 if (ip == INADDR_NONE)
25 struct hostent * hePtr = gethostbyname(server.c_str());
28 ip = *((uint32_t *)hePtr->h_addr_list[0]);
32 errorStr = "Unknown host: '";
35 printfd(__FILE__, "PROTO::PROTO() - %s\n", errorStr.c_str());
36 throw std::runtime_error(errorStr);
40 localAddr.sin_family = AF_INET;
41 localAddr.sin_port = htons(localPort);
42 localAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
44 serverAddr.sin_family = AF_INET;
45 serverAddr.sin_port = htons(port);
46 serverAddr.sin_addr.s_addr = ip;
48 unsigned char key[IA_PASSWD_LEN];
49 memset(key, 0, IA_PASSWD_LEN);
50 strncpy(reinterpret_cast<char *>(key), "pr7Hhen", 8);
51 Blowfish_Init(&ctx, key, IA_PASSWD_LEN);
53 processors["CONN_SYN_ACK"] = &PROTO::CONN_SYN_ACK_Proc;
54 processors["ALIVE_SYN"] = &PROTO::ALIVE_SYN_Proc;
55 processors["DISCONN_SYN_ACK"] = &PROTO::DISCONN_SYN_ACK_Proc;
56 processors["FIN"] = &PROTO::FIN_Proc;
57 processors["INFO"] = &PROTO::INFO_Proc;
58 // ERR_Proc will be handled explicitly
65 void * PROTO::Runner(void * data)
67 PROTO * protoPtr = static_cast<PROTO *>(data);
75 if (pthread_create(&tid, NULL, &Runner, NULL))
77 errorStr = "Failed to create listening thread: '";
78 errorStr += strerror(errno);
80 printfd(__FILE__, "PROTO::Start() - %s\n", errorStr.c_str());
90 while (!stopped && time < timeout)
92 struct timespec ts = {1, 0};
98 errorStr = "Failed to stop listening thread - timed out";
99 printfd(__FILE__, "PROTO::Stop() - %s\n", errorStr.c_str());
102 if (pthread_join(tid, NULL))
104 errorStr = "Failed to join listening thread after stop: '";
105 errorStr += strerror(errno);
107 printfd(__FILE__, "PROTO::Stop() - %s\n", errorStr.c_str());
113 void PROTO::AddUser(const USER & user)
115 users.push_back(std::make_pair(user.GetIP(), user));
117 pfd.fd = user.GetSocket();
120 pollFds.push_back(pfd);
123 bool PROTO::Connect(uint32_t ip)
125 /*std::vector<std::pair<uint32_t, USER> >::const_iterator it;
127 if (it == users.end())
135 bool PROTO::Disconnect(uint32_t ip)
137 /*std::vector<std::pair<uint32_t, USER> >::const_iterator it;
139 if (it == users.end())
151 int res = poll(&pollFds.front(), pollFds.size(), timeout);
163 bool PROTO::RecvPacket()
166 std::vector<struct pollfd>::iterator it;
167 std::vector<std::pair<uint32_t, USER> >::iterator userIt;
168 for (it = pollFds.begin(), userIt = users.begin(); it != pollFds.end() && userIt != users.end(); ++it, ++userIt)
173 assert(it->fd == userIt->second.GetSocket() && "File descriptors from poll fds and users must be syncked");
174 struct sockaddr_in addr;
175 socklen_t fromLen = sizeof(addr);
177 int res = recvfrom(userIt->second.GetSocket(), buffer, sizeof(buffer), 0, (struct sockaddr *)&addr, &fromLen);
185 result = result && HandlePacket(buffer, &(userIt->second));
192 bool PROTO::HandlePacket(const char * buffer, USER * user)
194 if (strcmp(buffer + 4 + sizeof(HDR_8), "ERR"))
196 return ERR_Proc(buffer, user);
199 std::string packetName(buffer + 12);
200 std::map<std::string, PacketProcessor>::const_iterator it;
201 it = processors.find(packetName);
202 if (it != processors.end())
203 return (this->*it->second)(buffer, user);
208 bool PROTO::CONN_SYN_ACK_Proc(const void * buffer, USER * user)
210 const CONN_SYN_ACK_8 * packet = static_cast<const CONN_SYN_ACK_8 *>(buffer);
212 uint32_t rnd = packet->rnd;
213 uint32_t userTimeout = packet->userTimeOut;
214 uint32_t aliveTimeout = packet->aliveDelay;
218 SwapBytes(userTimeout);
219 SwapBytes(aliveDelay);
224 if (user->GetPhase() != 2)
226 errorStr = "Unexpected CONN_SYN_ACK";
227 printfd(__FILE__, "PROTO::CONN_SYN_ACK_Proc() - wrong phase: %d\n", user->GetPhase());
231 user->SetAliveTimeout(aliveTimeout);
232 user->SetUserTimeout(userTimeout);
238 bool PROTO::ALIVE_SYN_Proc(const void * buffer, USER * user)
240 const ALIVE_SYN_8 * packet = static_cast<const ALIVE_SYN_8 *>(buffer);
242 uint32_t rnd = packet->rnd;
248 if (user->GetPhase() != 3)
250 errorStr = "Unexpected ALIVE_SYN";
251 printfd(__FILE__, "PROTO::ALIVE_SYN_Proc() - wrong phase: %d\n", user->GetPhase());
254 if (user->GetRnd() + 1 != rnd)
256 errorStr = "Wrong control value at ALIVE_SYN";
257 printfd(__FILE__, "PROTO::ALIVE_SYN_Proc() - wrong control value: %d, expected: %d\n", rnd, user->GetRnd() + 1);
263 Send_ALIVE_ACK(user);
268 bool PROTO::DISCONN_SYN_ACK_Proc(const void * buffer, USER * user)
270 const DISCONN_SYN_ACK_8 * packet = static_cast<const DISCONN_SYN_ACK_8 *>(buffer);
272 uint32_t rnd = packet->rnd;
278 if (user->GetPhase() != 4)
280 errorStr = "Unexpected DISCONN_SYN_ACK";
281 printfd(__FILE__, "PROTO::DISCONN_SYN_ACK_Proc() - wrong phase: %d\n", user->GetPhase());
284 if (user->GetRnd() + 1 != rnd)
286 errorStr = "Wrong control value at DISCONN_SYN_ACK";
287 printfd(__FILE__, "PROTO::DISCONN_SYN_ACK_Proc() - wrong control value: %d, expected: %d\n", rnd, user->GetRnd() + 1);
293 Send_DISCONN_ACK(user);
298 bool PROTO::FIN_Proc(const void * buffer, USER * user)
300 if (user->GetPhase() != 5)
302 errorStr = "Unexpected FIN";
303 printfd(__FILE__, "PROTO::FIN_Proc() - wrong phase: %d\n", user->GetPhase());
311 bool PROTO::INFO_Proc(const void * buffer, USER * user)
313 //const INFO_8 * packet = static_cast<const INFO_8 *>(buffer);
318 bool PROTO::ERR_Proc(const void * buffer, USER * user)
320 const ERR_8 * packet = static_cast<const ERR_8 *>(buffer);
321 const char * ptr = static_cast<const char *>(buffer);
323 for (int i = 0; i < sizeof(packet) / 8; i++)
324 Blowfish_Decrypt(user->GetCtx(), (uint32_t *)(ptr + i * 8), (uint32_t *)(ptr + i * 8 + 4));
326 //uint32_t len = packet->len;
332 user->SetPhase(1); //TODO: Check
333 /*KOIToWin((const char*)err.text, &messageText);
334 if (pErrorCb != NULL)
335 pErrorCb(messageText, IA_SERVER_ERROR, errorCbData);
336 phaseTime = GetTickCount();
337 codeError = IA_SERVER_ERROR;*/
342 bool PROTO::Send_CONN_SYN(USER * user)
346 packet.len = sizeof(packet);
349 SwapBytes(packet.len);
352 strncpy((char *)packet.type, "CONN_SYN", sizeof(packet.type));
353 strncpy((char *)packet.login, user->GetLogin().c_str(), sizeof(packet.login));
354 packet.dirs = 0xFFffFFff;
356 return SendPacket(&packet, sizeof(packet), user);
359 bool PROTO::Send_CONN_ACK(USER * user)
363 packet.len = sizeof(packet);
364 packet.rnd = user->IncRnd();
367 SwapBytes(packet.len);
368 SwapBytes(packet.rnd);
371 strncpy((char *)packet.loginS, user->GetLogin().c_str(), sizeof(packet.loginS));
372 strncpy((char *)packet.type, "CONN_ACK", sizeof(packet.type));
374 return SendPacket(&packet, sizeof(packet), user);
377 bool PROTO::Send_ALIVE_ACK(USER * user)
381 packet.len = sizeof(packet);
382 packet.rnd = user->IncRnd();
385 SwapBytes(packet.len);
386 SwapBytes(packet.rnd);
389 strncpy((char *)packet.loginS, user->GetLogin().c_str(), sizeof(packet.loginS));
390 strncpy((char *)packet.type, "ALIVE_ACK", sizeof(packet.type));
392 return SendPacket(&packet, sizeof(packet), user);
395 bool PROTO::Send_DISCONN_SYN(USER * user)
397 DISCONN_SYN_8 packet;
399 packet.len = sizeof(packet);
402 SwapBytes(packet.len);
405 strncpy((char *)packet.loginS, user->GetLogin().c_str(), sizeof(packet.loginS));
406 strncpy((char *)packet.type, "DISCONN_SYN", sizeof(packet.type));
407 strncpy((char *)packet.login, user->GetLogin().c_str(), sizeof(packet.login));
409 return SendPacket(&packet, sizeof(packet), user);
412 bool PROTO::Send_DISCONN_ACK(USER * user)
414 DISCONN_ACK_8 packet;
416 packet.len = sizeof(packet);
417 packet.rnd = user->IncRnd();
420 SwapBytes(packet.len);
421 SwapBytes(packet.rnd);
424 strncpy((char *)packet.loginS, user->GetLogin().c_str(), sizeof(packet.loginS));
425 strncpy((char *)packet.type, "DISCONN_ACK", sizeof(packet.type));
427 return SendPacket(&packet, sizeof(packet), user);
430 bool PROTO::SendPacket(const void * packet, size_t length, USER * user)
434 assert(sizeof(hdr) + length < 2048 && "Packet length must not exceed 2048 bytes");
436 strncpy((char *)hdr.magic, IA_ID, 6);
438 hdr.protoVer[1] = 8; // IA_PROTO_VER
440 unsigned char buffer[2048];
441 memcpy(buffer, &hdr, sizeof(hdr));
442 memcpy(buffer + sizeof(hdr), packet, length);
444 size_t offset = sizeof(HDR_8);
445 for (size_t i = 0; i < IA_LOGIN_LEN / 8; i++)
447 Blowfish_Encrypt(&ctx,
448 (uint32_t *)(buffer + offset + i * 8),
449 (uint32_t *)(buffer + offset + i * 8 + 4));
452 offset += IA_LOGIN_LEN;
453 size_t encLen = (length - IA_LOGIN_LEN) / 8;
454 for (size_t i = 0; i < encLen; i++)
456 Blowfish_Encrypt(user->GetCtx(),
457 (uint32_t*)(buffer + offset + i * 8),
458 (uint32_t*)(buffer + offset + i * 8 + 4));
461 int res = sendto(user->GetSocket(), buffer, sizeof(buffer), 0, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
465 errorStr = "Failed to send packet: '";
466 errorStr += strerror(errno);
468 printfd(__FILE__, "PROTO::SendPacket() - %s\n", errorStr.c_str());
472 if (res < sizeof(buffer))
474 errorStr = "Packet sent partially";
475 printfd(__FILE__, "PROTO::SendPacket() - %s\n", errorStr.c_str());