summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--ext/socket/udpsocket.c51
2 files changed, 36 insertions, 20 deletions
diff --git a/ChangeLog b/ChangeLog
index 469eb6534b..73bdd391f9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sat Oct 10 19:04:42 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * ext/socket/udpsocket.c (udp_connect, udp_bind): get open files
+ inside ensure functions.
+
Sat Oct 10 18:35:12 2015 Koichi Sasada <ko1@atdot.net>
* vm_insnhelper.c (vm_call_method0): do not propagate enable_fastpath,
diff --git a/ext/socket/udpsocket.c b/ext/socket/udpsocket.c
index bbd588ae02..2257fc95c8 100644
--- a/ext/socket/udpsocket.c
+++ b/ext/socket/udpsocket.c
@@ -44,15 +44,18 @@ udp_init(int argc, VALUE *argv, VALUE sock)
struct udp_arg
{
struct rb_addrinfo *res;
- int fd;
+ VALUE io;
};
static VALUE
udp_connect_internal(struct udp_arg *arg)
{
- int fd = arg->fd;
+ rb_io_t *fptr;
+ int fd;
struct addrinfo *res;
+ GetOpenFile(arg->io, fptr);
+ fd = fptr->fd;
for (res = arg->res->ai; res; res = res->ai_next) {
if (rsock_connect(fd, res->ai_addr, res->ai_addrlen, 0) >= 0) {
return Qtrue;
@@ -80,19 +83,35 @@ udp_connect_internal(struct udp_arg *arg)
static VALUE
udp_connect(VALUE sock, VALUE host, VALUE port)
{
- rb_io_t *fptr;
struct udp_arg arg;
VALUE ret;
- GetOpenFile(sock, fptr);
+ arg.io = sock;
arg.res = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
- arg.fd = fptr->fd;
ret = rb_ensure(udp_connect_internal, (VALUE)&arg,
rsock_freeaddrinfo, (VALUE)arg.res);
if (!ret) rsock_sys_fail_host_port("connect(2)", host, port);
return INT2FIX(0);
}
+static VALUE
+udp_bind_internal(struct udp_arg *arg)
+{
+ rb_io_t *fptr;
+ int fd;
+ struct addrinfo *res;
+
+ GetOpenFile(arg->io, fptr);
+ fd = fptr->fd;
+ for (res = arg->res->ai; res; res = res->ai_next) {
+ if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) {
+ continue;
+ }
+ return Qtrue;
+ }
+ return Qfalse;
+}
+
/*
* call-seq:
* udpsocket.bind(host, port) #=> 0
@@ -108,22 +127,14 @@ udp_connect(VALUE sock, VALUE host, VALUE port)
static VALUE
udp_bind(VALUE sock, VALUE host, VALUE port)
{
- rb_io_t *fptr;
- struct rb_addrinfo *res0;
- struct addrinfo *res;
-
- GetOpenFile(sock, fptr);
- res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
- for (res = res0->ai; res; res = res->ai_next) {
- if (bind(fptr->fd, res->ai_addr, res->ai_addrlen) < 0) {
- continue;
- }
- rb_freeaddrinfo(res0);
- return INT2FIX(0);
- }
- rb_freeaddrinfo(res0);
+ struct udp_arg arg;
+ VALUE ret;
- rsock_sys_fail_host_port("bind(2)", host, port);
+ arg.io = sock;
+ arg.res = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
+ ret = rb_ensure(udp_bind_internal, (VALUE)&arg,
+ rsock_freeaddrinfo, (VALUE)arg.res);
+ if (!ret) rsock_sys_fail_host_port("bind(2)", host, port);
return INT2FIX(0);
}