OSDN Git Service

Change to specify explicitly code pages.
[ffftp/ffftp.git] / socketwrapper.c
index d5a3d10..caae589 100644 (file)
@@ -2,15 +2,21 @@
 // Copyright (C) 2011 Suguru Kawamoto\r
 // ソケットラッパー\r
 // socket関連関数をOpenSSL用に置換\r
+// socket関連関数のIPv6対応\r
 // コンパイルにはOpenSSLのヘッダーファイルが必要\r
 // 実行にはOpenSSLのDLLが必要\r
 \r
+#include <ws2tcpip.h>\r
 #include <windows.h>\r
 #include <mmsystem.h>\r
 #include <openssl/ssl.h>\r
 \r
 #include "socketwrapper.h"\r
 #include "protectprocess.h"\r
+#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
@@ -32,14 +38,18 @@ 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 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
+typedef BIO* (__cdecl* _BIO_new_mem_buf)(void*, int);\r
 typedef long (__cdecl* _BIO_ctrl)(BIO*, int, long, void*);\r
 typedef void (__cdecl* _X509_free)(X509*);\r
 typedef int (__cdecl* _X509_print_ex)(BIO*, X509*, unsigned long, unsigned long);\r
 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
@@ -61,14 +71,18 @@ _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_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
+_BIO_new_mem_buf p_BIO_new_mem_buf;\r
 _BIO_ctrl p_BIO_ctrl;\r
 _X509_free p_X509_free;\r
 _X509_print_ex p_X509_print_ex;\r
 _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
@@ -93,21 +107,23 @@ BOOL __stdcall DefaultSSLConfirmCallback(BOOL* pbAborted, BOOL bVerified, LPCSTR
        return bVerified;\r
 }\r
 \r
+// OpenSSLを初期化\r
 BOOL LoadOpenSSL()\r
 {\r
        if(g_bOpenSSLLoaded)\r
                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
-       if(!g_hOpenSSL)\r
-               g_hOpenSSL = LoadLibrary("libssl32.dll");\r
+       // バージョン固定のためlibssl32.dllの読み込みは脆弱性の原因になり得るので廃止\r
+//     if(!g_hOpenSSL)\r
+//             g_hOpenSSL = LoadLibrary("libssl32.dll");\r
        if(!g_hOpenSSL\r
                || !(p_SSL_load_error_strings = (_SSL_load_error_strings)GetProcAddress(g_hOpenSSL, "SSL_load_error_strings"))\r
                || !(p_SSL_library_init = (_SSL_library_init)GetProcAddress(g_hOpenSSL, "SSL_library_init"))\r
@@ -128,7 +144,8 @@ BOOL LoadOpenSSL()
                || !(p_SSL_get_peer_certificate = (_SSL_get_peer_certificate)GetProcAddress(g_hOpenSSL, "SSL_get_peer_certificate"))\r
                || !(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_set_session = (_SSL_set_session)GetProcAddress(g_hOpenSSL, "SSL_set_session"))\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
@@ -140,11 +157,14 @@ BOOL LoadOpenSSL()
                || !(p_BIO_s_mem = (_BIO_s_mem)GetProcAddress(g_hOpenSSLCommon, "BIO_s_mem"))\r
                || !(p_BIO_new = (_BIO_new)GetProcAddress(g_hOpenSSLCommon, "BIO_new"))\r
                || !(p_BIO_free = (_BIO_free)GetProcAddress(g_hOpenSSLCommon, "BIO_free"))\r
+               || !(p_BIO_new_mem_buf = (_BIO_new_mem_buf)GetProcAddress(g_hOpenSSLCommon, "BIO_new_mem_buf"))\r
                || !(p_BIO_ctrl = (_BIO_ctrl)GetProcAddress(g_hOpenSSLCommon, "BIO_ctrl"))\r
                || !(p_X509_free = (_X509_free)GetProcAddress(g_hOpenSSLCommon, "X509_free"))\r
                || !(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_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_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
@@ -163,6 +183,7 @@ BOOL LoadOpenSSL()
        return TRUE;\r
 }\r
 \r
+// OpenSSLを解放\r
 void FreeOpenSSL()\r
 {\r
        int i;\r
@@ -190,6 +211,7 @@ void FreeOpenSSL()
        g_bOpenSSLLoaded = FALSE;\r
 }\r
 \r
+// OpenSSLが使用可能かどうか確認\r
 BOOL IsOpenSSLLoaded()\r
 {\r
        return g_bOpenSSLLoaded;\r
@@ -266,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
@@ -308,23 +330,103 @@ void SetSSLConfirmCallback(LPSSLCONFIRMCALLBACK pCallback)
        LeaveCriticalSection(&g_OpenSSLLock);\r
 }\r
 \r
+// SSLルート証明書を設定\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
+               return FALSE;\r
+       r = FALSE;\r
+       EnterCriticalSection(&g_OpenSSLLock);\r
+       if(!g_pOpenSSLCTX)\r
+               g_pOpenSSLCTX = p_SSL_CTX_new(p_SSLv23_method());\r
+       if(g_pOpenSSLCTX)\r
+       {\r
+               if(pStore = p_SSL_CTX_get_cert_store(g_pOpenSSLCTX))\r
+               {\r
+                       p = (BYTE*)pData;\r
+                       pBegin = NULL;\r
+                       pEnd = NULL;\r
+                       while(Length > 0)\r
+                       {\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
+               }\r
+       }\r
+       LeaveCriticalSection(&g_OpenSSLLock);\r
+       return r;\r
+}\r
+\r
+// ワイルドカードの比較\r
+// 主にSSL証明書のCN確認用\r
 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
@@ -333,6 +435,7 @@ BOOL IsHostNameMatched(LPCSTR HostName, LPCSTR CommonName)
        return bResult;\r
 }\r
 \r
+// SSLセッションを開始\r
 BOOL AttachSSL(SOCKET s, SOCKET parent, BOOL* pbAborted)\r
 {\r
        BOOL r;\r
@@ -420,6 +523,7 @@ BOOL AttachSSL(SOCKET s, SOCKET parent, BOOL* pbAborted)
        return r;\r
 }\r
 \r
+// SSLセッションを終了\r
 BOOL DetachSSL(SOCKET s)\r
 {\r
        BOOL r;\r
@@ -439,6 +543,8 @@ BOOL DetachSSL(SOCKET s)
        return r;\r
 }\r
 \r
+// SSLとしてマークされているか確認\r
+// マークされていればTRUEを返す\r
 BOOL IsSSLAttached(SOCKET s)\r
 {\r
        SSL** ppSSL;\r
@@ -452,22 +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
-SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)\r
+// accept相当の関数\r
+// ただし初めからSSLのネゴシエーションを行う\r
+SOCKET FTPS_accept(SOCKET s, struct sockaddr *addr, int *addrlen)\r
 {\r
        SOCKET r;\r
        BOOL bAborted;\r
@@ -481,7 +589,9 @@ SOCKET acceptS(SOCKET s, struct sockaddr *addr, int *addrlen)
        return r;\r
 }\r
 \r
-int connectS(SOCKET s, const struct sockaddr *name, int namelen)\r
+// connect相当の関数\r
+// ただし初めからSSLのネゴシエーションを行う\r
+int FTPS_connect(SOCKET s, const struct sockaddr *name, int namelen)\r
 {\r
        int r;\r
        BOOL bAborted;\r
@@ -492,13 +602,15 @@ int connectS(SOCKET s, const struct sockaddr *name, int namelen)
        return r;\r
 }\r
 \r
-int closesocketS(SOCKET s)\r
+// closesocket相当の関数\r
+int FTPS_closesocket(SOCKET s)\r
 {\r
        DetachSSL(s);\r
        return closesocket(s);\r
 }\r
 \r
-int sendS(SOCKET s, const char * buf, int len, int flags)\r
+// send相当の関数\r
+int FTPS_send(SOCKET s, const char * buf, int len, int flags)\r
 {\r
        SSL** ppSSL;\r
        if(!g_bOpenSSLLoaded)\r
@@ -511,7 +623,8 @@ int sendS(SOCKET s, const char * buf, int len, int flags)
        return p_SSL_write(*ppSSL, buf, len);\r
 }\r
 \r
-int recvS(SOCKET s, char * buf, int len, int flags)\r
+// recv相当の関数\r
+int FTPS_recv(SOCKET s, char * buf, int len, int flags)\r
 {\r
        SSL** ppSSL;\r
        if(!g_bOpenSSLLoaded)\r
@@ -526,3 +639,707 @@ int recvS(SOCKET s, char * buf, int len, int flags)
        return p_SSL_read(*ppSSL, buf, len);\r
 }\r
 \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
+       HWND hWnd;\r
+       u_int wMsg;\r
+       char * name;\r
+       char * buf;\r
+       int buflen;\r
+       short Family;\r
+} GETHOSTBYNAMEDATA;\r
+\r
+DWORD WINAPI WSAAsyncGetHostByNameIPv6ThreadProc(LPVOID lpParameter)\r
+{\r
+       GETHOSTBYNAMEDATA* pData;\r
+       struct hostent* pHost;\r
+       struct addrinfo* pAddr;\r
+       struct addrinfo* p;\r
+       pHost = NULL;\r
+       pData = (GETHOSTBYNAMEDATA*)lpParameter;\r
+       if(getaddrinfo(pData->name, NULL, NULL, &pAddr) == 0)\r
+       {\r
+               p = pAddr;\r
+               while(p)\r
+               {\r
+                       if(p->ai_family == pData->Family)\r
+                       {\r
+                               switch(p->ai_family)\r
+                               {\r
+                               case AF_INET:\r
+                                       pHost = (struct hostent*)pData->buf;\r
+                                       if((size_t)pData->buflen >= sizeof(struct hostent) + sizeof(char*) * 2 + sizeof(struct in_addr)\r
+                                               && p->ai_addrlen >= sizeof(struct sockaddr_in))\r
+                                       {\r
+                                               pHost->h_name = NULL;\r
+                                               pHost->h_aliases = NULL;\r
+                                               pHost->h_addrtype = p->ai_family;\r
+                                               pHost->h_length = sizeof(struct in_addr);\r
+                                               pHost->h_addr_list = (char**)(&pHost[1]);\r
+                                               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 + sizeof(struct in_addr)));\r
+                                       }\r
+                                       else\r
+                                               PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(WSAENOBUFS << 16));\r
+                                       break;\r
+                               case AF_INET6:\r
+                                       pHost = (struct hostent*)pData->buf;\r
+                                       if((size_t)pData->buflen >= sizeof(struct hostent) + sizeof(char*) * 2 + sizeof(struct in6_addr)\r
+                                               && p->ai_addrlen >= sizeof(struct sockaddr_in6))\r
+                                       {\r
+                                               pHost->h_name = NULL;\r
+                                               pHost->h_aliases = NULL;\r
+                                               pHost->h_addrtype = p->ai_family;\r
+                                               pHost->h_length = sizeof(struct in6_addr);\r
+                                               pHost->h_addr_list = (char**)(&pHost[1]);\r
+                                               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 + sizeof(struct in6_addr)));\r
+                                       }\r
+                                       else\r
+                                               PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(WSAENOBUFS << 16));\r
+                                       break;\r
+                               }\r
+                       }\r
+                       if(pHost)\r
+                               break;\r
+                       p = p->ai_next;\r
+               }\r
+               if(!p)\r
+                       PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(ERROR_INVALID_FUNCTION << 16));\r
+               freeaddrinfo(pAddr);\r
+       }\r
+       else\r
+               PostMessage(pData->hWnd, pData->wMsg, (WPARAM)pData->h, (LPARAM)(ERROR_INVALID_FUNCTION << 16));\r
+       // CreateThreadが返すハンドルが重複するのを回避\r
+       Sleep(10000);\r
+       CloseHandle(pData->h);\r
+       free(pData->name);\r
+       free(pData);\r
+       return 0;\r
+}\r
+\r
+// IPv6対応のWSAAsyncGetHostByName相当の関数\r
+// FamilyにはAF_INETまたはAF_INET6を指定可能\r
+// ただしANSI用\r
+HANDLE WSAAsyncGetHostByNameIPv6(HWND hWnd, u_int wMsg, const char * name, char * buf, int buflen, short Family)\r
+{\r
+       HANDLE hResult;\r
+       GETHOSTBYNAMEDATA* pData;\r
+       hResult = NULL;\r
+       if(pData = malloc(sizeof(GETHOSTBYNAMEDATA)))\r
+       {\r
+               pData->hWnd = hWnd;\r
+               pData->wMsg = wMsg;\r
+               if(pData->name = malloc(sizeof(char) * (strlen(name) + 1)))\r
+               {\r
+                       strcpy(pData->name, name);\r
+                       pData->buf = buf;\r
+                       pData->buflen = buflen;\r
+                       pData->Family = Family;\r
+                       if(pData->h = CreateThread(NULL, 0, WSAAsyncGetHostByNameIPv6ThreadProc, pData, CREATE_SUSPENDED, NULL))\r
+                       {\r
+                               ResumeThread(pData->h);\r
+                               hResult = pData->h;\r
+                       }\r
+               }\r
+       }\r
+       if(!hResult)\r
+       {\r
+               if(pData)\r
+               {\r
+                       if(pData->name)\r
+                               free(pData->name);\r
+                       free(pData);\r
+               }\r
+       }\r
+       return hResult;\r
+}\r
+\r
+// WSAAsyncGetHostByNameIPv6用のWSACancelAsyncRequest相当の関数\r
+int WSACancelAsyncRequestIPv6(HANDLE hAsyncTaskHandle)\r
+{\r
+       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
+char* AddressToStringIPv6(char* str, void* in6)\r
+{\r
+       char* pResult;\r
+       unsigned char* p;\r
+       int MaxZero;\r
+       int MaxZeroLen;\r
+       int i;\r
+       int j;\r
+       char Tmp[5];\r
+       pResult = str;\r
+       p = (unsigned char*)in6;\r
+       MaxZero = 8;\r
+       MaxZeroLen = 1;\r
+       for(i = 0; i < 8; i++)\r
+       {\r
+               for(j = i; j < 8; j++)\r
+               {\r
+                       if(p[j * 2] != 0 || p[j * 2 + 1] != 0)\r
+                               break;\r
+               }\r
+               if(j - i > MaxZeroLen)\r
+               {\r
+                       MaxZero = i;\r
+                       MaxZeroLen = j - i;\r
+               }\r
+       }\r
+       strcpy(str, "");\r
+       for(i = 0; i < 8; i++)\r
+       {\r
+               if(i == MaxZero)\r
+               {\r
+                       if(i == 0)\r
+                               strcat(str, ":");\r
+                       strcat(str, ":");\r
+               }\r
+               else if(i < MaxZero || i >= MaxZero + MaxZeroLen)\r
+               {\r
+                       sprintf(Tmp, "%x", (((int)p[i * 2] & 0xff) << 8) | ((int)p[i * 2 + 1] & 0xff));\r
+                       strcat(str, Tmp);\r
+                       if(i < 7)\r
+                               strcat(str, ":");\r
+               }\r
+       }\r
+       return pResult;\r
+}\r
+\r
+// IPv6対応のinet_ntoa相当の関数\r
+// ただしANSI用\r
+char* inet6_ntoa(struct in6_addr in6)\r
+{\r
+       char* pResult;\r
+       static char Adrs[40];\r
+       pResult = NULL;\r
+       memset(Adrs, 0, sizeof(Adrs));\r
+       pResult = AddressToStringIPv6(Adrs, &in6);\r
+       return pResult;\r
+}\r
+\r
+// IPv6対応のinet_addr相当の関数\r
+// ただしANSI用\r
+struct in6_addr inet6_addr(const char* cp)\r
+{\r
+       struct in6_addr Result;\r
+       int AfterZero;\r
+       int i;\r
+       char* p;\r
+       memset(&Result, 0, sizeof(Result));\r
+       AfterZero = 0;\r
+       for(i = 0; i < 8; i++)\r
+       {\r
+               if(!cp)\r
+               {\r
+                       memcpy(&Result, &IN6ADDR_NONE, sizeof(struct in6_addr));\r
+                       break;\r
+               }\r
+               if(i >= AfterZero)\r
+               {\r
+                       if(strncmp(cp, ":", 1) == 0)\r
+                       {\r
+                               cp = cp + 1;\r
+                               if(i == 0 && strncmp(cp, ":", 1) == 0)\r
+                                       cp = cp + 1;\r
+                               p = (char*)cp;\r
+                               AfterZero = 7;\r
+                               while(p = strstr(p, ":"))\r
+                               {\r
+                                       p = p + 1;\r
+                                       AfterZero--;\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               Result.u.Word[i] = (USHORT)strtol(cp, &p, 16);\r
+                               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
+                                       memcpy(&Result, &IN6ADDR_NONE, sizeof(struct in6_addr));\r
+                                       break;\r
+                               }\r
+                               if(cp = strstr(cp, ":"))\r
+                                       cp = cp + 1;\r
+                       }\r
+               }\r
+       }\r
+       return Result;\r
+}\r
+\r
+BOOL ConvertDomainNameToPunycode(LPSTR Output, DWORD Count, LPCSTR Input)\r
+{\r
+       BOOL bResult;\r
+       punycode_uint* pUnicode;\r
+       punycode_uint* p;\r
+       BOOL bNeeded;\r
+       LPCSTR InputString;\r
+       punycode_uint Length;\r
+       punycode_uint OutputLength;\r
+       bResult = FALSE;\r
+       if(pUnicode = malloc(sizeof(punycode_uint) * strlen(Input)))\r
+       {\r
+               p = pUnicode;\r
+               bNeeded = FALSE;\r
+               InputString = Input;\r
+               Length = 0;\r
+               while(*InputString != '\0')\r
+               {\r
+                       *p = (punycode_uint)GetNextCharM(InputString, &InputString);\r
+                       if(*p >= 0x80)\r
+                               bNeeded = TRUE;\r
+                       p++;\r
+                       Length++;\r
+               }\r
+               if(bNeeded)\r
+               {\r
+                       if(Count >= strlen("xn--") + 1)\r
+                       {\r
+                               strcpy(Output, "xn--");\r
+                               OutputLength = Count - strlen("xn--");\r
+                               if(punycode_encode(Length, pUnicode, NULL, (punycode_uint*)&OutputLength, Output + strlen("xn--")) == punycode_success)\r
+                               {\r
+                                       Output[strlen("xn--") + OutputLength] = '\0';\r
+                                       bResult = TRUE;\r
+                               }\r
+                       }\r
+               }\r
+               free(pUnicode);\r
+       }\r
+       if(!bResult)\r
+       {\r
+               if(Count >= strlen(Input) + 1)\r
+               {\r
+                       strcpy(Output, Input);\r
+                       bResult = TRUE;\r
+               }\r
+       }\r
+       return bResult;\r
+}\r
+\r
+BOOL ConvertNameToPunycode(LPSTR Output, LPCSTR Input)\r
+{\r
+       BOOL bResult;\r
+       DWORD Length;\r
+       char* pm0;\r
+       char* pm1;\r
+       char* p;\r
+       char* pNext;\r
+       bResult = FALSE;\r
+       Length = strlen(Input);\r
+       if(pm0 = AllocateStringM(Length + 1))\r
+       {\r
+               if(pm1 = AllocateStringM(Length * 4 + 1))\r
+               {\r
+                       strcpy(pm0, Input);\r
+                       p = pm0;\r
+                       while(p)\r
+                       {\r
+                               if(pNext = strchr(p, '.'))\r
+                               {\r
+                                       *pNext = '\0';\r
+                                       pNext++;\r
+                               }\r
+                               if(ConvertDomainNameToPunycode(pm1, Length * 4, p))\r
+                                       strcat(Output, pm1);\r
+                               if(pNext)\r
+                                       strcat(Output, ".");\r
+                               p = pNext;\r
+                       }\r
+                       bResult = TRUE;\r
+                       FreeDuplicatedString(pm1);\r
+               }\r
+               FreeDuplicatedString(pm0);\r
+       }\r
+       return bResult;\r
+}\r
+\r
+HANDLE WSAAsyncGetHostByNameM(HWND hWnd, u_int wMsg, const char * name, char * buf, int buflen)\r
+{\r
+       HANDLE r = NULL;\r
+       char* pa0 = NULL;\r
+       if(pa0 = AllocateStringA(strlen(name) * 4))\r
+       {\r
+               if(ConvertNameToPunycode(pa0, name))\r
+                       r = WSAAsyncGetHostByName(hWnd, wMsg, pa0, buf, buflen);\r
+       }\r
+       FreeDuplicatedString(pa0);\r
+       return r;\r
+}\r
+\r
+HANDLE WSAAsyncGetHostByNameIPv6M(HWND hWnd, u_int wMsg, const char * name, char * buf, int buflen, short Family)\r
+{\r
+       HANDLE r = NULL;\r
+       char* pa0 = NULL;\r
+       if(pa0 = AllocateStringA(strlen(name) * 4))\r
+       {\r
+               if(ConvertNameToPunycode(pa0, name))\r
+                       r = WSAAsyncGetHostByNameIPv6(hWnd, wMsg, pa0, buf, buflen, Family);\r
+       }\r
+       FreeDuplicatedString(pa0);\r
+       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