diff options
-rw-r--r-- | ChangeLog | 33 | ||||
-rw-r--r-- | ext/socket/init.c | 4 | ||||
-rw-r--r-- | ext/socket/ipsocket.c | 6 | ||||
-rw-r--r-- | ext/socket/raddrinfo.c | 34 | ||||
-rw-r--r-- | ext/socket/rubysocket.h | 4 | ||||
-rw-r--r-- | ext/socket/socket.c | 12 | ||||
-rw-r--r-- | ext/socket/tcpsocket.c | 2 |
7 files changed, 66 insertions, 29 deletions
@@ -1,3 +1,36 @@ +Thu Feb 14 20:11:23 2013 Tanaka Akira <akr@fsij.org> + + * ext/socket: always operate length of socket addess companion with + socket address. + + * ext/socket/rubysocket.h (rsock_make_ipaddr): add an argument for + socket address length. + (rsock_ipaddr): ditto. + + * ext/socket/ipsocket.c (ip_addr): pass length to rsock_ipaddr. + (ip_peeraddr): ditto. + (ip_s_getaddress): pass length to rsock_make_ipaddr. + + * ext/socket/socket.c (make_addrinfo): pass length to rsock_ipaddr. + (sock_s_getnameinfo): pass actual address length to rb_getnameinfo. + (sock_s_unpack_sockaddr_in): pass length to rsock_make_ipaddr. + + * ext/socket/init.c (rsock_s_recvfrom): pass length to rsock_ipaddr. + (rsock_s_recvfrom_nonblock): ditto. + + * ext/socket/tcpsocket.c (tcp_sockaddr): pass length to + rsock_make_ipaddr. + + * ext/socket/raddrinfo.c (make_ipaddr0): add an argument for socket + address length. pass the length to rb_getnameinfo. + (rsock_ipaddr): ditto. + (rsock_make_ipaddr): add an argument for socket address length. + pass the length to make_ipaddr0. + (make_inetaddr): pass length to make_ipaddr0. + a local variable renamed. + (host_str): a local variable renamed. + (port_str): ditto. + Thu Feb 14 14:31:43 2013 Eric Hodel <drbrain@segment7.net> * lib/net/http.rb: Removed OpenSSL dependency from Net::HTTP. diff --git a/ext/socket/init.c b/ext/socket/init.c index 4c1d17df98..ab3fcf2b89 100644 --- a/ext/socket/init.c +++ b/ext/socket/init.c @@ -155,7 +155,7 @@ rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from) } #endif if (arg.alen && arg.alen != sizeof(arg.buf)) /* OSX doesn't return a from result for connection-oriented sockets */ - return rb_assoc_new(str, rsock_ipaddr((struct sockaddr*)&arg.buf, fptr->mode & FMODE_NOREVLOOKUP)); + return rb_assoc_new(str, rsock_ipaddr((struct sockaddr*)&arg.buf, arg.alen, fptr->mode & FMODE_NOREVLOOKUP)); else return rb_assoc_new(str, Qnil); @@ -227,7 +227,7 @@ rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type case RECV_IP: if (alen && alen != sizeof(buf)) /* connection-oriented socket may not return a from result */ - addr = rsock_ipaddr((struct sockaddr*)&buf, fptr->mode & FMODE_NOREVLOOKUP); + addr = rsock_ipaddr((struct sockaddr*)&buf, alen, fptr->mode & FMODE_NOREVLOOKUP); break; case RECV_SOCKET: diff --git a/ext/socket/ipsocket.c b/ext/socket/ipsocket.c index b1085cd70c..bd14ba9d89 100644 --- a/ext/socket/ipsocket.c +++ b/ext/socket/ipsocket.c @@ -194,7 +194,7 @@ ip_addr(int argc, VALUE *argv, VALUE sock) norevlookup = fptr->mode & FMODE_NOREVLOOKUP; if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0) rb_sys_fail("getsockname(2)"); - return rsock_ipaddr((struct sockaddr*)&addr, norevlookup); + return rsock_ipaddr((struct sockaddr*)&addr, len, norevlookup); } /* @@ -235,7 +235,7 @@ ip_peeraddr(int argc, VALUE *argv, VALUE sock) norevlookup = fptr->mode & FMODE_NOREVLOOKUP; if (getpeername(fptr->fd, (struct sockaddr*)&addr, &len) < 0) rb_sys_fail("getpeername(2)"); - return rsock_ipaddr((struct sockaddr*)&addr, norevlookup); + return rsock_ipaddr((struct sockaddr*)&addr, len, norevlookup); } /* @@ -285,7 +285,7 @@ ip_s_getaddress(VALUE obj, VALUE host) memcpy(&addr, res->ai_addr, res->ai_addrlen); freeaddrinfo(res); - return rsock_make_ipaddr((struct sockaddr*)&addr); + return rsock_make_ipaddr((struct sockaddr*)&addr, res->ai_addrlen); } void diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index a730a1b4c4..82821730e7 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -229,27 +229,27 @@ rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, } static void -make_ipaddr0(struct sockaddr *addr, char *buf, size_t len) +make_ipaddr0(struct sockaddr *addr, socklen_t addrlen, char *buf, size_t buflen) { int error; - error = rb_getnameinfo(addr, SA_LEN(addr), buf, len, NULL, 0, NI_NUMERICHOST); + error = rb_getnameinfo(addr, addrlen, buf, buflen, NULL, 0, NI_NUMERICHOST); if (error) { rsock_raise_socket_error("getnameinfo", error); } } VALUE -rsock_make_ipaddr(struct sockaddr *addr) +rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen) { char hbuf[1024]; - make_ipaddr0(addr, hbuf, sizeof(hbuf)); + make_ipaddr0(addr, addrlen, hbuf, sizeof(hbuf)); return rb_str_new2(hbuf); } static void -make_inetaddr(unsigned int host, char *buf, size_t len) +make_inetaddr(unsigned int host, char *buf, size_t buflen) { struct sockaddr_in sin; @@ -257,7 +257,7 @@ make_inetaddr(unsigned int host, char *buf, size_t len) sin.sin_family = AF_INET; SET_SIN_LEN(&sin, sizeof(sin)); sin.sin_addr.s_addr = host; - make_ipaddr0((struct sockaddr*)&sin, buf, len); + make_ipaddr0((struct sockaddr*)&sin, sizeof(sin), buf, buflen); } static int @@ -276,7 +276,7 @@ str_is_number(const char *p) } static char* -host_str(VALUE host, char *hbuf, size_t len, int *flags_ptr) +host_str(VALUE host, char *hbuf, size_t hbuflen, int *flags_ptr) { if (NIL_P(host)) { return NULL; @@ -284,7 +284,7 @@ host_str(VALUE host, char *hbuf, size_t len, int *flags_ptr) else if (rb_obj_is_kind_of(host, rb_cInteger)) { unsigned int i = NUM2UINT(host); - make_inetaddr(htonl(i), hbuf, len); + make_inetaddr(htonl(i), hbuf, hbuflen); if (flags_ptr) *flags_ptr |= AI_NUMERICHOST; return hbuf; } @@ -294,14 +294,14 @@ host_str(VALUE host, char *hbuf, size_t len, int *flags_ptr) SafeStringValue(host); name = RSTRING_PTR(host); if (!name || *name == 0 || (name[0] == '<' && strcmp(name, "<any>") == 0)) { - make_inetaddr(INADDR_ANY, hbuf, len); + make_inetaddr(INADDR_ANY, hbuf, hbuflen); if (flags_ptr) *flags_ptr |= AI_NUMERICHOST; } else if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) { - make_inetaddr(INADDR_BROADCAST, hbuf, len); + make_inetaddr(INADDR_BROADCAST, hbuf, hbuflen); if (flags_ptr) *flags_ptr |= AI_NUMERICHOST; } - else if (strlen(name) >= len) { + else if (strlen(name) >= hbuflen) { rb_raise(rb_eArgError, "hostname too long (%"PRIuSIZE")", strlen(name)); } @@ -313,13 +313,13 @@ host_str(VALUE host, char *hbuf, size_t len, int *flags_ptr) } static char* -port_str(VALUE port, char *pbuf, size_t len, int *flags_ptr) +port_str(VALUE port, char *pbuf, size_t pbuflen, int *flags_ptr) { if (NIL_P(port)) { return 0; } else if (FIXNUM_P(port)) { - snprintf(pbuf, len, "%ld", FIX2LONG(port)); + snprintf(pbuf, pbuflen, "%ld", FIX2LONG(port)); #ifdef AI_NUMERICSERV if (flags_ptr) *flags_ptr |= AI_NUMERICSERV; #endif @@ -330,7 +330,7 @@ port_str(VALUE port, char *pbuf, size_t len, int *flags_ptr) SafeStringValue(port); serv = RSTRING_PTR(port); - if (strlen(serv) >= len) { + if (strlen(serv) >= pbuflen) { rb_raise(rb_eArgError, "service name too long (%"PRIuSIZE")", strlen(serv)); } @@ -380,7 +380,7 @@ rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags) } VALUE -rsock_ipaddr(struct sockaddr *sockaddr, int norevlookup) +rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup) { VALUE family, port, addr1, addr2; VALUE ary; @@ -399,13 +399,13 @@ rsock_ipaddr(struct sockaddr *sockaddr, int norevlookup) addr1 = Qnil; if (!norevlookup) { - error = rb_getnameinfo(sockaddr, SA_LEN(sockaddr), hbuf, sizeof(hbuf), + error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf), NULL, 0, 0); if (! error) { addr1 = rb_str_new2(hbuf); } } - error = rb_getnameinfo(sockaddr, SA_LEN(sockaddr), hbuf, sizeof(hbuf), + error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV); if (error) { rsock_raise_socket_error("getnameinfo", error); diff --git a/ext/socket/rubysocket.h b/ext/socket/rubysocket.h index a0fb71ba91..4ed024208e 100644 --- a/ext/socket/rubysocket.h +++ b/ext/socket/rubysocket.h @@ -235,8 +235,8 @@ VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len); VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname); -VALUE rsock_make_ipaddr(struct sockaddr *addr); -VALUE rsock_ipaddr(struct sockaddr *sockaddr, int norevlookup); +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 *, size_t)); int rsock_revlookup_flag(VALUE revlookup, int *norevlookup); diff --git a/ext/socket/socket.c b/ext/socket/socket.c index af9c182380..4473234447 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -937,7 +937,7 @@ make_addrinfo(struct addrinfo *res0, int norevlookup) } base = rb_ary_new(); for (res = res0; res; res = res->ai_next) { - ary = rsock_ipaddr(res->ai_addr, norevlookup); + ary = rsock_ipaddr(res->ai_addr, res->ai_addrlen, norevlookup); if (res->ai_canonname) { RARRAY_PTR(ary)[2] = rb_str_new2(res->ai_canonname); } @@ -1219,6 +1219,7 @@ sock_s_getnameinfo(int argc, VALUE *argv) int error; struct sockaddr_storage ss; struct sockaddr *sap; + socklen_t salen; sa = flags = Qnil; rb_scan_args(argc, argv, "11", &sa, &flags); @@ -1238,6 +1239,7 @@ sock_s_getnameinfo(int argc, VALUE *argv) rb_raise(rb_eTypeError, "sockaddr size differs - should not happen"); } sap = (struct sockaddr*)&ss; + salen = RSTRING_LEN(sa); goto call_nameinfo; } tmp = rb_check_array_type(sa); @@ -1299,13 +1301,14 @@ sock_s_getnameinfo(int argc, VALUE *argv) error = rb_getaddrinfo(hptr, pptr, &hints, &res); if (error) goto error_exit_addr; sap = res->ai_addr; + salen = res->ai_addrlen; } else { rb_raise(rb_eTypeError, "expecting String or Array"); } call_nameinfo: - error = rb_getnameinfo(sap, SA_LEN(sap), hbuf, sizeof(hbuf), + error = rb_getnameinfo(sap, salen, hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), fl); if (error) goto error_exit_name; if (res) { @@ -1313,7 +1316,8 @@ sock_s_getnameinfo(int argc, VALUE *argv) char hbuf2[1024], pbuf2[1024]; sap = r->ai_addr; - error = rb_getnameinfo(sap, SA_LEN(sap), hbuf2, sizeof(hbuf2), + salen = r->ai_addrlen; + error = rb_getnameinfo(sap, salen, hbuf2, sizeof(hbuf2), pbuf2, sizeof(pbuf2), fl); if (error) goto error_exit_name; if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) { @@ -1399,7 +1403,7 @@ sock_s_unpack_sockaddr_in(VALUE self, VALUE addr) rb_raise(rb_eArgError, "not an AF_INET sockaddr"); #endif } - host = rsock_make_ipaddr((struct sockaddr*)sockaddr); + host = rsock_make_ipaddr((struct sockaddr*)sockaddr, RSTRING_LEN(addr)); OBJ_INFECT(host, addr); return rb_assoc_new(INT2NUM(ntohs(sockaddr->sin_port)), host); } diff --git a/ext/socket/tcpsocket.c b/ext/socket/tcpsocket.c index 7eb6fc7aa2..608156f1b2 100644 --- a/ext/socket/tcpsocket.c +++ b/ext/socket/tcpsocket.c @@ -34,7 +34,7 @@ tcp_init(int argc, VALUE *argv, VALUE sock) static VALUE tcp_sockaddr(struct sockaddr *addr, size_t len) { - return rsock_make_ipaddr(addr); + return rsock_make_ipaddr(addr, len); } /* |