]> git.stg.codes - stg.git/blob - libs/ia/include/stg/ia.h
Port to CMake, get rid of os_int.h.
[stg.git] / libs / ia / 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 typedef void (*tpStatusChangedCb)(int status, void * data);
55 typedef void (*tpStatChangedCb)(const LOADSTAT & stat, void * data);
56 typedef void (*tpCallBackInfoFn)(const std::string & message, int infoType, int showTime, int sendTime, void * data);
57 typedef void (*tpCallBackErrorFn)(const std::string & message, int netError, void * data);
58 typedef void (*tpCallBackDirNameFn)(const std::vector<std::string> & dirName, void * data);
59
60 //---------------------------------------------------------------------------
61 class IA_CLIENT_PROT
62 {
63 #ifdef WIN32
64 friend unsigned long WINAPI RunW(void * data);
65 #else
66 friend void * RunL(void * data);
67 #endif
68
69 public:
70     IA_CLIENT_PROT(const std::string & sn, uint16_t p, const std::string & localName = "", uint16_t localPort = 0);
71     ~IA_CLIENT_PROT();
72
73     void        Start();
74     void        Stop();
75     void        GetStat(LOADSTAT * ls);
76
77     void        SetServer(const std::string & sn, unsigned short port);
78     void        SetLogin(const std::string & login);
79     void        SetPassword(const std::string & password);
80     void        SetEnabledDirs(const bool * selectedDirs);
81
82     void        SetStatusChangedCb(tpStatusChangedCb p, void * data);
83     void        SetStatChangedCb(tpStatChangedCb p, void * data);
84     void        SetInfoCb(tpCallBackInfoFn p, void * data);
85     void        SetErrorCb(tpCallBackErrorFn p, void * data);
86     void        SetDirNameCb(tpCallBackDirNameFn p, void * data);
87
88     int         Connect();
89     int         Disconnect();
90     int         GetAuthorized() const { return phase == 3 || phase == 4; };
91     int         GetPhase() const { return phase; };
92     int         GetStatus() const;
93     int         GetReconnect() const { return reconnect; };
94     void        SetReconnect(int r) { reconnect = r; };
95     char        GetProtoVer() const { return proxyMode ? IA_PROTO_PROXY_VER : IA_PROTO_VER; };
96     void        GetMessageText(std::string * text) const { *text = messageText; };
97     void        GetInfoText(std::string * text) const { *text = infoText; };
98     int         GetStrError(std::string * error) const;
99
100     void        SetProxyMode(bool on) { proxyMode = on; };
101     bool        GetProxyMode() const { return proxyMode; };
102
103     void        SetIP(uint32_t ip) { IA_CLIENT_PROT::ip = ip; };
104     uint32_t    GetIP() const { return ip; };
105
106 private:
107     void            Run();
108     int             NetRecv();
109     int             NetSend(int n);
110     bool            GetNonstop() const { return nonstop; };
111     void            PrepareNet();
112     int             DeterminatePacketType(const char * buffer);
113
114     int             Process_CONN_SYN_ACK_8(const char * buffer);
115     int             Process_ALIVE_SYN_8(const char * buffer);
116     int             Process_DISCONN_SYN_ACK_8(const char * buffer);
117     int             Process_FIN_8(const char * buffer);
118     int             Process_INFO_8(const char * buffer);
119     int             Process_ERROR(const char * buffer);
120
121     int             Prepare_CONN_SYN_8(char * buffer);
122     int             Prepare_CONN_ACK_8(char * buffer);
123     int             Prepare_ALIVE_ACK_8(char * buffer);
124     int             Prepare_DISCONN_SYN_8(char * buffer);
125     int             Prepare_DISCONN_ACK_8(char * buffer);
126
127     void            FillHdr8(char * buffer, unsigned long ip);
128     int             Send(char * buffer, int len);
129     int             Recv(char * buffer, int len);
130
131     LOADSTAT        stat;
132     int             action;
133     int             phase;
134     int             phaseTime;
135     std::string     messageText;
136     std::string     infoText;
137     mutable std::string strError;
138     mutable int     codeError;
139     bool            nonstop;
140     bool            isNetPrepared;
141     bool            proxyMode;
142
143     BLOWFISH_CTX    ctxPass;
144     BLOWFISH_CTX    ctxHdr;
145
146     bool            selectedDirs[DIR_NUM];
147
148     std::string     password;
149     std::string     login;
150
151     #ifdef WIN32
152     WSADATA wsaData;
153     #else
154     pthread_t thread;
155     #endif
156
157     std::string     serverName;
158     uint16_t        port;
159     uint32_t        ip;
160     std::string     localName;
161     uint32_t        localIP;
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     std::map<std::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