vconn-stream: Factor out port defaults into public helper functions.
authorBen Pfaff <blp@nicira.com>
Thu, 18 Mar 2010 19:59:33 +0000 (12:59 -0700)
committerBen Pfaff <blp@nicira.com>
Mon, 12 Apr 2010 18:13:13 +0000 (11:13 -0700)
These functions will be used elsewhere in an upcoming commit.

lib/stream.c
lib/stream.h
lib/vconn-stream.c

index 7d00caf..2349b0c 100644 (file)
@@ -647,3 +647,72 @@ pstream_init(struct pstream *pstream, struct pstream_class *class,
     pstream->class = class;
     pstream->name = xstrdup(name);
 }
+\f
+static int
+count_fields(const char *s_)
+{
+    char *s, *field, *save_ptr;
+    int n = 0;
+
+    save_ptr = NULL;
+    s = xstrdup(s_);
+    for (field = strtok_r(s, ":", &save_ptr); field != NULL;
+         field = strtok_r(NULL, ":", &save_ptr)) {
+        n++;
+    }
+    free(s);
+
+    return n;
+}
+
+/* Like stream_open(), but for tcp streams the port defaults to
+ * 'default_tcp_port' if no port number is given and for SSL streams the port
+ * defaults to 'default_ssl_port' if no port number is given. */
+int
+stream_open_with_default_ports(const char *name_,
+                               uint16_t default_tcp_port,
+                               uint16_t default_ssl_port,
+                               struct stream **streamp)
+{
+    char *name;
+    int error;
+
+    if (!strncmp(name_, "tcp:", 4) && count_fields(name_) < 3) {
+        name = xasprintf("%s:%d", name_, default_tcp_port);
+    } else if (!strncmp(name_, "ssl:", 4) && count_fields(name_) < 3) {
+        name = xasprintf("%s:%d", name_, default_ssl_port);
+    } else {
+        name = xstrdup(name_);
+    }
+    error = stream_open(name, streamp);
+    free(name);
+
+    return error;
+}
+
+/* Like pstream_open(), but for ptcp streams the port defaults to
+ * 'default_ptcp_port' if no port number is given and for passive SSL streams
+ * the port defaults to 'default_pssl_port' if no port number is given. */
+int
+pstream_open_with_default_ports(const char *name_,
+                                uint16_t default_ptcp_port,
+                                uint16_t default_pssl_port,
+                                struct pstream **pstreamp)
+{
+    char *name;
+    int error;
+
+    if (!strncmp(name_, "ptcp:", 5) && count_fields(name_) < 2) {
+        name = xasprintf("%s%d", name_, default_ptcp_port);
+    } else if (!strncmp(name_, "pssl:", 5) && count_fields(name_) < 2) {
+        name = xasprintf("%s%d", name_, default_pssl_port);
+    } else {
+        name = xstrdup(name_);
+    }
+    error = pstream_open(name, pstreamp);
+    free(name);
+
+    return error;
+}
+
+
index 8c4e78a..d05107d 100644 (file)
@@ -61,5 +61,16 @@ void pstream_close(struct pstream *);
 int pstream_accept(struct pstream *, struct stream **);
 int pstream_accept_block(struct pstream *, struct stream **);
 void pstream_wait(struct pstream *);
+\f
+/* Convenience funtions. */
+
+int stream_open_with_default_ports(const char *name,
+                                   uint16_t default_tcp_port,
+                                   uint16_t default_ssl_port,
+                                   struct stream **);
+int pstream_open_with_default_ports(const char *name,
+                                    uint16_t default_ptcp_port,
+                                    uint16_t default_pssl_port,
+                                    struct pstream **);
 
 #endif /* stream.h */
index aa2e619..034d1d5 100644 (file)
@@ -51,7 +51,6 @@ static struct vconn_class stream_vconn_class;
 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
 
 static void vconn_stream_clear_txbuf(struct vconn_stream *);
-static int count_fields(const char *);
 
 static struct vconn *
 vconn_stream_new(struct stream *stream, int connect_status)
@@ -76,22 +75,14 @@ vconn_stream_new(struct stream *stream, int connect_status)
  *
  * Returns 0 if successful, otherwise a positive errno value. */
 static int
-vconn_stream_open(const char *name_, char *suffix OVS_UNUSED,
+vconn_stream_open(const char *name, char *suffix OVS_UNUSED,
                   struct vconn **vconnp)
 {
     struct stream *stream;
-    char *name;
     int error;
 
-    if (!strncmp(name_, "tcp:", 4) && count_fields(name_) < 3) {
-        name = xasprintf("%s:%d", name_, OFP_TCP_PORT);
-    } else if (!strncmp(name_, "ssl:", 4) && count_fields(name_) < 3) {
-        name = xasprintf("%s:%d", name_, OFP_SSL_PORT);
-    } else {
-        name = xstrdup(name_);
-    }
-    error = stream_open(name, &stream);
-    free(name);
+    error = stream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
+                                           &stream);
 
     if (error && error != EAGAIN) {
         return error;
@@ -302,29 +293,21 @@ pvconn_pstream_cast(struct pvconn *pvconn)
  * Returns 0 if successful, otherwise a positive errno value.  (The current
  * implementation never fails.) */
 static int
-pvconn_pstream_listen(const char *name_, char *suffix OVS_UNUSED,
+pvconn_pstream_listen(const char *name, char *suffix OVS_UNUSED,
                       struct pvconn **pvconnp)
 {
     struct pvconn_pstream *ps;
     struct pstream *pstream;
-    char *name;
     int error;
 
-    if (!strncmp(name_, "ptcp:", 5) && count_fields(name_) < 2) {
-        name = xasprintf("%s%d", name_, OFP_TCP_PORT);
-    } else if (!strncmp(name_, "pssl:", 5) && count_fields(name_) < 2) {
-        name = xasprintf("%s%d", name_, OFP_SSL_PORT);
-    } else {
-        name = xstrdup(name_);
-    }
-    error = pstream_open(name, &pstream);
-    free(name);
+    error = pstream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
+                                            &pstream);
     if (error) {
         return error;
     }
 
     ps = xmalloc(sizeof *ps);
-    pvconn_init(&ps->pvconn, &pstream_pvconn_class, name_);
+    pvconn_init(&ps->pvconn, &pstream_pvconn_class, name);
     ps->pstream = pstream;
     *pvconnp = &ps->pvconn;
     return 0;
@@ -365,23 +348,6 @@ pvconn_pstream_wait(struct pvconn *pvconn)
     pstream_wait(ps->pstream);
 }
 \f
-static int
-count_fields(const char *s_)
-{
-    char *s, *field, *save_ptr;
-    int n = 0;
-
-    save_ptr = NULL;
-    s = xstrdup(s_);
-    for (field = strtok_r(s, ":", &save_ptr); field != NULL;
-         field = strtok_r(NULL, ":", &save_ptr)) {
-        n++;
-    }
-    free(s);
-
-    return n;
-}
-\f
 /* Stream-based vconns and pvconns. */
 
 #define DEFINE_VCONN_STREAM_CLASS(NAME)             \