summaryrefslogtreecommitdiff
path: root/ext/socket
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-28 09:32:20 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-11-28 09:32:20 +0000
commit46e431e3eb410a6168cd885ac0a327a4b74c9fde (patch)
tree28f3ffc3f4421aacea6f41fa223add37daf1c2e5 /ext/socket
parent3c6b1bfbe42623cfadc4a8ba7d4e246d023ab422 (diff)
* ext/socket/socket.c (sock_s_getaddrinfo): refactored to remove
code duplication regarding getaddrinfo. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20387 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket')
-rw-r--r--ext/socket/socket.c55
1 files changed, 18 insertions, 37 deletions
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index 09abc88c18..fc7388083e 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");
@@ -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);