2 * nanoftp.c: basic FTP client support
11 #define HAVE_SYS_SOCKET_H
12 #define HAVE_NETINET_IN_H
14 #define HAVE_SYS_TIME_H
22 #ifdef LIBXML_FTP_ENABLED
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
37 #ifdef HAVE_ARPA_INET_H
38 #include <arpa/inet.h>
49 #ifdef HAVE_SYS_TIME_H
52 #ifdef HAVE_SYS_SELECT_H
53 #include <sys/select.h>
55 #ifdef HAVE_SYS_SOCKET_H
56 #include <sys/socket.h>
58 #ifdef HAVE_SYS_TYPES_H
59 #include <sys/types.h>
65 #include <libxml/xmlmemory.h>
66 #include <libxml/parser.h>
67 #include <libxml/xmlerror.h>
68 #include <libxml/uri.h>
69 #include <libxml/nanoftp.h>
70 #include <libxml/globals.h>
72 /* #define DEBUG_FTP 1 */
80 #if defined(__MINGW32__) || defined(_WIN32_WCE)
84 #include <wsockcompat.h>
87 #define XML_SOCKLEN_T unsigned int
91 * A couple portability macros
94 #if !defined(__BEOS__) || defined(__HAIKU__)
95 #define closesocket(s) close(s)
101 #define PF_INET AF_INET
106 #ifdef HAVE_BROKEN_SS_FAMILY
107 #define ss_family __ss_family
111 #ifndef XML_SOCKLEN_T
112 #define XML_SOCKLEN_T unsigned int
115 #define FTP_COMMAND_OK 200
116 #define FTP_SYNTAX_ERROR 500
117 #define FTP_GET_PASSWD 331
118 #define FTP_BUF_SIZE 1024
120 #define XML_NANO_MAX_URLBUF 4096
122 typedef struct xmlNanoFTPCtxt
{
123 char *protocol
; /* the protocol name */
124 char *hostname
; /* the host name */
125 int port
; /* the port */
126 char *path
; /* the path within the URL */
127 char *user
; /* user string */
128 char *passwd
; /* passwd string */
130 struct sockaddr_storage ftpAddr
; /* this is large enough to hold IPv6 address*/
132 struct sockaddr_in ftpAddr
; /* the socket address struct */
134 int passive
; /* currently we support only passive !!! */
135 SOCKET controlFd
; /* the file descriptor for the control socket */
136 SOCKET dataFd
; /* the file descriptor for the data socket */
137 int state
; /* WRITE / READ / CLOSED */
138 int returnValue
; /* the protocol return value */
139 /* buffer for data received from the control connection */
140 char controlBuf
[FTP_BUF_SIZE
+ 1];
143 int controlBufAnswer
;
144 } xmlNanoFTPCtxt
, *xmlNanoFTPCtxtPtr
;
146 static int initialized
= 0;
147 static char *proxy
= NULL
; /* the proxy name if any */
148 static int proxyPort
= 0; /* the proxy port if any */
149 static char *proxyUser
= NULL
; /* user for proxy authentication */
150 static char *proxyPasswd
= NULL
;/* passwd for proxy authentication */
151 static int proxyType
= 0; /* uses TYPE or a@b ? */
155 int have_ipv6(void) {
158 s
= socket (AF_INET6
, SOCK_STREAM
, 0);
169 * @extra: extra informations
171 * Handle an out of memory condition
174 xmlFTPErrMemory(const char *extra
)
176 __xmlSimpleError(XML_FROM_FTP
, XML_ERR_NO_MEMORY
, NULL
, NULL
, extra
);
182 * Initialize the FTP protocol layer.
183 * Currently it just checks for proxy informations,
184 * and get the hostname
188 xmlNanoFTPInit(void) {
198 if (WSAStartup(MAKEWORD(1, 1), &wsaData
) != 0)
203 env
= getenv("no_proxy");
204 if (env
&& ((env
[0] == '*' ) && (env
[1] == 0)))
206 env
= getenv("ftp_proxy");
208 xmlNanoFTPScanProxy(env
);
210 env
= getenv("FTP_PROXY");
212 xmlNanoFTPScanProxy(env
);
215 env
= getenv("ftp_proxy_user");
217 proxyUser
= xmlMemStrdup(env
);
219 env
= getenv("ftp_proxy_password");
221 proxyPasswd
= xmlMemStrdup(env
);
229 * Cleanup the FTP protocol layer. This cleanup proxy informations.
233 xmlNanoFTPCleanup(void) {
238 if (proxyUser
!= NULL
) {
242 if (proxyPasswd
!= NULL
) {
243 xmlFree(proxyPasswd
);
255 * @host: the proxy host name
256 * @port: the proxy port
257 * @user: the proxy user name
258 * @passwd: the proxy password
259 * @type: the type of proxy 1 for using SITE, 2 for USER a@b
261 * Setup the FTP proxy informations.
262 * This can also be done by using ftp_proxy ftp_proxy_user and
263 * ftp_proxy_password environment variables.
267 xmlNanoFTPProxy(const char *host
, int port
, const char *user
,
268 const char *passwd
, int type
) {
273 if (proxyUser
!= NULL
) {
277 if (proxyPasswd
!= NULL
) {
278 xmlFree(proxyPasswd
);
282 proxy
= xmlMemStrdup(host
);
284 proxyUser
= xmlMemStrdup(user
);
286 proxyPasswd
= xmlMemStrdup(passwd
);
293 * @ctx: an FTP context
294 * @URL: The URL used to initialize the context
296 * (Re)Initialize an FTP context by parsing the URL and finding
297 * the protocol host port and path it indicates.
301 xmlNanoFTPScanURL(void *ctx
, const char *URL
) {
302 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
306 * Clear any existing data from the context
308 if (ctxt
->protocol
!= NULL
) {
309 xmlFree(ctxt
->protocol
);
310 ctxt
->protocol
= NULL
;
312 if (ctxt
->hostname
!= NULL
) {
313 xmlFree(ctxt
->hostname
);
314 ctxt
->hostname
= NULL
;
316 if (ctxt
->path
!= NULL
) {
320 if (URL
== NULL
) return;
322 uri
= xmlParseURIRaw(URL
, 1);
326 if ((uri
->scheme
== NULL
) || (uri
->server
== NULL
)) {
331 ctxt
->protocol
= xmlMemStrdup(uri
->scheme
);
332 ctxt
->hostname
= xmlMemStrdup(uri
->server
);
333 if (uri
->path
!= NULL
)
334 ctxt
->path
= xmlMemStrdup(uri
->path
);
336 ctxt
->path
= xmlMemStrdup("/");
338 ctxt
->port
= uri
->port
;
340 if (uri
->user
!= NULL
) {
342 if ((cptr
=strchr(uri
->user
, ':')) == NULL
)
343 ctxt
->user
= xmlMemStrdup(uri
->user
);
345 ctxt
->user
= (char *)xmlStrndup((xmlChar
*)uri
->user
,
347 ctxt
->passwd
= xmlMemStrdup(cptr
+1);
356 * xmlNanoFTPUpdateURL:
357 * @ctx: an FTP context
358 * @URL: The URL used to update the context
360 * Update an FTP context by parsing the URL and finding
361 * new path it indicates. If there is an error in the
362 * protocol, hostname, port or other information, the
363 * error is raised. It indicates a new connection has to
366 * Returns 0 if Ok, -1 in case of error (other host).
370 xmlNanoFTPUpdateURL(void *ctx
, const char *URL
) {
371 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
378 if (ctxt
->protocol
== NULL
)
380 if (ctxt
->hostname
== NULL
)
383 uri
= xmlParseURIRaw(URL
, 1);
387 if ((uri
->scheme
== NULL
) || (uri
->server
== NULL
)) {
391 if ((strcmp(ctxt
->protocol
, uri
->scheme
)) ||
392 (strcmp(ctxt
->hostname
, uri
->server
)) ||
393 ((uri
->port
!= 0) && (ctxt
->port
!= uri
->port
))) {
399 ctxt
->port
= uri
->port
;
401 if (ctxt
->path
!= NULL
) {
406 if (uri
->path
== NULL
)
407 ctxt
->path
= xmlMemStrdup("/");
409 ctxt
->path
= xmlMemStrdup(uri
->path
);
417 * xmlNanoFTPScanProxy:
418 * @URL: The proxy URL used to initialize the proxy context
420 * (Re)Initialize the FTP Proxy context by parsing the URL and finding
421 * the protocol host port it indicates.
422 * Should be like ftp://myproxy/ or ftp://myproxy:3128/
423 * A NULL URL cleans up proxy informations.
427 xmlNanoFTPScanProxy(const char *URL
) {
438 xmlGenericError(xmlGenericErrorContext
,
439 "Removing FTP proxy info\n");
441 xmlGenericError(xmlGenericErrorContext
,
442 "Using FTP proxy %s\n", URL
);
444 if (URL
== NULL
) return;
446 uri
= xmlParseURIRaw(URL
, 1);
447 if ((uri
== NULL
) || (uri
->scheme
== NULL
) ||
448 (strcmp(uri
->scheme
, "ftp")) || (uri
->server
== NULL
)) {
449 __xmlIOErr(XML_FROM_FTP
, XML_FTP_URL_SYNTAX
, "Syntax Error\n");
455 proxy
= xmlMemStrdup(uri
->server
);
457 proxyPort
= uri
->port
;
464 * @URL: The URL used to initialize the context
466 * Allocate and initialize a new FTP context.
468 * Returns an FTP context or NULL in case of error.
472 xmlNanoFTPNewCtxt(const char *URL
) {
473 xmlNanoFTPCtxtPtr ret
;
476 ret
= (xmlNanoFTPCtxtPtr
) xmlMalloc(sizeof(xmlNanoFTPCtxt
));
478 xmlFTPErrMemory("allocating FTP context");
482 memset(ret
, 0, sizeof(xmlNanoFTPCtxt
));
485 ret
->returnValue
= 0;
486 ret
->controlBufIndex
= 0;
487 ret
->controlBufUsed
= 0;
488 ret
->controlFd
= INVALID_SOCKET
;
490 unescaped
= xmlURIUnescapeString(URL
, 0, NULL
);
491 if (unescaped
!= NULL
) {
492 xmlNanoFTPScanURL(ret
, unescaped
);
494 } else if (URL
!= NULL
)
495 xmlNanoFTPScanURL(ret
, URL
);
501 * xmlNanoFTPFreeCtxt:
502 * @ctx: an FTP context
504 * Frees the context after closing the connection.
508 xmlNanoFTPFreeCtxt(void * ctx
) {
509 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
510 if (ctxt
== NULL
) return;
511 if (ctxt
->hostname
!= NULL
) xmlFree(ctxt
->hostname
);
512 if (ctxt
->protocol
!= NULL
) xmlFree(ctxt
->protocol
);
513 if (ctxt
->path
!= NULL
) xmlFree(ctxt
->path
);
515 if (ctxt
->controlFd
!= INVALID_SOCKET
) closesocket(ctxt
->controlFd
);
516 ctxt
->controlFd
= INVALID_SOCKET
;
517 ctxt
->controlBufIndex
= -1;
518 ctxt
->controlBufUsed
= -1;
523 * xmlNanoFTPParseResponse:
524 * @buf: the buffer containing the response
525 * @len: the buffer length
527 * Parsing of the server answer, we just extract the code.
529 * returns 0 for errors
530 * +XXX for last line of response
531 * -XXX for response to be continued
534 xmlNanoFTPParseResponse(char *buf
, int len
) {
537 if (len
< 3) return(-1);
538 if ((*buf
>= '0') && (*buf
<= '9'))
539 val
= val
* 10 + (*buf
- '0');
543 if ((*buf
>= '0') && (*buf
<= '9'))
544 val
= val
* 10 + (*buf
- '0');
548 if ((*buf
>= '0') && (*buf
<= '9'))
549 val
= val
* 10 + (*buf
- '0');
560 * @ctx: an FTP context
562 * Read more information from the FTP control connection
563 * Returns the number of bytes read, < 0 indicates an error
566 xmlNanoFTPGetMore(void *ctx
) {
567 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
571 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
573 if ((ctxt
->controlBufIndex
< 0) || (ctxt
->controlBufIndex
> FTP_BUF_SIZE
)) {
575 xmlGenericError(xmlGenericErrorContext
,
576 "xmlNanoFTPGetMore : controlBufIndex = %d\n",
577 ctxt
->controlBufIndex
);
582 if ((ctxt
->controlBufUsed
< 0) || (ctxt
->controlBufUsed
> FTP_BUF_SIZE
)) {
584 xmlGenericError(xmlGenericErrorContext
,
585 "xmlNanoFTPGetMore : controlBufUsed = %d\n",
586 ctxt
->controlBufUsed
);
590 if (ctxt
->controlBufIndex
> ctxt
->controlBufUsed
) {
592 xmlGenericError(xmlGenericErrorContext
,
593 "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
594 ctxt
->controlBufIndex
, ctxt
->controlBufUsed
);
600 * First pack the control buffer
602 if (ctxt
->controlBufIndex
> 0) {
603 memmove(&ctxt
->controlBuf
[0], &ctxt
->controlBuf
[ctxt
->controlBufIndex
],
604 ctxt
->controlBufUsed
- ctxt
->controlBufIndex
);
605 ctxt
->controlBufUsed
-= ctxt
->controlBufIndex
;
606 ctxt
->controlBufIndex
= 0;
608 size
= FTP_BUF_SIZE
- ctxt
->controlBufUsed
;
611 xmlGenericError(xmlGenericErrorContext
,
612 "xmlNanoFTPGetMore : buffer full %d \n", ctxt
->controlBufUsed
);
618 * Read the amount left on the control connection
620 if ((len
= recv(ctxt
->controlFd
, &ctxt
->controlBuf
[ctxt
->controlBufIndex
],
622 __xmlIOErr(XML_FROM_FTP
, 0, "recv failed");
623 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
624 ctxt
->controlFd
= INVALID_SOCKET
;
628 xmlGenericError(xmlGenericErrorContext
,
629 "xmlNanoFTPGetMore : read %d [%d - %d]\n", len
,
630 ctxt
->controlBufUsed
, ctxt
->controlBufUsed
+ len
);
632 ctxt
->controlBufUsed
+= len
;
633 ctxt
->controlBuf
[ctxt
->controlBufUsed
] = 0;
639 * xmlNanoFTPReadResponse:
640 * @ctx: an FTP context
642 * Read the response from the FTP server after a command.
643 * Returns the code number
646 xmlNanoFTPReadResponse(void *ctx
) {
647 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
650 int res
= -1, cur
= -1;
652 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
656 * Assumes everything up to controlBuf[controlBufIndex] has been read
659 len
= xmlNanoFTPGetMore(ctx
);
663 if ((ctxt
->controlBufUsed
== 0) && (len
== 0)) {
666 ptr
= &ctxt
->controlBuf
[ctxt
->controlBufIndex
];
667 end
= &ctxt
->controlBuf
[ctxt
->controlBufUsed
];
670 xmlGenericError(xmlGenericErrorContext
,
671 "\n<<<\n%s\n--\n", ptr
);
674 cur
= xmlNanoFTPParseResponse(ptr
, end
- ptr
);
677 * Successfully scanned the control code, scratch
678 * till the end of the line, but keep the index to be
679 * able to analyze the result if needed.
683 ctxt
->controlBufAnswer
= ptr
- ctxt
->controlBuf
;
684 while ((ptr
< end
) && (*ptr
!= '\n')) ptr
++;
685 if (*ptr
== '\n') ptr
++;
686 if (*ptr
== '\r') ptr
++;
689 while ((ptr
< end
) && (*ptr
!= '\n')) ptr
++;
691 ctxt
->controlBufIndex
= ctxt
->controlBufUsed
;
694 if (*ptr
!= '\r') ptr
++;
697 if (res
< 0) goto get_more
;
698 ctxt
->controlBufIndex
= ptr
- ctxt
->controlBuf
;
700 ptr
= &ctxt
->controlBuf
[ctxt
->controlBufIndex
];
701 xmlGenericError(xmlGenericErrorContext
, "\n---\n%s\n--\n", ptr
);
705 xmlGenericError(xmlGenericErrorContext
, "Got %d\n", res
);
711 * xmlNanoFTPGetResponse:
712 * @ctx: an FTP context
714 * Get the response from the FTP server after a command.
715 * Returns the code number
719 xmlNanoFTPGetResponse(void *ctx
) {
722 res
= xmlNanoFTPReadResponse(ctx
);
728 * xmlNanoFTPCheckResponse:
729 * @ctx: an FTP context
731 * Check if there is a response from the FTP server after a command.
732 * Returns the code number, or 0
736 xmlNanoFTPCheckResponse(void *ctx
) {
737 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
741 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
745 FD_SET(ctxt
->controlFd
, &rfd
);
746 switch(select(ctxt
->controlFd
+ 1, &rfd
, NULL
, NULL
, &tv
)) {
750 __xmlIOErr(XML_FROM_FTP
, 0, "select");
755 return(xmlNanoFTPReadResponse(ctx
));
759 * Send the user authentication
763 xmlNanoFTPSendUser(void *ctx
) {
764 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
769 if (ctxt
->user
== NULL
)
770 snprintf(buf
, sizeof(buf
), "USER anonymous\r\n");
772 snprintf(buf
, sizeof(buf
), "USER %s\r\n", ctxt
->user
);
773 buf
[sizeof(buf
) - 1] = 0;
776 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
778 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
780 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
787 * Send the password authentication
791 xmlNanoFTPSendPasswd(void *ctx
) {
792 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
797 if (ctxt
->passwd
== NULL
)
798 snprintf(buf
, sizeof(buf
), "PASS anonymous@\r\n");
800 snprintf(buf
, sizeof(buf
), "PASS %s\r\n", ctxt
->passwd
);
801 buf
[sizeof(buf
) - 1] = 0;
804 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
806 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
808 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
816 * @ctx: an FTP context
818 * Send a QUIT command to the server
820 * Returns -1 in case of error, 0 otherwise
825 xmlNanoFTPQuit(void *ctx
) {
826 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
830 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
832 snprintf(buf
, sizeof(buf
), "QUIT\r\n");
835 xmlGenericError(xmlGenericErrorContext
, "%s", buf
); /* Just to be consistent, even though we know it can't have a % in it */
837 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
839 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
847 * @ctx: an FTP context
849 * Tries to open a control connection
851 * Returns -1 in case of error, 0 otherwise
855 xmlNanoFTPConnect(void *ctx
) {
856 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
860 int addrlen
= sizeof (struct sockaddr_in
);
864 if (ctxt
->hostname
== NULL
)
868 * do the blocking DNS query.
878 memset (&ctxt
->ftpAddr
, 0, sizeof(ctxt
->ftpAddr
));
882 struct addrinfo hints
, *tmp
, *result
;
885 memset (&hints
, 0, sizeof(hints
));
886 hints
.ai_socktype
= SOCK_STREAM
;
889 if (getaddrinfo (proxy
, NULL
, &hints
, &result
) != 0) {
890 __xmlIOErr(XML_FROM_FTP
, 0, "getaddrinfo failed");
895 if (getaddrinfo (ctxt
->hostname
, NULL
, &hints
, &result
) != 0) {
896 __xmlIOErr(XML_FROM_FTP
, 0, "getaddrinfo failed");
900 for (tmp
= result
; tmp
; tmp
= tmp
->ai_next
)
901 if (tmp
->ai_family
== AF_INET
|| tmp
->ai_family
== AF_INET6
)
906 freeaddrinfo (result
);
907 __xmlIOErr(XML_FROM_FTP
, 0, "getaddrinfo failed");
910 if (tmp
->ai_addrlen
> sizeof(ctxt
->ftpAddr
)) {
912 freeaddrinfo (result
);
913 __xmlIOErr(XML_FROM_FTP
, 0, "gethostbyname address mismatch");
916 if (tmp
->ai_family
== AF_INET6
) {
917 memcpy (&ctxt
->ftpAddr
, tmp
->ai_addr
, tmp
->ai_addrlen
);
918 ((struct sockaddr_in6
*) &ctxt
->ftpAddr
)->sin6_port
= htons (port
);
919 ctxt
->controlFd
= socket (AF_INET6
, SOCK_STREAM
, 0);
922 memcpy (&ctxt
->ftpAddr
, tmp
->ai_addr
, tmp
->ai_addrlen
);
923 ((struct sockaddr_in
*) &ctxt
->ftpAddr
)->sin_port
= htons (port
);
924 ctxt
->controlFd
= socket (AF_INET
, SOCK_STREAM
, 0);
926 addrlen
= tmp
->ai_addrlen
;
927 freeaddrinfo (result
);
933 hp
= gethostbyname (GETHOSTBYNAME_ARG_CAST proxy
);
935 hp
= gethostbyname (GETHOSTBYNAME_ARG_CAST ctxt
->hostname
);
937 __xmlIOErr(XML_FROM_FTP
, 0, "gethostbyname failed");
940 if ((unsigned int) hp
->h_length
>
941 sizeof(((struct sockaddr_in
*)&ctxt
->ftpAddr
)->sin_addr
)) {
942 __xmlIOErr(XML_FROM_FTP
, 0, "gethostbyname address mismatch");
949 ((struct sockaddr_in
*)&ctxt
->ftpAddr
)->sin_family
= AF_INET
;
950 memcpy (&((struct sockaddr_in
*)&ctxt
->ftpAddr
)->sin_addr
,
951 hp
->h_addr_list
[0], hp
->h_length
);
952 ((struct sockaddr_in
*)&ctxt
->ftpAddr
)->sin_port
=
953 (unsigned short)htons ((unsigned short)port
);
954 ctxt
->controlFd
= socket (AF_INET
, SOCK_STREAM
, 0);
955 addrlen
= sizeof (struct sockaddr_in
);
958 if (ctxt
->controlFd
== INVALID_SOCKET
) {
959 __xmlIOErr(XML_FROM_FTP
, 0, "socket failed");
966 if (connect(ctxt
->controlFd
, (struct sockaddr
*) &ctxt
->ftpAddr
,
968 __xmlIOErr(XML_FROM_FTP
, 0, "Failed to create a connection");
969 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
970 ctxt
->controlFd
= INVALID_SOCKET
;
975 * Wait for the HELLO from the server.
977 res
= xmlNanoFTPGetResponse(ctxt
);
979 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
980 ctxt
->controlFd
= INVALID_SOCKET
;
985 * State diagram for the login operation on the FTP server
990 * +---+ USER +---+------------->+---+
991 * | B |---------->| W | 2 ---->| E |
992 * +---+ +---+------ | -->+---+
995 * -------------- ----- | | |
1001 * +---+ PASS +---+ 2 | ------>+---+
1002 * | |---------->| W |------------->| S |
1003 * +---+ +---+ ---------->+---+
1006 * -------------- -------- |
1012 * +---+ ACCT +---+-- | ----->+---+
1013 * | |---------->| W | 4,5 -------->| F |
1014 * +---+ +---+------------->+---+
1016 * Of course in case of using a proxy this get really nasty and is not
1017 * standardized at all :-(
1023 if (proxyUser
!= NULL
) {
1025 * We need proxy auth
1027 snprintf(buf
, sizeof(buf
), "USER %s\r\n", proxyUser
);
1028 buf
[sizeof(buf
) - 1] = 0;
1031 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1033 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1035 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1036 closesocket(ctxt
->controlFd
);
1037 ctxt
->controlFd
= INVALID_SOCKET
;
1040 res
= xmlNanoFTPGetResponse(ctxt
);
1043 if (proxyPasswd
== NULL
)
1046 if (proxyPasswd
!= NULL
)
1047 snprintf(buf
, sizeof(buf
), "PASS %s\r\n", proxyPasswd
);
1049 snprintf(buf
, sizeof(buf
), "PASS anonymous@\r\n");
1050 buf
[sizeof(buf
) - 1] = 0;
1053 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1055 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1057 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1058 closesocket(ctxt
->controlFd
);
1059 ctxt
->controlFd
= INVALID_SOCKET
;
1062 res
= xmlNanoFTPGetResponse(ctxt
);
1064 closesocket(ctxt
->controlFd
);
1065 ctxt
->controlFd
= INVALID_SOCKET
;
1075 closesocket(ctxt
->controlFd
);
1076 ctxt
->controlFd
= INVALID_SOCKET
;
1082 * We assume we don't need more authentication to the proxy
1083 * and that it succeeded :-\
1085 switch (proxyType
) {
1087 /* we will try in sequence */
1089 /* Using SITE command */
1090 snprintf(buf
, sizeof(buf
), "SITE %s\r\n", ctxt
->hostname
);
1091 buf
[sizeof(buf
) - 1] = 0;
1094 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1096 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1098 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1099 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1100 ctxt
->controlFd
= INVALID_SOCKET
;
1103 res
= xmlNanoFTPGetResponse(ctxt
);
1105 /* we assume it worked :-\ 1 is error for SITE command */
1109 if (proxyType
== 1) {
1110 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1111 ctxt
->controlFd
= INVALID_SOCKET
;
1115 /* USER user@host command */
1116 if (ctxt
->user
== NULL
)
1117 snprintf(buf
, sizeof(buf
), "USER anonymous@%s\r\n",
1120 snprintf(buf
, sizeof(buf
), "USER %s@%s\r\n",
1121 ctxt
->user
, ctxt
->hostname
);
1122 buf
[sizeof(buf
) - 1] = 0;
1125 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1127 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1129 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1130 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1131 ctxt
->controlFd
= INVALID_SOCKET
;
1134 res
= xmlNanoFTPGetResponse(ctxt
);
1135 if ((res
== 1) || (res
== 2)) {
1136 /* we assume it worked :-\ */
1140 if (ctxt
->passwd
== NULL
)
1141 snprintf(buf
, sizeof(buf
), "PASS anonymous@\r\n");
1143 snprintf(buf
, sizeof(buf
), "PASS %s\r\n", ctxt
->passwd
);
1144 buf
[sizeof(buf
) - 1] = 0;
1147 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1149 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1151 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1152 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1153 ctxt
->controlFd
= INVALID_SOCKET
;
1156 res
= xmlNanoFTPGetResponse(ctxt
);
1157 if ((res
== 1) || (res
== 2)) {
1158 /* we assume it worked :-\ */
1162 if (proxyType
== 2) {
1163 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1164 ctxt
->controlFd
= INVALID_SOCKET
;
1169 * If you need support for other Proxy authentication scheme
1170 * send the code or at least the sequence in use.
1173 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1174 ctxt
->controlFd
= INVALID_SOCKET
;
1179 * Non-proxy handling.
1181 res
= xmlNanoFTPSendUser(ctxt
);
1183 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1184 ctxt
->controlFd
= INVALID_SOCKET
;
1187 res
= xmlNanoFTPGetResponse(ctxt
);
1198 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1199 ctxt
->controlFd
= INVALID_SOCKET
;
1202 res
= xmlNanoFTPSendPasswd(ctxt
);
1204 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1205 ctxt
->controlFd
= INVALID_SOCKET
;
1208 res
= xmlNanoFTPGetResponse(ctxt
);
1213 __xmlIOErr(XML_FROM_FTP
, XML_FTP_ACCNT
,
1214 "FTP server asking for ACCNT on anonymous\n");
1220 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1221 ctxt
->controlFd
= INVALID_SOCKET
;
1229 * xmlNanoFTPConnectTo:
1230 * @server: an FTP server name
1231 * @port: the port (use 21 if 0)
1233 * Tries to open a control connection to the given server/port
1235 * Returns an fTP context or NULL if it failed
1239 xmlNanoFTPConnectTo(const char *server
, int port
) {
1240 xmlNanoFTPCtxtPtr ctxt
;
1248 ctxt
= (xmlNanoFTPCtxtPtr
) xmlNanoFTPNewCtxt(NULL
);
1251 ctxt
->hostname
= xmlMemStrdup(server
);
1252 if (ctxt
->hostname
== NULL
) {
1253 xmlNanoFTPFreeCtxt(ctxt
);
1258 res
= xmlNanoFTPConnect(ctxt
);
1260 xmlNanoFTPFreeCtxt(ctxt
);
1268 * @ctx: an FTP context
1269 * @directory: a directory on the server
1271 * Tries to change the remote directory
1273 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
1277 xmlNanoFTPCwd(void *ctx
, const char *directory
) {
1278 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1283 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
1284 if (directory
== NULL
) return 0;
1287 * Expected response code for CWD:
1291 * 500, 501, 502, 421, 530, 550
1293 snprintf(buf
, sizeof(buf
), "CWD %s\r\n", directory
);
1294 buf
[sizeof(buf
) - 1] = 0;
1297 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1299 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1301 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1304 res
= xmlNanoFTPGetResponse(ctxt
);
1308 if (res
== 2) return(1);
1317 * @ctx: an FTP context
1318 * @file: a file or directory on the server
1320 * Tries to delete an item (file or directory) from server
1322 * Returns -1 incase of error, 1 if DELE worked, 0 if it failed
1326 xmlNanoFTPDele(void *ctx
, const char *file
) {
1327 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1332 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
) ||
1333 (file
== NULL
)) return(-1);
1336 * Expected response code for DELE:
1341 * 500, 501, 502, 421, 530
1344 snprintf(buf
, sizeof(buf
), "DELE %s\r\n", file
);
1345 buf
[sizeof(buf
) - 1] = 0;
1348 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1350 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1352 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1355 res
= xmlNanoFTPGetResponse(ctxt
);
1359 if (res
== 2) return(1);
1366 * xmlNanoFTPGetConnection:
1367 * @ctx: an FTP context
1369 * Try to open a data connection to the server. Currently only
1370 * passive mode is supported.
1372 * Returns -1 incase of error, 0 otherwise
1376 xmlNanoFTPGetConnection(void *ctx
) {
1377 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1378 char buf
[200], *cur
;
1381 unsigned char ad
[6], *adp
, *portp
;
1382 unsigned int temp
[6];
1384 struct sockaddr_storage dataAddr
;
1386 struct sockaddr_in dataAddr
;
1388 XML_SOCKLEN_T dataAddrLen
;
1390 if (ctxt
== NULL
) return INVALID_SOCKET
;
1392 memset (&dataAddr
, 0, sizeof(dataAddr
));
1394 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
) {
1395 ctxt
->dataFd
= socket (AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1396 ((struct sockaddr_in6
*)&dataAddr
)->sin6_family
= AF_INET6
;
1397 dataAddrLen
= sizeof(struct sockaddr_in6
);
1401 ctxt
->dataFd
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
1402 ((struct sockaddr_in
*)&dataAddr
)->sin_family
= AF_INET
;
1403 dataAddrLen
= sizeof (struct sockaddr_in
);
1406 if (ctxt
->dataFd
== INVALID_SOCKET
) {
1407 __xmlIOErr(XML_FROM_FTP
, 0, "socket failed");
1408 return INVALID_SOCKET
;
1411 if (ctxt
->passive
) {
1413 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
)
1414 snprintf (buf
, sizeof(buf
), "EPSV\r\n");
1417 snprintf (buf
, sizeof(buf
), "PASV\r\n");
1420 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1422 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1424 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1425 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1426 return INVALID_SOCKET
;
1428 res
= xmlNanoFTPReadResponse(ctx
);
1431 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1432 return INVALID_SOCKET
;
1435 * retry with an active connection
1437 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1441 cur
= &ctxt
->controlBuf
[ctxt
->controlBufAnswer
];
1442 while (((*cur
< '0') || (*cur
> '9')) && *cur
!= '\0') cur
++;
1444 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
) {
1445 if (sscanf (cur
, "%u", &temp
[0]) != 1) {
1446 __xmlIOErr(XML_FROM_FTP
, XML_FTP_EPSV_ANSWER
,
1447 "Invalid answer to EPSV\n");
1448 if (ctxt
->dataFd
!= INVALID_SOCKET
) {
1449 closesocket (ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1451 return INVALID_SOCKET
;
1453 memcpy (&((struct sockaddr_in6
*)&dataAddr
)->sin6_addr
, &((struct sockaddr_in6
*)&ctxt
->ftpAddr
)->sin6_addr
, sizeof(struct in6_addr
));
1454 ((struct sockaddr_in6
*)&dataAddr
)->sin6_port
= htons (temp
[0]);
1459 if (sscanf (cur
, "%u,%u,%u,%u,%u,%u", &temp
[0], &temp
[1], &temp
[2],
1460 &temp
[3], &temp
[4], &temp
[5]) != 6) {
1461 __xmlIOErr(XML_FROM_FTP
, XML_FTP_PASV_ANSWER
,
1462 "Invalid answer to PASV\n");
1463 if (ctxt
->dataFd
!= INVALID_SOCKET
) {
1464 closesocket (ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1466 return INVALID_SOCKET
;
1468 for (i
=0; i
<6; i
++) ad
[i
] = (unsigned char) (temp
[i
] & 0xff);
1469 memcpy (&((struct sockaddr_in
*)&dataAddr
)->sin_addr
, &ad
[0], 4);
1470 memcpy (&((struct sockaddr_in
*)&dataAddr
)->sin_port
, &ad
[4], 2);
1473 if (connect(ctxt
->dataFd
, (struct sockaddr
*) &dataAddr
, dataAddrLen
) < 0) {
1474 __xmlIOErr(XML_FROM_FTP
, 0, "Failed to create a data connection");
1475 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1476 return INVALID_SOCKET
;
1479 getsockname(ctxt
->dataFd
, (struct sockaddr
*) &dataAddr
, &dataAddrLen
);
1481 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
)
1482 ((struct sockaddr_in6
*)&dataAddr
)->sin6_port
= 0;
1485 ((struct sockaddr_in
*)&dataAddr
)->sin_port
= 0;
1487 if (bind(ctxt
->dataFd
, (struct sockaddr
*) &dataAddr
, dataAddrLen
) < 0) {
1488 __xmlIOErr(XML_FROM_FTP
, 0, "bind failed");
1489 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1490 return INVALID_SOCKET
;
1492 getsockname(ctxt
->dataFd
, (struct sockaddr
*) &dataAddr
, &dataAddrLen
);
1494 if (listen(ctxt
->dataFd
, 1) < 0) {
1495 __xmlIOErr(XML_FROM_FTP
, 0, "listen failed");
1496 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1497 return INVALID_SOCKET
;
1500 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
) {
1501 char buf6
[INET6_ADDRSTRLEN
];
1502 inet_ntop (AF_INET6
, &((struct sockaddr_in6
*)&dataAddr
)->sin6_addr
,
1503 buf6
, INET6_ADDRSTRLEN
);
1504 adp
= (unsigned char *) buf6
;
1505 portp
= (unsigned char *) &((struct sockaddr_in6
*)&dataAddr
)->sin6_port
;
1506 snprintf (buf
, sizeof(buf
), "EPRT |2|%s|%s|\r\n", adp
, portp
);
1510 adp
= (unsigned char *) &((struct sockaddr_in
*)&dataAddr
)->sin_addr
;
1511 portp
= (unsigned char *) &((struct sockaddr_in
*)&dataAddr
)->sin_port
;
1512 snprintf (buf
, sizeof(buf
), "PORT %d,%d,%d,%d,%d,%d\r\n",
1513 adp
[0] & 0xff, adp
[1] & 0xff, adp
[2] & 0xff, adp
[3] & 0xff,
1514 portp
[0] & 0xff, portp
[1] & 0xff);
1517 buf
[sizeof(buf
) - 1] = 0;
1520 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1523 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1525 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1526 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1527 return INVALID_SOCKET
;
1529 res
= xmlNanoFTPGetResponse(ctxt
);
1531 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1532 return INVALID_SOCKET
;
1535 return(ctxt
->dataFd
);
1540 * xmlNanoFTPCloseConnection:
1541 * @ctx: an FTP context
1543 * Close the data connection from the server
1545 * Returns -1 incase of error, 0 otherwise
1549 xmlNanoFTPCloseConnection(void *ctx
) {
1550 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1555 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
1557 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1561 FD_SET(ctxt
->controlFd
, &rfd
);
1563 FD_SET(ctxt
->controlFd
, &efd
);
1564 res
= select(ctxt
->controlFd
+ 1, &rfd
, NULL
, &efd
, &tv
);
1569 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1574 xmlGenericError(xmlGenericErrorContext
,
1575 "xmlNanoFTPCloseConnection: timeout\n");
1577 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1579 res
= xmlNanoFTPGetResponse(ctxt
);
1581 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1589 * xmlNanoFTPParseList:
1590 * @list: some data listing received from the server
1591 * @callback: the user callback
1592 * @userData: the user callback data
1594 * Parse at most one entry from the listing.
1596 * Returns -1 incase of error, the length of data parsed otherwise
1600 xmlNanoFTPParseList(const char *list
, ftpListCallback callback
, void *userData
) {
1601 const char *cur
= list
;
1611 unsigned long size
= 0;
1615 if (!strncmp(cur
, "total", 5)) {
1617 while (*cur
== ' ') cur
++;
1618 while ((*cur
>= '0') && (*cur
<= '9'))
1619 links
= (links
* 10) + (*cur
++ - '0');
1620 while ((*cur
== ' ') || (*cur
== '\n') || (*cur
== '\r'))
1623 } else if (*list
== '+') {
1626 while ((*cur
== ' ') || (*cur
== '\n') || (*cur
== '\r'))
1628 if (*cur
== 0) return(0);
1630 while (*cur
!= ' ') {
1634 if (*cur
== 0) return(0);
1637 while (*cur
== ' ') cur
++;
1638 if (*cur
== 0) return(0);
1639 while ((*cur
>= '0') && (*cur
<= '9'))
1640 links
= (links
* 10) + (*cur
++ - '0');
1641 while (*cur
== ' ') cur
++;
1642 if (*cur
== 0) return(0);
1644 while (*cur
!= ' ') {
1648 if (*cur
== 0) return(0);
1651 while (*cur
== ' ') cur
++;
1652 if (*cur
== 0) return(0);
1654 while (*cur
!= ' ') {
1658 if (*cur
== 0) return(0);
1661 while (*cur
== ' ') cur
++;
1662 if (*cur
== 0) return(0);
1663 while ((*cur
>= '0') && (*cur
<= '9'))
1664 size
= (size
* 10) + (*cur
++ - '0');
1665 while (*cur
== ' ') cur
++;
1666 if (*cur
== 0) return(0);
1668 while (*cur
!= ' ') {
1672 if (*cur
== 0) return(0);
1675 while (*cur
== ' ') cur
++;
1676 if (*cur
== 0) return(0);
1677 while ((*cur
>= '0') && (*cur
<= '9'))
1678 day
= (day
* 10) + (*cur
++ - '0');
1679 while (*cur
== ' ') cur
++;
1680 if (*cur
== 0) return(0);
1681 if ((cur
[1] == 0) || (cur
[2] == 0)) return(0);
1682 if ((cur
[1] == ':') || (cur
[2] == ':')) {
1683 while ((*cur
>= '0') && (*cur
<= '9'))
1684 hour
= (hour
* 10) + (*cur
++ - '0');
1685 if (*cur
== ':') cur
++;
1686 while ((*cur
>= '0') && (*cur
<= '9'))
1687 minute
= (minute
* 10) + (*cur
++ - '0');
1689 while ((*cur
>= '0') && (*cur
<= '9'))
1690 year
= (year
* 10) + (*cur
++ - '0');
1692 while (*cur
== ' ') cur
++;
1693 if (*cur
== 0) return(0);
1695 while ((*cur
!= '\n') && (*cur
!= '\r')) {
1697 filename
[i
++] = *cur
;
1699 if (*cur
== 0) return(0);
1702 if ((*cur
!= '\n') && (*cur
!= '\r'))
1704 while ((*cur
== '\n') || (*cur
== '\r'))
1707 if (callback
!= NULL
) {
1708 callback(userData
, filename
, attrib
, owner
, group
, size
, links
,
1709 year
, month
, day
, hour
, minute
);
1716 * @ctx: an FTP context
1717 * @callback: the user callback
1718 * @userData: the user callback data
1719 * @filename: optional files to list
1721 * Do a listing on the server. All files info are passed back
1724 * Returns -1 incase of error, 0 otherwise
1728 xmlNanoFTPList(void *ctx
, ftpListCallback callback
, void *userData
,
1729 const char *filename
) {
1730 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1737 if (ctxt
== NULL
) return (-1);
1738 if (filename
== NULL
) {
1739 if (xmlNanoFTPCwd(ctxt
, ctxt
->path
) < 1)
1741 ctxt
->dataFd
= xmlNanoFTPGetConnection(ctxt
);
1742 if (ctxt
->dataFd
== INVALID_SOCKET
)
1744 snprintf(buf
, sizeof(buf
), "LIST -L\r\n");
1746 if (filename
[0] != '/') {
1747 if (xmlNanoFTPCwd(ctxt
, ctxt
->path
) < 1)
1750 ctxt
->dataFd
= xmlNanoFTPGetConnection(ctxt
);
1751 if (ctxt
->dataFd
== INVALID_SOCKET
)
1753 snprintf(buf
, sizeof(buf
), "LIST -L %s\r\n", filename
);
1755 buf
[sizeof(buf
) - 1] = 0;
1758 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1760 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1762 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1763 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1766 res
= xmlNanoFTPReadResponse(ctxt
);
1768 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1776 FD_SET(ctxt
->dataFd
, &rfd
);
1778 FD_SET(ctxt
->dataFd
, &efd
);
1779 res
= select(ctxt
->dataFd
+ 1, &rfd
, NULL
, &efd
, &tv
);
1784 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1788 res
= xmlNanoFTPCheckResponse(ctxt
);
1790 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1791 ctxt
->dataFd
= INVALID_SOCKET
;
1795 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1802 if ((len
= recv(ctxt
->dataFd
, &buf
[indx
], sizeof(buf
) - (indx
+ 1), 0)) < 0) {
1803 __xmlIOErr(XML_FROM_FTP
, 0, "recv");
1804 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1805 ctxt
->dataFd
= INVALID_SOCKET
;
1809 write(1, &buf
[indx
], len
);
1815 res
= xmlNanoFTPParseList(&buf
[base
], callback
, userData
);
1819 memmove(&buf
[0], &buf
[base
], indx
- base
);
1822 xmlNanoFTPCloseConnection(ctxt
);
1827 * xmlNanoFTPGetSocket:
1828 * @ctx: an FTP context
1829 * @filename: the file to retrieve (or NULL if path is in context).
1831 * Initiate fetch of the given file from the server.
1833 * Returns the socket for the data connection, or <0 in case of error
1838 xmlNanoFTPGetSocket(void *ctx
, const char *filename
) {
1839 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1843 return INVALID_SOCKET
;
1844 if ((filename
== NULL
) && (ctxt
->path
== NULL
))
1845 return INVALID_SOCKET
;
1846 ctxt
->dataFd
= xmlNanoFTPGetConnection(ctxt
);
1847 if (ctxt
->dataFd
== INVALID_SOCKET
)
1848 return INVALID_SOCKET
;
1850 snprintf(buf
, sizeof(buf
), "TYPE I\r\n");
1853 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1855 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1857 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1858 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1859 return INVALID_SOCKET
;
1861 res
= xmlNanoFTPReadResponse(ctxt
);
1863 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1864 return INVALID_SOCKET
;
1866 if (filename
== NULL
)
1867 snprintf(buf
, sizeof(buf
), "RETR %s\r\n", ctxt
->path
);
1869 snprintf(buf
, sizeof(buf
), "RETR %s\r\n", filename
);
1870 buf
[sizeof(buf
) - 1] = 0;
1873 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1875 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1877 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1878 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1879 return INVALID_SOCKET
;
1881 res
= xmlNanoFTPReadResponse(ctxt
);
1883 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1884 return INVALID_SOCKET
;
1886 return(ctxt
->dataFd
);
1891 * @ctx: an FTP context
1892 * @callback: the user callback
1893 * @userData: the user callback data
1894 * @filename: the file to retrieve
1896 * Fetch the given file from the server. All data are passed back
1897 * in the callbacks. The last callback has a size of 0 block.
1899 * Returns -1 incase of error, 0 otherwise
1903 xmlNanoFTPGet(void *ctx
, ftpDataCallback callback
, void *userData
,
1904 const char *filename
) {
1905 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1911 if (ctxt
== NULL
) return(-1);
1912 if ((filename
== NULL
) && (ctxt
->path
== NULL
))
1914 if (callback
== NULL
)
1916 if (xmlNanoFTPGetSocket(ctxt
, filename
) == INVALID_SOCKET
)
1923 FD_SET(ctxt
->dataFd
, &rfd
);
1924 res
= select(ctxt
->dataFd
+ 1, &rfd
, NULL
, NULL
, &tv
);
1929 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1933 res
= xmlNanoFTPCheckResponse(ctxt
);
1935 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1936 ctxt
->dataFd
= INVALID_SOCKET
;
1940 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1946 if ((len
= recv(ctxt
->dataFd
, buf
, sizeof(buf
), 0)) < 0) {
1947 __xmlIOErr(XML_FROM_FTP
, 0, "recv failed");
1948 callback(userData
, buf
, len
);
1949 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1952 callback(userData
, buf
, len
);
1955 return(xmlNanoFTPCloseConnection(ctxt
));
1960 * @ctx: the FTP context
1962 * @len: the buffer length
1964 * This function tries to read @len bytes from the existing FTP connection
1965 * and saves them in @dest. This is a blocking call.
1967 * Returns the number of byte read. 0 is an indication of an end of connection.
1968 * -1 indicates a parameter error.
1971 xmlNanoFTPRead(void *ctx
, void *dest
, int len
) {
1972 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1974 if (ctx
== NULL
) return(-1);
1975 if (ctxt
->dataFd
== INVALID_SOCKET
) return(0);
1976 if (dest
== NULL
) return(-1);
1977 if (len
<= 0) return(0);
1979 len
= recv(ctxt
->dataFd
, dest
, len
, 0);
1982 __xmlIOErr(XML_FROM_FTP
, 0, "recv failed");
1983 xmlNanoFTPCloseConnection(ctxt
);
1986 xmlGenericError(xmlGenericErrorContext
, "Recvd %d bytes\n", len
);
1993 * @URL: the URL to the resource
1995 * Start to fetch the given ftp:// resource
1997 * Returns an FTP context, or NULL
2001 xmlNanoFTPOpen(const char *URL
) {
2002 xmlNanoFTPCtxtPtr ctxt
;
2006 if (URL
== NULL
) return(NULL
);
2007 if (strncmp("ftp://", URL
, 6)) return(NULL
);
2009 ctxt
= (xmlNanoFTPCtxtPtr
) xmlNanoFTPNewCtxt(URL
);
2010 if (ctxt
== NULL
) return(NULL
);
2011 if (xmlNanoFTPConnect(ctxt
) < 0) {
2012 xmlNanoFTPFreeCtxt(ctxt
);
2015 sock
= xmlNanoFTPGetSocket(ctxt
, ctxt
->path
);
2016 if (sock
== INVALID_SOCKET
) {
2017 xmlNanoFTPFreeCtxt(ctxt
);
2025 * @ctx: an FTP context
2027 * Close the connection and both control and transport
2029 * Returns -1 incase of error, 0 otherwise
2033 xmlNanoFTPClose(void *ctx
) {
2034 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
2039 if (ctxt
->dataFd
!= INVALID_SOCKET
) {
2040 closesocket(ctxt
->dataFd
);
2041 ctxt
->dataFd
= INVALID_SOCKET
;
2043 if (ctxt
->controlFd
!= INVALID_SOCKET
) {
2044 xmlNanoFTPQuit(ctxt
);
2045 closesocket(ctxt
->controlFd
);
2046 ctxt
->controlFd
= INVALID_SOCKET
;
2048 xmlNanoFTPFreeCtxt(ctxt
);
2053 /************************************************************************
2055 * Basic test in Standalone mode *
2057 ************************************************************************/
2059 void ftpList(void *userData
, const char *filename
, const char* attrib
,
2060 const char *owner
, const char *group
, unsigned long size
, int links
,
2061 int year
, const char *month
, int day
, int hour
, int minute
) {
2062 xmlGenericError(xmlGenericErrorContext
,
2063 "%s %s %s %ld %s\n", attrib
, owner
, group
, size
, filename
);
2066 void ftpData(void *userData
, const char *data
, int len
) {
2067 if (userData
== NULL
) return;
2069 fclose((FILE*)userData
);
2072 fwrite(data
, len
, 1, (FILE*)userData
);
2075 int main(int argc
, char **argv
) {
2078 char *tstfile
= NULL
;
2082 ctxt
= xmlNanoFTPNewCtxt(argv
[1]);
2083 if (xmlNanoFTPConnect(ctxt
) < 0) {
2084 xmlGenericError(xmlGenericErrorContext
,
2085 "Couldn't connect to %s\n", argv
[1]);
2091 ctxt
= xmlNanoFTPConnectTo("localhost", 0);
2093 xmlGenericError(xmlGenericErrorContext
,
2094 "Couldn't connect to localhost\n");
2097 xmlNanoFTPList(ctxt
, ftpList
, NULL
, tstfile
);
2098 output
= fopen("/tmp/tstdata", "w");
2099 if (output
!= NULL
) {
2100 if (xmlNanoFTPGet(ctxt
, ftpData
, (void *) output
, tstfile
) < 0)
2101 xmlGenericError(xmlGenericErrorContext
,
2102 "Failed to get file\n");
2105 xmlNanoFTPClose(ctxt
);
2109 #endif /* STANDALONE */
2110 #else /* !LIBXML_FTP_ENABLED */
2113 int main(int argc
, char **argv
) {
2114 xmlGenericError(xmlGenericErrorContext
,
2115 "%s : FTP support not compiled in\n", argv
[0]);
2118 #endif /* STANDALONE */
2119 #endif /* LIBXML_FTP_ENABLED */
2120 #define bottom_nanoftp
2121 #include "elfgcchack.h"