summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/socket/mkconstants.rb35
-rw-r--r--ext/socket/socket.c48
2 files changed, 36 insertions, 47 deletions
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb
index 257b315c192..64743d723ba 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 fe972ce5f75..8b306197338 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);