summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--ext/socket/mkconstants.rb35
-rw-r--r--ext/socket/socket.c48
-rw-r--r--test/socket/test_socket.rb13
-rw-r--r--test/socket/test_unix.rb14
5 files changed, 69 insertions, 47 deletions
diff --git a/ChangeLog b/ChangeLog
index 0d0a14e0fd..89b7b381c5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Thu Jan 1 16:48:07 2009 Tanaka Akira <akr@fsij.org>
+
+ * ext/socket/mkconstants.rb: generate family_to_int().
+
+ * ext/socket/socket.c (setup_domain_and_type): use family_to_int.
+
Thu Jan 1 15:08:46 2009 Yuki Sonoda (Yugui) <yugui@yugui.jp>
* tool/flie2lastrev.rb: supports git repositories which are cloned
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb
index 257b315c19..64743d723b 100644
--- a/ext/socket/mkconstants.rb
+++ b/ext/socket/mkconstants.rb
@@ -34,10 +34,15 @@ result = ''
# workaround for NetBSD, OpenBSD and etc.
result << "#define pseudo_AF_FTIP pseudo_AF_RTIP\n"
-def each_data
- DATA.each_line {|s|
- name, default_value = s.scan(/\S+/)
- next unless name && name[0] != ?#
+DEFS = []
+DATA.each_line {|s|
+ name, default_value = s.scan(/\S+/)
+ next unless name && name[0] != ?#
+ DEFS << [name, default_value]
+}
+
+def each_const
+ DEFS.each {|name, default_value|
if name =~ /\AINADDR_/
define = "sock_define_uconst"
else
@@ -52,11 +57,18 @@ def each_data
}
end
+def each_name(pat)
+ DEFS.each {|name, default_value|
+ next if pat !~ name
+ yield name
+ }
+end
+
result << ERB.new(<<'EOS', nil, '%').result(binding)
static void
init_constants(VALUE mConst)
{
-% each_data {|guard, define, name, default_value|
+% each_const {|guard, define, name, default_value|
% if guard
#if <%=guard%>
% end
@@ -73,6 +85,19 @@ init_constants(VALUE mConst)
% }
}
+
+static int
+family_to_int(char *str, int len)
+{
+% each_name(/\A[AP]F_/) {|name|
+#ifdef <%=name%>
+% size = name.bytesize
+ if (len == <%=size%> && memcmp(str, <%=c_str name%>, <%=size%>) == 0) return <%=name%>;
+#endif
+% }
+ return -1;
+}
+
EOS
if opt_o
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index fe972ce5f7..8b30619733 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -2280,6 +2280,8 @@ unix_peeraddr(VALUE sock)
}
#endif
+static int family_to_int(char *str, int len);
+
static void
setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv)
{
@@ -2288,51 +2290,13 @@ setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv)
tmp = rb_check_string_type(domain);
if (!NIL_P(tmp)) {
+ int family;
domain = tmp;
rb_check_safe_obj(domain);
- ptr = RSTRING_PTR(domain);
- if (strcmp(ptr, "AF_INET") == 0)
- *dv = AF_INET;
-#ifdef AF_UNIX
- else if (strcmp(ptr, "AF_UNIX") == 0)
- *dv = AF_UNIX;
-#endif
-#ifdef AF_ISO
- else if (strcmp(ptr, "AF_ISO") == 0)
- *dv = AF_ISO;
-#endif
-#ifdef AF_NS
- else if (strcmp(ptr, "AF_NS") == 0)
- *dv = AF_NS;
-#endif
-#ifdef AF_IMPLINK
- else if (strcmp(ptr, "AF_IMPLINK") == 0)
- *dv = AF_IMPLINK;
-#endif
-#ifdef PF_INET
- else if (strcmp(ptr, "PF_INET") == 0)
- *dv = PF_INET;
-#endif
-#ifdef PF_UNIX
- else if (strcmp(ptr, "PF_UNIX") == 0)
- *dv = PF_UNIX;
-#endif
-#ifdef PF_IMPLINK
- else if (strcmp(ptr, "PF_IMPLINK") == 0)
- *dv = PF_IMPLINK;
- else if (strcmp(ptr, "AF_IMPLINK") == 0)
- *dv = AF_IMPLINK;
-#endif
-#ifdef PF_AX25
- else if (strcmp(ptr, "PF_AX25") == 0)
- *dv = PF_AX25;
-#endif
-#ifdef PF_IPX
- else if (strcmp(ptr, "PF_IPX") == 0)
- *dv = PF_IPX;
-#endif
- else
+ family = family_to_int(RSTRING_PTR(domain), RSTRING_LEN(domain));
+ if (family == -1)
rb_raise(rb_eSocket, "unknown socket domain %s", ptr);
+ *dv = family;
}
else {
*dv = NUM2INT(domain);
diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb
index 90f1037db8..3133590ab1 100644
--- a/test/socket/test_socket.rb
+++ b/test/socket/test_socket.rb
@@ -97,4 +97,17 @@ class TestSocket < Test::Unit::TestCase
c.close if c
IO.for_fd(fd).close if fd
end
+
+ def test_initialize
+ Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) {|s|
+ addr = s.getsockname
+ assert_nothing_raised { Socket.unpack_sockaddr_in(addr) }
+ assert_raise(ArgumentError) { Socket.unpack_sockaddr_un(addr) }
+ }
+ Socket.open("AF_INET", "SOCK_STREAM", 0) {|s|
+ addr = s.getsockname
+ assert_nothing_raised { Socket.unpack_sockaddr_in(addr) }
+ assert_raise(ArgumentError) { Socket.unpack_sockaddr_un(addr) }
+ }
+ end
end if defined?(Socket)
diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb
index 5b041f1733..853fb28ef5 100644
--- a/test/socket/test_unix.rb
+++ b/test/socket/test_unix.rb
@@ -166,4 +166,18 @@ class TestUNIXSocket < Test::Unit::TestCase
assert_kind_of(UNIXSocket, pair[1])
end
+ def test_initialize
+ Socket.open(Socket::AF_UNIX, Socket::SOCK_STREAM, 0) {|s|
+ addr = s.getsockname
+ assert_nothing_raised { Socket.unpack_sockaddr_un(addr) }
+ assert_raise(ArgumentError) { Socket.unpack_sockaddr_in(addr) }
+ }
+ Socket.open("AF_UNIX", "SOCK_STREAM", 0) {|s|
+ addr = s.getsockname
+ assert_nothing_raised { Socket.unpack_sockaddr_un(addr) }
+ assert_raise(ArgumentError) { Socket.unpack_sockaddr_in(addr) }
+ }
+ end
+
+
end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM