OSDN Git Service

Change to specify explicitly code pages.
[ffftp/ffftp.git] / socketwrapper.c
index edd8418..caae589 100644 (file)
@@ -16,6 +16,8 @@
 #include "mbswrapper.h"\r
 #include "punycode.h"\r
 \r
+// FTPS対応\r
+\r
 typedef void (__cdecl* _SSL_load_error_strings)();\r
 typedef int (__cdecl* _SSL_library_init)();\r
 typedef SSL_METHOD* (__cdecl* _SSLv23_method)();\r
@@ -36,7 +38,7 @@ typedef X509* (__cdecl* _SSL_get_peer_certificate)(const SSL*);
 typedef long (__cdecl* _SSL_get_verify_result)(const SSL*);\r
 typedef SSL_SESSION* (__cdecl* _SSL_get_session)(SSL*);\r
 typedef int (__cdecl* _SSL_set_session)(SSL*, SSL_SESSION*);\r
-typedef int (__cdecl* _SSL_CTX_use_certificate)(SSL_CTX*, X509*);\r
+typedef X509_STORE* (__cdecl* _SSL_CTX_get_cert_store)(const SSL_CTX*);\r
 typedef BIO_METHOD* (__cdecl* _BIO_s_mem)();\r
 typedef BIO* (__cdecl* _BIO_new)(BIO_METHOD*);\r
 typedef int (__cdecl* _BIO_free)(BIO*);\r
@@ -47,6 +49,7 @@ typedef int (__cdecl* _X509_print_ex)(BIO*, X509*, unsigned long, unsigned long)
 typedef X509_NAME* (__cdecl* _X509_get_subject_name)(X509*);\r
 typedef int (__cdecl* _X509_NAME_print_ex)(BIO*, X509_NAME*, int, unsigned long);\r
 typedef X509* (__cdecl* _PEM_read_bio_X509)(BIO*, X509**, pem_password_cb*, void*);\r
+typedef int (__cdecl* _X509_STORE_add_cert)(X509_STORE*, X509*);\r
 \r
 _SSL_load_error_strings p_SSL_load_error_strings;\r
 _SSL_library_init p_SSL_library_init;\r
@@ -68,7 +71,7 @@ _SSL_get_peer_certificate p_SSL_get_peer_certificate;
 _SSL_get_verify_result p_SSL_get_verify_result;\r
 _SSL_get_session p_SSL_get_session;\r
 _SSL_set_session p_SSL_set_session;\r
-_SSL_CTX_use_certificate p_SSL_CTX_use_certificate;\r
+_SSL_CTX_get_cert_store p_SSL_CTX_get_cert_store;\r
 _BIO_s_mem p_BIO_s_mem;\r
 _BIO_new p_BIO_new;\r
 _BIO_free p_BIO_free;\r
@@ -79,6 +82,7 @@ _X509_print_ex p_X509_print_ex;
 _X509_get_subject_name p_X509_get_subject_name;\r
 _X509_NAME_print_ex p_X509_NAME_print_ex;\r
 _PEM_read_bio_X509 p_PEM_read_bio_X509;\r
+_X509_STORE_add_cert p_X509_STORE_add_cert;\r
 \r
 #define MAX_SSL_SOCKET 16\r
 \r
@@ -110,11 +114,11 @@ BOOL LoadOpenSSL()
                return FALSE;\r
 #ifdef ENABLE_PROCESS_PROTECTION\r
        // 同梱するOpenSSLのバージョンに合わせてSHA1ハッシュ値を変更すること\r
-       // ssleay32.dll 1.0.0e\r
-       // libssl32.dll 1.0.0e\r
-       RegisterTrustedModuleSHA1Hash("\x4E\xB7\xA0\x22\x14\x4B\x58\x6D\xBC\xF5\x21\x0D\x96\x78\x0D\x79\x7D\x66\xB2\xB0");\r
-       // libeay32.dll 1.0.0e\r
-       RegisterTrustedModuleSHA1Hash("\x01\x32\x7A\xAE\x69\x26\xE6\x58\xC7\x63\x22\x1E\x53\x5A\x78\xBC\x61\xC7\xB5\xC1");\r
+       // ssleay32.dll 1.0.0g\r
+       // libssl32.dll 1.0.0g\r
+       RegisterTrustedModuleSHA1Hash("\x42\x32\x3E\x44\x35\xBC\x98\x6C\x45\xC9\xA2\xB8\x41\xE7\xDA\x7B\x6A\x98\xB2\x28");\r
+       // libeay32.dll 1.0.0g\r
+       RegisterTrustedModuleSHA1Hash("\x3F\xC8\x07\x84\xB3\xF0\x71\x4A\x18\x59\x52\x1F\x99\x09\x65\xB9\x49\xA7\x15\x36");\r
 #endif\r
        g_hOpenSSL = LoadLibrary("ssleay32.dll");\r
        // バージョン固定のためlibssl32.dllの読み込みは脆弱性の原因になり得るので廃止\r
@@ -141,7 +145,7 @@ BOOL LoadOpenSSL()
                || !(p_SSL_get_verify_result = (_SSL_get_verify_result)GetProcAddress(g_hOpenSSL, "SSL_get_verify_result"))\r
                || !(p_SSL_get_session = (_SSL_get_session)GetProcAddress(g_hOpenSSL, "SSL_get_session"))\r
                || !(p_SSL_set_session = (_SSL_set_session)GetProcAddress(g_hOpenSSL, "SSL_set_session"))\r
-               || !(p_SSL_CTX_use_certificate = (_SSL_CTX_use_certificate)GetProcAddress(g_hOpenSSL, "SSL_CTX_use_certificate")))\r
+               || !(p_SSL_CTX_get_cert_store = (_SSL_CTX_get_cert_store)GetProcAddress(g_hOpenSSL, "SSL_CTX_get_cert_store")))\r
        {\r
                if(g_hOpenSSL)\r
                        FreeLibrary(g_hOpenSSL);\r
@@ -159,7 +163,8 @@ BOOL LoadOpenSSL()
                || !(p_X509_print_ex = (_X509_print_ex)GetProcAddress(g_hOpenSSLCommon, "X509_print_ex"))\r
                || !(p_X509_get_subject_name = (_X509_get_subject_name)GetProcAddress(g_hOpenSSLCommon, "X509_get_subject_name"))\r
                || !(p_X509_NAME_print_ex = (_X509_NAME_print_ex)GetProcAddress(g_hOpenSSLCommon, "X509_NAME_print_ex"))\r
-               || !(p_PEM_read_bio_X509 = (_PEM_read_bio_X509)GetProcAddress(g_hOpenSSLCommon, "PEM_read_bio_X509")))\r
+               || !(p_PEM_read_bio_X509 = (_PEM_read_bio_X509)GetProcAddress(g_hOpenSSLCommon, "PEM_read_bio_X509"))\r
+               || !(p_X509_STORE_add_cert = (_X509_STORE_add_cert)GetProcAddress(g_hOpenSSLCommon, "X509_STORE_add_cert")))\r
        {\r
                if(g_hOpenSSL)\r
                        FreeLibrary(g_hOpenSSL);\r
@@ -283,7 +288,7 @@ BOOL ConfirmSSLCertificate(SSL* pSSL, BOOL* pbAborted)
                }\r
                p_X509_free(pX509);\r
        }\r
-       if(p_SSL_get_verify_result(pSSL) == X509_V_OK)\r
+       if(pX509 && p_SSL_get_verify_result(pSSL) == X509_V_OK)\r
                bVerified = TRUE;\r
        pCN = pSubject;\r
        while(pCN)\r
@@ -326,9 +331,14 @@ void SetSSLConfirmCallback(LPSSLCONFIRMCALLBACK pCallback)
 }\r
 \r
 // SSLルート証明書を設定\r
-BOOL SetSSLRootCertificate(void* pData, DWORD Length)\r
+// PEM形式のみ指定可能\r
+BOOL SetSSLRootCertificate(const void* pData, DWORD Length)\r
 {\r
        BOOL r;\r
+       X509_STORE* pStore;\r
+       BYTE* p;\r
+       BYTE* pBegin;\r
+       BYTE* pEnd;\r
        BIO* pBIO;\r
        X509* pX509;\r
        if(!g_bOpenSSLLoaded)\r
@@ -339,15 +349,45 @@ BOOL SetSSLRootCertificate(void* pData, DWORD Length)
                g_pOpenSSLCTX = p_SSL_CTX_new(p_SSLv23_method());\r
        if(g_pOpenSSLCTX)\r
        {\r
-               if(pBIO = p_BIO_new_mem_buf(pData, Length))\r
+               if(pStore = p_SSL_CTX_get_cert_store(g_pOpenSSLCTX))\r
                {\r
-                       if(pX509 = p_PEM_read_bio_X509(pBIO, NULL, NULL, NULL))\r
+                       p = (BYTE*)pData;\r
+                       pBegin = NULL;\r
+                       pEnd = NULL;\r
+                       while(Length > 0)\r
                        {\r
-                               if(p_SSL_CTX_use_certificate(g_pOpenSSLCTX, pX509) == 1)\r
-                                       r = TRUE;\r
-                               p_X509_free(pX509);\r
+                               if(!pBegin)\r
+                               {\r
+                                       if(Length < 27)\r
+                                               break;\r
+                                       if(memcmp(p, "-----BEGIN CERTIFICATE-----", 27) == 0)\r
+                                               pBegin = p;\r
+                               }\r
+                               else if(!pEnd)\r
+                               {\r
+                                       if(Length < 25)\r
+                                               break;\r
+                                       if(memcmp(p, "-----END CERTIFICATE-----", 25) == 0)\r
+                                               pEnd = p + 25;\r
+                               }\r
+                               if(pBegin && pEnd)\r
+                               {\r
+                                       if(pBIO = p_BIO_new_mem_buf(pBegin, (int)((size_t)pEnd - (size_t)pBegin)))\r
+                                       {\r
+                                               if(pX509 = p_PEM_read_bio_X509(pBIO, NULL, NULL, NULL))\r
+                                               {\r
+                                                       if(p_X509_STORE_add_cert(pStore, pX509) == 1)\r
+                                                               r = TRUE;\r
+                                                       p_X509_free(pX509);\r
+                                               }\r
+                                               p_BIO_free(pBIO);\r
+                                       }\r
+                                       pBegin = NULL;\r
+                                       pEnd = NULL;\r
+                               }\r
+                               p++;\r
+                               Length--;\r
                        }\r
-                       p_BIO_free(pBIO);\r
                }\r
        }\r
        LeaveCriticalSection(&g_OpenSSLLock);\r
@@ -359,20 +399,34 @@ BOOL SetSSLRootCertificate(void* pData, DWORD Length)
 BOOL IsHostNameMatched(LPCSTR HostName, LPCSTR CommonName)\r
 {\r
        BOOL bResult;\r
-       char* pAsterisk;\r
+       const char* pAsterisk;\r
+       size_t BeforeAsterisk;\r
+       const char* pBeginAsterisk;\r
+       const char* pEndAsterisk;\r
+       const char* pDot;\r
        bResult = FALSE;\r
        if(HostName && CommonName)\r
        {\r
                if(pAsterisk = strchr(CommonName, '*'))\r
                {\r
-                       if(_strnicmp(HostName, CommonName, ((size_t)pAsterisk - (size_t)CommonName) / sizeof(char)) == 0)\r
+                       BeforeAsterisk = ((size_t)pAsterisk - (size_t)CommonName) / sizeof(char);\r
+                       pBeginAsterisk = HostName + BeforeAsterisk;\r
+                       while(*pAsterisk == '*')\r
                        {\r
-                               while(*pAsterisk == '*')\r
+                               pAsterisk++;\r
+                       }\r
+                       pEndAsterisk = HostName + strlen(HostName) - strlen(pAsterisk);\r
+                       // "*"より前は大文字小文字を無視して完全一致\r
+                       if(_strnicmp(HostName, CommonName, BeforeAsterisk) == 0)\r
+                       {\r
+                               // "*"より後は大文字小文字を無視して完全一致\r
+                               if(_stricmp(pEndAsterisk, pAsterisk) == 0)\r
                                {\r
-                                       pAsterisk++;\r
+                                       // "*"と一致する範囲に"."が含まれてはならない\r
+                                       pDot = strchr(pBeginAsterisk, '.');\r
+                                       if(!pDot || pDot >= pEndAsterisk)\r
+                                               bResult = TRUE;\r
                                }\r
-                               if(_stricmp(HostName + strlen(HostName) - strlen(pAsterisk), pAsterisk) == 0)\r
-                                       bResult = TRUE;\r
                        }\r
                }\r
                else if(_stricmp(HostName, CommonName) == 0)\r
@@ -504,24 +558,24 @@ BOOL IsSSLAttached(SOCKET s)
        return TRUE;\r
 }\r
 \r
-SOCKET socketS(int af, int type, int protocol)\r
+SOCKET FTPS_socket(int af, int type, int protocol)\r
 {\r
        return socket(af, type, protocol);\r
 }\r
 \r
-int bindS(SOCKET s, const struct sockaddr *addr, int namelen)\r
+int FTPS_bind(SOCKET s, const struct sockaddr *addr, int namelen)\r
 {\r
        return bind(s, addr, namelen);\r
 }\r
 \r
-int listenS(SOCKET s, int backlog)\r
+int FTPS_listen(SOCKET s, int backlog)\r
 {\r
        return listen(s, backlog);\r
 }\r
 \r
 // accept相当の関数\r
 // ただし初めからSSLのネゴシエーションを行う\r
-SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)\r
+SOCKET FTPS_accept(SOCKET s, struct sockaddr *addr, int *addrlen)\r
 {\r
        SOCKET r;\r
        BOOL bAborted;\r
@@ -537,7 +591,7 @@ SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)
 \r
 // connect相当の関数\r
 // ただし初めからSSLのネゴシエーションを行う\r
-int connectS(SOCKET s, const struct sockaddr *name, int namelen)\r
+int FTPS_connect(SOCKET s, const struct sockaddr *name, int namelen)\r
 {\r
        int r;\r
        BOOL bAborted;\r
@@ -549,14 +603,14 @@ int connectS(SOCKET s, const struct sockaddr *name, int namelen)
 }\r
 \r
 // closesocket相当の関数\r
-int closesocketS(SOCKET s)\r
+int FTPS_closesocket(SOCKET s)\r
 {\r
        DetachSSL(s);\r
        return closesocket(s);\r
 }\r
 \r
 // send相当の関数\r
-int sendS(SOCKET s, const char * buf, int len, int flags)\r
+int FTPS_send(SOCKET s, const char * buf, int len, int flags)\r
 {\r
        SSL** ppSSL;\r
        if(!g_bOpenSSLLoaded)\r
@@ -570,7 +624,7 @@ int sendS(SOCKET s, const char * buf, int len, int flags)
 }\r
 \r
 // recv相当の関数\r
-int recvS(SOCKET s, char * buf, int len, int flags)\r
+int FTPS_recv(SOCKET s, char * buf, int len, int flags)\r
 {\r
        SSL** ppSSL;\r
        if(!g_bOpenSSLLoaded)\r
@@ -587,6 +641,8 @@ int recvS(SOCKET s, char * buf, int len, int flags)
 \r
 // IPv6対応\r
 \r
+const struct in6_addr IN6ADDR_NONE = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};\r
+\r
 typedef struct\r
 {\r
        HANDLE h;\r
@@ -628,7 +684,7 @@ DWORD WINAPI WSAAsyncGetHostByNameIPv6ThreadProc(LPVOID lpParameter)
                                                pHost->h_addr_list[0] = (char*)(&pHost->h_addr_list[2]);\r
                                                pHost->h_addr_list[1] = NULL;\r
                                                memcpy(pHost->h_addr_list[0], &((struct sockaddr_in*)p->ai_addr)->sin_addr, sizeof(struct in_addr));\r
-                                               PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(sizeof(struct hostent) + sizeof(char*) * 2 + p->ai_addrlen));\r
+                                               PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(sizeof(struct hostent) + sizeof(char*) * 2 + sizeof(struct in_addr)));\r
                                        }\r
                                        else\r
                                                PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(WSAENOBUFS << 16));\r
@@ -646,7 +702,7 @@ DWORD WINAPI WSAAsyncGetHostByNameIPv6ThreadProc(LPVOID lpParameter)
                                                pHost->h_addr_list[0] = (char*)(&pHost->h_addr_list[2]);\r
                                                pHost->h_addr_list[1] = NULL;\r
                                                memcpy(pHost->h_addr_list[0], &((struct sockaddr_in6*)p->ai_addr)->sin6_addr, sizeof(struct in6_addr));\r
-                                               PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(sizeof(struct hostent) + sizeof(char*) * 2 + p->ai_addrlen));\r
+                                               PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(sizeof(struct hostent) + sizeof(char*) * 2 + sizeof(struct in6_addr)));\r
                                        }\r
                                        else\r
                                                PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(WSAENOBUFS << 16));\r
@@ -663,10 +719,11 @@ DWORD WINAPI WSAAsyncGetHostByNameIPv6ThreadProc(LPVOID lpParameter)
        }\r
        else\r
                PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(ERROR_INVALID_FUNCTION << 16));\r
-       free(pData->name);\r
-       free(pData);\r
        // CreateThreadが返すハンドルが重複するのを回避\r
        Sleep(10000);\r
+       CloseHandle(pData->h);\r
+       free(pData->name);\r
+       free(pData);\r
        return 0;\r
 }\r
 \r
@@ -713,7 +770,10 @@ int WSACancelAsyncRequestIPv6(HANDLE hAsyncTaskHandle)
        int Result;\r
        Result = SOCKET_ERROR;\r
        if(TerminateThread(hAsyncTaskHandle, 0))\r
+       {\r
+               CloseHandle(hAsyncTaskHandle);\r
                Result = 0;\r
+       }\r
        return Result;\r
 }\r
 \r
@@ -789,7 +849,7 @@ struct in6_addr inet6_addr(const char* cp)
        {\r
                if(!cp)\r
                {\r
-                       memset(&Result, 0xff, sizeof(Result));\r
+                       memcpy(&Result, &IN6ADDR_NONE, sizeof(struct in6_addr));\r
                        break;\r
                }\r
                if(i >= AfterZero)\r
@@ -813,7 +873,7 @@ struct in6_addr inet6_addr(const char* cp)
                                Result.u.Word[i] = ((Result.u.Word[i] & 0xff00) >> 8) | ((Result.u.Word[i] & 0x00ff) << 8);\r
                                if(strncmp(p, ":", 1) != 0 && strlen(p) > 0)\r
                                {\r
-                                       memset(&Result, 0xff, sizeof(Result));\r
+                                       memcpy(&Result, &IN6ADDR_NONE, sizeof(struct in6_addr));\r
                                        break;\r
                                }\r
                                if(cp = strstr(cp, ":"))\r
@@ -937,3 +997,349 @@ HANDLE WSAAsyncGetHostByNameIPv6M(HWND hWnd, u_int wMsg, const char * name, char
        return r;\r
 }\r
 \r
+// SFTP対応\r
+\r
+typedef void* (__cdecl* _SFTP_Create)();\r
+typedef void (__cdecl* _SFTP_Destroy)(void*);\r
+typedef BOOL (__cdecl* _SFTP_IsExited)(void*);\r
+typedef BOOL (__cdecl* _SFTP_SetTimeoutCallback)(void*, void*);\r
+typedef size_t (__cdecl* _SFTP_PeekStdOut)(void*, void*, size_t);\r
+typedef size_t (__cdecl* _SFTP_ReadStdOut)(void*, void*, size_t);\r
+typedef size_t (__cdecl* _SFTP_WriteStdIn)(void*, const void*, size_t);\r
+typedef size_t (__cdecl* _SFTP_PeekDataOut)(void*, void*, size_t);\r
+typedef size_t (__cdecl* _SFTP_ReadDataOut)(void*, void*, size_t);\r
+typedef size_t (__cdecl* _SFTP_WriteDataIn)(void*, const void*, size_t);\r
+typedef BOOL (__cdecl* _SFTP_SetFilePosition)(void*, LONGLONG);\r
+\r
+_SFTP_Create p_SFTP_Create;\r
+_SFTP_Destroy p_SFTP_Destroy;\r
+_SFTP_IsExited p_SFTP_IsExited;\r
+_SFTP_SetTimeoutCallback p_SFTP_SetTimeoutCallback;\r
+_SFTP_PeekStdOut p_SFTP_PeekStdOut;\r
+_SFTP_ReadStdOut p_SFTP_ReadStdOut;\r
+_SFTP_WriteStdIn p_SFTP_WriteStdIn;\r
+_SFTP_PeekDataOut p_SFTP_PeekDataOut;\r
+_SFTP_ReadDataOut p_SFTP_ReadDataOut;\r
+_SFTP_WriteDataIn p_SFTP_WriteDataIn;\r
+_SFTP_SetFilePosition p_SFTP_SetFilePosition;\r
+\r
+typedef struct\r
+{\r
+       SOCKET Control;\r
+       SOCKET Data;\r
+       void* Handle;\r
+       char Host[1024];\r
+       int Port;\r
+       char User[1024];\r
+       char Password[1024];\r
+} SFTPDATA;\r
+\r
+#define MAX_SFTP_SOCKET 16\r
+\r
+BOOL g_bPuTTYLoaded;\r
+HMODULE g_hPuTTY;\r
+CRITICAL_SECTION g_PuTTYLock;\r
+SFTPDATA g_SFTPData[MAX_SFTP_SOCKET];\r
+\r
+// PuTTYを初期化\r
+BOOL LoadPuTTY()\r
+{\r
+       int i;\r
+       if(g_bPuTTYLoaded)\r
+               return FALSE;\r
+#ifdef ENABLE_PROCESS_PROTECTION\r
+       // ビルドしたputty.dllに合わせてSHA1ハッシュ値を変更すること\r
+       RegisterTrustedModuleSHA1Hash("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");\r
+#endif\r
+       // デバッグ用\r
+#ifdef _DEBUG\r
+       {\r
+               char Path[MAX_PATH];\r
+               GetModuleFileName(NULL, Path, MAX_PATH);\r
+               strcpy(strrchr(Path, '\\'), "\\..\\putty\\Debug\\PuTTY.dll");\r
+               g_hPuTTY = LoadLibrary(Path);\r
+       }\r
+#else\r
+       g_hPuTTY = LoadLibrary("putty.dll");\r
+#endif\r
+       if(!g_hPuTTY\r
+               || !(p_SFTP_Create = (_SFTP_Create)GetProcAddress(g_hPuTTY, "SFTP_Create"))\r
+               || !(p_SFTP_Destroy = (_SFTP_Destroy)GetProcAddress(g_hPuTTY, "SFTP_Destroy"))\r
+               || !(p_SFTP_IsExited = (_SFTP_IsExited)GetProcAddress(g_hPuTTY, "SFTP_IsExited"))\r
+               || !(p_SFTP_SetTimeoutCallback = (_SFTP_SetTimeoutCallback)GetProcAddress(g_hPuTTY, "SFTP_SetTimeoutCallback"))\r
+               || !(p_SFTP_PeekStdOut = (_SFTP_PeekStdOut)GetProcAddress(g_hPuTTY, "SFTP_PeekStdOut"))\r
+               || !(p_SFTP_ReadStdOut = (_SFTP_ReadStdOut)GetProcAddress(g_hPuTTY, "SFTP_ReadStdOut"))\r
+               || !(p_SFTP_WriteStdIn = (_SFTP_WriteStdIn)GetProcAddress(g_hPuTTY, "SFTP_WriteStdIn"))\r
+               || !(p_SFTP_PeekDataOut = (_SFTP_PeekDataOut)GetProcAddress(g_hPuTTY, "SFTP_PeekDataOut"))\r
+               || !(p_SFTP_ReadDataOut = (_SFTP_ReadDataOut)GetProcAddress(g_hPuTTY, "SFTP_ReadDataOut"))\r
+               || !(p_SFTP_WriteDataIn = (_SFTP_WriteDataIn)GetProcAddress(g_hPuTTY, "SFTP_WriteDataIn"))\r
+               || !(p_SFTP_SetFilePosition = (_SFTP_SetFilePosition)GetProcAddress(g_hPuTTY, "SFTP_SetFilePosition")))\r
+       {\r
+               if(g_hPuTTY)\r
+                       FreeLibrary(g_hPuTTY);\r
+               g_hPuTTY = NULL;\r
+               return FALSE;\r
+       }\r
+       for(i = 0; i < MAX_SFTP_SOCKET; i++)\r
+       {\r
+               g_SFTPData[i].Control = INVALID_SOCKET;\r
+               g_SFTPData[i].Data = INVALID_SOCKET;\r
+       }\r
+       InitializeCriticalSection(&g_PuTTYLock);\r
+       g_bPuTTYLoaded = TRUE;\r
+       return TRUE;\r
+}\r
+\r
+// PuTTYを解放\r
+void FreePuTTY()\r
+{\r
+       int i;\r
+       if(!g_bPuTTYLoaded)\r
+               return;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       for(i = 0; i < MAX_SFTP_SOCKET; i++)\r
+       {\r
+               if(g_SFTPData[i].Control != INVALID_SOCKET)\r
+               {\r
+                       closesocket(g_SFTPData[i].Control);\r
+                       g_SFTPData[i].Control = INVALID_SOCKET;\r
+                       p_SFTP_Destroy(g_SFTPData[i].Handle);\r
+               }\r
+               if(g_SFTPData[i].Data != INVALID_SOCKET)\r
+               {\r
+                       closesocket(g_SFTPData[i].Data);\r
+                       g_SFTPData[i].Data = INVALID_SOCKET;\r
+               }\r
+       }\r
+       FreeLibrary(g_hPuTTY);\r
+       g_hPuTTY = NULL;\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       DeleteCriticalSection(&g_PuTTYLock);\r
+       g_bPuTTYLoaded = FALSE;\r
+}\r
+\r
+// PuTTYが使用可能かどうか確認\r
+BOOL IsPuTTYLoaded()\r
+{\r
+       return g_bPuTTYLoaded;\r
+}\r
+\r
+SFTPDATA* GetUnusedSFTPData()\r
+{\r
+       int i;\r
+       for(i = 0; i < MAX_SFTP_SOCKET; i++)\r
+       {\r
+               if(g_SFTPData[i].Control == INVALID_SOCKET)\r
+               {\r
+                       memset(&g_SFTPData[i], 0, sizeof(SFTPDATA));\r
+                       return &g_SFTPData[i];\r
+               }\r
+       }\r
+       return NULL;\r
+}\r
+\r
+SFTPDATA* FindSFTPDataFromSocket(SOCKET s)\r
+{\r
+       int i;\r
+       for(i = 0; i < MAX_SFTP_SOCKET; i++)\r
+       {\r
+               if(g_SFTPData[i].Control == s || g_SFTPData[i].Data == s)\r
+                       return &g_SFTPData[i];\r
+       }\r
+       return NULL;\r
+}\r
+\r
+// SFTPとしてマークされているか確認\r
+// マークされていればTRUEを返す\r
+BOOL IsSFTPAttached(SOCKET s)\r
+{\r
+       SFTPDATA* pSFTPData;\r
+       if(!g_bPuTTYLoaded)\r
+               return FALSE;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       pSFTPData = FindSFTPDataFromSocket(s);\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       if(!pSFTPData)\r
+               return FALSE;\r
+       return TRUE;\r
+}\r
+\r
+// コントロール用の仮想的なソケットを取得\r
+// 識別子としてダミーのソケットを返す\r
+SOCKET SFTP_socket(int af, int type, int protocol)\r
+{\r
+       SOCKET r;\r
+       SFTPDATA* pSFTPData;\r
+       if(!g_bPuTTYLoaded)\r
+               return INVALID_SOCKET;\r
+       r = INVALID_SOCKET;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       if(pSFTPData = GetUnusedSFTPData())\r
+       {\r
+               r = socket(af, type, protocol);\r
+               pSFTPData->Control = r;\r
+               pSFTPData->Data = INVALID_SOCKET;\r
+               pSFTPData->Handle = p_SFTP_Create();\r
+       }\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       return r;\r
+}\r
+\r
+int SFTP_bind(SOCKET s, const struct sockaddr *addr, int namelen)\r
+{\r
+       return SOCKET_ERROR;\r
+}\r
+\r
+int SFTP_listen(SOCKET s, int backlog)\r
+{\r
+       return SOCKET_ERROR;\r
+}\r
+\r
+// accept相当の関数\r
+SOCKET SFTP_accept(SOCKET s, struct sockaddr *addr, int *addrlen)\r
+{\r
+       return INVALID_SOCKET;\r
+}\r
+\r
+// connect相当の関数\r
+int SFTP_connect(SOCKET s, const struct sockaddr *name, int namelen)\r
+{\r
+       SFTPDATA* pSFTPData;\r
+       if(!g_bPuTTYLoaded)\r
+               return SOCKET_ERROR;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       pSFTPData = FindSFTPDataFromSocket(s);\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       if(!pSFTPData)\r
+               return SOCKET_ERROR;\r
+       if(namelen == sizeof(struct sockaddr_in))\r
+       {\r
+       }\r
+       else if(namelen == sizeof(struct sockaddr_in6))\r
+       {\r
+       }\r
+       else\r
+               return SOCKET_ERROR;\r
+       return 0;\r
+}\r
+\r
+// closesocket相当の関数\r
+int SFTP_closesocket(SOCKET s)\r
+{\r
+       SFTPDATA* pSFTPData;\r
+       if(!g_bPuTTYLoaded)\r
+               return SOCKET_ERROR;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       if(pSFTPData = FindSFTPDataFromSocket(s))\r
+       {\r
+               if(pSFTPData->Control == s)\r
+               {\r
+                       pSFTPData->Control = INVALID_SOCKET;\r
+                       p_SFTP_Destroy(pSFTPData->Handle);\r
+               }\r
+               if(pSFTPData->Data == s)\r
+                       pSFTPData->Data = INVALID_SOCKET;\r
+       }\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       return closesocket(s);\r
+}\r
+\r
+// send相当の関数\r
+int SFTP_send(SOCKET s, const char * buf, int len, int flags)\r
+{\r
+       int r;\r
+       SFTPDATA* pSFTPData;\r
+       if(!g_bPuTTYLoaded)\r
+               return SOCKET_ERROR;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       pSFTPData = FindSFTPDataFromSocket(s);\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       if(!pSFTPData)\r
+               return SOCKET_ERROR;\r
+       if(p_SFTP_IsExited(pSFTPData->Handle))\r
+               return SOCKET_ERROR;\r
+       r = SOCKET_ERROR;\r
+       if(pSFTPData->Control == s)\r
+               r = (int)p_SFTP_WriteStdIn(pSFTPData->Handle, buf, len);\r
+       else if(pSFTPData->Data == s)\r
+               r = (int)p_SFTP_WriteDataIn(pSFTPData->Handle, buf, len);\r
+       return r;\r
+}\r
+\r
+// recv相当の関数\r
+int SFTP_recv(SOCKET s, char * buf, int len, int flags)\r
+{\r
+       int r;\r
+       SFTPDATA* pSFTPData;\r
+       if(!g_bPuTTYLoaded)\r
+               return SOCKET_ERROR;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       pSFTPData = FindSFTPDataFromSocket(s);\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       if(!pSFTPData)\r
+               return SOCKET_ERROR;\r
+       if(p_SFTP_IsExited(pSFTPData->Handle))\r
+               return SOCKET_ERROR;\r
+       r = SOCKET_ERROR;\r
+       if(pSFTPData->Control == s)\r
+       {\r
+               if(flags & MSG_PEEK)\r
+                       r = (int)p_SFTP_PeekStdOut(pSFTPData->Handle, buf, len);\r
+               else\r
+                       r = (int)p_SFTP_ReadStdOut(pSFTPData->Handle, buf, len);\r
+       }\r
+       else if(pSFTPData->Data == s)\r
+       {\r
+               if(flags & MSG_PEEK)\r
+                       r = (int)p_SFTP_PeekDataOut(pSFTPData->Handle, buf, len);\r
+               else\r
+                       r = (int)p_SFTP_ReadDataOut(pSFTPData->Handle, buf, len);\r
+       }\r
+       return r;\r
+}\r
+\r
+BOOL SFTP_SetTimeoutCallback(SOCKET s, void* pCallback)\r
+{\r
+       SFTPDATA* pSFTPData;\r
+       if(!g_bPuTTYLoaded)\r
+               return FALSE;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       pSFTPData = FindSFTPDataFromSocket(s);\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       if(!pSFTPData)\r
+               return FALSE;\r
+       return p_SFTP_SetTimeoutCallback(pSFTPData->Handle, pCallback);\r
+}\r
+\r
+// データ用の仮想的なソケットを取得\r
+// 識別子としてダミーのソケットを返す\r
+SOCKET SFTP_GetDataHandle(SOCKET s, int af, int type, int protocol)\r
+{\r
+       SOCKET r;\r
+       SFTPDATA* pSFTPData;\r
+       if(!g_bPuTTYLoaded)\r
+               return INVALID_SOCKET;\r
+       r = INVALID_SOCKET;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       if(pSFTPData = FindSFTPDataFromSocket(s))\r
+       {\r
+               r = socket(af, type, protocol);\r
+               pSFTPData->Data = r;\r
+       }\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       return r;\r
+}\r
+\r
+BOOL SFTP_SetFilePosition(SOCKET s, LONGLONG Position)\r
+{\r
+       SFTPDATA* pSFTPData;\r
+       if(!g_bPuTTYLoaded)\r
+               return FALSE;\r
+       EnterCriticalSection(&g_PuTTYLock);\r
+       pSFTPData = FindSFTPDataFromSocket(s);\r
+       LeaveCriticalSection(&g_PuTTYLock);\r
+       if(!pSFTPData)\r
+               return FALSE;\r
+       return p_SFTP_SetFilePosition(pSFTPData->Handle, Position);\r
+}\r
+\r