Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make MSG_TASK_CANCELLED deprecated, replace it by MSG_TASK_CANCELED
[simgrid.git] / src / gras / Transport / transport_plugin_tcp.c
index 90c4715..b74bf90 100644 (file)
@@ -223,6 +223,8 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock)
 
   uint32_t hisport;
 
+  int failed=0;
+
   XBT_IN("");
   gras_trp_socket_new(1, &res);
 
@@ -236,9 +238,14 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock)
            sock_errstr(tmp_errno));
   }
 
-  if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &i, s)
-      || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *) &i,
-                    s))
+  if (_gras_tcp_proto_number()!=-1)
+    if (setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *) &i,s))
+      failed=1;
+
+  if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &i, s))
+    failed=1;
+
+  if (failed)
     THROWF(system_error, 0,
            "setsockopt failed, cannot condition the socket: %s",
            sock_errstr(tmp_errno));
@@ -296,8 +303,7 @@ static void gras_trp_sock_socket_close(gras_socket_t sock)
   if (!sock)
     return;                     /* close only once */
 
-  if (((gras_trp_tcp_sock_data_t)sock->data)->peer_name)
-    free(((gras_trp_tcp_sock_data_t)sock->data)->peer_name);
+  free(((gras_trp_tcp_sock_data_t)sock->data)->peer_name);
   free(sock->data);
 
   XBT_VERB("close tcp connection %d", sock->sd);
@@ -750,25 +756,23 @@ void gras_trp_buf_socket_close(gras_socket_t sock)
           data->in_buf.size - data->in_buf.pos,
           data->in_buf.size, data->in_buf.pos);
   }
-  if (data->in_buf.data)
-    free(data->in_buf.data);
+  free(data->in_buf.data);
 
   if (data->out_buf.size != data->out_buf.pos) {
     XBT_DEBUG("Flush the socket before closing (in=%d,out=%d)",
            data->in_buf.size, data->out_buf.size);
     gras_trp_bufiov_flush(sock);
   }
-  if (data->out_buf.data)
-    free(data->out_buf.data);
+  free(data->out_buf.data);
 
 #ifdef HAVE_READV
   if (data->in_buf_v) {
-    if (xbt_dynar_length(data->in_buf_v))
+    if (!xbt_dynar_is_empty(data->in_buf_v))
       XBT_WARN("Socket closed, but some bytes were unread");
     xbt_dynar_free(&data->in_buf_v);
   }
   if (data->out_buf_v) {
-    if (xbt_dynar_length(data->out_buf_v)) {
+    if (!xbt_dynar_is_empty(data->out_buf_v)) {
       XBT_DEBUG("Flush the socket before closing");
       gras_trp_bufiov_flush(sock);
     }
@@ -785,7 +789,7 @@ void gras_trp_buf_socket_close(gras_socket_t sock)
 /****************************/
 
 /*
- * Returns the tcp protocol number from the network protocol data base.
+ * Returns the tcp protocol number from the network protocol data base, or -1 if not found
  *
  * getprotobyname() is not thread safe. We need to lock it.
  */
@@ -796,8 +800,12 @@ static int _gras_tcp_proto_number(void)
 
   if (returnValue == 0) {
     fetchedEntry = getprotobyname("tcp");
-    xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
-    returnValue = fetchedEntry->p_proto;
+    if (fetchedEntry == NULL) {
+      XBT_VERB("getprotobyname(tcp) gave NULL");
+      returnValue = -1;
+    } else {
+      returnValue = fetchedEntry->p_proto;
+    }
   }
 
   return returnValue;