• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

FFFTPのソースコードです。


Commit MetaInfo

修订版f453e0e463e50f18f50f051f23fa08c8eb28dd54 (tree)
时间2011-10-16 02:11:32
作者s_kawamoto <s_kawamoto@user...>
Commiters_kawamoto

Log Message

Fix bugs of asyncronous socket control.
Fix bugs of simultaneous connection.
Change to reuse SSL sessions.
This version may be nearly stable (not for 1.99 but for 1.98b).

更改概述

差异

Binary files a/FFFTP_Eng_Release/FFFTP.exe and b/FFFTP_Eng_Release/FFFTP.exe differ
Binary files a/Release/FFFTP.exe and b/Release/FFFTP.exe differ
--- a/common.h
+++ b/common.h
@@ -1028,8 +1028,6 @@ typedef struct transpacket {
10281028 int Mode; /* 転送モード (EXIST_xxx) */
10291029 HWND hWndTrans; /* 転送中ダイアログのウインドウハンドル */
10301030 int Abort; /* 転送中止フラグ (ABORT_xxx) */
1031- // 暗号化通信対応
1032- int CryptMode; /* 暗号化通信モード (CRYPT_xxx) */
10331031 // 同時接続対応
10341032 int ThreadCount;
10351033 struct transpacket *Next;
--- a/connect.c
+++ b/connect.c
@@ -1304,7 +1304,7 @@ static SOCKET DoConnectCrypt(int CryptMode, HOSTDATA* HostData, char *Host, char
13041304 // ;
13051305 if(CryptMode == CRYPT_FTPIS)
13061306 {
1307- if(AttachSSL(ContSock))
1307+ if(AttachSSL(ContSock, INVALID_SOCKET))
13081308 {
13091309 while((Sts = ReadReplyMessage(ContSock, Buf, 1024, &CancelFlg, TmpBuf) / 100) == FTP_PRELIM)
13101310 ;
@@ -1419,7 +1419,7 @@ static SOCKET DoConnectCrypt(int CryptMode, HOSTDATA* HostData, char *Host, char
14191419 {
14201420 if(IsOpenSSLLoaded() && (Sts = command(ContSock, Reply, &CancelFlg, "AUTH TLS")) == 234)
14211421 {
1422- if(AttachSSL(ContSock))
1422+ if(AttachSSL(ContSock, INVALID_SOCKET))
14231423 {
14241424 if((Sts = command(ContSock, Reply, &CancelFlg, "PBSZ 0")) == 200)
14251425 {
--- a/getput.c
+++ b/getput.c
@@ -107,6 +107,8 @@ static int IsSpecialDevice(char *Fname);
107107 static int MirrorDelNotify(int Cur, int Notify, TRANSPACKET *Pkt);
108108 static BOOL CALLBACK MirrorDeleteDialogCallBack(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam);
109109 static void SetErrorMsg(char *fmt, ...);
110+// 同時接続対応
111+static char* GetErrMsg();
110112
111113 /*===== ローカルなワーク =====*/
112114
@@ -141,7 +143,11 @@ static int KeepDlg = NO; /* 転送中ダイアログを消さないかどうか
141143 static int MoveToForeground = NO; /* ウインドウを前面に移動するかどうか (YES/NO) */
142144
143145 static char CurDir[FMAX_PATH+1] = { "" };
144-static char ErrMsg[ERR_MSG_LEN+7];
146+// 同時接続対応
147+//static char ErrMsg[ERR_MSG_LEN+7];
148+static char ErrMsg[MAX_DATA_CONNECTION+1][ERR_MSG_LEN+7];
149+static DWORD ErrMsgThreadId[MAX_DATA_CONNECTION+1];
150+static HANDLE hErrMsgMutex;
145151
146152 // 同時接続対応
147153 static int WaitForMainThread = NO;
@@ -175,6 +181,8 @@ int MakeTransferThread(void)
175181
176182 hListAccMutex = CreateMutex( NULL, FALSE, NULL );
177183 hRunMutex = CreateMutex( NULL, TRUE, NULL );
184+ // 同時接続対応
185+ hErrMsgMutex = CreateMutex( NULL, FALSE, NULL );
178186
179187 ClearAll = NO;
180188 ForceAbort = NO;
@@ -236,6 +244,8 @@ void CloseTransferThread(void)
236244
237245 CloseHandle( hListAccMutex );
238246 CloseHandle( hRunMutex );
247+ // 同時接続対応
248+ CloseHandle( hErrMsgMutex );
239249 return;
240250 }
241251
@@ -682,7 +692,8 @@ static ULONG WINAPI TransferThread(void *Dummy)
682692 BackgrndMessageProc();
683693 Sleep(1);
684694 }
685- memset(ErrMsg, NUL, ERR_MSG_LEN+7);
695+// memset(ErrMsg, NUL, ERR_MSG_LEN+7);
696+ memset(GetErrMsg(), NUL, ERR_MSG_LEN+7);
686697
687698 // Canceled = NO;
688699 Canceled[ThreadCount] = NO;
@@ -1304,12 +1315,12 @@ static int DownLoadNonPassive(TRANSPACKET *Pkt, int *CancelCheckWork)
13041315 ReleaseMutex(hListAccMutex);
13051316 // FTPS対応
13061317 // iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);
1307- if(AskCryptMode() == CRYPT_FTPES || AskCryptMode() == CRYPT_FTPIS)
1318+ if(IsSSLAttached(Pkt->ctrl_skt))
13081319 {
1309- if(AttachSSL(data_socket))
1320+ if(AttachSSL(data_socket, Pkt->ctrl_skt))
13101321 iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);
13111322 else
1312- iRetCode = FTP_ERROR;
1323+ iRetCode = 500;
13131324 }
13141325 else
13151326 iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);
@@ -1380,12 +1391,12 @@ static int DownLoadPassive(TRANSPACKET *Pkt, int *CancelCheckWork)
13801391 ReleaseMutex(hListAccMutex);
13811392 // FTPS対応
13821393 // iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);
1383- if(AskCryptMode() == CRYPT_FTPES || AskCryptMode() == CRYPT_FTPIS)
1394+ if(IsSSLAttached(Pkt->ctrl_skt))
13841395 {
1385- if(AttachSSL(data_socket))
1396+ if(AttachSSL(data_socket, Pkt->ctrl_skt))
13861397 iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);
13871398 else
1388- iRetCode = FTP_ERROR;
1399+ iRetCode = 500;
13891400 }
13901401 else
13911402 iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);
@@ -2094,7 +2105,9 @@ static BOOL CALLBACK UpDownErrorDialogProc(HWND hDlg, UINT message, WPARAM wPara
20942105 {
20952106 case WM_INITDIALOG :
20962107 SendDlgItemMessage(hDlg, UPDOWN_ERR_FNAME, WM_SETTEXT, 0, (LPARAM)lParam);
2097- SendDlgItemMessage(hDlg, UPDOWN_ERR_MSG, WM_SETTEXT, 0, (LPARAM)ErrMsg);
2108+ // 同時接続対応
2109+// SendDlgItemMessage(hDlg, UPDOWN_ERR_MSG, WM_SETTEXT, 0, (LPARAM)ErrMsg);
2110+ SendDlgItemMessage(hDlg, UPDOWN_ERR_MSG, WM_SETTEXT, 0, (LPARAM)GetErrMsg());
20982111 return(TRUE);
20992112
21002113 case WM_COMMAND :
@@ -2340,12 +2353,12 @@ static int UpLoadNonPassive(TRANSPACKET *Pkt)
23402353 ReleaseMutex(hListAccMutex);
23412354 // FTPS対応
23422355 // iRetCode = UpLoadFile(Pkt, data_socket);
2343- if(AskCryptMode() == CRYPT_FTPES || AskCryptMode() == CRYPT_FTPIS)
2356+ if(IsSSLAttached(Pkt->ctrl_skt))
23442357 {
2345- if(AttachSSL(data_socket))
2358+ if(AttachSSL(data_socket, Pkt->ctrl_skt))
23462359 iRetCode = UpLoadFile(Pkt, data_socket);
23472360 else
2348- iRetCode = FTP_ERROR;
2361+ iRetCode = 500;
23492362 }
23502363 else
23512364 iRetCode = UpLoadFile(Pkt, data_socket);
@@ -2422,12 +2435,12 @@ static int UpLoadPassive(TRANSPACKET *Pkt)
24222435 ReleaseMutex(hListAccMutex);
24232436 // FTPS対応
24242437 // iRetCode = UpLoadFile(Pkt, data_socket);
2425- if(AskCryptMode() == CRYPT_FTPES || AskCryptMode() == CRYPT_FTPIS)
2438+ if(IsSSLAttached(Pkt->ctrl_skt))
24262439 {
2427- if(AttachSSL(data_socket))
2440+ if(AttachSSL(data_socket, Pkt->ctrl_skt))
24282441 iRetCode = UpLoadFile(Pkt, data_socket);
24292442 else
2430- iRetCode = FTP_ERROR;
2443+ iRetCode = 500;
24312444 }
24322445 else
24332446 iRetCode = UpLoadFile(Pkt, data_socket);
@@ -3561,10 +3574,14 @@ static void SetErrorMsg(char *fmt, ...)
35613574 {
35623575 va_list Args;
35633576
3564- if(strlen(ErrMsg) == 0)
3577+ // 同時接続対応
3578+// if(strlen(ErrMsg) == 0)
3579+ if(strlen(GetErrMsg()) == 0)
35653580 {
35663581 va_start(Args, fmt);
3567- wvsprintf(ErrMsg, fmt, Args);
3582+ // 同時接続対応
3583+// wvsprintf(ErrMsg, fmt, Args);
3584+ wvsprintf(GetErrMsg(), fmt, Args);
35683585 va_end(Args);
35693586 }
35703587 return;
@@ -3606,3 +3623,39 @@ int CheckPathViolation(TRANSPACKET *packet)
36063623 }
36073624
36083625
3626+// 同時接続対応
3627+static char* GetErrMsg()
3628+{
3629+ char* r;
3630+ DWORD ThreadId;
3631+ int i;
3632+ r = NULL;
3633+ WaitForSingleObject(hErrMsgMutex, INFINITE);
3634+ ThreadId = GetCurrentThreadId();
3635+ i = 0;
3636+ while(i < MAX_DATA_CONNECTION + 1)
3637+ {
3638+ if(ErrMsgThreadId[i] == ThreadId)
3639+ {
3640+ r = ErrMsg[i];
3641+ break;
3642+ }
3643+ i++;
3644+ }
3645+ if(!r)
3646+ {
3647+ i = 0;
3648+ while(i < MAX_DATA_CONNECTION + 1)
3649+ {
3650+ if(ErrMsgThreadId[i] == 0)
3651+ {
3652+ ErrMsgThreadId[i] = ThreadId;
3653+ r = ErrMsg[i];
3654+ break;
3655+ }
3656+ i++;
3657+ }
3658+ }
3659+ ReleaseMutex(hErrMsgMutex);
3660+ return r;
3661+}
--- a/socket.c
+++ b/socket.c
@@ -271,6 +271,8 @@ static LRESULT CALLBACK SocketWndProc(HWND hWnd, UINT message, WPARAM wParam, LP
271271 break;
272272
273273 case WM_ASYNC_DBASE :
274+ // APIの仕様上ハンドルが登録される前にウィンドウメッセージが呼び出される可能性あり
275+ RegistAsyncTableDbase((HANDLE)wParam);
274276 // スレッド衝突のバグ修正
275277 WaitForSingleObject(hAsyncTblAccMutex, INFINITE);
276278 for(Pos = 0; Pos < MAX_SIGNAL_ENTRY_DBASE; Pos++)
@@ -291,31 +293,6 @@ static LRESULT CALLBACK SocketWndProc(HWND hWnd, UINT message, WPARAM wParam, LP
291293 break;
292294 }
293295 }
294- // APIの仕様上ハンドルが登録される前にウィンドウメッセージが呼び出される可能性あり
295- if(Pos == MAX_SIGNAL_ENTRY_DBASE)
296- {
297- for(Pos = 0; Pos < MAX_SIGNAL_ENTRY_DBASE; Pos++)
298- {
299- if(SignalDbase[Pos].Async == 0)
300- {
301- SignalDbase[Pos].Async = (HANDLE)wParam;
302- SignalDbase[Pos].Done = 0;
303- SignalDbase[Pos].ErrorDb = 0;
304- if(HIWORD(lParam) != 0)
305- {
306- SignalDbase[Pos].ErrorDb = 1;
307-#if DBG_MSG
308- DoPrintf("##### SignalDatabase: error");
309-#endif
310- }
311- SignalDbase[Pos].Done = 1;
312-#if DBG_MSG
313- DoPrintf("##### SignalDatabase: Done");
314-#endif
315- break;
316- }
317- }
318- }
319296 // スレッド衝突のバグ修正
320297 ReleaseMutex(hAsyncTblAccMutex);
321298 break;
@@ -738,6 +715,9 @@ int do_closesocket(SOCKET s)
738715 #endif
739716 CancelCheckWork = NO;
740717
718+ // スレッド衝突のバグ修正
719+ WSAAsyncSelect(s, hWndSocket, WM_ASYNC_SOCKET, 0);
720+ UnRegistAsyncTable(s);
741721 // FTPS対応
742722 // Ret = closesocket(s);
743723 Ret = closesocketS(s);
@@ -755,10 +735,12 @@ int do_closesocket(SOCKET s)
755735 Ret = 0;
756736 }
757737
758- WSAAsyncSelect(s, hWndSocket, WM_ASYNC_SOCKET, 0);
738+ // スレッド衝突のバグ修正
739+// WSAAsyncSelect(s, hWndSocket, WM_ASYNC_SOCKET, 0);
759740 if(BackgrndMessageProc() == YES)
760741 CancelCheckWork = YES;
761- UnRegistAsyncTable(s);
742+ // スレッド衝突のバグ修正
743+// UnRegistAsyncTable(s);
762744
763745 #if DBG_MSG
764746 DoPrintf("# Exit close");
--- a/socketwrapper.c
+++ b/socketwrapper.c
@@ -30,6 +30,8 @@ typedef int (__cdecl* _SSL_read)(SSL*, void*, int);
3030 typedef int (__cdecl* _SSL_get_error)(SSL*, int);
3131 typedef X509* (__cdecl* _SSL_get_peer_certificate)(const SSL*);
3232 typedef long (__cdecl* _SSL_get_verify_result)(const SSL*);
33+typedef SSL_SESSION* (__cdecl* _SSL_get_session)(SSL*);
34+typedef int (__cdecl* _SSL_set_session)(SSL*, SSL_SESSION*);
3335 typedef BIO_METHOD* (__cdecl* _BIO_s_mem)();
3436 typedef BIO* (__cdecl* _BIO_new)(BIO_METHOD*);
3537 typedef int (__cdecl* _BIO_free)(BIO*);
@@ -37,7 +39,6 @@ typedef long (__cdecl* _BIO_ctrl)(BIO*, int, long, void*);
3739 typedef void (__cdecl* _X509_free)(X509*);
3840 typedef int (__cdecl* _X509_print_ex)(BIO*, X509*, unsigned long, unsigned long);
3941 typedef X509_NAME* (__cdecl* _X509_get_subject_name)(X509*);
40-typedef X509_NAME* (__cdecl* _X509_get_issuer_name)(X509*);
4142 typedef int (__cdecl* _X509_NAME_print_ex)(BIO*, X509_NAME*, int, unsigned long);
4243
4344 _SSL_load_error_strings p_SSL_load_error_strings;
@@ -58,6 +59,8 @@ _SSL_read p_SSL_read;
5859 _SSL_get_error p_SSL_get_error;
5960 _SSL_get_peer_certificate p_SSL_get_peer_certificate;
6061 _SSL_get_verify_result p_SSL_get_verify_result;
62+_SSL_get_session p_SSL_get_session;
63+_SSL_set_session p_SSL_set_session;
6164 _BIO_s_mem p_BIO_s_mem;
6265 _BIO_new p_BIO_new;
6366 _BIO_free p_BIO_free;
@@ -65,7 +68,6 @@ _BIO_ctrl p_BIO_ctrl;
6568 _X509_free p_X509_free;
6669 _X509_print_ex p_X509_print_ex;
6770 _X509_get_subject_name p_X509_get_subject_name;
68-_X509_get_issuer_name p_X509_get_issuer_name;
6971 _X509_NAME_print_ex p_X509_NAME_print_ex;
7072
7173 #define MAX_SSL_SOCKET 64
@@ -124,7 +126,9 @@ BOOL LoadOpenSSL()
124126 || !(p_SSL_read = (_SSL_read)GetProcAddress(g_hOpenSSL, "SSL_read"))
125127 || !(p_SSL_get_error = (_SSL_get_error)GetProcAddress(g_hOpenSSL, "SSL_get_error"))
126128 || !(p_SSL_get_peer_certificate = (_SSL_get_peer_certificate)GetProcAddress(g_hOpenSSL, "SSL_get_peer_certificate"))
127- || !(p_SSL_get_verify_result = (_SSL_get_verify_result)GetProcAddress(g_hOpenSSL, "SSL_get_verify_result")))
129+ || !(p_SSL_get_verify_result = (_SSL_get_verify_result)GetProcAddress(g_hOpenSSL, "SSL_get_verify_result"))
130+ || !(p_SSL_get_session = (_SSL_get_session)GetProcAddress(g_hOpenSSL, "SSL_get_session"))
131+ || !(p_SSL_set_session = (_SSL_set_session)GetProcAddress(g_hOpenSSL, "SSL_set_session")))
128132 {
129133 if(g_hOpenSSL)
130134 FreeLibrary(g_hOpenSSL);
@@ -140,7 +144,6 @@ BOOL LoadOpenSSL()
140144 || !(p_X509_free = (_X509_free)GetProcAddress(g_hOpenSSLCommon, "X509_free"))
141145 || !(p_X509_print_ex = (_X509_print_ex)GetProcAddress(g_hOpenSSLCommon, "X509_print_ex"))
142146 || !(p_X509_get_subject_name = (_X509_get_subject_name)GetProcAddress(g_hOpenSSLCommon, "X509_get_subject_name"))
143- || !(p_X509_get_issuer_name = (_X509_get_issuer_name)GetProcAddress(g_hOpenSSLCommon, "X509_get_issuer_name"))
144147 || !(p_X509_NAME_print_ex = (_X509_NAME_print_ex)GetProcAddress(g_hOpenSSLCommon, "X509_NAME_print_ex")))
145148 {
146149 if(g_hOpenSSL)
@@ -330,11 +333,13 @@ BOOL IsHostNameMatched(LPCSTR HostName, LPCSTR CommonName)
330333 return bResult;
331334 }
332335
333-BOOL AttachSSL(SOCKET s)
336+BOOL AttachSSL(SOCKET s, SOCKET parent)
334337 {
335338 BOOL r;
336339 DWORD Time;
337340 SSL** ppSSL;
341+ SSL** ppSSLParent;
342+ SSL_SESSION* pSession;
338343 if(!g_bOpenSSLLoaded)
339344 return FALSE;
340345 r = FALSE;
@@ -350,8 +355,20 @@ BOOL AttachSSL(SOCKET s)
350355 {
351356 if(p_SSL_set_fd(*ppSSL, s) != 0)
352357 {
353- r = TRUE;
358+ if(parent != INVALID_SOCKET)
359+ {
360+ if(ppSSLParent = FindSSLPointerFromSocket(parent))
361+ {
362+ if(pSession = p_SSL_get_session(*ppSSLParent))
363+ {
364+ if(p_SSL_set_session(*ppSSL, pSession) == 1)
365+ {
366+ }
367+ }
368+ }
369+ }
354370 // SSLのネゴシエーションには時間がかかる場合がある
371+ r = TRUE;
355372 while(p_SSL_connect(*ppSSL) != 1)
356373 {
357374 LeaveCriticalSection(&g_OpenSSLLock);
@@ -364,6 +381,14 @@ BOOL AttachSSL(SOCKET s)
364381 }
365382 EnterCriticalSection(&g_OpenSSLLock);
366383 }
384+ if(ConfirmSSLCertificate(*ppSSL))
385+ {
386+ }
387+ else
388+ {
389+ DetachSSL(s);
390+ r = FALSE;
391+ }
367392 }
368393 else
369394 {
@@ -371,14 +396,6 @@ BOOL AttachSSL(SOCKET s)
371396 DetachSSL(s);
372397 EnterCriticalSection(&g_OpenSSLLock);
373398 }
374- if(ConfirmSSLCertificate(*ppSSL))
375- {
376- }
377- else
378- {
379- DetachSSL(s);
380- r = FALSE;
381- }
382399 }
383400 }
384401 }
@@ -437,7 +454,7 @@ SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)
437454 {
438455 SOCKET r;
439456 r = accept(s, addr, addrlen);
440- if(!AttachSSL(r))
457+ if(!AttachSSL(r, INVALID_SOCKET))
441458 {
442459 closesocket(r);
443460 return INVALID_SOCKET;
@@ -449,7 +466,7 @@ int connectS(SOCKET s, const struct sockaddr *name, int namelen)
449466 {
450467 int r;
451468 r = connect(s, name, namelen);
452- if(!AttachSSL(r))
469+ if(!AttachSSL(r, INVALID_SOCKET))
453470 return SOCKET_ERROR;
454471 return r;
455472 }
--- a/socketwrapper.h
+++ b/socketwrapper.h
@@ -16,7 +16,7 @@ BOOL IsOpenSSLLoaded();
1616 void SetSSLTimeoutCallback(DWORD Timeout, LPSSLTIMEOUTCALLBACK pCallback);
1717 void SetSSLConfirmCallback(LPSSLCONFIRMCALLBACK pCallback);
1818 BOOL IsHostNameMatched(LPCSTR HostName, LPCSTR CommonName);
19-BOOL AttachSSL(SOCKET s);
19+BOOL AttachSSL(SOCKET s, SOCKET parent);
2020 BOOL DetachSSL(SOCKET s);
2121 BOOL IsSSLAttached(SOCKET s);
2222 SOCKET socketS(int af, int type, int protocol);