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)
 
   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.
 
  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.
 
  20  $Date: 2010/03/15 12:57:24 $
 
  24 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
 
  26 //---------------------------------------------------------------------------
 
  29 #include "stg/blowfish.h"
 
  30 #include "stg/ia_packets.h"
 
  35 #pragma GCC diagnostic push
 
  36 #pragma GCC diagnostic ignored "-Wshadow"
 
  37 #include <jthread.hpp>
 
  38 #pragma GCC diagnostic pop
 
  41 #include <sys/types.h>
 
  42 #include <sys/socket.h>
 
  43 #include <netinet/in.h>
 
  48 #define IA_BIND_ERROR           (1)
 
  49 #define IA_SERVER_ERROR         (2)
 
  50 #define IA_FCNTL_ERROR          (3)
 
  51 #define IA_GETHOSTBYNAME_ERROR  (4)
 
  53 #define IA_PROTO_VER            (8)
 
  54 #define IA_PROTO_PROXY_VER      (101)
 
  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);
 
  62 //---------------------------------------------------------------------------
 
  66         IA_CLIENT_PROT(const std::string & sn, uint16_t p, const std::string & localName = "", uint16_t localPort = 0);
 
  71         void        GetStat(LOADSTAT * ls);
 
  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);
 
  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);
 
  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;
 
  96         void        SetProxyMode(bool on) { m_proxyMode = on; };
 
  97         bool        GetProxyMode() const { return m_proxyMode; };
 
  99         void        SetIP(uint32_t ip) { m_ip = ip; };
 
 100         uint32_t    GetIP() const { return m_ip; };
 
 103         void            Run(std::stop_token token);
 
 107         int             DeterminatePacketType(const char * buffer);
 
 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);
 
 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);
 
 122         void            FillHdr8(char* buffer, unsigned long ip);
 
 123         int             Send(char * buffer, int len);
 
 124         int             Recv(char * buffer, int len);
 
 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;
 
 137         BLOWFISH_CTX    m_ctxPass;
 
 138         BLOWFISH_CTX    m_ctxHdr;
 
 140         bool            m_selectedDirs[DIR_NUM];
 
 142         std::string     m_password;
 
 148         std::jthread m_thread;
 
 151         std::string     m_serverName;
 
 154         std::string     m_localName;
 
 156         uint32_t        m_localPort;
 
 158         struct sockaddr_in  m_servAddr;
 
 168         tpStatusChangedCb   m_pStatusChangedCb;
 
 169         tpStatChangedCb     m_pStatChangedCb;
 
 170         tpCallBackInfoFn    m_pInfoCb;
 
 171         tpCallBackErrorFn   m_pErrorCb;
 
 172         tpCallBackDirNameFn m_pDirNameCb;
 
 174         void              * m_statusChangedCbData;
 
 175         void              * m_statChangedCbData;
 
 177         void              * m_errorCbData;
 
 178         void              * m_dirNameCbData;
 
 180         std::map<std::string, int> m_packetTypes;
 
 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;
 
 192 //---------------------------------------------------------------------------