summaryrefslogtreecommitdiffstats
path: root/stream.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-02-27 18:40:58 -0800
committerKaz Kylheku <kaz@kylheku.com>2016-02-27 18:40:58 -0800
commit6f4ce69fc86b9272472ae356f8fa98eca7833c88 (patch)
tree8113264695747f920a352d2416ad2452410f2627 /stream.c
parentf589076e26aa831e3e7effed243860debd6f7f6a (diff)
downloadtxr-6f4ce69fc86b9272472ae356f8fa98eca7833c88.tar.gz
txr-6f4ce69fc86b9272472ae356f8fa98eca7833c88.tar.bz2
txr-6f4ce69fc86b9272472ae356f8fa98eca7833c88.zip
Socket streams know their connected peer address.
* stream.c (struct stdio_handle): New member, peer. (stdio_stream_mark): Mark the new member. (sock_peer, sock_set_peer): New functions. (stream_init): Registered sock-peer intrinsic. * stream.h (sock_peer, sock_set_peer): Declared. * socket.c (sock_connect): If the connect is successful, then store the address into the stream as the peer.
Diffstat (limited to 'stream.c')
-rw-r--r--stream.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/stream.c b/stream.c
index 69251bf9..012b5c55 100644
--- a/stream.c
+++ b/stream.c
@@ -325,6 +325,7 @@ struct stdio_handle {
#if HAVE_SOCKETS
val family;
val type;
+ val peer;
#endif
};
@@ -360,6 +361,7 @@ static void stdio_stream_mark(val stream)
#if HAVE_SOCKETS
gc_mark(h->family);
gc_mark(h->type);
+ gc_mark(h->peer);
#endif
}
@@ -1142,6 +1144,7 @@ static val make_stdio_stream_common(FILE *f, val descr, struct cobj_ops *ops)
#if HAVE_SOCKETS
h->family = nil;
h->type = nil;
+ h->peer = nil;
#endif
return stream;
}
@@ -1195,6 +1198,20 @@ val sock_type(val stream)
cobj_handle(stream, stdio_stream_s));
return h->type;
}
+
+val sock_peer(val stream)
+{
+ struct stdio_handle *h = coerce(struct stdio_handle *,
+ cobj_handle(stream, stdio_stream_s));
+ return h->peer;
+}
+
+val sock_set_peer(val stream, val peer)
+{
+ struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle);
+ h->peer = peer;
+ return stream;
+}
#endif
#if HAVE_FORK_STUFF
@@ -3635,6 +3652,7 @@ void stream_init(void)
#ifdef HAVE_SOCKETS
reg_fun(intern(lit("sock-family"), user_package), func_n1(sock_family));
reg_fun(intern(lit("sock-type"), user_package), func_n1(sock_type));
+ reg_fun(intern(lit("sock-peer"), user_package), func_n1(sock_peer));
#endif
reg_fun(intern(lit("make-catenated-stream"), user_package), func_n0v(make_catenated_stream_v));
reg_fun(intern(lit("cat-streams"), user_package), func_n1(make_catenated_stream));