]> git.stg.codes - stg.git/blob - stglibs/ia.lib/include/stg/ia.h
TUT framework updated to svn version
[stg.git] / stglibs / ia.lib / include / stg / ia.h
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 1, or (at your option)
5 ** 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., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17 /*
18  $Author: faust $
19  $Revision: 1.10 $
20  $Date: 2010/03/15 12:57:24 $
21 */
22
23 /*
24 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
25 */
26 //---------------------------------------------------------------------------
27 #ifndef IA_AUTH_C_H
28 #define IA_AUTH_C_H
29
30 #ifndef WIN32
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <pthread.h>
35 #else
36 #include <winsock2.h>
37 #endif
38
39 #include <string>
40 #include <vector>
41 #include <map>
42
43 #include "stg/blowfish.h"
44 #include "stg/ia_packets.h"
45
46 #define IA_BIND_ERROR           (1)
47 #define IA_SERVER_ERROR         (2)
48 #define IA_FCNTL_ERROR          (3)
49 #define IA_GETHOSTBYNAME_ERROR  (4)
50
51 #define IA_PROTO_VER            (8)
52 #define IA_PROTO_PROXY_VER      (101)
53
54 using namespace std;
55
56 typedef void (*tpStatusChangedCb)(int status, void * data);
57 typedef void (*tpStatChangedCb)(const LOADSTAT & stat, void * data);
58 typedef void (*tpCallBackInfoFn)(const string & message, int infoType, int showTime, int sendTime, void * data);
59 typedef void (*tpCallBackErrorFn)(const string & message, int netError, void * data);
60 typedef void (*tpCallBackDirNameFn)(const vector<string> & dirName, void * data);
61
62 //---------------------------------------------------------------------------
63 class IA_CLIENT_PROT
64 {
65 #ifdef WIN32
66 friend unsigned long WINAPI RunW(void * data);
67 #else
68 friend void * RunL(void * data);
69 #endif
70
71 public:
72     IA_CLIENT_PROT(const string & sn, uint16_t p, uint16_t localPort = 0);
73     ~IA_CLIENT_PROT();
74
75     void        Start();
76     void        Stop();
77     void        GetStat(LOADSTAT * ls);
78
79     void        SetServer(const string & sn, unsigned short port);
80     void        SetLogin(const string & login);
81     void        SetPassword(const string & password);
82     void        SetEnabledDirs(const bool * selectedDirs);
83
84     void        SetStatusChangedCb(tpStatusChangedCb p, void * data);
85     void        SetStatChangedCb(tpStatChangedCb p, void * data);
86     void        SetInfoCb(tpCallBackInfoFn p, void * data);
87     void        SetErrorCb(tpCallBackErrorFn p, void * data);
88     void        SetDirNameCb(tpCallBackDirNameFn p, void * data);
89
90     int         Connect();
91     int         Disconnect();
92     int         GetAuthorized() const { return phase == 3 || phase == 4; };
93     int         GetPhase() const { return phase; };
94     int         GetStatus() const;
95     int         GetReconnect() const { return reconnect; };
96     void        SetReconnect(int r) { reconnect = r; };
97     char        GetProtoVer() const { return proxyMode ? IA_PROTO_PROXY_VER : IA_PROTO_VER; };
98     void        GetMessageText(string * text) const { *text = messageText; };
99     void        GetInfoText(string * text) const { *text = infoText; };
100     int         GetStrError(string * error) const;
101
102     void        SetProxyMode(bool on) { proxyMode = on; };
103     bool        GetProxyMode() const { return proxyMode; };
104
105     void        SetIP(uint32_t ip) { IA_CLIENT_PROT::ip = ip; };
106     uint32_t    GetIP() const { return ip; };
107
108 private:
109     void            Run();
110     int             NetRecv();
111     int             NetSend(int n);
112     bool            GetNonstop() const { return nonstop; };
113     void            PrepareNet();
114     int             DeterminatePacketType(const char * buffer);
115
116     int             Process_CONN_SYN_ACK_8(const char * buffer);
117     int             Process_ALIVE_SYN_8(const char * buffer);
118     int             Process_DISCONN_SYN_ACK_8(const char * buffer);
119     int             Process_FIN_8(const char * buffer);
120     int             Process_INFO_8(const char * buffer);
121     int             Process_ERROR(const char * buffer);
122
123     int             Prepare_CONN_SYN_8(char * buffer);
124     int             Prepare_CONN_ACK_8(char * buffer);
125     int             Prepare_ALIVE_ACK_8(char * buffer);
126     int             Prepare_DISCONN_SYN_8(char * buffer);
127     int             Prepare_DISCONN_ACK_8(char * buffer);
128
129     void            FillHdr8(char * buffer, unsigned long ip);
130     int             Send(char * buffer, int len);
131     int             Recv(char * buffer, int len);
132
133     LOADSTAT        stat;
134     int             action;
135     int             phase;
136     int             phaseTime;
137     string          messageText;
138     string          infoText;
139     mutable string  strError;
140     mutable int     codeError;
141     bool            nonstop;
142     bool            isNetPrepared;
143     bool            proxyMode;
144
145     BLOWFISH_CTX    ctxPass;
146     BLOWFISH_CTX    ctxHdr;
147
148     bool            selectedDirs[DIR_NUM];
149
150     string          password;
151     string          login;
152
153     #ifdef WIN32
154     WSADATA wsaData;
155     #else
156     pthread_t thread;
157     #endif
158
159     string          serverName;
160     uint16_t        port;
161     uint32_t        ip;
162     uint32_t        localPort;
163
164     struct sockaddr_in  servAddr;
165
166     bool            firstConnect;
167     int             reconnect;
168     int             sockr;
169     int             protNum;
170     int             userTimeout;
171     int             aliveTimeout;
172     unsigned int    rnd;
173
174     tpStatusChangedCb   pStatusChangedCb;
175     tpStatChangedCb     pStatChangedCb;
176     tpCallBackInfoFn    pInfoCb;
177     tpCallBackErrorFn   pErrorCb;
178     tpCallBackDirNameFn pDirNameCb;
179
180     void              * statusChangedCbData;
181     void              * statChangedCbData;
182     void              * infoCbData;
183     void              * errorCbData;
184     void              * dirNameCbData;
185
186     map<string, int>    packetTypes;
187
188     CONN_SYN_8        * connSyn8;
189     CONN_SYN_ACK_8    * connSynAck8;
190     CONN_ACK_8        * connAck8;
191     ALIVE_SYN_8       * aliveSyn8;
192     ALIVE_ACK_8       * aliveAck8;
193     DISCONN_SYN_8     * disconnSyn8;
194     DISCONN_SYN_ACK_8 * disconnSynAck8;
195     DISCONN_ACK_8     * disconnAck8;
196     INFO_8            * info;
197 };
198 //---------------------------------------------------------------------------
199 #ifdef WIN32
200 unsigned long WINAPI RunW(void *);
201 #else
202 void * RunW(void *);
203 #endif
204
205 //---------------------------------------------------------------------------
206 #endif //IA_AUTH_C_H