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