OSDN Git Service

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