ruby-changes:8888
From: yugui <ko1@a...>
Date: Sun, 30 Nov 2008 18:26:11 +0900 (JST)
Subject: [ruby-changes:8888] Ruby:r20424 (ruby_1_9_1): merges r20387 and r20390 from trunk into ruby_1_9_1.
yugui 2008-11-30 18:24:58 +0900 (Sun, 30 Nov 2008) New Revision: 20424 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20424 Log: merges r20387 and r20390 from trunk into ruby_1_9_1. * ext/socket/socket.c (sock_s_getaddrinfo): refactored to remove code duplication regarding getaddrinfo. Modified files: branches/ruby_1_9_1/ChangeLog branches/ruby_1_9_1/ext/socket/socket.c Index: ruby_1_9_1/ChangeLog =================================================================== --- ruby_1_9_1/ChangeLog (revision 20423) +++ ruby_1_9_1/ChangeLog (revision 20424) @@ -1,3 +1,11 @@ +Fri Nov 28 18:31:21 2008 Yukihiro Matsumoto <matz@r...> + + * ext/socket/socket.c (sock_s_getaddrinfo): refactored to remove + code duplication regarding getaddrinfo. + + * ext/socket/socket.c (sock_getaddrinfo): should have updated for + Mac OS X. a patch from Shumpei Akai in [ruby-dev:37234] + Thu Nov 27 21:41:29 2008 Tadayoshi Funaba <tadf@d...> * strftime.c (rb_strftime): should add padding for %%. Index: ruby_1_9_1/ext/socket/socket.c =================================================================== --- ruby_1_9_1/ext/socket/socket.c (revision 20423) +++ ruby_1_9_1/ext/socket/socket.c (revision 20424) @@ -926,9 +926,8 @@ #endif static struct addrinfo* -sock_addrinfo(VALUE host, VALUE port, int socktype, int flags) +sock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints) { - struct addrinfo hints; struct addrinfo* res = NULL; char *hostp, *portp; int error; @@ -937,15 +936,11 @@ hostp = host_str(host, hbuf, sizeof(hbuf)); portp = port_str(port, pbuf, sizeof(pbuf)); - if (socktype == 0 && flags == 0 && str_isnumber(portp)) { - socktype = SOCK_DGRAM; + if (hints->ai_socktype == 0 && hints->ai_flags == 0 && str_isnumber(portp)) { + hints->ai_socktype = SOCK_DGRAM; } - MEMZERO(&hints, struct addrinfo, 1); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = socktype; - hints.ai_flags = flags; - error = getaddrinfo(hostp, portp, &hints, &res); + error = getaddrinfo(hostp, portp, hints, &res); if (error) { if (hostp && hostp[strlen(hostp)-1] == '\n') { rb_raise(rb_eSocket, "newline at the end of hostname"); @@ -958,7 +953,7 @@ struct addrinfo *r; r = res; while (r) { - if (! r->ai_socktype) r->ai_socktype = hints.ai_socktype; + if (! r->ai_socktype) r->ai_socktype = hints->ai_socktype; if (! r->ai_protocol) { if (r->ai_socktype == SOCK_DGRAM) { r->ai_protocol = IPPROTO_UDP; @@ -974,6 +969,18 @@ return res; } +static struct addrinfo* +sock_addrinfo(VALUE host, VALUE port, int socktype, int flags) +{ + struct addrinfo hints; + + MEMZERO(&hints, struct addrinfo, 1); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = socktype; + hints.ai_flags = flags; + return sock_getaddrinfo(host, port, &hints); +} + static VALUE ipaddr(struct sockaddr *sockaddr, int norevlookup) { @@ -3271,33 +3278,10 @@ sock_s_getaddrinfo(int argc, VALUE *argv) { VALUE host, port, family, socktype, protocol, flags, ret; - char hbuf[1024], pbuf[1024]; - char *hptr, *pptr, *ap; + char *ap; struct addrinfo hints, *res; - int error; - host = port = family = socktype = protocol = flags = Qnil; rb_scan_args(argc, argv, "24", &host, &port, &family, &socktype, &protocol, &flags); - if (NIL_P(host)) { - hptr = NULL; - } - else { - strncpy(hbuf, StringValuePtr(host), sizeof(hbuf)); - hbuf[sizeof(hbuf) - 1] = '\0'; - hptr = hbuf; - } - if (NIL_P(port)) { - pptr = NULL; - } - else if (FIXNUM_P(port)) { - snprintf(pbuf, sizeof(pbuf), "%ld", FIX2LONG(port)); - pptr = pbuf; - } - else { - strncpy(pbuf, StringValuePtr(port), sizeof(pbuf)); - pbuf[sizeof(pbuf) - 1] = '\0'; - pptr = pbuf; - } MEMZERO(&hints, struct addrinfo, 1); if (NIL_P(family)) { @@ -3326,10 +3310,7 @@ if (!NIL_P(flags)) { hints.ai_flags = NUM2INT(flags); } - error = getaddrinfo(hptr, pptr, &hints, &res); - if (error) { - raise_socket_error("getaddrinfo", error); - } + res = sock_getaddrinfo(host, port, &hints); ret = make_addrinfo(res); freeaddrinfo(res); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/