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 _WININETFTPSESSIONW WININETFTPSESSIONW;
75 WININETHANDLEHEADER hdr;
76 WININETFTPSESSIONW *lpFtpSession;
79 } WININETFTPFILE, *LPWININETFTPFILE;
81 typedef struct _WININETFTPSESSIONW
83 WININETHANDLEHEADER hdr;
84 WININETAPPINFOW *lpAppInfo;
87 int pasvSocket; /* data socket connected by us in case of passive FTP */
88 LPWININETFTPFILE download_in_progress;
89 struct sockaddr_in socketAddress;
90 struct sockaddr_in lstnSocketAddress;
93 } *LPWININETFTPSESSIONW;
100 SYSTEMTIME tmLastModified;
101 unsigned short permissions;
102 } FILEPROPERTIESW, *LPFILEPROPERTIESW;
106 WININETHANDLEHEADER hdr;
107 WININETFTPSESSIONW *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, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext);
176 static BOOL FTP_SendStore(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType);
177 static BOOL FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs, LPINT nDataSocket);
178 static BOOL FTP_SendData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile);
179 static INT FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs, DWORD_PTR dwContext);
180 static BOOL FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType);
181 static BOOL FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile);
182 static BOOL FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs);
183 static BOOL FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs);
184 static BOOL FTP_SendPassword(LPWININETFTPSESSIONW lpwfs);
185 static BOOL FTP_SendAccount(LPWININETFTPSESSIONW lpwfs);
186 static BOOL FTP_SendType(LPWININETFTPSESSIONW lpwfs, DWORD dwType);
187 static BOOL FTP_SendPort(LPWININETFTPSESSIONW lpwfs);
188 static BOOL FTP_DoPassive(LPWININETFTPSESSIONW lpwfs);
189 static BOOL FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs);
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(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
193 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp);
194 static HINTERNET FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs, 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(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszLocalFile,
199 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext);
200 static BOOL FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory);
201 static BOOL FTP_FtpCreateDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory);
202 static HINTERNET FTP_FtpFindFirstFileW(LPWININETFTPSESSIONW lpwfs,
203 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext);
204 static BOOL FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPWSTR lpszCurrentDirectory,
205 LPDWORD lpdwCurrentDirectory);
206 static BOOL FTP_FtpRenameFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszSrc, LPCWSTR lpszDest);
207 static BOOL FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory);
208 static BOOL FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszFileName);
209 static BOOL FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs, 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 = lpszLocalFile?WININET_strdup_AtoW(lpszLocalFile):NULL;
232 lpwzNewRemoteFile = lpszNewRemoteFile?WININET_strdup_AtoW(lpszNewRemoteFile):NULL;
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 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) 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 LPWININETFTPSESSIONW lpwfs;
268 LPWININETAPPINFOW hIC = NULL;
271 if (!lpszLocalFile || !lpszNewRemoteFile)
273 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
277 lpwfs = (LPWININETFTPSESSIONW) 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 = WININET_strdupW(lpszLocalFile);
311 req->lpszNewRemoteFile = WININET_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(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszLocalFile,
340 LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
343 BOOL bSuccess = FALSE;
344 LPWININETAPPINFOW 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)
383 closesocket(lpwfs->lstnSocket);
385 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
387 INTERNET_ASYNC_RESULT iar;
389 iar.dwResult = (DWORD)bSuccess;
390 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
391 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
392 &iar, sizeof(INTERNET_ASYNC_RESULT));
401 /***********************************************************************
402 * FtpSetCurrentDirectoryA (WININET.@)
404 * Change the working directory on the FTP server
411 BOOL WINAPI FtpSetCurrentDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
413 LPWSTR lpwzDirectory;
416 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
417 ret = FtpSetCurrentDirectoryW(hConnect, lpwzDirectory);
418 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
423 static void AsyncFtpSetCurrentDirectoryProc(WORKREQUEST *workRequest)
425 struct WORKREQ_FTPSETCURRENTDIRECTORYW const *req = &workRequest->u.FtpSetCurrentDirectoryW;
426 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
428 TRACE("%p\n", lpwfs);
430 FTP_FtpSetCurrentDirectoryW(lpwfs, req->lpszDirectory);
431 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
434 /***********************************************************************
435 * FtpSetCurrentDirectoryW (WININET.@)
437 * Change the working directory on the FTP server
444 BOOL WINAPI FtpSetCurrentDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
446 LPWININETFTPSESSIONW lpwfs = NULL;
447 LPWININETAPPINFOW hIC = NULL;
452 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
456 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
457 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
459 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
463 if (lpwfs->download_in_progress != NULL)
465 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
469 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
471 hIC = lpwfs->lpAppInfo;
472 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
474 WORKREQUEST workRequest;
475 struct WORKREQ_FTPSETCURRENTDIRECTORYW *req;
477 workRequest.asyncproc = AsyncFtpSetCurrentDirectoryProc;
478 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
479 req = &workRequest.u.FtpSetCurrentDirectoryW;
480 req->lpszDirectory = WININET_strdupW(lpszDirectory);
482 r = INTERNET_AsyncCall(&workRequest);
486 r = FTP_FtpSetCurrentDirectoryW(lpwfs, lpszDirectory);
491 WININET_Release( &lpwfs->hdr );
497 /***********************************************************************
498 * FTP_FtpSetCurrentDirectoryW (Internal)
500 * Change the working directory on the FTP server
507 static BOOL FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
510 LPWININETAPPINFOW hIC = NULL;
511 DWORD bSuccess = FALSE;
513 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
515 /* Clear any error information */
516 INTERNET_SetLastError(0);
518 hIC = lpwfs->lpAppInfo;
519 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_CWD, lpszDirectory,
520 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
523 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
530 FTP_SetResponseError(nResCode);
534 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
536 INTERNET_ASYNC_RESULT iar;
538 iar.dwResult = bSuccess;
539 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
540 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
541 &iar, sizeof(INTERNET_ASYNC_RESULT));
547 /***********************************************************************
548 * FtpCreateDirectoryA (WININET.@)
550 * Create new directory on the FTP server
557 BOOL WINAPI FtpCreateDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
559 LPWSTR lpwzDirectory;
562 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
563 ret = FtpCreateDirectoryW(hConnect, lpwzDirectory);
564 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
569 static void AsyncFtpCreateDirectoryProc(WORKREQUEST *workRequest)
571 struct WORKREQ_FTPCREATEDIRECTORYW const *req = &workRequest->u.FtpCreateDirectoryW;
572 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
574 TRACE(" %p\n", lpwfs);
576 FTP_FtpCreateDirectoryW(lpwfs, req->lpszDirectory);
577 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
580 /***********************************************************************
581 * FtpCreateDirectoryW (WININET.@)
583 * Create new directory on the FTP server
590 BOOL WINAPI FtpCreateDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
592 LPWININETFTPSESSIONW lpwfs;
593 LPWININETAPPINFOW hIC = NULL;
596 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
599 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
603 if (WH_HFTPSESSION != lpwfs->hdr.htype)
605 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
609 if (lpwfs->download_in_progress != NULL)
611 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
617 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
621 hIC = lpwfs->lpAppInfo;
622 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
624 WORKREQUEST workRequest;
625 struct WORKREQ_FTPCREATEDIRECTORYW *req;
627 workRequest.asyncproc = AsyncFtpCreateDirectoryProc;
628 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
629 req = &workRequest.u.FtpCreateDirectoryW;
630 req->lpszDirectory = WININET_strdupW(lpszDirectory);
632 r = INTERNET_AsyncCall(&workRequest);
636 r = FTP_FtpCreateDirectoryW(lpwfs, lpszDirectory);
639 WININET_Release( &lpwfs->hdr );
645 /***********************************************************************
646 * FTP_FtpCreateDirectoryW (Internal)
648 * Create new directory on the FTP server
655 static BOOL FTP_FtpCreateDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
658 BOOL bSuccess = FALSE;
659 LPWININETAPPINFOW hIC = NULL;
661 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
663 /* Clear any error information */
664 INTERNET_SetLastError(0);
666 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_MKD, lpszDirectory, 0, 0, 0))
669 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
675 FTP_SetResponseError(nResCode);
679 hIC = lpwfs->lpAppInfo;
680 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
682 INTERNET_ASYNC_RESULT iar;
684 iar.dwResult = (DWORD)bSuccess;
685 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
686 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
687 &iar, sizeof(INTERNET_ASYNC_RESULT));
693 /***********************************************************************
694 * FtpFindFirstFileA (WININET.@)
696 * Search the specified directory
699 * HINTERNET on success
703 HINTERNET WINAPI FtpFindFirstFileA(HINTERNET hConnect,
704 LPCSTR lpszSearchFile, LPWIN32_FIND_DATAA lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
706 LPWSTR lpwzSearchFile;
707 WIN32_FIND_DATAW wfd;
708 LPWIN32_FIND_DATAW lpFindFileDataW;
711 lpwzSearchFile = lpszSearchFile?WININET_strdup_AtoW(lpszSearchFile):NULL;
712 lpFindFileDataW = lpFindFileData?&wfd:NULL;
713 ret = FtpFindFirstFileW(hConnect, lpwzSearchFile, lpFindFileDataW, dwFlags, dwContext);
714 HeapFree(GetProcessHeap(), 0, lpwzSearchFile);
717 WININET_find_data_WtoA(lpFindFileDataW, lpFindFileData);
723 static void AsyncFtpFindFirstFileProc(WORKREQUEST *workRequest)
725 struct WORKREQ_FTPFINDFIRSTFILEW const *req = &workRequest->u.FtpFindFirstFileW;
726 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
728 TRACE("%p\n", lpwfs);
730 FTP_FtpFindFirstFileW(lpwfs, req->lpszSearchFile,
731 req->lpFindFileData, req->dwFlags, req->dwContext);
732 HeapFree(GetProcessHeap(), 0, req->lpszSearchFile);
735 /***********************************************************************
736 * FtpFindFirstFileW (WININET.@)
738 * Search the specified directory
741 * HINTERNET on success
745 HINTERNET WINAPI FtpFindFirstFileW(HINTERNET hConnect,
746 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
748 LPWININETFTPSESSIONW lpwfs;
749 LPWININETAPPINFOW hIC = NULL;
752 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
753 if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
755 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
759 if (lpwfs->download_in_progress != NULL)
761 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
765 hIC = lpwfs->lpAppInfo;
766 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
768 WORKREQUEST workRequest;
769 struct WORKREQ_FTPFINDFIRSTFILEW *req;
771 workRequest.asyncproc = AsyncFtpFindFirstFileProc;
772 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
773 req = &workRequest.u.FtpFindFirstFileW;
774 req->lpszSearchFile = (lpszSearchFile == NULL) ? NULL : WININET_strdupW(lpszSearchFile);
775 req->lpFindFileData = lpFindFileData;
776 req->dwFlags = dwFlags;
777 req->dwContext= dwContext;
779 INTERNET_AsyncCall(&workRequest);
784 r = FTP_FtpFindFirstFileW(lpwfs, lpszSearchFile, lpFindFileData,
789 WININET_Release( &lpwfs->hdr );
795 /***********************************************************************
796 * FTP_FtpFindFirstFileW (Internal)
798 * Search the specified directory
801 * HINTERNET on success
805 static HINTERNET FTP_FtpFindFirstFileW(LPWININETFTPSESSIONW lpwfs,
806 LPCWSTR lpszSearchFile, LPWIN32_FIND_DATAW lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext)
809 LPWININETAPPINFOW hIC = NULL;
810 HINTERNET hFindNext = NULL;
814 /* Clear any error information */
815 INTERNET_SetLastError(0);
817 if (!FTP_InitListenSocket(lpwfs))
820 if (!FTP_SendType(lpwfs, INTERNET_FLAG_TRANSFER_ASCII))
823 if (!FTP_SendPortOrPasv(lpwfs))
826 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_LIST, NULL,
827 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
830 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
833 if (nResCode == 125 || nResCode == 150)
837 /* Get data socket to server */
838 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
840 hFindNext = FTP_ReceiveFileList(lpwfs, nDataSocket, lpszSearchFile, lpFindFileData, dwContext);
841 closesocket(nDataSocket);
842 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
843 if (nResCode != 226 && nResCode != 250)
844 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
848 FTP_SetResponseError(nResCode);
852 if (lpwfs->lstnSocket != -1)
853 closesocket(lpwfs->lstnSocket);
855 hIC = lpwfs->lpAppInfo;
856 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
858 INTERNET_ASYNC_RESULT iar;
862 iar.dwResult = (DWORD_PTR)hFindNext;
863 iar.dwError = ERROR_SUCCESS;
864 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
865 &iar, sizeof(INTERNET_ASYNC_RESULT));
868 iar.dwResult = (DWORD_PTR)hFindNext;
869 iar.dwError = hFindNext ? ERROR_SUCCESS : INTERNET_GetLastError();
870 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
871 &iar, sizeof(INTERNET_ASYNC_RESULT));
878 /***********************************************************************
879 * FtpGetCurrentDirectoryA (WININET.@)
881 * Retrieves the current directory
888 BOOL WINAPI FtpGetCurrentDirectoryA(HINTERNET hFtpSession, LPSTR lpszCurrentDirectory,
889 LPDWORD lpdwCurrentDirectory)
895 if(lpdwCurrentDirectory) {
896 len = *lpdwCurrentDirectory;
897 if(lpszCurrentDirectory)
899 dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
902 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
907 ret = FtpGetCurrentDirectoryW(hFtpSession, lpszCurrentDirectory?dir:NULL, lpdwCurrentDirectory?&len:NULL);
909 if (ret && lpszCurrentDirectory)
910 WideCharToMultiByte(CP_ACP, 0, dir, -1, lpszCurrentDirectory, len, NULL, NULL);
912 if (lpdwCurrentDirectory) *lpdwCurrentDirectory = len;
913 HeapFree(GetProcessHeap(), 0, dir);
918 static void AsyncFtpGetCurrentDirectoryProc(WORKREQUEST *workRequest)
920 struct WORKREQ_FTPGETCURRENTDIRECTORYW const *req = &workRequest->u.FtpGetCurrentDirectoryW;
921 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
923 TRACE("%p\n", lpwfs);
925 FTP_FtpGetCurrentDirectoryW(lpwfs, req->lpszDirectory, req->lpdwDirectory);
928 /***********************************************************************
929 * FtpGetCurrentDirectoryW (WININET.@)
931 * Retrieves the current directory
938 BOOL WINAPI FtpGetCurrentDirectoryW(HINTERNET hFtpSession, LPWSTR lpszCurrentDirectory,
939 LPDWORD lpdwCurrentDirectory)
941 LPWININETFTPSESSIONW lpwfs;
942 LPWININETAPPINFOW hIC = NULL;
945 TRACE("%p %p %p\n", hFtpSession, lpszCurrentDirectory, lpdwCurrentDirectory);
947 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
950 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
954 if (WH_HFTPSESSION != lpwfs->hdr.htype)
956 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
960 if (!lpdwCurrentDirectory)
962 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
966 if (lpszCurrentDirectory == NULL)
968 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
972 if (lpwfs->download_in_progress != NULL)
974 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
978 hIC = lpwfs->lpAppInfo;
979 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
981 WORKREQUEST workRequest;
982 struct WORKREQ_FTPGETCURRENTDIRECTORYW *req;
984 workRequest.asyncproc = AsyncFtpGetCurrentDirectoryProc;
985 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
986 req = &workRequest.u.FtpGetCurrentDirectoryW;
987 req->lpszDirectory = lpszCurrentDirectory;
988 req->lpdwDirectory = lpdwCurrentDirectory;
990 r = INTERNET_AsyncCall(&workRequest);
994 r = FTP_FtpGetCurrentDirectoryW(lpwfs, lpszCurrentDirectory,
995 lpdwCurrentDirectory);
1000 WININET_Release( &lpwfs->hdr );
1006 /***********************************************************************
1007 * FTP_FtpGetCurrentDirectoryW (Internal)
1009 * Retrieves the current directory
1016 static BOOL FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPWSTR lpszCurrentDirectory,
1017 LPDWORD lpdwCurrentDirectory)
1020 LPWININETAPPINFOW hIC = NULL;
1021 DWORD bSuccess = FALSE;
1023 /* Clear any error information */
1024 INTERNET_SetLastError(0);
1026 hIC = lpwfs->lpAppInfo;
1027 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PWD, NULL,
1028 lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
1031 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1034 if (nResCode == 257) /* Extract directory name */
1036 DWORD firstpos, lastpos, len;
1037 LPWSTR lpszResponseBuffer = WININET_strdup_AtoW(INTERNET_GetResponseBuffer());
1039 for (firstpos = 0, lastpos = 0; lpszResponseBuffer[lastpos]; lastpos++)
1041 if ('"' == lpszResponseBuffer[lastpos])
1049 len = lastpos - firstpos;
1050 if (*lpdwCurrentDirectory >= len)
1052 memcpy(lpszCurrentDirectory, &lpszResponseBuffer[firstpos + 1], len * sizeof(WCHAR));
1053 lpszCurrentDirectory[len - 1] = 0;
1054 *lpdwCurrentDirectory = len;
1057 else INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1059 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer);
1062 FTP_SetResponseError(nResCode);
1066 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1068 INTERNET_ASYNC_RESULT iar;
1070 iar.dwResult = bSuccess;
1071 iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
1072 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1073 &iar, sizeof(INTERNET_ASYNC_RESULT));
1080 /***********************************************************************
1081 * FTPFILE_Destroy(internal)
1083 * Closes the file transfer handle. This also 'cleans' the data queue of
1084 * the 'transfer complete' message (this is a bit of a hack though :-/ )
1087 static void FTPFILE_Destroy(WININETHANDLEHEADER *hdr)
1089 LPWININETFTPFILE lpwh = (LPWININETFTPFILE) hdr;
1090 LPWININETFTPSESSIONW lpwfs = lpwh->lpFtpSession;
1095 WININET_Release(&lpwh->lpFtpSession->hdr);
1097 if (!lpwh->session_deleted)
1098 lpwfs->download_in_progress = NULL;
1100 if (lpwh->nDataSocket != -1)
1101 closesocket(lpwh->nDataSocket);
1103 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1104 if (nResCode > 0 && nResCode != 226) WARN("server reports failed transfer\n");
1106 HeapFree(GetProcessHeap(), 0, lpwh);
1109 static DWORD FTPFILE_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1112 case INTERNET_OPTION_HANDLE_TYPE:
1113 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1115 if (*size < sizeof(ULONG))
1116 return ERROR_INSUFFICIENT_BUFFER;
1118 *size = sizeof(DWORD);
1119 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_FTP_FILE;
1120 return ERROR_SUCCESS;
1123 return INET_QueryOption(option, buffer, size, unicode);
1126 static DWORD FTPFILE_ReadFile(WININETHANDLEHEADER *hdr, void *buffer, DWORD size, DWORD *read)
1128 WININETFTPFILE *file = (WININETFTPFILE*)hdr;
1131 if (file->nDataSocket == -1)
1132 return ERROR_INTERNET_DISCONNECTED;
1134 /* FIXME: FTP should use NETCON_ stuff */
1135 res = recv(file->nDataSocket, buffer, size, MSG_WAITALL);
1136 *read = res>0 ? res : 0;
1138 return res>=0 ? ERROR_SUCCESS : INTERNET_ERROR_BASE; /* FIXME*/
1141 static BOOL FTPFILE_WriteFile(WININETHANDLEHEADER *hdr, const void *buffer, DWORD size, DWORD *written)
1143 LPWININETFTPFILE lpwh = (LPWININETFTPFILE) hdr;
1146 res = send(lpwh->nDataSocket, buffer, size, 0);
1148 *written = res>0 ? res : 0;
1152 static void FTP_ReceiveRequestData(WININETFTPFILE *file, BOOL first_notif)
1154 INTERNET_ASYNC_RESULT iar;
1158 TRACE("%p\n", file);
1160 available = recv(file->nDataSocket, buffer, sizeof(buffer), MSG_PEEK);
1162 if(available != -1) {
1163 iar.dwResult = (DWORD_PTR)file->hdr.hInternet;
1164 iar.dwError = first_notif ? 0 : available;
1167 iar.dwError = INTERNET_GetLastError();
1170 INTERNET_SendCallback(&file->hdr, file->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1171 sizeof(INTERNET_ASYNC_RESULT));
1174 static void FTPFILE_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1176 WININETFTPFILE *file = (WININETFTPFILE*)workRequest->hdr;
1178 FTP_ReceiveRequestData(file, FALSE);
1181 static DWORD FTPFILE_QueryDataAvailable(WININETHANDLEHEADER *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1183 LPWININETFTPFILE file = (LPWININETFTPFILE) hdr;
1184 int retval, unread = 0;
1186 TRACE("(%p %p %x %lx)\n", file, available, flags, ctx);
1189 retval = ioctlsocket(file->nDataSocket, FIONREAD, &unread);
1191 TRACE("%d bytes of queued, but unread data\n", unread);
1193 FIXME("FIONREAD not available\n");
1196 *available = unread;
1203 retval = recv(file->nDataSocket, &byte, 1, MSG_PEEK);
1205 WORKREQUEST workRequest;
1208 workRequest.asyncproc = FTPFILE_AsyncQueryDataAvailableProc;
1209 workRequest.hdr = WININET_AddRef( &file->hdr );
1211 INTERNET_AsyncCall(&workRequest);
1213 return ERROR_IO_PENDING;
1217 return ERROR_SUCCESS;
1221 static const HANDLEHEADERVtbl FTPFILEVtbl = {
1224 FTPFILE_QueryOption,
1230 FTPFILE_QueryDataAvailable,
1234 /***********************************************************************
1235 * FTP_FtpOpenFileW (Internal)
1237 * Open a remote file for writing or reading
1240 * HINTERNET handle on success
1244 HINTERNET FTP_FtpOpenFileW(LPWININETFTPSESSIONW lpwfs,
1245 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1246 DWORD_PTR dwContext)
1249 BOOL bSuccess = FALSE;
1250 LPWININETFTPFILE lpwh = NULL;
1251 LPWININETAPPINFOW hIC = NULL;
1252 HINTERNET handle = NULL;
1256 /* Clear any error information */
1257 INTERNET_SetLastError(0);
1259 if (GENERIC_READ == fdwAccess)
1261 /* Set up socket to retrieve data */
1262 bSuccess = FTP_SendRetrieve(lpwfs, lpszFileName, dwFlags);
1264 else if (GENERIC_WRITE == fdwAccess)
1266 /* Set up socket to send data */
1267 bSuccess = FTP_SendStore(lpwfs, lpszFileName, dwFlags);
1270 /* Get data socket to server */
1271 if (bSuccess && FTP_GetDataSocket(lpwfs, &nDataSocket))
1273 lpwh = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPFILE));
1274 lpwh->hdr.htype = WH_HFILE;
1275 lpwh->hdr.vtbl = &FTPFILEVtbl;
1276 lpwh->hdr.dwFlags = dwFlags;
1277 lpwh->hdr.dwContext = dwContext;
1279 lpwh->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
1280 lpwh->nDataSocket = nDataSocket;
1281 lpwh->session_deleted = FALSE;
1283 WININET_AddRef( &lpwfs->hdr );
1284 lpwh->lpFtpSession = lpwfs;
1285 list_add_head( &lpwfs->hdr.children, &lpwh->hdr.entry );
1287 handle = WININET_AllocHandle( &lpwh->hdr );
1291 /* Indicate that a download is currently in progress */
1292 lpwfs->download_in_progress = lpwh;
1295 if (lpwfs->lstnSocket != -1)
1296 closesocket(lpwfs->lstnSocket);
1298 hIC = lpwfs->lpAppInfo;
1299 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1301 INTERNET_ASYNC_RESULT iar;
1305 iar.dwResult = (DWORD_PTR)handle;
1306 iar.dwError = ERROR_SUCCESS;
1307 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_HANDLE_CREATED,
1308 &iar, sizeof(INTERNET_ASYNC_RESULT));
1312 FTP_ReceiveRequestData(lpwh, TRUE);
1315 iar.dwError = INTERNET_GetLastError();
1316 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1317 &iar, sizeof(INTERNET_ASYNC_RESULT));
1323 WININET_Release( &lpwh->hdr );
1329 /***********************************************************************
1330 * FtpOpenFileA (WININET.@)
1332 * Open a remote file for writing or reading
1335 * HINTERNET handle on success
1339 HINTERNET WINAPI FtpOpenFileA(HINTERNET hFtpSession,
1340 LPCSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1341 DWORD_PTR dwContext)
1343 LPWSTR lpwzFileName;
1346 lpwzFileName = lpszFileName?WININET_strdup_AtoW(lpszFileName):NULL;
1347 ret = FtpOpenFileW(hFtpSession, lpwzFileName, fdwAccess, dwFlags, dwContext);
1348 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1353 static void AsyncFtpOpenFileProc(WORKREQUEST *workRequest)
1355 struct WORKREQ_FTPOPENFILEW const *req = &workRequest->u.FtpOpenFileW;
1356 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1358 TRACE("%p\n", lpwfs);
1360 FTP_FtpOpenFileW(lpwfs, req->lpszFilename,
1361 req->dwAccess, req->dwFlags, req->dwContext);
1362 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1365 /***********************************************************************
1366 * FtpOpenFileW (WININET.@)
1368 * Open a remote file for writing or reading
1371 * HINTERNET handle on success
1375 HINTERNET WINAPI FtpOpenFileW(HINTERNET hFtpSession,
1376 LPCWSTR lpszFileName, DWORD fdwAccess, DWORD dwFlags,
1377 DWORD_PTR dwContext)
1379 LPWININETFTPSESSIONW lpwfs;
1380 LPWININETAPPINFOW hIC = NULL;
1383 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession,
1384 debugstr_w(lpszFileName), fdwAccess, dwFlags, dwContext);
1386 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1389 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1393 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1395 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1399 if ((!lpszFileName) ||
1400 ((fdwAccess != GENERIC_READ) && (fdwAccess != GENERIC_WRITE)) ||
1401 ((dwFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY))
1403 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1407 if (lpwfs->download_in_progress != NULL)
1409 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1413 hIC = lpwfs->lpAppInfo;
1414 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1416 WORKREQUEST workRequest;
1417 struct WORKREQ_FTPOPENFILEW *req;
1419 workRequest.asyncproc = AsyncFtpOpenFileProc;
1420 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1421 req = &workRequest.u.FtpOpenFileW;
1422 req->lpszFilename = WININET_strdupW(lpszFileName);
1423 req->dwAccess = fdwAccess;
1424 req->dwFlags = dwFlags;
1425 req->dwContext = dwContext;
1427 INTERNET_AsyncCall(&workRequest);
1432 r = FTP_FtpOpenFileW(lpwfs, lpszFileName, fdwAccess, dwFlags, dwContext);
1436 WININET_Release( &lpwfs->hdr );
1442 /***********************************************************************
1443 * FtpGetFileA (WININET.@)
1445 * Retrieve file from the FTP server
1452 BOOL WINAPI FtpGetFileA(HINTERNET hInternet, LPCSTR lpszRemoteFile, LPCSTR lpszNewFile,
1453 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1454 DWORD_PTR dwContext)
1456 LPWSTR lpwzRemoteFile;
1460 lpwzRemoteFile = lpszRemoteFile?WININET_strdup_AtoW(lpszRemoteFile):NULL;
1461 lpwzNewFile = lpszNewFile?WININET_strdup_AtoW(lpszNewFile):NULL;
1462 ret = FtpGetFileW(hInternet, lpwzRemoteFile, lpwzNewFile, fFailIfExists,
1463 dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1464 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile);
1465 HeapFree(GetProcessHeap(), 0, lpwzNewFile);
1470 static void AsyncFtpGetFileProc(WORKREQUEST *workRequest)
1472 struct WORKREQ_FTPGETFILEW const *req = &workRequest->u.FtpGetFileW;
1473 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1475 TRACE("%p\n", lpwfs);
1477 FTP_FtpGetFileW(lpwfs, req->lpszRemoteFile,
1478 req->lpszNewFile, req->fFailIfExists,
1479 req->dwLocalFlagsAttribute, req->dwFlags, req->dwContext);
1480 HeapFree(GetProcessHeap(), 0, req->lpszRemoteFile);
1481 HeapFree(GetProcessHeap(), 0, req->lpszNewFile);
1485 /***********************************************************************
1486 * FtpGetFileW (WININET.@)
1488 * Retrieve file from the FTP server
1495 BOOL WINAPI FtpGetFileW(HINTERNET hInternet, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1496 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1497 DWORD_PTR dwContext)
1499 LPWININETFTPSESSIONW lpwfs;
1500 LPWININETAPPINFOW hIC = NULL;
1503 if (!lpszRemoteFile || !lpszNewFile)
1505 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1509 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hInternet );
1512 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1516 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1518 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1522 if ((dwInternetFlags & FTP_CONDITION_MASK) > FTP_TRANSFER_TYPE_BINARY)
1524 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1528 if (lpwfs->download_in_progress != NULL)
1530 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1534 hIC = lpwfs->lpAppInfo;
1535 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1537 WORKREQUEST workRequest;
1538 struct WORKREQ_FTPGETFILEW *req;
1540 workRequest.asyncproc = AsyncFtpGetFileProc;
1541 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1542 req = &workRequest.u.FtpGetFileW;
1543 req->lpszRemoteFile = WININET_strdupW(lpszRemoteFile);
1544 req->lpszNewFile = WININET_strdupW(lpszNewFile);
1545 req->dwLocalFlagsAttribute = dwLocalFlagsAttribute;
1546 req->fFailIfExists = fFailIfExists;
1547 req->dwFlags = dwInternetFlags;
1548 req->dwContext = dwContext;
1550 r = INTERNET_AsyncCall(&workRequest);
1554 r = FTP_FtpGetFileW(lpwfs, lpszRemoteFile, lpszNewFile,
1555 fFailIfExists, dwLocalFlagsAttribute, dwInternetFlags, dwContext);
1559 WININET_Release( &lpwfs->hdr );
1565 /***********************************************************************
1566 * FTP_FtpGetFileW (Internal)
1568 * Retrieve file from the FTP server
1575 static BOOL FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, LPCWSTR lpszNewFile,
1576 BOOL fFailIfExists, DWORD dwLocalFlagsAttribute, DWORD dwInternetFlags,
1577 DWORD_PTR dwContext)
1579 BOOL bSuccess = FALSE;
1581 LPWININETAPPINFOW hIC = NULL;
1583 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile), debugstr_w(lpszNewFile));
1585 /* Clear any error information */
1586 INTERNET_SetLastError(0);
1588 /* Ensure we can write to lpszNewfile by opening it */
1589 hFile = CreateFileW(lpszNewFile, GENERIC_WRITE, 0, 0, fFailIfExists ?
1590 CREATE_NEW : CREATE_ALWAYS, dwLocalFlagsAttribute, 0);
1591 if (INVALID_HANDLE_VALUE == hFile)
1594 /* Set up socket to retrieve data */
1595 if (FTP_SendRetrieve(lpwfs, lpszRemoteFile, dwInternetFlags))
1599 /* Get data socket to server */
1600 if (FTP_GetDataSocket(lpwfs, &nDataSocket))
1605 FTP_RetrieveFileData(lpwfs, nDataSocket, hFile);
1606 closesocket(nDataSocket);
1608 nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
1611 if (nResCode == 226)
1614 FTP_SetResponseError(nResCode);
1619 if (lpwfs->lstnSocket != -1)
1620 closesocket(lpwfs->lstnSocket);
1624 hIC = lpwfs->lpAppInfo;
1625 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1627 INTERNET_ASYNC_RESULT iar;
1629 iar.dwResult = (DWORD)bSuccess;
1630 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1631 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1632 &iar, sizeof(INTERNET_ASYNC_RESULT));
1635 if (!bSuccess) DeleteFileW(lpszNewFile);
1639 /***********************************************************************
1640 * FtpGetFileSize (WININET.@)
1642 DWORD WINAPI FtpGetFileSize( HINTERNET hFile, LPDWORD lpdwFileSizeHigh )
1644 FIXME("(%p, %p)\n", hFile, lpdwFileSizeHigh);
1646 if (lpdwFileSizeHigh)
1647 *lpdwFileSizeHigh = 0;
1652 /***********************************************************************
1653 * FtpDeleteFileA (WININET.@)
1655 * Delete a file on the ftp server
1662 BOOL WINAPI FtpDeleteFileA(HINTERNET hFtpSession, LPCSTR lpszFileName)
1664 LPWSTR lpwzFileName;
1667 lpwzFileName = lpszFileName?WININET_strdup_AtoW(lpszFileName):NULL;
1668 ret = FtpDeleteFileW(hFtpSession, lpwzFileName);
1669 HeapFree(GetProcessHeap(), 0, lpwzFileName);
1673 static void AsyncFtpDeleteFileProc(WORKREQUEST *workRequest)
1675 struct WORKREQ_FTPDELETEFILEW const *req = &workRequest->u.FtpDeleteFileW;
1676 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1678 TRACE("%p\n", lpwfs);
1680 FTP_FtpDeleteFileW(lpwfs, req->lpszFilename);
1681 HeapFree(GetProcessHeap(), 0, req->lpszFilename);
1684 /***********************************************************************
1685 * FtpDeleteFileW (WININET.@)
1687 * Delete a file on the ftp server
1694 BOOL WINAPI FtpDeleteFileW(HINTERNET hFtpSession, LPCWSTR lpszFileName)
1696 LPWININETFTPSESSIONW lpwfs;
1697 LPWININETAPPINFOW hIC = NULL;
1700 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1703 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1707 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1709 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1713 if (lpwfs->download_in_progress != NULL)
1715 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1721 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1725 hIC = lpwfs->lpAppInfo;
1726 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1728 WORKREQUEST workRequest;
1729 struct WORKREQ_FTPDELETEFILEW *req;
1731 workRequest.asyncproc = AsyncFtpDeleteFileProc;
1732 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1733 req = &workRequest.u.FtpDeleteFileW;
1734 req->lpszFilename = WININET_strdupW(lpszFileName);
1736 r = INTERNET_AsyncCall(&workRequest);
1740 r = FTP_FtpDeleteFileW(lpwfs, lpszFileName);
1744 WININET_Release( &lpwfs->hdr );
1749 /***********************************************************************
1750 * FTP_FtpDeleteFileW (Internal)
1752 * Delete a file on the ftp server
1759 BOOL FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszFileName)
1762 BOOL bSuccess = FALSE;
1763 LPWININETAPPINFOW hIC = NULL;
1765 TRACE("%p\n", lpwfs);
1767 /* Clear any error information */
1768 INTERNET_SetLastError(0);
1770 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_DELE, lpszFileName, 0, 0, 0))
1773 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1776 if (nResCode == 250)
1779 FTP_SetResponseError(nResCode);
1782 hIC = lpwfs->lpAppInfo;
1783 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1785 INTERNET_ASYNC_RESULT iar;
1787 iar.dwResult = (DWORD)bSuccess;
1788 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1789 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1790 &iar, sizeof(INTERNET_ASYNC_RESULT));
1797 /***********************************************************************
1798 * FtpRemoveDirectoryA (WININET.@)
1800 * Remove a directory on the ftp server
1807 BOOL WINAPI FtpRemoveDirectoryA(HINTERNET hFtpSession, LPCSTR lpszDirectory)
1809 LPWSTR lpwzDirectory;
1812 lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
1813 ret = FtpRemoveDirectoryW(hFtpSession, lpwzDirectory);
1814 HeapFree(GetProcessHeap(), 0, lpwzDirectory);
1818 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST *workRequest)
1820 struct WORKREQ_FTPREMOVEDIRECTORYW const *req = &workRequest->u.FtpRemoveDirectoryW;
1821 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1823 TRACE("%p\n", lpwfs);
1825 FTP_FtpRemoveDirectoryW(lpwfs, req->lpszDirectory);
1826 HeapFree(GetProcessHeap(), 0, req->lpszDirectory);
1829 /***********************************************************************
1830 * FtpRemoveDirectoryW (WININET.@)
1832 * Remove a directory on the ftp server
1839 BOOL WINAPI FtpRemoveDirectoryW(HINTERNET hFtpSession, LPCWSTR lpszDirectory)
1841 LPWININETFTPSESSIONW lpwfs;
1842 LPWININETAPPINFOW hIC = NULL;
1845 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1848 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
1852 if (WH_HFTPSESSION != lpwfs->hdr.htype)
1854 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1858 if (lpwfs->download_in_progress != NULL)
1860 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
1866 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1870 hIC = lpwfs->lpAppInfo;
1871 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1873 WORKREQUEST workRequest;
1874 struct WORKREQ_FTPREMOVEDIRECTORYW *req;
1876 workRequest.asyncproc = AsyncFtpRemoveDirectoryProc;
1877 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
1878 req = &workRequest.u.FtpRemoveDirectoryW;
1879 req->lpszDirectory = WININET_strdupW(lpszDirectory);
1881 r = INTERNET_AsyncCall(&workRequest);
1885 r = FTP_FtpRemoveDirectoryW(lpwfs, lpszDirectory);
1889 WININET_Release( &lpwfs->hdr );
1894 /***********************************************************************
1895 * FTP_FtpRemoveDirectoryW (Internal)
1897 * Remove a directory on the ftp server
1904 BOOL FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
1907 BOOL bSuccess = FALSE;
1908 LPWININETAPPINFOW hIC = NULL;
1912 /* Clear any error information */
1913 INTERNET_SetLastError(0);
1915 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RMD, lpszDirectory, 0, 0, 0))
1918 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
1921 if (nResCode == 250)
1924 FTP_SetResponseError(nResCode);
1928 hIC = lpwfs->lpAppInfo;
1929 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1931 INTERNET_ASYNC_RESULT iar;
1933 iar.dwResult = (DWORD)bSuccess;
1934 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1935 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
1936 &iar, sizeof(INTERNET_ASYNC_RESULT));
1943 /***********************************************************************
1944 * FtpRenameFileA (WININET.@)
1946 * Rename a file on the ftp server
1953 BOOL WINAPI FtpRenameFileA(HINTERNET hFtpSession, LPCSTR lpszSrc, LPCSTR lpszDest)
1959 lpwzSrc = lpszSrc?WININET_strdup_AtoW(lpszSrc):NULL;
1960 lpwzDest = lpszDest?WININET_strdup_AtoW(lpszDest):NULL;
1961 ret = FtpRenameFileW(hFtpSession, lpwzSrc, lpwzDest);
1962 HeapFree(GetProcessHeap(), 0, lpwzSrc);
1963 HeapFree(GetProcessHeap(), 0, lpwzDest);
1967 static void AsyncFtpRenameFileProc(WORKREQUEST *workRequest)
1969 struct WORKREQ_FTPRENAMEFILEW const *req = &workRequest->u.FtpRenameFileW;
1970 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) workRequest->hdr;
1972 TRACE("%p\n", lpwfs);
1974 FTP_FtpRenameFileW(lpwfs, req->lpszSrcFile, req->lpszDestFile);
1975 HeapFree(GetProcessHeap(), 0, req->lpszSrcFile);
1976 HeapFree(GetProcessHeap(), 0, req->lpszDestFile);
1979 /***********************************************************************
1980 * FtpRenameFileW (WININET.@)
1982 * Rename a file on the ftp server
1989 BOOL WINAPI FtpRenameFileW(HINTERNET hFtpSession, LPCWSTR lpszSrc, LPCWSTR lpszDest)
1991 LPWININETFTPSESSIONW lpwfs;
1992 LPWININETAPPINFOW hIC = NULL;
1995 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hFtpSession );
1998 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2002 if (WH_HFTPSESSION != lpwfs->hdr.htype)
2004 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2008 if (lpwfs->download_in_progress != NULL)
2010 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
2014 if (!lpszSrc || !lpszDest)
2016 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2020 hIC = lpwfs->lpAppInfo;
2021 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2023 WORKREQUEST workRequest;
2024 struct WORKREQ_FTPRENAMEFILEW *req;
2026 workRequest.asyncproc = AsyncFtpRenameFileProc;
2027 workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
2028 req = &workRequest.u.FtpRenameFileW;
2029 req->lpszSrcFile = WININET_strdupW(lpszSrc);
2030 req->lpszDestFile = WININET_strdupW(lpszDest);
2032 r = INTERNET_AsyncCall(&workRequest);
2036 r = FTP_FtpRenameFileW(lpwfs, lpszSrc, lpszDest);
2040 WININET_Release( &lpwfs->hdr );
2045 /***********************************************************************
2046 * FTP_FtpRenameFileW (Internal)
2048 * Rename a file on the ftp server
2055 BOOL FTP_FtpRenameFileW( LPWININETFTPSESSIONW lpwfs,
2056 LPCWSTR lpszSrc, LPCWSTR lpszDest)
2059 BOOL bSuccess = FALSE;
2060 LPWININETAPPINFOW hIC = NULL;
2064 /* Clear any error information */
2065 INTERNET_SetLastError(0);
2067 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNFR, lpszSrc, 0, 0, 0))
2070 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2071 if (nResCode == 350)
2073 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RNTO, lpszDest, 0, 0, 0))
2076 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2079 if (nResCode == 250)
2082 FTP_SetResponseError(nResCode);
2085 hIC = lpwfs->lpAppInfo;
2086 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2088 INTERNET_ASYNC_RESULT iar;
2090 iar.dwResult = (DWORD)bSuccess;
2091 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2092 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
2093 &iar, sizeof(INTERNET_ASYNC_RESULT));
2099 /***********************************************************************
2100 * FtpCommandA (WININET.@)
2102 BOOL WINAPI FtpCommandA( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
2103 LPCSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
2108 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
2109 debugstr_a(lpszCommand), dwContext, phFtpCommand);
2111 if (fExpectResponse)
2113 FIXME("data connection not supported\n");
2117 if (!lpszCommand || !lpszCommand[0])
2119 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2123 if (!(cmdW = WININET_strdup_AtoW(lpszCommand)))
2125 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2129 r = FtpCommandW(hConnect, fExpectResponse, dwFlags, cmdW, dwContext, phFtpCommand);
2131 HeapFree(GetProcessHeap(), 0, cmdW);
2135 /***********************************************************************
2136 * FtpCommandW (WININET.@)
2138 BOOL WINAPI FtpCommandW( HINTERNET hConnect, BOOL fExpectResponse, DWORD dwFlags,
2139 LPCWSTR lpszCommand, DWORD_PTR dwContext, HINTERNET* phFtpCommand )
2142 LPWININETFTPSESSIONW lpwfs;
2144 DWORD len, nBytesSent= 0;
2145 INT nResCode, nRC = 0;
2147 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect, fExpectResponse, dwFlags,
2148 debugstr_w(lpszCommand), dwContext, phFtpCommand);
2150 if (!lpszCommand || !lpszCommand[0])
2152 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2156 if (fExpectResponse)
2158 FIXME("data connection not supported\n");
2162 lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
2165 INTERNET_SetLastError(ERROR_INVALID_HANDLE);
2169 if (WH_HFTPSESSION != lpwfs->hdr.htype)
2171 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2175 if (lpwfs->download_in_progress != NULL)
2177 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS);
2181 len = WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, NULL, 0, NULL, NULL) + strlen(szCRLF);
2182 if ((cmd = HeapAlloc(GetProcessHeap(), 0, len )))
2183 WideCharToMultiByte(CP_ACP, 0, lpszCommand, -1, cmd, len, NULL, NULL);
2186 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2190 strcat(cmd, szCRLF);
2193 TRACE("Sending (%s) len(%d)\n", cmd, len);
2194 while ((nBytesSent < len) && (nRC != -1))
2196 nRC = send(lpwfs->sndSocket, cmd + nBytesSent, len - nBytesSent, 0);
2200 TRACE("Sent %d bytes\n", nRC);
2206 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2207 if (nResCode > 0 && nResCode < 400)
2210 FTP_SetResponseError(nResCode);
2214 WININET_Release( &lpwfs->hdr );
2215 HeapFree(GetProcessHeap(), 0, cmd);
2220 /***********************************************************************
2221 * FTPSESSION_Destroy (internal)
2223 * Deallocate session handle
2225 static void FTPSESSION_Destroy(WININETHANDLEHEADER *hdr)
2227 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) hdr;
2231 WININET_Release(&lpwfs->lpAppInfo->hdr);
2233 HeapFree(GetProcessHeap(), 0, lpwfs->lpszPassword);
2234 HeapFree(GetProcessHeap(), 0, lpwfs->lpszUserName);
2235 HeapFree(GetProcessHeap(), 0, lpwfs);
2238 static void FTPSESSION_CloseConnection(WININETHANDLEHEADER *hdr)
2240 LPWININETFTPSESSIONW lpwfs = (LPWININETFTPSESSIONW) hdr;
2244 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
2245 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2247 if (lpwfs->download_in_progress != NULL)
2248 lpwfs->download_in_progress->session_deleted = TRUE;
2250 if (lpwfs->sndSocket != -1)
2251 closesocket(lpwfs->sndSocket);
2253 if (lpwfs->lstnSocket != -1)
2254 closesocket(lpwfs->lstnSocket);
2256 if (lpwfs->pasvSocket != -1)
2257 closesocket(lpwfs->pasvSocket);
2259 SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext,
2260 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2263 static DWORD FTPSESSION_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
2266 case INTERNET_OPTION_HANDLE_TYPE:
2267 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2269 if (*size < sizeof(ULONG))
2270 return ERROR_INSUFFICIENT_BUFFER;
2272 *size = sizeof(DWORD);
2273 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_FTP;
2274 return ERROR_SUCCESS;
2277 return INET_QueryOption(option, buffer, size, unicode);
2280 static const HANDLEHEADERVtbl FTPSESSIONVtbl = {
2282 FTPSESSION_CloseConnection,
2283 FTPSESSION_QueryOption,
2293 /***********************************************************************
2294 * FTP_Connect (internal)
2296 * Connect to a ftp server
2299 * HINTERNET a session handle on success
2304 * Windows uses 'anonymous' as the username, when given a NULL username
2305 * and a NULL password. The password is first looked up in:
2307 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2309 * If this entry is not present it uses the current username as the password.
2313 HINTERNET FTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2314 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2315 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
2316 DWORD dwInternalFlags)
2318 static const WCHAR szKey[] = {'S','o','f','t','w','a','r','e','\\',
2319 'M','i','c','r','o','s','o','f','t','\\',
2320 'W','i','n','d','o','w','s','\\',
2321 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2322 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2323 static const WCHAR szValue[] = {'E','m','a','i','l','N','a','m','e',0};
2324 static const WCHAR szDefaultUsername[] = {'a','n','o','n','y','m','o','u','s','\0'};
2325 static const WCHAR szEmpty[] = {'\0'};
2326 struct sockaddr_in socketAddr;
2329 BOOL bSuccess = FALSE;
2330 LPWININETFTPSESSIONW lpwfs = NULL;
2331 HINTERNET handle = NULL;
2333 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2334 hIC, debugstr_w(lpszServerName),
2335 nServerPort, debugstr_w(lpszUserName), debugstr_w(lpszPassword));
2337 assert( hIC->hdr.htype == WH_HINIT );
2339 if ((!lpszUserName || !*lpszUserName) && lpszPassword && *lpszPassword)
2341 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2345 lpwfs = HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPSESSIONW));
2348 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2352 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
2353 nServerPort = INTERNET_DEFAULT_FTP_PORT;
2355 lpwfs->hdr.htype = WH_HFTPSESSION;
2356 lpwfs->hdr.vtbl = &FTPSESSIONVtbl;
2357 lpwfs->hdr.dwFlags = dwFlags;
2358 lpwfs->hdr.dwContext = dwContext;
2359 lpwfs->hdr.dwInternalFlags = dwInternalFlags;
2360 lpwfs->hdr.refs = 1;
2361 lpwfs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2362 lpwfs->download_in_progress = NULL;
2363 lpwfs->sndSocket = -1;
2364 lpwfs->lstnSocket = -1;
2365 lpwfs->pasvSocket = -1;
2367 WININET_AddRef( &hIC->hdr );
2368 lpwfs->lpAppInfo = hIC;
2369 list_add_head( &hIC->hdr.children, &lpwfs->hdr.entry );
2371 handle = WININET_AllocHandle( &lpwfs->hdr );
2374 ERR("Failed to alloc handle\n");
2375 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2379 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2380 if(strchrW(hIC->lpszProxy, ' '))
2381 FIXME("Several proxies not implemented.\n");
2382 if(hIC->lpszProxyBypass)
2383 FIXME("Proxy bypass is ignored.\n");
2385 if (!lpszUserName || !strlenW(lpszUserName)) {
2387 WCHAR szPassword[MAX_PATH];
2388 DWORD len = sizeof(szPassword);
2390 lpwfs->lpszUserName = WININET_strdupW(szDefaultUsername);
2392 RegOpenKeyW(HKEY_CURRENT_USER, szKey, &key);
2393 if (RegQueryValueExW(key, szValue, NULL, NULL, (LPBYTE)szPassword, &len)) {
2394 /* Nothing in the registry, get the username and use that as the password */
2395 if (!GetUserNameW(szPassword, &len)) {
2396 /* Should never get here, but use an empty password as failsafe */
2397 strcpyW(szPassword, szEmpty);
2402 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword));
2403 lpwfs->lpszPassword = WININET_strdupW(szPassword);
2406 lpwfs->lpszUserName = WININET_strdupW(lpszUserName);
2409 lpwfs->lpszPassword = WININET_strdupW(lpszPassword);
2411 lpwfs->lpszPassword = WININET_strdupW(szEmpty);
2414 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2415 if (!(lpwfs->hdr.dwInternalFlags & INET_OPENURL))
2417 INTERNET_ASYNC_RESULT iar;
2419 iar.dwResult = (DWORD_PTR)handle;
2420 iar.dwError = ERROR_SUCCESS;
2422 SendAsyncCallback(&hIC->hdr, dwContext,
2423 INTERNET_STATUS_HANDLE_CREATED, &iar,
2424 sizeof(INTERNET_ASYNC_RESULT));
2427 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_RESOLVING_NAME,
2428 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2430 if (!GetAddress(lpszServerName, nServerPort, &socketAddr))
2432 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2436 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_NAME_RESOLVED,
2437 (LPWSTR) lpszServerName, strlenW(lpszServerName));
2439 nsocket = socket(AF_INET,SOCK_STREAM,0);
2442 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2446 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTING_TO_SERVER,
2447 &socketAddr, sizeof(struct sockaddr_in));
2449 if (connect(nsocket, (struct sockaddr *)&socketAddr, sizeof(socketAddr)) < 0)
2451 ERR("Unable to connect (%s)\n", strerror(errno));
2452 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT);
2456 TRACE("Connected to server\n");
2457 lpwfs->sndSocket = nsocket;
2458 SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_CONNECTED_TO_SERVER,
2459 &socketAddr, sizeof(struct sockaddr_in));
2461 sock_namelen = sizeof(lpwfs->socketAddress);
2462 getsockname(nsocket, (struct sockaddr *) &lpwfs->socketAddress, &sock_namelen);
2464 if (FTP_ConnectToHost(lpwfs))
2466 TRACE("Successfully logged into server\n");
2472 if (lpwfs) WININET_Release( &lpwfs->hdr );
2474 if (!bSuccess && handle)
2476 WININET_FreeHandle( handle );
2484 /***********************************************************************
2485 * FTP_ConnectToHost (internal)
2487 * Connect to a ftp server
2494 static BOOL FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs)
2497 BOOL bSuccess = FALSE;
2500 FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2502 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_USER, lpwfs->lpszUserName, 0, 0, 0))
2505 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2508 /* Login successful... */
2509 if (nResCode == 230)
2511 /* User name okay, need password... */
2512 else if (nResCode == 331)
2513 bSuccess = FTP_SendPassword(lpwfs);
2514 /* Need account for login... */
2515 else if (nResCode == 332)
2516 bSuccess = FTP_SendAccount(lpwfs);
2518 FTP_SetResponseError(nResCode);
2521 TRACE("Returning %d\n", bSuccess);
2527 /***********************************************************************
2528 * FTP_SendCommandA (internal)
2530 * Send command to server
2537 static BOOL FTP_SendCommandA(INT nSocket, FTP_COMMAND ftpCmd, LPCSTR lpszParam,
2538 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext)
2542 DWORD nBytesSent = 0;
2546 TRACE("%d: (%s) %d\n", ftpCmd, lpszParam, nSocket);
2550 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2553 dwParamLen = lpszParam?strlen(lpszParam)+1:0;
2554 len = dwParamLen + strlen(szFtpCommands[ftpCmd]) + strlen(szCRLF);
2555 if (NULL == (buf = HeapAlloc(GetProcessHeap(), 0, len+1)))
2557 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2560 sprintf(buf, "%s%s%s%s", szFtpCommands[ftpCmd], dwParamLen ? " " : "",
2561 dwParamLen ? lpszParam : "", szCRLF);
2563 TRACE("Sending (%s) len(%d)\n", buf, len);
2564 while((nBytesSent < len) && (nRC != -1))
2566 nRC = send(nSocket, buf+nBytesSent, len - nBytesSent, 0);
2570 HeapFree(GetProcessHeap(), 0, (LPVOID)buf);
2574 lpfnStatusCB(hdr->hInternet, dwContext, INTERNET_STATUS_REQUEST_SENT,
2575 &nBytesSent, sizeof(DWORD));
2578 TRACE("Sent %d bytes\n", nBytesSent);
2582 /***********************************************************************
2583 * FTP_SendCommand (internal)
2585 * Send command to server
2592 static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
2593 INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext)
2596 LPSTR lpszParamA = lpszParam?WININET_strdup_WtoA(lpszParam):NULL;
2597 ret = FTP_SendCommandA(nSocket, ftpCmd, lpszParamA, lpfnStatusCB, hdr, dwContext);
2598 HeapFree(GetProcessHeap(), 0, lpszParamA);
2602 /***********************************************************************
2603 * FTP_ReceiveResponse (internal)
2605 * Receive response from server
2608 * Reply code on success
2612 INT FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs, DWORD_PTR dwContext)
2614 LPSTR lpszResponse = INTERNET_GetResponseBuffer();
2617 char firstprefix[5];
2618 BOOL multiline = FALSE;
2620 TRACE("socket(%d)\n", lpwfs->sndSocket);
2622 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2626 if (!INTERNET_GetNextLine(lpwfs->sndSocket, &nRecv))
2633 if(lpszResponse[3] != '-')
2636 { /* Start of multiline response. Loop until we get "nnn " */
2638 memcpy(firstprefix, lpszResponse, 3);
2639 firstprefix[3] = ' ';
2640 firstprefix[4] = '\0';
2645 if(!memcmp(firstprefix, lpszResponse, 4))
2653 rc = atoi(lpszResponse);
2655 SendAsyncCallback(&lpwfs->hdr, dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2656 &nRecv, sizeof(DWORD));
2660 TRACE("return %d\n", rc);
2665 /***********************************************************************
2666 * FTP_SendPassword (internal)
2668 * Send password to ftp server
2675 static BOOL FTP_SendPassword(LPWININETFTPSESSIONW lpwfs)
2678 BOOL bSuccess = FALSE;
2681 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASS, lpwfs->lpszPassword, 0, 0, 0))
2684 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2687 TRACE("Received reply code %d\n", nResCode);
2688 /* Login successful... */
2689 if (nResCode == 230)
2691 /* Command not implemented, superfluous at the server site... */
2692 /* Need account for login... */
2693 else if (nResCode == 332)
2694 bSuccess = FTP_SendAccount(lpwfs);
2696 FTP_SetResponseError(nResCode);
2700 TRACE("Returning %d\n", bSuccess);
2705 /***********************************************************************
2706 * FTP_SendAccount (internal)
2715 static BOOL FTP_SendAccount(LPWININETFTPSESSIONW lpwfs)
2718 BOOL bSuccess = FALSE;
2721 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_ACCT, szNoAccount, 0, 0, 0))
2724 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2728 FTP_SetResponseError(nResCode);
2735 /***********************************************************************
2736 * FTP_SendStore (internal)
2738 * Send request to upload file to ftp server
2745 static BOOL FTP_SendStore(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
2748 BOOL bSuccess = FALSE;
2751 if (!FTP_InitListenSocket(lpwfs))
2754 if (!FTP_SendType(lpwfs, dwType))
2757 if (!FTP_SendPortOrPasv(lpwfs))
2760 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_STOR, lpszRemoteFile, 0, 0, 0))
2762 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2765 if (nResCode == 150 || nResCode == 125)
2768 FTP_SetResponseError(nResCode);
2772 if (!bSuccess && lpwfs->lstnSocket != -1)
2774 closesocket(lpwfs->lstnSocket);
2775 lpwfs->lstnSocket = -1;
2782 /***********************************************************************
2783 * FTP_InitListenSocket (internal)
2785 * Create a socket to listen for server response
2792 static BOOL FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs)
2794 BOOL bSuccess = FALSE;
2795 socklen_t namelen = sizeof(struct sockaddr_in);
2799 lpwfs->lstnSocket = socket(PF_INET, SOCK_STREAM, 0);
2800 if (lpwfs->lstnSocket == -1)
2802 TRACE("Unable to create listening socket\n");
2806 /* We obtain our ip addr from the name of the command channel socket */
2807 lpwfs->lstnSocketAddress = lpwfs->socketAddress;
2809 /* and get the system to assign us a port */
2810 lpwfs->lstnSocketAddress.sin_port = htons(0);
2812 if (bind(lpwfs->lstnSocket,(struct sockaddr *) &lpwfs->lstnSocketAddress, sizeof(struct sockaddr_in)) == -1)
2814 TRACE("Unable to bind socket\n");
2818 if (listen(lpwfs->lstnSocket, MAX_BACKLOG) == -1)
2820 TRACE("listen failed\n");
2824 if (getsockname(lpwfs->lstnSocket, (struct sockaddr *) &lpwfs->lstnSocketAddress, &namelen) != -1)
2828 if (!bSuccess && lpwfs->lstnSocket != -1)
2830 closesocket(lpwfs->lstnSocket);
2831 lpwfs->lstnSocket = -1;
2838 /***********************************************************************
2839 * FTP_SendType (internal)
2841 * Tell server type of data being transferred
2847 * W98SE doesn't cache the type that's currently set
2848 * (i.e. it sends it always),
2849 * so we probably don't want to do that either.
2851 static BOOL FTP_SendType(LPWININETFTPSESSIONW lpwfs, DWORD dwType)
2854 WCHAR type[] = { 'I','\0' };
2855 BOOL bSuccess = FALSE;
2858 if (dwType & INTERNET_FLAG_TRANSFER_ASCII)
2861 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_TYPE, type, 0, 0, 0))
2864 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext)/100;
2870 FTP_SetResponseError(nResCode);
2878 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2879 /***********************************************************************
2880 * FTP_GetFileSize (internal)
2882 * Retrieves from the server the size of the given file
2889 static BOOL FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD *dwSize)
2892 BOOL bSuccess = FALSE;
2896 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_SIZE, lpszRemoteFile, 0, 0, 0))
2899 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2902 if (nResCode == 213) {
2903 /* Now parses the output to get the actual file size */
2905 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
2907 for (i = 0; (lpszResponseBuffer[i] != ' ') && (lpszResponseBuffer[i] != '\0'); i++) ;
2908 if (lpszResponseBuffer[i] == '\0') return FALSE;
2909 *dwSize = atol(&(lpszResponseBuffer[i + 1]));
2913 FTP_SetResponseError(nResCode);
2923 /***********************************************************************
2924 * FTP_SendPort (internal)
2926 * Tell server which port to use
2933 static BOOL FTP_SendPort(LPWININETFTPSESSIONW lpwfs)
2935 static const WCHAR szIPFormat[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
2937 WCHAR szIPAddress[64];
2938 BOOL bSuccess = FALSE;
2941 sprintfW(szIPAddress, szIPFormat,
2942 lpwfs->lstnSocketAddress.sin_addr.s_addr&0x000000FF,
2943 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x0000FF00)>>8,
2944 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0x00FF0000)>>16,
2945 (lpwfs->lstnSocketAddress.sin_addr.s_addr&0xFF000000)>>24,
2946 lpwfs->lstnSocketAddress.sin_port & 0xFF,
2947 (lpwfs->lstnSocketAddress.sin_port & 0xFF00)>>8);
2949 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PORT, szIPAddress, 0, 0, 0))
2952 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2955 if (nResCode == 200)
2958 FTP_SetResponseError(nResCode);
2966 /***********************************************************************
2967 * FTP_DoPassive (internal)
2969 * Tell server that we want to do passive transfers
2970 * and connect data socket
2977 static BOOL FTP_DoPassive(LPWININETFTPSESSIONW lpwfs)
2980 BOOL bSuccess = FALSE;
2983 if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_PASV, NULL, 0, 0, 0))
2986 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
2989 if (nResCode == 227)
2991 LPSTR lpszResponseBuffer = INTERNET_GetResponseBuffer();
2995 char *pAddr, *pPort;
2997 struct sockaddr_in dataSocketAddress;
2999 p = lpszResponseBuffer+4; /* skip status code */
3000 while (*p != '\0' && (*p < '0' || *p > '9')) p++;
3004 ERR("no address found in response, aborting\n");
3008 if (sscanf(p, "%d,%d,%d,%d,%d,%d", &f[0], &f[1], &f[2], &f[3],
3011 ERR("unknown response address format '%s', aborting\n", p);
3014 for (i=0; i < 6; i++)
3017 dataSocketAddress = lpwfs->socketAddress;
3018 pAddr = (char *)&(dataSocketAddress.sin_addr.s_addr);
3019 pPort = (char *)&(dataSocketAddress.sin_port);
3027 nsocket = socket(AF_INET,SOCK_STREAM,0);
3031 if (connect(nsocket, (struct sockaddr *)&dataSocketAddress, sizeof(dataSocketAddress)))
3033 ERR("can't connect passive FTP data port.\n");
3034 closesocket(nsocket);
3037 lpwfs->pasvSocket = nsocket;
3041 FTP_SetResponseError(nResCode);
3049 static BOOL FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs)
3051 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
3053 if (!FTP_DoPassive(lpwfs))
3058 if (!FTP_SendPort(lpwfs))
3065 /***********************************************************************
3066 * FTP_GetDataSocket (internal)
3068 * Either accepts an incoming data socket connection from the server
3069 * or just returns the already opened socket after a PASV command
3070 * in case of passive FTP.
3078 static BOOL FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs, LPINT nDataSocket)
3080 struct sockaddr_in saddr;
3081 socklen_t addrlen = sizeof(struct sockaddr);
3084 if (lpwfs->hdr.dwFlags & INTERNET_FLAG_PASSIVE)
3086 *nDataSocket = lpwfs->pasvSocket;
3090 *nDataSocket = accept(lpwfs->lstnSocket, (struct sockaddr *) &saddr, &addrlen);
3091 closesocket(lpwfs->lstnSocket);
3092 lpwfs->lstnSocket = -1;
3094 return *nDataSocket != -1;
3098 /***********************************************************************
3099 * FTP_SendData (internal)
3101 * Send data to the server
3108 static BOOL FTP_SendData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile)
3110 BY_HANDLE_FILE_INFORMATION fi;
3111 DWORD nBytesRead = 0;
3112 DWORD nBytesSent = 0;
3113 DWORD nTotalSent = 0;
3114 DWORD nBytesToSend, nLen;
3116 time_t s_long_time, e_long_time;
3121 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
3123 /* Get the size of the file. */
3124 GetFileInformationByHandle(hFile, &fi);
3129 nBytesToSend = nBytesRead - nBytesSent;
3131 if (nBytesToSend <= 0)
3133 /* Read data from file. */
3135 if (!ReadFile(hFile, lpszBuffer, DATA_PACKET_SIZE, &nBytesRead, 0))
3136 ERR("Failed reading from file\n");
3139 nBytesToSend = nBytesRead;
3144 nLen = DATA_PACKET_SIZE < nBytesToSend ?
3145 DATA_PACKET_SIZE : nBytesToSend;
3146 nRC = send(nDataSocket, lpszBuffer, nLen, 0);
3154 /* Do some computation to display the status. */
3156 nSeconds = e_long_time - s_long_time;
3157 if( nSeconds / 60 > 0 )
3159 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
3160 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds / 60,
3161 nSeconds % 60, (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent );
3165 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
3166 nTotalSent, fi.nFileSizeLow, nTotalSent*100/fi.nFileSizeLow, nSeconds,
3167 (fi.nFileSizeLow - nTotalSent) * nSeconds / nTotalSent);
3169 } while (nRC != -1);
3171 TRACE("file transfer complete!\n");
3173 HeapFree(GetProcessHeap(), 0, lpszBuffer);
3179 /***********************************************************************
3180 * FTP_SendRetrieve (internal)
3182 * Send request to retrieve a file
3185 * Number of bytes to be received on success
3189 static BOOL FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType)
3195 if (!(ret = FTP_InitListenSocket(lpwfs)))
3198 if (!(ret = FTP_SendType(lpwfs, dwType)))
3201 if (!(ret = FTP_SendPortOrPasv(lpwfs)))
3204 if (!(ret = FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_RETR, lpszRemoteFile, 0, 0, 0)))
3207 nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
3208 if ((nResCode != 125) && (nResCode != 150)) {
3209 /* That means that we got an error getting the file. */
3210 FTP_SetResponseError(nResCode);
3215 if (!ret && lpwfs->lstnSocket != -1)
3217 closesocket(lpwfs->lstnSocket);
3218 lpwfs->lstnSocket = -1;
3225 /***********************************************************************
3226 * FTP_RetrieveData (internal)
3228 * Retrieve data from server
3235 static BOOL FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile)
3237 DWORD nBytesWritten;
3243 lpszBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHAR)*DATA_PACKET_SIZE);
3244 if (NULL == lpszBuffer)
3246 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3252 nRC = recv(nDataSocket, lpszBuffer, DATA_PACKET_SIZE, 0);
3255 /* other side closed socket. */
3258 WriteFile(hFile, lpszBuffer, nRC, &nBytesWritten, NULL);
3262 TRACE("Data transfer complete\n");
3265 HeapFree(GetProcessHeap(), 0, lpszBuffer);
3270 /***********************************************************************
3271 * FTPFINDNEXT_Destroy (internal)
3273 * Deallocate session handle
3275 static void FTPFINDNEXT_Destroy(WININETHANDLEHEADER *hdr)
3277 LPWININETFTPFINDNEXTW lpwfn = (LPWININETFTPFINDNEXTW) hdr;
3282 WININET_Release(&lpwfn->lpFtpSession->hdr);
3284 for (i = 0; i < lpwfn->size; i++)
3286 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp[i].lpszName);
3289 HeapFree(GetProcessHeap(), 0, lpwfn->lpafp);
3290 HeapFree(GetProcessHeap(), 0, lpwfn);
3293 static DWORD FTPFINDNEXT_FindNextFileProc(WININETFTPFINDNEXTW *find, LPVOID data)
3295 WIN32_FIND_DATAW *find_data = data;
3296 DWORD res = ERROR_SUCCESS;
3298 TRACE("index(%d) size(%d)\n", find->index, find->size);
3300 ZeroMemory(find_data, sizeof(WIN32_FIND_DATAW));
3302 if (find->index < find->size) {
3303 FTP_ConvertFileProp(&find->lpafp[find->index], find_data);
3306 TRACE("Name: %s\nSize: %d\n", debugstr_w(find_data->cFileName), find_data->nFileSizeLow);
3308 res = ERROR_NO_MORE_FILES;
3311 if (find->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3313 INTERNET_ASYNC_RESULT iar;
3315 iar.dwResult = (res == ERROR_SUCCESS);
3318 INTERNET_SendCallback(&find->hdr, find->hdr.dwContext,
3319 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3320 sizeof(INTERNET_ASYNC_RESULT));
3326 static void FTPFINDNEXT_AsyncFindNextFileProc(WORKREQUEST *workRequest)
3328 struct WORKREQ_FTPFINDNEXTW *req = &workRequest->u.FtpFindNextW;
3330 FTPFINDNEXT_FindNextFileProc((WININETFTPFINDNEXTW*)workRequest->hdr, req->lpFindFileData);
3333 static DWORD FTPFINDNEXT_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3336 case INTERNET_OPTION_HANDLE_TYPE:
3337 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3339 if (*size < sizeof(ULONG))
3340 return ERROR_INSUFFICIENT_BUFFER;
3342 *size = sizeof(DWORD);
3343 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_FTP_FIND;
3344 return ERROR_SUCCESS;
3347 return INET_QueryOption(option, buffer, size, unicode);
3350 static DWORD FTPFINDNEXT_FindNextFileW(WININETHANDLEHEADER *hdr, void *data)
3352 WININETFTPFINDNEXTW *find = (WININETFTPFINDNEXTW*)hdr;
3354 if (find->lpFtpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3356 WORKREQUEST workRequest;
3357 struct WORKREQ_FTPFINDNEXTW *req;
3359 workRequest.asyncproc = FTPFINDNEXT_AsyncFindNextFileProc;
3360 workRequest.hdr = WININET_AddRef( &find->hdr );
3361 req = &workRequest.u.FtpFindNextW;
3362 req->lpFindFileData = data;
3364 INTERNET_AsyncCall(&workRequest);
3366 return ERROR_SUCCESS;
3369 return FTPFINDNEXT_FindNextFileProc(find, data);
3372 static const HANDLEHEADERVtbl FTPFINDNEXTVtbl = {
3373 FTPFINDNEXT_Destroy,
3375 FTPFINDNEXT_QueryOption,
3382 FTPFINDNEXT_FindNextFileW
3385 /***********************************************************************
3386 * FTP_ReceiveFileList (internal)
3388 * Read file list from server
3391 * Handle to file list on success
3395 static HINTERNET FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3396 LPWIN32_FIND_DATAW lpFindFileData, DWORD_PTR dwContext)
3399 LPFILEPROPERTIESW lpafp = NULL;
3400 LPWININETFTPFINDNEXTW lpwfn = NULL;
3401 HINTERNET handle = 0;
3403 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs, nSocket, debugstr_w(lpszSearchFile), lpFindFileData, dwContext);
3405 if (FTP_ParseDirectory(lpwfs, nSocket, lpszSearchFile, &lpafp, &dwSize))
3408 FTP_ConvertFileProp(lpafp, lpFindFileData);
3410 lpwfn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETFTPFINDNEXTW));
3413 lpwfn->hdr.htype = WH_HFTPFINDNEXT;
3414 lpwfn->hdr.vtbl = &FTPFINDNEXTVtbl;
3415 lpwfn->hdr.dwContext = dwContext;
3416 lpwfn->hdr.refs = 1;
3417 lpwfn->hdr.lpfnStatusCB = lpwfs->hdr.lpfnStatusCB;
3418 lpwfn->index = 1; /* Next index is 1 since we return index 0 */
3419 lpwfn->size = dwSize;
3420 lpwfn->lpafp = lpafp;
3422 WININET_AddRef( &lpwfs->hdr );
3423 lpwfn->lpFtpSession = lpwfs;
3424 list_add_head( &lpwfs->hdr.children, &lpwfn->hdr.entry );
3426 handle = WININET_AllocHandle( &lpwfn->hdr );
3431 WININET_Release( &lpwfn->hdr );
3433 TRACE("Matched %d files\n", dwSize);
3438 /***********************************************************************
3439 * FTP_ConvertFileProp (internal)
3441 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3448 static BOOL FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp, LPWIN32_FIND_DATAW lpFindFileData)
3450 BOOL bSuccess = FALSE;
3452 ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAW));
3456 SystemTimeToFileTime( &lpafp->tmLastModified, &lpFindFileData->ftLastAccessTime );
3457 lpFindFileData->ftLastWriteTime = lpFindFileData->ftLastAccessTime;
3458 lpFindFileData->ftCreationTime = lpFindFileData->ftLastAccessTime;
3460 /* Not all fields are filled in */
3461 lpFindFileData->nFileSizeHigh = 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3462 lpFindFileData->nFileSizeLow = lpafp->nSize;
3464 if (lpafp->bIsDirectory)
3465 lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
3467 if (lpafp->lpszName)
3468 lstrcpynW(lpFindFileData->cFileName, lpafp->lpszName, MAX_PATH);
3476 /***********************************************************************
3477 * FTP_ParseNextFile (internal)
3479 * Parse the next line in file listing
3485 static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW lpfp)
3487 static const char szSpace[] = " \t";
3495 lpfp->lpszName = NULL;
3497 if(!(pszLine = INTERNET_GetNextLine(nSocket, &nBufLen)))
3500 pszToken = strtok(pszLine, szSpace);
3502 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3505 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3507 if(!isdigit(pszToken[0]) && 10 == strlen(pszToken)) {
3508 if(!FTP_ParsePermission(pszToken, lpfp))
3509 lpfp->bIsDirectory = FALSE;
3510 for(i=0; i<=3; i++) {
3511 if(!(pszToken = strtok(NULL, szSpace)))
3514 if(!pszToken) continue;
3515 if(lpfp->bIsDirectory) {
3516 TRACE("Is directory\n");
3520 TRACE("Size: %s\n", pszToken);
3521 lpfp->nSize = atol(pszToken);
3524 lpfp->tmLastModified.wSecond = 0;
3525 lpfp->tmLastModified.wMinute = 0;
3526 lpfp->tmLastModified.wHour = 0;
3527 lpfp->tmLastModified.wDay = 0;
3528 lpfp->tmLastModified.wMonth = 0;
3529 lpfp->tmLastModified.wYear = 0;
3531 /* Determine month */
3532 pszToken = strtok(NULL, szSpace);
3533 if(!pszToken) continue;
3534 if(strlen(pszToken) >= 3) {
3536 if((pszTmp = StrStrIA(szMonths, pszToken)))
3537 lpfp->tmLastModified.wMonth = ((pszTmp - szMonths) / 3)+1;
3540 pszToken = strtok(NULL, szSpace);
3541 if(!pszToken) continue;
3542 lpfp->tmLastModified.wDay = atoi(pszToken);
3543 /* Determine time or year */
3544 pszToken = strtok(NULL, szSpace);
3545 if(!pszToken) continue;
3546 if((pszTmp = strchr(pszToken, ':'))) {
3547 SYSTEMTIME curr_time;
3550 lpfp->tmLastModified.wMinute = atoi(pszTmp);
3551 lpfp->tmLastModified.wHour = atoi(pszToken);
3552 GetLocalTime( &curr_time );
3553 lpfp->tmLastModified.wYear = curr_time.wYear;
3556 lpfp->tmLastModified.wYear = atoi(pszToken);
3557 lpfp->tmLastModified.wHour = 12;
3559 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3560 lpfp->tmLastModified.wHour, lpfp->tmLastModified.wMinute, lpfp->tmLastModified.wSecond,
3561 lpfp->tmLastModified.wYear, lpfp->tmLastModified.wMonth, lpfp->tmLastModified.wDay);
3563 pszToken = strtok(NULL, szSpace);
3564 if(!pszToken) continue;
3565 lpfp->lpszName = WININET_strdup_AtoW(pszToken);
3566 TRACE("File: %s\n", debugstr_w(lpfp->lpszName));
3568 /* NT way of parsing ... :
3570 07-13-03 08:55PM <DIR> sakpatch
3571 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3573 else if(isdigit(pszToken[0]) && 8 == strlen(pszToken)) {
3574 int mon, mday, year, hour, min;
3575 lpfp->permissions = 0xFFFF; /* No idea, put full permission :-) */
3577 sscanf(pszToken, "%d-%d-%d", &mon, &mday, &year);
3578 lpfp->tmLastModified.wDay = mday;
3579 lpfp->tmLastModified.wMonth = mon;
3580 lpfp->tmLastModified.wYear = year;
3582 /* Hacky and bad Y2K protection :-) */
3583 if (lpfp->tmLastModified.wYear < 70) lpfp->tmLastModified.wYear += 2000;
3585 pszToken = strtok(NULL, szSpace);
3586 if(!pszToken) continue;
3587 sscanf(pszToken, "%d:%d", &hour, &min);
3588 lpfp->tmLastModified.wHour = hour;
3589 lpfp->tmLastModified.wMinute = min;
3590 if((pszToken[5] == 'P') && (pszToken[6] == 'M')) {
3591 lpfp->tmLastModified.wHour += 12;
3593 lpfp->tmLastModified.wSecond = 0;
3595 TRACE("Mod time: %02d:%02d:%02d %04d/%02d/%02d\n",
3596 lpfp->tmLastModified.wHour, lpfp->tmLastModified.wMinute, lpfp->tmLastModified.wSecond,
3597 lpfp->tmLastModified.wYear, lpfp->tmLastModified.wMonth, lpfp->tmLastModified.wDay);
3599 pszToken = strtok(NULL, szSpace);
3600 if(!pszToken) continue;
3601 if(!strcasecmp(pszToken, "<DIR>")) {
3602 lpfp->bIsDirectory = TRUE;
3604 TRACE("Is directory\n");
3607 lpfp->bIsDirectory = FALSE;
3608 lpfp->nSize = atol(pszToken);
3609 TRACE("Size: %d\n", lpfp->nSize);
3612 pszToken = strtok(NULL, szSpace);
3613 if(!pszToken) continue;
3614 lpfp->lpszName = WININET_strdup_AtoW(pszToken);
3615 TRACE("Name: %s\n", debugstr_w(lpfp->lpszName));
3617 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3618 else if(pszToken[0] == '+') {
3619 FIXME("EPLF Format not implemented\n");
3622 if(lpfp->lpszName) {
3623 if((lpszSearchFile == NULL) ||
3624 (PathMatchSpecW(lpfp->lpszName, lpszSearchFile))) {
3626 TRACE("Matched: %s\n", debugstr_w(lpfp->lpszName));
3629 HeapFree(GetProcessHeap(), 0, lpfp->lpszName);
3630 lpfp->lpszName = NULL;
3637 /***********************************************************************
3638 * FTP_ParseDirectory (internal)
3640 * Parse string of directory information
3646 static BOOL FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
3647 LPFILEPROPERTIESW *lpafp, LPDWORD dwfp)
3649 BOOL bSuccess = TRUE;
3650 INT sizeFilePropArray = 500;/*20; */
3651 INT indexFilePropArray = -1;
3655 /* Allocate initial file properties array */
3656 *lpafp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FILEPROPERTIESW)*(sizeFilePropArray));
3661 if (indexFilePropArray+1 >= sizeFilePropArray)
3663 LPFILEPROPERTIESW tmpafp;
3665 sizeFilePropArray *= 2;
3666 tmpafp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *lpafp,
3667 sizeof(FILEPROPERTIESW)*sizeFilePropArray);
3676 indexFilePropArray++;
3677 } while (FTP_ParseNextFile(nSocket, lpszSearchFile, &(*lpafp)[indexFilePropArray]));
3679 if (bSuccess && indexFilePropArray)
3681 if (indexFilePropArray < sizeFilePropArray - 1)
3683 LPFILEPROPERTIESW tmpafp;
3685 tmpafp = HeapReAlloc(GetProcessHeap(), 0, *lpafp,
3686 sizeof(FILEPROPERTIESW)*indexFilePropArray);
3690 *dwfp = indexFilePropArray;
3694 HeapFree(GetProcessHeap(), 0, *lpafp);
3695 INTERNET_SetLastError(ERROR_NO_MORE_FILES);
3703 /***********************************************************************
3704 * FTP_ParsePermission (internal)
3706 * Parse permission string of directory information
3713 static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp)
3715 BOOL bSuccess = TRUE;
3716 unsigned short nPermission = 0;
3721 if ((*lpszPermission != 'd') && (*lpszPermission != '-') && (*lpszPermission != 'l'))
3727 lpfp->bIsDirectory = (*lpszPermission == 'd');
3733 nPermission |= (*(lpszPermission+1) == 'r' ? 1 : 0) << 8;
3736 nPermission |= (*(lpszPermission+2) == 'w' ? 1 : 0) << 7;
3739 nPermission |= (*(lpszPermission+3) == 'x' ? 1 : 0) << 6;
3742 nPermission |= (*(lpszPermission+4) == 'r' ? 1 : 0) << 5;
3745 nPermission |= (*(lpszPermission+5) == 'w' ? 1 : 0) << 4;
3748 nPermission |= (*(lpszPermission+6) == 'x' ? 1 : 0) << 3;
3751 nPermission |= (*(lpszPermission+7) == 'r' ? 1 : 0) << 2;
3754 nPermission |= (*(lpszPermission+8) == 'w' ? 1 : 0) << 1;
3757 nPermission |= (*(lpszPermission+9) == 'x' ? 1 : 0);
3761 }while (nPos <= nLast);
3763 lpfp->permissions = nPermission;
3768 /***********************************************************************
3769 * FTP_SetResponseError (internal)
3771 * Set the appropriate error code for a given response from the server
3776 static DWORD FTP_SetResponseError(DWORD dwResponse)
3782 case 425: /* Cannot open data connection. */
3783 dwCode = ERROR_INTERNET_CANNOT_CONNECT;
3786 case 426: /* Connection closed, transer aborted. */
3787 dwCode = ERROR_INTERNET_CONNECTION_ABORTED;
3790 case 530: /* Not logged in. Login incorrect. */
3791 dwCode = ERROR_INTERNET_LOGIN_FAILURE;
3794 case 421: /* Service not available - Server may be shutting down. */
3795 case 450: /* File action not taken. File may be busy. */
3796 case 451: /* Action aborted. Server error. */
3797 case 452: /* Action not taken. Insufficient storage space on server. */
3798 case 500: /* Syntax error. Command unrecognized. */
3799 case 501: /* Syntax error. Error in parameters or arguments. */
3800 case 502: /* Command not implemented. */
3801 case 503: /* Bad sequence of commands. */
3802 case 504: /* Command not implemented for that parameter. */
3803 case 532: /* Need account for storing files */
3804 case 550: /* File action not taken. File not found or no access. */
3805 case 551: /* Requested action aborted. Page type unknown */
3806 case 552: /* Action aborted. Exceeded storage allocation */
3807 case 553: /* Action not taken. File name not allowed. */
3810 dwCode = ERROR_INTERNET_EXTENDED_ERROR;
3814 INTERNET_SetLastError(dwCode);