]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp
Dead code removed
[stg.git] / projects / stargazer / plugins / authorization / inetaccess / inetaccess.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 /*
22  $Revision: 1.79 $
23  $Date: 2010/03/25 15:18:48 $
24  $Author: faust $
25  */
26  
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE
29 #endif
30
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <unistd.h> // usleep, close
34
35 #include <csignal>
36 #include <cstdlib>
37 #include <cstdio> // snprintf
38 #include <cerrno>
39 #include <algorithm>
40
41 #include "stg/common.h"
42 #include "stg/locker.h"
43 #include "stg/tariff.h"
44 #include "stg/user_property.h"
45 #include "stg/settings.h"
46 #include "stg/plugin_creator.h"
47 #include "inetaccess.h"
48
49 extern volatile const time_t stgTime;
50
51 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password);
52 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
53 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
54
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 PLUGIN_CREATOR<AUTH_IA> iac;
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 PLUGIN * GetPlugin()
63 {
64 return iac.GetPlugin();
65 }
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
69 AUTH_IA_SETTINGS::AUTH_IA_SETTINGS()
70     : userDelay(0),
71       userTimeout(0),
72       port(0),
73       freeMbShowType(freeMbCash)
74 {
75 }
76 //-----------------------------------------------------------------------------
77 int AUTH_IA_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
78 {
79 int p;
80 PARAM_VALUE pv;
81 vector<PARAM_VALUE>::const_iterator pvi;
82 ///////////////////////////
83 pv.param = "Port";
84 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
85 if (pvi == s.moduleParams.end())
86     {
87     errorStr = "Parameter \'Port\' not found.";
88     printfd(__FILE__, "Parameter 'Port' not found\n");
89     return -1;
90     }
91 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
92     {
93     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
94     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
95     return -1;
96     }
97 port = p;
98 ///////////////////////////
99 pv.param = "UserDelay";
100 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
101 if (pvi == s.moduleParams.end())
102     {
103     errorStr = "Parameter \'UserDelay\' not found.";
104     printfd(__FILE__, "Parameter 'UserDelay' not found\n");
105     return -1;
106     }
107
108 if (ParseIntInRange(pvi->value[0], 5, 600, &userDelay))
109     {
110     errorStr = "Cannot parse parameter \'UserDelay\': " + errorStr;
111     printfd(__FILE__, "Cannot parse parameter 'UserDelay'\n");
112     return -1;
113     }
114 ///////////////////////////
115 pv.param = "UserTimeout";
116 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
117 if (pvi == s.moduleParams.end())
118     {
119     errorStr = "Parameter \'UserTimeout\' not found.";
120     printfd(__FILE__, "Parameter 'UserTimeout' not found\n");
121     return -1;
122     }
123
124 if (ParseIntInRange(pvi->value[0], 15, 1200, &userTimeout))
125     {
126     errorStr = "Cannot parse parameter \'UserTimeout\': " + errorStr;
127     printfd(__FILE__, "Cannot parse parameter 'UserTimeout'\n");
128     return -1;
129     }
130 /////////////////////////////////////////////////////////////
131 string freeMbType;
132 int n = 0;
133 pv.param = "FreeMb";
134 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
135 if (pvi == s.moduleParams.end())
136     {
137     errorStr = "Parameter \'FreeMb\' not found.";
138     printfd(__FILE__, "Parameter 'FreeMb' not found\n");
139     return -1;
140     }
141 freeMbType = pvi->value[0];
142
143 if (strcasecmp(freeMbType.c_str(), "cash") == 0)
144     {
145     freeMbShowType = freeMbCash;
146     }
147 else if (strcasecmp(freeMbType.c_str(), "none") == 0)
148     {
149     freeMbShowType = freeMbNone;
150     }
151 else if (!str2x(freeMbType.c_str(), n))
152     {
153     if (n < 0 || n >= DIR_NUM)
154         {
155         errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
156         printfd(__FILE__, "%s\n", errorStr.c_str());
157         return -1;
158         }
159     freeMbShowType = (FREEMB)(freeMb0 + n);
160     }
161 else
162     {
163     errorStr = "Incorrect parameter \'" + freeMbType + "\'.";
164     printfd(__FILE__, "%s\n", errorStr.c_str());
165     return -1;
166     }
167 /////////////////////////////////////////////////////////////
168 return 0;
169 }
170 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
173 #ifdef IA_PHASE_DEBUG
174 IA_PHASE::IA_PHASE()
175     : phase(1),
176       flog(NULL)
177 {
178 gettimeofday(&phaseTime, NULL);
179 }
180 #else
181 IA_PHASE::IA_PHASE()
182     : phase(1)
183 {
184 gettimeofday(&phaseTime, NULL);
185 }
186 #endif
187 //-----------------------------------------------------------------------------
188 IA_PHASE::~IA_PHASE()
189 {
190 #ifdef IA_PHASE_DEBUG
191 flog = fopen(log.c_str(), "at");
192 if (flog)
193     {
194     fprintf(flog, "IA %s D\n", login.c_str());
195     fclose(flog);
196     }
197 #endif
198 }
199 //-----------------------------------------------------------------------------
200 #ifdef IA_PHASE_DEBUG
201 void IA_PHASE::SetLogFileName(const string & logFileName)
202 {
203 log = logFileName + ".ia.log";
204 }
205 //-----------------------------------------------------------------------------
206 void IA_PHASE::SetUserLogin(const string & login)
207 {
208 IA_PHASE::login = login;
209 }
210 //-----------------------------------------------------------------------------
211 void IA_PHASE::WritePhaseChange(int newPhase)
212 {
213 UTIME newPhaseTime;
214 gettimeofday(&newPhaseTime, NULL);
215 flog = fopen(log.c_str(), "at");
216 /*int64_t tn = newPhaseTime.GetSec()*1000000 + newPhaseTime.GetUSec();
217 int64_t to = phaseTime.GetSec()*1000000 + phaseTime.GetUSec();*/
218 if (flog)
219     {
220     string action = newPhase == phase ? "U" : "C";
221     double delta = newPhaseTime.GetSec() - phaseTime.GetSec();
222     delta += (newPhaseTime.GetUSec() - phaseTime.GetUSec()) * 1.0e-6;
223     fprintf(flog, "IA %s %s oldPhase = %d, newPhase = %d. dt = %.6f\n",
224             login.c_str(),
225             action.c_str(),
226             phase,
227             newPhase,
228             delta);
229     fclose(flog);
230     }
231 }
232 #endif
233 //-----------------------------------------------------------------------------
234 void IA_PHASE::SetPhase1()
235 {
236 #ifdef IA_PHASE_DEBUG
237 WritePhaseChange(1);
238 #endif
239 phase = 1;
240 gettimeofday(&phaseTime, NULL);
241 }
242 //-----------------------------------------------------------------------------
243 void IA_PHASE::SetPhase2()
244 {
245 #ifdef IA_PHASE_DEBUG
246 WritePhaseChange(2);
247 #endif
248 phase = 2;
249 gettimeofday(&phaseTime, NULL);
250 }
251 //-----------------------------------------------------------------------------
252 void IA_PHASE::SetPhase3()
253 {
254 #ifdef IA_PHASE_DEBUG
255 WritePhaseChange(3);
256 #endif
257 phase = 3;
258 gettimeofday(&phaseTime, NULL);
259 }
260 //-----------------------------------------------------------------------------
261 void IA_PHASE::SetPhase4()
262 {
263 #ifdef IA_PHASE_DEBUG
264 WritePhaseChange(4);
265 #endif
266 phase = 4;
267 gettimeofday(&phaseTime, NULL);
268 }
269 //-----------------------------------------------------------------------------
270 void IA_PHASE::SetPhase5()
271 {
272 #ifdef IA_PHASE_DEBUG
273 WritePhaseChange(5);
274 #endif
275 phase = 5;
276 gettimeofday(&phaseTime, NULL);
277 }
278 //-----------------------------------------------------------------------------
279 int IA_PHASE::GetPhase() const
280 {
281 return phase;
282 }
283 //-----------------------------------------------------------------------------
284 void IA_PHASE::UpdateTime()
285 {
286 #ifdef IA_PHASE_DEBUG
287 WritePhaseChange(phase);
288 #endif
289 gettimeofday(&phaseTime, NULL);
290 }
291 //-----------------------------------------------------------------------------
292 const UTIME & IA_PHASE::GetTime() const
293 {
294 return phaseTime;
295 }
296 //-----------------------------------------------------------------------------
297 //-----------------------------------------------------------------------------
298 //-----------------------------------------------------------------------------
299 AUTH_IA::AUTH_IA()
300     : nonstop(false),
301       isRunningRun(false),
302       isRunningRunTimeouter(false),
303       users(NULL),
304       stgSettings(NULL),
305       listenSocket(-1),
306       WriteServLog(GetStgLogger()),
307       enabledDirs(0xFFffFFff),
308       onDelUserNotifier(*this)
309 {
310 InitEncrypt(&ctxS, "pr7Hhen");
311
312 pthread_mutexattr_t attr;
313 pthread_mutexattr_init(&attr);
314 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
315 pthread_mutex_init(&mutex, &attr);
316
317 memset(&connSynAck6, 0, sizeof(CONN_SYN_ACK_6));
318 memset(&connSynAck8, 0, sizeof(CONN_SYN_ACK_8));
319 memset(&disconnSynAck6, 0, sizeof(DISCONN_SYN_ACK_6));
320 memset(&disconnSynAck8, 0, sizeof(DISCONN_SYN_ACK_8));
321 memset(&aliveSyn6, 0, sizeof(ALIVE_SYN_6));
322 memset(&aliveSyn8, 0, sizeof(ALIVE_SYN_8));
323 memset(&fin6, 0, sizeof(FIN_6));
324 memset(&fin8, 0, sizeof(FIN_8));
325
326 printfd(__FILE__, "sizeof(CONN_SYN_6) = %d %d\n",           sizeof(CONN_SYN_6),     Min8(sizeof(CONN_SYN_6)));
327 printfd(__FILE__, "sizeof(CONN_SYN_8) = %d %d\n",           sizeof(CONN_SYN_8),     Min8(sizeof(CONN_SYN_8)));
328 printfd(__FILE__, "sizeof(CONN_SYN_ACK_6) = %d %d\n",       sizeof(CONN_SYN_ACK_6), Min8(sizeof(CONN_SYN_ACK_6)));
329 printfd(__FILE__, "sizeof(CONN_SYN_ACK_8) = %d %d\n",       sizeof(CONN_SYN_ACK_8), Min8(sizeof(CONN_SYN_ACK_8)));
330 printfd(__FILE__, "sizeof(CONN_ACK_6) = %d %d\n",           sizeof(CONN_ACK_6),     Min8(sizeof(CONN_ACK_6)));
331 printfd(__FILE__, "sizeof(ALIVE_SYN_6) = %d %d\n",          sizeof(ALIVE_SYN_6),    Min8(sizeof(ALIVE_SYN_6)));
332 printfd(__FILE__, "sizeof(ALIVE_SYN_8) = %d %d\n",          sizeof(ALIVE_SYN_8),    Min8(sizeof(ALIVE_SYN_8)));
333 printfd(__FILE__, "sizeof(ALIVE_ACK_6) = %d %d\n",          sizeof(ALIVE_ACK_6),    Min8(sizeof(ALIVE_ACK_6)));
334 printfd(__FILE__, "sizeof(DISCONN_SYN_6) = %d %d\n",        sizeof(DISCONN_SYN_6),  Min8(sizeof(DISCONN_SYN_6)));
335 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_6) = %d %d\n",    sizeof(DISCONN_SYN_ACK_6), Min8(sizeof(DISCONN_SYN_ACK_6)));
336 printfd(__FILE__, "sizeof(DISCONN_SYN_ACK_8) = %d %d\n",    sizeof(DISCONN_SYN_ACK_8), Min8(sizeof(DISCONN_SYN_ACK_8)));
337 printfd(__FILE__, "sizeof(DISCONN_ACK_6) = %d %d\n",        sizeof(DISCONN_ACK_6),  Min8(sizeof(DISCONN_ACK_6)));
338 printfd(__FILE__, "sizeof(FIN_6) = %d %d\n",                sizeof(FIN_6),          Min8(sizeof(FIN_6)));
339 printfd(__FILE__, "sizeof(FIN_8) = %d %d\n",                sizeof(FIN_8),          Min8(sizeof(FIN_8)));
340 printfd(__FILE__, "sizeof(ERR) = %d %d\n",                  sizeof(ERR),            Min8(sizeof(ERR)));
341 printfd(__FILE__, "sizeof(INFO_6) = %d %d\n",               sizeof(INFO_6),         Min8(sizeof(INFO_6)));
342 printfd(__FILE__, "sizeof(INFO_7) = %d %d\n",               sizeof(INFO_7),         Min8(sizeof(INFO_7)));
343 printfd(__FILE__, "sizeof(INFO_8) = %d %d\n",               sizeof(INFO_8),         Min8(sizeof(INFO_8)));
344
345 packetTypes["CONN_SYN"] = CONN_SYN_N;
346 packetTypes["CONN_SYN_ACK"] = CONN_SYN_ACK_N;
347 packetTypes["CONN_ACK"] = CONN_ACK_N;
348 packetTypes["ALIVE_SYN"] = ALIVE_SYN_N;
349 packetTypes["ALIVE_ACK"] = ALIVE_ACK_N;
350 packetTypes["DISCONN_SYN"] = DISCONN_SYN_N;
351 packetTypes["DISCONN_SYN_ACK"] = DISCONN_SYN_ACK_N;
352 packetTypes["DISCONN_ACK"] = DISCONN_ACK_N;
353 packetTypes["FIN"] = FIN_N;
354 packetTypes["ERR"] = ERROR_N;
355 }
356 //-----------------------------------------------------------------------------
357 AUTH_IA::~AUTH_IA()
358 {
359 pthread_mutex_destroy(&mutex);
360 }
361 //-----------------------------------------------------------------------------
362 int AUTH_IA::Start()
363 {
364 users->AddNotifierUserDel(&onDelUserNotifier);
365 nonstop = true;
366
367 if (PrepareNet())
368     {
369     return -1;
370     }
371
372 if (!isRunningRun)
373     {
374     if (pthread_create(&recvThread, NULL, Run, this))
375         {
376         errorStr = "Cannot create thread.";
377         printfd(__FILE__, "Cannot create recv thread\n");
378         return -1;
379         }
380     }
381
382 if (!isRunningRunTimeouter)
383     {
384     if (pthread_create(&timeouterThread, NULL, RunTimeouter, this))
385         {
386         errorStr = "Cannot create thread.";
387         printfd(__FILE__, "Cannot create timeouter thread\n");
388         return -1;
389         }
390     }
391 errorStr = "";
392 return 0;
393 }
394 //-----------------------------------------------------------------------------
395 int AUTH_IA::Stop()
396 {
397 if (!IsRunning())
398     return 0;
399
400 nonstop = false;
401
402 std::for_each(
403         ip2user.begin(),
404         ip2user.end(),
405         UnauthorizeUser(this)
406         );
407
408 if (isRunningRun)
409     {
410     //5 seconds to thread stops itself
411     for (int i = 0; i < 25 && isRunningRun; i++)
412         {
413         usleep(200000);
414         }
415
416     //after 5 seconds waiting thread still running. now killing it
417     if (isRunningRun)
418         {
419         //TODO pthread_cancel()
420         if (pthread_kill(recvThread, SIGINT))
421             {
422             errorStr = "Cannot kill thread.";
423             printfd(__FILE__, "Cannot kill thread\n");
424             return -1;
425             }
426         for (int i = 0; i < 25 && isRunningRun; ++i)
427             usleep(200000);
428         if (isRunningRun)
429             {
430             printfd(__FILE__, "Failed to stop recv thread\n");
431             }
432         else
433             {
434             pthread_join(recvThread, NULL);
435             }
436         printfd(__FILE__, "AUTH_IA killed Run\n");
437         }
438     }
439
440 FinalizeNet();
441
442 if (isRunningRunTimeouter)
443     {
444     //5 seconds to thread stops itself
445     for (int i = 0; i < 25 && isRunningRunTimeouter; i++)
446         {
447         usleep(200000);
448         }
449
450     //after 5 seconds waiting thread still running. now killing it
451     if (isRunningRunTimeouter)
452         {
453         //TODO pthread_cancel()
454         if (pthread_kill(timeouterThread, SIGINT))
455             {
456             errorStr = "Cannot kill thread.";
457             return -1;
458             }
459         for (int i = 0; i < 25 && isRunningRunTimeouter; ++i)
460             usleep(200000);
461         if (isRunningRunTimeouter)
462             {
463             printfd(__FILE__, "Failed to stop timeouter thread\n");
464             }
465         else
466             {
467             pthread_join(timeouterThread, NULL);
468             }
469         printfd(__FILE__, "AUTH_IA killed Timeouter\n");
470         }
471     }
472 printfd(__FILE__, "AUTH_IA::Stoped successfully.\n");
473 users->DelNotifierUserDel(&onDelUserNotifier);
474 return 0;
475 }
476 //-----------------------------------------------------------------------------
477 void * AUTH_IA::Run(void * d)
478 {
479 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
480
481 ia->isRunningRun = true;
482
483 char buffer[512];
484
485 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
486
487 while (ia->nonstop)
488     {
489     ia->RecvData(buffer, sizeof(buffer));
490     if ((touchTime + MONITOR_TIME_DELAY_SEC <= stgTime) && ia->stgSettings->GetMonitoring())
491         {
492         touchTime = stgTime;
493         string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_r";
494         TouchFile(monFile.c_str());
495         }
496     }
497
498 ia->isRunningRun = false;
499 return NULL;
500 }
501 //-----------------------------------------------------------------------------
502 void * AUTH_IA::RunTimeouter(void * d)
503 {
504 AUTH_IA * ia = static_cast<AUTH_IA *>(d);
505
506 ia->isRunningRunTimeouter = true;
507
508 int a = -1;
509 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t";
510 while (ia->nonstop)
511     {
512     usleep(20000);
513     ia->Timeouter();
514     // TODO change counter to timer and MONITOR_TIME_DELAY_SEC
515     if (++a % (50 * 60) == 0 && ia->stgSettings->GetMonitoring())
516         {
517         TouchFile(monFile.c_str());
518         }
519     }
520
521 ia->isRunningRunTimeouter = false;
522 return NULL;
523 }
524 //-----------------------------------------------------------------------------
525 int AUTH_IA::ParseSettings()
526 {
527 int ret = iaSettings.ParseSettings(settings);
528 if (ret)
529     errorStr = iaSettings.GetStrError();
530 return ret;
531 }
532 //-----------------------------------------------------------------------------
533 int AUTH_IA::PrepareNet()
534 {
535 struct sockaddr_in listenAddr;
536
537 listenSocket = socket(AF_INET, SOCK_DGRAM, 0);
538
539 if (listenSocket < 0)
540     {
541     errorStr = "Cannot create socket.";
542     return -1;
543     }
544
545 listenAddr.sin_family = AF_INET;
546 listenAddr.sin_port = htons(iaSettings.GetUserPort());
547 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
548
549 if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) < 0)
550     {
551     errorStr = "AUTH_IA: Bind failed.";
552     return -1;
553     }
554
555 return 0;
556 }
557 //-----------------------------------------------------------------------------
558 int AUTH_IA::FinalizeNet()
559 {
560 close(listenSocket);
561 return 0;
562 }
563 //-----------------------------------------------------------------------------
564 int AUTH_IA::RecvData(char * buffer, int bufferSize)
565 {
566 if (!WaitPackets(listenSocket)) // Timeout
567     {
568     return 0;
569     }
570
571 struct sockaddr_in outerAddr;
572 socklen_t outerAddrLen(sizeof(outerAddr));
573 int dataLen = recvfrom(listenSocket, buffer, bufferSize, 0, (struct sockaddr *)&outerAddr, &outerAddrLen);
574
575 if (!dataLen) // EOF
576     {
577     return 0;
578     }
579
580 if (dataLen <= 0) // Error
581     {
582     if (errno != EINTR)
583         {
584         printfd(__FILE__, "recvfrom res=%d, error: '%s'\n", dataLen, strerror(errno));
585         return -1;
586         }
587     return 0;
588     }
589
590 if (dataLen > 256)
591     return -1;
592
593 int protoVer;
594 if (CheckHeader(buffer, &protoVer))
595     return -1;
596
597 char login[PASSWD_LEN];  //TODO why PASSWD_LEN ?
598 memset(login, 0, PASSWD_LEN);
599
600 Decrypt(&ctxS, login, buffer + 8, PASSWD_LEN / 8);
601
602 uint32_t sip = *((uint32_t*)&outerAddr.sin_addr);
603 uint16_t sport = htons(outerAddr.sin_port);
604
605 USER_PTR user;
606 if (users->FindByName(login, &user) == 0)
607     {
608     printfd(__FILE__, "User %s FOUND!\n", user->GetLogin().c_str());
609     PacketProcessor(buffer, dataLen, sip, sport, protoVer, user);
610     }
611 else
612     {
613     WriteServLog("User\'s connect failed:: user \'%s\' not found. IP \'%s\'",
614                  login,
615                  inet_ntostring(sip).c_str());
616     printfd(__FILE__, "User %s NOT found!\n", login);
617     SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉΠÉÌÉ ÐÁÒÏÌØ!");
618     }
619
620 return 0;
621
622 }
623 //-----------------------------------------------------------------------------
624 int AUTH_IA::CheckHeader(const char * buffer, int * protoVer)
625 {
626 if (strncmp(IA_ID, buffer, strlen(IA_ID)) != 0)
627     {
628     //SendError(userIP, updateMsg);
629     printfd(__FILE__, "update needed - IA_ID\n");
630     //SendError(userIP, "Incorrect header!");
631     return -1;
632     }
633
634 if (buffer[6] != 0) //proto[0] shoud be 0
635     {
636     printfd(__FILE__, "update needed - PROTO major: %d\n", buffer[6]);
637     //SendError(userIP, updateMsg);
638     return -1;
639     }
640
641 if (buffer[7] < 6)
642     {
643     // need update
644     //SendError(userIP, updateMsg);
645     printfd(__FILE__, "update needed - PROTO minor: %d\n", buffer[7]);
646     return -1;
647     }
648 else
649     {
650     *protoVer = buffer[7];
651     }
652 return 0;
653 }
654 //-----------------------------------------------------------------------------
655 int AUTH_IA::Timeouter()
656 {
657 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
658
659 map<uint32_t, IA_USER>::iterator it;
660 it = ip2user.begin();
661 uint32_t sip;
662
663 while (it != ip2user.end())
664     {
665     sip = it->first;
666
667     static UTIME currTime;
668     gettimeofday(&currTime, NULL);
669
670     if ((it->second.phase.GetPhase() == 2)
671         && (currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay())
672         {
673         it->second.phase.SetPhase1();
674         printfd(__FILE__, "Phase changed from 2 to 1. Reason: timeout\n");
675         }
676
677     if (it->second.phase.GetPhase() == 3)
678         {
679         if (!it->second.messagesToSend.empty())
680             {
681             if (it->second.protoVer == 6)
682                 RealSendMessage6(*it->second.messagesToSend.begin(), sip, it->second);
683
684             if (it->second.protoVer == 7)
685                 RealSendMessage7(*it->second.messagesToSend.begin(), sip, it->second);
686
687             if (it->second.protoVer == 8)
688                 RealSendMessage8(*it->second.messagesToSend.begin(), sip, it->second);
689
690             it->second.messagesToSend.erase(it->second.messagesToSend.begin());
691             }
692
693         if((currTime - it->second.lastSendAlive) > iaSettings.GetUserDelay())
694             {
695             switch (it->second.protoVer)
696                 {
697                 case 6:
698                     Send_ALIVE_SYN_6(&(it->second), sip);
699                     break;
700                 case 7:
701                     Send_ALIVE_SYN_7(&(it->second), sip);
702                     break;
703                 case 8:
704                     Send_ALIVE_SYN_8(&(it->second), sip);
705                     break;
706                 }
707
708             gettimeofday(&it->second.lastSendAlive, NULL);
709             }
710
711         if ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserTimeout())
712             {
713             users->Unauthorize(it->second.user->GetLogin(), this);
714             ip2user.erase(it++);
715             continue;
716             }
717         }
718
719     if ((it->second.phase.GetPhase() == 4)
720         && ((currTime - it->second.phase.GetTime()) > iaSettings.GetUserDelay()))
721         {
722         it->second.phase.SetPhase3();
723         printfd(__FILE__, "Phase changed from 4 to 3. Reason: timeout\n");
724         }
725
726     ++it;
727     }
728
729 return 0;
730 }
731 //-----------------------------------------------------------------------------
732 int AUTH_IA::PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR user)
733 {
734 std::string login(user->GetLogin());
735 // ôÕÔ ÓÏÂÒÁÎÙ ÏÂÒÁÂÏÔÞÉËÉ ÒÁÚÎÙÈ ÐÁËÅÔÏ×
736 const int offset = LOGIN_LEN + 2 + 6; // LOGIN_LEN + sizeOfMagic + sizeOfVer;
737
738 IA_USER * iaUser = NULL;
739
740 CONN_SYN_6 * connSyn6;
741 CONN_SYN_7 * connSyn7;
742 CONN_SYN_8 * connSyn8;
743
744 CONN_ACK_6 * connAck;
745 ALIVE_ACK_6 * aliveAck;
746 DISCONN_SYN_6 * disconnSyn;
747 DISCONN_ACK_6 * disconnAck;
748
749 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
750 map<uint32_t, IA_USER>::iterator it(ip2user.find(sip));
751
752 if (it == ip2user.end())
753     {
754     // åÝÅ ÎÅ ÂÙÌÏ ÚÁÐÒÏÓÏ× Ó ÜÔÏÇÏ IP
755     printfd(__FILE__, "Add new user\n");
756     ip2user[sip].login = user->GetLogin();
757     ip2user[sip].user = user;
758     ip2user[sip].protoVer = protoVer;
759     ip2user[sip].port = sport;
760     #ifdef IA_PHASE_DEBUG
761     ip2user[sip].phase.SetLogFileName(stgSettings->GetLogFileName());
762     ip2user[sip].phase.SetUserLogin(login);
763     #endif
764
765
766     it = ip2user.find(sip); //TODO
767     if (it == ip2user.end())
768         {
769         printfd(__FILE__, "+++ ERROR +++\n");
770         return -1;
771         }
772     }
773 else if (user->GetID() != it->second.user->GetID())
774     {
775     printfd(__FILE__, "IP address already in use. IP \'%s\'\n", inet_ntostring(sip).c_str());
776     WriteServLog("IP address already in use. IP \'%s\'", inet_ntostring(sip).c_str());
777     SendError(sip, sport, protoVer, "÷ÁÛ IP ÁÄÒÅÓ ÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
778     return 0;
779     }
780
781 iaUser = &(it->second);
782
783 if (iaUser->port != sport)
784     iaUser->port = sport;
785
786 if (iaUser->password != user->GetProperty().password.Get())
787     {
788     InitEncrypt(&iaUser->ctx, user->GetProperty().password.Get());
789     iaUser->password = user->GetProperty().password.Get();
790     }
791
792 buff += offset;
793 Decrypt(&iaUser->ctx, buff, buff, (dataLen - offset) / 8);
794
795 char packetName[IA_MAX_TYPE_LEN];
796 strncpy(packetName,  buff + 4, IA_MAX_TYPE_LEN);
797 packetName[IA_MAX_TYPE_LEN - 1] = 0;
798
799 map<string, int>::iterator pi(packetTypes.find(packetName));
800 if (pi == packetTypes.end())
801     {
802     SendError(sip, sport, protoVer, "îÅÐÒÁ×ÉÌØÎÙÊ ÌÏÇÉΠÉÌÉ ÐÁÒÏÌØ!");
803     printfd(__FILE__, "Login or password is wrong!\n");
804     WriteServLog("User's connect failed. IP \'%s\'. Wrong login or password", inet_ntostring(sip).c_str());
805     return 0;
806     }
807
808 if (user->GetProperty().disabled.Get())
809     {
810     SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÂÌÏËÉÒÏ×ÁÎÁ");
811     return 0;
812     }
813
814 if (user->GetProperty().passive.Get())
815     {
816     SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÍÏÒÏÖÅÎÁ");
817     return 0;
818     }
819
820 if (user->IsAuthorizedBy(this) && user->GetCurrIP() != sip)
821     {
822     printfd(__FILE__, "Login %s already in use. IP \'%s\'\n", login.c_str(), inet_ntostring(sip).c_str());
823     WriteServLog("Login %s already in use. IP \'%s\'", login.c_str(), inet_ntostring(sip).c_str());
824     SendError(sip, sport, protoVer, "÷ÁÛ ÌÏÇÉΠÕÖÅ ÉÓÐÏÌØÚÕÅÔÓÑ!");
825     return 0;
826     }
827
828 // ôÅÐÅÒØ ÍÙ ÄÏÌÖÎÙ ÐÒÏ×ÅÒÉÔØ, ÍÏÖÅÔ ÌÉ ÐÏÌØÚÏ×ÁÔÅÌØ ÐÏÄËÌÀÞÉÔÓÑ Ó ÜÔÏÇÏ ÁÄÒÅÓÁ.
829 int ipFound = user->GetProperty().ips.Get().IsIPInIPS(sip);
830 if (!ipFound)
831     {
832     printfd(__FILE__, "User %s. IP address is incorrect. IP \'%s\'\n", login.c_str(), inet_ntostring(sip).c_str());
833     WriteServLog("User %s. IP address is incorrect. IP \'%s\'", login.c_str(), inet_ntostring(sip).c_str());
834     SendError(sip, sport, protoVer, "ðÏÌØÚÏ×ÁÔÅÌØ ÎÅ ÏÐÏÚÎÁÎ! ðÒÏ×ÅÒØÔÅ IP ÁÄÒÅÓ.");
835     return 0;
836     }
837
838 int ret = -1;
839
840 switch (pi->second)
841     {
842     case CONN_SYN_N:
843         switch (protoVer)
844             {
845             case 6:
846                 connSyn6 = (CONN_SYN_6*)(buff - offset);
847                 ret = Process_CONN_SYN_6(connSyn6, &(it->second), sip);
848                 break;
849             case 7:
850                 connSyn7 = (CONN_SYN_7*)(buff - offset);
851                 ret = Process_CONN_SYN_7(connSyn7, &(it->second), sip);
852                 break;
853             case 8:
854                 connSyn8 = (CONN_SYN_8*)(buff - offset);
855                 ret = Process_CONN_SYN_8(connSyn8, &(it->second), sip);
856                 break;
857             }
858
859         if (ret != 0)
860             {
861             return 0;
862             }
863         switch (protoVer)
864             {
865             case 6:
866                 Send_CONN_SYN_ACK_6(iaUser, sip);
867                 break;
868             case 7:
869                 Send_CONN_SYN_ACK_7(iaUser, sip);
870                 break;
871             case 8:
872                 Send_CONN_SYN_ACK_8(iaUser, sip);
873                 break;
874             }
875         break;
876
877     case CONN_ACK_N:
878         connAck = (CONN_ACK_6*)(buff - offset);
879         switch (protoVer)
880             {
881             case 6:
882                 ret = Process_CONN_ACK_6(connAck, iaUser, sip);
883                 break;
884             case 7:
885                 ret = Process_CONN_ACK_7(connAck, iaUser, sip);
886                 break;
887             case 8:
888                 ret = Process_CONN_ACK_8((CONN_ACK_8*)(buff - offset), iaUser, sip);
889                 break;
890             }
891
892         if (ret != 0)
893             {
894             SendError(sip, sport, protoVer, errorStr);
895             return 0;
896             }
897
898         switch (protoVer)
899             {
900             case 6:
901                 Send_ALIVE_SYN_6(iaUser, sip);
902                 break;
903             case 7:
904                 Send_ALIVE_SYN_7(iaUser, sip);
905                 break;
906             case 8:
907                 Send_ALIVE_SYN_8(iaUser, sip);
908                 break;
909             }
910
911         break;
912
913         // ðÒÉÂÙÌ ÏÔ×ÅÔ Ó ÐÏÄÔ×ÅÒÖÄÅÎÉÅÍ ALIVE
914     case ALIVE_ACK_N:
915         aliveAck = (ALIVE_ACK_6*)(buff - offset);
916         switch (protoVer)
917             {
918             case 6:
919                 ret = Process_ALIVE_ACK_6(aliveAck, iaUser, sip);
920                 break;
921             case 7:
922                 ret = Process_ALIVE_ACK_7(aliveAck, iaUser, sip);
923                 break;
924             case 8:
925                 ret = Process_ALIVE_ACK_8((ALIVE_ACK_8*)(buff - offset), iaUser, sip);
926                 break;
927             }
928         break;
929
930         // úÁÐÒÏÓ ÎÁ ÏÔËÌÀÞÅÎÉÅ
931     case DISCONN_SYN_N:
932
933         disconnSyn = (DISCONN_SYN_6*)(buff - offset);
934         switch (protoVer)
935             {
936             case 6:
937                 ret = Process_DISCONN_SYN_6(disconnSyn, iaUser, sip);
938                 break;
939             case 7:
940                 ret = Process_DISCONN_SYN_7(disconnSyn, iaUser, sip);
941                 break;
942             case 8:
943                 ret = Process_DISCONN_SYN_8((DISCONN_SYN_8*)(buff - offset), iaUser, sip);
944                 break;
945             }
946
947         if (ret != 0)
948             return 0;
949
950         switch (protoVer)
951             {
952             case 6:
953                 Send_DISCONN_SYN_ACK_6(iaUser, sip);
954                 break;
955             case 7:
956                 Send_DISCONN_SYN_ACK_7(iaUser, sip);
957                 break;
958             case 8:
959                 Send_DISCONN_SYN_ACK_8(iaUser, sip);
960                 break;
961             }
962         break;
963
964     case DISCONN_ACK_N:
965         disconnAck = (DISCONN_ACK_6*)(buff - offset);
966
967         switch (protoVer)
968             {
969             case 6:
970                 ret = Process_DISCONN_ACK_6(disconnAck, iaUser, sip, it);
971                 break;
972             case 7:
973                 ret = Process_DISCONN_ACK_7(disconnAck, iaUser, sip, it);
974                 break;
975             case 8:
976                 ret = Process_DISCONN_ACK_8((DISCONN_ACK_8*)(buff - offset), iaUser, sip, it);
977                 break;
978             }
979
980         switch (protoVer)
981             {
982             case 6:
983                 Send_FIN_6(iaUser, sip, it);
984                 break;
985             case 7:
986                 Send_FIN_7(iaUser, sip, it);
987                 break;
988             case 8:
989                 Send_FIN_8(iaUser, sip, it);
990                 break;
991             }
992         break;
993     }
994
995 return 0;
996 }
997 //-----------------------------------------------------------------------------
998 void AUTH_IA::DelUser(USER_PTR u)
999 {
1000 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1001
1002 uint32_t ip = u->GetCurrIP();
1003
1004 if (!ip)
1005     return;
1006
1007 map<uint32_t, IA_USER>::iterator it;
1008 it = ip2user.find(ip);
1009 if (it == ip2user.end())
1010     {
1011     //Nothing to delete
1012     printfd(__FILE__, "Nothing to delete\n");
1013     return;
1014     }
1015
1016 if (it->second.user == u)
1017     {
1018     printfd(__FILE__, "User removed!\n");
1019     users->Unauthorize(u->GetLogin(), this);
1020     ip2user.erase(it);
1021     }
1022 }
1023 //-----------------------------------------------------------------------------
1024 int AUTH_IA::SendError(uint32_t ip, uint16_t port, int protoVer, const string & text)
1025 {
1026 struct sockaddr_in sendAddr;
1027 switch (protoVer)
1028     {
1029     int res;
1030     case 6:
1031     case 7:
1032         ERR err;
1033         memset(&err, 0, sizeof(ERR));
1034
1035         sendAddr.sin_family = AF_INET;
1036         sendAddr.sin_port = htons(port);
1037
1038         sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1039
1040         err.len = 1;
1041         strncpy((char*)err.type, "ERR", 16);
1042         strncpy((char*)err.text, text.c_str(), MAX_MSG_LEN);
1043
1044         #ifdef ARCH_BE
1045         SwapBytes(err.len);
1046         #endif
1047
1048         res = sendto(listenSocket, &err, sizeof(err), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1049         printfd(__FILE__, "SendError %d bytes sent\n", res);
1050         break;
1051
1052     case 8:
1053         ERR_8 err8;
1054         memset(&err8, 0, sizeof(ERR_8));
1055
1056         sendAddr.sin_family = AF_INET;
1057         sendAddr.sin_port = htons(port);
1058
1059         sendAddr.sin_addr.s_addr = ip;// IP ÐÏÌØÚÏ×ÁÔÅÌÑ
1060
1061         err8.len = 256;
1062         strncpy((char*)err8.type, "ERR", 16);
1063         strncpy((char*)err8.text, text.c_str(), MAX_MSG_LEN);
1064
1065         #ifdef ARCH_BE
1066         SwapBytes(err8.len);
1067         #endif
1068
1069         res = sendto(listenSocket, &err8, sizeof(err8), 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1070         printfd(__FILE__, "SendError_8 %d bytes sent\n", res);
1071         break;
1072     }
1073
1074 return 0;
1075 }
1076 //-----------------------------------------------------------------------------
1077 int AUTH_IA::Send(uint32_t ip, uint16_t port, const char * buffer, int len)
1078 {
1079 struct sockaddr_in sendAddr;
1080 int res;
1081
1082 sendAddr.sin_family = AF_INET;
1083 sendAddr.sin_port = htons(port);
1084 sendAddr.sin_addr.s_addr = ip;
1085
1086 res = sendto(listenSocket, buffer, len, 0, (struct sockaddr*)&sendAddr, sizeof(sendAddr));
1087
1088 static struct timeval tv;
1089 gettimeofday(&tv, NULL);
1090
1091 return res;
1092 }
1093 //-----------------------------------------------------------------------------
1094 int AUTH_IA::SendMessage(const STG_MSG & msg, uint32_t ip) const
1095 {
1096 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1097
1098 printfd(__FILE__, "SendMessage userIP=%s\n", inet_ntostring(ip).c_str());
1099
1100 map<uint32_t, IA_USER>::iterator it;
1101 it = ip2user.find(ip);
1102 if (it == ip2user.end())
1103     {
1104     errorStr = "Unknown user.";
1105     return -1;
1106     }
1107 it->second.messagesToSend.push_back(msg);
1108 return 0;
1109 }
1110 //-----------------------------------------------------------------------------
1111 int AUTH_IA::RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1112 {
1113 printfd(__FILE__, "RealSendMessage 6 user=%s\n", user.login.c_str());
1114
1115 char buffer[256];
1116 INFO_6 info;
1117
1118 memset(&info, 0, sizeof(INFO_6));
1119
1120 info.len = 256;
1121 strncpy((char*)info.type, "INFO", 16);
1122 info.infoType = 'I';
1123 strncpy((char*)info.text, msg.text.c_str(), 235);
1124 info.text[234] = 0;
1125
1126 size_t len = info.len;
1127 #ifdef ARCH_BE
1128 SwapBytes(info.len);
1129 #endif
1130
1131 memcpy(buffer, &info, sizeof(INFO_6));
1132 Encrypt(&user.ctx, buffer, buffer, len / 8);
1133 Send(ip, iaSettings.GetUserPort(), buffer, len);
1134
1135 return 0;
1136 }
1137 //-----------------------------------------------------------------------------
1138 int AUTH_IA::RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1139 {
1140 printfd(__FILE__, "RealSendMessage 7 user=%s\n", user.login.c_str());
1141
1142 char buffer[300];
1143 INFO_7 info;
1144
1145 memset(&info, 0, sizeof(INFO_7));
1146
1147 info.len = 264;
1148 strncpy((char*)info.type, "INFO_7", 16);
1149 info.infoType = msg.header.type;
1150 info.showTime = msg.header.showTime;
1151
1152 info.sendTime = msg.header.creationTime;
1153
1154 size_t len = info.len;
1155 #ifdef ARCH_BE
1156 SwapBytes(info.len);
1157 SwapBytes(info.sendTime);
1158 #endif
1159
1160 strncpy((char*)info.text, msg.text.c_str(), MAX_MSG_LEN - 1);
1161 info.text[MAX_MSG_LEN - 1] = 0;
1162
1163 memcpy(buffer, &info, sizeof(INFO_7));
1164
1165 Encrypt(&user.ctx, buffer, buffer, len / 8);
1166 Send(ip, iaSettings.GetUserPort(), buffer, len);
1167
1168 return 0;
1169 }
1170 //-----------------------------------------------------------------------------
1171 int AUTH_IA::RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user)
1172 {
1173 printfd(__FILE__, "RealSendMessage 8 user=%s\n", user.login.c_str());
1174
1175 char buffer[1500];
1176 memset(buffer, 0, sizeof(buffer));
1177
1178 INFO_8 info;
1179
1180 memset(&info, 0, sizeof(INFO_8));
1181
1182 info.len = 1056;
1183 strncpy((char*)info.type, "INFO_8", 16);
1184 info.infoType = msg.header.type;
1185 info.showTime = msg.header.showTime;
1186 info.sendTime = msg.header.creationTime;
1187
1188 strncpy((char*)info.text, msg.text.c_str(), IA_MAX_MSG_LEN_8 - 1);
1189 info.text[IA_MAX_MSG_LEN_8 - 1] = 0;
1190
1191 size_t len = info.len;
1192 #ifdef ARCH_BE
1193 SwapBytes(info.len);
1194 SwapBytes(info.sendTime);
1195 #endif
1196
1197 memcpy(buffer, &info, sizeof(INFO_8));
1198
1199 Encrypt(&user.ctx, buffer, buffer, len / 8);
1200 Send(ip, user.port, buffer, len);
1201
1202 return 0;
1203 }
1204 //-----------------------------------------------------------------------------
1205 int AUTH_IA::Process_CONN_SYN_6(CONN_SYN_6 *, IA_USER * iaUser, uint32_t)
1206 {
1207 if (!(iaUser->phase.GetPhase() == 1 || iaUser->phase.GetPhase() == 3))
1208     return -1;
1209
1210 enabledDirs = 0xFFffFFff;
1211
1212 iaUser->phase.SetPhase2();
1213 printfd(__FILE__, "Phase changed from %d to 2. Reason: CONN_SYN_6\n", iaUser->phase.GetPhase());
1214 return 0;
1215 }
1216 //-----------------------------------------------------------------------------
1217 int AUTH_IA::Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip)
1218 {
1219 return Process_CONN_SYN_6(connSyn, iaUser, sip);
1220 }
1221 //-----------------------------------------------------------------------------
1222 int AUTH_IA::Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip)
1223 {
1224 #ifdef ARCH_BE
1225 SwapBytes(connSyn->dirs);
1226 #endif
1227 int ret = Process_CONN_SYN_6((CONN_SYN_6*)connSyn, iaUser, sip);
1228 enabledDirs = connSyn->dirs;
1229 return ret;
1230 }
1231 //-----------------------------------------------------------------------------
1232 int AUTH_IA::Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip)
1233 {
1234 #ifdef ARCH_BE
1235 SwapBytes(connAck->len);
1236 SwapBytes(connAck->rnd);
1237 #endif
1238 printfd( __FILE__, "CONN_ACK_6 %s\n", connAck->type);
1239 // ÕÓÔÁÎÏ×ÉÔØ ÎÏ×ÕÀ ÆÁÚÕ É ×ÒÅÍÑ É ÒÁÚÒÅÛÉÔØ ÉÎÅÔ
1240 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1241     {
1242     iaUser->phase.UpdateTime();
1243
1244     iaUser->lastSendAlive = iaUser->phase.GetTime();
1245     if (users->Authorize(iaUser->login, sip, enabledDirs, this))
1246         {
1247         iaUser->phase.SetPhase3();
1248         printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_6\n");
1249         return 0;
1250         }
1251     else
1252         {
1253         errorStr = iaUser->user->GetStrError();
1254         iaUser->phase.SetPhase1();
1255         printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1256         return -1;
1257         }
1258     }
1259 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1260 return -1;
1261 }
1262 //-----------------------------------------------------------------------------
1263 int AUTH_IA::Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip)
1264 {
1265 return Process_CONN_ACK_6(connAck, iaUser, sip);
1266 }
1267 //-----------------------------------------------------------------------------
1268 int AUTH_IA::Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip)
1269 {
1270 #ifdef ARCH_BE
1271 SwapBytes(connAck->len);
1272 SwapBytes(connAck->rnd);
1273 #endif
1274 printfd( __FILE__, "CONN_ACK_8 %s\n", connAck->type);
1275
1276 if ((iaUser->phase.GetPhase() == 2) && (connAck->rnd == iaUser->rnd + 1))
1277     {
1278     iaUser->phase.UpdateTime();
1279     iaUser->lastSendAlive = iaUser->phase.GetTime();
1280     if (users->Authorize(iaUser->login, sip, enabledDirs, this))
1281         {
1282         iaUser->phase.SetPhase3();
1283         printfd(__FILE__, "Phase changed from 2 to 3. Reason: CONN_ACK_8\n");
1284         return 0;
1285         }
1286     else
1287         {
1288         errorStr = iaUser->user->GetStrError();
1289         iaUser->phase.SetPhase1();
1290         printfd(__FILE__, "Phase changed from 2 to 1. Reason: failed to authorize user\n");
1291         return -1;
1292         }
1293     }
1294 printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), connAck->rnd);
1295 return -1;
1296 }
1297 //-----------------------------------------------------------------------------
1298 int AUTH_IA::Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t)
1299 {
1300 #ifdef ARCH_BE
1301 SwapBytes(aliveAck->len);
1302 SwapBytes(aliveAck->rnd);
1303 #endif
1304 printfd(__FILE__, "ALIVE_ACK_6\n");
1305 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1306     {
1307     iaUser->phase.UpdateTime();
1308     #ifdef IA_DEBUG
1309     iaUser->aliveSent = false;
1310     #endif
1311     }
1312 return 0;
1313 }
1314 //-----------------------------------------------------------------------------
1315 int AUTH_IA::Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip)
1316 {
1317 return Process_ALIVE_ACK_6(aliveAck, iaUser, sip);
1318 }
1319 //-----------------------------------------------------------------------------
1320 int AUTH_IA::Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t)
1321 {
1322 #ifdef ARCH_BE
1323 SwapBytes(aliveAck->len);
1324 SwapBytes(aliveAck->rnd);
1325 #endif
1326 printfd(__FILE__, "ALIVE_ACK_8\n");
1327 if ((iaUser->phase.GetPhase() == 3) && (aliveAck->rnd == iaUser->rnd + 1))
1328     {
1329     iaUser->phase.UpdateTime();
1330     #ifdef IA_DEBUG
1331     iaUser->aliveSent = false;
1332     #endif
1333     }
1334 return 0;
1335 }
1336 //-----------------------------------------------------------------------------
1337 int AUTH_IA::Process_DISCONN_SYN_6(DISCONN_SYN_6 *, IA_USER * iaUser, uint32_t)
1338 {
1339 printfd(__FILE__, "DISCONN_SYN_6\n");
1340 if (iaUser->phase.GetPhase() != 3)
1341     {
1342     printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1343     errorStr = "Incorrect request DISCONN_SYN";
1344     return -1;
1345     }
1346
1347 iaUser->phase.SetPhase4();
1348 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1349
1350 return 0;
1351 }
1352 //-----------------------------------------------------------------------------
1353 int AUTH_IA::Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip)
1354 {
1355 return Process_DISCONN_SYN_6(disconnSyn, iaUser, sip);
1356 }
1357 //-----------------------------------------------------------------------------
1358 int AUTH_IA::Process_DISCONN_SYN_8(DISCONN_SYN_8 *, IA_USER * iaUser, uint32_t)
1359 {
1360 if (iaUser->phase.GetPhase() != 3)
1361     {
1362     errorStr = "Incorrect request DISCONN_SYN";
1363     printfd(__FILE__, "Invalid phase. Expected 3, actual %d\n", iaUser->phase.GetPhase());
1364     return -1;
1365     }
1366
1367 iaUser->phase.SetPhase4();
1368 printfd(__FILE__, "Phase changed from 3 to 4. Reason: DISCONN_SYN_6\n");
1369
1370 return 0;
1371 }
1372 //-----------------------------------------------------------------------------
1373 int AUTH_IA::Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnAck,
1374                                    IA_USER * iaUser,
1375                                    uint32_t,
1376                                    map<uint32_t, IA_USER>::iterator)
1377 {
1378 #ifdef ARCH_BE
1379 SwapBytes(disconnAck->len);
1380 SwapBytes(disconnAck->rnd);
1381 #endif
1382 printfd(__FILE__, "DISCONN_ACK_6\n");
1383 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1384     {
1385     printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1386     return -1;
1387     }
1388
1389 return 0;
1390 }
1391 //-----------------------------------------------------------------------------
1392 int AUTH_IA::Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnAck, IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1393 {
1394 return Process_DISCONN_ACK_6(disconnAck, iaUser, sip, it);
1395 }
1396 //-----------------------------------------------------------------------------
1397 int AUTH_IA::Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnAck, IA_USER * iaUser, uint32_t, map<uint32_t, IA_USER>::iterator)
1398 {
1399 #ifdef ARCH_BE
1400 SwapBytes(disconnAck->len);
1401 SwapBytes(disconnAck->rnd);
1402 #endif
1403 printfd(__FILE__, "DISCONN_ACK_8\n");
1404 if (!((iaUser->phase.GetPhase() == 4) && (disconnAck->rnd == iaUser->rnd + 1)))
1405     {
1406     printfd(__FILE__, "Invalid phase or control number. Phase: %d. Control number: %d\n", iaUser->phase.GetPhase(), disconnAck->rnd);
1407     return -1;
1408     }
1409
1410 return 0;
1411 }
1412 //-----------------------------------------------------------------------------
1413 int AUTH_IA::Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1414 {
1415 //+++ Fill static data in connSynAck +++
1416 // TODO Move this code. It must be executed only once
1417 connSynAck6.len = Min8(sizeof(CONN_SYN_ACK_6));
1418 strcpy((char*)connSynAck6.type, "CONN_SYN_ACK");
1419 for (int j = 0; j < DIR_NUM; j++)
1420     {
1421     strncpy((char*)connSynAck6.dirName[j],
1422             stgSettings->GetDirName(j).c_str(),
1423             sizeof(string16));
1424
1425     connSynAck6.dirName[j][sizeof(string16) - 1] = 0;
1426     }
1427 //--- Fill static data in connSynAck ---
1428
1429 iaUser->rnd = random();
1430 connSynAck6.rnd = iaUser->rnd;
1431
1432 connSynAck6.userTimeOut = iaSettings.GetUserTimeout();
1433 connSynAck6.aliveDelay = iaSettings.GetUserDelay();
1434
1435 #ifdef ARCH_BE
1436 SwapBytes(connSynAck6.len);
1437 SwapBytes(connSynAck6.rnd);
1438 SwapBytes(connSynAck6.userTimeOut);
1439 SwapBytes(connSynAck6.aliveDelay);
1440 #endif
1441
1442 Encrypt(&iaUser->ctx, (char*)&connSynAck6, (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6))/8);
1443 return Send(sip, iaSettings.GetUserPort(), (char*)&connSynAck6, Min8(sizeof(CONN_SYN_ACK_6)));;
1444 }
1445 //-----------------------------------------------------------------------------
1446 int AUTH_IA::Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1447 {
1448 return Send_CONN_SYN_ACK_6(iaUser, sip);
1449 }
1450 //-----------------------------------------------------------------------------
1451 int AUTH_IA::Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1452 {
1453 strcpy((char*)connSynAck8.hdr.magic, IA_ID);
1454 connSynAck8.hdr.protoVer[0] = 0;
1455 connSynAck8.hdr.protoVer[1] = 8;
1456
1457 //+++ Fill static data in connSynAck +++
1458 // TODO Move this code. It must be executed only once
1459 connSynAck8.len = Min8(sizeof(CONN_SYN_ACK_8));
1460 strcpy((char*)connSynAck8.type, "CONN_SYN_ACK");
1461 for (int j = 0; j < DIR_NUM; j++)
1462     {
1463     strncpy((char*)connSynAck8.dirName[j],
1464             stgSettings->GetDirName(j).c_str(),
1465             sizeof(string16));
1466
1467     connSynAck8.dirName[j][sizeof(string16) - 1] = 0;
1468     }
1469 //--- Fill static data in connSynAck ---
1470
1471 iaUser->rnd = random();
1472 connSynAck8.rnd = iaUser->rnd;
1473
1474 connSynAck8.userTimeOut = iaSettings.GetUserTimeout();
1475 connSynAck8.aliveDelay = iaSettings.GetUserDelay();
1476
1477 #ifdef ARCH_BE
1478 SwapBytes(connSynAck8.len);
1479 SwapBytes(connSynAck8.rnd);
1480 SwapBytes(connSynAck8.userTimeOut);
1481 SwapBytes(connSynAck8.aliveDelay);
1482 #endif
1483
1484 Encrypt(&iaUser->ctx, (char*)&connSynAck8, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8))/8);
1485 return Send(sip, iaUser->port, (char*)&connSynAck8, Min8(sizeof(CONN_SYN_ACK_8)));
1486 //return Send(sip, iaUser->port, (char*)&connSynAck8, 384);
1487 }
1488 //-----------------------------------------------------------------------------
1489 int AUTH_IA::Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip)
1490 {
1491 aliveSyn6.len = Min8(sizeof(ALIVE_SYN_6));
1492 aliveSyn6.rnd = iaUser->rnd = random();
1493
1494 strcpy((char*)aliveSyn6.type, "ALIVE_SYN");
1495
1496 for (int i = 0; i < DIR_NUM; i++)
1497     {
1498     aliveSyn6.md[i] = iaUser->user->GetProperty().down.Get()[i];
1499     aliveSyn6.mu[i] = iaUser->user->GetProperty().up.Get()[i];
1500
1501     aliveSyn6.sd[i] = iaUser->user->GetSessionDownload()[i];
1502     aliveSyn6.su[i] = iaUser->user->GetSessionUpload()[i];
1503     }
1504
1505 //TODO
1506 int dn = iaSettings.GetFreeMbShowType();
1507 const TARIFF * tf = iaUser->user->GetTariff();
1508
1509 if (dn < DIR_NUM)
1510     {
1511     double p = tf->GetPriceWithTraffType(aliveSyn6.mu[dn],
1512                                          aliveSyn6.md[dn],
1513                                          dn,
1514                                          stgTime);
1515     p *= (1024 * 1024);
1516     if (p == 0)
1517         {
1518         snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "---");
1519         }
1520     else
1521         {
1522         double fmb = iaUser->user->GetProperty().freeMb;
1523         fmb = fmb < 0 ? 0 : fmb;
1524         snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1525         }
1526     }
1527 else
1528     {
1529     if (freeMbNone == iaSettings.GetFreeMbShowType())
1530         {
1531         aliveSyn6.freeMb[0] = 0;
1532         }
1533     else
1534         {
1535         double fmb = iaUser->user->GetProperty().freeMb;
1536         fmb = fmb < 0 ? 0 : fmb;
1537         snprintf((char*)aliveSyn6.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1538         }
1539     }
1540
1541 #ifdef IA_DEBUG
1542 if (iaUser->aliveSent)
1543     {
1544     printfd(__FILE__, "========= ALIVE_ACK_6(7) TIMEOUT !!! %s =========\n", iaUser->login.c_str());
1545     }
1546 iaUser->aliveSent = true;
1547 #endif
1548
1549 aliveSyn6.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0);
1550 if (!stgSettings->GetShowFeeInCash())
1551     aliveSyn6.cash -= (int64_t)(tf->GetFee() * 1000.0);
1552
1553 #ifdef ARCH_BE
1554 SwapBytes(aliveSyn6.len);
1555 SwapBytes(aliveSyn6.rnd);
1556 SwapBytes(aliveSyn6.cash);
1557 for (int i = 0; i < DIR_NUM; ++i)
1558     {
1559     SwapBytes(aliveSyn6.mu[i]);
1560     SwapBytes(aliveSyn6.md[i]);
1561     SwapBytes(aliveSyn6.su[i]);
1562     SwapBytes(aliveSyn6.sd[i]);
1563     }
1564 #endif
1565
1566 Encrypt(&(iaUser->ctx), (char*)&aliveSyn6, (char*)&aliveSyn6, Min8(sizeof(aliveSyn6))/8);
1567 return Send(sip, iaSettings.GetUserPort(), (char*)&aliveSyn6, Min8(sizeof(aliveSyn6)));
1568 }
1569 //-----------------------------------------------------------------------------
1570 int AUTH_IA::Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip)
1571 {
1572 return Send_ALIVE_SYN_6(iaUser, sip);
1573 }
1574 //-----------------------------------------------------------------------------
1575 int AUTH_IA::Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip)
1576 {
1577 strcpy((char*)aliveSyn8.hdr.magic, IA_ID);
1578 aliveSyn8.hdr.protoVer[0] = 0;
1579 aliveSyn8.hdr.protoVer[1] = 8;
1580
1581 aliveSyn8.len = Min8(sizeof(ALIVE_SYN_8));
1582 aliveSyn8.rnd = iaUser->rnd = random();
1583
1584 strcpy((char*)aliveSyn8.type, "ALIVE_SYN");
1585
1586 for (int i = 0; i < DIR_NUM; i++)
1587     {
1588     aliveSyn8.md[i] = iaUser->user->GetProperty().down.Get()[i];
1589     aliveSyn8.mu[i] = iaUser->user->GetProperty().up.Get()[i];
1590
1591     aliveSyn8.sd[i] = iaUser->user->GetSessionDownload()[i];
1592     aliveSyn8.su[i] = iaUser->user->GetSessionUpload()[i];
1593     }
1594
1595 //TODO
1596 int dn = iaSettings.GetFreeMbShowType();
1597
1598 if (dn < DIR_NUM)
1599     {
1600     const TARIFF * tf = iaUser->user->GetTariff();
1601     double p = tf->GetPriceWithTraffType(aliveSyn8.mu[dn],
1602                                          aliveSyn8.md[dn],
1603                                          dn,
1604                                          stgTime);
1605     p *= (1024 * 1024);
1606     if (p == 0)
1607         {
1608         snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "---");
1609         }
1610     else
1611         {
1612         double fmb = iaUser->user->GetProperty().freeMb;
1613         fmb = fmb < 0 ? 0 : fmb;
1614         snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "%.3f", fmb / p);
1615         }
1616     }
1617 else
1618     {
1619     if (freeMbNone == iaSettings.GetFreeMbShowType())
1620         {
1621         aliveSyn8.freeMb[0] = 0;
1622         }
1623     else
1624         {
1625         double fmb = iaUser->user->GetProperty().freeMb;
1626         fmb = fmb < 0 ? 0 : fmb;
1627         snprintf((char*)aliveSyn8.freeMb, IA_FREEMB_LEN, "C%.3f", fmb);
1628         }
1629     }
1630
1631 #ifdef IA_DEBUG
1632 if (iaUser->aliveSent)
1633     {
1634     printfd(__FILE__, "========= ALIVE_ACK_8 TIMEOUT !!! =========\n");
1635     }
1636 iaUser->aliveSent = true;
1637 #endif
1638
1639 const TARIFF * tf = iaUser->user->GetTariff();
1640
1641 aliveSyn8.cash =(int64_t) (iaUser->user->GetProperty().cash.Get() * 1000.0);
1642 if (!stgSettings->GetShowFeeInCash())
1643     aliveSyn8.cash -= (int64_t)(tf->GetFee() * 1000.0);
1644
1645 #ifdef ARCH_BE
1646 SwapBytes(aliveSyn8.len);
1647 SwapBytes(aliveSyn8.rnd);
1648 SwapBytes(aliveSyn8.cash);
1649 SwapBytes(aliveSyn8.status);
1650 for (int i = 0; i < DIR_NUM; ++i)
1651     {
1652     SwapBytes(aliveSyn8.mu[i]);
1653     SwapBytes(aliveSyn8.md[i]);
1654     SwapBytes(aliveSyn8.su[i]);
1655     SwapBytes(aliveSyn8.sd[i]);
1656     }
1657 #endif
1658
1659 Encrypt(&(iaUser->ctx), (char*)&aliveSyn8, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8))/8);
1660 return Send(sip, iaUser->port, (char*)&aliveSyn8, Min8(sizeof(aliveSyn8)));
1661 }
1662 //-----------------------------------------------------------------------------
1663 int AUTH_IA::Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip)
1664 {
1665 disconnSynAck6.len = Min8(sizeof(DISCONN_SYN_ACK_6));
1666 strcpy((char*)disconnSynAck6.type, "DISCONN_SYN_ACK");
1667 disconnSynAck6.rnd = iaUser->rnd = random();
1668
1669 #ifdef ARCH_BE
1670 SwapBytes(disconnSynAck6.len);
1671 SwapBytes(disconnSynAck6.rnd);
1672 #endif
1673
1674 Encrypt(&iaUser->ctx, (char*)&disconnSynAck6, (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6))/8);
1675 return Send(sip, iaSettings.GetUserPort(), (char*)&disconnSynAck6, Min8(sizeof(disconnSynAck6)));
1676 }
1677 //-----------------------------------------------------------------------------
1678 int AUTH_IA::Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip)
1679 {
1680 return Send_DISCONN_SYN_ACK_6(iaUser, sip);
1681 }
1682 //-----------------------------------------------------------------------------
1683 int AUTH_IA::Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip)
1684 {
1685 strcpy((char*)disconnSynAck8.hdr.magic, IA_ID);
1686 disconnSynAck8.hdr.protoVer[0] = 0;
1687 disconnSynAck8.hdr.protoVer[1] = 8;
1688
1689 disconnSynAck8.len = Min8(sizeof(DISCONN_SYN_ACK_8));
1690 strcpy((char*)disconnSynAck8.type, "DISCONN_SYN_ACK");
1691 disconnSynAck8.rnd = iaUser->rnd = random();
1692
1693 #ifdef ARCH_BE
1694 SwapBytes(disconnSynAck8.len);
1695 SwapBytes(disconnSynAck8.rnd);
1696 #endif
1697
1698 Encrypt(&iaUser->ctx, (char*)&disconnSynAck8, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8))/8);
1699 return Send(sip, iaUser->port, (char*)&disconnSynAck8, Min8(sizeof(disconnSynAck8)));
1700 }
1701 //-----------------------------------------------------------------------------
1702 int AUTH_IA::Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1703 {
1704 fin6.len = Min8(sizeof(FIN_6));
1705 strcpy((char*)fin6.type, "FIN");
1706 strcpy((char*)fin6.ok, "OK");
1707
1708 #ifdef ARCH_BE
1709 SwapBytes(fin6.len);
1710 #endif
1711
1712 Encrypt(&iaUser->ctx, (char*)&fin6, (char*)&fin6, Min8(sizeof(fin6))/8);
1713
1714 users->Unauthorize(iaUser->login, this);
1715
1716 int ret = Send(sip, iaSettings.GetUserPort(), (char*)&fin6, Min8(sizeof(fin6)));
1717 ip2user.erase(it);
1718 return ret;
1719 }
1720 //-----------------------------------------------------------------------------
1721 int AUTH_IA::Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1722 {
1723 return Send_FIN_6(iaUser, sip, it);
1724 }
1725 //-----------------------------------------------------------------------------
1726 int AUTH_IA::Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it)
1727 {
1728 strcpy((char*)fin8.hdr.magic, IA_ID);
1729 fin8.hdr.protoVer[0] = 0;
1730 fin8.hdr.protoVer[1] = 8;
1731
1732 fin8.len = Min8(sizeof(FIN_8));
1733 strcpy((char*)fin8.type, "FIN");
1734 strcpy((char*)fin8.ok, "OK");
1735
1736 #ifdef ARCH_BE
1737 SwapBytes(fin8.len);
1738 #endif
1739
1740 Encrypt(&iaUser->ctx, (char*)&fin8, (char*)&fin8, Min8(sizeof(fin8))/8);
1741
1742 users->Unauthorize(iaUser->login, this);
1743
1744 int ret = Send(sip, iaUser->port, (char*)&fin8, Min8(sizeof(fin8)));
1745 ip2user.erase(it);
1746 return ret;
1747 }
1748 //-----------------------------------------------------------------------------
1749 inline
1750 void InitEncrypt(BLOWFISH_CTX * ctx, const string & password)
1751 {
1752 unsigned char keyL[PASSWD_LEN];  // ðÁÒÏÌØ ÄÌÑ ÛÉÆÒÏ×ËÉ
1753 memset(keyL, 0, PASSWD_LEN);
1754 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
1755 Blowfish_Init(ctx, keyL, PASSWD_LEN);
1756 }
1757 //-----------------------------------------------------------------------------
1758 inline
1759 void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1760 {
1761 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1762
1763 for (int i = 0; i < len8; i++)
1764     DecodeString(dst + i * 8, src + i * 8, ctx);
1765 }
1766 //-----------------------------------------------------------------------------
1767 inline
1768 void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8)
1769 {
1770 // len8 - ÄÌÉÎÁ × 8-ÍÉ ÂÁÊÔÏ×ÙÈ ÂÌÏËÁÈ
1771
1772 for (int i = 0; i < len8; i++)
1773     EncodeString(dst + i * 8, src + i * 8, ctx);
1774 }