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, 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, 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, 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)) {
911 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
914 if (tmp->ai_family == AF_INET6) {
915 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
916 ((struct sockaddr_in6 *) &ctxt->ftpAddr)->sin6_port = htons (port);
917 ctxt->controlFd = socket (AF_INET6, SOCK_STREAM, 0);
920 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
921 ((struct sockaddr_in *) &ctxt->ftpAddr)->sin_port = htons (port);
922 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
924 addrlen = tmp->ai_addrlen;
925 freeaddrinfo (result);
931 hp = gethostbyname (proxy);
933 hp = gethostbyname (ctxt->hostname);
935 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname failed");
938 if ((unsigned int) hp->h_length >
939 sizeof(((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr)) {
940 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
947 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET;
948 memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr,
949 hp->h_addr_list[0], hp->h_length);
950 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port = (u_short)htons ((unsigned short)port);
951 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
952 addrlen = sizeof (struct sockaddr_in);
955 if (ctxt->controlFd == INVALID_SOCKET) {
956 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
963 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
965 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a connection");
966 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
967 ctxt->controlFd = INVALID_SOCKET;
972 * Wait for the HELLO from the server.
974 res = xmlNanoFTPGetResponse(ctxt);
976 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
977 ctxt->controlFd = INVALID_SOCKET;
982 * State diagram for the login operation on the FTP server
987 * +---+ USER +---+------------->+---+
988 * | B |---------->| W | 2 ---->| E |
989 * +---+ +---+------ | -->+---+
992 * -------------- ----- | | |
998 * +---+ PASS +---+ 2 | ------>+---+
999 * | |---------->| W |------------->| S |
1000 * +---+ +---+ ---------->+---+
1003 * -------------- -------- |
1009 * +---+ ACCT +---+-- | ----->+---+
1010 * | |---------->| W | 4,5 -------->| F |
1011 * +---+ +---+------------->+---+
1013 * Of course in case of using a proxy this get really nasty and is not
1014 * standardized at all :-(
1020 if (proxyUser != NULL) {
1022 * We need proxy auth
1024 snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
1025 buf[sizeof(buf) - 1] = 0;
1028 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1030 res = send(ctxt->controlFd, buf, len, 0);
1032 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1033 closesocket(ctxt->controlFd);
1034 ctxt->controlFd = INVALID_SOCKET;
1037 res = xmlNanoFTPGetResponse(ctxt);
1040 if (proxyPasswd == NULL)
1043 if (proxyPasswd != NULL)
1044 snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
1046 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
1047 buf[sizeof(buf) - 1] = 0;
1050 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1052 res = send(ctxt->controlFd, buf, len, 0);
1054 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1055 closesocket(ctxt->controlFd);
1056 ctxt->controlFd = INVALID_SOCKET;
1059 res = xmlNanoFTPGetResponse(ctxt);
1061 closesocket(ctxt->controlFd);
1062 ctxt->controlFd = INVALID_SOCKET;
1072 closesocket(ctxt->controlFd);
1073 ctxt->controlFd = INVALID_SOCKET;
1079 * We assume we don't need more authentication to the proxy
1080 * and that it succeeded :-\
1082 switch (proxyType) {
1084 /* we will try in sequence */
1086 /* Using SITE command */
1087 snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
1088 buf[sizeof(buf) - 1] = 0;
1091 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1093 res = send(ctxt->controlFd, buf, len, 0);
1095 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1096 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1097 ctxt->controlFd = INVALID_SOCKET;
1100 res = xmlNanoFTPGetResponse(ctxt);
1102 /* we assume it worked :-\ 1 is error for SITE command */
1106 if (proxyType == 1) {
1107 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1108 ctxt->controlFd = INVALID_SOCKET;
1112 /* USER user@host command */
1113 if (ctxt->user == NULL)
1114 snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n",
1117 snprintf(buf, sizeof(buf), "USER %s@%s\r\n",
1118 ctxt->user, ctxt->hostname);
1119 buf[sizeof(buf) - 1] = 0;
1122 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1124 res = send(ctxt->controlFd, buf, len, 0);
1126 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1127 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1128 ctxt->controlFd = INVALID_SOCKET;
1131 res = xmlNanoFTPGetResponse(ctxt);
1132 if ((res == 1) || (res == 2)) {
1133 /* we assume it worked :-\ */
1137 if (ctxt->passwd == NULL)
1138 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
1140 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
1141 buf[sizeof(buf) - 1] = 0;
1144 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1146 res = send(ctxt->controlFd, buf, len, 0);
1148 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1149 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1150 ctxt->controlFd = INVALID_SOCKET;
1153 res = xmlNanoFTPGetResponse(ctxt);
1154 if ((res == 1) || (res == 2)) {
1155 /* we assume it worked :-\ */
1159 if (proxyType == 2) {
1160 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1161 ctxt->controlFd = INVALID_SOCKET;
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");
1217 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1218 ctxt->controlFd = INVALID_SOCKET;
1226 * xmlNanoFTPConnectTo:
1227 * @server: an FTP server name
1228 * @port: the port (use 21 if 0)
1230 * Tries to open a control connection to the given server/port
1232 * Returns an fTP context or NULL if it failed
1236 xmlNanoFTPConnectTo(const char *server, int port) {
1237 xmlNanoFTPCtxtPtr ctxt;
1245 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL);
1246 ctxt->hostname = xmlMemStrdup(server);
1249 res = xmlNanoFTPConnect(ctxt);
1251 xmlNanoFTPFreeCtxt(ctxt);
1259 * @ctx: an FTP context
1260 * @directory: a directory on the server
1262 * Tries to change the remote directory
1264 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
1268 xmlNanoFTPCwd(void *ctx, const char *directory) {
1269 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1274 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
1275 if (directory == NULL) return 0;
1278 * Expected response code for CWD:
1282 * 500, 501, 502, 421, 530, 550
1284 snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
1285 buf[sizeof(buf) - 1] = 0;
1288 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1290 res = send(ctxt->controlFd, buf, len, 0);
1292 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1295 res = xmlNanoFTPGetResponse(ctxt);
1299 if (res == 2) return(1);
1308 * @ctx: an FTP context
1309 * @file: a file or directory on the server
1311 * Tries to delete an item (file or directory) from server
1313 * Returns -1 incase of error, 1 if DELE worked, 0 if it failed
1317 xmlNanoFTPDele(void *ctx, const char *file) {
1318 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1323 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET) || (file == NULL)) return(-1);
1324 if (file == NULL) return (0);
1327 * Expected response code for DELE:
1332 * 500, 501, 502, 421, 530
1335 snprintf(buf, sizeof(buf), "DELE %s\r\n", file);
1336 buf[sizeof(buf) - 1] = 0;
1339 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1341 res = send(ctxt->controlFd, buf, len, 0);
1343 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1346 res = xmlNanoFTPGetResponse(ctxt);
1350 if (res == 2) return(1);
1357 * xmlNanoFTPGetConnection:
1358 * @ctx: an FTP context
1360 * Try to open a data connection to the server. Currently only
1361 * passive mode is supported.
1363 * Returns -1 incase of error, 0 otherwise
1367 xmlNanoFTPGetConnection(void *ctx) {
1368 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1369 char buf[200], *cur;
1372 unsigned char ad[6], *adp, *portp;
1373 unsigned int temp[6];
1375 struct sockaddr_storage dataAddr;
1377 struct sockaddr_in dataAddr;
1379 XML_SOCKLEN_T dataAddrLen;
1381 if (ctxt == NULL) return INVALID_SOCKET;
1383 memset (&dataAddr, 0, sizeof(dataAddr));
1385 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1386 ctxt->dataFd = socket (AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1387 ((struct sockaddr_in6 *)&dataAddr)->sin6_family = AF_INET6;
1388 dataAddrLen = sizeof(struct sockaddr_in6);
1392 ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1393 ((struct sockaddr_in *)&dataAddr)->sin_family = AF_INET;
1394 dataAddrLen = sizeof (struct sockaddr_in);
1397 if (ctxt->dataFd == INVALID_SOCKET) {
1398 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
1399 return INVALID_SOCKET;
1402 if (ctxt->passive) {
1404 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1405 snprintf (buf, sizeof(buf), "EPSV\r\n");
1408 snprintf (buf, sizeof(buf), "PASV\r\n");
1411 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1413 res = send(ctxt->controlFd, buf, len, 0);
1415 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1416 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1417 return INVALID_SOCKET;
1419 res = xmlNanoFTPReadResponse(ctx);
1422 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1423 return INVALID_SOCKET;
1426 * retry with an active connection
1428 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1432 cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
1433 while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
1435 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1436 if (sscanf (cur, "%u", &temp[0]) != 1) {
1437 __xmlIOErr(XML_FROM_FTP, XML_FTP_EPSV_ANSWER,
1438 "Invalid answer to EPSV\n");
1439 if (ctxt->dataFd != INVALID_SOCKET) {
1440 closesocket (ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1442 return INVALID_SOCKET;
1444 memcpy (&((struct sockaddr_in6 *)&dataAddr)->sin6_addr, &((struct sockaddr_in6 *)&ctxt->ftpAddr)->sin6_addr, sizeof(struct in6_addr));
1445 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = htons (temp[0]);
1450 if (sscanf (cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2],
1451 &temp[3], &temp[4], &temp[5]) != 6) {
1452 __xmlIOErr(XML_FROM_FTP, XML_FTP_PASV_ANSWER,
1453 "Invalid answer to PASV\n");
1454 if (ctxt->dataFd != INVALID_SOCKET) {
1455 closesocket (ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1457 return INVALID_SOCKET;
1459 for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
1460 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_addr, &ad[0], 4);
1461 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_port, &ad[4], 2);
1464 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1465 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a data connection");
1466 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1467 return INVALID_SOCKET;
1470 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1472 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1473 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = 0;
1476 ((struct sockaddr_in *)&dataAddr)->sin_port = 0;
1478 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1479 __xmlIOErr(XML_FROM_FTP, 0, "bind failed");
1480 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1481 return INVALID_SOCKET;
1483 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1485 if (listen(ctxt->dataFd, 1) < 0) {
1486 __xmlIOErr(XML_FROM_FTP, 0, "listen failed");
1487 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1488 return INVALID_SOCKET;
1491 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1492 char buf6[INET6_ADDRSTRLEN];
1493 inet_ntop (AF_INET6, &((struct sockaddr_in6 *)&dataAddr)->sin6_addr,
1494 buf6, INET6_ADDRSTRLEN);
1495 adp = (unsigned char *) buf6;
1496 portp = (unsigned char *) &((struct sockaddr_in6 *)&dataAddr)->sin6_port;
1497 snprintf (buf, sizeof(buf), "EPRT |2|%s|%s|\r\n", adp, portp);
1501 adp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_addr;
1502 portp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_port;
1503 snprintf (buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n",
1504 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1505 portp[0] & 0xff, portp[1] & 0xff);
1508 buf[sizeof(buf) - 1] = 0;
1511 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1514 res = send(ctxt->controlFd, buf, len, 0);
1516 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1517 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1518 return INVALID_SOCKET;
1520 res = xmlNanoFTPGetResponse(ctxt);
1522 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1523 return INVALID_SOCKET;
1526 return(ctxt->dataFd);
1531 * xmlNanoFTPCloseConnection:
1532 * @ctx: an FTP context
1534 * Close the data connection from the server
1536 * Returns -1 incase of error, 0 otherwise
1540 xmlNanoFTPCloseConnection(void *ctx) {
1541 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1546 if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
1548 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1552 FD_SET(ctxt->controlFd, &rfd);
1554 FD_SET(ctxt->controlFd, &efd);
1555 res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
1560 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1565 xmlGenericError(xmlGenericErrorContext,
1566 "xmlNanoFTPCloseConnection: timeout\n");
1568 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1570 res = xmlNanoFTPGetResponse(ctxt);
1572 closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
1580 * xmlNanoFTPParseList:
1581 * @list: some data listing received from the server
1582 * @callback: the user callback
1583 * @userData: the user callback data
1585 * Parse at most one entry from the listing.
1587 * Returns -1 incase of error, the length of data parsed otherwise
1591 xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) {
1592 const char *cur = list;
1602 unsigned long size = 0;
1606 if (!strncmp(cur, "total", 5)) {
1608 while (*cur == ' ') cur++;
1609 while ((*cur >= '0') && (*cur <= '9'))
1610 links = (links * 10) + (*cur++ - '0');
1611 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1614 } else if (*list == '+') {
1617 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1619 if (*cur == 0) return(0);
1621 while (*cur != ' ') {
1625 if (*cur == 0) return(0);
1628 while (*cur == ' ') cur++;
1629 if (*cur == 0) return(0);
1630 while ((*cur >= '0') && (*cur <= '9'))
1631 links = (links * 10) + (*cur++ - '0');
1632 while (*cur == ' ') cur++;
1633 if (*cur == 0) return(0);
1635 while (*cur != ' ') {
1639 if (*cur == 0) return(0);
1642 while (*cur == ' ') cur++;
1643 if (*cur == 0) return(0);
1645 while (*cur != ' ') {
1649 if (*cur == 0) return(0);
1652 while (*cur == ' ') cur++;
1653 if (*cur == 0) return(0);
1654 while ((*cur >= '0') && (*cur <= '9'))
1655 size = (size * 10) + (*cur++ - '0');
1656 while (*cur == ' ') cur++;
1657 if (*cur == 0) return(0);
1659 while (*cur != ' ') {
1663 if (*cur == 0) return(0);
1666 while (*cur == ' ') cur++;
1667 if (*cur == 0) return(0);
1668 while ((*cur >= '0') && (*cur <= '9'))
1669 day = (day * 10) + (*cur++ - '0');
1670 while (*cur == ' ') cur++;
1671 if (*cur == 0) return(0);
1672 if ((cur[1] == 0) || (cur[2] == 0)) return(0);
1673 if ((cur[1] == ':') || (cur[2] == ':')) {
1674 while ((*cur >= '0') && (*cur <= '9'))
1675 hour = (hour * 10) + (*cur++ - '0');
1676 if (*cur == ':') cur++;
1677 while ((*cur >= '0') && (*cur <= '9'))
1678 minute = (minute * 10) + (*cur++ - '0');
1680 while ((*cur >= '0') && (*cur <= '9'))
1681 year = (year * 10) + (*cur++ - '0');
1683 while (*cur == ' ') cur++;
1684 if (*cur == 0) return(0);
1686 while ((*cur != '\n') && (*cur != '\r')) {
1688 filename[i++] = *cur;
1690 if (*cur == 0) return(0);
1693 if ((*cur != '\n') && (*cur != '\r'))
1695 while ((*cur == '\n') || (*cur == '\r'))
1698 if (callback != NULL) {
1699 callback(userData, filename, attrib, owner, group, size, links,
1700 year, month, day, hour, minute);
1707 * @ctx: an FTP context
1708 * @callback: the user callback
1709 * @userData: the user callback data
1710 * @filename: optional files to list
1712 * Do a listing on the server. All files info are passed back
1715 * Returns -1 incase of error, 0 otherwise
1719 xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
1720 const char *filename) {
1721 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1728 if (ctxt == NULL) return (-1);
1729 if (filename == NULL) {
1730 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1732 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1733 if (ctxt->dataFd == INVALID_SOCKET)
1735 snprintf(buf, sizeof(buf), "LIST -L\r\n");
1737 if (filename[0] != '/') {
1738 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1741 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1742 if (ctxt->dataFd == INVALID_SOCKET)
1744 snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
1746 buf[sizeof(buf) - 1] = 0;
1749 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1751 res = send(ctxt->controlFd, buf, len, 0);
1753 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1754 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1757 res = xmlNanoFTPReadResponse(ctxt);
1759 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1767 FD_SET(ctxt->dataFd, &rfd);
1769 FD_SET(ctxt->dataFd, &efd);
1770 res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
1775 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1779 res = xmlNanoFTPCheckResponse(ctxt);
1781 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1782 ctxt->dataFd = INVALID_SOCKET;
1786 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1793 if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) {
1794 __xmlIOErr(XML_FROM_FTP, 0, "recv");
1795 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1796 ctxt->dataFd = INVALID_SOCKET;
1800 write(1, &buf[indx], len);
1806 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1810 memmove(&buf[0], &buf[base], indx - base);
1813 xmlNanoFTPCloseConnection(ctxt);
1818 * xmlNanoFTPGetSocket:
1819 * @ctx: an FTP context
1820 * @filename: the file to retrieve (or NULL if path is in context).
1822 * Initiate fetch of the given file from the server.
1824 * Returns the socket for the data connection, or <0 in case of error
1829 xmlNanoFTPGetSocket(void *ctx, const char *filename) {
1830 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1834 return INVALID_SOCKET;
1835 if ((filename == NULL) && (ctxt->path == NULL))
1836 return INVALID_SOCKET;
1837 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1838 if (ctxt->dataFd == INVALID_SOCKET)
1839 return INVALID_SOCKET;
1841 snprintf(buf, sizeof(buf), "TYPE I\r\n");
1844 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1846 res = send(ctxt->controlFd, buf, len, 0);
1848 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1849 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1850 return INVALID_SOCKET;
1852 res = xmlNanoFTPReadResponse(ctxt);
1854 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1855 return INVALID_SOCKET;
1857 if (filename == NULL)
1858 snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
1860 snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
1861 buf[sizeof(buf) - 1] = 0;
1864 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1866 res = send(ctxt->controlFd, buf, len, 0);
1868 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1869 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1870 return INVALID_SOCKET;
1872 res = xmlNanoFTPReadResponse(ctxt);
1874 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1875 return INVALID_SOCKET;
1877 return(ctxt->dataFd);
1882 * @ctx: an FTP context
1883 * @callback: the user callback
1884 * @userData: the user callback data
1885 * @filename: the file to retrieve
1887 * Fetch the given file from the server. All data are passed back
1888 * in the callbacks. The last callback has a size of 0 block.
1890 * Returns -1 incase of error, 0 otherwise
1894 xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
1895 const char *filename) {
1896 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1902 if (ctxt == NULL) return(-1);
1903 if ((filename == NULL) && (ctxt->path == NULL))
1905 if (callback == NULL)
1907 if (xmlNanoFTPGetSocket(ctxt, filename) == INVALID_SOCKET)
1914 FD_SET(ctxt->dataFd, &rfd);
1915 res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
1920 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1924 res = xmlNanoFTPCheckResponse(ctxt);
1926 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1927 ctxt->dataFd = INVALID_SOCKET;
1931 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1937 if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) {
1938 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
1939 callback(userData, buf, len);
1940 closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
1943 callback(userData, buf, len);
1946 return(xmlNanoFTPCloseConnection(ctxt));
1951 * @ctx: the FTP context
1953 * @len: the buffer length
1955 * This function tries to read @len bytes from the existing FTP connection
1956 * and saves them in @dest. This is a blocking call.
1958 * Returns the number of byte read. 0 is an indication of an end of connection.
1959 * -1 indicates a parameter error.
1962 xmlNanoFTPRead(void *ctx, void *dest, int len) {
1963 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1965 if (ctx == NULL) return(-1);
1966 if (ctxt->dataFd == INVALID_SOCKET) return(0);
1967 if (dest == NULL) return(-1);
1968 if (len <= 0) return(0);
1970 len = recv(ctxt->dataFd, dest, len, 0);
1973 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
1974 xmlNanoFTPCloseConnection(ctxt);
1977 xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
1984 * @URL: the URL to the resource
1986 * Start to fetch the given ftp:// resource
1988 * Returns an FTP context, or NULL
1992 xmlNanoFTPOpen(const char *URL) {
1993 xmlNanoFTPCtxtPtr ctxt;
1997 if (URL == NULL) return(NULL);
1998 if (strncmp("ftp://", URL, 6)) return(NULL);
2000 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL);
2001 if (ctxt == NULL) return(NULL);
2002 if (xmlNanoFTPConnect(ctxt) < 0) {
2003 xmlNanoFTPFreeCtxt(ctxt);
2006 sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
2007 if (sock == INVALID_SOCKET) {
2008 xmlNanoFTPFreeCtxt(ctxt);
2016 * @ctx: an FTP context
2018 * Close the connection and both control and transport
2020 * Returns -1 incase of error, 0 otherwise
2024 xmlNanoFTPClose(void *ctx) {
2025 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2030 if (ctxt->dataFd != INVALID_SOCKET) {
2031 closesocket(ctxt->dataFd);
2032 ctxt->dataFd = INVALID_SOCKET;
2034 if (ctxt->controlFd != INVALID_SOCKET) {
2035 xmlNanoFTPQuit(ctxt);
2036 closesocket(ctxt->controlFd);
2037 ctxt->controlFd = INVALID_SOCKET;
2039 xmlNanoFTPFreeCtxt(ctxt);
2044 /************************************************************************
2046 * Basic test in Standalone mode *
2048 ************************************************************************/
2050 void ftpList(void *userData, const char *filename, const char* attrib,
2051 const char *owner, const char *group, unsigned long size, int links,
2052 int year, const char *month, int day, int hour, int minute) {
2053 xmlGenericError(xmlGenericErrorContext,
2054 "%s %s %s %ld %s\n", attrib, owner, group, size, filename);
2057 void ftpData(void *userData, const char *data, int len) {
2058 if (userData == NULL) return;
2060 fclose((FILE*)userData);
2063 fwrite(data, len, 1, (FILE*)userData);
2066 int main(int argc, char **argv) {
2069 char *tstfile = NULL;
2073 ctxt = xmlNanoFTPNewCtxt(argv[1]);
2074 if (xmlNanoFTPConnect(ctxt) < 0) {
2075 xmlGenericError(xmlGenericErrorContext,
2076 "Couldn't connect to %s\n", argv[1]);
2082 ctxt = xmlNanoFTPConnectTo("localhost", 0);
2084 xmlGenericError(xmlGenericErrorContext,
2085 "Couldn't connect to localhost\n");
2088 xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
2089 output = fopen("/tmp/tstdata", "w");
2090 if (output != NULL) {
2091 if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
2092 xmlGenericError(xmlGenericErrorContext,
2093 "Failed to get file\n");
2096 xmlNanoFTPClose(ctxt);
2100 #endif /* STANDALONE */
2101 #else /* !LIBXML_FTP_ENABLED */
2104 int main(int argc, char **argv) {
2105 xmlGenericError(xmlGenericErrorContext,
2106 "%s : FTP support not compiled in\n", argv[0]);
2109 #endif /* STANDALONE */
2110 #endif /* LIBXML_FTP_ENABLED */
2111 #define bottom_nanoftp
2112 #include "elfgcchack.h"