OSDN Git Service

dd4d28846f446ce402c39dc0c923c7f2f6ab6a6c
[ffftp/ffftp.git] / connect.c
1 /*=============================================================================\r
2 *\r
3 *                                                               ホストへの接続/切断\r
4 *\r
5 ===============================================================================\r
6 / Copyright (C) 1997-2007 Sota. All rights reserved.\r
7 /\r
8 / Redistribution and use in source and binary forms, with or without \r
9 / modification, are permitted provided that the following conditions \r
10 / are met:\r
11 /\r
12 /  1. Redistributions of source code must retain the above copyright \r
13 /     notice, this list of conditions and the following disclaimer.\r
14 /  2. Redistributions in binary form must reproduce the above copyright \r
15 /     notice, this list of conditions and the following disclaimer in the \r
16 /     documentation and/or other materials provided with the distribution.\r
17 /\r
18 / THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR \r
19 / IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES \r
20 / OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
21 / IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, \r
22 / INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \r
23 / BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
24 / USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
25 / ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
26 / (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF \r
27 / THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 /============================================================================*/\r
29 \r
30 #define STRICT\r
31 #include <stdio.h>\r
32 #include <stddef.h>\r
33 #include <stdlib.h>\r
34 #include <stdarg.h>\r
35 #include <string.h>\r
36 #include <mbstring.h>\r
37 #include <time.h>\r
38 // IPv6対応\r
39 //#include <winsock.h>\r
40 #include <winsock2.h>\r
41 #include <windowsx.h>\r
42 #include <commctrl.h>\r
43 \r
44 #include "common.h"\r
45 #include "resource.h"\r
46 \r
47 #include <htmlhelp.h>\r
48 #include "helpid.h"\r
49 \r
50 // UTF-8対応\r
51 #undef __MBSWRAPPER_H__\r
52 #include "mbswrapper.h"\r
53 \r
54 \r
55 /*===== プロトタイプ =====*/\r
56 \r
57 // 64ビット対応\r
58 //static BOOL CALLBACK QuickConDialogCallBack(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam);\r
59 static INT_PTR CALLBACK QuickConDialogCallBack(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam);\r
60 static int SendInitCommand(char *Cmd);\r
61 static void AskUseFireWall(char *Host, int *Fire, int *Pasv, int *List);\r
62 static void SaveCurrentSetToHistory(void);\r
63 static int ReConnectSkt(SOCKET *Skt);\r
64 // 暗号化通信対応\r
65 // 同時接続対応\r
66 //static SOCKET DoConnect(char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security);\r
67 static SOCKET DoConnectCrypt(int CryptMode, HOSTDATA* HostData, char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security, int *CancelCheckWork);\r
68 static SOCKET DoConnect(HOSTDATA* HostData, char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security, int *CancelCheckWork);\r
69 static int CheckOneTimePassword(char *Pass, char *Reply, int Type);\r
70 static BOOL CALLBACK BlkHookFnc(void);\r
71 static int Socks5MakeCmdPacket(SOCKS5REQUEST *Packet, char Cmd, int ValidIP, ulong IP, char *Host, ushort Port);\r
72 // IPv6対応\r
73 static int Socks5MakeCmdPacketIPv6(SOCKS5REQUEST *Packet, char Cmd, int ValidIP, char *IP, char *Host, ushort Port);\r
74 static int SocksSendCmd(SOCKET Socket, void *Data, int Size, int *CancelCheckWork);\r
75 // 同時接続対応\r
76 //static int Socks5GetCmdReply(SOCKET Socket, SOCKS5REPLY *Packet);\r
77 static int Socks5GetCmdReply(SOCKET Socket, SOCKS5REPLY *Packet, int *CancelCheckWork);\r
78 // 同時接続対応\r
79 //static int Socks4GetCmdReply(SOCKET Socket, SOCKS4REPLY *Packet);\r
80 static int Socks4GetCmdReply(SOCKET Socket, SOCKS4REPLY *Packet, int *CancelCheckWork);\r
81 static int Socks5SelMethod(SOCKET Socket, int *CancelCheckWork);\r
82 \r
83 /*===== 外部参照 =====*/\r
84 \r
85 extern char FilterStr[FILTER_EXT_LEN+1];\r
86 extern char TitleHostName[HOST_ADRS_LEN+1];\r
87 extern int CancelFlg;\r
88 \r
89 /* 設定値 */\r
90 extern char UserMailAdrs[USER_MAIL_LEN+1];\r
91 extern char FwallHost[HOST_ADRS_LEN+1];\r
92 extern char FwallUser[USER_NAME_LEN+1];\r
93 extern char FwallPass[PASSWORD_LEN+1];\r
94 extern int FwallPort;\r
95 extern int FwallType;\r
96 extern int FwallDefault;\r
97 extern int FwallSecurity;\r
98 extern int FwallResolv;\r
99 extern int FwallLower;\r
100 extern int FwallDelimiter;\r
101 extern int PasvDefault;\r
102 extern int QuickAnonymous;\r
103 \r
104 /*===== ローカルなワーク =====*/\r
105 \r
106 static int Anonymous;\r
107 static int TryConnect = NO;\r
108 static SOCKET CmdCtrlSocket = INVALID_SOCKET;\r
109 static SOCKET TrnCtrlSocket = INVALID_SOCKET;\r
110 static HOSTDATA CurHost;\r
111 \r
112 /* 接続中の接続先、SOCKSサーバのアドレス情報を保存しておく */\r
113 /* この情報はlistenソケットを取得する際に用いる */\r
114 // IPv6対応\r
115 //static struct sockaddr_in SocksSockAddr;      /* SOCKSサーバのアドレス情報 */\r
116 //static struct sockaddr_in CurSockAddr;                /* 接続先ホストのアドレス情報 */\r
117 static struct sockaddr_in SocksSockAddrIPv4;    /* SOCKSサーバのアドレス情報 */\r
118 static struct sockaddr_in CurSockAddrIPv4;              /* 接続先ホストのアドレス情報 */\r
119 static struct sockaddr_in6 SocksSockAddrIPv6;   /* SOCKSサーバのアドレス情報 */\r
120 static struct sockaddr_in6 CurSockAddrIPv6;             /* 接続先ホストのアドレス情報 */\r
121 static const struct in6_addr IN6ADDR_NONE = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};\r
122 \r
123 static int UseIPadrs;\r
124 static char DomainName[HOST_ADRS_LEN+1];\r
125 \r
126 \r
127 \r
128 \r
129 /*----- ホスト一覧を使ってホストへ接続 ----------------------------------------\r
130 *\r
131 *       Parameter\r
132 *               int Type : ダイアログのタイプ (DLG_TYPE_xxx)\r
133 *               int Num : 接続するホスト番号(0~, -1=ダイアログを出す)\r
134 \r
135 *       Return Value\r
136 *               なし\r
137 *----------------------------------------------------------------------------*/\r
138 \r
139 void ConnectProc(int Type, int Num)\r
140 {\r
141         int Save;\r
142         int LFSort;\r
143         int LDSort;\r
144         int RFSort;\r
145         int RDSort;\r
146 \r
147         SaveBookMark();\r
148         SaveCurrentSetToHost();\r
149 \r
150         if((Num >= 0) || (SelectHost(Type) == YES))\r
151         {\r
152                 if(Num >= 0)\r
153                         SetCurrentHost(Num);\r
154 \r
155                 /* 接続中なら切断する */\r
156                 if(CmdCtrlSocket != INVALID_SOCKET)\r
157                         DisconnectProc();\r
158 \r
159                 SetTaskMsg("----------------------------");\r
160 \r
161                 InitPWDcommand();\r
162                 CopyHostFromList(AskCurrentHost(), &CurHost);\r
163                 // UTF-8対応\r
164                 CurHost.CurNameKanjiCode = CurHost.NameKanjiCode;\r
165                 // IPv6対応\r
166                 CurHost.CurNetType = CurHost.NetType;\r
167 \r
168                 if(ConnectRas(CurHost.Dialup, CurHost.DialupAlways, CurHost.DialupNotify, CurHost.DialEntry) == FFFTP_SUCCESS)\r
169                 {\r
170                         SetHostKanaCnvImm(CurHost.KanaCnv);\r
171                         SetHostKanjiCodeImm(CurHost.KanjiCode);\r
172                         SetSyncMoveMode(CurHost.SyncMove);\r
173 \r
174                         if((AskSaveSortToHost() == YES) && (CurHost.Sort != SORT_NOTSAVED))\r
175                         {\r
176                                 DecomposeSortType(CurHost.Sort, &LFSort, &LDSort, &RFSort, &RDSort);\r
177                                 SetSortTypeImm(LFSort, LDSort, RFSort, RDSort);\r
178                                 ReSortDispList(WIN_LOCAL, &CancelFlg);\r
179                         }\r
180 \r
181                         Save = NO;\r
182                         if(strlen(CurHost.PassWord) > 0)\r
183                                 Save = YES;\r
184 \r
185                         DisableUserOpe();\r
186                         // 暗号化通信対応\r
187                         // 同時接続対応\r
188 //                      CmdCtrlSocket = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, Save, CurHost.Security);\r
189                         CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, Save, CurHost.Security, &CancelFlg);\r
190                         TrnCtrlSocket = CmdCtrlSocket;\r
191 \r
192                         if(CmdCtrlSocket != INVALID_SOCKET)\r
193                         {\r
194                                 // 暗号化通信対応\r
195                                 switch(CurHost.CryptMode)\r
196                                 {\r
197                                 case CRYPT_NONE:\r
198                                         if(CurHost.UseFTPIS != NO || CurHost.UseSFTP != NO)\r
199                                         {\r
200                                                 if(DialogBox(GetFtpInst(), MAKEINTRESOURCE(savecrypt_dlg), GetMainHwnd(), ExeEscDialogProc) == YES)\r
201                                                         SetHostExcryption(AskCurrentHost(), CurHost.UseNoEncryption, CurHost.UseFTPES, NO, NO);\r
202                                         }\r
203                                         break;\r
204                                 case CRYPT_FTPES:\r
205                                         if(CurHost.UseNoEncryption != NO || CurHost.UseFTPIS != NO || CurHost.UseSFTP != NO)\r
206                                         {\r
207                                                 if(DialogBox(GetFtpInst(), MAKEINTRESOURCE(savecrypt_dlg), GetMainHwnd(), ExeEscDialogProc) == YES)\r
208                                                         SetHostExcryption(AskCurrentHost(), NO, CurHost.UseFTPES, NO, NO);\r
209                                         }\r
210                                         break;\r
211                                 case CRYPT_FTPIS:\r
212                                         if(CurHost.UseNoEncryption != NO || CurHost.UseFTPES != NO || CurHost.UseSFTP != NO)\r
213                                         {\r
214                                                 if(DialogBox(GetFtpInst(), MAKEINTRESOURCE(savecrypt_dlg), GetMainHwnd(), ExeEscDialogProc) == YES)\r
215                                                         SetHostExcryption(AskCurrentHost(), NO, NO, CurHost.UseFTPIS, NO);\r
216                                         }\r
217                                         break;\r
218                                 case CRYPT_SFTP:\r
219                                         if(CurHost.UseNoEncryption != NO || CurHost.UseFTPES != NO || CurHost.UseFTPIS != NO)\r
220                                         {\r
221                                                 if(DialogBox(GetFtpInst(), MAKEINTRESOURCE(savecrypt_dlg), GetMainHwnd(), ExeEscDialogProc) == YES)\r
222                                                         SetHostExcryption(AskCurrentHost(), NO, NO, NO, CurHost.UseSFTP);\r
223                                         }\r
224                                         break;\r
225                                 }\r
226 \r
227                                 // UTF-8対応\r
228                                 if(CurHost.CurNameKanjiCode == KANJI_AUTO)\r
229                                 {\r
230                                         if(DoDirListCmdSkt("", "", 999, &CancelFlg) == FTP_COMPLETE)\r
231                                                 CurHost.CurNameKanjiCode = AnalyzeNameKanjiCode(999);\r
232                                 }\r
233 \r
234                                 strcpy(TitleHostName, CurHost.HostName);\r
235                                 DispWindowTitle();\r
236                                 SoundPlay(SND_CONNECT);\r
237 \r
238                                 SendInitCommand(CurHost.InitCmd);\r
239 \r
240                                 if(strlen(CurHost.LocalInitDir) > 0)\r
241                                 {\r
242                                         DoLocalCWD(CurHost.LocalInitDir);\r
243                                         GetLocalDirForWnd();\r
244                                 }\r
245                                 InitTransCurDir();\r
246                                 DoCWD(CurHost.RemoteInitDir, YES, YES, YES);\r
247 \r
248                                 LoadBookMark();\r
249                                 GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);\r
250                         }\r
251                         else\r
252                                 SoundPlay(SND_ERROR);\r
253 \r
254                         EnableUserOpe();\r
255                 }\r
256                 else\r
257                         SetTaskMsg(MSGJPN001);\r
258         }\r
259         return;\r
260 }\r
261 \r
262 \r
263 /*----- ホスト名を入力してホストへ接続 ----------------------------------------\r
264 *\r
265 *       Parameter\r
266 *               なし\r
267 *\r
268 *       Return Value\r
269 *               なし\r
270 *----------------------------------------------------------------------------*/\r
271 \r
272 void QuickConnectProc(void)\r
273 {\r
274         char Tmp[FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1 + 2];\r
275         char File[FMAX_PATH+1];\r
276 \r
277         SaveBookMark();\r
278         SaveCurrentSetToHost();\r
279 \r
280         if(DialogBoxParam(GetFtpInst(), MAKEINTRESOURCE(hostname_dlg), GetMainHwnd(), QuickConDialogCallBack, (LPARAM)Tmp) == YES)\r
281         {\r
282                 /* 接続中なら切断する */\r
283                 if(CmdCtrlSocket != INVALID_SOCKET)\r
284                         DisconnectProc();\r
285 \r
286                 SetTaskMsg("----------------------------");\r
287 \r
288                 InitPWDcommand();\r
289                 CopyDefaultHost(&CurHost);\r
290                 // UTF-8対応\r
291                 CurHost.CurNameKanjiCode = CurHost.NameKanjiCode;\r
292                 // IPv6対応\r
293                 CurHost.CurNetType = CurHost.NetType;\r
294                 if(SplitUNCpath(Tmp, CurHost.HostAdrs, CurHost.RemoteInitDir, File, CurHost.UserName, CurHost.PassWord, &CurHost.Port) == FFFTP_SUCCESS)\r
295                 {\r
296                         if(strlen(CurHost.UserName) == 0)\r
297                         {\r
298                                 strcpy(CurHost.UserName, Tmp + FMAX_PATH+1);\r
299                                 strcpy(CurHost.PassWord, Tmp + FMAX_PATH+1 + USER_NAME_LEN+1);\r
300                         }\r
301 \r
302                         SetCurrentHost(HOSTNUM_NOENTRY);\r
303                         AskUseFireWall(CurHost.HostAdrs, &CurHost.FireWall, &CurHost.Pasv, &CurHost.ListCmdOnly);\r
304                         CurHost.FireWall = (int)Tmp[FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1];\r
305                         CurHost.Pasv = (int)Tmp[FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1 + 1];\r
306 \r
307                         SetHostKanaCnvImm(CurHost.KanaCnv);\r
308                         SetHostKanjiCodeImm(CurHost.KanjiCode);\r
309                         SetSyncMoveMode(CurHost.SyncMove);\r
310 \r
311                         DisableUserOpe();\r
312                         // 暗号化通信対応\r
313                         // 同時接続対応\r
314 //                      CmdCtrlSocket = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security);\r
315                         CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, &CancelFlg);\r
316                         TrnCtrlSocket = CmdCtrlSocket;\r
317 \r
318                         if(CmdCtrlSocket != INVALID_SOCKET)\r
319                         {\r
320                                 // UTF-8対応\r
321                                 if(CurHost.CurNameKanjiCode == KANJI_AUTO)\r
322                                 {\r
323                                         if(DoDirListCmdSkt("", "", 999, &CancelFlg) == FTP_COMPLETE)\r
324                                                 CurHost.CurNameKanjiCode = AnalyzeNameKanjiCode(999);\r
325                                 }\r
326 \r
327                                 strcpy(TitleHostName, CurHost.HostAdrs);\r
328                                 DispWindowTitle();\r
329                                 SoundPlay(SND_CONNECT);\r
330 \r
331                                 InitTransCurDir();\r
332                                 DoCWD(CurHost.RemoteInitDir, YES, YES, YES);\r
333 \r
334                                 GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);\r
335                                 EnableUserOpe();\r
336 \r
337                                 if(strlen(File) > 0)\r
338                                         DirectDownLoadProc(File);\r
339                         }\r
340                         else\r
341                         {\r
342                                 SoundPlay(SND_ERROR);\r
343                                 EnableUserOpe();\r
344                         }\r
345                 }\r
346         }\r
347         return;\r
348 }\r
349 \r
350 \r
351 /*----- クイック接続ダイアログのコールバック ----------------------------------\r
352 *\r
353 *       Parameter\r
354 *               HWND hDlg : ウインドウハンドル\r
355 *               UINT message : メッセージ番号\r
356 *               WPARAM wParam : メッセージの WPARAM 引数\r
357 *               LPARAM lParam : メッセージの LPARAM 引数\r
358 *\r
359 *       Return Value\r
360 *               BOOL TRUE/FALSE\r
361 *----------------------------------------------------------------------------*/\r
362 \r
363 // 64ビット対応\r
364 //static BOOL CALLBACK QuickConDialogCallBack(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)\r
365 static INT_PTR CALLBACK QuickConDialogCallBack(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)\r
366 {\r
367         static char *Buf;\r
368         int i;\r
369         HISTORYDATA Tmp;\r
370 \r
371 //char Str[HOST_ADRS_LEN+USER_NAME_LEN+INIT_DIR_LEN+5+1];\r
372 \r
373         switch (iMessage)\r
374         {\r
375                 case WM_INITDIALOG :\r
376                         SendDlgItemMessage(hDlg, QHOST_HOST, CB_LIMITTEXT, FMAX_PATH, 0);\r
377                         SendDlgItemMessage(hDlg, QHOST_HOST, WM_SETTEXT, 0, (LPARAM)"");\r
378                         SendDlgItemMessage(hDlg, QHOST_USER, EM_LIMITTEXT, USER_NAME_LEN, 0);\r
379                         if(QuickAnonymous == YES)\r
380                         {\r
381                                 SendDlgItemMessage(hDlg, QHOST_USER, WM_SETTEXT, 0, (LPARAM)"anonymous");\r
382                                 SendDlgItemMessage(hDlg, QHOST_PASS, WM_SETTEXT, 0, (LPARAM)UserMailAdrs);\r
383                         }\r
384                         else\r
385                         {\r
386                                 SendDlgItemMessage(hDlg, QHOST_USER, WM_SETTEXT, 0, (LPARAM)"");\r
387                                 SendDlgItemMessage(hDlg, QHOST_PASS, WM_SETTEXT, 0, (LPARAM)"");\r
388                         }\r
389                         SendDlgItemMessage(hDlg, QHOST_PASS, EM_LIMITTEXT, PASSWORD_LEN, 0);\r
390                         SendDlgItemMessage(hDlg, QHOST_FWALL, BM_SETCHECK, FwallDefault, 0);\r
391                         SendDlgItemMessage(hDlg, QHOST_PASV, BM_SETCHECK, PasvDefault, 0);\r
392                         for(i = 0; i < HISTORY_MAX; i++)\r
393                         {\r
394                                 if(GetHistoryByNum(i, &Tmp) == FFFTP_SUCCESS)\r
395                                 {\r
396 //sprintf(Str, "%s (%s) %s", Tmp.HostAdrs, Tmp.UserName, Tmp.RemoteInitDir);\r
397 //SendDlgItemMessage(hDlg, QHOST_HOST, CB_ADDSTRING, 0, (LPARAM)Str);\r
398                                         SendDlgItemMessage(hDlg, QHOST_HOST, CB_ADDSTRING, 0, (LPARAM)Tmp.HostAdrs);\r
399                                 }\r
400                         }\r
401                         Buf = (char *)lParam;\r
402                         return(TRUE);\r
403 \r
404                 case WM_COMMAND :\r
405                         switch(GET_WM_COMMAND_ID(wParam, lParam))\r
406                         {\r
407                                 case IDOK :\r
408                                         SendDlgItemMessage(hDlg, QHOST_HOST, WM_GETTEXT, FMAX_PATH+1, (LPARAM)Buf);\r
409                                         SendDlgItemMessage(hDlg, QHOST_USER, WM_GETTEXT, USER_NAME_LEN+1, (LPARAM)Buf + FMAX_PATH+1);\r
410                                         SendDlgItemMessage(hDlg, QHOST_PASS, WM_GETTEXT, PASSWORD_LEN+1, (LPARAM)Buf + FMAX_PATH+1 + USER_NAME_LEN+1);\r
411                                         *(Buf + FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1) = (char)SendDlgItemMessage(hDlg, QHOST_FWALL, BM_GETCHECK, 0, 0);\r
412                                         *(Buf + FMAX_PATH+1 + USER_NAME_LEN+1 + PASSWORD_LEN+1+1) = (char)SendDlgItemMessage(hDlg, QHOST_PASV, BM_GETCHECK, 0, 0);\r
413                                         EndDialog(hDlg, YES);\r
414                                         break;\r
415 \r
416                                 case IDCANCEL :\r
417                                         EndDialog(hDlg, NO);\r
418                                         break;\r
419 \r
420 //                              case QHOST_HOST :\r
421 //                                      if(HIWORD(wParam) == CBN_EDITCHANGE)\r
422 //                                              DoPrintf("EDIT");\r
423 //                                      break;\r
424                         }\r
425             return(TRUE);\r
426         }\r
427         return(FALSE);\r
428 }\r
429 \r
430 \r
431 /*----- 指定したホスト名でホストへ接続 ----------------------------------------\r
432 *\r
433 *       Parameter\r
434 *               char *unc : UNC文字列\r
435 *               int Kanji : ホストの漢字コード (KANJI_xxx)\r
436 *               int Kana : 半角かな→全角変換モード (YES/NO)\r
437 *               int Fkanji : ファイル名の漢字コード (KANJI_xxx)\r
438 *               int TrMode : 転送モード (TYPE_xx)\r
439 *\r
440 *       Return Value\r
441 *               なし\r
442 *----------------------------------------------------------------------------*/\r
443 \r
444 void DirectConnectProc(char *unc, int Kanji, int Kana, int Fkanji, int TrMode)\r
445 {\r
446         char Host[HOST_ADRS_LEN+1];\r
447         char Path[FMAX_PATH+1];\r
448         char File[FMAX_PATH+1];\r
449         char User[USER_NAME_LEN+1];\r
450         char Pass[PASSWORD_LEN+1];\r
451         int Port;\r
452 \r
453         SaveBookMark();\r
454         SaveCurrentSetToHost();\r
455 \r
456         /* 接続中なら切断する */\r
457         if(CmdCtrlSocket != INVALID_SOCKET)\r
458                 DisconnectProc();\r
459 \r
460         SetTaskMsg("----------------------------");\r
461 \r
462         InitPWDcommand();\r
463         if(SplitUNCpath(unc, Host, Path, File, User, Pass, &Port) == FFFTP_SUCCESS)\r
464         {\r
465                 if(strlen(User) == 0)\r
466                 {\r
467                         strcpy(User, "anonymous");\r
468                         strcpy(Pass, UserMailAdrs);\r
469                 }\r
470 \r
471                 CopyDefaultHost(&CurHost);\r
472 \r
473                 SetCurrentHost(HOSTNUM_NOENTRY);\r
474                 strcpy(CurHost.HostAdrs, Host);\r
475                 strcpy(CurHost.UserName, User);\r
476                 strcpy(CurHost.PassWord, Pass);\r
477                 strcpy(CurHost.RemoteInitDir, Path);\r
478                 AskUseFireWall(CurHost.HostAdrs, &CurHost.FireWall, &CurHost.Pasv, &CurHost.ListCmdOnly);\r
479                 CurHost.Port = Port;\r
480                 CurHost.KanjiCode = Kanji;\r
481                 CurHost.KanaCnv = Kana;\r
482                 CurHost.NameKanjiCode = Fkanji;\r
483                 CurHost.KanaCnv = YES;                  /* とりあえず */\r
484                 // UTF-8対応\r
485                 CurHost.CurNameKanjiCode = CurHost.NameKanjiCode;\r
486                 // IPv6対応\r
487                 CurHost.CurNetType = CurHost.NetType;\r
488 \r
489                 SetHostKanaCnvImm(CurHost.KanaCnv);\r
490                 SetHostKanjiCodeImm(CurHost.KanjiCode);\r
491                 SetSyncMoveMode(CurHost.SyncMove);\r
492 \r
493                 if(TrMode != TYPE_DEFAULT)\r
494                 {\r
495                         SetTransferTypeImm(TrMode);\r
496                         DispTransferType();\r
497                 }\r
498 \r
499                 DisableUserOpe();\r
500                 // 暗号化通信対応\r
501                 // 同時接続対応\r
502 //              CmdCtrlSocket = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security);\r
503                 CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, &CancelFlg);\r
504                 TrnCtrlSocket = CmdCtrlSocket;\r
505 \r
506                 if(CmdCtrlSocket != INVALID_SOCKET)\r
507                 {\r
508                         // UTF-8対応\r
509                         if(CurHost.CurNameKanjiCode == KANJI_AUTO)\r
510                         {\r
511                                 if(DoDirListCmdSkt("", "", 999, &CancelFlg) == FTP_COMPLETE)\r
512                                         CurHost.CurNameKanjiCode = AnalyzeNameKanjiCode(999);\r
513                         }\r
514 \r
515                         strcpy(TitleHostName, CurHost.HostAdrs);\r
516                         DispWindowTitle();\r
517                         SoundPlay(SND_CONNECT);\r
518 \r
519                         InitTransCurDir();\r
520                         DoCWD(CurHost.RemoteInitDir, YES, YES, YES);\r
521 \r
522                         GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);\r
523                         EnableUserOpe();\r
524 \r
525                         if(strlen(File) > 0)\r
526                                 DirectDownLoadProc(File);\r
527                         else\r
528                                 ResetAutoExitFlg();\r
529                 }\r
530                 else\r
531                 {\r
532                         SoundPlay(SND_ERROR);\r
533                         EnableUserOpe();\r
534                 }\r
535         }\r
536         return;\r
537 }\r
538 \r
539 \r
540 /*----- ホストのヒストリで指定されたホストへ接続 ------------------------------\r
541 *\r
542 *       Parameter\r
543 *               int MenuCmd : 取り出すヒストリに割り当てられたメニューコマンド\r
544 *\r
545 *       Return Value\r
546 *               なし\r
547 *----------------------------------------------------------------------------*/\r
548 \r
549 void HistoryConnectProc(int MenuCmd)\r
550 {\r
551         HISTORYDATA Hist;\r
552         int LFSort;\r
553         int LDSort;\r
554         int RFSort;\r
555         int RDSort;\r
556 \r
557         if(GetHistoryByCmd(MenuCmd, &Hist) == FFFTP_SUCCESS)\r
558         {\r
559                 SaveBookMark();\r
560                 SaveCurrentSetToHost();\r
561 \r
562                 /* 接続中なら切断する */\r
563                 if(CmdCtrlSocket != INVALID_SOCKET)\r
564                         DisconnectProc();\r
565 \r
566                 SetTaskMsg("----------------------------");\r
567 \r
568                 InitPWDcommand();\r
569                 CopyHistoryToHost(&Hist, &CurHost);\r
570                 // UTF-8対応\r
571                 CurHost.CurNameKanjiCode = CurHost.NameKanjiCode;\r
572                 // IPv6対応\r
573                 CurHost.CurNetType = CurHost.NetType;\r
574 \r
575                 if(ConnectRas(CurHost.Dialup, CurHost.DialupAlways, CurHost.DialupNotify, CurHost.DialEntry) == FFFTP_SUCCESS)\r
576                 {\r
577                         SetCurrentHost(HOSTNUM_NOENTRY);\r
578                         SetHostKanaCnvImm(CurHost.KanaCnv);\r
579                         SetHostKanjiCodeImm(CurHost.KanjiCode);\r
580                         SetSyncMoveMode(CurHost.SyncMove);\r
581 \r
582                         DecomposeSortType(CurHost.Sort, &LFSort, &LDSort, &RFSort, &RDSort);\r
583                         SetSortTypeImm(LFSort, LDSort, RFSort, RDSort);\r
584                         ReSortDispList(WIN_LOCAL, &CancelFlg);\r
585 \r
586                         SetTransferTypeImm(Hist.Type);\r
587                         DispTransferType();\r
588 \r
589                         DisableUserOpe();\r
590                         // 暗号化通信対応\r
591                         // 同時接続対応\r
592 //                      CmdCtrlSocket = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security);\r
593                         CmdCtrlSocket = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, &CancelFlg);\r
594                         TrnCtrlSocket = CmdCtrlSocket;\r
595 \r
596                         if(CmdCtrlSocket != INVALID_SOCKET)\r
597                         {\r
598                                 // UTF-8対応\r
599                                 if(CurHost.CurNameKanjiCode == KANJI_AUTO)\r
600                                 {\r
601                                         if(DoDirListCmdSkt("", "", 999, &CancelFlg) == FTP_COMPLETE)\r
602                                                 CurHost.CurNameKanjiCode = AnalyzeNameKanjiCode(999);\r
603                                 }\r
604 \r
605                                 strcpy(TitleHostName, CurHost.HostAdrs);\r
606                                 DispWindowTitle();\r
607                                 SoundPlay(SND_CONNECT);\r
608 \r
609                                 SendInitCommand(CurHost.InitCmd);\r
610 \r
611                                 DoLocalCWD(CurHost.LocalInitDir);\r
612                                 GetLocalDirForWnd();\r
613 \r
614                                 InitTransCurDir();\r
615                                 DoCWD(CurHost.RemoteInitDir, YES, YES, YES);\r
616 \r
617                                 GetRemoteDirForWnd(CACHE_NORMAL, &CancelFlg);\r
618                         }\r
619                         else\r
620                                 SoundPlay(SND_ERROR);\r
621 \r
622                         EnableUserOpe();\r
623                 }\r
624                 else\r
625                         SetTaskMsg(MSGJPN002);\r
626         }\r
627         else\r
628                 SoundPlay(SND_ERROR);\r
629 \r
630         return;\r
631 }\r
632 \r
633 \r
634 /*----- ホストの初期化コマンドを送る ------------------------------------------\r
635 *\r
636 *       Parameter\r
637 *               int Cmd : 初期化コマンドス\r
638 *\r
639 *       Return Value\r
640 *               なし\r
641 *\r
642 *       NOte\r
643 *               初期化コマンドは以下のようなフォーマットであること\r
644 *                       cmd1\0\r
645 *                       cmd1\r\ncmd2\r\n\0\r
646 *----------------------------------------------------------------------------*/\r
647 \r
648 static int SendInitCommand(char *Cmd)\r
649 {\r
650         char Tmp[INITCMD_LEN+1];\r
651         char *Pos;\r
652 \r
653         while(strlen(Cmd) > 0)\r
654         {\r
655                 strcpy(Tmp, Cmd);\r
656                 if((Pos = strchr(Tmp, '\r')) != NULL)\r
657                         *Pos = NUL;\r
658                 if(strlen(Tmp) > 0)\r
659                         DoQUOTE(Tmp);\r
660 \r
661                 if((Cmd = strchr(Cmd, '\n')) != NULL)\r
662                         Cmd++;\r
663                 else\r
664                         break;\r
665         }\r
666         return(0);\r
667 }\r
668 \r
669 \r
670 /*----- 指定のホストはFireWallを使う設定かどうかを返す ------------------------\r
671 *\r
672 *       Parameter\r
673 *               char *Hots : ホスト名\r
674 *               int *Fire : FireWallを使うかどうかを返すワーク\r
675 *               int *Pasv : PASVモードを返すワーク\r
676 *               int *List : LISTコマンドのみ使用フラグ\r
677 *\r
678 *       Return Value\r
679 *               なし\r
680 *----------------------------------------------------------------------------*/\r
681 \r
682 static void AskUseFireWall(char *Host, int *Fire, int *Pasv, int *List)\r
683 {\r
684         int i;\r
685         HOSTDATA Tmp;\r
686 \r
687         *Fire = FwallDefault;\r
688         *Pasv = PasvDefault;\r
689         // NLSTを送ってしまうバグ修正(ただしNLSTを使うべきホストへクイック接続できなくなる)\r
690 //      *List = NO;\r
691 \r
692         i = 0;\r
693         while(CopyHostFromList(i, &Tmp) == FFFTP_SUCCESS)\r
694         {\r
695                 if(strcmp(Host, Tmp.HostAdrs) == 0)\r
696                 {\r
697                         *Fire = Tmp.FireWall;\r
698                         *Pasv = Tmp.Pasv;\r
699                         *List = Tmp.ListCmdOnly;\r
700                         break;\r
701                 }\r
702                 i++;\r
703         }\r
704         return;\r
705 }\r
706 \r
707 \r
708 /*----- 接続しているホストのアドレスを返す ------------------------------------\r
709 *\r
710 *       Parameter\r
711 *               なし\r
712 *\r
713 *       Return Value\r
714 *               char *ホストのアドレス\r
715 *----------------------------------------------------------------------------*/\r
716 \r
717 char *AskHostAdrs(void)\r
718 {\r
719         return(CurHost.HostAdrs);\r
720 }\r
721 \r
722 \r
723 /*----- 接続しているホストのポートを返す --------------------------------------\r
724 *\r
725 *       Parameter\r
726 *               なし\r
727 *\r
728 *       Return Value\r
729 *               int ホストのポート\r
730 *----------------------------------------------------------------------------*/\r
731 \r
732 int AskHostPort(void)\r
733 {\r
734         return(CurHost.Port);\r
735 }\r
736 \r
737 /*----- 接続しているホストのファイル名の漢字コードを返す ----------------------\r
738 *\r
739 *       Parameter\r
740 *               なし\r
741 *\r
742 *       Return Value\r
743 *               int 漢字コード (KANJI_xxx)\r
744 *----------------------------------------------------------------------------*/\r
745 \r
746 int AskHostNameKanji(void)\r
747 {\r
748         // UTF-8対応\r
749 //      if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
750 //              CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
751 //\r
752 //      return(CurHost.NameKanjiCode);\r
753         return(CurHost.CurNameKanjiCode);\r
754 }\r
755 \r
756 \r
757 /*----- 接続しているホストのファイル名の半角カナ変換フラグを返す --------------\r
758 *\r
759 *       Parameter\r
760 *               なし\r
761 *\r
762 *       Return Value\r
763 *               int 半角カナを全角に変換するかどうか (YES/NO)\r
764 *----------------------------------------------------------------------------*/\r
765 \r
766 int AskHostNameKana(void)\r
767 {\r
768         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
769                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
770 \r
771         return(CurHost.NameKanaCnv);\r
772 }\r
773 \r
774 \r
775 /*----- 接続しているホストのLISTコマンドモードを返す --------------------------\r
776 *\r
777 *       Parameter\r
778 *               なし\r
779 *\r
780 *       Return Value\r
781 *               int LISTコマンドモード (YES/NO)\r
782 *----------------------------------------------------------------------------*/\r
783 \r
784 int AskListCmdMode(void)\r
785 {\r
786         if(CurHost.HostType == HTYPE_VMS)\r
787                 return(YES);\r
788         else\r
789         {\r
790                 if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
791                         CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
792                 return(CurHost.ListCmdOnly);\r
793         }\r
794 }\r
795 \r
796 \r
797 /*----- 接続しているホストでNLST -Rを使うかどうかを返す ------------------------\r
798 *\r
799 *       Parameter\r
800 *               なし\r
801 *\r
802 *       Return Value\r
803 *               int NLST -Rを使うかどうか (YES/NO)\r
804 *----------------------------------------------------------------------------*/\r
805 \r
806 int AskUseNLST_R(void)\r
807 {\r
808         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
809                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
810 \r
811         return(CurHost.UseNLST_R);\r
812 }\r
813 \r
814 \r
815 /*----- 接続しているホストのChmodコマンドを返す -------------------------------\r
816 *\r
817 *       Parameter\r
818 *               なし\r
819 *\r
820 *       Return Value\r
821 *               char *Chmodコマンド\r
822 *----------------------------------------------------------------------------*/\r
823 \r
824 char *AskHostChmodCmd(void)\r
825 {\r
826         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
827                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
828 \r
829         return(CurHost.ChmodCmd);\r
830 }\r
831 \r
832 \r
833 /*----- 接続しているホストのタイムゾーンを返す --------------------------------\r
834 *\r
835 *       Parameter\r
836 *               なし\r
837 *\r
838 *       Return Value\r
839 *               int タイムゾーン\r
840 *----------------------------------------------------------------------------*/\r
841 \r
842 int AskHostTimeZone(void)\r
843 {\r
844         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
845                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
846 \r
847         return(CurHost.TimeZone);\r
848 }\r
849 \r
850 \r
851 /*----- 接続しているホストのPASVモードを返す ----------------------------------\r
852 *\r
853 *       Parameter\r
854 *               なし\r
855 *\r
856 *       Return Value\r
857 *               int PASVモードかどうか (YES/NO)\r
858 *----------------------------------------------------------------------------*/\r
859 \r
860 int AskPasvMode(void)\r
861 {\r
862         return(CurHost.Pasv);\r
863 }\r
864 \r
865 \r
866 /*----- 接続しているホストのLNSTファイル名を返す ------------------------------\r
867 *\r
868 *       Parameter\r
869 *               なし\r
870 *\r
871 *       Return Value\r
872 *               char *ファイル名/オプション\r
873 *----------------------------------------------------------------------------*/\r
874 \r
875 char *AskHostLsName(void)\r
876 {\r
877         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
878                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
879 \r
880         return(CurHost.LsName);\r
881 }\r
882 \r
883 \r
884 /*----- 接続しているホストのホストタイプを返す --------------------------------\r
885 *\r
886 *       Parameter\r
887 *               なし\r
888 *\r
889 *       Return Value\r
890 *               char *ファイル名/オプション\r
891 *----------------------------------------------------------------------------*/\r
892 \r
893 int AskHostType(void)\r
894 {\r
895         if(AskCurrentHost() != HOSTNUM_NOENTRY)\r
896                 CopyHostFromListInConnect(AskCurrentHost(), &CurHost);\r
897 \r
898         return(CurHost.HostType);\r
899 }\r
900 \r
901 \r
902 /*----- 接続しているホストはFireWallを使うホストかどうかを返す ----------------\r
903 *\r
904 *       Parameter\r
905 *               なし\r
906 *\r
907 *       Return Value\r
908 *               int FireWallを使うかどうか (YES/NO)\r
909 *----------------------------------------------------------------------------*/\r
910 \r
911 int AskHostFireWall(void)\r
912 {\r
913         return(CurHost.FireWall);\r
914 }\r
915 \r
916 \r
917 /*----- 接続しているホストでフルパスでファイルアクセスしないかどうかを返す ----\r
918 *\r
919 *       Parameter\r
920 *               なし\r
921 *\r
922 *       Return Value\r
923 *               int フルパスでアクセスしない (YES=フルパス禁止/NO)\r
924 *----------------------------------------------------------------------------*/\r
925 \r
926 int AskNoFullPathMode(void)\r
927 {\r
928         if(CurHost.HostType == HTYPE_VMS)\r
929                 return(YES);\r
930         else\r
931                 return(CurHost.NoFullPath);\r
932 }\r
933 \r
934 \r
935 /*----- 接続しているユーザ名を返す --------------------------------------------\r
936 *\r
937 *       Parameter\r
938 *               なし\r
939 *\r
940 *       Return Value\r
941 *               char *ユーザ名\r
942 *----------------------------------------------------------------------------*/\r
943 \r
944 char *AskHostUserName(void)\r
945 {\r
946         return(CurHost.UserName);\r
947 }\r
948 \r
949 \r
950 /*----- 現在の設定をホストの設定にセットする ----------------------------------\r
951 *\r
952 *       Parameter\r
953 *               なし\r
954 *\r
955 *       Return Value\r
956 *               なし\r
957 *\r
958 *       Note\r
959 *               カレントディレクトリ、ソート方法をホストの設定にセットする\r
960 *----------------------------------------------------------------------------*/\r
961 \r
962 void SaveCurrentSetToHost(void)\r
963 {\r
964         int Host;\r
965         char LocDir[FMAX_PATH+1];\r
966         char HostDir[FMAX_PATH+1];\r
967 \r
968         if(TrnCtrlSocket != INVALID_SOCKET)\r
969         {\r
970                 if((Host = AskCurrentHost()) != HOSTNUM_NOENTRY)\r
971                 {\r
972                         CopyHostFromListInConnect(Host, &CurHost);\r
973                         if(CurHost.LastDir == YES)\r
974                         {\r
975                                 AskLocalCurDir(LocDir, FMAX_PATH);\r
976                                 AskRemoteCurDir(HostDir, FMAX_PATH);\r
977                                 SetHostDir(AskCurrentHost(), LocDir, HostDir);\r
978                         }\r
979                         SetHostSort(AskCurrentHost(), AskSortType(ITEM_LFILE), AskSortType(ITEM_LDIR), AskSortType(ITEM_RFILE), AskSortType(ITEM_RDIR));\r
980                 }\r
981         }\r
982         return;\r
983 }\r
984 \r
985 \r
986 /*----- 現在の設定をヒストリにセットする --------------------------------------\r
987 *\r
988 *       Parameter\r
989 *               なし\r
990 *\r
991 *       Return Value\r
992 *               なし\r
993 *----------------------------------------------------------------------------*/\r
994 \r
995 static void SaveCurrentSetToHistory(void)\r
996 {\r
997         char LocDir[FMAX_PATH+1];\r
998         char HostDir[FMAX_PATH+1];\r
999 \r
1000         AskLocalCurDir(LocDir, FMAX_PATH);\r
1001         AskRemoteCurDir(HostDir, FMAX_PATH);\r
1002         strcpy(CurHost.LocalInitDir, LocDir);\r
1003         strcpy(CurHost.RemoteInitDir, HostDir);\r
1004 \r
1005         CurHost.Sort = AskSortType(ITEM_LFILE) * 0x1000000 | AskSortType(ITEM_LDIR) * 0x10000 | AskSortType(ITEM_RFILE) * 0x100 | AskSortType(ITEM_RDIR);\r
1006 \r
1007         CurHost.KanjiCode = AskHostKanjiCode();\r
1008         CurHost.KanaCnv = AskHostKanaCnv();\r
1009 \r
1010         CurHost.SyncMove = AskSyncMoveMode();\r
1011 \r
1012         AddHostToHistory(&CurHost, AskTransferType());\r
1013         SetAllHistoryToMenu();\r
1014 \r
1015         return;\r
1016 }\r
1017 \r
1018 \r
1019 /*----- コマンドコントロールソケットの再接続 ----------------------------------\r
1020 *\r
1021 *       Parameter\r
1022 *               なし\r
1023 *\r
1024 *       Return Value\r
1025 *               int ステータス\r
1026 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1027 *----------------------------------------------------------------------------*/\r
1028 \r
1029 int ReConnectCmdSkt(void)\r
1030 {\r
1031         int Sts;\r
1032 \r
1033 \r
1034         if(CmdCtrlSocket != TrnCtrlSocket)\r
1035                 do_closesocket(TrnCtrlSocket);\r
1036         TrnCtrlSocket = INVALID_SOCKET;\r
1037 \r
1038         Sts = ReConnectSkt(&CmdCtrlSocket);\r
1039 \r
1040         TrnCtrlSocket = CmdCtrlSocket;\r
1041 \r
1042         return(Sts);\r
1043 }\r
1044 \r
1045 \r
1046 /*----- 転送コントロールソケットの再接続 --------------------------------------\r
1047 *\r
1048 *       Parameter\r
1049 *               なし\r
1050 *\r
1051 *       Return Value\r
1052 *               int ステータス\r
1053 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1054 *----------------------------------------------------------------------------*/\r
1055 \r
1056 //int ReConnectTrnSkt(void)\r
1057 //{\r
1058 //      return(ReConnectSkt(&TrnCtrlSocket));\r
1059 //}\r
1060 // 同時接続対応\r
1061 int ReConnectTrnSkt(SOCKET *Skt, int *CancelCheckWork)\r
1062 {\r
1063 //      char Path[FMAX_PATH+1];\r
1064         int Sts;\r
1065         // 暗号化通信対応\r
1066         HOSTDATA HostData;\r
1067 \r
1068         Sts = FFFTP_FAIL;\r
1069 \r
1070         SetTaskMsg(MSGJPN003);\r
1071 \r
1072 //      DisableUserOpe();\r
1073         /* 現在のソケットは切断 */\r
1074         if(*Skt != INVALID_SOCKET)\r
1075                 do_closesocket(*Skt);\r
1076         /* 再接続 */\r
1077         // 暗号化通信対応\r
1078         HostData = CurHost;\r
1079         if(HostData.CryptMode != CRYPT_NONE)\r
1080                 HostData.UseNoEncryption = NO;\r
1081         if(HostData.CryptMode != CRYPT_FTPES)\r
1082                 HostData.UseFTPES = NO;\r
1083         if(HostData.CryptMode != CRYPT_FTPIS)\r
1084                 HostData.UseFTPIS = NO;\r
1085         if(HostData.CryptMode != CRYPT_SFTP)\r
1086                 HostData.UseSFTP = NO;\r
1087         // UTF-8対応\r
1088         HostData.CurNameKanjiCode = HostData.NameKanjiCode;\r
1089         // IPv6対応\r
1090         HostData.CurNetType = HostData.NetType;\r
1091         // 暗号化通信対応\r
1092         // 同時接続対応\r
1093 //      if((*Skt = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security)) != INVALID_SOCKET)\r
1094         if((*Skt = DoConnect(&HostData, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, CancelCheckWork)) != INVALID_SOCKET)\r
1095         {\r
1096 //              AskRemoteCurDir(Path, FMAX_PATH);\r
1097 //              DoCWD(Path, YES, YES, YES);\r
1098                 Sts = FFFTP_SUCCESS;\r
1099         }\r
1100         else\r
1101                 SoundPlay(SND_ERROR);\r
1102 \r
1103 //      EnableUserOpe();\r
1104         return(Sts);\r
1105 }\r
1106 \r
1107 \r
1108 /*----- 回線の再接続 ----------------------------------------------------------\r
1109 *\r
1110 *       Parameter\r
1111 *               SOCKET *Skt : 接続したソケットを返すワーク\r
1112 *\r
1113 *       Return Value\r
1114 *               int ステータス\r
1115 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1116 *----------------------------------------------------------------------------*/\r
1117 \r
1118 static int ReConnectSkt(SOCKET *Skt)\r
1119 {\r
1120         char Path[FMAX_PATH+1];\r
1121         int Sts;\r
1122 \r
1123         Sts = FFFTP_FAIL;\r
1124 \r
1125         SetTaskMsg(MSGJPN003);\r
1126 \r
1127         DisableUserOpe();\r
1128         /* 現在のソケットは切断 */\r
1129         if(*Skt != INVALID_SOCKET)\r
1130                 do_closesocket(*Skt);\r
1131         /* 再接続 */\r
1132         // 暗号化通信対応\r
1133         // 同時接続対応\r
1134 //      if((*Skt = DoConnect(CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security)) != INVALID_SOCKET)\r
1135         if((*Skt = DoConnect(&CurHost, CurHost.HostAdrs, CurHost.UserName, CurHost.PassWord, CurHost.Account, CurHost.Port, CurHost.FireWall, NO, CurHost.Security, &CancelFlg)) != INVALID_SOCKET)\r
1136         {\r
1137                 AskRemoteCurDir(Path, FMAX_PATH);\r
1138                 DoCWD(Path, YES, YES, YES);\r
1139                 Sts = FFFTP_SUCCESS;\r
1140         }\r
1141         else\r
1142                 SoundPlay(SND_ERROR);\r
1143 \r
1144         EnableUserOpe();\r
1145         return(Sts);\r
1146 }\r
1147 \r
1148 \r
1149 /*----- コマンドコントロールソケットを返す ------------------------------------\r
1150 *\r
1151 *       Parameter\r
1152 *               なし\r
1153 *\r
1154 *       Return Value\r
1155 *               SOCKET コマンドコントロールソケット\r
1156 *----------------------------------------------------------------------------*/\r
1157 \r
1158 SOCKET AskCmdCtrlSkt(void)\r
1159 {\r
1160         return(CmdCtrlSocket);\r
1161 }\r
1162 \r
1163 \r
1164 /*----- 転送コントロールソケットを返す ----------------------------------------\r
1165 *\r
1166 *       Parameter\r
1167 *               なし\r
1168 *\r
1169 *       Return Value\r
1170 *               SOCKET 転送コントロールソケット\r
1171 *----------------------------------------------------------------------------*/\r
1172 \r
1173 SOCKET AskTrnCtrlSkt(void)\r
1174 {\r
1175         return(TrnCtrlSocket);\r
1176 }\r
1177 \r
1178 \r
1179 /*----- コマンド/転送コントロールソケットの共有を解除 ------------------------\r
1180 *\r
1181 *       Parameter\r
1182 *               なし\r
1183 *\r
1184 *       Return Value\r
1185 *               なし\r
1186 *----------------------------------------------------------------------------*/\r
1187 \r
1188 void SktShareProh(void)\r
1189 {\r
1190         if(CmdCtrlSocket == TrnCtrlSocket)\r
1191         {\r
1192 \r
1193 //SetTaskMsg("############### SktShareProh");\r
1194 \r
1195                 // 同時接続対応\r
1196 //              CmdCtrlSocket = INVALID_SOCKET;\r
1197 //              ReConnectSkt(&CmdCtrlSocket);\r
1198                 if(CurHost.ReuseCmdSkt == YES)\r
1199                 {\r
1200                         CmdCtrlSocket = INVALID_SOCKET;\r
1201                         ReConnectSkt(&CmdCtrlSocket);\r
1202                 }\r
1203         }\r
1204         return;\r
1205 }\r
1206 \r
1207 \r
1208 /*----- コマンド/転送コントロールソケットの共有が解除されているかチェック ----\r
1209 *\r
1210 *       Parameter\r
1211 *               なし\r
1212 *\r
1213 *       Return Value\r
1214 *               int ステータス\r
1215 *                       YES=共有解除/NO=共有\r
1216 *----------------------------------------------------------------------------*/\r
1217 \r
1218 int AskShareProh(void)\r
1219 {\r
1220         int Sts;\r
1221 \r
1222         Sts = YES;\r
1223         // 同時接続対応\r
1224 //      if(CmdCtrlSocket == TrnCtrlSocket)\r
1225         if(CmdCtrlSocket == TrnCtrlSocket || TrnCtrlSocket == INVALID_SOCKET)\r
1226                 Sts = NO;\r
1227 \r
1228         return(Sts);\r
1229 }\r
1230 \r
1231 \r
1232 /*----- ホストから切断 --------------------------------------------------------\r
1233 *\r
1234 *       Parameter\r
1235 *               なし\r
1236 *\r
1237 *       Return Value\r
1238 *               なし\r
1239 *----------------------------------------------------------------------------*/\r
1240 \r
1241 void DisconnectProc(void)\r
1242 {\r
1243 \r
1244 //SetTaskMsg("############### Disconnect Cmd=%x, Trn=%x", CmdCtrlSocket,TrnCtrlSocket);\r
1245 \r
1246         if((CmdCtrlSocket != INVALID_SOCKET) && (CmdCtrlSocket != TrnCtrlSocket))\r
1247         {\r
1248                 DoQUIT(CmdCtrlSocket);\r
1249                 DoClose(CmdCtrlSocket);\r
1250         }\r
1251 \r
1252         if(TrnCtrlSocket != INVALID_SOCKET)\r
1253         {\r
1254                 DoQUIT(TrnCtrlSocket);\r
1255                 DoClose(TrnCtrlSocket);\r
1256 \r
1257                 SaveCurrentSetToHistory();\r
1258 \r
1259                 EraseRemoteDirForWnd();\r
1260                 SetTaskMsg(MSGJPN004);\r
1261         }\r
1262 \r
1263         TrnCtrlSocket = INVALID_SOCKET;\r
1264         CmdCtrlSocket = INVALID_SOCKET;\r
1265 \r
1266         DispWindowTitle();\r
1267         MakeButtonsFocus();\r
1268         ClearBookMark();\r
1269 \r
1270         return;\r
1271 }\r
1272 \r
1273 \r
1274 /*----- ソケットが強制切断されたときの処理 ------------------------------------\r
1275 *\r
1276 *       Parameter\r
1277 *               なし\r
1278 *\r
1279 *       Return Value\r
1280 *               なし\r
1281 *----------------------------------------------------------------------------*/\r
1282 \r
1283 void DisconnectSet(void)\r
1284 {\r
1285         CmdCtrlSocket = INVALID_SOCKET;\r
1286         TrnCtrlSocket = INVALID_SOCKET;\r
1287 \r
1288         EraseRemoteDirForWnd();\r
1289         DispWindowTitle();\r
1290         MakeButtonsFocus();\r
1291         SetTaskMsg(MSGJPN005);\r
1292         return;\r
1293 }\r
1294 \r
1295 \r
1296 /*----- ホストに接続中かどうかを返す ------------------------------------------\r
1297 *\r
1298 *       Parameter\r
1299 *               なし\r
1300 *\r
1301 *       Return Value\r
1302 *               int ステータス (YES/NO)\r
1303 *----------------------------------------------------------------------------*/\r
1304 \r
1305 int AskConnecting(void)\r
1306 {\r
1307         int Sts;\r
1308 \r
1309         Sts = NO;\r
1310         if(TrnCtrlSocket != INVALID_SOCKET)\r
1311                 Sts = YES;\r
1312 \r
1313         return(Sts);\r
1314 }\r
1315 \r
1316 \r
1317 /*----- ホストへ接続する ------------------------------------------------------\r
1318 *\r
1319 *       Parameter\r
1320 *               char *Host : ホスト名\r
1321 *               char *User : ユーザ名\r
1322 *               char *Pass : パスワード\r
1323 *               char *Acct : アカウント\r
1324 *               int Port : ポート\r
1325 *               int Fwall : FireWallを使うかどうか (YES/NO)\r
1326 *               int SavePass : パスワードを再入力した時に保存するかどうか (YES/NO)\r
1327 *               int Security : セキュリティ (SECURITY_xxx, MDx)\r
1328 *\r
1329 *       Return Value\r
1330 *               SOCKET ソケット\r
1331 *\r
1332 *       Note\r
1333 *               ホスト名、ユーザ名、パスワードが指定されていなかったときは、接続に使用\r
1334 *               したものをコピーしてかえす\r
1335 *                       char *Host : ホスト名\r
1336 *                       char *User : ユーザ名\r
1337 *                       char *Pass : パスワード\r
1338 *                       char *Acct : アカウント\r
1339 *\r
1340 *               FireWallは次のように動作する\r
1341 *                       TYPE1   Connect fire → USER user(f) → PASS pass(f) → SITE host → USER user(h) →      PASS pass(h) → ACCT acct\r
1342 *                       TYPE2   Connect fire → USER user(f) → PASS pass(f) →              USER user(h)@host → PASS pass(h) → ACCT acct\r
1343 *                       TYPE3   Connect fire →                                              USER user(h)@host → PASS pass(h) → ACCT acct\r
1344 *                       TYPE4   Connect fire →                                 OPEN host → USER user(h) →      PASS pass(h) → ACCT acct\r
1345 *                       TYPE5   SOCKS4\r
1346 *                       none    Connect host →                                              USER user(h) →      PASS pass(h) → ACCT acct\r
1347 *----------------------------------------------------------------------------*/\r
1348 \r
1349 // 暗号化通信対応\r
1350 static SOCKET DoConnectCrypt(int CryptMode, HOSTDATA* HostData, char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security, int *CancelCheckWork)\r
1351 {\r
1352         int Sts;\r
1353         int Flg;\r
1354         int Anony;\r
1355         SOCKET ContSock;\r
1356         char Buf[1024];\r
1357         char Reply[1024];\r
1358         int Continue;\r
1359         int ReInPass;\r
1360         char *Tmp;\r
1361         int HostPort;\r
1362         static const char *SiteTbl[4] = { "SITE", "site", "OPEN", "open" };\r
1363         char TmpBuf[ONELINE_BUF_SIZE];\r
1364         struct linger LingerOpt;\r
1365 \r
1366         // 暗号化通信対応\r
1367         ContSock = INVALID_SOCKET;\r
1368 \r
1369         if(CryptMode == CRYPT_NONE || CryptMode == CRYPT_FTPES || CryptMode == CRYPT_FTPIS)\r
1370         {\r
1371                 if(Fwall == YES)\r
1372                         Fwall = FwallType;\r
1373                 else\r
1374                         Fwall = FWALL_NONE;\r
1375 \r
1376                 TryConnect = YES;\r
1377                 // 暗号化通信対応\r
1378 //              CancelFlg = NO;\r
1379 #if 0\r
1380 //              WSASetBlockingHook(BlkHookFnc);\r
1381 #endif\r
1382 \r
1383                 ContSock = INVALID_SOCKET;\r
1384 \r
1385                 HostPort = Port;\r
1386                 Tmp = Host;\r
1387                 if(((Fwall >= FWALL_FU_FP_SITE) && (Fwall <= FWALL_OPEN)) ||\r
1388                    (Fwall == FWALL_SIDEWINDER) ||\r
1389                    (Fwall == FWALL_FU_FP))\r
1390                 {\r
1391                         Tmp = FwallHost;\r
1392                         Port = FwallPort;\r
1393                 }\r
1394 \r
1395                 if(strlen(Tmp) != 0)\r
1396                 {\r
1397                         // 同時接続対応\r
1398 //                      if((ContSock = connectsock(Tmp, Port, "", &CancelFlg)) != INVALID_SOCKET)\r
1399                         if((ContSock = connectsock(Tmp, Port, "", CancelCheckWork)) != INVALID_SOCKET)\r
1400                         {\r
1401                                 // バッファを無効\r
1402 #ifdef DISABLE_CONTROL_NETWORK_BUFFERS\r
1403                                 int BufferSize = 0;\r
1404                                 setsockopt(ContSock, SOL_SOCKET, SO_SNDBUF, (char*)&BufferSize, sizeof(int));\r
1405                                 setsockopt(ContSock, SOL_SOCKET, SO_RCVBUF, (char*)&BufferSize, sizeof(int));\r
1406 #endif\r
1407                                 // FTPIS対応\r
1408 //                              while((Sts = ReadReplyMessage(ContSock, Buf, 1024, &CancelFlg, TmpBuf) / 100) == FTP_PRELIM)\r
1409 //                                      ;\r
1410                                 if(CryptMode == CRYPT_FTPIS)\r
1411                                 {\r
1412                                         if(AttachSSL(ContSock, INVALID_SOCKET, CancelCheckWork))\r
1413                                         {\r
1414                                                 while((Sts = ReadReplyMessage(ContSock, Buf, 1024, CancelCheckWork, TmpBuf) / 100) == FTP_PRELIM)\r
1415                                                         ;\r
1416                                         }\r
1417                                         else\r
1418                                                 Sts = FTP_ERROR;\r
1419                                 }\r
1420                                 else\r
1421                                 {\r
1422                                         while((Sts = ReadReplyMessage(ContSock, Buf, 1024, CancelCheckWork, TmpBuf) / 100) == FTP_PRELIM)\r
1423                                                 ;\r
1424                                 }\r
1425 \r
1426                                 if(Sts == FTP_COMPLETE)\r
1427                                 {\r
1428                                         Flg = 1;\r
1429                                         if(setsockopt(ContSock, SOL_SOCKET, SO_OOBINLINE, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
1430                                                 ReportWSError("setsockopt", WSAGetLastError());\r
1431                                         // データ転送用ソケットのTCP遅延転送が無効されているので念のため\r
1432                                         if(setsockopt(ContSock, IPPROTO_TCP, TCP_NODELAY, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
1433                                                 ReportWSError("setsockopt", WSAGetLastError());\r
1434 //#pragma aaa\r
1435                                         Flg = 1;\r
1436                                         if(setsockopt(ContSock, SOL_SOCKET, SO_KEEPALIVE, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
1437                                                 ReportWSError("setsockopt", WSAGetLastError());\r
1438                                         LingerOpt.l_onoff = 1;\r
1439                                         LingerOpt.l_linger = 90;\r
1440                                         if(setsockopt(ContSock, SOL_SOCKET, SO_LINGER, (LPSTR)&LingerOpt, sizeof(LingerOpt)) == SOCKET_ERROR)\r
1441                                                 ReportWSError("setsockopt", WSAGetLastError());\r
1442 ///////\r
1443 \r
1444 \r
1445                                         /*===== 認証を行なう =====*/\r
1446 \r
1447                                         Sts = FTP_COMPLETE;\r
1448                                         if((Fwall == FWALL_FU_FP_SITE) ||\r
1449                                            (Fwall == FWALL_FU_FP_USER) ||\r
1450                                            (Fwall == FWALL_FU_FP))\r
1451                                         {\r
1452                                                 // 同時接続対応\r
1453 //                                              if((Sts = command(ContSock, Reply, &CancelFlg, "USER %s", FwallUser) / 100) == FTP_CONTINUE)\r
1454                                                 if((Sts = command(ContSock, Reply, CancelCheckWork, "USER %s", FwallUser) / 100) == FTP_CONTINUE)\r
1455                                                 {\r
1456                                                         CheckOneTimePassword(FwallPass, Reply, FwallSecurity);\r
1457                                                         // 同時接続対応\r
1458 //                                                      Sts = command(ContSock, NULL, &CancelFlg, "PASS %s", Reply) / 100;\r
1459                                                         Sts = command(ContSock, NULL, CancelCheckWork, "PASS %s", Reply) / 100;\r
1460                                                 }\r
1461                                         }\r
1462                                         else if(Fwall == FWALL_SIDEWINDER)\r
1463                                         {\r
1464                                                 // 同時接続対応\r
1465 //                                              Sts = command(ContSock, Reply, &CancelFlg, "USER %s:%s%c%s", FwallUser, FwallPass, FwallDelimiter, Host) / 100;\r
1466                                                 Sts = command(ContSock, Reply, CancelCheckWork, "USER %s:%s%c%s", FwallUser, FwallPass, FwallDelimiter, Host) / 100;\r
1467                                         }\r
1468                                         if((Sts != FTP_COMPLETE) && (Sts != FTP_CONTINUE))\r
1469                                         {\r
1470                                                 SetTaskMsg(MSGJPN006);\r
1471                                                 DoClose(ContSock);\r
1472                                                 ContSock = INVALID_SOCKET;\r
1473                                         }\r
1474                                         else\r
1475                                         {\r
1476                                                 if((Fwall == FWALL_FU_FP_SITE) || (Fwall == FWALL_OPEN))\r
1477                                                 {\r
1478                                                         Flg = 0;\r
1479                                                         if(Fwall == FWALL_OPEN)\r
1480                                                                 Flg = 2;\r
1481                                                         if(FwallLower == YES)\r
1482                                                                 Flg++;\r
1483 \r
1484                                                         if(HostPort == PORT_NOR)\r
1485                                                                 // 同時接続対応\r
1486 //                                                              Sts = command(ContSock, NULL, &CancelFlg, "%s %s", SiteTbl[Flg], Host) / 100;\r
1487                                                                 Sts = command(ContSock, NULL, CancelCheckWork, "%s %s", SiteTbl[Flg], Host) / 100;\r
1488                                                         else\r
1489                                                                 // 同時接続対応\r
1490 //                                                              Sts = command(ContSock, NULL, &CancelFlg, "%s %s %d", SiteTbl[Flg], Host, HostPort) / 100;\r
1491                                                                 Sts = command(ContSock, NULL, CancelCheckWork, "%s %s %d", SiteTbl[Flg], Host, HostPort) / 100;\r
1492                                                 }\r
1493 \r
1494                                                 if((Sts != FTP_COMPLETE) && (Sts != FTP_CONTINUE))\r
1495                                                 {\r
1496                                                         SetTaskMsg(MSGJPN007, Host);\r
1497                                                         DoClose(ContSock);\r
1498                                                         ContSock = INVALID_SOCKET;\r
1499                                                 }\r
1500                                                 else\r
1501                                                 {\r
1502                                                         Anony = NO;\r
1503                                                         if((strlen(User) != 0) || \r
1504                                                            (InputDialogBox(username_dlg, GetMainHwnd(), NULL, User, USER_NAME_LEN+1, &Anony, IDH_HELP_TOPIC_0000001) == YES))\r
1505                                                         {\r
1506                                                                 if(Anony == YES)\r
1507                                                                 {\r
1508                                                                         strcpy(User, "anonymous");\r
1509                                                                         strcpy(Pass, UserMailAdrs);\r
1510                                                                 }\r
1511 \r
1512                                                                 if((Fwall == FWALL_FU_FP_USER) || (Fwall == FWALL_USER))\r
1513                                                                 {\r
1514                                                                         if(HostPort == PORT_NOR)\r
1515                                                                                 sprintf(Buf, "%s%c%s", User, FwallDelimiter, Host);\r
1516                                                                         else\r
1517                                                                                 sprintf(Buf, "%s%c%s %d", User, FwallDelimiter, Host, HostPort);\r
1518                                                                 }\r
1519                                                                 else\r
1520                                                                         strcpy(Buf, User);\r
1521 \r
1522                                                                 // FTPES対応\r
1523                                                                 if(CryptMode == CRYPT_FTPES)\r
1524                                                                 {\r
1525                                                                         if(IsOpenSSLLoaded() && (Sts = command(ContSock, Reply, CancelCheckWork, "AUTH TLS")) == 234)\r
1526                                                                         {\r
1527                                                                                 if(AttachSSL(ContSock, INVALID_SOCKET, CancelCheckWork))\r
1528                                                                                 {\r
1529                                                                                         if((Sts = command(ContSock, Reply, CancelCheckWork, "PBSZ 0")) == 200)\r
1530                                                                                         {\r
1531                                                                                                 if((Sts = command(ContSock, Reply, CancelCheckWork, "PROT P")) == 200)\r
1532                                                                                                 {\r
1533                                                                                                 }\r
1534                                                                                                 else\r
1535                                                                                                         Sts = FTP_ERROR;\r
1536                                                                                         }\r
1537                                                                                         else\r
1538                                                                                                 Sts = FTP_ERROR;\r
1539                                                                                 }\r
1540                                                                                 else\r
1541                                                                                         Sts = FTP_ERROR;\r
1542                                                                         }\r
1543                                                                         else\r
1544                                                                                 Sts = FTP_ERROR;\r
1545                                                                 }\r
1546 \r
1547                                                                 // FTPIS対応\r
1548                                                                 // "PBSZ 0"と"PROT P"は黙示的に設定されているはずだが念のため\r
1549                                                                 if(CryptMode == CRYPT_FTPIS)\r
1550                                                                 {\r
1551                                                                         if((Sts = command(ContSock, Reply, CancelCheckWork, "PBSZ 0")) == 200)\r
1552                                                                         {\r
1553                                                                                 if((Sts = command(ContSock, Reply, CancelCheckWork, "PROT P")) == 200)\r
1554                                                                                 {\r
1555                                                                                 }\r
1556                                                                         }\r
1557                                                                 }\r
1558 \r
1559                                                                 ReInPass = NO;\r
1560                                                                 do\r
1561                                                                 {\r
1562                                                                         // FTPES対応\r
1563                                                                         if(Sts == FTP_ERROR)\r
1564                                                                                 break;\r
1565                                                                         Continue = NO;\r
1566                                                                         // 同時接続対応\r
1567 //                                                                      if((Sts = command(ContSock, Reply, &CancelFlg, "USER %s", Buf) / 100) == FTP_CONTINUE)\r
1568                                                                         if((Sts = command(ContSock, Reply, CancelCheckWork, "USER %s", Buf) / 100) == FTP_CONTINUE)\r
1569                                                                         {\r
1570                                                                                 if((strlen(Pass) != 0) || \r
1571                                                                                    (InputDialogBox(passwd_dlg, GetMainHwnd(), NULL, Pass, PASSWORD_LEN+1, &Anony, IDH_HELP_TOPIC_0000001) == YES))\r
1572                                                                                 {\r
1573                                                                                         CheckOneTimePassword(Pass, Reply, Security);\r
1574 \r
1575                                                                                         /* パスワードがスペース1個の時はパスワードの実体なしとする */\r
1576                                                                                         if(strcmp(Reply, " ") == 0)\r
1577                                                                                                 strcpy(Reply, "");\r
1578 \r
1579                                                                                         // 同時接続対応\r
1580 //                                                                                      Sts = command(ContSock, NULL, &CancelFlg, "PASS %s", Reply) / 100;\r
1581                                                                                         Sts = command(ContSock, NULL, CancelCheckWork, "PASS %s", Reply) / 100;\r
1582                                                                                         if(Sts == FTP_ERROR)\r
1583                                                                                         {\r
1584                                                                                                 strcpy(Pass, "");\r
1585                                                                                                 if(InputDialogBox(re_passwd_dlg, GetMainHwnd(), NULL, Pass, PASSWORD_LEN+1, &Anony, IDH_HELP_TOPIC_0000001) == YES)\r
1586                                                                                                         Continue = YES;\r
1587                                                                                                 else\r
1588                                                                                                         DoPrintf("No password specified.");\r
1589                                                                                                 ReInPass = YES;\r
1590                                                                                         }\r
1591                                                                                         else if(Sts == FTP_CONTINUE)\r
1592                                                                                         {\r
1593                                                                                                 if((strlen(Acct) != 0) || \r
1594                                                                                                    (InputDialogBox(account_dlg, GetMainHwnd(), NULL, Acct, ACCOUNT_LEN+1, &Anony, IDH_HELP_TOPIC_0000001) == YES))\r
1595                                                                                                 {\r
1596                                                                                                         // 同時接続対応\r
1597 //                                                                                                      Sts = command(ContSock, NULL, &CancelFlg, "ACCT %s", Acct) / 100;\r
1598                                                                                                         Sts = command(ContSock, NULL, CancelCheckWork, "ACCT %s", Acct) / 100;\r
1599                                                                                                 }\r
1600                                                                                                 else\r
1601                                                                                                         DoPrintf("No account specified");\r
1602                                                                                         }\r
1603                                                                                 }\r
1604                                                                                 else\r
1605                                                                                 {\r
1606                                                                                         Sts = FTP_ERROR;\r
1607                                                                                         DoPrintf("No password specified.");\r
1608                                                                                 }\r
1609                                                                         }\r
1610                                                                 }\r
1611                                                                 while(Continue == YES);\r
1612                                                         }\r
1613                                                         else\r
1614                                                         {\r
1615                                                                 Sts = FTP_ERROR;\r
1616                                                                 DoPrintf("No user name specified");\r
1617                                                         }\r
1618 \r
1619                                                         if(Sts != FTP_COMPLETE)\r
1620                                                         {\r
1621                                                                 SetTaskMsg(MSGJPN008, Host);\r
1622                                                                 DoClose(ContSock);\r
1623                                                                 ContSock = INVALID_SOCKET;\r
1624                                                         }\r
1625                                                         else if((SavePass == YES) && (ReInPass == YES))\r
1626                                                         {\r
1627                                                                 if(DialogBox(GetFtpInst(), MAKEINTRESOURCE(savepass_dlg), GetMainHwnd(), ExeEscDialogProc) == YES)\r
1628                                                                         SetHostPassword(AskCurrentHost(), Pass);\r
1629                                                         }\r
1630                                                 }\r
1631                                         }\r
1632                                 }\r
1633                                 else\r
1634                                 {\r
1635 //#pragma aaa\r
1636                                         SetTaskMsg(MSGJPN009/*"接続できません(1) %x", ContSock*/);\r
1637                                         DoClose(ContSock);\r
1638                                         ContSock = INVALID_SOCKET;\r
1639                                 }\r
1640                         }\r
1641                 }\r
1642                 else\r
1643                 {\r
1644 \r
1645                         if(((Fwall >= FWALL_FU_FP_SITE) && (Fwall <= FWALL_OPEN)) ||\r
1646                            (Fwall == FWALL_FU_FP))\r
1647                                 SetTaskMsg(MSGJPN010);\r
1648                         else\r
1649                                 SetTaskMsg(MSGJPN011);\r
1650                 }\r
1651 \r
1652 #if 0\r
1653 //              WSAUnhookBlockingHook();\r
1654 #endif\r
1655                 TryConnect = NO;\r
1656 \r
1657                 // FEAT対応\r
1658                 // ホストの機能を確認\r
1659                 if(ContSock != INVALID_SOCKET)\r
1660                 {\r
1661                         if((Sts = command(ContSock, Reply, CancelCheckWork, "FEAT")) == 211)\r
1662                         {\r
1663                                 // 改行文字はReadReplyMessageで消去されるため区切り文字に空白を使用\r
1664                                 // UTF-8対応\r
1665                                 if(strstr(Reply, " UTF8 "))\r
1666                                         HostData->Feature |= FEATURE_UTF8;\r
1667                                 // MLST対応\r
1668                                 if(strstr(Reply, " MLST ") || strstr(Reply, " MLSD "))\r
1669                                         HostData->Feature |= FEATURE_MLSD;\r
1670                                 // IPv6対応\r
1671                                 if(strstr(Reply, " EPRT ") || strstr(Reply, " EPSV "))\r
1672                                         HostData->Feature |= FEATURE_EPRT | FEATURE_EPSV;\r
1673                         }\r
1674                         // UTF-8対応\r
1675                         if(HostData->CurNameKanjiCode == KANJI_AUTO && (HostData->Feature & FEATURE_UTF8))\r
1676                         {\r
1677                                 if((Sts = command(ContSock, Reply, CancelCheckWork, "OPTS UTF8 ON")) == 200)\r
1678                                         HostData->CurNameKanjiCode = KANJI_UTF8N;\r
1679                         }\r
1680                 }\r
1681         }\r
1682         else if(CryptMode == CRYPT_SFTP)\r
1683         {\r
1684         }\r
1685 \r
1686         return(ContSock);\r
1687 }\r
1688 \r
1689 // 同時接続対応\r
1690 //static SOCKET DoConnect(HOSTDATA* HostData, char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security)\r
1691 static SOCKET DoConnect(HOSTDATA* HostData, char *Host, char *User, char *Pass, char *Acct, int Port, int Fwall, int SavePass, int Security, int *CancelCheckWork)\r
1692 {\r
1693         SOCKET ContSock;\r
1694         ContSock = INVALID_SOCKET;\r
1695         *CancelCheckWork = NO;\r
1696 //      if(*CancelCheckWork == NO && ContSock == INVALID_SOCKET && HostData->UseSFTP == YES)\r
1697 //      {\r
1698 //              SetTaskMsg(MSGJPN317);\r
1699 //              if((ContSock = DoConnectCrypt(CRYPT_SFTP, HostData, Host, User, Pass, Acct, Port, Fwall, SavePass, Security, CancelCheckWork)) != INVALID_SOCKET)\r
1700 //                      HostData->CryptMode = CRYPT_SFTP;\r
1701 //      }\r
1702         if(*CancelCheckWork == NO && ContSock == INVALID_SOCKET && HostData->UseFTPIS == YES)\r
1703         {\r
1704                 SetTaskMsg(MSGJPN316);\r
1705                 if((ContSock = DoConnectCrypt(CRYPT_FTPIS, HostData, Host, User, Pass, Acct, Port, Fwall, SavePass, Security, CancelCheckWork)) != INVALID_SOCKET)\r
1706                         HostData->CryptMode = CRYPT_FTPIS;\r
1707         }\r
1708         if(*CancelCheckWork == NO && ContSock == INVALID_SOCKET && HostData->UseFTPES == YES)\r
1709         {\r
1710                 SetTaskMsg(MSGJPN315);\r
1711                 if((ContSock = DoConnectCrypt(CRYPT_FTPES, HostData, Host, User, Pass, Acct, Port, Fwall, SavePass, Security, CancelCheckWork)) != INVALID_SOCKET)\r
1712                         HostData->CryptMode = CRYPT_FTPES;\r
1713         }\r
1714         if(*CancelCheckWork == NO && ContSock == INVALID_SOCKET && HostData->UseNoEncryption == YES)\r
1715         {\r
1716                 SetTaskMsg(MSGJPN314);\r
1717                 if((ContSock = DoConnectCrypt(CRYPT_NONE, HostData, Host, User, Pass, Acct, Port, Fwall, SavePass, Security, CancelCheckWork)) != INVALID_SOCKET)\r
1718                         HostData->CryptMode = CRYPT_NONE;\r
1719         }\r
1720         return ContSock;\r
1721 }\r
1722 \r
1723 \r
1724 /*----- ワンタイムパスワードのチェック ----------------------------------------\r
1725 *\r
1726 *       Parameter\r
1727 *               chat *Pass : パスワード/パスフレーズ\r
1728 *               char *Reply : USERコマンドを送ったあとのリプライ文字列\r
1729 *                                               /PASSコマンドで送るパスワードを返すバッファ\r
1730 *               int Type : タイプ (SECURITY_xxx, MDx)\r
1731 *\r
1732 *       Return Value\r
1733 *               int ステータス\r
1734 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1735 *\r
1736 *       Note\r
1737 *               ワンタイムパスワードでない時はPassをそのままReplyにコピー\r
1738 *----------------------------------------------------------------------------*/\r
1739 \r
1740 static int CheckOneTimePassword(char *Pass, char *Reply, int Type)\r
1741 {\r
1742         int Sts;\r
1743         char *Pos;\r
1744         int Seq;\r
1745         char Seed[MAX_SEED_LEN+1];\r
1746         int i;\r
1747 \r
1748         Sts = FFFTP_SUCCESS;\r
1749         Pos = NULL;\r
1750 \r
1751         if(Type == SECURITY_AUTO)\r
1752         {\r
1753                 if((Pos = stristr(Reply, "otp-md5")) != NULL)\r
1754                 {\r
1755                         Type = MD5;\r
1756                         SetTaskMsg(MSGJPN012);\r
1757                 }\r
1758                 else if((Pos = stristr(Reply, "otp-sha1")) != NULL)\r
1759                 {\r
1760                         Type = SHA1;\r
1761                         SetTaskMsg(MSGJPN013);\r
1762                 }\r
1763                 else if(((Pos = stristr(Reply, "otp-md4")) != NULL) || ((Pos = stristr(Reply, "s/key")) != NULL))\r
1764                 {\r
1765                         Type = MD4;\r
1766                         SetTaskMsg(MSGJPN014);\r
1767                 }\r
1768         }\r
1769         else\r
1770                 Pos = GetNextField(Reply);\r
1771 \r
1772         if((Type == MD4) || (Type == MD5) || (Type == SHA1))\r
1773         {\r
1774                 /* シーケンス番号を見つけるループ */\r
1775                 DoPrintf("Analize OTP");\r
1776                 DoPrintf("%s", Pos);\r
1777                 Sts = FFFTP_FAIL;\r
1778                 while((Pos = GetNextField(Pos)) != NULL)\r
1779                 {\r
1780                         if(IsDigit(*Pos))\r
1781                         {\r
1782                                 Seq = atoi(Pos);\r
1783                                 DoPrintf("Sequence=%d", Seq);\r
1784 \r
1785                                 /* Seed */\r
1786                                 if((Pos = GetNextField(Pos)) != NULL)\r
1787                                 {\r
1788                                         if(GetOneField(Pos, Seed, MAX_SEED_LEN) == FFFTP_SUCCESS)\r
1789                                         {\r
1790                                                 /* Seedは英数字のみ有効とする */\r
1791                                                 for(i = strlen(Seed)-1; i >= 0; i--)\r
1792                                                 {\r
1793                                                         if((IsAlpha(Seed[i]) == 0) && (IsDigit(Seed[i]) == 0))\r
1794                                                                 Seed[i] = NUL;\r
1795                                                 }\r
1796                                                 if(strlen(Seed) > 0)\r
1797                                                 {\r
1798                                                         DoPrintf("Seed=%s", Seed);\r
1799                                                         Make6WordPass(Seq, Seed, Pass, Type, Reply);\r
1800                                                         DoPrintf("Response=%s", Reply);\r
1801 \r
1802                                                         /* シーケンス番号のチェックと警告 */\r
1803                                                         if(Seq <= 10)\r
1804                                                                 DialogBox(GetFtpInst(), MAKEINTRESOURCE(otp_notify_dlg), GetMainHwnd(), ExeEscDialogProc);\r
1805 \r
1806                                                         Sts = FFFTP_SUCCESS;\r
1807                                                 }\r
1808                                         }\r
1809                                 }\r
1810                                 break;\r
1811                         }\r
1812                 }\r
1813 \r
1814                 if(Sts == FFFTP_FAIL)\r
1815                         SetTaskMsg(MSGJPN015);\r
1816         }\r
1817         else\r
1818         {\r
1819                 strcpy(Reply, Pass);\r
1820                 DoPrintf("No OTP used.");\r
1821         }\r
1822         return(Sts);\r
1823 }\r
1824 \r
1825 \r
1826 \r
1827 \r
1828 \r
1829 \r
1830 \r
1831 \r
1832 \r
1833 \r
1834 \r
1835 \r
1836 \r
1837 \r
1838 /*----- ソケットを接続する ----------------------------------------------------\r
1839 *\r
1840 *       Parameter\r
1841 *               char *host : ホスト名\r
1842 *               int port : ポート番号\r
1843 *               char *PreMsg : メッセージの前半部分\r
1844 *\r
1845 *       Return Value\r
1846 *               SOCKET ソケット\r
1847 *----------------------------------------------------------------------------*/\r
1848 \r
1849 // IPv6対応\r
1850 SOCKET connectsock(char *host, int port, char *PreMsg, int *CancelCheckWork)\r
1851 {\r
1852         SOCKET Result;\r
1853         Result = INVALID_SOCKET;\r
1854         switch(CurHost.CurNetType)\r
1855         {\r
1856         case NTYPE_AUTO:\r
1857                 if((Result = connectsockIPv4(host, port, PreMsg, CancelCheckWork)) != INVALID_SOCKET)\r
1858                         CurHost.CurNetType = NTYPE_IPV4;\r
1859                 else if((Result = connectsockIPv6(host, port, PreMsg, CancelCheckWork)) != INVALID_SOCKET)\r
1860                         CurHost.CurNetType = NTYPE_IPV6;\r
1861                 break;\r
1862         case NTYPE_IPV4:\r
1863                 Result = connectsockIPv4(host, port, PreMsg, CancelCheckWork);\r
1864                 CurHost.CurNetType = NTYPE_IPV4;\r
1865                 break;\r
1866         case NTYPE_IPV6:\r
1867                 Result = connectsockIPv6(host, port, PreMsg, CancelCheckWork);\r
1868                 CurHost.CurNetType = NTYPE_IPV6;\r
1869                 break;\r
1870         }\r
1871         return Result;\r
1872 }\r
1873 \r
1874 \r
1875 // IPv6対応\r
1876 //SOCKET connectsock(char *host, int port, char *PreMsg, int *CancelCheckWork)\r
1877 SOCKET connectsockIPv4(char *host, int port, char *PreMsg, int *CancelCheckWork)\r
1878 {\r
1879         struct sockaddr_in saSockAddr;\r
1880         char HostEntry[MAXGETHOSTSTRUCT];\r
1881         struct hostent *pHostEntry;\r
1882         SOCKET sSocket;\r
1883         int Len;\r
1884         int Fwall;\r
1885         SOCKS4CMD Socks4Cmd;\r
1886         SOCKS4REPLY Socks4Reply;\r
1887         SOCKS5REQUEST Socks5Cmd;\r
1888         SOCKS5REPLY Socks5Reply;\r
1889 \r
1890         //////////////////////////////\r
1891         // ホスト名解決と接続の準備\r
1892         //////////////////////////////\r
1893 \r
1894         Fwall = FWALL_NONE;\r
1895         if(AskHostFireWall() == YES)\r
1896                 Fwall = FwallType;\r
1897 \r
1898         sSocket = INVALID_SOCKET;\r
1899 \r
1900         UseIPadrs = YES;\r
1901         strcpy(DomainName, host);\r
1902         // IPv6対応\r
1903 //      memset(&CurSockAddr, 0, sizeof(CurSockAddr));\r
1904 //      CurSockAddr.sin_port = htons((u_short)port);\r
1905 //      CurSockAddr.sin_family = AF_INET;\r
1906 //      if((CurSockAddr.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)\r
1907         memset(&CurSockAddrIPv4, 0, sizeof(CurSockAddrIPv4));\r
1908         CurSockAddrIPv4.sin_port = htons((u_short)port);\r
1909         CurSockAddrIPv4.sin_family = AF_INET;\r
1910         if((CurSockAddrIPv4.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)\r
1911         {\r
1912                 // ホスト名が指定された\r
1913                 // ホスト名からアドレスを求める\r
1914                 if(((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER)) &&\r
1915                    (FwallResolv == YES))\r
1916                 {\r
1917                         // ホスト名解決はSOCKSサーバに任せる\r
1918                         pHostEntry = NULL;\r
1919                 }\r
1920                 else\r
1921                 {\r
1922                         // アドレスを取得\r
1923                         SetTaskMsg(MSGJPN016, DomainName);\r
1924                         // IPv6対応\r
1925 //                      pHostEntry = do_gethostbyname(host, HostEntry, MAXGETHOSTSTRUCT, CancelCheckWork);\r
1926                         pHostEntry = do_gethostbynameIPv4(host, HostEntry, MAXGETHOSTSTRUCT, CancelCheckWork);\r
1927                 }\r
1928 \r
1929                 if(pHostEntry != NULL)\r
1930                 {\r
1931                         // IPv6対応\r
1932 //                      memcpy((char *)&CurSockAddr.sin_addr, pHostEntry->h_addr, pHostEntry->h_length);\r
1933 //                      SetTaskMsg(MSGJPN017, PreMsg, DomainName, inet_ntoa(CurSockAddr.sin_addr), ntohs(CurSockAddr.sin_port));\r
1934                         memcpy((char *)&CurSockAddrIPv4.sin_addr, pHostEntry->h_addr, pHostEntry->h_length);\r
1935                         SetTaskMsg(MSGJPN017, PreMsg, DomainName, inet_ntoa(CurSockAddrIPv4.sin_addr), ntohs(CurSockAddrIPv4.sin_port));\r
1936                 }\r
1937                 else\r
1938                 {\r
1939                         if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
1940                         {\r
1941                                 UseIPadrs = NO;\r
1942                                 // IPv6対応\r
1943 //                              SetTaskMsg(MSGJPN018, PreMsg, DomainName, ntohs(CurSockAddr.sin_port));\r
1944                                 SetTaskMsg(MSGJPN018, PreMsg, DomainName, ntohs(CurSockAddrIPv4.sin_port));\r
1945                         }\r
1946                         else\r
1947                         {\r
1948                                 SetTaskMsg(MSGJPN019, host);\r
1949                                 return(INVALID_SOCKET);\r
1950                         }\r
1951                 }\r
1952         }\r
1953         else\r
1954                 // IPv6対応\r
1955 //              SetTaskMsg(MSGJPN020, PreMsg, inet_ntoa(CurSockAddr.sin_addr), ntohs(CurSockAddr.sin_port));\r
1956                 SetTaskMsg(MSGJPN020, PreMsg, inet_ntoa(CurSockAddrIPv4.sin_addr), ntohs(CurSockAddrIPv4.sin_port));\r
1957 \r
1958         if((Fwall == FWALL_SOCKS4) || (Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
1959         {\r
1960                 // SOCKSを使う\r
1961                 // SOCKSに接続する準備\r
1962                 if(Fwall == FWALL_SOCKS4)\r
1963                 {\r
1964                         Socks4Cmd.Ver = SOCKS4_VER;\r
1965                         Socks4Cmd.Cmd = SOCKS4_CMD_CONNECT;\r
1966                         // IPv6対応\r
1967 //                      Socks4Cmd.Port = CurSockAddr.sin_port;\r
1968 //                      Socks4Cmd.AdrsInt = CurSockAddr.sin_addr.s_addr;\r
1969                         Socks4Cmd.Port = CurSockAddrIPv4.sin_port;\r
1970                         Socks4Cmd.AdrsInt = CurSockAddrIPv4.sin_addr.s_addr;\r
1971                         strcpy(Socks4Cmd.UserID, FwallUser);\r
1972                         Len = offsetof(SOCKS4CMD, UserID) + strlen(FwallUser) + 1;\r
1973                 }\r
1974                 else\r
1975                 {\r
1976                         // IPv6対応\r
1977 //                      Len = Socks5MakeCmdPacket(&Socks5Cmd, SOCKS5_CMD_CONNECT, UseIPadrs, CurSockAddr.sin_addr.s_addr, DomainName, CurSockAddr.sin_port);\r
1978                         Len = Socks5MakeCmdPacket(&Socks5Cmd, SOCKS5_CMD_CONNECT, UseIPadrs, CurSockAddrIPv4.sin_addr.s_addr, DomainName, CurSockAddrIPv4.sin_port);\r
1979                 }\r
1980 \r
1981                 // IPv6対応\r
1982 //              memset(&SocksSockAddr, 0, sizeof(SocksSockAddr));\r
1983 //              if((SocksSockAddr.sin_addr.s_addr = inet_addr(FwallHost)) == INADDR_NONE)\r
1984                 memset(&SocksSockAddrIPv4, 0, sizeof(SocksSockAddrIPv4));\r
1985                 if((SocksSockAddrIPv4.sin_addr.s_addr = inet_addr(FwallHost)) == INADDR_NONE)\r
1986                 {\r
1987                         // IPv6対応\r
1988 //                      if((pHostEntry = do_gethostbyname(FwallHost, HostEntry, MAXGETHOSTSTRUCT, CancelCheckWork)) != NULL)\r
1989 //                              memcpy((char *)&SocksSockAddr.sin_addr, pHostEntry->h_addr, pHostEntry->h_length);\r
1990                         if((pHostEntry = do_gethostbynameIPv4(FwallHost, HostEntry, MAXGETHOSTSTRUCT, CancelCheckWork)) != NULL)\r
1991                                 memcpy((char *)&SocksSockAddrIPv4.sin_addr, pHostEntry->h_addr, pHostEntry->h_length);\r
1992                         else\r
1993                         {\r
1994                                 SetTaskMsg(MSGJPN021, FwallHost);\r
1995                                 return INVALID_SOCKET;\r
1996                         }\r
1997                 }\r
1998                 // IPv6対応\r
1999 //              SocksSockAddr.sin_port = htons((u_short)FwallPort);\r
2000 //              SocksSockAddr.sin_family = AF_INET;\r
2001 //              SetTaskMsg(MSGJPN022, inet_ntoa(SocksSockAddr.sin_addr), ntohs(SocksSockAddr.sin_port));\r
2002                 SocksSockAddrIPv4.sin_port = htons((u_short)FwallPort);\r
2003                 SocksSockAddrIPv4.sin_family = AF_INET;\r
2004                 SetTaskMsg(MSGJPN022, inet_ntoa(SocksSockAddrIPv4.sin_addr), ntohs(SocksSockAddrIPv4.sin_port));\r
2005                 // connectで接続する先はSOCKSサーバ\r
2006                 // IPv6対応\r
2007 //              memcpy(&saSockAddr, &SocksSockAddr, sizeof(SocksSockAddr));\r
2008                 memcpy(&saSockAddr, &SocksSockAddrIPv4, sizeof(SocksSockAddrIPv4));\r
2009         }\r
2010         else\r
2011         {\r
2012                 // connectで接続するのは接続先のホスト\r
2013                 // IPv6対応\r
2014 //              memcpy(&saSockAddr, &CurSockAddr, sizeof(CurSockAddr));\r
2015                 memcpy(&saSockAddr, &CurSockAddrIPv4, sizeof(CurSockAddrIPv4));\r
2016         }\r
2017 \r
2018         /////////////\r
2019         // 接続実行\r
2020         /////////////\r
2021 \r
2022         if((sSocket = do_socket(AF_INET, SOCK_STREAM, TCP_PORT)) != INVALID_SOCKET)\r
2023         {\r
2024                 if(do_connect(sSocket, (struct sockaddr *)&saSockAddr, sizeof(saSockAddr), CancelCheckWork) != SOCKET_ERROR)\r
2025                 {\r
2026                         if(Fwall == FWALL_SOCKS4)\r
2027                         {\r
2028                                 Socks4Reply.Result = -1;\r
2029                                 // 同時接続対応\r
2030 //                              if((SocksSendCmd(sSocket, &Socks4Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2031 //                                 (Socks4GetCmdReply(sSocket, &Socks4Reply) != FFFTP_SUCCESS) || \r
2032 //                                 (Socks4Reply.Result != SOCKS4_RES_OK))\r
2033                                 if((SocksSendCmd(sSocket, &Socks4Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2034                                    (Socks4GetCmdReply(sSocket, &Socks4Reply, CancelCheckWork) != FFFTP_SUCCESS) || \r
2035                                    (Socks4Reply.Result != SOCKS4_RES_OK))\r
2036                                 {\r
2037                                         SetTaskMsg(MSGJPN023, Socks4Reply.Result);\r
2038                                         DoClose(sSocket);\r
2039                                         sSocket = INVALID_SOCKET;\r
2040                                 }\r
2041                         }\r
2042                         else if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
2043                         {\r
2044                                 if(Socks5SelMethod(sSocket, CancelCheckWork) == FFFTP_FAIL)\r
2045                                 {\r
2046                                         DoClose(sSocket);\r
2047                                         sSocket = INVALID_SOCKET;\r
2048                                 }\r
2049 \r
2050                                 Socks5Reply.Result = -1;\r
2051                                 // 同時接続対応\r
2052 //                              if((SocksSendCmd(sSocket, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2053 //                                 (Socks5GetCmdReply(sSocket, &Socks5Reply) != FFFTP_SUCCESS) || \r
2054 //                                 (Socks5Reply.Result != SOCKS5_RES_OK))\r
2055                                 if((SocksSendCmd(sSocket, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2056                                    (Socks5GetCmdReply(sSocket, &Socks5Reply, CancelCheckWork) != FFFTP_SUCCESS) || \r
2057                                    (Socks5Reply.Result != SOCKS5_RES_OK))\r
2058                                 {\r
2059                                         SetTaskMsg(MSGJPN024, Socks5Reply.Result);\r
2060                                         DoClose(sSocket);\r
2061                                         sSocket = INVALID_SOCKET;\r
2062                                 }\r
2063 \r
2064                         }\r
2065 \r
2066                         if(sSocket != INVALID_SOCKET)\r
2067                                 SetTaskMsg(MSGJPN025);\r
2068                 }\r
2069                 else\r
2070                 {\r
2071 //#pragma aaa\r
2072                         SetTaskMsg(MSGJPN026/*"接続できません(2) %x", sSocket*/);\r
2073                         DoClose(sSocket);\r
2074                         sSocket = INVALID_SOCKET;\r
2075                 }\r
2076         }\r
2077         else\r
2078                 SetTaskMsg(MSGJPN027);\r
2079 \r
2080         return(sSocket);\r
2081 }\r
2082 \r
2083 \r
2084 SOCKET connectsockIPv6(char *host, int port, char *PreMsg, int *CancelCheckWork)\r
2085 {\r
2086         struct sockaddr_in6 saSockAddr;\r
2087         char HostEntry[MAXGETHOSTSTRUCT];\r
2088         struct hostent *pHostEntry;\r
2089         SOCKET sSocket;\r
2090         int Len;\r
2091         int Fwall;\r
2092         SOCKS5REQUEST Socks5Cmd;\r
2093         SOCKS5REPLY Socks5Reply;\r
2094 \r
2095         //////////////////////////////\r
2096         // ホスト名解決と接続の準備\r
2097         //////////////////////////////\r
2098 \r
2099         Fwall = FWALL_NONE;\r
2100         if(AskHostFireWall() == YES)\r
2101                 Fwall = FwallType;\r
2102 \r
2103         sSocket = INVALID_SOCKET;\r
2104 \r
2105         UseIPadrs = YES;\r
2106         strcpy(DomainName, host);\r
2107         memset(&CurSockAddrIPv6, 0, sizeof(CurSockAddrIPv6));\r
2108         CurSockAddrIPv6.sin6_port = htons((u_short)port);\r
2109         CurSockAddrIPv6.sin6_family = AF_INET6;\r
2110         CurSockAddrIPv6.sin6_addr = inet6_addr(host);\r
2111         if(memcmp(&CurSockAddrIPv6.sin6_addr, &IN6ADDR_NONE, sizeof(struct in6_addr)) == 0)\r
2112         {\r
2113                 // ホスト名が指定された\r
2114                 // ホスト名からアドレスを求める\r
2115                 if(((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER)) &&\r
2116                    (FwallResolv == YES))\r
2117                 {\r
2118                         // ホスト名解決はSOCKSサーバに任せる\r
2119                         pHostEntry = NULL;\r
2120                 }\r
2121                 else\r
2122                 {\r
2123                         // アドレスを取得\r
2124                         SetTaskMsg(MSGJPN016, DomainName);\r
2125                         pHostEntry = do_gethostbynameIPv6(host, HostEntry, MAXGETHOSTSTRUCT, CancelCheckWork);\r
2126                 }\r
2127 \r
2128                 if(pHostEntry != NULL)\r
2129                 {\r
2130                         memcpy((char *)&CurSockAddrIPv6.sin6_addr, pHostEntry->h_addr, pHostEntry->h_length);\r
2131                         SetTaskMsg(MSGJPN017, PreMsg, DomainName, inet6_ntoa(CurSockAddrIPv6.sin6_addr), ntohs(CurSockAddrIPv6.sin6_port));\r
2132                 }\r
2133                 else\r
2134                 {\r
2135                         if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
2136                         {\r
2137                                 UseIPadrs = NO;\r
2138                                 SetTaskMsg(MSGJPN018, PreMsg, DomainName, ntohs(CurSockAddrIPv6.sin6_port));\r
2139                         }\r
2140                         else\r
2141                         {\r
2142                                 SetTaskMsg(MSGJPN019, host);\r
2143                                 return(INVALID_SOCKET);\r
2144                         }\r
2145                 }\r
2146         }\r
2147         else\r
2148                 SetTaskMsg(MSGJPN020, PreMsg, inet6_ntoa(CurSockAddrIPv6.sin6_addr), ntohs(CurSockAddrIPv6.sin6_port));\r
2149 \r
2150         if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
2151         {\r
2152                 // SOCKSを使う\r
2153                 // SOCKSに接続する準備\r
2154                 {\r
2155                         Len = Socks5MakeCmdPacketIPv6(&Socks5Cmd, SOCKS5_CMD_CONNECT, UseIPadrs, (char*)&CurSockAddrIPv6.sin6_addr, DomainName, CurSockAddrIPv6.sin6_port);\r
2156                 }\r
2157 \r
2158                 memset(&SocksSockAddrIPv6, 0, sizeof(SocksSockAddrIPv6));\r
2159                 SocksSockAddrIPv6.sin6_addr = inet6_addr(FwallHost);\r
2160                 if(memcmp(&SocksSockAddrIPv6.sin6_addr, &IN6ADDR_NONE, sizeof(struct in6_addr)) == 0)\r
2161                 {\r
2162                         if((pHostEntry = do_gethostbynameIPv6(FwallHost, HostEntry, MAXGETHOSTSTRUCT, CancelCheckWork)) != NULL)\r
2163                                 memcpy((char *)&SocksSockAddrIPv6.sin6_addr, pHostEntry->h_addr, pHostEntry->h_length);\r
2164                         else\r
2165                         {\r
2166                                 SetTaskMsg(MSGJPN021, FwallHost);\r
2167                                 return INVALID_SOCKET;\r
2168                         }\r
2169                 }\r
2170                 SocksSockAddrIPv6.sin6_port = htons((u_short)FwallPort);\r
2171                 SocksSockAddrIPv6.sin6_family = AF_INET6;\r
2172                 SetTaskMsg(MSGJPN022, inet6_ntoa(SocksSockAddrIPv6.sin6_addr), ntohs(SocksSockAddrIPv6.sin6_port));\r
2173                 // connectで接続する先はSOCKSサーバ\r
2174                 memcpy(&saSockAddr, &SocksSockAddrIPv6, sizeof(SocksSockAddrIPv6));\r
2175         }\r
2176         else\r
2177         {\r
2178                 // connectで接続するのは接続先のホスト\r
2179                 memcpy(&saSockAddr, &CurSockAddrIPv6, sizeof(CurSockAddrIPv6));\r
2180         }\r
2181 \r
2182         /////////////\r
2183         // 接続実行\r
2184         /////////////\r
2185 \r
2186         inet6_ntoa(saSockAddr.sin6_addr);\r
2187         if((sSocket = do_socket(AF_INET6, SOCK_STREAM, TCP_PORT)) != INVALID_SOCKET)\r
2188         {\r
2189                 if(do_connect(sSocket, (struct sockaddr *)&saSockAddr, sizeof(saSockAddr), CancelCheckWork) != SOCKET_ERROR)\r
2190                 {\r
2191                         if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
2192                         {\r
2193                                 if(Socks5SelMethod(sSocket, CancelCheckWork) == FFFTP_FAIL)\r
2194                                 {\r
2195                                         DoClose(sSocket);\r
2196                                         sSocket = INVALID_SOCKET;\r
2197                                 }\r
2198 \r
2199                                 Socks5Reply.Result = -1;\r
2200                                 // 同時接続対応\r
2201 //                              if((SocksSendCmd(sSocket, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2202 //                                 (Socks5GetCmdReply(sSocket, &Socks5Reply) != FFFTP_SUCCESS) || \r
2203 //                                 (Socks5Reply.Result != SOCKS5_RES_OK))\r
2204                                 if((SocksSendCmd(sSocket, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2205                                    (Socks5GetCmdReply(sSocket, &Socks5Reply, CancelCheckWork) != FFFTP_SUCCESS) || \r
2206                                    (Socks5Reply.Result != SOCKS5_RES_OK))\r
2207                                 {\r
2208                                         SetTaskMsg(MSGJPN024, Socks5Reply.Result);\r
2209                                         DoClose(sSocket);\r
2210                                         sSocket = INVALID_SOCKET;\r
2211                                 }\r
2212 \r
2213                         }\r
2214 \r
2215                         if(sSocket != INVALID_SOCKET)\r
2216                                 SetTaskMsg(MSGJPN025);\r
2217                 }\r
2218                 else\r
2219                 {\r
2220 //#pragma aaa\r
2221                         SetTaskMsg(MSGJPN026/*"接続できません(2) %x", sSocket*/);\r
2222                         DoClose(sSocket);\r
2223                         sSocket = INVALID_SOCKET;\r
2224                 }\r
2225         }\r
2226         else\r
2227                 SetTaskMsg(MSGJPN027);\r
2228 \r
2229         return(sSocket);\r
2230 }\r
2231 \r
2232 \r
2233 /*----- リッスンソケットを取得 ------------------------------------------------\r
2234 *\r
2235 *       Parameter\r
2236 *               SOCKET ctrl_skt : コントロールソケット\r
2237 *\r
2238 *       Return Value\r
2239 *               SOCKET リッスンソケット\r
2240 *----------------------------------------------------------------------------*/\r
2241 \r
2242 // IPv6対応\r
2243 SOCKET GetFTPListenSocket(SOCKET ctrl_skt, int *CancelCheckWork)\r
2244 {\r
2245         SOCKET Result;\r
2246         Result = INVALID_SOCKET;\r
2247         switch(CurHost.CurNetType)\r
2248         {\r
2249         case NTYPE_IPV4:\r
2250                 Result = GetFTPListenSocketIPv4(ctrl_skt, CancelCheckWork);\r
2251                 break;\r
2252         case NTYPE_IPV6:\r
2253                 Result = GetFTPListenSocketIPv6(ctrl_skt, CancelCheckWork);\r
2254                 break;\r
2255         }\r
2256         return Result;\r
2257 }\r
2258 \r
2259 \r
2260 // IPv6対応\r
2261 //SOCKET GetFTPListenSocket(SOCKET ctrl_skt, int *CancelCheckWork)\r
2262 SOCKET GetFTPListenSocketIPv4(SOCKET ctrl_skt, int *CancelCheckWork)\r
2263 {\r
2264     SOCKET listen_skt;\r
2265     int iLength;\r
2266     char *a,*p;\r
2267         struct sockaddr_in saCtrlAddr;\r
2268         struct sockaddr_in saTmpAddr;\r
2269         SOCKS4CMD Socks4Cmd;\r
2270         SOCKS4REPLY Socks4Reply;\r
2271         SOCKS5REQUEST Socks5Cmd;\r
2272         SOCKS5REPLY Socks5Reply;\r
2273 \r
2274         int Len;\r
2275         int Fwall;\r
2276 \r
2277         Fwall = FWALL_NONE;\r
2278         if(AskHostFireWall() == YES)\r
2279                 Fwall = FwallType;\r
2280 \r
2281         if((listen_skt = do_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != INVALID_SOCKET)\r
2282         {\r
2283                 if(Fwall == FWALL_SOCKS4)\r
2284                 {\r
2285                         /*===== SOCKS4を使う =====*/\r
2286                         DoPrintf("Use SOCKS4 BIND");\r
2287                         // IPv6対応\r
2288 //                      if(do_connect(listen_skt, (struct sockaddr *)&SocksSockAddr, sizeof(SocksSockAddr), CancelCheckWork) != SOCKET_ERROR)\r
2289                         if(do_connect(listen_skt, (struct sockaddr *)&SocksSockAddrIPv4, sizeof(SocksSockAddrIPv4), CancelCheckWork) != SOCKET_ERROR)\r
2290                         {\r
2291                                 Socks4Cmd.Ver = SOCKS4_VER;\r
2292                                 Socks4Cmd.Cmd = SOCKS4_CMD_BIND;\r
2293                                 // IPv6対応\r
2294 //                              Socks4Cmd.Port = CurSockAddr.sin_port;\r
2295 //                              Socks4Cmd.AdrsInt = CurSockAddr.sin_addr.s_addr;\r
2296                                 Socks4Cmd.Port = CurSockAddrIPv4.sin_port;\r
2297                                 Socks4Cmd.AdrsInt = CurSockAddrIPv4.sin_addr.s_addr;\r
2298                                 strcpy(Socks4Cmd.UserID, FwallUser);\r
2299                                 Len = offsetof(SOCKS4CMD, UserID) + strlen(FwallUser) + 1;\r
2300 \r
2301                                 Socks4Reply.Result = -1;\r
2302                                 // 同時接続対応\r
2303 //                              if((SocksSendCmd(listen_skt, &Socks4Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2304 //                                 (Socks4GetCmdReply(listen_skt, &Socks4Reply) != FFFTP_SUCCESS) || \r
2305 //                                 (Socks4Reply.Result != SOCKS4_RES_OK))\r
2306                                 if((SocksSendCmd(listen_skt, &Socks4Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2307                                    (Socks4GetCmdReply(listen_skt, &Socks4Reply, CancelCheckWork) != FFFTP_SUCCESS) || \r
2308                                    (Socks4Reply.Result != SOCKS4_RES_OK))\r
2309                                 {\r
2310                                         SetTaskMsg(MSGJPN028, Socks4Reply.Result);\r
2311                                         DoClose(listen_skt);\r
2312                                         listen_skt = INVALID_SOCKET;\r
2313                                 }\r
2314 \r
2315                                 if(Socks4Reply.AdrsInt == 0)\r
2316                                         // IPv6対応\r
2317 //                                      Socks4Reply.AdrsInt = SocksSockAddr.sin_addr.s_addr;\r
2318                                         Socks4Reply.AdrsInt = SocksSockAddrIPv4.sin_addr.s_addr;\r
2319 \r
2320                                 a = (char *)&Socks4Reply.AdrsInt;\r
2321                                 p = (char *)&Socks4Reply.Port;\r
2322                         }\r
2323                 }\r
2324                 else if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
2325                 {\r
2326                         /*===== SOCKS5を使う =====*/\r
2327                         DoPrintf("Use SOCKS5 BIND");\r
2328                         // IPv6対応\r
2329 //                      if(do_connect(listen_skt, (struct sockaddr *)&SocksSockAddr, sizeof(SocksSockAddr), CancelCheckWork) != SOCKET_ERROR)\r
2330                         if(do_connect(listen_skt, (struct sockaddr *)&SocksSockAddrIPv4, sizeof(SocksSockAddrIPv4), CancelCheckWork) != SOCKET_ERROR)\r
2331                         {\r
2332                                 if(Socks5SelMethod(listen_skt, CancelCheckWork) == FFFTP_FAIL)\r
2333                                 {\r
2334                                         DoClose(listen_skt);\r
2335                                         listen_skt = INVALID_SOCKET;\r
2336                                         return(listen_skt);\r
2337                                 }\r
2338 \r
2339                                 // IPv6対応\r
2340 //                              Len = Socks5MakeCmdPacket(&Socks5Cmd, SOCKS5_CMD_BIND, UseIPadrs, CurSockAddr.sin_addr.s_addr, DomainName, CurSockAddr.sin_port);\r
2341                                 Len = Socks5MakeCmdPacket(&Socks5Cmd, SOCKS5_CMD_BIND, UseIPadrs, CurSockAddrIPv4.sin_addr.s_addr, DomainName, CurSockAddrIPv4.sin_port);\r
2342 \r
2343                                 Socks5Reply.Result = -1;\r
2344                                 // 同時接続対応\r
2345 //                              if((SocksSendCmd(listen_skt, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2346 //                                 (Socks5GetCmdReply(listen_skt, &Socks5Reply) != FFFTP_SUCCESS) || \r
2347 //                                 (Socks5Reply.Result != SOCKS5_RES_OK))\r
2348                                 if((SocksSendCmd(listen_skt, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2349                                    (Socks5GetCmdReply(listen_skt, &Socks5Reply, CancelCheckWork) != FFFTP_SUCCESS) || \r
2350                                    (Socks5Reply.Result != SOCKS5_RES_OK))\r
2351                                 {\r
2352                                         SetTaskMsg(MSGJPN029, Socks5Reply.Result);\r
2353                                         DoClose(listen_skt);\r
2354                                         listen_skt = INVALID_SOCKET;\r
2355                                 }\r
2356 \r
2357                                 // IPv6対応\r
2358 //                              if(Socks5Reply.AdrsInt == 0)\r
2359 //                                      Socks5Reply.AdrsInt = SocksSockAddr.sin_addr.s_addr;\r
2360 \r
2361                                 // IPv6対応\r
2362 //                              a = (char *)&Socks5Reply.AdrsInt;\r
2363 //                              p = (char *)&Socks5Reply.Port;\r
2364                                 a = (char *)&Socks5Reply._dummy[0];\r
2365                                 p = (char *)&Socks5Reply._dummy[4];\r
2366                         }\r
2367                 }\r
2368                 else\r
2369                 {\r
2370                         /*===== SOCKSを使わない =====*/\r
2371                         DoPrintf("Use normal BIND");\r
2372                         saCtrlAddr.sin_port = htons(0);\r
2373                         saCtrlAddr.sin_family = AF_INET;\r
2374                         saCtrlAddr.sin_addr.s_addr = 0;\r
2375 \r
2376                         if(bind(listen_skt, (struct sockaddr *)&saCtrlAddr, sizeof(struct sockaddr)) != SOCKET_ERROR)\r
2377                         {\r
2378                                 iLength = sizeof(saCtrlAddr);\r
2379                                 if(getsockname(listen_skt, (struct sockaddr *)&saCtrlAddr, &iLength) != SOCKET_ERROR)\r
2380                                 {\r
2381                                         if(do_listen(listen_skt, 1) == 0)\r
2382                                         {\r
2383                                                 iLength = sizeof(saTmpAddr);\r
2384                                                 if(getsockname(ctrl_skt, (struct sockaddr *)&saTmpAddr, &iLength) == SOCKET_ERROR)\r
2385                                                         ReportWSError("getsockname", WSAGetLastError());\r
2386 \r
2387                                                 a = (char *)&saTmpAddr.sin_addr;\r
2388                                                 p = (char *)&saCtrlAddr.sin_port;\r
2389                                         }\r
2390                                         else\r
2391                                         {\r
2392                                                 ReportWSError("listen", WSAGetLastError());\r
2393                                                 do_closesocket(listen_skt);\r
2394                                                 listen_skt = INVALID_SOCKET;\r
2395                                         }\r
2396                                 }\r
2397                                 else\r
2398                                 {\r
2399                                         ReportWSError("getsockname", WSAGetLastError());\r
2400                                         do_closesocket(listen_skt);\r
2401                                         listen_skt = INVALID_SOCKET;\r
2402                                 }\r
2403                         }\r
2404                         else\r
2405                         {\r
2406                                 ReportWSError("bind", WSAGetLastError());\r
2407                                 do_closesocket(listen_skt);\r
2408                                 listen_skt = INVALID_SOCKET;\r
2409                         }\r
2410 \r
2411                         if(listen_skt == INVALID_SOCKET)\r
2412                                 SetTaskMsg(MSGJPN030);\r
2413                 }\r
2414         }\r
2415         else\r
2416                 ReportWSError("socket create", WSAGetLastError());\r
2417 \r
2418         if(listen_skt != INVALID_SOCKET)\r
2419         {\r
2420 #define  UC(b)  (((int)b)&0xff)\r
2421                 // 同時接続対応\r
2422 //              if((command(ctrl_skt,NULL, &CancelFlg, "PORT %d,%d,%d,%d,%d,%d",\r
2423 //                              UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),\r
2424 //                              UC(p[0]), UC(p[1])) / 100) != FTP_COMPLETE)\r
2425                 if((command(ctrl_skt,NULL, CancelCheckWork, "PORT %d,%d,%d,%d,%d,%d",\r
2426                                 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),\r
2427                                 UC(p[0]), UC(p[1])) / 100) != FTP_COMPLETE)\r
2428                 {\r
2429                         SetTaskMsg(MSGJPN031);\r
2430                         do_closesocket(listen_skt);\r
2431                         listen_skt = INVALID_SOCKET;\r
2432                 }\r
2433 //              else\r
2434 //                      DoPrintf("Skt=%u : listener %s port %u",listen_skt,inet_ntoa(saCtrlAddr.sin_addr),ntohs(saCtrlAddr.sin_port));\r
2435         }\r
2436 \r
2437         return(listen_skt);\r
2438 }\r
2439 \r
2440 \r
2441 SOCKET GetFTPListenSocketIPv6(SOCKET ctrl_skt, int *CancelCheckWork)\r
2442 {\r
2443     SOCKET listen_skt;\r
2444     int iLength;\r
2445     char *a,*p;\r
2446         struct sockaddr_in6 saCtrlAddr;\r
2447         struct sockaddr_in6 saTmpAddr;\r
2448         SOCKS5REQUEST Socks5Cmd;\r
2449         SOCKS5REPLY Socks5Reply;\r
2450 \r
2451         int Len;\r
2452         int Fwall;\r
2453 \r
2454         char Adrs[40];\r
2455 \r
2456         Fwall = FWALL_NONE;\r
2457         if(AskHostFireWall() == YES)\r
2458                 Fwall = FwallType;\r
2459 \r
2460         if((listen_skt = do_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) != INVALID_SOCKET)\r
2461         {\r
2462                 if((Fwall == FWALL_SOCKS5_NOAUTH) || (Fwall == FWALL_SOCKS5_USER))\r
2463                 {\r
2464                         /*===== SOCKS5を使う =====*/\r
2465                         DoPrintf("Use SOCKS5 BIND");\r
2466                         if(do_connect(listen_skt, (struct sockaddr *)&SocksSockAddrIPv6, sizeof(SocksSockAddrIPv6), CancelCheckWork) != SOCKET_ERROR)\r
2467                         {\r
2468                                 if(Socks5SelMethod(listen_skt, CancelCheckWork) == FFFTP_FAIL)\r
2469                                 {\r
2470                                         DoClose(listen_skt);\r
2471                                         listen_skt = INVALID_SOCKET;\r
2472                                         return(listen_skt);\r
2473                                 }\r
2474 \r
2475                                 Len = Socks5MakeCmdPacketIPv6(&Socks5Cmd, SOCKS5_CMD_BIND, UseIPadrs, (char*)&CurSockAddrIPv6.sin6_addr, DomainName, CurSockAddrIPv6.sin6_port);\r
2476 \r
2477                                 Socks5Reply.Result = -1;\r
2478                                 // 同時接続対応\r
2479 //                              if((SocksSendCmd(listen_skt, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2480 //                                 (Socks5GetCmdReply(listen_skt, &Socks5Reply) != FFFTP_SUCCESS) || \r
2481 //                                 (Socks5Reply.Result != SOCKS5_RES_OK))\r
2482                                 if((SocksSendCmd(listen_skt, &Socks5Cmd, Len, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2483                                    (Socks5GetCmdReply(listen_skt, &Socks5Reply, CancelCheckWork) != FFFTP_SUCCESS) || \r
2484                                    (Socks5Reply.Result != SOCKS5_RES_OK))\r
2485                                 {\r
2486                                         SetTaskMsg(MSGJPN029, Socks5Reply.Result);\r
2487                                         DoClose(listen_skt);\r
2488                                         listen_skt = INVALID_SOCKET;\r
2489                                 }\r
2490 \r
2491                                 // IPv6対応\r
2492 //                              if(Socks5Reply.AdrsInt == 0)\r
2493 //                                      Socks5Reply.AdrsInt = SocksSockAddr.sin_addr.s_addr;\r
2494 \r
2495                                 // IPv6対応\r
2496 //                              a = (char *)&Socks5Reply.AdrsInt;\r
2497 //                              p = (char *)&Socks5Reply.Port;\r
2498                                 a = (char *)&Socks5Reply._dummy[0];\r
2499                                 p = (char *)&Socks5Reply._dummy[16];\r
2500                         }\r
2501                 }\r
2502                 else\r
2503                 {\r
2504                         /*===== SOCKSを使わない =====*/\r
2505                         DoPrintf("Use normal BIND");\r
2506                         saCtrlAddr.sin6_port = htons(0);\r
2507                         saCtrlAddr.sin6_family = AF_INET6;\r
2508                         memset(&saCtrlAddr.sin6_addr, 0, 16);\r
2509 \r
2510                         if(bind(listen_skt, (struct sockaddr *)&saCtrlAddr, sizeof(struct sockaddr_in6)) != SOCKET_ERROR)\r
2511                         {\r
2512                                 iLength = sizeof(saCtrlAddr);\r
2513                                 if(getsockname(listen_skt, (struct sockaddr *)&saCtrlAddr, &iLength) != SOCKET_ERROR)\r
2514                                 {\r
2515                                         if(do_listen(listen_skt, 1) == 0)\r
2516                                         {\r
2517                                                 iLength = sizeof(saTmpAddr);\r
2518                                                 if(getsockname(ctrl_skt, (struct sockaddr *)&saTmpAddr, &iLength) == SOCKET_ERROR)\r
2519                                                         ReportWSError("getsockname", WSAGetLastError());\r
2520 \r
2521                                                 a = (char *)&saTmpAddr.sin6_addr;\r
2522                                                 p = (char *)&saCtrlAddr.sin6_port;\r
2523                                         }\r
2524                                         else\r
2525                                         {\r
2526                                                 ReportWSError("listen", WSAGetLastError());\r
2527                                                 do_closesocket(listen_skt);\r
2528                                                 listen_skt = INVALID_SOCKET;\r
2529                                         }\r
2530                                 }\r
2531                                 else\r
2532                                 {\r
2533                                         ReportWSError("getsockname", WSAGetLastError());\r
2534                                         do_closesocket(listen_skt);\r
2535                                         listen_skt = INVALID_SOCKET;\r
2536                                 }\r
2537                         }\r
2538                         else\r
2539                         {\r
2540                                 ReportWSError("bind", WSAGetLastError());\r
2541                                 do_closesocket(listen_skt);\r
2542                                 listen_skt = INVALID_SOCKET;\r
2543                         }\r
2544 \r
2545                         if(listen_skt == INVALID_SOCKET)\r
2546                                 SetTaskMsg(MSGJPN030);\r
2547                 }\r
2548         }\r
2549         else\r
2550                 ReportWSError("socket create", WSAGetLastError());\r
2551 \r
2552         if(listen_skt != INVALID_SOCKET)\r
2553         {\r
2554 #define  UC(b)  (((int)b)&0xff)\r
2555                 // 同時接続対応\r
2556 //              if((command(ctrl_skt,NULL, &CancelFlg, "PORT %d,%d,%d,%d,%d,%d",\r
2557 //                              UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),\r
2558 //                              UC(p[0]), UC(p[1])) / 100) != FTP_COMPLETE)\r
2559                 if((command(ctrl_skt,NULL, CancelCheckWork, "EPRT |2|%s|%d|",\r
2560                                 AddressToStringIPv6(Adrs, a),\r
2561                                 (UC(p[0]) << 8) | UC(p[1])) / 100) != FTP_COMPLETE)\r
2562                 {\r
2563                         SetTaskMsg(MSGJPN031);\r
2564                         do_closesocket(listen_skt);\r
2565                         listen_skt = INVALID_SOCKET;\r
2566                 }\r
2567 //              else\r
2568 //                      DoPrintf("Skt=%u : listener %s port %u",listen_skt,inet_ntoa(saCtrlAddr.sin_addr),ntohs(saCtrlAddr.sin_port));\r
2569         }\r
2570 \r
2571         return(listen_skt);\r
2572 }\r
2573 \r
2574 \r
2575 /*----- ホストへ接続処理中かどうかを返す---------------------------------------\r
2576 *\r
2577 *       Parameter\r
2578 *               なし\r
2579 *\r
2580 *       Return Value\r
2581 *               int ステータス\r
2582 *                       YES/NO\r
2583 *----------------------------------------------------------------------------*/\r
2584 \r
2585 int AskTryingConnect(void)\r
2586 {\r
2587         return(TryConnect);\r
2588 }\r
2589 \r
2590 \r
2591 #if 0\r
2592 ///*----- ブロッキングコールのフックコールバック --------------------------------\r
2593 //*\r
2594 //*     Parameter\r
2595 //*             なし\r
2596 //*\r
2597 //*     Return Value\r
2598 //*             BOOL FALSE\r
2599 //*----------------------------------------------------------------------------*/\r
2600 //\r
2601 //static BOOL CALLBACK BlkHookFnc(void)\r
2602 //{\r
2603 //      BackgrndMessageProc();\r
2604 //\r
2605 //      if(CancelFlg == YES)\r
2606 //      {\r
2607 //              SetTaskMsg(MSGJPN032);\r
2608 //              WSACancelBlockingCall();\r
2609 //              CancelFlg = NO;\r
2610 //      }\r
2611 //      return(FALSE);\r
2612 //}\r
2613 #endif\r
2614 \r
2615 \r
2616 \r
2617 /*----- SOCKS5のコマンドパケットを作成する ------------------------------------\r
2618 *\r
2619 *       Parameter\r
2620 *               SOCKS5REQUEST *Packet : パケットを作成するワーク\r
2621 *               char Cmd : コマンド\r
2622 *               int ValidIP : IPアドレスを使うかどうか(YES/NO)\r
2623 *               ulong IP : IPアドレス\r
2624 *               char *Host : ホスト名\r
2625 *               ushort Port : ポート\r
2626 *\r
2627 *       Return Value\r
2628 *               int コマンドパケットの長さ\r
2629 *----------------------------------------------------------------------------*/\r
2630 \r
2631 static int Socks5MakeCmdPacket(SOCKS5REQUEST *Packet, char Cmd, int ValidIP, ulong IP, char *Host, ushort Port)\r
2632 {\r
2633         uchar *Pos;\r
2634         int Len;\r
2635         int TotalLen;\r
2636 \r
2637         Pos = (uchar *)Packet;\r
2638         Pos += SOCKS5REQUEST_SIZE;\r
2639         TotalLen = SOCKS5REQUEST_SIZE + 2;      /* +2はポートの分 */\r
2640 \r
2641         Packet->Ver = SOCKS5_VER;\r
2642         Packet->Cmd = Cmd;\r
2643         Packet->Rsv = 0;\r
2644         if(ValidIP == YES)\r
2645         {\r
2646                 /* IPアドレスを指定 */\r
2647                 Packet->Type = SOCKS5_ADRS_IPV4;\r
2648                 *((ulong *)Pos) = IP;\r
2649                 Pos += 4;\r
2650                 TotalLen += 4;\r
2651         }\r
2652         else\r
2653         {\r
2654                 /* ホスト名を指定 */\r
2655                 Packet->Type = SOCKS5_ADRS_NAME;\r
2656                 Len = strlen(Host);\r
2657                 *Pos++ = Len;\r
2658                 strcpy(Pos, Host);\r
2659                 Pos += Len;\r
2660                 TotalLen += Len + 1;\r
2661         }\r
2662         *((ushort *)Pos) = Port;\r
2663 \r
2664         return(TotalLen);\r
2665 }\r
2666 \r
2667 \r
2668 // IPv6対応\r
2669 static int Socks5MakeCmdPacketIPv6(SOCKS5REQUEST *Packet, char Cmd, int ValidIP, char *IP, char *Host, ushort Port)\r
2670 {\r
2671         uchar *Pos;\r
2672         int Len;\r
2673         int TotalLen;\r
2674 \r
2675         Pos = (uchar *)Packet;\r
2676         Pos += SOCKS5REQUEST_SIZE;\r
2677         TotalLen = SOCKS5REQUEST_SIZE + 2;      /* +2はポートの分 */\r
2678 \r
2679         Packet->Ver = SOCKS5_VER;\r
2680         Packet->Cmd = Cmd;\r
2681         Packet->Rsv = 0;\r
2682         if(ValidIP == YES)\r
2683         {\r
2684                 /* IPアドレスを指定 */\r
2685                 Packet->Type = SOCKS5_ADRS_IPV6;\r
2686                 memcpy(Pos, IP, 16);\r
2687                 Pos += 16;\r
2688                 TotalLen += 16;\r
2689         }\r
2690         else\r
2691         {\r
2692                 /* ホスト名を指定 */\r
2693                 Packet->Type = SOCKS5_ADRS_NAME;\r
2694                 Len = strlen(Host);\r
2695                 *Pos++ = Len;\r
2696                 strcpy(Pos, Host);\r
2697                 Pos += Len;\r
2698                 TotalLen += Len + 1;\r
2699         }\r
2700         *((ushort *)Pos) = Port;\r
2701 \r
2702         return(TotalLen);\r
2703 }\r
2704 \r
2705 \r
2706 /*----- SOCKSのコマンドを送る -------------------------------------------------\r
2707 *\r
2708 *       Parameter\r
2709 *               SOCKET Socket : ソケット\r
2710 *               void *Data : 送るデータ\r
2711 *               int Size : サイズ\r
2712 *\r
2713 *       Return Value\r
2714 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
2715 *----------------------------------------------------------------------------*/\r
2716 \r
2717 static int SocksSendCmd(SOCKET Socket, void *Data, int Size, int *CancelCheckWork)\r
2718 {\r
2719         int Ret;\r
2720 \r
2721         Ret = SendData(Socket, (char *)Data, Size, 0, CancelCheckWork);\r
2722 \r
2723         if(Ret != FFFTP_SUCCESS)\r
2724                 SetTaskMsg(MSGJPN033, *((short *)Data));\r
2725 \r
2726         return(Ret);\r
2727 }\r
2728 \r
2729 \r
2730 /*----- SOCKS5のコマンドに対するリプライパケットを受信する --------------------\r
2731 *\r
2732 *       Parameter\r
2733 *               SOCKET Socket : ソケット\r
2734 *               SOCKS5REPLY *Packet : パケット\r
2735 *\r
2736 *       Return Value\r
2737 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
2738 *----------------------------------------------------------------------------*/\r
2739 \r
2740 // 同時接続対応\r
2741 //static int Socks5GetCmdReply(SOCKET Socket, SOCKS5REPLY *Packet)\r
2742 static int Socks5GetCmdReply(SOCKET Socket, SOCKS5REPLY *Packet, int *CancelCheckWork)\r
2743 {\r
2744         uchar *Pos;\r
2745         int Len;\r
2746         int Ret;\r
2747 \r
2748         Pos = (uchar *)Packet;\r
2749         Pos += SOCKS5REPLY_SIZE;\r
2750 \r
2751         // 同時接続対応\r
2752 //      if((Ret = ReadNchar(Socket, (char *)Packet, SOCKS5REPLY_SIZE, &CancelFlg)) == FFFTP_SUCCESS)\r
2753         if((Ret = ReadNchar(Socket, (char *)Packet, SOCKS5REPLY_SIZE, CancelCheckWork)) == FFFTP_SUCCESS)\r
2754         {\r
2755                 if(Packet->Type == SOCKS5_ADRS_IPV4)\r
2756                         Len = 4 + 2;\r
2757                 else if(Packet->Type == SOCKS5_ADRS_IPV6)\r
2758                         Len = 16 + 2;\r
2759                 else\r
2760                 {\r
2761                         // 同時接続対応\r
2762 //                      if((Ret = ReadNchar(Socket, (char *)Pos, 1, &CancelFlg)) == FFFTP_SUCCESS)\r
2763                         if((Ret = ReadNchar(Socket, (char *)Pos, 1, CancelCheckWork)) == FFFTP_SUCCESS)\r
2764                         {\r
2765                                 Len = *Pos + 2;\r
2766                                 Pos++;\r
2767                         }\r
2768                 }\r
2769 \r
2770                 if(Ret == FFFTP_SUCCESS)\r
2771                         // 同時接続対応\r
2772 //                      Ret = ReadNchar(Socket, (char *)Pos, Len, &CancelFlg);\r
2773                         Ret = ReadNchar(Socket, (char *)Pos, Len, CancelCheckWork);\r
2774         }\r
2775 \r
2776         if(Ret != FFFTP_SUCCESS)\r
2777                 SetTaskMsg(MSGJPN034);\r
2778 \r
2779         return(Ret);\r
2780 }\r
2781 \r
2782 \r
2783 /*----- SOCKS4のコマンドに対するリプライパケットを受信する --------------------\r
2784 *\r
2785 *       Parameter\r
2786 *               SOCKET Socket : ソケット\r
2787 *               SOCKS5REPLY *Packet : パケット\r
2788 *\r
2789 *       Return Value\r
2790 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
2791 *----------------------------------------------------------------------------*/\r
2792 \r
2793 // 同時接続対応\r
2794 //static int Socks4GetCmdReply(SOCKET Socket, SOCKS4REPLY *Packet)\r
2795 static int Socks4GetCmdReply(SOCKET Socket, SOCKS4REPLY *Packet, int *CancelCheckWork)\r
2796 {\r
2797         int Ret;\r
2798 \r
2799         // 同時接続対応\r
2800 //      Ret = ReadNchar(Socket, (char *)Packet, SOCKS4REPLY_SIZE, &CancelFlg);\r
2801         Ret = ReadNchar(Socket, (char *)Packet, SOCKS4REPLY_SIZE, CancelCheckWork);\r
2802 \r
2803         if(Ret != FFFTP_SUCCESS)\r
2804                 DoPrintf(MSGJPN035);\r
2805 \r
2806         return(Ret);\r
2807 }\r
2808 \r
2809 \r
2810 /*----- SOCKS5の認証を行う ----------------------------------------------------\r
2811 *\r
2812 *       Parameter\r
2813 *               SOCKET Socket : ソケット\r
2814 *\r
2815 *       Return Value\r
2816 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
2817 *----------------------------------------------------------------------------*/\r
2818 \r
2819 static int Socks5SelMethod(SOCKET Socket, int *CancelCheckWork)\r
2820 {\r
2821         int Ret;\r
2822         SOCKS5METHODREQUEST Socks5Method;\r
2823         SOCKS5METHODREPLY Socks5MethodReply;\r
2824         SOCKS5USERPASSSTATUS Socks5Status;\r
2825         char Buf[USER_NAME_LEN + PASSWORD_LEN + 4];\r
2826         int Len;\r
2827         int Len2;\r
2828 \r
2829         Ret = FFFTP_SUCCESS;\r
2830         Socks5Method.Ver = SOCKS5_VER;\r
2831         Socks5Method.Num = 1;\r
2832         if(FwallType == FWALL_SOCKS5_NOAUTH)\r
2833                 Socks5Method.Methods[0] = SOCKS5_AUTH_NONE;\r
2834         else\r
2835                 Socks5Method.Methods[0] = SOCKS5_AUTH_USER;\r
2836 \r
2837         // 同時接続対応\r
2838 //      if((SocksSendCmd(Socket, &Socks5Method, SOCKS5METHODREQUEST_SIZE, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2839 //         (ReadNchar(Socket, (char *)&Socks5MethodReply, SOCKS5METHODREPLY_SIZE, &CancelFlg) != FFFTP_SUCCESS) ||\r
2840 //         (Socks5MethodReply.Method == (uchar)0xFF))\r
2841         if((SocksSendCmd(Socket, &Socks5Method, SOCKS5METHODREQUEST_SIZE, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2842            (ReadNchar(Socket, (char *)&Socks5MethodReply, SOCKS5METHODREPLY_SIZE, CancelCheckWork) != FFFTP_SUCCESS) ||\r
2843            (Socks5MethodReply.Method == (uchar)0xFF))\r
2844         {\r
2845                 SetTaskMsg(MSGJPN036);\r
2846                 Ret = FFFTP_FAIL;\r