OSDN Git Service

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