socket-util: Close socket on failed dscp modification.
[openvswitch] / lib / socket-util.c
index 219433fb8f4968b0d6260223fe92af6a10c84399..aded01c35855acf760c820af5cd91f5a769c0324 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -458,7 +458,7 @@ error:
         error = EPROTO;
     }
     if (bind_path) {
-        fatal_signal_remove_file_to_unlink(bind_path);
+        fatal_signal_unlink_file_now(bind_path);
     }
     close(fd);
     return -error;
@@ -544,10 +544,13 @@ exit:
  * and stores -1 into '*fdp'.
  *
  * If 'sinp' is non-null, then on success the target address is stored into
- * '*sinp'. */
+ * '*sinp'.
+ *
+ * 'dscp'  If not DSCP_INVALID, its value becomes the DSCP bits in the IP
+ * headers for the new connection. */
 int
 inet_open_active(int style, const char *target, uint16_t default_port,
-                 struct sockaddr_in *sinp, int *fdp)
+                 struct sockaddr_in *sinp, int *fdp, uint8_t dscp)
 {
     struct sockaddr_in sin;
     int fd = -1;
@@ -568,31 +571,35 @@ inet_open_active(int style, const char *target, uint16_t default_port,
     }
     error = set_nonblocking(fd);
     if (error) {
-        goto exit_close;
+        goto exit;
+    }
+
+    /* The socket options set here ensure that the TOS bits are set during
+     * the connection establishment.  If set after connect(), the handshake
+     * SYN frames will be sent with a TOS of 0. */
+    if (dscp != DSCP_INVALID) {
+        if (setsockopt(fd, IPPROTO_IP, IP_TOS, &dscp, sizeof dscp)) {
+            VLOG_ERR("%s: socket: %s", target, strerror(errno));
+            error = errno;
+            goto exit;
+        }
     }
 
     /* Connect. */
     error = connect(fd, (struct sockaddr *) &sin, sizeof sin) == 0 ? 0 : errno;
     if (error == EINPROGRESS) {
         error = EAGAIN;
-    } else if (error && error != EAGAIN) {
-        goto exit_close;
     }
 
-    /* Success: error is 0 or EAGAIN. */
-    goto exit;
-
-exit_close:
-    close(fd);
 exit:
     if (!error || error == EAGAIN) {
         if (sinp) {
             *sinp = sin;
         }
-        *fdp = fd;
-    } else {
-        *fdp = -1;
+    } else if (fd >= 0) {
+        close(fd);
     }
+    *fdp = fd;
     return error;
 }
 
@@ -663,10 +670,13 @@ exit:
  * negative errno value.
  *
  * If 'sinp' is non-null, then on success the bound address is stored into
- * '*sinp'. */
+ * '*sinp'.
+ *
+ * 'dscp'  If not DSCP_INVALID, its value becomes the DSCP bits in the IP
+ * headers for the new connection. */
 int
 inet_open_passive(int style, const char *target, int default_port,
-                  struct sockaddr_in *sinp)
+                  struct sockaddr_in *sinp, uint8_t dscp)
 {
     struct sockaddr_in sin;
     int fd = 0, error;
@@ -701,8 +711,19 @@ inet_open_passive(int style, const char *target, int default_port,
         goto error;
     }
 
+    /* The socket options set here ensure that the TOS bits are set during
+     * the connection establishment.  If set after connect(), the handshake
+     * SYN frames will be sent with a TOS of 0. */
+    if (dscp != DSCP_INVALID) {
+        if (setsockopt(fd, IPPROTO_IP, IP_TOS, &dscp, sizeof dscp)) {
+            VLOG_ERR("%s: socket: %s", target, strerror(errno));
+            error = errno;
+            goto error;
+        }
+    }
+
     /* Listen. */
-    if (listen(fd, 10) < 0) {
+    if (style == SOCK_STREAM && listen(fd, 10) < 0) {
         error = errno;
         VLOG_ERR("%s: listen: %s", target, strerror(error));
         goto error;