summaryrefslogtreecommitdiffstats
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
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.
-rw-r--r--socket.c2
-rw-r--r--stream.c18
-rw-r--r--stream.h2
3 files changed, 22 insertions, 0 deletions
diff --git a/socket.c b/socket.c
index b0c90465..c263edb1 100644
--- a/socket.c
+++ b/socket.c
@@ -267,6 +267,8 @@ static val sock_connect(val sock, val sockaddr)
uw_throwf(socket_error_s, lit("connect failed: ~d/~s"),
num(errno), string_utf8(strerror(errno)), nao);
+ sock_set_peer(sock, sockaddr);
+
return t;
}
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));
diff --git a/stream.h b/stream.h
index 28d7ea70..148e6950 100644
--- a/stream.h
+++ b/stream.h
@@ -117,6 +117,8 @@ val stream_fd(val stream);
#if HAVE_SOCKETS
val sock_family(val stream);
val sock_type(val stream);
+val sock_peer(val stream);
+val sock_set_peer(val stream, val peer);
#endif
val make_string_input_stream(val);
val make_string_byte_input_stream(val);