ruby-changes:32967
From: akr <ko1@a...>
Date: Wed, 19 Feb 2014 18:38:29 +0900 (JST)
Subject: [ruby-changes:32967] akr:r45046 (trunk): * ext/socket: Wrap struct addrinfo by struct rb_addrinfo.
akr 2014-02-19 18:38:24 +0900 (Wed, 19 Feb 2014) New Revision: 45046 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45046 Log: * ext/socket: Wrap struct addrinfo by struct rb_addrinfo. Modified files: trunk/ChangeLog trunk/ext/socket/ipsocket.c trunk/ext/socket/raddrinfo.c trunk/ext/socket/rubysocket.h trunk/ext/socket/socket.c trunk/ext/socket/udpsocket.c Index: ChangeLog =================================================================== --- ChangeLog (revision 45045) +++ ChangeLog (revision 45046) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Feb 19 18:31:48 2014 Tanaka Akira <akr@f...> + + * ext/socket: Wrap struct addrinfo by struct rb_addrinfo. + Wed Feb 19 17:47:01 2014 Tanaka Akira <akr@f...> * ext/socket/ipsocket.c (ip_s_getaddress): Don't access freed memory. Index: ext/socket/rubysocket.h =================================================================== --- ext/socket/rubysocket.h (revision 45045) +++ ext/socket/rubysocket.h (revision 45046) @@ -278,10 +278,15 @@ int rsock_shutdown_how_arg(VALUE how); https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L278 int rsock_getfamily(int sockfd); -int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res); +struct rb_addrinfo { + struct addrinfo *ai; +}; +int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct rb_addrinfo **res); +void rb_freeaddrinfo(struct rb_addrinfo *ai); +VALUE rsock_freeaddrinfo(VALUE arg); int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags); -struct addrinfo *rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags); -struct addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack); +struct rb_addrinfo *rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags); +struct rb_addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack); VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len); VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len); @@ -290,7 +295,7 @@ VALUE rsock_addrinfo_inspect_sockaddr(VA https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L295 VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen); VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup); -VALUE rsock_make_hostent(VALUE host, struct addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t)); +VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t)); VALUE rsock_inspect_sockaddr(struct sockaddr *addr, socklen_t socklen, VALUE ret); socklen_t rsock_sockaddr_len(struct sockaddr *addr); VALUE rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len); Index: ext/socket/udpsocket.c =================================================================== --- ext/socket/udpsocket.c (revision 45045) +++ ext/socket/udpsocket.c (revision 45046) @@ -44,7 +44,7 @@ udp_init(int argc, VALUE *argv, VALUE so https://github.com/ruby/ruby/blob/trunk/ext/socket/udpsocket.c#L44 struct udp_arg { - struct addrinfo *res; + struct rb_addrinfo *res; int fd; }; @@ -54,7 +54,7 @@ udp_connect_internal(struct udp_arg *arg https://github.com/ruby/ruby/blob/trunk/ext/socket/udpsocket.c#L54 int fd = arg->fd; struct addrinfo *res; - for (res = arg->res; res; res = res->ai_next) { + for (res = arg->res->ai; res; res = res->ai_next) { if (rsock_connect(fd, res->ai_addr, res->ai_addrlen, 0) >= 0) { return Qtrue; } @@ -62,8 +62,6 @@ udp_connect_internal(struct udp_arg *arg https://github.com/ruby/ruby/blob/trunk/ext/socket/udpsocket.c#L62 return Qfalse; } -VALUE rsock_freeaddrinfo(struct addrinfo *addr); - /* * call-seq: * udpsocket.connect(host, port) => 0 @@ -113,19 +111,20 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/udpsocket.c#L111 udp_bind(VALUE sock, VALUE host, VALUE port) { rb_io_t *fptr; - struct addrinfo *res0, *res; + struct rb_addrinfo *res0; + struct addrinfo *res; rb_secure(3); res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0); GetOpenFile(sock, fptr); - for (res = res0; res; res = res->ai_next) { + for (res = res0->ai; res; res = res->ai_next) { if (bind(fptr->fd, res->ai_addr, res->ai_addrlen) < 0) { continue; } - freeaddrinfo(res0); + rb_freeaddrinfo(res0); return INT2FIX(0); } - freeaddrinfo(res0); + rb_freeaddrinfo(res0); rsock_sys_fail_host_port("bind(2)", host, port); @@ -160,7 +159,8 @@ udp_send(int argc, VALUE *argv, VALUE so https://github.com/ruby/ruby/blob/trunk/ext/socket/udpsocket.c#L159 VALUE flags, host, port; rb_io_t *fptr; int n; - struct addrinfo *res0, *res; + struct rb_addrinfo *res0; + struct addrinfo *res; struct rsock_send_arg arg; if (argc == 2 || argc == 3) { @@ -173,21 +173,21 @@ udp_send(int argc, VALUE *argv, VALUE so https://github.com/ruby/ruby/blob/trunk/ext/socket/udpsocket.c#L173 GetOpenFile(sock, fptr); arg.fd = fptr->fd; arg.flags = NUM2INT(flags); - for (res = res0; res; res = res->ai_next) { + for (res = res0->ai; res; res = res->ai_next) { retry: arg.to = res->ai_addr; arg.tolen = res->ai_addrlen; rsock_maybe_fd_writable(arg.fd); n = (int)BLOCKING_REGION_FD(rsock_sendto_blocking, &arg); if (n >= 0) { - freeaddrinfo(res0); + rb_freeaddrinfo(res0); return INT2FIX(n); } if (rb_io_wait_writable(fptr->fd)) { goto retry; } } - freeaddrinfo(res0); + rb_freeaddrinfo(res0); rsock_sys_fail_host_port("sendto(2)", host, port); return INT2FIX(n); } Index: ext/socket/raddrinfo.c =================================================================== --- ext/socket/raddrinfo.c (revision 45045) +++ ext/socket/raddrinfo.c (revision 45046) @@ -174,21 +174,35 @@ nogvl_getaddrinfo(void *arg) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L174 int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, - struct addrinfo **res) + struct rb_addrinfo **res) { + struct addrinfo *ai; + int ret; + #ifdef GETADDRINFO_EMU - return getaddrinfo(node, service, hints, res); + ret = getaddrinfo(node, service, hints, &ai); #else struct getaddrinfo_arg arg; - int ret; MEMZERO(&arg, sizeof arg, 1); arg.node = node; arg.service = service; arg.hints = hints; - arg.res = res; + arg.res = &ai; ret = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getaddrinfo, &arg, RUBY_UBF_IO, 0); - return ret; #endif + + if (ret == 0) { + *res = (struct rb_addrinfo *)xmalloc(sizeof(struct rb_addrinfo)); + (*res)->ai = ai; + } + return ret; +} + +void +rb_freeaddrinfo(struct rb_addrinfo *ai) +{ + freeaddrinfo(ai->ai); + xfree(ai); } #ifndef GETADDRINFO_EMU @@ -345,10 +359,10 @@ port_str(VALUE port, char *pbuf, size_t https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L359 } } -struct addrinfo* +struct rb_addrinfo* rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack) { - struct addrinfo* res = NULL; + struct rb_addrinfo* res = NULL; char *hostp, *portp; int error; char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; @@ -373,7 +387,7 @@ rsock_getaddrinfo(VALUE host, VALUE port https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L387 return res; } -struct addrinfo* +struct rb_addrinfo* rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags) { struct addrinfo hints; @@ -474,7 +488,7 @@ rsock_unix_sockaddr_len(VALUE path) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L488 struct hostent_arg { VALUE host; - struct addrinfo* addr; + struct rb_addrinfo* addr; VALUE (*ipaddr)(struct sockaddr*, socklen_t); }; @@ -482,7 +496,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L496 make_hostent_internal(struct hostent_arg *arg) { VALUE host = arg->host; - struct addrinfo* addr = arg->addr; + struct addrinfo* addr = arg->addr->ai; VALUE (*ipaddr)(struct sockaddr*, socklen_t) = arg->ipaddr; struct addrinfo *ai; @@ -522,14 +536,15 @@ make_hostent_internal(struct hostent_arg https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L536 } VALUE -rsock_freeaddrinfo(struct addrinfo *addr) +rsock_freeaddrinfo(VALUE arg) { - freeaddrinfo(addr); + struct rb_addrinfo *addr = (struct rb_addrinfo *)arg; + rb_freeaddrinfo(addr); return Qnil; } VALUE -rsock_make_hostent(VALUE host, struct addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t)) +rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t)) { struct hostent_arg arg; @@ -639,12 +654,13 @@ rsock_addrinfo_new(struct sockaddr *addr https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L654 return a; } -static struct addrinfo * +static struct rb_addrinfo * call_getaddrinfo(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags, int socktype_hack) { - struct addrinfo hints, *res; + struct addrinfo hints; + struct rb_addrinfo *res; MEMZERO(&hints, struct addrinfo, 1); hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family); @@ -672,21 +688,21 @@ init_addrinfo_getaddrinfo(rb_addrinfo_t https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L688 VALUE family, VALUE socktype, VALUE protocol, VALUE flags, VALUE inspectnode, VALUE inspectservice) { - struct addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 1); + struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 1); VALUE canonname; - VALUE inspectname = rb_str_equal(node, inspectnode) ? Qnil : make_inspectname(inspectnode, inspectservice, res); + VALUE inspectname = rb_str_equal(node, inspectnode) ? Qnil : make_inspectname(inspectnode, inspectservice, res->ai); canonname = Qnil; - if (res->ai_canonname) { - canonname = rb_tainted_str_new_cstr(res->ai_canonname); + if (res->ai->ai_canonname) { + canonname = rb_tainted_str_new_cstr(res->ai->ai_canonname); OBJ_FREEZE(canonname); } - init_addrinfo(rai, res->ai_addr, res->ai_addrlen, + init_addrinfo(rai, res->ai->ai_addr, res->ai->ai_addrlen, NUM2INT(family), NUM2INT(socktype), NUM2INT(protocol), canonname, inspectname); - freeaddrinfo(res); + rb_freeaddrinfo(res); } static VALUE @@ -742,21 +758,22 @@ addrinfo_firstonly_new(VALUE node, VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L758 VALUE canonname; VALUE inspectname; - struct addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0); + struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0); - inspectname = make_inspectname(node, service, res); + inspectname = make_inspectname(node, service, res->ai); canonname = Qnil; - if (res->ai_canonname) { - canonname = rb_tainted_str_new_cstr(res->ai_canonname); + if (res->ai->ai_canonname) { + canonname = rb_tainted_str_new_cstr(res->ai->ai_canonname); OBJ_FREEZE(canonname); } - ret = rsock_addrinfo_new(res->ai_addr, res->ai_addrlen, - res->ai_family, res->ai_socktype, res->ai_protocol, + ret = rsock_addrinfo_new(res->ai->ai_addr, res->ai->ai_addrlen, + res->ai->ai_family, res->ai->ai_socktype, + res->ai->ai_protocol, canonname, inspectname); - freeaddrinfo(res); + rb_freeaddrinfo(res); return ret; } @@ -767,12 +784,12 @@ addrinfo_list_new(VALUE node, VALUE serv https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L784 struct addrinfo *r; VALUE inspectname; - struct addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0); + struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0); - inspectname = make_inspectname(node, service, res); + inspectname = make_inspectname(node, service, res->ai); ret = rb_ary_new(); - for (r = res; r; r = r->ai_next) { + for (r = res->ai; r; r = r->ai_next) { VALUE addr; VALUE canonname = Qnil; @@ -788,7 +805,7 @@ addrinfo_list_new(VALUE node, VALUE serv https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L805 rb_ary_push(ret, addr); } - freeaddrinfo(res); + rb_freeaddrinfo(res); return ret; } @@ -1513,7 +1530,7 @@ addrinfo_mload(VALUE self, VALUE ary) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1530 default: { VALUE pair = rb_convert_type(v, T_ARRAY, "Array", "to_ary"); - struct addrinfo *res; + struct rb_addrinfo *res; int flags = AI_NUMERICHOST; #ifdef AI_NUMERICSERV flags |= AI_NUMERICSERV; @@ -1522,8 +1539,8 @@ addrinfo_mload(VALUE self, VALUE ary) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1539 INT2NUM(pfamily), INT2NUM(socktype), INT2NUM(protocol), INT2NUM(flags), 1); - len = res->ai_addrlen; - memcpy(&ss, res->ai_addr, res->ai_addrlen); + len = res->ai->ai_addrlen; + memcpy(&ss, res->ai->ai_addr, res->ai->ai_addrlen); break; } } Index: ext/socket/ipsocket.c =================================================================== --- ext/socket/ipsocket.c (revision 45045) +++ ext/socket/ipsocket.c (revision 45046) @@ -15,7 +15,7 @@ struct inetsock_arg https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L15 VALUE sock; struct { VALUE host, serv; - struct addrinfo *res; + struct rb_addrinfo *res; } remote, local; int type; int fd; @@ -25,11 +25,11 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L25 inetsock_cleanup(struct inetsock_arg *arg) { if (arg->remote.res) { - freeaddrinfo(arg->remote.res); + rb_freeaddrinfo(arg->remote.res); arg->remote.res = 0; } if (arg->local.res) { - freeaddrinfo(arg->local.res); + rb_freeaddrinfo(arg->local.res); arg->local.res = 0; } if (arg->fd >= 0) { @@ -57,14 +57,14 @@ init_inetsock_internal(struct inetsock_a https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L57 } arg->fd = fd = -1; - for (res = arg->remote.res; res; res = res->ai_next) { + for (res = arg->remote.res->ai; res; res = res->ai_next) { #if !defined(INET6) && defined(AF_INET6) if (res->ai_family == AF_INET6) continue; #endif lres = NULL; if (arg->local.res) { - for (lres = arg->local.res; lres; lres = lres->ai_next) { + for (lres = arg->local.res->ai; lres; lres = lres->ai_next) { if (lres->ai_family == res->ai_family) break; } @@ -73,7 +73,7 @@ init_inetsock_internal(struct inetsock_a https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L73 continue; /* Use a different family local address if no choice, this * will cause EAFNOSUPPORT. */ - lres = arg->local.res; + lres = arg->local.res->ai; } } status = rsock_socket(res->ai_family,res->ai_socktype,res->ai_protocol); @@ -304,12 +304,12 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L304 ip_s_getaddress(VALUE obj, VALUE host) { union_sockaddr addr; - struct addrinfo *res = rsock_addrinfo(host, Qnil, SOCK_STREAM, 0); - socklen_t len = res->ai_addrlen; + struct rb_addrinfo *res = rsock_addrinfo(host, Qnil, SOCK_STREAM, 0); + socklen_t len = res->ai->ai_addrlen; /* just take the first one */ - memcpy(&addr, res->ai_addr, len); - freeaddrinfo(res); + memcpy(&addr, res->ai->ai_addr, len); + rb_freeaddrinfo(res); return rsock_make_ipaddr(&addr.addr, len); } Index: ext/socket/socket.c =================================================================== --- ext/socket/socket.c (revision 45045) +++ ext/socket/socket.c (revision 45046) @@ -1055,7 +1055,7 @@ sock_gethostname(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1055 #endif static VALUE -make_addrinfo(struct addrinfo *res0, int norevlookup) +make_addrinfo(struct rb_addrinfo *res0, int norevlookup) { VALUE base, ary; struct addrinfo *res; @@ -1064,7 +1064,7 @@ make_addrinfo(struct addrinfo *res0, int https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1064 rb_raise(rb_eSocket, "host not found"); } base = rb_ary_new(); - for (res = res0; res; res = res->ai_next) { + for (res = res0->ai; res; res = res->ai_next) { ary = rsock_ipaddr(res->ai_addr, res->ai_addrlen, norevlookup); if (res->ai_canonname) { RARRAY_PTR(ary)[2] = rb_str_new2(res->ai_canonname); @@ -1287,7 +1287,8 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1287 sock_s_getaddrinfo(int argc, VALUE *argv) { VALUE host, port, family, socktype, protocol, flags, ret, revlookup; - struct addrinfo hints, *res; + struct addrinfo hints; + struct rb_addrinfo *res; int norevlookup; rb_scan_args(argc, argv, "25", &host, &port, &family, &socktype, &protocol, &flags, &revlookup); @@ -1310,7 +1311,7 @@ sock_s_getaddrinfo(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1311 res = rsock_getaddrinfo(host, port, &hints, 0); ret = make_addrinfo(res, norevlookup); - freeaddrinfo(res); + rb_freeaddrinfo(res); return ret; } @@ -1343,7 +1344,8 @@ sock_s_getnameinfo(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1344 char *hptr, *pptr; char hbuf[1024], pbuf[1024]; int fl; - struct addrinfo hints, *res = NULL, *r; + struct rb_addrinfo *res = NULL; + struct addrinfo hints, *r; int error; union_sockaddr ss; struct sockaddr *sap; @@ -1428,8 +1430,8 @@ sock_s_getnameinfo(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1430 hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af); error = rb_getaddrinfo(hptr, pptr, &hints, &res); if (error) goto error_exit_addr; - sap = res->ai_addr; - salen = res->ai_addrlen; + sap = res->ai->ai_addr; + salen = res->ai->ai_addrlen; } else { rb_raise(rb_eTypeError, "expecting String or Array"); @@ -1440,7 +1442,7 @@ sock_s_getnameinfo(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1442 pbuf, sizeof(pbuf), fl); if (error) goto error_exit_name; if (res) { - for (r = res->ai_next; r; r = r->ai_next) { + for (r = res->ai->ai_next; r; r = r->ai_next) { char hbuf2[1024], pbuf2[1024]; sap = r->ai_addr; @@ -1449,20 +1451,20 @@ sock_s_getnameinfo(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1451 pbuf2, sizeof(pbuf2), fl); if (error) goto error_exit_name; if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) { - freeaddrinfo(res); + rb_freeaddrinfo(res); rb_raise(rb_eSocket, "sockaddr resolved to multiple nodename"); } } - freeaddrinfo(res); + rb_freeaddrinfo(res); } return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf)); error_exit_addr: - if (res) freeaddrinfo(res); + if (res) rb_freeaddrinfo(res); rsock_raise_socket_error("getaddrinfo", error); error_exit_name: - if (res) freeaddrinfo(res); + if (res) rb_freeaddrinfo(res); rsock_raise_socket_error("getnameinfo", error); UNREACHABLE; @ (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/