зеркало из https://github.com/github/ruby.git
* ext/socket/socket.c (level_arg): defined.
(bsock_setsockopt): use level_arg. (bsock_getsockopt): ditto. * ext/socket/mkconstants.rb: generate level_to_int. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21258 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
c6bbf28a9f
Коммит
59556a0849
|
@ -1,3 +1,11 @@
|
|||
Fri Jan 2 01:15:11 2009 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* ext/socket/socket.c (level_arg): defined.
|
||||
(bsock_setsockopt): use level_arg.
|
||||
(bsock_getsockopt): ditto.
|
||||
|
||||
* ext/socket/mkconstants.rb: generate level_to_int.
|
||||
|
||||
Fri Jan 2 00:58:40 2009 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* ext/socket/socket.c (family_arg): accept symbols as well.
|
||||
|
|
|
@ -151,6 +151,12 @@ socktype_to_int(char *str, int len, int *valp)
|
|||
<%= gen_name_to_int("str", "len", "valp", /\ASOCK_/) %>
|
||||
}
|
||||
|
||||
static int
|
||||
level_to_int(char *str, int len, int *valp)
|
||||
{
|
||||
<%= gen_name_to_int("str", "len", "valp", /\A(SOL_SOCKET\z|IPPROTO_)/) %>
|
||||
}
|
||||
|
||||
static char *
|
||||
family_to_str(int val)
|
||||
{
|
||||
|
|
|
@ -294,6 +294,32 @@ socktype_arg(VALUE type)
|
|||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
level_arg(VALUE level)
|
||||
{
|
||||
/* convert SOL_SOCKET, IPPROTO_TCP, etc. */
|
||||
VALUE tmp;
|
||||
char *ptr;
|
||||
int ret;
|
||||
|
||||
if (SYMBOL_P(level)) {
|
||||
level = rb_sym_to_s(level);
|
||||
goto str;
|
||||
}
|
||||
else if (!NIL_P(tmp = rb_check_string_type(level))) {
|
||||
level = tmp;
|
||||
str:
|
||||
rb_check_safe_obj(level);
|
||||
ptr = RSTRING_PTR(level);
|
||||
if (level_to_int(ptr, RSTRING_LEN(level), &ret) == -1)
|
||||
rb_raise(rb_eSocket, "unknown protocol level %s", ptr);
|
||||
}
|
||||
else {
|
||||
ret = NUM2INT(level);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
init_sock(VALUE sock, int fd)
|
||||
|
@ -440,7 +466,7 @@ bsock_setsockopt(VALUE sock, VALUE lev, VALUE optname, VALUE val)
|
|||
int vlen;
|
||||
|
||||
rb_secure(2);
|
||||
level = NUM2INT(lev);
|
||||
level = level_arg(lev);
|
||||
option = NUM2INT(optname);
|
||||
|
||||
switch (TYPE(val)) {
|
||||
|
@ -520,7 +546,7 @@ bsock_getsockopt(VALUE sock, VALUE lev, VALUE optname)
|
|||
char *buf;
|
||||
rb_io_t *fptr;
|
||||
|
||||
level = NUM2INT(lev);
|
||||
level = level_arg(lev);
|
||||
option = NUM2INT(optname);
|
||||
len = 256;
|
||||
buf = ALLOCA_N(char,len);
|
||||
|
|
|
@ -16,6 +16,10 @@ class TestBasicSocket < Test::Unit::TestCase
|
|||
inet_stream do |s|
|
||||
n = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_TYPE)
|
||||
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
||||
n = s.getsockopt("SOL_SOCKET", Socket::SO_TYPE)
|
||||
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
||||
n = s.getsockopt(:SOL_SOCKET, Socket::SO_TYPE)
|
||||
assert_equal([Socket::SOCK_STREAM].pack("i"), n)
|
||||
n = s.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR)
|
||||
assert_equal([0].pack("i"), n)
|
||||
val = Object.new
|
||||
|
|
Загрузка…
Ссылка в новой задаче