OSDN Git Service

Fix bugs of UTF-8 to UTF-16 API bridge.
[ffftp/ffftp.git] / socketwrapper.c
index 744f7c2..3159e93 100644 (file)
@@ -1,9 +1,9 @@
-// socketwrapper.cpp
+// socketwrapper.cpp
 // Copyright (C) 2011 Suguru Kawamoto
-// \83\\83P\83b\83g\83\89\83b\83p\81[
-// socket\8aÖ\98A\8aÖ\90\94\82ðOpenSSL\97p\82É\92u\8a·
-// \83R\83\93\83p\83C\83\8b\82É\82ÍOpenSSL\82Ì\83w\83b\83_\81[\83t\83@\83C\83\8b\82ª\95K\97v
-// \8eÀ\8ds\82É\82ÍOpenSSL\82ÌDLL\82ª\95K\97v
+// ソケットラッパー
+// socket関連関数をOpenSSL用に置換
+// コンパイルにはOpenSSLのヘッダーファイルが必要
+// 実行にはOpenSSLのDLLが必要
 
 #include <windows.h>
 #include <mmsystem.h>
 
 #include "socketwrapper.h"
 
-typedef void (__stdcall* _SSL_load_error_strings)();
-typedef int (__stdcall* _SSL_library_init)();
-typedef SSL_METHOD* (__stdcall* _SSLv23_method)();
-typedef SSL_CTX* (__stdcall* _SSL_CTX_new)(SSL_METHOD*);
-typedef void (__stdcall* _SSL_CTX_free)(SSL_CTX*);
-typedef SSL* (__stdcall* _SSL_new)(SSL_CTX*);
-typedef void (__stdcall* _SSL_free)(SSL*);
-typedef int (__stdcall* _SSL_shutdown)(SSL*);
-typedef int (__stdcall* _SSL_get_fd)(SSL*);
-typedef int (__stdcall* _SSL_set_fd)(SSL*, int);
-typedef int (__stdcall* _SSL_accept)(SSL*);
-typedef int (__stdcall* _SSL_connect)(SSL*);
-typedef int (__stdcall* _SSL_write)(SSL*, const void*, int);
-typedef int (__stdcall* _SSL_peek)(SSL*, void*, int);
-typedef int (__stdcall* _SSL_read)(SSL*, void*, int);
-typedef int (__stdcall* _SSL_get_error)(SSL*, int);
+typedef void (__cdecl* _SSL_load_error_strings)();
+typedef int (__cdecl* _SSL_library_init)();
+typedef SSL_METHOD* (__cdecl* _SSLv23_method)();
+typedef SSL_CTX* (__cdecl* _SSL_CTX_new)(SSL_METHOD*);
+typedef void (__cdecl* _SSL_CTX_free)(SSL_CTX*);
+typedef SSL* (__cdecl* _SSL_new)(SSL_CTX*);
+typedef void (__cdecl* _SSL_free)(SSL*);
+typedef int (__cdecl* _SSL_shutdown)(SSL*);
+typedef int (__cdecl* _SSL_get_fd)(SSL*);
+typedef int (__cdecl* _SSL_set_fd)(SSL*, int);
+typedef int (__cdecl* _SSL_accept)(SSL*);
+typedef int (__cdecl* _SSL_connect)(SSL*);
+typedef int (__cdecl* _SSL_write)(SSL*, const void*, int);
+typedef int (__cdecl* _SSL_peek)(SSL*, void*, int);
+typedef int (__cdecl* _SSL_read)(SSL*, void*, int);
+typedef int (__cdecl* _SSL_get_error)(SSL*, int);
 
 _SSL_load_error_strings pSSL_load_error_strings;
 _SSL_library_init pSSL_library_init;
@@ -45,7 +45,7 @@ _SSL_peek pSSL_peek;
 _SSL_read pSSL_read;
 _SSL_get_error pSSL_get_error;
 
-#define MAX_SSL_SOCKET 16
+#define MAX_SSL_SOCKET 64
 
 BOOL g_bOpenSSLLoaded;
 HMODULE g_hOpenSSL;
@@ -86,7 +86,8 @@ BOOL LoadOpenSSL()
                || !(pSSL_read = (_SSL_read)GetProcAddress(g_hOpenSSL, "SSL_read"))
                || !(pSSL_get_error = (_SSL_get_error)GetProcAddress(g_hOpenSSL, "SSL_get_error")))
        {
-               FreeLibrary(g_hOpenSSL);
+               if(g_hOpenSSL)
+                       FreeLibrary(g_hOpenSSL);
                g_hOpenSSL = NULL;
                return FALSE;
        }
@@ -155,6 +156,8 @@ SSL** FindSSLPointerFromSocket(SOCKET s)
 
 void SetSSLTimeoutCallback(DWORD Timeout, LPSSLTIMEOUTCALLBACK pCallback)
 {
+       if(!g_bOpenSSLLoaded)
+               return;
        EnterCriticalSection(&g_OpenSSLLock);
        g_OpenSSLTimeout = Timeout;
        g_pOpenSSLTimeoutCallback = pCallback;
@@ -166,6 +169,8 @@ BOOL AttachSSL(SOCKET s)
        BOOL r;
        DWORD Time;
        SSL** ppSSL;
+       if(!g_bOpenSSLLoaded)
+               return FALSE;
        r = FALSE;
        Time = timeGetTime();
        EnterCriticalSection(&g_OpenSSLLock);
@@ -180,7 +185,7 @@ BOOL AttachSSL(SOCKET s)
                                if(pSSL_set_fd(*ppSSL, s) != 0)
                                {
                                        r = TRUE;
-                                       // SSL\82Ì\83l\83S\83V\83G\81[\83V\83\87\83\93\82É\82Í\8e\9e\8aÔ\82ª\82©\82©\82é\8fê\8d\87\82ª\82 \82é
+                                       // SSLのネゴシエーションには時間がかかる場合がある
                                        while(pSSL_connect(*ppSSL) != 1)
                                        {
                                                LeaveCriticalSection(&g_OpenSSLLock);
@@ -195,7 +200,11 @@ BOOL AttachSSL(SOCKET s)
                                        }
                                }
                                else
+                               {
+                                       LeaveCriticalSection(&g_OpenSSLLock);
                                        DetachSSL(s);
+                                       EnterCriticalSection(&g_OpenSSLLock);
+                               }
                        }
                }
        }
@@ -207,6 +216,8 @@ BOOL DetachSSL(SOCKET s)
 {
        BOOL r;
        SSL** ppSSL;
+       if(!g_bOpenSSLLoaded)
+               return FALSE;
        r = FALSE;
        EnterCriticalSection(&g_OpenSSLLock);
        if(ppSSL = FindSSLPointerFromSocket(s))
@@ -223,6 +234,8 @@ BOOL DetachSSL(SOCKET s)
 BOOL IsSSLAttached(SOCKET s)
 {
        SSL** ppSSL;
+       if(!g_bOpenSSLLoaded)
+               return FALSE;
        EnterCriticalSection(&g_OpenSSLLock);
        ppSSL = FindSSLPointerFromSocket(s);
        LeaveCriticalSection(&g_OpenSSLLock);
@@ -276,6 +289,8 @@ int closesocketS(SOCKET s)
 int sendS(SOCKET s, const char * buf, int len, int flags)
 {
        SSL** ppSSL;
+       if(!g_bOpenSSLLoaded)
+               return send(s, buf, len, flags);
        EnterCriticalSection(&g_OpenSSLLock);
        ppSSL = FindSSLPointerFromSocket(s);
        LeaveCriticalSection(&g_OpenSSLLock);
@@ -287,6 +302,8 @@ int sendS(SOCKET s, const char * buf, int len, int flags)
 int recvS(SOCKET s, char * buf, int len, int flags)
 {
        SSL** ppSSL;
+       if(!g_bOpenSSLLoaded)
+               return recv(s, buf, len, flags);
        EnterCriticalSection(&g_OpenSSLLock);
        ppSSL = FindSSLPointerFromSocket(s);
        LeaveCriticalSection(&g_OpenSSLLock);