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"
38 #include <sys/types.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 # include <sys/socket.h>
59 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
64 #define DATA_PACKET_SIZE 0x2000
68 /* Testing shows that Windows only accepts dwFlags where the last
69 * 3 (yes 3) bits define FTP_TRANSFER_TYPE_UNKNOWN, FTP_TRANSFER_TYPE_ASCII or FTP_TRANSFER_TYPE_BINARY.
71 #define FTP_CONDITION_MASK 0x0007
74 /* FTP commands with arguments. */
90 /* FTP commands without arguments. */
99 static const CHAR
*const szFtpCommands
[] = {
122 static const CHAR szMonths
[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
123 static const WCHAR szNoAccount
[] = {'n','o','a','c','c','o','u','n','t','\0'};
125 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr
);
126 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr
);
127 static void FTP_CloseConnection(LPWININETHANDLEHEADER hdr
);
128 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr
);
129 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
130 INTERNET_STATUS_CALLBACK lpfnStatusCB
, LPWININETHANDLEHEADER hdr
, DWORD_PTR dwContext
);
131 static BOOL
FTP_SendStore(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
);
132 static BOOL
FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs
, LPINT nDataSocket
);
133 static BOOL
FTP_SendData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
);
134 static INT
FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs
, DWORD_PTR dwContext
);
135 static BOOL
FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
);
136 static BOOL
FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
);
137 static BOOL
FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs
);
138 static BOOL
FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs
);
139 static BOOL
FTP_SendPassword(LPWININETFTPSESSIONW lpwfs
);
140 static BOOL
FTP_SendAccount(LPWININETFTPSESSIONW lpwfs
);
141 static BOOL
FTP_SendType(LPWININETFTPSESSIONW lpwfs
, DWORD dwType
);
142 static BOOL
FTP_SendPort(LPWININETFTPSESSIONW lpwfs
);
143 static BOOL
FTP_DoPassive(LPWININETFTPSESSIONW lpwfs
);
144 static BOOL
FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs
);
145 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
);
146 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW fileprop
);
147 static BOOL
FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
148 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
);
149 static HINTERNET
FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
150 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
);
151 static DWORD
FTP_SetResponseError(DWORD dwResponse
);
153 /***********************************************************************
154 * FtpPutFileA (WININET.@)
156 * Uploads a file to the FTP server
163 BOOL WINAPI
FtpPutFileA(HINTERNET hConnect
, LPCSTR lpszLocalFile
,
164 LPCSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
166 LPWSTR lpwzLocalFile
;
167 LPWSTR lpwzNewRemoteFile
;
170 lpwzLocalFile
= lpszLocalFile
?WININET_strdup_AtoW(lpszLocalFile
):NULL
;
171 lpwzNewRemoteFile
= lpszNewRemoteFile
?WININET_strdup_AtoW(lpszNewRemoteFile
):NULL
;
172 ret
= FtpPutFileW(hConnect
, lpwzLocalFile
, lpwzNewRemoteFile
,
174 HeapFree(GetProcessHeap(), 0, lpwzLocalFile
);
175 HeapFree(GetProcessHeap(), 0, lpwzNewRemoteFile
);
179 static void AsyncFtpPutFileProc(WORKREQUEST
*workRequest
)
181 struct WORKREQ_FTPPUTFILEW
const *req
= &workRequest
->u
.FtpPutFileW
;
182 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
184 TRACE("%p\n", lpwfs
);
186 FTP_FtpPutFileW(lpwfs
, req
->lpszLocalFile
,
187 req
->lpszNewRemoteFile
, req
->dwFlags
, req
->dwContext
);
189 HeapFree(GetProcessHeap(), 0, req
->lpszLocalFile
);
190 HeapFree(GetProcessHeap(), 0, req
->lpszNewRemoteFile
);
193 /***********************************************************************
194 * FtpPutFileW (WININET.@)
196 * Uploads a file to the FTP server
203 BOOL WINAPI
FtpPutFileW(HINTERNET hConnect
, LPCWSTR lpszLocalFile
,
204 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
206 LPWININETFTPSESSIONW lpwfs
;
207 LPWININETAPPINFOW hIC
= NULL
;
210 if (!lpszLocalFile
|| !lpszNewRemoteFile
)
212 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
216 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
219 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
223 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
225 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
229 if (lpwfs
->download_in_progress
!= NULL
)
231 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
235 if ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
237 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
241 hIC
= lpwfs
->lpAppInfo
;
242 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
244 WORKREQUEST workRequest
;
245 struct WORKREQ_FTPPUTFILEW
*req
= &workRequest
.u
.FtpPutFileW
;
247 workRequest
.asyncproc
= AsyncFtpPutFileProc
;
248 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
249 req
->lpszLocalFile
= WININET_strdupW(lpszLocalFile
);
250 req
->lpszNewRemoteFile
= WININET_strdupW(lpszNewRemoteFile
);
251 req
->dwFlags
= dwFlags
;
252 req
->dwContext
= dwContext
;
254 r
= INTERNET_AsyncCall(&workRequest
);
258 r
= FTP_FtpPutFileW(lpwfs
, lpszLocalFile
,
259 lpszNewRemoteFile
, dwFlags
, dwContext
);
263 WININET_Release( &lpwfs
->hdr
);
268 /***********************************************************************
269 * FTP_FtpPutFileW (Internal)
271 * Uploads a file to the FTP server
278 BOOL WINAPI
FTP_FtpPutFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszLocalFile
,
279 LPCWSTR lpszNewRemoteFile
, DWORD dwFlags
, DWORD_PTR dwContext
)
282 BOOL bSuccess
= FALSE
;
283 LPWININETAPPINFOW hIC
= NULL
;
286 TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile
), debugstr_w(lpszNewRemoteFile
));
288 /* Clear any error information */
289 INTERNET_SetLastError(0);
291 /* Open file to be uploaded */
292 if (INVALID_HANDLE_VALUE
==
293 (hFile
= CreateFileW(lpszLocalFile
, GENERIC_READ
, 0, 0, OPEN_EXISTING
, 0, 0)))
294 /* Let CreateFile set the appropriate error */
297 hIC
= lpwfs
->lpAppInfo
;
299 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
301 if (FTP_SendStore(lpwfs
, lpszNewRemoteFile
, dwFlags
))
305 /* Get data socket to server */
306 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
308 FTP_SendData(lpwfs
, nDataSocket
, hFile
);
309 closesocket(nDataSocket
);
310 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
316 FTP_SetResponseError(nResCode
);
321 if (lpwfs
->lstnSocket
!= -1)
322 closesocket(lpwfs
->lstnSocket
);
324 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
326 INTERNET_ASYNC_RESULT iar
;
328 iar
.dwResult
= (DWORD
)bSuccess
;
329 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
330 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
331 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
340 /***********************************************************************
341 * FtpSetCurrentDirectoryA (WININET.@)
343 * Change the working directory on the FTP server
350 BOOL WINAPI
FtpSetCurrentDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
352 LPWSTR lpwzDirectory
;
355 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
356 ret
= FtpSetCurrentDirectoryW(hConnect
, lpwzDirectory
);
357 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
362 static void AsyncFtpSetCurrentDirectoryProc(WORKREQUEST
*workRequest
)
364 struct WORKREQ_FTPSETCURRENTDIRECTORYW
const *req
= &workRequest
->u
.FtpSetCurrentDirectoryW
;
365 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
367 TRACE("%p\n", lpwfs
);
369 FTP_FtpSetCurrentDirectoryW(lpwfs
, req
->lpszDirectory
);
370 HeapFree(GetProcessHeap(), 0, req
->lpszDirectory
);
373 /***********************************************************************
374 * FtpSetCurrentDirectoryW (WININET.@)
376 * Change the working directory on the FTP server
383 BOOL WINAPI
FtpSetCurrentDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
385 LPWININETFTPSESSIONW lpwfs
= NULL
;
386 LPWININETAPPINFOW hIC
= NULL
;
391 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
395 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
396 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
398 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
402 if (lpwfs
->download_in_progress
!= NULL
)
404 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
408 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
410 hIC
= lpwfs
->lpAppInfo
;
411 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
413 WORKREQUEST workRequest
;
414 struct WORKREQ_FTPSETCURRENTDIRECTORYW
*req
;
416 workRequest
.asyncproc
= AsyncFtpSetCurrentDirectoryProc
;
417 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
418 req
= &workRequest
.u
.FtpSetCurrentDirectoryW
;
419 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
421 r
= INTERNET_AsyncCall(&workRequest
);
425 r
= FTP_FtpSetCurrentDirectoryW(lpwfs
, lpszDirectory
);
430 WININET_Release( &lpwfs
->hdr
);
436 /***********************************************************************
437 * FTP_FtpSetCurrentDirectoryW (Internal)
439 * Change the working directory on the FTP server
446 BOOL WINAPI
FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
449 LPWININETAPPINFOW hIC
= NULL
;
450 DWORD bSuccess
= FALSE
;
452 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
454 /* Clear any error information */
455 INTERNET_SetLastError(0);
457 hIC
= lpwfs
->lpAppInfo
;
458 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_CWD
, lpszDirectory
,
459 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
462 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
469 FTP_SetResponseError(nResCode
);
473 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
475 INTERNET_ASYNC_RESULT iar
;
477 iar
.dwResult
= (DWORD
)bSuccess
;
478 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
479 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
480 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
486 /***********************************************************************
487 * FtpCreateDirectoryA (WININET.@)
489 * Create new directory on the FTP server
496 BOOL WINAPI
FtpCreateDirectoryA(HINTERNET hConnect
, LPCSTR lpszDirectory
)
498 LPWSTR lpwzDirectory
;
501 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
502 ret
= FtpCreateDirectoryW(hConnect
, lpwzDirectory
);
503 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
508 static void AsyncFtpCreateDirectoryProc(WORKREQUEST
*workRequest
)
510 struct WORKREQ_FTPCREATEDIRECTORYW
const *req
= &workRequest
->u
.FtpCreateDirectoryW
;
511 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
513 TRACE(" %p\n", lpwfs
);
515 FTP_FtpCreateDirectoryW(lpwfs
, req
->lpszDirectory
);
516 HeapFree(GetProcessHeap(), 0, req
->lpszDirectory
);
519 /***********************************************************************
520 * FtpCreateDirectoryW (WININET.@)
522 * Create new directory on the FTP server
529 BOOL WINAPI
FtpCreateDirectoryW(HINTERNET hConnect
, LPCWSTR lpszDirectory
)
531 LPWININETFTPSESSIONW lpwfs
;
532 LPWININETAPPINFOW hIC
= NULL
;
535 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
538 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
542 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
544 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
548 if (lpwfs
->download_in_progress
!= NULL
)
550 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
556 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
560 hIC
= lpwfs
->lpAppInfo
;
561 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
563 WORKREQUEST workRequest
;
564 struct WORKREQ_FTPCREATEDIRECTORYW
*req
;
566 workRequest
.asyncproc
= AsyncFtpCreateDirectoryProc
;
567 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
568 req
= &workRequest
.u
.FtpCreateDirectoryW
;
569 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
571 r
= INTERNET_AsyncCall(&workRequest
);
575 r
= FTP_FtpCreateDirectoryW(lpwfs
, lpszDirectory
);
578 WININET_Release( &lpwfs
->hdr
);
584 /***********************************************************************
585 * FTP_FtpCreateDirectoryW (Internal)
587 * Create new directory on the FTP server
594 BOOL WINAPI
FTP_FtpCreateDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
597 BOOL bSuccess
= FALSE
;
598 LPWININETAPPINFOW hIC
= NULL
;
600 TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory
));
602 /* Clear any error information */
603 INTERNET_SetLastError(0);
605 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_MKD
, lpszDirectory
, 0, 0, 0))
608 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
614 FTP_SetResponseError(nResCode
);
618 hIC
= lpwfs
->lpAppInfo
;
619 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
621 INTERNET_ASYNC_RESULT iar
;
623 iar
.dwResult
= (DWORD
)bSuccess
;
624 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
625 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
626 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
632 /***********************************************************************
633 * FtpFindFirstFileA (WININET.@)
635 * Search the specified directory
638 * HINTERNET on success
642 HINTERNET WINAPI
FtpFindFirstFileA(HINTERNET hConnect
,
643 LPCSTR lpszSearchFile
, LPWIN32_FIND_DATAA lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
645 LPWSTR lpwzSearchFile
;
646 WIN32_FIND_DATAW wfd
;
647 LPWIN32_FIND_DATAW lpFindFileDataW
;
650 lpwzSearchFile
= lpszSearchFile
?WININET_strdup_AtoW(lpszSearchFile
):NULL
;
651 lpFindFileDataW
= lpFindFileData
?&wfd
:NULL
;
652 ret
= FtpFindFirstFileW(hConnect
, lpwzSearchFile
, lpFindFileDataW
, dwFlags
, dwContext
);
653 HeapFree(GetProcessHeap(), 0, lpwzSearchFile
);
656 WININET_find_data_WtoA(lpFindFileDataW
, lpFindFileData
);
662 static void AsyncFtpFindFirstFileProc(WORKREQUEST
*workRequest
)
664 struct WORKREQ_FTPFINDFIRSTFILEW
const *req
= &workRequest
->u
.FtpFindFirstFileW
;
665 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
667 TRACE("%p\n", lpwfs
);
669 FTP_FtpFindFirstFileW(lpwfs
, req
->lpszSearchFile
,
670 req
->lpFindFileData
, req
->dwFlags
, req
->dwContext
);
671 HeapFree(GetProcessHeap(), 0, req
->lpszSearchFile
);
674 /***********************************************************************
675 * FtpFindFirstFileW (WININET.@)
677 * Search the specified directory
680 * HINTERNET on success
684 HINTERNET WINAPI
FtpFindFirstFileW(HINTERNET hConnect
,
685 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
687 LPWININETFTPSESSIONW lpwfs
;
688 LPWININETAPPINFOW hIC
= NULL
;
691 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
692 if (NULL
== lpwfs
|| WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
694 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
698 if (lpwfs
->download_in_progress
!= NULL
)
700 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
704 hIC
= lpwfs
->lpAppInfo
;
705 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
707 WORKREQUEST workRequest
;
708 struct WORKREQ_FTPFINDFIRSTFILEW
*req
;
710 workRequest
.asyncproc
= AsyncFtpFindFirstFileProc
;
711 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
712 req
= &workRequest
.u
.FtpFindFirstFileW
;
713 req
->lpszSearchFile
= (lpszSearchFile
== NULL
) ? NULL
: WININET_strdupW(lpszSearchFile
);
714 req
->lpFindFileData
= lpFindFileData
;
715 req
->dwFlags
= dwFlags
;
716 req
->dwContext
= dwContext
;
718 INTERNET_AsyncCall(&workRequest
);
723 r
= FTP_FtpFindFirstFileW(lpwfs
, lpszSearchFile
, lpFindFileData
,
728 WININET_Release( &lpwfs
->hdr
);
734 /***********************************************************************
735 * FTP_FtpFindFirstFileW (Internal)
737 * Search the specified directory
740 * HINTERNET on success
744 HINTERNET WINAPI
FTP_FtpFindFirstFileW(LPWININETFTPSESSIONW lpwfs
,
745 LPCWSTR lpszSearchFile
, LPWIN32_FIND_DATAW lpFindFileData
, DWORD dwFlags
, DWORD_PTR dwContext
)
748 LPWININETAPPINFOW hIC
= NULL
;
749 HINTERNET hFindNext
= NULL
;
753 /* Clear any error information */
754 INTERNET_SetLastError(0);
756 if (!FTP_InitListenSocket(lpwfs
))
759 if (!FTP_SendType(lpwfs
, INTERNET_FLAG_TRANSFER_ASCII
))
762 if (!FTP_SendPortOrPasv(lpwfs
))
765 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_LIST
, NULL
,
766 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
769 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
772 if (nResCode
== 125 || nResCode
== 150)
776 /* Get data socket to server */
777 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
779 hFindNext
= FTP_ReceiveFileList(lpwfs
, nDataSocket
, lpszSearchFile
, lpFindFileData
, dwContext
);
780 closesocket(nDataSocket
);
781 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
782 if (nResCode
!= 226 && nResCode
!= 250)
783 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
787 FTP_SetResponseError(nResCode
);
791 if (lpwfs
->lstnSocket
!= -1)
792 closesocket(lpwfs
->lstnSocket
);
794 hIC
= lpwfs
->lpAppInfo
;
795 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
797 INTERNET_ASYNC_RESULT iar
;
801 iar
.dwResult
= (DWORD
)hFindNext
;
802 iar
.dwError
= ERROR_SUCCESS
;
803 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
804 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
807 iar
.dwResult
= (DWORD
)hFindNext
;
808 iar
.dwError
= hFindNext
? ERROR_SUCCESS
: INTERNET_GetLastError();
809 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
810 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
817 /***********************************************************************
818 * FtpGetCurrentDirectoryA (WININET.@)
820 * Retrieves the current directory
827 BOOL WINAPI
FtpGetCurrentDirectoryA(HINTERNET hFtpSession
, LPSTR lpszCurrentDirectory
,
828 LPDWORD lpdwCurrentDirectory
)
834 if(lpdwCurrentDirectory
) {
835 len
= *lpdwCurrentDirectory
;
836 if(lpszCurrentDirectory
)
838 dir
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
841 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
846 ret
= FtpGetCurrentDirectoryW(hFtpSession
, lpszCurrentDirectory
?dir
:NULL
, lpdwCurrentDirectory
?&len
:NULL
);
848 if (ret
&& lpszCurrentDirectory
)
849 WideCharToMultiByte(CP_ACP
, 0, dir
, -1, lpszCurrentDirectory
, len
, NULL
, NULL
);
851 if (lpdwCurrentDirectory
) *lpdwCurrentDirectory
= len
;
852 HeapFree(GetProcessHeap(), 0, dir
);
857 static void AsyncFtpGetCurrentDirectoryProc(WORKREQUEST
*workRequest
)
859 struct WORKREQ_FTPGETCURRENTDIRECTORYW
const *req
= &workRequest
->u
.FtpGetCurrentDirectoryW
;
860 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
862 TRACE("%p\n", lpwfs
);
864 FTP_FtpGetCurrentDirectoryW(lpwfs
, req
->lpszDirectory
, req
->lpdwDirectory
);
867 /***********************************************************************
868 * FtpGetCurrentDirectoryW (WININET.@)
870 * Retrieves the current directory
877 BOOL WINAPI
FtpGetCurrentDirectoryW(HINTERNET hFtpSession
, LPWSTR lpszCurrentDirectory
,
878 LPDWORD lpdwCurrentDirectory
)
880 LPWININETFTPSESSIONW lpwfs
;
881 LPWININETAPPINFOW hIC
= NULL
;
884 TRACE("len(%d)\n", *lpdwCurrentDirectory
);
886 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
889 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
893 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
895 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
899 if (!lpdwCurrentDirectory
)
901 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
905 if (lpszCurrentDirectory
== NULL
)
907 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
911 if (lpwfs
->download_in_progress
!= NULL
)
913 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
917 hIC
= lpwfs
->lpAppInfo
;
918 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
920 WORKREQUEST workRequest
;
921 struct WORKREQ_FTPGETCURRENTDIRECTORYW
*req
;
923 workRequest
.asyncproc
= AsyncFtpGetCurrentDirectoryProc
;
924 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
925 req
= &workRequest
.u
.FtpGetCurrentDirectoryW
;
926 req
->lpszDirectory
= lpszCurrentDirectory
;
927 req
->lpdwDirectory
= lpdwCurrentDirectory
;
929 r
= INTERNET_AsyncCall(&workRequest
);
933 r
= FTP_FtpGetCurrentDirectoryW(lpwfs
, lpszCurrentDirectory
,
934 lpdwCurrentDirectory
);
939 WININET_Release( &lpwfs
->hdr
);
945 /***********************************************************************
946 * FTP_FtpGetCurrentDirectoryW (Internal)
948 * Retrieves the current directory
955 BOOL WINAPI
FTP_FtpGetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPWSTR lpszCurrentDirectory
,
956 LPDWORD lpdwCurrentDirectory
)
959 LPWININETAPPINFOW hIC
= NULL
;
960 DWORD bSuccess
= FALSE
;
962 TRACE("len(%d)\n", *lpdwCurrentDirectory
);
964 /* Clear any error information */
965 INTERNET_SetLastError(0);
967 hIC
= lpwfs
->lpAppInfo
;
968 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PWD
, NULL
,
969 lpwfs
->hdr
.lpfnStatusCB
, &lpwfs
->hdr
, lpwfs
->hdr
.dwContext
))
972 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
975 if (nResCode
== 257) /* Extract directory name */
977 DWORD firstpos
, lastpos
, len
;
978 LPWSTR lpszResponseBuffer
= WININET_strdup_AtoW(INTERNET_GetResponseBuffer());
980 for (firstpos
= 0, lastpos
= 0; lpszResponseBuffer
[lastpos
]; lastpos
++)
982 if ('"' == lpszResponseBuffer
[lastpos
])
990 len
= lastpos
- firstpos
;
991 if (*lpdwCurrentDirectory
>= len
)
993 memcpy(lpszCurrentDirectory
, &lpszResponseBuffer
[firstpos
+ 1], len
* sizeof(WCHAR
));
994 lpszCurrentDirectory
[len
- 1] = 0;
995 *lpdwCurrentDirectory
= len
;
998 else INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1000 HeapFree(GetProcessHeap(), 0, lpszResponseBuffer
);
1003 FTP_SetResponseError(nResCode
);
1007 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1009 INTERNET_ASYNC_RESULT iar
;
1011 iar
.dwResult
= (DWORD
)bSuccess
;
1012 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: ERROR_INTERNET_EXTENDED_ERROR
;
1013 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1014 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1017 return (DWORD
) bSuccess
;
1020 /***********************************************************************
1021 * FtpOpenFileA (WININET.@)
1023 * Open a remote file for writing or reading
1026 * HINTERNET handle on success
1030 HINTERNET WINAPI
FtpOpenFileA(HINTERNET hFtpSession
,
1031 LPCSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1032 DWORD_PTR dwContext
)
1034 LPWSTR lpwzFileName
;
1037 lpwzFileName
= lpszFileName
?WININET_strdup_AtoW(lpszFileName
):NULL
;
1038 ret
= FtpOpenFileW(hFtpSession
, lpwzFileName
, fdwAccess
, dwFlags
, dwContext
);
1039 HeapFree(GetProcessHeap(), 0, lpwzFileName
);
1044 static void AsyncFtpOpenFileProc(WORKREQUEST
*workRequest
)
1046 struct WORKREQ_FTPOPENFILEW
const *req
= &workRequest
->u
.FtpOpenFileW
;
1047 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1049 TRACE("%p\n", lpwfs
);
1051 FTP_FtpOpenFileW(lpwfs
, req
->lpszFilename
,
1052 req
->dwAccess
, req
->dwFlags
, req
->dwContext
);
1053 HeapFree(GetProcessHeap(), 0, req
->lpszFilename
);
1056 /***********************************************************************
1057 * FtpOpenFileW (WININET.@)
1059 * Open a remote file for writing or reading
1062 * HINTERNET handle on success
1066 HINTERNET WINAPI
FtpOpenFileW(HINTERNET hFtpSession
,
1067 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1068 DWORD_PTR dwContext
)
1070 LPWININETFTPSESSIONW lpwfs
;
1071 LPWININETAPPINFOW hIC
= NULL
;
1074 TRACE("(%p,%s,0x%08x,0x%08x,0x%08lx)\n", hFtpSession
,
1075 debugstr_w(lpszFileName
), fdwAccess
, dwFlags
, dwContext
);
1077 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1080 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1084 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1086 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1090 if ((!lpszFileName
) ||
1091 ((fdwAccess
!= GENERIC_READ
) && (fdwAccess
!= GENERIC_WRITE
)) ||
1092 ((dwFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
))
1094 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1098 if (lpwfs
->download_in_progress
!= NULL
)
1100 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1104 hIC
= lpwfs
->lpAppInfo
;
1105 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1107 WORKREQUEST workRequest
;
1108 struct WORKREQ_FTPOPENFILEW
*req
;
1110 workRequest
.asyncproc
= AsyncFtpOpenFileProc
;
1111 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1112 req
= &workRequest
.u
.FtpOpenFileW
;
1113 req
->lpszFilename
= WININET_strdupW(lpszFileName
);
1114 req
->dwAccess
= fdwAccess
;
1115 req
->dwFlags
= dwFlags
;
1116 req
->dwContext
= dwContext
;
1118 INTERNET_AsyncCall(&workRequest
);
1123 r
= FTP_FtpOpenFileW(lpwfs
, lpszFileName
, fdwAccess
, dwFlags
, dwContext
);
1127 WININET_Release( &lpwfs
->hdr
);
1133 /***********************************************************************
1134 * FTP_FtpOpenFileW (Internal)
1136 * Open a remote file for writing or reading
1139 * HINTERNET handle on success
1143 HINTERNET
FTP_FtpOpenFileW(LPWININETFTPSESSIONW lpwfs
,
1144 LPCWSTR lpszFileName
, DWORD fdwAccess
, DWORD dwFlags
,
1145 DWORD_PTR dwContext
)
1148 BOOL bSuccess
= FALSE
;
1149 LPWININETFTPFILE lpwh
= NULL
;
1150 LPWININETAPPINFOW hIC
= NULL
;
1151 HINTERNET handle
= NULL
;
1155 /* Clear any error information */
1156 INTERNET_SetLastError(0);
1158 if (GENERIC_READ
== fdwAccess
)
1160 /* Set up socket to retrieve data */
1161 bSuccess
= FTP_SendRetrieve(lpwfs
, lpszFileName
, dwFlags
);
1163 else if (GENERIC_WRITE
== fdwAccess
)
1165 /* Set up socket to send data */
1166 bSuccess
= FTP_SendStore(lpwfs
, lpszFileName
, dwFlags
);
1169 /* Get data socket to server */
1170 if (bSuccess
&& FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1172 lpwh
= HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPFILE
));
1173 lpwh
->hdr
.htype
= WH_HFILE
;
1174 lpwh
->hdr
.dwFlags
= dwFlags
;
1175 lpwh
->hdr
.dwContext
= dwContext
;
1176 lpwh
->hdr
.dwRefCount
= 1;
1177 lpwh
->hdr
.close_connection
= NULL
;
1178 lpwh
->hdr
.destroy
= FTP_CloseFileTransferHandle
;
1179 lpwh
->hdr
.lpfnStatusCB
= lpwfs
->hdr
.lpfnStatusCB
;
1180 lpwh
->nDataSocket
= nDataSocket
;
1181 lpwh
->session_deleted
= FALSE
;
1183 WININET_AddRef( &lpwfs
->hdr
);
1184 lpwh
->lpFtpSession
= lpwfs
;
1185 list_add_head( &lpwfs
->hdr
.children
, &lpwh
->hdr
.entry
);
1187 handle
= WININET_AllocHandle( &lpwh
->hdr
);
1191 /* Indicate that a download is currently in progress */
1192 lpwfs
->download_in_progress
= lpwh
;
1195 if (lpwfs
->lstnSocket
!= -1)
1196 closesocket(lpwfs
->lstnSocket
);
1198 hIC
= lpwfs
->lpAppInfo
;
1199 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1201 INTERNET_ASYNC_RESULT iar
;
1205 iar
.dwResult
= (DWORD
)handle
;
1206 iar
.dwError
= ERROR_SUCCESS
;
1207 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_HANDLE_CREATED
,
1208 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1211 iar
.dwResult
= (DWORD
)bSuccess
;
1212 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1213 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1214 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1219 WININET_Release( &lpwh
->hdr
);
1225 /***********************************************************************
1226 * FtpGetFileA (WININET.@)
1228 * Retrieve file from the FTP server
1235 BOOL WINAPI
FtpGetFileA(HINTERNET hInternet
, LPCSTR lpszRemoteFile
, LPCSTR lpszNewFile
,
1236 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1237 DWORD_PTR dwContext
)
1239 LPWSTR lpwzRemoteFile
;
1243 lpwzRemoteFile
= lpszRemoteFile
?WININET_strdup_AtoW(lpszRemoteFile
):NULL
;
1244 lpwzNewFile
= lpszNewFile
?WININET_strdup_AtoW(lpszNewFile
):NULL
;
1245 ret
= FtpGetFileW(hInternet
, lpwzRemoteFile
, lpwzNewFile
, fFailIfExists
,
1246 dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1247 HeapFree(GetProcessHeap(), 0, lpwzRemoteFile
);
1248 HeapFree(GetProcessHeap(), 0, lpwzNewFile
);
1253 static void AsyncFtpGetFileProc(WORKREQUEST
*workRequest
)
1255 struct WORKREQ_FTPGETFILEW
const *req
= &workRequest
->u
.FtpGetFileW
;
1256 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1258 TRACE("%p\n", lpwfs
);
1260 FTP_FtpGetFileW(lpwfs
, req
->lpszRemoteFile
,
1261 req
->lpszNewFile
, req
->fFailIfExists
,
1262 req
->dwLocalFlagsAttribute
, req
->dwFlags
, req
->dwContext
);
1263 HeapFree(GetProcessHeap(), 0, req
->lpszRemoteFile
);
1264 HeapFree(GetProcessHeap(), 0, req
->lpszNewFile
);
1268 /***********************************************************************
1269 * FtpGetFileW (WININET.@)
1271 * Retrieve file from the FTP server
1278 BOOL WINAPI
FtpGetFileW(HINTERNET hInternet
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1279 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1280 DWORD_PTR dwContext
)
1282 LPWININETFTPSESSIONW lpwfs
;
1283 LPWININETAPPINFOW hIC
= NULL
;
1286 if (!lpszRemoteFile
|| !lpszNewFile
)
1288 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1292 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hInternet
);
1295 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1299 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1301 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1305 if ((dwInternetFlags
& FTP_CONDITION_MASK
) > FTP_TRANSFER_TYPE_BINARY
)
1307 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1311 if (lpwfs
->download_in_progress
!= NULL
)
1313 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1317 hIC
= lpwfs
->lpAppInfo
;
1318 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1320 WORKREQUEST workRequest
;
1321 struct WORKREQ_FTPGETFILEW
*req
;
1323 workRequest
.asyncproc
= AsyncFtpGetFileProc
;
1324 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1325 req
= &workRequest
.u
.FtpGetFileW
;
1326 req
->lpszRemoteFile
= WININET_strdupW(lpszRemoteFile
);
1327 req
->lpszNewFile
= WININET_strdupW(lpszNewFile
);
1328 req
->dwLocalFlagsAttribute
= dwLocalFlagsAttribute
;
1329 req
->fFailIfExists
= fFailIfExists
;
1330 req
->dwFlags
= dwInternetFlags
;
1331 req
->dwContext
= dwContext
;
1333 r
= INTERNET_AsyncCall(&workRequest
);
1337 r
= FTP_FtpGetFileW(lpwfs
, lpszRemoteFile
, lpszNewFile
,
1338 fFailIfExists
, dwLocalFlagsAttribute
, dwInternetFlags
, dwContext
);
1342 WININET_Release( &lpwfs
->hdr
);
1348 /***********************************************************************
1349 * FTP_FtpGetFileW (Internal)
1351 * Retrieve file from the FTP server
1358 BOOL WINAPI
FTP_FtpGetFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, LPCWSTR lpszNewFile
,
1359 BOOL fFailIfExists
, DWORD dwLocalFlagsAttribute
, DWORD dwInternetFlags
,
1360 DWORD_PTR dwContext
)
1362 BOOL bSuccess
= FALSE
;
1364 LPWININETAPPINFOW hIC
= NULL
;
1366 TRACE("lpszRemoteFile(%s) lpszNewFile(%s)\n", debugstr_w(lpszRemoteFile
), debugstr_w(lpszNewFile
));
1368 /* Clear any error information */
1369 INTERNET_SetLastError(0);
1371 /* Ensure we can write to lpszNewfile by opening it */
1372 hFile
= CreateFileW(lpszNewFile
, GENERIC_WRITE
, 0, 0, fFailIfExists
?
1373 CREATE_NEW
: CREATE_ALWAYS
, dwLocalFlagsAttribute
, 0);
1374 if (INVALID_HANDLE_VALUE
== hFile
)
1377 /* Set up socket to retrieve data */
1378 if (FTP_SendRetrieve(lpwfs
, lpszRemoteFile
, dwInternetFlags
))
1382 /* Get data socket to server */
1383 if (FTP_GetDataSocket(lpwfs
, &nDataSocket
))
1388 FTP_RetrieveFileData(lpwfs
, nDataSocket
, hFile
);
1389 closesocket(nDataSocket
);
1391 nResCode
= FTP_ReceiveResponse(lpwfs
, dwContext
);
1394 if (nResCode
== 226)
1397 FTP_SetResponseError(nResCode
);
1402 if (lpwfs
->lstnSocket
!= -1)
1403 closesocket(lpwfs
->lstnSocket
);
1407 hIC
= lpwfs
->lpAppInfo
;
1408 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1410 INTERNET_ASYNC_RESULT iar
;
1412 iar
.dwResult
= (DWORD
)bSuccess
;
1413 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1414 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1415 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1421 /***********************************************************************
1422 * FtpGetFileSize (WININET.@)
1424 DWORD WINAPI
FtpGetFileSize( HINTERNET hFile
, LPDWORD lpdwFileSizeHigh
)
1426 FIXME("(%p, %p)\n", hFile
, lpdwFileSizeHigh
);
1428 if (lpdwFileSizeHigh
)
1429 *lpdwFileSizeHigh
= 0;
1434 /***********************************************************************
1435 * FtpDeleteFileA (WININET.@)
1437 * Delete a file on the ftp server
1444 BOOL WINAPI
FtpDeleteFileA(HINTERNET hFtpSession
, LPCSTR lpszFileName
)
1446 LPWSTR lpwzFileName
;
1449 lpwzFileName
= lpszFileName
?WININET_strdup_AtoW(lpszFileName
):NULL
;
1450 ret
= FtpDeleteFileW(hFtpSession
, lpwzFileName
);
1451 HeapFree(GetProcessHeap(), 0, lpwzFileName
);
1455 static void AsyncFtpDeleteFileProc(WORKREQUEST
*workRequest
)
1457 struct WORKREQ_FTPDELETEFILEW
const *req
= &workRequest
->u
.FtpDeleteFileW
;
1458 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1460 TRACE("%p\n", lpwfs
);
1462 FTP_FtpDeleteFileW(lpwfs
, req
->lpszFilename
);
1463 HeapFree(GetProcessHeap(), 0, req
->lpszFilename
);
1466 /***********************************************************************
1467 * FtpDeleteFileW (WININET.@)
1469 * Delete a file on the ftp server
1476 BOOL WINAPI
FtpDeleteFileW(HINTERNET hFtpSession
, LPCWSTR lpszFileName
)
1478 LPWININETFTPSESSIONW lpwfs
;
1479 LPWININETAPPINFOW hIC
= NULL
;
1482 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1485 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1489 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1491 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1495 if (lpwfs
->download_in_progress
!= NULL
)
1497 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1503 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1507 hIC
= lpwfs
->lpAppInfo
;
1508 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1510 WORKREQUEST workRequest
;
1511 struct WORKREQ_FTPDELETEFILEW
*req
;
1513 workRequest
.asyncproc
= AsyncFtpDeleteFileProc
;
1514 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1515 req
= &workRequest
.u
.FtpDeleteFileW
;
1516 req
->lpszFilename
= WININET_strdupW(lpszFileName
);
1518 r
= INTERNET_AsyncCall(&workRequest
);
1522 r
= FTP_FtpDeleteFileW(lpwfs
, lpszFileName
);
1526 WININET_Release( &lpwfs
->hdr
);
1531 /***********************************************************************
1532 * FTP_FtpDeleteFileW (Internal)
1534 * Delete a file on the ftp server
1541 BOOL
FTP_FtpDeleteFileW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszFileName
)
1544 BOOL bSuccess
= FALSE
;
1545 LPWININETAPPINFOW hIC
= NULL
;
1547 TRACE("%p\n", lpwfs
);
1549 /* Clear any error information */
1550 INTERNET_SetLastError(0);
1552 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_DELE
, lpszFileName
, 0, 0, 0))
1555 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1558 if (nResCode
== 250)
1561 FTP_SetResponseError(nResCode
);
1564 hIC
= lpwfs
->lpAppInfo
;
1565 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1567 INTERNET_ASYNC_RESULT iar
;
1569 iar
.dwResult
= (DWORD
)bSuccess
;
1570 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1571 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1572 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1579 /***********************************************************************
1580 * FtpRemoveDirectoryA (WININET.@)
1582 * Remove a directory on the ftp server
1589 BOOL WINAPI
FtpRemoveDirectoryA(HINTERNET hFtpSession
, LPCSTR lpszDirectory
)
1591 LPWSTR lpwzDirectory
;
1594 lpwzDirectory
= lpszDirectory
?WININET_strdup_AtoW(lpszDirectory
):NULL
;
1595 ret
= FtpRemoveDirectoryW(hFtpSession
, lpwzDirectory
);
1596 HeapFree(GetProcessHeap(), 0, lpwzDirectory
);
1600 static void AsyncFtpRemoveDirectoryProc(WORKREQUEST
*workRequest
)
1602 struct WORKREQ_FTPREMOVEDIRECTORYW
const *req
= &workRequest
->u
.FtpRemoveDirectoryW
;
1603 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1605 TRACE("%p\n", lpwfs
);
1607 FTP_FtpRemoveDirectoryW(lpwfs
, req
->lpszDirectory
);
1608 HeapFree(GetProcessHeap(), 0, req
->lpszDirectory
);
1611 /***********************************************************************
1612 * FtpRemoveDirectoryW (WININET.@)
1614 * Remove a directory on the ftp server
1621 BOOL WINAPI
FtpRemoveDirectoryW(HINTERNET hFtpSession
, LPCWSTR lpszDirectory
)
1623 LPWININETFTPSESSIONW lpwfs
;
1624 LPWININETAPPINFOW hIC
= NULL
;
1627 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1630 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1634 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1636 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1640 if (lpwfs
->download_in_progress
!= NULL
)
1642 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1648 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1652 hIC
= lpwfs
->lpAppInfo
;
1653 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1655 WORKREQUEST workRequest
;
1656 struct WORKREQ_FTPREMOVEDIRECTORYW
*req
;
1658 workRequest
.asyncproc
= AsyncFtpRemoveDirectoryProc
;
1659 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1660 req
= &workRequest
.u
.FtpRemoveDirectoryW
;
1661 req
->lpszDirectory
= WININET_strdupW(lpszDirectory
);
1663 r
= INTERNET_AsyncCall(&workRequest
);
1667 r
= FTP_FtpRemoveDirectoryW(lpwfs
, lpszDirectory
);
1671 WININET_Release( &lpwfs
->hdr
);
1676 /***********************************************************************
1677 * FTP_FtpRemoveDirectoryW (Internal)
1679 * Remove a directory on the ftp server
1686 BOOL
FTP_FtpRemoveDirectoryW(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszDirectory
)
1689 BOOL bSuccess
= FALSE
;
1690 LPWININETAPPINFOW hIC
= NULL
;
1694 /* Clear any error information */
1695 INTERNET_SetLastError(0);
1697 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RMD
, lpszDirectory
, 0, 0, 0))
1700 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1703 if (nResCode
== 250)
1706 FTP_SetResponseError(nResCode
);
1710 hIC
= lpwfs
->lpAppInfo
;
1711 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1713 INTERNET_ASYNC_RESULT iar
;
1715 iar
.dwResult
= (DWORD
)bSuccess
;
1716 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1717 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1718 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1725 /***********************************************************************
1726 * FtpRenameFileA (WININET.@)
1728 * Rename a file on the ftp server
1735 BOOL WINAPI
FtpRenameFileA(HINTERNET hFtpSession
, LPCSTR lpszSrc
, LPCSTR lpszDest
)
1741 lpwzSrc
= lpszSrc
?WININET_strdup_AtoW(lpszSrc
):NULL
;
1742 lpwzDest
= lpszDest
?WININET_strdup_AtoW(lpszDest
):NULL
;
1743 ret
= FtpRenameFileW(hFtpSession
, lpwzSrc
, lpwzDest
);
1744 HeapFree(GetProcessHeap(), 0, lpwzSrc
);
1745 HeapFree(GetProcessHeap(), 0, lpwzDest
);
1749 static void AsyncFtpRenameFileProc(WORKREQUEST
*workRequest
)
1751 struct WORKREQ_FTPRENAMEFILEW
const *req
= &workRequest
->u
.FtpRenameFileW
;
1752 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) workRequest
->hdr
;
1754 TRACE("%p\n", lpwfs
);
1756 FTP_FtpRenameFileW(lpwfs
, req
->lpszSrcFile
, req
->lpszDestFile
);
1757 HeapFree(GetProcessHeap(), 0, req
->lpszSrcFile
);
1758 HeapFree(GetProcessHeap(), 0, req
->lpszDestFile
);
1761 /***********************************************************************
1762 * FtpRenameFileW (WININET.@)
1764 * Rename a file on the ftp server
1771 BOOL WINAPI
FtpRenameFileW(HINTERNET hFtpSession
, LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
1773 LPWININETFTPSESSIONW lpwfs
;
1774 LPWININETAPPINFOW hIC
= NULL
;
1777 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hFtpSession
);
1780 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1784 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1786 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1790 if (lpwfs
->download_in_progress
!= NULL
)
1792 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1796 if (!lpszSrc
|| !lpszDest
)
1798 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1802 hIC
= lpwfs
->lpAppInfo
;
1803 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1805 WORKREQUEST workRequest
;
1806 struct WORKREQ_FTPRENAMEFILEW
*req
;
1808 workRequest
.asyncproc
= AsyncFtpRenameFileProc
;
1809 workRequest
.hdr
= WININET_AddRef( &lpwfs
->hdr
);
1810 req
= &workRequest
.u
.FtpRenameFileW
;
1811 req
->lpszSrcFile
= WININET_strdupW(lpszSrc
);
1812 req
->lpszDestFile
= WININET_strdupW(lpszDest
);
1814 r
= INTERNET_AsyncCall(&workRequest
);
1818 r
= FTP_FtpRenameFileW(lpwfs
, lpszSrc
, lpszDest
);
1822 WININET_Release( &lpwfs
->hdr
);
1827 /***********************************************************************
1828 * FTP_FtpRenameFileW (Internal)
1830 * Rename a file on the ftp server
1837 BOOL
FTP_FtpRenameFileW( LPWININETFTPSESSIONW lpwfs
,
1838 LPCWSTR lpszSrc
, LPCWSTR lpszDest
)
1841 BOOL bSuccess
= FALSE
;
1842 LPWININETAPPINFOW hIC
= NULL
;
1846 /* Clear any error information */
1847 INTERNET_SetLastError(0);
1849 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNFR
, lpszSrc
, 0, 0, 0))
1852 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1853 if (nResCode
== 350)
1855 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RNTO
, lpszDest
, 0, 0, 0))
1858 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1861 if (nResCode
== 250)
1864 FTP_SetResponseError(nResCode
);
1867 hIC
= lpwfs
->lpAppInfo
;
1868 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1870 INTERNET_ASYNC_RESULT iar
;
1872 iar
.dwResult
= (DWORD
)bSuccess
;
1873 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
1874 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
1875 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
1881 /***********************************************************************
1882 * FtpCommandA (WININET.@)
1884 BOOL WINAPI
FtpCommandA( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
1885 LPCSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
1890 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
1891 debugstr_a(lpszCommand
), dwContext
, phFtpCommand
);
1893 if (fExpectResponse
)
1895 FIXME("data connection not supported\n");
1899 if (!lpszCommand
|| !lpszCommand
[0])
1901 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1905 if (!(cmdW
= WININET_strdup_AtoW(lpszCommand
)))
1907 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1911 r
= FtpCommandW(hConnect
, fExpectResponse
, dwFlags
, cmdW
, dwContext
, phFtpCommand
);
1913 HeapFree(GetProcessHeap(), 0, cmdW
);
1917 /***********************************************************************
1918 * FtpCommandW (WININET.@)
1920 BOOL WINAPI
FtpCommandW( HINTERNET hConnect
, BOOL fExpectResponse
, DWORD dwFlags
,
1921 LPCWSTR lpszCommand
, DWORD_PTR dwContext
, HINTERNET
* phFtpCommand
)
1924 LPWININETFTPSESSIONW lpwfs
;
1926 DWORD len
, nBytesSent
= 0;
1927 INT nResCode
, nRC
= 0;
1929 TRACE("%p %d 0x%08x %s 0x%08lx %p\n", hConnect
, fExpectResponse
, dwFlags
,
1930 debugstr_w(lpszCommand
), dwContext
, phFtpCommand
);
1932 if (!lpszCommand
|| !lpszCommand
[0])
1934 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
1938 if (fExpectResponse
)
1940 FIXME("data connection not supported\n");
1944 lpwfs
= (LPWININETFTPSESSIONW
) WININET_GetObject( hConnect
);
1947 INTERNET_SetLastError(ERROR_INVALID_HANDLE
);
1951 if (WH_HFTPSESSION
!= lpwfs
->hdr
.htype
)
1953 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1957 if (lpwfs
->download_in_progress
!= NULL
)
1959 INTERNET_SetLastError(ERROR_FTP_TRANSFER_IN_PROGRESS
);
1963 len
= WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, NULL
, 0, NULL
, NULL
) + strlen(szCRLF
);
1964 if ((cmd
= HeapAlloc(GetProcessHeap(), 0, len
)))
1965 WideCharToMultiByte(CP_ACP
, 0, lpszCommand
, -1, cmd
, len
, NULL
, NULL
);
1968 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1972 strcat(cmd
, szCRLF
);
1975 TRACE("Sending (%s) len(%d)\n", cmd
, len
);
1976 while ((nBytesSent
< len
) && (nRC
!= -1))
1978 nRC
= send(lpwfs
->sndSocket
, cmd
+ nBytesSent
, len
- nBytesSent
, 0);
1982 TRACE("Sent %d bytes\n", nRC
);
1988 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
1989 if (nResCode
> 0 && nResCode
< 400)
1992 FTP_SetResponseError(nResCode
);
1996 WININET_Release( &lpwfs
->hdr
);
1997 HeapFree(GetProcessHeap(), 0, cmd
);
2001 /***********************************************************************
2002 * FTP_Connect (internal)
2004 * Connect to a ftp server
2007 * HINTERNET a session handle on success
2012 * Windows uses 'anonymous' as the username, when given a NULL username
2013 * and a NULL password. The password is first looked up in:
2015 * HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\EmailName
2017 * If this entry is not present it uses the current username as the password.
2021 HINTERNET
FTP_Connect(LPWININETAPPINFOW hIC
, LPCWSTR lpszServerName
,
2022 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
2023 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
2024 DWORD dwInternalFlags
)
2026 static const WCHAR szKey
[] = {'S','o','f','t','w','a','r','e','\\',
2027 'M','i','c','r','o','s','o','f','t','\\',
2028 'W','i','n','d','o','w','s','\\',
2029 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2030 'I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
2031 static const WCHAR szValue
[] = {'E','m','a','i','l','N','a','m','e',0};
2032 static const WCHAR szDefaultUsername
[] = {'a','n','o','n','y','m','o','u','s','\0'};
2033 static const WCHAR szEmpty
[] = {'\0'};
2034 struct sockaddr_in socketAddr
;
2037 BOOL bSuccess
= FALSE
;
2038 LPWININETFTPSESSIONW lpwfs
= NULL
;
2039 HINTERNET handle
= NULL
;
2041 TRACE("%p Server(%s) Port(%d) User(%s) Paswd(%s)\n",
2042 hIC
, debugstr_w(lpszServerName
),
2043 nServerPort
, debugstr_w(lpszUserName
), debugstr_w(lpszPassword
));
2045 assert( hIC
->hdr
.htype
== WH_HINIT
);
2047 if (NULL
== lpszUserName
&& NULL
!= lpszPassword
)
2049 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2053 lpwfs
= HeapAlloc(GetProcessHeap(), 0, sizeof(WININETFTPSESSIONW
));
2056 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2060 if (nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
2061 nServerPort
= INTERNET_DEFAULT_FTP_PORT
;
2063 lpwfs
->hdr
.htype
= WH_HFTPSESSION
;
2064 lpwfs
->hdr
.dwFlags
= dwFlags
;
2065 lpwfs
->hdr
.dwContext
= dwContext
;
2066 lpwfs
->hdr
.dwInternalFlags
= dwInternalFlags
;
2067 lpwfs
->hdr
.dwRefCount
= 1;
2068 lpwfs
->hdr
.close_connection
= FTP_CloseConnection
;
2069 lpwfs
->hdr
.destroy
= FTP_CloseSessionHandle
;
2070 lpwfs
->hdr
.lpfnStatusCB
= hIC
->hdr
.lpfnStatusCB
;
2071 lpwfs
->download_in_progress
= NULL
;
2072 lpwfs
->sndSocket
= -1;
2073 lpwfs
->lstnSocket
= -1;
2074 lpwfs
->pasvSocket
= -1;
2076 WININET_AddRef( &hIC
->hdr
);
2077 lpwfs
->lpAppInfo
= hIC
;
2078 list_add_head( &hIC
->hdr
.children
, &lpwfs
->hdr
.entry
);
2080 handle
= WININET_AllocHandle( &lpwfs
->hdr
);
2083 ERR("Failed to alloc handle\n");
2084 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2088 if(hIC
->lpszProxy
&& hIC
->dwAccessType
== INTERNET_OPEN_TYPE_PROXY
) {
2089 if(strchrW(hIC
->lpszProxy
, ' '))
2090 FIXME("Several proxies not implemented.\n");
2091 if(hIC
->lpszProxyBypass
)
2092 FIXME("Proxy bypass is ignored.\n");
2094 if ( !lpszUserName
) {
2096 WCHAR szPassword
[MAX_PATH
];
2097 DWORD len
= sizeof(szPassword
);
2099 lpwfs
->lpszUserName
= WININET_strdupW(szDefaultUsername
);
2101 RegOpenKeyW(HKEY_CURRENT_USER
, szKey
, &key
);
2102 if (RegQueryValueExW(key
, szValue
, NULL
, NULL
, (LPBYTE
)szPassword
, &len
)) {
2103 /* Nothing in the registry, get the username and use that as the password */
2104 if (!GetUserNameW(szPassword
, &len
)) {
2105 /* Should never get here, but use an empty password as failsafe */
2106 strcpyW(szPassword
, szEmpty
);
2111 TRACE("Password used for anonymous ftp : (%s)\n", debugstr_w(szPassword
));
2112 lpwfs
->lpszPassword
= WININET_strdupW(szPassword
);
2115 lpwfs
->lpszUserName
= WININET_strdupW(lpszUserName
);
2118 lpwfs
->lpszPassword
= WININET_strdupW(lpszPassword
);
2120 lpwfs
->lpszPassword
= WININET_strdupW(szEmpty
);
2123 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2124 if (!(lpwfs
->hdr
.dwInternalFlags
& INET_OPENURL
))
2126 INTERNET_ASYNC_RESULT iar
;
2128 iar
.dwResult
= (DWORD
)handle
;
2129 iar
.dwError
= ERROR_SUCCESS
;
2131 SendAsyncCallback(&hIC
->hdr
, dwContext
,
2132 INTERNET_STATUS_HANDLE_CREATED
, &iar
,
2133 sizeof(INTERNET_ASYNC_RESULT
));
2136 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_RESOLVING_NAME
,
2137 (LPWSTR
) lpszServerName
, strlenW(lpszServerName
));
2139 if (!GetAddress(lpszServerName
, nServerPort
, &socketAddr
))
2141 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
2145 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_NAME_RESOLVED
,
2146 (LPWSTR
) lpszServerName
, strlenW(lpszServerName
));
2148 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2151 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2155 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTING_TO_SERVER
,
2156 &socketAddr
, sizeof(struct sockaddr_in
));
2158 if (connect(nsocket
, (struct sockaddr
*)&socketAddr
, sizeof(socketAddr
)) < 0)
2160 ERR("Unable to connect (%s)\n", strerror(errno
));
2161 INTERNET_SetLastError(ERROR_INTERNET_CANNOT_CONNECT
);
2165 TRACE("Connected to server\n");
2166 lpwfs
->sndSocket
= nsocket
;
2167 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_CONNECTED_TO_SERVER
,
2168 &socketAddr
, sizeof(struct sockaddr_in
));
2170 sock_namelen
= sizeof(lpwfs
->socketAddress
);
2171 getsockname(nsocket
, (struct sockaddr
*) &lpwfs
->socketAddress
, &sock_namelen
);
2173 if (FTP_ConnectToHost(lpwfs
))
2175 TRACE("Successfully logged into server\n");
2181 if (lpwfs
) WININET_Release( &lpwfs
->hdr
);
2183 if (!bSuccess
&& handle
)
2185 WININET_FreeHandle( handle
);
2189 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
2191 INTERNET_ASYNC_RESULT iar
;
2193 iar
.dwResult
= bSuccess
? (DWORD_PTR
)lpwfs
: 0;
2194 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2195 SendAsyncCallback(&hIC
->hdr
, dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
,
2196 &iar
, sizeof(INTERNET_ASYNC_RESULT
));
2203 /***********************************************************************
2204 * FTP_ConnectToHost (internal)
2206 * Connect to a ftp server
2213 static BOOL
FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs
)
2216 BOOL bSuccess
= FALSE
;
2219 FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2221 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_USER
, lpwfs
->lpszUserName
, 0, 0, 0))
2224 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2227 /* Login successful... */
2228 if (nResCode
== 230)
2230 /* User name okay, need password... */
2231 else if (nResCode
== 331)
2232 bSuccess
= FTP_SendPassword(lpwfs
);
2233 /* Need account for login... */
2234 else if (nResCode
== 332)
2235 bSuccess
= FTP_SendAccount(lpwfs
);
2237 FTP_SetResponseError(nResCode
);
2240 TRACE("Returning %d\n", bSuccess
);
2246 /***********************************************************************
2247 * FTP_SendCommandA (internal)
2249 * Send command to server
2256 static BOOL
FTP_SendCommandA(INT nSocket
, FTP_COMMAND ftpCmd
, LPCSTR lpszParam
,
2257 INTERNET_STATUS_CALLBACK lpfnStatusCB
, LPWININETHANDLEHEADER hdr
, DWORD_PTR dwContext
)
2261 DWORD nBytesSent
= 0;
2265 TRACE("%d: (%s) %d\n", ftpCmd
, lpszParam
, nSocket
);
2269 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2272 dwParamLen
= lpszParam
?strlen(lpszParam
)+1:0;
2273 len
= dwParamLen
+ strlen(szFtpCommands
[ftpCmd
]) + strlen(szCRLF
);
2274 if (NULL
== (buf
= HeapAlloc(GetProcessHeap(), 0, len
+1)))
2276 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2279 sprintf(buf
, "%s%s%s%s", szFtpCommands
[ftpCmd
], dwParamLen
? " " : "",
2280 dwParamLen
? lpszParam
: "", szCRLF
);
2282 TRACE("Sending (%s) len(%d)\n", buf
, len
);
2283 while((nBytesSent
< len
) && (nRC
!= -1))
2285 nRC
= send(nSocket
, buf
+nBytesSent
, len
- nBytesSent
, 0);
2289 HeapFree(GetProcessHeap(), 0, (LPVOID
)buf
);
2293 lpfnStatusCB(hdr
->hInternet
, dwContext
, INTERNET_STATUS_REQUEST_SENT
,
2294 &nBytesSent
, sizeof(DWORD
));
2297 TRACE("Sent %d bytes\n", nBytesSent
);
2301 /***********************************************************************
2302 * FTP_SendCommand (internal)
2304 * Send command to server
2311 static BOOL
FTP_SendCommand(INT nSocket
, FTP_COMMAND ftpCmd
, LPCWSTR lpszParam
,
2312 INTERNET_STATUS_CALLBACK lpfnStatusCB
, LPWININETHANDLEHEADER hdr
, DWORD_PTR dwContext
)
2315 LPSTR lpszParamA
= lpszParam
?WININET_strdup_WtoA(lpszParam
):NULL
;
2316 ret
= FTP_SendCommandA(nSocket
, ftpCmd
, lpszParamA
, lpfnStatusCB
, hdr
, dwContext
);
2317 HeapFree(GetProcessHeap(), 0, lpszParamA
);
2321 /***********************************************************************
2322 * FTP_ReceiveResponse (internal)
2324 * Receive response from server
2327 * Reply code on success
2331 INT
FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs
, DWORD_PTR dwContext
)
2333 LPSTR lpszResponse
= INTERNET_GetResponseBuffer();
2336 char firstprefix
[5];
2337 BOOL multiline
= FALSE
;
2338 LPWININETAPPINFOW hIC
= NULL
;
2340 TRACE("socket(%d)\n", lpwfs
->sndSocket
);
2342 hIC
= lpwfs
->lpAppInfo
;
2343 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2347 if (!INTERNET_GetNextLine(lpwfs
->sndSocket
, &nRecv
))
2354 if(lpszResponse
[3] != '-')
2357 { /* Start of multiline response. Loop until we get "nnn " */
2359 memcpy(firstprefix
, lpszResponse
, 3);
2360 firstprefix
[3] = ' ';
2361 firstprefix
[4] = '\0';
2366 if(!memcmp(firstprefix
, lpszResponse
, 4))
2374 rc
= atoi(lpszResponse
);
2376 SendAsyncCallback(&lpwfs
->hdr
, dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
2377 &nRecv
, sizeof(DWORD
));
2381 TRACE("return %d\n", rc
);
2386 /***********************************************************************
2387 * FTP_SendPassword (internal)
2389 * Send password to ftp server
2396 static BOOL
FTP_SendPassword(LPWININETFTPSESSIONW lpwfs
)
2399 BOOL bSuccess
= FALSE
;
2402 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASS
, lpwfs
->lpszPassword
, 0, 0, 0))
2405 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2408 TRACE("Received reply code %d\n", nResCode
);
2409 /* Login successful... */
2410 if (nResCode
== 230)
2412 /* Command not implemented, superfluous at the server site... */
2413 /* Need account for login... */
2414 else if (nResCode
== 332)
2415 bSuccess
= FTP_SendAccount(lpwfs
);
2417 FTP_SetResponseError(nResCode
);
2421 TRACE("Returning %d\n", bSuccess
);
2426 /***********************************************************************
2427 * FTP_SendAccount (internal)
2436 static BOOL
FTP_SendAccount(LPWININETFTPSESSIONW lpwfs
)
2439 BOOL bSuccess
= FALSE
;
2442 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_ACCT
, szNoAccount
, 0, 0, 0))
2445 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2449 FTP_SetResponseError(nResCode
);
2456 /***********************************************************************
2457 * FTP_SendStore (internal)
2459 * Send request to upload file to ftp server
2466 static BOOL
FTP_SendStore(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2469 BOOL bSuccess
= FALSE
;
2472 if (!FTP_InitListenSocket(lpwfs
))
2475 if (!FTP_SendType(lpwfs
, dwType
))
2478 if (!FTP_SendPortOrPasv(lpwfs
))
2481 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_STOR
, lpszRemoteFile
, 0, 0, 0))
2483 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2486 if (nResCode
== 150 || nResCode
== 125)
2489 FTP_SetResponseError(nResCode
);
2493 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2495 closesocket(lpwfs
->lstnSocket
);
2496 lpwfs
->lstnSocket
= -1;
2503 /***********************************************************************
2504 * FTP_InitListenSocket (internal)
2506 * Create a socket to listen for server response
2513 static BOOL
FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs
)
2515 BOOL bSuccess
= FALSE
;
2516 socklen_t namelen
= sizeof(struct sockaddr_in
);
2520 lpwfs
->lstnSocket
= socket(PF_INET
, SOCK_STREAM
, 0);
2521 if (lpwfs
->lstnSocket
== -1)
2523 TRACE("Unable to create listening socket\n");
2527 /* We obtain our ip addr from the name of the command channel socket */
2528 lpwfs
->lstnSocketAddress
= lpwfs
->socketAddress
;
2530 /* and get the system to assign us a port */
2531 lpwfs
->lstnSocketAddress
.sin_port
= htons((u_short
) 0);
2533 if (bind(lpwfs
->lstnSocket
,(struct sockaddr
*) &lpwfs
->lstnSocketAddress
, sizeof(struct sockaddr_in
)) == -1)
2535 TRACE("Unable to bind socket\n");
2539 if (listen(lpwfs
->lstnSocket
, MAX_BACKLOG
) == -1)
2541 TRACE("listen failed\n");
2545 if (getsockname(lpwfs
->lstnSocket
, (struct sockaddr
*) &lpwfs
->lstnSocketAddress
, &namelen
) != -1)
2549 if (!bSuccess
&& lpwfs
->lstnSocket
!= -1)
2551 closesocket(lpwfs
->lstnSocket
);
2552 lpwfs
->lstnSocket
= -1;
2559 /***********************************************************************
2560 * FTP_SendType (internal)
2562 * Tell server type of data being transferred
2568 * W98SE doesn't cache the type that's currently set
2569 * (i.e. it sends it always),
2570 * so we probably don't want to do that either.
2572 static BOOL
FTP_SendType(LPWININETFTPSESSIONW lpwfs
, DWORD dwType
)
2575 WCHAR type
[] = { 'I','\0' };
2576 BOOL bSuccess
= FALSE
;
2579 if (dwType
& INTERNET_FLAG_TRANSFER_ASCII
)
2582 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_TYPE
, type
, 0, 0, 0))
2585 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
)/100;
2591 FTP_SetResponseError(nResCode
);
2599 #if 0 /* FIXME: should probably be used for FtpGetFileSize */
2600 /***********************************************************************
2601 * FTP_GetFileSize (internal)
2603 * Retrieves from the server the size of the given file
2610 static BOOL
FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD
*dwSize
)
2613 BOOL bSuccess
= FALSE
;
2617 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_SIZE
, lpszRemoteFile
, 0, 0, 0))
2620 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2623 if (nResCode
== 213) {
2624 /* Now parses the output to get the actual file size */
2626 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
2628 for (i
= 0; (lpszResponseBuffer
[i
] != ' ') && (lpszResponseBuffer
[i
] != '\0'); i
++) ;
2629 if (lpszResponseBuffer
[i
] == '\0') return FALSE
;
2630 *dwSize
= atol(&(lpszResponseBuffer
[i
+ 1]));
2634 FTP_SetResponseError(nResCode
);
2644 /***********************************************************************
2645 * FTP_SendPort (internal)
2647 * Tell server which port to use
2654 static BOOL
FTP_SendPort(LPWININETFTPSESSIONW lpwfs
)
2656 static const WCHAR szIPFormat
[] = {'%','d',',','%','d',',','%','d',',','%','d',',','%','d',',','%','d','\0'};
2658 WCHAR szIPAddress
[64];
2659 BOOL bSuccess
= FALSE
;
2662 sprintfW(szIPAddress
, szIPFormat
,
2663 lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x000000FF,
2664 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x0000FF00)>>8,
2665 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0x00FF0000)>>16,
2666 (lpwfs
->lstnSocketAddress
.sin_addr
.s_addr
&0xFF000000)>>24,
2667 lpwfs
->lstnSocketAddress
.sin_port
& 0xFF,
2668 (lpwfs
->lstnSocketAddress
.sin_port
& 0xFF00)>>8);
2670 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PORT
, szIPAddress
, 0, 0, 0))
2673 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2676 if (nResCode
== 200)
2679 FTP_SetResponseError(nResCode
);
2687 /***********************************************************************
2688 * FTP_DoPassive (internal)
2690 * Tell server that we want to do passive transfers
2691 * and connect data socket
2698 static BOOL
FTP_DoPassive(LPWININETFTPSESSIONW lpwfs
)
2701 BOOL bSuccess
= FALSE
;
2704 if (!FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_PASV
, NULL
, 0, 0, 0))
2707 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2710 if (nResCode
== 227)
2712 LPSTR lpszResponseBuffer
= INTERNET_GetResponseBuffer();
2716 char *pAddr
, *pPort
;
2718 struct sockaddr_in dataSocketAddress
;
2720 p
= lpszResponseBuffer
+4; /* skip status code */
2721 while (*p
!= '\0' && (*p
< '0' || *p
> '9')) p
++;
2725 ERR("no address found in response, aborting\n");
2729 if (sscanf(p
, "%d,%d,%d,%d,%d,%d", &f
[0], &f
[1], &f
[2], &f
[3],
2732 ERR("unknown response address format '%s', aborting\n", p
);
2735 for (i
=0; i
< 6; i
++)
2738 dataSocketAddress
= lpwfs
->socketAddress
;
2739 pAddr
= (char *)&(dataSocketAddress
.sin_addr
.s_addr
);
2740 pPort
= (char *)&(dataSocketAddress
.sin_port
);
2748 nsocket
= socket(AF_INET
,SOCK_STREAM
,0);
2752 if (connect(nsocket
, (struct sockaddr
*)&dataSocketAddress
, sizeof(dataSocketAddress
)))
2754 ERR("can't connect passive FTP data port.\n");
2755 closesocket(nsocket
);
2758 lpwfs
->pasvSocket
= nsocket
;
2762 FTP_SetResponseError(nResCode
);
2770 static BOOL
FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs
)
2772 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
2774 if (!FTP_DoPassive(lpwfs
))
2779 if (!FTP_SendPort(lpwfs
))
2786 /***********************************************************************
2787 * FTP_GetDataSocket (internal)
2789 * Either accepts an incoming data socket connection from the server
2790 * or just returns the already opened socket after a PASV command
2791 * in case of passive FTP.
2799 static BOOL
FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs
, LPINT nDataSocket
)
2801 struct sockaddr_in saddr
;
2802 socklen_t addrlen
= sizeof(struct sockaddr
);
2805 if (lpwfs
->hdr
.dwFlags
& INTERNET_FLAG_PASSIVE
)
2807 *nDataSocket
= lpwfs
->pasvSocket
;
2811 *nDataSocket
= accept(lpwfs
->lstnSocket
, (struct sockaddr
*) &saddr
, &addrlen
);
2812 closesocket(lpwfs
->lstnSocket
);
2813 lpwfs
->lstnSocket
= -1;
2815 return *nDataSocket
!= -1;
2819 /***********************************************************************
2820 * FTP_SendData (internal)
2822 * Send data to the server
2829 static BOOL
FTP_SendData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
)
2831 BY_HANDLE_FILE_INFORMATION fi
;
2832 DWORD nBytesRead
= 0;
2833 DWORD nBytesSent
= 0;
2834 DWORD nTotalSent
= 0;
2835 DWORD nBytesToSend
, nLen
;
2837 time_t s_long_time
, e_long_time
;
2842 lpszBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(CHAR
)*DATA_PACKET_SIZE
);
2844 /* Get the size of the file. */
2845 GetFileInformationByHandle(hFile
, &fi
);
2850 nBytesToSend
= nBytesRead
- nBytesSent
;
2852 if (nBytesToSend
<= 0)
2854 /* Read data from file. */
2856 if (!ReadFile(hFile
, lpszBuffer
, DATA_PACKET_SIZE
, &nBytesRead
, 0))
2857 ERR("Failed reading from file\n");
2860 nBytesToSend
= nBytesRead
;
2865 nLen
= DATA_PACKET_SIZE
< nBytesToSend
?
2866 DATA_PACKET_SIZE
: nBytesToSend
;
2867 nRC
= send(nDataSocket
, lpszBuffer
, nLen
, 0);
2875 /* Do some computation to display the status. */
2877 nSeconds
= e_long_time
- s_long_time
;
2878 if( nSeconds
/ 60 > 0 )
2880 TRACE( "%d bytes of %d bytes (%d%%) in %d min %d sec estimated remaining time %d sec\n",
2881 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
/ 60,
2882 nSeconds
% 60, (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
2886 TRACE( "%d bytes of %d bytes (%d%%) in %d sec estimated remaining time %d sec\n",
2887 nTotalSent
, fi
.nFileSizeLow
, nTotalSent
*100/fi
.nFileSizeLow
, nSeconds
,
2888 (fi
.nFileSizeLow
- nTotalSent
) * nSeconds
/ nTotalSent
);
2890 } while (nRC
!= -1);
2892 TRACE("file transfer complete!\n");
2894 HeapFree(GetProcessHeap(), 0, lpszBuffer
);
2900 /***********************************************************************
2901 * FTP_SendRetrieve (internal)
2903 * Send request to retrieve a file
2906 * Number of bytes to be received on success
2910 static BOOL
FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs
, LPCWSTR lpszRemoteFile
, DWORD dwType
)
2916 if (!(ret
= FTP_InitListenSocket(lpwfs
)))
2919 if (!(ret
= FTP_SendType(lpwfs
, dwType
)))
2922 if (!(ret
= FTP_SendPortOrPasv(lpwfs
)))
2925 if (!(ret
= FTP_SendCommand(lpwfs
->sndSocket
, FTP_CMD_RETR
, lpszRemoteFile
, 0, 0, 0)))
2928 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
2929 if ((nResCode
!= 125) && (nResCode
!= 150)) {
2930 /* That means that we got an error getting the file. */
2931 FTP_SetResponseError(nResCode
);
2936 if (!ret
&& lpwfs
->lstnSocket
!= -1)
2938 closesocket(lpwfs
->lstnSocket
);
2939 lpwfs
->lstnSocket
= -1;
2946 /***********************************************************************
2947 * FTP_RetrieveData (internal)
2949 * Retrieve data from server
2956 static BOOL
FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs
, INT nDataSocket
, HANDLE hFile
)
2958 DWORD nBytesWritten
;
2959 DWORD nBytesReceived
= 0;
2965 lpszBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(CHAR
)*DATA_PACKET_SIZE
);
2966 if (NULL
== lpszBuffer
)
2968 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2974 nRC
= recv(nDataSocket
, lpszBuffer
, DATA_PACKET_SIZE
, 0);
2977 /* other side closed socket. */
2980 WriteFile(hFile
, lpszBuffer
, nRC
, &nBytesWritten
, NULL
);
2981 nBytesReceived
+= nRC
;
2985 TRACE("Data transfer complete\n");
2988 HeapFree(GetProcessHeap(), 0, lpszBuffer
);
2993 /***********************************************************************
2994 * FTP_CloseConnection (internal)
2998 static void FTP_CloseConnection(LPWININETHANDLEHEADER hdr
)
3000 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) hdr
;
3004 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
3005 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
3007 if (lpwfs
->download_in_progress
!= NULL
)
3008 lpwfs
->download_in_progress
->session_deleted
= TRUE
;
3010 if (lpwfs
->sndSocket
!= -1)
3011 closesocket(lpwfs
->sndSocket
);
3013 if (lpwfs
->lstnSocket
!= -1)
3014 closesocket(lpwfs
->lstnSocket
);
3016 if (lpwfs
->pasvSocket
!= -1)
3017 closesocket(lpwfs
->pasvSocket
);
3019 SendAsyncCallback(&lpwfs
->hdr
, lpwfs
->hdr
.dwContext
,
3020 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
3024 /***********************************************************************
3025 * FTP_CloseSessionHandle (internal)
3027 * Deallocate session handle
3029 static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr
)
3031 LPWININETFTPSESSIONW lpwfs
= (LPWININETFTPSESSIONW
) hdr
;
3035 WININET_Release(&lpwfs
->lpAppInfo
->hdr
);
3037 HeapFree(GetProcessHeap(), 0, lpwfs
->lpszPassword
);
3038 HeapFree(GetProcessHeap(), 0, lpwfs
->lpszUserName
);
3039 HeapFree(GetProcessHeap(), 0, lpwfs
);
3043 /***********************************************************************
3044 * FTP_FindNextFileW (Internal)
3046 * Continues a file search from a previous call to FindFirstFile
3053 BOOL WINAPI
FTP_FindNextFileW(LPWININETFTPFINDNEXTW lpwh
, LPVOID lpvFindData
)
3055 BOOL bSuccess
= TRUE
;
3056 LPWIN32_FIND_DATAW lpFindFileData
;
3058 TRACE("index(%d) size(%d)\n", lpwh
->index
, lpwh
->size
);
3060 assert (lpwh
->hdr
.htype
== WH_HFTPFINDNEXT
);
3062 /* Clear any error information */
3063 INTERNET_SetLastError(0);
3065 lpFindFileData
= (LPWIN32_FIND_DATAW
) lpvFindData
;
3066 ZeroMemory(lpFindFileData
, sizeof(WIN32_FIND_DATAA
));
3068 if (lpwh
->index
>= lpwh
->size
)
3070 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
3075 FTP_ConvertFileProp(&lpwh
->lpafp
[lpwh
->index
], lpFindFileData
);
3078 TRACE("\nName: %s\nSize: %d\n", debugstr_w(lpFindFileData
->cFileName
), lpFindFileData
->nFileSizeLow
);
3082 if (lpwh
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
3084 INTERNET_ASYNC_RESULT iar
;
3086 iar
.dwResult
= (DWORD
)bSuccess
;
3087 iar
.dwError
= iar
.dwError
= bSuccess
? ERROR_SUCCESS
:
3088 INTERNET_GetLastError();
3090 INTERNET_SendCallback(&lpwh
->hdr
, lpwh
->hdr
.dwContext
,
3091 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
3092 sizeof(INTERNET_ASYNC_RESULT
));
3099 /***********************************************************************
3100 * FTP_CloseFindNextHandle (internal)
3102 * Deallocate session handle
3109 static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr
)
3111 LPWININETFTPFINDNEXTW lpwfn
= (LPWININETFTPFINDNEXTW
) hdr
;
3116 WININET_Release(&lpwfn
->lpFtpSession
->hdr
);
3118 for (i
= 0; i
< lpwfn
->size
; i
++)
3120 HeapFree(GetProcessHeap(), 0, lpwfn
->lpafp
[i
].lpszName
);
3123 HeapFree(GetProcessHeap(), 0, lpwfn
->lpafp
);
3124 HeapFree(GetProcessHeap(), 0, lpwfn
);
3127 /***********************************************************************
3128 * FTP_CloseFileTransferHandle (internal)
3130 * Closes the file transfer handle. This also 'cleans' the data queue of
3131 * the 'transfer complete' message (this is a bit of a hack though :-/ )
3134 static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr
)
3136 LPWININETFTPFILE lpwh
= (LPWININETFTPFILE
) hdr
;
3137 LPWININETFTPSESSIONW lpwfs
= lpwh
->lpFtpSession
;
3142 WININET_Release(&lpwh
->lpFtpSession
->hdr
);
3144 if (!lpwh
->session_deleted
)
3145 lpwfs
->download_in_progress
= NULL
;
3147 if (lpwh
->nDataSocket
!= -1)
3148 closesocket(lpwh
->nDataSocket
);
3150 nResCode
= FTP_ReceiveResponse(lpwfs
, lpwfs
->hdr
.dwContext
);
3151 if (nResCode
> 0 && nResCode
!= 226) WARN("server reports failed transfer\n");
3153 HeapFree(GetProcessHeap(), 0, lpwh
);
3156 /***********************************************************************
3157 * FTP_ReceiveFileList (internal)
3159 * Read file list from server
3162 * Handle to file list on success
3166 static HINTERNET
FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3167 LPWIN32_FIND_DATAW lpFindFileData
, DWORD_PTR dwContext
)
3170 LPFILEPROPERTIESW lpafp
= NULL
;
3171 LPWININETFTPFINDNEXTW lpwfn
= NULL
;
3172 HINTERNET handle
= 0;
3174 TRACE("(%p,%d,%s,%p,%08lx)\n", lpwfs
, nSocket
, debugstr_w(lpszSearchFile
), lpFindFileData
, dwContext
);
3176 if (FTP_ParseDirectory(lpwfs
, nSocket
, lpszSearchFile
, &lpafp
, &dwSize
))
3179 FTP_ConvertFileProp(lpafp
, lpFindFileData
);
3181 lpwfn
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETFTPFINDNEXTW
));
3184 lpwfn
->hdr
.htype
= WH_HFTPFINDNEXT
;
3185 lpwfn
->hdr
.dwContext
= dwContext
;
3186 lpwfn
->hdr
.dwRefCount
= 1;
3187 lpwfn
->hdr
.close_connection
= NULL
;
3188 lpwfn
->hdr
.destroy
= FTP_CloseFindNextHandle
;
3189 lpwfn
->hdr
.lpfnStatusCB
= lpwfs
->hdr
.lpfnStatusCB
;
3190 lpwfn
->index
= 1; /* Next index is 1 since we return index 0 */
3191 lpwfn
->size
= dwSize
;
3192 lpwfn
->lpafp
= lpafp
;
3194 WININET_AddRef( &lpwfs
->hdr
);
3195 lpwfn
->lpFtpSession
= lpwfs
;
3196 list_add_head( &lpwfs
->hdr
.children
, &lpwfn
->hdr
.entry
);
3198 handle
= WININET_AllocHandle( &lpwfn
->hdr
);
3203 WININET_Release( &lpwfn
->hdr
);
3205 TRACE("Matched %d files\n", dwSize
);
3210 /***********************************************************************
3211 * FTP_ConvertFileProp (internal)
3213 * Converts FILEPROPERTIESW struct to WIN32_FIND_DATAA
3220 BOOL
FTP_ConvertFileProp(LPFILEPROPERTIESW lpafp
, LPWIN32_FIND_DATAW lpFindFileData
)
3222 BOOL bSuccess
= FALSE
;
3224 ZeroMemory(lpFindFileData
, sizeof(WIN32_FIND_DATAW
));
3228 /* Convert 'Unix' time to Windows time */
3229 RtlSecondsSince1970ToTime(mktime(&lpafp
->tmLastModified
),
3230 (LARGE_INTEGER
*) &(lpFindFileData
->ftLastAccessTime
));
3231 lpFindFileData
->ftLastWriteTime
= lpFindFileData
->ftLastAccessTime
;
3232 lpFindFileData
->ftCreationTime
= lpFindFileData
->ftLastAccessTime
;
3234 /* Not all fields are filled in */
3235 lpFindFileData
->nFileSizeHigh
= 0; /* We do not handle files bigger than 0xFFFFFFFF bytes yet :-) */
3236 lpFindFileData
->nFileSizeLow
= lpafp
->nSize
;
3238 if (lpafp
->bIsDirectory
)
3239 lpFindFileData
->dwFileAttributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3241 if (lpafp
->lpszName
)
3242 lstrcpynW(lpFindFileData
->cFileName
, lpafp
->lpszName
, MAX_PATH
);
3250 /***********************************************************************
3251 * FTP_ParseNextFile (internal)
3253 * Parse the next line in file listing
3259 static BOOL
FTP_ParseNextFile(INT nSocket
, LPCWSTR lpszSearchFile
, LPFILEPROPERTIESW lpfp
)
3261 static const char szSpace
[] = " \t";
3269 lpfp
->lpszName
= NULL
;
3271 if(!(pszLine
= INTERNET_GetNextLine(nSocket
, &nBufLen
)))
3274 pszToken
= strtok(pszLine
, szSpace
);
3276 * <Permissions> <NoLinks> <owner> <group> <size> <date> <time or year> <filename>
3279 * drwx--s--- 2 pcarrier ens 512 Sep 28 1995 pcarrier
3281 if(!isdigit(pszToken
[0]) && 10 == strlen(pszToken
)) {
3282 if(!FTP_ParsePermission(pszToken
, lpfp
))
3283 lpfp
->bIsDirectory
= FALSE
;
3284 for(i
=0; i
<=3; i
++) {
3285 if(!(pszToken
= strtok(NULL
, szSpace
)))
3288 if(!pszToken
) continue;
3289 if(lpfp
->bIsDirectory
) {
3290 TRACE("Is directory\n");
3294 TRACE("Size: %s\n", pszToken
);
3295 lpfp
->nSize
= atol(pszToken
);
3298 lpfp
->tmLastModified
.tm_sec
= 0;
3299 lpfp
->tmLastModified
.tm_min
= 0;
3300 lpfp
->tmLastModified
.tm_hour
= 0;
3301 lpfp
->tmLastModified
.tm_mday
= 0;
3302 lpfp
->tmLastModified
.tm_mon
= 0;
3303 lpfp
->tmLastModified
.tm_year
= 0;
3305 /* Determine month */
3306 pszToken
= strtok(NULL
, szSpace
);
3307 if(!pszToken
) continue;
3308 if(strlen(pszToken
) >= 3) {
3310 if((pszTmp
= StrStrIA(szMonths
, pszToken
)))
3311 lpfp
->tmLastModified
.tm_mon
= ((pszTmp
- szMonths
) / 3)+1;
3314 pszToken
= strtok(NULL
, szSpace
);
3315 if(!pszToken
) continue;
3316 lpfp
->tmLastModified
.tm_mday
= atoi(pszToken
);
3317 /* Determine time or year */
3318 pszToken
= strtok(NULL
, szSpace
);
3319 if(!pszToken
) continue;
3320 if((pszTmp
= strchr(pszToken
, ':'))) {
3325 lpfp
->tmLastModified
.tm_min
= atoi(pszTmp
);
3326 lpfp
->tmLastModified
.tm_hour
= atoi(pszToken
);
3328 apTM
= localtime(&aTime
);
3329 lpfp
->tmLastModified
.tm_year
= apTM
->tm_year
;
3332 lpfp
->tmLastModified
.tm_year
= atoi(pszToken
) - 1900;
3333 lpfp
->tmLastModified
.tm_hour
= 12;
3335 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3336 lpfp
->tmLastModified
.tm_hour
, lpfp
->tmLastModified
.tm_min
, lpfp
->tmLastModified
.tm_sec
,
3337 (lpfp
->tmLastModified
.tm_year
>= 100) ? lpfp
->tmLastModified
.tm_year
- 100 : lpfp
->tmLastModified
.tm_year
,
3338 lpfp
->tmLastModified
.tm_mon
, lpfp
->tmLastModified
.tm_mday
);
3340 pszToken
= strtok(NULL
, szSpace
);
3341 if(!pszToken
) continue;
3342 lpfp
->lpszName
= WININET_strdup_AtoW(pszToken
);
3343 TRACE("File: %s\n", debugstr_w(lpfp
->lpszName
));
3345 /* NT way of parsing ... :
3347 07-13-03 08:55PM <DIR> sakpatch
3348 05-09-03 06:02PM 12656686 2003-04-21bgm_cmd_e.rgz
3350 else if(isdigit(pszToken
[0]) && 8 == strlen(pszToken
)) {
3351 lpfp
->permissions
= 0xFFFF; /* No idea, put full permission :-) */
3353 sscanf(pszToken
, "%d-%d-%d",
3354 &lpfp
->tmLastModified
.tm_mon
,
3355 &lpfp
->tmLastModified
.tm_mday
,
3356 &lpfp
->tmLastModified
.tm_year
);
3358 /* Hacky and bad Y2K protection :-) */
3359 if (lpfp
->tmLastModified
.tm_year
< 70)
3360 lpfp
->tmLastModified
.tm_year
+= 100;
3362 pszToken
= strtok(NULL
, szSpace
);
3363 if(!pszToken
) continue;
3364 sscanf(pszToken
, "%d:%d",
3365 &lpfp
->tmLastModified
.tm_hour
,
3366 &lpfp
->tmLastModified
.tm_min
);
3367 if((pszToken
[5] == 'P') && (pszToken
[6] == 'M')) {
3368 lpfp
->tmLastModified
.tm_hour
+= 12;
3370 lpfp
->tmLastModified
.tm_sec
= 0;
3372 TRACE("Mod time: %02d:%02d:%02d %02d/%02d/%02d\n",
3373 lpfp
->tmLastModified
.tm_hour
, lpfp
->tmLastModified
.tm_min
, lpfp
->tmLastModified
.tm_sec
,
3374 (lpfp
->tmLastModified
.tm_year
>= 100) ? lpfp
->tmLastModified
.tm_year
- 100 : lpfp
->tmLastModified
.tm_year
,
3375 lpfp
->tmLastModified
.tm_mon
, lpfp
->tmLastModified
.tm_mday
);
3377 pszToken
= strtok(NULL
, szSpace
);
3378 if(!pszToken
) continue;
3379 if(!strcasecmp(pszToken
, "<DIR>")) {
3380 lpfp
->bIsDirectory
= TRUE
;
3382 TRACE("Is directory\n");
3385 lpfp
->bIsDirectory
= FALSE
;
3386 lpfp
->nSize
= atol(pszToken
);
3387 TRACE("Size: %d\n", lpfp
->nSize
);
3390 pszToken
= strtok(NULL
, szSpace
);
3391 if(!pszToken
) continue;
3392 lpfp
->lpszName
= WININET_strdup_AtoW(pszToken
);
3393 TRACE("Name: %s\n", debugstr_w(lpfp
->lpszName
));
3395 /* EPLF format - http://cr.yp.to/ftp/list/eplf.html */
3396 else if(pszToken
[0] == '+') {
3397 FIXME("EPLF Format not implemented\n");
3400 if(lpfp
->lpszName
) {
3401 if((lpszSearchFile
== NULL
) ||
3402 (PathMatchSpecW(lpfp
->lpszName
, lpszSearchFile
))) {
3404 TRACE("Matched: %s\n", debugstr_w(lpfp
->lpszName
));
3407 HeapFree(GetProcessHeap(), 0, lpfp
->lpszName
);
3408 lpfp
->lpszName
= NULL
;
3415 /***********************************************************************
3416 * FTP_ParseDirectory (internal)
3418 * Parse string of directory information
3424 static BOOL
FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs
, INT nSocket
, LPCWSTR lpszSearchFile
,
3425 LPFILEPROPERTIESW
*lpafp
, LPDWORD dwfp
)
3427 BOOL bSuccess
= TRUE
;
3428 INT sizeFilePropArray
= 500;/*20; */
3429 INT indexFilePropArray
= -1;
3433 /* Allocate initial file properties array */
3434 *lpafp
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(FILEPROPERTIESW
)*(sizeFilePropArray
));
3439 if (indexFilePropArray
+1 >= sizeFilePropArray
)
3441 LPFILEPROPERTIESW tmpafp
;
3443 sizeFilePropArray
*= 2;
3444 tmpafp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *lpafp
,
3445 sizeof(FILEPROPERTIESW
)*sizeFilePropArray
);
3454 indexFilePropArray
++;
3455 } while (FTP_ParseNextFile(nSocket
, lpszSearchFile
, &(*lpafp
)[indexFilePropArray
]));
3457 if (bSuccess
&& indexFilePropArray
)
3459 if (indexFilePropArray
< sizeFilePropArray
- 1)
3461 LPFILEPROPERTIESW tmpafp
;
3463 tmpafp
= HeapReAlloc(GetProcessHeap(), 0, *lpafp
,
3464 sizeof(FILEPROPERTIESW
)*indexFilePropArray
);
3468 *dwfp
= indexFilePropArray
;
3472 HeapFree(GetProcessHeap(), 0, *lpafp
);
3473 INTERNET_SetLastError(ERROR_NO_MORE_FILES
);
3481 /***********************************************************************
3482 * FTP_ParsePermission (internal)
3484 * Parse permission string of directory information
3491 static BOOL
FTP_ParsePermission(LPCSTR lpszPermission
, LPFILEPROPERTIESW lpfp
)
3493 BOOL bSuccess
= TRUE
;
3494 unsigned short nPermission
= 0;
3499 if ((*lpszPermission
!= 'd') && (*lpszPermission
!= '-') && (*lpszPermission
!= 'l'))
3505 lpfp
->bIsDirectory
= (*lpszPermission
== 'd');
3511 nPermission
|= (*(lpszPermission
+1) == 'r' ? 1 : 0) << 8;
3514 nPermission
|= (*(lpszPermission
+2) == 'w' ? 1 : 0) << 7;
3517 nPermission
|= (*(lpszPermission
+3) == 'x' ? 1 : 0) << 6;
3520 nPermission
|= (*(lpszPermission
+4) == 'r' ? 1 : 0) << 5;
3523 nPermission
|= (*(lpszPermission
+5) == 'w' ? 1 : 0) << 4;
3526 nPermission
|= (*(lpszPermission
+6) == 'x' ? 1 : 0) << 3;
3529 nPermission
|= (*(lpszPermission
+7) == 'r' ? 1 : 0) << 2;
3532 nPermission
|= (*(lpszPermission
+8) == 'w' ? 1 : 0) << 1;
3535 nPermission
|= (*(lpszPermission
+9) == 'x' ? 1 : 0);
3539 }while (nPos
<= nLast
);
3541 lpfp
->permissions
= nPermission
;
3546 /***********************************************************************
3547 * FTP_SetResponseError (internal)
3549 * Set the appropriate error code for a given response from the server
3554 static DWORD
FTP_SetResponseError(DWORD dwResponse
)
3560 case 425: /* Cannot open data connection. */
3561 dwCode
= ERROR_INTERNET_CANNOT_CONNECT
;
3564 case 426: /* Connection closed, transer aborted. */
3565 dwCode
= ERROR_INTERNET_CONNECTION_ABORTED
;
3568 case 530: /* Not logged in. Login incorrect. */
3569 dwCode
= ERROR_INTERNET_LOGIN_FAILURE
;
3572 case 421: /* Service not available - Server may be shutting down. */
3573 case 450: /* File action not taken. File may be busy. */
3574 case 451: /* Action aborted. Server error. */
3575 case 452: /* Action not taken. Insufficient storage space on server. */
3576 case 500: /* Syntax error. Command unrecognized. */
3577 case 501: /* Syntax error. Error in parameters or arguments. */
3578 case 502: /* Command not implemented. */
3579 case 503: /* Bad sequence of commands. */
3580 case 504: /* Command not implemented for that parameter. */
3581 case 532: /* Need account for storing files */
3582 case 550: /* File action not taken. File not found or no access. */
3583 case 551: /* Requested action aborted. Page type unknown */
3584 case 552: /* Action aborted. Exceeded storage allocation */
3585 case 553: /* Action not taken. File name not allowed. */
3588 dwCode
= ERROR_INTERNET_EXTENDED_ERROR
;
3592 INTERNET_SetLastError(dwCode
);