Project

General

Profile

Feature #6583 » socket.improve_bind_error.2.patch

drbrain (Eric Hodel), 07/04/2012 11:07 AM

View differences:

ext/socket/rubysocket.h (working copy)
void rsock_init_sockopt(void);
void rsock_init_socket_init(void);
void rsock_sys_fail_host_port(const char *, VALUE, VALUE);
void rsock_sys_fail_path(const char *, VALUE);
void rsock_sys_fail_sockaddr(const char *, VALUE, VALUE);
#endif
ext/socket/udpsocket.c (working copy)
arg.fd = fptr->fd;
ret = rb_ensure(udp_connect_internal, (VALUE)&arg,
rsock_freeaddrinfo, (VALUE)arg.res);
if (!ret) rb_sys_fail("connect(2)");
if (!ret) rsock_sys_fail_host_port("connect(2)", host, port);
return INT2FIX(0);
}
......
return INT2FIX(0);
}
freeaddrinfo(res0);
rb_sys_fail("bind(2)");
rsock_sys_fail_host_port("bind(2)", host, port);
return INT2FIX(0);
}
......
}
}
freeaddrinfo(res0);
rb_sys_fail("sendto(2)");
rsock_sys_fail_host_port("sendto(2)", host, port);
return INT2FIX(n);
}
ext/socket/init.c (working copy)
struct stat sbuf;
if (fstat(fd, &sbuf) < 0)
rb_sys_fail(0);
rb_sys_fail("fstat(2)");
rb_update_max_fd(fd);
if (!S_ISSOCK(sbuf.st_mode))
rb_raise(rb_eArgError, "not a socket file descriptor");
......
#ifdef F_GETFL
flags = fcntl(fd, F_GETFL);
if (flags == -1) {
rb_sys_fail(0);
rb_sys_fail("fnctl(2)");
}
#else
flags = 0;
#endif
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) == -1) {
rb_sys_fail(0);
rb_sys_fail("fnctl(2)");
}
}
......
retry = 0;
goto retry;
}
rb_sys_fail(0);
rb_sys_fail("accept(2)");
}
rb_update_max_fd(fd2);
if (!klass) return INT2NUM(fd2);
ext/socket/ipsocket.c (working copy)
{
int type = arg->type;
struct addrinfo *res;
int fd, status = 0;
int fd, status = 0, local = 0;
const char *syscall = 0;
arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv, SOCK_STREAM,
......
else {
if (arg->local.res) {
status = bind(fd, arg->local.res->ai_addr, arg->local.res->ai_addrlen);
local = status;
syscall = "bind(2)";
}
......
break;
}
if (status < 0) {
rb_sys_fail(syscall);
VALUE host, port;
if (local < 0) {
host = arg->local.host;
port = arg->local.serv;
} else {
host = arg->remote.host;
port = arg->remote.serv;
}
rsock_sys_fail_host_port(syscall, host, port);
}
arg->fd = -1;
ext/socket/socket.c (working copy)
#include "rubysocket.h"
static VALUE sock_s_unpack_sockaddr_in(VALUE, VALUE);
void
rsock_sys_fail_host_port(const char *mesg, VALUE host, VALUE port)
{
VALUE message;
port = rb_String(port);
message = rb_sprintf("%s for \"%s\" port %s",
mesg, StringValueCStr(host), StringValueCStr(port));
rb_sys_fail_str(message);
}
void
rsock_sys_fail_path(const char *mesg, VALUE path)
{
VALUE message = rb_sprintf("%s for \"%s\"",
mesg, StringValueCStr(path));
rb_sys_fail_str(message);
}
void
rsock_sys_fail_sockaddr(const char *mesg, VALUE sock, VALUE addr)
{
VALUE host_port = sock_s_unpack_sockaddr_in(sock, addr);
rsock_sys_fail_host_port(mesg,
RARRAY_PTR(host_port)[1],
RARRAY_PTR(host_port)[0]);
}
static void
setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv)
{
......
fd = fptr->fd;
n = rsock_connect(fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LENINT(addr), 0);
if (n < 0) {
rb_sys_fail("connect(2)");
rsock_sys_fail_sockaddr("connect(2)", sock, addr);
}
return INT2FIX(n);
......
if (n < 0) {
if (errno == EINPROGRESS)
rb_mod_sys_fail(rb_mWaitWritable, "connect(2) would block");
rb_sys_fail("connect(2)");
rsock_sys_fail_sockaddr("connect(2)", sock, addr);
}
return INT2FIX(n);
......
SockAddrStringValue(addr);
GetOpenFile(sock, fptr);
if (bind(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LENINT(addr)) < 0)
rb_sys_fail("bind(2)");
rsock_sys_fail_sockaddr("bind(2)", sock, addr);
return INT2FIX(0);
}
......
rb_secure(3);
if (gethostname(buf, (int)sizeof buf - 1) < 0)
rb_sys_fail("gethostname");
rb_sys_fail("gethostname(3)");
buf[sizeof buf - 1] = '\0';
return rb_str_new2(buf);
......
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
rb_sys_fail("socket");
rb_sys_fail("socket(2)");
memset(&ln, 0, sizeof(ln));
ln.lifn_family = AF_UNSPEC;
......
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
rb_sys_fail("socket");
rb_sys_fail("socket(2)");
bufsize = sizeof(initbuf);
buf = initbuf;
ext/socket/basicsocket.c (working copy)
}
GetOpenFile(sock, fptr);
if (shutdown(fptr->fd, how) == -1)
rb_sys_fail(0);
rb_sys_fail("shutdown(2)");
return INT2FIX(0);
}
......
break;
}
#define rb_sys_fail_path(path) rb_sys_fail_str(path)
rb_io_check_closed(fptr);
if (setsockopt(fptr->fd, level, option, v, vlen) < 0)
rb_sys_fail_path(fptr->pathv);
rsock_sys_fail_path("setsockopt(2)", fptr->pathv);
return INT2FIX(0);
}
......
rb_io_check_closed(fptr);
if (getsockopt(fptr->fd, level, option, buf, &len) < 0)
rb_sys_fail_path(fptr->pathv);
rsock_sys_fail_path("getsockopt(2)", fptr->pathv);
return rsock_sockopt_new(family, level, option, rb_str_new(buf, len));
}
......
gid_t egid;
GetOpenFile(self, fptr);
if (getpeereid(fptr->fd, &euid, &egid) == -1)
rb_sys_fail("getpeereid");
rb_sys_fail("getpeereid(3)");
return rb_assoc_new(UIDT2NUM(euid), GIDT2NUM(egid));
#elif defined(SO_PEERCRED) /* GNU/Linux */
rb_io_t *fptr;
......
VALUE ret;
GetOpenFile(self, fptr);
if (getpeerucred(fptr->fd, &uc) == -1)
rb_sys_fail("getpeerucred");
rb_sys_fail("getpeerucred(3C)");
ret = rb_assoc_new(UIDT2NUM(ucred_geteuid(uc)), GIDT2NUM(ucred_getegid(uc)));
ucred_free(uc);
return ret;
ext/socket/unixsocket.c (working copy)
SafeStringValue(path);
fd = rsock_socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
rb_sys_fail("socket(2)");
rsock_sys_fail_path("socket(2)", path);
}
MEMZERO(&sockaddr, struct sockaddr_un, 1);
......
if (status < 0) {
close(fd);
rb_sys_fail_str(path);
rsock_sys_fail_path("connect(2)", path);
}
if (server) {
if (listen(fd, SOMAXCONN) < 0) {
close(fd);
rb_sys_fail("listen(2)");
rsock_sys_fail_path("listen(2)", path);
}
}
......
struct sockaddr_un addr;
socklen_t len = (socklen_t)sizeof(addr);
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail(0);
rsock_sys_fail_path("getsockname(2)", fptr->pathv);
fptr->pathv = rb_obj_freeze(rsock_unixpath_str(&addr, len));
}
return rb_str_dup(fptr->pathv);
......
arg.fd = fptr->fd;
while ((int)BLOCKING_REGION_FD(sendmsg_blocking, &arg) == -1) {
if (!rb_io_wait_writable(arg.fd))
rb_sys_fail("sendmsg(2)");
rsock_sys_fail_path("sendmsg(2)", fptr->pathv);
}
return Qnil;
......
arg.fd = fptr->fd;
while ((int)BLOCKING_REGION_FD(recvmsg_blocking, &arg) == -1) {
if (!rb_io_wait_readable(arg.fd))
rb_sys_fail("recvmsg(2)");
rsock_sys_fail_path("recvmsg(2)", fptr->pathv);
}
#if FD_PASSING_BY_MSG_CONTROL
......
GetOpenFile(sock, fptr);
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail("getsockname(2)");
rsock_sys_fail_path("getsockname(2)", fptr->pathv);
return rsock_unixaddr(&addr, len);
}
......
GetOpenFile(sock, fptr);
if (getpeername(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail("getpeername(2)");
rsock_sys_fail_path("getpeername(2)", fptr->pathv);
return rsock_unixaddr(&addr, len);
}
test/socket/test_tcp.rb (working copy)
class TestSocket_TCPSocket < Test::Unit::TestCase
def test_initialize_failure
s = TCPServer.new("localhost", nil)
server_port = s.addr[1]
c = TCPSocket.new("localhost", server_port)
client_port = c.addr[1]
begin
# TCPServer.new uses SO_REUSEADDR so we must create a failure on the
# local address.
TCPSocket.new("localhost", server_port, "localhost", client_port)
flunk "expected SystemCallError"
rescue SystemCallError => e
assert_match "for \"localhost\" port #{client_port}", e.message
end
end
def test_recvfrom
svr = TCPServer.new("localhost", 0)
th = Thread.new {
test/socket/test_socket.rb (working copy)
}
end
def test_bind
Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) {|bound|
bound.bind(Socket.sockaddr_in(0, "127.0.0.1"))
addr = bound.getsockname
port, = Socket.unpack_sockaddr_in(addr)
Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) {|s|
e = assert_raises(Errno::EADDRINUSE) do
s.bind(Socket.sockaddr_in(port, "127.0.0.1"))
end
assert_match "bind(2) for \"127.0.0.1\" port #{port}", e.message
}
}
end
def test_getaddrinfo
# This should not send a DNS query because AF_UNIX.
assert_raise(SocketError) { Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") }
test/socket/test_udp.rb (working copy)
s.bind(host, 2000)
}
end
def test_bind_addrinuse
host = "127.0.0.1"
port = 2001
in_use = UDPSocket.new
in_use.bind(host, port)
s = UDPSocket.new
e = assert_raises(Errno::EADDRINUSE) do
s.bind(host, port)
end
assert_match "bind(2) for \"#{host}\" port #{port}", e.message
end
def test_send_too_long
u = UDPSocket.new
e = assert_raises Errno::EMSGSIZE do
u.send "\0" * 100_000, 0, "127.0.0.1", 7 # echo
end
assert_match 'for "127.0.0.1" port 7', e.message
end
end if defined?(UDPSocket)
(2-2/2)