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
31 #include "wine/port.h"
33 #if defined(__MINGW32__) || defined (_MSC_VER)
42 #include <sys/types.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 # include <sys/socket.h>
46 #ifdef HAVE_ARPA_INET_H
47 # include <arpa/inet.h>
52 #ifdef HAVE_SYS_IOCTL_H
53 # include <sys/ioctl.h>
69 #include "wine/debug.h"
72 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
74 typedef struct _ftp_session_t ftp_session_t;
79 ftp_session_t *lpFtpSession;
83 HANDLE cache_file_handle;
92 int pasvSocket; /* data socket connected by us in case of passive FTP */
93 ftp_file_t *download_in_progress;
94 struct sockaddr_in socketAddress;
95 struct sockaddr_in lstnSocketAddress;
97 INTERNET_PORT serverport;
107 SYSTEMTIME tmLastModified;
108 unsigned short permissions;
109 } FILEPROPERTIESW, *LPFILEPROPERTIESW;
114 ftp_session_t *lpFtpSession;
117 LPFILEPROPERTIESW lpafp;
118 } WININETFTPFINDNEXTW, *LPWININETFTPFINDNEXTW;
120 #define DATA_PACKET_SIZE 0x2000
121 #define szCRLF "\r\n"
122 #define MAX_BACKLOG 5
124 /* Testing shows that Windows only accepts dwFlags where the last
125 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
127 #define FTP_CONDITION_MASK 0x0007
130 /* FTP commands with arguments. */
146 /* FTP commands without arguments. */
155 static const CHAR *const szFtpCommands[] = {
178 static const CHAR szMonths[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
179 static const WCHAR szNoAccount[] = {'n','o','a','c','c','o','u','n','t','\0'};
181 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
182 INTERNET_STATUS_CALLBACK lpfnStatusCB, object_header_t *hdr, DWORD_PTR dwContext);
183 static BOOL FTP_SendStore(ftp_session_t*, LPCWSTR lpszRemoteFile, DWORD dwType);
184 static BOOL FTP_GetDataSocket(ftp_session_t*, LPINT nDataSocket);
185 static BOOL FTP_SendData(ftp_session_t*, INT nDataSocket, HANDLE hFile);
186 static INT FTP_ReceiveResponse(ftp_session_t*, DWORD_PTR dwContext);
187 static BOOL FTP_SendRetrieve(ftp_session_t*, LPCWSTR lpszRemoteFile, DWORD dwType);
188 static BOOL FTP_RetrieveFileData(ftp_session_t*, INT nDataSocket, HANDLE hFile);
189 static BOOL FTP_InitListenSocket(ftp_session_t*);
190 static BOOL FTP_ConnectToHost(ftp_session_t*);
191 static BOOL FTP_SendPassword(ftp_session_t*);
192 static BOOL FTP_SendAccount(ftp_session_t*);
193 static BOOL FTP_SendType(ftp_session_t*, DWORD dwType);
194 static BOOL FTP_SendPort(ftp_session_t*);
195 static BOOL FTP_DoPassive(ftp_session_t*);
196 static BOOL FTP_SendPortOrPasv(ftp_session_t*);
197 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp);
198 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW fileprop);
199 static BOOL FTP_ParseDirectory(ftp_session_t*, INT nSocket, LPCWSTR lpszSearchFile,
200 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp);
201 static HINTERNET FTP_ReceiveFileList(ftp_session_t*, INT nSocket, LPCWSTR lpszSearchFile,
202 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext);
203 static DWORD FTP_SetResponseError(DWORD dwResponse);
204 static BOOL FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp, LPWIN32_FIND_DATAW lpFindFileData);
205 static BOOL FTP_FtpPutFileW(ftp_session_t*, LPCWSTR lpszLocalFile,
206 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext);
207 static BOOL FTP_FtpSetCurrentDirectoryW(ftp_session_t*, LPCWSTR lpszDirectory);
208 static BOOL FTP_FtpCreateDirectoryW(ftp_session_t*, LPCWSTR lpszDirectory);
209 static HINTERNET FTP_FtpFindFirstFileW(ftp_session_t*,
210 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext);
211 static BOOL FTP_FtpGetCurrentDirectoryW(ftp_session_t*, LPWSTR lpszCurrentDirectory,
212 LPDWORD lpdwCurrentDirectory);
213 static BOOL FTP_FtpRenameFileW(ftp_session_t*, LPCWSTR lpszSrc, LPCWSTR lpszDest);
214 static BOOL FTP_FtpRemoveDirectoryW(ftp_session_t*, LPCWSTR lpszDirectory);
215 static BOOL FTP_FtpDeleteFileW(ftp_session_t*, LPCWSTR lpszFileName);
216 static BOOL FTP_FtpGetFileW(ftp_session_t*, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
217 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
218 DWORD_PTR dwContext);
220 /* A temporary helper until we get rid of INTERNET_GetLastError calls */
221 static BOOL res_to_le(DWORD res)
223 if(res != ERROR_SUCCESS)
224 INTERNET_SetLastError(res);
225 return res == ERROR_SUCCESS;
228 /***********************************************************************
229 * FtpPutFileA (WININET.@)
231 * Uploads a file to the FTP server
238 BOOL WINAPI FtpPutFileA(HINTERNET hConnect, LPCSTR lpszLocalFile,
239 LPCSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
241 LPWSTR lpwzLocalFile;
242 LPWSTR lpwzNewRemoteFile;
245 lpwzLocalFile = heap_strdupAtoW(lpszLocalFile);
246 lpwzNewRemoteFile = heap_strdupAtoW(lpszNewRemoteFile);
247 ret = FtpPutFileW(hConnect, lpwzLocalFile, lpwzNewRemoteFile,
249 HeapFree(GetProcessHeap(), 0, lpwzLocalFile);
250 HeapFree(GetProcessHeap(), 0, lpwzNewRemoteFile);
254 static void AsyncFtpPutFileProc(WORKREQUEST *workRequest)
256 struct WORKREQ_FTPPUTFILEW const *req = &workRequest->u.FtpPutFileW;
257 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
259 TRACE("%p\n", lpwfs);
261 FTP_FtpPutFileW(lpwfs, req->lpszLocalFile,
262 req->lpszNewRemoteFile, req->dwFlags, req->dwContext);
264 HeapFree(GetProcessHeap(), 0, req->lpszLocalFile);
265 HeapFree(GetProcessHeap(), 0, req->lpszNewRemoteFile);
268 /***********************************************************************
269 * FtpPutFileW (WININET.@)
271 * Uploads a file to the FTP server
278 BOOL WINAPI FtpPutFileW(HINTERNET hConnect, LPCWSTR lpszLocalFile,
279 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
281 ftp_session_t *lpwfs;
282 appinfo_t *hIC = NULL;
285 if (!lpszLocalFile || !lpszNewRemoteFile)
287 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
291 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
294 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
298 if (WH_HFTPSESSION != lpwfs->hdr.htype)
300 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
304 if (lpwfs->download_in_progress != NULL)
306 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
310 if ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
312 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
316 hIC = lpwfs->lpAppInfo;
317 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
319 WORKREQUEST workRequest;
320 struct WORKREQ_FTPPUTFILEW *req = &workRequest.u.FtpPutFileW;
322 workRequest.asyncproc = AsyncFtpPutFileProc;
323 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
324 req->lpszLocalFile = heap_strdupW(lpszLocalFile);
325 req->lpszNewRemoteFile = heap_strdupW(lpszNewRemoteFile);
326 req->dwFlags = dwFlags;
327 req->dwContext = dwContext;
329 r = res_to_le(INTERNET_AsyncCall(&workRequest));
333 r = FTP_FtpPutFileW(lpwfs, lpszLocalFile,
334 lpszNewRemoteFile, dwFlags, dwContext);
338 WININET_Release( &lpwfs->hdr );
343 /***********************************************************************
344 * FTP_FtpPutFileW (Internal)
346 * Uploads a file to the FTP server
353 static BOOL FTP_FtpPutFileW(ftp_session_t *lpwfs, LPCWSTR lpszLocalFile,
354 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
357 BOOL bSuccess = FALSE;
358 appinfo_t *hIC = NULL;
361 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile), debugstr_w(lpszNewRemoteFile));
363 /* Clear any error information */
364 INTERNET_SetLastError(0);
366 /* Open file to be uploaded */
367 if (INVALID_HANDLE_VALUE ==
368 (hFile = CreateFileW(lpszLocalFile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)))
369 /* Let CreateFile set the appropriate error */
372 hIC = lpwfs->lpAppInfo;
374 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
376 if (FTP_SendStore(lpwfs, lpszNewRemoteFile, dwFlags))
380 /* Get data socket to server */
381 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
383 FTP_SendData(lpwfs, nDataSocket, hFile);
384 closesocket(nDataSocket);
385 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
391 FTP_SetResponseError(nResCode);
396 if (lpwfs->lstnSocket != -1)
398 closesocket(lpwfs->lstnSocket);
399 lpwfs->lstnSocket = -1;
402 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
404 INTERNET_ASYNC_RESULT iar;
406 iar.dwResult = (DWORD)bSuccess;
407 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
408 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
409 &iar, sizeof(INTERNET_ASYNC_RESULT));
418 /***********************************************************************
419 * FtpSetCurrentDirectoryA (WININET.@)
421 * Change the working directory on the FTP server
428 BOOL WINAPI FtpSetCurrentDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
430 LPWSTR lpwzDirectory;
433 lpwzDirectory = heap_strdupAtoW(lpszDirectory);
434 ret = FtpSetCurrentDirectoryW(hConnect, lpwzDirectory);
435 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
440 static void AsyncFtpSetCurrentDirectoryProc(WORKREQUEST *workRequest)
442 struct WORKREQ_FTPSETCURRENTDIRECTORYW const *req = &workRequest->u.FtpSetCurrentDirectoryW;
443 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
445 TRACE("%p\n", lpwfs);
447 FTP_FtpSetCurrentDirectoryW(lpwfs, req->lpszDirectory);
448 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
451 /***********************************************************************
452 * FtpSetCurrentDirectoryW (WININET.@)
454 * Change the working directory on the FTP server
461 BOOL WINAPI FtpSetCurrentDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
463 ftp_session_t *lpwfs = NULL;
464 appinfo_t *hIC = NULL;
469 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
473 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
474 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
476 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
480 if (lpwfs->download_in_progress != NULL)
482 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
486 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
488 hIC = lpwfs->lpAppInfo;
489 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
491 WORKREQUEST workRequest;
492 struct WORKREQ_FTPSETCURRENTDIRECTORYW *req;
494 workRequest.asyncproc = AsyncFtpSetCurrentDirectoryProc;
495 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
496 req = &workRequest.u.FtpSetCurrentDirectoryW;
497 req->lpszDirectory = heap_strdupW(lpszDirectory);
499 r = res_to_le(INTERNET_AsyncCall(&workRequest));
503 r = FTP_FtpSetCurrentDirectoryW(lpwfs, lpszDirectory);
508 WININET_Release( &lpwfs->hdr );
514 /***********************************************************************
515 * FTP_FtpSetCurrentDirectoryW (Internal)
517 * Change the working directory on the FTP server
524 static BOOL FTP_FtpSetCurrentDirectoryW(ftp_session_t *lpwfs, LPCWSTR lpszDirectory)
527 appinfo_t *hIC = NULL;
528 DWORD bSuccess = FALSE;
530 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
532 /* Clear any error information */
533 INTERNET_SetLastError(0);
535 hIC = lpwfs->lpAppInfo;
536 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_CWD, lpszDirectory,
537 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
540 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
547 FTP_SetResponseError(nResCode);
551 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
553 INTERNET_ASYNC_RESULT iar;
555 iar.dwResult = bSuccess;
556 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
557 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
558 &iar, sizeof(INTERNET_ASYNC_RESULT));
564 /***********************************************************************
565 * FtpCreateDirectoryA (WININET.@)
567 * Create new directory on the FTP server
574 BOOL WINAPI FtpCreateDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
576 LPWSTR lpwzDirectory;
579 lpwzDirectory = heap_strdupAtoW(lpszDirectory);
580 ret = FtpCreateDirectoryW(hConnect, lpwzDirectory);
581 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
586 static void AsyncFtpCreateDirectoryProc(WORKREQUEST *workRequest)
588 struct WORKREQ_FTPCREATEDIRECTORYW const *req = &workRequest->u.FtpCreateDirectoryW;
589 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
591 TRACE(" %p\n", lpwfs);
593 FTP_FtpCreateDirectoryW(lpwfs, req->lpszDirectory);
594 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
597 /***********************************************************************
598 * FtpCreateDirectoryW (WININET.@)
600 * Create new directory on the FTP server
607 BOOL WINAPI FtpCreateDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
609 ftp_session_t *lpwfs;
610 appinfo_t *hIC = NULL;
613 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
616 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
620 if (WH_HFTPSESSION != lpwfs->hdr.htype)
622 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
626 if (lpwfs->download_in_progress != NULL)
628 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
634 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
638 hIC = lpwfs->lpAppInfo;
639 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
641 WORKREQUEST workRequest;
642 struct WORKREQ_FTPCREATEDIRECTORYW *req;
644 workRequest.asyncproc = AsyncFtpCreateDirectoryProc;
645 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
646 req = &workRequest.u.FtpCreateDirectoryW;
647 req->lpszDirectory = heap_strdupW(lpszDirectory);
649 r = res_to_le(INTERNET_AsyncCall(&workRequest));
653 r = FTP_FtpCreateDirectoryW(lpwfs, lpszDirectory);
656 WININET_Release( &lpwfs->hdr );
662 /***********************************************************************
663 * FTP_FtpCreateDirectoryW (Internal)
665 * Create new directory on the FTP server
672 static BOOL FTP_FtpCreateDirectoryW(ftp_session_t *lpwfs, LPCWSTR lpszDirectory)
675 BOOL bSuccess = FALSE;
676 appinfo_t *hIC = NULL;
678 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
680 /* Clear any error information */
681 INTERNET_SetLastError(0);
683 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_MKD, lpszDirectory, 0, 0, 0))
686 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
692 FTP_SetResponseError(nResCode);
696 hIC = lpwfs->lpAppInfo;
697 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
699 INTERNET_ASYNC_RESULT iar;
701 iar.dwResult = (DWORD)bSuccess;
702 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
703 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
704 &iar, sizeof(INTERNET_ASYNC_RESULT));
710 /***********************************************************************
711 * FtpFindFirstFileA (WININET.@)
713 * Search the specified directory
716 * HINTERNET on success
720 HINTERNET WINAPI FtpFindFirstFileA(HINTERNET hConnect,
721 LPCSTR lpszSearchFile, LPWIN32_FIND_DATAA lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
723 LPWSTR lpwzSearchFile;
724 WIN32_FIND_DATAW wfd;
725 LPWIN32_FIND_DATAW lpFindFileDataW;
728 lpwzSearchFile = heap_strdupAtoW(lpszSearchFile);
729 lpFindFileDataW = lpFindFileData?&wfd:NULL;
730 ret = FtpFindFirstFileW(hConnect, lpwzSearchFile, lpFindFileDataW, dwFlags, dwContext);
731 HeapFree(GetProcessHeap(), 0, lpwzSearchFile);
733 if (ret && lpFindFileData)
734 WININET_find_data_WtoA(lpFindFileDataW, lpFindFileData);
740 static void AsyncFtpFindFirstFileProc(WORKREQUEST *workRequest)
742 struct WORKREQ_FTPFINDFIRSTFILEW const *req = &workRequest->u.FtpFindFirstFileW;
743 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
745 TRACE("%p\n", lpwfs);
747 FTP_FtpFindFirstFileW(lpwfs, req->lpszSearchFile,
748 req->lpFindFileData, req->dwFlags, req->dwContext);
749 HeapFree(GetProcessHeap(), 0, req->lpszSearchFile);
752 /***********************************************************************
753 * FtpFindFirstFileW (WININET.@)
755 * Search the specified directory
758 * HINTERNET on success
762 HINTERNET WINAPI FtpFindFirstFileW(HINTERNET hConnect,
763 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
765 ftp_session_t *lpwfs;
766 appinfo_t *hIC = NULL;
769 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
770 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
772 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
776 if (lpwfs->download_in_progress != NULL)
778 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
782 hIC = lpwfs->lpAppInfo;
783 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
785 WORKREQUEST workRequest;
786 struct WORKREQ_FTPFINDFIRSTFILEW *req;
788 workRequest.asyncproc = AsyncFtpFindFirstFileProc;
789 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
790 req = &workRequest.u.FtpFindFirstFileW;
791 req->lpszSearchFile = (lpszSearchFile == NULL) ? NULL : heap_strdupW(lpszSearchFile);
792 req->lpFindFileData = lpFindFileData;
793 req->dwFlags = dwFlags;
794 req->dwContext= dwContext;
796 INTERNET_AsyncCall(&workRequest);
801 r = FTP_FtpFindFirstFileW(lpwfs, lpszSearchFile, lpFindFileData,
806 WININET_Release( &lpwfs->hdr );
812 /***********************************************************************
813 * FTP_FtpFindFirstFileW (Internal)
815 * Search the specified directory
818 * HINTERNET on success
822 static HINTERNET FTP_FtpFindFirstFileW(ftp_session_t *lpwfs,
823 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
826 appinfo_t *hIC = NULL;
827 HINTERNET hFindNext = NULL;
831 /* Clear any error information */
832 INTERNET_SetLastError(0);
834 if (!FTP_InitListenSocket(lpwfs))
837 if (!FTP_SendType(lpwfs, INTERNET_FLAG_TRANSFER_ASCII))
840 if (!FTP_SendPortOrPasv(lpwfs))
843 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_LIST, NULL,
844 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
847 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
850 if (nResCode == 125 || nResCode == 150)
854 /* Get data socket to server */
855 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
857 hFindNext = FTP_ReceiveFileList(lpwfs, nDataSocket, lpszSearchFile, lpFindFileData, dwContext);
858 closesocket(nDataSocket);
859 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
860 if (nResCode != 226 && nResCode != 250)
861 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
865 FTP_SetResponseError(nResCode);
869 if (lpwfs->lstnSocket != -1)
871 closesocket(lpwfs->lstnSocket);
872 lpwfs->lstnSocket = -1;
875 hIC = lpwfs->lpAppInfo;
876 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
878 INTERNET_ASYNC_RESULT iar;
882 iar.dwResult = (DWORD_PTR)hFindNext;
883 iar.dwError = ERROR_SUCCESS;
884 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
885 &iar, sizeof(INTERNET_ASYNC_RESULT));
888 iar.dwResult = (DWORD_PTR)hFindNext;
889 iar.dwError = hFindNext ? ERROR_SUCCESS : INTERNET_GetLastError();
890 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
891 &iar, sizeof(INTERNET_ASYNC_RESULT));
898 /***********************************************************************
899 * FtpGetCurrentDirectoryA (WININET.@)
901 * Retrieves the current directory
908 BOOL WINAPI FtpGetCurrentDirectoryA(HINTERNET hFtpSession, LPSTR lpszCurrentDirectory,
909 LPDWORD lpdwCurrentDirectory)
915 if(lpdwCurrentDirectory) {
916 len = *lpdwCurrentDirectory;
917 if(lpszCurrentDirectory)
919 dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
922 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
927 ret = FtpGetCurrentDirectoryW(hFtpSession, lpszCurrentDirectory?dir:NULL, lpdwCurrentDirectory?&len:NULL);
929 if (ret && lpszCurrentDirectory)
930 WideCharToMultiByte(CP_ACP, 0, dir, -1, lpszCurrentDirectory, len, NULL, NULL);
932 if (lpdwCurrentDirectory) *lpdwCurrentDirectory = len;
933 HeapFree(GetProcessHeap(), 0, dir);
938 static void AsyncFtpGetCurrentDirectoryProc(WORKREQUEST *workRequest)
940 struct WORKREQ_FTPGETCURRENTDIRECTORYW const *req = &workRequest->u.FtpGetCurrentDirectoryW;
941 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
943 TRACE("%p\n", lpwfs);
945 FTP_FtpGetCurrentDirectoryW(lpwfs, req->lpszDirectory, req->lpdwDirectory);
948 /***********************************************************************
949 * FtpGetCurrentDirectoryW (WININET.@)
951 * Retrieves the current directory
958 BOOL WINAPI FtpGetCurrentDirectoryW(HINTERNET hFtpSession, LPWSTR lpszCurrentDirectory,
959 LPDWORD lpdwCurrentDirectory)
961 ftp_session_t *lpwfs;
962 appinfo_t *hIC = NULL;
965 TRACE("%p %p %p\n", hFtpSession, lpszCurrentDirectory, lpdwCurrentDirectory);
967 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
970 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
974 if (WH_HFTPSESSION != lpwfs->hdr.htype)
976 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
980 if (!lpdwCurrentDirectory)
982 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
986 if (lpszCurrentDirectory == NULL)
988 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
992 if (lpwfs->download_in_progress != NULL)
994 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
998 hIC = lpwfs->lpAppInfo;
999 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1001 WORKREQUEST workRequest;
1002 struct WORKREQ_FTPGETCURRENTDIRECTORYW *req;
1004 workRequest.asyncproc = AsyncFtpGetCurrentDirectoryProc;
1005 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1006 req = &workRequest.u.FtpGetCurrentDirectoryW;
1007 req->lpszDirectory = lpszCurrentDirectory;
1008 req->lpdwDirectory = lpdwCurrentDirectory;
1010 r = res_to_le(INTERNET_AsyncCall(&workRequest));
1014 r = FTP_FtpGetCurrentDirectoryW(lpwfs, lpszCurrentDirectory,
1015 lpdwCurrentDirectory);
1020 WININET_Release( &lpwfs->hdr );
1026 /***********************************************************************
1027 * FTP_FtpGetCurrentDirectoryW (Internal)
1029 * Retrieves the current directory
1036 static BOOL FTP_FtpGetCurrentDirectoryW(ftp_session_t *lpwfs, LPWSTR lpszCurrentDirectory,
1037 LPDWORD lpdwCurrentDirectory)
1040 appinfo_t *hIC = NULL;
1041 DWORD bSuccess = FALSE;
1043 /* Clear any error information */
1044 INTERNET_SetLastError(0);
1046 hIC = lpwfs->lpAppInfo;
1047 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PWD, NULL,
1048 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
1051 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1054 if (nResCode == 257) /* Extract directory name */
1056 DWORD firstpos, lastpos, len;
1057 LPWSTR lpszResponseBuffer = heap_strdupAtoW(INTERNET_GetResponseBuffer());
1059 for (firstpos = 0, lastpos = 0; lpszResponseBuffer[lastpos]; lastpos++)
1061 if ('"' == lpszResponseBuffer[lastpos])
1069 len = lastpos - firstpos;
1070 if (*lpdwCurrentDirectory >= len)
1072 memcpy(lpszCurrentDirectory, &lpszResponseBuffer[firstpos + 1], len * sizeof(WCHAR));
1073 lpszCurrentDirectory[len - 1] = 0;
1074 *lpdwCurrentDirectory = len;
1077 else INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1079 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer);
1082 FTP_SetResponseError(nResCode);
1086 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1088 INTERNET_ASYNC_RESULT iar;
1090 iar.dwResult = bSuccess;
1091 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
1092 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1093 &iar, sizeof(INTERNET_ASYNC_RESULT));
1100 /***********************************************************************
1101 * FTPFILE_Destroy(internal)
1103 * Closes the file transfer handle. This also 'cleans' the data queue of
1104 * the 'transfer complete' message (this is a bit of a hack though :-/ )
1107 static void FTPFILE_Destroy(object_header_t *hdr)
1109 ftp_file_t *lpwh = (ftp_file_t*) hdr;
1110 ftp_session_t *lpwfs = lpwh->lpFtpSession;
1115 if (lpwh->cache_file_handle != INVALID_HANDLE_VALUE)
1116 CloseHandle(lpwh->cache_file_handle);
1118 HeapFree(GetProcessHeap(), 0, lpwh->cache_file);
1120 if (!lpwh->session_deleted)
1121 lpwfs->download_in_progress = NULL;
1123 if (lpwh->nDataSocket != -1)
1124 closesocket(lpwh->nDataSocket);
1126 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1127 if (nResCode > 0 && nResCode != 226) WARN("server reports failed transfer\n");
1129 WININET_Release(&lpwh->lpFtpSession->hdr);
1131 HeapFree(GetProcessHeap(), 0, lpwh);
1134 static DWORD FTPFILE_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1137 case INTERNET_OPTION_HANDLE_TYPE:
1138 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1140 if (*size < sizeof(ULONG))
1141 return ERROR_INSUFFICIENT_BUFFER;
1143 *size = sizeof(DWORD);
1144 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_FTP_FILE;
1145 return ERROR_SUCCESS;
1146 case INTERNET_OPTION_DATAFILE_NAME:
1149 ftp_file_t *file = (ftp_file_t *)hdr;
1151 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1153 if (!file->cache_file)
1156 return ERROR_INTERNET_ITEM_NOT_FOUND;
1160 required = (lstrlenW(file->cache_file) + 1) * sizeof(WCHAR);
1161 if (*size < required)
1162 return ERROR_INSUFFICIENT_BUFFER;
1165 memcpy(buffer, file->cache_file, *size);
1166 return ERROR_SUCCESS;
1170 required = WideCharToMultiByte(CP_ACP, 0, file->cache_file, -1, NULL, 0, NULL, NULL);
1171 if (required > *size)
1172 return ERROR_INSUFFICIENT_BUFFER;
1174 *size = WideCharToMultiByte(CP_ACP, 0, file->cache_file, -1, buffer, *size, NULL, NULL);
1175 return ERROR_SUCCESS;
1179 return INET_QueryOption(option, buffer, size, unicode);
1182 static DWORD FTPFILE_ReadFile(object_header_t *hdr, void *buffer, DWORD size, DWORD *read)
1184 ftp_file_t *file = (ftp_file_t*)hdr;
1188 if (file->nDataSocket == -1)
1189 return ERROR_INTERNET_DISCONNECTED;
1191 /* FIXME: FTP should use NETCON_ stuff */
1192 res = recv(file->nDataSocket, buffer, size, MSG_WAITALL);
1193 *read = res>0 ? res : 0;
1195 error = res >= 0 ? ERROR_SUCCESS : INTERNET_ERROR_BASE; /* FIXME */
1196 if (error == ERROR_SUCCESS && file->cache_file)
1198 DWORD bytes_written;
1200 if (!WriteFile(file->cache_file_handle, buffer, *read, &bytes_written, NULL))
1201 WARN("WriteFile failed: %u\n", GetLastError());
1206 static DWORD FTPFILE_ReadFileExA(object_header_t *hdr, INTERNET_BUFFERSA *buffers,
1207 DWORD flags, DWORD_PTR context)
1209 return FTPFILE_ReadFile(hdr, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength);
1212 static DWORD FTPFILE_ReadFileExW(object_header_t *hdr, INTERNET_BUFFERSW *buffers,
1213 DWORD flags, DWORD_PTR context)
1215 return FTPFILE_ReadFile(hdr, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength);
1218 static DWORD FTPFILE_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
1220 ftp_file_t *lpwh = (ftp_file_t*) hdr;
1223 res = send(lpwh->nDataSocket, buffer, size, 0);
1225 *written = res>0 ? res : 0;
1226 return res >= 0 ? ERROR_SUCCESS : sock_get_error(errno);
1229 static void FTP_ReceiveRequestData(ftp_file_t *file, BOOL first_notif)
1231 INTERNET_ASYNC_RESULT iar;
1235 TRACE("%p\n", file);
1237 available = recv(file->nDataSocket, buffer, sizeof(buffer), MSG_PEEK);
1239 if(available != -1) {
1240 iar.dwResult = (DWORD_PTR)file->hdr.hInternet;
1241 iar.dwError = first_notif ? 0 : available;
1244 iar.dwError = INTERNET_GetLastError();
1247 INTERNET_SendCallback(&file->hdr, file->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1248 sizeof(INTERNET_ASYNC_RESULT));
1251 static void FTPFILE_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1253 ftp_file_t *file = (ftp_file_t*)workRequest->hdr;
1255 FTP_ReceiveRequestData(file, FALSE);
1258 static DWORD FTPFILE_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1260 ftp_file_t *file = (ftp_file_t*) hdr;
1261 int retval, unread = 0;
1263 TRACE("(%p %p %x %lx)\n", file, available, flags, ctx);
1266 retval = ioctlsocket(file->nDataSocket, FIONREAD, &unread);
1268 TRACE("%d bytes of queued, but unread data\n", unread);
1270 FIXME("FIONREAD not available\n");
1273 *available = unread;
1280 retval = recv(file->nDataSocket, &byte, 1, MSG_PEEK);
1282 WORKREQUEST workRequest;
1285 workRequest.asyncproc = FTPFILE_AsyncQueryDataAvailableProc;
1286 workRequest.hdr = WININET_AddRef( &file->hdr );
1288 INTERNET_AsyncCall(&workRequest);
1290 return ERROR_IO_PENDING;
1294 return ERROR_SUCCESS;
1298 static const object_vtbl_t FTPFILEVtbl = {
1301 FTPFILE_QueryOption,
1304 FTPFILE_ReadFileExA,
1305 FTPFILE_ReadFileExW,
1307 FTPFILE_QueryDataAvailable,
1311 /***********************************************************************
1312 * FTP_FtpOpenFileW (Internal)
1314 * Open a remote file for writing or reading
1317 * HINTERNET handle on success
1321 static HINTERNET FTP_FtpOpenFileW(ftp_session_t *lpwfs,
1322 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1323 DWORD_PTR dwContext)
1326 BOOL bSuccess = FALSE;
1327 ftp_file_t *lpwh = NULL;
1328 appinfo_t *hIC = NULL;
1329 HINTERNET handle = NULL;
1333 /* Clear any error information */
1334 INTERNET_SetLastError(0);
1336 if (GENERIC_READ == fdwAccess)
1338 /* Set up socket to retrieve data */
1339 bSuccess = FTP_SendRetrieve(lpwfs, lpszFileName, dwFlags);
1341 else if (GENERIC_WRITE == fdwAccess)
1343 /* Set up socket to send data */
1344 bSuccess = FTP_SendStore(lpwfs, lpszFileName, dwFlags);
1347 /* Get data socket to server */
1348 if (bSuccess && FTP_GetDataSocket(lpwfs, &nDataSocket))
1350 lpwh = HeapAlloc(GetProcessHeap(), 0, sizeof(ftp_file_t));
1351 lpwh->hdr.htype = WH_HFILE;
1352 lpwh->hdr.vtbl = &FTPFILEVtbl;
1353 lpwh->hdr.dwFlags = dwFlags;
1354 lpwh->hdr.dwContext = dwContext;
1355 lpwh->hdr.dwInternalFlags = 0;
1357 lpwh->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
1358 lpwh->nDataSocket = nDataSocket;
1359 lpwh->cache_file = NULL;
1360 lpwh->cache_file_handle = INVALID_HANDLE_VALUE;
1361 lpwh->session_deleted = FALSE;
1363 WININET_AddRef( &lpwfs->hdr );
1364 lpwh->lpFtpSession = lpwfs;
1365 list_add_head( &lpwfs->hdr.children, &lpwh->hdr.entry );
1367 handle = WININET_AllocHandle( &lpwh->hdr );
1371 /* Indicate that a download is currently in progress */
1372 lpwfs->download_in_progress = lpwh;
1375 if (lpwfs->lstnSocket != -1)
1377 closesocket(lpwfs->lstnSocket);
1378 lpwfs->lstnSocket = -1;
1381 if (bSuccess && fdwAccess == GENERIC_READ)
1383 WCHAR filename[MAX_PATH + 1];
1387 memset(&uc, 0, sizeof(uc));
1388 uc.dwStructSize = sizeof(uc);
1389 uc.nScheme = INTERNET_SCHEME_FTP;
1390 uc.lpszHostName = lpwfs->servername;
1391 uc.nPort = lpwfs->serverport;
1392 uc.lpszUserName = lpwfs->lpszUserName;
1393 uc.lpszUrlPath = heap_strdupW(lpszFileName);
1395 if (!InternetCreateUrlW(&uc, 0, NULL, &len) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1397 WCHAR *url = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1399 if (url && InternetCreateUrlW(&uc, 0, url, &len) && CreateUrlCacheEntryW(url, 0, NULL, filename, 0))
1401 lpwh->cache_file = heap_strdupW(filename);
1402 lpwh->cache_file_handle = CreateFileW(filename, GENERIC_WRITE, FILE_SHARE_READ,
1403 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1404 if (lpwh->cache_file_handle == INVALID_HANDLE_VALUE)
1406 WARN("Could not create cache file: %u\n", GetLastError());
1407 HeapFree(GetProcessHeap(), 0, lpwh->cache_file);
1408 lpwh->cache_file = NULL;
1411 HeapFree(GetProcessHeap(), 0, url);
1413 HeapFree(GetProcessHeap(), 0, uc.lpszUrlPath);
1416 hIC = lpwfs->lpAppInfo;
1417 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1419 INTERNET_ASYNC_RESULT iar;
1423 iar.dwResult = (DWORD_PTR)handle;
1424 iar.dwError = ERROR_SUCCESS;
1425 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
1426 &iar, sizeof(INTERNET_ASYNC_RESULT));
1430 FTP_ReceiveRequestData(lpwh, TRUE);
1433 iar.dwError = INTERNET_GetLastError();
1434 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1435 &iar, sizeof(INTERNET_ASYNC_RESULT));
1441 WININET_Release( &lpwh->hdr );
1447 /***********************************************************************
1448 * FtpOpenFileA (WININET.@)
1450 * Open a remote file for writing or reading
1453 * HINTERNET handle on success
1457 HINTERNET WINAPI FtpOpenFileA(HINTERNET hFtpSession,
1458 LPCSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1459 DWORD_PTR dwContext)
1461 LPWSTR lpwzFileName;
1464 lpwzFileName = heap_strdupAtoW(lpszFileName);
1465 ret = FtpOpenFileW(hFtpSession, lpwzFileName, fdwAccess, dwFlags, dwContext);
1466 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1471 static void AsyncFtpOpenFileProc(WORKREQUEST *workRequest)
1473 struct WORKREQ_FTPOPENFILEW const *req = &workRequest->u.FtpOpenFileW;
1474 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
1476 TRACE("%p\n", lpwfs);
1478 FTP_FtpOpenFileW(lpwfs, req->lpszFilename,
1479 req->dwAccess, req->dwFlags, req->dwContext);
1480 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1483 /***********************************************************************
1484 * FtpOpenFileW (WININET.@)
1486 * Open a remote file for writing or reading
1489 * HINTERNET handle on success
1493 HINTERNET WINAPI FtpOpenFileW(HINTERNET hFtpSession,
1494 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1495 DWORD_PTR dwContext)
1497 ftp_session_t *lpwfs;
1498 appinfo_t *hIC = NULL;
1501 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession,
1502 debugstr_w(lpszFileName), fdwAccess, dwFlags, dwContext);
1504 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
1507 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1511 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1513 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1517 if ((!lpszFileName) ||
1518 ((fdwAccess != GENERIC_READ) && (fdwAccess != GENERIC_WRITE)) ||
1519 ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY))
1521 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1525 if (lpwfs->download_in_progress != NULL)
1527 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1531 hIC = lpwfs->lpAppInfo;
1532 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1534 WORKREQUEST workRequest;
1535 struct WORKREQ_FTPOPENFILEW *req;
1537 workRequest.asyncproc = AsyncFtpOpenFileProc;
1538 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1539 req = &workRequest.u.FtpOpenFileW;
1540 req->lpszFilename = heap_strdupW(lpszFileName);
1541 req->dwAccess = fdwAccess;
1542 req->dwFlags = dwFlags;
1543 req->dwContext = dwContext;
1545 INTERNET_AsyncCall(&workRequest);
1550 r = FTP_FtpOpenFileW(lpwfs, lpszFileName, fdwAccess, dwFlags, dwContext);
1554 WININET_Release( &lpwfs->hdr );
1560 /***********************************************************************
1561 * FtpGetFileA (WININET.@)
1563 * Retrieve file from the FTP server
1570 BOOL WINAPI FtpGetFileA(HINTERNET hInternet, LPCSTR lpszRemoteFile, LPCSTR lpszNewFile,
1571 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1572 DWORD_PTR dwContext)
1574 LPWSTR lpwzRemoteFile;
1578 lpwzRemoteFile = heap_strdupAtoW(lpszRemoteFile);
1579 lpwzNewFile = heap_strdupAtoW(lpszNewFile);
1580 ret = FtpGetFileW(hInternet, lpwzRemoteFile, lpwzNewFile, fFailIfExists,
1581 dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1582 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile);
1583 HeapFree(GetProcessHeap(), 0, lpwzNewFile);
1588 static void AsyncFtpGetFileProc(WORKREQUEST *workRequest)
1590 struct WORKREQ_FTPGETFILEW const *req = &workRequest->u.FtpGetFileW;
1591 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
1593 TRACE("%p\n", lpwfs);
1595 FTP_FtpGetFileW(lpwfs, req->lpszRemoteFile,
1596 req->lpszNewFile, req->fFailIfExists,
1597 req->dwLocalFlagsAttribute, req->dwFlags, req->dwContext);
1598 HeapFree(GetProcessHeap(), 0, req->lpszRemoteFile);
1599 HeapFree(GetProcessHeap(), 0, req->lpszNewFile);
1603 /***********************************************************************
1604 * FtpGetFileW (WININET.@)
1606 * Retrieve file from the FTP server
1613 BOOL WINAPI FtpGetFileW(HINTERNET hInternet, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1614 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1615 DWORD_PTR dwContext)
1617 ftp_session_t *lpwfs;
1618 appinfo_t *hIC = NULL;
1621 if (!lpszRemoteFile || !lpszNewFile)
1623 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1627 lpwfs = (ftp_session_t*) WININET_GetObject( hInternet );
1630 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1634 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1636 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1640 if ((dwInternetFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
1642 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1646 if (lpwfs->download_in_progress != NULL)
1648 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1652 hIC = lpwfs->lpAppInfo;
1653 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1655 WORKREQUEST workRequest;
1656 struct WORKREQ_FTPGETFILEW *req;
1658 workRequest.asyncproc = AsyncFtpGetFileProc;
1659 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1660 req = &workRequest.u.FtpGetFileW;
1661 req->lpszRemoteFile = heap_strdupW(lpszRemoteFile);
1662 req->lpszNewFile = heap_strdupW(lpszNewFile);
1663 req->dwLocalFlagsAttribute = dwLocalFlagsAttribute;
1664 req->fFailIfExists = fFailIfExists;
1665 req->dwFlags = dwInternetFlags;
1666 req->dwContext = dwContext;
1668 r = res_to_le(INTERNET_AsyncCall(&workRequest));
1672 r = FTP_FtpGetFileW(lpwfs, lpszRemoteFile, lpszNewFile,
1673 fFailIfExists, dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1677 WININET_Release( &lpwfs->hdr );
1683 /***********************************************************************
1684 * FTP_FtpGetFileW (Internal)
1686 * Retrieve file from the FTP server
1693 static BOOL FTP_FtpGetFileW(ftp_session_t *lpwfs, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1694 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1695 DWORD_PTR dwContext)
1697 BOOL bSuccess = FALSE;
1699 appinfo_t *hIC = NULL;
1701 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile), debugstr_w(lpszNewFile));
1703 /* Clear any error information */
1704 INTERNET_SetLastError(0);
1706 /* Ensure we can write to lpszNewfile by opening it */
1707 hFile = CreateFileW(lpszNewFile, GENERIC_WRITE, 0, 0, fFailIfExists ?
1708 CREATE_NEW : CREATE_ALWAYS, dwLocalFlagsAttribute, 0);
1709 if (INVALID_HANDLE_VALUE == hFile)
1712 /* Set up socket to retrieve data */
1713 if (FTP_SendRetrieve(lpwfs, lpszRemoteFile, dwInternetFlags))
1717 /* Get data socket to server */
1718 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
1723 FTP_RetrieveFileData(lpwfs, nDataSocket, hFile);
1724 closesocket(nDataSocket);
1726 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
1729 if (nResCode == 226)
1732 FTP_SetResponseError(nResCode);
1737 if (lpwfs->lstnSocket != -1)
1739 closesocket(lpwfs->lstnSocket);
1740 lpwfs->lstnSocket = -1;
1745 hIC = lpwfs->lpAppInfo;
1746 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1748 INTERNET_ASYNC_RESULT iar;
1750 iar.dwResult = (DWORD)bSuccess;
1751 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1752 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1753 &iar, sizeof(INTERNET_ASYNC_RESULT));
1756 if (!bSuccess) DeleteFileW(lpszNewFile);
1760 /***********************************************************************
1761 * FtpGetFileSize (WININET.@)
1763 DWORD WINAPI FtpGetFileSize( HINTERNET hFile, LPDWORD lpdwFileSizeHigh )
1765 FIXME("(%p, %p)\n", hFile, lpdwFileSizeHigh);
1767 if (lpdwFileSizeHigh)
1768 *lpdwFileSizeHigh = 0;
1773 /***********************************************************************
1774 * FtpDeleteFileA (WININET.@)
1776 * Delete a file on the ftp server
1783 BOOL WINAPI FtpDeleteFileA(HINTERNET hFtpSession, LPCSTR lpszFileName)
1785 LPWSTR lpwzFileName;
1788 lpwzFileName = heap_strdupAtoW(lpszFileName);
1789 ret = FtpDeleteFileW(hFtpSession, lpwzFileName);
1790 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1794 static void AsyncFtpDeleteFileProc(WORKREQUEST *workRequest)
1796 struct WORKREQ_FTPDELETEFILEW const *req = &workRequest->u.FtpDeleteFileW;
1797 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
1799 TRACE("%p\n", lpwfs);
1801 FTP_FtpDeleteFileW(lpwfs, req->lpszFilename);
1802 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1805 /***********************************************************************
1806 * FtpDeleteFileW (WININET.@)
1808 * Delete a file on the ftp server
1815 BOOL WINAPI FtpDeleteFileW(HINTERNET hFtpSession, LPCWSTR lpszFileName)
1817 ftp_session_t *lpwfs;
1818 appinfo_t *hIC = NULL;
1821 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
1824 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1828 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1830 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1834 if (lpwfs->download_in_progress != NULL)
1836 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1842 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1846 hIC = lpwfs->lpAppInfo;
1847 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1849 WORKREQUEST workRequest;
1850 struct WORKREQ_FTPDELETEFILEW *req;
1852 workRequest.asyncproc = AsyncFtpDeleteFileProc;
1853 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1854 req = &workRequest.u.FtpDeleteFileW;
1855 req->lpszFilename = heap_strdupW(lpszFileName);
1857 r = res_to_le(INTERNET_AsyncCall(&workRequest));
1861 r = FTP_FtpDeleteFileW(lpwfs, lpszFileName);
1865 WININET_Release( &lpwfs->hdr );
1870 /***********************************************************************
1871 * FTP_FtpDeleteFileW (Internal)
1873 * Delete a file on the ftp server
1880 BOOL FTP_FtpDeleteFileW(ftp_session_t *lpwfs, LPCWSTR lpszFileName)
1883 BOOL bSuccess = FALSE;
1884 appinfo_t *hIC = NULL;
1886 TRACE("%p\n", lpwfs);
1888 /* Clear any error information */
1889 INTERNET_SetLastError(0);
1891 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_DELE, lpszFileName, 0, 0, 0))
1894 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1897 if (nResCode == 250)
1900 FTP_SetResponseError(nResCode);
1903 hIC = lpwfs->lpAppInfo;
1904 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1906 INTERNET_ASYNC_RESULT iar;
1908 iar.dwResult = (DWORD)bSuccess;
1909 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1910 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1911 &iar, sizeof(INTERNET_ASYNC_RESULT));
1918 /***********************************************************************
1919 * FtpRemoveDirectoryA (WININET.@)
1921 * Remove a directory on the ftp server
1928 BOOL WINAPI FtpRemoveDirectoryA(HINTERNET hFtpSession, LPCSTR lpszDirectory)
1930 LPWSTR lpwzDirectory;
1933 lpwzDirectory = heap_strdupAtoW(lpszDirectory);
1934 ret = FtpRemoveDirectoryW(hFtpSession, lpwzDirectory);
1935 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
1939 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST *workRequest)
1941 struct WORKREQ_FTPREMOVEDIRECTORYW const *req = &workRequest->u.FtpRemoveDirectoryW;
1942 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
1944 TRACE("%p\n", lpwfs);
1946 FTP_FtpRemoveDirectoryW(lpwfs, req->lpszDirectory);
1947 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
1950 /***********************************************************************
1951 * FtpRemoveDirectoryW (WININET.@)
1953 * Remove a directory on the ftp server
1960 BOOL WINAPI FtpRemoveDirectoryW(HINTERNET hFtpSession, LPCWSTR lpszDirectory)
1962 ftp_session_t *lpwfs;
1963 appinfo_t *hIC = NULL;
1966 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
1969 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1973 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1975 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1979 if (lpwfs->download_in_progress != NULL)
1981 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1987 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1991 hIC = lpwfs->lpAppInfo;
1992 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1994 WORKREQUEST workRequest;
1995 struct WORKREQ_FTPREMOVEDIRECTORYW *req;
1997 workRequest.asyncproc = AsyncFtpRemoveDirectoryProc;
1998 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1999 req = &workRequest.u.FtpRemoveDirectoryW;
2000 req->lpszDirectory = heap_strdupW(lpszDirectory);
2002 r = res_to_le(INTERNET_AsyncCall(&workRequest));
2006 r = FTP_FtpRemoveDirectoryW(lpwfs, lpszDirectory);
2010 WININET_Release( &lpwfs->hdr );
2015 /***********************************************************************
2016 * FTP_FtpRemoveDirectoryW (Internal)
2018 * Remove a directory on the ftp server
2025 BOOL FTP_FtpRemoveDirectoryW(ftp_session_t *lpwfs, LPCWSTR lpszDirectory)
2028 BOOL bSuccess = FALSE;
2029 appinfo_t *hIC = NULL;
2033 /* Clear any error information */
2034 INTERNET_SetLastError(0);
2036 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RMD, lpszDirectory, 0, 0, 0))
2039 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2042 if (nResCode == 250)
2045 FTP_SetResponseError(nResCode);
2049 hIC = lpwfs->lpAppInfo;
2050 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2052 INTERNET_ASYNC_RESULT iar;
2054 iar.dwResult = (DWORD)bSuccess;
2055 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2056 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
2057 &iar, sizeof(INTERNET_ASYNC_RESULT));
2064 /***********************************************************************
2065 * FtpRenameFileA (WININET.@)
2067 * Rename a file on the ftp server
2074 BOOL WINAPI FtpRenameFileA(HINTERNET hFtpSession, LPCSTR lpszSrc, LPCSTR lpszDest)
2080 lpwzSrc = heap_strdupAtoW(lpszSrc);
2081 lpwzDest = heap_strdupAtoW(lpszDest);
2082 ret = FtpRenameFileW(hFtpSession, lpwzSrc, lpwzDest);
2083 HeapFree(GetProcessHeap(), 0, lpwzSrc);
2084 HeapFree(GetProcessHeap(), 0, lpwzDest);
2088 static void AsyncFtpRenameFileProc(WORKREQUEST *workRequest)
2090 struct WORKREQ_FTPRENAMEFILEW const *req = &workRequest->u.FtpRenameFileW;
2091 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
2093 TRACE("%p\n", lpwfs);
2095 FTP_FtpRenameFileW(lpwfs, req->lpszSrcFile, req->lpszDestFile);
2096 HeapFree(GetProcessHeap(), 0, req->lpszSrcFile);
2097 HeapFree(GetProcessHeap(), 0, req->lpszDestFile);
2100 /***********************************************************************
2101 * FtpRenameFileW (WININET.@)
2103 * Rename a file on the ftp server
2110 BOOL WINAPI FtpRenameFileW(HINTERNET hFtpSession, LPCWSTR lpszSrc, LPCWSTR lpszDest)
2112 ftp_session_t *lpwfs;
2113 appinfo_t *hIC = NULL;
2116 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
2119 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2123 if (WH_HFTPSESSION != lpwfs->hdr.htype)
2125 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2129 if (lpwfs->download_in_progress != NULL)
2131 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
2135 if (!lpszSrc || !lpszDest)
2137 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2141 hIC = lpwfs->lpAppInfo;
2142 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2144 WORKREQUEST workRequest;
2145 struct WORKREQ_FTPRENAMEFILEW *req;
2147 workRequest.asyncproc = AsyncFtpRenameFileProc;
2148 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
2149 req = &workRequest.u.FtpRenameFileW;
2150 req->lpszSrcFile = heap_strdupW(lpszSrc);
2151 req->lpszDestFile = heap_strdupW(lpszDest);
2153 r = res_to_le(INTERNET_AsyncCall(&workRequest));
2157 r = FTP_FtpRenameFileW(lpwfs, lpszSrc, lpszDest);
2161 WININET_Release( &lpwfs->hdr );
2166 /***********************************************************************
2167 * FTP_FtpRenameFileW (Internal)
2169 * Rename a file on the ftp server
2176 BOOL FTP_FtpRenameFileW(ftp_session_t *lpwfs, LPCWSTR lpszSrc, LPCWSTR lpszDest)
2179 BOOL bSuccess = FALSE;
2180 appinfo_t *hIC = NULL;
2184 /* Clear any error information */
2185 INTERNET_SetLastError(0);
2187 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNFR, lpszSrc, 0, 0, 0))
2190 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2191 if (nResCode == 350)
2193 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNTO, lpszDest, 0, 0, 0))
2196 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2199 if (nResCode == 250)
2202 FTP_SetResponseError(nResCode);
2205 hIC = lpwfs->lpAppInfo;
2206 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2208 INTERNET_ASYNC_RESULT iar;
2210 iar.dwResult = (DWORD)bSuccess;
2211 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2212 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
2213 &iar, sizeof(INTERNET_ASYNC_RESULT));
2219 /***********************************************************************
2220 * FtpCommandA (WININET.@)
2222 BOOL WINAPI FtpCommandA( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
2223 LPCSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
2228 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
2229 debugstr_a(lpszCommand), dwContext, phFtpCommand);
2231 if (fExpectResponse)
2233 FIXME("data connection not supported\n");
2237 if (!lpszCommand || !lpszCommand[0])
2239 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2243 if (!(cmdW = heap_strdupAtoW(lpszCommand)))
2245 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2249 r = FtpCommandW(hConnect, fExpectResponse, dwFlags, cmdW, dwContext, phFtpCommand);
2251 HeapFree(GetProcessHeap(), 0, cmdW);
2255 /***********************************************************************
2256 * FtpCommandW (WININET.@)
2258 BOOL WINAPI FtpCommandW( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
2259 LPCWSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
2262 ftp_session_t *lpwfs;
2264 DWORD len, nBytesSent= 0;
2265 INT nResCode, nRC = 0;
2267 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
2268 debugstr_w(lpszCommand), dwContext, phFtpCommand);
2270 if (!lpszCommand || !lpszCommand[0])
2272 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2276 if (fExpectResponse)
2278 FIXME("data connection not supported\n");
2282 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
2285 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2289 if (WH_HFTPSESSION != lpwfs->hdr.htype)
2291 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2295 if (lpwfs->download_in_progress != NULL)
2297 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
2301 len = WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, NULL, 0, NULL, NULL) + strlen(szCRLF);
2302 if ((cmd = HeapAlloc(GetProcessHeap(), 0, len )))
2303 WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, cmd, len, NULL, NULL);
2306 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2310 strcat(cmd, szCRLF);
2313 TRACE("Sending (%s) len(%d)\n", cmd, len);
2314 while ((nBytesSent < len) && (nRC != -1))
2316 nRC = send(lpwfs->sndSocket, cmd + nBytesSent, len - nBytesSent, 0);
2320 TRACE("Sent %d bytes\n", nRC);
2326 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2327 if (nResCode > 0 && nResCode < 400)
2330 FTP_SetResponseError(nResCode);
2334 WININET_Release( &lpwfs->hdr );
2335 HeapFree(GetProcessHeap(), 0, cmd);
2340 /***********************************************************************
2341 * FTPSESSION_Destroy (internal)
2343 * Deallocate session handle
2345 static void FTPSESSION_Destroy(object_header_t *hdr)
2347 ftp_session_t *lpwfs = (ftp_session_t*) hdr;
2351 WININET_Release(&lpwfs->lpAppInfo->hdr);
2353 HeapFree(GetProcessHeap(), 0, lpwfs->lpszPassword);
2354 HeapFree(GetProcessHeap(), 0, lpwfs->lpszUserName);
2355 HeapFree(GetProcessHeap(), 0, lpwfs->servername);
2356 HeapFree(GetProcessHeap(), 0, lpwfs);
2359 static void FTPSESSION_CloseConnection(object_header_t *hdr)
2361 ftp_session_t *lpwfs = (ftp_session_t*) hdr;
2365 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
2366 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2368 if (lpwfs->download_in_progress != NULL)
2369 lpwfs->download_in_progress->session_deleted = TRUE;
2371 if (lpwfs->sndSocket != -1)
2372 closesocket(lpwfs->sndSocket);
2374 if (lpwfs->lstnSocket != -1)
2375 closesocket(lpwfs->lstnSocket);
2377 if (lpwfs->pasvSocket != -1)
2378 closesocket(lpwfs->pasvSocket);
2380 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
2381 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2384 static DWORD FTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2387 case INTERNET_OPTION_HANDLE_TYPE:
2388 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2390 if (*size < sizeof(ULONG))
2391 return ERROR_INSUFFICIENT_BUFFER;
2393 *size = sizeof(DWORD);
2394 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_FTP;
2395 return ERROR_SUCCESS;
2398 return INET_QueryOption(option, buffer, size, unicode);
2401 static const object_vtbl_t FTPSESSIONVtbl = {
2403 FTPSESSION_CloseConnection,
2404 FTPSESSION_QueryOption,
2414 /***********************************************************************
2415 * FTP_Connect (internal)
2417 * Connect to a ftp server
2420 * HINTERNET a session handle on success
2425 * Windows uses 'anonymous' as the username, when given a NULL username
2426 * and a NULL password. The password is first looked up in:
2428 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2430 * If this entry is not present it uses the current username as the password.
2434 HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
2435 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2436 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
2437 DWORD dwInternalFlags)
2439 static const WCHAR szKey[] = {'S','o','f','t','w','a','r','e','\\',
2440 'M','i','c','r','o','s','o','f','t','\\',
2441 'W','i','n','d','o','w','s','\\',
2442 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2443 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2444 static const WCHAR szValue[] = {'E','m','a','i','l','N','a','m','e',0};
2445 static const WCHAR szDefaultUsername[] = {'a','n','o','n','y','m','o','u','s','\0'};
2446 static const WCHAR szEmpty[] = {'\0'};
2447 struct sockaddr_in socketAddr;
2450 BOOL bSuccess = FALSE;
2451 ftp_session_t *lpwfs = NULL;
2452 HINTERNET handle = NULL;
2453 char szaddr[INET_ADDRSTRLEN];
2455 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2456 hIC, debugstr_w(lpszServerName),
2457 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword));
2459 assert( hIC->hdr.htype == WH_HINIT );
2461 if ((!lpszUserName || !*lpszUserName) && lpszPassword && *lpszPassword)
2463 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2467 lpwfs = HeapAlloc(GetProcessHeap(), 0, sizeof(ftp_session_t));
2470 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2474 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
2475 lpwfs->serverport = INTERNET_DEFAULT_FTP_PORT;
2477 lpwfs->serverport = nServerPort;
2479 lpwfs->hdr.htype = WH_HFTPSESSION;
2480 lpwfs->hdr.vtbl = &FTPSESSIONVtbl;
2481 lpwfs->hdr.dwFlags = dwFlags;
2482 lpwfs->hdr.dwContext = dwContext;
2483 lpwfs->hdr.dwInternalFlags = dwInternalFlags;
2484 lpwfs->hdr.refs = 1;
2485 lpwfs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2486 lpwfs->download_in_progress = NULL;
2487 lpwfs->sndSocket = -1;
2488 lpwfs->lstnSocket = -1;
2489 lpwfs->pasvSocket = -1;
2491 WININET_AddRef( &hIC->hdr );
2492 lpwfs->lpAppInfo = hIC;
2493 list_add_head( &hIC->hdr.children, &lpwfs->hdr.entry );
2495 handle = WININET_AllocHandle( &lpwfs->hdr );
2498 ERR("Failed to alloc handle\n");
2499 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2503 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2504 if(strchrW(hIC->lpszProxy, ' '))
2505 FIXME("Several proxies not implemented.\n");
2506 if(hIC->lpszProxyBypass)
2507 FIXME("Proxy bypass is ignored.\n");
2509 if (!lpszUserName || !strlenW(lpszUserName)) {
2511 WCHAR szPassword[MAX_PATH];
2512 DWORD len = sizeof(szPassword);
2514 lpwfs->lpszUserName = heap_strdupW(szDefaultUsername);
2516 RegOpenKeyW(HKEY_CURRENT_USER, szKey, &key);
2517 if (RegQueryValueExW(key, szValue, NULL, NULL, (LPBYTE)szPassword, &len)) {
2518 /* Nothing in the registry, get the username and use that as the password */
2519 if (!GetUserNameW(szPassword, &len)) {
2520 /* Should never get here, but use an empty password as failsafe */
2521 strcpyW(szPassword, szEmpty);
2526 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword));
2527 lpwfs->lpszPassword = heap_strdupW(szPassword);
2530 lpwfs->lpszUserName = heap_strdupW(lpszUserName);
2531 lpwfs->lpszPassword = heap_strdupW(lpszPassword ? lpszPassword : szEmpty);
2533 lpwfs->servername = heap_strdupW(lpszServerName);
2535 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2536 if (!(lpwfs->hdr.dwInternalFlags & INET_OPENURL))
2538 INTERNET_ASYNC_RESULT iar;
2540 iar.dwResult = (DWORD_PTR)handle;
2541 iar.dwError = ERROR_SUCCESS;
2543 SendAsyncCallback(&hIC->hdr, dwContext,
2544 INTERNET_STATUS_HANDLE_CREATED, &iar,
2545 sizeof(INTERNET_ASYNC_RESULT));
2548 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_RESOLVING_NAME,
2549 (LPWSTR) lpszServerName, (strlenW(lpszServerName)+1) * sizeof(WCHAR));
2551 sock_namelen = sizeof(socketAddr);
2552 if (!GetAddress(lpszServerName, lpwfs->serverport, (struct sockaddr *)&socketAddr, &sock_namelen))
2554 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2558 if (socketAddr.sin_family != AF_INET)
2560 WARN("unsupported address family %d\n", socketAddr.sin_family);
2561 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2565 inet_ntop(socketAddr.sin_family, &socketAddr.sin_addr, szaddr, sizeof(szaddr));
2566 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_NAME_RESOLVED,
2567 szaddr, strlen(szaddr)+1);
2569 nsocket = socket(AF_INET,SOCK_STREAM,0);
2572 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2576 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTING_TO_SERVER,
2577 szaddr, strlen(szaddr)+1);
2579 if (connect(nsocket, (struct sockaddr *)&socketAddr, sock_namelen) < 0)
2581 ERR("Unable to connect (%s)\n", strerror(errno));
2582 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2583 closesocket(nsocket);
2587 TRACE("Connected to server\n");
2588 lpwfs->sndSocket = nsocket;
2589 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTED_TO_SERVER,
2590 szaddr, strlen(szaddr)+1);
2592 sock_namelen = sizeof(lpwfs->socketAddress);
2593 getsockname(nsocket, (struct sockaddr *) &lpwfs->socketAddress, &sock_namelen);
2595 if (FTP_ConnectToHost(lpwfs))
2597 TRACE("Successfully logged into server\n");
2603 if (lpwfs) WININET_Release( &lpwfs->hdr );
2605 if (!bSuccess && handle)
2607 WININET_FreeHandle( handle );
2615 /***********************************************************************
2616 * FTP_ConnectToHost (internal)
2618 * Connect to a ftp server
2625 static BOOL FTP_ConnectToHost(ftp_session_t *lpwfs)
2628 BOOL bSuccess = FALSE;
2631 FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2633 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_USER, lpwfs->lpszUserName, 0, 0, 0))
2636 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2639 /* Login successful... */
2640 if (nResCode == 230)
2642 /* User name okay, need password... */
2643 else if (nResCode == 331)
2644 bSuccess = FTP_SendPassword(lpwfs);
2645 /* Need account for login... */
2646 else if (nResCode == 332)
2647 bSuccess = FTP_SendAccount(lpwfs);
2649 FTP_SetResponseError(nResCode);
2652 TRACE("Returning %d\n", bSuccess);
2658 /***********************************************************************
2659 * FTP_SendCommandA (internal)
2661 * Send command to server
2668 static BOOL FTP_SendCommandA(INT nSocket, FTP_COMMAND ftpCmd, LPCSTR lpszParam,
2669 INTERNET_STATUS_CALLBACK lpfnStatusCB, object_header_t *hdr, DWORD_PTR dwContext)
2673 DWORD nBytesSent = 0;
2677 TRACE("%d: (%s) %d\n", ftpCmd, lpszParam, nSocket);
2681 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2684 dwParamLen = lpszParam?strlen(lpszParam)+1:0;
2685 len = dwParamLen + strlen(szFtpCommands[ftpCmd]) + strlen(szCRLF);
2686 if (NULL == (buf = HeapAlloc(GetProcessHeap(), 0, len+1)))
2688 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2691 sprintf(buf, "%s%s%s%s", szFtpCommands[ftpCmd], dwParamLen ? " " : "",
2692 dwParamLen ? lpszParam : "", szCRLF);
2694 TRACE("Sending (%s) len(%d)\n", buf, len);
2695 while((nBytesSent < len) && (nRC != -1))
2697 nRC = send(nSocket, buf+nBytesSent, len - nBytesSent, 0);
2701 HeapFree(GetProcessHeap(), 0, (LPVOID)buf);
2705 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_REQUEST_SENT,
2706 &nBytesSent, sizeof(DWORD));
2709 TRACE("Sent %d bytes\n", nBytesSent);
2713 /***********************************************************************
2714 * FTP_SendCommand (internal)
2716 * Send command to server
2723 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
2724 INTERNET_STATUS_CALLBACK lpfnStatusCB, object_header_t *hdr, DWORD_PTR dwContext)
2727 LPSTR lpszParamA = heap_strdupWtoA(lpszParam);
2728 ret = FTP_SendCommandA(nSocket, ftpCmd, lpszParamA, lpfnStatusCB, hdr, dwContext);
2729 HeapFree(GetProcessHeap(), 0, lpszParamA);
2733 /***********************************************************************
2734 * FTP_ReceiveResponse (internal)
2736 * Receive response from server
2739 * Reply code on success
2743 INT FTP_ReceiveResponse(ftp_session_t *lpwfs, DWORD_PTR dwContext)
2745 LPSTR lpszResponse = INTERNET_GetResponseBuffer();
2748 char firstprefix[5];
2749 BOOL multiline = FALSE;
2751 TRACE("socket(%d)\n", lpwfs->sndSocket);
2753 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2757 if (!INTERNET_GetNextLine(lpwfs->sndSocket, &nRecv))
2764 if(lpszResponse[3] != '-')
2767 { /* Start of multiline response. Loop until we get "nnn " */
2769 memcpy(firstprefix, lpszResponse, 3);
2770 firstprefix[3] = ' ';
2771 firstprefix[4] = '\0';
2776 if(!memcmp(firstprefix, lpszResponse, 4))
2784 rc = atoi(lpszResponse);
2786 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2787 &nRecv, sizeof(DWORD));
2791 TRACE("return %d\n", rc);
2796 /***********************************************************************
2797 * FTP_SendPassword (internal)
2799 * Send password to ftp server
2806 static BOOL FTP_SendPassword(ftp_session_t *lpwfs)
2809 BOOL bSuccess = FALSE;
2812 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASS, lpwfs->lpszPassword, 0, 0, 0))
2815 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2818 TRACE("Received reply code %d\n", nResCode);
2819 /* Login successful... */
2820 if (nResCode == 230)
2822 /* Command not implemented, superfluous at the server site... */
2823 /* Need account for login... */
2824 else if (nResCode == 332)
2825 bSuccess = FTP_SendAccount(lpwfs);
2827 FTP_SetResponseError(nResCode);
2831 TRACE("Returning %d\n", bSuccess);
2836 /***********************************************************************
2837 * FTP_SendAccount (internal)
2846 static BOOL FTP_SendAccount(ftp_session_t *lpwfs)
2849 BOOL bSuccess = FALSE;
2852 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_ACCT, szNoAccount, 0, 0, 0))
2855 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2859 FTP_SetResponseError(nResCode);
2866 /***********************************************************************
2867 * FTP_SendStore (internal)
2869 * Send request to upload file to ftp server
2876 static BOOL FTP_SendStore(ftp_session_t *lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
2879 BOOL bSuccess = FALSE;
2882 if (!FTP_InitListenSocket(lpwfs))
2885 if (!FTP_SendType(lpwfs, dwType))
2888 if (!FTP_SendPortOrPasv(lpwfs))
2891 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_STOR, lpszRemoteFile, 0, 0, 0))
2893 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2896 if (nResCode == 150 || nResCode == 125)
2899 FTP_SetResponseError(nResCode);
2903 if (!bSuccess && lpwfs->lstnSocket != -1)
2905 closesocket(lpwfs->lstnSocket);
2906 lpwfs->lstnSocket = -1;
2913 /***********************************************************************
2914 * FTP_InitListenSocket (internal)
2916 * Create a socket to listen for server response
2923 static BOOL FTP_InitListenSocket(ftp_session_t *lpwfs)
2925 BOOL bSuccess = FALSE;
2926 socklen_t namelen = sizeof(lpwfs->lstnSocketAddress);
2930 lpwfs->lstnSocket = socket(PF_INET, SOCK_STREAM, 0);
2931 if (lpwfs->lstnSocket == -1)
2933 TRACE("Unable to create listening socket\n");
2937 /* We obtain our ip addr from the name of the command channel socket */
2938 lpwfs->lstnSocketAddress = lpwfs->socketAddress;
2940 /* and get the system to assign us a port */
2941 lpwfs->lstnSocketAddress.sin_port = htons(0);
2943 if (bind(lpwfs->lstnSocket,(struct sockaddr *) &lpwfs->lstnSocketAddress, sizeof(lpwfs->lstnSocketAddress)) == -1)
2945 TRACE("Unable to bind socket\n");
2949 if (listen(lpwfs->lstnSocket, MAX_BACKLOG) == -1)
2951 TRACE("listen failed\n");
2955 if (getsockname(lpwfs->lstnSocket, (struct sockaddr *) &lpwfs->lstnSocketAddress, &namelen) != -1)
2959 if (!bSuccess && lpwfs->lstnSocket != -1)
2961 closesocket(lpwfs->lstnSocket);
2962 lpwfs->lstnSocket = -1;
2969 /***********************************************************************
2970 * FTP_SendType (internal)
2972 * Tell server type of data being transferred
2978 * W98SE doesn't cache the type that's currently set
2979 * (i.e. it sends it always),
2980 * so we probably don't want to do that either.
2982 static BOOL FTP_SendType(ftp_session_t *lpwfs, DWORD dwType)
2985 WCHAR type[] = { 'I','\0' };
2986 BOOL bSuccess = FALSE;
2989 if (dwType & INTERNET_FLAG_TRANSFER_ASCII)
2992 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_TYPE, type, 0, 0, 0))
2995 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext)/100;
3001 FTP_SetResponseError(nResCode);
3009 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
3010 /***********************************************************************
3011 * FTP_GetFileSize (internal)
3013 * Retrieves from the server the size of the given file
3020 static BOOL FTP_GetFileSize(ftp_session_t *lpwfs, LPCWSTR lpszRemoteFile, DWORD *dwSize)
3023 BOOL bSuccess = FALSE;
3027 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_SIZE, lpszRemoteFile, 0, 0, 0))
3030 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3033 if (nResCode == 213) {
3034 /* Now parses the output to get the actual file size */
3036 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
3038 for (i = 0; (lpszResponseBuffer[i] != ' ') && (lpszResponseBuffer[i] != '\0'); i++) ;
3039 if (lpszResponseBuffer[i] == '\0') return FALSE;
3040 *dwSize = atol(&(lpszResponseBuffer[i + 1]));
3044 FTP_SetResponseError(nResCode);
3054 /***********************************************************************
3055 * FTP_SendPort (internal)
3057 * Tell server which port to use
3064 static BOOL FTP_SendPort(ftp_session_t *lpwfs)
3066 static const WCHAR szIPFormat[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
3068 WCHAR szIPAddress[64];
3069 BOOL bSuccess = FALSE;
3072 sprintfW(szIPAddress, szIPFormat,
3073 lpwfs->lstnSocketAddress.sin_addr.s_addr&0x000000FF,
3074 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x0000FF00)>>8,
3075 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x00FF0000)>>16,
3076 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0xFF000000)>>24,
3077 lpwfs->lstnSocketAddress.sin_port & 0xFF,
3078 (lpwfs->lstnSocketAddress.sin_port & 0xFF00)>>8);
3080 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PORT, szIPAddress, 0, 0, 0))
3083 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3086 if (nResCode == 200)
3089 FTP_SetResponseError(nResCode);
3097 /***********************************************************************
3098 * FTP_DoPassive (internal)
3100 * Tell server that we want to do passive transfers
3101 * and connect data socket
3108 static BOOL FTP_DoPassive(ftp_session_t *lpwfs)
3111 BOOL bSuccess = FALSE;
3114 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASV, NULL, 0, 0, 0))
3117 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3120 if (nResCode == 227)
3122 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
3126 char *pAddr, *pPort;
3128 struct sockaddr_in dataSocketAddress;
3130 p = lpszResponseBuffer+4; /* skip status code */
3131 while (*p != '\0' && (*p < '0' || *p > '9')) p++;
3135 ERR("no address found in response, aborting\n");
3139 if (sscanf(p, "%d,%d,%d,%d,%d,%d", &f[0], &f[1], &f[2], &f[3],
3142 ERR("unknown response address format '%s', aborting\n", p);
3145 for (i=0; i < 6; i++)
3148 dataSocketAddress = lpwfs->socketAddress;
3149 pAddr = (char *)&(dataSocketAddress.sin_addr.s_addr);
3150 pPort = (char *)&(dataSocketAddress.sin_port);
3158 nsocket = socket(AF_INET,SOCK_STREAM,0);
3162 if (connect(nsocket, (struct sockaddr *)&dataSocketAddress, sizeof(dataSocketAddress)))
3164 ERR("can't connect passive FTP data port.\n");
3165 closesocket(nsocket);
3168 lpwfs->pasvSocket = nsocket;
3172 FTP_SetResponseError(nResCode);
3180 static BOOL FTP_SendPortOrPasv(ftp_session_t *lpwfs)
3182 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
3184 if (!FTP_DoPassive(lpwfs))
3189 if (!FTP_SendPort(lpwfs))
3196 /***********************************************************************
3197 * FTP_GetDataSocket (internal)
3199 * Either accepts an incoming data socket connection from the server
3200 * or just returns the already opened socket after a PASV command
3201 * in case of passive FTP.
3209 static BOOL FTP_GetDataSocket(ftp_session_t *lpwfs, LPINT nDataSocket)
3211 struct sockaddr_in saddr;
3212 socklen_t addrlen = sizeof(struct sockaddr);
3215 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
3217 *nDataSocket = lpwfs->pasvSocket;
3218 lpwfs->pasvSocket = -1;
3222 *nDataSocket = accept(lpwfs->lstnSocket, (struct sockaddr *) &saddr, &addrlen);
3223 closesocket(lpwfs->lstnSocket);
3224 lpwfs->lstnSocket = -1;
3226 return *nDataSocket != -1;
3230 /***********************************************************************
3231 * FTP_SendData (internal)
3233 * Send data to the server
3240 static BOOL FTP_SendData(ftp_session_t *lpwfs, INT nDataSocket, HANDLE hFile)
3242 BY_HANDLE_FILE_INFORMATION fi;
3243 DWORD nBytesRead = 0;
3244 DWORD nBytesSent = 0;
3245 DWORD nTotalSent = 0;
3246 DWORD nBytesToSend, nLen;
3248 time_t s_long_time, e_long_time;
3253 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
3255 /* Get the size of the file. */
3256 GetFileInformationByHandle(hFile, &fi);
3261 nBytesToSend = nBytesRead - nBytesSent;
3263 if (nBytesToSend <= 0)
3265 /* Read data from file. */
3267 if (!ReadFile(hFile, lpszBuffer, DATA_PACKET_SIZE, &nBytesRead, 0))
3268 ERR("Failed reading from file\n");
3271 nBytesToSend = nBytesRead;
3276 nLen = DATA_PACKET_SIZE < nBytesToSend ?
3277 DATA_PACKET_SIZE : nBytesToSend;
3278 nRC = send(nDataSocket, lpszBuffer, nLen, 0);
3286 /* Do some computation to display the status. */
3288 nSeconds = e_long_time - s_long_time;
3289 if( nSeconds / 60 > 0 )
3291 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
3292 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds / 60,
3293 nSeconds % 60, (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent );
3297 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
3298 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds,
3299 (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent);
3301 } while (nRC != -1);
3303 TRACE("file transfer complete!\n");
3305 HeapFree(GetProcessHeap(), 0, lpszBuffer);
3311 /***********************************************************************
3312 * FTP_SendRetrieve (internal)
3314 * Send request to retrieve a file
3317 * Number of bytes to be received on success
3321 static BOOL FTP_SendRetrieve(ftp_session_t *lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
3327 if (!(ret = FTP_InitListenSocket(lpwfs)))
3330 if (!(ret = FTP_SendType(lpwfs, dwType)))
3333 if (!(ret = FTP_SendPortOrPasv(lpwfs)))
3336 if (!(ret = FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RETR, lpszRemoteFile, 0, 0, 0)))
3339 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3340 if ((nResCode != 125) && (nResCode != 150)) {
3341 /* That means that we got an error getting the file. */
3342 FTP_SetResponseError(nResCode);
3347 if (!ret && lpwfs->lstnSocket != -1)
3349 closesocket(lpwfs->lstnSocket);
3350 lpwfs->lstnSocket = -1;
3357 /***********************************************************************
3358 * FTP_RetrieveData (internal)
3360 * Retrieve data from server
3367 static BOOL FTP_RetrieveFileData(ftp_session_t *lpwfs, INT nDataSocket, HANDLE hFile)
3369 DWORD nBytesWritten;
3375 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
3376 if (NULL == lpszBuffer)
3378 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3384 nRC = recv(nDataSocket, lpszBuffer, DATA_PACKET_SIZE, 0);
3387 /* other side closed socket. */
3390 WriteFile(hFile, lpszBuffer, nRC, &nBytesWritten, NULL);
3394 TRACE("Data transfer complete\n");
3397 HeapFree(GetProcessHeap(), 0, lpszBuffer);
3402 /***********************************************************************
3403 * FTPFINDNEXT_Destroy (internal)
3405 * Deallocate session handle
3407 static void FTPFINDNEXT_Destroy(object_header_t *hdr)
3409 LPWININETFTPFINDNEXTW lpwfn = (LPWININETFTPFINDNEXTW) hdr;
3414 WININET_Release(&lpwfn->lpFtpSession->hdr);
3416 for (i = 0; i < lpwfn->size; i++)
3418 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp[i].lpszName);
3421 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp);
3422 HeapFree(GetProcessHeap(), 0, lpwfn);
3425 static DWORD FTPFINDNEXT_FindNextFileProc(WININETFTPFINDNEXTW *find, LPVOID data)
3427 WIN32_FIND_DATAW *find_data = data;
3428 DWORD res = ERROR_SUCCESS;
3430 TRACE("index(%d) size(%d)\n", find->index, find->size);
3432 ZeroMemory(find_data, sizeof(WIN32_FIND_DATAW));
3434 if (find->index < find->size) {
3435 FTP_ConvertFileProp(&find->lpafp[find->index], find_data);
3438 TRACE("Name: %s\nSize: %d\n", debugstr_w(find_data->cFileName), find_data->nFileSizeLow);
3440 res = ERROR_NO_MORE_FILES;
3443 if (find->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3445 INTERNET_ASYNC_RESULT iar;
3447 iar.dwResult = (res == ERROR_SUCCESS);
3450 INTERNET_SendCallback(&find->hdr, find->hdr.dwContext,
3451 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3452 sizeof(INTERNET_ASYNC_RESULT));
3458 static void FTPFINDNEXT_AsyncFindNextFileProc(WORKREQUEST *workRequest)
3460 struct WORKREQ_FTPFINDNEXTW *req = &workRequest->u.FtpFindNextW;
3462 FTPFINDNEXT_FindNextFileProc((WININETFTPFINDNEXTW*)workRequest->hdr, req->lpFindFileData);
3465 static DWORD FTPFINDNEXT_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3468 case INTERNET_OPTION_HANDLE_TYPE:
3469 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3471 if (*size < sizeof(ULONG))
3472 return ERROR_INSUFFICIENT_BUFFER;
3474 *size = sizeof(DWORD);
3475 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_FTP_FIND;
3476 return ERROR_SUCCESS;
3479 return INET_QueryOption(option, buffer, size, unicode);
3482 static DWORD FTPFINDNEXT_FindNextFileW(object_header_t *hdr, void *data)
3484 WININETFTPFINDNEXTW *find = (WININETFTPFINDNEXTW*)hdr;
3486 if (find->lpFtpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3488 WORKREQUEST workRequest;
3489 struct WORKREQ_FTPFINDNEXTW *req;
3491 workRequest.asyncproc = FTPFINDNEXT_AsyncFindNextFileProc;
3492 workRequest.hdr = WININET_AddRef( &find->hdr );
3493 req = &workRequest.u.FtpFindNextW;
3494 req->lpFindFileData = data;
3496 INTERNET_AsyncCall(&workRequest);
3498 return ERROR_SUCCESS;
3501 return FTPFINDNEXT_FindNextFileProc(find, data);
3504 static const object_vtbl_t FTPFINDNEXTVtbl = {
3505 FTPFINDNEXT_Destroy,
3507 FTPFINDNEXT_QueryOption,
3514 FTPFINDNEXT_FindNextFileW
3517 /***********************************************************************
3518 * FTP_ReceiveFileList (internal)
3520 * Read file list from server
3523 * Handle to file list on success
3527 static HINTERNET FTP_ReceiveFileList(ftp_session_t *lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3528 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext)
3531 LPFILEPROPERTIESW lpafp = NULL;
3532 LPWININETFTPFINDNEXTW lpwfn = NULL;
3533 HINTERNET handle = 0;
3535 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs, nSocket, debugstr_w(lpszSearchFile), lpFindFileData, dwContext);
3537 if (FTP_ParseDirectory(lpwfs, nSocket, lpszSearchFile, &lpafp, &dwSize))
3540 FTP_ConvertFileProp(lpafp, lpFindFileData);
3542 lpwfn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETFTPFINDNEXTW));
3545 lpwfn->hdr.htype = WH_HFTPFINDNEXT;
3546 lpwfn->hdr.vtbl = &FTPFINDNEXTVtbl;
3547 lpwfn->hdr.dwContext = dwContext;
3548 lpwfn->hdr.refs = 1;
3549 lpwfn->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
3550 lpwfn->index = 1; /* Next index is 1 since we return index 0 */
3551 lpwfn->size = dwSize;
3552 lpwfn->lpafp = lpafp;
3554 WININET_AddRef( &lpwfs->hdr );
3555 lpwfn->lpFtpSession = lpwfs;
3556 list_add_head( &lpwfs->hdr.children, &lpwfn->hdr.entry );
3558 handle = WININET_AllocHandle( &lpwfn->hdr );
3563 WININET_Release( &lpwfn->hdr );
3565 TRACE("Matched %d files\n", dwSize);
3570 /***********************************************************************
3571 * FTP_ConvertFileProp (internal)
3573 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3580 static BOOL FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp, LPWIN32_FIND_DATAW lpFindFileData)
3582 BOOL bSuccess = FALSE;
3584 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAW));
3588 SystemTimeToFileTime( &lpafp->tmLastModified, &lpFindFileData->ftLastAccessTime );
3589 lpFindFileData->ftLastWriteTime = lpFindFileData->ftLastAccessTime;
3590 lpFindFileData->ftCreationTime = lpFindFileData->ftLastAccessTime;
3592 /* Not all fields are filled in */
3593 lpFindFileData->nFileSizeHigh = 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3594 lpFindFileData->nFileSizeLow = lpafp->nSize;
3596 if (lpafp->bIsDirectory)
3597 lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
3599 if (lpafp->lpszName)
3600 lstrcpynW(lpFindFileData->cFileName, lpafp->lpszName, MAX_PATH);
3608 /***********************************************************************
3609 * FTP_ParseNextFile (internal)
3611 * Parse the next line in file listing
3617 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW lpfp)
3619 static const char szSpace[] = " \t";
3627 lpfp->lpszName = NULL;
3629 if(!(pszLine = INTERNET_GetNextLine(nSocket, &nBufLen)))
3632 pszToken = strtok(pszLine, szSpace);
3634 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3637 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3639 if(!isdigit(pszToken[0]) && 10 == strlen(pszToken)) {
3640 if(!FTP_ParsePermission(pszToken, lpfp))
3641 lpfp->bIsDirectory = FALSE;
3642 for(i=0; i<=3; i++) {
3643 if(!(pszToken = strtok(NULL, szSpace)))
3646 if(!pszToken) continue;
3647 if(lpfp->bIsDirectory) {
3648 TRACE("Is directory\n");
3652 TRACE("Size: %s\n", pszToken);
3653 lpfp->nSize = atol(pszToken);
3656 lpfp->tmLastModified.wSecond = 0;
3657 lpfp->tmLastModified.wMinute = 0;
3658 lpfp->tmLastModified.wHour = 0;
3659 lpfp->tmLastModified.wDay = 0;
3660 lpfp->tmLastModified.wMonth = 0;
3661 lpfp->tmLastModified.wYear = 0;
3663 /* Determine month */
3664 pszToken = strtok(NULL, szSpace);
3665 if(!pszToken) continue;
3666 if(strlen(pszToken) >= 3) {
3668 if((pszTmp = StrStrIA(szMonths, pszToken)))
3669 lpfp->tmLastModified.wMonth = ((pszTmp - szMonths) / 3)+1;
3672 pszToken = strtok(NULL, szSpace);
3673 if(!pszToken) continue;
3674 lpfp->tmLastModified.wDay = atoi(pszToken);
3675 /* Determine time or year */
3676 pszToken = strtok(NULL, szSpace);
3677 if(!pszToken) continue;
3678 if((pszTmp = strchr(pszToken, ':'))) {
3679 SYSTEMTIME curr_time;
3682 lpfp->tmLastModified.wMinute = atoi(pszTmp);
3683 lpfp->tmLastModified.wHour = atoi(pszToken);
3684 GetLocalTime( &curr_time );
3685 lpfp->tmLastModified.wYear = curr_time.wYear;
3688 lpfp->tmLastModified.wYear = atoi(pszToken);
3689 lpfp->tmLastModified.wHour = 12;
3691 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3692 lpfp->tmLastModified.wHour, lpfp->tmLastModified.wMinute, lpfp->tmLastModified.wSecond,
3693 lpfp->tmLastModified.wYear, lpfp->tmLastModified.wMonth, lpfp->tmLastModified.wDay);
3695 pszToken = strtok(NULL, szSpace);
3696 if(!pszToken) continue;
3697 lpfp->lpszName = heap_strdupAtoW(pszToken);
3698 TRACE("File: %s\n", debugstr_w(lpfp->lpszName));
3700 /* NT way of parsing ... :
3702 07-13-03 08:55PM <DIR> sakpatch
3703 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3705 else if(isdigit(pszToken[0]) && 8 == strlen(pszToken)) {
3706 int mon, mday, year, hour, min;
3707 lpfp->permissions = 0xFFFF; /* No idea, put full permission :-) */
3709 sscanf(pszToken, "%d-%d-%d", &mon, &mday, &year);
3710 lpfp->tmLastModified.wDay = mday;
3711 lpfp->tmLastModified.wMonth = mon;
3712 lpfp->tmLastModified.wYear = year;
3714 /* Hacky and bad Y2K protection :-) */
3715 if (lpfp->tmLastModified.wYear < 70) lpfp->tmLastModified.wYear += 2000;
3717 pszToken = strtok(NULL, szSpace);
3718 if(!pszToken) continue;
3719 sscanf(pszToken, "%d:%d", &hour, &min);
3720 lpfp->tmLastModified.wHour = hour;
3721 lpfp->tmLastModified.wMinute = min;
3722 if((pszToken[5] == 'P') && (pszToken[6] == 'M')) {
3723 lpfp->tmLastModified.wHour += 12;
3725 lpfp->tmLastModified.wSecond = 0;
3727 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3728 lpfp->tmLastModified.wHour, lpfp->tmLastModified.wMinute, lpfp->tmLastModified.wSecond,
3729 lpfp->tmLastModified.wYear, lpfp->tmLastModified.wMonth, lpfp->tmLastModified.wDay);
3731 pszToken = strtok(NULL, szSpace);
3732 if(!pszToken) continue;
3733 if(!strcasecmp(pszToken, "<DIR>")) {
3734 lpfp->bIsDirectory = TRUE;
3736 TRACE("Is directory\n");
3739 lpfp->bIsDirectory = FALSE;
3740 lpfp->nSize = atol(pszToken);
3741 TRACE("Size: %d\n", lpfp->nSize);
3744 pszToken = strtok(NULL, szSpace);
3745 if(!pszToken) continue;
3746 lpfp->lpszName = heap_strdupAtoW(pszToken);
3747 TRACE("Name: %s\n", debugstr_w(lpfp->lpszName));
3749 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3750 else if(pszToken[0] == '+') {
3751 FIXME("EPLF Format not implemented\n");
3754 if(lpfp->lpszName) {
3755 if((lpszSearchFile == NULL) ||
3756 (PathMatchSpecW(lpfp->lpszName, lpszSearchFile))) {
3758 TRACE("Matched: %s\n", debugstr_w(lpfp->lpszName));
3761 HeapFree(GetProcessHeap(), 0, lpfp->lpszName);
3762 lpfp->lpszName = NULL;
3769 /***********************************************************************
3770 * FTP_ParseDirectory (internal)
3772 * Parse string of directory information
3778 static BOOL FTP_ParseDirectory(ftp_session_t *lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3779 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp)
3781 BOOL bSuccess = TRUE;
3782 INT sizeFilePropArray = 500;/*20; */
3783 INT indexFilePropArray = -1;
3787 /* Allocate initial file properties array */
3788 *lpafp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FILEPROPERTIESW)*(sizeFilePropArray));
3793 if (indexFilePropArray+1 >= sizeFilePropArray)
3795 LPFILEPROPERTIESW tmpafp;
3797 sizeFilePropArray *= 2;
3798 tmpafp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *lpafp,
3799 sizeof(FILEPROPERTIESW)*sizeFilePropArray);
3808 indexFilePropArray++;
3809 } while (FTP_ParseNextFile(nSocket, lpszSearchFile, &(*lpafp)[indexFilePropArray]));
3811 if (bSuccess && indexFilePropArray)
3813 if (indexFilePropArray < sizeFilePropArray - 1)
3815 LPFILEPROPERTIESW tmpafp;
3817 tmpafp = HeapReAlloc(GetProcessHeap(), 0, *lpafp,
3818 sizeof(FILEPROPERTIESW)*indexFilePropArray);
3822 *dwfp = indexFilePropArray;
3826 HeapFree(GetProcessHeap(), 0, *lpafp);
3827 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
3835 /***********************************************************************
3836 * FTP_ParsePermission (internal)
3838 * Parse permission string of directory information
3845 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp)
3847 BOOL bSuccess = TRUE;
3848 unsigned short nPermission = 0;
3853 if ((*lpszPermission != 'd') && (*lpszPermission != '-') && (*lpszPermission != 'l'))
3859 lpfp->bIsDirectory = (*lpszPermission == 'd');
3865 nPermission |= (*(lpszPermission+1) == 'r' ? 1 : 0) << 8;
3868 nPermission |= (*(lpszPermission+2) == 'w' ? 1 : 0) << 7;
3871 nPermission |= (*(lpszPermission+3) == 'x' ? 1 : 0) << 6;
3874 nPermission |= (*(lpszPermission+4) == 'r' ? 1 : 0) << 5;
3877 nPermission |= (*(lpszPermission+5) == 'w' ? 1 : 0) << 4;
3880 nPermission |= (*(lpszPermission+6) == 'x' ? 1 : 0) << 3;
3883 nPermission |= (*(lpszPermission+7) == 'r' ? 1 : 0) << 2;
3886 nPermission |= (*(lpszPermission+8) == 'w' ? 1 : 0) << 1;
3889 nPermission |= (*(lpszPermission+9) == 'x' ? 1 : 0);
3893 }while (nPos <= nLast);
3895 lpfp->permissions = nPermission;
3900 /***********************************************************************
3901 * FTP_SetResponseError (internal)
3903 * Set the appropriate error code for a given response from the server
3908 static DWORD FTP_SetResponseError(DWORD dwResponse)
3914 case 425: /* Cannot open data connection. */
3915 dwCode = ERROR_INTERNET_CANNOT_CONNECT;
3918 case 426: /* Connection closed, transer aborted. */
3919 dwCode = ERROR_INTERNET_CONNECTION_ABORTED;
3922 case 530: /* Not logged in. Login incorrect. */
3923 dwCode = ERROR_INTERNET_LOGIN_FAILURE;
3926 case 421: /* Service not available - Server may be shutting down. */
3927 case 450: /* File action not taken. File may be busy. */
3928 case 451: /* Action aborted. Server error. */
3929 case 452: /* Action not taken. Insufficient storage space on server. */
3930 case 500: /* Syntax error. Command unrecognized. */
3931 case 501: /* Syntax error. Error in parameters or arguments. */
3932 case 502: /* Command not implemented. */
3933 case 503: /* Bad sequence of commands. */
3934 case 504: /* Command not implemented for that parameter. */
3935 case 532: /* Need account for storing files */
3936 case 550: /* File action not taken. File not found or no access. */
3937 case 551: /* Requested action aborted. Page type unknown */
3938 case 552: /* Action aborted. Exceeded storage allocation */
3939 case 553: /* Action not taken. File name not allowed. */
3942 dwCode = ERROR_INTERNET_EXTENDED_ERROR;
3946 INTERNET_SetLastError(dwCode);