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>
49 #ifdef HAVE_SYS_IOCTL_H
50 # include <sys/ioctl.h>
66 #include "wine/debug.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
71 typedef struct _ftp_session_t ftp_session_t;
76 ftp_session_t *lpFtpSession;
87 int pasvSocket; /* data socket connected by us in case of passive FTP */
88 ftp_file_t *download_in_progress;
89 struct sockaddr_in socketAddress;
90 struct sockaddr_in lstnSocketAddress;
100 SYSTEMTIME tmLastModified;
101 unsigned short permissions;
102 } FILEPROPERTIESW, *LPFILEPROPERTIESW;
107 ftp_session_t *lpFtpSession;
110 LPFILEPROPERTIESW lpafp;
111 } WININETFTPFINDNEXTW, *LPWININETFTPFINDNEXTW;
113 #define DATA_PACKET_SIZE 0x2000
114 #define szCRLF "\r\n"
115 #define MAX_BACKLOG 5
117 /* Testing shows that Windows only accepts dwFlags where the last
118 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
120 #define FTP_CONDITION_MASK 0x0007
123 /* FTP commands with arguments. */
139 /* FTP commands without arguments. */
148 static const CHAR *const szFtpCommands[] = {
171 static const CHAR szMonths[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
172 static const WCHAR szNoAccount[] = {'n','o','a','c','c','o','u','n','t','\0'};
174 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
175 INTERNET_STATUS_CALLBACK lpfnStatusCB, object_header_t *hdr, DWORD_PTR dwContext);
176 static BOOL FTP_SendStore(ftp_session_t*, LPCWSTR lpszRemoteFile, DWORD dwType);
177 static BOOL FTP_GetDataSocket(ftp_session_t*, LPINT nDataSocket);
178 static BOOL FTP_SendData(ftp_session_t*, INT nDataSocket, HANDLE hFile);
179 static INT FTP_ReceiveResponse(ftp_session_t*, DWORD_PTR dwContext);
180 static BOOL FTP_SendRetrieve(ftp_session_t*, LPCWSTR lpszRemoteFile, DWORD dwType);
181 static BOOL FTP_RetrieveFileData(ftp_session_t*, INT nDataSocket, HANDLE hFile);
182 static BOOL FTP_InitListenSocket(ftp_session_t*);
183 static BOOL FTP_ConnectToHost(ftp_session_t*);
184 static BOOL FTP_SendPassword(ftp_session_t*);
185 static BOOL FTP_SendAccount(ftp_session_t*);
186 static BOOL FTP_SendType(ftp_session_t*, DWORD dwType);
187 static BOOL FTP_SendPort(ftp_session_t*);
188 static BOOL FTP_DoPassive(ftp_session_t*);
189 static BOOL FTP_SendPortOrPasv(ftp_session_t*);
190 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp);
191 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW fileprop);
192 static BOOL FTP_ParseDirectory(ftp_session_t*, INT nSocket, LPCWSTR lpszSearchFile,
193 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp);
194 static HINTERNET FTP_ReceiveFileList(ftp_session_t*, INT nSocket, LPCWSTR lpszSearchFile,
195 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext);
196 static DWORD FTP_SetResponseError(DWORD dwResponse);
197 static BOOL FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp, LPWIN32_FIND_DATAW lpFindFileData);
198 static BOOL FTP_FtpPutFileW(ftp_session_t*, LPCWSTR lpszLocalFile,
199 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext);
200 static BOOL FTP_FtpSetCurrentDirectoryW(ftp_session_t*, LPCWSTR lpszDirectory);
201 static BOOL FTP_FtpCreateDirectoryW(ftp_session_t*, LPCWSTR lpszDirectory);
202 static HINTERNET FTP_FtpFindFirstFileW(ftp_session_t*,
203 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext);
204 static BOOL FTP_FtpGetCurrentDirectoryW(ftp_session_t*, LPWSTR lpszCurrentDirectory,
205 LPDWORD lpdwCurrentDirectory);
206 static BOOL FTP_FtpRenameFileW(ftp_session_t*, LPCWSTR lpszSrc, LPCWSTR lpszDest);
207 static BOOL FTP_FtpRemoveDirectoryW(ftp_session_t*, LPCWSTR lpszDirectory);
208 static BOOL FTP_FtpDeleteFileW(ftp_session_t*, LPCWSTR lpszFileName);
209 static BOOL FTP_FtpGetFileW(ftp_session_t*, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
210 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
211 DWORD_PTR dwContext);
214 /***********************************************************************
215 * FtpPutFileA (WININET.@)
217 * Uploads a file to the FTP server
224 BOOL WINAPI FtpPutFileA(HINTERNET hConnect, LPCSTR lpszLocalFile,
225 LPCSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
227 LPWSTR lpwzLocalFile;
228 LPWSTR lpwzNewRemoteFile;
231 lpwzLocalFile = heap_strdupAtoW(lpszLocalFile);
232 lpwzNewRemoteFile = heap_strdupAtoW(lpszNewRemoteFile);
233 ret = FtpPutFileW(hConnect, lpwzLocalFile, lpwzNewRemoteFile,
235 HeapFree(GetProcessHeap(), 0, lpwzLocalFile);
236 HeapFree(GetProcessHeap(), 0, lpwzNewRemoteFile);
240 static void AsyncFtpPutFileProc(WORKREQUEST *workRequest)
242 struct WORKREQ_FTPPUTFILEW const *req = &workRequest->u.FtpPutFileW;
243 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
245 TRACE("%p\n", lpwfs);
247 FTP_FtpPutFileW(lpwfs, req->lpszLocalFile,
248 req->lpszNewRemoteFile, req->dwFlags, req->dwContext);
250 HeapFree(GetProcessHeap(), 0, req->lpszLocalFile);
251 HeapFree(GetProcessHeap(), 0, req->lpszNewRemoteFile);
254 /***********************************************************************
255 * FtpPutFileW (WININET.@)
257 * Uploads a file to the FTP server
264 BOOL WINAPI FtpPutFileW(HINTERNET hConnect, LPCWSTR lpszLocalFile,
265 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
267 ftp_session_t *lpwfs;
268 appinfo_t *hIC = NULL;
271 if (!lpszLocalFile || !lpszNewRemoteFile)
273 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
277 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
280 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
284 if (WH_HFTPSESSION != lpwfs->hdr.htype)
286 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
290 if (lpwfs->download_in_progress != NULL)
292 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
296 if ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
298 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
302 hIC = lpwfs->lpAppInfo;
303 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
305 WORKREQUEST workRequest;
306 struct WORKREQ_FTPPUTFILEW *req = &workRequest.u.FtpPutFileW;
308 workRequest.asyncproc = AsyncFtpPutFileProc;
309 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
310 req->lpszLocalFile = heap_strdupW(lpszLocalFile);
311 req->lpszNewRemoteFile = heap_strdupW(lpszNewRemoteFile);
312 req->dwFlags = dwFlags;
313 req->dwContext = dwContext;
315 r = INTERNET_AsyncCall(&workRequest);
319 r = FTP_FtpPutFileW(lpwfs, lpszLocalFile,
320 lpszNewRemoteFile, dwFlags, dwContext);
324 WININET_Release( &lpwfs->hdr );
329 /***********************************************************************
330 * FTP_FtpPutFileW (Internal)
332 * Uploads a file to the FTP server
339 static BOOL FTP_FtpPutFileW(ftp_session_t *lpwfs, LPCWSTR lpszLocalFile,
340 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
343 BOOL bSuccess = FALSE;
344 appinfo_t *hIC = NULL;
347 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile), debugstr_w(lpszNewRemoteFile));
349 /* Clear any error information */
350 INTERNET_SetLastError(0);
352 /* Open file to be uploaded */
353 if (INVALID_HANDLE_VALUE ==
354 (hFile = CreateFileW(lpszLocalFile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)))
355 /* Let CreateFile set the appropriate error */
358 hIC = lpwfs->lpAppInfo;
360 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
362 if (FTP_SendStore(lpwfs, lpszNewRemoteFile, dwFlags))
366 /* Get data socket to server */
367 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
369 FTP_SendData(lpwfs, nDataSocket, hFile);
370 closesocket(nDataSocket);
371 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
377 FTP_SetResponseError(nResCode);
382 if (lpwfs->lstnSocket != -1)
384 closesocket(lpwfs->lstnSocket);
385 lpwfs->lstnSocket = -1;
388 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
390 INTERNET_ASYNC_RESULT iar;
392 iar.dwResult = (DWORD)bSuccess;
393 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
394 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
395 &iar, sizeof(INTERNET_ASYNC_RESULT));
404 /***********************************************************************
405 * FtpSetCurrentDirectoryA (WININET.@)
407 * Change the working directory on the FTP server
414 BOOL WINAPI FtpSetCurrentDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
416 LPWSTR lpwzDirectory;
419 lpwzDirectory = heap_strdupAtoW(lpszDirectory);
420 ret = FtpSetCurrentDirectoryW(hConnect, lpwzDirectory);
421 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
426 static void AsyncFtpSetCurrentDirectoryProc(WORKREQUEST *workRequest)
428 struct WORKREQ_FTPSETCURRENTDIRECTORYW const *req = &workRequest->u.FtpSetCurrentDirectoryW;
429 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
431 TRACE("%p\n", lpwfs);
433 FTP_FtpSetCurrentDirectoryW(lpwfs, req->lpszDirectory);
434 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
437 /***********************************************************************
438 * FtpSetCurrentDirectoryW (WININET.@)
440 * Change the working directory on the FTP server
447 BOOL WINAPI FtpSetCurrentDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
449 ftp_session_t *lpwfs = NULL;
450 appinfo_t *hIC = NULL;
455 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
459 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
460 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
462 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
466 if (lpwfs->download_in_progress != NULL)
468 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
472 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
474 hIC = lpwfs->lpAppInfo;
475 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
477 WORKREQUEST workRequest;
478 struct WORKREQ_FTPSETCURRENTDIRECTORYW *req;
480 workRequest.asyncproc = AsyncFtpSetCurrentDirectoryProc;
481 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
482 req = &workRequest.u.FtpSetCurrentDirectoryW;
483 req->lpszDirectory = heap_strdupW(lpszDirectory);
485 r = INTERNET_AsyncCall(&workRequest);
489 r = FTP_FtpSetCurrentDirectoryW(lpwfs, lpszDirectory);
494 WININET_Release( &lpwfs->hdr );
500 /***********************************************************************
501 * FTP_FtpSetCurrentDirectoryW (Internal)
503 * Change the working directory on the FTP server
510 static BOOL FTP_FtpSetCurrentDirectoryW(ftp_session_t *lpwfs, LPCWSTR lpszDirectory)
513 appinfo_t *hIC = NULL;
514 DWORD bSuccess = FALSE;
516 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
518 /* Clear any error information */
519 INTERNET_SetLastError(0);
521 hIC = lpwfs->lpAppInfo;
522 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_CWD, lpszDirectory,
523 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
526 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
533 FTP_SetResponseError(nResCode);
537 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
539 INTERNET_ASYNC_RESULT iar;
541 iar.dwResult = bSuccess;
542 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
543 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
544 &iar, sizeof(INTERNET_ASYNC_RESULT));
550 /***********************************************************************
551 * FtpCreateDirectoryA (WININET.@)
553 * Create new directory on the FTP server
560 BOOL WINAPI FtpCreateDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
562 LPWSTR lpwzDirectory;
565 lpwzDirectory = heap_strdupAtoW(lpszDirectory);
566 ret = FtpCreateDirectoryW(hConnect, lpwzDirectory);
567 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
572 static void AsyncFtpCreateDirectoryProc(WORKREQUEST *workRequest)
574 struct WORKREQ_FTPCREATEDIRECTORYW const *req = &workRequest->u.FtpCreateDirectoryW;
575 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
577 TRACE(" %p\n", lpwfs);
579 FTP_FtpCreateDirectoryW(lpwfs, req->lpszDirectory);
580 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
583 /***********************************************************************
584 * FtpCreateDirectoryW (WININET.@)
586 * Create new directory on the FTP server
593 BOOL WINAPI FtpCreateDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
595 ftp_session_t *lpwfs;
596 appinfo_t *hIC = NULL;
599 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
602 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
606 if (WH_HFTPSESSION != lpwfs->hdr.htype)
608 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
612 if (lpwfs->download_in_progress != NULL)
614 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
620 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
624 hIC = lpwfs->lpAppInfo;
625 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
627 WORKREQUEST workRequest;
628 struct WORKREQ_FTPCREATEDIRECTORYW *req;
630 workRequest.asyncproc = AsyncFtpCreateDirectoryProc;
631 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
632 req = &workRequest.u.FtpCreateDirectoryW;
633 req->lpszDirectory = heap_strdupW(lpszDirectory);
635 r = INTERNET_AsyncCall(&workRequest);
639 r = FTP_FtpCreateDirectoryW(lpwfs, lpszDirectory);
642 WININET_Release( &lpwfs->hdr );
648 /***********************************************************************
649 * FTP_FtpCreateDirectoryW (Internal)
651 * Create new directory on the FTP server
658 static BOOL FTP_FtpCreateDirectoryW(ftp_session_t *lpwfs, LPCWSTR lpszDirectory)
661 BOOL bSuccess = FALSE;
662 appinfo_t *hIC = NULL;
664 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
666 /* Clear any error information */
667 INTERNET_SetLastError(0);
669 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_MKD, lpszDirectory, 0, 0, 0))
672 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
678 FTP_SetResponseError(nResCode);
682 hIC = lpwfs->lpAppInfo;
683 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
685 INTERNET_ASYNC_RESULT iar;
687 iar.dwResult = (DWORD)bSuccess;
688 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
689 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
690 &iar, sizeof(INTERNET_ASYNC_RESULT));
696 /***********************************************************************
697 * FtpFindFirstFileA (WININET.@)
699 * Search the specified directory
702 * HINTERNET on success
706 HINTERNET WINAPI FtpFindFirstFileA(HINTERNET hConnect,
707 LPCSTR lpszSearchFile, LPWIN32_FIND_DATAA lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
709 LPWSTR lpwzSearchFile;
710 WIN32_FIND_DATAW wfd;
711 LPWIN32_FIND_DATAW lpFindFileDataW;
714 lpwzSearchFile = heap_strdupAtoW(lpszSearchFile);
715 lpFindFileDataW = lpFindFileData?&wfd:NULL;
716 ret = FtpFindFirstFileW(hConnect, lpwzSearchFile, lpFindFileDataW, dwFlags, dwContext);
717 HeapFree(GetProcessHeap(), 0, lpwzSearchFile);
719 if (ret && lpFindFileData)
720 WININET_find_data_WtoA(lpFindFileDataW, lpFindFileData);
726 static void AsyncFtpFindFirstFileProc(WORKREQUEST *workRequest)
728 struct WORKREQ_FTPFINDFIRSTFILEW const *req = &workRequest->u.FtpFindFirstFileW;
729 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
731 TRACE("%p\n", lpwfs);
733 FTP_FtpFindFirstFileW(lpwfs, req->lpszSearchFile,
734 req->lpFindFileData, req->dwFlags, req->dwContext);
735 HeapFree(GetProcessHeap(), 0, req->lpszSearchFile);
738 /***********************************************************************
739 * FtpFindFirstFileW (WININET.@)
741 * Search the specified directory
744 * HINTERNET on success
748 HINTERNET WINAPI FtpFindFirstFileW(HINTERNET hConnect,
749 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
751 ftp_session_t *lpwfs;
752 appinfo_t *hIC = NULL;
755 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
756 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
758 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
762 if (lpwfs->download_in_progress != NULL)
764 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
768 hIC = lpwfs->lpAppInfo;
769 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
771 WORKREQUEST workRequest;
772 struct WORKREQ_FTPFINDFIRSTFILEW *req;
774 workRequest.asyncproc = AsyncFtpFindFirstFileProc;
775 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
776 req = &workRequest.u.FtpFindFirstFileW;
777 req->lpszSearchFile = (lpszSearchFile == NULL) ? NULL : heap_strdupW(lpszSearchFile);
778 req->lpFindFileData = lpFindFileData;
779 req->dwFlags = dwFlags;
780 req->dwContext= dwContext;
782 INTERNET_AsyncCall(&workRequest);
787 r = FTP_FtpFindFirstFileW(lpwfs, lpszSearchFile, lpFindFileData,
792 WININET_Release( &lpwfs->hdr );
798 /***********************************************************************
799 * FTP_FtpFindFirstFileW (Internal)
801 * Search the specified directory
804 * HINTERNET on success
808 static HINTERNET FTP_FtpFindFirstFileW(ftp_session_t *lpwfs,
809 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
812 appinfo_t *hIC = NULL;
813 HINTERNET hFindNext = NULL;
817 /* Clear any error information */
818 INTERNET_SetLastError(0);
820 if (!FTP_InitListenSocket(lpwfs))
823 if (!FTP_SendType(lpwfs, INTERNET_FLAG_TRANSFER_ASCII))
826 if (!FTP_SendPortOrPasv(lpwfs))
829 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_LIST, NULL,
830 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
833 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
836 if (nResCode == 125 || nResCode == 150)
840 /* Get data socket to server */
841 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
843 hFindNext = FTP_ReceiveFileList(lpwfs, nDataSocket, lpszSearchFile, lpFindFileData, dwContext);
844 closesocket(nDataSocket);
845 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
846 if (nResCode != 226 && nResCode != 250)
847 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
851 FTP_SetResponseError(nResCode);
855 if (lpwfs->lstnSocket != -1)
857 closesocket(lpwfs->lstnSocket);
858 lpwfs->lstnSocket = -1;
861 hIC = lpwfs->lpAppInfo;
862 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
864 INTERNET_ASYNC_RESULT iar;
868 iar.dwResult = (DWORD_PTR)hFindNext;
869 iar.dwError = ERROR_SUCCESS;
870 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
871 &iar, sizeof(INTERNET_ASYNC_RESULT));
874 iar.dwResult = (DWORD_PTR)hFindNext;
875 iar.dwError = hFindNext ? ERROR_SUCCESS : INTERNET_GetLastError();
876 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
877 &iar, sizeof(INTERNET_ASYNC_RESULT));
884 /***********************************************************************
885 * FtpGetCurrentDirectoryA (WININET.@)
887 * Retrieves the current directory
894 BOOL WINAPI FtpGetCurrentDirectoryA(HINTERNET hFtpSession, LPSTR lpszCurrentDirectory,
895 LPDWORD lpdwCurrentDirectory)
901 if(lpdwCurrentDirectory) {
902 len = *lpdwCurrentDirectory;
903 if(lpszCurrentDirectory)
905 dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
908 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
913 ret = FtpGetCurrentDirectoryW(hFtpSession, lpszCurrentDirectory?dir:NULL, lpdwCurrentDirectory?&len:NULL);
915 if (ret && lpszCurrentDirectory)
916 WideCharToMultiByte(CP_ACP, 0, dir, -1, lpszCurrentDirectory, len, NULL, NULL);
918 if (lpdwCurrentDirectory) *lpdwCurrentDirectory = len;
919 HeapFree(GetProcessHeap(), 0, dir);
924 static void AsyncFtpGetCurrentDirectoryProc(WORKREQUEST *workRequest)
926 struct WORKREQ_FTPGETCURRENTDIRECTORYW const *req = &workRequest->u.FtpGetCurrentDirectoryW;
927 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
929 TRACE("%p\n", lpwfs);
931 FTP_FtpGetCurrentDirectoryW(lpwfs, req->lpszDirectory, req->lpdwDirectory);
934 /***********************************************************************
935 * FtpGetCurrentDirectoryW (WININET.@)
937 * Retrieves the current directory
944 BOOL WINAPI FtpGetCurrentDirectoryW(HINTERNET hFtpSession, LPWSTR lpszCurrentDirectory,
945 LPDWORD lpdwCurrentDirectory)
947 ftp_session_t *lpwfs;
948 appinfo_t *hIC = NULL;
951 TRACE("%p %p %p\n", hFtpSession, lpszCurrentDirectory, lpdwCurrentDirectory);
953 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
956 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
960 if (WH_HFTPSESSION != lpwfs->hdr.htype)
962 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
966 if (!lpdwCurrentDirectory)
968 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
972 if (lpszCurrentDirectory == NULL)
974 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
978 if (lpwfs->download_in_progress != NULL)
980 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
984 hIC = lpwfs->lpAppInfo;
985 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
987 WORKREQUEST workRequest;
988 struct WORKREQ_FTPGETCURRENTDIRECTORYW *req;
990 workRequest.asyncproc = AsyncFtpGetCurrentDirectoryProc;
991 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
992 req = &workRequest.u.FtpGetCurrentDirectoryW;
993 req->lpszDirectory = lpszCurrentDirectory;
994 req->lpdwDirectory = lpdwCurrentDirectory;
996 r = INTERNET_AsyncCall(&workRequest);
1000 r = FTP_FtpGetCurrentDirectoryW(lpwfs, lpszCurrentDirectory,
1001 lpdwCurrentDirectory);
1006 WININET_Release( &lpwfs->hdr );
1012 /***********************************************************************
1013 * FTP_FtpGetCurrentDirectoryW (Internal)
1015 * Retrieves the current directory
1022 static BOOL FTP_FtpGetCurrentDirectoryW(ftp_session_t *lpwfs, LPWSTR lpszCurrentDirectory,
1023 LPDWORD lpdwCurrentDirectory)
1026 appinfo_t *hIC = NULL;
1027 DWORD bSuccess = FALSE;
1029 /* Clear any error information */
1030 INTERNET_SetLastError(0);
1032 hIC = lpwfs->lpAppInfo;
1033 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PWD, NULL,
1034 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
1037 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1040 if (nResCode == 257) /* Extract directory name */
1042 DWORD firstpos, lastpos, len;
1043 LPWSTR lpszResponseBuffer = heap_strdupAtoW(INTERNET_GetResponseBuffer());
1045 for (firstpos = 0, lastpos = 0; lpszResponseBuffer[lastpos]; lastpos++)
1047 if ('"' == lpszResponseBuffer[lastpos])
1055 len = lastpos - firstpos;
1056 if (*lpdwCurrentDirectory >= len)
1058 memcpy(lpszCurrentDirectory, &lpszResponseBuffer[firstpos + 1], len * sizeof(WCHAR));
1059 lpszCurrentDirectory[len - 1] = 0;
1060 *lpdwCurrentDirectory = len;
1063 else INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1065 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer);
1068 FTP_SetResponseError(nResCode);
1072 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1074 INTERNET_ASYNC_RESULT iar;
1076 iar.dwResult = bSuccess;
1077 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
1078 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1079 &iar, sizeof(INTERNET_ASYNC_RESULT));
1086 /***********************************************************************
1087 * FTPFILE_Destroy(internal)
1089 * Closes the file transfer handle. This also 'cleans' the data queue of
1090 * the 'transfer complete' message (this is a bit of a hack though :-/ )
1093 static void FTPFILE_Destroy(object_header_t *hdr)
1095 ftp_file_t *lpwh = (ftp_file_t*) hdr;
1096 ftp_session_t *lpwfs = lpwh->lpFtpSession;
1101 if (!lpwh->session_deleted)
1102 lpwfs->download_in_progress = NULL;
1104 if (lpwh->nDataSocket != -1)
1105 closesocket(lpwh->nDataSocket);
1107 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1108 if (nResCode > 0 && nResCode != 226) WARN("server reports failed transfer\n");
1110 WININET_Release(&lpwh->lpFtpSession->hdr);
1112 HeapFree(GetProcessHeap(), 0, lpwh);
1115 static DWORD FTPFILE_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1118 case INTERNET_OPTION_HANDLE_TYPE:
1119 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1121 if (*size < sizeof(ULONG))
1122 return ERROR_INSUFFICIENT_BUFFER;
1124 *size = sizeof(DWORD);
1125 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_FTP_FILE;
1126 return ERROR_SUCCESS;
1129 return INET_QueryOption(option, buffer, size, unicode);
1132 static DWORD FTPFILE_ReadFile(object_header_t *hdr, void *buffer, DWORD size, DWORD *read)
1134 ftp_file_t *file = (ftp_file_t*)hdr;
1137 if (file->nDataSocket == -1)
1138 return ERROR_INTERNET_DISCONNECTED;
1140 /* FIXME: FTP should use NETCON_ stuff */
1141 res = recv(file->nDataSocket, buffer, size, MSG_WAITALL);
1142 *read = res>0 ? res : 0;
1144 return res>=0 ? ERROR_SUCCESS : INTERNET_ERROR_BASE; /* FIXME*/
1147 static DWORD FTPFILE_ReadFileExA(object_header_t *hdr, INTERNET_BUFFERSA *buffers,
1148 DWORD flags, DWORD_PTR context)
1150 return FTPFILE_ReadFile(hdr, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength);
1153 static DWORD FTPFILE_ReadFileExW(object_header_t *hdr, INTERNET_BUFFERSW *buffers,
1154 DWORD flags, DWORD_PTR context)
1156 return FTPFILE_ReadFile(hdr, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength);
1159 static DWORD FTPFILE_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
1161 ftp_file_t *lpwh = (ftp_file_t*) hdr;
1164 res = send(lpwh->nDataSocket, buffer, size, 0);
1166 *written = res>0 ? res : 0;
1167 return res >= 0 ? ERROR_SUCCESS : sock_get_error(errno);
1170 static void FTP_ReceiveRequestData(ftp_file_t *file, BOOL first_notif)
1172 INTERNET_ASYNC_RESULT iar;
1176 TRACE("%p\n", file);
1178 available = recv(file->nDataSocket, buffer, sizeof(buffer), MSG_PEEK);
1180 if(available != -1) {
1181 iar.dwResult = (DWORD_PTR)file->hdr.hInternet;
1182 iar.dwError = first_notif ? 0 : available;
1185 iar.dwError = INTERNET_GetLastError();
1188 INTERNET_SendCallback(&file->hdr, file->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1189 sizeof(INTERNET_ASYNC_RESULT));
1192 static void FTPFILE_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1194 ftp_file_t *file = (ftp_file_t*)workRequest->hdr;
1196 FTP_ReceiveRequestData(file, FALSE);
1199 static DWORD FTPFILE_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1201 ftp_file_t *file = (ftp_file_t*) hdr;
1202 int retval, unread = 0;
1204 TRACE("(%p %p %x %lx)\n", file, available, flags, ctx);
1207 retval = ioctlsocket(file->nDataSocket, FIONREAD, &unread);
1209 TRACE("%d bytes of queued, but unread data\n", unread);
1211 FIXME("FIONREAD not available\n");
1214 *available = unread;
1221 retval = recv(file->nDataSocket, &byte, 1, MSG_PEEK);
1223 WORKREQUEST workRequest;
1226 workRequest.asyncproc = FTPFILE_AsyncQueryDataAvailableProc;
1227 workRequest.hdr = WININET_AddRef( &file->hdr );
1229 INTERNET_AsyncCall(&workRequest);
1231 return ERROR_IO_PENDING;
1235 return ERROR_SUCCESS;
1239 static const object_vtbl_t FTPFILEVtbl = {
1242 FTPFILE_QueryOption,
1245 FTPFILE_ReadFileExA,
1246 FTPFILE_ReadFileExW,
1248 FTPFILE_QueryDataAvailable,
1252 /***********************************************************************
1253 * FTP_FtpOpenFileW (Internal)
1255 * Open a remote file for writing or reading
1258 * HINTERNET handle on success
1262 HINTERNET FTP_FtpOpenFileW(ftp_session_t *lpwfs,
1263 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1264 DWORD_PTR dwContext)
1267 BOOL bSuccess = FALSE;
1268 ftp_file_t *lpwh = NULL;
1269 appinfo_t *hIC = NULL;
1270 HINTERNET handle = NULL;
1274 /* Clear any error information */
1275 INTERNET_SetLastError(0);
1277 if (GENERIC_READ == fdwAccess)
1279 /* Set up socket to retrieve data */
1280 bSuccess = FTP_SendRetrieve(lpwfs, lpszFileName, dwFlags);
1282 else if (GENERIC_WRITE == fdwAccess)
1284 /* Set up socket to send data */
1285 bSuccess = FTP_SendStore(lpwfs, lpszFileName, dwFlags);
1288 /* Get data socket to server */
1289 if (bSuccess && FTP_GetDataSocket(lpwfs, &nDataSocket))
1291 lpwh = HeapAlloc(GetProcessHeap(), 0, sizeof(ftp_file_t));
1292 lpwh->hdr.htype = WH_HFILE;
1293 lpwh->hdr.vtbl = &FTPFILEVtbl;
1294 lpwh->hdr.dwFlags = dwFlags;
1295 lpwh->hdr.dwContext = dwContext;
1296 lpwh->hdr.dwInternalFlags = 0;
1298 lpwh->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
1299 lpwh->nDataSocket = nDataSocket;
1300 lpwh->session_deleted = FALSE;
1302 WININET_AddRef( &lpwfs->hdr );
1303 lpwh->lpFtpSession = lpwfs;
1304 list_add_head( &lpwfs->hdr.children, &lpwh->hdr.entry );
1306 handle = WININET_AllocHandle( &lpwh->hdr );
1310 /* Indicate that a download is currently in progress */
1311 lpwfs->download_in_progress = lpwh;
1314 if (lpwfs->lstnSocket != -1)
1316 closesocket(lpwfs->lstnSocket);
1317 lpwfs->lstnSocket = -1;
1320 hIC = lpwfs->lpAppInfo;
1321 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1323 INTERNET_ASYNC_RESULT iar;
1327 iar.dwResult = (DWORD_PTR)handle;
1328 iar.dwError = ERROR_SUCCESS;
1329 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
1330 &iar, sizeof(INTERNET_ASYNC_RESULT));
1334 FTP_ReceiveRequestData(lpwh, TRUE);
1337 iar.dwError = INTERNET_GetLastError();
1338 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1339 &iar, sizeof(INTERNET_ASYNC_RESULT));
1345 WININET_Release( &lpwh->hdr );
1351 /***********************************************************************
1352 * FtpOpenFileA (WININET.@)
1354 * Open a remote file for writing or reading
1357 * HINTERNET handle on success
1361 HINTERNET WINAPI FtpOpenFileA(HINTERNET hFtpSession,
1362 LPCSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1363 DWORD_PTR dwContext)
1365 LPWSTR lpwzFileName;
1368 lpwzFileName = heap_strdupAtoW(lpszFileName);
1369 ret = FtpOpenFileW(hFtpSession, lpwzFileName, fdwAccess, dwFlags, dwContext);
1370 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1375 static void AsyncFtpOpenFileProc(WORKREQUEST *workRequest)
1377 struct WORKREQ_FTPOPENFILEW const *req = &workRequest->u.FtpOpenFileW;
1378 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
1380 TRACE("%p\n", lpwfs);
1382 FTP_FtpOpenFileW(lpwfs, req->lpszFilename,
1383 req->dwAccess, req->dwFlags, req->dwContext);
1384 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1387 /***********************************************************************
1388 * FtpOpenFileW (WININET.@)
1390 * Open a remote file for writing or reading
1393 * HINTERNET handle on success
1397 HINTERNET WINAPI FtpOpenFileW(HINTERNET hFtpSession,
1398 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1399 DWORD_PTR dwContext)
1401 ftp_session_t *lpwfs;
1402 appinfo_t *hIC = NULL;
1405 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession,
1406 debugstr_w(lpszFileName), fdwAccess, dwFlags, dwContext);
1408 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
1411 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1415 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1417 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1421 if ((!lpszFileName) ||
1422 ((fdwAccess != GENERIC_READ) && (fdwAccess != GENERIC_WRITE)) ||
1423 ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY))
1425 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1429 if (lpwfs->download_in_progress != NULL)
1431 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1435 hIC = lpwfs->lpAppInfo;
1436 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1438 WORKREQUEST workRequest;
1439 struct WORKREQ_FTPOPENFILEW *req;
1441 workRequest.asyncproc = AsyncFtpOpenFileProc;
1442 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1443 req = &workRequest.u.FtpOpenFileW;
1444 req->lpszFilename = heap_strdupW(lpszFileName);
1445 req->dwAccess = fdwAccess;
1446 req->dwFlags = dwFlags;
1447 req->dwContext = dwContext;
1449 INTERNET_AsyncCall(&workRequest);
1454 r = FTP_FtpOpenFileW(lpwfs, lpszFileName, fdwAccess, dwFlags, dwContext);
1458 WININET_Release( &lpwfs->hdr );
1464 /***********************************************************************
1465 * FtpGetFileA (WININET.@)
1467 * Retrieve file from the FTP server
1474 BOOL WINAPI FtpGetFileA(HINTERNET hInternet, LPCSTR lpszRemoteFile, LPCSTR lpszNewFile,
1475 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1476 DWORD_PTR dwContext)
1478 LPWSTR lpwzRemoteFile;
1482 lpwzRemoteFile = heap_strdupAtoW(lpszRemoteFile);
1483 lpwzNewFile = heap_strdupAtoW(lpszNewFile);
1484 ret = FtpGetFileW(hInternet, lpwzRemoteFile, lpwzNewFile, fFailIfExists,
1485 dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1486 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile);
1487 HeapFree(GetProcessHeap(), 0, lpwzNewFile);
1492 static void AsyncFtpGetFileProc(WORKREQUEST *workRequest)
1494 struct WORKREQ_FTPGETFILEW const *req = &workRequest->u.FtpGetFileW;
1495 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
1497 TRACE("%p\n", lpwfs);
1499 FTP_FtpGetFileW(lpwfs, req->lpszRemoteFile,
1500 req->lpszNewFile, req->fFailIfExists,
1501 req->dwLocalFlagsAttribute, req->dwFlags, req->dwContext);
1502 HeapFree(GetProcessHeap(), 0, req->lpszRemoteFile);
1503 HeapFree(GetProcessHeap(), 0, req->lpszNewFile);
1507 /***********************************************************************
1508 * FtpGetFileW (WININET.@)
1510 * Retrieve file from the FTP server
1517 BOOL WINAPI FtpGetFileW(HINTERNET hInternet, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1518 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1519 DWORD_PTR dwContext)
1521 ftp_session_t *lpwfs;
1522 appinfo_t *hIC = NULL;
1525 if (!lpszRemoteFile || !lpszNewFile)
1527 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1531 lpwfs = (ftp_session_t*) WININET_GetObject( hInternet );
1534 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1538 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1540 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1544 if ((dwInternetFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
1546 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1550 if (lpwfs->download_in_progress != NULL)
1552 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1556 hIC = lpwfs->lpAppInfo;
1557 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1559 WORKREQUEST workRequest;
1560 struct WORKREQ_FTPGETFILEW *req;
1562 workRequest.asyncproc = AsyncFtpGetFileProc;
1563 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1564 req = &workRequest.u.FtpGetFileW;
1565 req->lpszRemoteFile = heap_strdupW(lpszRemoteFile);
1566 req->lpszNewFile = heap_strdupW(lpszNewFile);
1567 req->dwLocalFlagsAttribute = dwLocalFlagsAttribute;
1568 req->fFailIfExists = fFailIfExists;
1569 req->dwFlags = dwInternetFlags;
1570 req->dwContext = dwContext;
1572 r = INTERNET_AsyncCall(&workRequest);
1576 r = FTP_FtpGetFileW(lpwfs, lpszRemoteFile, lpszNewFile,
1577 fFailIfExists, dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1581 WININET_Release( &lpwfs->hdr );
1587 /***********************************************************************
1588 * FTP_FtpGetFileW (Internal)
1590 * Retrieve file from the FTP server
1597 static BOOL FTP_FtpGetFileW(ftp_session_t *lpwfs, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1598 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1599 DWORD_PTR dwContext)
1601 BOOL bSuccess = FALSE;
1603 appinfo_t *hIC = NULL;
1605 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile), debugstr_w(lpszNewFile));
1607 /* Clear any error information */
1608 INTERNET_SetLastError(0);
1610 /* Ensure we can write to lpszNewfile by opening it */
1611 hFile = CreateFileW(lpszNewFile, GENERIC_WRITE, 0, 0, fFailIfExists ?
1612 CREATE_NEW : CREATE_ALWAYS, dwLocalFlagsAttribute, 0);
1613 if (INVALID_HANDLE_VALUE == hFile)
1616 /* Set up socket to retrieve data */
1617 if (FTP_SendRetrieve(lpwfs, lpszRemoteFile, dwInternetFlags))
1621 /* Get data socket to server */
1622 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
1627 FTP_RetrieveFileData(lpwfs, nDataSocket, hFile);
1628 closesocket(nDataSocket);
1630 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
1633 if (nResCode == 226)
1636 FTP_SetResponseError(nResCode);
1641 if (lpwfs->lstnSocket != -1)
1643 closesocket(lpwfs->lstnSocket);
1644 lpwfs->lstnSocket = -1;
1649 hIC = lpwfs->lpAppInfo;
1650 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1652 INTERNET_ASYNC_RESULT iar;
1654 iar.dwResult = (DWORD)bSuccess;
1655 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1656 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1657 &iar, sizeof(INTERNET_ASYNC_RESULT));
1660 if (!bSuccess) DeleteFileW(lpszNewFile);
1664 /***********************************************************************
1665 * FtpGetFileSize (WININET.@)
1667 DWORD WINAPI FtpGetFileSize( HINTERNET hFile, LPDWORD lpdwFileSizeHigh )
1669 FIXME("(%p, %p)\n", hFile, lpdwFileSizeHigh);
1671 if (lpdwFileSizeHigh)
1672 *lpdwFileSizeHigh = 0;
1677 /***********************************************************************
1678 * FtpDeleteFileA (WININET.@)
1680 * Delete a file on the ftp server
1687 BOOL WINAPI FtpDeleteFileA(HINTERNET hFtpSession, LPCSTR lpszFileName)
1689 LPWSTR lpwzFileName;
1692 lpwzFileName = heap_strdupAtoW(lpszFileName);
1693 ret = FtpDeleteFileW(hFtpSession, lpwzFileName);
1694 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1698 static void AsyncFtpDeleteFileProc(WORKREQUEST *workRequest)
1700 struct WORKREQ_FTPDELETEFILEW const *req = &workRequest->u.FtpDeleteFileW;
1701 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
1703 TRACE("%p\n", lpwfs);
1705 FTP_FtpDeleteFileW(lpwfs, req->lpszFilename);
1706 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1709 /***********************************************************************
1710 * FtpDeleteFileW (WININET.@)
1712 * Delete a file on the ftp server
1719 BOOL WINAPI FtpDeleteFileW(HINTERNET hFtpSession, LPCWSTR lpszFileName)
1721 ftp_session_t *lpwfs;
1722 appinfo_t *hIC = NULL;
1725 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
1728 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1732 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1734 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1738 if (lpwfs->download_in_progress != NULL)
1740 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1746 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1750 hIC = lpwfs->lpAppInfo;
1751 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1753 WORKREQUEST workRequest;
1754 struct WORKREQ_FTPDELETEFILEW *req;
1756 workRequest.asyncproc = AsyncFtpDeleteFileProc;
1757 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1758 req = &workRequest.u.FtpDeleteFileW;
1759 req->lpszFilename = heap_strdupW(lpszFileName);
1761 r = INTERNET_AsyncCall(&workRequest);
1765 r = FTP_FtpDeleteFileW(lpwfs, lpszFileName);
1769 WININET_Release( &lpwfs->hdr );
1774 /***********************************************************************
1775 * FTP_FtpDeleteFileW (Internal)
1777 * Delete a file on the ftp server
1784 BOOL FTP_FtpDeleteFileW(ftp_session_t *lpwfs, LPCWSTR lpszFileName)
1787 BOOL bSuccess = FALSE;
1788 appinfo_t *hIC = NULL;
1790 TRACE("%p\n", lpwfs);
1792 /* Clear any error information */
1793 INTERNET_SetLastError(0);
1795 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_DELE, lpszFileName, 0, 0, 0))
1798 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1801 if (nResCode == 250)
1804 FTP_SetResponseError(nResCode);
1807 hIC = lpwfs->lpAppInfo;
1808 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1810 INTERNET_ASYNC_RESULT iar;
1812 iar.dwResult = (DWORD)bSuccess;
1813 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1814 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1815 &iar, sizeof(INTERNET_ASYNC_RESULT));
1822 /***********************************************************************
1823 * FtpRemoveDirectoryA (WININET.@)
1825 * Remove a directory on the ftp server
1832 BOOL WINAPI FtpRemoveDirectoryA(HINTERNET hFtpSession, LPCSTR lpszDirectory)
1834 LPWSTR lpwzDirectory;
1837 lpwzDirectory = heap_strdupAtoW(lpszDirectory);
1838 ret = FtpRemoveDirectoryW(hFtpSession, lpwzDirectory);
1839 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
1843 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST *workRequest)
1845 struct WORKREQ_FTPREMOVEDIRECTORYW const *req = &workRequest->u.FtpRemoveDirectoryW;
1846 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
1848 TRACE("%p\n", lpwfs);
1850 FTP_FtpRemoveDirectoryW(lpwfs, req->lpszDirectory);
1851 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
1854 /***********************************************************************
1855 * FtpRemoveDirectoryW (WININET.@)
1857 * Remove a directory on the ftp server
1864 BOOL WINAPI FtpRemoveDirectoryW(HINTERNET hFtpSession, LPCWSTR lpszDirectory)
1866 ftp_session_t *lpwfs;
1867 appinfo_t *hIC = NULL;
1870 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
1873 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1877 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1879 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1883 if (lpwfs->download_in_progress != NULL)
1885 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1891 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1895 hIC = lpwfs->lpAppInfo;
1896 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1898 WORKREQUEST workRequest;
1899 struct WORKREQ_FTPREMOVEDIRECTORYW *req;
1901 workRequest.asyncproc = AsyncFtpRemoveDirectoryProc;
1902 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1903 req = &workRequest.u.FtpRemoveDirectoryW;
1904 req->lpszDirectory = heap_strdupW(lpszDirectory);
1906 r = INTERNET_AsyncCall(&workRequest);
1910 r = FTP_FtpRemoveDirectoryW(lpwfs, lpszDirectory);
1914 WININET_Release( &lpwfs->hdr );
1919 /***********************************************************************
1920 * FTP_FtpRemoveDirectoryW (Internal)
1922 * Remove a directory on the ftp server
1929 BOOL FTP_FtpRemoveDirectoryW(ftp_session_t *lpwfs, LPCWSTR lpszDirectory)
1932 BOOL bSuccess = FALSE;
1933 appinfo_t *hIC = NULL;
1937 /* Clear any error information */
1938 INTERNET_SetLastError(0);
1940 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RMD, lpszDirectory, 0, 0, 0))
1943 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1946 if (nResCode == 250)
1949 FTP_SetResponseError(nResCode);
1953 hIC = lpwfs->lpAppInfo;
1954 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1956 INTERNET_ASYNC_RESULT iar;
1958 iar.dwResult = (DWORD)bSuccess;
1959 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1960 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1961 &iar, sizeof(INTERNET_ASYNC_RESULT));
1968 /***********************************************************************
1969 * FtpRenameFileA (WININET.@)
1971 * Rename a file on the ftp server
1978 BOOL WINAPI FtpRenameFileA(HINTERNET hFtpSession, LPCSTR lpszSrc, LPCSTR lpszDest)
1984 lpwzSrc = heap_strdupAtoW(lpszSrc);
1985 lpwzDest = heap_strdupAtoW(lpszDest);
1986 ret = FtpRenameFileW(hFtpSession, lpwzSrc, lpwzDest);
1987 HeapFree(GetProcessHeap(), 0, lpwzSrc);
1988 HeapFree(GetProcessHeap(), 0, lpwzDest);
1992 static void AsyncFtpRenameFileProc(WORKREQUEST *workRequest)
1994 struct WORKREQ_FTPRENAMEFILEW const *req = &workRequest->u.FtpRenameFileW;
1995 ftp_session_t *lpwfs = (ftp_session_t*) workRequest->hdr;
1997 TRACE("%p\n", lpwfs);
1999 FTP_FtpRenameFileW(lpwfs, req->lpszSrcFile, req->lpszDestFile);
2000 HeapFree(GetProcessHeap(), 0, req->lpszSrcFile);
2001 HeapFree(GetProcessHeap(), 0, req->lpszDestFile);
2004 /***********************************************************************
2005 * FtpRenameFileW (WININET.@)
2007 * Rename a file on the ftp server
2014 BOOL WINAPI FtpRenameFileW(HINTERNET hFtpSession, LPCWSTR lpszSrc, LPCWSTR lpszDest)
2016 ftp_session_t *lpwfs;
2017 appinfo_t *hIC = NULL;
2020 lpwfs = (ftp_session_t*) WININET_GetObject( hFtpSession );
2023 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2027 if (WH_HFTPSESSION != lpwfs->hdr.htype)
2029 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2033 if (lpwfs->download_in_progress != NULL)
2035 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
2039 if (!lpszSrc || !lpszDest)
2041 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2045 hIC = lpwfs->lpAppInfo;
2046 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2048 WORKREQUEST workRequest;
2049 struct WORKREQ_FTPRENAMEFILEW *req;
2051 workRequest.asyncproc = AsyncFtpRenameFileProc;
2052 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
2053 req = &workRequest.u.FtpRenameFileW;
2054 req->lpszSrcFile = heap_strdupW(lpszSrc);
2055 req->lpszDestFile = heap_strdupW(lpszDest);
2057 r = INTERNET_AsyncCall(&workRequest);
2061 r = FTP_FtpRenameFileW(lpwfs, lpszSrc, lpszDest);
2065 WININET_Release( &lpwfs->hdr );
2070 /***********************************************************************
2071 * FTP_FtpRenameFileW (Internal)
2073 * Rename a file on the ftp server
2080 BOOL FTP_FtpRenameFileW(ftp_session_t *lpwfs, LPCWSTR lpszSrc, LPCWSTR lpszDest)
2083 BOOL bSuccess = FALSE;
2084 appinfo_t *hIC = NULL;
2088 /* Clear any error information */
2089 INTERNET_SetLastError(0);
2091 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNFR, lpszSrc, 0, 0, 0))
2094 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2095 if (nResCode == 350)
2097 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNTO, lpszDest, 0, 0, 0))
2100 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2103 if (nResCode == 250)
2106 FTP_SetResponseError(nResCode);
2109 hIC = lpwfs->lpAppInfo;
2110 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2112 INTERNET_ASYNC_RESULT iar;
2114 iar.dwResult = (DWORD)bSuccess;
2115 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2116 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
2117 &iar, sizeof(INTERNET_ASYNC_RESULT));
2123 /***********************************************************************
2124 * FtpCommandA (WININET.@)
2126 BOOL WINAPI FtpCommandA( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
2127 LPCSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
2132 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
2133 debugstr_a(lpszCommand), dwContext, phFtpCommand);
2135 if (fExpectResponse)
2137 FIXME("data connection not supported\n");
2141 if (!lpszCommand || !lpszCommand[0])
2143 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2147 if (!(cmdW = heap_strdupAtoW(lpszCommand)))
2149 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2153 r = FtpCommandW(hConnect, fExpectResponse, dwFlags, cmdW, dwContext, phFtpCommand);
2155 HeapFree(GetProcessHeap(), 0, cmdW);
2159 /***********************************************************************
2160 * FtpCommandW (WININET.@)
2162 BOOL WINAPI FtpCommandW( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
2163 LPCWSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
2166 ftp_session_t *lpwfs;
2168 DWORD len, nBytesSent= 0;
2169 INT nResCode, nRC = 0;
2171 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
2172 debugstr_w(lpszCommand), dwContext, phFtpCommand);
2174 if (!lpszCommand || !lpszCommand[0])
2176 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2180 if (fExpectResponse)
2182 FIXME("data connection not supported\n");
2186 lpwfs = (ftp_session_t*) WININET_GetObject( hConnect );
2189 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2193 if (WH_HFTPSESSION != lpwfs->hdr.htype)
2195 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2199 if (lpwfs->download_in_progress != NULL)
2201 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
2205 len = WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, NULL, 0, NULL, NULL) + strlen(szCRLF);
2206 if ((cmd = HeapAlloc(GetProcessHeap(), 0, len )))
2207 WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, cmd, len, NULL, NULL);
2210 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2214 strcat(cmd, szCRLF);
2217 TRACE("Sending (%s) len(%d)\n", cmd, len);
2218 while ((nBytesSent < len) && (nRC != -1))
2220 nRC = send(lpwfs->sndSocket, cmd + nBytesSent, len - nBytesSent, 0);
2224 TRACE("Sent %d bytes\n", nRC);
2230 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2231 if (nResCode > 0 && nResCode < 400)
2234 FTP_SetResponseError(nResCode);
2238 WININET_Release( &lpwfs->hdr );
2239 HeapFree(GetProcessHeap(), 0, cmd);
2244 /***********************************************************************
2245 * FTPSESSION_Destroy (internal)
2247 * Deallocate session handle
2249 static void FTPSESSION_Destroy(object_header_t *hdr)
2251 ftp_session_t *lpwfs = (ftp_session_t*) hdr;
2255 WININET_Release(&lpwfs->lpAppInfo->hdr);
2257 HeapFree(GetProcessHeap(), 0, lpwfs->lpszPassword);
2258 HeapFree(GetProcessHeap(), 0, lpwfs->lpszUserName);
2259 HeapFree(GetProcessHeap(), 0, lpwfs);
2262 static void FTPSESSION_CloseConnection(object_header_t *hdr)
2264 ftp_session_t *lpwfs = (ftp_session_t*) hdr;
2268 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
2269 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2271 if (lpwfs->download_in_progress != NULL)
2272 lpwfs->download_in_progress->session_deleted = TRUE;
2274 if (lpwfs->sndSocket != -1)
2275 closesocket(lpwfs->sndSocket);
2277 if (lpwfs->lstnSocket != -1)
2278 closesocket(lpwfs->lstnSocket);
2280 if (lpwfs->pasvSocket != -1)
2281 closesocket(lpwfs->pasvSocket);
2283 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
2284 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2287 static DWORD FTPSESSION_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2290 case INTERNET_OPTION_HANDLE_TYPE:
2291 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2293 if (*size < sizeof(ULONG))
2294 return ERROR_INSUFFICIENT_BUFFER;
2296 *size = sizeof(DWORD);
2297 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_FTP;
2298 return ERROR_SUCCESS;
2301 return INET_QueryOption(option, buffer, size, unicode);
2304 static const object_vtbl_t FTPSESSIONVtbl = {
2306 FTPSESSION_CloseConnection,
2307 FTPSESSION_QueryOption,
2317 /***********************************************************************
2318 * FTP_Connect (internal)
2320 * Connect to a ftp server
2323 * HINTERNET a session handle on success
2328 * Windows uses 'anonymous' as the username, when given a NULL username
2329 * and a NULL password. The password is first looked up in:
2331 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2333 * If this entry is not present it uses the current username as the password.
2337 HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
2338 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2339 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
2340 DWORD dwInternalFlags)
2342 static const WCHAR szKey[] = {'S','o','f','t','w','a','r','e','\\',
2343 'M','i','c','r','o','s','o','f','t','\\',
2344 'W','i','n','d','o','w','s','\\',
2345 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2346 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2347 static const WCHAR szValue[] = {'E','m','a','i','l','N','a','m','e',0};
2348 static const WCHAR szDefaultUsername[] = {'a','n','o','n','y','m','o','u','s','\0'};
2349 static const WCHAR szEmpty[] = {'\0'};
2350 struct sockaddr_in socketAddr;
2353 BOOL bSuccess = FALSE;
2354 ftp_session_t *lpwfs = NULL;
2355 HINTERNET handle = NULL;
2357 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2358 hIC, debugstr_w(lpszServerName),
2359 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword));
2361 assert( hIC->hdr.htype == WH_HINIT );
2363 if ((!lpszUserName || !*lpszUserName) && lpszPassword && *lpszPassword)
2365 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2369 lpwfs = HeapAlloc(GetProcessHeap(), 0, sizeof(ftp_session_t));
2372 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2376 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
2377 nServerPort = INTERNET_DEFAULT_FTP_PORT;
2379 lpwfs->hdr.htype = WH_HFTPSESSION;
2380 lpwfs->hdr.vtbl = &FTPSESSIONVtbl;
2381 lpwfs->hdr.dwFlags = dwFlags;
2382 lpwfs->hdr.dwContext = dwContext;
2383 lpwfs->hdr.dwInternalFlags = dwInternalFlags;
2384 lpwfs->hdr.refs = 1;
2385 lpwfs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2386 lpwfs->download_in_progress = NULL;
2387 lpwfs->sndSocket = -1;
2388 lpwfs->lstnSocket = -1;
2389 lpwfs->pasvSocket = -1;
2391 WININET_AddRef( &hIC->hdr );
2392 lpwfs->lpAppInfo = hIC;
2393 list_add_head( &hIC->hdr.children, &lpwfs->hdr.entry );
2395 handle = WININET_AllocHandle( &lpwfs->hdr );
2398 ERR("Failed to alloc handle\n");
2399 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2403 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2404 if(strchrW(hIC->lpszProxy, ' '))
2405 FIXME("Several proxies not implemented.\n");
2406 if(hIC->lpszProxyBypass)
2407 FIXME("Proxy bypass is ignored.\n");
2409 if (!lpszUserName || !strlenW(lpszUserName)) {
2411 WCHAR szPassword[MAX_PATH];
2412 DWORD len = sizeof(szPassword);
2414 lpwfs->lpszUserName = heap_strdupW(szDefaultUsername);
2416 RegOpenKeyW(HKEY_CURRENT_USER, szKey, &key);
2417 if (RegQueryValueExW(key, szValue, NULL, NULL, (LPBYTE)szPassword, &len)) {
2418 /* Nothing in the registry, get the username and use that as the password */
2419 if (!GetUserNameW(szPassword, &len)) {
2420 /* Should never get here, but use an empty password as failsafe */
2421 strcpyW(szPassword, szEmpty);
2426 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword));
2427 lpwfs->lpszPassword = heap_strdupW(szPassword);
2430 lpwfs->lpszUserName = heap_strdupW(lpszUserName);
2431 lpwfs->lpszPassword = heap_strdupW(lpszPassword ? lpszPassword : szEmpty);
2434 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2435 if (!(lpwfs->hdr.dwInternalFlags & INET_OPENURL))
2437 INTERNET_ASYNC_RESULT iar;
2439 iar.dwResult = (DWORD_PTR)handle;
2440 iar.dwError = ERROR_SUCCESS;
2442 SendAsyncCallback(&hIC->hdr, dwContext,
2443 INTERNET_STATUS_HANDLE_CREATED, &iar,
2444 sizeof(INTERNET_ASYNC_RESULT));
2447 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_RESOLVING_NAME,
2448 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2450 sock_namelen = sizeof(socketAddr);
2451 if (!GetAddress(lpszServerName, nServerPort,
2452 (struct sockaddr *)&socketAddr, &sock_namelen))
2454 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2458 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_NAME_RESOLVED,
2459 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2461 if (socketAddr.sin_family != AF_INET)
2463 WARN("unsupported address family %d\n", socketAddr.sin_family);
2464 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2467 nsocket = socket(AF_INET,SOCK_STREAM,0);
2470 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2474 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTING_TO_SERVER,
2475 &socketAddr, sock_namelen);
2477 if (connect(nsocket, (struct sockaddr *)&socketAddr, sock_namelen) < 0)
2479 ERR("Unable to connect (%s)\n", strerror(errno));
2480 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2481 closesocket(nsocket);
2485 TRACE("Connected to server\n");
2486 lpwfs->sndSocket = nsocket;
2487 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTED_TO_SERVER,
2488 &socketAddr, sock_namelen);
2490 sock_namelen = sizeof(lpwfs->socketAddress);
2491 getsockname(nsocket, (struct sockaddr *) &lpwfs->socketAddress, &sock_namelen);
2493 if (FTP_ConnectToHost(lpwfs))
2495 TRACE("Successfully logged into server\n");
2501 if (lpwfs) WININET_Release( &lpwfs->hdr );
2503 if (!bSuccess && handle)
2505 WININET_FreeHandle( handle );
2513 /***********************************************************************
2514 * FTP_ConnectToHost (internal)
2516 * Connect to a ftp server
2523 static BOOL FTP_ConnectToHost(ftp_session_t *lpwfs)
2526 BOOL bSuccess = FALSE;
2529 FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2531 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_USER, lpwfs->lpszUserName, 0, 0, 0))
2534 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2537 /* Login successful... */
2538 if (nResCode == 230)
2540 /* User name okay, need password... */
2541 else if (nResCode == 331)
2542 bSuccess = FTP_SendPassword(lpwfs);
2543 /* Need account for login... */
2544 else if (nResCode == 332)
2545 bSuccess = FTP_SendAccount(lpwfs);
2547 FTP_SetResponseError(nResCode);
2550 TRACE("Returning %d\n", bSuccess);
2556 /***********************************************************************
2557 * FTP_SendCommandA (internal)
2559 * Send command to server
2566 static BOOL FTP_SendCommandA(INT nSocket, FTP_COMMAND ftpCmd, LPCSTR lpszParam,
2567 INTERNET_STATUS_CALLBACK lpfnStatusCB, object_header_t *hdr, DWORD_PTR dwContext)
2571 DWORD nBytesSent = 0;
2575 TRACE("%d: (%s) %d\n", ftpCmd, lpszParam, nSocket);
2579 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2582 dwParamLen = lpszParam?strlen(lpszParam)+1:0;
2583 len = dwParamLen + strlen(szFtpCommands[ftpCmd]) + strlen(szCRLF);
2584 if (NULL == (buf = HeapAlloc(GetProcessHeap(), 0, len+1)))
2586 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2589 sprintf(buf, "%s%s%s%s", szFtpCommands[ftpCmd], dwParamLen ? " " : "",
2590 dwParamLen ? lpszParam : "", szCRLF);
2592 TRACE("Sending (%s) len(%d)\n", buf, len);
2593 while((nBytesSent < len) && (nRC != -1))
2595 nRC = send(nSocket, buf+nBytesSent, len - nBytesSent, 0);
2599 HeapFree(GetProcessHeap(), 0, (LPVOID)buf);
2603 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_REQUEST_SENT,
2604 &nBytesSent, sizeof(DWORD));
2607 TRACE("Sent %d bytes\n", nBytesSent);
2611 /***********************************************************************
2612 * FTP_SendCommand (internal)
2614 * Send command to server
2621 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
2622 INTERNET_STATUS_CALLBACK lpfnStatusCB, object_header_t *hdr, DWORD_PTR dwContext)
2625 LPSTR lpszParamA = heap_strdupWtoA(lpszParam);
2626 ret = FTP_SendCommandA(nSocket, ftpCmd, lpszParamA, lpfnStatusCB, hdr, dwContext);
2627 HeapFree(GetProcessHeap(), 0, lpszParamA);
2631 /***********************************************************************
2632 * FTP_ReceiveResponse (internal)
2634 * Receive response from server
2637 * Reply code on success
2641 INT FTP_ReceiveResponse(ftp_session_t *lpwfs, DWORD_PTR dwContext)
2643 LPSTR lpszResponse = INTERNET_GetResponseBuffer();
2646 char firstprefix[5];
2647 BOOL multiline = FALSE;
2649 TRACE("socket(%d)\n", lpwfs->sndSocket);
2651 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2655 if (!INTERNET_GetNextLine(lpwfs->sndSocket, &nRecv))
2662 if(lpszResponse[3] != '-')
2665 { /* Start of multiline response. Loop until we get "nnn " */
2667 memcpy(firstprefix, lpszResponse, 3);
2668 firstprefix[3] = ' ';
2669 firstprefix[4] = '\0';
2674 if(!memcmp(firstprefix, lpszResponse, 4))
2682 rc = atoi(lpszResponse);
2684 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2685 &nRecv, sizeof(DWORD));
2689 TRACE("return %d\n", rc);
2694 /***********************************************************************
2695 * FTP_SendPassword (internal)
2697 * Send password to ftp server
2704 static BOOL FTP_SendPassword(ftp_session_t *lpwfs)
2707 BOOL bSuccess = FALSE;
2710 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASS, lpwfs->lpszPassword, 0, 0, 0))
2713 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2716 TRACE("Received reply code %d\n", nResCode);
2717 /* Login successful... */
2718 if (nResCode == 230)
2720 /* Command not implemented, superfluous at the server site... */
2721 /* Need account for login... */
2722 else if (nResCode == 332)
2723 bSuccess = FTP_SendAccount(lpwfs);
2725 FTP_SetResponseError(nResCode);
2729 TRACE("Returning %d\n", bSuccess);
2734 /***********************************************************************
2735 * FTP_SendAccount (internal)
2744 static BOOL FTP_SendAccount(ftp_session_t *lpwfs)
2747 BOOL bSuccess = FALSE;
2750 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_ACCT, szNoAccount, 0, 0, 0))
2753 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2757 FTP_SetResponseError(nResCode);
2764 /***********************************************************************
2765 * FTP_SendStore (internal)
2767 * Send request to upload file to ftp server
2774 static BOOL FTP_SendStore(ftp_session_t *lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
2777 BOOL bSuccess = FALSE;
2780 if (!FTP_InitListenSocket(lpwfs))
2783 if (!FTP_SendType(lpwfs, dwType))
2786 if (!FTP_SendPortOrPasv(lpwfs))
2789 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_STOR, lpszRemoteFile, 0, 0, 0))
2791 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2794 if (nResCode == 150 || nResCode == 125)
2797 FTP_SetResponseError(nResCode);
2801 if (!bSuccess && lpwfs->lstnSocket != -1)
2803 closesocket(lpwfs->lstnSocket);
2804 lpwfs->lstnSocket = -1;
2811 /***********************************************************************
2812 * FTP_InitListenSocket (internal)
2814 * Create a socket to listen for server response
2821 static BOOL FTP_InitListenSocket(ftp_session_t *lpwfs)
2823 BOOL bSuccess = FALSE;
2824 socklen_t namelen = sizeof(lpwfs->lstnSocketAddress);
2828 lpwfs->lstnSocket = socket(PF_INET, SOCK_STREAM, 0);
2829 if (lpwfs->lstnSocket == -1)
2831 TRACE("Unable to create listening socket\n");
2835 /* We obtain our ip addr from the name of the command channel socket */
2836 lpwfs->lstnSocketAddress = lpwfs->socketAddress;
2838 /* and get the system to assign us a port */
2839 lpwfs->lstnSocketAddress.sin_port = htons(0);
2841 if (bind(lpwfs->lstnSocket,(struct sockaddr *) &lpwfs->lstnSocketAddress, sizeof(lpwfs->lstnSocketAddress)) == -1)
2843 TRACE("Unable to bind socket\n");
2847 if (listen(lpwfs->lstnSocket, MAX_BACKLOG) == -1)
2849 TRACE("listen failed\n");
2853 if (getsockname(lpwfs->lstnSocket, (struct sockaddr *) &lpwfs->lstnSocketAddress, &namelen) != -1)
2857 if (!bSuccess && lpwfs->lstnSocket != -1)
2859 closesocket(lpwfs->lstnSocket);
2860 lpwfs->lstnSocket = -1;
2867 /***********************************************************************
2868 * FTP_SendType (internal)
2870 * Tell server type of data being transferred
2876 * W98SE doesn't cache the type that's currently set
2877 * (i.e. it sends it always),
2878 * so we probably don't want to do that either.
2880 static BOOL FTP_SendType(ftp_session_t *lpwfs, DWORD dwType)
2883 WCHAR type[] = { 'I','\0' };
2884 BOOL bSuccess = FALSE;
2887 if (dwType & INTERNET_FLAG_TRANSFER_ASCII)
2890 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_TYPE, type, 0, 0, 0))
2893 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext)/100;
2899 FTP_SetResponseError(nResCode);
2907 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2908 /***********************************************************************
2909 * FTP_GetFileSize (internal)
2911 * Retrieves from the server the size of the given file
2918 static BOOL FTP_GetFileSize(ftp_session_t *lpwfs, LPCWSTR lpszRemoteFile, DWORD *dwSize)
2921 BOOL bSuccess = FALSE;
2925 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_SIZE, lpszRemoteFile, 0, 0, 0))
2928 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2931 if (nResCode == 213) {
2932 /* Now parses the output to get the actual file size */
2934 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
2936 for (i = 0; (lpszResponseBuffer[i] != ' ') && (lpszResponseBuffer[i] != '\0'); i++) ;
2937 if (lpszResponseBuffer[i] == '\0') return FALSE;
2938 *dwSize = atol(&(lpszResponseBuffer[i + 1]));
2942 FTP_SetResponseError(nResCode);
2952 /***********************************************************************
2953 * FTP_SendPort (internal)
2955 * Tell server which port to use
2962 static BOOL FTP_SendPort(ftp_session_t *lpwfs)
2964 static const WCHAR szIPFormat[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
2966 WCHAR szIPAddress[64];
2967 BOOL bSuccess = FALSE;
2970 sprintfW(szIPAddress, szIPFormat,
2971 lpwfs->lstnSocketAddress.sin_addr.s_addr&0x000000FF,
2972 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x0000FF00)>>8,
2973 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x00FF0000)>>16,
2974 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0xFF000000)>>24,
2975 lpwfs->lstnSocketAddress.sin_port & 0xFF,
2976 (lpwfs->lstnSocketAddress.sin_port & 0xFF00)>>8);
2978 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PORT, szIPAddress, 0, 0, 0))
2981 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2984 if (nResCode == 200)
2987 FTP_SetResponseError(nResCode);
2995 /***********************************************************************
2996 * FTP_DoPassive (internal)
2998 * Tell server that we want to do passive transfers
2999 * and connect data socket
3006 static BOOL FTP_DoPassive(ftp_session_t *lpwfs)
3009 BOOL bSuccess = FALSE;
3012 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASV, NULL, 0, 0, 0))
3015 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3018 if (nResCode == 227)
3020 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
3024 char *pAddr, *pPort;
3026 struct sockaddr_in dataSocketAddress;
3028 p = lpszResponseBuffer+4; /* skip status code */
3029 while (*p != '\0' && (*p < '0' || *p > '9')) p++;
3033 ERR("no address found in response, aborting\n");
3037 if (sscanf(p, "%d,%d,%d,%d,%d,%d", &f[0], &f[1], &f[2], &f[3],
3040 ERR("unknown response address format '%s', aborting\n", p);
3043 for (i=0; i < 6; i++)
3046 dataSocketAddress = lpwfs->socketAddress;
3047 pAddr = (char *)&(dataSocketAddress.sin_addr.s_addr);
3048 pPort = (char *)&(dataSocketAddress.sin_port);
3056 nsocket = socket(AF_INET,SOCK_STREAM,0);
3060 if (connect(nsocket, (struct sockaddr *)&dataSocketAddress, sizeof(dataSocketAddress)))
3062 ERR("can't connect passive FTP data port.\n");
3063 closesocket(nsocket);
3066 lpwfs->pasvSocket = nsocket;
3070 FTP_SetResponseError(nResCode);
3078 static BOOL FTP_SendPortOrPasv(ftp_session_t *lpwfs)
3080 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
3082 if (!FTP_DoPassive(lpwfs))
3087 if (!FTP_SendPort(lpwfs))
3094 /***********************************************************************
3095 * FTP_GetDataSocket (internal)
3097 * Either accepts an incoming data socket connection from the server
3098 * or just returns the already opened socket after a PASV command
3099 * in case of passive FTP.
3107 static BOOL FTP_GetDataSocket(ftp_session_t *lpwfs, LPINT nDataSocket)
3109 struct sockaddr_in saddr;
3110 socklen_t addrlen = sizeof(struct sockaddr);
3113 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
3115 *nDataSocket = lpwfs->pasvSocket;
3116 lpwfs->pasvSocket = -1;
3120 *nDataSocket = accept(lpwfs->lstnSocket, (struct sockaddr *) &saddr, &addrlen);
3121 closesocket(lpwfs->lstnSocket);
3122 lpwfs->lstnSocket = -1;
3124 return *nDataSocket != -1;
3128 /***********************************************************************
3129 * FTP_SendData (internal)
3131 * Send data to the server
3138 static BOOL FTP_SendData(ftp_session_t *lpwfs, INT nDataSocket, HANDLE hFile)
3140 BY_HANDLE_FILE_INFORMATION fi;
3141 DWORD nBytesRead = 0;
3142 DWORD nBytesSent = 0;
3143 DWORD nTotalSent = 0;
3144 DWORD nBytesToSend, nLen;
3146 time_t s_long_time, e_long_time;
3151 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
3153 /* Get the size of the file. */
3154 GetFileInformationByHandle(hFile, &fi);
3159 nBytesToSend = nBytesRead - nBytesSent;
3161 if (nBytesToSend <= 0)
3163 /* Read data from file. */
3165 if (!ReadFile(hFile, lpszBuffer, DATA_PACKET_SIZE, &nBytesRead, 0))
3166 ERR("Failed reading from file\n");
3169 nBytesToSend = nBytesRead;
3174 nLen = DATA_PACKET_SIZE < nBytesToSend ?
3175 DATA_PACKET_SIZE : nBytesToSend;
3176 nRC = send(nDataSocket, lpszBuffer, nLen, 0);
3184 /* Do some computation to display the status. */
3186 nSeconds = e_long_time - s_long_time;
3187 if( nSeconds / 60 > 0 )
3189 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
3190 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds / 60,
3191 nSeconds % 60, (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent );
3195 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
3196 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds,
3197 (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent);
3199 } while (nRC != -1);
3201 TRACE("file transfer complete!\n");
3203 HeapFree(GetProcessHeap(), 0, lpszBuffer);
3209 /***********************************************************************
3210 * FTP_SendRetrieve (internal)
3212 * Send request to retrieve a file
3215 * Number of bytes to be received on success
3219 static BOOL FTP_SendRetrieve(ftp_session_t *lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
3225 if (!(ret = FTP_InitListenSocket(lpwfs)))
3228 if (!(ret = FTP_SendType(lpwfs, dwType)))
3231 if (!(ret = FTP_SendPortOrPasv(lpwfs)))
3234 if (!(ret = FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RETR, lpszRemoteFile, 0, 0, 0)))
3237 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3238 if ((nResCode != 125) && (nResCode != 150)) {
3239 /* That means that we got an error getting the file. */
3240 FTP_SetResponseError(nResCode);
3245 if (!ret && lpwfs->lstnSocket != -1)
3247 closesocket(lpwfs->lstnSocket);
3248 lpwfs->lstnSocket = -1;
3255 /***********************************************************************
3256 * FTP_RetrieveData (internal)
3258 * Retrieve data from server
3265 static BOOL FTP_RetrieveFileData(ftp_session_t *lpwfs, INT nDataSocket, HANDLE hFile)
3267 DWORD nBytesWritten;
3273 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
3274 if (NULL == lpszBuffer)
3276 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3282 nRC = recv(nDataSocket, lpszBuffer, DATA_PACKET_SIZE, 0);
3285 /* other side closed socket. */
3288 WriteFile(hFile, lpszBuffer, nRC, &nBytesWritten, NULL);
3292 TRACE("Data transfer complete\n");
3295 HeapFree(GetProcessHeap(), 0, lpszBuffer);
3300 /***********************************************************************
3301 * FTPFINDNEXT_Destroy (internal)
3303 * Deallocate session handle
3305 static void FTPFINDNEXT_Destroy(object_header_t *hdr)
3307 LPWININETFTPFINDNEXTW lpwfn = (LPWININETFTPFINDNEXTW) hdr;
3312 WININET_Release(&lpwfn->lpFtpSession->hdr);
3314 for (i = 0; i < lpwfn->size; i++)
3316 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp[i].lpszName);
3319 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp);
3320 HeapFree(GetProcessHeap(), 0, lpwfn);
3323 static DWORD FTPFINDNEXT_FindNextFileProc(WININETFTPFINDNEXTW *find, LPVOID data)
3325 WIN32_FIND_DATAW *find_data = data;
3326 DWORD res = ERROR_SUCCESS;
3328 TRACE("index(%d) size(%d)\n", find->index, find->size);
3330 ZeroMemory(find_data, sizeof(WIN32_FIND_DATAW));
3332 if (find->index < find->size) {
3333 FTP_ConvertFileProp(&find->lpafp[find->index], find_data);
3336 TRACE("Name: %s\nSize: %d\n", debugstr_w(find_data->cFileName), find_data->nFileSizeLow);
3338 res = ERROR_NO_MORE_FILES;
3341 if (find->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3343 INTERNET_ASYNC_RESULT iar;
3345 iar.dwResult = (res == ERROR_SUCCESS);
3348 INTERNET_SendCallback(&find->hdr, find->hdr.dwContext,
3349 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3350 sizeof(INTERNET_ASYNC_RESULT));
3356 static void FTPFINDNEXT_AsyncFindNextFileProc(WORKREQUEST *workRequest)
3358 struct WORKREQ_FTPFINDNEXTW *req = &workRequest->u.FtpFindNextW;
3360 FTPFINDNEXT_FindNextFileProc((WININETFTPFINDNEXTW*)workRequest->hdr, req->lpFindFileData);
3363 static DWORD FTPFINDNEXT_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3366 case INTERNET_OPTION_HANDLE_TYPE:
3367 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3369 if (*size < sizeof(ULONG))
3370 return ERROR_INSUFFICIENT_BUFFER;
3372 *size = sizeof(DWORD);
3373 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_FTP_FIND;
3374 return ERROR_SUCCESS;
3377 return INET_QueryOption(option, buffer, size, unicode);
3380 static DWORD FTPFINDNEXT_FindNextFileW(object_header_t *hdr, void *data)
3382 WININETFTPFINDNEXTW *find = (WININETFTPFINDNEXTW*)hdr;
3384 if (find->lpFtpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3386 WORKREQUEST workRequest;
3387 struct WORKREQ_FTPFINDNEXTW *req;
3389 workRequest.asyncproc = FTPFINDNEXT_AsyncFindNextFileProc;
3390 workRequest.hdr = WININET_AddRef( &find->hdr );
3391 req = &workRequest.u.FtpFindNextW;
3392 req->lpFindFileData = data;
3394 INTERNET_AsyncCall(&workRequest);
3396 return ERROR_SUCCESS;
3399 return FTPFINDNEXT_FindNextFileProc(find, data);
3402 static const object_vtbl_t FTPFINDNEXTVtbl = {
3403 FTPFINDNEXT_Destroy,
3405 FTPFINDNEXT_QueryOption,
3412 FTPFINDNEXT_FindNextFileW
3415 /***********************************************************************
3416 * FTP_ReceiveFileList (internal)
3418 * Read file list from server
3421 * Handle to file list on success
3425 static HINTERNET FTP_ReceiveFileList(ftp_session_t *lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3426 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext)
3429 LPFILEPROPERTIESW lpafp = NULL;
3430 LPWININETFTPFINDNEXTW lpwfn = NULL;
3431 HINTERNET handle = 0;
3433 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs, nSocket, debugstr_w(lpszSearchFile), lpFindFileData, dwContext);
3435 if (FTP_ParseDirectory(lpwfs, nSocket, lpszSearchFile, &lpafp, &dwSize))
3438 FTP_ConvertFileProp(lpafp, lpFindFileData);
3440 lpwfn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETFTPFINDNEXTW));
3443 lpwfn->hdr.htype = WH_HFTPFINDNEXT;
3444 lpwfn->hdr.vtbl = &FTPFINDNEXTVtbl;
3445 lpwfn->hdr.dwContext = dwContext;
3446 lpwfn->hdr.refs = 1;
3447 lpwfn->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
3448 lpwfn->index = 1; /* Next index is 1 since we return index 0 */
3449 lpwfn->size = dwSize;
3450 lpwfn->lpafp = lpafp;
3452 WININET_AddRef( &lpwfs->hdr );
3453 lpwfn->lpFtpSession = lpwfs;
3454 list_add_head( &lpwfs->hdr.children, &lpwfn->hdr.entry );
3456 handle = WININET_AllocHandle( &lpwfn->hdr );
3461 WININET_Release( &lpwfn->hdr );
3463 TRACE("Matched %d files\n", dwSize);
3468 /***********************************************************************
3469 * FTP_ConvertFileProp (internal)
3471 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3478 static BOOL FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp, LPWIN32_FIND_DATAW lpFindFileData)
3480 BOOL bSuccess = FALSE;
3482 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAW));
3486 SystemTimeToFileTime( &lpafp->tmLastModified, &lpFindFileData->ftLastAccessTime );
3487 lpFindFileData->ftLastWriteTime = lpFindFileData->ftLastAccessTime;
3488 lpFindFileData->ftCreationTime = lpFindFileData->ftLastAccessTime;
3490 /* Not all fields are filled in */
3491 lpFindFileData->nFileSizeHigh = 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3492 lpFindFileData->nFileSizeLow = lpafp->nSize;
3494 if (lpafp->bIsDirectory)
3495 lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
3497 if (lpafp->lpszName)
3498 lstrcpynW(lpFindFileData->cFileName, lpafp->lpszName, MAX_PATH);
3506 /***********************************************************************
3507 * FTP_ParseNextFile (internal)
3509 * Parse the next line in file listing
3515 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW lpfp)
3517 static const char szSpace[] = " \t";
3525 lpfp->lpszName = NULL;
3527 if(!(pszLine = INTERNET_GetNextLine(nSocket, &nBufLen)))
3530 pszToken = strtok(pszLine, szSpace);
3532 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3535 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3537 if(!isdigit(pszToken[0]) && 10 == strlen(pszToken)) {
3538 if(!FTP_ParsePermission(pszToken, lpfp))
3539 lpfp->bIsDirectory = FALSE;
3540 for(i=0; i<=3; i++) {
3541 if(!(pszToken = strtok(NULL, szSpace)))
3544 if(!pszToken) continue;
3545 if(lpfp->bIsDirectory) {
3546 TRACE("Is directory\n");
3550 TRACE("Size: %s\n", pszToken);
3551 lpfp->nSize = atol(pszToken);
3554 lpfp->tmLastModified.wSecond = 0;
3555 lpfp->tmLastModified.wMinute = 0;
3556 lpfp->tmLastModified.wHour = 0;
3557 lpfp->tmLastModified.wDay = 0;
3558 lpfp->tmLastModified.wMonth = 0;
3559 lpfp->tmLastModified.wYear = 0;
3561 /* Determine month */
3562 pszToken = strtok(NULL, szSpace);
3563 if(!pszToken) continue;
3564 if(strlen(pszToken) >= 3) {
3566 if((pszTmp = StrStrIA(szMonths, pszToken)))
3567 lpfp->tmLastModified.wMonth = ((pszTmp - szMonths) / 3)+1;
3570 pszToken = strtok(NULL, szSpace);
3571 if(!pszToken) continue;
3572 lpfp->tmLastModified.wDay = atoi(pszToken);
3573 /* Determine time or year */
3574 pszToken = strtok(NULL, szSpace);
3575 if(!pszToken) continue;
3576 if((pszTmp = strchr(pszToken, ':'))) {
3577 SYSTEMTIME curr_time;
3580 lpfp->tmLastModified.wMinute = atoi(pszTmp);
3581 lpfp->tmLastModified.wHour = atoi(pszToken);
3582 GetLocalTime( &curr_time );
3583 lpfp->tmLastModified.wYear = curr_time.wYear;
3586 lpfp->tmLastModified.wYear = atoi(pszToken);
3587 lpfp->tmLastModified.wHour = 12;
3589 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3590 lpfp->tmLastModified.wHour, lpfp->tmLastModified.wMinute, lpfp->tmLastModified.wSecond,
3591 lpfp->tmLastModified.wYear, lpfp->tmLastModified.wMonth, lpfp->tmLastModified.wDay);
3593 pszToken = strtok(NULL, szSpace);
3594 if(!pszToken) continue;
3595 lpfp->lpszName = heap_strdupAtoW(pszToken);
3596 TRACE("File: %s\n", debugstr_w(lpfp->lpszName));
3598 /* NT way of parsing ... :
3600 07-13-03 08:55PM <DIR> sakpatch
3601 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3603 else if(isdigit(pszToken[0]) && 8 == strlen(pszToken)) {
3604 int mon, mday, year, hour, min;
3605 lpfp->permissions = 0xFFFF; /* No idea, put full permission :-) */
3607 sscanf(pszToken, "%d-%d-%d", &mon, &mday, &year);
3608 lpfp->tmLastModified.wDay = mday;
3609 lpfp->tmLastModified.wMonth = mon;
3610 lpfp->tmLastModified.wYear = year;
3612 /* Hacky and bad Y2K protection :-) */
3613 if (lpfp->tmLastModified.wYear < 70) lpfp->tmLastModified.wYear += 2000;
3615 pszToken = strtok(NULL, szSpace);
3616 if(!pszToken) continue;
3617 sscanf(pszToken, "%d:%d", &hour, &min);
3618 lpfp->tmLastModified.wHour = hour;
3619 lpfp->tmLastModified.wMinute = min;
3620 if((pszToken[5] == 'P') && (pszToken[6] == 'M')) {
3621 lpfp->tmLastModified.wHour += 12;
3623 lpfp->tmLastModified.wSecond = 0;
3625 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3626 lpfp->tmLastModified.wHour, lpfp->tmLastModified.wMinute, lpfp->tmLastModified.wSecond,
3627 lpfp->tmLastModified.wYear, lpfp->tmLastModified.wMonth, lpfp->tmLastModified.wDay);
3629 pszToken = strtok(NULL, szSpace);
3630 if(!pszToken) continue;
3631 if(!strcasecmp(pszToken, "<DIR>")) {
3632 lpfp->bIsDirectory = TRUE;
3634 TRACE("Is directory\n");
3637 lpfp->bIsDirectory = FALSE;
3638 lpfp->nSize = atol(pszToken);
3639 TRACE("Size: %d\n", lpfp->nSize);
3642 pszToken = strtok(NULL, szSpace);
3643 if(!pszToken) continue;
3644 lpfp->lpszName = heap_strdupAtoW(pszToken);
3645 TRACE("Name: %s\n", debugstr_w(lpfp->lpszName));
3647 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3648 else if(pszToken[0] == '+') {
3649 FIXME("EPLF Format not implemented\n");
3652 if(lpfp->lpszName) {
3653 if((lpszSearchFile == NULL) ||
3654 (PathMatchSpecW(lpfp->lpszName, lpszSearchFile))) {
3656 TRACE("Matched: %s\n", debugstr_w(lpfp->lpszName));
3659 HeapFree(GetProcessHeap(), 0, lpfp->lpszName);
3660 lpfp->lpszName = NULL;
3667 /***********************************************************************
3668 * FTP_ParseDirectory (internal)
3670 * Parse string of directory information
3676 static BOOL FTP_ParseDirectory(ftp_session_t *lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3677 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp)
3679 BOOL bSuccess = TRUE;
3680 INT sizeFilePropArray = 500;/*20; */
3681 INT indexFilePropArray = -1;
3685 /* Allocate initial file properties array */
3686 *lpafp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FILEPROPERTIESW)*(sizeFilePropArray));
3691 if (indexFilePropArray+1 >= sizeFilePropArray)
3693 LPFILEPROPERTIESW tmpafp;
3695 sizeFilePropArray *= 2;
3696 tmpafp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *lpafp,
3697 sizeof(FILEPROPERTIESW)*sizeFilePropArray);
3706 indexFilePropArray++;
3707 } while (FTP_ParseNextFile(nSocket, lpszSearchFile, &(*lpafp)[indexFilePropArray]));
3709 if (bSuccess && indexFilePropArray)
3711 if (indexFilePropArray < sizeFilePropArray - 1)
3713 LPFILEPROPERTIESW tmpafp;
3715 tmpafp = HeapReAlloc(GetProcessHeap(), 0, *lpafp,
3716 sizeof(FILEPROPERTIESW)*indexFilePropArray);
3720 *dwfp = indexFilePropArray;
3724 HeapFree(GetProcessHeap(), 0, *lpafp);
3725 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
3733 /***********************************************************************
3734 * FTP_ParsePermission (internal)
3736 * Parse permission string of directory information
3743 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp)
3745 BOOL bSuccess = TRUE;
3746 unsigned short nPermission = 0;
3751 if ((*lpszPermission != 'd') && (*lpszPermission != '-') && (*lpszPermission != 'l'))
3757 lpfp->bIsDirectory = (*lpszPermission == 'd');
3763 nPermission |= (*(lpszPermission+1) == 'r' ? 1 : 0) << 8;
3766 nPermission |= (*(lpszPermission+2) == 'w' ? 1 : 0) << 7;
3769 nPermission |= (*(lpszPermission+3) == 'x' ? 1 : 0) << 6;
3772 nPermission |= (*(lpszPermission+4) == 'r' ? 1 : 0) << 5;
3775 nPermission |= (*(lpszPermission+5) == 'w' ? 1 : 0) << 4;
3778 nPermission |= (*(lpszPermission+6) == 'x' ? 1 : 0) << 3;
3781 nPermission |= (*(lpszPermission+7) == 'r' ? 1 : 0) << 2;
3784 nPermission |= (*(lpszPermission+8) == 'w' ? 1 : 0) << 1;
3787 nPermission |= (*(lpszPermission+9) == 'x' ? 1 : 0);
3791 }while (nPos <= nLast);
3793 lpfp->permissions = nPermission;
3798 /***********************************************************************
3799 * FTP_SetResponseError (internal)
3801 * Set the appropriate error code for a given response from the server
3806 static DWORD FTP_SetResponseError(DWORD dwResponse)
3812 case 425: /* Cannot open data connection. */
3813 dwCode = ERROR_INTERNET_CANNOT_CONNECT;
3816 case 426: /* Connection closed, transer aborted. */
3817 dwCode = ERROR_INTERNET_CONNECTION_ABORTED;
3820 case 530: /* Not logged in. Login incorrect. */
3821 dwCode = ERROR_INTERNET_LOGIN_FAILURE;
3824 case 421: /* Service not available - Server may be shutting down. */
3825 case 450: /* File action not taken. File may be busy. */
3826 case 451: /* Action aborted. Server error. */
3827 case 452: /* Action not taken. Insufficient storage space on server. */
3828 case 500: /* Syntax error. Command unrecognized. */
3829 case 501: /* Syntax error. Error in parameters or arguments. */
3830 case 502: /* Command not implemented. */
3831 case 503: /* Bad sequence of commands. */
3832 case 504: /* Command not implemented for that parameter. */
3833 case 532: /* Need account for storing files */
3834 case 550: /* File action not taken. File not found or no access. */
3835 case 551: /* Requested action aborted. Page type unknown */
3836 case 552: /* Action aborted. Exceeded storage allocation */
3837 case 553: /* Action not taken. File name not allowed. */
3840 dwCode = ERROR_INTERNET_EXTENDED_ERROR;
3844 INTERNET_SetLastError(dwCode);