#include <netdb.h>
#endif
#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
+#include <fcntl.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
-#ifdef __MINGW32__
+#if defined(__MINGW32__) || defined(_WIN32_WCE)
+#ifndef _WINSOCKAPI_
#define _WINSOCKAPI_
+#endif
#include <wsockcompat.h>
#include <winsock2.h>
#undef XML_SOCKLEN_T
* A couple portability macros
*/
#ifndef _WINSOCKAPI_
-#ifndef __BEOS__
+#if !defined(__BEOS__) || defined(__HAIKU__)
#define closesocket(s) close(s)
#endif
-#define SOCKET int
#endif
#ifdef __BEOS__
#endif
#ifdef _AIX
+#ifdef HAVE_BROKEN_SS_FAMILY
#define ss_family __ss_family
#endif
+#endif
#ifndef XML_SOCKLEN_T
#define XML_SOCKLEN_T unsigned int
xmlNanoFTPInit(void) {
const char *env;
#ifdef _WINSOCKAPI_
- WSADATA wsaData;
+ WSADATA wsaData;
#endif
if (initialized)
/*
* Clear any existing data from the context
*/
- if (ctxt->protocol != NULL) {
+ if (ctxt->protocol != NULL) {
xmlFree(ctxt->protocol);
ctxt->protocol = NULL;
}
- if (ctxt->hostname != NULL) {
+ if (ctxt->hostname != NULL) {
xmlFree(ctxt->hostname);
ctxt->hostname = NULL;
}
- if (ctxt->path != NULL) {
+ if (ctxt->path != NULL) {
xmlFree(ctxt->path);
ctxt->path = NULL;
}
xmlFreeURI(uri);
return;
}
-
+
ctxt->protocol = xmlMemStrdup(uri->scheme);
ctxt->hostname = xmlMemStrdup(uri->server);
if (uri->path != NULL)
* @URL: The URL used to update the context
*
* Update an FTP context by parsing the URL and finding
- * new path it indicates. If there is an error in the
+ * new path it indicates. If there is an error in the
* protocol, hostname, port or other information, the
* error is raised. It indicates a new connection has to
* be established.
ctxt->path = NULL;
}
- if (uri->path == NULL)
+ if (uri->path == NULL)
ctxt->path = xmlMemStrdup("/");
else
ctxt->path = xmlMemStrdup(uri->path);
xmlNanoFTPScanProxy(const char *URL) {
xmlURIPtr uri;
- if (proxy != NULL) {
+ if (proxy != NULL) {
xmlFree(proxy);
proxy = NULL;
}
xmlFreeURI(uri);
return;
}
-
+
proxy = xmlMemStrdup(uri->server);
if (uri->port != 0)
proxyPort = uri->port;
ret->returnValue = 0;
ret->controlBufIndex = 0;
ret->controlBufUsed = 0;
- ret->controlFd = -1;
+ ret->controlFd = INVALID_SOCKET;
unescaped = xmlURIUnescapeString(URL, 0, NULL);
if (unescaped != NULL) {
if (ctxt->protocol != NULL) xmlFree(ctxt->protocol);
if (ctxt->path != NULL) xmlFree(ctxt->path);
ctxt->passive = 1;
- if (ctxt->controlFd >= 0) closesocket(ctxt->controlFd);
- ctxt->controlFd = -1;
+ if (ctxt->controlFd != INVALID_SOCKET) closesocket(ctxt->controlFd);
+ ctxt->controlFd = INVALID_SOCKET;
ctxt->controlBufIndex = -1;
ctxt->controlBufUsed = -1;
xmlFree(ctxt);
* xmlNanoFTPParseResponse:
* @buf: the buffer containing the response
* @len: the buffer length
- *
+ *
* Parsing of the server answer, we just extract the code.
*
* returns 0 for errors
int val = 0;
if (len < 3) return(-1);
- if ((*buf >= '0') && (*buf <= '9'))
+ if ((*buf >= '0') && (*buf <= '9'))
val = val * 10 + (*buf - '0');
else
return(0);
buf++;
- if ((*buf >= '0') && (*buf <= '9'))
+ if ((*buf >= '0') && (*buf <= '9'))
val = val * 10 + (*buf - '0');
else
return(0);
buf++;
- if ((*buf >= '0') && (*buf <= '9'))
+ if ((*buf >= '0') && (*buf <= '9'))
val = val * 10 + (*buf - '0');
else
return(0);
buf++;
- if (*buf == '-')
+ if (*buf == '-')
return(-val);
return(val);
}
int len;
int size;
- if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
+ if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) {
#ifdef DEBUG_FTP
if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex],
size, 0)) < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "recv failed");
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
#ifdef DEBUG_FTP
int len;
int res = -1, cur = -1;
- if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
+ if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
get_more:
/*
fd_set rfd;
struct timeval tv;
- if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
+ if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rfd);
case -1:
__xmlIOErr(XML_FROM_FTP, 0, "select");
return(-1);
-
+
}
return(xmlNanoFTPReadResponse(ctx));
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
return(res);
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
return(res);
char buf[200];
int len, res;
- if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
+ if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
snprintf(buf, sizeof(buf), "QUIT\r\n");
len = strlen(buf);
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf); /* Just to be consistent, even though we know it can't have a % in it */
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
return(res);
return (-1);
}
if (tmp->ai_addrlen > sizeof(ctxt->ftpAddr)) {
+ if (result)
+ freeaddrinfo (result);
__xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
return (-1);
}
#endif
{
if (proxy)
- hp = gethostbyname (proxy);
+ hp = gethostbyname (GETHOSTBYNAME_ARG_CAST proxy);
else
- hp = gethostbyname (ctxt->hostname);
+ hp = gethostbyname (GETHOSTBYNAME_ARG_CAST ctxt->hostname);
if (hp == NULL) {
__xmlIOErr(XML_FROM_FTP, 0, "gethostbyname failed");
return (-1);
((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET;
memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr,
hp->h_addr_list[0], hp->h_length);
- ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port = (u_short)htons ((unsigned short)port);
+ ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port =
+ (unsigned short)htons ((unsigned short)port);
ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0);
addrlen = sizeof (struct sockaddr_in);
}
- if (ctxt->controlFd < 0) {
+ if (ctxt->controlFd == INVALID_SOCKET) {
__xmlIOErr(XML_FROM_FTP, 0, "socket failed");
return(-1);
}
if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr,
addrlen) < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "Failed to create a connection");
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
*/
res = xmlNanoFTPGetResponse(ctxt);
if (res != 2) {
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
closesocket(ctxt->controlFd);
- ctxt->controlFd = -1;
+ ctxt->controlFd = INVALID_SOCKET;
return(res);
}
res = xmlNanoFTPGetResponse(ctxt);
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
closesocket(ctxt->controlFd);
- ctxt->controlFd = -1;
+ ctxt->controlFd = INVALID_SOCKET;
return(res);
}
res = xmlNanoFTPGetResponse(ctxt);
if (res > 3) {
closesocket(ctxt->controlFd);
- ctxt->controlFd = -1;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
break;
case -1:
default:
closesocket(ctxt->controlFd);
- ctxt->controlFd = -1;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
}
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(res);
}
res = xmlNanoFTPGetResponse(ctxt);
/* we assume it worked :-\ 1 is error for SITE command */
proxyType = 1;
break;
- }
+ }
if (proxyType == 1) {
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
case 2:
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(res);
}
res = xmlNanoFTPGetResponse(ctxt);
/* we assume it worked :-\ */
proxyType = 2;
return(0);
- }
+ }
if (ctxt->passwd == NULL)
snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
else
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(res);
}
res = xmlNanoFTPGetResponse(ctxt);
return(0);
}
if (proxyType == 2) {
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
case 3:
* send the code or at least the sequence in use.
*/
default:
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
}
*/
res = xmlNanoFTPSendUser(ctxt);
if (res < 0) {
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
res = xmlNanoFTPGetResponse(ctxt);
case 5:
case -1:
default:
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
res = xmlNanoFTPSendPasswd(ctxt);
if (res < 0) {
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
res = xmlNanoFTPGetResponse(ctxt);
case 5:
case -1:
default:
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
- ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
+ ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
int res;
xmlNanoFTPInit();
- if (server == NULL)
+ if (server == NULL)
return(NULL);
if (port <= 0)
return(NULL);
ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL);
+ if (ctxt == NULL)
+ return(NULL);
ctxt->hostname = xmlMemStrdup(server);
+ if (ctxt->hostname == NULL) {
+ xmlNanoFTPFreeCtxt(ctxt);
+ return(NULL);
+ }
if (port != 0)
ctxt->port = port;
res = xmlNanoFTPConnect(ctxt);
int len;
int res;
- if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
+ if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
if (directory == NULL) return 0;
/*
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
return(res);
int len;
int res;
- if ((ctxt == NULL) || (ctxt->controlFd < 0) || (file == NULL)) return(-1);
- if (file == NULL) return (0);
+ if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET) ||
+ (file == NULL)) return(-1);
/*
* Expected response code for DELE:
* 450, 550
* 500, 501, 502, 421, 530
*/
-
+
snprintf(buf, sizeof(buf), "DELE %s\r\n", file);
buf[sizeof(buf) - 1] = 0;
len = strlen(buf);
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
return(res);
* Returns -1 incase of error, 0 otherwise
*/
-int
+SOCKET
xmlNanoFTPGetConnection(void *ctx) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
char buf[200], *cur;
#endif
XML_SOCKLEN_T dataAddrLen;
- if (ctxt == NULL) return(-1);
+ if (ctxt == NULL) return INVALID_SOCKET;
memset (&dataAddr, 0, sizeof(dataAddr));
#ifdef SUPPORT_IP6
dataAddrLen = sizeof (struct sockaddr_in);
}
- if (ctxt->dataFd < 0) {
+ if (ctxt->dataFd == INVALID_SOCKET) {
__xmlIOErr(XML_FROM_FTP, 0, "socket failed");
- return (-1);
+ return INVALID_SOCKET;
}
if (ctxt->passive) {
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return(res);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
res = xmlNanoFTPReadResponse(ctx);
if (res != 2) {
if (res == 5) {
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return(-1);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
} else {
/*
* retry with an active connection
*/
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
ctxt->passive = 0;
}
}
- cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
+ cur = &ctxt->controlBuf[ctxt->controlBufAnswer];
while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++;
#ifdef SUPPORT_IP6
if ((ctxt->ftpAddr).ss_family == AF_INET6) {
if (sscanf (cur, "%u", &temp[0]) != 1) {
__xmlIOErr(XML_FROM_FTP, XML_FTP_EPSV_ANSWER,
"Invalid answer to EPSV\n");
- if (ctxt->dataFd != -1) {
- closesocket (ctxt->dataFd); ctxt->dataFd = -1;
+ if (ctxt->dataFd != INVALID_SOCKET) {
+ closesocket (ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
}
- return (-1);
+ return INVALID_SOCKET;
}
memcpy (&((struct sockaddr_in6 *)&dataAddr)->sin6_addr, &((struct sockaddr_in6 *)&ctxt->ftpAddr)->sin6_addr, sizeof(struct in6_addr));
((struct sockaddr_in6 *)&dataAddr)->sin6_port = htons (temp[0]);
&temp[3], &temp[4], &temp[5]) != 6) {
__xmlIOErr(XML_FROM_FTP, XML_FTP_PASV_ANSWER,
"Invalid answer to PASV\n");
- if (ctxt->dataFd != -1) {
- closesocket (ctxt->dataFd); ctxt->dataFd = -1;
+ if (ctxt->dataFd != INVALID_SOCKET) {
+ closesocket (ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
}
- return (-1);
+ return INVALID_SOCKET;
}
for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff);
memcpy (&((struct sockaddr_in *)&dataAddr)->sin_addr, &ad[0], 4);
if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "Failed to create a data connection");
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return (-1);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
} else {
getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "bind failed");
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return (-1);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen);
if (listen(ctxt->dataFd, 1) < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "listen failed");
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return (-1);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
#ifdef SUPPORT_IP6
if ((ctxt->ftpAddr).ss_family == AF_INET6) {
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return(res);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
res = xmlNanoFTPGetResponse(ctxt);
if (res != 2) {
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return(-1);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
}
return(ctxt->dataFd);
-
+
}
/**
fd_set rfd, efd;
struct timeval tv;
- if ((ctxt == NULL) || (ctxt->controlFd < 0)) return(-1);
+ if ((ctxt == NULL) || (ctxt->controlFd == INVALID_SOCKET)) return(-1);
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
tv.tv_sec = 15;
tv.tv_usec = 0;
FD_ZERO(&rfd);
#ifdef DEBUG_FTP
perror("select");
#endif
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
if (res == 0) {
xmlGenericError(xmlGenericErrorContext,
"xmlNanoFTPCloseConnection: timeout\n");
#endif
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
} else {
res = xmlNanoFTPGetResponse(ctxt);
if (res != 2) {
- closesocket(ctxt->controlFd); ctxt->controlFd = -1;
+ closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
return(-1);
}
}
* @callback: the user callback
* @userData: the user callback data
*
- * Parse at most one entry from the listing.
+ * Parse at most one entry from the listing.
*
* Returns -1 incase of error, the length of data parsed otherwise
*/
if (*cur == 0) return(0);
i = 0;
while (*cur != ' ') {
- if (i < 10)
+ if (i < 10)
attrib[i++] = *cur;
cur++;
if (*cur == 0) return(0);
if (*cur == 0) return(0);
i = 0;
while (*cur != ' ') {
- if (i < 10)
+ if (i < 10)
owner[i++] = *cur;
cur++;
if (*cur == 0) return(0);
if (*cur == 0) return(0);
i = 0;
while (*cur != ' ') {
- if (i < 10)
+ if (i < 10)
group[i++] = *cur;
cur++;
if (*cur == 0) return(0);
if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1)
return(-1);
ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
- if (ctxt->dataFd == -1)
+ if (ctxt->dataFd == INVALID_SOCKET)
return(-1);
snprintf(buf, sizeof(buf), "LIST -L\r\n");
} else {
return(-1);
}
ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
- if (ctxt->dataFd == -1)
+ if (ctxt->dataFd == INVALID_SOCKET)
return(-1);
snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename);
}
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
return(res);
}
res = xmlNanoFTPReadResponse(ctxt);
if (res != 1) {
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
return(-res);
}
#ifdef DEBUG_FTP
perror("select");
#endif
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
return(-1);
}
if (res == 0) {
res = xmlNanoFTPCheckResponse(ctxt);
if (res < 0) {
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ ctxt->dataFd = INVALID_SOCKET;
return(-1);
}
if (res == 2) {
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
return(0);
}
if ((len = recv(ctxt->dataFd, &buf[indx], sizeof(buf) - (indx + 1), 0)) < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "recv");
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ ctxt->dataFd = INVALID_SOCKET;
return(-1);
}
#ifdef DEBUG_FTP
*/
-int
+SOCKET
xmlNanoFTPGetSocket(void *ctx, const char *filename) {
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
char buf[300];
int res, len;
if (ctx == NULL)
- return(-1);
+ return INVALID_SOCKET;
if ((filename == NULL) && (ctxt->path == NULL))
- return(-1);
+ return INVALID_SOCKET;
ctxt->dataFd = xmlNanoFTPGetConnection(ctxt);
- if (ctxt->dataFd == -1)
- return(-1);
+ if (ctxt->dataFd == INVALID_SOCKET)
+ return INVALID_SOCKET;
snprintf(buf, sizeof(buf), "TYPE I\r\n");
len = strlen(buf);
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return(res);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
res = xmlNanoFTPReadResponse(ctxt);
if (res != 2) {
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return(-res);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
if (filename == NULL)
snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path);
#ifdef DEBUG_FTP
xmlGenericError(xmlGenericErrorContext, "%s", buf);
#endif
- res = send(ctxt->controlFd, buf, len, 0);
+ res = send(ctxt->controlFd, SEND_ARG2_CAST buf, len, 0);
if (res < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "send failed");
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return(res);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
res = xmlNanoFTPReadResponse(ctxt);
if (res != 1) {
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- return(-res);
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ return INVALID_SOCKET;
}
return(ctxt->dataFd);
}
return(-1);
if (callback == NULL)
return(-1);
- if (xmlNanoFTPGetSocket(ctxt, filename) < 0)
+ if (xmlNanoFTPGetSocket(ctxt, filename) == INVALID_SOCKET)
return(-1);
do {
#ifdef DEBUG_FTP
perror("select");
#endif
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
return(-1);
}
if (res == 0) {
res = xmlNanoFTPCheckResponse(ctxt);
if (res < 0) {
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
- ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
+ ctxt->dataFd = INVALID_SOCKET;
return(-1);
}
if (res == 2) {
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
return(0);
}
if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) {
__xmlIOErr(XML_FROM_FTP, 0, "recv failed");
callback(userData, buf, len);
- closesocket(ctxt->dataFd); ctxt->dataFd = -1;
+ closesocket(ctxt->dataFd); ctxt->dataFd = INVALID_SOCKET;
return(-1);
}
callback(userData, buf, len);
xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
if (ctx == NULL) return(-1);
- if (ctxt->dataFd < 0) return(0);
+ if (ctxt->dataFd == INVALID_SOCKET) return(0);
if (dest == NULL) return(-1);
if (len <= 0) return(0);
*
* Start to fetch the given ftp:// resource
*
- * Returns an FTP context, or NULL
+ * Returns an FTP context, or NULL
*/
void*
xmlNanoFTPOpen(const char *URL) {
xmlNanoFTPCtxtPtr ctxt;
- int sock;
+ SOCKET sock;
xmlNanoFTPInit();
if (URL == NULL) return(NULL);
return(NULL);
}
sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
- if (sock < 0) {
+ if (sock == INVALID_SOCKET) {
xmlNanoFTPFreeCtxt(ctxt);
return(NULL);
}
if (ctxt == NULL)
return(-1);
- if (ctxt->dataFd >= 0) {
+ if (ctxt->dataFd != INVALID_SOCKET) {
closesocket(ctxt->dataFd);
- ctxt->dataFd = -1;
+ ctxt->dataFd = INVALID_SOCKET;
}
- if (ctxt->controlFd >= 0) {
+ if (ctxt->controlFd != INVALID_SOCKET) {
xmlNanoFTPQuit(ctxt);
closesocket(ctxt->controlFd);
- ctxt->controlFd = -1;
+ ctxt->controlFd = INVALID_SOCKET;
}
xmlNanoFTPFreeCtxt(ctxt);
return(0);
#ifdef STANDALONE
/************************************************************************
- * *
- * Basic test in Standalone mode *
- * *
+ * *
+ * Basic test in Standalone mode *
+ * *
************************************************************************/
static
void ftpList(void *userData, const char *filename, const char* attrib,
if (len <= 0) {
fclose((FILE*)userData);
return;
- }
+ }
fwrite(data, len, 1, (FILE*)userData);
}
if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
xmlGenericError(xmlGenericErrorContext,
"Failed to get file\n");
-
+
}
xmlNanoFTPClose(ctxt);
xmlMemoryDump();