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 #define RESPONSE_TIMEOUT 30
34 typedef struct _ftp_session_t ftp_session_t
;
39 ftp_session_t
*lpFtpSession
;
43 HANDLE cache_file_handle
;
52 int pasvSocket
; /* data socket connected by us in case of passive FTP */
53 ftp_file_t
*download_in_progress
;
54 struct sockaddr_in socketAddress
;
55 struct sockaddr_in lstnSocketAddress
;
57 INTERNET_PORT serverport
;
67 SYSTEMTIME tmLastModified
;
68 unsigned short permissions
;
69 } FILEPROPERTIESW
, *LPFILEPROPERTIESW
;
74 ftp_session_t
*lpFtpSession
;
77 LPFILEPROPERTIESW lpafp
;
78 } WININETFTPFINDNEXTW
, *LPWININETFTPFINDNEXTW
;
80 #define DATA_PACKET_SIZE 0x2000
84 /* Testing shows that Windows only accepts dwFlags where the last
85 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
87 #define FTP_CONDITION_MASK 0x0007
90 /* FTP commands with arguments. */
106 /* FTP commands without arguments. */
115 static const CHAR
*const szFtpCommands
[] = {
138 static const CHAR szMonths
[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
139 static const WCHAR szNoAccount
[] = {'n','o','a','c','c','o','u','n','t','\0'};
141 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
142 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
);
143 static BOOL
FTP_SendStore(ftp_session_t
*, LPCWSTR lpszRemoteFile
, DWORD dwType
);
144 static BOOL
FTP_GetDataSocket(ftp_session_t
*, LPINT nDataSocket
);
145 static BOOL
FTP_SendData(ftp_session_t
*, INT nDataSocket
, HANDLE hFile
);
146 static INT
FTP_ReceiveResponse(ftp_session_t
*, DWORD_PTR dwContext
);
147 static BOOL
FTP_SendRetrieve(ftp_session_t
*, LPCWSTR lpszRemoteFile
, DWORD dwType
);
148 static BOOL
FTP_RetrieveFileData(ftp_session_t
*, INT nDataSocket
, HANDLE hFile
);
149 static BOOL
FTP_InitListenSocket(ftp_session_t
*);
150 static BOOL
FTP_ConnectToHost(ftp_session_t
*);
151 static BOOL
FTP_SendPassword(ftp_session_t
*);
152 static BOOL
FTP_SendAccount(ftp_session_t
*);
153 static BOOL
FTP_SendType(ftp_session_t
*, DWORD dwType
);
154 static BOOL
FTP_SendPort(ftp_session_t
*);
155 static BOOL
FTP_DoPassive(ftp_session_t
*);
156 static BOOL
FTP_SendPortOrPasv(ftp_session_t
*);
157 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
);
158 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW fileprop
);
159 static BOOL
FTP_ParseDirectory(ftp_session_t
*, INT nSocket
, LPCWSTR lpszSearchFile
,
160 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
);
161 static HINTERNET
FTP_ReceiveFileList(ftp_session_t
*, INT nSocket
, LPCWSTR lpszSearchFile
,
162 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
);
163 static DWORD
FTP_SetResponseError(DWORD dwResponse
);
164 static BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
);
165 static BOOL
FTP_FtpPutFileW(ftp_session_t
*, LPCWSTR lpszLocalFile
,
166 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
);
167 static BOOL
FTP_FtpSetCurrentDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
168 static BOOL
FTP_FtpCreateDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
169 static HINTERNET
FTP_FtpFindFirstFileW(ftp_session_t
*,
170 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
);
171 static BOOL
FTP_FtpGetCurrentDirectoryW(ftp_session_t
*, LPWSTR lpszCurrentDirectory
,
172 LPDWORD lpdwCurrentDirectory
);
173 static BOOL
FTP_FtpRenameFileW(ftp_session_t
*, LPCWSTR lpszSrc
, LPCWSTR lpszDest
);
174 static BOOL
FTP_FtpRemoveDirectoryW(ftp_session_t
*, LPCWSTR lpszDirectory
);
175 static BOOL
FTP_FtpDeleteFileW(ftp_session_t
*, LPCWSTR lpszFileName
);
176 static BOOL
FTP_FtpGetFileW(ftp_session_t
*, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
177 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
178 DWORD_PTR dwContext
);
180 /* A temporary helper until we get rid of INTERNET_GetLastError calls */
181 static BOOL
res_to_le(DWORD res
)
183 if(res
!= ERROR_SUCCESS
)
184 INTERNET_SetLastError(res
);
185 return res
== ERROR_SUCCESS
;
188 /***********************************************************************
189 * FtpPutFileA (WININET.@)
191 * Uploads a file to the FTP server
198 BOOL WINAPI
FtpPutFileA(HINTERNET hConnect
, LPCSTR lpszLocalFile
,
199 LPCSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
201 LPWSTR lpwzLocalFile
;
202 LPWSTR lpwzNewRemoteFile
;
205 lpwzLocalFile
= heap_strdupAtoW(lpszLocalFile
);
206 lpwzNewRemoteFile
= heap_strdupAtoW(lpszNewRemoteFile
);
207 ret
= FtpPutFileW(hConnect
, lpwzLocalFile
, lpwzNewRemoteFile
,
209 heap_free(lpwzLocalFile
);
210 heap_free(lpwzNewRemoteFile
);
222 static void AsyncFtpPutFileProc(task_header_t
*hdr
)
224 put_file_task_t
*task
= (put_file_task_t
*)hdr
;
225 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
227 TRACE("%p\n", session
);
229 FTP_FtpPutFileW(session
, task
->local_file
, task
->remote_file
,
230 task
->flags
, task
->context
);
232 heap_free(task
->local_file
);
233 heap_free(task
->remote_file
);
236 /***********************************************************************
237 * FtpPutFileW (WININET.@)
239 * Uploads a file to the FTP server
246 BOOL WINAPI
FtpPutFileW(HINTERNET hConnect
, LPCWSTR lpszLocalFile
,
247 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
249 ftp_session_t
*lpwfs
;
250 appinfo_t
*hIC
= NULL
;
253 if (!lpszLocalFile
|| !lpszNewRemoteFile
)
255 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
259 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
262 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
266 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
268 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
272 if (lpwfs
->download_in_progress
!= NULL
)
274 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
278 if ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
280 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
284 hIC
= lpwfs
->lpAppInfo
;
285 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
287 put_file_task_t
*task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpPutFileProc
, sizeof(*task
));
289 task
->local_file
= heap_strdupW(lpszLocalFile
);
290 task
->remote_file
= heap_strdupW(lpszNewRemoteFile
);
291 task
->flags
= dwFlags
;
292 task
->context
= dwContext
;
294 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
298 r
= FTP_FtpPutFileW(lpwfs
, lpszLocalFile
,
299 lpszNewRemoteFile
, dwFlags
, dwContext
);
303 WININET_Release( &lpwfs
->hdr
);
308 /***********************************************************************
309 * FTP_FtpPutFileW (Internal)
311 * Uploads a file to the FTP server
318 static BOOL
FTP_FtpPutFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszLocalFile
,
319 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
322 BOOL bSuccess
= FALSE
;
323 appinfo_t
*hIC
= NULL
;
326 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile
), debugstr_w(lpszNewRemoteFile
));
328 /* Clear any error information */
329 INTERNET_SetLastError(0);
331 /* Open file to be uploaded */
332 if (INVALID_HANDLE_VALUE
==
333 (hFile
= CreateFileW(lpszLocalFile
, GENERIC_READ
, 0, 0, OPEN_EXISTING
, 0, 0)))
334 /* Let CreateFile set the appropriate error */
337 hIC
= lpwfs
->lpAppInfo
;
339 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
341 if (FTP_SendStore(lpwfs
, lpszNewRemoteFile
, dwFlags
))
345 /* Get data socket to server */
346 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
348 FTP_SendData(lpwfs
, nDataSocket
, hFile
);
349 closesocket(nDataSocket
);
350 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
356 FTP_SetResponseError(nResCode
);
361 if (lpwfs
->lstnSocket
!= -1)
363 closesocket(lpwfs
->lstnSocket
);
364 lpwfs
->lstnSocket
= -1;
367 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
369 INTERNET_ASYNC_RESULT iar
;
371 iar
.dwResult
= (DWORD
)bSuccess
;
372 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
373 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
374 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
383 /***********************************************************************
384 * FtpSetCurrentDirectoryA (WININET.@)
386 * Change the working directory on the FTP server
393 BOOL WINAPI
FtpSetCurrentDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
395 LPWSTR lpwzDirectory
;
398 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
399 ret
= FtpSetCurrentDirectoryW(hConnect
, lpwzDirectory
);
400 heap_free(lpwzDirectory
);
409 static void AsyncFtpSetCurrentDirectoryProc(task_header_t
*hdr
)
411 directory_task_t
*task
= (directory_task_t
*)hdr
;
412 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
414 TRACE("%p\n", session
);
416 FTP_FtpSetCurrentDirectoryW(session
, task
->directory
);
417 heap_free(task
->directory
);
420 /***********************************************************************
421 * FtpSetCurrentDirectoryW (WININET.@)
423 * Change the working directory on the FTP server
430 BOOL WINAPI
FtpSetCurrentDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
432 ftp_session_t
*lpwfs
= NULL
;
433 appinfo_t
*hIC
= NULL
;
438 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
442 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
443 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
445 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
449 if (lpwfs
->download_in_progress
!= NULL
)
451 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
455 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
457 hIC
= lpwfs
->lpAppInfo
;
458 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
460 directory_task_t
*task
;
462 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpSetCurrentDirectoryProc
, sizeof(*task
));
463 task
->directory
= heap_strdupW(lpszDirectory
);
465 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
469 r
= FTP_FtpSetCurrentDirectoryW(lpwfs
, lpszDirectory
);
474 WININET_Release( &lpwfs
->hdr
);
480 /***********************************************************************
481 * FTP_FtpSetCurrentDirectoryW (Internal)
483 * Change the working directory on the FTP server
490 static BOOL
FTP_FtpSetCurrentDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
493 appinfo_t
*hIC
= NULL
;
494 BOOL bSuccess
= FALSE
;
496 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
498 /* Clear any error information */
499 INTERNET_SetLastError(0);
501 hIC
= lpwfs
->lpAppInfo
;
502 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_CWD
, lpszDirectory
,
503 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
506 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
513 FTP_SetResponseError(nResCode
);
517 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
519 INTERNET_ASYNC_RESULT iar
;
521 iar
.dwResult
= bSuccess
;
522 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
523 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
524 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
530 /***********************************************************************
531 * FtpCreateDirectoryA (WININET.@)
533 * Create new directory on the FTP server
540 BOOL WINAPI
FtpCreateDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
542 LPWSTR lpwzDirectory
;
545 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
546 ret
= FtpCreateDirectoryW(hConnect
, lpwzDirectory
);
547 heap_free(lpwzDirectory
);
552 static void AsyncFtpCreateDirectoryProc(task_header_t
*hdr
)
554 directory_task_t
*task
= (directory_task_t
*)hdr
;
555 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
557 TRACE(" %p\n", session
);
559 FTP_FtpCreateDirectoryW(session
, task
->directory
);
560 heap_free(task
->directory
);
563 /***********************************************************************
564 * FtpCreateDirectoryW (WININET.@)
566 * Create new directory on the FTP server
573 BOOL WINAPI
FtpCreateDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
575 ftp_session_t
*lpwfs
;
576 appinfo_t
*hIC
= NULL
;
579 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
582 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
586 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
588 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
592 if (lpwfs
->download_in_progress
!= NULL
)
594 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
600 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
604 hIC
= lpwfs
->lpAppInfo
;
605 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
607 directory_task_t
*task
;
609 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpCreateDirectoryProc
, sizeof(*task
));
610 task
->directory
= heap_strdupW(lpszDirectory
);
612 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
616 r
= FTP_FtpCreateDirectoryW(lpwfs
, lpszDirectory
);
619 WININET_Release( &lpwfs
->hdr
);
625 /***********************************************************************
626 * FTP_FtpCreateDirectoryW (Internal)
628 * Create new directory on the FTP server
635 static BOOL
FTP_FtpCreateDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
638 BOOL bSuccess
= FALSE
;
639 appinfo_t
*hIC
= NULL
;
641 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
643 /* Clear any error information */
644 INTERNET_SetLastError(0);
646 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_MKD
, lpszDirectory
, 0, 0, 0))
649 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
655 FTP_SetResponseError(nResCode
);
659 hIC
= lpwfs
->lpAppInfo
;
660 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
662 INTERNET_ASYNC_RESULT iar
;
664 iar
.dwResult
= (DWORD
)bSuccess
;
665 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
666 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
667 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
673 /***********************************************************************
674 * FtpFindFirstFileA (WININET.@)
676 * Search the specified directory
679 * HINTERNET on success
683 HINTERNET WINAPI
FtpFindFirstFileA(HINTERNET hConnect
,
684 LPCSTR lpszSearchFile
, LPWIN32_FIND_DATAA lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
686 LPWSTR lpwzSearchFile
;
687 WIN32_FIND_DATAW wfd
;
688 LPWIN32_FIND_DATAW lpFindFileDataW
;
691 lpwzSearchFile
= heap_strdupAtoW(lpszSearchFile
);
692 lpFindFileDataW
= lpFindFileData
?&wfd
:NULL
;
693 ret
= FtpFindFirstFileW(hConnect
, lpwzSearchFile
, lpFindFileDataW
, dwFlags
, dwContext
);
694 heap_free(lpwzSearchFile
);
696 if (ret
&& lpFindFileData
)
697 WININET_find_data_WtoA(lpFindFileDataW
, lpFindFileData
);
705 WIN32_FIND_DATAW
*find_file_data
;
708 } find_first_file_task_t
;
710 static void AsyncFtpFindFirstFileProc(task_header_t
*hdr
)
712 find_first_file_task_t
*task
= (find_first_file_task_t
*)hdr
;
713 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
715 TRACE("%p\n", session
);
717 FTP_FtpFindFirstFileW(session
, task
->search_file
, task
->find_file_data
, task
->flags
, task
->context
);
718 heap_free(task
->search_file
);
721 /***********************************************************************
722 * FtpFindFirstFileW (WININET.@)
724 * Search the specified directory
727 * HINTERNET on success
731 HINTERNET WINAPI
FtpFindFirstFileW(HINTERNET hConnect
,
732 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
734 ftp_session_t
*lpwfs
;
735 appinfo_t
*hIC
= NULL
;
738 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
739 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
741 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
745 if (lpwfs
->download_in_progress
!= NULL
)
747 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
751 hIC
= lpwfs
->lpAppInfo
;
752 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
754 find_first_file_task_t
*task
;
756 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpFindFirstFileProc
, sizeof(*task
));
757 task
->search_file
= heap_strdupW(lpszSearchFile
);
758 task
->find_file_data
= lpFindFileData
;
759 task
->flags
= dwFlags
;
760 task
->context
= dwContext
;
762 INTERNET_AsyncCall(&task
->hdr
);
767 r
= FTP_FtpFindFirstFileW(lpwfs
, lpszSearchFile
, lpFindFileData
,
772 WININET_Release( &lpwfs
->hdr
);
778 /***********************************************************************
779 * FTP_FtpFindFirstFileW (Internal)
781 * Search the specified directory
784 * HINTERNET on success
788 static HINTERNET
FTP_FtpFindFirstFileW(ftp_session_t
*lpwfs
,
789 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
792 appinfo_t
*hIC
= NULL
;
793 HINTERNET hFindNext
= NULL
;
794 LPWSTR lpszSearchPath
= NULL
;
798 /* Clear any error information */
799 INTERNET_SetLastError(0);
801 if (!FTP_InitListenSocket(lpwfs
))
804 if (!FTP_SendType(lpwfs
, INTERNET_FLAG_TRANSFER_ASCII
))
807 if (!FTP_SendPortOrPasv(lpwfs
))
810 /* split search path into file and path */
813 LPCWSTR name
= lpszSearchFile
, p
;
814 if ((p
= strrchrW( name
, '\\' ))) name
= p
+ 1;
815 if ((p
= strrchrW( name
, '/' ))) name
= p
+ 1;
816 if (name
!= lpszSearchFile
)
818 lpszSearchPath
= heap_strndupW(lpszSearchFile
, name
- lpszSearchFile
);
819 lpszSearchFile
= name
;
823 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_LIST
, lpszSearchPath
,
824 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
827 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
830 if (nResCode
== 125 || nResCode
== 150)
834 /* Get data socket to server */
835 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
837 hFindNext
= FTP_ReceiveFileList(lpwfs
, nDataSocket
, lpszSearchFile
, lpFindFileData
, dwContext
);
838 closesocket(nDataSocket
);
839 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
840 if (nResCode
!= 226 && nResCode
!= 250)
841 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
845 FTP_SetResponseError(nResCode
);
849 heap_free(lpszSearchPath
);
851 if (lpwfs
->lstnSocket
!= -1)
853 closesocket(lpwfs
->lstnSocket
);
854 lpwfs
->lstnSocket
= -1;
857 hIC
= lpwfs
->lpAppInfo
;
858 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
860 INTERNET_ASYNC_RESULT iar
;
864 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
865 iar
.dwError
= ERROR_SUCCESS
;
866 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
867 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
870 iar
.dwResult
= (DWORD_PTR
)hFindNext
;
871 iar
.dwError
= hFindNext
? ERROR_SUCCESS
: INTERNET_GetLastError();
872 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
873 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
880 /***********************************************************************
881 * FtpGetCurrentDirectoryA (WININET.@)
883 * Retrieves the current directory
890 BOOL WINAPI
FtpGetCurrentDirectoryA(HINTERNET hFtpSession
, LPSTR lpszCurrentDirectory
,
891 LPDWORD lpdwCurrentDirectory
)
897 if(lpdwCurrentDirectory
) {
898 len
= *lpdwCurrentDirectory
;
899 if(lpszCurrentDirectory
)
901 dir
= heap_alloc(len
* sizeof(WCHAR
));
904 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
909 ret
= FtpGetCurrentDirectoryW(hFtpSession
, lpszCurrentDirectory
?dir
:NULL
, lpdwCurrentDirectory
?&len
:NULL
);
911 if (ret
&& lpszCurrentDirectory
)
912 WideCharToMultiByte(CP_ACP
, 0, dir
, -1, lpszCurrentDirectory
, len
, NULL
, NULL
);
914 if (lpdwCurrentDirectory
) *lpdwCurrentDirectory
= len
;
922 DWORD
*directory_len
;
923 } get_current_dir_task_t
;
925 static void AsyncFtpGetCurrentDirectoryProc(task_header_t
*hdr
)
927 get_current_dir_task_t
*task
= (get_current_dir_task_t
*)hdr
;
928 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
930 TRACE("%p\n", session
);
932 FTP_FtpGetCurrentDirectoryW(session
, task
->directory
, task
->directory_len
);
935 /***********************************************************************
936 * FtpGetCurrentDirectoryW (WININET.@)
938 * Retrieves the current directory
945 BOOL WINAPI
FtpGetCurrentDirectoryW(HINTERNET hFtpSession
, LPWSTR lpszCurrentDirectory
,
946 LPDWORD lpdwCurrentDirectory
)
948 ftp_session_t
*lpwfs
;
949 appinfo_t
*hIC
= NULL
;
952 TRACE("%p %p %p\n", hFtpSession
, lpszCurrentDirectory
, lpdwCurrentDirectory
);
954 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
957 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
961 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
963 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
967 if (!lpdwCurrentDirectory
)
969 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
973 if (lpszCurrentDirectory
== NULL
)
975 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
979 if (lpwfs
->download_in_progress
!= NULL
)
981 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
985 hIC
= lpwfs
->lpAppInfo
;
986 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
988 get_current_dir_task_t
*task
;
990 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpGetCurrentDirectoryProc
, sizeof(*task
));
991 task
->directory
= lpszCurrentDirectory
;
992 task
->directory_len
= lpdwCurrentDirectory
;
994 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
998 r
= FTP_FtpGetCurrentDirectoryW(lpwfs
, lpszCurrentDirectory
,
999 lpdwCurrentDirectory
);
1004 WININET_Release( &lpwfs
->hdr
);
1010 /***********************************************************************
1011 * FTP_FtpGetCurrentDirectoryW (Internal)
1013 * Retrieves the current directory
1020 static BOOL
FTP_FtpGetCurrentDirectoryW(ftp_session_t
*lpwfs
, LPWSTR lpszCurrentDirectory
,
1021 LPDWORD lpdwCurrentDirectory
)
1024 appinfo_t
*hIC
= NULL
;
1025 BOOL bSuccess
= FALSE
;
1027 /* Clear any error information */
1028 INTERNET_SetLastError(0);
1030 hIC
= lpwfs
->lpAppInfo
;
1031 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PWD
, NULL
,
1032 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
1035 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1038 if (nResCode
== 257) /* Extract directory name */
1040 DWORD firstpos
, lastpos
, len
;
1041 LPWSTR lpszResponseBuffer
= heap_strdupAtoW(INTERNET_GetResponseBuffer());
1043 for (firstpos
= 0, lastpos
= 0; lpszResponseBuffer
[lastpos
]; lastpos
++)
1045 if ('"' == lpszResponseBuffer
[lastpos
])
1053 len
= lastpos
- firstpos
;
1054 if (*lpdwCurrentDirectory
>= len
)
1056 memcpy(lpszCurrentDirectory
, &lpszResponseBuffer
[firstpos
+ 1], len
* sizeof(WCHAR
));
1057 lpszCurrentDirectory
[len
- 1] = 0;
1058 *lpdwCurrentDirectory
= len
;
1061 else INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1063 heap_free(lpszResponseBuffer
);
1066 FTP_SetResponseError(nResCode
);
1070 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1072 INTERNET_ASYNC_RESULT iar
;
1074 iar
.dwResult
= bSuccess
;
1075 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
1076 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1077 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1084 /***********************************************************************
1085 * FTPFILE_Destroy(internal)
1087 * Closes the file transfer handle. This also 'cleans' the data queue of
1088 * the 'transfer complete' message (this is a bit of a hack though :-/ )
1091 static void FTPFILE_Destroy(object_header_t
*hdr
)
1093 ftp_file_t
*lpwh
= (ftp_file_t
*) hdr
;
1094 ftp_session_t
*lpwfs
= lpwh
->lpFtpSession
;
1099 if (lpwh
->cache_file_handle
!= INVALID_HANDLE_VALUE
)
1100 CloseHandle(lpwh
->cache_file_handle
);
1102 heap_free(lpwh
->cache_file
);
1104 if (!lpwh
->session_deleted
)
1105 lpwfs
->download_in_progress
= NULL
;
1107 if (lpwh
->nDataSocket
!= -1)
1108 closesocket(lpwh
->nDataSocket
);
1110 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1111 if (nResCode
> 0 && nResCode
!= 226) WARN("server reports failed transfer\n");
1113 WININET_Release(&lpwh
->lpFtpSession
->hdr
);
1116 static DWORD
FTPFILE_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
1119 case INTERNET_OPTION_HANDLE_TYPE
:
1120 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1122 if (*size
< sizeof(ULONG
))
1123 return ERROR_INSUFFICIENT_BUFFER
;
1125 *size
= sizeof(DWORD
);
1126 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_FTP_FILE
;
1127 return ERROR_SUCCESS
;
1128 case INTERNET_OPTION_DATAFILE_NAME
:
1131 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1133 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1135 if (!file
->cache_file
)
1138 return ERROR_INTERNET_ITEM_NOT_FOUND
;
1142 required
= (lstrlenW(file
->cache_file
) + 1) * sizeof(WCHAR
);
1143 if (*size
< required
)
1144 return ERROR_INSUFFICIENT_BUFFER
;
1147 memcpy(buffer
, file
->cache_file
, *size
);
1148 return ERROR_SUCCESS
;
1152 required
= WideCharToMultiByte(CP_ACP
, 0, file
->cache_file
, -1, NULL
, 0, NULL
, NULL
);
1153 if (required
> *size
)
1154 return ERROR_INSUFFICIENT_BUFFER
;
1156 *size
= WideCharToMultiByte(CP_ACP
, 0, file
->cache_file
, -1, buffer
, *size
, NULL
, NULL
);
1157 return ERROR_SUCCESS
;
1161 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
1164 static DWORD
FTPFILE_ReadFile(object_header_t
*hdr
, void *buffer
, DWORD size
, DWORD
*read
)
1166 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1170 if (file
->nDataSocket
== -1)
1171 return ERROR_INTERNET_DISCONNECTED
;
1173 /* FIXME: FTP should use NETCON_ stuff */
1174 res
= sock_recv(file
->nDataSocket
, buffer
, size
, MSG_WAITALL
);
1175 *read
= res
>0 ? res
: 0;
1177 error
= res
>= 0 ? ERROR_SUCCESS
: INTERNET_ERROR_BASE
; /* FIXME */
1178 if (error
== ERROR_SUCCESS
&& file
->cache_file
)
1180 DWORD bytes_written
;
1182 if (!WriteFile(file
->cache_file_handle
, buffer
, *read
, &bytes_written
, NULL
))
1183 WARN("WriteFile failed: %u\n", GetLastError());
1188 static DWORD
FTPFILE_ReadFileEx(object_header_t
*hdr
, void *buf
, DWORD size
, DWORD
*ret_size
,
1189 DWORD flags
, DWORD_PTR context
)
1191 return FTPFILE_ReadFile(hdr
, buf
, size
, ret_size
);
1194 static DWORD
FTPFILE_WriteFile(object_header_t
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
1196 ftp_file_t
*lpwh
= (ftp_file_t
*) hdr
;
1199 res
= sock_send(lpwh
->nDataSocket
, buffer
, size
, 0);
1201 *written
= res
>0 ? res
: 0;
1202 return res
>= 0 ? ERROR_SUCCESS
: WSAGetLastError();
1205 static void FTP_ReceiveRequestData(ftp_file_t
*file
, BOOL first_notif
)
1207 INTERNET_ASYNC_RESULT iar
;
1211 TRACE("%p\n", file
);
1213 available
= sock_recv(file
->nDataSocket
, buffer
, sizeof(buffer
), MSG_PEEK
);
1215 if(available
!= -1) {
1216 iar
.dwResult
= (DWORD_PTR
)file
->hdr
.hInternet
;
1217 iar
.dwError
= first_notif
? 0 : available
;
1220 iar
.dwError
= INTERNET_GetLastError();
1223 INTERNET_SendCallback(&file
->hdr
, file
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
1224 sizeof(INTERNET_ASYNC_RESULT
));
1227 static void FTPFILE_AsyncQueryDataAvailableProc(task_header_t
*task
)
1229 ftp_file_t
*file
= (ftp_file_t
*)task
->hdr
;
1231 FTP_ReceiveRequestData(file
, FALSE
);
1234 static DWORD
FTPFILE_QueryDataAvailable(object_header_t
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
1236 ftp_file_t
*file
= (ftp_file_t
*) hdr
;
1240 TRACE("(%p %p %x %lx)\n", file
, available
, flags
, ctx
);
1242 retval
= ioctlsocket(file
->nDataSocket
, FIONREAD
, &unread
);
1244 TRACE("%d bytes of queued, but unread data\n", unread
);
1246 *available
= unread
;
1253 retval
= sock_recv(file
->nDataSocket
, &byte
, 1, MSG_PEEK
);
1255 task_header_t
*task
;
1257 task
= alloc_async_task(&file
->hdr
, FTPFILE_AsyncQueryDataAvailableProc
, sizeof(*task
));
1258 INTERNET_AsyncCall(task
);
1260 return ERROR_IO_PENDING
;
1264 return ERROR_SUCCESS
;
1267 static DWORD
FTPFILE_LockRequestFile(object_header_t
*hdr
, req_file_t
**ret
)
1269 ftp_file_t
*file
= (ftp_file_t
*)hdr
;
1270 FIXME("%p\n", file
);
1271 return ERROR_NOT_SUPPORTED
;
1274 static const object_vtbl_t FTPFILEVtbl
= {
1277 FTPFILE_QueryOption
,
1282 FTPFILE_QueryDataAvailable
,
1284 FTPFILE_LockRequestFile
1287 /***********************************************************************
1288 * FTP_FtpOpenFileW (Internal)
1290 * Open a remote file for writing or reading
1293 * HINTERNET handle on success
1297 static HINTERNET
FTP_FtpOpenFileW(ftp_session_t
*lpwfs
,
1298 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1299 DWORD_PTR dwContext
)
1302 BOOL bSuccess
= FALSE
;
1303 ftp_file_t
*lpwh
= NULL
;
1304 appinfo_t
*hIC
= NULL
;
1308 /* Clear any error information */
1309 INTERNET_SetLastError(0);
1311 if (GENERIC_READ
== fdwAccess
)
1313 /* Set up socket to retrieve data */
1314 bSuccess
= FTP_SendRetrieve(lpwfs
, lpszFileName
, dwFlags
);
1316 else if (GENERIC_WRITE
== fdwAccess
)
1318 /* Set up socket to send data */
1319 bSuccess
= FTP_SendStore(lpwfs
, lpszFileName
, dwFlags
);
1322 /* Get data socket to server */
1323 if (bSuccess
&& FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1325 lpwh
= alloc_object(&lpwfs
->hdr
, &FTPFILEVtbl
, sizeof(ftp_file_t
));
1326 lpwh
->hdr
.htype
= WH_HFILE
;
1327 lpwh
->hdr
.dwFlags
= dwFlags
;
1328 lpwh
->hdr
.dwContext
= dwContext
;
1329 lpwh
->nDataSocket
= nDataSocket
;
1330 lpwh
->cache_file
= NULL
;
1331 lpwh
->cache_file_handle
= INVALID_HANDLE_VALUE
;
1332 lpwh
->session_deleted
= FALSE
;
1334 WININET_AddRef( &lpwfs
->hdr
);
1335 lpwh
->lpFtpSession
= lpwfs
;
1336 list_add_head( &lpwfs
->hdr
.children
, &lpwh
->hdr
.entry
);
1338 /* Indicate that a download is currently in progress */
1339 lpwfs
->download_in_progress
= lpwh
;
1342 if (lpwfs
->lstnSocket
!= -1)
1344 closesocket(lpwfs
->lstnSocket
);
1345 lpwfs
->lstnSocket
= -1;
1348 if (bSuccess
&& fdwAccess
== GENERIC_READ
)
1350 WCHAR filename
[MAX_PATH
+ 1];
1354 memset(&uc
, 0, sizeof(uc
));
1355 uc
.dwStructSize
= sizeof(uc
);
1356 uc
.nScheme
= INTERNET_SCHEME_FTP
;
1357 uc
.lpszHostName
= lpwfs
->servername
;
1358 uc
.nPort
= lpwfs
->serverport
;
1359 uc
.lpszUserName
= lpwfs
->lpszUserName
;
1360 uc
.lpszUrlPath
= heap_strdupW(lpszFileName
);
1362 if (!InternetCreateUrlW(&uc
, 0, NULL
, &len
) && GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1364 WCHAR
*url
= heap_alloc(len
* sizeof(WCHAR
));
1366 if (url
&& InternetCreateUrlW(&uc
, 0, url
, &len
) && CreateUrlCacheEntryW(url
, 0, NULL
, filename
, 0))
1368 lpwh
->cache_file
= heap_strdupW(filename
);
1369 lpwh
->cache_file_handle
= CreateFileW(filename
, GENERIC_WRITE
, FILE_SHARE_READ
,
1370 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1371 if (lpwh
->cache_file_handle
== INVALID_HANDLE_VALUE
)
1373 WARN("Could not create cache file: %u\n", GetLastError());
1374 heap_free(lpwh
->cache_file
);
1375 lpwh
->cache_file
= NULL
;
1380 heap_free(uc
.lpszUrlPath
);
1383 hIC
= lpwfs
->lpAppInfo
;
1384 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1386 INTERNET_ASYNC_RESULT iar
;
1390 iar
.dwResult
= (DWORD_PTR
)lpwh
->hdr
.hInternet
;
1391 iar
.dwError
= ERROR_SUCCESS
;
1392 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
1393 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1397 FTP_ReceiveRequestData(lpwh
, TRUE
);
1400 iar
.dwError
= INTERNET_GetLastError();
1401 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1402 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1409 return lpwh
->hdr
.hInternet
;
1413 /***********************************************************************
1414 * FtpOpenFileA (WININET.@)
1416 * Open a remote file for writing or reading
1419 * HINTERNET handle on success
1423 HINTERNET WINAPI
FtpOpenFileA(HINTERNET hFtpSession
,
1424 LPCSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1425 DWORD_PTR dwContext
)
1427 LPWSTR lpwzFileName
;
1430 lpwzFileName
= heap_strdupAtoW(lpszFileName
);
1431 ret
= FtpOpenFileW(hFtpSession
, lpwzFileName
, fdwAccess
, dwFlags
, dwContext
);
1432 heap_free(lpwzFileName
);
1444 static void AsyncFtpOpenFileProc(task_header_t
*hdr
)
1446 open_file_task_t
*task
= (open_file_task_t
*)hdr
;
1447 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1449 TRACE("%p\n", session
);
1451 FTP_FtpOpenFileW(session
, task
->file_name
, task
->access
, task
->flags
, task
->context
);
1452 heap_free(task
->file_name
);
1455 /***********************************************************************
1456 * FtpOpenFileW (WININET.@)
1458 * Open a remote file for writing or reading
1461 * HINTERNET handle on success
1465 HINTERNET WINAPI
FtpOpenFileW(HINTERNET hFtpSession
,
1466 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1467 DWORD_PTR dwContext
)
1469 ftp_session_t
*lpwfs
;
1470 appinfo_t
*hIC
= NULL
;
1473 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession
,
1474 debugstr_w(lpszFileName
), fdwAccess
, dwFlags
, dwContext
);
1476 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1479 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1483 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1485 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1489 if ((!lpszFileName
) ||
1490 ((fdwAccess
!= GENERIC_READ
) && (fdwAccess
!= GENERIC_WRITE
)) ||
1491 ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
))
1493 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1497 if (lpwfs
->download_in_progress
!= NULL
)
1499 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1503 hIC
= lpwfs
->lpAppInfo
;
1504 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1506 open_file_task_t
*task
;
1508 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpOpenFileProc
, sizeof(*task
));
1509 task
->file_name
= heap_strdupW(lpszFileName
);
1510 task
->access
= fdwAccess
;
1511 task
->flags
= dwFlags
;
1512 task
->context
= dwContext
;
1514 INTERNET_AsyncCall(&task
->hdr
);
1519 r
= FTP_FtpOpenFileW(lpwfs
, lpszFileName
, fdwAccess
, dwFlags
, dwContext
);
1523 WININET_Release( &lpwfs
->hdr
);
1529 /***********************************************************************
1530 * FtpGetFileA (WININET.@)
1532 * Retrieve file from the FTP server
1539 BOOL WINAPI
FtpGetFileA(HINTERNET hInternet
, LPCSTR lpszRemoteFile
, LPCSTR lpszNewFile
,
1540 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1541 DWORD_PTR dwContext
)
1543 LPWSTR lpwzRemoteFile
;
1547 lpwzRemoteFile
= heap_strdupAtoW(lpszRemoteFile
);
1548 lpwzNewFile
= heap_strdupAtoW(lpszNewFile
);
1549 ret
= FtpGetFileW(hInternet
, lpwzRemoteFile
, lpwzNewFile
, fFailIfExists
,
1550 dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1551 heap_free(lpwzRemoteFile
);
1552 heap_free(lpwzNewFile
);
1560 BOOL fail_if_exists
;
1566 static void AsyncFtpGetFileProc(task_header_t
*hdr
)
1568 get_file_task_t
*task
= (get_file_task_t
*)hdr
;
1569 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1571 TRACE("%p\n", session
);
1573 FTP_FtpGetFileW(session
, task
->remote_file
, task
->new_file
, task
->fail_if_exists
,
1574 task
->local_attr
, task
->flags
, task
->context
);
1575 heap_free(task
->remote_file
);
1576 heap_free(task
->new_file
);
1580 /***********************************************************************
1581 * FtpGetFileW (WININET.@)
1583 * Retrieve file from the FTP server
1590 BOOL WINAPI
FtpGetFileW(HINTERNET hInternet
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1591 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1592 DWORD_PTR dwContext
)
1594 ftp_session_t
*lpwfs
;
1595 appinfo_t
*hIC
= NULL
;
1598 if (!lpszRemoteFile
|| !lpszNewFile
)
1600 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1604 lpwfs
= (ftp_session_t
*) get_handle_object( hInternet
);
1607 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1611 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1613 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1617 if ((dwInternetFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
1619 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1623 if (lpwfs
->download_in_progress
!= NULL
)
1625 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1629 hIC
= lpwfs
->lpAppInfo
;
1630 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1632 get_file_task_t
*task
;
1634 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpGetFileProc
, sizeof(*task
));
1635 task
->remote_file
= heap_strdupW(lpszRemoteFile
);
1636 task
->new_file
= heap_strdupW(lpszNewFile
);
1637 task
->local_attr
= dwLocalFlagsAttribute
;
1638 task
->fail_if_exists
= fFailIfExists
;
1639 task
->flags
= dwInternetFlags
;
1640 task
->context
= dwContext
;
1642 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
1646 r
= FTP_FtpGetFileW(lpwfs
, lpszRemoteFile
, lpszNewFile
,
1647 fFailIfExists
, dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1651 WININET_Release( &lpwfs
->hdr
);
1657 /***********************************************************************
1658 * FTP_FtpGetFileW (Internal)
1660 * Retrieve file from the FTP server
1667 static BOOL
FTP_FtpGetFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1668 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1669 DWORD_PTR dwContext
)
1671 BOOL bSuccess
= FALSE
;
1673 appinfo_t
*hIC
= NULL
;
1675 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile
), debugstr_w(lpszNewFile
));
1677 /* Clear any error information */
1678 INTERNET_SetLastError(0);
1680 /* Ensure we can write to lpszNewfile by opening it */
1681 hFile
= CreateFileW(lpszNewFile
, GENERIC_WRITE
, 0, 0, fFailIfExists
?
1682 CREATE_NEW
: CREATE_ALWAYS
, dwLocalFlagsAttribute
, 0);
1683 if (INVALID_HANDLE_VALUE
== hFile
)
1686 /* Set up socket to retrieve data */
1687 if (FTP_SendRetrieve(lpwfs
, lpszRemoteFile
, dwInternetFlags
))
1691 /* Get data socket to server */
1692 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1697 FTP_RetrieveFileData(lpwfs
, nDataSocket
, hFile
);
1698 closesocket(nDataSocket
);
1700 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
1703 if (nResCode
== 226)
1706 FTP_SetResponseError(nResCode
);
1711 if (lpwfs
->lstnSocket
!= -1)
1713 closesocket(lpwfs
->lstnSocket
);
1714 lpwfs
->lstnSocket
= -1;
1719 hIC
= lpwfs
->lpAppInfo
;
1720 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1722 INTERNET_ASYNC_RESULT iar
;
1724 iar
.dwResult
= (DWORD
)bSuccess
;
1725 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1726 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1727 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1730 if (!bSuccess
) DeleteFileW(lpszNewFile
);
1734 /***********************************************************************
1735 * FtpGetFileSize (WININET.@)
1737 DWORD WINAPI
FtpGetFileSize( HINTERNET hFile
, LPDWORD lpdwFileSizeHigh
)
1739 FIXME("(%p, %p)\n", hFile
, lpdwFileSizeHigh
);
1741 if (lpdwFileSizeHigh
)
1742 *lpdwFileSizeHigh
= 0;
1747 /***********************************************************************
1748 * FtpDeleteFileA (WININET.@)
1750 * Delete a file on the ftp server
1757 BOOL WINAPI
FtpDeleteFileA(HINTERNET hFtpSession
, LPCSTR lpszFileName
)
1759 LPWSTR lpwzFileName
;
1762 lpwzFileName
= heap_strdupAtoW(lpszFileName
);
1763 ret
= FtpDeleteFileW(hFtpSession
, lpwzFileName
);
1764 heap_free(lpwzFileName
);
1771 } delete_file_task_t
;
1773 static void AsyncFtpDeleteFileProc(task_header_t
*hdr
)
1775 delete_file_task_t
*task
= (delete_file_task_t
*)hdr
;
1776 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1778 TRACE("%p\n", session
);
1780 FTP_FtpDeleteFileW(session
, task
->file_name
);
1781 heap_free(task
->file_name
);
1784 /***********************************************************************
1785 * FtpDeleteFileW (WININET.@)
1787 * Delete a file on the ftp server
1794 BOOL WINAPI
FtpDeleteFileW(HINTERNET hFtpSession
, LPCWSTR lpszFileName
)
1796 ftp_session_t
*lpwfs
;
1797 appinfo_t
*hIC
= NULL
;
1800 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1803 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1807 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1809 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1813 if (lpwfs
->download_in_progress
!= NULL
)
1815 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1821 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1825 hIC
= lpwfs
->lpAppInfo
;
1826 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1828 delete_file_task_t
*task
;
1830 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpDeleteFileProc
, sizeof(*task
));
1831 task
->file_name
= heap_strdupW(lpszFileName
);
1833 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
1837 r
= FTP_FtpDeleteFileW(lpwfs
, lpszFileName
);
1841 WININET_Release( &lpwfs
->hdr
);
1846 /***********************************************************************
1847 * FTP_FtpDeleteFileW (Internal)
1849 * Delete a file on the ftp server
1856 BOOL
FTP_FtpDeleteFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszFileName
)
1859 BOOL bSuccess
= FALSE
;
1860 appinfo_t
*hIC
= NULL
;
1862 TRACE("%p\n", lpwfs
);
1864 /* Clear any error information */
1865 INTERNET_SetLastError(0);
1867 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_DELE
, lpszFileName
, 0, 0, 0))
1870 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1873 if (nResCode
== 250)
1876 FTP_SetResponseError(nResCode
);
1879 hIC
= lpwfs
->lpAppInfo
;
1880 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1882 INTERNET_ASYNC_RESULT iar
;
1884 iar
.dwResult
= (DWORD
)bSuccess
;
1885 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1886 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1887 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1894 /***********************************************************************
1895 * FtpRemoveDirectoryA (WININET.@)
1897 * Remove a directory on the ftp server
1904 BOOL WINAPI
FtpRemoveDirectoryA(HINTERNET hFtpSession
, LPCSTR lpszDirectory
)
1906 LPWSTR lpwzDirectory
;
1909 lpwzDirectory
= heap_strdupAtoW(lpszDirectory
);
1910 ret
= FtpRemoveDirectoryW(hFtpSession
, lpwzDirectory
);
1911 heap_free(lpwzDirectory
);
1915 static void AsyncFtpRemoveDirectoryProc(task_header_t
*hdr
)
1917 directory_task_t
*task
= (directory_task_t
*)hdr
;
1918 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
1920 TRACE("%p\n", session
);
1922 FTP_FtpRemoveDirectoryW(session
, task
->directory
);
1923 heap_free(task
->directory
);
1926 /***********************************************************************
1927 * FtpRemoveDirectoryW (WININET.@)
1929 * Remove a directory on the ftp server
1936 BOOL WINAPI
FtpRemoveDirectoryW(HINTERNET hFtpSession
, LPCWSTR lpszDirectory
)
1938 ftp_session_t
*lpwfs
;
1939 appinfo_t
*hIC
= NULL
;
1942 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
1945 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1949 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1951 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1955 if (lpwfs
->download_in_progress
!= NULL
)
1957 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1963 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1967 hIC
= lpwfs
->lpAppInfo
;
1968 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1970 directory_task_t
*task
;
1972 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpRemoveDirectoryProc
, sizeof(*task
));
1973 task
->directory
= heap_strdupW(lpszDirectory
);
1975 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
1979 r
= FTP_FtpRemoveDirectoryW(lpwfs
, lpszDirectory
);
1983 WININET_Release( &lpwfs
->hdr
);
1988 /***********************************************************************
1989 * FTP_FtpRemoveDirectoryW (Internal)
1991 * Remove a directory on the ftp server
1998 BOOL
FTP_FtpRemoveDirectoryW(ftp_session_t
*lpwfs
, LPCWSTR lpszDirectory
)
2001 BOOL bSuccess
= FALSE
;
2002 appinfo_t
*hIC
= NULL
;
2006 /* Clear any error information */
2007 INTERNET_SetLastError(0);
2009 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RMD
, lpszDirectory
, 0, 0, 0))
2012 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2015 if (nResCode
== 250)
2018 FTP_SetResponseError(nResCode
);
2022 hIC
= lpwfs
->lpAppInfo
;
2023 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2025 INTERNET_ASYNC_RESULT iar
;
2027 iar
.dwResult
= (DWORD
)bSuccess
;
2028 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2029 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2030 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2037 /***********************************************************************
2038 * FtpRenameFileA (WININET.@)
2040 * Rename a file on the ftp server
2047 BOOL WINAPI
FtpRenameFileA(HINTERNET hFtpSession
, LPCSTR lpszSrc
, LPCSTR lpszDest
)
2053 lpwzSrc
= heap_strdupAtoW(lpszSrc
);
2054 lpwzDest
= heap_strdupAtoW(lpszDest
);
2055 ret
= FtpRenameFileW(hFtpSession
, lpwzSrc
, lpwzDest
);
2057 heap_free(lpwzDest
);
2065 } rename_file_task_t
;
2067 static void AsyncFtpRenameFileProc(task_header_t
*hdr
)
2069 rename_file_task_t
*task
= (rename_file_task_t
*)hdr
;
2070 ftp_session_t
*session
= (ftp_session_t
*)task
->hdr
.hdr
;
2072 TRACE("%p\n", session
);
2074 FTP_FtpRenameFileW(session
, task
->src_file
, task
->dst_file
);
2075 heap_free(task
->src_file
);
2076 heap_free(task
->dst_file
);
2079 /***********************************************************************
2080 * FtpRenameFileW (WININET.@)
2082 * Rename a file on the ftp server
2089 BOOL WINAPI
FtpRenameFileW(HINTERNET hFtpSession
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2091 ftp_session_t
*lpwfs
;
2092 appinfo_t
*hIC
= NULL
;
2095 lpwfs
= (ftp_session_t
*) get_handle_object( hFtpSession
);
2098 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2102 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2104 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2108 if (lpwfs
->download_in_progress
!= NULL
)
2110 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2114 if (!lpszSrc
|| !lpszDest
)
2116 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2120 hIC
= lpwfs
->lpAppInfo
;
2121 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2123 rename_file_task_t
*task
;
2125 task
= alloc_async_task(&lpwfs
->hdr
, AsyncFtpRenameFileProc
, sizeof(*task
));
2126 task
->src_file
= heap_strdupW(lpszSrc
);
2127 task
->dst_file
= heap_strdupW(lpszDest
);
2129 r
= res_to_le(INTERNET_AsyncCall(&task
->hdr
));
2133 r
= FTP_FtpRenameFileW(lpwfs
, lpszSrc
, lpszDest
);
2137 WININET_Release( &lpwfs
->hdr
);
2142 /***********************************************************************
2143 * FTP_FtpRenameFileW (Internal)
2145 * Rename a file on the ftp server
2152 BOOL
FTP_FtpRenameFileW(ftp_session_t
*lpwfs
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
2155 BOOL bSuccess
= FALSE
;
2156 appinfo_t
*hIC
= NULL
;
2160 /* Clear any error information */
2161 INTERNET_SetLastError(0);
2163 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNFR
, lpszSrc
, 0, 0, 0))
2166 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2167 if (nResCode
== 350)
2169 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNTO
, lpszDest
, 0, 0, 0))
2172 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2175 if (nResCode
== 250)
2178 FTP_SetResponseError(nResCode
);
2181 hIC
= lpwfs
->lpAppInfo
;
2182 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2184 INTERNET_ASYNC_RESULT iar
;
2186 iar
.dwResult
= (DWORD
)bSuccess
;
2187 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2188 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2189 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2195 /***********************************************************************
2196 * FtpCommandA (WININET.@)
2198 BOOL WINAPI
FtpCommandA( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2199 LPCSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2204 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2205 debugstr_a(lpszCommand
), dwContext
, phFtpCommand
);
2207 if (fExpectResponse
)
2209 FIXME("data connection not supported\n");
2213 if (!lpszCommand
|| !lpszCommand
[0])
2215 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2219 if (!(cmdW
= heap_strdupAtoW(lpszCommand
)))
2221 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2225 r
= FtpCommandW(hConnect
, fExpectResponse
, dwFlags
, cmdW
, dwContext
, phFtpCommand
);
2231 /***********************************************************************
2232 * FtpCommandW (WININET.@)
2234 BOOL WINAPI
FtpCommandW( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
2235 LPCWSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
2238 ftp_session_t
*lpwfs
;
2240 DWORD len
, nBytesSent
= 0;
2241 INT nResCode
, nRC
= 0;
2243 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
2244 debugstr_w(lpszCommand
), dwContext
, phFtpCommand
);
2246 if (!lpszCommand
|| !lpszCommand
[0])
2248 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2252 if (fExpectResponse
)
2254 FIXME("data connection not supported\n");
2258 lpwfs
= (ftp_session_t
*) get_handle_object( hConnect
);
2261 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
2265 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
2267 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
2271 if (lpwfs
->download_in_progress
!= NULL
)
2273 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2277 len
= WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, NULL
, 0, NULL
, NULL
) + strlen(szCRLF
);
2278 if ((cmd
= heap_alloc(len
)))
2279 WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, cmd
, len
, NULL
, NULL
);
2282 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2286 strcat(cmd
, szCRLF
);
2289 TRACE("Sending (%s) len(%d)\n", cmd
, len
);
2290 while ((nBytesSent
< len
) && (nRC
!= -1))
2292 nRC
= sock_send(lpwfs
->sndSocket
, cmd
+ nBytesSent
, len
- nBytesSent
, 0);
2296 TRACE("Sent %d bytes\n", nRC
);
2302 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2303 if (nResCode
> 0 && nResCode
< 400)
2306 FTP_SetResponseError(nResCode
);
2310 WININET_Release( &lpwfs
->hdr
);
2316 /***********************************************************************
2317 * FTPSESSION_Destroy (internal)
2319 * Deallocate session handle
2321 static void FTPSESSION_Destroy(object_header_t
*hdr
)
2323 ftp_session_t
*lpwfs
= (ftp_session_t
*) hdr
;
2327 WININET_Release(&lpwfs
->lpAppInfo
->hdr
);
2329 heap_free(lpwfs
->lpszPassword
);
2330 heap_free(lpwfs
->lpszUserName
);
2331 heap_free(lpwfs
->servername
);
2334 static void FTPSESSION_CloseConnection(object_header_t
*hdr
)
2336 ftp_session_t
*lpwfs
= (ftp_session_t
*) hdr
;
2340 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2341 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
2343 if (lpwfs
->download_in_progress
!= NULL
)
2344 lpwfs
->download_in_progress
->session_deleted
= TRUE
;
2346 if (lpwfs
->sndSocket
!= -1)
2347 closesocket(lpwfs
->sndSocket
);
2349 if (lpwfs
->lstnSocket
!= -1)
2350 closesocket(lpwfs
->lstnSocket
);
2352 if (lpwfs
->pasvSocket
!= -1)
2353 closesocket(lpwfs
->pasvSocket
);
2355 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
2356 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
2359 static DWORD
FTPSESSION_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
2362 case INTERNET_OPTION_HANDLE_TYPE
:
2363 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2365 if (*size
< sizeof(ULONG
))
2366 return ERROR_INSUFFICIENT_BUFFER
;
2368 *size
= sizeof(DWORD
);
2369 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_FTP
;
2370 return ERROR_SUCCESS
;
2373 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
2376 static const object_vtbl_t FTPSESSIONVtbl
= {
2378 FTPSESSION_CloseConnection
,
2379 FTPSESSION_QueryOption
,
2389 /***********************************************************************
2390 * FTP_Connect (internal)
2392 * Connect to a ftp server
2395 * HINTERNET a session handle on success
2400 * Windows uses 'anonymous' as the username, when given a NULL username
2401 * and a NULL password. The password is first looked up in:
2403 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2405 * If this entry is not present it uses the current username as the password.
2409 HINTERNET
FTP_Connect(appinfo_t
*hIC
, LPCWSTR lpszServerName
,
2410 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
2411 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
2412 DWORD dwInternalFlags
)
2414 static const WCHAR szKey
[] = {'S','o','f','t','w','a','r','e','\\',
2415 'M','i','c','r','o','s','o','f','t','\\',
2416 'W','i','n','d','o','w','s','\\',
2417 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2418 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2419 static const WCHAR szValue
[] = {'E','m','a','i','l','N','a','m','e',0};
2420 static const WCHAR szDefaultUsername
[] = {'a','n','o','n','y','m','o','u','s','\0'};
2421 static const WCHAR szEmpty
[] = {'\0'};
2422 struct sockaddr_in socketAddr
;
2424 socklen_t sock_namelen
;
2425 BOOL bSuccess
= FALSE
;
2426 ftp_session_t
*lpwfs
= NULL
;
2427 char szaddr
[INET6_ADDRSTRLEN
];
2429 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2430 hIC
, debugstr_w(lpszServerName
),
2431 nServerPort
, debugstr_w(lpszUserName
), debugstr_w(lpszPassword
));
2433 assert( hIC
->hdr
.htype
== WH_HINIT
);
2435 if ((!lpszUserName
|| !*lpszUserName
) && lpszPassword
&& *lpszPassword
)
2437 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2441 lpwfs
= alloc_object(&hIC
->hdr
, &FTPSESSIONVtbl
, sizeof(ftp_session_t
));
2444 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2448 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
2449 lpwfs
->serverport
= INTERNET_DEFAULT_FTP_PORT
;
2451 lpwfs
->serverport
= nServerPort
;
2453 lpwfs
->hdr
.htype
= WH_HFTPSESSION
;
2454 lpwfs
->hdr
.dwFlags
= dwFlags
;
2455 lpwfs
->hdr
.dwContext
= dwContext
;
2456 lpwfs
->hdr
.dwInternalFlags
|= dwInternalFlags
;
2457 lpwfs
->download_in_progress
= NULL
;
2458 lpwfs
->sndSocket
= -1;
2459 lpwfs
->lstnSocket
= -1;
2460 lpwfs
->pasvSocket
= -1;
2462 WININET_AddRef( &hIC
->hdr
);
2463 lpwfs
->lpAppInfo
= hIC
;
2464 list_add_head( &hIC
->hdr
.children
, &lpwfs
->hdr
.entry
);
2466 if(hIC
->proxy
&& hIC
->accessType
== INTERNET_OPEN_TYPE_PROXY
) {
2467 if(strchrW(hIC
->proxy
, ' '))
2468 FIXME("Several proxies not implemented.\n");
2469 if(hIC
->proxyBypass
)
2470 FIXME("Proxy bypass is ignored.\n");
2472 if (!lpszUserName
|| !lpszUserName
[0]) {
2474 WCHAR szPassword
[MAX_PATH
];
2475 DWORD len
= sizeof(szPassword
);
2477 lpwfs
->lpszUserName
= heap_strdupW(szDefaultUsername
);
2479 RegOpenKeyW(HKEY_CURRENT_USER
, szKey
, &key
);
2480 if (RegQueryValueExW(key
, szValue
, NULL
, NULL
, (LPBYTE
)szPassword
, &len
)) {
2481 /* Nothing in the registry, get the username and use that as the password */
2482 if (!GetUserNameW(szPassword
, &len
)) {
2483 /* Should never get here, but use an empty password as failsafe */
2484 strcpyW(szPassword
, szEmpty
);
2489 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword
));
2490 lpwfs
->lpszPassword
= heap_strdupW(szPassword
);
2493 lpwfs
->lpszUserName
= heap_strdupW(lpszUserName
);
2494 lpwfs
->lpszPassword
= heap_strdupW(lpszPassword
? lpszPassword
: szEmpty
);
2496 lpwfs
->servername
= heap_strdupW(lpszServerName
);
2498 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2499 if (!(lpwfs
->hdr
.dwInternalFlags
& INET_OPENURL
))
2501 INTERNET_ASYNC_RESULT iar
;
2503 iar
.dwResult
= (DWORD_PTR
)lpwfs
->hdr
.hInternet
;
2504 iar
.dwError
= ERROR_SUCCESS
;
2506 SendAsyncCallback(&hIC
->hdr
, dwContext
,
2507 INTERNET_STATUS_HANDLE_CREATED
, &iar
,
2508 sizeof(INTERNET_ASYNC_RESULT
));
2511 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
2512 (LPWSTR
) lpszServerName
, (strlenW(lpszServerName
)+1) * sizeof(WCHAR
));
2514 sock_namelen
= sizeof(socketAddr
);
2515 if (!GetAddress(lpszServerName
, lpwfs
->serverport
, (struct sockaddr
*)&socketAddr
, &sock_namelen
, szaddr
))
2517 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
2521 if (socketAddr
.sin_family
!= AF_INET
)
2523 WARN("unsupported address family %d\n", socketAddr
.sin_family
);
2524 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2528 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
2529 szaddr
, strlen(szaddr
)+1);
2532 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2535 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2539 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTING_TO_SERVER
,
2540 szaddr
, strlen(szaddr
)+1);
2542 if (connect(nsocket
, (struct sockaddr
*)&socketAddr
, sock_namelen
) < 0)
2544 ERR("Unable to connect (%d)\n", WSAGetLastError());
2545 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2546 closesocket(nsocket
);
2550 TRACE("Connected to server\n");
2551 lpwfs
->sndSocket
= nsocket
;
2552 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTED_TO_SERVER
,
2553 szaddr
, strlen(szaddr
)+1);
2555 sock_namelen
= sizeof(lpwfs
->socketAddress
);
2556 getsockname(nsocket
, (struct sockaddr
*) &lpwfs
->socketAddress
, &sock_namelen
);
2558 if (FTP_ConnectToHost(lpwfs
))
2560 TRACE("Successfully logged into server\n");
2569 WININET_Release( &lpwfs
->hdr
);
2573 return lpwfs
->hdr
.hInternet
;
2577 /***********************************************************************
2578 * FTP_ConnectToHost (internal)
2580 * Connect to a ftp server
2587 static BOOL
FTP_ConnectToHost(ftp_session_t
*lpwfs
)
2590 BOOL bSuccess
= FALSE
;
2593 FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2595 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_USER
, lpwfs
->lpszUserName
, 0, 0, 0))
2598 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2601 /* Login successful... */
2602 if (nResCode
== 230)
2604 /* User name okay, need password... */
2605 else if (nResCode
== 331)
2606 bSuccess
= FTP_SendPassword(lpwfs
);
2607 /* Need account for login... */
2608 else if (nResCode
== 332)
2609 bSuccess
= FTP_SendAccount(lpwfs
);
2611 FTP_SetResponseError(nResCode
);
2614 TRACE("Returning %d\n", bSuccess
);
2619 /***********************************************************************
2620 * FTP_GetNextLine (internal)
2622 * Parse next line in directory string listing
2625 * Pointer to beginning of next line
2630 static LPSTR
FTP_GetNextLine(INT nSocket
, LPDWORD dwLen
)
2632 struct timeval tv
= {RESPONSE_TIMEOUT
,0};
2635 LPSTR lpszBuffer
= INTERNET_GetResponseBuffer();
2640 FD_SET(nSocket
, &set
);
2642 while (nRecv
< MAX_REPLY_LEN
)
2644 if (select(nSocket
+1, &set
, NULL
, NULL
, &tv
) > 0)
2646 if (sock_recv(nSocket
, &lpszBuffer
[nRecv
], 1, 0) <= 0)
2648 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
2652 if (lpszBuffer
[nRecv
] == '\n')
2654 lpszBuffer
[nRecv
] = '\0';
2656 TRACE(":%d %s\n", nRecv
, lpszBuffer
);
2659 if (lpszBuffer
[nRecv
] != '\r')
2664 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT
);
2672 /***********************************************************************
2673 * FTP_SendCommandA (internal)
2675 * Send command to server
2682 static BOOL
FTP_SendCommandA(INT nSocket
, FTP_COMMAND ftpCmd
, LPCSTR lpszParam
,
2683 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2687 DWORD nBytesSent
= 0;
2691 TRACE("%d: (%s) %d\n", ftpCmd
, debugstr_a(lpszParam
), nSocket
);
2695 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2698 dwParamLen
= lpszParam
?strlen(lpszParam
)+1:0;
2699 len
= dwParamLen
+ strlen(szFtpCommands
[ftpCmd
]) + strlen(szCRLF
);
2700 if (NULL
== (buf
= heap_alloc(len
+1)))
2702 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2705 sprintf(buf
, "%s%s%s%s", szFtpCommands
[ftpCmd
], dwParamLen
? " " : "",
2706 dwParamLen
? lpszParam
: "", szCRLF
);
2708 TRACE("Sending (%s) len(%d)\n", buf
, len
);
2709 while((nBytesSent
< len
) && (nRC
!= -1))
2711 nRC
= sock_send(nSocket
, buf
+nBytesSent
, len
- nBytesSent
, 0);
2718 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_REQUEST_SENT
,
2719 &nBytesSent
, sizeof(DWORD
));
2722 TRACE("Sent %d bytes\n", nBytesSent
);
2726 /***********************************************************************
2727 * FTP_SendCommand (internal)
2729 * Send command to server
2736 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
2737 INTERNET_STATUS_CALLBACK lpfnStatusCB
, object_header_t
*hdr
, DWORD_PTR dwContext
)
2740 LPSTR lpszParamA
= heap_strdupWtoA(lpszParam
);
2741 ret
= FTP_SendCommandA(nSocket
, ftpCmd
, lpszParamA
, lpfnStatusCB
, hdr
, dwContext
);
2742 heap_free(lpszParamA
);
2746 /***********************************************************************
2747 * FTP_ReceiveResponse (internal)
2749 * Receive response from server
2752 * Reply code on success
2756 INT
FTP_ReceiveResponse(ftp_session_t
*lpwfs
, DWORD_PTR dwContext
)
2758 LPSTR lpszResponse
= INTERNET_GetResponseBuffer();
2761 char firstprefix
[5];
2762 BOOL multiline
= FALSE
;
2764 TRACE("socket(%d)\n", lpwfs
->sndSocket
);
2766 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2770 if (!FTP_GetNextLine(lpwfs
->sndSocket
, &nRecv
))
2777 if(lpszResponse
[3] != '-')
2780 { /* Start of multiline response. Loop until we get "nnn " */
2782 memcpy(firstprefix
, lpszResponse
, 3);
2783 firstprefix
[3] = ' ';
2784 firstprefix
[4] = '\0';
2789 if(!memcmp(firstprefix
, lpszResponse
, 4))
2797 rc
= atoi(lpszResponse
);
2799 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
2800 &nRecv
, sizeof(DWORD
));
2804 TRACE("return %d\n", rc
);
2809 /***********************************************************************
2810 * FTP_SendPassword (internal)
2812 * Send password to ftp server
2819 static BOOL
FTP_SendPassword(ftp_session_t
*lpwfs
)
2822 BOOL bSuccess
= FALSE
;
2825 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASS
, lpwfs
->lpszPassword
, 0, 0, 0))
2828 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2831 TRACE("Received reply code %d\n", nResCode
);
2832 /* Login successful... */
2833 if (nResCode
== 230)
2835 /* Command not implemented, superfluous at the server site... */
2836 /* Need account for login... */
2837 else if (nResCode
== 332)
2838 bSuccess
= FTP_SendAccount(lpwfs
);
2840 FTP_SetResponseError(nResCode
);
2844 TRACE("Returning %d\n", bSuccess
);
2849 /***********************************************************************
2850 * FTP_SendAccount (internal)
2859 static BOOL
FTP_SendAccount(ftp_session_t
*lpwfs
)
2862 BOOL bSuccess
= FALSE
;
2865 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_ACCT
, szNoAccount
, 0, 0, 0))
2868 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2872 FTP_SetResponseError(nResCode
);
2879 /***********************************************************************
2880 * FTP_SendStore (internal)
2882 * Send request to upload file to ftp server
2889 static BOOL
FTP_SendStore(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2892 BOOL bSuccess
= FALSE
;
2895 if (!FTP_InitListenSocket(lpwfs
))
2898 if (!FTP_SendType(lpwfs
, dwType
))
2901 if (!FTP_SendPortOrPasv(lpwfs
))
2904 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_STOR
, lpszRemoteFile
, 0, 0, 0))
2906 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2909 if (nResCode
== 150 || nResCode
== 125)
2912 FTP_SetResponseError(nResCode
);
2916 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2918 closesocket(lpwfs
->lstnSocket
);
2919 lpwfs
->lstnSocket
= -1;
2926 /***********************************************************************
2927 * FTP_InitListenSocket (internal)
2929 * Create a socket to listen for server response
2936 static BOOL
FTP_InitListenSocket(ftp_session_t
*lpwfs
)
2938 BOOL bSuccess
= FALSE
;
2939 socklen_t namelen
= sizeof(lpwfs
->lstnSocketAddress
);
2944 lpwfs
->lstnSocket
= socket(AF_INET
, SOCK_STREAM
, 0);
2945 if (lpwfs
->lstnSocket
== -1)
2947 TRACE("Unable to create listening socket\n");
2951 /* We obtain our ip addr from the name of the command channel socket */
2952 lpwfs
->lstnSocketAddress
= lpwfs
->socketAddress
;
2954 /* and get the system to assign us a port */
2955 lpwfs
->lstnSocketAddress
.sin_port
= htons(0);
2957 if (bind(lpwfs
->lstnSocket
,(struct sockaddr
*) &lpwfs
->lstnSocketAddress
, sizeof(lpwfs
->lstnSocketAddress
)) == -1)
2959 TRACE("Unable to bind socket\n");
2963 if (listen(lpwfs
->lstnSocket
, MAX_BACKLOG
) == -1)
2965 TRACE("listen failed\n");
2969 if (getsockname(lpwfs
->lstnSocket
, (struct sockaddr
*) &lpwfs
->lstnSocketAddress
, &namelen
) != -1)
2973 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2975 closesocket(lpwfs
->lstnSocket
);
2976 lpwfs
->lstnSocket
= -1;
2983 /***********************************************************************
2984 * FTP_SendType (internal)
2986 * Tell server type of data being transferred
2992 * W98SE doesn't cache the type that's currently set
2993 * (i.e. it sends it always),
2994 * so we probably don't want to do that either.
2996 static BOOL
FTP_SendType(ftp_session_t
*lpwfs
, DWORD dwType
)
2999 WCHAR type
[] = { 'I','\0' };
3000 BOOL bSuccess
= FALSE
;
3003 if (dwType
& INTERNET_FLAG_TRANSFER_ASCII
)
3006 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_TYPE
, type
, 0, 0, 0))
3009 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
)/100;
3015 FTP_SetResponseError(nResCode
);
3023 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
3024 /***********************************************************************
3025 * FTP_GetFileSize (internal)
3027 * Retrieves from the server the size of the given file
3034 static BOOL
FTP_GetFileSize(ftp_session_t
*lpwfs
, LPCWSTR lpszRemoteFile
, DWORD
*dwSize
)
3037 BOOL bSuccess
= FALSE
;
3041 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_SIZE
, lpszRemoteFile
, 0, 0, 0))
3044 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3047 if (nResCode
== 213) {
3048 /* Now parses the output to get the actual file size */
3050 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
3052 for (i
= 0; (lpszResponseBuffer
[i
] != ' ') && (lpszResponseBuffer
[i
] != '\0'); i
++) ;
3053 if (lpszResponseBuffer
[i
] == '\0') return FALSE
;
3054 *dwSize
= atol(&(lpszResponseBuffer
[i
+ 1]));
3058 FTP_SetResponseError(nResCode
);
3068 /***********************************************************************
3069 * FTP_SendPort (internal)
3071 * Tell server which port to use
3078 static BOOL
FTP_SendPort(ftp_session_t
*lpwfs
)
3080 static const WCHAR szIPFormat
[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
3082 WCHAR szIPAddress
[64];
3083 BOOL bSuccess
= FALSE
;
3086 sprintfW(szIPAddress
, szIPFormat
,
3087 lpwfs
->lstnSocketAddress
.sin_addr
.S_un
.S_addr
&0x000000FF,
3088 (lpwfs
->lstnSocketAddress
.sin_addr
.S_un
.S_addr
&0x0000FF00)>>8,
3089 (lpwfs
->lstnSocketAddress
.sin_addr
.S_un
.S_addr
&0x00FF0000)>>16,
3090 (lpwfs
->lstnSocketAddress
.sin_addr
.S_un
.S_addr
&0xFF000000)>>24,
3091 lpwfs
->lstnSocketAddress
.sin_port
& 0xFF,
3092 (lpwfs
->lstnSocketAddress
.sin_port
& 0xFF00)>>8);
3094 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PORT
, szIPAddress
, 0, 0, 0))
3097 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3100 if (nResCode
== 200)
3103 FTP_SetResponseError(nResCode
);
3111 /***********************************************************************
3112 * FTP_DoPassive (internal)
3114 * Tell server that we want to do passive transfers
3115 * and connect data socket
3122 static BOOL
FTP_DoPassive(ftp_session_t
*lpwfs
)
3125 BOOL bSuccess
= FALSE
;
3128 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASV
, NULL
, 0, 0, 0))
3131 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3134 if (nResCode
== 227)
3136 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
3140 char *pAddr
, *pPort
;
3142 struct sockaddr_in dataSocketAddress
;
3144 p
= lpszResponseBuffer
+4; /* skip status code */
3145 while (*p
!= '\0' && (*p
< '0' || *p
> '9')) p
++;
3149 ERR("no address found in response, aborting\n");
3153 if (sscanf(p
, "%d,%d,%d,%d,%d,%d", &f
[0], &f
[1], &f
[2], &f
[3],
3156 ERR("unknown response address format '%s', aborting\n", p
);
3159 for (i
=0; i
< 6; i
++)
3162 dataSocketAddress
= lpwfs
->socketAddress
;
3163 pAddr
= (char *)&(dataSocketAddress
.sin_addr
.S_un
.S_addr
);
3164 pPort
= (char *)&(dataSocketAddress
.sin_port
);
3172 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
3176 if (connect(nsocket
, (struct sockaddr
*)&dataSocketAddress
, sizeof(dataSocketAddress
)))
3178 ERR("can't connect passive FTP data port.\n");
3179 closesocket(nsocket
);
3182 lpwfs
->pasvSocket
= nsocket
;
3186 FTP_SetResponseError(nResCode
);
3194 static BOOL
FTP_SendPortOrPasv(ftp_session_t
*lpwfs
)
3196 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3198 if (!FTP_DoPassive(lpwfs
))
3203 if (!FTP_SendPort(lpwfs
))
3210 /***********************************************************************
3211 * FTP_GetDataSocket (internal)
3213 * Either accepts an incoming data socket connection from the server
3214 * or just returns the already opened socket after a PASV command
3215 * in case of passive FTP.
3223 static BOOL
FTP_GetDataSocket(ftp_session_t
*lpwfs
, LPINT nDataSocket
)
3225 struct sockaddr_in saddr
;
3226 socklen_t addrlen
= sizeof(saddr
);
3229 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
3231 *nDataSocket
= lpwfs
->pasvSocket
;
3232 lpwfs
->pasvSocket
= -1;
3236 *nDataSocket
= accept(lpwfs
->lstnSocket
, (struct sockaddr
*) &saddr
, &addrlen
);
3237 closesocket(lpwfs
->lstnSocket
);
3238 lpwfs
->lstnSocket
= -1;
3240 return *nDataSocket
!= -1;
3244 /***********************************************************************
3245 * FTP_SendData (internal)
3247 * Send data to the server
3254 static BOOL
FTP_SendData(ftp_session_t
*lpwfs
, INT nDataSocket
, HANDLE hFile
)
3256 BY_HANDLE_FILE_INFORMATION fi
;
3257 DWORD nBytesRead
= 0;
3258 DWORD nBytesSent
= 0;
3259 DWORD nTotalSent
= 0;
3260 DWORD nBytesToSend
, nLen
;
3262 time_t s_long_time
, e_long_time
;
3267 lpszBuffer
= heap_alloc_zero(sizeof(CHAR
)*DATA_PACKET_SIZE
);
3269 /* Get the size of the file. */
3270 GetFileInformationByHandle(hFile
, &fi
);
3275 nBytesToSend
= nBytesRead
- nBytesSent
;
3277 if (nBytesToSend
<= 0)
3279 /* Read data from file. */
3281 if (!ReadFile(hFile
, lpszBuffer
, DATA_PACKET_SIZE
, &nBytesRead
, 0))
3282 ERR("Failed reading from file\n");
3285 nBytesToSend
= nBytesRead
;
3290 nLen
= DATA_PACKET_SIZE
< nBytesToSend
?
3291 DATA_PACKET_SIZE
: nBytesToSend
;
3292 nRC
= sock_send(nDataSocket
, lpszBuffer
, nLen
, 0);
3300 /* Do some computation to display the status. */
3302 nSeconds
= e_long_time
- s_long_time
;
3303 if( nSeconds
/ 60 > 0 )
3305 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
3306 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
/ 60,
3307 nSeconds
% 60, (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3311 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
3312 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
,
3313 (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
3315 } while (nRC
!= -1);