2 * WININET - Ftp implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2004 Mike McCormack for CodeWeavers
6 * Copyright 2004 Kevin Koltzau
7 * Copyright 2007 Hans Leidekker
12 * Copyright 2000 Andreas Mohr
13 * Copyright 2002 Jaco Greeff
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #define WIN32_NO_STATUS
32 #define COM_NO_WINDOWS_H
35 //#include "wine/port.h"
42 //#include <sys/types.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 # include <sys/socket.h>
46 #ifdef HAVE_ARPA_INET_H
47 # include <arpa/inet.h>
52 #ifdef HAVE_SYS_IOCTL_H
53 # include <sys/ioctl.h>
61 //#include "winuser.h"
64 //#include "winerror.h"
66 //#include "winternl.h"
69 #if defined(__MINGW32__) || defined (_MSC_VER)
73 #include <wine/debug.h>
76 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
78 typedef struct _ftp_session_t ftp_session_t
;
83 ftp_session_t
*lpFtpSession
;
87 HANDLE cache_file_handle
;
96 int pasvSocket
; /* data socket connected by us in case of passive FTP */
97 ftp_file_t
*download_in_progress
;
98 struct sockaddr_in socketAddress
;
99 struct sockaddr_in lstnSocketAddress
;
101 INTERNET_PORT serverport
;
111 SYSTEMTIME tmLastModified
;
112 unsigned short permissions
;
113 } FILEPROPERTIESW
, *LPFILEPROPERTIESW
;
118 ftp_session_t
*lpFtpSession
;
121 LPFILEPROPERTIESW lpafp
;
122 } WININETFTPFINDNEXTW
, *LPWININETFTPFINDNEXTW
;
124 #define DATA_PACKET_SIZE 0x2000
125 #define szCRLF "\r\n"
126 #define MAX_BACKLOG 5
128 /* Testing shows that Windows only accepts dwFlags where the last
129 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
131 #define FTP_CONDITION_MASK 0x0007
134 /* FTP commands with arguments. */
150 /* FTP commands without arguments. */
159 static const CHAR
*const szFtpCommands
[] = {
182 static const CHAR szMonths
[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
183 static const WCHAR szNoAccount
[] = {'n','o','a','c','c','o','u','n','t','\0'};
185 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
186 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
);
187 static BOOL
FTP_SendStore(ftp_session_t
*, LPCWSTR lpszRemoteFile
, DWORD dwType
);
188 static BOOL
FTP_GetDataSocket(ftp_session_t
*, LPINT nDataSocket
);
189 static BOOL
FTP_SendData(ftp_session_t
*, INT nDataSocket
, HANDLE hFile
);
190 static INT
FTP_ReceiveResponse(ftp_session_t
*, DWORD_PTR dwContext
);
191 static BOOL
FTP_SendRetrieve(ftp_session_t
*, LPCWSTR lpszRemoteFile
, DWORD dwType
);
192 static BOOL
FTP_RetrieveFileData(ftp_session_t
*, INT nDataSocket
, HANDLE hFile
);
193 static BOOL
FTP_InitListenSocket(ftp_session_t
*);
194 static BOOL
FTP_ConnectToHost(ftp_session_t
*);
195 static BOOL
FTP_SendPassword(ftp_session_t
*);
196 static BOOL
FTP_SendAccount(ftp_session_t
*);
197 static BOOL
FTP_SendType(ftp_session_t
*, DWORD dwType
);
198 static BOOL
FTP_SendPort(ftp_session_t
*);
199 static BOOL
FTP_DoPassive(ftp_session_t
*);
200 static BOOL
FTP_SendPortOrPasv(ftp_session_t
*);
201 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
);
202 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW fileprop
);
203 static BOOL
FTP_ParseDirectory(ftp_session_t
*, INT nSocket
, LPCWSTR lpszSearchFile
,
204 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
);
205 static HINTERNET
FTP_ReceiveFileList(ftp_session_t
*, INT nSocket
, LPCWSTR lpszSearchFile
,
206 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
);
207 static DWORD
FTP_SetResponseError(DWORD dwResponse
);
208 static BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
);
209 static BOOL
FTP_FtpPutFileW(ftp_session_t
*, LPCWSTR lpszLocalFile
,
210 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
);
211 static BOOL
FTP_FtpSetCurrentDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
212 static BOOL
FTP_FtpCreateDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
213 static HINTERNET
FTP_FtpFindFirstFileW(ftp_session_t
*,
214 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
);
215 static BOOL
FTP_FtpGetCurrentDirectoryW(ftp_session_t
*, LPWSTR lpszCurrentDirectory
,
216 LPDWORD lpdwCurrentDirectory
);
217 static BOOL
FTP_FtpRenameFileW(ftp_session_t
*, LPCWSTR lpszSrc
, LPCWSTR lpszDest
);
218 static BOOL
FTP_FtpRemoveDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
219 static BOOL
FTP_FtpDeleteFileW(ftp_session_t
*, LPCWSTR lpszFileName
);
220 static BOOL
FTP_FtpGetFileW(ftp_session_t
*, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
221 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
222 DWORD_PTR dwContext
);
224 /* A temporary helper until we get rid of INTERNET_GetLastError calls */
225 static BOOL
res_to_le(DWORD res
)
227 if(res
!= ERROR_SUCCESS
)
228 INTERNET_SetLastError(res
);
229 return res
== ERROR_SUCCESS
;
232 /***********************************************************************
233 * FtpPutFileA (WININET.@)
235 * Uploads a file to the FTP server
242 BOOL WINAPI
FtpPutFileA(HINTERNET hConnect
, LPCSTR lpszLocalFile
,
243 LPCSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
245 LPWSTR lpwzLocalFile
;
246 LPWSTR lpwzNewRemoteFile
;
249 lpwzLocalFile
= heap_strdupAtoW(lpszLocalFile
);
250 lpwzNewRemoteFile
= heap_strdupAtoW(lpszNewRemoteFile
);
251 ret
= FtpPutFileW(hConnect
, lpwzLocalFile
, lpwzNewRemoteFile
,
253 heap_free(lpwzLocalFile
);
254 heap_free(lpwzNewRemoteFile
);
266 static void AsyncFtpPutFileProc(task_header_t
*hdr
)
268 put_file_task_t
*task
= (put_file_task_t
*)hdr
;
269 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
271 TRACE("%p\n", session
);
273 FTP_FtpPutFileW(session
, task
->local_file
, task
->remote_file
,
274 task
->flags
, task
->context
);
276 heap_free(task
->local_file
);
277 heap_free(task
->remote_file
);
280 /***********************************************************************
281 * FtpPutFileW (WININET.@)
283 * Uploads a file to the FTP server
290 BOOL WINAPI
FtpPutFileW(HINTERNET hConnect
, LPCWSTR lpszLocalFile
,
291 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
293 ftp_session_t
*lpwfs
;
294 appinfo_t
*hIC
= NULL
;
297 if (!lpszLocalFile
|| !lpszNewRemoteFile
)
299 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
303 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
306 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
310 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
312 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
316 if (lpwfs
->download_in_progress
!= NULL
)
318 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
322 if ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
324 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
328 hIC
= lpwfs
->lpAppInfo
;
329 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
331 put_file_task_t
*task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpPutFileProc
, sizeof(*task
));
333 task
->local_file
= heap_strdupW(lpszLocalFile
);
334 task
->remote_file
= heap_strdupW(lpszNewRemoteFile
);
335 task
->flags
= dwFlags
;
336 task
->context
= dwContext
;
338 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
342 r
= FTP_FtpPutFileW(lpwfs
, lpszLocalFile
,
343 lpszNewRemoteFile
, dwFlags
, dwContext
);
347 WININET_Release( &lpwfs
->hdr
);
352 /***********************************************************************
353 * FTP_FtpPutFileW (Internal)
355 * Uploads a file to the FTP server
362 static BOOL
FTP_FtpPutFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszLocalFile
,
363 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
366 BOOL bSuccess
= FALSE
;
367 appinfo_t
*hIC
= NULL
;
370 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile
), debugstr_w(lpszNewRemoteFile
));
372 /* Clear any error information */
373 INTERNET_SetLastError(0);
375 /* Open file to be uploaded */
376 if (INVALID_HANDLE_VALUE
==
377 (hFile
= CreateFileW(lpszLocalFile
, GENERIC_READ
, 0, 0, OPEN_EXISTING
, 0, 0)))
378 /* Let CreateFile set the appropriate error */
381 hIC
= lpwfs
->lpAppInfo
;
383 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
385 if (FTP_SendStore(lpwfs
, lpszNewRemoteFile
, dwFlags
))
389 /* Get data socket to server */
390 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
392 FTP_SendData(lpwfs
, nDataSocket
, hFile
);
393 closesocket(nDataSocket
);
394 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
400 FTP_SetResponseError(nResCode
);
405 if (lpwfs
->lstnSocket
!= -1)
407 closesocket(lpwfs
->lstnSocket
);
408 lpwfs
->lstnSocket
= -1;
411 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
413 INTERNET_ASYNC_RESULT iar
;
415 iar
.dwResult
= (DWORD
)bSuccess
;
416 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
417 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
418 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
427 /***********************************************************************
428 * FtpSetCurrentDirectoryA (WININET.@)
430 * Change the working directory on the FTP server
437 BOOL WINAPI
FtpSetCurrentDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
439 LPWSTR lpwzDirectory
;
442 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
443 ret
= FtpSetCurrentDirectoryW(hConnect
, lpwzDirectory
);
444 heap_free(lpwzDirectory
);
453 static void AsyncFtpSetCurrentDirectoryProc(task_header_t
*hdr
)
455 directory_task_t
*task
= (directory_task_t
*)hdr
;
456 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
458 TRACE("%p\n", session
);
460 FTP_FtpSetCurrentDirectoryW(session
, task
->directory
);
461 heap_free(task
->directory
);
464 /***********************************************************************
465 * FtpSetCurrentDirectoryW (WININET.@)
467 * Change the working directory on the FTP server
474 BOOL WINAPI
FtpSetCurrentDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
476 ftp_session_t
*lpwfs
= NULL
;
477 appinfo_t
*hIC
= NULL
;
482 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
486 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
487 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
489 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
493 if (lpwfs
->download_in_progress
!= NULL
)
495 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
499 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
501 hIC
= lpwfs
->lpAppInfo
;
502 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
504 directory_task_t
*task
;
506 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpSetCurrentDirectoryProc
, sizeof(*task
));
507 task
->directory
= heap_strdupW(lpszDirectory
);
509 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
513 r
= FTP_FtpSetCurrentDirectoryW(lpwfs
, lpszDirectory
);
518 WININET_Release( &lpwfs
->hdr
);
524 /***********************************************************************
525 * FTP_FtpSetCurrentDirectoryW (Internal)
527 * Change the working directory on the FTP server
534 static BOOL
FTP_FtpSetCurrentDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
537 appinfo_t
*hIC
= NULL
;
538 DWORD bSuccess
= FALSE
;
540 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
542 /* Clear any error information */
543 INTERNET_SetLastError(0);
545 hIC
= lpwfs
->lpAppInfo
;
546 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_CWD
, lpszDirectory
,
547 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
550 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
557 FTP_SetResponseError(nResCode
);
561 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
563 INTERNET_ASYNC_RESULT iar
;
565 iar
.dwResult
= bSuccess
;
566 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
567 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
568 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
574 /***********************************************************************
575 * FtpCreateDirectoryA (WININET.@)
577 * Create new directory on the FTP server
584 BOOL WINAPI
FtpCreateDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
586 LPWSTR lpwzDirectory
;
589 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
590 ret
= FtpCreateDirectoryW(hConnect
, lpwzDirectory
);
591 heap_free(lpwzDirectory
);
596 static void AsyncFtpCreateDirectoryProc(task_header_t
*hdr
)
598 directory_task_t
*task
= (directory_task_t
*)hdr
;
599 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
601 TRACE(" %p\n", session
);
603 FTP_FtpCreateDirectoryW(session
, task
->directory
);
604 heap_free(task
->directory
);
607 /***********************************************************************
608 * FtpCreateDirectoryW (WININET.@)
610 * Create new directory on the FTP server
617 BOOL WINAPI
FtpCreateDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
619 ftp_session_t
*lpwfs
;
620 appinfo_t
*hIC
= NULL
;
623 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
626 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
630 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
632 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
636 if (lpwfs
->download_in_progress
!= NULL
)
638 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
644 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
648 hIC
= lpwfs
->lpAppInfo
;
649 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
651 directory_task_t
*task
;
653 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpCreateDirectoryProc
, sizeof(*task
));
654 task
->directory
= heap_strdupW(lpszDirectory
);
656 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
660 r
= FTP_FtpCreateDirectoryW(lpwfs
, lpszDirectory
);
663 WININET_Release( &lpwfs
->hdr
);
669 /***********************************************************************
670 * FTP_FtpCreateDirectoryW (Internal)
672 * Create new directory on the FTP server
679 static BOOL
FTP_FtpCreateDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
682 BOOL bSuccess
= FALSE
;
683 appinfo_t
*hIC
= NULL
;
685 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
687 /* Clear any error information */
688 INTERNET_SetLastError(0);
690 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_MKD
, lpszDirectory
, 0, 0, 0))
693 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
699 FTP_SetResponseError(nResCode
);
703 hIC
= lpwfs
->lpAppInfo
;
704 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
706 INTERNET_ASYNC_RESULT iar
;
708 iar
.dwResult
= (DWORD
)bSuccess
;
709 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
710 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
711 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
717 /***********************************************************************
718 * FtpFindFirstFileA (WININET.@)
720 * Search the specified directory
723 * HINTERNET on success
727 HINTERNET WINAPI
FtpFindFirstFileA(HINTERNET hConnect
,
728 LPCSTR lpszSearchFile
, LPWIN32_FIND_DATAA lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
730 LPWSTR lpwzSearchFile
;
731 WIN32_FIND_DATAW wfd
;
732 LPWIN32_FIND_DATAW lpFindFileDataW
;
735 lpwzSearchFile
= heap_strdupAtoW(lpszSearchFile
);
736 lpFindFileDataW
= lpFindFileData
?&wfd
:NULL
;
737 ret
= FtpFindFirstFileW(hConnect
, lpwzSearchFile
, lpFindFileDataW
, dwFlags
, dwContext
);
738 heap_free(lpwzSearchFile
);
740 if (ret
&& lpFindFileData
)
741 WININET_find_data_WtoA(lpFindFileDataW
, lpFindFileData
);
749 WIN32_FIND_DATAW
*find_file_data
;
752 } find_first_file_task_t
;
754 static void AsyncFtpFindFirstFileProc(task_header_t
*hdr
)
756 find_first_file_task_t
*task
= (find_first_file_task_t
*)hdr
;
757 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
759 TRACE("%p\n", session
);
761 FTP_FtpFindFirstFileW(session
, task
->search_file
, task
->find_file_data
, task
->flags
, task
->context
);
762 heap_free(task
->search_file
);
765 /***********************************************************************
766 * FtpFindFirstFileW (WININET.@)
768 * Search the specified directory
771 * HINTERNET on success
775 HINTERNET WINAPI
FtpFindFirstFileW(HINTERNET hConnect
,
776 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
778 ftp_session_t
*lpwfs
;
779 appinfo_t
*hIC
= NULL
;
782 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
783 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
785 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
789 if (lpwfs
->download_in_progress
!= NULL
)
791 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
795 hIC
= lpwfs
->lpAppInfo
;
796 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
798 find_first_file_task_t
*task
;
800 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpFindFirstFileProc
, sizeof(*task
));
801 task
->search_file
= heap_strdupW(lpszSearchFile
);
802 task
->find_file_data
= lpFindFileData
;
803 task
->flags
= dwFlags
;
804 task
->context
= dwContext
;
806 INTERNET_AsyncCall(&task
->hdr
);
811 r
= FTP_FtpFindFirstFileW(lpwfs
, lpszSearchFile
, lpFindFileData
,
816 WININET_Release( &lpwfs
->hdr
);
822 /***********************************************************************
823 * FTP_FtpFindFirstFileW (Internal)
825 * Search the specified directory
828 * HINTERNET on success
832 static HINTERNET
FTP_FtpFindFirstFileW(ftp_session_t
*lpwfs
,
833 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
836 appinfo_t
*hIC
= NULL
;
837 HINTERNET hFindNext
= NULL
;
841 /* Clear any error information */
842 INTERNET_SetLastError(0);
844 if (!FTP_InitListenSocket(lpwfs
))
847 if (!FTP_SendType(lpwfs
, INTERNET_FLAG_TRANSFER_ASCII
))
850 if (!FTP_SendPortOrPasv(lpwfs
))
853 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_LIST
, NULL
,
854 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
857 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
860 if (nResCode
== 125 || nResCode
== 150)
864 /* Get data socket to server */
865 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
867 hFindNext
= FTP_ReceiveFileList(lpwfs
, nDataSocket
, lpszSearchFile
, lpFindFileData
, dwContext
);
868 closesocket(nDataSocket
);
869 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
870 if (nResCode
!= 226 && nResCode
!= 250)
871 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
875 FTP_SetResponseError(nResCode
);
879 if (lpwfs
->lstnSocket
!= -1)
881 closesocket(lpwfs
->lstnSocket
);
882 lpwfs
->lstnSocket
= -1;
885 hIC
= lpwfs
->lpAppInfo
;
886 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
888 INTERNET_ASYNC_RESULT iar
;
892 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
893 iar
.dwError
= ERROR_SUCCESS
;
894 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
895 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
898 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
899 iar
.dwError
= hFindNext
? ERROR_SUCCESS
: INTERNET_GetLastError();
900 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
901 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
908 /***********************************************************************
909 * FtpGetCurrentDirectoryA (WININET.@)
911 * Retrieves the current directory
918 BOOL WINAPI
FtpGetCurrentDirectoryA(HINTERNET hFtpSession
, LPSTR lpszCurrentDirectory
,
919 LPDWORD lpdwCurrentDirectory
)
925 if(lpdwCurrentDirectory
) {
926 len
= *lpdwCurrentDirectory
;
927 if(lpszCurrentDirectory
)
929 dir
= heap_alloc(len
* sizeof(WCHAR
));
932 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
937 ret
= FtpGetCurrentDirectoryW(hFtpSession
, lpszCurrentDirectory
?dir
:NULL
, lpdwCurrentDirectory
?&len
:NULL
);
939 if (ret
&& lpszCurrentDirectory
)
940 WideCharToMultiByte(CP_ACP
, 0, dir
, -1, lpszCurrentDirectory
, len
, NULL
, NULL
);
942 if (lpdwCurrentDirectory
) *lpdwCurrentDirectory
= len
;
950 DWORD
*directory_len
;
951 } get_current_dir_task_t
;
953 static void AsyncFtpGetCurrentDirectoryProc(task_header_t
*hdr
)
955 get_current_dir_task_t
*task
= (get_current_dir_task_t
*)hdr
;
956 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
958 TRACE("%p\n", session
);
960 FTP_FtpGetCurrentDirectoryW(session
, task
->directory
, task
->directory_len
);
963 /***********************************************************************
964 * FtpGetCurrentDirectoryW (WININET.@)
966 * Retrieves the current directory
973 BOOL WINAPI
FtpGetCurrentDirectoryW(HINTERNET hFtpSession
, LPWSTR lpszCurrentDirectory
,
974 LPDWORD lpdwCurrentDirectory
)
976 ftp_session_t
*lpwfs
;
977 appinfo_t
*hIC
= NULL
;
980 TRACE("%p %p %p\n", hFtpSession
, lpszCurrentDirectory
, lpdwCurrentDirectory
);
982 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
985 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
989 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
991 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
995 if (!lpdwCurrentDirectory
)
997 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1001 if (lpszCurrentDirectory
== NULL
)
1003 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1007 if (lpwfs
->download_in_progress
!= NULL
)
1009 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1013 hIC
= lpwfs
->lpAppInfo
;
1014 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1016 get_current_dir_task_t
*task
;
1018 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpGetCurrentDirectoryProc
, sizeof(*task
));
1019 task
->directory
= lpszCurrentDirectory
;
1020 task
->directory_len
= lpdwCurrentDirectory
;
1022 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
1026 r
= FTP_FtpGetCurrentDirectoryW(lpwfs
, lpszCurrentDirectory
,
1027 lpdwCurrentDirectory
);
1032 WININET_Release( &lpwfs
->hdr
);
1038 /***********************************************************************
1039 * FTP_FtpGetCurrentDirectoryW (Internal)
1041 * Retrieves the current directory
1048 static BOOL
FTP_FtpGetCurrentDirectoryW(ftp_session_t
*lpwfs
, LPWSTR lpszCurrentDirectory
,
1049 LPDWORD lpdwCurrentDirectory
)
1052 appinfo_t
*hIC
= NULL
;
1053 DWORD bSuccess
= FALSE
;
1055 /* Clear any error information */
1056 INTERNET_SetLastError(0);
1058 hIC
= lpwfs
->lpAppInfo
;
1059 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PWD
, NULL
,
1060 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
1063 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1066 if (nResCode
== 257) /* Extract directory name */
1068 DWORD firstpos
, lastpos
, len
;
1069 LPWSTR lpszResponseBuffer
= heap_strdupAtoW(INTERNET_GetResponseBuffer());
1071 for (firstpos
= 0, lastpos
= 0; lpszResponseBuffer
[lastpos
]; lastpos
++)
1073 if ('"' == lpszResponseBuffer
[lastpos
])
1081 len
= lastpos
- firstpos
;
1082 if (*lpdwCurrentDirectory
>= len
)
1084 memcpy(lpszCurrentDirectory
, &lpszResponseBuffer
[firstpos
+ 1], len
* sizeof(WCHAR
));
1085 lpszCurrentDirectory
[len
- 1] = 0;
1086 *lpdwCurrentDirectory
= len
;
1089 else INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1091 heap_free(lpszResponseBuffer
);
1094 FTP_SetResponseError(nResCode
);
1098 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1100 INTERNET_ASYNC_RESULT iar
;
1102 iar
.dwResult
= bSuccess
;
1103 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
1104 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1105 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1112 /***********************************************************************
1113 * FTPFILE_Destroy(internal)
1115 * Closes the file transfer handle. This also 'cleans' the data queue of
1116 * the 'transfer complete' message (this is a bit of a hack though :-/ )
1119 static void FTPFILE_Destroy(object_header_t
*hdr
)
1121 ftp_file_t
*lpwh
= (ftp_file_t
*) hdr
;
1122 ftp_session_t
*lpwfs
= lpwh
->lpFtpSession
;
1127 if (lpwh
->cache_file_handle
!= INVALID_HANDLE_VALUE
)
1128 CloseHandle(lpwh
->cache_file_handle
);
1130 heap_free(lpwh
->cache_file
);
1132 if (!lpwh
->session_deleted
)
1133 lpwfs
->download_in_progress
= NULL
;
1135 if (lpwh
->nDataSocket
!= -1)
1136 closesocket(lpwh
->nDataSocket
);
1138 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1139 if (nResCode
> 0 && nResCode
!= 226) WARN("server reports failed transfer\n");
1141 WININET_Release(&lpwh
->lpFtpSession
->hdr
);
1144 static DWORD
FTPFILE_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
1147 case INTERNET_OPTION_HANDLE_TYPE
:
1148 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1150 if (*size
< sizeof(ULONG
))
1151 return ERROR_INSUFFICIENT_BUFFER
;
1153 *size
= sizeof(DWORD
);
1154 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_FTP_FILE
;
1155 return ERROR_SUCCESS
;
1156 case INTERNET_OPTION_DATAFILE_NAME
:
1159 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1161 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1163 if (!file
->cache_file
)
1166 return ERROR_INTERNET_ITEM_NOT_FOUND
;
1170 required
= (lstrlenW(file
->cache_file
) + 1) * sizeof(WCHAR
);
1171 if (*size
< required
)
1172 return ERROR_INSUFFICIENT_BUFFER
;
1175 memcpy(buffer
, file
->cache_file
, *size
);
1176 return ERROR_SUCCESS
;
1180 required
= WideCharToMultiByte(CP_ACP
, 0, file
->cache_file
, -1, NULL
, 0, NULL
, NULL
);
1181 if (required
> *size
)
1182 return ERROR_INSUFFICIENT_BUFFER
;
1184 *size
= WideCharToMultiByte(CP_ACP
, 0, file
->cache_file
, -1, buffer
, *size
, NULL
, NULL
);
1185 return ERROR_SUCCESS
;
1189 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
1192 static DWORD
FTPFILE_ReadFile(object_header_t
*hdr
, void *buffer
, DWORD size
, DWORD
*read
)
1194 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1198 if (file
->nDataSocket
== -1)
1199 return ERROR_INTERNET_DISCONNECTED
;
1201 /* FIXME: FTP should use NETCON_ stuff */
1202 res
= recv(file
->nDataSocket
, buffer
, size
, MSG_WAITALL
);
1203 *read
= res
>0 ? res
: 0;
1205 error
= res
>= 0 ? ERROR_SUCCESS
: INTERNET_ERROR_BASE
; /* FIXME */
1206 if (error
== ERROR_SUCCESS
&& file
->cache_file
)
1208 DWORD bytes_written
;
1210 if (!WriteFile(file
->cache_file_handle
, buffer
, *read
, &bytes_written
, NULL
))
1211 WARN("WriteFile failed: %u\n", GetLastError());
1216 static DWORD
FTPFILE_ReadFileEx(object_header_t
*hdr
, void *buf
, DWORD size
, DWORD
*ret_size
,
1217 DWORD flags
, DWORD_PTR context
)
1219 return FTPFILE_ReadFile(hdr
, buf
, size
, ret_size
);
1222 static DWORD
FTPFILE_WriteFile(object_header_t
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
1224 ftp_file_t
*lpwh
= (ftp_file_t
*) hdr
;
1227 res
= send(lpwh
->nDataSocket
, buffer
, size
, 0);
1229 *written
= res
>0 ? res
: 0;
1230 return res
>= 0 ? ERROR_SUCCESS
: sock_get_error(errno
);
1233 static void FTP_ReceiveRequestData(ftp_file_t
*file
, BOOL first_notif
)
1235 INTERNET_ASYNC_RESULT iar
;
1239 TRACE("%p\n", file
);
1241 available
= recv(file
->nDataSocket
, buffer
, sizeof(buffer
), MSG_PEEK
);
1243 if(available
!= -1) {
1244 iar
.dwResult
= (DWORD_PTR
)file
->hdr
.hInternet
;
1245 iar
.dwError
= first_notif
? 0 : available
;
1248 iar
.dwError
= INTERNET_GetLastError();
1251 INTERNET_SendCallback(&file
->hdr
, file
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1252 sizeof(INTERNET_ASYNC_RESULT
));
1255 static void FTPFILE_AsyncQueryDataAvailableProc(task_header_t
*task
)
1257 ftp_file_t
*file
= (ftp_file_t
*)task
->hdr
;
1259 FTP_ReceiveRequestData(file
, FALSE
);
1262 static DWORD
FTPFILE_QueryDataAvailable(object_header_t
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
1264 ftp_file_t
*file
= (ftp_file_t
*) hdr
;
1265 int retval
, unread
= 0;
1267 TRACE("(%p %p %x %lx)\n", file
, available
, flags
, ctx
);
1270 retval
= ioctlsocket(file
->nDataSocket
, FIONREAD
, &unread
);
1272 TRACE("%d bytes of queued, but unread data\n", unread
);
1274 FIXME("FIONREAD not available\n");
1277 *available
= unread
;
1284 retval
= recv(file
->nDataSocket
, &byte
, 1, MSG_PEEK
);
1286 task_header_t
*task
;
1288 task
= alloc_async_task(&file
->hdr
, FTPFILE_AsyncQueryDataAvailableProc
, sizeof(*task
));
1289 INTERNET_AsyncCall(task
);
1291 return ERROR_IO_PENDING
;
1295 return ERROR_SUCCESS
;
1299 static const object_vtbl_t FTPFILEVtbl
= {
1302 FTPFILE_QueryOption
,
1307 FTPFILE_QueryDataAvailable
,
1311 /***********************************************************************
1312 * FTP_FtpOpenFileW (Internal)
1314 * Open a remote file for writing or reading
1317 * HINTERNET handle on success
1321 static HINTERNET
FTP_FtpOpenFileW(ftp_session_t
*lpwfs
,
1322 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1323 DWORD_PTR dwContext
)
1326 BOOL bSuccess
= FALSE
;
1327 ftp_file_t
*lpwh
= NULL
;
1328 appinfo_t
*hIC
= NULL
;
1332 /* Clear any error information */
1333 INTERNET_SetLastError(0);
1335 if (GENERIC_READ
== fdwAccess
)
1337 /* Set up socket to retrieve data */
1338 bSuccess
= FTP_SendRetrieve(lpwfs
, lpszFileName
, dwFlags
);
1340 else if (GENERIC_WRITE
== fdwAccess
)
1342 /* Set up socket to send data */
1343 bSuccess
= FTP_SendStore(lpwfs
, lpszFileName
, dwFlags
);
1346 /* Get data socket to server */
1347 if (bSuccess
&& FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1349 lpwh
= alloc_object(&lpwfs
->hdr
, &FTPFILEVtbl
, sizeof(ftp_file_t
));
1350 lpwh
->hdr
.htype
= WH_HFILE
;
1351 lpwh
->hdr
.dwFlags
= dwFlags
;
1352 lpwh
->hdr
.dwContext
= dwContext
;
1353 lpwh
->nDataSocket
= nDataSocket
;
1354 lpwh
->cache_file
= NULL
;
1355 lpwh
->cache_file_handle
= INVALID_HANDLE_VALUE
;
1356 lpwh
->session_deleted
= FALSE
;
1358 WININET_AddRef( &lpwfs
->hdr
);
1359 lpwh
->lpFtpSession
= lpwfs
;
1360 list_add_head( &lpwfs
->hdr
.children
, &lpwh
->hdr
.entry
);
1362 /* Indicate that a download is currently in progress */
1363 lpwfs
->download_in_progress
= lpwh
;
1366 if (lpwfs
->lstnSocket
!= -1)
1368 closesocket(lpwfs
->lstnSocket
);
1369 lpwfs
->lstnSocket
= -1;
1372 if (bSuccess
&& fdwAccess
== GENERIC_READ
)
1374 WCHAR filename
[MAX_PATH
+ 1];
1378 memset(&uc
, 0, sizeof(uc
));
1379 uc
.dwStructSize
= sizeof(uc
);
1380 uc
.nScheme
= INTERNET_SCHEME_FTP
;
1381 uc
.lpszHostName
= lpwfs
->servername
;
1382 uc
.nPort
= lpwfs
->serverport
;
1383 uc
.lpszUserName
= lpwfs
->lpszUserName
;
1384 uc
.lpszUrlPath
= heap_strdupW(lpszFileName
);
1386 if (!InternetCreateUrlW(&uc
, 0, NULL
, &len
) && GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1388 WCHAR
*url
= heap_alloc(len
* sizeof(WCHAR
));
1390 if (url
&& InternetCreateUrlW(&uc
, 0, url
, &len
) && CreateUrlCacheEntryW(url
, 0, NULL
, filename
, 0))
1392 lpwh
->cache_file
= heap_strdupW(filename
);
1393 lpwh
->cache_file_handle
= CreateFileW(filename
, GENERIC_WRITE
, FILE_SHARE_READ
,
1394 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1395 if (lpwh
->cache_file_handle
== INVALID_HANDLE_VALUE
)
1397 WARN("Could not create cache file: %u\n", GetLastError());
1398 heap_free(lpwh
->cache_file
);
1399 lpwh
->cache_file
= NULL
;
1404 heap_free(uc
.lpszUrlPath
);
1407 hIC
= lpwfs
->lpAppInfo
;
1408 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1410 INTERNET_ASYNC_RESULT iar
;
1414 iar
.dwResult
= (DWORD_PTR
)lpwh
->hdr
.hInternet
;
1415 iar
.dwError
= ERROR_SUCCESS
;
1416 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
1417 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1421 FTP_ReceiveRequestData(lpwh
, TRUE
);
1424 iar
.dwError
= INTERNET_GetLastError();
1425 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1426 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1433 return lpwh
->hdr
.hInternet
;
1437 /***********************************************************************
1438 * FtpOpenFileA (WININET.@)
1440 * Open a remote file for writing or reading
1443 * HINTERNET handle on success
1447 HINTERNET WINAPI
FtpOpenFileA(HINTERNET hFtpSession
,
1448 LPCSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1449 DWORD_PTR dwContext
)
1451 LPWSTR lpwzFileName
;
1454 lpwzFileName
= heap_strdupAtoW(lpszFileName
);
1455 ret
= FtpOpenFileW(hFtpSession
, lpwzFileName
, fdwAccess
, dwFlags
, dwContext
);
1456 heap_free(lpwzFileName
);
1468 static void AsyncFtpOpenFileProc(task_header_t
*hdr
)
1470 open_file_task_t
*task
= (open_file_task_t
*)hdr
;
1471 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1473 TRACE("%p\n", session
);
1475 FTP_FtpOpenFileW(session
, task
->file_name
, task
->access
, task
->flags
, task
->context
);
1476 heap_free(task
->file_name
);
1479 /***********************************************************************
1480 * FtpOpenFileW (WININET.@)
1482 * Open a remote file for writing or reading
1485 * HINTERNET handle on success
1489 HINTERNET WINAPI
FtpOpenFileW(HINTERNET hFtpSession
,
1490 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1491 DWORD_PTR dwContext
)
1493 ftp_session_t
*lpwfs
;
1494 appinfo_t
*hIC
= NULL
;
1497 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession
,
1498 debugstr_w(lpszFileName
), fdwAccess
, dwFlags
, dwContext
);
1500 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1503 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1507 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1509 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1513 if ((!lpszFileName
) ||
1514 ((fdwAccess
!= GENERIC_READ
) && (fdwAccess
!= GENERIC_WRITE
)) ||
1515 ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
))
1517 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1521 if (lpwfs
->download_in_progress
!= NULL
)
1523 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1527 hIC
= lpwfs
->lpAppInfo
;
1528 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1530 open_file_task_t
*task
;
1532 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpOpenFileProc
, sizeof(*task
));
1533 task
->file_name
= heap_strdupW(lpszFileName
);
1534 task
->access
= fdwAccess
;
1535 task
->flags
= dwFlags
;
1536 task
->context
= dwContext
;
1538 INTERNET_AsyncCall(&task
->hdr
);
1543 r
= FTP_FtpOpenFileW(lpwfs
, lpszFileName
, fdwAccess
, dwFlags
, dwContext
);
1547 WININET_Release( &lpwfs
->hdr
);
1553 /***********************************************************************
1554 * FtpGetFileA (WININET.@)
1556 * Retrieve file from the FTP server
1563 BOOL WINAPI
FtpGetFileA(HINTERNET hInternet
, LPCSTR lpszRemoteFile
, LPCSTR lpszNewFile
,
1564 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1565 DWORD_PTR dwContext
)
1567 LPWSTR lpwzRemoteFile
;
1571 lpwzRemoteFile
= heap_strdupAtoW(lpszRemoteFile
);
1572 lpwzNewFile
= heap_strdupAtoW(lpszNewFile
);
1573 ret
= FtpGetFileW(hInternet
, lpwzRemoteFile
, lpwzNewFile
, fFailIfExists
,
1574 dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1575 heap_free(lpwzRemoteFile
);
1576 heap_free(lpwzNewFile
);
1584 BOOL fail_if_exists
;
1590 static void AsyncFtpGetFileProc(task_header_t
*hdr
)
1592 get_file_task_t
*task
= (get_file_task_t
*)hdr
;
1593 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1595 TRACE("%p\n", session
);
1597 FTP_FtpGetFileW(session
, task
->remote_file
, task
->new_file
, task
->fail_if_exists
,
1598 task
->local_attr
, task
->flags
, task
->context
);
1599 heap_free(task
->remote_file
);
1600 heap_free(task
->new_file
);
1604 /***********************************************************************
1605 * FtpGetFileW (WININET.@)
1607 * Retrieve file from the FTP server
1614 BOOL WINAPI
FtpGetFileW(HINTERNET hInternet
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1615 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1616 DWORD_PTR dwContext
)
1618 ftp_session_t
*lpwfs
;
1619 appinfo_t
*hIC
= NULL
;
1622 if (!lpszRemoteFile
|| !lpszNewFile
)
1624 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1628 lpwfs
= (ftp_session_t
*) get_handle_object( hInternet
);
1631 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1635 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1637 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1641 if ((dwInternetFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
1643 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1647 if (lpwfs
->download_in_progress
!= NULL
)
1649 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1653 hIC
= lpwfs
->lpAppInfo
;
1654 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1656 get_file_task_t
*task
;
1658 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpGetFileProc
, sizeof(*task
));
1659 task
->remote_file
= heap_strdupW(lpszRemoteFile
);
1660 task
->new_file
= heap_strdupW(lpszNewFile
);
1661 task
->local_attr
= dwLocalFlagsAttribute
;
1662 task
->fail_if_exists
= fFailIfExists
;
1663 task
->flags
= dwInternetFlags
;
1664 task
->context
= dwContext
;
1666 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
1670 r
= FTP_FtpGetFileW(lpwfs
, lpszRemoteFile
, lpszNewFile
,
1671 fFailIfExists
, dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1675 WININET_Release( &lpwfs
->hdr
);
1681 /***********************************************************************
1682 * FTP_FtpGetFileW (Internal)
1684 * Retrieve file from the FTP server
1691 static BOOL
FTP_FtpGetFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1692 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1693 DWORD_PTR dwContext
)
1695 BOOL bSuccess
= FALSE
;
1697 appinfo_t
*hIC
= NULL
;
1699 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile
), debugstr_w(lpszNewFile
));
1701 /* Clear any error information */
1702 INTERNET_SetLastError(0);
1704 /* Ensure we can write to lpszNewfile by opening it */
1705 hFile
= CreateFileW(lpszNewFile
, GENERIC_WRITE
, 0, 0, fFailIfExists
?
1706 CREATE_NEW
: CREATE_ALWAYS
, dwLocalFlagsAttribute
, 0);
1707 if (INVALID_HANDLE_VALUE
== hFile
)
1710 /* Set up socket to retrieve data */
1711 if (FTP_SendRetrieve(lpwfs
, lpszRemoteFile
, dwInternetFlags
))
1715 /* Get data socket to server */
1716 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1721 FTP_RetrieveFileData(lpwfs
, nDataSocket
, hFile
);
1722 closesocket(nDataSocket
);
1724 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
1727 if (nResCode
== 226)
1730 FTP_SetResponseError(nResCode
);
1735 if (lpwfs
->lstnSocket
!= -1)
1737 closesocket(lpwfs
->lstnSocket
);
1738 lpwfs
->lstnSocket
= -1;
1743 hIC
= lpwfs
->lpAppInfo
;
1744 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1746 INTERNET_ASYNC_RESULT iar
;
1748 iar
.dwResult
= (DWORD
)bSuccess
;
1749 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1750 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1751 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1754 if (!bSuccess
) DeleteFileW(lpszNewFile
);
1758 /***********************************************************************
1759 * FtpGetFileSize (WININET.@)
1761 DWORD WINAPI
FtpGetFileSize( HINTERNET hFile
, LPDWORD lpdwFileSizeHigh
)
1763 FIXME("(%p, %p)\n", hFile
, lpdwFileSizeHigh
);
1765 if (lpdwFileSizeHigh
)
1766 *lpdwFileSizeHigh
= 0;
1771 /***********************************************************************
1772 * FtpDeleteFileA (WININET.@)
1774 * Delete a file on the ftp server
1781 BOOL WINAPI
FtpDeleteFileA(HINTERNET hFtpSession
, LPCSTR lpszFileName
)
1783 LPWSTR lpwzFileName
;
1786 lpwzFileName
= heap_strdupAtoW(lpszFileName
);
1787 ret
= FtpDeleteFileW(hFtpSession
, lpwzFileName
);
1788 heap_free(lpwzFileName
);
1795 } delete_file_task_t
;
1797 static void AsyncFtpDeleteFileProc(task_header_t
*hdr
)
1799 delete_file_task_t
*task
= (delete_file_task_t
*)hdr
;
1800 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1802 TRACE("%p\n", session
);
1804 FTP_FtpDeleteFileW(session
, task
->file_name
);
1805 heap_free(task
->file_name
);
1808 /***********************************************************************
1809 * FtpDeleteFileW (WININET.@)
1811 * Delete a file on the ftp server
1818 BOOL WINAPI
FtpDeleteFileW(HINTERNET hFtpSession
, LPCWSTR lpszFileName
)
1820 ftp_session_t
*lpwfs
;
1821 appinfo_t
*hIC
= NULL
;
1824 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1827 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1831 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1833 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1837 if (lpwfs
->download_in_progress
!= NULL
)
1839 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1845 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1849 hIC
= lpwfs
->lpAppInfo
;
1850 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1852 delete_file_task_t
*task
;
1854 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpDeleteFileProc
, sizeof(*task
));
1855 task
->file_name
= heap_strdupW(lpszFileName
);
1857 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
1861 r
= FTP_FtpDeleteFileW(lpwfs
, lpszFileName
);
1865 WININET_Release( &lpwfs
->hdr
);
1870 /***********************************************************************
1871 * FTP_FtpDeleteFileW (Internal)
1873 * Delete a file on the ftp server
1880 BOOL
FTP_FtpDeleteFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszFileName
)
1883 BOOL bSuccess
= FALSE
;
1884 appinfo_t
*hIC
= NULL
;
1886 TRACE("%p\n", lpwfs
);
1888 /* Clear any error information */
1889 INTERNET_SetLastError(0);
1891 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_DELE
, lpszFileName
, 0, 0, 0))
1894 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1897 if (nResCode
== 250)
1900 FTP_SetResponseError(nResCode
);
1903 hIC
= lpwfs
->lpAppInfo
;
1904 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1906 INTERNET_ASYNC_RESULT iar
;
1908 iar
.dwResult
= (DWORD
)bSuccess
;
1909 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1910 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1911 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1918 /***********************************************************************
1919 * FtpRemoveDirectoryA (WININET.@)
1921 * Remove a directory on the ftp server
1928 BOOL WINAPI
FtpRemoveDirectoryA(HINTERNET hFtpSession
, LPCSTR lpszDirectory
)
1930 LPWSTR lpwzDirectory
;
1933 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
1934 ret
= FtpRemoveDirectoryW(hFtpSession
, lpwzDirectory
);
1935 heap_free(lpwzDirectory
);
1939 static void AsyncFtpRemoveDirectoryProc(task_header_t
*hdr
)
1941 directory_task_t
*task
= (directory_task_t
*)hdr
;
1942 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1944 TRACE("%p\n", session
);
1946 FTP_FtpRemoveDirectoryW(session
, task
->directory
);
1947 heap_free(task
->directory
);
1950 /***********************************************************************
1951 * FtpRemoveDirectoryW (WININET.@)
1953 * Remove a directory on the ftp server
1960 BOOL WINAPI
FtpRemoveDirectoryW(HINTERNET hFtpSession
, LPCWSTR lpszDirectory
)
1962 ftp_session_t
*lpwfs
;
1963 appinfo_t
*hIC
= NULL
;
1966 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1969 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1973 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1975 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1979 if (lpwfs
->download_in_progress
!= NULL
)
1981 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1987 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1991 hIC
= lpwfs
->lpAppInfo
;
1992 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1994 directory_task_t
*task
;
1996 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpRemoveDirectoryProc
, sizeof(*task
));
1997 task
->directory
= heap_strdupW(lpszDirectory
);
1999 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
2003 r
= FTP_FtpRemoveDirectoryW(lpwfs
, lpszDirectory
);
2007 WININET_Release( &lpwfs
->hdr
);
2012 /***********************************************************************
2013 * FTP_FtpRemoveDirectoryW (Internal)
2015 * Remove a directory on the ftp server
2022 BOOL
FTP_FtpRemoveDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
2025 BOOL bSuccess
= FALSE
;
2026 appinfo_t
*hIC
= NULL
;
2030 /* Clear any error information */
2031 INTERNET_SetLastError(0);
2033 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RMD
, lpszDirectory
, 0, 0, 0))
2036 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2039 if (nResCode
== 250)
2042 FTP_SetResponseError(nResCode
);
2046 hIC
= lpwfs
->lpAppInfo
;
2047 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2049 INTERNET_ASYNC_RESULT iar
;
2051 iar
.dwResult
= (DWORD
)bSuccess
;
2052 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2053 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2054 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2061 /***********************************************************************
2062 * FtpRenameFileA (WININET.@)
2064 * Rename a file on the ftp server
2071 BOOL WINAPI
FtpRenameFileA(HINTERNET hFtpSession
, LPCSTR lpszSrc
, LPCSTR lpszDest
)
2077 lpwzSrc
= heap_strdupAtoW(lpszSrc
);
2078 lpwzDest
= heap_strdupAtoW(lpszDest
);
2079 ret
= FtpRenameFileW(hFtpSession
, lpwzSrc
, lpwzDest
);
2081 heap_free(lpwzDest
);
2089 } rename_file_task_t
;
2091 static void AsyncFtpRenameFileProc(task_header_t
*hdr
)
2093 rename_file_task_t
*task
= (rename_file_task_t
*)hdr
;
2094 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
2096 TRACE("%p\n", session
);
2098 FTP_FtpRenameFileW(session
, task
->src_file
, task
->dst_file
);
2099 heap_free(task
->src_file
);
2100 heap_free(task
->dst_file
);
2103 /***********************************************************************
2104 * FtpRenameFileW (WININET.@)
2106 * Rename a file on the ftp server
2113 BOOL WINAPI
FtpRenameFileW(HINTERNET hFtpSession
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2115 ftp_session_t
*lpwfs
;
2116 appinfo_t
*hIC
= NULL
;
2119 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
2122 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2126 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2128 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2132 if (lpwfs
->download_in_progress
!= NULL
)
2134 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2138 if (!lpszSrc
|| !lpszDest
)
2140 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2144 hIC
= lpwfs
->lpAppInfo
;
2145 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2147 rename_file_task_t
*task
;
2149 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpRenameFileProc
, sizeof(*task
));
2150 task
->src_file
= heap_strdupW(lpszSrc
);
2151 task
->dst_file
= heap_strdupW(lpszDest
);
2153 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
2157 r
= FTP_FtpRenameFileW(lpwfs
, lpszSrc
, lpszDest
);
2161 WININET_Release( &lpwfs
->hdr
);
2166 /***********************************************************************
2167 * FTP_FtpRenameFileW (Internal)
2169 * Rename a file on the ftp server
2176 BOOL
FTP_FtpRenameFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2179 BOOL bSuccess
= FALSE
;
2180 appinfo_t
*hIC
= NULL
;
2184 /* Clear any error information */
2185 INTERNET_SetLastError(0);
2187 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNFR
, lpszSrc
, 0, 0, 0))
2190 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2191 if (nResCode
== 350)
2193 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNTO
, lpszDest
, 0, 0, 0))
2196 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2199 if (nResCode
== 250)
2202 FTP_SetResponseError(nResCode
);
2205 hIC
= lpwfs
->lpAppInfo
;
2206 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2208 INTERNET_ASYNC_RESULT iar
;
2210 iar
.dwResult
= (DWORD
)bSuccess
;
2211 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2212 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2213 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2219 /***********************************************************************
2220 * FtpCommandA (WININET.@)
2222 BOOL WINAPI
FtpCommandA( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2223 LPCSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2228 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2229 debugstr_a(lpszCommand
), dwContext
, phFtpCommand
);
2231 if (fExpectResponse
)
2233 FIXME("data connection not supported\n");
2237 if (!lpszCommand
|| !lpszCommand
[0])
2239 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2243 if (!(cmdW
= heap_strdupAtoW(lpszCommand
)))
2245 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2249 r
= FtpCommandW(hConnect
, fExpectResponse
, dwFlags
, cmdW
, dwContext
, phFtpCommand
);
2255 /***********************************************************************
2256 * FtpCommandW (WININET.@)
2258 BOOL WINAPI
FtpCommandW( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2259 LPCWSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2262 ftp_session_t
*lpwfs
;
2264 DWORD len
, nBytesSent
= 0;
2265 INT nResCode
, nRC
= 0;
2267 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2268 debugstr_w(lpszCommand
), dwContext
, phFtpCommand
);
2270 if (!lpszCommand
|| !lpszCommand
[0])
2272 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2276 if (fExpectResponse
)
2278 FIXME("data connection not supported\n");
2282 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
2285 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2289 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2291 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2295 if (lpwfs
->download_in_progress
!= NULL
)
2297 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2301 len
= WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, NULL
, 0, NULL
, NULL
) + strlen(szCRLF
);
2302 if ((cmd
= heap_alloc(len
)))
2303 WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, cmd
, len
, NULL
, NULL
);
2306 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2310 strcat(cmd
, szCRLF
);
2313 TRACE("Sending (%s) len(%d)\n", cmd
, len
);
2314 while ((nBytesSent
< len
) && (nRC
!= -1))
2316 nRC
= send(lpwfs
->sndSocket
, cmd
+ nBytesSent
, len
- nBytesSent
, 0);
2320 TRACE("Sent %d bytes\n", nRC
);
2326 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2327 if (nResCode
> 0 && nResCode
< 400)
2330 FTP_SetResponseError(nResCode
);
2334 WININET_Release( &lpwfs
->hdr
);
2340 /***********************************************************************
2341 * FTPSESSION_Destroy (internal)
2343 * Deallocate session handle
2345 static void FTPSESSION_Destroy(object_header_t
*hdr
)
2347 ftp_session_t
*lpwfs
= (ftp_session_t
*) hdr
;
2351 WININET_Release(&lpwfs
->lpAppInfo
->hdr
);
2353 heap_free(lpwfs
->lpszPassword
);
2354 heap_free(lpwfs
->lpszUserName
);
2355 heap_free(lpwfs
->servername
);
2358 static void FTPSESSION_CloseConnection(object_header_t
*hdr
)
2360 ftp_session_t
*lpwfs
= (ftp_session_t
*) hdr
;
2364 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2365 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
2367 if (lpwfs
->download_in_progress
!= NULL
)
2368 lpwfs
->download_in_progress
->session_deleted
= TRUE
;
2370 if (lpwfs
->sndSocket
!= -1)
2371 closesocket(lpwfs
->sndSocket
);
2373 if (lpwfs
->lstnSocket
!= -1)
2374 closesocket(lpwfs
->lstnSocket
);
2376 if (lpwfs
->pasvSocket
!= -1)
2377 closesocket(lpwfs
->pasvSocket
);
2379 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2380 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
2383 static DWORD
FTPSESSION_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
2386 case INTERNET_OPTION_HANDLE_TYPE
:
2387 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2389 if (*size
< sizeof(ULONG
))
2390 return ERROR_INSUFFICIENT_BUFFER
;
2392 *size
= sizeof(DWORD
);
2393 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_FTP
;
2394 return ERROR_SUCCESS
;
2397 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
2400 static const object_vtbl_t FTPSESSIONVtbl
= {
2402 FTPSESSION_CloseConnection
,
2403 FTPSESSION_QueryOption
,
2413 /***********************************************************************
2414 * FTP_Connect (internal)
2416 * Connect to a ftp server
2419 * HINTERNET a session handle on success
2424 * Windows uses 'anonymous' as the username, when given a NULL username
2425 * and a NULL password. The password is first looked up in:
2427 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2429 * If this entry is not present it uses the current username as the password.
2433 HINTERNET
FTP_Connect(appinfo_t
*hIC
, LPCWSTR lpszServerName
,
2434 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
2435 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
2436 DWORD dwInternalFlags
)
2438 static const WCHAR szKey
[] = {'S','o','f','t','w','a','r','e','\\',
2439 'M','i','c','r','o','s','o','f','t','\\',
2440 'W','i','n','d','o','w','s','\\',
2441 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2442 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2443 static const WCHAR szValue
[] = {'E','m','a','i','l','N','a','m','e',0};
2444 static const WCHAR szDefaultUsername
[] = {'a','n','o','n','y','m','o','u','s','\0'};
2445 static const WCHAR szEmpty
[] = {'\0'};
2446 struct sockaddr_in socketAddr
;
2448 socklen_t sock_namelen
;
2449 BOOL bSuccess
= FALSE
;
2450 ftp_session_t
*lpwfs
= NULL
;
2451 char szaddr
[INET_ADDRSTRLEN
];
2453 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2454 hIC
, debugstr_w(lpszServerName
),
2455 nServerPort
, debugstr_w(lpszUserName
), debugstr_w(lpszPassword
));
2457 assert( hIC
->hdr
.htype
== WH_HINIT
);
2459 if ((!lpszUserName
|| !*lpszUserName
) && lpszPassword
&& *lpszPassword
)
2461 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2465 lpwfs
= alloc_object(&hIC
->hdr
, &FTPSESSIONVtbl
, sizeof(ftp_session_t
));
2468 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2472 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
2473 lpwfs
->serverport
= INTERNET_DEFAULT_FTP_PORT
;
2475 lpwfs
->serverport
= nServerPort
;
2477 lpwfs
->hdr
.htype
= WH_HFTPSESSION
;
2478 lpwfs
->hdr
.dwFlags
= dwFlags
;
2479 lpwfs
->hdr
.dwContext
= dwContext
;
2480 lpwfs
->hdr
.dwInternalFlags
|= dwInternalFlags
;
2481 lpwfs
->download_in_progress
= NULL
;
2482 lpwfs
->sndSocket
= -1;
2483 lpwfs
->lstnSocket
= -1;
2484 lpwfs
->pasvSocket
= -1;
2486 WININET_AddRef( &hIC
->hdr
);
2487 lpwfs
->lpAppInfo
= hIC
;
2488 list_add_head( &hIC
->hdr
.children
, &lpwfs
->hdr
.entry
);
2490 if(hIC
->proxy
&& hIC
->accessType
== INTERNET_OPEN_TYPE_PROXY
) {
2491 if(strchrW(hIC
->proxy
, ' '))
2492 FIXME("Several proxies not implemented.\n");
2493 if(hIC
->proxyBypass
)
2494 FIXME("Proxy bypass is ignored.\n");
2496 if (!lpszUserName
|| !strlenW(lpszUserName
)) {
2498 WCHAR szPassword
[MAX_PATH
];
2499 DWORD len
= sizeof(szPassword
);
2501 lpwfs
->lpszUserName
= heap_strdupW(szDefaultUsername
);
2503 RegOpenKeyW(HKEY_CURRENT_USER
, szKey
, &key
);
2504 if (RegQueryValueExW(key
, szValue
, NULL
, NULL
, (LPBYTE
)szPassword
, &len
)) {
2505 /* Nothing in the registry, get the username and use that as the password */
2506 if (!GetUserNameW(szPassword
, &len
)) {
2507 /* Should never get here, but use an empty password as failsafe */
2508 strcpyW(szPassword
, szEmpty
);
2513 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword
));
2514 lpwfs
->lpszPassword
= heap_strdupW(szPassword
);
2517 lpwfs
->lpszUserName
= heap_strdupW(lpszUserName
);
2518 lpwfs
->lpszPassword
= heap_strdupW(lpszPassword
? lpszPassword
: szEmpty
);
2520 lpwfs
->servername
= heap_strdupW(lpszServerName
);
2522 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2523 if (!(lpwfs
->hdr
.dwInternalFlags
& INET_OPENURL
))
2525 INTERNET_ASYNC_RESULT iar
;
2527 iar
.dwResult
= (DWORD_PTR
)lpwfs
->hdr
.hInternet
;
2528 iar
.dwError
= ERROR_SUCCESS
;
2530 SendAsyncCallback(&hIC
->hdr
, dwContext
,
2531 INTERNET_STATUS_HANDLE_CREATED
, &iar
,
2532 sizeof(INTERNET_ASYNC_RESULT
));
2535 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
2536 (LPWSTR
) lpszServerName
, (strlenW(lpszServerName
)+1) * sizeof(WCHAR
));
2538 sock_namelen
= sizeof(socketAddr
);
2539 if (!GetAddress(lpszServerName
, lpwfs
->serverport
, (struct sockaddr
*)&socketAddr
, &sock_namelen
))
2541 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
2545 if (socketAddr
.sin_family
!= AF_INET
)
2547 WARN("unsupported address family %d\n", socketAddr
.sin_family
);
2548 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2552 inet_ntop(socketAddr
.sin_family
, &socketAddr
.sin_addr
, szaddr
, sizeof(szaddr
));
2553 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
2554 szaddr
, strlen(szaddr
)+1);
2556 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2559 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2563 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTING_TO_SERVER
,
2564 szaddr
, strlen(szaddr
)+1);
2566 if (connect(nsocket
, (struct sockaddr
*)&socketAddr
, sock_namelen
) < 0)
2568 ERR("Unable to connect (%s)\n", strerror(errno
));
2569 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2570 closesocket(nsocket
);
2574 TRACE("Connected to server\n");
2575 lpwfs
->sndSocket
= nsocket
;
2576 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTED_TO_SERVER
,
2577 szaddr
, strlen(szaddr
)+1);
2579 sock_namelen
= sizeof(lpwfs
->socketAddress
);
2580 getsockname(nsocket
, (struct sockaddr
*) &lpwfs
->socketAddress
, &sock_namelen
);
2582 if (FTP_ConnectToHost(lpwfs
))
2584 TRACE("Successfully logged into server\n");
2593 WININET_Release( &lpwfs
->hdr
);
2597 return lpwfs
->hdr
.hInternet
;
2601 /***********************************************************************
2602 * FTP_ConnectToHost (internal)
2604 * Connect to a ftp server
2611 static BOOL
FTP_ConnectToHost(ftp_session_t
*lpwfs
)
2614 BOOL bSuccess
= FALSE
;
2617 FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2619 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_USER
, lpwfs
->lpszUserName
, 0, 0, 0))
2622 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2625 /* Login successful... */
2626 if (nResCode
== 230)
2628 /* User name okay, need password... */
2629 else if (nResCode
== 331)
2630 bSuccess
= FTP_SendPassword(lpwfs
);
2631 /* Need account for login... */
2632 else if (nResCode
== 332)
2633 bSuccess
= FTP_SendAccount(lpwfs
);
2635 FTP_SetResponseError(nResCode
);
2638 TRACE("Returning %d\n", bSuccess
);
2644 /***********************************************************************
2645 * FTP_SendCommandA (internal)
2647 * Send command to server
2654 static BOOL
FTP_SendCommandA(INT nSocket
, FTP_COMMAND ftpCmd
, LPCSTR lpszParam
,
2655 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2659 DWORD nBytesSent
= 0;
2663 TRACE("%d: (%s) %d\n", ftpCmd
, debugstr_a(lpszParam
), nSocket
);
2667 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2670 dwParamLen
= lpszParam
?strlen(lpszParam
)+1:0;
2671 len
= dwParamLen
+ strlen(szFtpCommands
[ftpCmd
]) + strlen(szCRLF
);
2672 if (NULL
== (buf
= heap_alloc(len
+1)))
2674 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2677 sprintf(buf
, "%s%s%s%s", szFtpCommands
[ftpCmd
], dwParamLen
? " " : "",
2678 dwParamLen
? lpszParam
: "", szCRLF
);
2680 TRACE("Sending (%s) len(%d)\n", buf
, len
);
2681 while((nBytesSent
< len
) && (nRC
!= -1))
2683 nRC
= send(nSocket
, buf
+nBytesSent
, len
- nBytesSent
, 0);
2690 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_REQUEST_SENT
,
2691 &nBytesSent
, sizeof(DWORD
));
2694 TRACE("Sent %d bytes\n", nBytesSent
);
2698 /***********************************************************************
2699 * FTP_SendCommand (internal)
2701 * Send command to server
2708 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
2709 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2712 LPSTR lpszParamA
= heap_strdupWtoA(lpszParam
);
2713 ret
= FTP_SendCommandA(nSocket
, ftpCmd
, lpszParamA
, lpfnStatusCB
, hdr
, dwContext
);
2714 heap_free(lpszParamA
);
2718 /***********************************************************************
2719 * FTP_ReceiveResponse (internal)
2721 * Receive response from server
2724 * Reply code on success
2728 INT
FTP_ReceiveResponse(ftp_session_t
*lpwfs
, DWORD_PTR dwContext
)
2730 LPSTR lpszResponse
= INTERNET_GetResponseBuffer();
2733 char firstprefix
[5];
2734 BOOL multiline
= FALSE
;
2736 TRACE("socket(%d)\n", lpwfs
->sndSocket
);
2738 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2742 if (!INTERNET_GetNextLine(lpwfs
->sndSocket
, &nRecv
))
2749 if(lpszResponse
[3] != '-')
2752 { /* Start of multiline response. Loop until we get "nnn " */
2754 memcpy(firstprefix
, lpszResponse
, 3);
2755 firstprefix
[3] = ' ';
2756 firstprefix
[4] = '\0';
2761 if(!memcmp(firstprefix
, lpszResponse
, 4))
2769 rc
= atoi(lpszResponse
);
2771 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
2772 &nRecv
, sizeof(DWORD
));
2776 TRACE("return %d\n", rc
);
2781 /***********************************************************************
2782 * FTP_SendPassword (internal)
2784 * Send password to ftp server
2791 static BOOL
FTP_SendPassword(ftp_session_t
*lpwfs
)
2794 BOOL bSuccess
= FALSE
;
2797 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASS
, lpwfs
->lpszPassword
, 0, 0, 0))
2800 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2803 TRACE("Received reply code %d\n", nResCode
);
2804 /* Login successful... */
2805 if (nResCode
== 230)
2807 /* Command not implemented, superfluous at the server site... */
2808 /* Need account for login... */
2809 else if (nResCode
== 332)
2810 bSuccess
= FTP_SendAccount(lpwfs
);
2812 FTP_SetResponseError(nResCode
);
2816 TRACE("Returning %d\n", bSuccess
);
2821 /***********************************************************************
2822 * FTP_SendAccount (internal)
2831 static BOOL
FTP_SendAccount(ftp_session_t
*lpwfs
)
2834 BOOL bSuccess
= FALSE
;
2837 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_ACCT
, szNoAccount
, 0, 0, 0))
2840 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2844 FTP_SetResponseError(nResCode
);
2851 /***********************************************************************
2852 * FTP_SendStore (internal)
2854 * Send request to upload file to ftp server
2861 static BOOL
FTP_SendStore(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2864 BOOL bSuccess
= FALSE
;
2867 if (!FTP_InitListenSocket(lpwfs
))
2870 if (!FTP_SendType(lpwfs
, dwType
))
2873 if (!FTP_SendPortOrPasv(lpwfs
))
2876 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_STOR
, lpszRemoteFile
, 0, 0, 0))
2878 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2881 if (nResCode
== 150 || nResCode
== 125)
2884 FTP_SetResponseError(nResCode
);
2888 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2890 closesocket(lpwfs
->lstnSocket
);
2891 lpwfs
->lstnSocket
= -1;
2898 /***********************************************************************
2899 * FTP_InitListenSocket (internal)
2901 * Create a socket to listen for server response
2908 static BOOL
FTP_InitListenSocket(ftp_session_t
*lpwfs
)
2910 BOOL bSuccess
= FALSE
;
2911 socklen_t namelen
= sizeof(lpwfs
->lstnSocketAddress
);
2915 lpwfs
->lstnSocket
= socket(PF_INET
, SOCK_STREAM
, 0);
2916 if (lpwfs
->lstnSocket
== -1)
2918 TRACE("Unable to create listening socket\n");
2922 /* We obtain our ip addr from the name of the command channel socket */
2923 lpwfs
->lstnSocketAddress
= lpwfs
->socketAddress
;
2925 /* and get the system to assign us a port */
2926 lpwfs
->lstnSocketAddress
.sin_port
= htons(0);
2928 if (bind(lpwfs
->lstnSocket
,(struct sockaddr
*) &lpwfs
->lstnSocketAddress
, sizeof(lpwfs
->lstnSocketAddress
)) == -1)
2930 TRACE("Unable to bind socket\n");
2934 if (listen(lpwfs
->lstnSocket
, MAX_BACKLOG
) == -1)
2936 TRACE("listen failed\n");
2940 if (getsockname(lpwfs
->lstnSocket
, (struct sockaddr
*) &lpwfs
->lstnSocketAddress
, &namelen
) != -1)
2944 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2946 closesocket(lpwfs
->lstnSocket
);
2947 lpwfs
->lstnSocket
= -1;
2954 /***********************************************************************
2955 * FTP_SendType (internal)
2957 * Tell server type of data being transferred
2963 * W98SE doesn't cache the type that's currently set
2964 * (i.e. it sends it always),
2965 * so we probably don't want to do that either.
2967 static BOOL
FTP_SendType(ftp_session_t
*lpwfs
, DWORD dwType
)
2970 WCHAR type
[] = { 'I','\0' };
2971 BOOL bSuccess
= FALSE
;
2974 if (dwType
& INTERNET_FLAG_TRANSFER_ASCII
)
2977 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_TYPE
, type
, 0, 0, 0))
2980 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
)/100;
2986 FTP_SetResponseError(nResCode
);
2994 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2995 /***********************************************************************
2996 * FTP_GetFileSize (internal)
2998 * Retrieves from the server the size of the given file
3005 static BOOL
FTP_GetFileSize(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD
*dwSize
)
3008 BOOL bSuccess
= FALSE
;
3012 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_SIZE
, lpszRemoteFile
, 0, 0, 0))
3015 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3018 if (nResCode
== 213) {
3019 /* Now parses the output to get the actual file size */
3021 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
3023 for (i
= 0; (lpszResponseBuffer
[i
] != ' ') && (lpszResponseBuffer
[i
] != '\0'); i
++) ;
3024 if (lpszResponseBuffer
[i
] == '\0') return FALSE
;
3025 *dwSize
= atol(&(lpszResponseBuffer
[i
+ 1]));
3029 FTP_SetResponseError(nResCode
);
3039 /***********************************************************************
3040 * FTP_SendPort (internal)
3042 * Tell server which port to use
3049 static BOOL
FTP_SendPort(ftp_session_t
*lpwfs
)
3051 static const WCHAR szIPFormat
[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
3053 WCHAR szIPAddress
[64];
3054 BOOL bSuccess
= FALSE
;
3057 sprintfW(szIPAddress
, szIPFormat
,
3058 lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x000000FF,
3059 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x0000FF00)>>8,
3060 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x00FF0000)>>16,
3061 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0xFF000000)>>24,
3062 lpwfs
->lstnSocketAddress
.sin_port
& 0xFF,
3063 (lpwfs
->lstnSocketAddress
.sin_port
& 0xFF00)>>8);
3065 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PORT
, szIPAddress
, 0, 0, 0))
3068 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3071 if (nResCode
== 200)
3074 FTP_SetResponseError(nResCode
);
3082 /***********************************************************************
3083 * FTP_DoPassive (internal)
3085 * Tell server that we want to do passive transfers
3086 * and connect data socket
3093 static BOOL
FTP_DoPassive(ftp_session_t
*lpwfs
)
3096 BOOL bSuccess
= FALSE
;
3099 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASV
, NULL
, 0, 0, 0))
3102 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3105 if (nResCode
== 227)
3107 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
3111 char *pAddr
, *pPort
;
3113 struct sockaddr_in dataSocketAddress
;
3115 p
= lpszResponseBuffer
+4; /* skip status code */
3116 while (*p
!= '\0' && (*p
< '0' || *p
> '9')) p
++;
3120 ERR("no address found in response, aborting\n");
3124 if (sscanf(p
, "%d,%d,%d,%d,%d,%d", &f
[0], &f
[1], &f
[2], &f
[3],
3127 ERR("unknown response address format '%s', aborting\n", p
);
3130 for (i
=0; i
< 6; i
++)
3133 dataSocketAddress
= lpwfs
->socketAddress
;
3134 pAddr
= (char *)&(dataSocketAddress
.sin_addr
.s_addr
);
3135 pPort
= (char *)&(dataSocketAddress
.sin_port
);
3143 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
3147 if (connect(nsocket
, (struct sockaddr
*)&dataSocketAddress
, sizeof(dataSocketAddress
)))
3149 ERR("can't connect passive FTP data port.\n");
3150 closesocket(nsocket
);
3153 lpwfs
->pasvSocket
= nsocket
;
3157 FTP_SetResponseError(nResCode
);
3165 static BOOL
FTP_SendPortOrPasv(ftp_session_t
*lpwfs
)
3167 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3169 if (!FTP_DoPassive(lpwfs
))
3174 if (!FTP_SendPort(lpwfs
))
3181 /***********************************************************************
3182 * FTP_GetDataSocket (internal)
3184 * Either accepts an incoming data socket connection from the server
3185 * or just returns the already opened socket after a PASV command
3186 * in case of passive FTP.
3194 static BOOL
FTP_GetDataSocket(ftp_session_t
*lpwfs
, LPINT nDataSocket
)
3196 struct sockaddr_in saddr
;
3197 socklen_t addrlen
= sizeof(struct sockaddr
);
3200 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3202 *nDataSocket
= lpwfs
->pasvSocket
;
3203 lpwfs
->pasvSocket
= -1;
3207 *nDataSocket
= accept(lpwfs
->lstnSocket
, (struct sockaddr
*) &saddr
, &addrlen
);
3208 closesocket(lpwfs
->lstnSocket
);
3209 lpwfs
->lstnSocket
= -1;
3211 return *nDataSocket
!= -1;
3215 /***********************************************************************
3216 * FTP_SendData (internal)
3218 * Send data to the server
3225 static BOOL
FTP_SendData(ftp_session_t
*lpwfs
, INT nDataSocket
, HANDLE hFile
)
3227 BY_HANDLE_FILE_INFORMATION fi
;
3228 DWORD nBytesRead
= 0;
3229 DWORD nBytesSent
= 0;
3230 DWORD nTotalSent
= 0;
3231 DWORD nBytesToSend
, nLen
;
3233 time_t s_long_time
, e_long_time
;
3238 lpszBuffer
= heap_alloc_zero(sizeof(CHAR
)*DATA_PACKET_SIZE
);
3240 /* Get the size of the file. */
3241 GetFileInformationByHandle(hFile
, &fi
);
3246 nBytesToSend
= nBytesRead
- nBytesSent
;
3248 if (nBytesToSend
<= 0)
3250 /* Read data from file. */
3252 if (!ReadFile(hFile
, lpszBuffer
, DATA_PACKET_SIZE
, &nBytesRead
, 0))
3253 ERR("Failed reading from file\n");
3256 nBytesToSend
= nBytesRead
;
3261 nLen
= DATA_PACKET_SIZE
< nBytesToSend
?
3262 DATA_PACKET_SIZE
: nBytesToSend
;
3263 nRC
= send(nDataSocket
, lpszBuffer
, nLen
, 0);
3271 /* Do some computation to display the status. */
3273 nSeconds
= e_long_time
- s_long_time
;
3274 if( nSeconds
/ 60 > 0 )
3276 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
3277 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
/ 60,
3278 nSeconds
% 60, (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3282 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
3283 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
,
3284 (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3286 } while (nRC
!= -1);
3288 TRACE("file transfer complete!\n");
3290 heap_free(lpszBuffer
);
3295 /***********************************************************************
3296 * FTP_SendRetrieve (internal)
3298 * Send request to retrieve a file
3301 * Number of bytes to be received on success
3305 static BOOL
FTP_SendRetrieve(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
3311 if (!(ret
= FTP_InitListenSocket(lpwfs
)))
3314 if (!(ret
= FTP_SendType(lpwfs
, dwType
)))
3317 if (!(ret
= FTP_SendPortOrPasv(lpwfs
)))
3320 if (!(ret
= FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RETR
, lpszRemoteFile
, 0, 0, 0)))
3323 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3324 if ((nResCode
!= 125) && (nResCode
!= 150)) {
3325 /* That means that we got an error getting the file. */
3326 FTP_SetResponseError(nResCode
);
3331 if (!ret
&& lpwfs
->lstnSocket
!= -1)
3333 closesocket(lpwfs
->lstnSocket
);
3334 lpwfs
->lstnSocket
= -1;
3341 /***********************************************************************
3342 * FTP_RetrieveData (internal)
3344 * Retrieve data from server
3351 static BOOL
FTP_RetrieveFileData(ftp_session_t
*lpwfs
, INT nDataSocket
, HANDLE hFile
)
3353 DWORD nBytesWritten
;
3359 lpszBuffer
= heap_alloc_zero(sizeof(CHAR
)*DATA_PACKET_SIZE
);
3360 if (NULL
== lpszBuffer
)
3362 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3368 nRC
= recv(nDataSocket
, lpszBuffer
, DATA_PACKET_SIZE
, 0);
3371 /* other side closed socket. */
3374 WriteFile(hFile
, lpszBuffer
, nRC
, &nBytesWritten
, NULL
);
3378 TRACE("Data transfer complete\n");
3381 heap_free(lpszBuffer
);
3385 /***********************************************************************
3386 * FTPFINDNEXT_Destroy (internal)
3388 * Deallocate session handle
3390 static void FTPFINDNEXT_Destroy(object_header_t
*hdr
)
3392 LPWININETFTPFINDNEXTW lpwfn
= (LPWININETFTPFINDNEXTW
) hdr
;
3397 WININET_Release(&lpwfn
->lpFtpSession
->hdr
);
3399 for (i
= 0; i
< lpwfn
->size
; i
++)
3401 heap_free(lpwfn
->lpafp
[i
].lpszName
);
3403 heap_free(lpwfn
->lpafp
);
3406 static DWORD
FTPFINDNEXT_FindNextFileProc(WININETFTPFINDNEXTW
*find
, LPVOID data
)
3408 WIN32_FIND_DATAW
*find_data
= data
;
3409 DWORD res
= ERROR_SUCCESS
;
3411 TRACE("index(%d) size(%d)\n", find
->index
, find
->size
);
3413 ZeroMemory(find_data
, sizeof(WIN32_FIND_DATAW
));
3415 if (find
->index
< find
->size
) {
3416 FTP_ConvertFileProp(&find
->lpafp
[find
->index
], find_data
);
3419 TRACE("Name: %s\nSize: %d\n", debugstr_w(find_data
->cFileName
), find_data
->nFileSizeLow
);
3421 res
= ERROR_NO_MORE_FILES
;
3424 if (find
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3426 INTERNET_ASYNC_RESULT iar
;
3428 iar
.dwResult
= (res
== ERROR_SUCCESS
);
3431 INTERNET_SendCallback(&find
->hdr
, find
->hdr
.dwContext
,
3432 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3433 sizeof(INTERNET_ASYNC_RESULT
));
3441 WIN32_FIND_DATAW
*find_data
;
3444 static void FTPFINDNEXT_AsyncFindNextFileProc(task_header_t
*hdr
)
3446 find_next_task_t
*task
= (find_next_task_t
*)hdr
;
3448 FTPFINDNEXT_FindNextFileProc((WININETFTPFINDNEXTW
*)task
->hdr
.hdr
, task
->find_data
);
3451 static DWORD
FTPFINDNEXT_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
3454 case INTERNET_OPTION_HANDLE_TYPE
:
3455 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3457 if (*size
< sizeof(ULONG
))
3458 return ERROR_INSUFFICIENT_BUFFER
;
3460 *size
= sizeof(DWORD
);
3461 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_FTP_FIND
;
3462 return ERROR_SUCCESS
;
3465 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
3468 static DWORD
FTPFINDNEXT_FindNextFileW(object_header_t
*hdr
, void *data
)
3470 WININETFTPFINDNEXTW
*find
= (WININETFTPFINDNEXTW
*)hdr
;
3472 if (find
->lpFtpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3474 find_next_task_t
*task
;
3476 task
= alloc_async_task(&find
->hdr
, FTPFINDNEXT_AsyncFindNextFileProc
, sizeof(*task
));
3477 task
->find_data
= data
;
3479 INTERNET_AsyncCall(&task
->hdr
);
3480 return ERROR_SUCCESS
;
3483 return FTPFINDNEXT_FindNextFileProc(find
, data
);
3486 static const object_vtbl_t FTPFINDNEXTVtbl
= {
3487 FTPFINDNEXT_Destroy
,
3489 FTPFINDNEXT_QueryOption
,
3495 FTPFINDNEXT_FindNextFileW
3498 /***********************************************************************
3499 * FTP_ReceiveFileList (internal)
3501 * Read file list from server
3504 * Handle to file list on success
3508 static HINTERNET
FTP_ReceiveFileList(ftp_session_t
*lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3509 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
)
3512 LPFILEPROPERTIESW lpafp
= NULL
;
3513 LPWININETFTPFINDNEXTW lpwfn
= NULL
;
3515 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs
, nSocket
, debugstr_w(lpszSearchFile
), lpFindFileData
, dwContext
);
3517 if (FTP_ParseDirectory(lpwfs
, nSocket
, lpszSearchFile
, &lpafp
, &dwSize
))
3520 FTP_ConvertFileProp(lpafp
, lpFindFileData
);
3522 lpwfn
= alloc_object(&lpwfs
->hdr
, &FTPFINDNEXTVtbl
, sizeof(WININETFTPFINDNEXTW
));
3525 lpwfn
->hdr
.htype
= WH_HFTPFINDNEXT
;
3526 lpwfn
->hdr
.dwContext
= dwContext
;
3527 lpwfn
->index
= 1; /* Next index is 1 since we return index 0 */
3528 lpwfn
->size
= dwSize
;
3529 lpwfn
->lpafp
= lpafp
;
3531 WININET_AddRef( &lpwfs
->hdr
);
3532 lpwfn
->lpFtpSession
= lpwfs
;
3533 list_add_head( &lpwfs
->hdr
.children
, &lpwfn
->hdr
.entry
);
3537 TRACE("Matched %d files\n", dwSize
);
3538 return lpwfn
? lpwfn
->hdr
.hInternet
: NULL
;
3542 /***********************************************************************
3543 * FTP_ConvertFileProp (internal)
3545 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3552 static BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
)
3554 BOOL bSuccess
= FALSE
;
3556 ZeroMemory(lpFindFileData
, sizeof(WIN32_FIND_DATAW
));
3560 SystemTimeToFileTime( &lpafp
->tmLastModified
, &lpFindFileData
->ftLastAccessTime
);
3561 lpFindFileData
->ftLastWriteTime
= lpFindFileData
->ftLastAccessTime
;
3562 lpFindFileData
->ftCreationTime
= lpFindFileData
->ftLastAccessTime
;
3564 /* Not all fields are filled in */
3565 lpFindFileData
->nFileSizeHigh
= 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3566 lpFindFileData
->nFileSizeLow
= lpafp
->nSize
;
3568 if (lpafp
->bIsDirectory
)
3569 lpFindFileData
->dwFileAttributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3571 if (lpafp
->lpszName
)
3572 lstrcpynW(lpFindFileData
->cFileName
, lpafp
->lpszName
, MAX_PATH
);
3580 /***********************************************************************
3581 * FTP_ParseNextFile (internal)
3583 * Parse the next line in file listing
3589 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW lpfp
)
3591 static const char szSpace
[] = " \t";
3599 lpfp
->lpszName
= NULL
;
3601 if(!(pszLine
= INTERNET_GetNextLine(nSocket
, &nBufLen
)))
3604 pszToken
= strtok(pszLine
, szSpace
);
3606 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3609 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3611 if(!isdigit(pszToken
[0]) && 10 == strlen(pszToken
)) {
3612 if(!FTP_ParsePermission(pszToken
, lpfp
))
3613 lpfp
->bIsDirectory
= FALSE
;
3614 for(i
=0; i
<=3; i
++) {
3615 if(!(pszToken
= strtok(NULL
, szSpace
)))
3618 if(!pszToken
) continue;
3619 if(lpfp
->bIsDirectory
) {
3620 TRACE("Is directory\n");
3624 TRACE("Size: %s\n", pszToken
);
3625 lpfp
->nSize
= atol(pszToken
);
3628 lpfp
->tmLastModified
.wSecond
= 0;
3629 lpfp
->tmLastModified
.wMinute
= 0;
3630 lpfp
->tmLastModified
.wHour
= 0;
3631 lpfp
->tmLastModified
.wDay
= 0;
3632 lpfp
->tmLastModified
.wMonth
= 0;
3633 lpfp
->tmLastModified
.wYear
= 0;
3635 /* Determine month */
3636 pszToken
= strtok(NULL
, szSpace
);
3637 if(!pszToken
) continue;
3638 if(strlen(pszToken
) >= 3) {
3640 if((pszTmp
= StrStrIA(szMonths
, pszToken
)))
3641 lpfp
->tmLastModified
.wMonth
= ((pszTmp
- szMonths
) / 3)+1;
3644 pszToken
= strtok(NULL
, szSpace
);
3645 if(!pszToken
) continue;
3646 lpfp
->tmLastModified
.wDay
= atoi(pszToken
);
3647 /* Determine time or year */
3648 pszToken
= strtok(NULL
, szSpace
);
3649 if(!pszToken
) continue;
3650 if((pszTmp
= strchr(pszToken
, ':'))) {
3651 SYSTEMTIME curr_time
;
3654 lpfp
->tmLastModified
.wMinute
= atoi(pszTmp
);
3655 lpfp
->tmLastModified
.wHour
= atoi(pszToken
);
3656 GetLocalTime( &curr_time
);
3657 lpfp
->tmLastModified
.wYear
= curr_time
.wYear
;
3660 lpfp
->tmLastModified
.wYear
= atoi(pszToken
);
3661 lpfp
->tmLastModified
.wHour
= 12;
3663 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3664 lpfp
->tmLastModified
.wHour
, lpfp
->tmLastModified
.wMinute
, lpfp
->tmLastModified
.wSecond
,
3665 lpfp
->tmLastModified
.wYear
, lpfp
->tmLastModified
.wMonth
, lpfp
->tmLastModified
.wDay
);
3667 pszToken
= strtok(NULL
, szSpace
);
3668 if(!pszToken
) continue;
3669 lpfp
->lpszName
= heap_strdupAtoW(pszToken
);
3670 TRACE("File: %s\n", debugstr_w(lpfp
->lpszName
));
3672 /* NT way of parsing ... :
3674 07-13-03 08:55PM <DIR> sakpatch
3675 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3677 else if(isdigit(pszToken
[0]) && 8 == strlen(pszToken
)) {
3678 int mon
, mday
, year
, hour
, min
;
3679 lpfp
->permissions
= 0xFFFF; /* No idea, put full permission :-) */
3681 sscanf(pszToken
, "%d-%d-%d", &mon
, &mday
, &year
);
3682 lpfp
->tmLastModified
.wDay
= mday
;
3683 lpfp
->tmLastModified
.wMonth
= mon
;
3684 lpfp
->tmLastModified
.wYear
= year
;
3686 /* Hacky and bad Y2K protection :-) */
3687 if (lpfp
->tmLastModified
.wYear
< 70) lpfp
->tmLastModified
.wYear
+= 2000;
3689 pszToken
= strtok(NULL
, szSpace
);
3690 if(!pszToken
) continue;
3691 sscanf(pszToken
, "%d:%d", &hour
, &min
);
3692 lpfp
->tmLastModified
.wHour
= hour
;
3693 lpfp
->tmLastModified
.wMinute
= min
;
3694 if((pszToken
[5] == 'P') && (pszToken
[6] == 'M')) {
3695 lpfp
->tmLastModified
.wHour
+= 12;
3697 lpfp
->tmLastModified
.wSecond
= 0;
3699 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3700 lpfp
->tmLastModified
.wHour
, lpfp
->tmLastModified
.wMinute
, lpfp
->tmLastModified
.wSecond
,
3701 lpfp
->tmLastModified
.wYear
, lpfp
->tmLastModified
.wMonth
, lpfp
->tmLastModified
.wDay
);
3703 pszToken
= strtok(NULL
, szSpace
);
3704 if(!pszToken
) continue;
3705 if(!strcasecmp(pszToken
, "<DIR>")) {
3706 lpfp
->bIsDirectory
= TRUE
;
3708 TRACE("Is directory\n");
3711 lpfp
->bIsDirectory
= FALSE
;
3712 lpfp
->nSize
= atol(pszToken
);
3713 TRACE("Size: %d\n", lpfp
->nSize
);
3716 pszToken
= strtok(NULL
, szSpace
);
3717 if(!pszToken
) continue;
3718 lpfp
->lpszName
= heap_strdupAtoW(pszToken
);
3719 TRACE("Name: %s\n", debugstr_w(lpfp
->lpszName
));
3721 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3722 else if(pszToken
[0] == '+') {
3723 FIXME("EPLF Format not implemented\n");
3726 if(lpfp
->lpszName
) {
3727 if((lpszSearchFile
== NULL
) ||
3728 (PathMatchSpecW(lpfp
->lpszName
, lpszSearchFile
))) {
3730 TRACE("Matched: %s\n", debugstr_w(lpfp
->lpszName
));
3733 heap_free(lpfp
->lpszName
);
3734 lpfp
->lpszName
= NULL
;
3741 /***********************************************************************
3742 * FTP_ParseDirectory (internal)
3744 * Parse string of directory information
3750 static BOOL
FTP_ParseDirectory(ftp_session_t
*lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3751 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
)
3753 BOOL bSuccess
= TRUE
;
3754 INT sizeFilePropArray
= 500;/*20; */
3755 INT indexFilePropArray
= -1;
3759 /* Allocate initial file properties array */
3760 *lpafp
= heap_alloc_zero(sizeof(FILEPROPERTIESW
)*(sizeFilePropArray
));
3765 if (indexFilePropArray
+1 >= sizeFilePropArray
)
3767 LPFILEPROPERTIESW tmpafp
;
3769 sizeFilePropArray
*= 2;
3770 tmpafp
= heap_realloc_zero(*lpafp
, sizeof(FILEPROPERTIESW
)*sizeFilePropArray
);
3779 indexFilePropArray
++;
3780 } while (FTP_ParseNextFile(nSocket
, lpszSearchFile
, &(*lpafp
)[indexFilePropArray
]));
3782 if (bSuccess
&& indexFilePropArray
)
3784 if (indexFilePropArray
< sizeFilePropArray
- 1)
3786 LPFILEPROPERTIESW tmpafp
;
3788 tmpafp
= heap_realloc(*lpafp
, sizeof(FILEPROPERTIESW
)*indexFilePropArray
);
3792 *dwfp
= indexFilePropArray
;
3797 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
3805 /***********************************************************************
3806 * FTP_ParsePermission (internal)
3808 * Parse permission string of directory information
3815 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
)
3817 BOOL bSuccess
= TRUE
;
3818 unsigned short nPermission
= 0;
3823 if ((*lpszPermission
!= 'd') && (*lpszPermission
!= '-') && (*lpszPermission
!= 'l'))
3829 lpfp
->bIsDirectory
= (*lpszPermission
== 'd');
3835 nPermission
|= (*(lpszPermission
+1) == 'r' ? 1 : 0) << 8;
3838 nPermission
|= (*(lpszPermission
+2) == 'w' ? 1 : 0) << 7;
3841 nPermission
|= (*(lpszPermission
+3) == 'x' ? 1 : 0) << 6;
3844 nPermission
|= (*(lpszPermission
+4) == 'r' ? 1 : 0) << 5;
3847 nPermission
|= (*(lpszPermission
+5) == 'w' ? 1 : 0) << 4;
3850 nPermission
|= (*(lpszPermission
+6) == 'x' ? 1 : 0) << 3;
3853 nPermission
|= (*(lpszPermission
+7) == 'r' ? 1 : 0) << 2;
3856 nPermission
|= (*(lpszPermission
+8) == 'w' ? 1 : 0) << 1;
3859 nPermission
|= (*(lpszPermission
+9) == 'x' ? 1 : 0);
3863 }while (nPos
<= nLast
);
3865 lpfp
->permissions
= nPermission
;
3870 /***********************************************************************
3871 * FTP_SetResponseError (internal)
3873 * Set the appropriate error code for a given response from the server
3878 static DWORD
FTP_SetResponseError(DWORD dwResponse
)
3884 case 425: /* Cannot open data connection. */
3885 dwCode
= ERROR_INTERNET_CANNOT_CONNECT
;
3888 case 426: /* Connection closed, transer aborted. */
3889 dwCode
= ERROR_INTERNET_CONNECTION_ABORTED
;
3892 case 530: /* Not logged in. Login incorrect. */
3893 dwCode
= ERROR_INTERNET_LOGIN_FAILURE
;
3896 case 421: /* Service not available - Server may be shutting down. */
3897 case 450: /* File action not taken. File may be busy. */
3898 case 451: /* Action aborted. Server error. */
3899 case 452: /* Action not taken. Insufficient storage space on server. */
3900 case 500: /* Syntax error. Command unrecognized. */
3901 case 501: /* Syntax error. Error in parameters or arguments. */
3902 case 502: /* Command not implemented. */
3903 case 503: /* Bad sequence of commands. */
3904 case 504: /* Command not implemented for that parameter. */
3905 case 532: /* Need account for storing files */
3906 case 550: /* File action not taken. File not found or no access. */
3907 case 551: /* Requested action aborted. Page type unknown */
3908 case 552: /* Action aborted. Exceeded storage allocation */
3909 case 553: /* Action not taken. File name not allowed. */
3912 dwCode
= ERROR_INTERNET_EXTENDED_ERROR
;
3916 INTERNET_SetLastError(dwCode
);