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"
 
  37 #include <sys/types.h>
 
  38 #include <sys/socket.h>
 
  39 #include <netinet/in.h>
 
  45 #define IA_BIND_ERROR           (1)
 
  46 #define IA_SERVER_ERROR         (2)
 
  47 #define IA_FCNTL_ERROR          (3)
 
  48 #define IA_GETHOSTBYNAME_ERROR  (4)
 
  50 #define IA_PROTO_VER            (8)
 
  51 #define IA_PROTO_PROXY_VER      (101)
 
  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);
 
  59 //---------------------------------------------------------------------------
 
  63 friend unsigned long WINAPI RunW(void * data);
 
  65 friend void * RunL(void * data);
 
  69         IA_CLIENT_PROT(const std::string & sn, uint16_t p, const std::string & localName = "", uint16_t localPort = 0);
 
  74         void        GetStat(LOADSTAT * ls);
 
  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);
 
  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);
 
  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;
 
  99         void        SetProxyMode(bool on) { m_proxyMode = on; };
 
 100         bool        GetProxyMode() const { return m_proxyMode; };
 
 102         void        SetIP(uint32_t ip) { m_ip = ip; };
 
 103         uint32_t    GetIP() const { return m_ip; };
 
 109         bool            GetNonstop() const { return m_nonstop; };
 
 111         int             DeterminatePacketType(const char * buffer);
 
 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);
 
 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);
 
 126         void            FillHdr8(char* buffer, unsigned long ip);
 
 127         int             Send(char * buffer, int len);
 
 128         int             Recv(char * buffer, int len);
 
 134         std::string     m_messageText;
 
 135         std::string     m_infoText;
 
 136         mutable std::string m_strError;
 
 137         mutable int     m_codeError;
 
 139         bool            m_isNetPrepared;
 
 142         BLOWFISH_CTX    m_ctxPass;
 
 143         BLOWFISH_CTX    m_ctxHdr;
 
 145         bool            m_selectedDirs[DIR_NUM];
 
 147         std::string     m_password;
 
 156         std::string     m_serverName;
 
 159         std::string     m_localName;
 
 161         uint32_t        m_localPort;
 
 163         struct sockaddr_in  m_servAddr;
 
 173         tpStatusChangedCb   m_pStatusChangedCb;
 
 174         tpStatChangedCb     m_pStatChangedCb;
 
 175         tpCallBackInfoFn    m_pInfoCb;
 
 176         tpCallBackErrorFn   m_pErrorCb;
 
 177         tpCallBackDirNameFn m_pDirNameCb;
 
 179         void              * m_statusChangedCbData;
 
 180         void              * m_statChangedCbData;
 
 182         void              * m_errorCbData;
 
 183         void              * m_dirNameCbData;
 
 185         std::map<std::string, int> m_packetTypes;
 
 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;
 
 197 //---------------------------------------------------------------------------
 
 199 unsigned long WINAPI RunW(void *);