]> git.ozlabs.org Git - ccan/commitdiff
io: add io_conn_fd()
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 21 Oct 2013 05:33:58 +0000 (16:03 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 21 Oct 2013 05:33:58 +0000 (16:03 +1030)
Useful for getsockname().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io/io.c
ccan/io/io.h
ccan/io/test/run-01-start-finish.c

index e2dd44430b79d0f3a0977a92861cb72a86f56df6..f45589c0fac71888b1be312debcb3ec04597b0bb 100644 (file)
@@ -472,6 +472,11 @@ struct io_plan io_break_(void *ret, struct io_plan plan)
        return plan;
 }
 
+int io_conn_fd(const struct io_conn *conn)
+{
+       return conn->fd.fd;
+}
+
 void io_set_alloc(void *(*allocfn)(size_t size),
                  void *(*reallocfn)(void *ptr, size_t size),
                  void (*freefn)(void *ptr))
index 067a69c1dec441a752508dfe50ee8f83b8421466..6248fec02c2e5c5ae3d42e65d80eb8483af1d5aa 100644 (file)
@@ -491,6 +491,14 @@ struct io_plan io_close_cb(struct io_conn *, void *unused);
  */
 void *io_loop(void);
 
+/**
+ * io_conn_fd - get the fd from a connection.
+ * @conn: the connection.
+ *
+ * Sometimes useful, eg for getsockname().
+ */
+int io_conn_fd(const struct io_conn *conn);
+
 /**
  * io_set_alloc - set alloc/realloc/free function for io to use.
  * @allocfn: allocator function
index a63baf7706c12970ed25b27e84c0cec76328c65d..d8a24a52fe42b35e1ddf3a740b3d31d151f8165c 100644 (file)
@@ -9,10 +9,12 @@
 #ifndef PORT
 #define PORT "65001"
 #endif
+static int expected_fd;
 
 static void finish_ok(struct io_conn *conn, int *state)
 {
        ok1(*state == 1);
+       ok1(io_conn_fd(conn) == expected_fd);
        (*state)++;
        io_break(state + 1, io_idle());
 }
@@ -21,6 +23,7 @@ static void init_conn(int fd, int *state)
 {
        ok1(*state == 0);
        (*state)++;
+       expected_fd = fd;
        io_set_finish(io_new_conn(fd, io_close()), finish_ok, state);
 }
 
@@ -64,7 +67,7 @@ int main(void)
        int fd;
 
        /* This is how many tests you plan to run */
-       plan_tests(9);
+       plan_tests(10);
        fd = make_listen_fd(PORT, &addrinfo);
        ok1(fd >= 0);
        l = io_new_listener(fd, init_conn, &state);