]> granicus.if.org Git - curl/commitdiff
portabilty: use proper variable type to hold sockets
authorDaniel Stenberg <daniel@haxx.se>
Sun, 5 Sep 2010 22:02:54 +0000 (00:02 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Sun, 5 Sep 2010 22:02:54 +0000 (00:02 +0200)
Curl_getconnectinfo() is changed to return a proper curl_socket_t for
the last socket so that it'll work more portably (and cause less
compiler warnings).

lib/connect.c
lib/connect.h
lib/easy.c
lib/getinfo.c
lib/url.c

index 771129b5a2ad7c8b7e9b2aab9ca72e2dc213dbd1..172ccbbbfbea09711af24e696f5a26e4df9e1440 100644 (file)
@@ -1093,12 +1093,12 @@ CURLcode Curl_connecthost(struct connectdata *conn,  /* context */
  * Used to extract socket and connectdata struct for the most recent
  * transfer on the given SessionHandle.
  *
- * The socket 'long' will be -1 in case of failure!
+ * The returned socket will be CURL_SOCKET_BAD in case of failure!
  */
-CURLcode Curl_getconnectinfo(struct SessionHandle *data,
-                             long *param_longp,
-                             struct connectdata **connp)
+curl_socket_t Curl_getconnectinfo(struct SessionHandle *data,
+                                  struct connectdata **connp)
 {
+  curl_socket_t sockfd;
   if((data->state.lastconnect != -1) &&
      (data->state.connc->connects[data->state.lastconnect] != NULL)) {
     struct connectdata *c =
@@ -1106,13 +1106,13 @@ CURLcode Curl_getconnectinfo(struct SessionHandle *data,
     if(connp)
       /* only store this if the caller cares for it */
       *connp = c;
-    *param_longp = c->sock[FIRSTSOCKET];
+    sockfd = c->sock[FIRSTSOCKET];
     /* we have a socket connected, let's determine if the server shut down */
     /* determine if ssl */
     if(c->ssl[FIRSTSOCKET].use) {
       /* use the SSL context */
       if(!Curl_ssl_check_cxn(c))
-        *param_longp = -1;   /* FIN received */
+        return CURL_SOCKET_BAD;   /* FIN received */
     }
 /* Minix 3.1 doesn't support any flags on recv; just assume socket is OK */
 #ifdef MSG_PEEK
@@ -1121,13 +1121,13 @@ CURLcode Curl_getconnectinfo(struct SessionHandle *data,
       char buf;
       if(recv((RECV_TYPE_ARG1)c->sock[FIRSTSOCKET], (RECV_TYPE_ARG2)&buf,
               (RECV_TYPE_ARG3)1, (RECV_TYPE_ARG4)MSG_PEEK) == 0) {
-        *param_longp = -1;   /* FIN received */
+        return CURL_SOCKET_BAD;   /* FIN received */
       }
     }
 #endif
   }
   else
-    *param_longp = -1;
+    return CURL_SOCKET_BAD;
 
-  return CURLE_OK;
+  return sockfd;
 }
index 57b1c2f2d1795c1305e5fc9aeb883a5965ac1c8e..2c6b10a386b61a41ae0a238aefe46a2531eb5e97 100644 (file)
@@ -47,11 +47,10 @@ long Curl_timeleft(struct connectdata *conn,
  * Used to extract socket and connectdata struct for the most recent
  * transfer on the given SessionHandle.
  *
- * The socket 'long' will be -1 in case of failure!
+ * The returned socket will be CURL_SOCKET_BAD in case of failure!
  */
-CURLcode Curl_getconnectinfo(struct SessionHandle *data,
-                             long *param_longp,
-                             struct connectdata **connp);
+curl_socket_t Curl_getconnectinfo(struct SessionHandle *data,
+                                  struct connectdata **connp);
 
 #ifdef WIN32
 /* When you run a program that uses the Windows Sockets API, you may
index 637d99bb06463bf9e3a7b358fb6d4f743200d153..bebeeb17f95154c743d313923c2852d3b34a2926 100644 (file)
@@ -1072,9 +1072,6 @@ static CURLcode easy_connection(struct SessionHandle *data,
                                 curl_socket_t *sfd,
                                 struct connectdata **connp)
 {
-  CURLcode ret;
-  long sockfd;
-
   if(data == NULL)
     return CURLE_BAD_FUNCTION_ARGUMENT;
 
@@ -1084,18 +1081,13 @@ static CURLcode easy_connection(struct SessionHandle *data,
     return CURLE_UNSUPPORTED_PROTOCOL;
   }
 
-  ret = Curl_getconnectinfo(data, &sockfd, connp);
-  if(ret != CURLE_OK)
-    return ret;
+  *sfd = Curl_getconnectinfo(data, connp);
 
-  if(sockfd == -1) {
+  if(*sfd == CURL_SOCKET_BAD) {
     failf(data, "Failed to get recent socket");
     return CURLE_UNSUPPORTED_PROTOCOL;
   }
 
-  *sfd = (curl_socket_t)sockfd; /* we know that this is actually a socket
-                                   descriptor so the typecast is fine here */
-
   return CURLE_OK;
 }
 
index d5517e4899d919c97ca8d2241c2279ae499a00cc..c00e675ebb072b3bb397f46e65e00ebe2d43e5c7 100644 (file)
@@ -83,6 +83,7 @@ CURLcode Curl_getinfo(struct SessionHandle *data, CURLINFO info, ...)
   char **param_charp=NULL;
   struct curl_slist **param_slistp=NULL;
   int type;
+  curl_socket_t sockfd;
 
   union {
     struct curl_certinfo * to_certinfo;
@@ -219,7 +220,16 @@ CURLcode Curl_getinfo(struct SessionHandle *data, CURLINFO info, ...)
     *param_charp = data->state.most_recent_ftp_entrypath;
     break;
   case CURLINFO_LASTSOCKET:
-    (void)Curl_getconnectinfo(data, param_longp, NULL);
+    sockfd = Curl_getconnectinfo(data, NULL);
+
+    /* note: this is not a good conversion for systems with 64 bit sockets and
+       32 bit longs */
+    if(sockfd != CURL_SOCKET_BAD)
+      *param_longp = (long)sockfd;
+    else
+      /* this interface is documented to return -1 in case of badness, which
+         may not be the same as the CURL_SOCKET_BAD value */
+      *param_longp = -1;
     break;
   case CURLINFO_REDIRECT_URL:
     /* Return the URL this request would have been redirected to if that
index 8abf6e9bc59e94e83e087c7c64adefe431a8924a..1334186815f66c150c7553d6a0a087b630ff034c 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -2698,11 +2698,10 @@ static bool RTSPConnIsDead(struct connectdata *check)
   }
   else if (sval & CURL_CSELECT_IN) {
     /* readable with no error. could be closed or could be alive */
-    long connectinfo = 0;
-    Curl_getconnectinfo(check->data, &connectinfo, &check);
-    if(connectinfo != -1) {
+    curl_socket_t connectinfo =
+      Curl_getconnectinfo(check->data, &check);
+    if(connectinfo != CURL_SOCKET_BAD)
       ret_val = FALSE;
-    }
   }
 
   return ret_val;