Fix tcpip_set_blocking for Windows. Set comstack cerrno to CSYSERR in
[yaz-moved-to-github.git] / src / tcpip.c
index 9a7458c..5eb4ea4 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: tcpip.c,v 1.25 2006-09-01 10:39:09 adam Exp $
+ * $Id: tcpip.c,v 1.27 2006-09-01 12:42:31 adam Exp $
  */
 /**
  * \file tcpip.c
@@ -243,7 +243,7 @@ struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
     host[sizeof(host)-1] = 0;
     if ((p = strchr(host, '/')))
         *p = 0;
-    if ((p = strchr(host, ':')))
+    if ((p = strrchr(host, ':')))
     {
         *p = '\0';
         port = p+1;
@@ -282,7 +282,7 @@ int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
     buf[sizeof(buf)-1] = 0;
     if ((p = strchr(buf, '/')))
         *p = 0;
-    if ((p = strchr(buf, ':')))
+    if ((p = strrchr(buf, ':')))
     {
         *p = 0;
         port = atoi(p + 1);
@@ -874,7 +874,7 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize)
         TRC(fprintf(stderr, "  recv res=%d, hasread=%d\n", res, hasread));
         if (res < 0)
         {
-          TRC(fprintf(stderr, "  recv errno=%d, (%s)\n", yaz_errno(), 
+            TRC(fprintf(stderr, "  recv errno=%d, (%s)\n", yaz_errno(), 
                       strerror(yaz_errno())));
 #ifdef WIN32
             if (WSAGetLastError() == WSAEWOULDBLOCK)
@@ -883,7 +883,10 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize)
                 break;
             }
             else
+            {
+                h->cerrno = CSYSERR;
                 return -1;
+            }
 #else
             if (yaz_errno() == EWOULDBLOCK 
 #ifdef EAGAIN   
@@ -903,7 +906,10 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize)
             else if (yaz_errno() == 0)
                 continue;
             else
+            {
+                h->cerrno = CSYSERR;
                 return -1;
+            }
 #endif
         }
         else if (!res)
@@ -927,10 +933,16 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize)
         if (!sp->altbuf)
         {
             if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
+            {
+                h->cerrno = CSYSERR;
                 return -1;
+            }
         } else if (sp->altsize < req)
             if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
+            {
+                h->cerrno = CSYSERR;
                 return -1;
+            }
         TRC(fprintf(stderr, "  Moving %d bytes to altbuf(0x%x)\n", tomove,
             (unsigned) sp->altbuf));
         memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
@@ -1231,7 +1243,7 @@ int static tcpip_set_blocking(COMSTACK p, int blocking)
     unsigned long flag;
     
 #ifdef WIN32
-    flag = 1;
+       flag = blocking ? 0 : 1;
     if (ioctlsocket(p->iofile, FIONBIO, &flag) < 0)
         return 0;
 #else