OSDN Git Service

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