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
32 typedef struct _ftp_session_t ftp_session_t
;
37 ftp_session_t
*lpFtpSession
;
41 HANDLE cache_file_handle
;
50 int pasvSocket
; /* data socket connected by us in case of passive FTP */
51 ftp_file_t
*download_in_progress
;
52 struct sockaddr_in socketAddress
;
53 struct sockaddr_in lstnSocketAddress
;
55 INTERNET_PORT serverport
;
65 SYSTEMTIME tmLastModified
;
66 unsigned short permissions
;
67 } FILEPROPERTIESW
, *LPFILEPROPERTIESW
;
72 ftp_session_t
*lpFtpSession
;
75 LPFILEPROPERTIESW lpafp
;
76 } WININETFTPFINDNEXTW
, *LPWININETFTPFINDNEXTW
;
78 #define DATA_PACKET_SIZE 0x2000
82 /* Testing shows that Windows only accepts dwFlags where the last
83 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
85 #define FTP_CONDITION_MASK 0x0007
88 /* FTP commands with arguments. */
104 /* FTP commands without arguments. */
113 static const CHAR
*const szFtpCommands
[] = {
136 static const CHAR szMonths
[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
137 static const WCHAR szNoAccount
[] = {'n','o','a','c','c','o','u','n','t','\0'};
139 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
140 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
);
141 static BOOL
FTP_SendStore(ftp_session_t
*, LPCWSTR lpszRemoteFile
, DWORD dwType
);
142 static BOOL
FTP_GetDataSocket(ftp_session_t
*, LPINT nDataSocket
);
143 static BOOL
FTP_SendData(ftp_session_t
*, INT nDataSocket
, HANDLE hFile
);
144 static INT
FTP_ReceiveResponse(ftp_session_t
*, DWORD_PTR dwContext
);
145 static BOOL
FTP_SendRetrieve(ftp_session_t
*, LPCWSTR lpszRemoteFile
, DWORD dwType
);
146 static BOOL
FTP_RetrieveFileData(ftp_session_t
*, INT nDataSocket
, HANDLE hFile
);
147 static BOOL
FTP_InitListenSocket(ftp_session_t
*);
148 static BOOL
FTP_ConnectToHost(ftp_session_t
*);
149 static BOOL
FTP_SendPassword(ftp_session_t
*);
150 static BOOL
FTP_SendAccount(ftp_session_t
*);
151 static BOOL
FTP_SendType(ftp_session_t
*, DWORD dwType
);
152 static BOOL
FTP_SendPort(ftp_session_t
*);
153 static BOOL
FTP_DoPassive(ftp_session_t
*);
154 static BOOL
FTP_SendPortOrPasv(ftp_session_t
*);
155 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
);
156 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW fileprop
);
157 static BOOL
FTP_ParseDirectory(ftp_session_t
*, INT nSocket
, LPCWSTR lpszSearchFile
,
158 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
);
159 static HINTERNET
FTP_ReceiveFileList(ftp_session_t
*, INT nSocket
, LPCWSTR lpszSearchFile
,
160 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
);
161 static DWORD
FTP_SetResponseError(DWORD dwResponse
);
162 static BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
);
163 static BOOL
FTP_FtpPutFileW(ftp_session_t
*, LPCWSTR lpszLocalFile
,
164 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
);
165 static BOOL
FTP_FtpSetCurrentDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
166 static BOOL
FTP_FtpCreateDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
167 static HINTERNET
FTP_FtpFindFirstFileW(ftp_session_t
*,
168 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
);
169 static BOOL
FTP_FtpGetCurrentDirectoryW(ftp_session_t
*, LPWSTR lpszCurrentDirectory
,
170 LPDWORD lpdwCurrentDirectory
);
171 static BOOL
FTP_FtpRenameFileW(ftp_session_t
*, LPCWSTR lpszSrc
, LPCWSTR lpszDest
);
172 static BOOL
FTP_FtpRemoveDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
173 static BOOL
FTP_FtpDeleteFileW(ftp_session_t
*, LPCWSTR lpszFileName
);
174 static BOOL
FTP_FtpGetFileW(ftp_session_t
*, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
175 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
176 DWORD_PTR dwContext
);
178 /* A temporary helper until we get rid of INTERNET_GetLastError calls */
179 static BOOL
res_to_le(DWORD res
)
181 if(res
!= ERROR_SUCCESS
)
182 INTERNET_SetLastError(res
);
183 return res
== ERROR_SUCCESS
;
186 /***********************************************************************
187 * FtpPutFileA (WININET.@)
189 * Uploads a file to the FTP server
196 BOOL WINAPI
FtpPutFileA(HINTERNET hConnect
, LPCSTR lpszLocalFile
,
197 LPCSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
199 LPWSTR lpwzLocalFile
;
200 LPWSTR lpwzNewRemoteFile
;
203 lpwzLocalFile
= heap_strdupAtoW(lpszLocalFile
);
204 lpwzNewRemoteFile
= heap_strdupAtoW(lpszNewRemoteFile
);
205 ret
= FtpPutFileW(hConnect
, lpwzLocalFile
, lpwzNewRemoteFile
,
207 heap_free(lpwzLocalFile
);
208 heap_free(lpwzNewRemoteFile
);
220 static void AsyncFtpPutFileProc(task_header_t
*hdr
)
222 put_file_task_t
*task
= (put_file_task_t
*)hdr
;
223 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
225 TRACE("%p\n", session
);
227 FTP_FtpPutFileW(session
, task
->local_file
, task
->remote_file
,
228 task
->flags
, task
->context
);
230 heap_free(task
->local_file
);
231 heap_free(task
->remote_file
);
234 /***********************************************************************
235 * FtpPutFileW (WININET.@)
237 * Uploads a file to the FTP server
244 BOOL WINAPI
FtpPutFileW(HINTERNET hConnect
, LPCWSTR lpszLocalFile
,
245 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
247 ftp_session_t
*lpwfs
;
248 appinfo_t
*hIC
= NULL
;
251 if (!lpszLocalFile
|| !lpszNewRemoteFile
)
253 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
257 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
260 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
264 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
266 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
270 if (lpwfs
->download_in_progress
!= NULL
)
272 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
276 if ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
278 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
282 hIC
= lpwfs
->lpAppInfo
;
283 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
285 put_file_task_t
*task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpPutFileProc
, sizeof(*task
));
287 task
->local_file
= heap_strdupW(lpszLocalFile
);
288 task
->remote_file
= heap_strdupW(lpszNewRemoteFile
);
289 task
->flags
= dwFlags
;
290 task
->context
= dwContext
;
292 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
296 r
= FTP_FtpPutFileW(lpwfs
, lpszLocalFile
,
297 lpszNewRemoteFile
, dwFlags
, dwContext
);
301 WININET_Release( &lpwfs
->hdr
);
306 /***********************************************************************
307 * FTP_FtpPutFileW (Internal)
309 * Uploads a file to the FTP server
316 static BOOL
FTP_FtpPutFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszLocalFile
,
317 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
320 BOOL bSuccess
= FALSE
;
321 appinfo_t
*hIC
= NULL
;
324 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile
), debugstr_w(lpszNewRemoteFile
));
326 /* Clear any error information */
327 INTERNET_SetLastError(0);
329 /* Open file to be uploaded */
330 if (INVALID_HANDLE_VALUE
==
331 (hFile
= CreateFileW(lpszLocalFile
, GENERIC_READ
, 0, 0, OPEN_EXISTING
, 0, 0)))
332 /* Let CreateFile set the appropriate error */
335 hIC
= lpwfs
->lpAppInfo
;
337 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
339 if (FTP_SendStore(lpwfs
, lpszNewRemoteFile
, dwFlags
))
343 /* Get data socket to server */
344 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
346 FTP_SendData(lpwfs
, nDataSocket
, hFile
);
347 closesocket(nDataSocket
);
348 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
354 FTP_SetResponseError(nResCode
);
359 if (lpwfs
->lstnSocket
!= -1)
361 closesocket(lpwfs
->lstnSocket
);
362 lpwfs
->lstnSocket
= -1;
365 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
367 INTERNET_ASYNC_RESULT iar
;
369 iar
.dwResult
= (DWORD
)bSuccess
;
370 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
371 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
372 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
381 /***********************************************************************
382 * FtpSetCurrentDirectoryA (WININET.@)
384 * Change the working directory on the FTP server
391 BOOL WINAPI
FtpSetCurrentDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
393 LPWSTR lpwzDirectory
;
396 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
397 ret
= FtpSetCurrentDirectoryW(hConnect
, lpwzDirectory
);
398 heap_free(lpwzDirectory
);
407 static void AsyncFtpSetCurrentDirectoryProc(task_header_t
*hdr
)
409 directory_task_t
*task
= (directory_task_t
*)hdr
;
410 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
412 TRACE("%p\n", session
);
414 FTP_FtpSetCurrentDirectoryW(session
, task
->directory
);
415 heap_free(task
->directory
);
418 /***********************************************************************
419 * FtpSetCurrentDirectoryW (WININET.@)
421 * Change the working directory on the FTP server
428 BOOL WINAPI
FtpSetCurrentDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
430 ftp_session_t
*lpwfs
= NULL
;
431 appinfo_t
*hIC
= NULL
;
436 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
440 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
441 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
443 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
447 if (lpwfs
->download_in_progress
!= NULL
)
449 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
453 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
455 hIC
= lpwfs
->lpAppInfo
;
456 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
458 directory_task_t
*task
;
460 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpSetCurrentDirectoryProc
, sizeof(*task
));
461 task
->directory
= heap_strdupW(lpszDirectory
);
463 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
467 r
= FTP_FtpSetCurrentDirectoryW(lpwfs
, lpszDirectory
);
472 WININET_Release( &lpwfs
->hdr
);
478 /***********************************************************************
479 * FTP_FtpSetCurrentDirectoryW (Internal)
481 * Change the working directory on the FTP server
488 static BOOL
FTP_FtpSetCurrentDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
491 appinfo_t
*hIC
= NULL
;
492 BOOL bSuccess
= FALSE
;
494 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
496 /* Clear any error information */
497 INTERNET_SetLastError(0);
499 hIC
= lpwfs
->lpAppInfo
;
500 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_CWD
, lpszDirectory
,
501 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
504 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
511 FTP_SetResponseError(nResCode
);
515 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
517 INTERNET_ASYNC_RESULT iar
;
519 iar
.dwResult
= bSuccess
;
520 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
521 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
522 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
528 /***********************************************************************
529 * FtpCreateDirectoryA (WININET.@)
531 * Create new directory on the FTP server
538 BOOL WINAPI
FtpCreateDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
540 LPWSTR lpwzDirectory
;
543 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
544 ret
= FtpCreateDirectoryW(hConnect
, lpwzDirectory
);
545 heap_free(lpwzDirectory
);
550 static void AsyncFtpCreateDirectoryProc(task_header_t
*hdr
)
552 directory_task_t
*task
= (directory_task_t
*)hdr
;
553 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
555 TRACE(" %p\n", session
);
557 FTP_FtpCreateDirectoryW(session
, task
->directory
);
558 heap_free(task
->directory
);
561 /***********************************************************************
562 * FtpCreateDirectoryW (WININET.@)
564 * Create new directory on the FTP server
571 BOOL WINAPI
FtpCreateDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
573 ftp_session_t
*lpwfs
;
574 appinfo_t
*hIC
= NULL
;
577 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
580 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
584 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
586 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
590 if (lpwfs
->download_in_progress
!= NULL
)
592 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
598 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
602 hIC
= lpwfs
->lpAppInfo
;
603 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
605 directory_task_t
*task
;
607 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpCreateDirectoryProc
, sizeof(*task
));
608 task
->directory
= heap_strdupW(lpszDirectory
);
610 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
614 r
= FTP_FtpCreateDirectoryW(lpwfs
, lpszDirectory
);
617 WININET_Release( &lpwfs
->hdr
);
623 /***********************************************************************
624 * FTP_FtpCreateDirectoryW (Internal)
626 * Create new directory on the FTP server
633 static BOOL
FTP_FtpCreateDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
636 BOOL bSuccess
= FALSE
;
637 appinfo_t
*hIC
= NULL
;
639 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
641 /* Clear any error information */
642 INTERNET_SetLastError(0);
644 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_MKD
, lpszDirectory
, 0, 0, 0))
647 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
653 FTP_SetResponseError(nResCode
);
657 hIC
= lpwfs
->lpAppInfo
;
658 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
660 INTERNET_ASYNC_RESULT iar
;
662 iar
.dwResult
= (DWORD
)bSuccess
;
663 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
664 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
665 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
671 /***********************************************************************
672 * FtpFindFirstFileA (WININET.@)
674 * Search the specified directory
677 * HINTERNET on success
681 HINTERNET WINAPI
FtpFindFirstFileA(HINTERNET hConnect
,
682 LPCSTR lpszSearchFile
, LPWIN32_FIND_DATAA lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
684 LPWSTR lpwzSearchFile
;
685 WIN32_FIND_DATAW wfd
;
686 LPWIN32_FIND_DATAW lpFindFileDataW
;
689 lpwzSearchFile
= heap_strdupAtoW(lpszSearchFile
);
690 lpFindFileDataW
= lpFindFileData
?&wfd
:NULL
;
691 ret
= FtpFindFirstFileW(hConnect
, lpwzSearchFile
, lpFindFileDataW
, dwFlags
, dwContext
);
692 heap_free(lpwzSearchFile
);
694 if (ret
&& lpFindFileData
)
695 WININET_find_data_WtoA(lpFindFileDataW
, lpFindFileData
);
703 WIN32_FIND_DATAW
*find_file_data
;
706 } find_first_file_task_t
;
708 static void AsyncFtpFindFirstFileProc(task_header_t
*hdr
)
710 find_first_file_task_t
*task
= (find_first_file_task_t
*)hdr
;
711 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
713 TRACE("%p\n", session
);
715 FTP_FtpFindFirstFileW(session
, task
->search_file
, task
->find_file_data
, task
->flags
, task
->context
);
716 heap_free(task
->search_file
);
719 /***********************************************************************
720 * FtpFindFirstFileW (WININET.@)
722 * Search the specified directory
725 * HINTERNET on success
729 HINTERNET WINAPI
FtpFindFirstFileW(HINTERNET hConnect
,
730 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
732 ftp_session_t
*lpwfs
;
733 appinfo_t
*hIC
= NULL
;
736 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
737 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
739 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
743 if (lpwfs
->download_in_progress
!= NULL
)
745 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
749 hIC
= lpwfs
->lpAppInfo
;
750 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
752 find_first_file_task_t
*task
;
754 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpFindFirstFileProc
, sizeof(*task
));
755 task
->search_file
= heap_strdupW(lpszSearchFile
);
756 task
->find_file_data
= lpFindFileData
;
757 task
->flags
= dwFlags
;
758 task
->context
= dwContext
;
760 INTERNET_AsyncCall(&task
->hdr
);
765 r
= FTP_FtpFindFirstFileW(lpwfs
, lpszSearchFile
, lpFindFileData
,
770 WININET_Release( &lpwfs
->hdr
);
776 /***********************************************************************
777 * FTP_FtpFindFirstFileW (Internal)
779 * Search the specified directory
782 * HINTERNET on success
786 static HINTERNET
FTP_FtpFindFirstFileW(ftp_session_t
*lpwfs
,
787 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
790 appinfo_t
*hIC
= NULL
;
791 HINTERNET hFindNext
= NULL
;
795 /* Clear any error information */
796 INTERNET_SetLastError(0);
798 if (!FTP_InitListenSocket(lpwfs
))
801 if (!FTP_SendType(lpwfs
, INTERNET_FLAG_TRANSFER_ASCII
))
804 if (!FTP_SendPortOrPasv(lpwfs
))
807 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_LIST
, NULL
,
808 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
811 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
814 if (nResCode
== 125 || nResCode
== 150)
818 /* Get data socket to server */
819 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
821 hFindNext
= FTP_ReceiveFileList(lpwfs
, nDataSocket
, lpszSearchFile
, lpFindFileData
, dwContext
);
822 closesocket(nDataSocket
);
823 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
824 if (nResCode
!= 226 && nResCode
!= 250)
825 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
829 FTP_SetResponseError(nResCode
);
833 if (lpwfs
->lstnSocket
!= -1)
835 closesocket(lpwfs
->lstnSocket
);
836 lpwfs
->lstnSocket
= -1;
839 hIC
= lpwfs
->lpAppInfo
;
840 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
842 INTERNET_ASYNC_RESULT iar
;
846 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
847 iar
.dwError
= ERROR_SUCCESS
;
848 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
849 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
852 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
853 iar
.dwError
= hFindNext
? ERROR_SUCCESS
: INTERNET_GetLastError();
854 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
855 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
862 /***********************************************************************
863 * FtpGetCurrentDirectoryA (WININET.@)
865 * Retrieves the current directory
872 BOOL WINAPI
FtpGetCurrentDirectoryA(HINTERNET hFtpSession
, LPSTR lpszCurrentDirectory
,
873 LPDWORD lpdwCurrentDirectory
)
879 if(lpdwCurrentDirectory
) {
880 len
= *lpdwCurrentDirectory
;
881 if(lpszCurrentDirectory
)
883 dir
= heap_alloc(len
* sizeof(WCHAR
));
886 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
891 ret
= FtpGetCurrentDirectoryW(hFtpSession
, lpszCurrentDirectory
?dir
:NULL
, lpdwCurrentDirectory
?&len
:NULL
);
893 if (ret
&& lpszCurrentDirectory
)
894 WideCharToMultiByte(CP_ACP
, 0, dir
, -1, lpszCurrentDirectory
, len
, NULL
, NULL
);
896 if (lpdwCurrentDirectory
) *lpdwCurrentDirectory
= len
;
904 DWORD
*directory_len
;
905 } get_current_dir_task_t
;
907 static void AsyncFtpGetCurrentDirectoryProc(task_header_t
*hdr
)
909 get_current_dir_task_t
*task
= (get_current_dir_task_t
*)hdr
;
910 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
912 TRACE("%p\n", session
);
914 FTP_FtpGetCurrentDirectoryW(session
, task
->directory
, task
->directory_len
);
917 /***********************************************************************
918 * FtpGetCurrentDirectoryW (WININET.@)
920 * Retrieves the current directory
927 BOOL WINAPI
FtpGetCurrentDirectoryW(HINTERNET hFtpSession
, LPWSTR lpszCurrentDirectory
,
928 LPDWORD lpdwCurrentDirectory
)
930 ftp_session_t
*lpwfs
;
931 appinfo_t
*hIC
= NULL
;
934 TRACE("%p %p %p\n", hFtpSession
, lpszCurrentDirectory
, lpdwCurrentDirectory
);
936 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
939 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
943 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
945 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
949 if (!lpdwCurrentDirectory
)
951 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
955 if (lpszCurrentDirectory
== NULL
)
957 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
961 if (lpwfs
->download_in_progress
!= NULL
)
963 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
967 hIC
= lpwfs
->lpAppInfo
;
968 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
970 get_current_dir_task_t
*task
;
972 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpGetCurrentDirectoryProc
, sizeof(*task
));
973 task
->directory
= lpszCurrentDirectory
;
974 task
->directory_len
= lpdwCurrentDirectory
;
976 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
980 r
= FTP_FtpGetCurrentDirectoryW(lpwfs
, lpszCurrentDirectory
,
981 lpdwCurrentDirectory
);
986 WININET_Release( &lpwfs
->hdr
);
992 /***********************************************************************
993 * FTP_FtpGetCurrentDirectoryW (Internal)
995 * Retrieves the current directory
1002 static BOOL
FTP_FtpGetCurrentDirectoryW(ftp_session_t
*lpwfs
, LPWSTR lpszCurrentDirectory
,
1003 LPDWORD lpdwCurrentDirectory
)
1006 appinfo_t
*hIC
= NULL
;
1007 BOOL bSuccess
= FALSE
;
1009 /* Clear any error information */
1010 INTERNET_SetLastError(0);
1012 hIC
= lpwfs
->lpAppInfo
;
1013 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PWD
, NULL
,
1014 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
1017 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1020 if (nResCode
== 257) /* Extract directory name */
1022 DWORD firstpos
, lastpos
, len
;
1023 LPWSTR lpszResponseBuffer
= heap_strdupAtoW(INTERNET_GetResponseBuffer());
1025 for (firstpos
= 0, lastpos
= 0; lpszResponseBuffer
[lastpos
]; lastpos
++)
1027 if ('"' == lpszResponseBuffer
[lastpos
])
1035 len
= lastpos
- firstpos
;
1036 if (*lpdwCurrentDirectory
>= len
)
1038 memcpy(lpszCurrentDirectory
, &lpszResponseBuffer
[firstpos
+ 1], len
* sizeof(WCHAR
));
1039 lpszCurrentDirectory
[len
- 1] = 0;
1040 *lpdwCurrentDirectory
= len
;
1043 else INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1045 heap_free(lpszResponseBuffer
);
1048 FTP_SetResponseError(nResCode
);
1052 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1054 INTERNET_ASYNC_RESULT iar
;
1056 iar
.dwResult
= bSuccess
;
1057 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
1058 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1059 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1066 /***********************************************************************
1067 * FTPFILE_Destroy(internal)
1069 * Closes the file transfer handle. This also 'cleans' the data queue of
1070 * the 'transfer complete' message (this is a bit of a hack though :-/ )
1073 static void FTPFILE_Destroy(object_header_t
*hdr
)
1075 ftp_file_t
*lpwh
= (ftp_file_t
*) hdr
;
1076 ftp_session_t
*lpwfs
= lpwh
->lpFtpSession
;
1081 if (lpwh
->cache_file_handle
!= INVALID_HANDLE_VALUE
)
1082 CloseHandle(lpwh
->cache_file_handle
);
1084 heap_free(lpwh
->cache_file
);
1086 if (!lpwh
->session_deleted
)
1087 lpwfs
->download_in_progress
= NULL
;
1089 if (lpwh
->nDataSocket
!= -1)
1090 closesocket(lpwh
->nDataSocket
);
1092 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1093 if (nResCode
> 0 && nResCode
!= 226) WARN("server reports failed transfer\n");
1095 WININET_Release(&lpwh
->lpFtpSession
->hdr
);
1098 static DWORD
FTPFILE_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
1101 case INTERNET_OPTION_HANDLE_TYPE
:
1102 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1104 if (*size
< sizeof(ULONG
))
1105 return ERROR_INSUFFICIENT_BUFFER
;
1107 *size
= sizeof(DWORD
);
1108 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_FTP_FILE
;
1109 return ERROR_SUCCESS
;
1110 case INTERNET_OPTION_DATAFILE_NAME
:
1113 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1115 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1117 if (!file
->cache_file
)
1120 return ERROR_INTERNET_ITEM_NOT_FOUND
;
1124 required
= (lstrlenW(file
->cache_file
) + 1) * sizeof(WCHAR
);
1125 if (*size
< required
)
1126 return ERROR_INSUFFICIENT_BUFFER
;
1129 memcpy(buffer
, file
->cache_file
, *size
);
1130 return ERROR_SUCCESS
;
1134 required
= WideCharToMultiByte(CP_ACP
, 0, file
->cache_file
, -1, NULL
, 0, NULL
, NULL
);
1135 if (required
> *size
)
1136 return ERROR_INSUFFICIENT_BUFFER
;
1138 *size
= WideCharToMultiByte(CP_ACP
, 0, file
->cache_file
, -1, buffer
, *size
, NULL
, NULL
);
1139 return ERROR_SUCCESS
;
1143 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
1146 static DWORD
FTPFILE_ReadFile(object_header_t
*hdr
, void *buffer
, DWORD size
, DWORD
*read
)
1148 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1152 if (file
->nDataSocket
== -1)
1153 return ERROR_INTERNET_DISCONNECTED
;
1155 /* FIXME: FTP should use NETCON_ stuff */
1156 res
= recv(file
->nDataSocket
, buffer
, size
, MSG_WAITALL
);
1157 *read
= res
>0 ? res
: 0;
1159 error
= res
>= 0 ? ERROR_SUCCESS
: INTERNET_ERROR_BASE
; /* FIXME */
1160 if (error
== ERROR_SUCCESS
&& file
->cache_file
)
1162 DWORD bytes_written
;
1164 if (!WriteFile(file
->cache_file_handle
, buffer
, *read
, &bytes_written
, NULL
))
1165 WARN("WriteFile failed: %u\n", GetLastError());
1170 static DWORD
FTPFILE_ReadFileEx(object_header_t
*hdr
, void *buf
, DWORD size
, DWORD
*ret_size
,
1171 DWORD flags
, DWORD_PTR context
)
1173 return FTPFILE_ReadFile(hdr
, buf
, size
, ret_size
);
1176 static DWORD
FTPFILE_WriteFile(object_header_t
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
1178 ftp_file_t
*lpwh
= (ftp_file_t
*) hdr
;
1181 res
= send(lpwh
->nDataSocket
, buffer
, size
, 0);
1183 *written
= res
>0 ? res
: 0;
1184 return res
>= 0 ? ERROR_SUCCESS
: sock_get_error(errno
);
1187 static void FTP_ReceiveRequestData(ftp_file_t
*file
, BOOL first_notif
)
1189 INTERNET_ASYNC_RESULT iar
;
1193 TRACE("%p\n", file
);
1195 available
= recv(file
->nDataSocket
, buffer
, sizeof(buffer
), MSG_PEEK
);
1197 if(available
!= -1) {
1198 iar
.dwResult
= (DWORD_PTR
)file
->hdr
.hInternet
;
1199 iar
.dwError
= first_notif
? 0 : available
;
1202 iar
.dwError
= INTERNET_GetLastError();
1205 INTERNET_SendCallback(&file
->hdr
, file
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1206 sizeof(INTERNET_ASYNC_RESULT
));
1209 static void FTPFILE_AsyncQueryDataAvailableProc(task_header_t
*task
)
1211 ftp_file_t
*file
= (ftp_file_t
*)task
->hdr
;
1213 FTP_ReceiveRequestData(file
, FALSE
);
1216 static DWORD
FTPFILE_QueryDataAvailable(object_header_t
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
1218 ftp_file_t
*file
= (ftp_file_t
*) hdr
;
1219 int retval
, unread
= 0;
1221 TRACE("(%p %p %x %lx)\n", file
, available
, flags
, ctx
);
1224 retval
= ioctlsocket(file
->nDataSocket
, FIONREAD
, &unread
);
1226 TRACE("%d bytes of queued, but unread data\n", unread
);
1228 FIXME("FIONREAD not available\n");
1231 *available
= unread
;
1238 retval
= recv(file
->nDataSocket
, &byte
, 1, MSG_PEEK
);
1240 task_header_t
*task
;
1242 task
= alloc_async_task(&file
->hdr
, FTPFILE_AsyncQueryDataAvailableProc
, sizeof(*task
));
1243 INTERNET_AsyncCall(task
);
1245 return ERROR_IO_PENDING
;
1249 return ERROR_SUCCESS
;
1252 static DWORD
FTPFILE_LockRequestFile(object_header_t
*hdr
, req_file_t
**ret
)
1254 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1255 FIXME("%p\n", file
);
1256 return ERROR_NOT_SUPPORTED
;
1259 static const object_vtbl_t FTPFILEVtbl
= {
1262 FTPFILE_QueryOption
,
1267 FTPFILE_QueryDataAvailable
,
1269 FTPFILE_LockRequestFile
1272 /***********************************************************************
1273 * FTP_FtpOpenFileW (Internal)
1275 * Open a remote file for writing or reading
1278 * HINTERNET handle on success
1282 static HINTERNET
FTP_FtpOpenFileW(ftp_session_t
*lpwfs
,
1283 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1284 DWORD_PTR dwContext
)
1287 BOOL bSuccess
= FALSE
;
1288 ftp_file_t
*lpwh
= NULL
;
1289 appinfo_t
*hIC
= NULL
;
1293 /* Clear any error information */
1294 INTERNET_SetLastError(0);
1296 if (GENERIC_READ
== fdwAccess
)
1298 /* Set up socket to retrieve data */
1299 bSuccess
= FTP_SendRetrieve(lpwfs
, lpszFileName
, dwFlags
);
1301 else if (GENERIC_WRITE
== fdwAccess
)
1303 /* Set up socket to send data */
1304 bSuccess
= FTP_SendStore(lpwfs
, lpszFileName
, dwFlags
);
1307 /* Get data socket to server */
1308 if (bSuccess
&& FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1310 lpwh
= alloc_object(&lpwfs
->hdr
, &FTPFILEVtbl
, sizeof(ftp_file_t
));
1311 lpwh
->hdr
.htype
= WH_HFILE
;
1312 lpwh
->hdr
.dwFlags
= dwFlags
;
1313 lpwh
->hdr
.dwContext
= dwContext
;
1314 lpwh
->nDataSocket
= nDataSocket
;
1315 lpwh
->cache_file
= NULL
;
1316 lpwh
->cache_file_handle
= INVALID_HANDLE_VALUE
;
1317 lpwh
->session_deleted
= FALSE
;
1319 WININET_AddRef( &lpwfs
->hdr
);
1320 lpwh
->lpFtpSession
= lpwfs
;
1321 list_add_head( &lpwfs
->hdr
.children
, &lpwh
->hdr
.entry
);
1323 /* Indicate that a download is currently in progress */
1324 lpwfs
->download_in_progress
= lpwh
;
1327 if (lpwfs
->lstnSocket
!= -1)
1329 closesocket(lpwfs
->lstnSocket
);
1330 lpwfs
->lstnSocket
= -1;
1333 if (bSuccess
&& fdwAccess
== GENERIC_READ
)
1335 WCHAR filename
[MAX_PATH
+ 1];
1339 memset(&uc
, 0, sizeof(uc
));
1340 uc
.dwStructSize
= sizeof(uc
);
1341 uc
.nScheme
= INTERNET_SCHEME_FTP
;
1342 uc
.lpszHostName
= lpwfs
->servername
;
1343 uc
.nPort
= lpwfs
->serverport
;
1344 uc
.lpszUserName
= lpwfs
->lpszUserName
;
1345 uc
.lpszUrlPath
= heap_strdupW(lpszFileName
);
1347 if (!InternetCreateUrlW(&uc
, 0, NULL
, &len
) && GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1349 WCHAR
*url
= heap_alloc(len
* sizeof(WCHAR
));
1351 if (url
&& InternetCreateUrlW(&uc
, 0, url
, &len
) && CreateUrlCacheEntryW(url
, 0, NULL
, filename
, 0))
1353 lpwh
->cache_file
= heap_strdupW(filename
);
1354 lpwh
->cache_file_handle
= CreateFileW(filename
, GENERIC_WRITE
, FILE_SHARE_READ
,
1355 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1356 if (lpwh
->cache_file_handle
== INVALID_HANDLE_VALUE
)
1358 WARN("Could not create cache file: %u\n", GetLastError());
1359 heap_free(lpwh
->cache_file
);
1360 lpwh
->cache_file
= NULL
;
1365 heap_free(uc
.lpszUrlPath
);
1368 hIC
= lpwfs
->lpAppInfo
;
1369 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1371 INTERNET_ASYNC_RESULT iar
;
1375 iar
.dwResult
= (DWORD_PTR
)lpwh
->hdr
.hInternet
;
1376 iar
.dwError
= ERROR_SUCCESS
;
1377 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
1378 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1382 FTP_ReceiveRequestData(lpwh
, TRUE
);
1385 iar
.dwError
= INTERNET_GetLastError();
1386 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1387 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1394 return lpwh
->hdr
.hInternet
;
1398 /***********************************************************************
1399 * FtpOpenFileA (WININET.@)
1401 * Open a remote file for writing or reading
1404 * HINTERNET handle on success
1408 HINTERNET WINAPI
FtpOpenFileA(HINTERNET hFtpSession
,
1409 LPCSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1410 DWORD_PTR dwContext
)
1412 LPWSTR lpwzFileName
;
1415 lpwzFileName
= heap_strdupAtoW(lpszFileName
);
1416 ret
= FtpOpenFileW(hFtpSession
, lpwzFileName
, fdwAccess
, dwFlags
, dwContext
);
1417 heap_free(lpwzFileName
);
1429 static void AsyncFtpOpenFileProc(task_header_t
*hdr
)
1431 open_file_task_t
*task
= (open_file_task_t
*)hdr
;
1432 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1434 TRACE("%p\n", session
);
1436 FTP_FtpOpenFileW(session
, task
->file_name
, task
->access
, task
->flags
, task
->context
);
1437 heap_free(task
->file_name
);
1440 /***********************************************************************
1441 * FtpOpenFileW (WININET.@)
1443 * Open a remote file for writing or reading
1446 * HINTERNET handle on success
1450 HINTERNET WINAPI
FtpOpenFileW(HINTERNET hFtpSession
,
1451 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1452 DWORD_PTR dwContext
)
1454 ftp_session_t
*lpwfs
;
1455 appinfo_t
*hIC
= NULL
;
1458 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession
,
1459 debugstr_w(lpszFileName
), fdwAccess
, dwFlags
, dwContext
);
1461 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1464 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1468 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1470 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1474 if ((!lpszFileName
) ||
1475 ((fdwAccess
!= GENERIC_READ
) && (fdwAccess
!= GENERIC_WRITE
)) ||
1476 ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
))
1478 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1482 if (lpwfs
->download_in_progress
!= NULL
)
1484 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1488 hIC
= lpwfs
->lpAppInfo
;
1489 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1491 open_file_task_t
*task
;
1493 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpOpenFileProc
, sizeof(*task
));
1494 task
->file_name
= heap_strdupW(lpszFileName
);
1495 task
->access
= fdwAccess
;
1496 task
->flags
= dwFlags
;
1497 task
->context
= dwContext
;
1499 INTERNET_AsyncCall(&task
->hdr
);
1504 r
= FTP_FtpOpenFileW(lpwfs
, lpszFileName
, fdwAccess
, dwFlags
, dwContext
);
1508 WININET_Release( &lpwfs
->hdr
);
1514 /***********************************************************************
1515 * FtpGetFileA (WININET.@)
1517 * Retrieve file from the FTP server
1524 BOOL WINAPI
FtpGetFileA(HINTERNET hInternet
, LPCSTR lpszRemoteFile
, LPCSTR lpszNewFile
,
1525 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1526 DWORD_PTR dwContext
)
1528 LPWSTR lpwzRemoteFile
;
1532 lpwzRemoteFile
= heap_strdupAtoW(lpszRemoteFile
);
1533 lpwzNewFile
= heap_strdupAtoW(lpszNewFile
);
1534 ret
= FtpGetFileW(hInternet
, lpwzRemoteFile
, lpwzNewFile
, fFailIfExists
,
1535 dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1536 heap_free(lpwzRemoteFile
);
1537 heap_free(lpwzNewFile
);
1545 BOOL fail_if_exists
;
1551 static void AsyncFtpGetFileProc(task_header_t
*hdr
)
1553 get_file_task_t
*task
= (get_file_task_t
*)hdr
;
1554 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1556 TRACE("%p\n", session
);
1558 FTP_FtpGetFileW(session
, task
->remote_file
, task
->new_file
, task
->fail_if_exists
,
1559 task
->local_attr
, task
->flags
, task
->context
);
1560 heap_free(task
->remote_file
);
1561 heap_free(task
->new_file
);
1565 /***********************************************************************
1566 * FtpGetFileW (WININET.@)
1568 * Retrieve file from the FTP server
1575 BOOL WINAPI
FtpGetFileW(HINTERNET hInternet
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1576 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1577 DWORD_PTR dwContext
)
1579 ftp_session_t
*lpwfs
;
1580 appinfo_t
*hIC
= NULL
;
1583 if (!lpszRemoteFile
|| !lpszNewFile
)
1585 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1589 lpwfs
= (ftp_session_t
*) get_handle_object( hInternet
);
1592 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1596 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1598 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1602 if ((dwInternetFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
1604 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1608 if (lpwfs
->download_in_progress
!= NULL
)
1610 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1614 hIC
= lpwfs
->lpAppInfo
;
1615 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1617 get_file_task_t
*task
;
1619 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpGetFileProc
, sizeof(*task
));
1620 task
->remote_file
= heap_strdupW(lpszRemoteFile
);
1621 task
->new_file
= heap_strdupW(lpszNewFile
);
1622 task
->local_attr
= dwLocalFlagsAttribute
;
1623 task
->fail_if_exists
= fFailIfExists
;
1624 task
->flags
= dwInternetFlags
;
1625 task
->context
= dwContext
;
1627 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
1631 r
= FTP_FtpGetFileW(lpwfs
, lpszRemoteFile
, lpszNewFile
,
1632 fFailIfExists
, dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1636 WININET_Release( &lpwfs
->hdr
);
1642 /***********************************************************************
1643 * FTP_FtpGetFileW (Internal)
1645 * Retrieve file from the FTP server
1652 static BOOL
FTP_FtpGetFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1653 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1654 DWORD_PTR dwContext
)
1656 BOOL bSuccess
= FALSE
;
1658 appinfo_t
*hIC
= NULL
;
1660 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile
), debugstr_w(lpszNewFile
));
1662 /* Clear any error information */
1663 INTERNET_SetLastError(0);
1665 /* Ensure we can write to lpszNewfile by opening it */
1666 hFile
= CreateFileW(lpszNewFile
, GENERIC_WRITE
, 0, 0, fFailIfExists
?
1667 CREATE_NEW
: CREATE_ALWAYS
, dwLocalFlagsAttribute
, 0);
1668 if (INVALID_HANDLE_VALUE
== hFile
)
1671 /* Set up socket to retrieve data */
1672 if (FTP_SendRetrieve(lpwfs
, lpszRemoteFile
, dwInternetFlags
))
1676 /* Get data socket to server */
1677 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1682 FTP_RetrieveFileData(lpwfs
, nDataSocket
, hFile
);
1683 closesocket(nDataSocket
);
1685 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
1688 if (nResCode
== 226)
1691 FTP_SetResponseError(nResCode
);
1696 if (lpwfs
->lstnSocket
!= -1)
1698 closesocket(lpwfs
->lstnSocket
);
1699 lpwfs
->lstnSocket
= -1;
1704 hIC
= lpwfs
->lpAppInfo
;
1705 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1707 INTERNET_ASYNC_RESULT iar
;
1709 iar
.dwResult
= (DWORD
)bSuccess
;
1710 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1711 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1712 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1715 if (!bSuccess
) DeleteFileW(lpszNewFile
);
1719 /***********************************************************************
1720 * FtpGetFileSize (WININET.@)
1722 DWORD WINAPI
FtpGetFileSize( HINTERNET hFile
, LPDWORD lpdwFileSizeHigh
)
1724 FIXME("(%p, %p)\n", hFile
, lpdwFileSizeHigh
);
1726 if (lpdwFileSizeHigh
)
1727 *lpdwFileSizeHigh
= 0;
1732 /***********************************************************************
1733 * FtpDeleteFileA (WININET.@)
1735 * Delete a file on the ftp server
1742 BOOL WINAPI
FtpDeleteFileA(HINTERNET hFtpSession
, LPCSTR lpszFileName
)
1744 LPWSTR lpwzFileName
;
1747 lpwzFileName
= heap_strdupAtoW(lpszFileName
);
1748 ret
= FtpDeleteFileW(hFtpSession
, lpwzFileName
);
1749 heap_free(lpwzFileName
);
1756 } delete_file_task_t
;
1758 static void AsyncFtpDeleteFileProc(task_header_t
*hdr
)
1760 delete_file_task_t
*task
= (delete_file_task_t
*)hdr
;
1761 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1763 TRACE("%p\n", session
);
1765 FTP_FtpDeleteFileW(session
, task
->file_name
);
1766 heap_free(task
->file_name
);
1769 /***********************************************************************
1770 * FtpDeleteFileW (WININET.@)
1772 * Delete a file on the ftp server
1779 BOOL WINAPI
FtpDeleteFileW(HINTERNET hFtpSession
, LPCWSTR lpszFileName
)
1781 ftp_session_t
*lpwfs
;
1782 appinfo_t
*hIC
= NULL
;
1785 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1788 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1792 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1794 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1798 if (lpwfs
->download_in_progress
!= NULL
)
1800 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1806 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1810 hIC
= lpwfs
->lpAppInfo
;
1811 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1813 delete_file_task_t
*task
;
1815 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpDeleteFileProc
, sizeof(*task
));
1816 task
->file_name
= heap_strdupW(lpszFileName
);
1818 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
1822 r
= FTP_FtpDeleteFileW(lpwfs
, lpszFileName
);
1826 WININET_Release( &lpwfs
->hdr
);
1831 /***********************************************************************
1832 * FTP_FtpDeleteFileW (Internal)
1834 * Delete a file on the ftp server
1841 BOOL
FTP_FtpDeleteFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszFileName
)
1844 BOOL bSuccess
= FALSE
;
1845 appinfo_t
*hIC
= NULL
;
1847 TRACE("%p\n", lpwfs
);
1849 /* Clear any error information */
1850 INTERNET_SetLastError(0);
1852 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_DELE
, lpszFileName
, 0, 0, 0))
1855 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1858 if (nResCode
== 250)
1861 FTP_SetResponseError(nResCode
);
1864 hIC
= lpwfs
->lpAppInfo
;
1865 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1867 INTERNET_ASYNC_RESULT iar
;
1869 iar
.dwResult
= (DWORD
)bSuccess
;
1870 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1871 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1872 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1879 /***********************************************************************
1880 * FtpRemoveDirectoryA (WININET.@)
1882 * Remove a directory on the ftp server
1889 BOOL WINAPI
FtpRemoveDirectoryA(HINTERNET hFtpSession
, LPCSTR lpszDirectory
)
1891 LPWSTR lpwzDirectory
;
1894 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
1895 ret
= FtpRemoveDirectoryW(hFtpSession
, lpwzDirectory
);
1896 heap_free(lpwzDirectory
);
1900 static void AsyncFtpRemoveDirectoryProc(task_header_t
*hdr
)
1902 directory_task_t
*task
= (directory_task_t
*)hdr
;
1903 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1905 TRACE("%p\n", session
);
1907 FTP_FtpRemoveDirectoryW(session
, task
->directory
);
1908 heap_free(task
->directory
);
1911 /***********************************************************************
1912 * FtpRemoveDirectoryW (WININET.@)
1914 * Remove a directory on the ftp server
1921 BOOL WINAPI
FtpRemoveDirectoryW(HINTERNET hFtpSession
, LPCWSTR lpszDirectory
)
1923 ftp_session_t
*lpwfs
;
1924 appinfo_t
*hIC
= NULL
;
1927 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1930 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1934 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1936 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1940 if (lpwfs
->download_in_progress
!= NULL
)
1942 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1948 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1952 hIC
= lpwfs
->lpAppInfo
;
1953 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1955 directory_task_t
*task
;
1957 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpRemoveDirectoryProc
, sizeof(*task
));
1958 task
->directory
= heap_strdupW(lpszDirectory
);
1960 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
1964 r
= FTP_FtpRemoveDirectoryW(lpwfs
, lpszDirectory
);
1968 WININET_Release( &lpwfs
->hdr
);
1973 /***********************************************************************
1974 * FTP_FtpRemoveDirectoryW (Internal)
1976 * Remove a directory on the ftp server
1983 BOOL
FTP_FtpRemoveDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
1986 BOOL bSuccess
= FALSE
;
1987 appinfo_t
*hIC
= NULL
;
1991 /* Clear any error information */
1992 INTERNET_SetLastError(0);
1994 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RMD
, lpszDirectory
, 0, 0, 0))
1997 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2000 if (nResCode
== 250)
2003 FTP_SetResponseError(nResCode
);
2007 hIC
= lpwfs
->lpAppInfo
;
2008 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2010 INTERNET_ASYNC_RESULT iar
;
2012 iar
.dwResult
= (DWORD
)bSuccess
;
2013 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2014 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2015 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2022 /***********************************************************************
2023 * FtpRenameFileA (WININET.@)
2025 * Rename a file on the ftp server
2032 BOOL WINAPI
FtpRenameFileA(HINTERNET hFtpSession
, LPCSTR lpszSrc
, LPCSTR lpszDest
)
2038 lpwzSrc
= heap_strdupAtoW(lpszSrc
);
2039 lpwzDest
= heap_strdupAtoW(lpszDest
);
2040 ret
= FtpRenameFileW(hFtpSession
, lpwzSrc
, lpwzDest
);
2042 heap_free(lpwzDest
);
2050 } rename_file_task_t
;
2052 static void AsyncFtpRenameFileProc(task_header_t
*hdr
)
2054 rename_file_task_t
*task
= (rename_file_task_t
*)hdr
;
2055 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
2057 TRACE("%p\n", session
);
2059 FTP_FtpRenameFileW(session
, task
->src_file
, task
->dst_file
);
2060 heap_free(task
->src_file
);
2061 heap_free(task
->dst_file
);
2064 /***********************************************************************
2065 * FtpRenameFileW (WININET.@)
2067 * Rename a file on the ftp server
2074 BOOL WINAPI
FtpRenameFileW(HINTERNET hFtpSession
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2076 ftp_session_t
*lpwfs
;
2077 appinfo_t
*hIC
= NULL
;
2080 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
2083 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2087 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2089 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2093 if (lpwfs
->download_in_progress
!= NULL
)
2095 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2099 if (!lpszSrc
|| !lpszDest
)
2101 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2105 hIC
= lpwfs
->lpAppInfo
;
2106 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2108 rename_file_task_t
*task
;
2110 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpRenameFileProc
, sizeof(*task
));
2111 task
->src_file
= heap_strdupW(lpszSrc
);
2112 task
->dst_file
= heap_strdupW(lpszDest
);
2114 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
2118 r
= FTP_FtpRenameFileW(lpwfs
, lpszSrc
, lpszDest
);
2122 WININET_Release( &lpwfs
->hdr
);
2127 /***********************************************************************
2128 * FTP_FtpRenameFileW (Internal)
2130 * Rename a file on the ftp server
2137 BOOL
FTP_FtpRenameFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2140 BOOL bSuccess
= FALSE
;
2141 appinfo_t
*hIC
= NULL
;
2145 /* Clear any error information */
2146 INTERNET_SetLastError(0);
2148 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNFR
, lpszSrc
, 0, 0, 0))
2151 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2152 if (nResCode
== 350)
2154 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNTO
, lpszDest
, 0, 0, 0))
2157 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2160 if (nResCode
== 250)
2163 FTP_SetResponseError(nResCode
);
2166 hIC
= lpwfs
->lpAppInfo
;
2167 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2169 INTERNET_ASYNC_RESULT iar
;
2171 iar
.dwResult
= (DWORD
)bSuccess
;
2172 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2173 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2174 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2180 /***********************************************************************
2181 * FtpCommandA (WININET.@)
2183 BOOL WINAPI
FtpCommandA( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2184 LPCSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2189 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2190 debugstr_a(lpszCommand
), dwContext
, phFtpCommand
);
2192 if (fExpectResponse
)
2194 FIXME("data connection not supported\n");
2198 if (!lpszCommand
|| !lpszCommand
[0])
2200 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2204 if (!(cmdW
= heap_strdupAtoW(lpszCommand
)))
2206 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2210 r
= FtpCommandW(hConnect
, fExpectResponse
, dwFlags
, cmdW
, dwContext
, phFtpCommand
);
2216 /***********************************************************************
2217 * FtpCommandW (WININET.@)
2219 BOOL WINAPI
FtpCommandW( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2220 LPCWSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2223 ftp_session_t
*lpwfs
;
2225 DWORD len
, nBytesSent
= 0;
2226 INT nResCode
, nRC
= 0;
2228 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2229 debugstr_w(lpszCommand
), dwContext
, phFtpCommand
);
2231 if (!lpszCommand
|| !lpszCommand
[0])
2233 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2237 if (fExpectResponse
)
2239 FIXME("data connection not supported\n");
2243 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
2246 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2250 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2252 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2256 if (lpwfs
->download_in_progress
!= NULL
)
2258 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2262 len
= WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, NULL
, 0, NULL
, NULL
) + strlen(szCRLF
);
2263 if ((cmd
= heap_alloc(len
)))
2264 WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, cmd
, len
, NULL
, NULL
);
2267 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2271 strcat(cmd
, szCRLF
);
2274 TRACE("Sending (%s) len(%d)\n", cmd
, len
);
2275 while ((nBytesSent
< len
) && (nRC
!= -1))
2277 nRC
= send(lpwfs
->sndSocket
, cmd
+ nBytesSent
, len
- nBytesSent
, 0);
2281 TRACE("Sent %d bytes\n", nRC
);
2287 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2288 if (nResCode
> 0 && nResCode
< 400)
2291 FTP_SetResponseError(nResCode
);
2295 WININET_Release( &lpwfs
->hdr
);
2301 /***********************************************************************
2302 * FTPSESSION_Destroy (internal)
2304 * Deallocate session handle
2306 static void FTPSESSION_Destroy(object_header_t
*hdr
)
2308 ftp_session_t
*lpwfs
= (ftp_session_t
*) hdr
;
2312 WININET_Release(&lpwfs
->lpAppInfo
->hdr
);
2314 heap_free(lpwfs
->lpszPassword
);
2315 heap_free(lpwfs
->lpszUserName
);
2316 heap_free(lpwfs
->servername
);
2319 static void FTPSESSION_CloseConnection(object_header_t
*hdr
)
2321 ftp_session_t
*lpwfs
= (ftp_session_t
*) hdr
;
2325 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2326 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
2328 if (lpwfs
->download_in_progress
!= NULL
)
2329 lpwfs
->download_in_progress
->session_deleted
= TRUE
;
2331 if (lpwfs
->sndSocket
!= -1)
2332 closesocket(lpwfs
->sndSocket
);
2334 if (lpwfs
->lstnSocket
!= -1)
2335 closesocket(lpwfs
->lstnSocket
);
2337 if (lpwfs
->pasvSocket
!= -1)
2338 closesocket(lpwfs
->pasvSocket
);
2340 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2341 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
2344 static DWORD
FTPSESSION_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
2347 case INTERNET_OPTION_HANDLE_TYPE
:
2348 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2350 if (*size
< sizeof(ULONG
))
2351 return ERROR_INSUFFICIENT_BUFFER
;
2353 *size
= sizeof(DWORD
);
2354 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_FTP
;
2355 return ERROR_SUCCESS
;
2358 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
2361 static const object_vtbl_t FTPSESSIONVtbl
= {
2363 FTPSESSION_CloseConnection
,
2364 FTPSESSION_QueryOption
,
2374 /***********************************************************************
2375 * FTP_Connect (internal)
2377 * Connect to a ftp server
2380 * HINTERNET a session handle on success
2385 * Windows uses 'anonymous' as the username, when given a NULL username
2386 * and a NULL password. The password is first looked up in:
2388 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2390 * If this entry is not present it uses the current username as the password.
2394 HINTERNET
FTP_Connect(appinfo_t
*hIC
, LPCWSTR lpszServerName
,
2395 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
2396 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
2397 DWORD dwInternalFlags
)
2399 static const WCHAR szKey
[] = {'S','o','f','t','w','a','r','e','\\',
2400 'M','i','c','r','o','s','o','f','t','\\',
2401 'W','i','n','d','o','w','s','\\',
2402 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2403 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2404 static const WCHAR szValue
[] = {'E','m','a','i','l','N','a','m','e',0};
2405 static const WCHAR szDefaultUsername
[] = {'a','n','o','n','y','m','o','u','s','\0'};
2406 static const WCHAR szEmpty
[] = {'\0'};
2407 struct sockaddr_in socketAddr
;
2409 socklen_t sock_namelen
;
2410 BOOL bSuccess
= FALSE
;
2411 ftp_session_t
*lpwfs
= NULL
;
2412 char szaddr
[INET_ADDRSTRLEN
];
2414 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2415 hIC
, debugstr_w(lpszServerName
),
2416 nServerPort
, debugstr_w(lpszUserName
), debugstr_w(lpszPassword
));
2418 assert( hIC
->hdr
.htype
== WH_HINIT
);
2420 if ((!lpszUserName
|| !*lpszUserName
) && lpszPassword
&& *lpszPassword
)
2422 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2426 lpwfs
= alloc_object(&hIC
->hdr
, &FTPSESSIONVtbl
, sizeof(ftp_session_t
));
2429 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2433 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
2434 lpwfs
->serverport
= INTERNET_DEFAULT_FTP_PORT
;
2436 lpwfs
->serverport
= nServerPort
;
2438 lpwfs
->hdr
.htype
= WH_HFTPSESSION
;
2439 lpwfs
->hdr
.dwFlags
= dwFlags
;
2440 lpwfs
->hdr
.dwContext
= dwContext
;
2441 lpwfs
->hdr
.dwInternalFlags
|= dwInternalFlags
;
2442 lpwfs
->download_in_progress
= NULL
;
2443 lpwfs
->sndSocket
= -1;
2444 lpwfs
->lstnSocket
= -1;
2445 lpwfs
->pasvSocket
= -1;
2447 WININET_AddRef( &hIC
->hdr
);
2448 lpwfs
->lpAppInfo
= hIC
;
2449 list_add_head( &hIC
->hdr
.children
, &lpwfs
->hdr
.entry
);
2451 if(hIC
->proxy
&& hIC
->accessType
== INTERNET_OPEN_TYPE_PROXY
) {
2452 if(strchrW(hIC
->proxy
, ' '))
2453 FIXME("Several proxies not implemented.\n");
2454 if(hIC
->proxyBypass
)
2455 FIXME("Proxy bypass is ignored.\n");
2457 if (!lpszUserName
|| !strlenW(lpszUserName
)) {
2459 WCHAR szPassword
[MAX_PATH
];
2460 DWORD len
= sizeof(szPassword
);
2462 lpwfs
->lpszUserName
= heap_strdupW(szDefaultUsername
);
2464 RegOpenKeyW(HKEY_CURRENT_USER
, szKey
, &key
);
2465 if (RegQueryValueExW(key
, szValue
, NULL
, NULL
, (LPBYTE
)szPassword
, &len
)) {
2466 /* Nothing in the registry, get the username and use that as the password */
2467 if (!GetUserNameW(szPassword
, &len
)) {
2468 /* Should never get here, but use an empty password as failsafe */
2469 strcpyW(szPassword
, szEmpty
);
2474 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword
));
2475 lpwfs
->lpszPassword
= heap_strdupW(szPassword
);
2478 lpwfs
->lpszUserName
= heap_strdupW(lpszUserName
);
2479 lpwfs
->lpszPassword
= heap_strdupW(lpszPassword
? lpszPassword
: szEmpty
);
2481 lpwfs
->servername
= heap_strdupW(lpszServerName
);
2483 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2484 if (!(lpwfs
->hdr
.dwInternalFlags
& INET_OPENURL
))
2486 INTERNET_ASYNC_RESULT iar
;
2488 iar
.dwResult
= (DWORD_PTR
)lpwfs
->hdr
.hInternet
;
2489 iar
.dwError
= ERROR_SUCCESS
;
2491 SendAsyncCallback(&hIC
->hdr
, dwContext
,
2492 INTERNET_STATUS_HANDLE_CREATED
, &iar
,
2493 sizeof(INTERNET_ASYNC_RESULT
));
2496 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
2497 (LPWSTR
) lpszServerName
, (strlenW(lpszServerName
)+1) * sizeof(WCHAR
));
2499 sock_namelen
= sizeof(socketAddr
);
2500 if (!GetAddress(lpszServerName
, lpwfs
->serverport
, (struct sockaddr
*)&socketAddr
, &sock_namelen
))
2502 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
2506 if (socketAddr
.sin_family
!= AF_INET
)
2508 WARN("unsupported address family %d\n", socketAddr
.sin_family
);
2509 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2513 inet_ntop(socketAddr
.sin_family
, &socketAddr
.sin_addr
, szaddr
, sizeof(szaddr
));
2514 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
2515 szaddr
, strlen(szaddr
)+1);
2517 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2520 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2524 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTING_TO_SERVER
,
2525 szaddr
, strlen(szaddr
)+1);
2527 if (connect(nsocket
, (struct sockaddr
*)&socketAddr
, sock_namelen
) < 0)
2529 ERR("Unable to connect (%s)\n", strerror(errno
));
2530 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2531 closesocket(nsocket
);
2535 TRACE("Connected to server\n");
2536 lpwfs
->sndSocket
= nsocket
;
2537 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTED_TO_SERVER
,
2538 szaddr
, strlen(szaddr
)+1);
2540 sock_namelen
= sizeof(lpwfs
->socketAddress
);
2541 getsockname(nsocket
, (struct sockaddr
*) &lpwfs
->socketAddress
, &sock_namelen
);
2543 if (FTP_ConnectToHost(lpwfs
))
2545 TRACE("Successfully logged into server\n");
2554 WININET_Release( &lpwfs
->hdr
);
2558 return lpwfs
->hdr
.hInternet
;
2562 /***********************************************************************
2563 * FTP_ConnectToHost (internal)
2565 * Connect to a ftp server
2572 static BOOL
FTP_ConnectToHost(ftp_session_t
*lpwfs
)
2575 BOOL bSuccess
= FALSE
;
2578 FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2580 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_USER
, lpwfs
->lpszUserName
, 0, 0, 0))
2583 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2586 /* Login successful... */
2587 if (nResCode
== 230)
2589 /* User name okay, need password... */
2590 else if (nResCode
== 331)
2591 bSuccess
= FTP_SendPassword(lpwfs
);
2592 /* Need account for login... */
2593 else if (nResCode
== 332)
2594 bSuccess
= FTP_SendAccount(lpwfs
);
2596 FTP_SetResponseError(nResCode
);
2599 TRACE("Returning %d\n", bSuccess
);
2605 /***********************************************************************
2606 * FTP_SendCommandA (internal)
2608 * Send command to server
2615 static BOOL
FTP_SendCommandA(INT nSocket
, FTP_COMMAND ftpCmd
, LPCSTR lpszParam
,
2616 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2620 DWORD nBytesSent
= 0;
2624 TRACE("%d: (%s) %d\n", ftpCmd
, debugstr_a(lpszParam
), nSocket
);
2628 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2631 dwParamLen
= lpszParam
?strlen(lpszParam
)+1:0;
2632 len
= dwParamLen
+ strlen(szFtpCommands
[ftpCmd
]) + strlen(szCRLF
);
2633 if (NULL
== (buf
= heap_alloc(len
+1)))
2635 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2638 sprintf(buf
, "%s%s%s%s", szFtpCommands
[ftpCmd
], dwParamLen
? " " : "",
2639 dwParamLen
? lpszParam
: "", szCRLF
);
2641 TRACE("Sending (%s) len(%d)\n", buf
, len
);
2642 while((nBytesSent
< len
) && (nRC
!= -1))
2644 nRC
= send(nSocket
, buf
+nBytesSent
, len
- nBytesSent
, 0);
2651 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_REQUEST_SENT
,
2652 &nBytesSent
, sizeof(DWORD
));
2655 TRACE("Sent %d bytes\n", nBytesSent
);
2659 /***********************************************************************
2660 * FTP_SendCommand (internal)
2662 * Send command to server
2669 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
2670 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2673 LPSTR lpszParamA
= heap_strdupWtoA(lpszParam
);
2674 ret
= FTP_SendCommandA(nSocket
, ftpCmd
, lpszParamA
, lpfnStatusCB
, hdr
, dwContext
);
2675 heap_free(lpszParamA
);
2679 /***********************************************************************
2680 * FTP_ReceiveResponse (internal)
2682 * Receive response from server
2685 * Reply code on success
2689 INT
FTP_ReceiveResponse(ftp_session_t
*lpwfs
, DWORD_PTR dwContext
)
2691 LPSTR lpszResponse
= INTERNET_GetResponseBuffer();
2694 char firstprefix
[5];
2695 BOOL multiline
= FALSE
;
2697 TRACE("socket(%d)\n", lpwfs
->sndSocket
);
2699 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2703 if (!INTERNET_GetNextLine(lpwfs
->sndSocket
, &nRecv
))
2710 if(lpszResponse
[3] != '-')
2713 { /* Start of multiline response. Loop until we get "nnn " */
2715 memcpy(firstprefix
, lpszResponse
, 3);
2716 firstprefix
[3] = ' ';
2717 firstprefix
[4] = '\0';
2722 if(!memcmp(firstprefix
, lpszResponse
, 4))
2730 rc
= atoi(lpszResponse
);
2732 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
2733 &nRecv
, sizeof(DWORD
));
2737 TRACE("return %d\n", rc
);
2742 /***********************************************************************
2743 * FTP_SendPassword (internal)
2745 * Send password to ftp server
2752 static BOOL
FTP_SendPassword(ftp_session_t
*lpwfs
)
2755 BOOL bSuccess
= FALSE
;
2758 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASS
, lpwfs
->lpszPassword
, 0, 0, 0))
2761 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2764 TRACE("Received reply code %d\n", nResCode
);
2765 /* Login successful... */
2766 if (nResCode
== 230)
2768 /* Command not implemented, superfluous at the server site... */
2769 /* Need account for login... */
2770 else if (nResCode
== 332)
2771 bSuccess
= FTP_SendAccount(lpwfs
);
2773 FTP_SetResponseError(nResCode
);
2777 TRACE("Returning %d\n", bSuccess
);
2782 /***********************************************************************
2783 * FTP_SendAccount (internal)
2792 static BOOL
FTP_SendAccount(ftp_session_t
*lpwfs
)
2795 BOOL bSuccess
= FALSE
;
2798 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_ACCT
, szNoAccount
, 0, 0, 0))
2801 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2805 FTP_SetResponseError(nResCode
);
2812 /***********************************************************************
2813 * FTP_SendStore (internal)
2815 * Send request to upload file to ftp server
2822 static BOOL
FTP_SendStore(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2825 BOOL bSuccess
= FALSE
;
2828 if (!FTP_InitListenSocket(lpwfs
))
2831 if (!FTP_SendType(lpwfs
, dwType
))
2834 if (!FTP_SendPortOrPasv(lpwfs
))
2837 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_STOR
, lpszRemoteFile
, 0, 0, 0))
2839 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2842 if (nResCode
== 150 || nResCode
== 125)
2845 FTP_SetResponseError(nResCode
);
2849 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2851 closesocket(lpwfs
->lstnSocket
);
2852 lpwfs
->lstnSocket
= -1;
2859 /***********************************************************************
2860 * FTP_InitListenSocket (internal)
2862 * Create a socket to listen for server response
2869 static BOOL
FTP_InitListenSocket(ftp_session_t
*lpwfs
)
2871 BOOL bSuccess
= FALSE
;
2872 socklen_t namelen
= sizeof(lpwfs
->lstnSocketAddress
);
2876 lpwfs
->lstnSocket
= socket(PF_INET
, SOCK_STREAM
, 0);
2877 if (lpwfs
->lstnSocket
== -1)
2879 TRACE("Unable to create listening socket\n");
2883 /* We obtain our ip addr from the name of the command channel socket */
2884 lpwfs
->lstnSocketAddress
= lpwfs
->socketAddress
;
2886 /* and get the system to assign us a port */
2887 lpwfs
->lstnSocketAddress
.sin_port
= htons(0);
2889 if (bind(lpwfs
->lstnSocket
,(struct sockaddr
*) &lpwfs
->lstnSocketAddress
, sizeof(lpwfs
->lstnSocketAddress
)) == -1)
2891 TRACE("Unable to bind socket\n");
2895 if (listen(lpwfs
->lstnSocket
, MAX_BACKLOG
) == -1)
2897 TRACE("listen failed\n");
2901 if (getsockname(lpwfs
->lstnSocket
, (struct sockaddr
*) &lpwfs
->lstnSocketAddress
, &namelen
) != -1)
2905 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2907 closesocket(lpwfs
->lstnSocket
);
2908 lpwfs
->lstnSocket
= -1;
2915 /***********************************************************************
2916 * FTP_SendType (internal)
2918 * Tell server type of data being transferred
2924 * W98SE doesn't cache the type that's currently set
2925 * (i.e. it sends it always),
2926 * so we probably don't want to do that either.
2928 static BOOL
FTP_SendType(ftp_session_t
*lpwfs
, DWORD dwType
)
2931 WCHAR type
[] = { 'I','\0' };
2932 BOOL bSuccess
= FALSE
;
2935 if (dwType
& INTERNET_FLAG_TRANSFER_ASCII
)
2938 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_TYPE
, type
, 0, 0, 0))
2941 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
)/100;
2947 FTP_SetResponseError(nResCode
);
2955 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2956 /***********************************************************************
2957 * FTP_GetFileSize (internal)
2959 * Retrieves from the server the size of the given file
2966 static BOOL
FTP_GetFileSize(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD
*dwSize
)
2969 BOOL bSuccess
= FALSE
;
2973 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_SIZE
, lpszRemoteFile
, 0, 0, 0))
2976 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2979 if (nResCode
== 213) {
2980 /* Now parses the output to get the actual file size */
2982 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
2984 for (i
= 0; (lpszResponseBuffer
[i
] != ' ') && (lpszResponseBuffer
[i
] != '\0'); i
++) ;
2985 if (lpszResponseBuffer
[i
] == '\0') return FALSE
;
2986 *dwSize
= atol(&(lpszResponseBuffer
[i
+ 1]));
2990 FTP_SetResponseError(nResCode
);
3000 /***********************************************************************
3001 * FTP_SendPort (internal)
3003 * Tell server which port to use
3010 static BOOL
FTP_SendPort(ftp_session_t
*lpwfs
)
3012 static const WCHAR szIPFormat
[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
3014 WCHAR szIPAddress
[64];
3015 BOOL bSuccess
= FALSE
;
3018 sprintfW(szIPAddress
, szIPFormat
,
3019 lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x000000FF,
3020 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x0000FF00)>>8,
3021 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x00FF0000)>>16,
3022 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0xFF000000)>>24,
3023 lpwfs
->lstnSocketAddress
.sin_port
& 0xFF,
3024 (lpwfs
->lstnSocketAddress
.sin_port
& 0xFF00)>>8);
3026 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PORT
, szIPAddress
, 0, 0, 0))
3029 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3032 if (nResCode
== 200)
3035 FTP_SetResponseError(nResCode
);
3043 /***********************************************************************
3044 * FTP_DoPassive (internal)
3046 * Tell server that we want to do passive transfers
3047 * and connect data socket
3054 static BOOL
FTP_DoPassive(ftp_session_t
*lpwfs
)
3057 BOOL bSuccess
= FALSE
;
3060 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASV
, NULL
, 0, 0, 0))
3063 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3066 if (nResCode
== 227)
3068 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
3072 char *pAddr
, *pPort
;
3074 struct sockaddr_in dataSocketAddress
;
3076 p
= lpszResponseBuffer
+4; /* skip status code */
3077 while (*p
!= '\0' && (*p
< '0' || *p
> '9')) p
++;
3081 ERR("no address found in response, aborting\n");
3085 if (sscanf(p
, "%d,%d,%d,%d,%d,%d", &f
[0], &f
[1], &f
[2], &f
[3],
3088 ERR("unknown response address format '%s', aborting\n", p
);
3091 for (i
=0; i
< 6; i
++)
3094 dataSocketAddress
= lpwfs
->socketAddress
;
3095 pAddr
= (char *)&(dataSocketAddress
.sin_addr
.s_addr
);
3096 pPort
= (char *)&(dataSocketAddress
.sin_port
);
3104 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
3108 if (connect(nsocket
, (struct sockaddr
*)&dataSocketAddress
, sizeof(dataSocketAddress
)))
3110 ERR("can't connect passive FTP data port.\n");
3111 closesocket(nsocket
);
3114 lpwfs
->pasvSocket
= nsocket
;
3118 FTP_SetResponseError(nResCode
);
3126 static BOOL
FTP_SendPortOrPasv(ftp_session_t
*lpwfs
)
3128 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3130 if (!FTP_DoPassive(lpwfs
))
3135 if (!FTP_SendPort(lpwfs
))
3142 /***********************************************************************
3143 * FTP_GetDataSocket (internal)
3145 * Either accepts an incoming data socket connection from the server
3146 * or just returns the already opened socket after a PASV command
3147 * in case of passive FTP.
3155 static BOOL
FTP_GetDataSocket(ftp_session_t
*lpwfs
, LPINT nDataSocket
)
3157 struct sockaddr_in saddr
;
3158 socklen_t addrlen
= sizeof(struct sockaddr
);
3161 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3163 *nDataSocket
= lpwfs
->pasvSocket
;
3164 lpwfs
->pasvSocket
= -1;
3168 *nDataSocket
= accept(lpwfs
->lstnSocket
, (struct sockaddr
*) &saddr
, &addrlen
);
3169 closesocket(lpwfs
->lstnSocket
);
3170 lpwfs
->lstnSocket
= -1;
3172 return *nDataSocket
!= -1;
3176 /***********************************************************************
3177 * FTP_SendData (internal)
3179 * Send data to the server
3186 static BOOL
FTP_SendData(ftp_session_t
*lpwfs
, INT nDataSocket
, HANDLE hFile
)
3188 BY_HANDLE_FILE_INFORMATION fi
;
3189 DWORD nBytesRead
= 0;
3190 DWORD nBytesSent
= 0;
3191 DWORD nTotalSent
= 0;
3192 DWORD nBytesToSend
, nLen
;
3194 time_t s_long_time
, e_long_time
;
3199 lpszBuffer
= heap_alloc_zero(sizeof(CHAR
)*DATA_PACKET_SIZE
);
3201 /* Get the size of the file. */
3202 GetFileInformationByHandle(hFile
, &fi
);
3207 nBytesToSend
= nBytesRead
- nBytesSent
;
3209 if (nBytesToSend
<= 0)
3211 /* Read data from file. */
3213 if (!ReadFile(hFile
, lpszBuffer
, DATA_PACKET_SIZE
, &nBytesRead
, 0))
3214 ERR("Failed reading from file\n");
3217 nBytesToSend
= nBytesRead
;
3222 nLen
= DATA_PACKET_SIZE
< nBytesToSend
?
3223 DATA_PACKET_SIZE
: nBytesToSend
;
3224 nRC
= send(nDataSocket
, lpszBuffer
, nLen
, 0);
3232 /* Do some computation to display the status. */
3234 nSeconds
= e_long_time
- s_long_time
;
3235 if( nSeconds
/ 60 > 0 )
3237 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
3238 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
/ 60,
3239 nSeconds
% 60, (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3243 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
3244 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
,
3245 (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3247 } while (nRC
!= -1);
3249 TRACE("file transfer complete!\n");
3251 heap_free(lpszBuffer
);
3256 /***********************************************************************
3257 * FTP_SendRetrieve (internal)
3259 * Send request to retrieve a file
3262 * Number of bytes to be received on success
3266 static BOOL
FTP_SendRetrieve(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
3272 if (!(ret
= FTP_InitListenSocket(lpwfs
)))
3275 if (!(ret
= FTP_SendType(lpwfs
, dwType
)))
3278 if (!(ret
= FTP_SendPortOrPasv(lpwfs
)))
3281 if (!(ret
= FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RETR
, lpszRemoteFile
, 0, 0, 0)))
3284 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3285 if ((nResCode
!= 125) && (nResCode
!= 150)) {
3286 /* That means that we got an error getting the file. */
3287 FTP_SetResponseError(nResCode
);
3292 if (!ret
&& lpwfs
->lstnSocket
!= -1)
3294 closesocket(lpwfs
->lstnSocket
);
3295 lpwfs
->lstnSocket
= -1;
3302 /***********************************************************************
3303 * FTP_RetrieveData (internal)
3305 * Retrieve data from server
3312 static BOOL
FTP_RetrieveFileData(ftp_session_t
*lpwfs
, INT nDataSocket
, HANDLE hFile
)
3314 DWORD nBytesWritten
;
3320 lpszBuffer
= heap_alloc_zero(sizeof(CHAR
)*DATA_PACKET_SIZE
);
3321 if (NULL
== lpszBuffer
)
3323 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3329 nRC
= recv(nDataSocket
, lpszBuffer
, DATA_PACKET_SIZE
, 0);
3332 /* other side closed socket. */
3335 WriteFile(hFile
, lpszBuffer
, nRC
, &nBytesWritten
, NULL
);
3339 TRACE("Data transfer complete\n");
3342 heap_free(lpszBuffer
);
3346 /***********************************************************************
3347 * FTPFINDNEXT_Destroy (internal)
3349 * Deallocate session handle
3351 static void FTPFINDNEXT_Destroy(object_header_t
*hdr
)
3353 LPWININETFTPFINDNEXTW lpwfn
= (LPWININETFTPFINDNEXTW
) hdr
;
3358 WININET_Release(&lpwfn
->lpFtpSession
->hdr
);
3360 for (i
= 0; i
< lpwfn
->size
; i
++)
3362 heap_free(lpwfn
->lpafp
[i
].lpszName
);
3364 heap_free(lpwfn
->lpafp
);
3367 static DWORD
FTPFINDNEXT_FindNextFileProc(WININETFTPFINDNEXTW
*find
, LPVOID data
)
3369 WIN32_FIND_DATAW
*find_data
= data
;
3370 DWORD res
= ERROR_SUCCESS
;
3372 TRACE("index(%d) size(%d)\n", find
->index
, find
->size
);
3374 ZeroMemory(find_data
, sizeof(WIN32_FIND_DATAW
));
3376 if (find
->index
< find
->size
) {
3377 FTP_ConvertFileProp(&find
->lpafp
[find
->index
], find_data
);
3380 TRACE("Name: %s\nSize: %d\n", debugstr_w(find_data
->cFileName
), find_data
->nFileSizeLow
);
3382 res
= ERROR_NO_MORE_FILES
;
3385 if (find
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3387 INTERNET_ASYNC_RESULT iar
;
3389 iar
.dwResult
= (res
== ERROR_SUCCESS
);
3392 INTERNET_SendCallback(&find
->hdr
, find
->hdr
.dwContext
,
3393 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3394 sizeof(INTERNET_ASYNC_RESULT
));
3402 WIN32_FIND_DATAW
*find_data
;
3405 static void FTPFINDNEXT_AsyncFindNextFileProc(task_header_t
*hdr
)
3407 find_next_task_t
*task
= (find_next_task_t
*)hdr
;
3409 FTPFINDNEXT_FindNextFileProc((WININETFTPFINDNEXTW
*)task
->hdr
.hdr
, task
->find_data
);
3412 static DWORD
FTPFINDNEXT_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
3415 case INTERNET_OPTION_HANDLE_TYPE
:
3416 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3418 if (*size
< sizeof(ULONG
))
3419 return ERROR_INSUFFICIENT_BUFFER
;
3421 *size
= sizeof(DWORD
);
3422 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_FTP_FIND
;
3423 return ERROR_SUCCESS
;
3426 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
3429 static DWORD
FTPFINDNEXT_FindNextFileW(object_header_t
*hdr
, void *data
)
3431 WININETFTPFINDNEXTW
*find
= (WININETFTPFINDNEXTW
*)hdr
;
3433 if (find
->lpFtpSession
->lpAppInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3435 find_next_task_t
*task
;
3437 task
= alloc_async_task(&find
->hdr
, FTPFINDNEXT_AsyncFindNextFileProc
, sizeof(*task
));
3438 task
->find_data
= data
;
3440 INTERNET_AsyncCall(&task
->hdr
);
3441 return ERROR_SUCCESS
;
3444 return FTPFINDNEXT_FindNextFileProc(find
, data
);
3447 static const object_vtbl_t FTPFINDNEXTVtbl
= {
3448 FTPFINDNEXT_Destroy
,
3450 FTPFINDNEXT_QueryOption
,
3456 FTPFINDNEXT_FindNextFileW
3459 /***********************************************************************
3460 * FTP_ReceiveFileList (internal)
3462 * Read file list from server
3465 * Handle to file list on success
3469 static HINTERNET
FTP_ReceiveFileList(ftp_session_t
*lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3470 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
)
3473 LPFILEPROPERTIESW lpafp
= NULL
;
3474 LPWININETFTPFINDNEXTW lpwfn
= NULL
;
3476 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs
, nSocket
, debugstr_w(lpszSearchFile
), lpFindFileData
, dwContext
);
3478 if (FTP_ParseDirectory(lpwfs
, nSocket
, lpszSearchFile
, &lpafp
, &dwSize
))
3481 FTP_ConvertFileProp(lpafp
, lpFindFileData
);
3483 lpwfn
= alloc_object(&lpwfs
->hdr
, &FTPFINDNEXTVtbl
, sizeof(WININETFTPFINDNEXTW
));
3486 lpwfn
->hdr
.htype
= WH_HFTPFINDNEXT
;
3487 lpwfn
->hdr
.dwContext
= dwContext
;
3488 lpwfn
->index
= 1; /* Next index is 1 since we return index 0 */
3489 lpwfn
->size
= dwSize
;
3490 lpwfn
->lpafp
= lpafp
;
3492 WININET_AddRef( &lpwfs
->hdr
);
3493 lpwfn
->lpFtpSession
= lpwfs
;
3494 list_add_head( &lpwfs
->hdr
.children
, &lpwfn
->hdr
.entry
);
3498 TRACE("Matched %d files\n", dwSize
);
3499 return lpwfn
? lpwfn
->hdr
.hInternet
: NULL
;
3503 /***********************************************************************
3504 * FTP_ConvertFileProp (internal)
3506 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3513 static BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
)
3515 BOOL bSuccess
= FALSE
;
3517 ZeroMemory(lpFindFileData
, sizeof(WIN32_FIND_DATAW
));
3521 SystemTimeToFileTime( &lpafp
->tmLastModified
, &lpFindFileData
->ftLastAccessTime
);
3522 lpFindFileData
->ftLastWriteTime
= lpFindFileData
->ftLastAccessTime
;
3523 lpFindFileData
->ftCreationTime
= lpFindFileData
->ftLastAccessTime
;
3525 /* Not all fields are filled in */
3526 lpFindFileData
->nFileSizeHigh
= 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3527 lpFindFileData
->nFileSizeLow
= lpafp
->nSize
;
3529 if (lpafp
->bIsDirectory
)
3530 lpFindFileData
->dwFileAttributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3532 if (lpafp
->lpszName
)
3533 lstrcpynW(lpFindFileData
->cFileName
, lpafp
->lpszName
, MAX_PATH
);
3541 /***********************************************************************
3542 * FTP_ParseNextFile (internal)
3544 * Parse the next line in file listing
3550 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW lpfp
)
3552 static const char szSpace
[] = " \t";
3560 lpfp
->lpszName
= NULL
;
3562 if(!(pszLine
= INTERNET_GetNextLine(nSocket
, &nBufLen
)))
3565 pszToken
= strtok(pszLine
, szSpace
);
3567 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3570 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3572 if(!isdigit(pszToken
[0]) && 10 == strlen(pszToken
)) {
3573 if(!FTP_ParsePermission(pszToken
, lpfp
))
3574 lpfp
->bIsDirectory
= FALSE
;
3575 for(i
=0; i
<=3; i
++) {
3576 if(!(pszToken
= strtok(NULL
, szSpace
)))
3579 if(!pszToken
) continue;
3580 if(lpfp
->bIsDirectory
) {
3581 TRACE("Is directory\n");
3585 TRACE("Size: %s\n", pszToken
);
3586 lpfp
->nSize
= atol(pszToken
);
3589 lpfp
->tmLastModified
.wSecond
= 0;
3590 lpfp
->tmLastModified
.wMinute
= 0;
3591 lpfp
->tmLastModified
.wHour
= 0;
3592 lpfp
->tmLastModified
.wDay
= 0;
3593 lpfp
->tmLastModified
.wMonth
= 0;
3594 lpfp
->tmLastModified
.wYear
= 0;
3596 /* Determine month */
3597 pszToken
= strtok(NULL
, szSpace
);
3598 if(!pszToken
) continue;
3599 if(strlen(pszToken
) >= 3) {
3601 if((pszTmp
= StrStrIA(szMonths
, pszToken
)))
3602 lpfp
->tmLastModified
.wMonth
= ((pszTmp
- szMonths
) / 3)+1;
3605 pszToken
= strtok(NULL
, szSpace
);
3606 if(!pszToken
) continue;
3607 lpfp
->tmLastModified
.wDay
= atoi(pszToken
);
3608 /* Determine time or year */
3609 pszToken
= strtok(NULL
, szSpace
);
3610 if(!pszToken
) continue;
3611 if((pszTmp
= strchr(pszToken
, ':'))) {
3612 SYSTEMTIME curr_time
;
3615 lpfp
->tmLastModified
.wMinute
= atoi(pszTmp
);
3616 lpfp
->tmLastModified
.wHour
= atoi(pszToken
);
3617 GetLocalTime( &curr_time
);
3618 lpfp
->tmLastModified
.wYear
= curr_time
.wYear
;
3621 lpfp
->tmLastModified
.wYear
= atoi(pszToken
);
3622 lpfp
->tmLastModified
.wHour
= 12;
3624 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3625 lpfp
->tmLastModified
.wHour
, lpfp
->tmLastModified
.wMinute
, lpfp
->tmLastModified
.wSecond
,
3626 lpfp
->tmLastModified
.wYear
, lpfp
->tmLastModified
.wMonth
, lpfp
->tmLastModified
.wDay
);
3628 pszToken
= strtok(NULL
, szSpace
);
3629 if(!pszToken
) continue;
3630 lpfp
->lpszName
= heap_strdupAtoW(pszToken
);
3631 TRACE("File: %s\n", debugstr_w(lpfp
->lpszName
));
3633 /* NT way of parsing ... :
3635 07-13-03 08:55PM <DIR> sakpatch
3636 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3638 else if(isdigit(pszToken
[0]) && 8 == strlen(pszToken
)) {
3639 int mon
, mday
, year
, hour
, min
;
3640 lpfp
->permissions
= 0xFFFF; /* No idea, put full permission :-) */
3642 sscanf(pszToken
, "%d-%d-%d", &mon
, &mday
, &year
);
3643 lpfp
->tmLastModified
.wDay
= mday
;
3644 lpfp
->tmLastModified
.wMonth
= mon
;
3645 lpfp
->tmLastModified
.wYear
= year
;
3647 /* Hacky and bad Y2K protection :-) */
3648 if (lpfp
->tmLastModified
.wYear
< 70) lpfp
->tmLastModified
.wYear
+= 2000;
3650 pszToken
= strtok(NULL
, szSpace
);
3651 if(!pszToken
) continue;
3652 sscanf(pszToken
, "%d:%d", &hour
, &min
);
3653 lpfp
->tmLastModified
.wHour
= hour
;
3654 lpfp
->tmLastModified
.wMinute
= min
;
3655 if((pszToken
[5] == 'P') && (pszToken
[6] == 'M')) {
3656 lpfp
->tmLastModified
.wHour
+= 12;
3658 lpfp
->tmLastModified
.wSecond
= 0;
3660 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3661 lpfp
->tmLastModified
.wHour
, lpfp
->tmLastModified
.wMinute
, lpfp
->tmLastModified
.wSecond
,
3662 lpfp
->tmLastModified
.wYear
, lpfp
->tmLastModified
.wMonth
, lpfp
->tmLastModified
.wDay
);
3664 pszToken
= strtok(NULL
, szSpace
);
3665 if(!pszToken
) continue;
3666 if(!strcasecmp(pszToken
, "<DIR>")) {
3667 lpfp
->bIsDirectory
= TRUE
;
3669 TRACE("Is directory\n");
3672 lpfp
->bIsDirectory
= FALSE
;
3673 lpfp
->nSize
= atol(pszToken
);
3674 TRACE("Size: %d\n", lpfp
->nSize
);
3677 pszToken
= strtok(NULL
, szSpace
);
3678 if(!pszToken
) continue;
3679 lpfp
->lpszName
= heap_strdupAtoW(pszToken
);
3680 TRACE("Name: %s\n", debugstr_w(lpfp
->lpszName
));
3682 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3683 else if(pszToken
[0] == '+') {
3684 FIXME("EPLF Format not implemented\n");
3687 if(lpfp
->lpszName
) {
3688 if((lpszSearchFile
== NULL
) ||
3689 (PathMatchSpecW(lpfp
->lpszName
, lpszSearchFile
))) {
3691 TRACE("Matched: %s\n", debugstr_w(lpfp
->lpszName
));
3694 heap_free(lpfp
->lpszName
);
3695 lpfp
->lpszName
= NULL
;
3702 /***********************************************************************
3703 * FTP_ParseDirectory (internal)
3705 * Parse string of directory information
3711 static BOOL
FTP_ParseDirectory(ftp_session_t
*lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3712 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
)
3714 BOOL bSuccess
= TRUE
;
3715 INT sizeFilePropArray
= 500;/*20; */
3716 INT indexFilePropArray
= -1;
3720 /* Allocate initial file properties array */
3721 *lpafp
= heap_alloc_zero(sizeof(FILEPROPERTIESW
)*(sizeFilePropArray
));
3726 if (indexFilePropArray
+1 >= sizeFilePropArray
)
3728 LPFILEPROPERTIESW tmpafp
;
3730 sizeFilePropArray
*= 2;
3731 tmpafp
= heap_realloc_zero(*lpafp
, sizeof(FILEPROPERTIESW
)*sizeFilePropArray
);
3740 indexFilePropArray
++;
3741 } while (FTP_ParseNextFile(nSocket
, lpszSearchFile
, &(*lpafp
)[indexFilePropArray
]));
3743 if (bSuccess
&& indexFilePropArray
)
3745 if (indexFilePropArray
< sizeFilePropArray
- 1)
3747 LPFILEPROPERTIESW tmpafp
;
3749 tmpafp
= heap_realloc(*lpafp
, sizeof(FILEPROPERTIESW
)*indexFilePropArray
);
3753 *dwfp
= indexFilePropArray
;
3758 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
3766 /***********************************************************************
3767 * FTP_ParsePermission (internal)
3769 * Parse permission string of directory information
3776 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
)
3778 BOOL bSuccess
= TRUE
;
3779 unsigned short nPermission
= 0;
3784 if ((*lpszPermission
!= 'd') && (*lpszPermission
!= '-') && (*lpszPermission
!= 'l'))
3790 lpfp
->bIsDirectory
= (*lpszPermission
== 'd');
3796 nPermission
|= (*(lpszPermission
+1) == 'r' ? 1 : 0) << 8;
3799 nPermission
|= (*(lpszPermission
+2) == 'w' ? 1 : 0) << 7;
3802 nPermission
|= (*(lpszPermission
+3) == 'x' ? 1 : 0) << 6;
3805 nPermission
|= (*(lpszPermission
+4) == 'r' ? 1 : 0) << 5;
3808 nPermission
|= (*(lpszPermission
+5) == 'w' ? 1 : 0) << 4;
3811 nPermission
|= (*(lpszPermission
+6) == 'x' ? 1 : 0) << 3;
3814 nPermission
|= (*(lpszPermission
+7) == 'r' ? 1 : 0) << 2;
3817 nPermission
|= (*(lpszPermission
+8) == 'w' ? 1 : 0) << 1;
3820 nPermission
|= (*(lpszPermission
+9) == 'x' ? 1 : 0);
3824 }while (nPos
<= nLast
);
3826 lpfp
->permissions
= nPermission
;
3831 /***********************************************************************
3832 * FTP_SetResponseError (internal)
3834 * Set the appropriate error code for a given response from the server
3839 static DWORD
FTP_SetResponseError(DWORD dwResponse
)
3845 case 425: /* Cannot open data connection. */
3846 dwCode
= ERROR_INTERNET_CANNOT_CONNECT
;
3849 case 426: /* Connection closed, transer aborted. */
3850 dwCode
= ERROR_INTERNET_CONNECTION_ABORTED
;
3853 case 530: /* Not logged in. Login incorrect. */
3854 dwCode
= ERROR_INTERNET_LOGIN_FAILURE
;
3857 case 421: /* Service not available - Server may be shutting down. */
3858 case 450: /* File action not taken. File may be busy. */
3859 case 451: /* Action aborted. Server error. */
3860 case 452: /* Action not taken. Insufficient storage space on server. */
3861 case 500: /* Syntax error. Command unrecognized. */
3862 case 501: /* Syntax error. Error in parameters or arguments. */
3863 case 502: /* Command not implemented. */
3864 case 503: /* Bad sequence of commands. */
3865 case 504: /* Command not implemented for that parameter. */
3866 case 532: /* Need account for storing files */
3867 case 550: /* File action not taken. File not found or no access. */
3868 case 551: /* Requested action aborted. Page type unknown */
3869 case 552: /* Action aborted. Exceeded storage allocation */
3870 case 553: /* Action not taken. File name not allowed. */
3873 dwCode
= ERROR_INTERNET_EXTENDED_ERROR
;
3877 INTERNET_SetLastError(dwCode
);