OSDN Git Service

Enhance routines for extra commands.
[ffftp/ffftp.git] / socket.c
index f999f77..622dba0 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -29,7 +29,7 @@
 \r
 #define        STRICT\r
 // IPv6対応\r
-#include <ws2tcpip.h>\r
+#include <winsock2.h>\r
 #include <windows.h>\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -40,8 +40,6 @@
 \r
 #include "common.h"\r
 #include "resource.h"\r
-// UTF-8対応\r
-#include "punycode.h"\r
 \r
 #define USE_THIS       1\r
 #define DBG_MSG                0\r
@@ -97,10 +95,6 @@ static int RegistAsyncTable(SOCKET s);
 static int RegistAsyncTableDbase(HANDLE Async);\r
 static int UnRegistAsyncTable(SOCKET s);\r
 static int UnRegistAsyncTableDbase(HANDLE Async);\r
-// UTF-8対応\r
-static HANDLE WSAAsyncGetHostByNameM(HWND hWnd, u_int wMsg, const char * name, char * buf, int buflen);\r
-// IPv6対応\r
-static HANDLE WSAAsyncGetHostByNameIPv6M(HWND hWnd, u_int wMsg, const char * name, char * buf, int buflen, short Family);\r
 \r
 \r
 /*===== 外部参照 =====*/\r
@@ -651,7 +645,53 @@ static int UnRegistAsyncTableDbase(HANDLE Async)
 \r
 \r
 \r
-struct hostent *do_gethostbyname(const char *Name, char *Buf, int Len, int *CancelCheckWork)\r
+// IPv6対応\r
+//struct hostent *do_gethostbyname(const char *Name, char *Buf, int Len, int *CancelCheckWork)\r
+struct hostent *do_gethostbynameIPv4(const char *Name, char *Buf, int Len, int *CancelCheckWork)\r
+{\r
+#if USE_THIS\r
+       struct hostent *Ret;\r
+       HANDLE hAsync;\r
+       int Error;\r
+\r
+#if DBG_MSG\r
+       DoPrintf("# Start gethostbyname");\r
+#endif\r
+       Ret = NULL;\r
+       // 同時接続対応\r
+//     *CancelCheckWork = NO;\r
+\r
+       // UTF-8対応\r
+//     hAsync = WSAAsyncGetHostByName(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len);\r
+       hAsync = WSAAsyncGetHostByNameM(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len);\r
+       if(hAsync != NULL)\r
+       {\r
+               RegistAsyncTableDbase(hAsync);\r
+               while((*CancelCheckWork == NO) && (AskAsyncDoneDbase(hAsync, &Error) != YES))\r
+               {\r
+                       Sleep(1);\r
+                       if(BackgrndMessageProc() == YES)\r
+                               *CancelCheckWork = YES;\r
+               }\r
+\r
+               if(*CancelCheckWork == YES)\r
+               {\r
+                       WSACancelAsyncRequest(hAsync);\r
+               }\r
+               else if(Error == 0)\r
+               {\r
+                       Ret = (struct hostent *)Buf;\r
+               }\r
+               UnRegistAsyncTableDbase(hAsync);\r
+       }\r
+       return(Ret);\r
+#else\r
+       return(gethostbyname(Name));\r
+#endif\r
+}\r
+\r
+\r
+struct hostent *do_gethostbynameIPv6(const char *Name, char *Buf, int Len, int *CancelCheckWork)\r
 {\r
 #if USE_THIS\r
        struct hostent *Ret;\r
@@ -667,9 +707,7 @@ struct hostent *do_gethostbyname(const char *Name, char *Buf, int Len, int *Canc
 \r
        // UTF-8対応\r
 //     hAsync = WSAAsyncGetHostByName(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len);\r
-       // IPv6対応\r
-//     hAsync = WSAAsyncGetHostByNameM(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len);\r
-       hAsync = WSAAsyncGetHostByNameIPv6M(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len, AF_INET);\r
+       hAsync = WSAAsyncGetHostByNameIPv6M(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len, AF_INET6);\r
        if(hAsync != NULL)\r
        {\r
                RegistAsyncTableDbase(hAsync);\r
@@ -734,7 +772,7 @@ int do_closesocket(SOCKET s)
        UnRegistAsyncTable(s);\r
        // FTPS対応\r
 //     Ret = closesocket(s);\r
-       Ret = closesocketS(s);\r
+       Ret = FTPS_closesocket(s);\r
        if(Ret == SOCKET_ERROR)\r
        {\r
                Error = 0;\r
@@ -991,7 +1029,7 @@ int do_recv(SOCKET s, char *buf, int len, int flags, int *TimeOutErr, int *Cance
 \r
                        // FTPS対応\r
 //                     Ret = recv(s, buf, len, flags);\r
-                       Ret = recvS(s, buf, len, flags);\r
+                       Ret = FTPS_recv(s, buf, len, flags);\r
                        if(Ret != SOCKET_ERROR)\r
                                break;\r
                        Error = WSAGetLastError();\r
@@ -1101,7 +1139,7 @@ int do_send(SOCKET s, const char *buf, int len, int flags, int *TimeOutErr, int
 \r
                        // FTPS対応\r
 //                     Ret = send(s, buf, len, flags);\r
-                       Ret = sendS(s, buf, len, flags);\r
+                       Ret = FTPS_send(s, buf, len, flags);\r
                        if(Ret != SOCKET_ERROR)\r
                        {\r
 #if DBG_MSG\r
@@ -1153,10 +1191,10 @@ void RemoveReceivedData(SOCKET s)
        char buf[1024];\r
        int len;\r
        int Error;\r
-       while((len = recvS(s, buf, sizeof(buf), MSG_PEEK)) >= 0)\r
+       while((len = FTPS_recv(s, buf, sizeof(buf), MSG_PEEK)) >= 0)\r
        {\r
                AskAsyncDone(s, &Error, FD_READ);\r
-               recvS(s, buf, len, 0);\r
+               FTPS_recv(s, buf, len, 0);\r
        }\r
 }\r
 \r
@@ -1187,250 +1225,19 @@ int CheckClosedAndReconnect(void)
 \r
 \r
 \r
-// IPv6対応\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
-static 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 + p->ai_addrlen));\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 + p->ai_addrlen));\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
-       free(pData->name);\r
-       free(pData);\r
-       // CreateThreadが返すハンドルが重複するのを回避\r
-       Sleep(10000);\r
-       return 0;\r
-}\r
-\r
-// IPv6対応のWSAAsyncGetHostByName相当の関数\r
-// FamilyにはAF_INETまたはAF_INET6を指定可能\r
-// ただしANSI用\r
-static 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
-               Result = 0;\r
-       return Result;\r
-}\r
-\r
-// UTF-8対応\r
-\r
-static BOOL ConvertStringToPunycode(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
-static BOOL ConvertNameToPunycode(LPSTR Output, LPCSTR Input)\r
+// 同時接続対応\r
+int CheckClosedAndReconnectTrnSkt(SOCKET *Skt, int *CancelCheckWork)\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(ConvertStringToPunycode(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
+       int Error;\r
+       int Sts;\r
 \r
-static 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
+//SetTaskMsg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");\r
 \r
-static 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
+       Sts = FFFTP_SUCCESS;\r
+       if(AskAsyncDone(*Skt, &Error, FD_CLOSE) == YES)\r
        {\r
-               if(ConvertNameToPunycode(pa0, name))\r
-                       r = WSAAsyncGetHostByNameIPv6(hWnd, wMsg, pa0, buf, buflen, Family);\r
+               Sts = ReConnectTrnSkt(Skt, CancelCheckWork);\r
        }\r
-       FreeDuplicatedString(pa0);\r
-       return r;\r
+       return(Sts);\r
 }\r
 \r