OSDN Git Service

Modify documents.
[ffftp/ffftp.git] / socket.c
index db7b69f..1da9215 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -66,6 +66,11 @@ typedef struct {
        int FdRead;\r
        int FdWrite;\r
        int Error;\r
+       // ソケットにデータを付与\r
+       struct sockaddr_in HostAddrIPv4;\r
+       struct sockaddr_in SocksAddrIPv4;\r
+       struct sockaddr_in6 HostAddrIPv6;\r
+       struct sockaddr_in6 SocksAddrIPv6;\r
 } ASYNCSIGNAL;\r
 \r
 \r
@@ -91,10 +96,10 @@ typedef struct {
 static LRESULT CALLBACK SocketWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);\r
 static int AskAsyncDone(SOCKET s, int *Error, int Mask);\r
 static int AskAsyncDoneDbase(HANDLE Async, int *Error);\r
-static int RegistAsyncTable(SOCKET s);\r
-static int RegistAsyncTableDbase(HANDLE Async);\r
-static int UnRegistAsyncTable(SOCKET s);\r
-static int UnRegistAsyncTableDbase(HANDLE Async);\r
+static int RegisterAsyncTable(SOCKET s);\r
+static int RegisterAsyncTableDbase(HANDLE Async);\r
+static int UnregisterAsyncTable(SOCKET s);\r
+static int UnregisterAsyncTableDbase(HANDLE Async);\r
 \r
 \r
 /*===== 外部参照 =====*/\r
@@ -275,7 +280,7 @@ static LRESULT CALLBACK SocketWndProc(HWND hWnd, UINT message, WPARAM wParam, LP
 \r
                case WM_ASYNC_DBASE :\r
                        // APIの仕様上ハンドルが登録される前にウィンドウメッセージが呼び出される可能性あり\r
-                       RegistAsyncTableDbase((HANDLE)wParam);\r
+                       RegisterAsyncTableDbase((HANDLE)wParam);\r
                        // スレッド衝突のバグ修正\r
                        WaitForSingleObject(hAsyncTblAccMutex, INFINITE);\r
                        for(Pos = 0; Pos < MAX_SIGNAL_ENTRY_DBASE; Pos++)\r
@@ -450,7 +455,7 @@ static int AskAsyncDoneDbase(HANDLE Async, int *Error)
 *              \r
 *----------------------------------------------------------------------------*/\r
 \r
-static int RegistAsyncTable(SOCKET s)\r
+static int RegisterAsyncTable(SOCKET s)\r
 {\r
        int Sts;\r
        int Pos;\r
@@ -489,6 +494,11 @@ static int RegistAsyncTable(SOCKET s)
                                Signal[Pos].FdAccept = 0;\r
                                Signal[Pos].FdRead = 0;\r
                                Signal[Pos].FdWrite = 0;\r
+                               // ソケットにデータを付与\r
+                               memset(&Signal[Pos].HostAddrIPv4, 0, sizeof(struct sockaddr_in));\r
+                               memset(&Signal[Pos].SocksAddrIPv4, 0, sizeof(struct sockaddr_in));\r
+                               memset(&Signal[Pos].HostAddrIPv6, 0, sizeof(struct sockaddr_in6));\r
+                               memset(&Signal[Pos].SocksAddrIPv6, 0, sizeof(struct sockaddr_in6));\r
                                Sts = YES;\r
                                break;\r
                        }\r
@@ -516,7 +526,7 @@ static int RegistAsyncTable(SOCKET s)
 *              \r
 *----------------------------------------------------------------------------*/\r
 \r
-static int RegistAsyncTableDbase(HANDLE Async)\r
+static int RegisterAsyncTableDbase(HANDLE Async)\r
 {\r
        int Sts;\r
        int Pos;\r
@@ -578,7 +588,7 @@ static int RegistAsyncTableDbase(HANDLE Async)
 *              \r
 *----------------------------------------------------------------------------*/\r
 \r
-static int UnRegistAsyncTable(SOCKET s)\r
+static int UnregisterAsyncTable(SOCKET s)\r
 {\r
        int Sts;\r
        int Pos;\r
@@ -613,7 +623,7 @@ static int UnRegistAsyncTable(SOCKET s)
 *              \r
 *----------------------------------------------------------------------------*/\r
 \r
-static int UnRegistAsyncTableDbase(HANDLE Async)\r
+static int UnregisterAsyncTableDbase(HANDLE Async)\r
 {\r
        int Sts;\r
        int Pos;\r
@@ -639,6 +649,104 @@ static int UnRegistAsyncTableDbase(HANDLE Async)
 }\r
 \r
 \r
+// ソケットにデータを付与\r
+\r
+int SetAsyncTableDataIPv4(SOCKET s, struct sockaddr_in* Host, struct sockaddr_in* Socks)\r
+{\r
+       int Sts;\r
+       int Pos;\r
+\r
+       WaitForSingleObject(hAsyncTblAccMutex, INFINITE);\r
+       Sts = NO;\r
+       for(Pos = 0; Pos < MAX_SIGNAL_ENTRY; Pos++)\r
+       {\r
+               if(Signal[Pos].Socket == s)\r
+               {\r
+                       if(Host != NULL)\r
+                               memcpy(&Signal[Pos].HostAddrIPv4, Host, sizeof(struct sockaddr_in));\r
+                       if(Socks != NULL)\r
+                               memcpy(&Signal[Pos].SocksAddrIPv4, Socks, sizeof(struct sockaddr_in));\r
+                       Sts = YES;\r
+                       break;\r
+               }\r
+       }\r
+       ReleaseMutex(hAsyncTblAccMutex);\r
+\r
+       return(Sts);\r
+}\r
+\r
+int SetAsyncTableDataIPv6(SOCKET s, struct sockaddr_in6* Host, struct sockaddr_in6* Socks)\r
+{\r
+       int Sts;\r
+       int Pos;\r
+\r
+       WaitForSingleObject(hAsyncTblAccMutex, INFINITE);\r
+       Sts = NO;\r
+       for(Pos = 0; Pos < MAX_SIGNAL_ENTRY; Pos++)\r
+       {\r
+               if(Signal[Pos].Socket == s)\r
+               {\r
+                       if(Host != NULL)\r
+                               memcpy(&Signal[Pos].HostAddrIPv6, Host, sizeof(struct sockaddr_in6));\r
+                       if(Socks != NULL)\r
+                               memcpy(&Signal[Pos].SocksAddrIPv6, Socks, sizeof(struct sockaddr_in6));\r
+                       Sts = YES;\r
+                       break;\r
+               }\r
+       }\r
+       ReleaseMutex(hAsyncTblAccMutex);\r
+\r
+       return(Sts);\r
+}\r
+\r
+int GetAsyncTableDataIPv4(SOCKET s, struct sockaddr_in* Host, struct sockaddr_in* Socks)\r
+{\r
+       int Sts;\r
+       int Pos;\r
+\r
+       WaitForSingleObject(hAsyncTblAccMutex, INFINITE);\r
+       Sts = NO;\r
+       for(Pos = 0; Pos < MAX_SIGNAL_ENTRY; Pos++)\r
+       {\r
+               if(Signal[Pos].Socket == s)\r
+               {\r
+                       if(Host != NULL)\r
+                               memcpy(Host, &Signal[Pos].HostAddrIPv4, sizeof(struct sockaddr_in));\r
+                       if(Socks != NULL)\r
+                               memcpy(Socks, &Signal[Pos].SocksAddrIPv4, sizeof(struct sockaddr_in));\r
+                       Sts = YES;\r
+                       break;\r
+               }\r
+       }\r
+       ReleaseMutex(hAsyncTblAccMutex);\r
+\r
+       return(Sts);\r
+}\r
+\r
+int GetAsyncTableDataIPv6(SOCKET s, struct sockaddr_in6* Host, struct sockaddr_in6* Socks)\r
+{\r
+       int Sts;\r
+       int Pos;\r
+\r
+       WaitForSingleObject(hAsyncTblAccMutex, INFINITE);\r
+       Sts = NO;\r
+       for(Pos = 0; Pos < MAX_SIGNAL_ENTRY; Pos++)\r
+       {\r
+               if(Signal[Pos].Socket == s)\r
+               {\r
+                       if(Host != NULL)\r
+                               memcpy(Host, &Signal[Pos].HostAddrIPv6, sizeof(struct sockaddr_in6));\r
+                       if(Socks != NULL)\r
+                               memcpy(Socks, &Signal[Pos].SocksAddrIPv6, sizeof(struct sockaddr_in6));\r
+                       Sts = YES;\r
+                       break;\r
+               }\r
+       }\r
+       ReleaseMutex(hAsyncTblAccMutex);\r
+\r
+       return(Sts);\r
+}\r
+\r
 \r
 \r
 \r
@@ -666,7 +774,7 @@ struct hostent *do_gethostbynameIPv4(const char *Name, char *Buf, int Len, int *
        hAsync = WSAAsyncGetHostByNameM(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len);\r
        if(hAsync != NULL)\r
        {\r
-               RegistAsyncTableDbase(hAsync);\r
+               RegisterAsyncTableDbase(hAsync);\r
                while((*CancelCheckWork == NO) && (AskAsyncDoneDbase(hAsync, &Error) != YES))\r
                {\r
                        Sleep(1);\r
@@ -682,7 +790,7 @@ struct hostent *do_gethostbynameIPv4(const char *Name, char *Buf, int Len, int *
                {\r
                        Ret = (struct hostent *)Buf;\r
                }\r
-               UnRegistAsyncTableDbase(hAsync);\r
+               UnregisterAsyncTableDbase(hAsync);\r
        }\r
        return(Ret);\r
 #else\r
@@ -710,7 +818,7 @@ struct hostent *do_gethostbynameIPv6(const char *Name, char *Buf, int Len, int *
        hAsync = WSAAsyncGetHostByNameIPv6M(hWndSocket, WM_ASYNC_DBASE, Name, Buf, Len, AF_INET6);\r
        if(hAsync != NULL)\r
        {\r
-               RegistAsyncTableDbase(hAsync);\r
+               RegisterAsyncTableDbase(hAsync);\r
                while((*CancelCheckWork == NO) && (AskAsyncDoneDbase(hAsync, &Error) != YES))\r
                {\r
                        Sleep(1);\r
@@ -720,13 +828,13 @@ struct hostent *do_gethostbynameIPv6(const char *Name, char *Buf, int Len, int *
 \r
                if(*CancelCheckWork == YES)\r
                {\r
-                       WSACancelAsyncRequest(hAsync);\r
+                       WSACancelAsyncRequestIPv6(hAsync);\r
                }\r
                else if(Error == 0)\r
                {\r
                        Ret = (struct hostent *)Buf;\r
                }\r
-               UnRegistAsyncTableDbase(hAsync);\r
+               UnregisterAsyncTableDbase(hAsync);\r
        }\r
        return(Ret);\r
 #else\r
@@ -745,7 +853,7 @@ SOCKET do_socket(int af, int type, int protocol)
        Ret = socket(af, type, protocol);\r
        if(Ret != INVALID_SOCKET)\r
        {\r
-               RegistAsyncTable(Ret);\r
+               RegisterAsyncTable(Ret);\r
        }\r
 #if DBG_MSG\r
        DoPrintf("# do_socket (S=%x)", Ret);\r
@@ -769,7 +877,7 @@ int do_closesocket(SOCKET s)
 \r
        // スレッド衝突のバグ修正\r
        WSAAsyncSelect(s, hWndSocket, WM_ASYNC_SOCKET, 0);\r
-       UnRegistAsyncTable(s);\r
+       UnregisterAsyncTable(s);\r
        // FTPS対応\r
 //     Ret = closesocket(s);\r
        Ret = FTPS_closesocket(s);\r
@@ -792,7 +900,7 @@ int do_closesocket(SOCKET s)
        if(BackgrndMessageProc() == YES)\r
                CancelCheckWork = YES;\r
        // スレッド衝突のバグ修正\r
-//     UnRegistAsyncTable(s);\r
+//     UnregisterAsyncTable(s);\r
 \r
 #if DBG_MSG\r
        DoPrintf("# Exit close");\r
@@ -929,8 +1037,10 @@ SOCKET do_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
                                DoPrintf("## do_sccept (S=%x)", Ret2);\r
                                DoPrintf("## Async set: FD_CONNECT|FD_CLOSE|FD_ACCEPT|FD_READ|FD_WRITE");\r
 #endif\r
-                               RegistAsyncTable(Ret2);\r
-                               if(WSAAsyncSelect(Ret2, hWndSocket, WM_ASYNC_SOCKET, FD_CONNECT | FD_CLOSE | FD_ACCEPT | FD_READ | FD_WRITE) == SOCKET_ERROR)\r
+                               RegisterAsyncTable(Ret2);\r
+                               // 高速化のためFD_READとFD_WRITEを使用しない\r
+//                             if(WSAAsyncSelect(Ret2, hWndSocket, WM_ASYNC_SOCKET, FD_CONNECT | FD_CLOSE | FD_ACCEPT | FD_READ | FD_WRITE) == SOCKET_ERROR)\r
+                               if(WSAAsyncSelect(Ret2, hWndSocket, WM_ASYNC_SOCKET, FD_CONNECT | FD_CLOSE | FD_ACCEPT) == SOCKET_ERROR)\r
                                {\r
                                        do_closesocket(Ret2);\r
                                        Ret2 = INVALID_SOCKET;\r
@@ -1190,10 +1300,10 @@ void RemoveReceivedData(SOCKET s)
 {\r
        char buf[1024];\r
        int len;\r
-       int Error;\r
-       while((len = FTPS_recv(s, buf, sizeof(buf), MSG_PEEK)) >= 0)\r
+//     int Error;\r
+       while((len = FTPS_recv(s, buf, sizeof(buf), MSG_PEEK)) > 0)\r
        {\r
-               AskAsyncDone(s, &Error, FD_READ);\r
+//             AskAsyncDone(s, &Error, FD_READ);\r
                FTPS_recv(s, buf, len, 0);\r
        }\r
 }\r
@@ -1225,3 +1335,19 @@ int CheckClosedAndReconnect(void)
 \r
 \r
 \r
+// 同時接続対応\r
+int CheckClosedAndReconnectTrnSkt(SOCKET *Skt, int *CancelCheckWork)\r
+{\r
+       int Error;\r
+       int Sts;\r
+\r
+//SetTaskMsg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");\r
+\r
+       Sts = FFFTP_SUCCESS;\r
+       if(AskAsyncDone(*Skt, &Error, FD_CLOSE) == YES)\r
+       {\r
+               Sts = ReConnectTrnSkt(Skt, CancelCheckWork);\r
+       }\r
+       return(Sts);\r
+}\r
+\r