summaryrefslogtreecommitdiff
path: root/ext/socket/raddrinfo.c
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-25 08:15:26 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-25 08:15:26 +0000
commitad55d141ebd0922be310b66c8e4c0e81e94521d1 (patch)
treef13d4d87daf11313efb560d8e85ddc1a4a2ceb6a /ext/socket/raddrinfo.c
parent76ade818ae8bc3bc72979e5fa880add04b165636 (diff)
* ext/socket/raddrinfo.c (rsock_unixpath_len, init_unix_addrinfo),
ext/socket/unixsocket.c (unixsock_connect_internal, rsock_init_unixsock): calculate the correct address length of an abstract socket. Without this fix, sizeof(struct sockaddr_un) is specified as the length of an abstract socket for bind(2) or connect(2), so the address of the socket is filled with extra NUL characters. See unix(7) for details. * ext/socket/lib/socket.rb (unix_server_socket): don't access the file system if the platform is Linux and path starts with NUL, which means that the socket is an abstract socket. * test/socket/test_unix.rb: related test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38939 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/raddrinfo.c')
-rw-r--r--ext/socket/raddrinfo.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c
index 6a5129efa8..fdc3d4e61c 100644
--- a/ext/socket/raddrinfo.c
+++ b/ext/socket/raddrinfo.c
@@ -441,6 +441,23 @@ rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len)
return rb_assoc_new(rb_str_new2("AF_UNIX"),
rsock_unixpath_str(sockaddr, len));
}
+
+socklen_t
+rsock_unixpath_len(VALUE path)
+{
+#ifdef __linux__
+ if (RSTRING_PTR(path)[0] == '\0') {
+ /* abstract namespace; see unix(7) for details. */
+ return (socklen_t) offsetof(struct sockaddr_un, sun_path) +
+ RSTRING_LEN(path);
+ }
+ else {
+#endif
+ return (socklen_t) sizeof(struct sockaddr_un);
+#ifdef __linux__
+ }
+#endif
+}
#endif
struct hostent_arg {
@@ -769,6 +786,7 @@ static void
init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype)
{
struct sockaddr_un un;
+ socklen_t len;
StringValue(path);
@@ -782,7 +800,8 @@ init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype)
un.sun_family = AF_UNIX;
memcpy((void*)&un.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
- init_addrinfo(rai, (struct sockaddr *)&un, (socklen_t)sizeof(un),
+ len = rsock_unixpath_len(path);
+ init_addrinfo(rai, (struct sockaddr *)&un, len,
PF_UNIX, socktype, 0, Qnil, Qnil);
}
#endif