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
20 #ifdef LIBXML_FTP_ENABLED
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
47 #ifdef HAVE_SYS_TIME_H
50 #ifdef HAVE_SYS_SELECT_H
51 #include <sys/select.h>
53 #ifdef HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
63 #include <libxml/xmlmemory.h>
64 #include <libxml/parser.h>
65 #include <libxml/xmlerror.h>
66 #include <libxml/uri.h>
67 #include <libxml/nanoftp.h>
68 #include <libxml/globals.h>
70 /* #define DEBUG_FTP 1 */
78 #if defined(_WIN32) && !defined(__CYGWIN__)
79 #include <wsockcompat.h>
83 * A couple portability macros
86 #if !defined(__BEOS__) || defined(__HAIKU__)
87 #define closesocket(s) close(s)
93 #define PF_INET AF_INET
98 #ifdef HAVE_BROKEN_SS_FAMILY
99 #define ss_family __ss_family
103 #ifndef XML_SOCKLEN_T
104 #define XML_SOCKLEN_T unsigned int
107 #define FTP_COMMAND_OK 200
108 #define FTP_SYNTAX_ERROR 500
109 #define FTP_GET_PASSWD 331
110 #define FTP_BUF_SIZE 1024
112 #define XML_NANO_MAX_URLBUF 4096
114 typedef struct xmlNanoFTPCtxt
{
115 char *protocol
; /* the protocol name */
116 char *hostname
; /* the host name */
117 int port
; /* the port */
118 char *path
; /* the path within the URL */
119 char *user
; /* user string */
120 char *passwd
; /* passwd string */
122 struct sockaddr_storage ftpAddr
; /* this is large enough to hold IPv6 address*/
124 struct sockaddr_in ftpAddr
; /* the socket address struct */
126 int passive
; /* currently we support only passive !!! */
127 SOCKET controlFd
; /* the file descriptor for the control socket */
128 SOCKET dataFd
; /* the file descriptor for the data socket */
129 int state
; /* WRITE / READ / CLOSED */
130 int returnValue
; /* the protocol return value */
131 /* buffer for data received from the control connection */
132 char controlBuf
[FTP_BUF_SIZE
+ 1];
135 int controlBufAnswer
;
136 } xmlNanoFTPCtxt
, *xmlNanoFTPCtxtPtr
;
138 static int initialized
= 0;
139 static char *proxy
= NULL
; /* the proxy name if any */
140 static int proxyPort
= 0; /* the proxy port if any */
141 static char *proxyUser
= NULL
; /* user for proxy authentication */
142 static char *proxyPasswd
= NULL
;/* passwd for proxy authentication */
143 static int proxyType
= 0; /* uses TYPE or a@b ? */
147 int have_ipv6(void) {
150 s
= socket (AF_INET6
, SOCK_STREAM
, 0);
161 * @extra: extra informations
163 * Handle an out of memory condition
166 xmlFTPErrMemory(const char *extra
)
168 __xmlSimpleError(XML_FROM_FTP
, XML_ERR_NO_MEMORY
, NULL
, NULL
, extra
);
174 * Initialize the FTP protocol layer.
175 * Currently it just checks for proxy informations,
176 * and get the hostname
180 xmlNanoFTPInit(void) {
190 if (WSAStartup(MAKEWORD(1, 1), &wsaData
) != 0)
195 env
= getenv("no_proxy");
196 if (env
&& ((env
[0] == '*' ) && (env
[1] == 0)))
198 env
= getenv("ftp_proxy");
200 xmlNanoFTPScanProxy(env
);
202 env
= getenv("FTP_PROXY");
204 xmlNanoFTPScanProxy(env
);
207 env
= getenv("ftp_proxy_user");
209 proxyUser
= xmlMemStrdup(env
);
211 env
= getenv("ftp_proxy_password");
213 proxyPasswd
= xmlMemStrdup(env
);
221 * Cleanup the FTP protocol layer. This cleanup proxy informations.
225 xmlNanoFTPCleanup(void) {
230 if (proxyUser
!= NULL
) {
234 if (proxyPasswd
!= NULL
) {
235 xmlFree(proxyPasswd
);
247 * @host: the proxy host name
248 * @port: the proxy port
249 * @user: the proxy user name
250 * @passwd: the proxy password
251 * @type: the type of proxy 1 for using SITE, 2 for USER a@b
253 * Setup the FTP proxy informations.
254 * This can also be done by using ftp_proxy ftp_proxy_user and
255 * ftp_proxy_password environment variables.
259 xmlNanoFTPProxy(const char *host
, int port
, const char *user
,
260 const char *passwd
, int type
) {
265 if (proxyUser
!= NULL
) {
269 if (proxyPasswd
!= NULL
) {
270 xmlFree(proxyPasswd
);
274 proxy
= xmlMemStrdup(host
);
276 proxyUser
= xmlMemStrdup(user
);
278 proxyPasswd
= xmlMemStrdup(passwd
);
285 * @ctx: an FTP context
286 * @URL: The URL used to initialize the context
288 * (Re)Initialize an FTP context by parsing the URL and finding
289 * the protocol host port and path it indicates.
293 xmlNanoFTPScanURL(void *ctx
, const char *URL
) {
294 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
298 * Clear any existing data from the context
300 if (ctxt
->protocol
!= NULL
) {
301 xmlFree(ctxt
->protocol
);
302 ctxt
->protocol
= NULL
;
304 if (ctxt
->hostname
!= NULL
) {
305 xmlFree(ctxt
->hostname
);
306 ctxt
->hostname
= NULL
;
308 if (ctxt
->path
!= NULL
) {
312 if (URL
== NULL
) return;
314 uri
= xmlParseURIRaw(URL
, 1);
318 if ((uri
->scheme
== NULL
) || (uri
->server
== NULL
)) {
323 ctxt
->protocol
= xmlMemStrdup(uri
->scheme
);
324 ctxt
->hostname
= xmlMemStrdup(uri
->server
);
325 if (uri
->path
!= NULL
)
326 ctxt
->path
= xmlMemStrdup(uri
->path
);
328 ctxt
->path
= xmlMemStrdup("/");
330 ctxt
->port
= uri
->port
;
332 if (uri
->user
!= NULL
) {
334 if ((cptr
=strchr(uri
->user
, ':')) == NULL
)
335 ctxt
->user
= xmlMemStrdup(uri
->user
);
337 ctxt
->user
= (char *)xmlStrndup((xmlChar
*)uri
->user
,
339 ctxt
->passwd
= xmlMemStrdup(cptr
+1);
348 * xmlNanoFTPUpdateURL:
349 * @ctx: an FTP context
350 * @URL: The URL used to update the context
352 * Update an FTP context by parsing the URL and finding
353 * new path it indicates. If there is an error in the
354 * protocol, hostname, port or other information, the
355 * error is raised. It indicates a new connection has to
358 * Returns 0 if Ok, -1 in case of error (other host).
362 xmlNanoFTPUpdateURL(void *ctx
, const char *URL
) {
363 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
370 if (ctxt
->protocol
== NULL
)
372 if (ctxt
->hostname
== NULL
)
375 uri
= xmlParseURIRaw(URL
, 1);
379 if ((uri
->scheme
== NULL
) || (uri
->server
== NULL
)) {
383 if ((strcmp(ctxt
->protocol
, uri
->scheme
)) ||
384 (strcmp(ctxt
->hostname
, uri
->server
)) ||
385 ((uri
->port
!= 0) && (ctxt
->port
!= uri
->port
))) {
391 ctxt
->port
= uri
->port
;
393 if (ctxt
->path
!= NULL
) {
398 if (uri
->path
== NULL
)
399 ctxt
->path
= xmlMemStrdup("/");
401 ctxt
->path
= xmlMemStrdup(uri
->path
);
409 * xmlNanoFTPScanProxy:
410 * @URL: The proxy URL used to initialize the proxy context
412 * (Re)Initialize the FTP Proxy context by parsing the URL and finding
413 * the protocol host port it indicates.
414 * Should be like ftp://myproxy/ or ftp://myproxy:3128/
415 * A NULL URL cleans up proxy informations.
419 xmlNanoFTPScanProxy(const char *URL
) {
430 xmlGenericError(xmlGenericErrorContext
,
431 "Removing FTP proxy info\n");
433 xmlGenericError(xmlGenericErrorContext
,
434 "Using FTP proxy %s\n", URL
);
436 if (URL
== NULL
) return;
438 uri
= xmlParseURIRaw(URL
, 1);
439 if ((uri
== NULL
) || (uri
->scheme
== NULL
) ||
440 (strcmp(uri
->scheme
, "ftp")) || (uri
->server
== NULL
)) {
441 __xmlIOErr(XML_FROM_FTP
, XML_FTP_URL_SYNTAX
, "Syntax Error\n");
447 proxy
= xmlMemStrdup(uri
->server
);
449 proxyPort
= uri
->port
;
456 * @URL: The URL used to initialize the context
458 * Allocate and initialize a new FTP context.
460 * Returns an FTP context or NULL in case of error.
464 xmlNanoFTPNewCtxt(const char *URL
) {
465 xmlNanoFTPCtxtPtr ret
;
468 ret
= (xmlNanoFTPCtxtPtr
) xmlMalloc(sizeof(xmlNanoFTPCtxt
));
470 xmlFTPErrMemory("allocating FTP context");
474 memset(ret
, 0, sizeof(xmlNanoFTPCtxt
));
477 ret
->returnValue
= 0;
478 ret
->controlBufIndex
= 0;
479 ret
->controlBufUsed
= 0;
480 ret
->controlFd
= INVALID_SOCKET
;
482 unescaped
= xmlURIUnescapeString(URL
, 0, NULL
);
483 if (unescaped
!= NULL
) {
484 xmlNanoFTPScanURL(ret
, unescaped
);
486 } else if (URL
!= NULL
)
487 xmlNanoFTPScanURL(ret
, URL
);
493 * xmlNanoFTPFreeCtxt:
494 * @ctx: an FTP context
496 * Frees the context after closing the connection.
500 xmlNanoFTPFreeCtxt(void * ctx
) {
501 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
502 if (ctxt
== NULL
) return;
503 if (ctxt
->hostname
!= NULL
) xmlFree(ctxt
->hostname
);
504 if (ctxt
->protocol
!= NULL
) xmlFree(ctxt
->protocol
);
505 if (ctxt
->path
!= NULL
) xmlFree(ctxt
->path
);
506 if (ctxt
->user
!= NULL
) xmlFree(ctxt
->user
);
507 if (ctxt
->passwd
!= NULL
) xmlFree(ctxt
->passwd
);
509 if (ctxt
->controlFd
!= INVALID_SOCKET
) closesocket(ctxt
->controlFd
);
510 ctxt
->controlFd
= INVALID_SOCKET
;
511 ctxt
->controlBufIndex
= -1;
512 ctxt
->controlBufUsed
= -1;
517 * xmlNanoFTPParseResponse:
518 * @buf: the buffer containing the response
519 * @len: the buffer length
521 * Parsing of the server answer, we just extract the code.
523 * returns 0 for errors
524 * +XXX for last line of response
525 * -XXX for response to be continued
528 xmlNanoFTPParseResponse(char *buf
, int len
) {
531 if (len
< 3) return(-1);
532 if ((*buf
>= '0') && (*buf
<= '9'))
533 val
= val
* 10 + (*buf
- '0');
537 if ((*buf
>= '0') && (*buf
<= '9'))
538 val
= val
* 10 + (*buf
- '0');
542 if ((*buf
>= '0') && (*buf
<= '9'))
543 val
= val
* 10 + (*buf
- '0');
554 * @ctx: an FTP context
556 * Read more information from the FTP control connection
557 * Returns the number of bytes read, < 0 indicates an error
560 xmlNanoFTPGetMore(void *ctx
) {
561 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
565 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
567 if ((ctxt
->controlBufIndex
< 0) || (ctxt
->controlBufIndex
> FTP_BUF_SIZE
)) {
569 xmlGenericError(xmlGenericErrorContext
,
570 "xmlNanoFTPGetMore : controlBufIndex = %d\n",
571 ctxt
->controlBufIndex
);
576 if ((ctxt
->controlBufUsed
< 0) || (ctxt
->controlBufUsed
> FTP_BUF_SIZE
)) {
578 xmlGenericError(xmlGenericErrorContext
,
579 "xmlNanoFTPGetMore : controlBufUsed = %d\n",
580 ctxt
->controlBufUsed
);
584 if (ctxt
->controlBufIndex
> ctxt
->controlBufUsed
) {
586 xmlGenericError(xmlGenericErrorContext
,
587 "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
588 ctxt
->controlBufIndex
, ctxt
->controlBufUsed
);
594 * First pack the control buffer
596 if (ctxt
->controlBufIndex
> 0) {
597 memmove(&ctxt
->controlBuf
[0], &ctxt
->controlBuf
[ctxt
->controlBufIndex
],
598 ctxt
->controlBufUsed
- ctxt
->controlBufIndex
);
599 ctxt
->controlBufUsed
-= ctxt
->controlBufIndex
;
600 ctxt
->controlBufIndex
= 0;
602 size
= FTP_BUF_SIZE
- ctxt
->controlBufUsed
;
605 xmlGenericError(xmlGenericErrorContext
,
606 "xmlNanoFTPGetMore : buffer full %d \n", ctxt
->controlBufUsed
);
612 * Read the amount left on the control connection
614 if ((len
= recv(ctxt
->controlFd
, &ctxt
->controlBuf
[ctxt
->controlBufIndex
],
616 __xmlIOErr(XML_FROM_FTP
, 0, "recv failed");
617 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
618 ctxt
->controlFd
= INVALID_SOCKET
;
622 xmlGenericError(xmlGenericErrorContext
,
623 "xmlNanoFTPGetMore : read %d [%d - %d]\n", len
,
624 ctxt
->controlBufUsed
, ctxt
->controlBufUsed
+ len
);
626 ctxt
->controlBufUsed
+= len
;
627 ctxt
->controlBuf
[ctxt
->controlBufUsed
] = 0;
633 * xmlNanoFTPReadResponse:
634 * @ctx: an FTP context
636 * Read the response from the FTP server after a command.
637 * Returns the code number
640 xmlNanoFTPReadResponse(void *ctx
) {
641 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
644 int res
= -1, cur
= -1;
646 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
650 * Assumes everything up to controlBuf[controlBufIndex] has been read
653 len
= xmlNanoFTPGetMore(ctx
);
657 if ((ctxt
->controlBufUsed
== 0) && (len
== 0)) {
660 ptr
= &ctxt
->controlBuf
[ctxt
->controlBufIndex
];
661 end
= &ctxt
->controlBuf
[ctxt
->controlBufUsed
];
664 xmlGenericError(xmlGenericErrorContext
,
665 "\n<<<\n%s\n--\n", ptr
);
668 cur
= xmlNanoFTPParseResponse(ptr
, end
- ptr
);
671 * Successfully scanned the control code, scratch
672 * till the end of the line, but keep the index to be
673 * able to analyze the result if needed.
677 ctxt
->controlBufAnswer
= ptr
- ctxt
->controlBuf
;
678 while ((ptr
< end
) && (*ptr
!= '\n')) ptr
++;
679 if (*ptr
== '\n') ptr
++;
680 if (*ptr
== '\r') ptr
++;
683 while ((ptr
< end
) && (*ptr
!= '\n')) ptr
++;
685 ctxt
->controlBufIndex
= ctxt
->controlBufUsed
;
688 if (*ptr
!= '\r') ptr
++;
691 if (res
< 0) goto get_more
;
692 ctxt
->controlBufIndex
= ptr
- ctxt
->controlBuf
;
694 ptr
= &ctxt
->controlBuf
[ctxt
->controlBufIndex
];
695 xmlGenericError(xmlGenericErrorContext
, "\n---\n%s\n--\n", ptr
);
699 xmlGenericError(xmlGenericErrorContext
, "Got %d\n", res
);
705 * xmlNanoFTPGetResponse:
706 * @ctx: an FTP context
708 * Get the response from the FTP server after a command.
709 * Returns the code number
713 xmlNanoFTPGetResponse(void *ctx
) {
716 res
= xmlNanoFTPReadResponse(ctx
);
722 * xmlNanoFTPCheckResponse:
723 * @ctx: an FTP context
725 * Check if there is a response from the FTP server after a command.
726 * Returns the code number, or 0
730 xmlNanoFTPCheckResponse(void *ctx
) {
731 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
735 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
739 FD_SET(ctxt
->controlFd
, &rfd
);
740 switch(select(ctxt
->controlFd
+ 1, &rfd
, NULL
, NULL
, &tv
)) {
744 __xmlIOErr(XML_FROM_FTP
, 0, "select");
749 return(xmlNanoFTPReadResponse(ctx
));
753 * Send the user authentication
757 xmlNanoFTPSendUser(void *ctx
) {
758 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
763 if (ctxt
->user
== NULL
)
764 snprintf(buf
, sizeof(buf
), "USER anonymous\r\n");
766 snprintf(buf
, sizeof(buf
), "USER %s\r\n", ctxt
->user
);
767 buf
[sizeof(buf
) - 1] = 0;
770 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
772 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
774 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
781 * Send the password authentication
785 xmlNanoFTPSendPasswd(void *ctx
) {
786 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
791 if (ctxt
->passwd
== NULL
)
792 snprintf(buf
, sizeof(buf
), "PASS anonymous@\r\n");
794 snprintf(buf
, sizeof(buf
), "PASS %s\r\n", ctxt
->passwd
);
795 buf
[sizeof(buf
) - 1] = 0;
798 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
800 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
802 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
810 * @ctx: an FTP context
812 * Send a QUIT command to the server
814 * Returns -1 in case of error, 0 otherwise
819 xmlNanoFTPQuit(void *ctx
) {
820 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
824 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
826 snprintf(buf
, sizeof(buf
), "QUIT\r\n");
829 xmlGenericError(xmlGenericErrorContext
, "%s", buf
); /* Just to be consistent, even though we know it can't have a % in it */
831 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
833 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
841 * @ctx: an FTP context
843 * Tries to open a control connection
845 * Returns -1 in case of error, 0 otherwise
849 xmlNanoFTPConnect(void *ctx
) {
850 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
854 int addrlen
= sizeof (struct sockaddr_in
);
858 if (ctxt
->hostname
== NULL
)
862 * do the blocking DNS query.
872 memset (&ctxt
->ftpAddr
, 0, sizeof(ctxt
->ftpAddr
));
876 struct addrinfo hints
, *tmp
, *result
;
879 memset (&hints
, 0, sizeof(hints
));
880 hints
.ai_socktype
= SOCK_STREAM
;
883 if (getaddrinfo (proxy
, NULL
, &hints
, &result
) != 0) {
884 __xmlIOErr(XML_FROM_FTP
, 0, "getaddrinfo failed");
889 if (getaddrinfo (ctxt
->hostname
, NULL
, &hints
, &result
) != 0) {
890 __xmlIOErr(XML_FROM_FTP
, 0, "getaddrinfo failed");
894 for (tmp
= result
; tmp
; tmp
= tmp
->ai_next
)
895 if (tmp
->ai_family
== AF_INET
|| tmp
->ai_family
== AF_INET6
)
900 freeaddrinfo (result
);
901 __xmlIOErr(XML_FROM_FTP
, 0, "getaddrinfo failed");
904 if ((size_t)tmp
->ai_addrlen
> sizeof(ctxt
->ftpAddr
)) {
906 freeaddrinfo (result
);
907 __xmlIOErr(XML_FROM_FTP
, 0, "gethostbyname address mismatch");
910 if (tmp
->ai_family
== AF_INET6
) {
911 memcpy (&ctxt
->ftpAddr
, tmp
->ai_addr
, tmp
->ai_addrlen
);
912 ((struct sockaddr_in6
*) &ctxt
->ftpAddr
)->sin6_port
= htons (port
);
913 ctxt
->controlFd
= socket (AF_INET6
, SOCK_STREAM
, 0);
916 memcpy (&ctxt
->ftpAddr
, tmp
->ai_addr
, tmp
->ai_addrlen
);
917 ((struct sockaddr_in
*) &ctxt
->ftpAddr
)->sin_port
= htons (port
);
918 ctxt
->controlFd
= socket (AF_INET
, SOCK_STREAM
, 0);
920 addrlen
= tmp
->ai_addrlen
;
921 freeaddrinfo (result
);
927 hp
= gethostbyname (GETHOSTBYNAME_ARG_CAST proxy
);
929 hp
= gethostbyname (GETHOSTBYNAME_ARG_CAST ctxt
->hostname
);
931 __xmlIOErr(XML_FROM_FTP
, 0, "gethostbyname failed");
934 if ((unsigned int) hp
->h_length
>
935 sizeof(((struct sockaddr_in
*)&ctxt
->ftpAddr
)->sin_addr
)) {
936 __xmlIOErr(XML_FROM_FTP
, 0, "gethostbyname address mismatch");
943 ((struct sockaddr_in
*)&ctxt
->ftpAddr
)->sin_family
= AF_INET
;
944 memcpy (&((struct sockaddr_in
*)&ctxt
->ftpAddr
)->sin_addr
,
945 hp
->h_addr_list
[0], hp
->h_length
);
946 ((struct sockaddr_in
*)&ctxt
->ftpAddr
)->sin_port
=
947 (unsigned short)htons ((unsigned short)port
);
948 ctxt
->controlFd
= socket (AF_INET
, SOCK_STREAM
, 0);
949 addrlen
= sizeof (struct sockaddr_in
);
952 if (ctxt
->controlFd
== INVALID_SOCKET
) {
953 __xmlIOErr(XML_FROM_FTP
, 0, "socket failed");
960 if (connect(ctxt
->controlFd
, (struct sockaddr
*) &ctxt
->ftpAddr
,
962 __xmlIOErr(XML_FROM_FTP
, 0, "Failed to create a connection");
963 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
964 ctxt
->controlFd
= INVALID_SOCKET
;
969 * Wait for the HELLO from the server.
971 res
= xmlNanoFTPGetResponse(ctxt
);
973 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
974 ctxt
->controlFd
= INVALID_SOCKET
;
979 * State diagram for the login operation on the FTP server
984 * +---+ USER +---+------------->+---+
985 * | B |---------->| W | 2 ---->| E |
986 * +---+ +---+------ | -->+---+
989 * -------------- ----- | | |
995 * +---+ PASS +---+ 2 | ------>+---+
996 * | |---------->| W |------------->| S |
997 * +---+ +---+ ---------->+---+
1000 * -------------- -------- |
1006 * +---+ ACCT +---+-- | ----->+---+
1007 * | |---------->| W | 4,5 -------->| F |
1008 * +---+ +---+------------->+---+
1010 * Of course in case of using a proxy this get really nasty and is not
1011 * standardized at all :-(
1017 if (proxyUser
!= NULL
) {
1019 * We need proxy auth
1021 snprintf(buf
, sizeof(buf
), "USER %s\r\n", proxyUser
);
1022 buf
[sizeof(buf
) - 1] = 0;
1025 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1027 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1029 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1030 closesocket(ctxt
->controlFd
);
1031 ctxt
->controlFd
= INVALID_SOCKET
;
1034 res
= xmlNanoFTPGetResponse(ctxt
);
1037 if (proxyPasswd
== NULL
)
1039 /* Falls through. */
1041 if (proxyPasswd
!= NULL
)
1042 snprintf(buf
, sizeof(buf
), "PASS %s\r\n", proxyPasswd
);
1044 snprintf(buf
, sizeof(buf
), "PASS anonymous@\r\n");
1045 buf
[sizeof(buf
) - 1] = 0;
1048 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1050 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1052 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1053 closesocket(ctxt
->controlFd
);
1054 ctxt
->controlFd
= INVALID_SOCKET
;
1057 res
= xmlNanoFTPGetResponse(ctxt
);
1059 closesocket(ctxt
->controlFd
);
1060 ctxt
->controlFd
= INVALID_SOCKET
;
1070 closesocket(ctxt
->controlFd
);
1071 ctxt
->controlFd
= INVALID_SOCKET
;
1077 * We assume we don't need more authentication to the proxy
1078 * and that it succeeded :-\
1080 switch (proxyType
) {
1082 /* we will try in sequence */
1084 /* Using SITE command */
1085 snprintf(buf
, sizeof(buf
), "SITE %s\r\n", ctxt
->hostname
);
1086 buf
[sizeof(buf
) - 1] = 0;
1089 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1091 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1093 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1094 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1095 ctxt
->controlFd
= INVALID_SOCKET
;
1098 res
= xmlNanoFTPGetResponse(ctxt
);
1100 /* we assume it worked :-\ 1 is error for SITE command */
1104 if (proxyType
== 1) {
1105 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1106 ctxt
->controlFd
= INVALID_SOCKET
;
1109 /* Falls through. */
1111 /* USER user@host command */
1112 if (ctxt
->user
== NULL
)
1113 snprintf(buf
, sizeof(buf
), "USER anonymous@%s\r\n",
1116 snprintf(buf
, sizeof(buf
), "USER %s@%s\r\n",
1117 ctxt
->user
, ctxt
->hostname
);
1118 buf
[sizeof(buf
) - 1] = 0;
1121 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1123 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1125 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1126 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1127 ctxt
->controlFd
= INVALID_SOCKET
;
1130 res
= xmlNanoFTPGetResponse(ctxt
);
1131 if ((res
== 1) || (res
== 2)) {
1132 /* we assume it worked :-\ */
1136 if (ctxt
->passwd
== NULL
)
1137 snprintf(buf
, sizeof(buf
), "PASS anonymous@\r\n");
1139 snprintf(buf
, sizeof(buf
), "PASS %s\r\n", ctxt
->passwd
);
1140 buf
[sizeof(buf
) - 1] = 0;
1143 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1145 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1147 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1148 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1149 ctxt
->controlFd
= INVALID_SOCKET
;
1152 res
= xmlNanoFTPGetResponse(ctxt
);
1153 if ((res
== 1) || (res
== 2)) {
1154 /* we assume it worked :-\ */
1158 if (proxyType
== 2) {
1159 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1160 ctxt
->controlFd
= INVALID_SOCKET
;
1163 /* Falls through. */
1166 * If you need support for other Proxy authentication scheme
1167 * send the code or at least the sequence in use.
1170 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1171 ctxt
->controlFd
= INVALID_SOCKET
;
1176 * Non-proxy handling.
1178 res
= xmlNanoFTPSendUser(ctxt
);
1180 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1181 ctxt
->controlFd
= INVALID_SOCKET
;
1184 res
= xmlNanoFTPGetResponse(ctxt
);
1195 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1196 ctxt
->controlFd
= INVALID_SOCKET
;
1199 res
= xmlNanoFTPSendPasswd(ctxt
);
1201 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1202 ctxt
->controlFd
= INVALID_SOCKET
;
1205 res
= xmlNanoFTPGetResponse(ctxt
);
1210 __xmlIOErr(XML_FROM_FTP
, XML_FTP_ACCNT
,
1211 "FTP server asking for ACCNT on anonymous\n");
1212 /* Falls through. */
1218 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1219 ctxt
->controlFd
= INVALID_SOCKET
;
1227 * xmlNanoFTPConnectTo:
1228 * @server: an FTP server name
1229 * @port: the port (use 21 if 0)
1231 * Tries to open a control connection to the given server/port
1233 * Returns an fTP context or NULL if it failed
1237 xmlNanoFTPConnectTo(const char *server
, int port
) {
1238 xmlNanoFTPCtxtPtr ctxt
;
1246 ctxt
= (xmlNanoFTPCtxtPtr
) xmlNanoFTPNewCtxt(NULL
);
1249 ctxt
->hostname
= xmlMemStrdup(server
);
1250 if (ctxt
->hostname
== NULL
) {
1251 xmlNanoFTPFreeCtxt(ctxt
);
1256 res
= xmlNanoFTPConnect(ctxt
);
1258 xmlNanoFTPFreeCtxt(ctxt
);
1266 * @ctx: an FTP context
1267 * @directory: a directory on the server
1269 * Tries to change the remote directory
1271 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
1275 xmlNanoFTPCwd(void *ctx
, const char *directory
) {
1276 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1281 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
1282 if (directory
== NULL
) return 0;
1285 * Expected response code for CWD:
1289 * 500, 501, 502, 421, 530, 550
1291 snprintf(buf
, sizeof(buf
), "CWD %s\r\n", directory
);
1292 buf
[sizeof(buf
) - 1] = 0;
1295 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1297 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1299 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1302 res
= xmlNanoFTPGetResponse(ctxt
);
1306 if (res
== 2) return(1);
1315 * @ctx: an FTP context
1316 * @file: a file or directory on the server
1318 * Tries to delete an item (file or directory) from server
1320 * Returns -1 incase of error, 1 if DELE worked, 0 if it failed
1324 xmlNanoFTPDele(void *ctx
, const char *file
) {
1325 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1330 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
) ||
1331 (file
== NULL
)) return(-1);
1334 * Expected response code for DELE:
1339 * 500, 501, 502, 421, 530
1342 snprintf(buf
, sizeof(buf
), "DELE %s\r\n", file
);
1343 buf
[sizeof(buf
) - 1] = 0;
1346 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1348 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1350 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1353 res
= xmlNanoFTPGetResponse(ctxt
);
1357 if (res
== 2) return(1);
1364 * xmlNanoFTPGetConnection:
1365 * @ctx: an FTP context
1367 * Try to open a data connection to the server. Currently only
1368 * passive mode is supported.
1370 * Returns -1 incase of error, 0 otherwise
1374 xmlNanoFTPGetConnection(void *ctx
) {
1375 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1376 char buf
[200], *cur
;
1379 unsigned char ad
[6], *adp
, *portp
;
1380 unsigned int temp
[6];
1382 struct sockaddr_storage dataAddr
;
1384 struct sockaddr_in dataAddr
;
1386 XML_SOCKLEN_T dataAddrLen
;
1388 if (ctxt
== NULL
) return INVALID_SOCKET
;
1390 memset (&dataAddr
, 0, sizeof(dataAddr
));
1392 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
) {
1393 ctxt
->dataFd
= socket (AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
);
1394 ((struct sockaddr_in6
*)&dataAddr
)->sin6_family
= AF_INET6
;
1395 dataAddrLen
= sizeof(struct sockaddr_in6
);
1399 ctxt
->dataFd
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
1400 ((struct sockaddr_in
*)&dataAddr
)->sin_family
= AF_INET
;
1401 dataAddrLen
= sizeof (struct sockaddr_in
);
1404 if (ctxt
->dataFd
== INVALID_SOCKET
) {
1405 __xmlIOErr(XML_FROM_FTP
, 0, "socket failed");
1406 return INVALID_SOCKET
;
1409 if (ctxt
->passive
) {
1411 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
)
1412 snprintf (buf
, sizeof(buf
), "EPSV\r\n");
1415 snprintf (buf
, sizeof(buf
), "PASV\r\n");
1418 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1420 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1422 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1423 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1424 return INVALID_SOCKET
;
1426 res
= xmlNanoFTPReadResponse(ctx
);
1429 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1430 return INVALID_SOCKET
;
1433 * retry with an active connection
1435 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1439 cur
= &ctxt
->controlBuf
[ctxt
->controlBufAnswer
];
1440 while (((*cur
< '0') || (*cur
> '9')) && *cur
!= '\0') cur
++;
1442 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
) {
1443 if (sscanf (cur
, "%u", &temp
[0]) != 1) {
1444 __xmlIOErr(XML_FROM_FTP
, XML_FTP_EPSV_ANSWER
,
1445 "Invalid answer to EPSV\n");
1446 if (ctxt
->dataFd
!= INVALID_SOCKET
) {
1447 closesocket (ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1449 return INVALID_SOCKET
;
1451 memcpy (&((struct sockaddr_in6
*)&dataAddr
)->sin6_addr
, &((struct sockaddr_in6
*)&ctxt
->ftpAddr
)->sin6_addr
, sizeof(struct in6_addr
));
1452 ((struct sockaddr_in6
*)&dataAddr
)->sin6_port
= htons (temp
[0]);
1457 if (sscanf (cur
, "%u,%u,%u,%u,%u,%u", &temp
[0], &temp
[1], &temp
[2],
1458 &temp
[3], &temp
[4], &temp
[5]) != 6) {
1459 __xmlIOErr(XML_FROM_FTP
, XML_FTP_PASV_ANSWER
,
1460 "Invalid answer to PASV\n");
1461 if (ctxt
->dataFd
!= INVALID_SOCKET
) {
1462 closesocket (ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1464 return INVALID_SOCKET
;
1466 for (i
=0; i
<6; i
++) ad
[i
] = (unsigned char) (temp
[i
] & 0xff);
1467 memcpy (&((struct sockaddr_in
*)&dataAddr
)->sin_addr
, &ad
[0], 4);
1468 memcpy (&((struct sockaddr_in
*)&dataAddr
)->sin_port
, &ad
[4], 2);
1471 if (connect(ctxt
->dataFd
, (struct sockaddr
*) &dataAddr
, dataAddrLen
) < 0) {
1472 __xmlIOErr(XML_FROM_FTP
, 0, "Failed to create a data connection");
1473 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1474 return INVALID_SOCKET
;
1477 getsockname(ctxt
->dataFd
, (struct sockaddr
*) &dataAddr
, &dataAddrLen
);
1479 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
)
1480 ((struct sockaddr_in6
*)&dataAddr
)->sin6_port
= 0;
1483 ((struct sockaddr_in
*)&dataAddr
)->sin_port
= 0;
1485 if (bind(ctxt
->dataFd
, (struct sockaddr
*) &dataAddr
, dataAddrLen
) < 0) {
1486 __xmlIOErr(XML_FROM_FTP
, 0, "bind failed");
1487 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1488 return INVALID_SOCKET
;
1490 getsockname(ctxt
->dataFd
, (struct sockaddr
*) &dataAddr
, &dataAddrLen
);
1492 if (listen(ctxt
->dataFd
, 1) < 0) {
1493 __xmlIOErr(XML_FROM_FTP
, 0, "listen failed");
1494 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1495 return INVALID_SOCKET
;
1498 if ((ctxt
->ftpAddr
).ss_family
== AF_INET6
) {
1499 char buf6
[INET6_ADDRSTRLEN
];
1500 inet_ntop (AF_INET6
, &((struct sockaddr_in6
*)&dataAddr
)->sin6_addr
,
1501 buf6
, INET6_ADDRSTRLEN
);
1502 adp
= (unsigned char *) buf6
;
1503 portp
= (unsigned char *) &((struct sockaddr_in6
*)&dataAddr
)->sin6_port
;
1504 snprintf (buf
, sizeof(buf
), "EPRT |2|%s|%s|\r\n", adp
, portp
);
1508 adp
= (unsigned char *) &((struct sockaddr_in
*)&dataAddr
)->sin_addr
;
1509 portp
= (unsigned char *) &((struct sockaddr_in
*)&dataAddr
)->sin_port
;
1510 snprintf (buf
, sizeof(buf
), "PORT %d,%d,%d,%d,%d,%d\r\n",
1511 adp
[0] & 0xff, adp
[1] & 0xff, adp
[2] & 0xff, adp
[3] & 0xff,
1512 portp
[0] & 0xff, portp
[1] & 0xff);
1515 buf
[sizeof(buf
) - 1] = 0;
1518 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1521 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1523 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1524 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1525 return INVALID_SOCKET
;
1527 res
= xmlNanoFTPGetResponse(ctxt
);
1529 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1530 return INVALID_SOCKET
;
1533 return(ctxt
->dataFd
);
1538 * xmlNanoFTPCloseConnection:
1539 * @ctx: an FTP context
1541 * Close the data connection from the server
1543 * Returns -1 incase of error, 0 otherwise
1547 xmlNanoFTPCloseConnection(void *ctx
) {
1548 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1553 if ((ctxt
== NULL
) || (ctxt
->controlFd
== INVALID_SOCKET
)) return(-1);
1555 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1559 FD_SET(ctxt
->controlFd
, &rfd
);
1561 FD_SET(ctxt
->controlFd
, &efd
);
1562 res
= select(ctxt
->controlFd
+ 1, &rfd
, NULL
, &efd
, &tv
);
1567 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1572 xmlGenericError(xmlGenericErrorContext
,
1573 "xmlNanoFTPCloseConnection: timeout\n");
1575 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1577 res
= xmlNanoFTPGetResponse(ctxt
);
1579 closesocket(ctxt
->controlFd
); ctxt
->controlFd
= INVALID_SOCKET
;
1587 * xmlNanoFTPParseList:
1588 * @list: some data listing received from the server
1589 * @callback: the user callback
1590 * @userData: the user callback data
1592 * Parse at most one entry from the listing.
1594 * Returns -1 incase of error, the length of data parsed otherwise
1598 xmlNanoFTPParseList(const char *list
, ftpListCallback callback
, void *userData
) {
1599 const char *cur
= list
;
1609 unsigned long size
= 0;
1613 if (!strncmp(cur
, "total", 5)) {
1615 while (*cur
== ' ') cur
++;
1616 while ((*cur
>= '0') && (*cur
<= '9'))
1617 links
= (links
* 10) + (*cur
++ - '0');
1618 while ((*cur
== ' ') || (*cur
== '\n') || (*cur
== '\r'))
1621 } else if (*list
== '+') {
1624 while ((*cur
== ' ') || (*cur
== '\n') || (*cur
== '\r'))
1626 if (*cur
== 0) return(0);
1628 while (*cur
!= ' ') {
1632 if (*cur
== 0) return(0);
1635 while (*cur
== ' ') cur
++;
1636 if (*cur
== 0) return(0);
1637 while ((*cur
>= '0') && (*cur
<= '9'))
1638 links
= (links
* 10) + (*cur
++ - '0');
1639 while (*cur
== ' ') cur
++;
1640 if (*cur
== 0) return(0);
1642 while (*cur
!= ' ') {
1646 if (*cur
== 0) return(0);
1649 while (*cur
== ' ') cur
++;
1650 if (*cur
== 0) return(0);
1652 while (*cur
!= ' ') {
1656 if (*cur
== 0) return(0);
1659 while (*cur
== ' ') cur
++;
1660 if (*cur
== 0) return(0);
1661 while ((*cur
>= '0') && (*cur
<= '9'))
1662 size
= (size
* 10) + (*cur
++ - '0');
1663 while (*cur
== ' ') cur
++;
1664 if (*cur
== 0) return(0);
1666 while (*cur
!= ' ') {
1670 if (*cur
== 0) return(0);
1673 while (*cur
== ' ') cur
++;
1674 if (*cur
== 0) return(0);
1675 while ((*cur
>= '0') && (*cur
<= '9'))
1676 day
= (day
* 10) + (*cur
++ - '0');
1677 while (*cur
== ' ') cur
++;
1678 if (*cur
== 0) return(0);
1679 if ((cur
[1] == 0) || (cur
[2] == 0)) return(0);
1680 if ((cur
[1] == ':') || (cur
[2] == ':')) {
1681 while ((*cur
>= '0') && (*cur
<= '9'))
1682 hour
= (hour
* 10) + (*cur
++ - '0');
1683 if (*cur
== ':') cur
++;
1684 while ((*cur
>= '0') && (*cur
<= '9'))
1685 minute
= (minute
* 10) + (*cur
++ - '0');
1687 while ((*cur
>= '0') && (*cur
<= '9'))
1688 year
= (year
* 10) + (*cur
++ - '0');
1690 while (*cur
== ' ') cur
++;
1691 if (*cur
== 0) return(0);
1693 while ((*cur
!= '\n') && (*cur
!= '\r')) {
1695 filename
[i
++] = *cur
;
1697 if (*cur
== 0) return(0);
1700 if ((*cur
!= '\n') && (*cur
!= '\r'))
1702 while ((*cur
== '\n') || (*cur
== '\r'))
1705 if (callback
!= NULL
) {
1706 callback(userData
, filename
, attrib
, owner
, group
, size
, links
,
1707 year
, month
, day
, hour
, minute
);
1714 * @ctx: an FTP context
1715 * @callback: the user callback
1716 * @userData: the user callback data
1717 * @filename: optional files to list
1719 * Do a listing on the server. All files info are passed back
1722 * Returns -1 incase of error, 0 otherwise
1726 xmlNanoFTPList(void *ctx
, ftpListCallback callback
, void *userData
,
1727 const char *filename
) {
1728 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1735 if (ctxt
== NULL
) return (-1);
1736 if (filename
== NULL
) {
1737 if (xmlNanoFTPCwd(ctxt
, ctxt
->path
) < 1)
1739 ctxt
->dataFd
= xmlNanoFTPGetConnection(ctxt
);
1740 if (ctxt
->dataFd
== INVALID_SOCKET
)
1742 snprintf(buf
, sizeof(buf
), "LIST -L\r\n");
1744 if (filename
[0] != '/') {
1745 if (xmlNanoFTPCwd(ctxt
, ctxt
->path
) < 1)
1748 ctxt
->dataFd
= xmlNanoFTPGetConnection(ctxt
);
1749 if (ctxt
->dataFd
== INVALID_SOCKET
)
1751 snprintf(buf
, sizeof(buf
), "LIST -L %s\r\n", filename
);
1753 buf
[sizeof(buf
) - 1] = 0;
1756 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1758 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1760 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1761 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1764 res
= xmlNanoFTPReadResponse(ctxt
);
1766 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1774 FD_SET(ctxt
->dataFd
, &rfd
);
1776 FD_SET(ctxt
->dataFd
, &efd
);
1777 res
= select(ctxt
->dataFd
+ 1, &rfd
, NULL
, &efd
, &tv
);
1782 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1786 res
= xmlNanoFTPCheckResponse(ctxt
);
1788 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1789 ctxt
->dataFd
= INVALID_SOCKET
;
1793 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1800 if ((len
= recv(ctxt
->dataFd
, &buf
[indx
], sizeof(buf
) - (indx
+ 1), 0)) < 0) {
1801 __xmlIOErr(XML_FROM_FTP
, 0, "recv");
1802 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1803 ctxt
->dataFd
= INVALID_SOCKET
;
1807 write(1, &buf
[indx
], len
);
1813 res
= xmlNanoFTPParseList(&buf
[base
], callback
, userData
);
1817 memmove(&buf
[0], &buf
[base
], indx
- base
);
1820 xmlNanoFTPCloseConnection(ctxt
);
1825 * xmlNanoFTPGetSocket:
1826 * @ctx: an FTP context
1827 * @filename: the file to retrieve (or NULL if path is in context).
1829 * Initiate fetch of the given file from the server.
1831 * Returns the socket for the data connection, or <0 in case of error
1836 xmlNanoFTPGetSocket(void *ctx
, const char *filename
) {
1837 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1841 return INVALID_SOCKET
;
1842 if ((filename
== NULL
) && (ctxt
->path
== NULL
))
1843 return INVALID_SOCKET
;
1844 ctxt
->dataFd
= xmlNanoFTPGetConnection(ctxt
);
1845 if (ctxt
->dataFd
== INVALID_SOCKET
)
1846 return INVALID_SOCKET
;
1848 snprintf(buf
, sizeof(buf
), "TYPE I\r\n");
1851 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1853 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1855 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1856 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1857 return INVALID_SOCKET
;
1859 res
= xmlNanoFTPReadResponse(ctxt
);
1861 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1862 return INVALID_SOCKET
;
1864 if (filename
== NULL
)
1865 snprintf(buf
, sizeof(buf
), "RETR %s\r\n", ctxt
->path
);
1867 snprintf(buf
, sizeof(buf
), "RETR %s\r\n", filename
);
1868 buf
[sizeof(buf
) - 1] = 0;
1871 xmlGenericError(xmlGenericErrorContext
, "%s", buf
);
1873 res
= send(ctxt
->controlFd
, SEND_ARG2_CAST buf
, len
, 0);
1875 __xmlIOErr(XML_FROM_FTP
, 0, "send failed");
1876 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1877 return INVALID_SOCKET
;
1879 res
= xmlNanoFTPReadResponse(ctxt
);
1881 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1882 return INVALID_SOCKET
;
1884 return(ctxt
->dataFd
);
1889 * @ctx: an FTP context
1890 * @callback: the user callback
1891 * @userData: the user callback data
1892 * @filename: the file to retrieve
1894 * Fetch the given file from the server. All data are passed back
1895 * in the callbacks. The last callback has a size of 0 block.
1897 * Returns -1 incase of error, 0 otherwise
1901 xmlNanoFTPGet(void *ctx
, ftpDataCallback callback
, void *userData
,
1902 const char *filename
) {
1903 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1909 if (ctxt
== NULL
) return(-1);
1910 if ((filename
== NULL
) && (ctxt
->path
== NULL
))
1912 if (callback
== NULL
)
1914 if (xmlNanoFTPGetSocket(ctxt
, filename
) == INVALID_SOCKET
)
1921 FD_SET(ctxt
->dataFd
, &rfd
);
1922 res
= select(ctxt
->dataFd
+ 1, &rfd
, NULL
, NULL
, &tv
);
1927 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1931 res
= xmlNanoFTPCheckResponse(ctxt
);
1933 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1934 ctxt
->dataFd
= INVALID_SOCKET
;
1938 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1944 if ((len
= recv(ctxt
->dataFd
, buf
, sizeof(buf
), 0)) < 0) {
1945 __xmlIOErr(XML_FROM_FTP
, 0, "recv failed");
1946 callback(userData
, buf
, len
);
1947 closesocket(ctxt
->dataFd
); ctxt
->dataFd
= INVALID_SOCKET
;
1950 callback(userData
, buf
, len
);
1953 return(xmlNanoFTPCloseConnection(ctxt
));
1958 * @ctx: the FTP context
1960 * @len: the buffer length
1962 * This function tries to read @len bytes from the existing FTP connection
1963 * and saves them in @dest. This is a blocking call.
1965 * Returns the number of byte read. 0 is an indication of an end of connection.
1966 * -1 indicates a parameter error.
1969 xmlNanoFTPRead(void *ctx
, void *dest
, int len
) {
1970 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
1972 if (ctx
== NULL
) return(-1);
1973 if (ctxt
->dataFd
== INVALID_SOCKET
) return(0);
1974 if (dest
== NULL
) return(-1);
1975 if (len
<= 0) return(0);
1977 len
= recv(ctxt
->dataFd
, dest
, len
, 0);
1980 __xmlIOErr(XML_FROM_FTP
, 0, "recv failed");
1981 xmlNanoFTPCloseConnection(ctxt
);
1984 xmlGenericError(xmlGenericErrorContext
, "Recvd %d bytes\n", len
);
1991 * @URL: the URL to the resource
1993 * Start to fetch the given ftp:// resource
1995 * Returns an FTP context, or NULL
1999 xmlNanoFTPOpen(const char *URL
) {
2000 xmlNanoFTPCtxtPtr ctxt
;
2004 if (URL
== NULL
) return(NULL
);
2005 if (strncmp("ftp://", URL
, 6)) return(NULL
);
2007 ctxt
= (xmlNanoFTPCtxtPtr
) xmlNanoFTPNewCtxt(URL
);
2008 if (ctxt
== NULL
) return(NULL
);
2009 if (xmlNanoFTPConnect(ctxt
) < 0) {
2010 xmlNanoFTPFreeCtxt(ctxt
);
2013 sock
= xmlNanoFTPGetSocket(ctxt
, ctxt
->path
);
2014 if (sock
== INVALID_SOCKET
) {
2015 xmlNanoFTPFreeCtxt(ctxt
);
2023 * @ctx: an FTP context
2025 * Close the connection and both control and transport
2027 * Returns -1 incase of error, 0 otherwise
2031 xmlNanoFTPClose(void *ctx
) {
2032 xmlNanoFTPCtxtPtr ctxt
= (xmlNanoFTPCtxtPtr
) ctx
;
2037 if (ctxt
->dataFd
!= INVALID_SOCKET
) {
2038 closesocket(ctxt
->dataFd
);
2039 ctxt
->dataFd
= INVALID_SOCKET
;
2041 if (ctxt
->controlFd
!= INVALID_SOCKET
) {
2042 xmlNanoFTPQuit(ctxt
);
2043 closesocket(ctxt
->controlFd
);
2044 ctxt
->controlFd
= INVALID_SOCKET
;
2046 xmlNanoFTPFreeCtxt(ctxt
);
2051 /************************************************************************
2053 * Basic test in Standalone mode *
2055 ************************************************************************/
2057 void ftpList(void *userData
, const char *filename
, const char* attrib
,
2058 const char *owner
, const char *group
, unsigned long size
, int links
,
2059 int year
, const char *month
, int day
, int hour
, int minute
) {
2060 xmlGenericError(xmlGenericErrorContext
,
2061 "%s %s %s %ld %s\n", attrib
, owner
, group
, size
, filename
);
2064 void ftpData(void *userData
, const char *data
, int len
) {
2065 if (userData
== NULL
) return;
2067 fclose((FILE*)userData
);
2070 fwrite(data
, len
, 1, (FILE*)userData
);
2073 int main(int argc
, char **argv
) {
2076 char *tstfile
= NULL
;
2080 ctxt
= xmlNanoFTPNewCtxt(argv
[1]);
2081 if (xmlNanoFTPConnect(ctxt
) < 0) {
2082 xmlGenericError(xmlGenericErrorContext
,
2083 "Couldn't connect to %s\n", argv
[1]);
2089 ctxt
= xmlNanoFTPConnectTo("localhost", 0);
2091 xmlGenericError(xmlGenericErrorContext
,
2092 "Couldn't connect to localhost\n");
2095 xmlNanoFTPList(ctxt
, ftpList
, NULL
, tstfile
);
2096 output
= fopen("/tmp/tstdata", "w");
2097 if (output
!= NULL
) {
2098 if (xmlNanoFTPGet(ctxt
, ftpData
, (void *) output
, tstfile
) < 0)
2099 xmlGenericError(xmlGenericErrorContext
,
2100 "Failed to get file\n");
2103 xmlNanoFTPClose(ctxt
);
2107 #endif /* STANDALONE */
2108 #else /* !LIBXML_FTP_ENABLED */
2111 int main(int argc
, char **argv
) {
2112 xmlGenericError(xmlGenericErrorContext
,
2113 "%s : FTP support not compiled in\n", argv
[0]);
2116 #endif /* STANDALONE */
2117 #endif /* LIBXML_FTP_ENABLED */
2118 #define bottom_nanoftp
2119 #include "elfgcchack.h"