00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "FdmApp.h"
00008 #include "vmsBtSupport.h"
00009 #include "mfchelp.h"
00010
00011 #ifdef _DEBUG
00012 #undef THIS_FILE
00013 static char THIS_FILE[]=__FILE__;
00014 #define new DEBUG_NEW
00015 #endif
00016
00017 vmsBtSupport::vmsBtSupport()
00018 {
00019 m_pbDHTstate = NULL;
00020 InitializeCriticalSection (&m_cs1);
00021 }
00022
00023 vmsBtSupport::~vmsBtSupport()
00024 {
00025 DeleteCriticalSection (&m_cs1);
00026 Shutdown ();
00027 }
00028
00029 BOOL vmsBtSupport::Initialize()
00030 {
00031 if (false == LoadBtDll (m_dllBt))
00032 return FALSE;
00033 return LoadState ();
00034 }
00035
00036 vmsBtSession* vmsBtSupport::get_Session()
00037 {
00038 EnterCriticalSection (&m_cs1);
00039
00040 if (m_dllBt.is_Loaded () == false)
00041 Initialize ();
00042
00043 typedef vmsBtSession* (WINAPI *FNS)();
00044 static FNS _pfnSession = NULL;
00045
00046 if (_pfnSession == NULL)
00047 {
00048 _pfnSession = (FNS) m_dllBt.GetProcAddress ("vmsBt_getSession");
00049 if (_pfnSession)
00050 {
00051 _pfnSession ()->SetUserAgent (PRG_AGENT_NAME);
00052 ApplyListenPortSettings ();
00053 ApplyDHTSettings ();
00054 ApplyRestrainAllDownloadsMode ();
00055 ApplyProxySettings ();
00056 _DldsMgr.AttachToBtSession ();
00057 _DldsMgr.ApplyTrafficLimit ();
00058 }
00059 }
00060
00061 LeaveCriticalSection (&m_cs1);
00062
00063 if (_pfnSession)
00064 return _pfnSession ();
00065
00066 return NULL;
00067 }
00068
00069 BOOL vmsBtSupport::is_Initialized()
00070 {
00071 return m_dllBt.is_Loaded ();
00072 }
00073
00074 void vmsBtSupport::ApplyRestrainAllDownloadsMode()
00075 {
00076 if (is_Initialized ())
00077 {
00078 vmsBtSession *pBtSession = get_Session ();
00079
00080 int limit = fsInternetDownloader::is_PauseMode () ? 1 : -1;
00081 pBtSession->SetDownloadLimit (limit);
00082 pBtSession->SetUploadLimit (
00083 _App.Bittorrent_UploadTrafficLimit (_DldsMgr.GetTUM ()));
00084 }
00085 }
00086
00087 void vmsBtSupport::ApplyListenPortSettings()
00088 {
00089 if (is_Initialized ())
00090 {
00091 vmsBtSession *pBtSession = get_Session ();
00092
00093 int portFrom = _App.Bittorrent_ListenPort_From (),
00094 portTo = _App.Bittorrent_ListenPort_To ();
00095
00096 if (pBtSession->IsListening () == FALSE ||
00097 pBtSession->get_ListenPort () > portTo ||
00098 pBtSession->get_ListenPort () < portFrom)
00099 pBtSession->ListenOn (portFrom, portTo);
00100 }
00101 }
00102
00103 void vmsBtSupport::ApplyDHTSettings()
00104 {
00105 if (is_Initialized () == FALSE)
00106 return;
00107
00108 vmsBtSession *pBtSession = get_Session ();
00109
00110 if (_App.Bittorrent_EnableDHT ())
00111 {
00112 if (pBtSession->DHT_isStarted () == FALSE)
00113 pBtSession->DHT_start (m_pbDHTstate, m_dwDHTstateSize);
00114 }
00115 else
00116 {
00117 if (pBtSession->DHT_isStarted ())
00118 pBtSession->DHT_stop ();
00119 }
00120 }
00121
00122 BOOL vmsBtSupport::SaveState()
00123 {
00124 if (is_Initialized () == FALSE)
00125 return TRUE;
00126
00127 vmsBtSession *pBtSession = get_Session ();
00128
00129 if (pBtSession->DHT_isStarted ())
00130 {
00131 SAFE_DELETE_ARRAY (m_pbDHTstate);
00132
00133 if (FALSE == pBtSession->DHT_getState (NULL, 0, &m_dwDHTstateSize))
00134 return FALSE;
00135
00136 m_pbDHTstate = new BYTE [m_dwDHTstateSize];
00137
00138 if (FALSE == pBtSession->DHT_getState (m_pbDHTstate, m_dwDHTstateSize, &m_dwDHTstateSize))
00139 return FALSE;
00140 }
00141
00142 if (m_pbDHTstate == NULL)
00143 return TRUE;
00144
00145 HANDLE hFile = CreateFile (fsGetDataFilePath ("btdht.sav"), GENERIC_WRITE,
00146 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
00147 if (hFile == INVALID_HANDLE_VALUE)
00148 return FALSE;
00149
00150 DWORD dw;
00151 WriteFile (hFile, m_pbDHTstate, m_dwDHTstateSize, &dw, NULL);
00152
00153 CloseHandle (hFile);
00154
00155 return TRUE;
00156 }
00157
00158 BOOL vmsBtSupport::LoadState()
00159 {
00160 if (GetFileAttributes (fsGetDataFilePath ("btdht.sav")) == DWORD (-1))
00161 return TRUE;
00162
00163 HANDLE hFile = CreateFile (fsGetDataFilePath ("btdht.sav"), GENERIC_READ,
00164 0, NULL, OPEN_EXISTING, 0, NULL);
00165 if (hFile == INVALID_HANDLE_VALUE)
00166 return FALSE;
00167
00168 SAFE_DELETE_ARRAY (m_pbDHTstate);
00169 m_dwDHTstateSize = GetFileSize (hFile, NULL);
00170 m_pbDHTstate = new BYTE [m_dwDHTstateSize];
00171
00172 DWORD dw;
00173 ReadFile (hFile, m_pbDHTstate, m_dwDHTstateSize, &dw, NULL);
00174
00175 CloseHandle (hFile);
00176
00177 return TRUE;
00178 }
00179
00180 void vmsBtSupport::Shutdown()
00181 {
00182 if (is_Initialized () == FALSE)
00183 return;
00184
00185 typedef void (WINAPI *FNS)();
00186 FNS pfn = (FNS) m_dllBt.GetProcAddress ("vmsBt_Shutdown");
00187 if (pfn)
00188 pfn ();
00189
00190 m_dllBt.Free ();
00191 }
00192
00193 void vmsBtSupport::ApplyProxySettings()
00194 {
00195 if (is_Initialized () == FALSE)
00196 return;
00197
00198 fsString strIp, strUser, strPwd;
00199 int nPort = 0;
00200
00201 switch (_App.InternetAccessType ())
00202 {
00203 case IATE_PRECONFIGPROXY:
00204 GetIeProxySettings (strIp, strUser, strPwd, nPort);
00205 break;
00206
00207 case IATE_NOPROXY:
00208 break;
00209
00210 case IATE_MANUALPROXY:
00211 strIp = _App.HttpProxy_Name ();
00212 strUser = _App.HttpProxy_UserName ();
00213 strPwd = _App.HttpProxy_UserName ();
00214 if (strIp.IsEmpty () == FALSE)
00215 {
00216 char sz [1000];
00217 strcpy (sz, strIp);
00218 LPSTR pszPort = strrchr (sz, ':');
00219 if (pszPort)
00220 {
00221 *pszPort++ = 0;
00222 nPort = atoi (pszPort);
00223 strIp = sz;
00224 }
00225 }
00226 break;
00227
00228 case IATE_FIREFOXPROXY:
00229 GetFirefoxProxySettings (strIp, strUser, strPwd, nPort);
00230 break;
00231 }
00232
00233 get_Session ()->SetProxySettings (strIp, nPort, strUser, strPwd);
00234 }
00235
00236 void vmsBtSupport::GetIeProxySettings(fsString &strIp, fsString &strUser, fsString &strPwd, int &nPort)
00237 {
00238 strIp = strUser = strPwd = "";
00239 nPort = 0;
00240
00241 CRegKey key;
00242 if (ERROR_SUCCESS != key.Open (HKEY_CURRENT_USER,
00243 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
00244 KEY_READ))
00245 return;
00246
00247 DWORD dw;
00248 if (ERROR_SUCCESS != key.QueryValue (dw, "ProxyEnable"))
00249 return;
00250 if (dw == FALSE)
00251 return;
00252
00253 char szProxy [1000];
00254 dw = sizeof (szProxy);
00255 if (ERROR_SUCCESS != key.QueryValue (szProxy, "ProxyServer", &dw))
00256 return;
00257
00258 LPSTR pszPort = strrchr (szProxy, ':');
00259 if (pszPort)
00260 {
00261 *pszPort = 0;
00262 pszPort++;
00263 nPort = atoi (pszPort);
00264 }
00265
00266 strIp = szProxy;
00267 }
00268
00269 void vmsBtSupport::GetFirefoxProxySettings(fsString &strIp, fsString &strUser, fsString &strPwd, int &nPort)
00270 {
00271 strIp = strUser = strPwd = "";
00272 nPort = 0;
00273
00274 if (1 != _App.FirefoxSettings_Proxy_Type ())
00275 return;
00276
00277 strIp = _App.FirefoxSettings_Proxy_Addr ("http");
00278 nPort = _App.FirefoxSettings_Proxy_Port ("http");
00279 }
00280
00281 vmsBtFile* vmsBtSupport::CreateTorrentFileObject()
00282 {
00283 if (m_dllBtFile.is_Loaded () == false &&
00284 false == LoadBtDll (m_dllBtFile))
00285 return NULL;
00286
00287 typedef vmsBtFile* (WINAPI *FNBTF)();
00288 static FNBTF _pfnCreateTorrentFileObject = NULL;
00289
00290 if (_pfnCreateTorrentFileObject == NULL)
00291 {
00292 _pfnCreateTorrentFileObject = (FNBTF) m_dllBtFile.GetProcAddress ("vmsBt_CreateTorrentFileObject");
00293 if (_pfnCreateTorrentFileObject == NULL)
00294 return NULL;
00295 }
00296
00297 return _pfnCreateTorrentFileObject ();
00298 }
00299
00300 bool vmsBtSupport::LoadBtDll(vmsDLL &dll)
00301 {
00302 CString str = ((CFdmApp*)AfxGetApp ())->m_strAppPath;
00303 if (str [str.GetLength () - 1] != '\\')
00304 str += '\\';
00305 str += "fdmbtsupp.dll";
00306 return dll.Load (str);
00307 }
00308