summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-10-10 10:04:44 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-10-10 10:04:44 +0000
commitd9ac64fdc79ec2aefe5ed21b43b151caa296c5e3 (patch)
treefe8b486d58e9a18aca41f1c646a9656f33366689 /ext
parenta00fc76ce5059e504cbdc10e257624ac893f30c0 (diff)
udpsocket.c: refix r52097
* ext/socket/udpsocket.c (udp_connect, udp_bind): get open files inside ensure functions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52101 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/socket/udpsocket.c51
1 files changed, 31 insertions, 20 deletions
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);
}