* 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.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39239 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-02-14 11:28:49 +00:00
Родитель 64db9dcbd5
Коммит 18a8046d1c
7 изменённых файлов: 66 добавлений и 29 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -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:

Просмотреть файл

@ -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

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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);
}
/*