summaryrefslogtreecommitdiff
path: root/ext/socket
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-01 16:16:08 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-01 16:16:08 +0000
commit59556a0849afb29c699261a9a837e72c590686bc (patch)
treeb86bb7e4f681a3c08dac949067f36dc5a70e09c5 /ext/socket
parentc6bbf28a9f139a5fbf34e54673328d3d58e8e397 (diff)
* 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
Diffstat (limited to 'ext/socket')
-rw-r--r--ext/socket/mkconstants.rb6
-rw-r--r--ext/socket/socket.c30
2 files changed, 34 insertions, 2 deletions
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb
index d3eac7f3f1..a4f7569f88 100644
--- a/ext/socket/mkconstants.rb
+++ b/ext/socket/mkconstants.rb
@@ -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)
{
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index 7b7aa416ac..0c8b0a6296 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -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);