* ext/socket/raddrinfo.c (init_unix_addrinfo): support the longest

path in sockaddr_un.
  (inspect_sockaddr): ditto.
  (addrinfo_mdump): ditto.
  (addrinfo_mload): ditto.
  (rsock_unixpath_str): new function.
  (rsock_unixpath): removed.
  (rsock_unixaddr): use rsock_unixpath_str.

* ext/socket/socket.c (sock_s_pack_sockaddr_un): support the longest
  path in sockaddr_un.
  (sock_s_unpack_sockaddr_un): ditto.
  (sock_s_gethostbyaddr): unused variable removed.

* ext/socket/unixsocket.c (rsock_init_unixsock): support the longest
  path in sockaddr_un.

* ext/socket/rubysocket.h (rsock_unixpath_str): declared.
  (rsock_unixpath): removed.

* test/socket/test_unix.rb: comment out test_nul because abstract unix
  sockets may contain NULs.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35474 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2012-04-26 03:34:12 +00:00
Родитель 2bc4268e79
Коммит 1413510ed2
6 изменённых файлов: 65 добавлений и 52 удалений

Просмотреть файл

@ -1,3 +1,28 @@
Thu Apr 26 12:28:06 2012 Tanaka Akira <akr@fsij.org>
* ext/socket/raddrinfo.c (init_unix_addrinfo): support the longest
path in sockaddr_un.
(inspect_sockaddr): ditto.
(addrinfo_mdump): ditto.
(addrinfo_mload): ditto.
(rsock_unixpath_str): new function.
(rsock_unixpath): removed.
(rsock_unixaddr): use rsock_unixpath_str.
* ext/socket/socket.c (sock_s_pack_sockaddr_un): support the longest
path in sockaddr_un.
(sock_s_unpack_sockaddr_un): ditto.
(sock_s_gethostbyaddr): unused variable removed.
* ext/socket/unixsocket.c (rsock_init_unixsock): support the longest
path in sockaddr_un.
* ext/socket/rubysocket.h (rsock_unixpath_str): declared.
(rsock_unixpath): removed.
* test/socket/test_unix.rb: comment out test_nul because abstract unix
sockets may contain NULs.
Thu Apr 26 01:32:33 2012 CHIKANAGA Tomoyuki <nagachika00@gmail.com>
* test/optparse/test_summary.rb (test_summary_containing_space): add

Просмотреть файл

@ -421,20 +421,25 @@ rsock_ipaddr(struct sockaddr *sockaddr, int norevlookup)
}
#ifdef HAVE_SYS_UN_H
const char*
rsock_unixpath(struct sockaddr_un *sockaddr, socklen_t len)
VALUE
rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len)
{
if (sockaddr->sun_path < (char*)sockaddr + len)
return sockaddr->sun_path;
char *s, *e;
s = sockaddr->sun_path;
e = (char *)sockaddr + len;
while (s < e && *(e-1) == '\0')
e--;
if (s <= e)
return rb_str_new(s, e-s);
else
return "";
return rb_str_new2("");
}
VALUE
rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len)
{
return rb_assoc_new(rb_str_new2("AF_UNIX"),
rb_str_new2(rsock_unixpath(sockaddr, len)));
rsock_unixpath_str(sockaddr, len));
}
#endif
@ -767,10 +772,10 @@ init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype)
StringValue(path);
if (sizeof(un.sun_path) <= (size_t)RSTRING_LEN(path))
if (sizeof(un.sun_path) < (size_t)RSTRING_LEN(path))
rb_raise(rb_eArgError,
"too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
(size_t)RSTRING_LEN(path), sizeof(un.sun_path)-1);
(size_t)RSTRING_LEN(path), sizeof(un.sun_path));
MEMZERO(&un, struct sockaddr_un, 1);
@ -998,9 +1003,11 @@ inspect_sockaddr(VALUE addrinfo, VALUE ret)
case AF_UNIX:
{
struct sockaddr_un *addr = (struct sockaddr_un *)&rai->addr;
char *p, *s, *t, *e;
char *p, *s, *e;
s = addr->sun_path;
e = (char*)addr + rai->sockaddr_len;
while (s < e && *(e-1) == '\0')
e--;
if (e < s)
rb_str_cat2(ret, "too-short-AF_UNIX-sockaddr");
else if (s == e)
@ -1008,28 +1015,17 @@ inspect_sockaddr(VALUE addrinfo, VALUE ret)
else {
int printable_only = 1;
p = s;
while (p < e && *p != '\0') {
while (p < e) {
printable_only = printable_only && ISPRINT(*p) && !ISSPACE(*p);
p++;
}
t = p;
while (p < e && *p == '\0')
p++;
if (printable_only && /* only printable, no space */
t < e && /* NUL terminated */
p == e) { /* no data after NUL */
if (s == t)
rb_str_cat2(ret, "empty-path-AF_UNIX-sockaddr");
else if (s[0] == '/') /* absolute path */
rb_str_cat2(ret, s);
else
rb_str_catf(ret, "AF_UNIX %s", s);
if (printable_only) { /* only printable, no space */
if (s[0] != '/') /* relative path */
rb_str_cat2(ret, "AF_UNIX ");
rb_str_cat(ret, s, p - s);
}
else {
rb_str_cat2(ret, "AF_UNIX");
e = (char *)addr->sun_path + sizeof(addr->sun_path);
while (s < e && *(e-1) == '\0')
e--;
while (s < e)
rb_str_catf(ret, ":%02x", (unsigned char)*s++);
}
@ -1203,7 +1199,7 @@ addrinfo_mdump(VALUE self)
struct sockaddr_un *su = (struct sockaddr_un *)&rai->addr;
char *s, *e;
s = su->sun_path;
e = (char*)s + sizeof(su->sun_path);
e = (char*)su + rai->sockaddr_len;
while (s < e && *(e-1) == '\0')
e--;
sockaddr = rb_str_new(s, e-s);
@ -1300,14 +1296,14 @@ addrinfo_mload(VALUE self, VALUE ary)
case AF_UNIX:
{
struct sockaddr_un uaddr;
memset(&uaddr, 0, sizeof(uaddr));
MEMZERO(&uaddr, struct sockaddr_un, 1);
uaddr.sun_family = AF_UNIX;
StringValue(v);
if (sizeof(uaddr.sun_path) <= (size_t)RSTRING_LEN(v))
if (sizeof(uaddr.sun_path) < (size_t)RSTRING_LEN(v))
rb_raise(rb_eSocket,
"too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
(size_t)RSTRING_LEN(v), sizeof(uaddr.sun_path)-1);
(size_t)RSTRING_LEN(v), sizeof(uaddr.sun_path));
memcpy(uaddr.sun_path, RSTRING_PTR(v), RSTRING_LEN(v));
len = (socklen_t)sizeof(uaddr);
memcpy(&ss, &uaddr, len);

Просмотреть файл

@ -233,7 +233,7 @@ VALUE rsock_make_hostent(VALUE host, struct addrinfo *addr, VALUE (*ipaddr)(stru
int rsock_revlookup_flag(VALUE revlookup, int *norevlookup);
#ifdef HAVE_SYS_UN_H
const char* rsock_unixpath(struct sockaddr_un *sockaddr, socklen_t len);
VALUE rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len);
VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len);
#endif

Просмотреть файл

@ -1003,13 +1003,12 @@ sock_s_gethostbyaddr(int argc, VALUE *argv)
{
VALUE addr, family;
struct hostent *h;
struct sockaddr *sa;
char **pch;
VALUE ary, names;
int t = AF_INET;
rb_scan_args(argc, argv, "11", &addr, &family);
sa = (struct sockaddr*)StringValuePtr(addr);
StringValue(addr);
if (!NIL_P(family)) {
t = rsock_family_arg(family);
}
@ -1422,17 +1421,16 @@ static VALUE
sock_s_pack_sockaddr_un(VALUE self, VALUE path)
{
struct sockaddr_un sockaddr;
char *sun_path;
VALUE addr;
StringValue(path);
MEMZERO(&sockaddr, struct sockaddr_un, 1);
sockaddr.sun_family = AF_UNIX;
sun_path = StringValueCStr(path);
if (sizeof(sockaddr.sun_path) <= strlen(sun_path)) {
rb_raise(rb_eArgError, "too long unix socket path (%ldbytes given but %dbytes max)",
RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path)-1);
if (sizeof(sockaddr.sun_path) <= RSTRING_LEN(path)) {
rb_raise(rb_eArgError, "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
(size_t)RSTRING_LEN(path), sizeof(sockaddr.sun_path));
}
strncpy(sockaddr.sun_path, sun_path, sizeof(sockaddr.sun_path)-1);
memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
addr = rb_str_new((char*)&sockaddr, sizeof(sockaddr));
OBJ_INFECT(addr, path);
@ -1455,7 +1453,6 @@ static VALUE
sock_s_unpack_sockaddr_un(VALUE self, VALUE addr)
{
struct sockaddr_un * sockaddr;
const char *sun_path;
VALUE path;
sockaddr = (struct sockaddr_un*)SockAddrStringValuePtr(addr);
@ -1471,13 +1468,7 @@ sock_s_unpack_sockaddr_un(VALUE self, VALUE addr)
rb_raise(rb_eTypeError, "too long sockaddr_un - %ld longer than %d",
RSTRING_LEN(addr), (int)sizeof(struct sockaddr_un));
}
sun_path = rsock_unixpath(sockaddr, RSTRING_LENINT(addr));
if (sizeof(struct sockaddr_un) == RSTRING_LEN(addr) &&
sun_path == sockaddr->sun_path &&
sun_path + strlen(sun_path) == RSTRING_PTR(addr) + RSTRING_LEN(addr)) {
rb_raise(rb_eArgError, "sockaddr_un.sun_path not NUL terminated");
}
path = rb_str_new2(sun_path);
path = rsock_unixpath_str(sockaddr, RSTRING_LENINT(addr));
OBJ_INFECT(path, addr);
return path;
}

Просмотреть файл

@ -39,9 +39,9 @@ rsock_init_unixsock(VALUE sock, VALUE path, int server)
MEMZERO(&sockaddr, struct sockaddr_un, 1);
sockaddr.sun_family = AF_UNIX;
if (sizeof(sockaddr.sun_path) <= (size_t)RSTRING_LEN(path)) {
if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) {
rb_raise(rb_eArgError, "too long unix socket path (%ldbytes given but %dbytes max)",
RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path)-1);
RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path));
}
memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
@ -118,7 +118,7 @@ unix_path(VALUE sock)
socklen_t len = (socklen_t)sizeof(addr);
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail(0);
fptr->pathv = rb_obj_freeze(rb_str_new_cstr(rsock_unixpath(&addr, len)));
fptr->pathv = rb_obj_freeze(rsock_unixpath_str(&addr, len));
}
return rb_str_dup(fptr->pathv);
}

Просмотреть файл

@ -339,9 +339,10 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase
assert_raise(ArgumentError) { UNIXServer.new("a" * 300) }
end
def test_nul
assert_raise(ArgumentError) { Socket.sockaddr_un("a\0b") }
end
#def test_nul
# # path may contain NULs for abstract unix sockets. [ruby-core:10288]
# assert_raise(ArgumentError) { Socket.sockaddr_un("a\0b") }
#end
def test_dgram_pair
s1, s2 = UNIXSocket.pair(Socket::SOCK_DGRAM)