OSDN Git Service

Normalize half / full width characters
[ffftp/ffftp.git] / socket.c
index 419d9c4..db7b69f 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -29,7 +29,7 @@
 \r
 #define        STRICT\r
 // IPv6対応\r
 \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
 #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
 \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
 \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
 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
 \r
 \r
 /*===== 外部参照 =====*/\r
@@ -778,7 +772,7 @@ int do_closesocket(SOCKET s)
        UnRegistAsyncTable(s);\r
        // FTPS対応\r
 //     Ret = closesocket(s);\r
        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
        if(Ret == SOCKET_ERROR)\r
        {\r
                Error = 0;\r
@@ -1035,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
 \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
                        if(Ret != SOCKET_ERROR)\r
                                break;\r
                        Error = WSAGetLastError();\r
@@ -1145,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
 \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
                        if(Ret != SOCKET_ERROR)\r
                        {\r
 #if DBG_MSG\r
@@ -1197,10 +1191,10 @@ void RemoveReceivedData(SOCKET s)
        char buf[1024];\r
        int len;\r
        int Error;\r
        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
        {\r
                AskAsyncDone(s, &Error, FD_READ);\r
-               recvS(s, buf, len, 0);\r
+               FTPS_recv(s, buf, len, 0);\r
        }\r
 }\r
 \r
        }\r
 }\r
 \r
@@ -1231,357 +1225,3 @@ int CheckClosedAndReconnect(void)
 \r
 \r
 \r
 \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
-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
-                       memset(&Result, 0xff, sizeof(Result));\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
-                                       memset(&Result, 0xff, sizeof(Result));\r
-                                       break;\r
-                               }\r
-                               if(cp = strstr(cp, ":"))\r
-                                       cp = cp + 1;\r
-                       }\r
-               }\r
-       }\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
-       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
-\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
-\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
-       {\r
-               if(ConvertNameToPunycode(pa0, name))\r
-                       r = WSAAsyncGetHostByNameIPv6(hWnd, wMsg, pa0, buf, buflen, Family);\r
-       }\r
-       FreeDuplicatedString(pa0);\r
-       return r;\r
-}\r
-\r