[ovs-dev] [PATCH 2/7] socket-util: Make inet_open_passive() pass back the bound address.

Ben Pfaff blp at nicira.com
Thu Jan 7 23:01:26 UTC 2010


This feature is useful in an upcoming commit.
---
 lib/socket-util.c |   23 +++++++++++++++++++++--
 lib/socket-util.h |    3 ++-
 lib/stream-ssl.c  |    4 ++--
 lib/stream-tcp.c  |    2 +-
 4 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/lib/socket-util.c b/lib/socket-util.c
index adc1c6b..d2ee8ea 100644
--- a/lib/socket-util.c
+++ b/lib/socket-util.c
@@ -400,9 +400,13 @@ exit:
  * For TCP, the socket will have SO_REUSEADDR turned on.
  *
  * On success, returns a non-negative file descriptor.  On failure, returns a
- * negative errno value. */
+ * negative errno value.
+ *
+ * If 'sinp' is non-null, then on success the bound address is stored into
+ * '*sinp'. */
 int
-inet_open_passive(int style, const char *target_, int default_port)
+inet_open_passive(int style, const char *target_, int default_port,
+                  struct sockaddr_in *sinp)
 {
     char *target = xstrdup(target_);
     char *string_ptr = target;
@@ -468,6 +472,21 @@ inet_open_passive(int style, const char *target_, int default_port)
         VLOG_ERR("%s: listen: %s", target_, strerror(error));
         goto exit_close;
     }
+
+    if (sinp) {
+        socklen_t sin_len = sizeof sin;
+        if (getsockname(fd, (struct sockaddr *) &sin, &sin_len) < 0){
+            error = errno;
+            VLOG_ERR("%s: getsockname: %s", target_, strerror(error));
+            goto exit_close;
+        }
+        if (sin.sin_family != AF_INET || sin_len != sizeof sin) {
+            VLOG_ERR("%s: getsockname: invalid socket name", target_);
+            goto exit_close;
+        }
+        *sinp = sin;
+    }
+
     error = 0;
     goto exit;
 
diff --git a/lib/socket-util.h b/lib/socket-util.h
index def2132..11f21c2 100644
--- a/lib/socket-util.h
+++ b/lib/socket-util.h
@@ -36,7 +36,8 @@ int get_null_fd(void);
 
 int inet_open_active(int style, const char *target, uint16_t default_port,
                     struct sockaddr_in *sinp, int *fdp);
-int inet_open_passive(int style, const char *target, int default_port);
+int inet_open_passive(int style, const char *target, int default_port,
+                      struct sockaddr_in *sinp);
 
 int read_fully(int fd, void *, size_t, size_t *bytes_read);
 int write_fully(int fd, const void *, size_t, size_t *bytes_written);
diff --git a/lib/stream-ssl.c b/lib/stream-ssl.c
index b14ce55..442a1e6 100644
--- a/lib/stream-ssl.c
+++ b/lib/stream-ssl.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -721,7 +721,7 @@ pssl_open(const char *name, char *suffix, struct pstream **pstreamp)
         return retval;
     }
 
-    fd = inet_open_passive(SOCK_STREAM, suffix, OFP_SSL_PORT);
+    fd = inet_open_passive(SOCK_STREAM, suffix, OFP_SSL_PORT, NULL);
     if (fd < 0) {
         return -fd;
     }
diff --git a/lib/stream-tcp.c b/lib/stream-tcp.c
index 94ff74c..dd55845 100644
--- a/lib/stream-tcp.c
+++ b/lib/stream-tcp.c
@@ -105,7 +105,7 @@ ptcp_open(const char *name UNUSED, char *suffix, struct pstream **pstreamp)
 {
     int fd;
 
-    fd = inet_open_passive(SOCK_STREAM, suffix, -1);
+    fd = inet_open_passive(SOCK_STREAM, suffix, -1, NULL);
     if (fd < 0) {
         return -fd;
     } else {
-- 
1.6.3.3





More information about the dev mailing list