summaryrefslogtreecommitdiff
path: root/ext/socket/socket.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-06 02:39:44 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-06 02:39:44 +0000
commitd933fb2296e4ace25c5eb558be56cd23a8fcc88f (patch)
tree597f59b7faf29ed9e5b83692495338a3cb7576fe /ext/socket/socket.c
parente4283fd39d9e8c508ce5ee2dc069b6f5545cd3d8 (diff)
* ext/socket: Improve socket exception message to show socket address.
[ruby-core:45617] [Feature #6583] proposed Eric Hodel. * ext/socket/rubysocket.h (rsock_sys_fail_host_port): Declared. (rsock_sys_fail_path): Ditto. (rsock_sys_fail_sockaddr): Ditto. * ext/socket/udpsocket.c (udp_connect): Use rsock_sys_fail_host_port. (udp_bind): Ditto. (udp_send): Ditto. * ext/socket/init.c (rsock_init_sock): Specify a string for rb_sys_fail argument. (make_fd_nonblock): Ditto. (rsock_s_accept): Ditto. * ext/socket/ipsocket.c (init_inetsock_internal): Use rsock_sys_fail_host_port. * ext/socket/socket.c (rsock_sys_fail_host_port): Defined. (rsock_sys_fail_path): Ditto. (rsock_sys_fail_sockaddr): Ditto. (setup_domain_and_type): Use rsock_sys_fail_sockaddr. (sock_connect_nonblock): Ditto. (sock_bind): Ditto. (sock_gethostname): Specify a string for rb_sys_fail argument. (socket_s_ip_address_list): Ditto. * ext/socket/basicsocket.c (bsock_shutdown): Specify a string for rb_sys_fail argument. (bsock_setsockopt): Use rsock_sys_fail_path. (bsock_getsockopt): Ditto. (bsock_getpeereid): Refine the argument for rb_sys_fail. * ext/socket/unixsocket.c (rsock_init_unixsock): Use rsock_sys_fail_path. (unix_path): Ditto. (unix_send_io): Ditto. (unix_recv_io): Ditto. (unix_addr): Ditto. (unix_peeraddr): Ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40149 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/socket.c')
-rw-r--r--ext/socket/socket.c46
1 files changed, 40 insertions, 6 deletions
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index 3254894..10412be 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -10,6 +10,40 @@
#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)
{
@@ -313,7 +347,7 @@ sock_connect(VALUE sock, VALUE addr)
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);
@@ -375,7 +409,7 @@ sock_connect_nonblock(VALUE sock, VALUE addr)
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);
@@ -475,7 +509,7 @@ sock_bind(VALUE sock, VALUE addr)
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);
}
@@ -910,7 +944,7 @@ sock_gethostname(VALUE obj)
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);
@@ -1608,7 +1642,7 @@ socket_s_ip_address_list(VALUE self)
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;
@@ -1678,7 +1712,7 @@ socket_s_ip_address_list(VALUE self)
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
- rb_sys_fail("socket");
+ rb_sys_fail("socket(2)");
bufsize = sizeof(initbuf);
buf = initbuf;