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)
82 #include <wsockcompat.h>
85 #define XML_SOCKLEN_T unsigned int
89 * A couple portability macros
92 #if !defined(__BEOS__) || defined(__HAIKU__)
93 #define closesocket(s) close(s)
100 #define PF_INET AF_INET
105 #ifdef HAVE_BROKEN_SS_FAMILY
106 #define ss_family __ss_family
110 #ifndef XML_SOCKLEN_T
111 #define XML_SOCKLEN_T unsigned int
114 #define FTP_COMMAND_OK 200
115 #define FTP_SYNTAX_ERROR 500
116 #define FTP_GET_PASSWD 331
117 #define FTP_BUF_SIZE 1024
119 #define XML_NANO_MAX_URLBUF 4096
121 typedef struct xmlNanoFTPCtxt {
122 char *protocol; /* the protocol name */
123 char *hostname; /* the host name */
124 int port; /* the port */
125 char *path; /* the path within the URL */
126 char *user; /* user string */
127 char *passwd; /* passwd string */
129 struct sockaddr_storage ftpAddr; /* this is large enough to hold IPv6 address*/
131 struct sockaddr_in ftpAddr; /* the socket address struct */
133 int passive; /* currently we support only passive !!! */
134 SOCKET controlFd; /* the file descriptor for the control socket */
135 SOCKET dataFd; /* the file descriptor for the data socket */
136 int state; /* WRITE / READ / CLOSED */
137 int returnValue; /* the protocol return value */
138 /* buffer for data received from the control connection */
139 char controlBuf[FTP_BUF_SIZE + 1];
142 int controlBufAnswer;
143 } xmlNanoFTPCtxt, *xmlNanoFTPCtxtPtr;
145 static int initialized = 0;
146 static char *proxy = NULL; /* the proxy name if any */
147 static int proxyPort = 0; /* the proxy port if any */
148 static char *proxyUser = NULL; /* user for proxy authentication */
149 static char *proxyPasswd = NULL;/* passwd for proxy authentication */
150 static int proxyType = 0; /* uses TYPE or a@b ? */
154 int have_ipv6(void) {
157 s = socket (AF_INET6, SOCK_STREAM, 0);
168 * @extra: extra informations
170 * Handle an out of memory condition
173 xmlFTPErrMemory(const char *extra)
175 __xmlSimpleError(XML_FROM_FTP, XML_ERR_NO_MEMORY, NULL, NULL, extra);
181 * Initialize the FTP protocol layer.
182 * Currently it just checks for proxy informations,
183 * and get the hostname
187 xmlNanoFTPInit(void) {
197 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
202 env = getenv("no_proxy");
203 if (env && ((env[0] == '*' ) && (env[1] == 0)))
205 env = getenv("ftp_proxy");
207 xmlNanoFTPScanProxy(env);
209 env = getenv("FTP_PROXY");
211 xmlNanoFTPScanProxy(env);
214 env = getenv("ftp_proxy_user");
216 proxyUser = xmlMemStrdup(env);
218 env = getenv("ftp_proxy_password");
220 proxyPasswd = xmlMemStrdup(env);
228 * Cleanup the FTP protocol layer. This cleanup proxy informations.
232 xmlNanoFTPCleanup(void) {
237 if (proxyUser != NULL) {
241 if (proxyPasswd != NULL) {
242 xmlFree(proxyPasswd);
254 * @host: the proxy host name
255 * @port: the proxy port
256 * @user: the proxy user name
257 * @passwd: the proxy password
258 * @type: the type of proxy 1 for using SITE, 2 for USER a@b
260 * Setup the FTP proxy informations.
261 * This can also be done by using ftp_proxy ftp_proxy_user and
262 * ftp_proxy_password environment variables.
266 xmlNanoFTPProxy(const char *host, int port, const char *user,
267 const char *passwd, int type) {
272 if (proxyUser != NULL) {
276 if (proxyPasswd != NULL) {
277 xmlFree(proxyPasswd);
281 proxy = xmlMemStrdup(host);
283 proxyUser = xmlMemStrdup(user);
285 proxyPasswd = xmlMemStrdup(passwd);
292 * @ctx: an FTP context
293 * @URL: The URL used to initialize the context
295 * (Re)Initialize an FTP context by parsing the URL and finding
296 * the protocol host port and path it indicates.
300 xmlNanoFTPScanURL(void *ctx, const char *URL) {
301 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
305 * Clear any existing data from the context
307 if (ctxt->protocol != NULL) {
308 xmlFree(ctxt->protocol);
309 ctxt->protocol = NULL;
311 if (ctxt->hostname != NULL) {
312 xmlFree(ctxt->hostname);
313 ctxt->hostname = NULL;
315 if (ctxt->path != NULL) {
319 if (URL == NULL) return;
321 uri = xmlParseURIRaw(URL, 1);
325 if ((uri->scheme == NULL) || (uri->server == NULL)) {
330 ctxt->protocol = xmlMemStrdup(uri->scheme);
331 ctxt->hostname = xmlMemStrdup(uri->server);
332 if (uri->path != NULL)
333 ctxt->path = xmlMemStrdup(uri->path);
335 ctxt->path = xmlMemStrdup("/");
337 ctxt->port = uri->port;
339 if (uri->user != NULL) {
341 if ((cptr=strchr(uri->user, ':')) == NULL)
342 ctxt->user = xmlMemStrdup(uri->user);
344 ctxt->user = (char *)xmlStrndup((xmlChar *)uri->user,
346 ctxt->passwd = xmlMemStrdup(cptr+1);
355 * xmlNanoFTPUpdateURL:
356 * @ctx: an FTP context
357 * @URL: The URL used to update the context
359 * Update an FTP context by parsing the URL and finding
360 * new path it indicates. If there is an error in the
361 * protocol, hostname, port or other information, the
362 * error is raised. It indicates a new connection has to
365 * Returns 0 if Ok, -1 in case of error (other host).
369 xmlNanoFTPUpdateURL(void *ctx, const char *URL) {
370 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
377 if (ctxt->protocol == NULL)
379 if (ctxt->hostname == NULL)
382 uri = xmlParseURIRaw(URL, 1);
386 if ((uri->scheme == NULL) || (uri->server == NULL)) {
390 if ((strcmp(ctxt->protocol, uri->scheme)) ||
391 (strcmp(ctxt->hostname, uri->server)) ||
392 ((uri->port != 0) && (ctxt->port != uri->port))) {
398 ctxt->port = uri->port;
400 if (ctxt->path != NULL) {
405 if (uri->path == NULL)
406 ctxt->path = xmlMemStrdup("/");
408 ctxt->path = xmlMemStrdup(uri->path);
416 * xmlNanoFTPScanProxy:
417 * @URL: The proxy URL used to initialize the proxy context
419 * (Re)Initialize the FTP Proxy context by parsing the URL and finding
420 * the protocol host port it indicates.
421 * Should be like ftp://myproxy/ or ftp://myproxy:3128/
422 * A NULL URL cleans up proxy informations.
426 xmlNanoFTPScanProxy(const char *URL) {
437 xmlGenericError(xmlGenericErrorContext,
438 "Removing FTP proxy info\n");
440 xmlGenericError(xmlGenericErrorContext,
441 "Using FTP proxy %s\n", URL);
443 if (URL == NULL) return;
445 uri = xmlParseURIRaw(URL, 1);
446 if ((uri == NULL) || (uri->scheme == NULL) ||
447 (strcmp(uri->scheme, "ftp")) || (uri->server == NULL)) {
448 __xmlIOErr(XML_FROM_FTP, XML_FTP_URL_SYNTAX, "Syntax Error\n");
454 proxy = xmlMemStrdup(uri->server);
456 proxyPort = uri->port;
463 * @URL: The URL used to initialize the context
465 * Allocate and initialize a new FTP context.
467 * Returns an FTP context or NULL in case of error.
471 xmlNanoFTPNewCtxt(const char *URL) {
472 xmlNanoFTPCtxtPtr ret;
475 ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt));
477 xmlFTPErrMemory("allocating FTP context");
481 memset(ret, 0, sizeof(xmlNanoFTPCtxt));
484 ret->returnValue = 0;
485 ret->controlBufIndex = 0;
486 ret->controlBufUsed = 0;
489 unescaped = xmlURIUnescapeString(URL, 0, NULL);
490 if (unescaped != NULL) {
491 xmlNanoFTPScanURL(ret, unescaped);
493 } else if (URL != NULL)
494 xmlNanoFTPScanURL(ret, URL);
500 * xmlNanoFTPFreeCtxt:
501 * @ctx: an FTP context
503 * Frees the context after closing the connection.
507 xmlNanoFTPFreeCtxt(void * ctx) {
508 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
509 if (ctxt == NULL) return;
510 if (ctxt->hostname != NULL) xmlFree(ctxt->hostname);
511 if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
512 if (ctxt->path != NULL) xmlFree(ctxt->path);
514 if (ctxt->controlFd >= 0) closesocket(ctxt->controlFd);
515 ctxt->controlFd = -1;
516 ctxt->controlBufIndex = -1;
517 ctxt->controlBufUsed = -1;
522 * xmlNanoFTPParseResponse:
523 * @buf: the buffer containing the response
524 * @len: the buffer length
526 * Parsing of the server answer, we just extract the code.
528 * returns 0 for errors
529 * +XXX for last line of response
530 * -XXX for response to be continued
533 xmlNanoFTPParseResponse(char *buf, int len) {
536 if (len < 3) return(-1);
537 if ((*buf >= '0') && (*buf <= '9'))
538 val = val * 10 + (*buf - '0');
542 if ((*buf >= '0') && (*buf <= '9'))
543 val = val * 10 + (*buf - '0');
547 if ((*buf >= '0') && (*buf <= '9'))
548 val = val * 10 + (*buf - '0');
559 * @ctx: an FTP context
561 * Read more information from the FTP control connection
562 * Returns the number of bytes read, < 0 indicates an error
565 xmlNanoFTPGetMore(void *ctx) {
566 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
570 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
572 if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) {
574 xmlGenericError(xmlGenericErrorContext,
575 "xmlNanoFTPGetMore : controlBufIndex = %d\n",
576 ctxt->controlBufIndex);
581 if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) {
583 xmlGenericError(xmlGenericErrorContext,
584 "xmlNanoFTPGetMore : controlBufUsed = %d\n",
585 ctxt->controlBufUsed);
589 if (ctxt->controlBufIndex > ctxt->controlBufUsed) {
591 xmlGenericError(xmlGenericErrorContext,
592 "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n",
593 ctxt->controlBufIndex, ctxt->controlBufUsed);
599 * First pack the control buffer
601 if (ctxt->controlBufIndex > 0) {
602 memmove(&ctxt->controlBuf[0], &ctxt->controlBuf[ctxt->controlBufIndex],
603 ctxt->controlBufUsed - ctxt->controlBufIndex);
604 ctxt->controlBufUsed -= ctxt->controlBufIndex;
605 ctxt->controlBufIndex = 0;
607 size = FTP_BUF_SIZE - ctxt->controlBufUsed;
610 xmlGenericError(xmlGenericErrorContext,
611 "xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed);
617 * Read the amount left on the control connection
619 if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex],
621 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
622 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
623 ctxt->controlFd = -1;
627 xmlGenericError(xmlGenericErrorContext,
628 "xmlNanoFTPGetMore : read %d [%d - %d]\n", len,
629 ctxt->controlBufUsed, ctxt->controlBufUsed + len);
631 ctxt->controlBufUsed += len;
632 ctxt->controlBuf[ctxt->controlBufUsed] = 0;
638 * xmlNanoFTPReadResponse:
639 * @ctx: an FTP context
641 * Read the response from the FTP server after a command.
642 * Returns the code number
645 xmlNanoFTPReadResponse(void *ctx) {
646 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
649 int res = -1, cur = -1;
651 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
655 * Assumes everything up to controlBuf[controlBufIndex] has been read
658 len = xmlNanoFTPGetMore(ctx);
662 if ((ctxt->controlBufUsed == 0) && (len == 0)) {
665 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
666 end = &ctxt->controlBuf[ctxt->controlBufUsed];
669 xmlGenericError(xmlGenericErrorContext,
670 "\n<<<\n%s\n--\n", ptr);
673 cur = xmlNanoFTPParseResponse(ptr, end - ptr);
676 * Successfully scanned the control code, scratch
677 * till the end of the line, but keep the index to be
678 * able to analyze the result if needed.
682 ctxt->controlBufAnswer = ptr - ctxt->controlBuf;
683 while ((ptr < end) && (*ptr != '\n')) ptr++;
684 if (*ptr == '\n') ptr++;
685 if (*ptr == '\r') ptr++;
688 while ((ptr < end) && (*ptr != '\n')) ptr++;
690 ctxt->controlBufIndex = ctxt->controlBufUsed;
693 if (*ptr != '\r') ptr++;
696 if (res < 0) goto get_more;
697 ctxt->controlBufIndex = ptr - ctxt->controlBuf;
699 ptr = &ctxt->controlBuf[ctxt->controlBufIndex];
700 xmlGenericError(xmlGenericErrorContext, "\n---\n%s\n--\n", ptr);
704 xmlGenericError(xmlGenericErrorContext, "Got %d\n", res);
710 * xmlNanoFTPGetResponse:
711 * @ctx: an FTP context
713 * Get the response from the FTP server after a command.
714 * Returns the code number
718 xmlNanoFTPGetResponse(void *ctx) {
721 res = xmlNanoFTPReadResponse(ctx);
727 * xmlNanoFTPCheckResponse:
728 * @ctx: an FTP context
730 * Check if there is a response from the FTP server after a command.
731 * Returns the code number, or 0
735 xmlNanoFTPCheckResponse(void *ctx) {
736 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
740 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
744 FD_SET(ctxt->controlFd, &rfd);
745 switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) {
749 __xmlIOErr(XML_FROM_FTP, 0, "select");
754 return(xmlNanoFTPReadResponse(ctx));
758 * Send the user authentication
762 xmlNanoFTPSendUser(void *ctx) {
763 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
768 if (ctxt->user == NULL)
769 snprintf(buf, sizeof(buf), "USER anonymous\r\n");
771 snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user);
772 buf[sizeof(buf) - 1] = 0;
775 xmlGenericError(xmlGenericErrorContext, "%s", buf);
777 res = send(ctxt->controlFd, buf, len, 0);
779 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
786 * Send the password authentication
790 xmlNanoFTPSendPasswd(void *ctx) {
791 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
796 if (ctxt->passwd == NULL)
797 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
799 snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd);
800 buf[sizeof(buf) - 1] = 0;
803 xmlGenericError(xmlGenericErrorContext, "%s", buf);
805 res = send(ctxt->controlFd, buf, len, 0);
807 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
815 * @ctx: an FTP context
817 * Send a QUIT command to the server
819 * Returns -1 in case of error, 0 otherwise
824 xmlNanoFTPQuit(void *ctx) {
825 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
829 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
831 snprintf(buf, sizeof(buf), "QUIT\r\n");
834 xmlGenericError(xmlGenericErrorContext, "%s", buf); /* Just to be consistent, even though we know it can't have a % in it */
836 res = send(ctxt->controlFd, buf, len, 0);
838 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
846 * @ctx: an FTP context
848 * Tries to open a control connection
850 * Returns -1 in case of error, 0 otherwise
854 xmlNanoFTPConnect(void *ctx) {
855 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
859 int addrlen = sizeof (struct sockaddr_in);
863 if (ctxt->hostname == NULL)
867 * do the blocking DNS query.
877 memset (&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr));
881 struct addrinfo hints, *tmp, *result;
884 memset (&hints, 0, sizeof(hints));
885 hints.ai_socktype = SOCK_STREAM;
888 if (getaddrinfo (proxy, NULL, &hints, &result) != 0) {
889 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
894 if (getaddrinfo (ctxt->hostname, NULL, &hints, &result) != 0) {
895 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
899 for (tmp = result; tmp; tmp = tmp->ai_next)
900 if (tmp->ai_family == AF_INET || tmp->ai_family == AF_INET6)
905 freeaddrinfo (result);
906 __xmlIOErr(XML_FROM_FTP, 0, "getaddrinfo failed");
909 if (tmp->ai_addrlen > sizeof(ctxt->ftpAddr)) {
910 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
913 if (tmp->ai_family == AF_INET6) {
914 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
915 ((struct sockaddr_in6 *) &ctxt->ftpAddr)->sin6_port = htons (port);
916 ctxt->controlFd = socket (AF_INET6, SOCK_STREAM, 0);
919 memcpy (&ctxt->ftpAddr, tmp->ai_addr, tmp->ai_addrlen);
920 ((struct sockaddr_in *) &ctxt->ftpAddr)->sin_port = htons (port);
921 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
923 addrlen = tmp->ai_addrlen;
924 freeaddrinfo (result);
930 hp = gethostbyname (proxy);
932 hp = gethostbyname (ctxt->hostname);
934 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname failed");
937 if ((unsigned int) hp->h_length >
938 sizeof(((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr)) {
939 __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
946 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET;
947 memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr,
948 hp->h_addr_list[0], hp->h_length);
949 ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port = (u_short)htons ((unsigned short)port);
950 ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
951 addrlen = sizeof (struct sockaddr_in);
954 if (ctxt->controlFd < 0) {
955 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
962 if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
964 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a connection");
965 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
966 ctxt->controlFd = -1;
971 * Wait for the HELLO from the server.
973 res = xmlNanoFTPGetResponse(ctxt);
975 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
976 ctxt->controlFd = -1;
981 * State diagram for the login operation on the FTP server
986 * +---+ USER +---+------------->+---+
987 * | B |---------->| W | 2 ---->| E |
988 * +---+ +---+------ | -->+---+
991 * -------------- ----- | | |
997 * +---+ PASS +---+ 2 | ------>+---+
998 * | |---------->| W |------------->| S |
999 * +---+ +---+ ---------->+---+
1002 * -------------- -------- |
1008 * +---+ ACCT +---+-- | ----->+---+
1009 * | |---------->| W | 4,5 -------->| F |
1010 * +---+ +---+------------->+---+
1012 * Of course in case of using a proxy this get really nasty and is not
1013 * standardized at all :-(
1019 if (proxyUser != NULL) {
1021 * We need proxy auth
1023 snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser);
1024 buf[sizeof(buf) - 1] = 0;
1027 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1029 res = send(ctxt->controlFd, buf, len, 0);
1031 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1032 closesocket(ctxt->controlFd);
1033 ctxt->controlFd = -1;
1036 res = xmlNanoFTPGetResponse(ctxt);
1039 if (proxyPasswd == NULL)
1042 if (proxyPasswd != NULL)
1043 snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd);
1045 snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
1046 buf[sizeof(buf) - 1] = 0;
1049 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1051 res = send(ctxt->controlFd, buf, len, 0);
1053 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1054 closesocket(ctxt->controlFd);
1055 ctxt->controlFd = -1;
1058 res = xmlNanoFTPGetResponse(ctxt);
1060 closesocket(ctxt->controlFd);
1061 ctxt->controlFd = -1;
1071 closesocket(ctxt->controlFd);
1072 ctxt->controlFd = -1;
1078 * We assume we don't need more authentication to the proxy
1079 * and that it succeeded :-\
1081 switch (proxyType) {
1083 /* we will try in sequence */
1085 /* Using SITE command */
1086 snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname);
1087 buf[sizeof(buf) - 1] = 0;
1090 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1092 res = send(ctxt->controlFd, buf, len, 0);
1094 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1095 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1096 ctxt->controlFd = -1;
1099 res = xmlNanoFTPGetResponse(ctxt);
1101 /* we assume it worked :-\ 1 is error for SITE command */
1105 if (proxyType == 1) {
1106 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1107 ctxt->controlFd = -1;
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, buf, len, 0);
1125 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1126 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1127 ctxt->controlFd = -1;
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, buf, len, 0);
1147 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1148 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1149 ctxt->controlFd = -1;
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 = -1;
1160 ctxt->controlFd = -1;
1165 * If you need support for other Proxy authentication scheme
1166 * send the code or at least the sequence in use.
1169 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1170 ctxt->controlFd = -1;
1175 * Non-proxy handling.
1177 res = xmlNanoFTPSendUser(ctxt);
1179 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1180 ctxt->controlFd = -1;
1183 res = xmlNanoFTPGetResponse(ctxt);
1194 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1195 ctxt->controlFd = -1;
1198 res = xmlNanoFTPSendPasswd(ctxt);
1200 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1201 ctxt->controlFd = -1;
1204 res = xmlNanoFTPGetResponse(ctxt);
1209 __xmlIOErr(XML_FROM_FTP, XML_FTP_ACCNT,
1210 "FTP server asking for ACCNT on anonymous\n");
1216 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1217 ctxt->controlFd = -1;
1225 * xmlNanoFTPConnectTo:
1226 * @server: an FTP server name
1227 * @port: the port (use 21 if 0)
1229 * Tries to open a control connection to the given server/port
1231 * Returns an fTP context or NULL if it failed
1235 xmlNanoFTPConnectTo(const char *server, int port) {
1236 xmlNanoFTPCtxtPtr ctxt;
1244 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL);
1245 ctxt->hostname = xmlMemStrdup(server);
1248 res = xmlNanoFTPConnect(ctxt);
1250 xmlNanoFTPFreeCtxt(ctxt);
1258 * @ctx: an FTP context
1259 * @directory: a directory on the server
1261 * Tries to change the remote directory
1263 * Returns -1 incase of error, 1 if CWD worked, 0 if it failed
1267 xmlNanoFTPCwd(void *ctx, const char *directory) {
1268 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1273 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
1274 if (directory == NULL) return 0;
1277 * Expected response code for CWD:
1281 * 500, 501, 502, 421, 530, 550
1283 snprintf(buf, sizeof(buf), "CWD %s\r\n", directory);
1284 buf[sizeof(buf) - 1] = 0;
1287 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1289 res = send(ctxt->controlFd, buf, len, 0);
1291 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1294 res = xmlNanoFTPGetResponse(ctxt);
1298 if (res == 2) return(1);
1307 * @ctx: an FTP context
1308 * @file: a file or directory on the server
1310 * Tries to delete an item (file or directory) from server
1312 * Returns -1 incase of error, 1 if DELE worked, 0 if it failed
1316 xmlNanoFTPDele(void *ctx, const char *file) {
1317 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1322 if ((ctxt == NULL) || (ctxt->controlFd < 0) || (file == NULL)) return(-1);
1323 if (file == NULL) return (0);
1326 * Expected response code for DELE:
1331 * 500, 501, 502, 421, 530
1334 snprintf(buf, sizeof(buf), "DELE %s\r\n", file);
1335 buf[sizeof(buf) - 1] = 0;
1338 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1340 res = send(ctxt->controlFd, buf, len, 0);
1342 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1345 res = xmlNanoFTPGetResponse(ctxt);
1349 if (res == 2) return(1);
1356 * xmlNanoFTPGetConnection:
1357 * @ctx: an FTP context
1359 * Try to open a data connection to the server. Currently only
1360 * passive mode is supported.
1362 * Returns -1 incase of error, 0 otherwise
1366 xmlNanoFTPGetConnection(void *ctx) {
1367 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1368 char buf[200], *cur;
1371 unsigned char ad[6], *adp, *portp;
1372 unsigned int temp[6];
1374 struct sockaddr_storage dataAddr;
1376 struct sockaddr_in dataAddr;
1378 XML_SOCKLEN_T dataAddrLen;
1380 if (ctxt == NULL) return(-1);
1382 memset (&dataAddr, 0, sizeof(dataAddr));
1384 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1385 ctxt->dataFd = socket (AF_INET6, SOCK_STREAM, IPPROTO_TCP);
1386 ((struct sockaddr_in6 *)&dataAddr)->sin6_family = AF_INET6;
1387 dataAddrLen = sizeof(struct sockaddr_in6);
1391 ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1392 ((struct sockaddr_in *)&dataAddr)->sin_family = AF_INET;
1393 dataAddrLen = sizeof (struct sockaddr_in);
1396 if (ctxt->dataFd < 0) {
1397 __xmlIOErr(XML_FROM_FTP, 0, "socket failed");
1401 if (ctxt->passive) {
1403 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1404 snprintf (buf, sizeof(buf), "EPSV\r\n");
1407 snprintf (buf, sizeof(buf), "PASV\r\n");
1410 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1412 res = send(ctxt->controlFd, buf, len, 0);
1414 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1415 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1418 res = xmlNanoFTPReadResponse(ctx);
1421 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1425 * retry with an active connection
1427 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1431 cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
1432 while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
1434 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1435 if (sscanf (cur, "%u", &temp[0]) != 1) {
1436 __xmlIOErr(XML_FROM_FTP, XML_FTP_EPSV_ANSWER,
1437 "Invalid answer to EPSV\n");
1438 if (ctxt->dataFd != -1) {
1439 closesocket (ctxt->dataFd); ctxt->dataFd = -1;
1443 memcpy (&((struct sockaddr_in6 *)&dataAddr)->sin6_addr, &((struct sockaddr_in6 *)&ctxt->ftpAddr)->sin6_addr, sizeof(struct in6_addr));
1444 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = htons (temp[0]);
1449 if (sscanf (cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2],
1450 &temp[3], &temp[4], &temp[5]) != 6) {
1451 __xmlIOErr(XML_FROM_FTP, XML_FTP_PASV_ANSWER,
1452 "Invalid answer to PASV\n");
1453 if (ctxt->dataFd != -1) {
1454 closesocket (ctxt->dataFd); ctxt->dataFd = -1;
1458 for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
1459 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_addr, &ad[0], 4);
1460 memcpy (&((struct sockaddr_in *)&dataAddr)->sin_port, &ad[4], 2);
1463 if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1464 __xmlIOErr(XML_FROM_FTP, 0, "Failed to create a data connection");
1465 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1469 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1471 if ((ctxt->ftpAddr).ss_family == AF_INET6)
1472 ((struct sockaddr_in6 *)&dataAddr)->sin6_port = 0;
1475 ((struct sockaddr_in *)&dataAddr)->sin_port = 0;
1477 if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
1478 __xmlIOErr(XML_FROM_FTP, 0, "bind failed");
1479 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1482 getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
1484 if (listen(ctxt->dataFd, 1) < 0) {
1485 __xmlIOErr(XML_FROM_FTP, 0, "listen failed");
1486 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1490 if ((ctxt->ftpAddr).ss_family == AF_INET6) {
1491 char buf6[INET6_ADDRSTRLEN];
1492 inet_ntop (AF_INET6, &((struct sockaddr_in6 *)&dataAddr)->sin6_addr,
1493 buf6, INET6_ADDRSTRLEN);
1494 adp = (unsigned char *) buf6;
1495 portp = (unsigned char *) &((struct sockaddr_in6 *)&dataAddr)->sin6_port;
1496 snprintf (buf, sizeof(buf), "EPRT |2|%s|%s|\r\n", adp, portp);
1500 adp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_addr;
1501 portp = (unsigned char *) &((struct sockaddr_in *)&dataAddr)->sin_port;
1502 snprintf (buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n",
1503 adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff,
1504 portp[0] & 0xff, portp[1] & 0xff);
1507 buf[sizeof(buf) - 1] = 0;
1510 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1513 res = send(ctxt->controlFd, buf, len, 0);
1515 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1516 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1519 res = xmlNanoFTPGetResponse(ctxt);
1521 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1525 return(ctxt->dataFd);
1530 * xmlNanoFTPCloseConnection:
1531 * @ctx: an FTP context
1533 * Close the data connection from the server
1535 * Returns -1 incase of error, 0 otherwise
1539 xmlNanoFTPCloseConnection(void *ctx) {
1540 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1545 if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
1547 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1551 FD_SET(ctxt->controlFd, &rfd);
1553 FD_SET(ctxt->controlFd, &efd);
1554 res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv);
1559 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1564 xmlGenericError(xmlGenericErrorContext,
1565 "xmlNanoFTPCloseConnection: timeout\n");
1567 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1569 res = xmlNanoFTPGetResponse(ctxt);
1571 closesocket(ctxt->controlFd); ctxt->controlFd = -1;
1579 * xmlNanoFTPParseList:
1580 * @list: some data listing received from the server
1581 * @callback: the user callback
1582 * @userData: the user callback data
1584 * Parse at most one entry from the listing.
1586 * Returns -1 incase of error, the length of data parsed otherwise
1590 xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) {
1591 const char *cur = list;
1601 unsigned long size = 0;
1605 if (!strncmp(cur, "total", 5)) {
1607 while (*cur == ' ') cur++;
1608 while ((*cur >= '0') && (*cur <= '9'))
1609 links = (links * 10) + (*cur++ - '0');
1610 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1613 } else if (*list == '+') {
1616 while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r'))
1618 if (*cur == 0) return(0);
1620 while (*cur != ' ') {
1624 if (*cur == 0) return(0);
1627 while (*cur == ' ') cur++;
1628 if (*cur == 0) return(0);
1629 while ((*cur >= '0') && (*cur <= '9'))
1630 links = (links * 10) + (*cur++ - '0');
1631 while (*cur == ' ') cur++;
1632 if (*cur == 0) return(0);
1634 while (*cur != ' ') {
1638 if (*cur == 0) return(0);
1641 while (*cur == ' ') cur++;
1642 if (*cur == 0) return(0);
1644 while (*cur != ' ') {
1648 if (*cur == 0) return(0);
1651 while (*cur == ' ') cur++;
1652 if (*cur == 0) return(0);
1653 while ((*cur >= '0') && (*cur <= '9'))
1654 size = (size * 10) + (*cur++ - '0');
1655 while (*cur == ' ') cur++;
1656 if (*cur == 0) return(0);
1658 while (*cur != ' ') {
1662 if (*cur == 0) return(0);
1665 while (*cur == ' ') cur++;
1666 if (*cur == 0) return(0);
1667 while ((*cur >= '0') && (*cur <= '9'))
1668 day = (day * 10) + (*cur++ - '0');
1669 while (*cur == ' ') cur++;
1670 if (*cur == 0) return(0);
1671 if ((cur[1] == 0) || (cur[2] == 0)) return(0);
1672 if ((cur[1] == ':') || (cur[2] == ':')) {
1673 while ((*cur >= '0') && (*cur <= '9'))
1674 hour = (hour * 10) + (*cur++ - '0');
1675 if (*cur == ':') cur++;
1676 while ((*cur >= '0') && (*cur <= '9'))
1677 minute = (minute * 10) + (*cur++ - '0');
1679 while ((*cur >= '0') && (*cur <= '9'))
1680 year = (year * 10) + (*cur++ - '0');
1682 while (*cur == ' ') cur++;
1683 if (*cur == 0) return(0);
1685 while ((*cur != '\n') && (*cur != '\r')) {
1687 filename[i++] = *cur;
1689 if (*cur == 0) return(0);
1692 if ((*cur != '\n') && (*cur != '\r'))
1694 while ((*cur == '\n') || (*cur == '\r'))
1697 if (callback != NULL) {
1698 callback(userData, filename, attrib, owner, group, size, links,
1699 year, month, day, hour, minute);
1706 * @ctx: an FTP context
1707 * @callback: the user callback
1708 * @userData: the user callback data
1709 * @filename: optional files to list
1711 * Do a listing on the server. All files info are passed back
1714 * Returns -1 incase of error, 0 otherwise
1718 xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
1719 const char *filename) {
1720 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1727 if (ctxt == NULL) return (-1);
1728 if (filename == NULL) {
1729 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1731 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1732 if (ctxt->dataFd == -1)
1734 snprintf(buf, sizeof(buf), "LIST -L\r\n");
1736 if (filename[0] != '/') {
1737 if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
1740 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1741 if (ctxt->dataFd == -1)
1743 snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
1745 buf[sizeof(buf) - 1] = 0;
1748 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1750 res = send(ctxt->controlFd, buf, len, 0);
1752 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1753 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1756 res = xmlNanoFTPReadResponse(ctxt);
1758 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1766 FD_SET(ctxt->dataFd, &rfd);
1768 FD_SET(ctxt->dataFd, &efd);
1769 res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv);
1774 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1778 res = xmlNanoFTPCheckResponse(ctxt);
1780 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1785 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1792 if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) {
1793 __xmlIOErr(XML_FROM_FTP, 0, "recv");
1794 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1799 write(1, &buf[indx], len);
1805 res = xmlNanoFTPParseList(&buf[base], callback, userData);
1809 memmove(&buf[0], &buf[base], indx - base);
1812 xmlNanoFTPCloseConnection(ctxt);
1817 * xmlNanoFTPGetSocket:
1818 * @ctx: an FTP context
1819 * @filename: the file to retrieve (or NULL if path is in context).
1821 * Initiate fetch of the given file from the server.
1823 * Returns the socket for the data connection, or <0 in case of error
1828 xmlNanoFTPGetSocket(void *ctx, const char *filename) {
1829 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1834 if ((filename == NULL) && (ctxt->path == NULL))
1836 ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
1837 if (ctxt->dataFd == -1)
1840 snprintf(buf, sizeof(buf), "TYPE I\r\n");
1843 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1845 res = send(ctxt->controlFd, buf, len, 0);
1847 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1848 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1851 res = xmlNanoFTPReadResponse(ctxt);
1853 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1856 if (filename == NULL)
1857 snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
1859 snprintf(buf, sizeof(buf), "RETR %s\r\n", filename);
1860 buf[sizeof(buf) - 1] = 0;
1863 xmlGenericError(xmlGenericErrorContext, "%s", buf);
1865 res = send(ctxt->controlFd, buf, len, 0);
1867 __xmlIOErr(XML_FROM_FTP, 0, "send failed");
1868 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1871 res = xmlNanoFTPReadResponse(ctxt);
1873 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1876 return(ctxt->dataFd);
1881 * @ctx: an FTP context
1882 * @callback: the user callback
1883 * @userData: the user callback data
1884 * @filename: the file to retrieve
1886 * Fetch the given file from the server. All data are passed back
1887 * in the callbacks. The last callback has a size of 0 block.
1889 * Returns -1 incase of error, 0 otherwise
1893 xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
1894 const char *filename) {
1895 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1901 if (ctxt == NULL) return(-1);
1902 if ((filename == NULL) && (ctxt->path == NULL))
1904 if (callback == NULL)
1906 if (xmlNanoFTPGetSocket(ctxt, filename) < 0)
1913 FD_SET(ctxt->dataFd, &rfd);
1914 res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv);
1919 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1923 res = xmlNanoFTPCheckResponse(ctxt);
1925 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1930 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1936 if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) {
1937 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
1938 callback(userData, buf, len);
1939 closesocket(ctxt->dataFd); ctxt->dataFd = -1;
1942 callback(userData, buf, len);
1945 return(xmlNanoFTPCloseConnection(ctxt));
1950 * @ctx: the FTP context
1952 * @len: the buffer length
1954 * This function tries to read @len bytes from the existing FTP connection
1955 * and saves them in @dest. This is a blocking call.
1957 * Returns the number of byte read. 0 is an indication of an end of connection.
1958 * -1 indicates a parameter error.
1961 xmlNanoFTPRead(void *ctx, void *dest, int len) {
1962 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
1964 if (ctx == NULL) return(-1);
1965 if (ctxt->dataFd < 0) return(0);
1966 if (dest == NULL) return(-1);
1967 if (len <= 0) return(0);
1969 len = recv(ctxt->dataFd, dest, len, 0);
1972 __xmlIOErr(XML_FROM_FTP, 0, "recv failed");
1973 xmlNanoFTPCloseConnection(ctxt);
1976 xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len);
1983 * @URL: the URL to the resource
1985 * Start to fetch the given ftp:// resource
1987 * Returns an FTP context, or NULL
1991 xmlNanoFTPOpen(const char *URL) {
1992 xmlNanoFTPCtxtPtr ctxt;
1996 if (URL == NULL) return(NULL);
1997 if (strncmp("ftp://", URL, 6)) return(NULL);
1999 ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL);
2000 if (ctxt == NULL) return(NULL);
2001 if (xmlNanoFTPConnect(ctxt) < 0) {
2002 xmlNanoFTPFreeCtxt(ctxt);
2005 sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
2007 xmlNanoFTPFreeCtxt(ctxt);
2015 * @ctx: an FTP context
2017 * Close the connection and both control and transport
2019 * Returns -1 incase of error, 0 otherwise
2023 xmlNanoFTPClose(void *ctx) {
2024 xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
2029 if (ctxt->dataFd >= 0) {
2030 closesocket(ctxt->dataFd);
2033 if (ctxt->controlFd >= 0) {
2034 xmlNanoFTPQuit(ctxt);
2035 closesocket(ctxt->controlFd);
2036 ctxt->controlFd = -1;
2038 xmlNanoFTPFreeCtxt(ctxt);
2043 /************************************************************************
2045 * Basic test in Standalone mode *
2047 ************************************************************************/
2049 void ftpList(void *userData, const char *filename, const char* attrib,
2050 const char *owner, const char *group, unsigned long size, int links,
2051 int year, const char *month, int day, int hour, int minute) {
2052 xmlGenericError(xmlGenericErrorContext,
2053 "%s %s %s %ld %s\n", attrib, owner, group, size, filename);
2056 void ftpData(void *userData, const char *data, int len) {
2057 if (userData == NULL) return;
2059 fclose((FILE*)userData);
2062 fwrite(data, len, 1, (FILE*)userData);
2065 int main(int argc, char **argv) {
2068 char *tstfile = NULL;
2072 ctxt = xmlNanoFTPNewCtxt(argv[1]);
2073 if (xmlNanoFTPConnect(ctxt) < 0) {
2074 xmlGenericError(xmlGenericErrorContext,
2075 "Couldn't connect to %s\n", argv[1]);
2081 ctxt = xmlNanoFTPConnectTo("localhost", 0);
2083 xmlGenericError(xmlGenericErrorContext,
2084 "Couldn't connect to localhost\n");
2087 xmlNanoFTPList(ctxt, ftpList, NULL, tstfile);
2088 output = fopen("/tmp/tstdata", "w");
2089 if (output != NULL) {
2090 if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
2091 xmlGenericError(xmlGenericErrorContext,
2092 "Failed to get file\n");
2095 xmlNanoFTPClose(ctxt);
2099 #endif /* STANDALONE */
2100 #else /* !LIBXML_FTP_ENABLED */
2103 int main(int argc, char **argv) {
2104 xmlGenericError(xmlGenericErrorContext,
2105 "%s : FTP support not compiled in\n", argv[0]);
2108 #endif /* STANDALONE */
2109 #endif /* LIBXML_FTP_ENABLED */
2110 #define bottom_nanoftp
2111 #include "elfgcchack.h"