summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-30 09:24:58 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-30 09:24:58 +0000
commitf3f7dffdb4b0dd4eb183a6c1067caa515640f04f (patch)
treee30a62b1ee0fdefbe27494ac685f37640f32db95 /ext
parent7367da56c0e018f8194dd92535f8db9d1983f6d7 (diff)
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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@20424 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/socket/socket.c57
1 files changed, 19 insertions, 38 deletions
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index 09abc88c18..2f7a07f864 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -926,9 +926,8 @@ port_str(VALUE port, char *pbuf, size_t len)
#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 @@ sock_addrinfo(VALUE host, VALUE port, int socktype, int flags)
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 @@ sock_addrinfo(VALUE host, VALUE port, int socktype, int flags)
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 @@ sock_addrinfo(VALUE host, VALUE port, int socktype, int flags)
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 @@ static VALUE
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 @@ sock_s_getaddrinfo(int argc, VALUE *argv)
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);