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);
514 if (ctxt->user != NULL) xmlFree(ctxt->user);
515 if (ctxt->passwd != NULL) xmlFree(ctxt->passwd);
517 if (ctxt->controlFd != INVALID_SOCKET) closesocket(ctxt->controlFd);
518 ctxt->controlFd = INVALID_SOCKET;
519 ctxt->controlBufIndex = -1;
520 ctxt->controlBufUsed = -1;
525 * xmlNanoFTPParseResponse:
526 * @buf: the buffer containing the response
527 * @len: the buffer length
529 * Parsing of the server answer, we just extract the code.
531 * returns 0 for errors
532 * +XXX for last line of response
533 * -XXX for response to be continued
536 xmlNanoFTPParseResponse(char *buf, int len) {
539 if (len < 3) return(-1);
540 if ((*buf >= '0') && (*buf <= '9'))
541 val = val * 10 + (*buf - '0');
545 if ((*buf >= '0') && (*buf <= '9'))
546 val = val * 10 + (*buf - '0');
550 if ((*buf >= '0') && (*buf <= '9'))
551 val = val * 10 + (*buf - '0');
562 * @ctx: an FTP context
564 * Read more information from the FTP control connection
565 * Returns the number of bytes read, < 0 indicates an error
568 xmlNanoFTPGetMore(void *ctx) {
569 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
573 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
575 if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) {
577 xmlGenericError(xmlGenericErrorContext,
578 "xmlNanoFTPGetMore : controlBufIndex = %d\n",
579 ctxt->controlBufIndex);
584 if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) {
586 xmlGenericError(xmlGenericErrorContext,
587 "xmlNanoFTPGetMore : controlBufUsed = %d\n",
588 ctxt->controlBufUsed);
592 if (ctxt->controlBufIndex > ctxt->controlBufUsed) {
594 xmlGenericError(xmlGenericErrorContext,
595 "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
596 ctxt->controlBufIndex, ctxt->controlBufUsed);
602 * First pack the control buffer
604 if (ctxt->controlBufIndex > 0) {
605 memmove(&ctxt->controlBuf[0], &ctxt->controlBuf[ctxt->controlBufIndex],
606 ctxt->controlBufUsed - ctxt->controlBufIndex);
607 ctxt->controlBufUsed -= ctxt->controlBufIndex;
608 ctxt->controlBufIndex = 0;
610 size = FTP_BUF_SIZE - ctxt->controlBufUsed;
613 xmlGenericError(xmlGenericErrorContext,
614 "xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed);
620 * Read the amount left on the control connection
622 if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex],
624 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
625 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
626 ctxt->controlFd = INVALID_SOCKET;
630 xmlGenericError(xmlGenericErrorContext,
631 "xmlNanoFTPGetMore : read %d [%d - %d]\n", len,
632 ctxt->controlBufUsed, ctxt->controlBufUsed + len);
634 ctxt->controlBufUsed += len;
635 ctxt->controlBuf[ctxt->controlBufUsed] = 0;
641 * xmlNanoFTPReadResponse:
642 * @ctx: an FTP context
644 * Read the response from the FTP server after a command.
645 * Returns the code number
648 xmlNanoFTPReadResponse(void *ctx) {
649 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
652 int res = -1, cur = -1;
654 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
658 * Assumes everything up to controlBuf[controlBufIndex] has been read
661 len = xmlNanoFTPGetMore(ctx);
665 if ((ctxt->controlBufUsed == 0) && (len == 0)) {
668 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
669 end = &ctxt->controlBuf[ctxt->controlBufUsed];
672 xmlGenericError(xmlGenericErrorContext,
673 "\n<<<\n%s\n--\n", ptr);
676 cur = xmlNanoFTPParseResponse(ptr, end - ptr);
679 * Successfully scanned the control code, scratch
680 * till the end of the line, but keep the index to be
681 * able to analyze the result if needed.
685 ctxt->controlBufAnswer = ptr - ctxt->controlBuf;
686 while ((ptr < end) && (*ptr != '\n')) ptr++;
687 if (*ptr == '\n') ptr++;
688 if (*ptr == '\r') ptr++;
691 while ((ptr < end) && (*ptr != '\n')) ptr++;
693 ctxt->controlBufIndex = ctxt->controlBufUsed;
696 if (*ptr != '\r') ptr++;
699 if (res < 0) goto get_more;
700 ctxt->controlBufIndex = ptr - ctxt->controlBuf;
702 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
703 xmlGenericError(xmlGenericErrorContext, "\n---\n%s\n--\n", ptr);
707 xmlGenericError(xmlGenericErrorContext, "Got %d\n", res);
713 * xmlNanoFTPGetResponse:
714 * @ctx: an FTP context
716 * Get the response from the FTP server after a command.
717 * Returns the code number
721 xmlNanoFTPGetResponse(void *ctx) {
724 res = xmlNanoFTPReadResponse(ctx);
730 * xmlNanoFTPCheckResponse:
731 * @ctx: an FTP context
733 * Check if there is a response from the FTP server after a command.
734 * Returns the code number, or 0
738 xmlNanoFTPCheckResponse(void *ctx) {
739 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
743 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
747 FD_SET(ctxt->controlFd, &rfd);
748 switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) {
752 __xmlIOErr(XML_FROM_FTP, 0, "select");
757 return(xmlNanoFTPReadResponse(ctx));
761 * Send the user authentication
765 xmlNanoFTPSendUser(void *ctx) {
766 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
771 if (ctxt->user == NULL)
772 snprintf(buf, sizeof(buf), "USER anonymous\r\n");
774 snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user);
775 buf[sizeof(buf) - 1] = 0;
778 xmlGenericError(xmlGenericErrorContext, "%s", buf);
780 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
782 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
789 * Send the password authentication
793 xmlNanoFTPSendPasswd(void *ctx) {
794 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
799 if (ctxt->passwd == NULL)
800 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
802 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
803 buf[sizeof(buf) - 1] = 0;
806 xmlGenericError(xmlGenericErrorContext, "%s", buf);
808 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
810 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
818 * @ctx: an FTP context
820 * Send a QUIT command to the server
822 * Returns -1 in case of error, 0 otherwise
827 xmlNanoFTPQuit(void *ctx) {
828 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
832 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
834 snprintf(buf, sizeof(buf), "QUIT\r\n");
837 xmlGenericError(xmlGenericErrorContext, "%s", buf); /* Just to be consistent, even though we know it can't have a % in it */
839 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
841 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
849 * @ctx: an FTP context
851 * Tries to open a control connection
853 * Returns -1 in case of error, 0 otherwise
857 xmlNanoFTPConnect(void *ctx) {
858 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
862 int addrlen = sizeof (struct sockaddr_in);
866 if (ctxt->hostname == NULL)
870 * do the blocking DNS query.
880 memset (&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
884 struct addrinfo hints, *tmp, *result;
887 memset (&hints, 0, sizeof(hints));
888 hints.ai_socktype = SOCK_STREAM;
891 if (getaddrinfo (proxy, NULL, &hints, &result) != 0) {
892 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
897 if (getaddrinfo (ctxt->hostname, NULL, &hints, &result) != 0) {
898 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
902 for (tmp = result; tmp; tmp = tmp->ai_next)
903 if (tmp->ai_family == AF_INET || tmp->ai_family == AF_INET6)
908 freeaddrinfo (result);
909 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
912 if (tmp->ai_addrlen > sizeof(ctxt->ftpAddr)) {
914 freeaddrinfo (result);
915 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
918 if (tmp->ai_family == AF_INET6) {
919 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
920 ((struct sockaddr_in6 *) &ctxt->ftpAddr)->sin6_port = htons (port);
921 ctxt->controlFd = socket (AF_INET6, SOCK_STREAM, 0);
924 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
925 ((struct sockaddr_in *) &ctxt->ftpAddr)->sin_port = htons (port);
926 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
928 addrlen = tmp->ai_addrlen;
929 freeaddrinfo (result);
935 hp = gethostbyname (GETHOSTBYNAME_ARG_CAST proxy);
937 hp = gethostbyname (GETHOSTBYNAME_ARG_CAST ctxt->hostname);
939 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname failed");
942 if ((unsigned int) hp->h_length >
943 sizeof(((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr)) {
944 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
951 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET;
952 memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr,
953 hp->h_addr_list[0], hp->h_length);
954 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port =
955 (unsigned short)htons ((unsigned short)port);
956 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
957 addrlen = sizeof (struct sockaddr_in);
960 if (ctxt->controlFd == INVALID_SOCKET) {
961 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
968 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
970 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a connection");
971 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
972 ctxt->controlFd = INVALID_SOCKET;
977 * Wait for the HELLO from the server.
979 res = xmlNanoFTPGetResponse(ctxt);
981 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
982 ctxt->controlFd = INVALID_SOCKET;
987 * State diagram for the login operation on the FTP server
992 * +---+ USER +---+------------->+---+
993 * | B |---------->| W | 2 ---->| E |
994 * +---+ +---+------ | -->+---+
997 * -------------- ----- | | |
1003 * +---+ PASS +---+ 2 | ------>+---+
1004 * | |---------->| W |------------->| S |
1005 * +---+ +---+ ---------->+---+
1008 * -------------- -------- |
1014 * +---+ ACCT +---+-- | ----->+---+
1015 * | |---------->| W | 4,5 -------->| F |
1016 * +---+ +---+------------->+---+
1018 * Of course in case of using a proxy this get really nasty and is not
1019 * standardized at all :-(
1025 if (proxyUser != NULL) {
1027 * We need proxy auth
1029 snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
1030 buf[sizeof(buf) - 1] = 0;
1033 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1035 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1037 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1038 closesocket(ctxt->controlFd);
1039 ctxt->controlFd = INVALID_SOCKET;
1042 res = xmlNanoFTPGetResponse(ctxt);
1045 if (proxyPasswd == NULL)
1048 if (proxyPasswd != NULL)
1049 snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
1051 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
1052 buf[sizeof(buf) - 1] = 0;
1055 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1057 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1059 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1060 closesocket(ctxt->controlFd);
1061 ctxt->controlFd = INVALID_SOCKET;
1064 res = xmlNanoFTPGetResponse(ctxt);
1066 closesocket(ctxt->controlFd);
1067 ctxt->controlFd = INVALID_SOCKET;
1077 closesocket(ctxt->controlFd);
1078 ctxt->controlFd = INVALID_SOCKET;
1084 * We assume we don't need more authentication to the proxy
1085 * and that it succeeded :-\
1087 switch (proxyType) {
1089 /* we will try in sequence */
1091 /* Using SITE command */
1092 snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
1093 buf[sizeof(buf) - 1] = 0;
1096 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1098 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1100 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1101 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1102 ctxt->controlFd = INVALID_SOCKET;
1105 res = xmlNanoFTPGetResponse(ctxt);
1107 /* we assume it worked :-\ 1 is error for SITE command */
1111 if (proxyType == 1) {
1112 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1113 ctxt->controlFd = INVALID_SOCKET;
1117 /* USER user@host command */
1118 if (ctxt->user == NULL)
1119 snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n",
1122 snprintf(buf, sizeof(buf), "USER %s@%s\r\n",
1123 ctxt->user, ctxt->hostname);
1124 buf[sizeof(buf) - 1] = 0;
1127 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1129 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1131 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1132 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1133 ctxt->controlFd = INVALID_SOCKET;
1136 res = xmlNanoFTPGetResponse(ctxt);
1137 if ((res == 1) || (res == 2)) {
1138 /* we assume it worked :-\ */
1142 if (ctxt->passwd == NULL)
1143 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
1145 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
1146 buf[sizeof(buf) - 1] = 0;
1149 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1151 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1153 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1154 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1155 ctxt->controlFd = INVALID_SOCKET;
1158 res = xmlNanoFTPGetResponse(ctxt);
1159 if ((res == 1) || (res == 2)) {
1160 /* we assume it worked :-\ */
1164 if (proxyType == 2) {
1165 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1166 ctxt->controlFd = INVALID_SOCKET;
1171 * If you need support for other Proxy authentication scheme
1172 * send the code or at least the sequence in use.
1175 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1176 ctxt->controlFd = INVALID_SOCKET;
1181 * Non-proxy handling.
1183 res = xmlNanoFTPSendUser(ctxt);
1185 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1186 ctxt->controlFd = INVALID_SOCKET;
1189 res = xmlNanoFTPGetResponse(ctxt);
1200 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1201 ctxt->controlFd = INVALID_SOCKET;
1204 res = xmlNanoFTPSendPasswd(ctxt);
1206 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1207 ctxt->controlFd = INVALID_SOCKET;
1210 res = xmlNanoFTPGetResponse(ctxt);
1215 __xmlIOErr(XML_FROM_FTP, XML_FTP_ACCNT,
1216 "FTP server asking for ACCNT on anonymous\n");
1222 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1223 ctxt->controlFd = INVALID_SOCKET;
1231 * xmlNanoFTPConnectTo:
1232 * @server: an FTP server name
1233 * @port: the port (use 21 if 0)
1235 * Tries to open a control connection to the given server/port
1237 * Returns an fTP context or NULL if it failed
1241 xmlNanoFTPConnectTo(const char *server, int port) {
1242 xmlNanoFTPCtxtPtr ctxt;
1250 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL);
1253 ctxt->hostname = xmlMemStrdup(server);
1254 if (ctxt->hostname == NULL) {
1255 xmlNanoFTPFreeCtxt(ctxt);
1260 res = xmlNanoFTPConnect(ctxt);
1262 xmlNanoFTPFreeCtxt(ctxt);
1270 * @ctx: an FTP context
1271 * @directory: a directory on the server
1273 * Tries to change the remote directory
1275 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
1279 xmlNanoFTPCwd(void *ctx, const char *directory) {
1280 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1285 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
1286 if (directory == NULL) return 0;
1289 * Expected response code for CWD:
1293 * 500, 501, 502, 421, 530, 550
1295 snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
1296 buf[sizeof(buf) - 1] = 0;
1299 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1301 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1303 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1306 res = xmlNanoFTPGetResponse(ctxt);
1310 if (res == 2) return(1);
1319 * @ctx: an FTP context
1320 * @file: a file or directory on the server
1322 * Tries to delete an item (file or directory) from server
1324 * Returns -1 incase of error, 1 if DELE worked, 0 if it failed
1328 xmlNanoFTPDele(void *ctx, const char *file) {
1329 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1334 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET) ||
1335 (file == NULL)) return(-1);
1338 * Expected response code for DELE:
1343 * 500, 501, 502, 421, 530
1346 snprintf(buf, sizeof(buf), "DELE %s\r\n", file);
1347 buf[sizeof(buf) - 1] = 0;
1350 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1352 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1354 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1357 res = xmlNanoFTPGetResponse(ctxt);
1361 if (res == 2) return(1);
1368 * xmlNanoFTPGetConnection:
1369 * @ctx: an FTP context
1371 * Try to open a data connection to the server. Currently only
1372 * passive mode is supported.
1374 * Returns -1 incase of error, 0 otherwise
1378 xmlNanoFTPGetConnection(void *ctx) {
1379 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1380 char buf[200], *cur;
1383 unsigned char ad[6], *adp, *portp;
1384 unsigned int temp[6];
1386 struct sockaddr_storage dataAddr;
1388 struct sockaddr_in dataAddr;
1390 XML_SOCKLEN_T dataAddrLen;
1392 if (ctxt == NULL) return INVALID_SOCKET;
1394 memset (&dataAddr, 0, sizeof(dataAddr));
1396 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1397 ctxt->dataFd = socket (AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1398 ((struct sockaddr_in6 *)&dataAddr)->sin6_family = AF_INET6;
1399 dataAddrLen = sizeof(struct sockaddr_in6);
1403 ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1404 ((struct sockaddr_in *)&dataAddr)->sin_family = AF_INET;
1405 dataAddrLen = sizeof (struct sockaddr_in);
1408 if (ctxt->dataFd == INVALID_SOCKET) {
1409 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
1410 return INVALID_SOCKET;
1413 if (ctxt->passive) {
1415 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1416 snprintf (buf, sizeof(buf), "EPSV\r\n");
1419 snprintf (buf, sizeof(buf), "PASV\r\n");
1422 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1424 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1426 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1427 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1428 return INVALID_SOCKET;
1430 res = xmlNanoFTPReadResponse(ctx);
1433 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1434 return INVALID_SOCKET;
1437 * retry with an active connection
1439 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1443 cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
1444 while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
1446 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1447 if (sscanf (cur, "%u", &temp[0]) != 1) {
1448 __xmlIOErr(XML_FROM_FTP, XML_FTP_EPSV_ANSWER,
1449 "Invalid answer to EPSV\n");
1450 if (ctxt->dataFd != INVALID_SOCKET) {
1451 closesocket (ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1453 return INVALID_SOCKET;
1455 memcpy (&((struct sockaddr_in6 *)&dataAddr)->sin6_addr, &((struct sockaddr_in6 *)&ctxt->ftpAddr)->sin6_addr, sizeof(struct in6_addr));
1456 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = htons (temp[0]);
1461 if (sscanf (cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2],
1462 &temp[3], &temp[4], &temp[5]) != 6) {
1463 __xmlIOErr(XML_FROM_FTP, XML_FTP_PASV_ANSWER,
1464 "Invalid answer to PASV\n");
1465 if (ctxt->dataFd != INVALID_SOCKET) {
1466 closesocket (ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1468 return INVALID_SOCKET;
1470 for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
1471 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_addr, &ad[0], 4);
1472 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_port, &ad[4], 2);
1475 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1476 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a data connection");
1477 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1478 return INVALID_SOCKET;
1481 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1483 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1484 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = 0;
1487 ((struct sockaddr_in *)&dataAddr)->sin_port = 0;
1489 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1490 __xmlIOErr(XML_FROM_FTP, 0, "bind failed");
1491 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1492 return INVALID_SOCKET;
1494 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1496 if (listen(ctxt->dataFd, 1) < 0) {
1497 __xmlIOErr(XML_FROM_FTP, 0, "listen failed");
1498 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1499 return INVALID_SOCKET;
1502 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1503 char buf6[INET6_ADDRSTRLEN];
1504 inet_ntop (AF_INET6, &((struct sockaddr_in6 *)&dataAddr)->sin6_addr,
1505 buf6, INET6_ADDRSTRLEN);
1506 adp = (unsigned char *) buf6;
1507 portp = (unsigned char *) &((struct sockaddr_in6 *)&dataAddr)->sin6_port;
1508 snprintf (buf, sizeof(buf), "EPRT |2|%s|%s|\r\n", adp, portp);
1512 adp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_addr;
1513 portp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_port;
1514 snprintf (buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n",
1515 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1516 portp[0] & 0xff, portp[1] & 0xff);
1519 buf[sizeof(buf) - 1] = 0;
1522 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1525 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1527 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1528 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1529 return INVALID_SOCKET;
1531 res = xmlNanoFTPGetResponse(ctxt);
1533 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1534 return INVALID_SOCKET;
1537 return(ctxt->dataFd);
1542 * xmlNanoFTPCloseConnection:
1543 * @ctx: an FTP context
1545 * Close the data connection from the server
1547 * Returns -1 incase of error, 0 otherwise
1551 xmlNanoFTPCloseConnection(void *ctx) {
1552 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1557 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
1559 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1563 FD_SET(ctxt->controlFd, &rfd);
1565 FD_SET(ctxt->controlFd, &efd);
1566 res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
1571 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1576 xmlGenericError(xmlGenericErrorContext,
1577 "xmlNanoFTPCloseConnection: timeout\n");
1579 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1581 res = xmlNanoFTPGetResponse(ctxt);
1583 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1591 * xmlNanoFTPParseList:
1592 * @list: some data listing received from the server
1593 * @callback: the user callback
1594 * @userData: the user callback data
1596 * Parse at most one entry from the listing.
1598 * Returns -1 incase of error, the length of data parsed otherwise
1602 xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) {
1603 const char *cur = list;
1613 unsigned long size = 0;
1617 if (!strncmp(cur, "total", 5)) {
1619 while (*cur == ' ') cur++;
1620 while ((*cur >= '0') && (*cur <= '9'))
1621 links = (links * 10) + (*cur++ - '0');
1622 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1625 } else if (*list == '+') {
1628 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1630 if (*cur == 0) return(0);
1632 while (*cur != ' ') {
1636 if (*cur == 0) return(0);
1639 while (*cur == ' ') cur++;
1640 if (*cur == 0) return(0);
1641 while ((*cur >= '0') && (*cur <= '9'))
1642 links = (links * 10) + (*cur++ - '0');
1643 while (*cur == ' ') cur++;
1644 if (*cur == 0) return(0);
1646 while (*cur != ' ') {
1650 if (*cur == 0) return(0);
1653 while (*cur == ' ') cur++;
1654 if (*cur == 0) return(0);
1656 while (*cur != ' ') {
1660 if (*cur == 0) return(0);
1663 while (*cur == ' ') cur++;
1664 if (*cur == 0) return(0);
1665 while ((*cur >= '0') && (*cur <= '9'))
1666 size = (size * 10) + (*cur++ - '0');
1667 while (*cur == ' ') cur++;
1668 if (*cur == 0) return(0);
1670 while (*cur != ' ') {
1674 if (*cur == 0) return(0);
1677 while (*cur == ' ') cur++;
1678 if (*cur == 0) return(0);
1679 while ((*cur >= '0') && (*cur <= '9'))
1680 day = (day * 10) + (*cur++ - '0');
1681 while (*cur == ' ') cur++;
1682 if (*cur == 0) return(0);
1683 if ((cur[1] == 0) || (cur[2] == 0)) return(0);
1684 if ((cur[1] == ':') || (cur[2] == ':')) {
1685 while ((*cur >= '0') && (*cur <= '9'))
1686 hour = (hour * 10) + (*cur++ - '0');
1687 if (*cur == ':') cur++;
1688 while ((*cur >= '0') && (*cur <= '9'))
1689 minute = (minute * 10) + (*cur++ - '0');
1691 while ((*cur >= '0') && (*cur <= '9'))
1692 year = (year * 10) + (*cur++ - '0');
1694 while (*cur == ' ') cur++;
1695 if (*cur == 0) return(0);
1697 while ((*cur != '\n') && (*cur != '\r')) {
1699 filename[i++] = *cur;
1701 if (*cur == 0) return(0);
1704 if ((*cur != '\n') && (*cur != '\r'))
1706 while ((*cur == '\n') || (*cur == '\r'))
1709 if (callback != NULL) {
1710 callback(userData, filename, attrib, owner, group, size, links,
1711 year, month, day, hour, minute);
1718 * @ctx: an FTP context
1719 * @callback: the user callback
1720 * @userData: the user callback data
1721 * @filename: optional files to list
1723 * Do a listing on the server. All files info are passed back
1726 * Returns -1 incase of error, 0 otherwise
1730 xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
1731 const char *filename) {
1732 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1739 if (ctxt == NULL) return (-1);
1740 if (filename == NULL) {
1741 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1743 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1744 if (ctxt->dataFd == INVALID_SOCKET)
1746 snprintf(buf, sizeof(buf), "LIST -L\r\n");
1748 if (filename[0] != '/') {
1749 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1752 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1753 if (ctxt->dataFd == INVALID_SOCKET)
1755 snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
1757 buf[sizeof(buf) - 1] = 0;
1760 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1762 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1764 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1765 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1768 res = xmlNanoFTPReadResponse(ctxt);
1770 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1778 FD_SET(ctxt->dataFd, &rfd);
1780 FD_SET(ctxt->dataFd, &efd);
1781 res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
1786 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1790 res = xmlNanoFTPCheckResponse(ctxt);
1792 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1793 ctxt->dataFd = INVALID_SOCKET;
1797 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1804 if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) {
1805 __xmlIOErr(XML_FROM_FTP, 0, "recv");
1806 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1807 ctxt->dataFd = INVALID_SOCKET;
1811 write(1, &buf[indx], len);
1817 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1821 memmove(&buf[0], &buf[base], indx - base);
1824 xmlNanoFTPCloseConnection(ctxt);
1829 * xmlNanoFTPGetSocket:
1830 * @ctx: an FTP context
1831 * @filename: the file to retrieve (or NULL if path is in context).
1833 * Initiate fetch of the given file from the server.
1835 * Returns the socket for the data connection, or <0 in case of error
1840 xmlNanoFTPGetSocket(void *ctx, const char *filename) {
1841 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1845 return INVALID_SOCKET;
1846 if ((filename == NULL) && (ctxt->path == NULL))
1847 return INVALID_SOCKET;
1848 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1849 if (ctxt->dataFd == INVALID_SOCKET)
1850 return INVALID_SOCKET;
1852 snprintf(buf, sizeof(buf), "TYPE I\r\n");
1855 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1857 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1859 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1860 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1861 return INVALID_SOCKET;
1863 res = xmlNanoFTPReadResponse(ctxt);
1865 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1866 return INVALID_SOCKET;
1868 if (filename == NULL)
1869 snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
1871 snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
1872 buf[sizeof(buf) - 1] = 0;
1875 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1877 res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
1879 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1880 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1881 return INVALID_SOCKET;
1883 res = xmlNanoFTPReadResponse(ctxt);
1885 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1886 return INVALID_SOCKET;
1888 return(ctxt->dataFd);
1893 * @ctx: an FTP context
1894 * @callback: the user callback
1895 * @userData: the user callback data
1896 * @filename: the file to retrieve
1898 * Fetch the given file from the server. All data are passed back
1899 * in the callbacks. The last callback has a size of 0 block.
1901 * Returns -1 incase of error, 0 otherwise
1905 xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
1906 const char *filename) {
1907 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1913 if (ctxt == NULL) return(-1);
1914 if ((filename == NULL) && (ctxt->path == NULL))
1916 if (callback == NULL)
1918 if (xmlNanoFTPGetSocket(ctxt, filename) == INVALID_SOCKET)
1925 FD_SET(ctxt->dataFd, &rfd);
1926 res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
1931 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1935 res = xmlNanoFTPCheckResponse(ctxt);
1937 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1938 ctxt->dataFd = INVALID_SOCKET;
1942 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1948 if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) {
1949 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
1950 callback(userData, buf, len);
1951 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1954 callback(userData, buf, len);
1957 return(xmlNanoFTPCloseConnection(ctxt));
1962 * @ctx: the FTP context
1964 * @len: the buffer length
1966 * This function tries to read @len bytes from the existing FTP connection
1967 * and saves them in @dest. This is a blocking call.
1969 * Returns the number of byte read. 0 is an indication of an end of connection.
1970 * -1 indicates a parameter error.
1973 xmlNanoFTPRead(void *ctx, void *dest, int len) {
1974 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1976 if (ctx == NULL) return(-1);
1977 if (ctxt->dataFd == INVALID_SOCKET) return(0);
1978 if (dest == NULL) return(-1);
1979 if (len <= 0) return(0);
1981 len = recv(ctxt->dataFd, dest, len, 0);
1984 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
1985 xmlNanoFTPCloseConnection(ctxt);
1988 xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
1995 * @URL: the URL to the resource
1997 * Start to fetch the given ftp:// resource
1999 * Returns an FTP context, or NULL
2003 xmlNanoFTPOpen(const char *URL) {
2004 xmlNanoFTPCtxtPtr ctxt;
2008 if (URL == NULL) return(NULL);
2009 if (strncmp("ftp://", URL, 6)) return(NULL);
2011 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL);
2012 if (ctxt == NULL) return(NULL);
2013 if (xmlNanoFTPConnect(ctxt) < 0) {
2014 xmlNanoFTPFreeCtxt(ctxt);
2017 sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
2018 if (sock == INVALID_SOCKET) {
2019 xmlNanoFTPFreeCtxt(ctxt);
2027 * @ctx: an FTP context
2029 * Close the connection and both control and transport
2031 * Returns -1 incase of error, 0 otherwise
2035 xmlNanoFTPClose(void *ctx) {
2036 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2041 if (ctxt->dataFd != INVALID_SOCKET) {
2042 closesocket(ctxt->dataFd);
2043 ctxt->dataFd = INVALID_SOCKET;
2045 if (ctxt->controlFd != INVALID_SOCKET) {
2046 xmlNanoFTPQuit(ctxt);
2047 closesocket(ctxt->controlFd);
2048 ctxt->controlFd = INVALID_SOCKET;
2050 xmlNanoFTPFreeCtxt(ctxt);
2055 /************************************************************************
2057 * Basic test in Standalone mode *
2059 ************************************************************************/
2061 void ftpList(void *userData, const char *filename, const char* attrib,
2062 const char *owner, const char *group, unsigned long size, int links,
2063 int year, const char *month, int day, int hour, int minute) {
2064 xmlGenericError(xmlGenericErrorContext,
2065 "%s %s %s %ld %s\n", attrib, owner, group, size, filename);
2068 void ftpData(void *userData, const char *data, int len) {
2069 if (userData == NULL) return;
2071 fclose((FILE*)userData);
2074 fwrite(data, len, 1, (FILE*)userData);
2077 int main(int argc, char **argv) {
2080 char *tstfile = NULL;
2084 ctxt = xmlNanoFTPNewCtxt(argv[1]);
2085 if (xmlNanoFTPConnect(ctxt) < 0) {
2086 xmlGenericError(xmlGenericErrorContext,
2087 "Couldn't connect to %s\n", argv[1]);
2093 ctxt = xmlNanoFTPConnectTo("localhost", 0);
2095 xmlGenericError(xmlGenericErrorContext,
2096 "Couldn't connect to localhost\n");
2099 xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
2100 output = fopen("/tmp/tstdata", "w");
2101 if (output != NULL) {
2102 if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
2103 xmlGenericError(xmlGenericErrorContext,
2104 "Failed to get file\n");
2107 xmlNanoFTPClose(ctxt);
2111 #endif /* STANDALONE */
2112 #else /* !LIBXML_FTP_ENABLED */
2115 int main(int argc, char **argv) {
2116 xmlGenericError(xmlGenericErrorContext,
2117 "%s : FTP support not compiled in\n", argv[0]);
2120 #endif /* STANDALONE */
2121 #endif /* LIBXML_FTP_ENABLED */
2122 #define bottom_nanoftp
2123 #include "elfgcchack.h"