OSDN Git Service

Add routines for IPv6.
[ffftp/ffftp.git] / getput.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 /* このソースは一部、WS_FTP Version 93.12.05 のソースを参考にしました。 */\r
31 /* スレッドの作成/終了に関して、樋口殿作成のパッチを組み込みました。 */\r
32 \r
33 /*\r
34         一部、高速化のためのコード追加 by H.Shirouzu at 2002/10/02\r
35 */\r
36 \r
37 #define STRICT\r
38 #include <stdio.h>\r
39 #include <stdlib.h>\r
40 #include <stdarg.h>\r
41 #include <string.h>\r
42 #include <mbstring.h>\r
43 #include <time.h>\r
44 // IPv6対応\r
45 //#include <winsock.h>\r
46 #include <winsock2.h>\r
47 #include <windowsx.h>\r
48 #include <commctrl.h>\r
49 #include <process.h>\r
50 \r
51 #include "common.h"\r
52 #include "resource.h"\r
53 \r
54 #define SET_BUFFER_SIZE\r
55 \r
56 /* Add by H.Shirouzu at 2002/10/02 */\r
57 #undef BUFSIZE\r
58 #define BUFSIZE                 (32 * 1024)\r
59 #define SOCKBUF_SIZE    (256 * 1024)\r
60 /* End */\r
61 \r
62 #ifdef DISABLE_TRANSFER_NETWORK_BUFFERS\r
63 #undef BUFSIZE\r
64 #define BUFSIZE                 (64 * 1024)     // RWIN値以下で充分な大きさが望ましいと思われる。\r
65 #undef SET_BUFFER_SIZE\r
66 #endif\r
67 \r
68 #define TIMER_DISPLAY           1               /* 表示更新用タイマのID */\r
69 #define DISPLAY_TIMING          500             /* 表示更新時間 0.5秒 */\r
70 \r
71 #define ERR_MSG_LEN                     1024\r
72 \r
73 \r
74 /* 削除確認ダイアログの情報 */\r
75 typedef struct {\r
76         int Cur;\r
77         TRANSPACKET *Pkt;\r
78 } MIRRORDELETEINFO;\r
79 \r
80 \r
81 /*===== プロトタイプ =====*/\r
82 \r
83 static void DispTransPacket(TRANSPACKET *Pkt);\r
84 static void EraseTransFileList(void);\r
85 static ULONG WINAPI TransferThread(void *Dummy);\r
86 static int MakeNonFullPath(TRANSPACKET *Pkt, char *CurDir, char *Tmp);\r
87 static int DownLoadNonPassive(TRANSPACKET *Pkt, int *CancelCheckWork);\r
88 static int DownLoadPassive(TRANSPACKET *Pkt, int *CancelCheckWork);\r
89 static int DownLoadFile(TRANSPACKET *Pkt, SOCKET dSkt, int CreateMode, int *CancelCheckWork);\r
90 static void DispDownloadFinishMsg(TRANSPACKET *Pkt, int iRetCode);\r
91 static int DispUpDownErrDialog(int ResID, HWND hWnd, char *Fname);\r
92 static BOOL CALLBACK UpDownErrorDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);\r
93 static int SetDownloadResume(TRANSPACKET *Pkt, int ProcMode, LONGLONG Size, int *Mode, int *CancelCheckWork);\r
94 static BOOL CALLBACK NoResumeWndProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam);\r
95 static int DoUpLoad(SOCKET cSkt, TRANSPACKET *Pkt);\r
96 static int UpLoadNonPassive(TRANSPACKET *Pkt);\r
97 static int UpLoadPassive(TRANSPACKET *Pkt);\r
98 static int UpLoadFile(TRANSPACKET *Pkt, SOCKET dSkt);\r
99 // 同時接続対応\r
100 //static int TermCodeConvAndSend(TERMCODECONVINFO *tInfo, SOCKET Skt, char *Data, int Size, int Ascii);\r
101 static int TermCodeConvAndSend(TERMCODECONVINFO *tInfo, SOCKET Skt, char *Data, int Size, int Ascii, int *CancelCheckWork);\r
102 static void DispUploadFinishMsg(TRANSPACKET *Pkt, int iRetCode);\r
103 static int SetUploadResume(TRANSPACKET *Pkt, int ProcMode, LONGLONG Size, int *Mode);\r
104 static LRESULT CALLBACK TransDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam);\r
105 static void DispTransferStatus(HWND hWnd, int End, TRANSPACKET *Pkt);\r
106 static void DispTransFileInfo(TRANSPACKET *Pkt, char *Title, int SkipButton, int Info);\r
107 static int GetAdrsAndPort(char *Str, char *Adrs, int *Port, int Max);\r
108 static int IsSpecialDevice(char *Fname);\r
109 static int MirrorDelNotify(int Cur, int Notify, TRANSPACKET *Pkt);\r
110 static BOOL CALLBACK MirrorDeleteDialogCallBack(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam);\r
111 static void SetErrorMsg(char *fmt, ...);\r
112 // 同時接続対応\r
113 static char* GetErrMsg();\r
114 \r
115 /*===== ローカルなワーク =====*/\r
116 \r
117 // 同時接続対応\r
118 //static HANDLE hTransferThread;\r
119 static HANDLE hTransferThread[MAX_DATA_CONNECTION];\r
120 static int fTransferThreadExit = FALSE;\r
121 \r
122 static HANDLE hRunMutex;                                /* 転送スレッド実行ミューテックス */\r
123 static HANDLE hListAccMutex;                    /* 転送ファイルアクセス用ミューテックス */\r
124 \r
125 static int TransFiles = 0;                              /* 転送待ちファイル数 */\r
126 static TRANSPACKET *TransPacketBase = NULL;     /* 転送ファイルリスト */\r
127 // 同時接続対応\r
128 static TRANSPACKET *NextTransPacketBase = NULL;\r
129 \r
130 // 同時接続対応\r
131 //static int Canceled;          /* 中止フラグ YES/NO */\r
132 static int Canceled[MAX_DATA_CONNECTION];               /* 中止フラグ YES/NO */\r
133 static int ClearAll;            /* 全て中止フラグ YES/NO */\r
134 \r
135 static int ForceAbort;          /* 転送中止フラグ */\r
136                                                         /* このフラグはスレッドを終了させるときに使う */\r
137 \r
138 // 同時接続対応\r
139 //static LONGLONG AllTransSizeNow;      /* 今回の転送で転送したサイズ */\r
140 //static time_t TimeStart;      /* 転送開始時間 */\r
141 static LONGLONG AllTransSizeNow[MAX_DATA_CONNECTION];   /* 今回の転送で転送したサイズ */\r
142 static time_t TimeStart[MAX_DATA_CONNECTION];   /* 転送開始時間 */\r
143 \r
144 static int KeepDlg = NO;        /* 転送中ダイアログを消さないかどうか (YES/NO) */\r
145 static int MoveToForeground = NO;               /* ウインドウを前面に移動するかどうか (YES/NO) */\r
146 \r
147 // 同時接続対応\r
148 //static char CurDir[FMAX_PATH+1] = { "" };\r
149 static char CurDir[MAX_DATA_CONNECTION][FMAX_PATH+1];\r
150 // 同時接続対応\r
151 //static char ErrMsg[ERR_MSG_LEN+7];\r
152 static char ErrMsg[MAX_DATA_CONNECTION+1][ERR_MSG_LEN+7];\r
153 static DWORD ErrMsgThreadId[MAX_DATA_CONNECTION+1];\r
154 static HANDLE hErrMsgMutex;\r
155 \r
156 // 同時接続対応\r
157 static int WaitForMainThread = NO;\r
158 \r
159 /*===== 外部参照 =====*/\r
160 \r
161 /* 設定値 */\r
162 extern int SaveTimeStamp;\r
163 extern int RmEOF;\r
164 // extern int TimeOut;\r
165 extern int FwallType;\r
166 extern int MirUpDelNotify;\r
167 extern int MirDownDelNotify;\r
168 extern int FolderAttr;\r
169 extern int FolderAttrNum;\r
170 \r
171 \r
172 /*----- ファイル転送スレッドを起動する ----------------------------------------\r
173 *\r
174 *       Parameter\r
175 *               なし\r
176 *\r
177 *       Return Value\r
178 *               なし\r
179 *----------------------------------------------------------------------------*/\r
180 \r
181 int MakeTransferThread(void)\r
182 {\r
183         DWORD dwID;\r
184         int i;\r
185 \r
186         hListAccMutex = CreateMutex( NULL, FALSE, NULL );\r
187         hRunMutex = CreateMutex( NULL, TRUE, NULL );\r
188         // 同時接続対応\r
189         hErrMsgMutex = CreateMutex( NULL, FALSE, NULL );\r
190 \r
191         ClearAll = NO;\r
192         ForceAbort = NO;\r
193 \r
194         fTransferThreadExit = FALSE;\r
195         // 同時接続対応\r
196 //      hTransferThread = (HANDLE)_beginthreadex(NULL, 0, TransferThread, 0, 0, &dwID);\r
197 //      if (hTransferThread == NULL)\r
198 //              return(FFFTP_FAIL); /* XXX */\r
199         for(i = 0; i < MAX_DATA_CONNECTION; i++)\r
200         {\r
201                 hTransferThread[i] = (HANDLE)_beginthreadex(NULL, 0, TransferThread, (void*)i, 0, &dwID);\r
202                 if(hTransferThread[i] == NULL)\r
203                         return FFFTP_FAIL;\r
204         }\r
205 \r
206         return(FFFTP_SUCCESS);\r
207 }\r
208 \r
209 \r
210 /*----- ファイル転送スレッドを終了する ----------------------------------------\r
211 *\r
212 *       Parameter\r
213 *               なし\r
214 *\r
215 *       Return Value\r
216 *               なし\r
217 *----------------------------------------------------------------------------*/\r
218 \r
219 void CloseTransferThread(void)\r
220 {\r
221         int i;\r
222         // 同時接続対応\r
223 //      Canceled = YES;\r
224         for(i = 0; i < MAX_DATA_CONNECTION; i++)\r
225                 Canceled[i] = YES;\r
226         ClearAll = YES;\r
227         ForceAbort = YES;\r
228 \r
229         fTransferThreadExit = TRUE;\r
230         // 同時接続対応\r
231 //      while(WaitForSingleObject(hTransferThread, 10) == WAIT_TIMEOUT)\r
232 //      {\r
233 //              BackgrndMessageProc();\r
234 //              Canceled = YES;\r
235 //      }\r
236 //      CloseHandle(hTransferThread);\r
237         for(i = 0; i < MAX_DATA_CONNECTION; i++)\r
238         {\r
239                 while(WaitForSingleObject(hTransferThread[i], 10) == WAIT_TIMEOUT)\r
240                 {\r
241                         BackgrndMessageProc();\r
242                         Canceled[i] = YES;\r
243                 }\r
244                 CloseHandle(hTransferThread[i]);\r
245         }\r
246 \r
247         ReleaseMutex( hRunMutex );\r
248 \r
249         CloseHandle( hListAccMutex );\r
250         CloseHandle( hRunMutex );\r
251         // 同時接続対応\r
252         CloseHandle( hErrMsgMutex );\r
253         return;\r
254 }\r
255 \r
256 \r
257 /*----- 転送するファイル情報をリストに追加する --------------------------------\r
258 *\r
259 *       Parameter\r
260 *               TRANSPACKET *Pkt : 転送ファイル情報\r
261 *               TRANSPACKET **Base : リストの先頭\r
262 *\r
263 *       Return Value\r
264 *               int ステータス\r
265 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
266 *----------------------------------------------------------------------------*/\r
267 \r
268 int AddTmpTransFileList(TRANSPACKET *Pkt, TRANSPACKET **Base)\r
269 {\r
270         TRANSPACKET *Pos;\r
271         TRANSPACKET *Prev;\r
272         int Sts;\r
273 \r
274         Sts = FFFTP_FAIL;\r
275         if((Pos = malloc(sizeof(TRANSPACKET))) != NULL)\r
276         {\r
277                 memcpy(Pos, Pkt, sizeof(TRANSPACKET));\r
278                 Pos->Next = NULL;\r
279 \r
280                 if(*Base == NULL)\r
281                         *Base = Pos;\r
282                 else\r
283                 {\r
284                         Prev = *Base;\r
285                         while(Prev->Next != NULL)\r
286                                 Prev = Prev->Next;\r
287                         Prev->Next = Pos;\r
288                 }\r
289                 Sts = FFFTP_SUCCESS;\r
290         }\r
291         return(Sts);\r
292 }\r
293 \r
294 \r
295 /*----- 転送するファイル情報リストをクリアする --------------------------------\r
296 *\r
297 *       Parameter\r
298 *               TRANSPACKET **Base : リストの先頭\r
299 *\r
300 *       Return Value\r
301 *               なし\r
302 *----------------------------------------------------------------------------*/\r
303 \r
304 void EraseTmpTransFileList(TRANSPACKET **Base)\r
305 {\r
306         TRANSPACKET *Pos;\r
307         TRANSPACKET *Next;\r
308 \r
309         Pos = *Base;\r
310         while(Pos != NULL)\r
311         {\r
312                 Next = Pos->Next;\r
313                 free(Pos);\r
314                 Pos = Next;\r
315         }\r
316         *Base = NULL;\r
317         return;\r
318 }\r
319 \r
320 \r
321 /*----- 転送するファイル情報リストから1つの情報を取り除く --------------------\r
322 *\r
323 *       Parameter\r
324 *               TRANSPACKET *Pkt : 転送ファイル情報\r
325 *               TRANSPACKET **Base : リストの先頭\r
326 *\r
327 *       Return Value\r
328 *               int ステータス\r
329 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
330 *----------------------------------------------------------------------------*/\r
331 \r
332 int RemoveTmpTransFileListItem(TRANSPACKET **Base, int Num)\r
333 {\r
334         TRANSPACKET *Pos;\r
335         TRANSPACKET *Prev;\r
336         int Sts;\r
337 \r
338         Sts = FFFTP_FAIL;\r
339         Pos = *Base;\r
340         if(Num == 0)\r
341         {\r
342                 *Base = Pos->Next;\r
343                 free(Pos);\r
344                 Sts = FFFTP_SUCCESS;\r
345         }\r
346         else\r
347         {\r
348                 while(Pos != NULL)\r
349                 {\r
350                         Prev = Pos;\r
351                         Pos = Pos->Next;\r
352                         if(--Num == 0)\r
353                         {\r
354                                 Prev->Next = Pos->Next;\r
355                                 free(Pos);\r
356                                 Sts = FFFTP_SUCCESS;\r
357                                 break;\r
358                         }\r
359                 }\r
360         }\r
361         return(Sts);\r
362 }\r
363 \r
364 \r
365 /*----- 転送するファイル情報を転送ファイルリストに登録する --------------------\r
366 *\r
367 *       Parameter\r
368 *               TRANSPACKET *Pkt : 転送ファイル情報\r
369 *\r
370 *       Return Value\r
371 *               なし\r
372 *----------------------------------------------------------------------------*/\r
373 \r
374 void AddTransFileList(TRANSPACKET *Pkt)\r
375 {\r
376         DispTransPacket(Pkt);\r
377 \r
378         // 同時接続対応\r
379 //      WaitForSingleObject(hListAccMutex, INFINITE);\r
380         while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
381         {\r
382                 WaitForMainThread = YES;\r
383                 BackgrndMessageProc();\r
384                 Sleep(1);\r
385         }\r
386 \r
387         if(AddTmpTransFileList(Pkt, &TransPacketBase) == FFFTP_SUCCESS)\r
388         {\r
389                 if((strncmp(Pkt->Cmd, "RETR", 4) == 0) ||\r
390                    (strncmp(Pkt->Cmd, "STOR", 4) == 0))\r
391                 {\r
392                         TransFiles++;\r
393                         PostMessage(GetMainHwnd(), WM_CHANGE_COND, 0, 0);\r
394                 }\r
395         }\r
396         // 同時接続対応\r
397         if(NextTransPacketBase == NULL)\r
398                 NextTransPacketBase = TransPacketBase;\r
399         ReleaseMutex(hListAccMutex);\r
400         // 同時接続対応\r
401         WaitForMainThread = NO;\r
402 \r
403         return;\r
404 }\r
405 \r
406 \r
407 /*----- 転送ファイル情報を転送ファイルリストに追加する ------------------------\r
408 *\r
409 *       Parameter\r
410 *               TRANSPACKET *Pkt : 転送ファイル情報\r
411 *               TRANSPACKET **Base : リストの先頭\r
412 *\r
413 *       Return Value\r
414 *               なし\r
415 *\r
416 *       Note\r
417 *               Pkt自体をリストに連結する\r
418 *----------------------------------------------------------------------------*/\r
419 \r
420 void AppendTransFileList(TRANSPACKET *Pkt)\r
421 {\r
422         TRANSPACKET *Pos;\r
423 \r
424         // 同時接続対応\r
425 //      WaitForSingleObject(hListAccMutex, INFINITE);\r
426         while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
427         {\r
428                 WaitForMainThread = YES;\r
429                 BackgrndMessageProc();\r
430                 Sleep(1);\r
431         }\r
432 \r
433         if(TransPacketBase == NULL)\r
434                 TransPacketBase = Pkt;\r
435         else\r
436         {\r
437                 Pos = TransPacketBase;\r
438                 while(Pos->Next != NULL)\r
439                         Pos = Pos->Next;\r
440                 Pos->Next = Pkt;\r
441         }\r
442         // 同時接続対応\r
443         if(NextTransPacketBase == NULL)\r
444                 NextTransPacketBase = TransPacketBase;\r
445 \r
446         while(Pkt != NULL)\r
447         {\r
448                 DispTransPacket(Pkt);\r
449 \r
450                 if((strncmp(Pkt->Cmd, "RETR", 4) == 0) ||\r
451                    (strncmp(Pkt->Cmd, "STOR", 4) == 0))\r
452                 {\r
453                         TransFiles++;\r
454                         PostMessage(GetMainHwnd(), WM_CHANGE_COND, 0, 0);\r
455                 }\r
456                 Pkt = Pkt->Next;\r
457         }\r
458 \r
459         ReleaseMutex(hListAccMutex);\r
460         // 同時接続対応\r
461         WaitForMainThread = NO;\r
462         return;\r
463 }\r
464 \r
465 \r
466 /*----- 転送ファイル情報を表示する --------------------------------------------\r
467 *\r
468 *       Parameter\r
469 *               TRANSPACKET *Pkt : 転送ファイル情報\r
470 *\r
471 *       Return Value\r
472 *               なし\r
473 *----------------------------------------------------------------------------*/\r
474 \r
475 static void DispTransPacket(TRANSPACKET *Pkt)\r
476 {\r
477         if((strncmp(Pkt->Cmd, "RETR", 4) == 0) || (strncmp(Pkt->Cmd, "STOR", 4) == 0))\r
478                 DoPrintf("TransList Cmd=%s : %s : %s", Pkt->Cmd, Pkt->RemoteFile, Pkt->LocalFile);\r
479         else if(strncmp(Pkt->Cmd, "R-", 2) == 0)\r
480                 DoPrintf("TransList Cmd=%s : %s", Pkt->Cmd, Pkt->RemoteFile);\r
481         else if(strncmp(Pkt->Cmd, "L-", 2) == 0)\r
482                 DoPrintf("TransList Cmd=%s : %s", Pkt->Cmd, Pkt->LocalFile);\r
483         else if(strncmp(Pkt->Cmd, "MKD", 3) == 0)\r
484         {\r
485                 if(strlen(Pkt->LocalFile) > 0)\r
486                         DoPrintf("TransList Cmd=%s : %s", Pkt->Cmd, Pkt->LocalFile);\r
487                 else\r
488                         DoPrintf("TransList Cmd=%s : %s", Pkt->Cmd, Pkt->RemoteFile);\r
489         }\r
490         else\r
491                 DoPrintf("TransList Cmd=%s", Pkt->Cmd);\r
492         return;\r
493 }\r
494 \r
495 \r
496 /*----- 転送ファイルリストをクリアする ----------------------------------------\r
497 *\r
498 *       Parameter\r
499 *               なし\r
500 *\r
501 *       Return Value\r
502 *               なし\r
503 *----------------------------------------------------------------------------*/\r
504 \r
505 static void EraseTransFileList(void)\r
506 {\r
507         TRANSPACKET *New;\r
508         TRANSPACKET *Next;\r
509         TRANSPACKET *NotDel;\r
510         TRANSPACKET Pkt;\r
511 \r
512         NotDel = NULL;\r
513 \r
514         // 同時接続対応\r
515 //      WaitForSingleObject(hListAccMutex, INFINITE);\r
516         while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
517         {\r
518                 WaitForMainThread = YES;\r
519                 BackgrndMessageProc();\r
520                 Sleep(1);\r
521         }\r
522         New = TransPacketBase;\r
523         while(New != NULL)\r
524         {\r
525                 /* 最後の"BACKCUR"は必要なので消さない */\r
526                 if(strcmp(New->Cmd, "BACKCUR") == 0)\r
527                 {\r
528                         if(NotDel != NULL)\r
529                                 // 同時接続対応\r
530 //                              free(NotDel);\r
531                                 strcpy(NotDel->Cmd, "");\r
532                         NotDel = New;\r
533                         New = New->Next;\r
534                         // 同時接続対応\r
535 //                      NotDel->Next = NULL;\r
536                 }\r
537                 else\r
538                 {\r
539                         Next = New->Next;\r
540                         // 同時接続対応\r
541 //                      free(New);\r
542                         strcpy(New->Cmd, "");\r
543                         New = Next;\r
544                 }\r
545         }\r
546         TransPacketBase = NotDel;\r
547         // 同時接続対応\r
548         NextTransPacketBase = TransPacketBase;\r
549         TransFiles = 0;\r
550         PostMessage(GetMainHwnd(), WM_CHANGE_COND, 0, 0);\r
551         ReleaseMutex(hListAccMutex);\r
552         // 同時接続対応\r
553         WaitForMainThread = NO;\r
554 \r
555         strcpy(Pkt.Cmd, "GOQUIT");\r
556         AddTransFileList(&Pkt);\r
557         return;\r
558 }\r
559 \r
560 \r
561 /*----- 転送中ダイアログを消さないようにするかどうかを設定 --------------------\r
562 *\r
563 *       Parameter\r
564 *               int Sw : 転送中ダイアログを消さないかどうか (YES/NO)\r
565 *\r
566 *       Return Value\r
567 *               なし\r
568 *----------------------------------------------------------------------------*/\r
569 \r
570 void KeepTransferDialog(int Sw)\r
571 {\r
572         KeepDlg = Sw;\r
573         return;\r
574 }\r
575 \r
576 \r
577 /*----- 現在転送中かどうかを返す ----------------------------------------------\r
578 *\r
579 *       Parameter\r
580 *               なし\r
581 *\r
582 *       Return Value\r
583 *               int ステータス (YES/NO=転送中ではない)\r
584 *----------------------------------------------------------------------------*/\r
585 \r
586 int AskTransferNow(void)\r
587 {\r
588         return(TransPacketBase != NULL ? YES : NO);\r
589 }\r
590 \r
591 \r
592 /*----- 転送するファイルの数を返す --------------------------------------------\r
593 *\r
594 *       Parameter\r
595 *               なし\r
596 *\r
597 *       Return Value\r
598 *               int 転送するファイルの数\r
599 *----------------------------------------------------------------------------*/\r
600 \r
601 int AskTransferFileNum(void)\r
602 {\r
603         return(TransFiles);\r
604 }\r
605 \r
606 \r
607 /*----- 転送中ウインドウを前面に出す ------------------------------------------\r
608 *\r
609 *       Parameter\r
610 *               なし\r
611 *\r
612 *       Return Value\r
613 *               なし\r
614 *----------------------------------------------------------------------------*/\r
615 \r
616 void GoForwardTransWindow(void)\r
617 {\r
618         MoveToForeground = YES;\r
619         return;\r
620 }\r
621 \r
622 \r
623 /*----- 転送ソケットのカレントディレクトリ情報を初期化 ------------------------\r
624 *\r
625 *       Parameter\r
626 *               なし\r
627 *\r
628 *       Return Value\r
629 *               なし\r
630 *----------------------------------------------------------------------------*/\r
631 \r
632 void InitTransCurDir(void)\r
633 {\r
634         // 同時接続対応\r
635 //      strcpy(CurDir, "");\r
636         int i;\r
637         for(i = 0; i < MAX_DATA_CONNECTION; i++)\r
638                 strcpy(CurDir[i], "");\r
639         return;\r
640 }\r
641 \r
642 \r
643 /*----- ファイル転送スレッドのメインループ ------------------------------------\r
644 *\r
645 *       Parameter\r
646 *               void *Dummy : 使わない\r
647 *\r
648 *       Return Value\r
649 *               なし\r
650 *----------------------------------------------------------------------------*/\r
651 \r
652 static ULONG WINAPI TransferThread(void *Dummy)\r
653 {\r
654         TRANSPACKET *Pos;\r
655         HWND hWndTrans;\r
656         char Tmp[FMAX_PATH+1];\r
657         int CwdSts;\r
658         int GoExit;\r
659 //      int Down;\r
660 //      int Up;\r
661         static int Down;\r
662         static int Up;\r
663         int DelNotify;\r
664         int ThreadCount;\r
665         SOCKET CmdSkt;\r
666         SOCKET NewCmdSkt;\r
667         SOCKET TrnSkt;\r
668         RECT WndRect;\r
669         int i;\r
670 \r
671         hWndTrans = NULL;\r
672         Down = NO;\r
673         Up = NO;\r
674         GoExit = NO;\r
675         DelNotify = NO;\r
676         // 同時接続対応\r
677         // ソケットは各転送スレッドが管理\r
678         ThreadCount = (int)Dummy;\r
679         CmdSkt = INVALID_SOCKET;\r
680         NewCmdSkt = INVALID_SOCKET;\r
681         TrnSkt = INVALID_SOCKET;\r
682         SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);\r
683 \r
684         while((TransPacketBase != NULL) ||\r
685                   (WaitForSingleObject(hRunMutex, 200) == WAIT_TIMEOUT))\r
686         {\r
687                 if(fTransferThreadExit == TRUE)\r
688                         break;\r
689 \r
690                 if(WaitForMainThread == YES)\r
691                 {\r
692                         BackgrndMessageProc();\r
693                         Sleep(100);\r
694                         continue;\r
695                 }\r
696 \r
697 //              WaitForSingleObject(hListAccMutex, INFINITE);\r
698                 while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
699                 {\r
700                         BackgrndMessageProc();\r
701                         Sleep(1);\r
702                 }\r
703 //              memset(ErrMsg, NUL, ERR_MSG_LEN+7);\r
704                 memset(GetErrMsg(), NUL, ERR_MSG_LEN+7);\r
705 \r
706 //              Canceled = NO;\r
707                 Canceled[ThreadCount] = NO;\r
708 \r
709                 while(TransPacketBase != NULL && strcmp(TransPacketBase->Cmd, "") == 0)\r
710                 {\r
711                         Pos = TransPacketBase;\r
712                         TransPacketBase = TransPacketBase->Next;\r
713                         free(Pos);\r
714                 }\r
715                 NewCmdSkt = AskCmdCtrlSkt();\r
716                 if(AskReuseCmdSkt() == YES && ThreadCount == 0)\r
717                 {\r
718                         if(TransPacketBase && ThreadCount < AskMaxThreadCount())\r
719                                 TrnSkt = AskTrnCtrlSkt();\r
720                 }\r
721                 else\r
722                 {\r
723                         if(TransPacketBase && NewCmdSkt != INVALID_SOCKET && ThreadCount < AskMaxThreadCount())\r
724                         {\r
725                                 if(TrnSkt == INVALID_SOCKET || NewCmdSkt != CmdSkt)\r
726                                 {\r
727                                         ReleaseMutex(hListAccMutex);\r
728                                         ReConnectTrnSkt(&TrnSkt, &Canceled[ThreadCount]);\r
729                                         // 同時ログイン数制限に引っかかった可能性あり\r
730                                         // 負荷を下げるためにしばらく待機\r
731                                         if(TrnSkt == INVALID_SOCKET)\r
732                                         {\r
733                                                 i = 10000;\r
734                                                 while(NewCmdSkt != CmdSkt && i > 0)\r
735                                                 {\r
736                                                         BackgrndMessageProc();\r
737                                                         Sleep(1);\r
738                                                         i--;\r
739                                                 }\r
740                                         }\r
741 //                                      WaitForSingleObject(hListAccMutex, INFINITE);\r
742                                         while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
743                                         {\r
744                                                 BackgrndMessageProc();\r
745                                                 Sleep(1);\r
746                                         }\r
747                                 }\r
748                         }\r
749                         else\r
750                         {\r
751                                 if(TrnSkt != INVALID_SOCKET)\r
752                                 {\r
753                                         ReleaseMutex(hListAccMutex);\r
754                                         SendData(TrnSkt, "QUIT\r\n", 6, 0, &Canceled[ThreadCount]);\r
755                                         DoClose(TrnSkt);\r
756                                         TrnSkt = INVALID_SOCKET;\r
757 //                                      WaitForSingleObject(hListAccMutex, INFINITE);\r
758                                         while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
759                                         {\r
760                                                 BackgrndMessageProc();\r
761                                                 Sleep(1);\r
762                                         }\r
763                                 }\r
764                         }\r
765                 }\r
766                 CmdSkt = NewCmdSkt;\r
767 //              if(TransPacketBase != NULL)\r
768                 if(TrnSkt != INVALID_SOCKET && NextTransPacketBase != NULL)\r
769                 {\r
770                         Pos = NextTransPacketBase;\r
771                         NextTransPacketBase = NextTransPacketBase->Next;\r
772                         // ディレクトリ操作は非同期で行わない\r
773 //                      ReleaseMutex(hListAccMutex);\r
774                         if(hWndTrans == NULL)\r
775                         {\r
776 //                              if((strncmp(TransPacketBase->Cmd, "RETR", 4) == 0) ||\r
777 //                                 (strncmp(TransPacketBase->Cmd, "STOR", 4) == 0) ||\r
778 //                                 (strncmp(TransPacketBase->Cmd, "MKD", 3) == 0) ||\r
779 //                                 (strncmp(TransPacketBase->Cmd, "L-", 2) == 0) ||\r
780 //                                 (strncmp(TransPacketBase->Cmd, "R-", 2) == 0))\r
781                                 if((strncmp(Pos->Cmd, "RETR", 4) == 0) ||\r
782                                    (strncmp(Pos->Cmd, "STOR", 4) == 0) ||\r
783                                    (strncmp(Pos->Cmd, "MKD", 3) == 0) ||\r
784                                    (strncmp(Pos->Cmd, "L-", 2) == 0) ||\r
785                                    (strncmp(Pos->Cmd, "R-", 2) == 0))\r
786                                 {\r
787                                         hWndTrans = CreateDialog(GetFtpInst(), MAKEINTRESOURCE(transfer_dlg), HWND_DESKTOP, (DLGPROC)TransDlgProc);\r
788                                         if(MoveToForeground == YES)\r
789                                                 SetForegroundWindow(hWndTrans);\r
790                                         ShowWindow(hWndTrans, SW_SHOWNOACTIVATE);\r
791                                         GetWindowRect(hWndTrans, &WndRect);\r
792                                         SetWindowPos(hWndTrans, NULL, WndRect.left, WndRect.top + (WndRect.bottom - WndRect.top) * ThreadCount - (WndRect.bottom - WndRect.top) * (AskMaxThreadCount() - 1) / 2, 0, 0, SWP_NOSIZE | SWP_NOZORDER);\r
793                                 }\r
794                         }\r
795 //                      TransPacketBase->hWndTrans = hWndTrans;\r
796                         Pos->hWndTrans = hWndTrans;\r
797                         Pos->ctrl_skt = TrnSkt;\r
798                         Pos->ThreadCount = ThreadCount;\r
799 \r
800                         if(hWndTrans != NULL)\r
801                         {\r
802                                 if(MoveToForeground == YES)\r
803                                 {\r
804                                         SetForegroundWindow(hWndTrans);\r
805                                         MoveToForeground = NO;\r
806                                 }\r
807                         }\r
808 \r
809                         if(hWndTrans != NULL)\r
810 //                              SendMessage(hWndTrans, WM_SET_PACKET, 0, (LPARAM)TransPacketBase);\r
811                                 SendMessage(hWndTrans, WM_SET_PACKET, 0, (LPARAM)Pos);\r
812 \r
813                         /* ダウンロード */\r
814 //                      if(strncmp(TransPacketBase->Cmd, "RETR", 4) == 0)\r
815                         if(strncmp(Pos->Cmd, "RETR", 4) == 0)\r
816                         {\r
817                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
818 //                              ReleaseMutex(hListAccMutex);\r
819                                 /* 不正なパスを検出 */\r
820 //                              if(CheckPathViolation(TransPacketBase) == NO)\r
821                                 if(CheckPathViolation(Pos) == NO)\r
822                                 {\r
823                                         /* フルパスを使わないための処理 */\r
824 //                                      if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
825                                         if(MakeNonFullPath(Pos, CurDir[Pos->ThreadCount], Tmp) == FFFTP_SUCCESS)\r
826                                         {\r
827 //                                              if(strncmp(TransPacketBase->Cmd, "RETR-S", 6) == 0)\r
828                                                 if(strncmp(Pos->Cmd, "RETR-S", 6) == 0)\r
829                                                 {\r
830                                                         /* サイズと日付を取得 */\r
831 //                                                      DoSIZE(TransPacketBase->RemoteFile, &TransPacketBase->Size);\r
832 //                                                      DoMDTM(TransPacketBase->RemoteFile, &TransPacketBase->Time);\r
833 //                                                      strcpy(TransPacketBase->Cmd, "RETR ");\r
834                                                         DoSIZE(TrnSkt, Pos->RemoteFile, &Pos->Size, &Canceled[Pos->ThreadCount]);\r
835                                                         DoMDTM(TrnSkt, Pos->RemoteFile, &Pos->Time, &Canceled[Pos->ThreadCount]);\r
836                                                         strcpy(Pos->Cmd, "RETR ");\r
837                                                 }\r
838 \r
839                                                 Down = YES;\r
840 //                                              if(DoDownLoad(AskTrnCtrlSkt(), TransPacketBase, NO) == 429)\r
841 //                                              {\r
842 //                                                      if(ReConnectTrnSkt() == FFFTP_SUCCESS)\r
843 //                                                              DoDownLoad(AskTrnCtrlSkt(), TransPacketBase, NO, &Canceled);\r
844                                                                 DoDownLoad(TrnSkt, Pos, NO, &Canceled[Pos->ThreadCount]);\r
845 //                                              }\r
846                                         }\r
847                                 }\r
848                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
849                                 ReleaseMutex(hListAccMutex);\r
850                         }\r
851                         /* アップロード */\r
852 //                      else if(strncmp(TransPacketBase->Cmd, "STOR", 4) == 0)\r
853                         else if(strncmp(Pos->Cmd, "STOR", 4) == 0)\r
854                         {\r
855                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
856 //                              ReleaseMutex(hListAccMutex);\r
857                                 /* フルパスを使わないための処理 */\r
858 //                              if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
859                                 if(MakeNonFullPath(Pos, CurDir[Pos->ThreadCount], Tmp) == FFFTP_SUCCESS)\r
860                                 {\r
861                                         Up = YES;\r
862 //                                      if(DoUpLoad(AskTrnCtrlSkt(), TransPacketBase) == 429)\r
863 //                                      {\r
864 //                                              if(ReConnectTrnSkt() == FFFTP_SUCCESS)\r
865 //                                                      DoUpLoad(AskTrnCtrlSkt(), TransPacketBase);\r
866                                                         DoUpLoad(TrnSkt, Pos);\r
867 //                                      }\r
868                                 }\r
869                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
870                                 ReleaseMutex(hListAccMutex);\r
871                         }\r
872                         /* フォルダ作成(ローカルまたはホスト) */\r
873 //                      else if(strncmp(TransPacketBase->Cmd, "MKD", 3) == 0)\r
874                         else if(strncmp(Pos->Cmd, "MKD", 3) == 0)\r
875                         {\r
876 //                              DispTransFileInfo(TransPacketBase, MSGJPN078, FALSE, YES);\r
877                                 DispTransFileInfo(Pos, MSGJPN078, FALSE, YES);\r
878 \r
879 //                              if(strlen(TransPacketBase->RemoteFile) > 0)\r
880                                 if(strlen(Pos->RemoteFile) > 0)\r
881                                 {\r
882                                         /* フルパスを使わないための処理 */\r
883                                         CwdSts = FTP_COMPLETE;\r
884 \r
885 //                                      strcpy(Tmp, TransPacketBase->RemoteFile);\r
886                                         strcpy(Tmp, Pos->RemoteFile);\r
887 //                                      if(ProcForNonFullpath(Tmp, CurDir, hWndTrans, 1) == FFFTP_FAIL)\r
888                                         if(ProcForNonFullpath(TrnSkt, Tmp, CurDir[Pos->ThreadCount], hWndTrans, &Canceled[Pos->ThreadCount]) == FFFTP_FAIL)\r
889                                         {\r
890                                                 ClearAll = YES;\r
891                                                 CwdSts = FTP_ERROR;\r
892                                         }\r
893 \r
894                                         if(CwdSts == FTP_COMPLETE)\r
895                                         {\r
896                                                 Up = YES;\r
897 //                                              CommandProcTrn(NULL, "MKD %s", Tmp);\r
898                                                 CommandProcTrn(TrnSkt, NULL, &Canceled[Pos->ThreadCount], "MKD %s", Tmp);\r
899                                                 /* すでにフォルダがある場合もあるので、 */\r
900                                                 /* ここではエラーチェックはしない */\r
901 \r
902                                         if(FolderAttr)\r
903 //                                              CommandProcTrn(NULL, "%s %03d %s", AskHostChmodCmd(), FolderAttrNum, Tmp);\r
904                                                 CommandProcTrn(TrnSkt, NULL, &Canceled[Pos->ThreadCount], "%s %03d %s", AskHostChmodCmd(), FolderAttrNum, Tmp);\r
905                                         }\r
906                                 }\r
907 //                              else if(strlen(TransPacketBase->LocalFile) > 0)\r
908                                 else if(strlen(Pos->LocalFile) > 0)\r
909                                 {\r
910                                         Down = YES;\r
911 //                                      DoLocalMKD(TransPacketBase->LocalFile);\r
912                                         DoLocalMKD(Pos->LocalFile);\r
913                                 }\r
914                                 ReleaseMutex(hListAccMutex);\r
915                         }\r
916                         /* ディレクトリ作成(常にホスト側) */\r
917 //                      else if(strncmp(TransPacketBase->Cmd, "R-MKD", 5) == 0)\r
918                         else if(strncmp(Pos->Cmd, "R-MKD", 5) == 0)\r
919                         {\r
920 //                              DispTransFileInfo(TransPacketBase, MSGJPN079, FALSE, YES);\r
921                                 DispTransFileInfo(Pos, MSGJPN079, FALSE, YES);\r
922 \r
923                                 /* フルパスを使わないための処理 */\r
924 //                              if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
925                                 if(MakeNonFullPath(Pos, CurDir[Pos->ThreadCount], Tmp) == FFFTP_SUCCESS)\r
926                                 {\r
927                                         Up = YES;\r
928 //                                      CommandProcTrn(NULL, "%s%s", TransPacketBase->Cmd+2, TransPacketBase->RemoteFile);\r
929                                         CommandProcTrn(TrnSkt, NULL, &Canceled[Pos->ThreadCount], "%s%s", Pos->Cmd+2, Pos->RemoteFile);\r
930 \r
931                                         if(FolderAttr)\r
932 //                                              CommandProcTrn(NULL, "%s %03d %s", AskHostChmodCmd(), FolderAttrNum, TransPacketBase->RemoteFile);\r
933                                                 CommandProcTrn(TrnSkt, NULL, &Canceled[Pos->ThreadCount], "%s %03d %s", AskHostChmodCmd(), FolderAttrNum, Pos->RemoteFile);\r
934                                 }\r
935                                 ReleaseMutex(hListAccMutex);\r
936                         }\r
937                         /* ディレクトリ削除(常にホスト側) */\r
938 //                      else if(strncmp(TransPacketBase->Cmd, "R-RMD", 5) == 0)\r
939                         else if(strncmp(Pos->Cmd, "R-RMD", 5) == 0)\r
940                         {\r
941 //                              DispTransFileInfo(TransPacketBase, MSGJPN080, FALSE, YES);\r
942                                 DispTransFileInfo(Pos, MSGJPN080, FALSE, YES);\r
943 \r
944 //                              DelNotify = MirrorDelNotify(WIN_REMOTE, DelNotify, TransPacketBase);\r
945                                 DelNotify = MirrorDelNotify(WIN_REMOTE, DelNotify, Pos);\r
946                                 if((DelNotify == YES) || (DelNotify == YES_ALL))\r
947                                 {\r
948                                         /* フルパスを使わないための処理 */\r
949 //                                      if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
950                                         if(MakeNonFullPath(Pos, CurDir[Pos->ThreadCount], Tmp) == FFFTP_SUCCESS)\r
951                                         {\r
952                                                 Up = YES;\r
953 //                                              CommandProcTrn(NULL, "%s%s", TransPacketBase->Cmd+2, TransPacketBase->RemoteFile);\r
954                                                 CommandProcTrn(TrnSkt, NULL, &Canceled[Pos->ThreadCount], "%s%s", Pos->Cmd+2, Pos->RemoteFile);\r
955                                         }\r
956                                 }\r
957                                 ReleaseMutex(hListAccMutex);\r
958                         }\r
959                         /* ファイル削除(常にホスト側) */\r
960 //                      else if(strncmp(TransPacketBase->Cmd, "R-DELE", 6) == 0)\r
961                         else if(strncmp(Pos->Cmd, "R-DELE", 6) == 0)\r
962                         {\r
963 //                              DispTransFileInfo(TransPacketBase, MSGJPN081, FALSE, YES);\r
964                                 DispTransFileInfo(Pos, MSGJPN081, FALSE, YES);\r
965 \r
966 //                              DelNotify = MirrorDelNotify(WIN_REMOTE, DelNotify, TransPacketBase);\r
967                                 DelNotify = MirrorDelNotify(WIN_REMOTE, DelNotify, Pos);\r
968                                 if((DelNotify == YES) || (DelNotify == YES_ALL))\r
969                                 {\r
970                                         /* フルパスを使わないための処理 */\r
971 //                                      if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
972                                         if(MakeNonFullPath(Pos, CurDir[Pos->ThreadCount], Tmp) == FFFTP_SUCCESS)\r
973                                         {\r
974                                                 Up = YES;\r
975 //                                              CommandProcTrn(NULL, "%s%s", TransPacketBase->Cmd+2, TransPacketBase->RemoteFile);\r
976                                                 CommandProcTrn(TrnSkt, NULL, &Canceled[Pos->ThreadCount], "%s%s", Pos->Cmd+2, Pos->RemoteFile);\r
977                                         }\r
978                                 }\r
979                                 ReleaseMutex(hListAccMutex);\r
980                         }\r
981                         /* ディレクトリ作成(常にローカル側) */\r
982 //                      else if(strncmp(TransPacketBase->Cmd, "L-MKD", 5) == 0)\r
983                         else if(strncmp(Pos->Cmd, "L-MKD", 5) == 0)\r
984                         {\r
985 //                              DispTransFileInfo(TransPacketBase, MSGJPN082, FALSE, YES);\r
986                                 DispTransFileInfo(Pos, MSGJPN082, FALSE, YES);\r
987 \r
988                                 Down = YES;\r
989 //                              DoLocalMKD(TransPacketBase->LocalFile);\r
990                                 DoLocalMKD(Pos->LocalFile);\r
991                                 ReleaseMutex(hListAccMutex);\r
992                         }\r
993                         /* ディレクトリ削除(常にローカル側) */\r
994 //                      else if(strncmp(TransPacketBase->Cmd, "L-RMD", 5) == 0)\r
995                         else if(strncmp(Pos->Cmd, "L-RMD", 5) == 0)\r
996                         {\r
997 //                              DispTransFileInfo(TransPacketBase, MSGJPN083, FALSE, YES);\r
998                                 DispTransFileInfo(Pos, MSGJPN083, FALSE, YES);\r
999 \r
1000 //                              DelNotify = MirrorDelNotify(WIN_LOCAL, DelNotify, TransPacketBase);\r
1001                                 DelNotify = MirrorDelNotify(WIN_LOCAL, DelNotify, Pos);\r
1002                                 if((DelNotify == YES) || (DelNotify == YES_ALL))\r
1003                                 {\r
1004                                         Down = YES;\r
1005 //                                      DoLocalRMD(TransPacketBase->LocalFile);\r
1006                                         DoLocalRMD(Pos->LocalFile);\r
1007                                 }\r
1008                                 ReleaseMutex(hListAccMutex);\r
1009                         }\r
1010                         /* ファイル削除(常にローカル側) */\r
1011 //                      else if(strncmp(TransPacketBase->Cmd, "L-DELE", 6) == 0)\r
1012                         else if(strncmp(Pos->Cmd, "L-DELE", 6) == 0)\r
1013                         {\r
1014 //                              DispTransFileInfo(TransPacketBase, MSGJPN084, FALSE, YES);\r
1015                                 DispTransFileInfo(Pos, MSGJPN084, FALSE, YES);\r
1016 \r
1017 //                              DelNotify = MirrorDelNotify(WIN_LOCAL, DelNotify, TransPacketBase);\r
1018                                 DelNotify = MirrorDelNotify(WIN_LOCAL, DelNotify, Pos);\r
1019                                 if((DelNotify == YES) || (DelNotify == YES_ALL))\r
1020                                 {\r
1021                                         Down = YES;\r
1022 //                                      DoLocalDELE(TransPacketBase->LocalFile);\r
1023                                         DoLocalDELE(Pos->LocalFile);\r
1024                                 }\r
1025                                 ReleaseMutex(hListAccMutex);\r
1026                         }\r
1027                         /* カレントディレクトリを設定 */\r
1028 //                      else if(strcmp(TransPacketBase->Cmd, "SETCUR") == 0)\r
1029                         else if(strcmp(Pos->Cmd, "SETCUR") == 0)\r
1030                         {\r
1031 //                              if(AskShareProh() == YES)\r
1032                                 if(AskReuseCmdSkt() == NO || AskShareProh() == YES)\r
1033                                 {\r
1034 //                                      if(strcmp(CurDir, TransPacketBase->RemoteFile) != 0)\r
1035                                         if(strcmp(CurDir[Pos->ThreadCount], Pos->RemoteFile) != 0)\r
1036                                         {\r
1037 //                                              if(CommandProcTrn(NULL, "CWD %s", TransPacketBase->RemoteFile)/100 != FTP_COMPLETE)\r
1038                                                 if(CommandProcTrn(TrnSkt, NULL, &Canceled[Pos->ThreadCount], "CWD %s", Pos->RemoteFile)/100 != FTP_COMPLETE)\r
1039                                                 {\r
1040                                                         DispCWDerror(hWndTrans);\r
1041                                                         ClearAll = YES;\r
1042                                                 }\r
1043                                         }\r
1044                                 }\r
1045 //                              strcpy(CurDir, TransPacketBase->RemoteFile);\r
1046                                 strcpy(CurDir[Pos->ThreadCount], Pos->RemoteFile);\r
1047                                 ReleaseMutex(hListAccMutex);\r
1048                         }\r
1049                         /* カレントディレクトリを戻す */\r
1050 //                      else if(strcmp(TransPacketBase->Cmd, "BACKCUR") == 0)\r
1051                         else if(strcmp(Pos->Cmd, "BACKCUR") == 0)\r
1052                         {\r
1053 //                              if(AskShareProh() == NO)\r
1054                                 if(AskReuseCmdSkt() == YES && AskShareProh() == NO)\r
1055                                 {\r
1056 //                                      if(strcmp(CurDir, TransPacketBase->RemoteFile) != 0)\r
1057 //                                              CommandProcTrn(NULL, "CWD %s", TransPacketBase->RemoteFile);\r
1058 //                                      strcpy(CurDir, TransPacketBase->RemoteFile);\r
1059                                         if(strcmp(CurDir[Pos->ThreadCount], Pos->RemoteFile) != 0)\r
1060                                                 CommandProcTrn(TrnSkt, NULL, &Canceled[Pos->ThreadCount], "CWD %s", Pos->RemoteFile);\r
1061                                         strcpy(CurDir[Pos->ThreadCount], Pos->RemoteFile);\r
1062                                 }\r
1063                                 ReleaseMutex(hListAccMutex);\r
1064                         }\r
1065                         /* 自動終了のための通知 */\r
1066 //                      else if(strcmp(TransPacketBase->Cmd, "GOQUIT") == 0)\r
1067                         else if(strcmp(Pos->Cmd, "GOQUIT") == 0)\r
1068                         {\r
1069                                 ReleaseMutex(hListAccMutex);\r
1070                                 GoExit = YES;\r
1071                         }\r
1072                         else\r
1073                                 ReleaseMutex(hListAccMutex);\r
1074 \r
1075                         /*===== 1つの処理終わり =====*/\r
1076 \r
1077                         if(ForceAbort == NO)\r
1078                         {\r
1079 //                              WaitForSingleObject(hListAccMutex, INFINITE);\r
1080                                 while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
1081                                 {\r
1082                                         BackgrndMessageProc();\r
1083                                         Sleep(1);\r
1084                                 }\r
1085                                 if(ClearAll == YES)\r
1086 //                                      EraseTransFileList();\r
1087                                 {\r
1088                                         for(i = 0; i < MAX_DATA_CONNECTION; i++)\r
1089                                                 Canceled[i] = YES;\r
1090                                         EraseTransFileList();\r
1091                                         Pos = NULL;\r
1092                                 }\r
1093                                 else\r
1094                                 {\r
1095 //                                      if((strncmp(TransPacketBase->Cmd, "RETR", 4) == 0) ||\r
1096 //                                         (strncmp(TransPacketBase->Cmd, "STOR", 4) == 0))\r
1097                                         if((strncmp(Pos->Cmd, "RETR", 4) == 0) ||\r
1098                                            (strncmp(Pos->Cmd, "STOR", 4) == 0))\r
1099                                         {\r
1100 //                                              TransFiles--;\r
1101                                                 if(TransFiles > 0)\r
1102                                                         TransFiles--;\r
1103                                                 PostMessage(GetMainHwnd(), WM_CHANGE_COND, 0, 0);\r
1104                                         }\r
1105 //                                      Pos = TransPacketBase;\r
1106 //                                      TransPacketBase = TransPacketBase->Next;\r
1107 //                                      free(Pos);\r
1108                                 }\r
1109                                 ClearAll = NO;\r
1110                                 ReleaseMutex(hListAccMutex);\r
1111 \r
1112                                 if(BackgrndMessageProc() == YES)\r
1113                                 {\r
1114                                         WaitForSingleObject(hListAccMutex, INFINITE);\r
1115                                         EraseTransFileList();\r
1116                                         ReleaseMutex(hListAccMutex);\r
1117                                 }\r
1118                         }\r
1119                         if(hWndTrans != NULL)\r
1120                                 SendMessage(hWndTrans, WM_SET_PACKET, 0, 0);\r
1121                         if(Pos != NULL)\r
1122                                 strcpy(Pos->Cmd, "");\r
1123                 }\r
1124 //              else\r
1125                 else if(TransPacketBase == NULL)\r
1126                 {\r
1127                         DelNotify = NO;\r
1128 \r
1129                         ReleaseMutex(hListAccMutex);\r
1130                         if(KeepDlg == NO)\r
1131                         {\r
1132                                 if(hWndTrans != NULL)\r
1133                                 {\r
1134                                         DestroyWindow(hWndTrans);\r
1135                                         hWndTrans = NULL;\r
1136 \r
1137 //                                      if(GoExit == YES)\r
1138 //                                      {\r
1139 //                                              SoundPlay(SND_TRANS);\r
1140 //\r
1141 //                                              if(AskAutoExit() == NO)\r
1142 //                                              {\r
1143 //                                                      if(Down == YES)\r
1144 //                                                              PostMessage(GetMainHwnd(), WM_REFRESH_LOCAL_FLG, 0, 0);\r
1145 //                                                      if(Up == YES)\r
1146 //                                                              PostMessage(GetMainHwnd(), WM_REFRESH_REMOTE_FLG, 0, 0);\r
1147 //                                              }\r
1148 //                                              Down = NO;\r
1149 //                                              Up = NO;\r
1150 //                                      }\r
1151                                 }\r
1152                         }\r
1153                         BackgrndMessageProc();\r
1154 //                      Sleep(1);\r
1155                         Sleep(100);\r
1156 \r
1157                         if(GoExit == YES)\r
1158                         {\r
1159                                 SoundPlay(SND_TRANS);\r
1160                                 if(AskAutoExit() == NO)\r
1161                                 {\r
1162                                         if(Down == YES)\r
1163                                                 PostMessage(GetMainHwnd(), WM_REFRESH_LOCAL_FLG, 0, 0);\r
1164                                         if(Up == YES)\r
1165                                                 PostMessage(GetMainHwnd(), WM_REFRESH_REMOTE_FLG, 0, 0);\r
1166                                 }\r
1167                                 Down = NO;\r
1168                                 Up = NO;\r
1169                                 PostMessage(GetMainHwnd(), WM_COMMAND, MAKEWPARAM(MENU_AUTO_EXIT, 0), 0);\r
1170                                 GoExit = NO;\r
1171                         }\r
1172                 }\r
1173                 else\r
1174                 {\r
1175                         ReleaseMutex(hListAccMutex);\r
1176                         if(hWndTrans != NULL)\r
1177                         {\r
1178                                 DestroyWindow(hWndTrans);\r
1179                                 hWndTrans = NULL;\r
1180                         }\r
1181                         BackgrndMessageProc();\r
1182                         if(ThreadCount < AskMaxThreadCount())\r
1183                                 Sleep(1);\r
1184                         else\r
1185                                 Sleep(100);\r
1186                 }\r
1187         }\r
1188         if(AskReuseCmdSkt() == NO || ThreadCount > 0)\r
1189         {\r
1190                 if(TrnSkt != INVALID_SOCKET)\r
1191                 {\r
1192                         SendData(TrnSkt, "QUIT\r\n", 6, 0, &Canceled[ThreadCount]);\r
1193                         DoClose(TrnSkt);\r
1194                 }\r
1195         }\r
1196         return 0;\r
1197 }\r
1198 \r
1199 \r
1200 /*----- フルパスを使わないファイルアクセスの準備 ------------------------------\r
1201 *\r
1202 *       Parameter\r
1203 *               TRANSPACKET *Pkt : 転送パケット\r
1204 *               char *Cur : カレントディレクトリ\r
1205 *               char *Tmp : 作業用エリア\r
1206 *\r
1207 *       Return Value\r
1208 *               int ステータス(FFFTP_SUCCESS/FFFTP_FAIL)\r
1209 *\r
1210 *       Note\r
1211 *               フルパスを使わない時は、\r
1212 *                       このモジュール内で CWD を行ない、\r
1213 *                       Pkt->RemoteFile にファイル名のみ残す。(パス名は消す)\r
1214 *----------------------------------------------------------------------------*/\r
1215 \r
1216 // 同時接続対応\r
1217 static int MakeNonFullPath(TRANSPACKET *Pkt, char *Cur, char *Tmp)\r
1218 {\r
1219         int Sts;\r
1220 \r
1221 //      Sts = ProcForNonFullpath(Pkt->RemoteFile, Cur, Pkt->hWndTrans, 1);\r
1222         Sts = ProcForNonFullpath(Pkt->ctrl_skt, Pkt->RemoteFile, Cur, Pkt->hWndTrans, &Canceled[Pkt->ThreadCount]);\r
1223         if(Sts == FFFTP_FAIL)\r
1224                 ClearAll = YES;\r
1225 \r
1226         return(Sts);\r
1227 }\r
1228 \r
1229 \r
1230 \r
1231 \r
1232 /*----- ダウンロードを行なう --------------------------------------------------\r
1233 *\r
1234 *       Parameter\r
1235 *               SOCKET cSkt : コントロールソケット\r
1236 *               TRANSPACKET *Pkt : 転送ファイル情報\r
1237 *               int DirList : ディレクトリリストのダウンロード(YES/NO)\r
1238 *\r
1239 *       Return Value\r
1240 *               int 応答コード\r
1241 *\r
1242 *       Note\r
1243 *               このモジュールは、ファイル一覧の取得などを行なう際にメインのスレッド\r
1244 *               からも呼ばれる。メインのスレッドから呼ばれる時は Pkt->hWndTrans == NULL。\r
1245 *----------------------------------------------------------------------------*/\r
1246 \r
1247 int DoDownLoad(SOCKET cSkt, TRANSPACKET *Pkt, int DirList, int *CancelCheckWork)\r
1248 {\r
1249         int iRetCode;\r
1250         char Reply[ERR_MSG_LEN+7];\r
1251 \r
1252         Pkt->ctrl_skt = cSkt;\r
1253         if(IsSpecialDevice(GetFileName(Pkt->LocalFile)) == YES)\r
1254         {\r
1255                 iRetCode = 500;\r
1256                 SetTaskMsg(MSGJPN085, GetFileName(Pkt->LocalFile));\r
1257                 DispDownloadFinishMsg(Pkt, iRetCode);\r
1258         }\r
1259         else if(Pkt->Mode != EXIST_IGNORE)\r
1260         {\r
1261                 if(Pkt->Type == TYPE_I)\r
1262                         Pkt->KanjiCode = KANJI_NOCNV;\r
1263 \r
1264                 iRetCode = command(Pkt->ctrl_skt, Reply, CancelCheckWork, "TYPE %c", Pkt->Type);\r
1265                 if(iRetCode/100 < FTP_RETRY)\r
1266                 {\r
1267                         if(Pkt->hWndTrans != NULL)\r
1268                         {\r
1269                                 // 同時接続対応\r
1270 //                              AllTransSizeNow = 0;\r
1271                                 AllTransSizeNow[Pkt->ThreadCount] = 0;\r
1272 \r
1273                                 if(DirList == NO)\r
1274                                         DispTransFileInfo(Pkt, MSGJPN086, TRUE, YES);\r
1275                                 else\r
1276                                         DispTransFileInfo(Pkt, MSGJPN087, FALSE, NO);\r
1277                         }\r
1278 \r
1279                         if(BackgrndMessageProc() == NO)\r
1280                         {\r
1281                                 if(AskPasvMode() != YES)\r
1282                                         iRetCode = DownLoadNonPassive(Pkt, CancelCheckWork);\r
1283                                 else\r
1284                                         iRetCode = DownLoadPassive(Pkt, CancelCheckWork);\r
1285                         }\r
1286                         else\r
1287                                 iRetCode = 500;\r
1288                 }\r
1289                 else\r
1290                         SetErrorMsg(Reply);\r
1291         }\r
1292         else\r
1293         {\r
1294                 DispTransFileInfo(Pkt, MSGJPN088, TRUE, YES);\r
1295                 SetTaskMsg(MSGJPN089, Pkt->RemoteFile);\r
1296                 iRetCode = 200;\r
1297         }\r
1298         return(iRetCode);\r
1299 }\r
1300 \r
1301 \r
1302 /*----- 通常モードでファイルをダウンロード ------------------------------------\r
1303 *\r
1304 *       Parameter\r
1305 *               TRANSPACKET *Pkt : 転送ファイル情報\r
1306 *\r
1307 *       Return Value\r
1308 *               int 応答コード\r
1309 *----------------------------------------------------------------------------*/\r
1310 \r
1311 static int DownLoadNonPassive(TRANSPACKET *Pkt, int *CancelCheckWork)\r
1312 {\r
1313         int iRetCode;\r
1314         int iLength;\r
1315         SOCKET data_socket = INVALID_SOCKET;   // data channel socket\r
1316         SOCKET listen_socket = INVALID_SOCKET; // data listen socket\r
1317         char Buf[1024];\r
1318         int CreateMode;\r
1319         struct sockaddr_in saSockAddr1;\r
1320         char Reply[ERR_MSG_LEN+7];\r
1321 \r
1322         if((listen_socket = GetFTPListenSocket(Pkt->ctrl_skt, CancelCheckWork)) != INVALID_SOCKET)\r
1323         {\r
1324                 if(SetDownloadResume(Pkt, Pkt->Mode, Pkt->ExistSize, &CreateMode, CancelCheckWork) == YES)\r
1325                 {\r
1326                         sprintf(Buf, "%s%s", Pkt->Cmd, Pkt->RemoteFile);\r
1327                         iRetCode = command(Pkt->ctrl_skt, Reply, CancelCheckWork, "%s", Buf);\r
1328                         if(iRetCode/100 == FTP_PRELIM)\r
1329                         {\r
1330                                 // 同時接続対応\r
1331 //                              if(SocksGet2ndBindReply(listen_socket, &data_socket) == FFFTP_FAIL)\r
1332                                 if(SocksGet2ndBindReply(listen_socket, &data_socket, CancelCheckWork) == FFFTP_FAIL)\r
1333                                 {\r
1334                                         iLength = sizeof(saSockAddr1);\r
1335                                         data_socket = do_accept(listen_socket, (struct sockaddr *)&saSockAddr1, (int *)&iLength);\r
1336 \r
1337                                         if(shutdown(listen_socket, 1) != 0)\r
1338                                                 ReportWSError("shutdown listen", WSAGetLastError());\r
1339                                         listen_socket = DoClose(listen_socket);\r
1340 \r
1341                                         if(data_socket == INVALID_SOCKET)\r
1342                                         {\r
1343                                                 SetErrorMsg(MSGJPN280);\r
1344                                                 ReportWSError("accept", WSAGetLastError());\r
1345                                                 iRetCode = 500;\r
1346                                         }\r
1347                                         else\r
1348                                                 DoPrintf("Skt=%u : accept from %s port %u", data_socket, inet_ntoa(saSockAddr1.sin_addr), ntohs(saSockAddr1.sin_port));\r
1349                                 }\r
1350 \r
1351                                 if(data_socket != INVALID_SOCKET)\r
1352                                 {\r
1353                                         // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
1354                                         ReleaseMutex(hListAccMutex);\r
1355                                         // FTPS対応\r
1356 //                                      iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1357                                         if(IsSSLAttached(Pkt->ctrl_skt))\r
1358                                         {\r
1359                                                 if(AttachSSL(data_socket, Pkt->ctrl_skt, CancelCheckWork))\r
1360                                                         iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1361                                                 else\r
1362                                                         iRetCode = 500;\r
1363                                         }\r
1364                                         else\r
1365                                                 iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1366 //                                      data_socket = DoClose(data_socket);\r
1367                                 }\r
1368                         }\r
1369                         else\r
1370                         {\r
1371                                 SetErrorMsg(Reply);\r
1372                                 SetTaskMsg(MSGJPN090);\r
1373                                 listen_socket = DoClose(listen_socket);\r
1374                                 iRetCode = 500;\r
1375                         }\r
1376                 }\r
1377                 else\r
1378                         iRetCode = 500;\r
1379         }\r
1380         else\r
1381         {\r
1382                 iRetCode = 500;\r
1383                 SetErrorMsg(MSGJPN279);\r
1384         }\r
1385         DispDownloadFinishMsg(Pkt, iRetCode);\r
1386 \r
1387         return(iRetCode);\r
1388 }\r
1389 \r
1390 \r
1391 /*----- Passiveモードでファイルをダウンロード ---------------------------------\r
1392 *\r
1393 *       Parameter\r
1394 *               TRANSPACKET *Pkt : 転送ファイル情報\r
1395 *\r
1396 *       Return Value\r
1397 *               int 応答コード\r
1398 *----------------------------------------------------------------------------*/\r
1399 \r
1400 static int DownLoadPassive(TRANSPACKET *Pkt, int *CancelCheckWork)\r
1401 {\r
1402         int iRetCode;\r
1403         SOCKET data_socket = INVALID_SOCKET;   // data channel socket\r
1404         char Buf[1024];\r
1405         int CreateMode;\r
1406         char Adrs[20];\r
1407         int Port;\r
1408         int Flg;\r
1409         char Reply[ERR_MSG_LEN+7];\r
1410 \r
1411         iRetCode = command(Pkt->ctrl_skt, Buf, CancelCheckWork, "PASV");\r
1412         if(iRetCode/100 == FTP_COMPLETE)\r
1413         {\r
1414                 if(GetAdrsAndPort(Buf, Adrs, &Port, 19) == FFFTP_SUCCESS)\r
1415                 {\r
1416                         if((data_socket = connectsock(Adrs, Port, MSGJPN091, CancelCheckWork)) != INVALID_SOCKET)\r
1417                         {\r
1418                                 // 変数が未初期化のバグ修正\r
1419                                 Flg = 1;\r
1420                                 if(setsockopt(data_socket, IPPROTO_TCP, TCP_NODELAY, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
1421                                         ReportWSError("setsockopt", WSAGetLastError());\r
1422 \r
1423                                 if(SetDownloadResume(Pkt, Pkt->Mode, Pkt->ExistSize, &CreateMode, CancelCheckWork) == YES)\r
1424                                 {\r
1425                                         sprintf(Buf, "%s%s", Pkt->Cmd, Pkt->RemoteFile);\r
1426                                         iRetCode = command(Pkt->ctrl_skt, Reply, CancelCheckWork, "%s", Buf);\r
1427                                         if(iRetCode/100 == FTP_PRELIM)\r
1428                                         {\r
1429                                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
1430                                                 ReleaseMutex(hListAccMutex);\r
1431                                                 // FTPS対応\r
1432 //                                              iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1433                                                 if(IsSSLAttached(Pkt->ctrl_skt))\r
1434                                                 {\r
1435                                                         if(AttachSSL(data_socket, Pkt->ctrl_skt, CancelCheckWork))\r
1436                                                                 iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1437                                                         else\r
1438                                                                 iRetCode = 500;\r
1439                                                 }\r
1440                                                 else\r
1441                                                         iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1442 //                                              data_socket = DoClose(data_socket);\r
1443                                         }\r
1444                                         else\r
1445                                         {\r
1446                                                 SetErrorMsg(Reply);\r
1447                                                 SetTaskMsg(MSGJPN092);\r
1448                                                 data_socket = DoClose(data_socket);\r
1449                                                 iRetCode = 500;\r
1450                                         }\r
1451                                 }\r
1452                                 else\r
1453                                         iRetCode = 500;\r
1454                         }\r
1455                         else\r
1456                                 iRetCode = 500;\r
1457                 }\r
1458                 else\r
1459                 {\r
1460                         SetErrorMsg(MSGJPN093);\r
1461                         SetTaskMsg(MSGJPN093);\r
1462                         iRetCode = 500;\r
1463                 }\r
1464         }\r
1465         else\r
1466                 SetErrorMsg(Buf);\r
1467 \r
1468         DispDownloadFinishMsg(Pkt, iRetCode);\r
1469 \r
1470         return(iRetCode);\r
1471 }\r
1472 \r
1473 \r
1474 /*----- ダウンロードの実行 ----------------------------------------------------\r
1475 *\r
1476 *       Parameter\r
1477 *               TRANSPACKET *Pkt : 転送ファイル情報\r
1478 *               SOCKET dSkt : データソケット\r
1479 *               int CreateMode : ファイル作成モード (CREATE_ALWAYS/OPEN_ALWAYS)\r
1480 *\r
1481 *       Return Value\r
1482 *               int 応答コード\r
1483 *\r
1484 *       Note\r
1485 *               転送の経過表示は\r
1486 *                       ダイアログを出す(Pkt->hWndTrans!=NULL)場合、インターバルタイマで経過を表示する\r
1487 *                       ダイアログを出さない場合、このルーチンからDispDownloadSize()を呼ぶ\r
1488 *----------------------------------------------------------------------------*/\r
1489 \r
1490 static int DownLoadFile(TRANSPACKET *Pkt, SOCKET dSkt, int CreateMode, int *CancelCheckWork)\r
1491 {\r
1492         int iNumBytes;\r
1493         char Buf[BUFSIZE];\r
1494         char Buf2[BUFSIZE+3];\r
1495         HANDLE iFileHandle;\r
1496         SECURITY_ATTRIBUTES Sec;\r
1497         DWORD Writed;\r
1498         CODECONVINFO cInfo;\r
1499         int Continue;\r
1500 //      fd_set ReadFds;\r
1501 //      struct timeval Tout;\r
1502 //      struct timeval *ToutPtr;\r
1503         int iRetCode;\r
1504         int TimeOutErr;\r
1505         char TmpBuf[ONELINE_BUF_SIZE];\r
1506         DWORD dwFileAttributes;\r
1507 \r
1508 #ifdef SET_BUFFER_SIZE\r
1509 /* Add by H.Shirouzu at 2002/10/02 */\r
1510         int buf_size = SOCKBUF_SIZE;\r
1511         for ( ; buf_size > 0; buf_size /= 2)\r
1512                 if (setsockopt(dSkt, SOL_SOCKET, SO_RCVBUF, (char *)&buf_size, sizeof(buf_size)) == 0)\r
1513                         break;\r
1514 /* End */\r
1515 #endif\r
1516 \r
1517         // 念のため受信バッファを無効にする\r
1518 #ifdef DISABLE_TRANSFER_NETWORK_BUFFERS\r
1519         int buf_size = 0;\r
1520         setsockopt(dSkt, SOL_SOCKET, SO_RCVBUF, (char *)&buf_size, sizeof(buf_size));\r
1521 #endif\r
1522 \r
1523         Pkt->Abort = ABORT_NONE;\r
1524 \r
1525         Sec.nLength = sizeof(SECURITY_ATTRIBUTES);\r
1526         Sec.lpSecurityDescriptor = NULL;\r
1527         Sec.bInheritHandle = FALSE;\r
1528 \r
1529         dwFileAttributes = GetFileAttributes(Pkt->LocalFile);\r
1530         if (dwFileAttributes != INVALID_FILE_ATTRIBUTES && (dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {\r
1531                 // 読み取り専用\r
1532                 if (MessageBox(GetMainHwnd(), MSGJPN296, MSGJPN086, MB_YESNO) == IDYES) {\r
1533                         // 属性を解除\r
1534                         SetFileAttributes(Pkt->LocalFile, dwFileAttributes ^ FILE_ATTRIBUTE_READONLY);\r
1535                 }\r
1536         }\r
1537 \r
1538         if((iFileHandle = CreateFile(Pkt->LocalFile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &Sec, CreateMode, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)\r
1539         {\r
1540                 // UTF-8対応\r
1541                 char Buf3[(BUFSIZE + 3) * 4];\r
1542                 CODECONVINFO cInfo2;\r
1543                 int ProcessedBOM = NO;\r
1544                 // 4GB超対応(kaokunさん提供)\r
1545                 DWORD High = 0;\r
1546                 if(CreateMode == OPEN_ALWAYS)\r
1547                         // 4GB超対応(kaokunさん提供)\r
1548 //                      SetFilePointer(iFileHandle, 0, 0, FILE_END);\r
1549                         SetFilePointer(iFileHandle, 0, &High, FILE_END);\r
1550 \r
1551                 if(Pkt->hWndTrans != NULL)\r
1552                 {\r
1553                         // 同時接続対応\r
1554 //                      TimeStart = time(NULL);\r
1555                         TimeStart[Pkt->ThreadCount] = time(NULL);\r
1556                         SetTimer(Pkt->hWndTrans, TIMER_DISPLAY, DISPLAY_TIMING, NULL);\r
1557                 }\r
1558 \r
1559                 InitCodeConvInfo(&cInfo);\r
1560                 cInfo.KanaCnv = Pkt->KanaCnv;\r
1561 \r
1562                 InitCodeConvInfo(&cInfo2);\r
1563                 cInfo2.KanaCnv = Pkt->KanaCnv;\r
1564 \r
1565                 /*===== ファイルを受信するループ =====*/\r
1566                 while((Pkt->Abort == ABORT_NONE) && (ForceAbort == NO))\r
1567                 {\r
1568 //                      FD_ZERO(&ReadFds);\r
1569 //                      FD_SET(dSkt, &ReadFds);\r
1570 //                      ToutPtr = NULL;\r
1571 //                      if(TimeOut != 0)\r
1572 //                      {\r
1573 //                              Tout.tv_sec = TimeOut;\r
1574 //                              Tout.tv_usec = 0;\r
1575 //                              ToutPtr = &Tout;\r
1576 //                      }\r
1577 //                      iNumBytes = select(0, &ReadFds, NULL, NULL, ToutPtr);\r
1578 //                      if(iNumBytes == SOCKET_ERROR)\r
1579 //                      {\r
1580 //                              ReportWSError("select", WSAGetLastError());\r
1581 //                              if(Pkt->Abort == ABORT_NONE)\r
1582 //                                      Pkt->Abort = ABORT_ERROR;\r
1583 //                              break;\r
1584 //                      }\r
1585 //                      else if(iNumBytes == 0)\r
1586 //                      {\r
1587 //                              SetErrorMsg(MSGJPN094);\r
1588 //                              SetTaskMsg(MSGJPN094);\r
1589 //                              Pkt->Abort = ABORT_ERROR;\r
1590 //                              break;\r
1591 //                      }\r
1592 \r
1593                         if((iNumBytes = do_recv(dSkt, Buf, BUFSIZE, 0, &TimeOutErr, CancelCheckWork)) <= 0)\r
1594                         {\r
1595                                 if(TimeOutErr == YES)\r
1596                                 {\r
1597                                         SetErrorMsg(MSGJPN094);\r
1598                                         SetTaskMsg(MSGJPN094);\r
1599                                         if(Pkt->hWndTrans != NULL)\r
1600                                                 ClearAll = YES;\r
1601                                         if(Pkt->Abort == ABORT_NONE)\r
1602                                                 Pkt->Abort = ABORT_ERROR;\r
1603                                 }\r
1604                                 else if(iNumBytes == SOCKET_ERROR)\r
1605                                 {\r
1606                                         if(Pkt->Abort == ABORT_NONE)\r
1607                                                 Pkt->Abort = ABORT_ERROR;\r
1608                                 }\r
1609                                 break;\r
1610                         }\r
1611 \r
1612                         /* 漢字コード変換 */\r
1613                         if(Pkt->KanjiCode != KANJI_NOCNV)\r
1614                         {\r
1615                                 cInfo.Str = Buf;\r
1616                                 cInfo.StrLen = iNumBytes;\r
1617                                 cInfo.Buf = Buf2;\r
1618                                 cInfo.BufSize = BUFSIZE+3;\r
1619                                 do\r
1620                                 {\r
1621                                         // ここで全てUTF-8へ変換する\r
1622                                         // TODO: SJIS以外も直接UTF-8へ変換\r
1623 //                                      if(Pkt->KanjiCode == KANJI_JIS)\r
1624 //                                              Continue = ConvJIStoSJIS(&cInfo);\r
1625 //                                      else\r
1626 //                                              Continue = ConvEUCtoSJIS(&cInfo);\r
1627                                         char Buf3[(BUFSIZE + 3) * 4];\r
1628                                         CODECONVINFO cInfo2;\r
1629                                         switch(Pkt->KanjiCode)\r
1630                                         {\r
1631                                         case KANJI_SJIS:\r
1632                                                 switch(Pkt->KanjiCodeDesired)\r
1633                                                 {\r
1634                                                 case KANJI_SJIS:\r
1635 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1636 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
1637 //                                                      Continue = NO;\r
1638                                                         // カナ変換のため\r
1639                                                         Continue = ConvSJIStoJIS(&cInfo);\r
1640                                                         cInfo2.Str = cInfo.Buf;\r
1641                                                         cInfo2.StrLen = cInfo.OutLen;\r
1642                                                         cInfo2.Buf = Buf3;\r
1643                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1644                                                         ConvJIStoSJIS(&cInfo2);\r
1645                                                         break;\r
1646                                                 case KANJI_JIS:\r
1647                                                         Continue = ConvSJIStoJIS(&cInfo);\r
1648                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1649                                                         cInfo2.OutLen = cInfo.OutLen;\r
1650                                                         break;\r
1651                                                 case KANJI_EUC:\r
1652                                                         Continue = ConvSJIStoEUC(&cInfo);\r
1653                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1654                                                         cInfo2.OutLen = cInfo.OutLen;\r
1655                                                         break;\r
1656                                                 case KANJI_UTF8N:\r
1657                                                         Continue = ConvSJIStoUTF8N(&cInfo);\r
1658                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1659                                                         cInfo2.OutLen = cInfo.OutLen;\r
1660                                                         break;\r
1661                                                 case KANJI_UTF8BOM:\r
1662                                                         if(ProcessedBOM == NO)\r
1663                                                         {\r
1664                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
1665                                                                 cInfo2.OutLen = 3;\r
1666                                                                 Continue = YES;\r
1667                                                                 ProcessedBOM = YES;\r
1668                                                                 break;\r
1669                                                         }\r
1670                                                         Continue = ConvSJIStoUTF8N(&cInfo);\r
1671                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1672                                                         cInfo2.OutLen = cInfo.OutLen;\r
1673                                                         break;\r
1674                                                 }\r
1675                                                 break;\r
1676                                         case KANJI_JIS:\r
1677                                                 switch(Pkt->KanjiCodeDesired)\r
1678                                                 {\r
1679                                                 case KANJI_SJIS:\r
1680                                                         Continue = ConvJIStoSJIS(&cInfo);\r
1681                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1682                                                         cInfo2.OutLen = cInfo.OutLen;\r
1683                                                         break;\r
1684                                                 case KANJI_JIS:\r
1685 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1686 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
1687 //                                                      Continue = NO;\r
1688                                                         // カナ変換のため\r
1689                                                         Continue = ConvJIStoSJIS(&cInfo);\r
1690                                                         cInfo2.Str = cInfo.Buf;\r
1691                                                         cInfo2.StrLen = cInfo.OutLen;\r
1692                                                         cInfo2.Buf = Buf3;\r
1693                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1694                                                         ConvSJIStoJIS(&cInfo2);\r
1695                                                         break;\r
1696                                                 case KANJI_EUC:\r
1697                                                         Continue = ConvJIStoSJIS(&cInfo);\r
1698                                                         cInfo2.Str = cInfo.Buf;\r
1699                                                         cInfo2.StrLen = cInfo.OutLen;\r
1700                                                         cInfo2.Buf = Buf3;\r
1701                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1702                                                         ConvSJIStoEUC(&cInfo2);\r
1703                                                         break;\r
1704                                                 case KANJI_UTF8N:\r
1705                                                         Continue = ConvJIStoSJIS(&cInfo);\r
1706                                                         cInfo2.Str = cInfo.Buf;\r
1707                                                         cInfo2.StrLen = cInfo.OutLen;\r
1708                                                         cInfo2.Buf = Buf3;\r
1709                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1710                                                         ConvSJIStoUTF8N(&cInfo2);\r
1711                                                         break;\r
1712                                                 case KANJI_UTF8BOM:\r
1713                                                         if(ProcessedBOM == NO)\r
1714                                                         {\r
1715                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
1716                                                                 cInfo2.OutLen = 3;\r
1717                                                                 Continue = YES;\r
1718                                                                 ProcessedBOM = YES;\r
1719                                                                 break;\r
1720                                                         }\r
1721                                                         Continue = ConvJIStoSJIS(&cInfo);\r
1722                                                         cInfo2.Str = cInfo.Buf;\r
1723                                                         cInfo2.StrLen = cInfo.OutLen;\r
1724                                                         cInfo2.Buf = Buf3;\r
1725                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1726                                                         ConvSJIStoUTF8N(&cInfo2);\r
1727                                                         break;\r
1728                                                 }\r
1729                                                 break;\r
1730                                         case KANJI_EUC:\r
1731                                                 switch(Pkt->KanjiCodeDesired)\r
1732                                                 {\r
1733                                                 case KANJI_SJIS:\r
1734                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
1735                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1736                                                         cInfo2.OutLen = cInfo.OutLen;\r
1737                                                         break;\r
1738                                                 case KANJI_JIS:\r
1739                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
1740                                                         cInfo2.Str = cInfo.Buf;\r
1741                                                         cInfo2.StrLen = cInfo.OutLen;\r
1742                                                         cInfo2.Buf = Buf3;\r
1743                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1744                                                         ConvSJIStoJIS(&cInfo2);\r
1745                                                         break;\r
1746                                                 case KANJI_EUC:\r
1747 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1748 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
1749 //                                                      Continue = NO;\r
1750                                                         // カナ変換のため\r
1751                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
1752                                                         cInfo2.Str = cInfo.Buf;\r
1753                                                         cInfo2.StrLen = cInfo.OutLen;\r
1754                                                         cInfo2.Buf = Buf3;\r
1755                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1756                                                         ConvSJIStoEUC(&cInfo2);\r
1757                                                         break;\r
1758                                                 case KANJI_UTF8N:\r
1759                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
1760                                                         cInfo2.Str = cInfo.Buf;\r
1761                                                         cInfo2.StrLen = cInfo.OutLen;\r
1762                                                         cInfo2.Buf = Buf3;\r
1763                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1764                                                         ConvSJIStoUTF8N(&cInfo2);\r
1765                                                         break;\r
1766                                                 case KANJI_UTF8BOM:\r
1767                                                         if(ProcessedBOM == NO)\r
1768                                                         {\r
1769                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
1770                                                                 cInfo2.OutLen = 3;\r
1771                                                                 Continue = YES;\r
1772                                                                 ProcessedBOM = YES;\r
1773                                                                 break;\r
1774                                                         }\r
1775                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
1776                                                         cInfo2.Str = cInfo.Buf;\r
1777                                                         cInfo2.StrLen = cInfo.OutLen;\r
1778                                                         cInfo2.Buf = Buf3;\r
1779                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1780                                                         ConvSJIStoUTF8N(&cInfo2);\r
1781                                                         break;\r
1782                                                 }\r
1783                                                 break;\r
1784                                         case KANJI_UTF8N:\r
1785                                                 switch(Pkt->KanjiCodeDesired)\r
1786                                                 {\r
1787                                                 case KANJI_SJIS:\r
1788                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
1789                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1790                                                         cInfo2.OutLen = cInfo.OutLen;\r
1791                                                         break;\r
1792                                                 case KANJI_JIS:\r
1793                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
1794                                                         cInfo2.Str = cInfo.Buf;\r
1795                                                         cInfo2.StrLen = cInfo.OutLen;\r
1796                                                         cInfo2.Buf = Buf3;\r
1797                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1798                                                         ConvSJIStoJIS(&cInfo2);\r
1799                                                         break;\r
1800                                                 case KANJI_EUC:\r
1801                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
1802                                                         cInfo2.Str = cInfo.Buf;\r
1803                                                         cInfo2.StrLen = cInfo.OutLen;\r
1804                                                         cInfo2.Buf = Buf3;\r
1805                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1806                                                         ConvSJIStoEUC(&cInfo2);\r
1807                                                         break;\r
1808                                                 case KANJI_UTF8N:\r
1809                                                         memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1810                                                         cInfo2.OutLen = cInfo.StrLen;\r
1811                                                         Continue = NO;\r
1812                                                         break;\r
1813                                                 case KANJI_UTF8BOM:\r
1814                                                         if(ProcessedBOM == NO)\r
1815                                                         {\r
1816                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
1817                                                                 cInfo2.OutLen = 3;\r
1818                                                                 Continue = YES;\r
1819                                                                 ProcessedBOM = YES;\r
1820                                                                 break;\r
1821                                                         }\r
1822                                                         memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1823                                                         cInfo2.OutLen = cInfo.StrLen;\r
1824                                                         Continue = NO;\r
1825                                                         break;\r
1826                                                 }\r
1827                                                 break;\r
1828                                         case KANJI_UTF8BOM:\r
1829                                                 if(ProcessedBOM == NO)\r
1830                                                 {\r
1831                                                         if(memcmp(Buf, "\xEF\xBB\xBF", 3) == 0)\r
1832                                                         {\r
1833                                                                 cInfo.Str += 3;\r
1834                                                                 cInfo.StrLen -= 3;\r
1835                                                         }\r
1836                                                         cInfo2.OutLen = 0;\r
1837                                                         switch(Pkt->KanjiCodeDesired)\r
1838                                                         {\r
1839                                                         case KANJI_UTF8BOM:\r
1840                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
1841                                                                 cInfo2.OutLen = 3;\r
1842                                                                 break;\r
1843                                                         }\r
1844                                                         Continue = YES;\r
1845                                                         ProcessedBOM = YES;\r
1846                                                         break;\r
1847                                                 }\r
1848                                                 switch(Pkt->KanjiCodeDesired)\r
1849                                                 {\r
1850                                                 case KANJI_SJIS:\r
1851                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
1852                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1853                                                         cInfo2.OutLen = cInfo.OutLen;\r
1854                                                         break;\r
1855                                                 case KANJI_JIS:\r
1856                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
1857                                                         cInfo2.Str = cInfo.Buf;\r
1858                                                         cInfo2.StrLen = cInfo.OutLen;\r
1859                                                         cInfo2.Buf = Buf3;\r
1860                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1861                                                         ConvSJIStoJIS(&cInfo2);\r
1862                                                         break;\r
1863                                                 case KANJI_EUC:\r
1864                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
1865                                                         cInfo2.Str = cInfo.Buf;\r
1866                                                         cInfo2.StrLen = cInfo.OutLen;\r
1867                                                         cInfo2.Buf = Buf3;\r
1868                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1869                                                         ConvSJIStoEUC(&cInfo2);\r
1870                                                         break;\r
1871                                                 case KANJI_UTF8N:\r
1872                                                         memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1873                                                         cInfo2.OutLen = cInfo.StrLen;\r
1874                                                         Continue = NO;\r
1875                                                         break;\r
1876                                                 case KANJI_UTF8BOM:\r
1877                                                         memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1878                                                         cInfo2.OutLen = cInfo.StrLen;\r
1879                                                         Continue = NO;\r
1880                                                         break;\r
1881                                                 }\r
1882                                                 break;\r
1883                                         }\r
1884 //                                      if(WriteFile(iFileHandle, Buf2, cInfo.OutLen, &Writed, NULL) == FALSE)\r
1885                                         if(WriteFile(iFileHandle, Buf3, cInfo2.OutLen, &Writed, NULL) == FALSE)\r
1886                                                 Pkt->Abort = ABORT_DISKFULL;\r
1887                                 }\r
1888                                 while((Continue == YES) && (Pkt->Abort == ABORT_NONE));\r
1889                         }\r
1890                         else\r
1891                         {\r
1892                                 if(WriteFile(iFileHandle, Buf, iNumBytes, &Writed, NULL) == FALSE)\r
1893                                         Pkt->Abort = ABORT_DISKFULL;\r
1894                         }\r
1895 \r
1896                         Pkt->ExistSize += iNumBytes;\r
1897                         if(Pkt->hWndTrans != NULL)\r
1898                                 // 同時接続対応\r
1899 //                              AllTransSizeNow += iNumBytes;\r
1900                                 AllTransSizeNow[Pkt->ThreadCount] += iNumBytes;\r
1901                         else\r
1902                         {\r
1903                                 /* 転送ダイアログを出さない時の経過表示 */\r
1904                                 DispDownloadSize(Pkt->ExistSize);\r
1905                         }\r
1906 \r
1907                         if(BackgrndMessageProc() == YES)\r
1908                                 ForceAbort = YES;\r
1909                 }\r
1910 \r
1911                 /* 書き残したデータを書き込む */\r
1912                 if(Pkt->KanjiCode != KANJI_NOCNV)\r
1913                 {\r
1914                         cInfo.Buf = Buf2;\r
1915                         cInfo.BufSize = BUFSIZE+3;\r
1916                         FlushRestData(&cInfo);\r
1917                         switch(Pkt->KanjiCode)\r
1918                         {\r
1919                         case KANJI_SJIS:\r
1920                                 switch(Pkt->KanjiCodeDesired)\r
1921                                 {\r
1922                                 case KANJI_SJIS:\r
1923                                         // カナ変換のため\r
1924                                         cInfo2.Str = cInfo.Buf;\r
1925                                         cInfo2.StrLen = cInfo.OutLen;\r
1926                                         cInfo2.Buf = Buf3;\r
1927                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1928                                         ConvJIStoSJIS(&cInfo2);\r
1929                                         break;\r
1930                                 case KANJI_JIS:\r
1931                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1932                                         cInfo2.OutLen = cInfo.OutLen;\r
1933                                         break;\r
1934                                 case KANJI_EUC:\r
1935                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1936                                         cInfo2.OutLen = cInfo.OutLen;\r
1937                                         break;\r
1938                                 case KANJI_UTF8N:\r
1939                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1940                                         cInfo2.OutLen = cInfo.OutLen;\r
1941                                         break;\r
1942                                 case KANJI_UTF8BOM:\r
1943                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1944                                         cInfo2.OutLen = cInfo.OutLen;\r
1945                                         break;\r
1946                                 }\r
1947                                 break;\r
1948                         case KANJI_JIS:\r
1949                                 switch(Pkt->KanjiCodeDesired)\r
1950                                 {\r
1951                                 case KANJI_SJIS:\r
1952                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1953                                         cInfo2.OutLen = cInfo.OutLen;\r
1954                                         break;\r
1955                                 case KANJI_JIS:\r
1956                                         // カナ変換のため\r
1957                                         cInfo2.Str = cInfo.Buf;\r
1958                                         cInfo2.StrLen = cInfo.OutLen;\r
1959                                         cInfo2.Buf = Buf3;\r
1960                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1961                                         ConvSJIStoJIS(&cInfo2);\r
1962                                         break;\r
1963                                 case KANJI_EUC:\r
1964                                         cInfo2.Str = cInfo.Buf;\r
1965                                         cInfo2.StrLen = cInfo.OutLen;\r
1966                                         cInfo2.Buf = Buf3;\r
1967                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1968                                         ConvSJIStoEUC(&cInfo2);\r
1969                                         break;\r
1970                                 case KANJI_UTF8N:\r
1971                                         cInfo2.Str = cInfo.Buf;\r
1972                                         cInfo2.StrLen = cInfo.OutLen;\r
1973                                         cInfo2.Buf = Buf3;\r
1974                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1975                                         ConvSJIStoUTF8N(&cInfo2);\r
1976                                         break;\r
1977                                 case KANJI_UTF8BOM:\r
1978                                         cInfo2.Str = cInfo.Buf;\r
1979                                         cInfo2.StrLen = cInfo.OutLen;\r
1980                                         cInfo2.Buf = Buf3;\r
1981                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1982                                         ConvSJIStoUTF8N(&cInfo2);\r
1983                                         break;\r
1984                                 }\r
1985                                 break;\r
1986                         case KANJI_EUC:\r
1987                                 switch(Pkt->KanjiCodeDesired)\r
1988                                 {\r
1989                                 case KANJI_SJIS:\r
1990                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1991                                         cInfo2.OutLen = cInfo.OutLen;\r
1992                                         break;\r
1993                                 case KANJI_JIS:\r
1994                                         cInfo2.Str = cInfo.Buf;\r
1995                                         cInfo2.StrLen = cInfo.OutLen;\r
1996                                         cInfo2.Buf = Buf3;\r
1997                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1998                                         ConvSJIStoJIS(&cInfo2);\r
1999                                         break;\r
2000                                 case KANJI_EUC:\r
2001                                         // カナ変換のため\r
2002                                         cInfo2.Str = cInfo.Buf;\r
2003                                         cInfo2.StrLen = cInfo.OutLen;\r
2004                                         cInfo2.Buf = Buf3;\r
2005                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2006                                         ConvSJIStoEUC(&cInfo2);\r
2007                                         break;\r
2008                                 case KANJI_UTF8N:\r
2009                                         cInfo2.Str = cInfo.Buf;\r
2010                                         cInfo2.StrLen = cInfo.OutLen;\r
2011                                         cInfo2.Buf = Buf3;\r
2012                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2013                                         ConvSJIStoUTF8N(&cInfo2);\r
2014                                         break;\r
2015                                 case KANJI_UTF8BOM:\r
2016                                         cInfo2.Str = cInfo.Buf;\r
2017                                         cInfo2.StrLen = cInfo.OutLen;\r
2018                                         cInfo2.Buf = Buf3;\r
2019                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2020                                         ConvSJIStoUTF8N(&cInfo2);\r
2021                                         break;\r
2022                                 }\r
2023                                 break;\r
2024                         case KANJI_UTF8N:\r
2025                                 switch(Pkt->KanjiCodeDesired)\r
2026                                 {\r
2027                                 case KANJI_SJIS:\r
2028                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2029                                         cInfo2.OutLen = cInfo.OutLen;\r
2030                                         break;\r
2031                                 case KANJI_JIS:\r
2032                                         cInfo2.Str = cInfo.Buf;\r
2033                                         cInfo2.StrLen = cInfo.OutLen;\r
2034                                         cInfo2.Buf = Buf3;\r
2035                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2036                                         ConvSJIStoJIS(&cInfo2);\r
2037                                         break;\r
2038                                 case KANJI_EUC:\r
2039                                         cInfo2.Str = cInfo.Buf;\r
2040                                         cInfo2.StrLen = cInfo.OutLen;\r
2041                                         cInfo2.Buf = Buf3;\r
2042                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2043                                         ConvSJIStoEUC(&cInfo2);\r
2044                                         break;\r
2045                                 case KANJI_UTF8N:\r
2046                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2047                                         cInfo2.OutLen = cInfo.OutLen;\r
2048                                         break;\r
2049                                 case KANJI_UTF8BOM:\r
2050                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2051                                         cInfo2.OutLen = cInfo.OutLen;\r
2052                                         break;\r
2053                                 }\r
2054                                 break;\r
2055                         case KANJI_UTF8BOM:\r
2056                                 switch(Pkt->KanjiCodeDesired)\r
2057                                 {\r
2058                                 case KANJI_SJIS:\r
2059                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2060                                         cInfo2.OutLen = cInfo.OutLen;\r
2061                                         break;\r
2062                                 case KANJI_JIS:\r
2063                                         cInfo2.Str = cInfo.Buf;\r
2064                                         cInfo2.StrLen = cInfo.OutLen;\r
2065                                         cInfo2.Buf = Buf3;\r
2066                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2067                                         ConvSJIStoJIS(&cInfo2);\r
2068                                         break;\r
2069                                 case KANJI_EUC:\r
2070                                         cInfo2.Str = cInfo.Buf;\r
2071                                         cInfo2.StrLen = cInfo.OutLen;\r
2072                                         cInfo2.Buf = Buf3;\r
2073                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2074                                         ConvSJIStoEUC(&cInfo2);\r
2075                                         break;\r
2076                                 case KANJI_UTF8N:\r
2077                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2078                                         cInfo2.OutLen = cInfo.OutLen;\r
2079                                         break;\r
2080                                 case KANJI_UTF8BOM:\r
2081                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2082                                         cInfo2.OutLen = cInfo.OutLen;\r
2083                                         break;\r
2084                                 }\r
2085                                 break;\r
2086                         }\r
2087 //                      if(WriteFile(iFileHandle, Buf2, cInfo.OutLen, &Writed, NULL) == FALSE)\r
2088                         if(WriteFile(iFileHandle, Buf3, cInfo2.OutLen, &Writed, NULL) == FALSE)\r
2089                                 Pkt->Abort = ABORT_DISKFULL;\r
2090                         cInfo2.Buf = Buf3;\r
2091                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2092                         FlushRestData(&cInfo2);\r
2093                         if(WriteFile(iFileHandle, Buf3, cInfo2.OutLen, &Writed, NULL) == FALSE)\r
2094                                 Pkt->Abort = ABORT_DISKFULL;\r
2095                 }\r
2096 \r
2097                 /* グラフ表示を更新 */\r
2098                 if(Pkt->hWndTrans != NULL)\r
2099                 {\r
2100                         KillTimer(Pkt->hWndTrans, TIMER_DISPLAY);\r
2101                         DispTransferStatus(Pkt->hWndTrans, YES, Pkt);\r
2102                         // 同時接続対応\r
2103 //                      TimeStart = time(NULL) - TimeStart + 1;\r
2104                         TimeStart[Pkt->ThreadCount] = time(NULL) - TimeStart[Pkt->ThreadCount] + 1;\r
2105                 }\r
2106                 else\r
2107                 {\r
2108                         /* 転送ダイアログを出さない時の経過表示を消す */\r
2109                         DispDownloadSize(-1);\r
2110                 }\r
2111 \r
2112                 /* ファイルのタイムスタンプを合わせる */\r
2113                 if((SaveTimeStamp == YES) &&\r
2114                    ((Pkt->Time.dwLowDateTime != 0) || (Pkt->Time.dwHighDateTime != 0)))\r
2115                 {\r
2116                         SetFileTime(iFileHandle, &Pkt->Time, &Pkt->Time, &Pkt->Time);\r
2117                 }\r
2118 \r
2119                 CloseHandle(iFileHandle);\r
2120 \r
2121                 if(iNumBytes == SOCKET_ERROR)\r
2122                         ReportWSError("recv",WSAGetLastError());\r
2123         }\r
2124         else\r
2125         {\r
2126                 SetErrorMsg(MSGJPN095, Pkt->LocalFile);\r
2127                 SetTaskMsg(MSGJPN095, Pkt->LocalFile);\r
2128                 Pkt->Abort = ABORT_ERROR;\r
2129         }\r
2130 \r
2131 \r
2132         if(shutdown(dSkt, 1) != 0)\r
2133                 ReportWSError("shutdown", WSAGetLastError());\r
2134 \r
2135         DoClose(dSkt);\r
2136 \r
2137         if(ForceAbort == NO)\r
2138         {\r
2139                 /* Abortをホストに伝える */\r
2140                 if(Pkt->Abort != ABORT_NONE && iFileHandle != INVALID_HANDLE_VALUE)\r
2141                 {\r
2142                         SendData(Pkt->ctrl_skt, "\xFF\xF4\xFF", 3, MSG_OOB, CancelCheckWork);   /* MSG_OOBに注意 */\r
2143                         SendData(Pkt->ctrl_skt, "\xF2", 1, 0, CancelCheckWork);\r
2144                         command(Pkt->ctrl_skt, NULL, CancelCheckWork, "ABOR");\r
2145                 }\r
2146         }\r
2147 \r
2148         iRetCode = ReadReplyMessage(Pkt->ctrl_skt, Buf, 1024, CancelCheckWork, TmpBuf);\r
2149 \r
2150 //#pragma aaa\r
2151 //DoPrintf("##DOWN REPLY : %s", Buf);\r
2152 \r
2153         if(Pkt->Abort == ABORT_DISKFULL)\r
2154         {\r
2155                 SetErrorMsg(MSGJPN096);\r
2156                 SetTaskMsg(MSGJPN096);\r
2157         }\r
2158         if(iRetCode >= FTP_RETRY)\r
2159                 SetErrorMsg(Buf);\r
2160         if(Pkt->Abort != ABORT_NONE)\r
2161                 iRetCode = 500;\r
2162 \r
2163         return(iRetCode);\r
2164 }\r
2165 \r
2166 \r
2167 /*----- ダウンロード終了/中止時のメッセージを表示 ----------------------------\r
2168 *\r
2169 *       Parameter\r
2170 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2171 *               int iRetCode : 応答コード\r
2172 *\r
2173 *       Return Value\r
2174 *               なし\r
2175 *----------------------------------------------------------------------------*/\r
2176 \r
2177 static void DispDownloadFinishMsg(TRANSPACKET *Pkt, int iRetCode)\r
2178 {\r
2179         char Fname[FMAX_PATH+1];\r
2180 \r
2181         if(ForceAbort == NO)\r
2182         {\r
2183                 if((iRetCode/100) >= FTP_CONTINUE)\r
2184                 {\r
2185                         strcpy(Fname, Pkt->RemoteFile);\r
2186 \r
2187 #if defined(HAVE_OPENVMS)\r
2188                         /* OpenVMSの場合、空ディレクトリへ移動すると550 File not foundになって\r
2189                          * エラーダイアログやエラーメッセージが出るので何もしない */\r
2190                         if (AskHostType() == HTYPE_VMS)\r
2191                                 return;\r
2192 #endif\r
2193 \r
2194                         if((strncmp(Pkt->Cmd, "NLST", 4) == 0) || (strncmp(Pkt->Cmd, "LIST", 4) == 0))\r
2195                         {\r
2196                                 SetTaskMsg(MSGJPN097);\r
2197                                 strcpy(Fname, MSGJPN098);\r
2198                         }\r
2199                         // 同時接続対応\r
2200 //                      else if((Pkt->hWndTrans != NULL) && (TimeStart != 0))\r
2201 //                              SetTaskMsg(MSGJPN099, TimeStart, Pkt->ExistSize/TimeStart);\r
2202                         else if((Pkt->hWndTrans != NULL) && (TimeStart[Pkt->ThreadCount] != 0))\r
2203                                 SetTaskMsg(MSGJPN099, TimeStart[Pkt->ThreadCount], Pkt->ExistSize/TimeStart[Pkt->ThreadCount]);\r
2204                         else\r
2205                                 SetTaskMsg(MSGJPN100);\r
2206 \r
2207                         if(Pkt->Abort != ABORT_USER)\r
2208                         {\r
2209                                 // 全て中止を選択後にダイアログが表示されるバグ対策\r
2210 //                              if(DispUpDownErrDialog(downerr_dlg, Pkt->hWndTrans, Fname) == NO)\r
2211                                 if(Canceled[Pkt->ThreadCount] == NO && ClearAll == NO && DispUpDownErrDialog(downerr_dlg, Pkt->hWndTrans, Fname) == NO)\r
2212                                         ClearAll = YES;\r
2213                         }\r
2214                 }\r
2215                 else\r
2216                 {\r
2217                         if((strncmp(Pkt->Cmd, "NLST", 4) == 0) || (strncmp(Pkt->Cmd, "LIST", 4) == 0))\r
2218                                 SetTaskMsg(MSGJPN101, Pkt->ExistSize);\r
2219                         // 同時接続対応\r
2220 //                      else if((Pkt->hWndTrans != NULL) && (TimeStart != 0))\r
2221 //                              SetTaskMsg(MSGJPN102, TimeStart, Pkt->ExistSize/TimeStart);\r
2222                         else if((Pkt->hWndTrans != NULL) && (TimeStart[Pkt->ThreadCount] != 0))\r
2223                                 // "0 B/S"と表示されるバグを修正\r
2224                                 // 原因は%dにあたる部分に64ビット値が渡されているため\r
2225 //                              SetTaskMsg(MSGJPN102, TimeStart[Pkt->ThreadCount], Pkt->ExistSize/TimeStart[Pkt->ThreadCount]);\r
2226                                 SetTaskMsg(MSGJPN102, (LONG)TimeStart[Pkt->ThreadCount], (LONG)(Pkt->ExistSize/TimeStart[Pkt->ThreadCount]));\r
2227                         else\r
2228                                 SetTaskMsg(MSGJPN103, Pkt->ExistSize);\r
2229                 }\r
2230         }\r
2231         return;\r
2232 }\r
2233 \r
2234 \r
2235 /*----- ダウンロード/アップロードエラーのダイアログを表示 --------------------\r
2236 *\r
2237 *       Parameter\r
2238 *               int RedID : ダイアログボックスのリソースID\r
2239 *               HWND hWnd : 書き込み中ダイアログのウインドウ\r
2240 *               char *Fname : ファイル名\r
2241 *\r
2242 *       Return Value\r
2243 *               int ステータス (YES=中止/NO=全て中止)\r
2244 *----------------------------------------------------------------------------*/\r
2245 \r
2246 static int DispUpDownErrDialog(int ResID, HWND hWnd, char *Fname)\r
2247 {\r
2248         if(hWnd == NULL)\r
2249                 hWnd = GetMainHwnd();\r
2250 \r
2251         SoundPlay(SND_ERROR);\r
2252         return(DialogBoxParam(GetFtpInst(), MAKEINTRESOURCE(ResID), hWnd, UpDownErrorDialogProc, (LPARAM)Fname));\r
2253 }\r
2254 \r
2255 \r
2256 /*----- ダウンロードエラー/アップロードエラーダイアログのコールバック --------\r
2257 *\r
2258 *       Parameter\r
2259 *               HWND hDlg : ウインドウハンドル\r
2260 *               UINT message : メッセージ番号\r
2261 *               WPARAM wParam : メッセージの WPARAM 引数\r
2262 *               LPARAM lParam : メッセージの LPARAM 引数\r
2263 *\r
2264 *       Return Value\r
2265 *               BOOL TRUE/FALSE\r
2266 *----------------------------------------------------------------------------*/\r
2267 \r
2268 static BOOL CALLBACK UpDownErrorDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
2269 {\r
2270         switch (message)\r
2271         {\r
2272                 case WM_INITDIALOG :\r
2273                         SendDlgItemMessage(hDlg, UPDOWN_ERR_FNAME, WM_SETTEXT, 0, (LPARAM)lParam);\r
2274                         // 同時接続対応\r
2275 //                      SendDlgItemMessage(hDlg, UPDOWN_ERR_MSG, WM_SETTEXT, 0, (LPARAM)ErrMsg);\r
2276                         SendDlgItemMessage(hDlg, UPDOWN_ERR_MSG, WM_SETTEXT, 0, (LPARAM)GetErrMsg());\r
2277                         return(TRUE);\r
2278 \r
2279                 case WM_COMMAND :\r
2280                         switch(GET_WM_COMMAND_ID(wParam, lParam))\r
2281                         {\r
2282                                 case IDOK :\r
2283                                         EndDialog(hDlg, YES);\r
2284                                         break;\r
2285 \r
2286                                 case IDCANCEL :\r
2287                                         EndDialog(hDlg, NO);\r
2288                                         break;\r
2289                         }\r
2290                         return(TRUE);\r
2291         }\r
2292     return(FALSE);\r
2293 }\r
2294 \r
2295 \r
2296 /*----- ダウンロードのリジュームの準備を行う ----------------------------------\r
2297 *\r
2298 *       Parameter\r
2299 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2300 *               iont ProcMode : 処理モード(EXIST_xxx)\r
2301 *               LONGLONG Size : ロード済みのファイルのサイズ\r
2302 *               int *Mode : ファイル作成モード (CREATE_xxxx)\r
2303 *\r
2304 *       Return Value\r
2305 *               int 転送を行うかどうか(YES/NO=このファイルを中止/NO_ALL=全て中止)\r
2306 *\r
2307 *       Note\r
2308 *               Pkt->ExistSizeのセットを行なう\r
2309 *----------------------------------------------------------------------------*/\r
2310 \r
2311 static int SetDownloadResume(TRANSPACKET *Pkt, int ProcMode, LONGLONG Size, int *Mode, int *CancelCheckWork)\r
2312 {\r
2313         int iRetCode;\r
2314         int Com;\r
2315         char Reply[ERR_MSG_LEN+7];\r
2316         char Tmp[40];\r
2317 \r
2318         Com = YES;\r
2319 \r
2320         Pkt->ExistSize = 0;\r
2321         *Mode = CREATE_ALWAYS;\r
2322 \r
2323         if(ProcMode == EXIST_RESUME)\r
2324         {\r
2325                 iRetCode = command(Pkt->ctrl_skt, Reply, CancelCheckWork, "REST %s", MakeNumString(Size, Tmp, FALSE));\r
2326                 if(iRetCode/100 < FTP_RETRY)\r
2327                 {\r
2328                         /* リジューム */\r
2329                         if(Pkt->hWndTrans != NULL)\r
2330                                 Pkt->ExistSize = Size;\r
2331                         *Mode = OPEN_ALWAYS;\r
2332                 }\r
2333                 else\r
2334                 {\r
2335                         Com = DialogBox(GetFtpInst(), MAKEINTRESOURCE(noresume_dlg), Pkt->hWndTrans, NoResumeWndProc);\r
2336                         if(Com != YES)\r
2337                         {\r
2338                                 if(Com == NO_ALL)               /* 全て中止 */\r
2339                                         ClearAll = YES;\r
2340                                 Pkt->Abort = ABORT_USER;\r
2341                         }\r
2342                 }\r
2343         }\r
2344         return(Com);\r
2345 }\r
2346 \r
2347 \r
2348 /*----- resumeエラーダイアログのコールバック ----------------------------------\r
2349 *\r
2350 *       Parameter\r
2351 *               HWND hDlg : ウインドウハンドル\r
2352 *               UINT message : メッセージ番号\r
2353 *               WPARAM wParam : メッセージの WPARAM 引数\r
2354 *               LPARAM lParam : メッセージの LPARAM 引数\r
2355 *\r
2356 *       Return Value\r
2357 *               BOOL TRUE/FALSE\r
2358 *----------------------------------------------------------------------------*/\r
2359 \r
2360 static BOOL CALLBACK NoResumeWndProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)\r
2361 {\r
2362         switch (iMessage)\r
2363         {\r
2364                 case WM_INITDIALOG :\r
2365                         return(TRUE);\r
2366 \r
2367                 case WM_COMMAND :\r
2368                         switch(GET_WM_COMMAND_ID(wParam, lParam))\r
2369                         {\r
2370                                 case IDOK :\r
2371                                         EndDialog(hDlg, YES);\r
2372                                         break;\r
2373 \r
2374                                 case IDCANCEL :\r
2375                                         EndDialog(hDlg, NO);\r
2376                                         break;\r
2377 \r
2378                                 case RESUME_CANCEL_ALL :\r
2379                                         EndDialog(hDlg, NO_ALL);\r
2380                                         break;\r
2381                         }\r
2382             return(TRUE);\r
2383         }\r
2384         return(FALSE);\r
2385 }\r
2386 \r
2387 \r
2388 \r
2389 /*----- アップロードを行なう --------------------------------------------------\r
2390 *\r
2391 *       Parameter\r
2392 *               SOCKET cSkt : コントロールソケット\r
2393 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2394 *\r
2395 *       Return Value\r
2396 *               int 応答コード\r
2397 *----------------------------------------------------------------------------*/\r
2398 \r
2399 static int DoUpLoad(SOCKET cSkt, TRANSPACKET *Pkt)\r
2400 {\r
2401         int iRetCode;\r
2402         char Reply[ERR_MSG_LEN+7];\r
2403 \r
2404         Pkt->ctrl_skt = cSkt;\r
2405 \r
2406         if(Pkt->Mode != EXIST_IGNORE)\r
2407         {\r
2408                 if(CheckFileReadable(Pkt->LocalFile) == FFFTP_SUCCESS)\r
2409                 {\r
2410                         if(Pkt->Type == TYPE_I)\r
2411                                 Pkt->KanjiCode = KANJI_NOCNV;\r
2412 \r
2413                         // 同時接続対応\r
2414 //                      iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled, "TYPE %c", Pkt->Type);\r
2415                         iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled[Pkt->ThreadCount], "TYPE %c", Pkt->Type);\r
2416                         if(iRetCode/100 < FTP_RETRY)\r
2417                         {\r
2418                                 if(Pkt->Mode == EXIST_UNIQUE)\r
2419                                         strcpy(Pkt->Cmd, "STOU ");\r
2420 \r
2421                                 if(Pkt->hWndTrans != NULL)\r
2422                                         DispTransFileInfo(Pkt, MSGJPN104, TRUE, YES);\r
2423 \r
2424                                 if(BackgrndMessageProc() == NO)\r
2425                                 {\r
2426                                         if(AskPasvMode() != YES)\r
2427                                                 iRetCode = UpLoadNonPassive(Pkt);\r
2428                                         else\r
2429                                                 iRetCode = UpLoadPassive(Pkt);\r
2430                                 }\r
2431                                 else\r
2432                                         iRetCode = 500;\r
2433                         }\r
2434                         else\r
2435                                 SetErrorMsg(Reply);\r
2436 \r
2437                         /* 属性変更 */\r
2438                         if((Pkt->Attr != -1) && ((iRetCode/100) == FTP_COMPLETE))\r
2439                                 // 同時接続対応\r
2440 //                              command(Pkt->ctrl_skt, Reply, &Canceled, "%s %03X %s", AskHostChmodCmd(), Pkt->Attr, Pkt->RemoteFile);\r
2441                                 command(Pkt->ctrl_skt, Reply, &Canceled[Pkt->ThreadCount], "%s %03X %s", AskHostChmodCmd(), Pkt->Attr, Pkt->RemoteFile);\r
2442                 }\r
2443                 else\r
2444                 {\r
2445                         SetErrorMsg(MSGJPN105, Pkt->LocalFile);\r
2446                         SetTaskMsg(MSGJPN105, Pkt->LocalFile);\r
2447                         iRetCode = 500;\r
2448                         Pkt->Abort = ABORT_ERROR;\r
2449                         DispUploadFinishMsg(Pkt, iRetCode);\r
2450                 }\r
2451         }\r
2452         else\r
2453         {\r
2454                 DispTransFileInfo(Pkt, MSGJPN106, TRUE, YES);\r
2455                 SetTaskMsg(MSGJPN107, Pkt->LocalFile);\r
2456                 iRetCode = 200;\r
2457         }\r
2458         return(iRetCode);\r
2459 }\r
2460 \r
2461 \r
2462 /*----- 通常モードでファイルをアップロード ------------------------------------\r
2463 *\r
2464 *       Parameter\r
2465 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2466 *\r
2467 *       Return Value\r
2468 *               int 応答コード\r
2469 *----------------------------------------------------------------------------*/\r
2470 \r
2471 static int UpLoadNonPassive(TRANSPACKET *Pkt)\r
2472 {\r
2473         int iRetCode;\r
2474         int iLength;\r
2475         SOCKET data_socket = INVALID_SOCKET;   // data channel socket\r
2476         SOCKET listen_socket = INVALID_SOCKET; // data listen socket\r
2477         char Buf[1024];\r
2478         struct sockaddr_in saSockAddr1;\r
2479         int Resume;\r
2480         char Reply[ERR_MSG_LEN+7];\r
2481 \r
2482         // 同時接続対応\r
2483 //      if((listen_socket = GetFTPListenSocket(Pkt->ctrl_skt, &Canceled)) != INVALID_SOCKET)\r
2484         if((listen_socket = GetFTPListenSocket(Pkt->ctrl_skt, &Canceled[Pkt->ThreadCount])) != INVALID_SOCKET)\r
2485         {\r
2486                 SetUploadResume(Pkt, Pkt->Mode, Pkt->ExistSize, &Resume);\r
2487                 if(Resume == NO)\r
2488                         sprintf(Buf, "%s%s", Pkt->Cmd, Pkt->RemoteFile);\r
2489                 else\r
2490                         sprintf(Buf, "%s%s", "APPE ", Pkt->RemoteFile);\r
2491 \r
2492                 // 同時接続対応\r
2493 //              iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled, "%s", Buf);\r
2494                 iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled[Pkt->ThreadCount], "%s", Buf);\r
2495                 if((iRetCode/100) == FTP_PRELIM)\r
2496                 {\r
2497                         // 同時接続対応\r
2498 //                      if(SocksGet2ndBindReply(listen_socket, &data_socket) == FFFTP_FAIL)\r
2499                         if(SocksGet2ndBindReply(listen_socket, &data_socket, &Canceled[Pkt->ThreadCount]) == FFFTP_FAIL)\r
2500                         {\r
2501                                 iLength=sizeof(saSockAddr1);\r
2502                                 data_socket = do_accept(listen_socket,(struct sockaddr *)&saSockAddr1, (int *)&iLength);\r
2503 \r
2504                                 if(shutdown(listen_socket, 1) != 0)\r
2505                                         ReportWSError("shutdown listen", WSAGetLastError());\r
2506                                 listen_socket = DoClose(listen_socket);\r
2507 \r
2508                                 if(data_socket == INVALID_SOCKET)\r
2509                                 {\r
2510                                         SetErrorMsg(MSGJPN280);\r
2511                                         ReportWSError("accept", WSAGetLastError());\r
2512                                         iRetCode = 500;\r
2513                                 }\r
2514                                 else\r
2515                                         DoPrintf("Skt=%u : accept from %s port %u", data_socket, inet_ntoa(saSockAddr1.sin_addr), ntohs(saSockAddr1.sin_port));\r
2516                         }\r
2517 \r
2518                         if(data_socket != INVALID_SOCKET)\r
2519                         {\r
2520                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
2521                                 ReleaseMutex(hListAccMutex);\r
2522                                 // FTPS対応\r
2523 //                              iRetCode = UpLoadFile(Pkt, data_socket);\r
2524                                 if(IsSSLAttached(Pkt->ctrl_skt))\r
2525                                 {\r
2526                                         if(AttachSSL(data_socket, Pkt->ctrl_skt, &Canceled[Pkt->ThreadCount]))\r
2527                                                 iRetCode = UpLoadFile(Pkt, data_socket);\r
2528                                         else\r
2529                                                 iRetCode = 500;\r
2530                                 }\r
2531                                 else\r
2532                                         iRetCode = UpLoadFile(Pkt, data_socket);\r
2533                                 data_socket = DoClose(data_socket);\r
2534                         }\r
2535                 }\r
2536                 else\r
2537                 {\r
2538                         SetErrorMsg(Reply);\r
2539                         SetTaskMsg(MSGJPN108);\r
2540                         listen_socket = DoClose(listen_socket);\r
2541                         iRetCode = 500;\r
2542                 }\r
2543         }\r
2544         else\r
2545         {\r
2546                 SetErrorMsg(MSGJPN279);\r
2547                 iRetCode = 500;\r
2548         }\r
2549         DispUploadFinishMsg(Pkt, iRetCode);\r
2550 \r
2551         return(iRetCode);\r
2552 }\r
2553 \r
2554 \r
2555 /*----- Passiveモードでファイルをアップロード ---------------------------------\r
2556 *\r
2557 *       Parameter\r
2558 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2559 *\r
2560 *       Return Value\r
2561 *               int 応答コード\r
2562 *----------------------------------------------------------------------------*/\r
2563 \r
2564 static int UpLoadPassive(TRANSPACKET *Pkt)\r
2565 {\r
2566         int iRetCode;\r
2567         SOCKET data_socket = INVALID_SOCKET;   // data channel socket\r
2568         char Buf[1024];\r
2569         char Adrs[20];\r
2570         int Port;\r
2571         int Flg;\r
2572         int Resume;\r
2573         char Reply[ERR_MSG_LEN+7];\r
2574 \r
2575         // 同時接続対応\r
2576 //      iRetCode = command(Pkt->ctrl_skt, Buf, &Canceled, "PASV");\r
2577         iRetCode = command(Pkt->ctrl_skt, Buf, &Canceled[Pkt->ThreadCount], "PASV");\r
2578         if(iRetCode/100 == FTP_COMPLETE)\r
2579         {\r
2580                 if(GetAdrsAndPort(Buf, Adrs, &Port, 19) == FFFTP_SUCCESS)\r
2581                 {\r
2582                         // 同時接続対応\r
2583 //                      if((data_socket = connectsock(Adrs, Port, MSGJPN109, &Canceled)) != INVALID_SOCKET)\r
2584                         if((data_socket = connectsock(Adrs, Port, MSGJPN109, &Canceled[Pkt->ThreadCount])) != INVALID_SOCKET)\r
2585                         {\r
2586                                 // 変数が未初期化のバグ修正\r
2587                                 Flg = 1;\r
2588                                 if(setsockopt(data_socket, IPPROTO_TCP, TCP_NODELAY, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
2589                                         ReportWSError("setsockopt", WSAGetLastError());\r
2590 \r
2591                                 SetUploadResume(Pkt, Pkt->Mode, Pkt->ExistSize, &Resume);\r
2592                                 if(Resume == NO)\r
2593                                         sprintf(Buf, "%s%s", Pkt->Cmd, Pkt->RemoteFile);\r
2594                                 else\r
2595                                         sprintf(Buf, "%s%s", "APPE ", Pkt->RemoteFile);\r
2596 \r
2597                                 // 同時接続対応\r
2598 //                              iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled, "%s", Buf);\r
2599                                 iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled[Pkt->ThreadCount], "%s", Buf);\r
2600                                 if(iRetCode/100 == FTP_PRELIM)\r
2601                                 {\r
2602                                         // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
2603                                         ReleaseMutex(hListAccMutex);\r
2604                                         // FTPS対応\r
2605 //                                      iRetCode = UpLoadFile(Pkt, data_socket);\r
2606                                         if(IsSSLAttached(Pkt->ctrl_skt))\r
2607                                         {\r
2608                                                 if(AttachSSL(data_socket, Pkt->ctrl_skt, &Canceled[Pkt->ThreadCount]))\r
2609                                                         iRetCode = UpLoadFile(Pkt, data_socket);\r
2610                                                 else\r
2611                                                         iRetCode = 500;\r
2612                                         }\r
2613                                         else\r
2614                                                 iRetCode = UpLoadFile(Pkt, data_socket);\r
2615 \r
2616                                         data_socket = DoClose(data_socket);\r
2617                                 }\r
2618                                 else\r
2619                                 {\r
2620                                         SetErrorMsg(Reply);\r
2621                                         SetTaskMsg(MSGJPN110);\r
2622                                         data_socket = DoClose(data_socket);\r
2623                                         iRetCode = 500;\r
2624                                 }\r
2625                         }\r
2626                         else\r
2627                         {\r
2628                                 SetErrorMsg(MSGJPN281);\r
2629                                 iRetCode = 500;\r
2630                         }\r
2631                 }\r
2632                 else\r
2633                 {\r
2634                         SetErrorMsg(Buf);\r
2635                         SetTaskMsg(MSGJPN111);\r
2636                         iRetCode = 500;\r
2637                 }\r
2638         }\r
2639         else\r
2640                 SetErrorMsg(Buf);\r
2641 \r
2642         DispUploadFinishMsg(Pkt, iRetCode);\r
2643 \r
2644         return(iRetCode);\r
2645 }\r
2646 \r
2647 \r
2648 /*----- アップロードの実行 ----------------------------------------------------\r
2649 *\r
2650 *       Parameter\r
2651 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2652 *               SOCKET dSkt : データソケット\r
2653 *\r
2654 *       Return Value\r
2655 *               int 応答コード\r
2656 *\r
2657 *       Note\r
2658 *               転送の経過表示は、インターバルタイマで経過を表示する\r
2659 *               転送ダイアログを出さないでアップロードすることはない\r
2660 *----------------------------------------------------------------------------*/\r
2661 \r
2662 static int UpLoadFile(TRANSPACKET *Pkt, SOCKET dSkt)\r
2663 {\r
2664         DWORD iNumBytes;\r
2665         HANDLE iFileHandle;\r
2666         SECURITY_ATTRIBUTES Sec;\r
2667         char Buf[BUFSIZE];\r
2668         char Buf2[BUFSIZE+3];\r
2669         CODECONVINFO cInfo;\r
2670         TERMCODECONVINFO tInfo;\r
2671         int Continue;\r
2672         char *EofPos;\r
2673         int iRetCode;\r
2674 #if 0\r
2675         int TimeOutErr;\r
2676 #endif\r
2677         char TmpBuf[ONELINE_BUF_SIZE];\r
2678         DWORD Low;\r
2679         DWORD High;\r
2680 \r
2681 #ifdef SET_BUFFER_SIZE\r
2682 /* Add by H.Shirouzu at 2002/10/02 */\r
2683         int buf_size = SOCKBUF_SIZE;\r
2684         for ( ; buf_size > 0; buf_size /= 2)\r
2685                 if (setsockopt(dSkt, SOL_SOCKET, SO_SNDBUF, (char *)&buf_size, sizeof(buf_size)) == 0)\r
2686                         break;\r
2687 /* End */\r
2688 #endif\r
2689 \r
2690         // 念のため送信バッファを無効にする\r
2691 #ifdef DISABLE_TRANSFER_NETWORK_BUFFERS\r
2692         int buf_size = 0;\r
2693         setsockopt(dSkt, SOL_SOCKET, SO_SNDBUF, (char *)&buf_size, sizeof(buf_size));\r
2694 #endif\r
2695 \r
2696         Pkt->Abort = ABORT_NONE;\r
2697 \r
2698         Sec.nLength = sizeof(SECURITY_ATTRIBUTES);\r
2699         Sec.lpSecurityDescriptor = NULL;\r
2700         Sec.bInheritHandle = FALSE;\r
2701 \r
2702         if((iFileHandle = CreateFile(Pkt->LocalFile, GENERIC_READ,\r
2703                 FILE_SHARE_READ|FILE_SHARE_WRITE, &Sec, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE)\r
2704         {\r
2705                 // UTF-8対応\r
2706                 char Buf3[(BUFSIZE + 3) * 4];\r
2707                 CODECONVINFO cInfo2;\r
2708                 int ProcessedBOM = NO;\r
2709                 if(Pkt->hWndTrans != NULL)\r
2710                 {\r
2711                         Low = GetFileSize(iFileHandle, &High);\r
2712                         Pkt->Size = MakeLongLong(High, Low);\r
2713 \r
2714                         High = (DWORD)HIGH32(Pkt->ExistSize);\r
2715                         Low = (DWORD)LOW32(Pkt->ExistSize);\r
2716                         SetFilePointer(iFileHandle, Low, &High, FILE_BEGIN);\r
2717 \r
2718                         // 同時接続対応\r
2719 //                      AllTransSizeNow = 0;\r
2720 //                      TimeStart = time(NULL);\r
2721                         AllTransSizeNow[Pkt->ThreadCount] = 0;\r
2722                         TimeStart[Pkt->ThreadCount] = time(NULL);\r
2723                         SetTimer(Pkt->hWndTrans, TIMER_DISPLAY, DISPLAY_TIMING, NULL);\r
2724                 }\r
2725 \r
2726                 InitCodeConvInfo(&cInfo);\r
2727                 cInfo.KanaCnv = Pkt->KanaCnv;\r
2728                 InitTermCodeConvInfo(&tInfo);\r
2729 \r
2730                 InitCodeConvInfo(&cInfo2);\r
2731                 cInfo2.KanaCnv = Pkt->KanaCnv;\r
2732 \r
2733                 /*===== ファイルを送信するループ =====*/\r
2734                 while((Pkt->Abort == ABORT_NONE) &&\r
2735                           (ForceAbort == NO) &&\r
2736                           (ReadFile(iFileHandle, Buf, BUFSIZE, &iNumBytes, NULL) == TRUE))\r
2737                 {\r
2738                         if(iNumBytes == 0)\r
2739                                 break;\r
2740 \r
2741                         /* EOF除去 */\r
2742                         EofPos = NULL;\r
2743                         if((RmEOF == YES) && (Pkt->Type == TYPE_A))\r
2744                         {\r
2745                                 if((EofPos = memchr(Buf, 0x1A, iNumBytes)) != NULL)\r
2746                                         iNumBytes = EofPos - Buf;\r
2747                         }\r
2748 \r
2749                         /* 漢字コード変換 */\r
2750                         if(Pkt->KanjiCode != KANJI_NOCNV)\r
2751                         {\r
2752                                 cInfo.Str = Buf;\r
2753                                 cInfo.StrLen = iNumBytes;\r
2754                                 cInfo.Buf = Buf2;\r
2755                                 cInfo.BufSize = BUFSIZE+3;\r
2756                                 do\r
2757                                 {\r
2758                                         // ここで全てUTF-8へ変換する\r
2759                                         // TODO: SJIS以外も直接UTF-8へ変換\r
2760 //                                      if(Pkt->KanjiCode == KANJI_JIS)\r
2761 //                                              Continue = ConvSJIStoJIS(&cInfo);\r
2762 //                                      else\r
2763 //                                              Continue = ConvSJIStoEUC(&cInfo);\r
2764                                         switch(Pkt->KanjiCodeDesired)\r
2765                                         {\r
2766                                         case KANJI_SJIS:\r
2767                                                 switch(Pkt->KanjiCode)\r
2768                                                 {\r
2769                                                 case KANJI_SJIS:\r
2770 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
2771 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
2772 //                                                      Continue = NO;\r
2773                                                         // カナ変換のため\r
2774                                                         Continue = ConvSJIStoJIS(&cInfo);\r
2775                                                         cInfo2.Str = cInfo.Buf;\r
2776                                                         cInfo2.StrLen = cInfo.OutLen;\r
2777                                                         cInfo2.Buf = Buf3;\r
2778                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2779                                                         ConvJIStoSJIS(&cInfo2);\r
2780                                                         break;\r
2781                                                 case KANJI_JIS:\r
2782                                                         Continue = ConvSJIStoJIS(&cInfo);\r
2783                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2784                                                         cInfo2.OutLen = cInfo.OutLen;\r
2785                                                         break;\r
2786                                                 case KANJI_EUC:\r
2787                                                         Continue = ConvSJIStoEUC(&cInfo);\r
2788                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2789                                                         cInfo2.OutLen = cInfo.OutLen;\r
2790                                                         break;\r
2791                                                 case KANJI_UTF8N:\r
2792                                                         Continue = ConvSJIStoUTF8N(&cInfo);\r
2793                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2794                                                         cInfo2.OutLen = cInfo.OutLen;\r
2795                                                         break;\r
2796                                                 case KANJI_UTF8BOM:\r
2797                                                         if(ProcessedBOM == NO)\r
2798                                                         {\r
2799                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
2800                                                                 cInfo2.OutLen = 3;\r
2801                                                                 Continue = YES;\r
2802                                                                 ProcessedBOM = YES;\r
2803                                                                 break;\r
2804                                                         }\r
2805                                                         Continue = ConvSJIStoUTF8N(&cInfo);\r
2806                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2807                                                         cInfo2.OutLen = cInfo.OutLen;\r
2808                                                         break;\r
2809                                                 }\r
2810                                                 break;\r
2811                                         case KANJI_JIS:\r
2812                                                 switch(Pkt->KanjiCode)\r
2813                                                 {\r
2814                                                 case KANJI_SJIS:\r
2815                                                         Continue = ConvJIStoSJIS(&cInfo);\r
2816                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2817                                                         cInfo2.OutLen = cInfo.OutLen;\r
2818                                                         break;\r
2819                                                 case KANJI_JIS:\r
2820 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
2821 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
2822 //                                                      Continue = NO;\r
2823                                                         // カナ変換のため\r
2824                                                         Continue = ConvJIStoSJIS(&cInfo);\r
2825                                                         cInfo2.Str = cInfo.Buf;\r
2826                                                         cInfo2.StrLen = cInfo.OutLen;\r
2827                                                         cInfo2.Buf = Buf3;\r
2828                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2829                                                         ConvSJIStoJIS(&cInfo2);\r
2830                                                         break;\r
2831                                                 case KANJI_EUC:\r
2832                                                         Continue = ConvJIStoSJIS(&cInfo);\r
2833                                                         cInfo2.Str = cInfo.Buf;\r
2834                                                         cInfo2.StrLen = cInfo.OutLen;\r
2835                                                         cInfo2.Buf = Buf3;\r
2836                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2837                                                         ConvSJIStoEUC(&cInfo2);\r
2838                                                         break;\r
2839                                                 case KANJI_UTF8N:\r
2840                                                         Continue = ConvJIStoSJIS(&cInfo);\r
2841                                                         cInfo2.Str = cInfo.Buf;\r
2842                                                         cInfo2.StrLen = cInfo.OutLen;\r
2843                                                         cInfo2.Buf = Buf3;\r
2844                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2845                                                         ConvSJIStoUTF8N(&cInfo2);\r
2846                                                         break;\r
2847                                                 case KANJI_UTF8BOM:\r
2848                                                         if(ProcessedBOM == NO)\r
2849                                                         {\r
2850                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
2851                                                                 cInfo2.OutLen = 3;\r
2852                                                                 Continue = YES;\r
2853                                                                 ProcessedBOM = YES;\r
2854                                                                 break;\r
2855                                                         }\r
2856                                                         Continue = ConvJIStoSJIS(&cInfo);\r
2857                                                         cInfo2.Str = cInfo.Buf;\r
2858                                                         cInfo2.StrLen = cInfo.OutLen;\r
2859                                                         cInfo2.Buf = Buf3;\r
2860                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2861                                                         ConvSJIStoUTF8N(&cInfo2);\r
2862                                                         break;\r
2863                                                 }\r
2864                                                 break;\r
2865                                         case KANJI_EUC:\r
2866                                                 switch(Pkt->KanjiCode)\r
2867                                                 {\r
2868                                                 case KANJI_SJIS:\r
2869                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
2870                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2871                                                         cInfo2.OutLen = cInfo.OutLen;\r
2872                                                         break;\r
2873                                                 case KANJI_JIS:\r
2874                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
2875                                                         cInfo2.Str = cInfo.Buf;\r
2876                                                         cInfo2.StrLen = cInfo.OutLen;\r
2877                                                         cInfo2.Buf = Buf3;\r
2878                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2879                                                         ConvSJIStoJIS(&cInfo2);\r
2880                                                         break;\r
2881                                                 case KANJI_EUC:\r
2882 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
2883 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
2884 //                                                      Continue = NO;\r
2885                                                         // カナ変換のため\r
2886                     &nbs