OSDN Git Service

Normalize half / full width characters
[ffftp/ffftp.git] / socket.c
index 8773978..db7b69f 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -28,6 +28,8 @@
 /============================================================================*/\r
 \r
 #define        STRICT\r
+// IPv6対応\r
+#include <winsock2.h>\r
 #include <windows.h>\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 \r
 \r
 \r
-#define FD_CONNECT_BIT         0x0001\r
-#define FD_CLOSE_BIT           0x0002\r
-#define FD_ACCEPT_BIT          0x0004\r
-#define FD_READ_BIT                    0x0008\r
-#define FD_WRITE_BIT           0x0010\r
+// Winsock2で定義される定数と名前が重複し値が異なるため使用不可\r
+//#define FD_CONNECT_BIT               0x0001\r
+//#define FD_CLOSE_BIT         0x0002\r
+//#define FD_ACCEPT_BIT                0x0004\r
+//#define FD_READ_BIT                  0x0008\r
+//#define FD_WRITE_BIT         0x0010\r
 \r
 \r
 \r
@@ -77,8 +80,8 @@ typedef struct {
 // 念のためテーブルを増量\r
 //#define MAX_SIGNAL_ENTRY             10\r
 //#define MAX_SIGNAL_ENTRY_DBASE       5\r
-#define MAX_SIGNAL_ENTRY               100\r
-#define MAX_SIGNAL_ENTRY_DBASE 50\r
+#define MAX_SIGNAL_ENTRY               16\r
+#define MAX_SIGNAL_ENTRY_DBASE 16\r
 \r
 \r
 \r
@@ -331,22 +334,22 @@ static int AskAsyncDone(SOCKET s, int *Error, int Mask)
                        *Error = Signal[Pos].Error;\r
                        if(Signal[Pos].Error != 0)\r
                                Sts = YES;\r
-                       if((Mask & FD_CONNECT_BIT) && (Signal[Pos].FdConnect != 0))\r
+                       if((Mask & FD_CONNECT) && (Signal[Pos].FdConnect != 0))\r
                        {\r
                                Sts = YES;\r
 #if DBG_MSG\r
                                DoPrintf("### Ask: connect (Sts=%d, Error=%d)", Sts, *Error);\r
 #endif\r
                        }\r
-                       if((Mask & FD_CLOSE_BIT) && (Signal[Pos].FdClose != 0))\r
-//                     if(Mask & FD_CLOSE_BIT)\r
+                       if((Mask & FD_CLOSE) && (Signal[Pos].FdClose != 0))\r
+//                     if(Mask & FD_CLOSE)\r
                        {\r
                                Sts = YES;\r
 #if DBG_MSG\r
                                DoPrintf("### Ask: close (Sts=%d, Error=%d)", Sts, *Error);\r
 #endif\r
                        }\r
-                       if((Mask & FD_ACCEPT_BIT) && (Signal[Pos].FdAccept != 0))\r
+                       if((Mask & FD_ACCEPT) && (Signal[Pos].FdAccept != 0))\r
                        {\r
                                Signal[Pos].FdAccept = 0;\r
                                Sts = YES;\r
@@ -354,7 +357,7 @@ static int AskAsyncDone(SOCKET s, int *Error, int Mask)
                                DoPrintf("### Ask: accept (Sts=%d, Error=%d)", Sts, *Error);\r
 #endif\r
                        }\r
-                       if((Mask & FD_READ_BIT) && (Signal[Pos].FdRead != 0))\r
+                       if((Mask & FD_READ) && (Signal[Pos].FdRead != 0))\r
                        {\r
                                Signal[Pos].FdRead = 0;\r
                                Sts = YES;\r
@@ -362,7 +365,7 @@ static int AskAsyncDone(SOCKET s, int *Error, int Mask)
                                DoPrintf("### Ask: read (Sts=%d, Error=%d)", Sts, *Error);\r
 #endif\r
                        }\r
-                       if((Mask & FD_WRITE_BIT) && (Signal[Pos].FdWrite != 0))\r
+                       if((Mask & FD_WRITE) && (Signal[Pos].FdWrite != 0))\r
                        {\r
                                Signal[Pos].FdWrite = 0;\r
                                Sts = YES;\r
@@ -378,7 +381,7 @@ static int AskAsyncDone(SOCKET s, int *Error, int Mask)
 \r
        if(Pos == MAX_SIGNAL_ENTRY)\r
        {\r
-               if(Mask & FD_CLOSE_BIT)\r
+               if(Mask & FD_CLOSE)\r
                {\r
                                Sts = YES;\r
                }\r
@@ -642,7 +645,9 @@ 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
@@ -653,9 +658,56 @@ struct hostent *do_gethostbyname(const char *Name, char *Buf, int Len, int *Canc
        DoPrintf("# Start gethostbyname");\r
 #endif\r
        Ret = NULL;\r
-       *CancelCheckWork = NO;\r
+       // 同時接続対応\r
+//     *CancelCheckWork = NO;\r
 \r
-       hAsync = WSAAsyncGetHostByName(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len);\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
+       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 = WSAAsyncGetHostByNameIPv6M(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len, AF_INET6);\r
        if(hAsync != NULL)\r
        {\r
                RegistAsyncTableDbase(hAsync);\r
@@ -720,11 +772,11 @@ 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
-               while((CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_CLOSE_BIT) != YES))\r
+               while((CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_CLOSE) != YES))\r
                {\r
                        Sleep(1);\r
                        if(BackgrndMessageProc() == YES)\r
@@ -765,26 +817,24 @@ int do_connect(SOCKET s, const struct sockaddr *name, int namelen, int *CancelCh
 #if DBG_MSG\r
        DoPrintf("# Start connect (S=%x)", s);\r
 #endif\r
-       *CancelCheckWork = NO;\r
+       // 同時接続対応\r
+//     *CancelCheckWork = NO;\r
 \r
 #if DBG_MSG\r
        DoPrintf("## Async set: FD_CONNECT|FD_CLOSE|FD_ACCEPT|FD_READ|FD_WRITE");\r
 #endif\r
-       Ret = WSAAsyncSelect(s, hWndSocket, WM_ASYNC_SOCKET, FD_CONNECT | FD_CLOSE | FD_ACCEPT | FD_READ | FD_WRITE);\r
+       // 高速化のためFD_READとFD_WRITEを使用しない\r
+//     Ret = WSAAsyncSelect(s, hWndSocket, WM_ASYNC_SOCKET, FD_CONNECT | FD_CLOSE | FD_ACCEPT | FD_READ | FD_WRITE);\r
+       Ret = WSAAsyncSelect(s, hWndSocket, WM_ASYNC_SOCKET, FD_CONNECT | FD_CLOSE | FD_ACCEPT);\r
        if(Ret != SOCKET_ERROR)\r
        {\r
-               // FTPS対応\r
-//             Ret = connect(s, name, namelen);\r
-               if(AskCryptMode() == CRYPT_FTPIS)\r
-                       Ret = connectS(s, name, namelen);\r
-               else\r
-                       Ret = connect(s, name, namelen);\r
+               Ret = connect(s, name, namelen);\r
                if(Ret == SOCKET_ERROR)\r
                {\r
                        do\r
                        {\r
                                Error = 0;\r
-                               while((*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_CONNECT_BIT) != YES))\r
+                               while((*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_CONNECT) != YES))\r
                                {\r
                                        Sleep(1);\r
                                        if(BackgrndMessageProc() == YES)\r
@@ -856,9 +906,9 @@ SOCKET do_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
        Ret2 = INVALID_SOCKET;\r
        Error = 0;\r
 \r
-       while((CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_ACCEPT_BIT) != YES))\r
+       while((CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_ACCEPT) != YES))\r
        {\r
-               if(AskAsyncDone(s, &Error, FD_CLOSE_BIT) == YES)\r
+               if(AskAsyncDone(s, &Error, FD_CLOSE) == YES)\r
                {\r
                        Error = 1;\r
                        break;\r
@@ -872,12 +922,7 @@ SOCKET do_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
        {\r
                do\r
                {\r
-                       // FTPS対応\r
-//                     Ret2 = accept(s, addr, addrlen);\r
-                       if(AskCryptMode() == CRYPT_FTPIS)\r
-                               Ret2 = acceptS(s, addr, addrlen);\r
-                       else\r
-                               Ret2 = accept(s, addr, addrlen);\r
+                       Ret2 = accept(s, addr, addrlen);\r
                        if(Ret2 != INVALID_SOCKET)\r
                        {\r
 #if DBG_MSG\r
@@ -939,7 +984,8 @@ int do_recv(SOCKET s, char *buf, int len, int flags, int *TimeOutErr, int *Cance
        DoPrintf("# Start recv (S=%x)", s);\r
 #endif\r
        *TimeOutErr = NO;\r
-       *CancelCheckWork = NO;\r
+       // 同時接続対応\r
+//     *CancelCheckWork = NO;\r
        Ret = SOCKET_ERROR;\r
        Error = 0;\r
 \r
@@ -948,29 +994,30 @@ int do_recv(SOCKET s, char *buf, int len, int flags, int *TimeOutErr, int *Cance
 \r
        // FTPS対応\r
        // OpenSSLでは受信確認はFD_READが複数回受信される可能性がある\r
-//     while((*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_READ_BIT) != YES))\r
-       while(!IsSSLAttached(s) && (*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_READ_BIT) != YES))\r
-       {\r
-               if(AskAsyncDone(s, &Error, FD_CLOSE_BIT) == YES)\r
-               {\r
-                       Ret = 0;\r
-                       break;\r
-               }\r
-               Sleep(1);\r
-               if(BackgrndMessageProc() == YES)\r
-                       *CancelCheckWork = YES;\r
-               else if(TimeOut != 0)\r
-               {\r
-                       time(&ElapseTime);\r
-                       ElapseTime -= StartTime;\r
-                       if(ElapseTime >= TimeOut)\r
-                       {\r
-                               DoPrintf("do_recv timed out");\r
-                               *TimeOutErr = YES;\r
-                               *CancelCheckWork = YES;\r
-                       }\r
-               }\r
-       }\r
+//     while((*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_READ) != YES))\r
+       // 短時間にFD_READが2回以上通知される対策\r
+//     while(!IsSSLAttached(s) && (*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_READ) != YES))\r
+//     {\r
+//             if(AskAsyncDone(s, &Error, FD_CLOSE) == YES)\r
+//             {\r
+//                     Ret = 0;\r
+//                     break;\r
+//             }\r
+//             Sleep(1);\r
+//             if(BackgrndMessageProc() == YES)\r
+//                     *CancelCheckWork = YES;\r
+//             else if(TimeOut != 0)\r
+//             {\r
+//                     time(&ElapseTime);\r
+//                     ElapseTime -= StartTime;\r
+//                     if(ElapseTime >= TimeOut)\r
+//                     {\r
+//                             DoPrintf("do_recv timed out");\r
+//                             *TimeOutErr = YES;\r
+//                             *CancelCheckWork = YES;\r
+//                     }\r
+//             }\r
+//     }\r
 \r
        if(/*(Ret != 0) && */(Error == 0) && (*CancelCheckWork == NO) && (*TimeOutErr == NO))\r
        {\r
@@ -982,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
@@ -1036,7 +1083,8 @@ int do_send(SOCKET s, const char *buf, int len, int flags, int *TimeOutErr, int
        DoPrintf("# Start send (S=%x)", s);\r
 #endif\r
        *TimeOutErr = NO;\r
-       *CancelCheckWork = NO;\r
+       // 同時接続対応\r
+//     *CancelCheckWork = NO;\r
        Ret = SOCKET_ERROR;\r
        Error = 0;\r
 \r
@@ -1046,36 +1094,40 @@ int do_send(SOCKET s, const char *buf, int len, int flags, int *TimeOutErr, int
 #if DBG_MSG\r
        DoPrintf("## Async set: FD_CONNECT|FD_CLOSE|FD_ACCEPT|FD_READ|FD_WRITE");\r
 #endif\r
-       WSAAsyncSelect(s, hWndSocket, WM_ASYNC_SOCKET, FD_CONNECT | FD_CLOSE | FD_ACCEPT | FD_READ | FD_WRITE);\r
+       // Windows 2000でFD_WRITEが通知されないことがあるバグ修正\r
+       // 毎回通知されたのはNT 4.0までのバグであり仕様ではない\r
+       // XP以降は互換性のためか毎回通知される\r
+//     WSAAsyncSelect(s, hWndSocket, WM_ASYNC_SOCKET, FD_CONNECT | FD_CLOSE | FD_ACCEPT | FD_READ | FD_WRITE);\r
        if(BackgrndMessageProc() == YES)\r
                *CancelCheckWork = YES;\r
 \r
        // FTPS対応\r
        // 送信バッファの空き確認には影響しないが念のため\r
-//     while((*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_WRITE_BIT) != YES))\r
-       while(!IsSSLAttached(s) && (*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_WRITE_BIT) != YES))\r
-       {\r
-               if(AskAsyncDone(s, &Error, FD_CLOSE_BIT) == YES)\r
-               {\r
-                       Error = 1;\r
-                       break;\r
-               }\r
-\r
-               Sleep(1);\r
-               if(BackgrndMessageProc() == YES)\r
-                       *CancelCheckWork = YES;\r
-               else if(TimeOut != 0)\r
-               {\r
-                       time(&ElapseTime);\r
-                       ElapseTime -= StartTime;\r
-                       if(ElapseTime >= TimeOut)\r
-                       {\r
-                               DoPrintf("do_write timed out");\r
-                               *TimeOutErr = YES;\r
-                               *CancelCheckWork = YES;\r
-                       }\r
-               }\r
-       }\r
+//     while((*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_WRITE) != YES))\r
+       // Windows 2000でFD_WRITEが通知されないことがあるバグ修正\r
+//     while(!IsSSLAttached(s) && (*CancelCheckWork == NO) && (AskAsyncDone(s, &Error, FD_WRITE) != YES))\r
+//     {\r
+//             if(AskAsyncDone(s, &Error, FD_CLOSE) == YES)\r
+//             {\r
+//                     Error = 1;\r
+//                     break;\r
+//             }\r
+//\r
+//             Sleep(1);\r
+//             if(BackgrndMessageProc() == YES)\r
+//                     *CancelCheckWork = YES;\r
+//             else if(TimeOut != 0)\r
+//             {\r
+//                     time(&ElapseTime);\r
+//                     ElapseTime -= StartTime;\r
+//                     if(ElapseTime >= TimeOut)\r
+//                     {\r
+//                             DoPrintf("do_write timed out");\r
+//                             *TimeOutErr = YES;\r
+//                             *CancelCheckWork = YES;\r
+//                     }\r
+//             }\r
+//     }\r
 \r
        if((Error == 0) && (*CancelCheckWork == NO) && (*TimeOutErr == NO))\r
        {\r
@@ -1087,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
@@ -1133,6 +1185,20 @@ int do_send(SOCKET s, const char *buf, int len, int flags, int *TimeOutErr, int
 }\r
 \r
 \r
+// 同時接続対応\r
+void RemoveReceivedData(SOCKET s)\r
+{\r
+       char buf[1024];\r
+       int len;\r
+       int Error;\r
+       while((len = FTPS_recv(s, buf, sizeof(buf), MSG_PEEK)) >= 0)\r
+       {\r
+               AskAsyncDone(s, &Error, FD_READ);\r
+               FTPS_recv(s, buf, len, 0);\r
+       }\r
+}\r
+\r
+\r
 /*----- \r
 *\r
 *      Parameter\r
@@ -1150,7 +1216,7 @@ int CheckClosedAndReconnect(void)
 //SetTaskMsg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");\r
 \r
        Sts = FFFTP_SUCCESS;\r
-       if(AskAsyncDone(AskCmdCtrlSkt(), &Error, FD_CLOSE_BIT) == YES)\r
+       if(AskAsyncDone(AskCmdCtrlSkt(), &Error, FD_CLOSE) == YES)\r
        {\r
                Sts = ReConnectCmdSkt();\r
        }\r