]> git.stg.codes - stg.git/blob - libs/ia/include/stg/ia.h
Fight CLang warnings.
[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 #pragma once
28
29 #include "stg/blowfish.h"
30 #include "stg/ia_packets.h"
31
32 #include <string>
33 #include <vector>
34 #include <map>
35
36 #ifndef WIN32
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <pthread.h>
41 #else
42 #include <winsock2.h>
43 #endif
44
45 #define IA_BIND_ERROR           (1)
46 #define IA_SERVER_ERROR         (2)
47 #define IA_FCNTL_ERROR          (3)
48 #define IA_GETHOSTBYNAME_ERROR  (4)
49
50 #define IA_PROTO_VER            (8)
51 #define IA_PROTO_PROXY_VER      (101)
52
53 typedef void (*tpStatusChangedCb)(int status, void * data);
54 typedef void (*tpStatChangedCb)(const LOADSTAT & stat, void * data);
55 typedef void (*tpCallBackInfoFn)(const std::string & message, int infoType, int showTime, int sendTime, void * data);
56 typedef void (*tpCallBackErrorFn)(const std::string & message, int netError, void * data);
57 typedef void (*tpCallBackDirNameFn)(const std::vector<std::string> & dirName, void * data);
58
59 //---------------------------------------------------------------------------
60 class IA_CLIENT_PROT
61 {
62 #ifdef WIN32
63 friend unsigned long WINAPI RunW(void * data);
64 #else
65 friend void * RunL(void * data);
66 #endif
67
68     public:
69         IA_CLIENT_PROT(const std::string & sn, uint16_t p, const std::string & localName = "", uint16_t localPort = 0);
70         ~IA_CLIENT_PROT();
71
72         void        Start();
73         void        Stop();
74         void        GetStat(LOADSTAT * ls);
75
76         void        SetServer(const std::string & sn, unsigned short port);
77         void        SetLogin(const std::string & login);
78         void        SetPassword(const std::string & password);
79         void        SetEnabledDirs(const bool * selectedDirs);
80
81         void        SetStatusChangedCb(tpStatusChangedCb p, void * data);
82         void        SetStatChangedCb(tpStatChangedCb p, void * data);
83         void        SetInfoCb(tpCallBackInfoFn p, void * data);
84         void        SetErrorCb(tpCallBackErrorFn p, void * data);
85         void        SetDirNameCb(tpCallBackDirNameFn p, void * data);
86
87         int         Connect();
88         int         Disconnect();
89         int         GetAuthorized() const { return m_phase == 3 || m_phase == 4; };
90         int         GetPhase() const { return m_phase; };
91         int         GetStatus() const;
92         int         GetReconnect() const { return m_reconnect; };
93         void        SetReconnect(int r) { m_reconnect = r; };
94         char        GetProtoVer() const { return m_proxyMode ? IA_PROTO_PROXY_VER : IA_PROTO_VER; };
95         void        GetMessageText(std::string * text) const { *text = m_messageText; };
96         void        GetInfoText(std::string * text) const { *text = m_infoText; };
97         int         GetStrError(std::string * error) const;
98
99         void        SetProxyMode(bool on) { m_proxyMode = on; };
100         bool        GetProxyMode() const { return m_proxyMode; };
101
102         void        SetIP(uint32_t ip) { m_ip = ip; };
103         uint32_t    GetIP() const { return m_ip; };
104
105     private:
106         void            Run();
107         int             NetRecv();
108         int             NetSend(int n);
109         bool            GetNonstop() const { return m_nonstop; };
110         void            PrepareNet();
111         int             DeterminatePacketType(const char * buffer);
112
113         int             Process_CONN_SYN_ACK_8(const void* buffer);
114         int             Process_ALIVE_SYN_8(const void* buffer);
115         int             Process_DISCONN_SYN_ACK_8(const void* buffer);
116         int             Process_FIN_8(const void* buffer);
117         int             Process_INFO_8(const void* buffer);
118         int             Process_ERROR(const void* buffer);
119
120         int             Prepare_CONN_SYN_8(void* buffer);
121         int             Prepare_CONN_ACK_8(void* buffer);
122         int             Prepare_ALIVE_ACK_8(void* buffer);
123         int             Prepare_DISCONN_SYN_8(void* buffer);
124         int             Prepare_DISCONN_ACK_8(void* buffer);
125
126         void            FillHdr8(char* buffer, unsigned long ip);
127         int             Send(char * buffer, int len);
128         int             Recv(char * buffer, int len);
129
130         LOADSTAT        m_stat;
131         int             m_action;
132         int             m_phase;
133         int             m_phaseTime;
134         std::string     m_messageText;
135         std::string     m_infoText;
136         mutable std::string m_strError;
137         mutable int     m_codeError;
138         bool            m_nonstop;
139         bool            m_isNetPrepared;
140         bool            m_proxyMode;
141
142         BLOWFISH_CTX    m_ctxPass;
143         BLOWFISH_CTX    m_ctxHdr;
144
145         bool            m_selectedDirs[DIR_NUM];
146
147         std::string     m_password;
148         std::string     m_login;
149
150         #ifdef WIN32
151         WSADATA m_wsaData;
152         #else
153         pthread_t m_thread;
154         #endif
155
156         std::string     m_serverName;
157         uint16_t        m_port;
158         uint32_t        m_ip;
159         std::string     m_localName;
160         uint32_t        m_localIP;
161         uint32_t        m_localPort;
162
163         struct sockaddr_in  m_servAddr;
164
165         bool            m_firstConnect;
166         int             m_reconnect;
167         int             m_sockr;
168         int             m_protNum;
169         int             m_userTimeout;
170         int             m_aliveTimeout;
171         unsigned int    m_rnd;
172
173         tpStatusChangedCb   m_pStatusChangedCb;
174         tpStatChangedCb     m_pStatChangedCb;
175         tpCallBackInfoFn    m_pInfoCb;
176         tpCallBackErrorFn   m_pErrorCb;
177         tpCallBackDirNameFn m_pDirNameCb;
178
179         void              * m_statusChangedCbData;
180         void              * m_statChangedCbData;
181         void              * m_infoCbData;
182         void              * m_errorCbData;
183         void              * m_dirNameCbData;
184
185         std::map<std::string, int> m_packetTypes;
186
187         CONN_SYN_8        * m_connSyn8;
188         const CONN_SYN_ACK_8    * m_connSynAck8;
189         CONN_ACK_8        * m_connAck8;
190         const ALIVE_SYN_8       * m_aliveSyn8;
191         ALIVE_ACK_8       * m_aliveAck8;
192         DISCONN_SYN_8     * m_disconnSyn8;
193         const DISCONN_SYN_ACK_8 * m_disconnSynAck8;
194         DISCONN_ACK_8     * m_disconnAck8;
195         const INFO_8            * m_info;
196 };
197 //---------------------------------------------------------------------------
198 #ifdef WIN32
199 unsigned long WINAPI RunW(void *);
200 #else
201 void * RunW(void *);
202 #endif