Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / gnulib-lib / libxml / nanoftp.c
index c78503b..2135ab9 100644 (file)
@@ -41,7 +41,7 @@
 #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
@@ -185,7 +188,7 @@ void
 xmlNanoFTPInit(void) {
     const char *env;
 #ifdef _WINSOCKAPI_
-    WSADATA wsaData;    
+    WSADATA wsaData;
 #endif
 
     if (initialized)
@@ -302,15 +305,15 @@ xmlNanoFTPScanURL(void *ctx, const char *URL) {
     /*
      * 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;
     }
@@ -324,7 +327,7 @@ xmlNanoFTPScanURL(void *ctx, const char *URL) {
        xmlFreeURI(uri);
        return;
     }
-    
+
     ctxt->protocol = xmlMemStrdup(uri->scheme);
     ctxt->hostname = xmlMemStrdup(uri->server);
     if (uri->path != NULL)
@@ -355,7 +358,7 @@ xmlNanoFTPScanURL(void *ctx, const char *URL) {
  * @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.
@@ -400,7 +403,7 @@ xmlNanoFTPUpdateURL(void *ctx, const char *URL) {
        ctxt->path = NULL;
     }
 
-    if (uri->path == NULL) 
+    if (uri->path == NULL)
         ctxt->path = xmlMemStrdup("/");
     else
        ctxt->path = xmlMemStrdup(uri->path);
@@ -424,7 +427,7 @@ void
 xmlNanoFTPScanProxy(const char *URL) {
     xmlURIPtr uri;
 
-    if (proxy != NULL) { 
+    if (proxy != NULL) {
         xmlFree(proxy);
        proxy = NULL;
     }
@@ -448,7 +451,7 @@ xmlNanoFTPScanProxy(const char *URL) {
            xmlFreeURI(uri);
        return;
     }
-    
+
     proxy = xmlMemStrdup(uri->server);
     if (uri->port != 0)
        proxyPort = uri->port;
@@ -482,7 +485,7 @@ xmlNanoFTPNewCtxt(const char *URL) {
     ret->returnValue = 0;
     ret->controlBufIndex = 0;
     ret->controlBufUsed = 0;
-    ret->controlFd = -1;
+    ret->controlFd = INVALID_SOCKET;
 
     unescaped = xmlURIUnescapeString(URL, 0, NULL);
     if (unescaped != NULL) {
@@ -509,8 +512,8 @@ xmlNanoFTPFreeCtxt(void * ctx) {
     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);
@@ -520,7 +523,7 @@ xmlNanoFTPFreeCtxt(void * ctx) {
  * 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
@@ -532,22 +535,22 @@ xmlNanoFTPParseResponse(char *buf, int len) {
     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);
 }
@@ -565,7 +568,7 @@ xmlNanoFTPGetMore(void *ctx) {
     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
@@ -617,8 +620,8 @@ xmlNanoFTPGetMore(void *ctx) {
     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
@@ -646,7 +649,7 @@ xmlNanoFTPReadResponse(void *ctx) {
     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:
     /*
@@ -735,7 +738,7 @@ xmlNanoFTPCheckResponse(void *ctx) {
     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);
@@ -746,7 +749,7 @@ xmlNanoFTPCheckResponse(void *ctx) {
        case -1:
            __xmlIOErr(XML_FROM_FTP, 0, "select");
            return(-1);
-                       
+
     }
 
     return(xmlNanoFTPReadResponse(ctx));
@@ -772,7 +775,7 @@ xmlNanoFTPSendUser(void *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);
@@ -800,7 +803,7 @@ xmlNanoFTPSendPasswd(void *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);
@@ -824,14 +827,14 @@ xmlNanoFTPQuit(void *ctx) {
     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);
@@ -905,6 +908,8 @@ xmlNanoFTPConnect(void *ctx) {
            return (-1);
        }
        if (tmp->ai_addrlen > sizeof(ctxt->ftpAddr)) {
+           if (result)
+               freeaddrinfo (result);
            __xmlIOErr(XML_FROM_FTP, 0, "gethostbyname address mismatch");
            return (-1);
        }
@@ -925,9 +930,9 @@ xmlNanoFTPConnect(void *ctx) {
 #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);
@@ -944,12 +949,13 @@ xmlNanoFTPConnect(void *ctx) {
        ((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);
     }
@@ -960,8 +966,8 @@ xmlNanoFTPConnect(void *ctx) {
     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);
     }
 
@@ -970,8 +976,8 @@ xmlNanoFTPConnect(void *ctx) {
      */
     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);
     }
 
@@ -1024,11 +1030,11 @@ xmlNanoFTPConnect(void *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");
                closesocket(ctxt->controlFd);
-               ctxt->controlFd = -1;
+               ctxt->controlFd = INVALID_SOCKET;
                return(res);
            }
            res = xmlNanoFTPGetResponse(ctxt);
@@ -1046,17 +1052,17 @@ xmlNanoFTPConnect(void *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");
                        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;
@@ -1067,7 +1073,7 @@ xmlNanoFTPConnect(void *ctx) {
                case -1:
                default:
                    closesocket(ctxt->controlFd);
-                   ctxt->controlFd = -1;
+                   ctxt->controlFd = INVALID_SOCKET;
                    return(-1);
            }
        }
@@ -1087,11 +1093,11 @@ xmlNanoFTPConnect(void *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");
-                   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);
@@ -1099,10 +1105,10 @@ xmlNanoFTPConnect(void *ctx) {
                    /* 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:
@@ -1118,11 +1124,11 @@ xmlNanoFTPConnect(void *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");
-                   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);
@@ -1130,7 +1136,7 @@ xmlNanoFTPConnect(void *ctx) {
                    /* we assume it worked :-\ */
                    proxyType = 2;
                    return(0);
-               }    
+               }
                if (ctxt->passwd == NULL)
                    snprintf(buf, sizeof(buf), "PASS anonymous@\r\n");
                else
@@ -1140,11 +1146,11 @@ xmlNanoFTPConnect(void *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");
-                   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);
@@ -1154,8 +1160,8 @@ xmlNanoFTPConnect(void *ctx) {
                    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:
@@ -1164,8 +1170,8 @@ xmlNanoFTPConnect(void *ctx) {
                 * 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);
        }
     }
@@ -1174,8 +1180,8 @@ xmlNanoFTPConnect(void *ctx) {
      */
     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);
@@ -1189,14 +1195,14 @@ xmlNanoFTPConnect(void *ctx) {
        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);
@@ -1211,8 +1217,8 @@ xmlNanoFTPConnect(void *ctx) {
        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);
     }
 
@@ -1235,12 +1241,18 @@ xmlNanoFTPConnectTo(const char *server, int port) {
     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);
@@ -1268,7 +1280,7 @@ xmlNanoFTPCwd(void *ctx, const char *directory) {
     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;
 
     /*
@@ -1284,7 +1296,7 @@ xmlNanoFTPCwd(void *ctx, const char *directory) {
 #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);
@@ -1317,8 +1329,8 @@ xmlNanoFTPDele(void *ctx, const char *file) {
     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:
@@ -1328,14 +1340,14 @@ xmlNanoFTPDele(void *ctx, const char *file) {
      *       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);
@@ -1360,7 +1372,7 @@ xmlNanoFTPDele(void *ctx, const char *file) {
  * Returns -1 incase of error, 0 otherwise
  */
 
-int
+SOCKET
 xmlNanoFTPGetConnection(void *ctx) {
     xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx;
     char buf[200], *cur;
@@ -1375,7 +1387,7 @@ xmlNanoFTPGetConnection(void *ctx) {
 #endif
     XML_SOCKLEN_T dataAddrLen;
 
-    if (ctxt == NULL) return(-1);
+    if (ctxt == NULL) return INVALID_SOCKET;
 
     memset (&dataAddr, 0, sizeof(dataAddr));
 #ifdef SUPPORT_IP6
@@ -1391,9 +1403,9 @@ xmlNanoFTPGetConnection(void *ctx) {
        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) {
@@ -1407,36 +1419,36 @@ xmlNanoFTPGetConnection(void *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");
-           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]);
@@ -1448,10 +1460,10 @@ xmlNanoFTPGetConnection(void *ctx) {
                &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);
@@ -1460,8 +1472,8 @@ xmlNanoFTPGetConnection(void *ctx) {
 
        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);
@@ -1474,15 +1486,15 @@ xmlNanoFTPGetConnection(void *ctx) {
 
        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) {
@@ -1508,20 +1520,20 @@ xmlNanoFTPGetConnection(void *ctx) {
        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);
-    
+
 }
 
 /**
@@ -1540,9 +1552,9 @@ xmlNanoFTPCloseConnection(void *ctx) {
     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);
@@ -1554,7 +1566,7 @@ xmlNanoFTPCloseConnection(void *ctx) {
 #ifdef DEBUG_FTP
        perror("select");
 #endif
-       closesocket(ctxt->controlFd); ctxt->controlFd = -1;
+       closesocket(ctxt->controlFd); ctxt->controlFd = INVALID_SOCKET;
        return(-1);
     }
     if (res == 0) {
@@ -1562,11 +1574,11 @@ xmlNanoFTPCloseConnection(void *ctx) {
        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);
        }
     }
@@ -1579,7 +1591,7 @@ xmlNanoFTPCloseConnection(void *ctx) {
  * @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
  */
@@ -1616,7 +1628,7 @@ xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData)
        if (*cur == 0) return(0);
        i = 0;
        while (*cur != ' ') {
-           if (i < 10) 
+           if (i < 10)
                attrib[i++] = *cur;
            cur++;
            if (*cur == 0) return(0);
@@ -1630,7 +1642,7 @@ xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData)
        if (*cur == 0) return(0);
        i = 0;
        while (*cur != ' ') {
-           if (i < 10) 
+           if (i < 10)
                owner[i++] = *cur;
            cur++;
            if (*cur == 0) return(0);
@@ -1640,7 +1652,7 @@ xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData)
        if (*cur == 0) return(0);
        i = 0;
        while (*cur != ' ') {
-           if (i < 10) 
+           if (i < 10)
                group[i++] = *cur;
            cur++;
            if (*cur == 0) return(0);
@@ -1727,7 +1739,7 @@ xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
         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 {
@@ -1736,7 +1748,7 @@ xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
                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);
     }
@@ -1745,15 +1757,15 @@ xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
 #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);
     }
 
@@ -1769,18 +1781,18 @@ xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
 #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);
            }
 
@@ -1789,8 +1801,8 @@ xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
 
        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
@@ -1822,34 +1834,34 @@ xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData,
  */
 
 
-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);
@@ -1860,16 +1872,16 @@ xmlNanoFTPGetSocket(void *ctx, const char *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;
-       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);
 }
@@ -1901,7 +1913,7 @@ xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
        return(-1);
     if (callback == NULL)
        return(-1);
-    if (xmlNanoFTPGetSocket(ctxt, filename) < 0)
+    if (xmlNanoFTPGetSocket(ctxt, filename) == INVALID_SOCKET)
        return(-1);
 
     do {
@@ -1914,18 +1926,18 @@ xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
 #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);
            }
 
@@ -1934,7 +1946,7 @@ xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData,
        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);
@@ -1960,7 +1972,7 @@ xmlNanoFTPRead(void *ctx, void *dest, int 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);
 
@@ -1982,13 +1994,13 @@ xmlNanoFTPRead(void *ctx, void *dest, int len) {
  *
  * 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);
@@ -2001,7 +2013,7 @@ xmlNanoFTPOpen(const char *URL) {
        return(NULL);
     }
     sock = xmlNanoFTPGetSocket(ctxt, ctxt->path);
-    if (sock < 0) {
+    if (sock == INVALID_SOCKET) {
        xmlNanoFTPFreeCtxt(ctxt);
        return(NULL);
     }
@@ -2024,14 +2036,14 @@ xmlNanoFTPClose(void *ctx) {
     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);
@@ -2039,9 +2051,9 @@ xmlNanoFTPClose(void *ctx) {
 
 #ifdef STANDALONE
 /************************************************************************
- *                                                                     *
- *                     Basic test in Standalone mode                   *
- *                                                                     *
+ *                                                                     *
+ *                     Basic test in Standalone mode                   *
+ *                                                                     *
  ************************************************************************/
 static
 void ftpList(void *userData, const char *filename, const char* attrib,
@@ -2056,7 +2068,7 @@ void ftpData(void *userData, const char *data, int len) {
     if (len <= 0) {
        fclose((FILE*)userData);
        return;
-    }  
+    }
     fwrite(data, len, 1, (FILE*)userData);
 }
 
@@ -2088,7 +2100,7 @@ int main(int argc, char **argv) {
        if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0)
            xmlGenericError(xmlGenericErrorContext,
                    "Failed to get file\n");
-       
+
     }
     xmlNanoFTPClose(ctxt);
     xmlMemoryDump();