summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/socket
diff options
context:
space:
mode:
authorJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-16 18:49:26 +0900
committerJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-30 00:39:06 +0900
commitfcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch)
tree64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/ext/socket
Fresh start
Diffstat (limited to 'jni/ruby/ext/socket')
-rw-r--r--jni/ruby/ext/socket/.document17
-rw-r--r--jni/ruby/ext/socket/Makefile571
-rw-r--r--jni/ruby/ext/socket/addrinfo.h189
-rw-r--r--jni/ruby/ext/socket/ancdata.c1842
-rw-r--r--jni/ruby/ext/socket/basicsocket.c767
-rw-r--r--jni/ruby/ext/socket/constants.c145
-rw-r--r--jni/ruby/ext/socket/constdefs.c6852
-rw-r--r--jni/ruby/ext/socket/constdefs.h2254
-rw-r--r--jni/ruby/ext/socket/depend295
-rw-r--r--jni/ruby/ext/socket/extconf.h127
-rw-r--r--jni/ruby/ext/socket/extconf.rb695
-rw-r--r--jni/ruby/ext/socket/getaddrinfo.c679
-rw-r--r--jni/ruby/ext/socket/getnameinfo.c261
-rw-r--r--jni/ruby/ext/socket/ifaddr.c459
-rw-r--r--jni/ruby/ext/socket/init.c615
-rw-r--r--jni/ruby/ext/socket/ipsocket.c338
-rw-r--r--jni/ruby/ext/socket/lib/socket.rb871
-rw-r--r--jni/ruby/ext/socket/mkconstants.rb804
-rw-r--r--jni/ruby/ext/socket/option.c1473
-rw-r--r--jni/ruby/ext/socket/raddrinfo.c2575
-rw-r--r--jni/ruby/ext/socket/rubysocket.h415
-rw-r--r--jni/ruby/ext/socket/socket.c2182
-rw-r--r--jni/ruby/ext/socket/sockport.h116
-rw-r--r--jni/ruby/ext/socket/sockssocket.c68
-rw-r--r--jni/ruby/ext/socket/tcpserver.c178
-rw-r--r--jni/ruby/ext/socket/tcpsocket.c82
-rw-r--r--jni/ruby/ext/socket/udpsocket.c266
-rw-r--r--jni/ruby/ext/socket/unixserver.c155
-rw-r--r--jni/ruby/ext/socket/unixsocket.c534
29 files changed, 25825 insertions, 0 deletions
diff --git a/jni/ruby/ext/socket/.document b/jni/ruby/ext/socket/.document
new file mode 100644
index 0000000..53cfac0
--- /dev/null
+++ b/jni/ruby/ext/socket/.document
@@ -0,0 +1,17 @@
+ancdata.c
+basicsocket.c
+constants.c
+constdefs.c
+ifaddr.c
+init.c
+ipsocket.c
+option.c
+raddrinfo.c
+socket.c
+sockssocket.c
+tcpserver.c
+tcpsocket.c
+udpsocket.c
+unixserver.c
+unixsocket.c
+lib
diff --git a/jni/ruby/ext/socket/Makefile b/jni/ruby/ext/socket/Makefile
new file mode 100644
index 0000000..4d4d6eb
--- /dev/null
+++ b/jni/ruby/ext/socket/Makefile
@@ -0,0 +1,571 @@
+
+SHELL = /bin/sh
+
+# V=0 quiet, V=1 verbose. other values don't work.
+V = 0
+Q1 = $(V:1=)
+Q = $(Q1:0=@)
+ECHO1 = $(V:1=@:)
+ECHO = $(ECHO1:0=@echo)
+NULLCMD = :
+
+#### Start of system configuration section. ####
+top_srcdir = $(topdir)/.
+srcdir = $(top_srcdir)/ext/socket
+topdir = ../..
+hdrdir = $(top_srcdir)/include
+arch_hdrdir = $(extout)/include/$(arch)
+PATH_SEPARATOR = :
+VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
+RUBYLIB =
+RUBYOPT = -
+prefix = $(DESTDIR)/usr/local
+rubysitearchprefix = $(rubylibprefix)/$(sitearch)
+rubyarchprefix = $(rubylibprefix)/$(arch)
+rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
+exec_prefix = $(prefix)
+vendorarchhdrdir = $(vendorhdrdir)/$(sitearch)
+sitearchhdrdir = $(sitehdrdir)/$(sitearch)
+rubyarchhdrdir = $(rubyhdrdir)/$(arch)
+vendorhdrdir = $(rubyhdrdir)/vendor_ruby
+sitehdrdir = $(rubyhdrdir)/site_ruby
+rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME)
+vendorarchdir = $(vendorlibdir)/$(sitearch)
+vendorlibdir = $(vendordir)/$(ruby_version)
+vendordir = $(rubylibprefix)/vendor_ruby
+sitearchdir = $(sitelibdir)/$(sitearch)
+sitelibdir = $(sitedir)/$(ruby_version)
+sitedir = $(rubylibprefix)/site_ruby
+rubyarchdir = $(rubylibdir)/$(arch)
+rubylibdir = $(rubylibprefix)/$(ruby_version)
+sitearchincludedir = $(includedir)/$(sitearch)
+archincludedir = $(includedir)/$(arch)
+sitearchlibdir = $(libdir)/$(sitearch)
+archlibdir = $(libdir)/$(arch)
+ridir = $(datarootdir)/$(RI_BASE_NAME)
+mandir = $(datarootdir)/man
+localedir = $(datarootdir)/locale
+libdir = $(exec_prefix)/lib
+psdir = $(docdir)
+pdfdir = $(docdir)
+dvidir = $(docdir)
+htmldir = $(docdir)
+infodir = $(datarootdir)/info
+docdir = $(datarootdir)/doc/$(PACKAGE)
+oldincludedir = $(DESTDIR)/usr/include
+includedir = $(prefix)/include
+localstatedir = $(prefix)/var
+sharedstatedir = $(prefix)/com
+sysconfdir = $(prefix)/etc
+datadir = $(datarootdir)
+datarootdir = $(prefix)/share
+libexecdir = $(exec_prefix)/libexec
+sbindir = $(exec_prefix)/sbin
+bindir = $(exec_prefix)/bin
+archdir = $(rubyarchdir)
+
+
+CC = gcc
+CXX = g++
+LIBRUBY = $(LIBRUBY_SO)
+LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
+LIBRUBYARG_SHARED = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)
+LIBRUBYARG_STATIC = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static
+empty =
+OUTFLAG = -o $(empty)
+COUTFLAG = -o $(empty)
+
+RUBY_EXTCONF_H = extconf.h
+cflags = $(optflags) $(debugflags) $(warnflags)
+optflags = -O3 -fno-fast-math
+debugflags = -ggdb3
+warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat
+CCDLFLAGS = -fPIC
+CFLAGS = $(CCDLFLAGS) $(cflags) -fPIC $(ARCH_FLAG)
+INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir) -I$(srcdir) -I$(topdir) -I$(top_srcdir)
+DEFS =
+CPPFLAGS = -DRUBY_EXTCONF_H=\"$(RUBY_EXTCONF_H)\" $(DEFS) $(cppflags)
+CXXFLAGS = $(CCDLFLAGS) $(cxxflags) $(ARCH_FLAG)
+ldflags = -L. -fstack-protector -rdynamic -Wl,-export-dynamic
+dldflags =
+ARCH_FLAG =
+DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
+LDSHARED = $(CC) -shared
+LDSHAREDXX = $(CXX) -shared
+AR = ar
+EXEEXT =
+
+RUBY_INSTALL_NAME = $(RUBY_BASE_NAME)
+RUBY_SO_NAME = ruby
+RUBYW_INSTALL_NAME =
+RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
+RUBYW_BASE_NAME = rubyw
+RUBY_BASE_NAME = ruby
+
+arch = x86_64-linux
+sitearch = $(arch)
+ruby_version = 2.2.0
+ruby = $(topdir)/miniruby -I'$(topdir)' -I'$(top_srcdir)/lib' -I'$(extout)/$(arch)' -I'$(extout)/common'
+RUBY = $(ruby)
+ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h $(RUBY_EXTCONF_H)
+
+RM = rm -f
+RM_RF = $(RUBY) -run -e rm -- -rf
+RMDIRS = rmdir --ignore-fail-on-non-empty -p
+MAKEDIRS = /bin/mkdir -p
+INSTALL = /usr/bin/install -c
+INSTALL_PROG = $(INSTALL) -m 0755
+INSTALL_DATA = $(INSTALL) -m 644
+COPY = cp
+TOUCH = exit >
+
+#### End of system configuration section. ####
+
+preload =
+
+libpath = . $(topdir)
+LIBPATH = -L. -L$(topdir)
+DEFFILE =
+
+CLEANFILES = mkmf.log
+DISTCLEANFILES = constants.h constdefs.*
+DISTCLEANDIRS =
+
+extout = $(topdir)/.ext
+extout_prefix = $(extout)$(target_prefix)/
+target_prefix =
+LOCAL_LIBS =
+LIBS = $(LIBRUBYARG_SHARED) -lpthread -lgmp -ldl -lcrypt -lm -lc
+ORIG_SRCS = unixsocket.c constdefs.c basicsocket.c getaddrinfo.c sockssocket.c option.c tcpserver.c ifaddr.c constants.c socket.c init.c getnameinfo.c udpsocket.c ipsocket.c ancdata.c unixserver.c tcpsocket.c raddrinfo.c
+SRCS = $(ORIG_SRCS) init.c constants.c basicsocket.c socket.c ipsocket.c tcpsocket.c tcpserver.c sockssocket.c udpsocket.c unixsocket.c unixserver.c option.c ancdata.c raddrinfo.c ifaddr.c
+OBJS = init.o constants.o basicsocket.o socket.o ipsocket.o tcpsocket.o tcpserver.o sockssocket.o udpsocket.o unixsocket.o unixserver.o option.o ancdata.o raddrinfo.o ifaddr.o
+HDRS = $(srcdir)/addrinfo.h $(srcdir)/sockport.h $(srcdir)/extconf.h $(srcdir)/constdefs.h $(srcdir)/rubysocket.h
+TARGET = socket
+TARGET_NAME = socket
+TARGET_ENTRY = Init_$(TARGET_NAME)
+DLLIB = $(TARGET).so
+EXTSTATIC =
+STATIC_LIB =
+
+TIMESTAMP_DIR = $(extout)/.timestamp
+BINDIR = $(extout)/bin
+RUBYCOMMONDIR = $(extout)/common
+RUBYLIBDIR = $(RUBYCOMMONDIR)$(target_prefix)
+RUBYARCHDIR = $(extout)/$(arch)$(target_prefix)
+HDRDIR = $(extout)/include/ruby$(target_prefix)
+ARCHHDRDIR = $(extout)/include/$(arch)/ruby$(target_prefix)
+
+TARGET_SO = $(RUBYARCHDIR)/$(DLLIB)
+CLEANLIBS = $(RUBYARCHDIR)/$(TARGET).so
+CLEANOBJS = *.o *.bak
+
+all: install
+static: all
+.PHONY: all install static install-so install-rb
+.PHONY: clean clean-so clean-static clean-rb
+
+clean-static::
+clean-rb-default::
+clean-rb::
+clean-so::
+clean: clean-so clean-static clean-rb-default clean-rb
+ -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
+
+distclean-rb-default::
+distclean-rb::
+distclean-so::
+distclean-static::
+distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
+ -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
+ -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
+ -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
+
+realclean: distclean
+install: install-so install-rb
+
+install-so: $(RUBYARCHDIR)/$(DLLIB)
+clean-so::
+ -$(Q)$(RM) $(RUBYARCHDIR)/$(DLLIB)
+ -$(Q)$(RMDIRS) $(RUBYARCHDIR) 2> /dev/null || true
+clean-static::
+ -$(Q)$(RM) $(STATIC_LIB)
+install-rb: pre-install-rb install-rb-default
+install-rb-default: pre-install-rb-default
+pre-install-rb: Makefile
+pre-install-rb-default: Makefile
+pre-install-rb-default: $(TIMESTAMP_DIR)/.RUBYLIBDIR.time
+install-rb-default: $(RUBYLIBDIR)/socket.rb
+$(RUBYLIBDIR)/socket.rb: $(srcdir)/lib/socket.rb $(TIMESTAMP_DIR)/.RUBYLIBDIR.time
+ $(Q) $(COPY) $(srcdir)/lib/socket.rb $(@D)
+clean-rb-default::
+ -$(Q)$(RM) $(RUBYLIBDIR)/socket.rb
+pre-install-rb-default:
+ $(ECHO) installing default socket libraries
+clean-rb-default::
+ -$(Q)$(RMDIRS) $(RUBYLIBDIR) 2> /dev/null || true
+$(TIMESTAMP_DIR)/.RUBYARCHDIR.time:
+ $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
+ $(Q) $(TOUCH) $@
+$(TIMESTAMP_DIR)/.RUBYLIBDIR.time:
+ $(Q) $(MAKEDIRS) $(@D) $(RUBYLIBDIR)
+ $(Q) $(TOUCH) $@
+
+site-install: site-install-so site-install-rb
+site-install-so: install-so
+site-install-rb: install-rb
+
+.SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S
+
+.cc.o:
+ $(ECHO) compiling $(<)
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.cc.S:
+ $(ECHO) translating $(<)
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $<
+
+.mm.o:
+ $(ECHO) compiling $(<)
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.mm.S:
+ $(ECHO) translating $(<)
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $<
+
+.cxx.o:
+ $(ECHO) compiling $(<)
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.cxx.S:
+ $(ECHO) translating $(<)
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $<
+
+.cpp.o:
+ $(ECHO) compiling $(<)
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.cpp.S:
+ $(ECHO) translating $(<)
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $<
+
+.c.o:
+ $(ECHO) compiling $(<)
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
+
+.c.S:
+ $(ECHO) translating $(<)
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $<
+
+.m.o:
+ $(ECHO) compiling $(<)
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
+
+.m.S:
+ $(ECHO) translating $(<)
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $<
+
+$(RUBYARCHDIR)/$(DLLIB): $(OBJS) Makefile $(TIMESTAMP_DIR)/.RUBYARCHDIR.time
+ $(ECHO) linking shared-object $(DLLIB)
+ -$(Q)$(RM) $(@)
+ $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
+
+
+
+###
+$(OBJS): $(RUBY_EXTCONF_H)
+
+getnameinfo.o: getnameinfo.c $(arch_hdrdir)/ruby/config.h addrinfo.h sockport.h
+getaddrinfo.o: getaddrinfo.c $(arch_hdrdir)/ruby/config.h addrinfo.h sockport.h
+
+constdefs.h constdefs.c : $(srcdir)/mkconstants.rb
+ @echo "generating constant definitions"
+ @$(RUBY) $(srcdir)/mkconstants.rb -H constdefs.h -o constdefs.c
+
+# AUTOGENERATED DEPENDENCIES START
+ancdata.o: $(RUBY_EXTCONF_H)
+ancdata.o: $(arch_hdrdir)/ruby/config.h
+ancdata.o: $(hdrdir)/ruby/defines.h
+ancdata.o: $(hdrdir)/ruby/encoding.h
+ancdata.o: $(hdrdir)/ruby/intern.h
+ancdata.o: $(hdrdir)/ruby/io.h
+ancdata.o: $(hdrdir)/ruby/missing.h
+ancdata.o: $(hdrdir)/ruby/oniguruma.h
+ancdata.o: $(hdrdir)/ruby/ruby.h
+ancdata.o: $(hdrdir)/ruby/st.h
+ancdata.o: $(hdrdir)/ruby/subst.h
+ancdata.o: $(hdrdir)/ruby/thread.h
+ancdata.o: $(hdrdir)/ruby/util.h
+ancdata.o: $(top_srcdir)/include/ruby.h
+ancdata.o: $(top_srcdir)/internal.h
+ancdata.o: ancdata.c
+ancdata.o: constdefs.h
+ancdata.o: rubysocket.h
+ancdata.o: sockport.h
+basicsocket.o: $(RUBY_EXTCONF_H)
+basicsocket.o: $(arch_hdrdir)/ruby/config.h
+basicsocket.o: $(hdrdir)/ruby/defines.h
+basicsocket.o: $(hdrdir)/ruby/encoding.h
+basicsocket.o: $(hdrdir)/ruby/intern.h
+basicsocket.o: $(hdrdir)/ruby/io.h
+basicsocket.o: $(hdrdir)/ruby/missing.h
+basicsocket.o: $(hdrdir)/ruby/oniguruma.h
+basicsocket.o: $(hdrdir)/ruby/ruby.h
+basicsocket.o: $(hdrdir)/ruby/st.h
+basicsocket.o: $(hdrdir)/ruby/subst.h
+basicsocket.o: $(hdrdir)/ruby/thread.h
+basicsocket.o: $(hdrdir)/ruby/util.h
+basicsocket.o: $(top_srcdir)/include/ruby.h
+basicsocket.o: $(top_srcdir)/internal.h
+basicsocket.o: basicsocket.c
+basicsocket.o: constdefs.h
+basicsocket.o: rubysocket.h
+basicsocket.o: sockport.h
+constants.o: $(RUBY_EXTCONF_H)
+constants.o: $(arch_hdrdir)/ruby/config.h
+constants.o: $(hdrdir)/ruby/defines.h
+constants.o: $(hdrdir)/ruby/encoding.h
+constants.o: $(hdrdir)/ruby/intern.h
+constants.o: $(hdrdir)/ruby/io.h
+constants.o: $(hdrdir)/ruby/missing.h
+constants.o: $(hdrdir)/ruby/oniguruma.h
+constants.o: $(hdrdir)/ruby/ruby.h
+constants.o: $(hdrdir)/ruby/st.h
+constants.o: $(hdrdir)/ruby/subst.h
+constants.o: $(hdrdir)/ruby/thread.h
+constants.o: $(hdrdir)/ruby/util.h
+constants.o: $(top_srcdir)/include/ruby.h
+constants.o: $(top_srcdir)/internal.h
+constants.o: constants.c
+constants.o: constdefs.c
+constants.o: constdefs.h
+constants.o: rubysocket.h
+constants.o: sockport.h
+ifaddr.o: $(RUBY_EXTCONF_H)
+ifaddr.o: $(arch_hdrdir)/ruby/config.h
+ifaddr.o: $(hdrdir)/ruby/defines.h
+ifaddr.o: $(hdrdir)/ruby/encoding.h
+ifaddr.o: $(hdrdir)/ruby/intern.h
+ifaddr.o: $(hdrdir)/ruby/io.h
+ifaddr.o: $(hdrdir)/ruby/missing.h
+ifaddr.o: $(hdrdir)/ruby/oniguruma.h
+ifaddr.o: $(hdrdir)/ruby/ruby.h
+ifaddr.o: $(hdrdir)/ruby/st.h
+ifaddr.o: $(hdrdir)/ruby/subst.h
+ifaddr.o: $(hdrdir)/ruby/thread.h
+ifaddr.o: $(hdrdir)/ruby/util.h
+ifaddr.o: $(top_srcdir)/include/ruby.h
+ifaddr.o: $(top_srcdir)/internal.h
+ifaddr.o: constdefs.h
+ifaddr.o: ifaddr.c
+ifaddr.o: rubysocket.h
+ifaddr.o: sockport.h
+init.o: $(RUBY_EXTCONF_H)
+init.o: $(arch_hdrdir)/ruby/config.h
+init.o: $(hdrdir)/ruby/defines.h
+init.o: $(hdrdir)/ruby/encoding.h
+init.o: $(hdrdir)/ruby/intern.h
+init.o: $(hdrdir)/ruby/io.h
+init.o: $(hdrdir)/ruby/missing.h
+init.o: $(hdrdir)/ruby/oniguruma.h
+init.o: $(hdrdir)/ruby/ruby.h
+init.o: $(hdrdir)/ruby/st.h
+init.o: $(hdrdir)/ruby/subst.h
+init.o: $(hdrdir)/ruby/thread.h
+init.o: $(hdrdir)/ruby/util.h
+init.o: $(top_srcdir)/include/ruby.h
+init.o: $(top_srcdir)/internal.h
+init.o: constdefs.h
+init.o: init.c
+init.o: rubysocket.h
+init.o: sockport.h
+ipsocket.o: $(RUBY_EXTCONF_H)
+ipsocket.o: $(arch_hdrdir)/ruby/config.h
+ipsocket.o: $(hdrdir)/ruby/defines.h
+ipsocket.o: $(hdrdir)/ruby/encoding.h
+ipsocket.o: $(hdrdir)/ruby/intern.h
+ipsocket.o: $(hdrdir)/ruby/io.h
+ipsocket.o: $(hdrdir)/ruby/missing.h
+ipsocket.o: $(hdrdir)/ruby/oniguruma.h
+ipsocket.o: $(hdrdir)/ruby/ruby.h
+ipsocket.o: $(hdrdir)/ruby/st.h
+ipsocket.o: $(hdrdir)/ruby/subst.h
+ipsocket.o: $(hdrdir)/ruby/thread.h
+ipsocket.o: $(hdrdir)/ruby/util.h
+ipsocket.o: $(top_srcdir)/include/ruby.h
+ipsocket.o: $(top_srcdir)/internal.h
+ipsocket.o: constdefs.h
+ipsocket.o: ipsocket.c
+ipsocket.o: rubysocket.h
+ipsocket.o: sockport.h
+option.o: $(RUBY_EXTCONF_H)
+option.o: $(arch_hdrdir)/ruby/config.h
+option.o: $(hdrdir)/ruby/defines.h
+option.o: $(hdrdir)/ruby/encoding.h
+option.o: $(hdrdir)/ruby/intern.h
+option.o: $(hdrdir)/ruby/io.h
+option.o: $(hdrdir)/ruby/missing.h
+option.o: $(hdrdir)/ruby/oniguruma.h
+option.o: $(hdrdir)/ruby/ruby.h
+option.o: $(hdrdir)/ruby/st.h
+option.o: $(hdrdir)/ruby/subst.h
+option.o: $(hdrdir)/ruby/thread.h
+option.o: $(hdrdir)/ruby/util.h
+option.o: $(top_srcdir)/include/ruby.h
+option.o: $(top_srcdir)/internal.h
+option.o: constdefs.h
+option.o: option.c
+option.o: rubysocket.h
+option.o: sockport.h
+raddrinfo.o: $(RUBY_EXTCONF_H)
+raddrinfo.o: $(arch_hdrdir)/ruby/config.h
+raddrinfo.o: $(hdrdir)/ruby/defines.h
+raddrinfo.o: $(hdrdir)/ruby/encoding.h
+raddrinfo.o: $(hdrdir)/ruby/intern.h
+raddrinfo.o: $(hdrdir)/ruby/io.h
+raddrinfo.o: $(hdrdir)/ruby/missing.h
+raddrinfo.o: $(hdrdir)/ruby/oniguruma.h
+raddrinfo.o: $(hdrdir)/ruby/ruby.h
+raddrinfo.o: $(hdrdir)/ruby/st.h
+raddrinfo.o: $(hdrdir)/ruby/subst.h
+raddrinfo.o: $(hdrdir)/ruby/thread.h
+raddrinfo.o: $(hdrdir)/ruby/util.h
+raddrinfo.o: $(top_srcdir)/include/ruby.h
+raddrinfo.o: $(top_srcdir)/internal.h
+raddrinfo.o: constdefs.h
+raddrinfo.o: raddrinfo.c
+raddrinfo.o: rubysocket.h
+raddrinfo.o: sockport.h
+socket.o: $(RUBY_EXTCONF_H)
+socket.o: $(arch_hdrdir)/ruby/config.h
+socket.o: $(hdrdir)/ruby/defines.h
+socket.o: $(hdrdir)/ruby/encoding.h
+socket.o: $(hdrdir)/ruby/intern.h
+socket.o: $(hdrdir)/ruby/io.h
+socket.o: $(hdrdir)/ruby/missing.h
+socket.o: $(hdrdir)/ruby/oniguruma.h
+socket.o: $(hdrdir)/ruby/ruby.h
+socket.o: $(hdrdir)/ruby/st.h
+socket.o: $(hdrdir)/ruby/subst.h
+socket.o: $(hdrdir)/ruby/thread.h
+socket.o: $(hdrdir)/ruby/util.h
+socket.o: $(top_srcdir)/include/ruby.h
+socket.o: $(top_srcdir)/internal.h
+socket.o: constdefs.h
+socket.o: rubysocket.h
+socket.o: socket.c
+socket.o: sockport.h
+sockssocket.o: $(RUBY_EXTCONF_H)
+sockssocket.o: $(arch_hdrdir)/ruby/config.h
+sockssocket.o: $(hdrdir)/ruby/defines.h
+sockssocket.o: $(hdrdir)/ruby/encoding.h
+sockssocket.o: $(hdrdir)/ruby/intern.h
+sockssocket.o: $(hdrdir)/ruby/io.h
+sockssocket.o: $(hdrdir)/ruby/missing.h
+sockssocket.o: $(hdrdir)/ruby/oniguruma.h
+sockssocket.o: $(hdrdir)/ruby/ruby.h
+sockssocket.o: $(hdrdir)/ruby/st.h
+sockssocket.o: $(hdrdir)/ruby/subst.h
+sockssocket.o: $(hdrdir)/ruby/thread.h
+sockssocket.o: $(hdrdir)/ruby/util.h
+sockssocket.o: $(top_srcdir)/include/ruby.h
+sockssocket.o: $(top_srcdir)/internal.h
+sockssocket.o: constdefs.h
+sockssocket.o: rubysocket.h
+sockssocket.o: sockport.h
+sockssocket.o: sockssocket.c
+tcpserver.o: $(RUBY_EXTCONF_H)
+tcpserver.o: $(arch_hdrdir)/ruby/config.h
+tcpserver.o: $(hdrdir)/ruby/defines.h
+tcpserver.o: $(hdrdir)/ruby/encoding.h
+tcpserver.o: $(hdrdir)/ruby/intern.h
+tcpserver.o: $(hdrdir)/ruby/io.h
+tcpserver.o: $(hdrdir)/ruby/missing.h
+tcpserver.o: $(hdrdir)/ruby/oniguruma.h
+tcpserver.o: $(hdrdir)/ruby/ruby.h
+tcpserver.o: $(hdrdir)/ruby/st.h
+tcpserver.o: $(hdrdir)/ruby/subst.h
+tcpserver.o: $(hdrdir)/ruby/thread.h
+tcpserver.o: $(hdrdir)/ruby/util.h
+tcpserver.o: $(top_srcdir)/include/ruby.h
+tcpserver.o: $(top_srcdir)/internal.h
+tcpserver.o: constdefs.h
+tcpserver.o: rubysocket.h
+tcpserver.o: sockport.h
+tcpserver.o: tcpserver.c
+tcpsocket.o: $(RUBY_EXTCONF_H)
+tcpsocket.o: $(arch_hdrdir)/ruby/config.h
+tcpsocket.o: $(hdrdir)/ruby/defines.h
+tcpsocket.o: $(hdrdir)/ruby/encoding.h
+tcpsocket.o: $(hdrdir)/ruby/intern.h
+tcpsocket.o: $(hdrdir)/ruby/io.h
+tcpsocket.o: $(hdrdir)/ruby/missing.h
+tcpsocket.o: $(hdrdir)/ruby/oniguruma.h
+tcpsocket.o: $(hdrdir)/ruby/ruby.h
+tcpsocket.o: $(hdrdir)/ruby/st.h
+tcpsocket.o: $(hdrdir)/ruby/subst.h
+tcpsocket.o: $(hdrdir)/ruby/thread.h
+tcpsocket.o: $(hdrdir)/ruby/util.h
+tcpsocket.o: $(top_srcdir)/include/ruby.h
+tcpsocket.o: $(top_srcdir)/internal.h
+tcpsocket.o: constdefs.h
+tcpsocket.o: rubysocket.h
+tcpsocket.o: sockport.h
+tcpsocket.o: tcpsocket.c
+udpsocket.o: $(RUBY_EXTCONF_H)
+udpsocket.o: $(arch_hdrdir)/ruby/config.h
+udpsocket.o: $(hdrdir)/ruby/defines.h
+udpsocket.o: $(hdrdir)/ruby/encoding.h
+udpsocket.o: $(hdrdir)/ruby/intern.h
+udpsocket.o: $(hdrdir)/ruby/io.h
+udpsocket.o: $(hdrdir)/ruby/missing.h
+udpsocket.o: $(hdrdir)/ruby/oniguruma.h
+udpsocket.o: $(hdrdir)/ruby/ruby.h
+udpsocket.o: $(hdrdir)/ruby/st.h
+udpsocket.o: $(hdrdir)/ruby/subst.h
+udpsocket.o: $(hdrdir)/ruby/thread.h
+udpsocket.o: $(hdrdir)/ruby/util.h
+udpsocket.o: $(top_srcdir)/include/ruby.h
+udpsocket.o: $(top_srcdir)/internal.h
+udpsocket.o: constdefs.h
+udpsocket.o: rubysocket.h
+udpsocket.o: sockport.h
+udpsocket.o: udpsocket.c
+unixserver.o: $(RUBY_EXTCONF_H)
+unixserver.o: $(arch_hdrdir)/ruby/config.h
+unixserver.o: $(hdrdir)/ruby/defines.h
+unixserver.o: $(hdrdir)/ruby/encoding.h
+unixserver.o: $(hdrdir)/ruby/intern.h
+unixserver.o: $(hdrdir)/ruby/io.h
+unixserver.o: $(hdrdir)/ruby/missing.h
+unixserver.o: $(hdrdir)/ruby/oniguruma.h
+unixserver.o: $(hdrdir)/ruby/ruby.h
+unixserver.o: $(hdrdir)/ruby/st.h
+unixserver.o: $(hdrdir)/ruby/subst.h
+unixserver.o: $(hdrdir)/ruby/thread.h
+unixserver.o: $(hdrdir)/ruby/util.h
+unixserver.o: $(top_srcdir)/include/ruby.h
+unixserver.o: $(top_srcdir)/internal.h
+unixserver.o: constdefs.h
+unixserver.o: rubysocket.h
+unixserver.o: sockport.h
+unixserver.o: unixserver.c
+unixsocket.o: $(RUBY_EXTCONF_H)
+unixsocket.o: $(arch_hdrdir)/ruby/config.h
+unixsocket.o: $(hdrdir)/ruby/defines.h
+unixsocket.o: $(hdrdir)/ruby/encoding.h
+unixsocket.o: $(hdrdir)/ruby/intern.h
+unixsocket.o: $(hdrdir)/ruby/io.h
+unixsocket.o: $(hdrdir)/ruby/missing.h
+unixsocket.o: $(hdrdir)/ruby/oniguruma.h
+unixsocket.o: $(hdrdir)/ruby/ruby.h
+unixsocket.o: $(hdrdir)/ruby/st.h
+unixsocket.o: $(hdrdir)/ruby/subst.h
+unixsocket.o: $(hdrdir)/ruby/thread.h
+unixsocket.o: $(hdrdir)/ruby/util.h
+unixsocket.o: $(top_srcdir)/include/ruby.h
+unixsocket.o: $(top_srcdir)/internal.h
+unixsocket.o: constdefs.h
+unixsocket.o: rubysocket.h
+unixsocket.o: sockport.h
+unixsocket.o: unixsocket.c
+# AUTOGENERATED DEPENDENCIES END
diff --git a/jni/ruby/ext/socket/addrinfo.h b/jni/ruby/ext/socket/addrinfo.h
new file mode 100644
index 0000000..f0b977d
--- /dev/null
+++ b/jni/ruby/ext/socket/addrinfo.h
@@ -0,0 +1,189 @@
+/*
+ * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef ADDR_INFO_H
+#define ADDR_INFO_H
+
+/* special compatibility hack */
+#undef EAI_ADDRFAMILY
+#undef EAI_AGAIN
+#undef EAI_BADFLAGS
+#undef EAI_FAIL
+#undef EAI_FAMILY
+#undef EAI_MEMORY
+#undef EAI_NODATA
+#undef EAI_NONAME
+#undef EAI_SERVICE
+#undef EAI_SOCKTYPE
+#undef EAI_SYSTEM
+#undef EAI_BADHINTS
+#undef EAI_PROTOCOL
+#undef EAI_MAX
+
+#undef AI_PASSIVE
+#undef AI_CANONNAME
+#undef AI_NUMERICHOST
+#undef AI_NUMERICSERV
+#undef AI_ALL
+#undef AI_ADDRCONFIG
+#undef AI_V4MAPPED
+#undef AI_DEFAULT
+
+#undef NI_NOFQDN
+#undef NI_NUMERICHOST
+#undef NI_NAMEREQD
+#undef NI_NUMERICSERV
+#undef NI_DGRAM
+
+#ifndef __P
+# ifdef HAVE_PROTOTYPES
+# define __P(args) args
+# else
+# define __P(args) ()
+# endif
+#endif
+
+/* special compatibility hack -- end*/
+
+
+/*
+ * Error return codes from getaddrinfo()
+ */
+#define EAI_ADDRFAMILY 1 /* address family for hostname not supported */
+#define EAI_AGAIN 2 /* temporary failure in name resolution */
+#define EAI_BADFLAGS 3 /* invalid value for ai_flags */
+#define EAI_FAIL 4 /* non-recoverable failure in name resolution */
+#define EAI_FAMILY 5 /* ai_family not supported */
+#define EAI_MEMORY 6 /* memory allocation failure */
+#define EAI_NODATA 7 /* no address associated with hostname */
+#define EAI_NONAME 8 /* hostname nor servname provided, or not known */
+#define EAI_SERVICE 9 /* servname not supported for ai_socktype */
+#define EAI_SOCKTYPE 10 /* ai_socktype not supported */
+#define EAI_SYSTEM 11 /* system error returned in errno */
+#define EAI_BADHINTS 12
+#define EAI_PROTOCOL 13
+#define EAI_MAX 14
+
+/*
+ * Flag values for getaddrinfo()
+ */
+#define AI_PASSIVE 0x00000001 /* get address to use bind() */
+#define AI_CANONNAME 0x00000002 /* fill ai_canonname */
+#define AI_NUMERICHOST 0x00000004 /* prevent name resolution */
+#define AI_NUMERICSERV 0x00000008 /* prevent service name resolution */
+/* valid flags for addrinfo */
+#ifndef __HAIKU__
+#undef AI_MASK
+#define AI_MASK (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV)
+#endif
+
+#define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */
+#define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */
+#define AI_ADDRCONFIG 0x00000400 /* only if any address is assigned */
+#define AI_V4MAPPED 0x00000800 /* accept IPv4-mapped IPv6 address */
+/* special recommended flags for getipnodebyname */
+#define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG)
+
+/*
+ * Constants for getnameinfo()
+ */
+#ifndef NI_MAXHOST
+#define NI_MAXHOST 1025
+#define NI_MAXSERV 32
+#endif
+
+/*
+ * Flag values for getnameinfo()
+ */
+#define NI_NOFQDN 0x00000001
+#define NI_NUMERICHOST 0x00000002
+#define NI_NAMEREQD 0x00000004
+#define NI_NUMERICSERV 0x00000008
+#define NI_DGRAM 0x00000010
+
+#ifndef HAVE_TYPE_STRUCT_ADDRINFO
+struct addrinfo {
+ int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
+ int ai_family; /* PF_xxx */
+ int ai_socktype; /* SOCK_xxx */
+ int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
+ size_t ai_addrlen; /* length of ai_addr */
+ char *ai_canonname; /* canonical name for hostname */
+ struct sockaddr *ai_addr; /* binary address */
+ struct addrinfo *ai_next; /* next structure in linked list */
+};
+#endif
+
+#ifndef HAVE_GETADDRINFO
+#undef getaddrinfo
+#define getaddrinfo getaddrinfo__compat
+#endif
+#ifndef HAVE_GETNAMEINFO
+#undef getnameinfo
+#define getnameinfo getnameinfo__compat
+#endif
+#ifndef HAVE_FREEHOSTENT
+#undef freehostent
+#define freehostent freehostent__compat
+#endif
+#ifndef HAVE_FREEADDRINFO
+#undef freeaddrinfo
+#define freeaddrinfo freeaddrinfo__compat
+#endif
+
+extern int getaddrinfo __P((
+ const char *hostname, const char *servname,
+ const struct addrinfo *hints,
+ struct addrinfo **res));
+
+extern int getnameinfo __P((
+ const struct sockaddr *sa,
+ socklen_t salen,
+ char *host,
+ socklen_t hostlen,
+ char *serv,
+ socklen_t servlen,
+ int flags));
+
+extern void freehostent __P((struct hostent *));
+extern void freeaddrinfo __P((struct addrinfo *));
+extern
+#ifdef GAI_STRERROR_CONST
+const
+#endif
+char *gai_strerror __P((int));
+
+/* In case there is no definition of offsetof() provided - though any proper
+Standard C system should have one. */
+
+#ifndef offsetof
+#define offsetof(p_type,field) ((size_t)&(((p_type *)0)->field))
+#endif
+
+#endif
diff --git a/jni/ruby/ext/socket/ancdata.c b/jni/ruby/ext/socket/ancdata.c
new file mode 100644
index 0000000..b69417e
--- /dev/null
+++ b/jni/ruby/ext/socket/ancdata.c
@@ -0,0 +1,1842 @@
+#include "rubysocket.h"
+
+#include <time.h>
+
+int rsock_cmsg_cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
+
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+static VALUE rb_cAncillaryData;
+
+static VALUE
+constant_to_sym(int constant, ID (*intern_const)(int))
+{
+ ID name = intern_const(constant);
+ if (name) {
+ return ID2SYM(name);
+ }
+
+ return INT2NUM(constant);
+}
+
+static VALUE
+ip_cmsg_type_to_sym(int level, int cmsg_type)
+{
+ switch (level) {
+ case SOL_SOCKET:
+ return constant_to_sym(cmsg_type, rsock_intern_scm_optname);
+ case IPPROTO_IP:
+ return constant_to_sym(cmsg_type, rsock_intern_ip_optname);
+#ifdef IPPROTO_IPV6
+ case IPPROTO_IPV6:
+ return constant_to_sym(cmsg_type, rsock_intern_ipv6_optname);
+#endif
+ case IPPROTO_TCP:
+ return constant_to_sym(cmsg_type, rsock_intern_tcp_optname);
+ case IPPROTO_UDP:
+ return constant_to_sym(cmsg_type, rsock_intern_udp_optname);
+ default:
+ return INT2NUM(cmsg_type);
+ }
+}
+
+/*
+ * call-seq:
+ * Socket::AncillaryData.new(family, cmsg_level, cmsg_type, cmsg_data) -> ancillarydata
+ *
+ * _family_ should be an integer, a string or a symbol.
+ * - Socket::AF_INET, "AF_INET", "INET", :AF_INET, :INET
+ * - Socket::AF_UNIX, "AF_UNIX", "UNIX", :AF_UNIX, :UNIX
+ * - etc.
+ *
+ * _cmsg_level_ should be an integer, a string or a symbol.
+ * - Socket::SOL_SOCKET, "SOL_SOCKET", "SOCKET", :SOL_SOCKET and :SOCKET
+ * - Socket::IPPROTO_IP, "IP" and :IP
+ * - Socket::IPPROTO_IPV6, "IPV6" and :IPV6
+ * - Socket::IPPROTO_TCP, "TCP" and :TCP
+ * - etc.
+ *
+ * _cmsg_type_ should be an integer, a string or a symbol.
+ * If a string/symbol is specified, it is interpreted depend on _cmsg_level_.
+ * - Socket::SCM_RIGHTS, "SCM_RIGHTS", "RIGHTS", :SCM_RIGHTS, :RIGHTS for SOL_SOCKET
+ * - Socket::IP_RECVTTL, "RECVTTL" and :RECVTTL for IPPROTO_IP
+ * - Socket::IPV6_PKTINFO, "PKTINFO" and :PKTINFO for IPPROTO_IPV6
+ * - etc.
+ *
+ * _cmsg_data_ should be a string.
+ *
+ * p Socket::AncillaryData.new(:INET, :TCP, :NODELAY, "")
+ * #=> #<Socket::AncillaryData: INET TCP NODELAY "">
+ *
+ * p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "")
+ * #=> #<Socket::AncillaryData: INET6 IPV6 PKTINFO "">
+ *
+ */
+static VALUE
+ancillary_initialize(VALUE self, VALUE vfamily, VALUE vlevel, VALUE vtype, VALUE data)
+{
+ int family = rsock_family_arg(vfamily);
+ int level = rsock_level_arg(family, vlevel);
+ int type = rsock_cmsg_type_arg(family, level, vtype);
+ StringValue(data);
+ rb_ivar_set(self, rb_intern("family"), INT2NUM(family));
+ rb_ivar_set(self, rb_intern("level"), INT2NUM(level));
+ rb_ivar_set(self, rb_intern("type"), INT2NUM(type));
+ rb_ivar_set(self, rb_intern("data"), data);
+ return self;
+}
+
+static VALUE
+ancdata_new(int family, int level, int type, VALUE data)
+{
+ NEWOBJ_OF(obj, struct RObject, rb_cAncillaryData, T_OBJECT);
+ StringValue(data);
+ ancillary_initialize((VALUE)obj, INT2NUM(family), INT2NUM(level), INT2NUM(type), data);
+ return (VALUE)obj;
+}
+
+static int
+ancillary_family(VALUE self)
+{
+ VALUE v = rb_attr_get(self, rb_intern("family"));
+ return NUM2INT(v);
+}
+
+/*
+ * call-seq:
+ * ancillarydata.family => integer
+ *
+ * returns the socket family as an integer.
+ *
+ * p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").family
+ * #=> 10
+ */
+static VALUE
+ancillary_family_m(VALUE self)
+{
+ return INT2NUM(ancillary_family(self));
+}
+
+static int
+ancillary_level(VALUE self)
+{
+ VALUE v = rb_attr_get(self, rb_intern("level"));
+ return NUM2INT(v);
+}
+
+/*
+ * call-seq:
+ * ancillarydata.level => integer
+ *
+ * returns the cmsg level as an integer.
+ *
+ * p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").level
+ * #=> 41
+ */
+static VALUE
+ancillary_level_m(VALUE self)
+{
+ return INT2NUM(ancillary_level(self));
+}
+
+static int
+ancillary_type(VALUE self)
+{
+ VALUE v = rb_attr_get(self, rb_intern("type"));
+ return NUM2INT(v);
+}
+
+/*
+ * call-seq:
+ * ancillarydata.type => integer
+ *
+ * returns the cmsg type as an integer.
+ *
+ * p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").type
+ * #=> 2
+ */
+static VALUE
+ancillary_type_m(VALUE self)
+{
+ return INT2NUM(ancillary_type(self));
+}
+
+/*
+ * call-seq:
+ * ancillarydata.data => string
+ *
+ * returns the cmsg data as a string.
+ *
+ * p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").data
+ * #=> ""
+ */
+static VALUE
+ancillary_data(VALUE self)
+{
+ VALUE v = rb_attr_get(self, rb_intern("data"));
+ StringValue(v);
+ return v;
+}
+
+#ifdef SCM_RIGHTS
+/*
+ * call-seq:
+ * Socket::AncillaryData.unix_rights(io1, io2, ...) => ancillarydata
+ *
+ * Creates a new Socket::AncillaryData object which contains file descriptors as data.
+ *
+ * p Socket::AncillaryData.unix_rights(STDERR)
+ * #=> #<Socket::AncillaryData: UNIX SOCKET RIGHTS 2>
+ */
+static VALUE
+ancillary_s_unix_rights(int argc, VALUE *argv, VALUE klass)
+{
+ VALUE result, str, ary;
+ int i;
+
+ ary = rb_ary_new();
+
+ for (i = 0 ; i < argc; i++) {
+ VALUE obj = argv[i];
+ if (!RB_TYPE_P(obj, T_FILE)) {
+ rb_raise(rb_eTypeError, "IO expected");
+ }
+ rb_ary_push(ary, obj);
+ }
+
+ str = rb_str_buf_new(sizeof(int) * argc);
+
+ for (i = 0 ; i < argc; i++) {
+ VALUE obj = RARRAY_PTR(ary)[i];
+ rb_io_t *fptr;
+ int fd;
+ GetOpenFile(obj, fptr);
+ fd = fptr->fd;
+ rb_str_buf_cat(str, (char *)&fd, sizeof(int));
+ }
+
+ result = ancdata_new(AF_UNIX, SOL_SOCKET, SCM_RIGHTS, str);
+ rb_ivar_set(result, rb_intern("unix_rights"), ary);
+ return result;
+}
+#else
+#define ancillary_s_unix_rights rb_f_notimplement
+#endif
+
+#ifdef SCM_RIGHTS
+/*
+ * call-seq:
+ * ancillarydata.unix_rights => array-of-IOs or nil
+ *
+ * returns the array of IO objects for SCM_RIGHTS control message in UNIX domain socket.
+ *
+ * The class of the IO objects in the array is IO or Socket.
+ *
+ * The array is attached to _ancillarydata_ when it is instantiated.
+ * For example, BasicSocket#recvmsg attach the array when
+ * receives a SCM_RIGHTS control message and :scm_rights=>true option is given.
+ *
+ * # recvmsg needs :scm_rights=>true for unix_rights
+ * s1, s2 = UNIXSocket.pair
+ * p s1 #=> #<UNIXSocket:fd 3>
+ * s1.sendmsg "stdin and a socket", 0, nil, Socket::AncillaryData.unix_rights(STDIN, s1)
+ * _, _, _, ctl = s2.recvmsg(:scm_rights=>true)
+ * p ctl #=> #<Socket::AncillaryData: UNIX SOCKET RIGHTS 6 7>
+ * p ctl.unix_rights #=> [#<IO:fd 6>, #<Socket:fd 7>]
+ * p File.identical?(STDIN, ctl.unix_rights[0]) #=> true
+ * p File.identical?(s1, ctl.unix_rights[1]) #=> true
+ *
+ * # If :scm_rights=>true is not given, unix_rights returns nil
+ * s1, s2 = UNIXSocket.pair
+ * s1.sendmsg "stdin and a socket", 0, nil, Socket::AncillaryData.unix_rights(STDIN, s1)
+ * _, _, _, ctl = s2.recvmsg
+ * p ctl #=> #<Socket::AncillaryData: UNIX SOCKET RIGHTS 6 7>
+ * p ctl.unix_rights #=> nil
+ *
+ */
+static VALUE
+ancillary_unix_rights(VALUE self)
+{
+ int level, type;
+
+ level = ancillary_level(self);
+ type = ancillary_type(self);
+
+ if (level != SOL_SOCKET || type != SCM_RIGHTS)
+ rb_raise(rb_eTypeError, "SCM_RIGHTS ancillary data expected");
+
+ return rb_attr_get(self, rb_intern("unix_rights"));
+}
+#else
+#define ancillary_unix_rights rb_f_notimplement
+#endif
+
+#if defined(SCM_TIMESTAMP) || defined(SCM_TIMESTAMPNS) || defined(SCM_BINTIME)
+/*
+ * call-seq:
+ * ancillarydata.timestamp => time
+ *
+ * returns the timestamp as a time object.
+ *
+ * _ancillarydata_ should be one of following type:
+ * - SOL_SOCKET/SCM_TIMESTAMP (microsecond) GNU/Linux, FreeBSD, NetBSD, OpenBSD, Solaris, MacOS X
+ * - SOL_SOCKET/SCM_TIMESTAMPNS (nanosecond) GNU/Linux
+ * - SOL_SOCKET/SCM_BINTIME (2**(-64) second) FreeBSD
+ *
+ * Addrinfo.udp("127.0.0.1", 0).bind {|s1|
+ * Addrinfo.udp("127.0.0.1", 0).bind {|s2|
+ * s1.setsockopt(:SOCKET, :TIMESTAMP, true)
+ * s2.send "a", 0, s1.local_address
+ * ctl = s1.recvmsg.last
+ * p ctl #=> #<Socket::AncillaryData: INET SOCKET TIMESTAMP 2009-02-24 17:35:46.775581>
+ * t = ctl.timestamp
+ * p t #=> 2009-02-24 17:35:46 +0900
+ * p t.usec #=> 775581
+ * p t.nsec #=> 775581000
+ * }
+ * }
+ *
+ */
+static VALUE
+ancillary_timestamp(VALUE self)
+{
+ int level, type;
+ VALUE data;
+ VALUE result = Qnil;
+
+ level = ancillary_level(self);
+ type = ancillary_type(self);
+ data = ancillary_data(self);
+
+# ifdef SCM_TIMESTAMP
+ if (level == SOL_SOCKET && type == SCM_TIMESTAMP &&
+ RSTRING_LEN(data) == sizeof(struct timeval)) {
+ struct timeval tv;
+ memcpy((char*)&tv, RSTRING_PTR(data), sizeof(tv));
+ result = rb_time_new(tv.tv_sec, tv.tv_usec);
+ }
+# endif
+
+# ifdef SCM_TIMESTAMPNS
+ if (level == SOL_SOCKET && type == SCM_TIMESTAMPNS &&
+ RSTRING_LEN(data) == sizeof(struct timespec)) {
+ struct timespec ts;
+ memcpy((char*)&ts, RSTRING_PTR(data), sizeof(ts));
+ result = rb_time_nano_new(ts.tv_sec, ts.tv_nsec);
+ }
+# endif
+
+#define add(x,y) (rb_funcall((x), '+', 1, (y)))
+#define mul(x,y) (rb_funcall((x), '*', 1, (y)))
+#define quo(x,y) (rb_funcall((x), rb_intern("quo"), 1, (y)))
+
+# ifdef SCM_BINTIME
+ if (level == SOL_SOCKET && type == SCM_BINTIME &&
+ RSTRING_LEN(data) == sizeof(struct bintime)) {
+ struct bintime bt;
+ VALUE d, timev;
+ memcpy((char*)&bt, RSTRING_PTR(data), sizeof(bt));
+ d = ULL2NUM(0x100000000ULL);
+ d = mul(d,d);
+ timev = add(TIMET2NUM(bt.sec), quo(ULL2NUM(bt.frac), d));
+ result = rb_time_num_new(timev, Qnil);
+ }
+# endif
+
+ if (result == Qnil)
+ rb_raise(rb_eTypeError, "timestamp ancillary data expected");
+
+ return result;
+}
+#else
+#define ancillary_timestamp rb_f_notimplement
+#endif
+
+/*
+ * call-seq:
+ * Socket::AncillaryData.int(family, cmsg_level, cmsg_type, integer) => ancillarydata
+ *
+ * Creates a new Socket::AncillaryData object which contains a int as data.
+ *
+ * The size and endian is dependent on the host.
+ *
+ * p Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, STDERR.fileno)
+ * #=> #<Socket::AncillaryData: UNIX SOCKET RIGHTS 2>
+ */
+static VALUE
+ancillary_s_int(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE vtype, VALUE integer)
+{
+ int family = rsock_family_arg(vfamily);
+ int level = rsock_level_arg(family, vlevel);
+ int type = rsock_cmsg_type_arg(family, level, vtype);
+ int i = NUM2INT(integer);
+ return ancdata_new(family, level, type, rb_str_new((char*)&i, sizeof(i)));
+}
+
+/*
+ * call-seq:
+ * ancillarydata.int => integer
+ *
+ * Returns the data in _ancillarydata_ as an int.
+ *
+ * The size and endian is dependent on the host.
+ *
+ * ancdata = Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, STDERR.fileno)
+ * p ancdata.int #=> 2
+ */
+static VALUE
+ancillary_int(VALUE self)
+{
+ VALUE data;
+ int i;
+ data = ancillary_data(self);
+ if (RSTRING_LEN(data) != sizeof(int))
+ rb_raise(rb_eTypeError, "size differ. expected as sizeof(int)=%d but %ld", (int)sizeof(int), (long)RSTRING_LEN(data));
+ memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
+ return INT2NUM(i);
+}
+
+#if defined(IPPROTO_IP) && defined(IP_PKTINFO) && defined(HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST) /* GNU/Linux */
+/*
+ * call-seq:
+ * Socket::AncillaryData.ip_pktinfo(addr, ifindex) => ancdata
+ * Socket::AncillaryData.ip_pktinfo(addr, ifindex, spec_dst) => ancdata
+ *
+ * Returns new ancillary data for IP_PKTINFO.
+ *
+ * If spec_dst is not given, addr is used.
+ *
+ * IP_PKTINFO is not standard.
+ *
+ * Supported platform: GNU/Linux
+ *
+ * addr = Addrinfo.ip("127.0.0.1")
+ * ifindex = 0
+ * spec_dst = Addrinfo.ip("127.0.0.1")
+ * p Socket::AncillaryData.ip_pktinfo(addr, ifindex, spec_dst)
+ * #=> #<Socket::AncillaryData: INET IP PKTINFO 127.0.0.1 ifindex:0 spec_dst:127.0.0.1>
+ *
+ */
+static VALUE
+ancillary_s_ip_pktinfo(int argc, VALUE *argv, VALUE self)
+{
+ VALUE v_addr, v_ifindex, v_spec_dst;
+ unsigned int ifindex;
+ struct sockaddr_in sa;
+ struct in_pktinfo pktinfo;
+
+ rb_scan_args(argc, argv, "21", &v_addr, &v_ifindex, &v_spec_dst);
+
+ SockAddrStringValue(v_addr);
+ ifindex = NUM2UINT(v_ifindex);
+ if (NIL_P(v_spec_dst))
+ v_spec_dst = v_addr;
+ else
+ SockAddrStringValue(v_spec_dst);
+
+ memset(&pktinfo, 0, sizeof(pktinfo));
+
+ memset(&sa, 0, sizeof(sa));
+ if (RSTRING_LEN(v_addr) != sizeof(sa))
+ rb_raise(rb_eArgError, "addr size different to AF_INET sockaddr");
+ memcpy(&sa, RSTRING_PTR(v_addr), sizeof(sa));
+ if (sa.sin_family != AF_INET)
+ rb_raise(rb_eArgError, "addr is not AF_INET sockaddr");
+ memcpy(&pktinfo.ipi_addr, &sa.sin_addr, sizeof(pktinfo.ipi_addr));
+
+ pktinfo.ipi_ifindex = ifindex;
+
+ memset(&sa, 0, sizeof(sa));
+ if (RSTRING_LEN(v_spec_dst) != sizeof(sa))
+ rb_raise(rb_eArgError, "spec_dat size different to AF_INET sockaddr");
+ memcpy(&sa, RSTRING_PTR(v_spec_dst), sizeof(sa));
+ if (sa.sin_family != AF_INET)
+ rb_raise(rb_eArgError, "spec_dst is not AF_INET sockaddr");
+ memcpy(&pktinfo.ipi_spec_dst, &sa.sin_addr, sizeof(pktinfo.ipi_spec_dst));
+
+ return ancdata_new(AF_INET, IPPROTO_IP, IP_PKTINFO, rb_str_new((char *)&pktinfo, sizeof(pktinfo)));
+}
+#else
+#define ancillary_s_ip_pktinfo rb_f_notimplement
+#endif
+
+#if defined(IPPROTO_IP) && defined(IP_PKTINFO) && defined(HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST) /* GNU/Linux */
+/*
+ * call-seq:
+ * ancdata.ip_pktinfo => [addr, ifindex, spec_dst]
+ *
+ * Extracts addr, ifindex and spec_dst from IP_PKTINFO ancillary data.
+ *
+ * IP_PKTINFO is not standard.
+ *
+ * Supported platform: GNU/Linux
+ *
+ * addr = Addrinfo.ip("127.0.0.1")
+ * ifindex = 0
+ * spec_dest = Addrinfo.ip("127.0.0.1")
+ * ancdata = Socket::AncillaryData.ip_pktinfo(addr, ifindex, spec_dest)
+ * p ancdata.ip_pktinfo
+ * #=> [#<Addrinfo: 127.0.0.1>, 0, #<Addrinfo: 127.0.0.1>]
+ *
+ *
+ */
+static VALUE
+ancillary_ip_pktinfo(VALUE self)
+{
+ int level, type;
+ VALUE data;
+ struct in_pktinfo pktinfo;
+ struct sockaddr_in sa;
+ VALUE v_spec_dst, v_addr;
+
+ level = ancillary_level(self);
+ type = ancillary_type(self);
+ data = ancillary_data(self);
+
+ if (level != IPPROTO_IP || type != IP_PKTINFO ||
+ RSTRING_LEN(data) != sizeof(struct in_pktinfo)) {
+ rb_raise(rb_eTypeError, "IP_PKTINFO ancillary data expected");
+ }
+
+ memcpy(&pktinfo, RSTRING_PTR(data), sizeof(struct in_pktinfo));
+ memset(&sa, 0, sizeof(sa));
+
+ sa.sin_family = AF_INET;
+ memcpy(&sa.sin_addr, &pktinfo.ipi_addr, sizeof(sa.sin_addr));
+ v_addr = rsock_addrinfo_new((struct sockaddr *)&sa, sizeof(sa), PF_INET, 0, 0, Qnil, Qnil);
+
+ sa.sin_family = AF_INET;
+ memcpy(&sa.sin_addr, &pktinfo.ipi_spec_dst, sizeof(sa.sin_addr));
+ v_spec_dst = rsock_addrinfo_new((struct sockaddr *)&sa, sizeof(sa), PF_INET, 0, 0, Qnil, Qnil);
+
+ return rb_ary_new3(3, v_addr, UINT2NUM(pktinfo.ipi_ifindex), v_spec_dst);
+}
+#else
+#define ancillary_ip_pktinfo rb_f_notimplement
+#endif
+
+#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
+/*
+ * call-seq:
+ * Socket::AncillaryData.ipv6_pktinfo(addr, ifindex) => ancdata
+ *
+ * Returns new ancillary data for IPV6_PKTINFO.
+ *
+ * IPV6_PKTINFO is defined by RFC 3542.
+ *
+ * addr = Addrinfo.ip("::1")
+ * ifindex = 0
+ * p Socket::AncillaryData.ipv6_pktinfo(addr, ifindex)
+ * #=> #<Socket::AncillaryData: INET6 IPV6 PKTINFO ::1 ifindex:0>
+ *
+ */
+static VALUE
+ancillary_s_ipv6_pktinfo(VALUE self, VALUE v_addr, VALUE v_ifindex)
+{
+ unsigned int ifindex;
+ struct sockaddr_in6 sa;
+ struct in6_pktinfo pktinfo;
+
+ SockAddrStringValue(v_addr);
+ ifindex = NUM2UINT(v_ifindex);
+
+ memset(&pktinfo, 0, sizeof(pktinfo));
+
+ memset(&sa, 0, sizeof(sa));
+ if (RSTRING_LEN(v_addr) != sizeof(sa))
+ rb_raise(rb_eArgError, "addr size different to AF_INET6 sockaddr");
+ memcpy(&sa, RSTRING_PTR(v_addr), sizeof(sa));
+ if (sa.sin6_family != AF_INET6)
+ rb_raise(rb_eArgError, "addr is not AF_INET6 sockaddr");
+ memcpy(&pktinfo.ipi6_addr, &sa.sin6_addr, sizeof(pktinfo.ipi6_addr));
+
+ pktinfo.ipi6_ifindex = ifindex;
+
+ return ancdata_new(AF_INET6, IPPROTO_IPV6, IPV6_PKTINFO, rb_str_new((char *)&pktinfo, sizeof(pktinfo)));
+}
+#else
+#define ancillary_s_ipv6_pktinfo rb_f_notimplement
+#endif
+
+#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
+static void
+extract_ipv6_pktinfo(VALUE self, struct in6_pktinfo *pktinfo_ptr, struct sockaddr_in6 *sa_ptr)
+{
+ int level, type;
+ VALUE data;
+
+ level = ancillary_level(self);
+ type = ancillary_type(self);
+ data = ancillary_data(self);
+
+ if (level != IPPROTO_IPV6 || type != IPV6_PKTINFO ||
+ RSTRING_LEN(data) != sizeof(struct in6_pktinfo)) {
+ rb_raise(rb_eTypeError, "IPV6_PKTINFO ancillary data expected");
+ }
+
+ memcpy(pktinfo_ptr, RSTRING_PTR(data), sizeof(*pktinfo_ptr));
+
+ INIT_SOCKADDR((struct sockaddr *)sa_ptr, AF_INET6, sizeof(*sa_ptr));
+ memcpy(&sa_ptr->sin6_addr, &pktinfo_ptr->ipi6_addr, sizeof(sa_ptr->sin6_addr));
+ if (IN6_IS_ADDR_LINKLOCAL(&sa_ptr->sin6_addr))
+ sa_ptr->sin6_scope_id = pktinfo_ptr->ipi6_ifindex;
+}
+#endif
+
+#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
+/*
+ * call-seq:
+ * ancdata.ipv6_pktinfo => [addr, ifindex]
+ *
+ * Extracts addr and ifindex from IPV6_PKTINFO ancillary data.
+ *
+ * IPV6_PKTINFO is defined by RFC 3542.
+ *
+ * addr = Addrinfo.ip("::1")
+ * ifindex = 0
+ * ancdata = Socket::AncillaryData.ipv6_pktinfo(addr, ifindex)
+ * p ancdata.ipv6_pktinfo #=> [#<Addrinfo: ::1>, 0]
+ *
+ */
+static VALUE
+ancillary_ipv6_pktinfo(VALUE self)
+{
+ struct in6_pktinfo pktinfo;
+ struct sockaddr_in6 sa;
+ VALUE v_addr;
+
+ extract_ipv6_pktinfo(self, &pktinfo, &sa);
+ v_addr = rsock_addrinfo_new((struct sockaddr *)&sa, (socklen_t)sizeof(sa), PF_INET6, 0, 0, Qnil, Qnil);
+ return rb_ary_new3(2, v_addr, UINT2NUM(pktinfo.ipi6_ifindex));
+}
+#else
+#define ancillary_ipv6_pktinfo rb_f_notimplement
+#endif
+
+#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
+/*
+ * call-seq:
+ * ancdata.ipv6_pktinfo_addr => addr
+ *
+ * Extracts addr from IPV6_PKTINFO ancillary data.
+ *
+ * IPV6_PKTINFO is defined by RFC 3542.
+ *
+ * addr = Addrinfo.ip("::1")
+ * ifindex = 0
+ * ancdata = Socket::AncillaryData.ipv6_pktinfo(addr, ifindex)
+ * p ancdata.ipv6_pktinfo_addr #=> #<Addrinfo: ::1>
+ *
+ */
+static VALUE
+ancillary_ipv6_pktinfo_addr(VALUE self)
+{
+ struct in6_pktinfo pktinfo;
+ struct sockaddr_in6 sa;
+ extract_ipv6_pktinfo(self, &pktinfo, &sa);
+ return rsock_addrinfo_new((struct sockaddr *)&sa, (socklen_t)sizeof(sa), PF_INET6, 0, 0, Qnil, Qnil);
+}
+#else
+#define ancillary_ipv6_pktinfo_addr rb_f_notimplement
+#endif
+
+#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) /* IPv6 RFC3542 */
+/*
+ * call-seq:
+ * ancdata.ipv6_pktinfo_ifindex => addr
+ *
+ * Extracts ifindex from IPV6_PKTINFO ancillary data.
+ *
+ * IPV6_PKTINFO is defined by RFC 3542.
+ *
+ * addr = Addrinfo.ip("::1")
+ * ifindex = 0
+ * ancdata = Socket::AncillaryData.ipv6_pktinfo(addr, ifindex)
+ * p ancdata.ipv6_pktinfo_ifindex #=> 0
+ *
+ */
+static VALUE
+ancillary_ipv6_pktinfo_ifindex(VALUE self)
+{
+ struct in6_pktinfo pktinfo;
+ struct sockaddr_in6 sa;
+ extract_ipv6_pktinfo(self, &pktinfo, &sa);
+ return UINT2NUM(pktinfo.ipi6_ifindex);
+}
+#else
+#define ancillary_ipv6_pktinfo_ifindex rb_f_notimplement
+#endif
+
+#if defined(SOL_SOCKET) && defined(SCM_RIGHTS) /* 4.4BSD */
+static int
+anc_inspect_socket_rights(int level, int type, VALUE data, VALUE ret)
+{
+ if (level == SOL_SOCKET && type == SCM_RIGHTS &&
+ 0 < RSTRING_LEN(data) && (RSTRING_LEN(data) % sizeof(int) == 0)) {
+ long off;
+ for (off = 0; off < RSTRING_LEN(data); off += sizeof(int)) {
+ int fd;
+ memcpy((char*)&fd, RSTRING_PTR(data)+off, sizeof(int));
+ rb_str_catf(ret, " %d", fd);
+ }
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(SCM_CREDENTIALS) /* GNU/Linux */
+static int
+anc_inspect_passcred_credentials(int level, int type, VALUE data, VALUE ret)
+{
+ if (level == SOL_SOCKET && type == SCM_CREDENTIALS &&
+ RSTRING_LEN(data) == sizeof(struct ucred)) {
+ struct ucred cred;
+ memcpy(&cred, RSTRING_PTR(data), sizeof(struct ucred));
+ rb_str_catf(ret, " pid=%u uid=%u gid=%u", cred.pid, cred.uid, cred.gid);
+ rb_str_cat2(ret, " (ucred)");
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(SCM_CREDS)
+#define INSPECT_SCM_CREDS
+static int
+anc_inspect_socket_creds(int level, int type, VALUE data, VALUE ret)
+{
+ if (level != SOL_SOCKET && type != SCM_CREDS)
+ return 0;
+
+ /*
+ * FreeBSD has struct cmsgcred and struct sockcred.
+ * They use both SOL_SOCKET/SCM_CREDS in the ancillary message.
+ * They are not ambiguous from the view of the caller
+ * because struct sockcred is sent if and only if the caller sets LOCAL_CREDS socket option.
+ * But inspect method doesn't know it.
+ * So they are ambiguous from the view of inspect.
+ * This function distinguish them by the size of the ancillary message.
+ * This heuristics works well except when sc_ngroups == CMGROUP_MAX.
+ */
+
+#if defined(HAVE_TYPE_STRUCT_CMSGCRED) /* FreeBSD */
+ if (RSTRING_LEN(data) == sizeof(struct cmsgcred)) {
+ struct cmsgcred cred;
+ memcpy(&cred, RSTRING_PTR(data), sizeof(struct cmsgcred));
+ rb_str_catf(ret, " pid=%u", cred.cmcred_pid);
+ rb_str_catf(ret, " uid=%u", cred.cmcred_uid);
+ rb_str_catf(ret, " euid=%u", cred.cmcred_euid);
+ rb_str_catf(ret, " gid=%u", cred.cmcred_gid);
+ if (cred.cmcred_ngroups) {
+ int i;
+ const char *sep = " groups=";
+ for (i = 0; i < cred.cmcred_ngroups; i++) {
+ rb_str_catf(ret, "%s%u", sep, cred.cmcred_groups[i]);
+ sep = ",";
+ }
+ }
+ rb_str_cat2(ret, " (cmsgcred)");
+ return 1;
+ }
+#endif
+#if defined(HAVE_TYPE_STRUCT_SOCKCRED) /* FreeBSD, NetBSD */
+ if ((size_t)RSTRING_LEN(data) >= SOCKCREDSIZE(0)) {
+ struct sockcred cred0, *cred;
+ memcpy(&cred0, RSTRING_PTR(data), SOCKCREDSIZE(0));
+ if ((size_t)RSTRING_LEN(data) == SOCKCREDSIZE(cred0.sc_ngroups)) {
+ cred = (struct sockcred *)ALLOCA_N(char, SOCKCREDSIZE(cred0.sc_ngroups));
+ memcpy(cred, RSTRING_PTR(data), SOCKCREDSIZE(cred0.sc_ngroups));
+ rb_str_catf(ret, " uid=%u", cred->sc_uid);
+ rb_str_catf(ret, " euid=%u", cred->sc_euid);
+ rb_str_catf(ret, " gid=%u", cred->sc_gid);
+ rb_str_catf(ret, " egid=%u", cred->sc_egid);
+ if (cred0.sc_ngroups) {
+ int i;
+ const char *sep = " groups=";
+ for (i = 0; i < cred0.sc_ngroups; i++) {
+ rb_str_catf(ret, "%s%u", sep, cred->sc_groups[i]);
+ sep = ",";
+ }
+ }
+ rb_str_cat2(ret, " (sockcred)");
+ return 1;
+ }
+ }
+#endif
+ return 0;
+}
+#endif
+
+#if defined(IPPROTO_IP) && defined(IP_RECVDSTADDR) /* 4.4BSD */
+static int
+anc_inspect_ip_recvdstaddr(int level, int type, VALUE data, VALUE ret)
+{
+ if (level == IPPROTO_IP && type == IP_RECVDSTADDR &&
+ RSTRING_LEN(data) == sizeof(struct in_addr)) {
+ struct in_addr addr;
+ char addrbuf[INET_ADDRSTRLEN];
+ memcpy(&addr, RSTRING_PTR(data), sizeof(addr));
+ if (inet_ntop(AF_INET, &addr, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
+ rb_str_cat2(ret, " invalid-address");
+ else
+ rb_str_catf(ret, " %s", addrbuf);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(IPPROTO_IP) && defined(IP_PKTINFO) && defined(HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST) /* GNU/Linux */
+static int
+anc_inspect_ip_pktinfo(int level, int type, VALUE data, VALUE ret)
+{
+ if (level == IPPROTO_IP && type == IP_PKTINFO &&
+ RSTRING_LEN(data) == sizeof(struct in_pktinfo)) {
+ struct in_pktinfo pktinfo;
+ char buf[INET_ADDRSTRLEN > IFNAMSIZ ? INET_ADDRSTRLEN : IFNAMSIZ];
+ memcpy(&pktinfo, RSTRING_PTR(data), sizeof(pktinfo));
+ if (inet_ntop(AF_INET, &pktinfo.ipi_addr, buf, sizeof(buf)) == NULL)
+ rb_str_cat2(ret, " invalid-address");
+ else
+ rb_str_catf(ret, " %s", buf);
+ if (if_indextoname(pktinfo.ipi_ifindex, buf) == NULL)
+ rb_str_catf(ret, " ifindex:%d", pktinfo.ipi_ifindex);
+ else
+ rb_str_catf(ret, " %s", buf);
+ if (inet_ntop(AF_INET, &pktinfo.ipi_spec_dst, buf, sizeof(buf)) == NULL)
+ rb_str_cat2(ret, " spec_dst:invalid-address");
+ else
+ rb_str_catf(ret, " spec_dst:%s", buf);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO) && defined(HAVE_TYPE_STRUCT_IN6_PKTINFO) /* IPv6 RFC3542 */
+static int
+anc_inspect_ipv6_pktinfo(int level, int type, VALUE data, VALUE ret)
+{
+ if (level == IPPROTO_IPV6 && type == IPV6_PKTINFO &&
+ RSTRING_LEN(data) == sizeof(struct in6_pktinfo)) {
+ struct in6_pktinfo *pktinfo = (struct in6_pktinfo *)RSTRING_PTR(data);
+ struct in6_addr addr;
+ unsigned int ifindex;
+ char addrbuf[INET6_ADDRSTRLEN], ifbuf[IFNAMSIZ];
+ memcpy(&addr, &pktinfo->ipi6_addr, sizeof(addr));
+ memcpy(&ifindex, &pktinfo->ipi6_ifindex, sizeof(ifindex));
+ if (inet_ntop(AF_INET6, &addr, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
+ rb_str_cat2(ret, " invalid-address");
+ else
+ rb_str_catf(ret, " %s", addrbuf);
+ if (if_indextoname(ifindex, ifbuf) == NULL)
+ rb_str_catf(ret, " ifindex:%d", ifindex);
+ else
+ rb_str_catf(ret, " %s", ifbuf);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(SCM_TIMESTAMP) /* GNU/Linux, FreeBSD, NetBSD, OpenBSD, MacOS X, Solaris */
+static int
+inspect_timeval_as_abstime(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct timeval)) {
+ struct timeval tv;
+ time_t time;
+ struct tm tm;
+ char buf[32];
+ memcpy((char*)&tv, RSTRING_PTR(data), sizeof(tv));
+ time = tv.tv_sec;
+ tm = *localtime(&time);
+ strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
+ rb_str_catf(ret, " %s.%06ld", buf, (long)tv.tv_usec);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(SCM_TIMESTAMPNS) /* GNU/Linux */
+static int
+inspect_timespec_as_abstime(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct timespec)) {
+ struct timespec ts;
+ struct tm tm;
+ char buf[32];
+ memcpy((char*)&ts, RSTRING_PTR(data), sizeof(ts));
+ tm = *localtime(&ts.tv_sec);
+ strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
+ rb_str_catf(ret, " %s.%09ld", buf, (long)ts.tv_nsec);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(SCM_BINTIME) /* FreeBSD */
+static int
+inspect_bintime_as_abstime(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct bintime)) {
+ struct bintime bt;
+ struct tm tm;
+ uint64_t frac_h, frac_l;
+ uint64_t scale_h, scale_l;
+ uint64_t tmp1, tmp2;
+ uint64_t res_h, res_l;
+ char buf[32];
+ memcpy((char*)&bt, RSTRING_PTR(data), sizeof(bt));
+ tm = *localtime(&bt.sec);
+ strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
+
+ /* res_h = frac * 10**19 / 2**64 */
+
+ frac_h = bt.frac >> 32;
+ frac_l = bt.frac & 0xffffffff;
+
+ scale_h = 0x8ac72304; /* 0x8ac7230489e80000 == 10**19 */
+ scale_l = 0x89e80000;
+
+ res_h = frac_h * scale_h;
+ res_l = frac_l * scale_l;
+
+ tmp1 = frac_h * scale_l;
+ res_h += tmp1 >> 32;
+ tmp2 = res_l;
+ res_l += tmp1 & 0xffffffff;
+ if (res_l < tmp2) res_h++;
+
+ tmp1 = frac_l * scale_h;
+ res_h += tmp1 >> 32;
+ tmp2 = res_l;
+ res_l += tmp1 & 0xffffffff;
+ if (res_l < tmp2) res_h++;
+
+ rb_str_catf(ret, " %s.%019"PRIu64, buf, res_h);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+/*
+ * call-seq:
+ * ancillarydata.inspect => string
+ *
+ * returns a string which shows ancillarydata in human-readable form.
+ *
+ * p Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "").inspect
+ * #=> "#<Socket::AncillaryData: INET6 IPV6 PKTINFO \"\">"
+ */
+static VALUE
+ancillary_inspect(VALUE self)
+{
+ VALUE ret;
+ int family, level, type;
+ VALUE data;
+ ID family_id, level_id, type_id;
+ VALUE vtype;
+ int inspected;
+
+ family = ancillary_family(self);
+ level = ancillary_level(self);
+ type = ancillary_type(self);
+ data = ancillary_data(self);
+
+ ret = rb_sprintf("#<%s:", rb_obj_classname(self));
+
+ family_id = rsock_intern_family_noprefix(family);
+ if (family_id)
+ rb_str_catf(ret, " %s", rb_id2name(family_id));
+ else
+ rb_str_catf(ret, " family:%d", family);
+
+ if (level == SOL_SOCKET) {
+ rb_str_cat2(ret, " SOCKET");
+
+ type_id = rsock_intern_scm_optname(type);
+ if (type_id)
+ rb_str_catf(ret, " %s", rb_id2name(type_id));
+ else
+ rb_str_catf(ret, " cmsg_type:%d", type);
+ }
+ else if (IS_IP_FAMILY(family)) {
+ level_id = rsock_intern_iplevel(level);
+ if (level_id)
+ rb_str_catf(ret, " %s", rb_id2name(level_id));
+ else
+ rb_str_catf(ret, " cmsg_level:%d", level);
+
+ vtype = ip_cmsg_type_to_sym(level, type);
+ if (SYMBOL_P(vtype))
+ rb_str_catf(ret, " %s", rb_id2name(SYM2ID(vtype)));
+ else
+ rb_str_catf(ret, " cmsg_type:%d", type);
+ }
+ else {
+ rb_str_catf(ret, " cmsg_level:%d", level);
+ rb_str_catf(ret, " cmsg_type:%d", type);
+ }
+
+ inspected = 0;
+
+ if (level == SOL_SOCKET)
+ family = AF_UNSPEC;
+
+ switch (family) {
+ case AF_UNSPEC:
+ switch (level) {
+# if defined(SOL_SOCKET)
+ case SOL_SOCKET:
+ switch (type) {
+# if defined(SCM_TIMESTAMP) /* GNU/Linux, FreeBSD, NetBSD, OpenBSD, MacOS X, Solaris */
+ case SCM_TIMESTAMP: inspected = inspect_timeval_as_abstime(level, type, data, ret); break;
+# endif
+# if defined(SCM_TIMESTAMPNS) /* GNU/Linux */
+ case SCM_TIMESTAMPNS: inspected = inspect_timespec_as_abstime(level, type, data, ret); break;
+# endif
+# if defined(SCM_BINTIME) /* FreeBSD */
+ case SCM_BINTIME: inspected = inspect_bintime_as_abstime(level, type, data, ret); break;
+# endif
+# if defined(SCM_RIGHTS) /* 4.4BSD */
+ case SCM_RIGHTS: inspected = anc_inspect_socket_rights(level, type, data, ret); break;
+# endif
+# if defined(SCM_CREDENTIALS) /* GNU/Linux */
+ case SCM_CREDENTIALS: inspected = anc_inspect_passcred_credentials(level, type, data, ret); break;
+# endif
+# if defined(INSPECT_SCM_CREDS) /* NetBSD */
+ case SCM_CREDS: inspected = anc_inspect_socket_creds(level, type, data, ret); break;
+# endif
+ }
+ break;
+# endif
+ }
+ break;
+
+ case AF_INET:
+#ifdef INET6
+ case AF_INET6:
+#endif
+ switch (level) {
+# if defined(IPPROTO_IP)
+ case IPPROTO_IP:
+ switch (type) {
+# if defined(IP_RECVDSTADDR) /* 4.4BSD */
+ case IP_RECVDSTADDR: inspected = anc_inspect_ip_recvdstaddr(level, type, data, ret); break;
+# endif
+# if defined(IP_PKTINFO) && defined(HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST) /* GNU/Linux */
+ case IP_PKTINFO: inspected = anc_inspect_ip_pktinfo(level, type, data, ret); break;
+# endif
+ }
+ break;
+# endif
+
+# if defined(IPPROTO_IPV6)
+ case IPPROTO_IPV6:
+ switch (type) {
+# if defined(IPV6_PKTINFO) && defined(HAVE_TYPE_STRUCT_IN6_PKTINFO) /* RFC 3542 */
+ case IPV6_PKTINFO: inspected = anc_inspect_ipv6_pktinfo(level, type, data, ret); break;
+# endif
+ }
+ break;
+# endif
+ }
+ break;
+ }
+
+ if (!inspected) {
+ rb_str_cat2(ret, " ");
+ rb_str_append(ret, rb_str_dump(data));
+ }
+
+ rb_str_cat2(ret, ">");
+
+ return ret;
+}
+
+/*
+ * call-seq:
+ * ancillarydata.cmsg_is?(level, type) => true or false
+ *
+ * tests the level and type of _ancillarydata_.
+ *
+ * ancdata = Socket::AncillaryData.new(:INET6, :IPV6, :PKTINFO, "")
+ * ancdata.cmsg_is?(Socket::IPPROTO_IPV6, Socket::IPV6_PKTINFO) #=> true
+ * ancdata.cmsg_is?(:IPV6, :PKTINFO) #=> true
+ * ancdata.cmsg_is?(:IP, :PKTINFO) #=> false
+ * ancdata.cmsg_is?(:SOCKET, :RIGHTS) #=> false
+ */
+static VALUE
+ancillary_cmsg_is_p(VALUE self, VALUE vlevel, VALUE vtype)
+{
+ int family = ancillary_family(self);
+ int level = rsock_level_arg(family, vlevel);
+ int type = rsock_cmsg_type_arg(family, level, vtype);
+
+ if (ancillary_level(self) == level &&
+ ancillary_type(self) == type)
+ return Qtrue;
+ else
+ return Qfalse;
+}
+
+#endif
+
+#if defined(HAVE_SENDMSG)
+struct sendmsg_args_struct {
+ int fd;
+ int flags;
+ const struct msghdr *msg;
+};
+
+static void *
+nogvl_sendmsg_func(void *ptr)
+{
+ struct sendmsg_args_struct *args = ptr;
+ return (void *)(VALUE)sendmsg(args->fd, args->msg, args->flags);
+}
+
+static ssize_t
+rb_sendmsg(int fd, const struct msghdr *msg, int flags)
+{
+ struct sendmsg_args_struct args;
+ args.fd = fd;
+ args.msg = msg;
+ args.flags = flags;
+ return (ssize_t)rb_thread_call_without_gvl(nogvl_sendmsg_func, &args, RUBY_UBF_IO, 0);
+}
+
+static VALUE
+bsock_sendmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
+{
+ rb_io_t *fptr;
+ VALUE data, vflags, dest_sockaddr;
+ int controls_num;
+ struct msghdr mh;
+ struct iovec iov;
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ VALUE controls_str = 0;
+ VALUE *controls_ptr = NULL;
+ int family;
+#endif
+ int flags;
+ ssize_t ss;
+
+ GetOpenFile(sock, fptr);
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ family = rsock_getfamily(fptr->fd);
+#endif
+
+ data = vflags = dest_sockaddr = Qnil;
+
+ if (argc == 0)
+ rb_raise(rb_eArgError, "mesg argument required");
+ data = argv[0];
+ if (1 < argc) vflags = argv[1];
+ if (2 < argc) dest_sockaddr = argv[2];
+ controls_num = 3 < argc ? argc - 3 : 0;
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ if (3 < argc) { controls_ptr = &argv[3]; }
+#endif
+
+ StringValue(data);
+
+ if (controls_num) {
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ int i;
+ size_t last_pad = 0;
+#if defined(__NetBSD__)
+ int last_level = 0;
+ int last_type = 0;
+#endif
+ controls_str = rb_str_tmp_new(0);
+ for (i = 0; i < controls_num; i++) {
+ VALUE elt = controls_ptr[i], v;
+ VALUE vlevel, vtype;
+ int level, type;
+ VALUE cdata;
+ long oldlen;
+ struct cmsghdr cmh;
+ char *cmsg;
+ size_t cspace;
+ v = rb_check_convert_type(elt, T_ARRAY, "Array", "to_ary");
+ if (!NIL_P(v)) {
+ elt = v;
+ if (RARRAY_LEN(elt) != 3)
+ rb_raise(rb_eArgError, "an element of controls should be 3-elements array");
+ vlevel = rb_ary_entry(elt, 0);
+ vtype = rb_ary_entry(elt, 1);
+ cdata = rb_ary_entry(elt, 2);
+ }
+ else {
+ vlevel = rb_funcall(elt, rb_intern("level"), 0);
+ vtype = rb_funcall(elt, rb_intern("type"), 0);
+ cdata = rb_funcall(elt, rb_intern("data"), 0);
+ }
+ level = rsock_level_arg(family, vlevel);
+ type = rsock_cmsg_type_arg(family, level, vtype);
+ StringValue(cdata);
+ oldlen = RSTRING_LEN(controls_str);
+ cspace = CMSG_SPACE(RSTRING_LEN(cdata));
+ rb_str_resize(controls_str, oldlen + cspace);
+ cmsg = RSTRING_PTR(controls_str)+oldlen;
+ memset((char *)cmsg, 0, cspace);
+ memset((char *)&cmh, 0, sizeof(cmh));
+ cmh.cmsg_level = level;
+ cmh.cmsg_type = type;
+ cmh.cmsg_len = (socklen_t)CMSG_LEN(RSTRING_LEN(cdata));
+ MEMCPY(cmsg, &cmh, char, sizeof(cmh));
+ MEMCPY(cmsg+((char*)CMSG_DATA(&cmh)-(char*)&cmh), RSTRING_PTR(cdata), char, RSTRING_LEN(cdata));
+#if defined(__NetBSD__)
+ last_level = cmh.cmsg_level;
+ last_type = cmh.cmsg_type;
+#endif
+ last_pad = cspace - cmh.cmsg_len;
+ }
+ if (last_pad) {
+ /*
+ * This code removes the last padding from msg_controllen.
+ *
+ * 4.3BSD-Reno reject the padding for SCM_RIGHTS. (There was no 64bit environments in those days?)
+ * RFC 2292 require the padding.
+ * RFC 3542 relaxes the condition - implementation must accept both as valid.
+ *
+ * Actual problems:
+ *
+ * - NetBSD 4.0.1
+ * SCM_RIGHTS with padding causes EINVAL
+ * IPV6_PKTINFO without padding causes "page fault trap"
+ * http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=40661
+ *
+ * - OpenBSD 4.4
+ * IPV6_PKTINFO without padding causes EINVAL
+ *
+ * Basically, msg_controllen should contains the padding.
+ * So the padding is removed only if a problem really exists.
+ */
+#if defined(__NetBSD__)
+ if (last_level == SOL_SOCKET && last_type == SCM_RIGHTS)
+ rb_str_set_len(controls_str, RSTRING_LEN(controls_str)-last_pad);
+#endif
+ }
+#else
+ rb_raise(rb_eNotImpError, "control message for sendmsg is unimplemented");
+#endif
+ }
+
+ flags = NIL_P(vflags) ? 0 : NUM2INT(vflags);
+#ifdef MSG_DONTWAIT
+ if (nonblock)
+ flags |= MSG_DONTWAIT;
+#endif
+
+ if (!NIL_P(dest_sockaddr))
+ SockAddrStringValue(dest_sockaddr);
+
+ rb_io_check_closed(fptr);
+
+ retry:
+ memset(&mh, 0, sizeof(mh));
+ if (!NIL_P(dest_sockaddr)) {
+ mh.msg_name = RSTRING_PTR(dest_sockaddr);
+ mh.msg_namelen = RSTRING_SOCKLEN(dest_sockaddr);
+ }
+ mh.msg_iovlen = 1;
+ mh.msg_iov = &iov;
+ iov.iov_base = RSTRING_PTR(data);
+ iov.iov_len = RSTRING_LEN(data);
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ if (controls_str) {
+ mh.msg_control = RSTRING_PTR(controls_str);
+ mh.msg_controllen = RSTRING_SOCKLEN(controls_str);
+ }
+ else {
+ mh.msg_control = NULL;
+ mh.msg_controllen = 0;
+ }
+#endif
+
+ rb_io_check_closed(fptr);
+ if (nonblock)
+ rb_io_set_nonblock(fptr);
+
+ ss = rb_sendmsg(fptr->fd, &mh, flags);
+
+ if (ss == -1) {
+ if (!nonblock && rb_io_wait_writable(fptr->fd)) {
+ rb_io_check_closed(fptr);
+ goto retry;
+ }
+ if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN))
+ rb_readwrite_sys_fail(RB_IO_WAIT_WRITABLE, "sendmsg(2) would block");
+ rb_sys_fail("sendmsg(2)");
+ }
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ RB_GC_GUARD(controls_str);
+#endif
+
+ return SSIZET2NUM(ss);
+}
+#endif
+
+#if defined(HAVE_SENDMSG)
+/*
+ * call-seq:
+ * basicsocket.sendmsg(mesg, flags=0, dest_sockaddr=nil, *controls) => numbytes_sent
+ *
+ * sendmsg sends a message using sendmsg(2) system call in blocking manner.
+ *
+ * _mesg_ is a string to send.
+ *
+ * _flags_ is bitwise OR of MSG_* constants such as Socket::MSG_OOB.
+ *
+ * _dest_sockaddr_ is a destination socket address for connection-less socket.
+ * It should be a sockaddr such as a result of Socket.sockaddr_in.
+ * An Addrinfo object can be used too.
+ *
+ * _controls_ is a list of ancillary data.
+ * The element of _controls_ should be Socket::AncillaryData or
+ * 3-elements array.
+ * The 3-element array should contains cmsg_level, cmsg_type and data.
+ *
+ * The return value, _numbytes_sent_ is an integer which is the number of bytes sent.
+ *
+ * sendmsg can be used to implement send_io as follows:
+ *
+ * # use Socket::AncillaryData.
+ * ancdata = Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, io.fileno)
+ * sock.sendmsg("a", 0, nil, ancdata)
+ *
+ * # use 3-element array.
+ * ancdata = [:SOCKET, :RIGHTS, [io.fileno].pack("i!")]
+ * sock.sendmsg("\0", 0, nil, ancdata)
+ *
+ */
+VALUE
+rsock_bsock_sendmsg(int argc, VALUE *argv, VALUE sock)
+{
+ return bsock_sendmsg_internal(argc, argv, sock, 0);
+}
+#endif
+
+#if defined(HAVE_SENDMSG)
+/*
+ * call-seq:
+ * basicsocket.sendmsg_nonblock(mesg, flags=0, dest_sockaddr=nil, *controls) => numbytes_sent
+ *
+ * sendmsg_nonblock sends a message using sendmsg(2) system call in non-blocking manner.
+ *
+ * It is similar to BasicSocket#sendmsg
+ * but the non-blocking flag is set before the system call
+ * and it doesn't retry the system call.
+ *
+ */
+VALUE
+rsock_bsock_sendmsg_nonblock(int argc, VALUE *argv, VALUE sock)
+{
+ return bsock_sendmsg_internal(argc, argv, sock, 1);
+}
+#endif
+
+#if defined(HAVE_RECVMSG)
+struct recvmsg_args_struct {
+ int fd;
+ int flags;
+ struct msghdr *msg;
+};
+
+ssize_t
+rsock_recvmsg(int socket, struct msghdr *message, int flags)
+{
+ ssize_t ret;
+ socklen_t len0;
+#ifdef MSG_CMSG_CLOEXEC
+ /* MSG_CMSG_CLOEXEC is available since Linux 2.6.23. Linux 2.6.18 silently ignore it. */
+ flags |= MSG_CMSG_CLOEXEC;
+#endif
+ len0 = message->msg_namelen;
+ ret = recvmsg(socket, message, flags);
+ if (ret != -1 && len0 < message->msg_namelen)
+ message->msg_namelen = len0;
+ return ret;
+}
+
+static void *
+nogvl_recvmsg_func(void *ptr)
+{
+ struct recvmsg_args_struct *args = ptr;
+ int flags = args->flags;
+ return (void *)rsock_recvmsg(args->fd, args->msg, flags);
+}
+
+static ssize_t
+rb_recvmsg(int fd, struct msghdr *msg, int flags)
+{
+ struct recvmsg_args_struct args;
+ args.fd = fd;
+ args.msg = msg;
+ args.flags = flags;
+ return (ssize_t)rb_thread_call_without_gvl(nogvl_recvmsg_func, &args, RUBY_UBF_IO, 0);
+}
+
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+static void
+discard_cmsg(struct cmsghdr *cmh, char *msg_end, int msg_peek_p)
+{
+# if !defined(FD_PASSING_WORK_WITH_RECVMSG_MSG_PEEK)
+ /*
+ * FreeBSD 8.2.0, NetBSD 5 and MacOS X Snow Leopard doesn't
+ * allocate fds by recvmsg with MSG_PEEK.
+ * [ruby-dev:44189]
+ * http://bugs.ruby-lang.org/issues/5075
+ *
+ * Linux 2.6.38 allocate fds by recvmsg with MSG_PEEK.
+ */
+ if (msg_peek_p)
+ return;
+# endif
+ if (cmh->cmsg_level == SOL_SOCKET && cmh->cmsg_type == SCM_RIGHTS) {
+ int *fdp = (int *)CMSG_DATA(cmh);
+ int *end = (int *)((char *)cmh + cmh->cmsg_len);
+ while ((char *)fdp + sizeof(int) <= (char *)end &&
+ (char *)fdp + sizeof(int) <= msg_end) {
+ rb_update_max_fd(*fdp);
+ close(*fdp);
+ fdp++;
+ }
+ }
+}
+#endif
+
+void
+rsock_discard_cmsg_resource(struct msghdr *mh, int msg_peek_p)
+{
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ struct cmsghdr *cmh;
+ char *msg_end;
+
+ if (mh->msg_controllen == 0)
+ return;
+
+ msg_end = (char *)mh->msg_control + mh->msg_controllen;
+
+ for (cmh = CMSG_FIRSTHDR(mh); cmh != NULL; cmh = CMSG_NXTHDR(mh, cmh)) {
+ discard_cmsg(cmh, msg_end, msg_peek_p);
+ }
+#endif
+}
+
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+static void
+make_io_for_unix_rights(VALUE ctl, struct cmsghdr *cmh, char *msg_end)
+{
+ if (cmh->cmsg_level == SOL_SOCKET && cmh->cmsg_type == SCM_RIGHTS) {
+ int *fdp, *end;
+ VALUE ary = rb_ary_new();
+ rb_ivar_set(ctl, rb_intern("unix_rights"), ary);
+ fdp = (int *)CMSG_DATA(cmh);
+ end = (int *)((char *)cmh + cmh->cmsg_len);
+ while ((char *)fdp + sizeof(int) <= (char *)end &&
+ (char *)fdp + sizeof(int) <= msg_end) {
+ int fd = *fdp;
+ struct stat stbuf;
+ VALUE io;
+ if (fstat(fd, &stbuf) == -1)
+ rb_raise(rb_eSocket, "invalid fd in SCM_RIGHTS");
+ rb_update_max_fd(fd);
+ if (rsock_cmsg_cloexec_state < 0)
+ rsock_cmsg_cloexec_state = rsock_detect_cloexec(fd);
+ if (rsock_cmsg_cloexec_state == 0 || fd <= 2)
+ rb_maygvl_fd_fix_cloexec(fd);
+ if (S_ISSOCK(stbuf.st_mode))
+ io = rsock_init_sock(rb_obj_alloc(rb_cSocket), fd);
+ else
+ io = rb_io_fdopen(fd, O_RDWR, NULL);
+ ary = rb_attr_get(ctl, rb_intern("unix_rights"));
+ rb_ary_push(ary, io);
+ fdp++;
+ }
+ OBJ_FREEZE(ary);
+ }
+}
+#endif
+
+static VALUE
+bsock_recvmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
+{
+ rb_io_t *fptr;
+ VALUE vmaxdatlen, vmaxctllen, vflags;
+ VALUE vopts;
+ int grow_buffer;
+ size_t maxdatlen;
+ int flags, orig_flags;
+ struct msghdr mh;
+ struct iovec iov;
+ union_sockaddr namebuf;
+ char datbuf0[4096], *datbuf;
+ VALUE dat_str = Qnil;
+ VALUE ret;
+ ssize_t ss;
+ int request_scm_rights;
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ struct cmsghdr *cmh;
+ size_t maxctllen;
+ union {
+ char bytes[4096];
+ struct cmsghdr align;
+ } ctlbuf0;
+ char *ctlbuf;
+ VALUE ctl_str = Qnil;
+ int family;
+ int gc_done = 0;
+#endif
+
+
+ rb_scan_args(argc, argv, "03:", &vmaxdatlen, &vflags, &vmaxctllen, &vopts);
+
+ maxdatlen = NIL_P(vmaxdatlen) ? sizeof(datbuf0) : NUM2SIZET(vmaxdatlen);
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ maxctllen = NIL_P(vmaxctllen) ? sizeof(ctlbuf0) : NUM2SIZET(vmaxctllen);
+#else
+ if (!NIL_P(vmaxctllen))
+ rb_raise(rb_eArgError, "control message not supported");
+#endif
+ flags = NIL_P(vflags) ? 0 : NUM2INT(vflags);
+#ifdef MSG_DONTWAIT
+ if (nonblock)
+ flags |= MSG_DONTWAIT;
+#endif
+ orig_flags = flags;
+
+ grow_buffer = NIL_P(vmaxdatlen) || NIL_P(vmaxctllen);
+
+ request_scm_rights = 0;
+ if (!NIL_P(vopts) && RTEST(rb_hash_aref(vopts, ID2SYM(rb_intern("scm_rights")))))
+ request_scm_rights = 1;
+#if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ if (request_scm_rights)
+ rb_raise(rb_eNotImpError, "control message for recvmsg is unimplemented");
+#endif
+
+ GetOpenFile(sock, fptr);
+ if (rb_io_read_pending(fptr)) {
+ rb_raise(rb_eIOError, "recvmsg for buffered IO");
+ }
+
+#if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ if (grow_buffer) {
+ int socktype;
+ socklen_t optlen = (socklen_t)sizeof(socktype);
+ if (getsockopt(fptr->fd, SOL_SOCKET, SO_TYPE, (void*)&socktype, &optlen) == -1) {
+ rb_sys_fail("getsockopt(SO_TYPE)");
+ }
+ if (socktype == SOCK_STREAM)
+ grow_buffer = 0;
+ }
+#endif
+
+ retry:
+ if (maxdatlen <= sizeof(datbuf0))
+ datbuf = datbuf0;
+ else {
+ if (NIL_P(dat_str))
+ dat_str = rb_str_tmp_new(maxdatlen);
+ else
+ rb_str_resize(dat_str, maxdatlen);
+ datbuf = RSTRING_PTR(dat_str);
+ }
+
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ if (maxctllen <= sizeof(ctlbuf0))
+ ctlbuf = ctlbuf0.bytes;
+ else {
+ if (NIL_P(ctl_str))
+ ctl_str = rb_str_tmp_new(maxctllen);
+ else
+ rb_str_resize(ctl_str, maxctllen);
+ ctlbuf = RSTRING_PTR(ctl_str);
+ }
+#endif
+
+ memset(&mh, 0, sizeof(mh));
+
+ memset(&namebuf, 0, sizeof(namebuf));
+ mh.msg_name = &namebuf.addr;
+ mh.msg_namelen = (socklen_t)sizeof(namebuf);
+
+ mh.msg_iov = &iov;
+ mh.msg_iovlen = 1;
+ iov.iov_base = datbuf;
+ iov.iov_len = maxdatlen;
+
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ mh.msg_control = ctlbuf;
+ mh.msg_controllen = (socklen_t)maxctllen;
+#endif
+
+ if (grow_buffer)
+ flags |= MSG_PEEK;
+
+ rb_io_check_closed(fptr);
+ if (nonblock)
+ rb_io_set_nonblock(fptr);
+
+ ss = rb_recvmsg(fptr->fd, &mh, flags);
+
+ if (ss == -1) {
+ if (!nonblock && rb_io_wait_readable(fptr->fd)) {
+ rb_io_check_closed(fptr);
+ goto retry;
+ }
+ if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN))
+ rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "recvmsg(2) would block");
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ if (!gc_done && (errno == EMFILE || errno == EMSGSIZE)) {
+ /*
+ * When SCM_RIGHTS hit the file descriptors limit:
+ * - Linux 2.6.18 causes success with MSG_CTRUNC
+ * - MacOS X 10.4 causes EMSGSIZE (and lost file descriptors?)
+ * - Solaris 11 causes EMFILE
+ */
+ gc_and_retry:
+ rb_gc();
+ gc_done = 1;
+ goto retry;
+ }
+#endif
+ rb_sys_fail("recvmsg(2)");
+ }
+
+ if (grow_buffer) {
+ int grown = 0;
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ if (NIL_P(vmaxdatlen) && (mh.msg_flags & MSG_TRUNC)) {
+ if (SIZE_MAX/2 < maxdatlen)
+ rb_raise(rb_eArgError, "max data length too big");
+ maxdatlen *= 2;
+ grown = 1;
+ }
+ if (NIL_P(vmaxctllen) && (mh.msg_flags & MSG_CTRUNC)) {
+#define BIG_ENOUGH_SPACE 65536
+ if (BIG_ENOUGH_SPACE < maxctllen &&
+ (socklen_t)mh.msg_controllen < (socklen_t)(maxctllen - BIG_ENOUGH_SPACE)) {
+ /* there are big space bug truncated.
+ * file descriptors limit? */
+ if (!gc_done) {
+ rsock_discard_cmsg_resource(&mh, (flags & MSG_PEEK) != 0);
+ goto gc_and_retry;
+ }
+ }
+ else {
+ if (SIZE_MAX/2 < maxctllen)
+ rb_raise(rb_eArgError, "max control message length too big");
+ maxctllen *= 2;
+ grown = 1;
+ }
+#undef BIG_ENOUGH_SPACE
+ }
+#else
+ if (NIL_P(vmaxdatlen) && ss != -1 && ss == (ssize_t)iov.iov_len) {
+ if (SIZE_MAX/2 < maxdatlen)
+ rb_raise(rb_eArgError, "max data length too big");
+ maxdatlen *= 2;
+ grown = 1;
+ }
+#endif
+ if (grown) {
+ rsock_discard_cmsg_resource(&mh, (flags & MSG_PEEK) != 0);
+ goto retry;
+ }
+ else {
+ grow_buffer = 0;
+ if (flags != orig_flags) {
+ rsock_discard_cmsg_resource(&mh, (flags & MSG_PEEK) != 0);
+ flags = orig_flags;
+ goto retry;
+ }
+ }
+ }
+
+ if (NIL_P(dat_str))
+ dat_str = rb_tainted_str_new(datbuf, ss);
+ else {
+ rb_str_resize(dat_str, ss);
+ OBJ_TAINT(dat_str);
+ rb_obj_reveal(dat_str, rb_cString);
+ }
+
+ ret = rb_ary_new3(3, dat_str,
+ rsock_io_socket_addrinfo(sock, mh.msg_name, mh.msg_namelen),
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ INT2NUM(mh.msg_flags)
+#else
+ Qnil
+#endif
+ );
+
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ family = rsock_getfamily(fptr->fd);
+ if (mh.msg_controllen) {
+ char *msg_end = (char *)mh.msg_control + mh.msg_controllen;
+ for (cmh = CMSG_FIRSTHDR(&mh); cmh != NULL; cmh = CMSG_NXTHDR(&mh, cmh)) {
+ VALUE ctl;
+ char *ctl_end;
+ size_t clen;
+ if (cmh->cmsg_len == 0) {
+ rb_raise(rb_eTypeError, "invalid control message (cmsg_len == 0)");
+ }
+ ctl_end = (char*)cmh + cmh->cmsg_len;
+ clen = (ctl_end <= msg_end ? ctl_end : msg_end) - (char*)CMSG_DATA(cmh);
+ ctl = ancdata_new(family, cmh->cmsg_level, cmh->cmsg_type, rb_tainted_str_new((char*)CMSG_DATA(cmh), clen));
+ if (request_scm_rights)
+ make_io_for_unix_rights(ctl, cmh, msg_end);
+ else
+ discard_cmsg(cmh, msg_end, (flags & MSG_PEEK) != 0);
+ rb_ary_push(ret, ctl);
+ }
+ RB_GC_GUARD(ctl_str);
+ }
+#endif
+
+ return ret;
+}
+#endif
+
+#if defined(HAVE_RECVMSG)
+/*
+ * call-seq:
+ * basicsocket.recvmsg(maxmesglen=nil, flags=0, maxcontrollen=nil, opts={}) => [mesg, sender_addrinfo, rflags, *controls]
+ *
+ * recvmsg receives a message using recvmsg(2) system call in blocking manner.
+ *
+ * _maxmesglen_ is the maximum length of mesg to receive.
+ *
+ * _flags_ is bitwise OR of MSG_* constants such as Socket::MSG_PEEK.
+ *
+ * _maxcontrollen_ is the maximum length of controls (ancillary data) to receive.
+ *
+ * _opts_ is option hash.
+ * Currently :scm_rights=>bool is the only option.
+ *
+ * :scm_rights option specifies that application expects SCM_RIGHTS control message.
+ * If the value is nil or false, application don't expects SCM_RIGHTS control message.
+ * In this case, recvmsg closes the passed file descriptors immediately.
+ * This is the default behavior.
+ *
+ * If :scm_rights value is neither nil nor false, application expects SCM_RIGHTS control message.
+ * In this case, recvmsg creates IO objects for each file descriptors for
+ * Socket::AncillaryData#unix_rights method.
+ *
+ * The return value is 4-elements array.
+ *
+ * _mesg_ is a string of the received message.
+ *
+ * _sender_addrinfo_ is a sender socket address for connection-less socket.
+ * It is an Addrinfo object.
+ * For connection-oriented socket such as TCP, sender_addrinfo is platform dependent.
+ *
+ * _rflags_ is a flags on the received message which is bitwise OR of MSG_* constants such as Socket::MSG_TRUNC.
+ * It will be nil if the system uses 4.3BSD style old recvmsg system call.
+ *
+ * _controls_ is ancillary data which is an array of Socket::AncillaryData objects such as:
+ *
+ * #<Socket::AncillaryData: AF_UNIX SOCKET RIGHTS 7>
+ *
+ * _maxmesglen_ and _maxcontrollen_ can be nil.
+ * In that case, the buffer will be grown until the message is not truncated.
+ * Internally, MSG_PEEK is used and MSG_TRUNC/MSG_CTRUNC are checked.
+ *
+ * recvmsg can be used to implement recv_io as follows:
+ *
+ * mesg, sender_sockaddr, rflags, *controls = sock.recvmsg(:scm_rights=>true)
+ * controls.each {|ancdata|
+ * if ancdata.cmsg_is?(:SOCKET, :RIGHTS)
+ * return ancdata.unix_rights[0]
+ * end
+ * }
+ *
+ */
+VALUE
+rsock_bsock_recvmsg(int argc, VALUE *argv, VALUE sock)
+{
+ return bsock_recvmsg_internal(argc, argv, sock, 0);
+}
+#endif
+
+#if defined(HAVE_RECVMSG)
+/*
+ * call-seq:
+ * basicsocket.recvmsg_nonblock(maxdatalen=nil, flags=0, maxcontrollen=nil, opts={}) => [data, sender_addrinfo, rflags, *controls]
+ *
+ * recvmsg receives a message using recvmsg(2) system call in non-blocking manner.
+ *
+ * It is similar to BasicSocket#recvmsg
+ * but non-blocking flag is set before the system call
+ * and it doesn't retry the system call.
+ *
+ */
+VALUE
+rsock_bsock_recvmsg_nonblock(int argc, VALUE *argv, VALUE sock)
+{
+ return bsock_recvmsg_internal(argc, argv, sock, 1);
+}
+#endif
+
+void
+rsock_init_ancdata(void)
+{
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
+ /*
+ * Document-class: Socket::AncillaryData
+ *
+ * Socket::AncillaryData represents the ancillary data (control information)
+ * used by sendmsg and recvmsg system call. It contains socket #family,
+ * control message (cmsg) #level, cmsg #type and cmsg #data.
+ */
+ rb_cAncillaryData = rb_define_class_under(rb_cSocket, "AncillaryData", rb_cObject);
+ rb_define_method(rb_cAncillaryData, "initialize", ancillary_initialize, 4);
+ rb_define_method(rb_cAncillaryData, "inspect", ancillary_inspect, 0);
+ rb_define_method(rb_cAncillaryData, "family", ancillary_family_m, 0);
+ rb_define_method(rb_cAncillaryData, "level", ancillary_level_m, 0);
+ rb_define_method(rb_cAncillaryData, "type", ancillary_type_m, 0);
+ rb_define_method(rb_cAncillaryData, "data", ancillary_data, 0);
+
+ rb_define_method(rb_cAncillaryData, "cmsg_is?", ancillary_cmsg_is_p, 2);
+
+ rb_define_singleton_method(rb_cAncillaryData, "int", ancillary_s_int, 4);
+ rb_define_method(rb_cAncillaryData, "int", ancillary_int, 0);
+
+ rb_define_singleton_method(rb_cAncillaryData, "unix_rights", ancillary_s_unix_rights, -1);
+ rb_define_method(rb_cAncillaryData, "unix_rights", ancillary_unix_rights, 0);
+
+ rb_define_method(rb_cAncillaryData, "timestamp", ancillary_timestamp, 0);
+
+ rb_define_singleton_method(rb_cAncillaryData, "ip_pktinfo", ancillary_s_ip_pktinfo, -1);
+ rb_define_method(rb_cAncillaryData, "ip_pktinfo", ancillary_ip_pktinfo, 0);
+
+ rb_define_singleton_method(rb_cAncillaryData, "ipv6_pktinfo", ancillary_s_ipv6_pktinfo, 2);
+ rb_define_method(rb_cAncillaryData, "ipv6_pktinfo", ancillary_ipv6_pktinfo, 0);
+ rb_define_method(rb_cAncillaryData, "ipv6_pktinfo_addr", ancillary_ipv6_pktinfo_addr, 0);
+ rb_define_method(rb_cAncillaryData, "ipv6_pktinfo_ifindex", ancillary_ipv6_pktinfo_ifindex, 0);
+#endif
+}
diff --git a/jni/ruby/ext/socket/basicsocket.c b/jni/ruby/ext/socket/basicsocket.c
new file mode 100644
index 0000000..5455977
--- /dev/null
+++ b/jni/ruby/ext/socket/basicsocket.c
@@ -0,0 +1,767 @@
+/************************************************
+
+ basicsocket.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+/*
+ * call-seq:
+ * BasicSocket.for_fd(fd) => basicsocket
+ *
+ * Returns a socket object which contains the file descriptor, _fd_.
+ *
+ * # If invoked by inetd, STDIN/STDOUT/STDERR is a socket.
+ * STDIN_SOCK = Socket.for_fd(STDIN.fileno)
+ * p STDIN_SOCK.remote_address
+ *
+ */
+static VALUE
+bsock_s_for_fd(VALUE klass, VALUE fd)
+{
+ rb_io_t *fptr;
+ VALUE sock = rsock_init_sock(rb_obj_alloc(klass), NUM2INT(fd));
+
+ GetOpenFile(sock, fptr);
+
+ return sock;
+}
+
+/*
+ * call-seq:
+ * basicsocket.shutdown([how]) => 0
+ *
+ * Calls shutdown(2) system call.
+ *
+ * s.shutdown(Socket::SHUT_RD) disallows further read.
+ *
+ * s.shutdown(Socket::SHUT_WR) disallows further write.
+ *
+ * s.shutdown(Socket::SHUT_RDWR) disallows further read and write.
+ *
+ * _how_ can be symbol or string:
+ * - :RD, :SHUT_RD, "RD" and "SHUT_RD" are accepted as Socket::SHUT_RD.
+ * - :WR, :SHUT_WR, "WR" and "SHUT_WR" are accepted as Socket::SHUT_WR.
+ * - :RDWR, :SHUT_RDWR, "RDWR" and "SHUT_RDWR" are accepted as Socket::SHUT_RDWR.
+ *
+ * UNIXSocket.pair {|s1, s2|
+ * s1.puts "ping"
+ * s1.shutdown(:WR)
+ * p s2.read #=> "ping\n"
+ * s2.puts "pong"
+ * s2.close
+ * p s1.read #=> "pong\n"
+ * }
+ *
+ */
+static VALUE
+bsock_shutdown(int argc, VALUE *argv, VALUE sock)
+{
+ VALUE howto;
+ int how;
+ rb_io_t *fptr;
+
+ rb_scan_args(argc, argv, "01", &howto);
+ if (howto == Qnil)
+ how = SHUT_RDWR;
+ else {
+ how = rsock_shutdown_how_arg(howto);
+ if (how != SHUT_WR && how != SHUT_RD && how != SHUT_RDWR) {
+ rb_raise(rb_eArgError, "`how' should be either :SHUT_RD, :SHUT_WR, :SHUT_RDWR");
+ }
+ }
+ GetOpenFile(sock, fptr);
+ if (shutdown(fptr->fd, how) == -1)
+ rb_sys_fail("shutdown(2)");
+
+ return INT2FIX(0);
+}
+
+/*
+ * call-seq:
+ * basicsocket.close_read => nil
+ *
+ * Disallows further read using shutdown system call.
+ *
+ * s1, s2 = UNIXSocket.pair
+ * s1.close_read
+ * s2.puts #=> Broken pipe (Errno::EPIPE)
+ */
+static VALUE
+bsock_close_read(VALUE sock)
+{
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ shutdown(fptr->fd, 0);
+ if (!(fptr->mode & FMODE_WRITABLE)) {
+ return rb_io_close(sock);
+ }
+ fptr->mode &= ~FMODE_READABLE;
+
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * basicsocket.close_write => nil
+ *
+ * Disallows further write using shutdown system call.
+ *
+ * UNIXSocket.pair {|s1, s2|
+ * s1.print "ping"
+ * s1.close_write
+ * p s2.read #=> "ping"
+ * s2.print "pong"
+ * s2.close
+ * p s1.read #=> "pong"
+ * }
+ */
+static VALUE
+bsock_close_write(VALUE sock)
+{
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ if (!(fptr->mode & FMODE_READABLE)) {
+ return rb_io_close(sock);
+ }
+ shutdown(fptr->fd, 1);
+ fptr->mode &= ~FMODE_WRITABLE;
+
+ return Qnil;
+}
+
+/*
+ * Document-method: setsockopt
+ * call-seq:
+ * setsockopt(level, optname, optval)
+ * setsockopt(socketoption)
+ *
+ * Sets a socket option. These are protocol and system specific, see your
+ * local system documentation for details.
+ *
+ * === Parameters
+ * * +level+ is an integer, usually one of the SOL_ constants such as
+ * Socket::SOL_SOCKET, or a protocol level.
+ * A string or symbol of the name, possibly without prefix, is also
+ * accepted.
+ * * +optname+ is an integer, usually one of the SO_ constants, such
+ * as Socket::SO_REUSEADDR.
+ * A string or symbol of the name, possibly without prefix, is also
+ * accepted.
+ * * +optval+ is the value of the option, it is passed to the underlying
+ * setsockopt() as a pointer to a certain number of bytes. How this is
+ * done depends on the type:
+ * - Fixnum: value is assigned to an int, and a pointer to the int is
+ * passed, with length of sizeof(int).
+ * - true or false: 1 or 0 (respectively) is assigned to an int, and the
+ * int is passed as for a Fixnum. Note that +false+ must be passed,
+ * not +nil+.
+ * - String: the string's data and length is passed to the socket.
+ * * +socketoption+ is an instance of Socket::Option
+ *
+ * === Examples
+ *
+ * Some socket options are integers with boolean values, in this case
+ * #setsockopt could be called like this:
+ * sock.setsockopt(:SOCKET, :REUSEADDR, true)
+ * sock.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
+ * sock.setsockopt(Socket::Option.bool(:INET, :SOCKET, :REUSEADDR, true))
+ *
+ * Some socket options are integers with numeric values, in this case
+ * #setsockopt could be called like this:
+ * sock.setsockopt(:IP, :TTL, 255)
+ * sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, 255)
+ * sock.setsockopt(Socket::Option.int(:INET, :IP, :TTL, 255))
+ *
+ * Option values may be structs. Passing them can be complex as it involves
+ * examining your system headers to determine the correct definition. An
+ * example is an +ip_mreq+, which may be defined in your system headers as:
+ * struct ip_mreq {
+ * struct in_addr imr_multiaddr;
+ * struct in_addr imr_interface;
+ * };
+ *
+ * In this case #setsockopt could be called like this:
+ * optval = IPAddr.new("224.0.0.251").hton +
+ * IPAddr.new(Socket::INADDR_ANY, Socket::AF_INET).hton
+ * sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, optval)
+ *
+*/
+static VALUE
+bsock_setsockopt(int argc, VALUE *argv, VALUE sock)
+{
+ VALUE lev, optname, val;
+ int family, level, option;
+ rb_io_t *fptr;
+ int i;
+ char *v;
+ int vlen;
+
+ if (argc == 1) {
+ lev = rb_funcall(argv[0], rb_intern("level"), 0);
+ optname = rb_funcall(argv[0], rb_intern("optname"), 0);
+ val = rb_funcall(argv[0], rb_intern("data"), 0);
+ }
+ else {
+ rb_scan_args(argc, argv, "30", &lev, &optname, &val);
+ }
+
+ rb_secure(2);
+ GetOpenFile(sock, fptr);
+ family = rsock_getfamily(fptr->fd);
+ level = rsock_level_arg(family, lev);
+ option = rsock_optname_arg(family, level, optname);
+
+ switch (TYPE(val)) {
+ case T_FIXNUM:
+ i = FIX2INT(val);
+ goto numval;
+ case T_FALSE:
+ i = 0;
+ goto numval;
+ case T_TRUE:
+ i = 1;
+ numval:
+ v = (char*)&i; vlen = (int)sizeof(i);
+ break;
+ default:
+ StringValue(val);
+ v = RSTRING_PTR(val);
+ vlen = RSTRING_SOCKLEN(val);
+ break;
+ }
+
+ rb_io_check_closed(fptr);
+ if (setsockopt(fptr->fd, level, option, v, vlen) < 0)
+ rsock_sys_fail_path("setsockopt(2)", fptr->pathv);
+
+ return INT2FIX(0);
+}
+
+#if !defined(__BEOS__)
+/*
+ * Document-method: getsockopt
+ * call-seq:
+ * getsockopt(level, optname) => socketoption
+ *
+ * Gets a socket option. These are protocol and system specific, see your
+ * local system documentation for details. The option is returned as
+ * a Socket::Option object.
+ *
+ * === Parameters
+ * * +level+ is an integer, usually one of the SOL_ constants such as
+ * Socket::SOL_SOCKET, or a protocol level.
+ * A string or symbol of the name, possibly without prefix, is also
+ * accepted.
+ * * +optname+ is an integer, usually one of the SO_ constants, such
+ * as Socket::SO_REUSEADDR.
+ * A string or symbol of the name, possibly without prefix, is also
+ * accepted.
+ *
+ * === Examples
+ *
+ * Some socket options are integers with boolean values, in this case
+ * #getsockopt could be called like this:
+ *
+ * reuseaddr = sock.getsockopt(:SOCKET, :REUSEADDR).bool
+ *
+ * optval = sock.getsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR)
+ * optval = optval.unpack "i"
+ * reuseaddr = optval[0] == 0 ? false : true
+ *
+ * Some socket options are integers with numeric values, in this case
+ * #getsockopt could be called like this:
+ *
+ * ipttl = sock.getsockopt(:IP, :TTL).int
+ *
+ * optval = sock.getsockopt(Socket::IPPROTO_IP, Socket::IP_TTL)
+ * ipttl = optval.unpack("i")[0]
+ *
+ * Option values may be structs. Decoding them can be complex as it involves
+ * examining your system headers to determine the correct definition. An
+ * example is a +struct linger+, which may be defined in your system headers
+ * as:
+ * struct linger {
+ * int l_onoff;
+ * int l_linger;
+ * };
+ *
+ * In this case #getsockopt could be called like this:
+ *
+ * # Socket::Option knows linger structure.
+ * onoff, linger = sock.getsockopt(:SOCKET, :LINGER).linger
+ *
+ * optval = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER)
+ * onoff, linger = optval.unpack "ii"
+ * onoff = onoff == 0 ? false : true
+*/
+static VALUE
+bsock_getsockopt(VALUE sock, VALUE lev, VALUE optname)
+{
+ int level, option;
+ socklen_t len;
+ char *buf;
+ rb_io_t *fptr;
+ int family;
+
+ GetOpenFile(sock, fptr);
+ family = rsock_getfamily(fptr->fd);
+ level = rsock_level_arg(family, lev);
+ option = rsock_optname_arg(family, level, optname);
+ len = 256;
+ buf = ALLOCA_N(char,len);
+
+ rb_io_check_closed(fptr);
+
+ if (getsockopt(fptr->fd, level, option, buf, &len) < 0)
+ rsock_sys_fail_path("getsockopt(2)", fptr->pathv);
+
+ return rsock_sockopt_new(family, level, option, rb_str_new(buf, len));
+}
+#else
+#define bsock_getsockopt rb_f_notimplement
+#endif
+
+/*
+ * call-seq:
+ * basicsocket.getsockname => sockaddr
+ *
+ * Returns the local address of the socket as a sockaddr string.
+ *
+ * TCPServer.open("127.0.0.1", 15120) {|serv|
+ * p serv.getsockname #=> "\x02\x00;\x10\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
+ * }
+ *
+ * If Addrinfo object is preferred over the binary string,
+ * use BasicSocket#local_address.
+ */
+static VALUE
+bsock_getsockname(VALUE sock)
+{
+ union_sockaddr buf;
+ socklen_t len = (socklen_t)sizeof buf;
+ socklen_t len0 = len;
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ if (getsockname(fptr->fd, &buf.addr, &len) < 0)
+ rb_sys_fail("getsockname(2)");
+ if (len0 < len) len = len0;
+ return rb_str_new((char*)&buf, len);
+}
+
+/*
+ * call-seq:
+ * basicsocket.getpeername => sockaddr
+ *
+ * Returns the remote address of the socket as a sockaddr string.
+ *
+ * TCPServer.open("127.0.0.1", 1440) {|serv|
+ * c = TCPSocket.new("127.0.0.1", 1440)
+ * s = serv.accept
+ * p s.getpeername #=> "\x02\x00\x82u\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
+ * }
+ *
+ * If Addrinfo object is preferred over the binary string,
+ * use BasicSocket#remote_address.
+ *
+ */
+static VALUE
+bsock_getpeername(VALUE sock)
+{
+ union_sockaddr buf;
+ socklen_t len = (socklen_t)sizeof buf;
+ socklen_t len0 = len;
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ if (getpeername(fptr->fd, &buf.addr, &len) < 0)
+ rb_sys_fail("getpeername(2)");
+ if (len0 < len) len = len0;
+ return rb_str_new((char*)&buf, len);
+}
+
+#if defined(HAVE_GETPEEREID) || defined(SO_PEERCRED) || defined(HAVE_GETPEERUCRED)
+/*
+ * call-seq:
+ * basicsocket.getpeereid => [euid, egid]
+ *
+ * Returns the user and group on the peer of the UNIX socket.
+ * The result is a two element array which contains the effective uid and the effective gid.
+ *
+ * Socket.unix_server_loop("/tmp/sock") {|s|
+ * begin
+ * euid, egid = s.getpeereid
+ *
+ * # Check the connected client is myself or not.
+ * next if euid != Process.uid
+ *
+ * # do something about my resource.
+ *
+ * ensure
+ * s.close
+ * end
+ * }
+ *
+ */
+static VALUE
+bsock_getpeereid(VALUE self)
+{
+#if defined(HAVE_GETPEEREID)
+ rb_io_t *fptr;
+ uid_t euid;
+ gid_t egid;
+ GetOpenFile(self, fptr);
+ if (getpeereid(fptr->fd, &euid, &egid) == -1)
+ rb_sys_fail("getpeereid(3)");
+ return rb_assoc_new(UIDT2NUM(euid), GIDT2NUM(egid));
+#elif defined(SO_PEERCRED) /* GNU/Linux */
+ rb_io_t *fptr;
+ struct ucred cred;
+ socklen_t len = sizeof(cred);
+ GetOpenFile(self, fptr);
+ if (getsockopt(fptr->fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1)
+ rb_sys_fail("getsockopt(SO_PEERCRED)");
+ return rb_assoc_new(UIDT2NUM(cred.uid), GIDT2NUM(cred.gid));
+#elif defined(HAVE_GETPEERUCRED) /* Solaris */
+ rb_io_t *fptr;
+ ucred_t *uc = NULL;
+ VALUE ret;
+ GetOpenFile(self, fptr);
+ if (getpeerucred(fptr->fd, &uc) == -1)
+ rb_sys_fail("getpeerucred(3C)");
+ ret = rb_assoc_new(UIDT2NUM(ucred_geteuid(uc)), GIDT2NUM(ucred_getegid(uc)));
+ ucred_free(uc);
+ return ret;
+#endif
+}
+#else
+#define bsock_getpeereid rb_f_notimplement
+#endif
+
+/*
+ * call-seq:
+ * bsock.local_address => addrinfo
+ *
+ * Returns an Addrinfo object for local address obtained by getsockname.
+ *
+ * Note that addrinfo.protocol is filled by 0.
+ *
+ * TCPSocket.open("www.ruby-lang.org", 80) {|s|
+ * p s.local_address #=> #<Addrinfo: 192.168.0.129:36873 TCP>
+ * }
+ *
+ * TCPServer.open("127.0.0.1", 1512) {|serv|
+ * p serv.local_address #=> #<Addrinfo: 127.0.0.1:1512 TCP>
+ * }
+ *
+ */
+static VALUE
+bsock_local_address(VALUE sock)
+{
+ union_sockaddr buf;
+ socklen_t len = (socklen_t)sizeof buf;
+ socklen_t len0 = len;
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ if (getsockname(fptr->fd, &buf.addr, &len) < 0)
+ rb_sys_fail("getsockname(2)");
+ if (len0 < len) len = len0;
+ return rsock_fd_socket_addrinfo(fptr->fd, &buf.addr, len);
+}
+
+/*
+ * call-seq:
+ * bsock.remote_address => addrinfo
+ *
+ * Returns an Addrinfo object for remote address obtained by getpeername.
+ *
+ * Note that addrinfo.protocol is filled by 0.
+ *
+ * TCPSocket.open("www.ruby-lang.org", 80) {|s|
+ * p s.remote_address #=> #<Addrinfo: 221.186.184.68:80 TCP>
+ * }
+ *
+ * TCPServer.open("127.0.0.1", 1728) {|serv|
+ * c = TCPSocket.new("127.0.0.1", 1728)
+ * s = serv.accept
+ * p s.remote_address #=> #<Addrinfo: 127.0.0.1:36504 TCP>
+ * }
+ *
+ */
+static VALUE
+bsock_remote_address(VALUE sock)
+{
+ union_sockaddr buf;
+ socklen_t len = (socklen_t)sizeof buf;
+ socklen_t len0 = len;
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ if (getpeername(fptr->fd, &buf.addr, &len) < 0)
+ rb_sys_fail("getpeername(2)");
+ if (len0 < len) len = len0;
+ return rsock_fd_socket_addrinfo(fptr->fd, &buf.addr, len);
+}
+
+/*
+ * call-seq:
+ * basicsocket.send(mesg, flags [, dest_sockaddr]) => numbytes_sent
+ *
+ * send _mesg_ via _basicsocket_.
+ *
+ * _mesg_ should be a string.
+ *
+ * _flags_ should be a bitwise OR of Socket::MSG_* constants.
+ *
+ * _dest_sockaddr_ should be a packed sockaddr string or an addrinfo.
+ *
+ * TCPSocket.open("localhost", 80) {|s|
+ * s.send "GET / HTTP/1.0\r\n\r\n", 0
+ * p s.read
+ * }
+ */
+VALUE
+rsock_bsock_send(int argc, VALUE *argv, VALUE sock)
+{
+ struct rsock_send_arg arg;
+ VALUE flags, to;
+ rb_io_t *fptr;
+ int n;
+ rb_blocking_function_t *func;
+
+ rb_scan_args(argc, argv, "21", &arg.mesg, &flags, &to);
+
+ StringValue(arg.mesg);
+ if (!NIL_P(to)) {
+ SockAddrStringValue(to);
+ to = rb_str_new4(to);
+ arg.to = (struct sockaddr *)RSTRING_PTR(to);
+ arg.tolen = RSTRING_SOCKLEN(to);
+ func = rsock_sendto_blocking;
+ }
+ else {
+ func = rsock_send_blocking;
+ }
+ GetOpenFile(sock, fptr);
+ arg.fd = fptr->fd;
+ arg.flags = NUM2INT(flags);
+ while (rsock_maybe_fd_writable(arg.fd),
+ (n = (int)BLOCKING_REGION_FD(func, &arg)) < 0) {
+ if (rb_io_wait_writable(arg.fd)) {
+ continue;
+ }
+ rb_sys_fail("send(2)");
+ }
+ return INT2FIX(n);
+}
+
+/*
+ * call-seq:
+ * basicsocket.do_not_reverse_lookup => true or false
+ *
+ * Gets the do_not_reverse_lookup flag of _basicsocket_.
+ *
+ * TCPSocket.open("www.ruby-lang.org", 80) {|sock|
+ * p sock.do_not_reverse_lookup #=> false
+ * p sock.peeraddr #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
+ * sock.do_not_reverse_lookup = true
+ * p sock.peeraddr #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"]
+ * }
+ */
+static VALUE
+bsock_do_not_reverse_lookup(VALUE sock)
+{
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ return (fptr->mode & FMODE_NOREVLOOKUP) ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ * basicsocket.do_not_reverse_lookup = bool
+ *
+ * Sets the do_not_reverse_lookup flag of _basicsocket_.
+ *
+ * BasicSocket.do_not_reverse_lookup = false
+ * p TCPSocket.new("127.0.0.1", 80).do_not_reverse_lookup #=> false
+ * BasicSocket.do_not_reverse_lookup = true
+ * p TCPSocket.new("127.0.0.1", 80).do_not_reverse_lookup #=> true
+ *
+ */
+static VALUE
+bsock_do_not_reverse_lookup_set(VALUE sock, VALUE state)
+{
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ if (RTEST(state)) {
+ fptr->mode |= FMODE_NOREVLOOKUP;
+ }
+ else {
+ fptr->mode &= ~FMODE_NOREVLOOKUP;
+ }
+ return sock;
+}
+
+/*
+ * call-seq:
+ * basicsocket.recv(maxlen) => mesg
+ * basicsocket.recv(maxlen, flags) => mesg
+ *
+ * Receives a message.
+ *
+ * _maxlen_ is the maximum number of bytes to receive.
+ *
+ * _flags_ should be a bitwise OR of Socket::MSG_* constants.
+ *
+ * UNIXSocket.pair {|s1, s2|
+ * s1.puts "Hello World"
+ * p s2.recv(4) #=> "Hell"
+ * p s2.recv(4, Socket::MSG_PEEK) #=> "o Wo"
+ * p s2.recv(4) #=> "o Wo"
+ * p s2.recv(10) #=> "rld\n"
+ * }
+ */
+static VALUE
+bsock_recv(int argc, VALUE *argv, VALUE sock)
+{
+ return rsock_s_recvfrom(sock, argc, argv, RECV_RECV);
+}
+
+/*
+ * call-seq:
+ * basicsocket.recv_nonblock(maxlen) => mesg
+ * basicsocket.recv_nonblock(maxlen, flags) => mesg
+ *
+ * Receives up to _maxlen_ bytes from +socket+ using recvfrom(2) after
+ * O_NONBLOCK is set for the underlying file descriptor.
+ * _flags_ is zero or more of the +MSG_+ options.
+ * The result, _mesg_, is the data received.
+ *
+ * When recvfrom(2) returns 0, Socket#recv_nonblock returns
+ * an empty string as data.
+ * The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.
+ *
+ * === Parameters
+ * * +maxlen+ - the number of bytes to receive from the socket
+ * * +flags+ - zero or more of the +MSG_+ options
+ *
+ * === Example
+ * serv = TCPServer.new("127.0.0.1", 0)
+ * af, port, host, addr = serv.addr
+ * c = TCPSocket.new(addr, port)
+ * s = serv.accept
+ * c.send "aaa", 0
+ * begin # emulate blocking recv.
+ * p s.recv_nonblock(10) #=> "aaa"
+ * rescue IO::WaitReadable
+ * IO.select([s])
+ * retry
+ * end
+ *
+ * Refer to Socket#recvfrom for the exceptions that may be thrown if the call
+ * to _recv_nonblock_ fails.
+ *
+ * BasicSocket#recv_nonblock may raise any error corresponding to recvfrom(2) failure,
+ * including Errno::EWOULDBLOCK.
+ *
+ * If the exception is Errno::EWOULDBLOCK or Errno::AGAIN,
+ * it is extended by IO::WaitReadable.
+ * So IO::WaitReadable can be used to rescue the exceptions for retrying recv_nonblock.
+ *
+ * === See
+ * * Socket#recvfrom
+ */
+
+static VALUE
+bsock_recv_nonblock(int argc, VALUE *argv, VALUE sock)
+{
+ return rsock_s_recvfrom_nonblock(sock, argc, argv, RECV_RECV);
+}
+
+/*
+ * call-seq:
+ * BasicSocket.do_not_reverse_lookup => true or false
+ *
+ * Gets the global do_not_reverse_lookup flag.
+ *
+ * BasicSocket.do_not_reverse_lookup #=> false
+ */
+static VALUE
+bsock_do_not_rev_lookup(void)
+{
+ return rsock_do_not_reverse_lookup?Qtrue:Qfalse;
+}
+
+/*
+ * call-seq:
+ * BasicSocket.do_not_reverse_lookup = bool
+ *
+ * Sets the global do_not_reverse_lookup flag.
+ *
+ * The flag is used for initial value of do_not_reverse_lookup for each socket.
+ *
+ * s1 = TCPSocket.new("localhost", 80)
+ * p s1.do_not_reverse_lookup #=> true
+ * BasicSocket.do_not_reverse_lookup = false
+ * s2 = TCPSocket.new("localhost", 80)
+ * p s2.do_not_reverse_lookup #=> false
+ * p s1.do_not_reverse_lookup #=> true
+ *
+ */
+static VALUE
+bsock_do_not_rev_lookup_set(VALUE self, VALUE val)
+{
+ rsock_do_not_reverse_lookup = RTEST(val);
+ return val;
+}
+
+void
+rsock_init_basicsocket(void)
+{
+ /*
+ * Document-class: BasicSocket < IO
+ *
+ * BasicSocket is the super class for all the Socket classes.
+ */
+ rb_cBasicSocket = rb_define_class("BasicSocket", rb_cIO);
+ rb_undef_method(rb_cBasicSocket, "initialize");
+
+ rb_define_singleton_method(rb_cBasicSocket, "do_not_reverse_lookup",
+ bsock_do_not_rev_lookup, 0);
+ rb_define_singleton_method(rb_cBasicSocket, "do_not_reverse_lookup=",
+ bsock_do_not_rev_lookup_set, 1);
+ rb_define_singleton_method(rb_cBasicSocket, "for_fd", bsock_s_for_fd, 1);
+
+ rb_define_method(rb_cBasicSocket, "close_read", bsock_close_read, 0);
+ rb_define_method(rb_cBasicSocket, "close_write", bsock_close_write, 0);
+ rb_define_method(rb_cBasicSocket, "shutdown", bsock_shutdown, -1);
+ rb_define_method(rb_cBasicSocket, "setsockopt", bsock_setsockopt, -1);
+ rb_define_method(rb_cBasicSocket, "getsockopt", bsock_getsockopt, 2);
+ rb_define_method(rb_cBasicSocket, "getsockname", bsock_getsockname, 0);
+ rb_define_method(rb_cBasicSocket, "getpeername", bsock_getpeername, 0);
+ rb_define_method(rb_cBasicSocket, "getpeereid", bsock_getpeereid, 0);
+ rb_define_method(rb_cBasicSocket, "local_address", bsock_local_address, 0);
+ rb_define_method(rb_cBasicSocket, "remote_address", bsock_remote_address, 0);
+ rb_define_method(rb_cBasicSocket, "send", rsock_bsock_send, -1);
+ rb_define_method(rb_cBasicSocket, "recv", bsock_recv, -1);
+ rb_define_method(rb_cBasicSocket, "recv_nonblock", bsock_recv_nonblock, -1);
+ rb_define_method(rb_cBasicSocket, "do_not_reverse_lookup", bsock_do_not_reverse_lookup, 0);
+ rb_define_method(rb_cBasicSocket, "do_not_reverse_lookup=", bsock_do_not_reverse_lookup_set, 1);
+
+ rb_define_method(rb_cBasicSocket, "sendmsg", rsock_bsock_sendmsg, -1); /* in ancdata.c */
+ rb_define_method(rb_cBasicSocket, "sendmsg_nonblock", rsock_bsock_sendmsg_nonblock, -1); /* in ancdata.c */
+ rb_define_method(rb_cBasicSocket, "recvmsg", rsock_bsock_recvmsg, -1); /* in ancdata.c */
+ rb_define_method(rb_cBasicSocket, "recvmsg_nonblock", rsock_bsock_recvmsg_nonblock, -1); /* in ancdata.c */
+
+}
diff --git a/jni/ruby/ext/socket/constants.c b/jni/ruby/ext/socket/constants.c
new file mode 100644
index 0000000..bab27b2
--- /dev/null
+++ b/jni/ruby/ext/socket/constants.c
@@ -0,0 +1,145 @@
+/************************************************
+
+ constants.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+static VALUE rb_mSockConst;
+
+#include "constdefs.c"
+
+static int
+constant_arg(VALUE arg, int (*str_to_int)(const char*, long, int*), const char *errmsg)
+{
+ VALUE tmp;
+ char *ptr;
+ int ret;
+
+ if (SYMBOL_P(arg)) {
+ arg = rb_sym_to_s(arg);
+ goto str;
+ }
+ else if (!NIL_P(tmp = rb_check_string_type(arg))) {
+ arg = tmp;
+ str:
+ rb_check_safe_obj(arg);
+ ptr = RSTRING_PTR(arg);
+ if (str_to_int(ptr, RSTRING_LEN(arg), &ret) == -1)
+ rb_raise(rb_eSocket, "%s: %s", errmsg, ptr);
+ }
+ else {
+ ret = NUM2INT(arg);
+ }
+ return ret;
+}
+
+int
+rsock_family_arg(VALUE domain)
+{
+ /* convert AF_INET, etc. */
+ return constant_arg(domain, rsock_family_to_int, "unknown socket domain");
+}
+
+int
+rsock_socktype_arg(VALUE type)
+{
+ /* convert SOCK_STREAM, etc. */
+ return constant_arg(type, rsock_socktype_to_int, "unknown socket type");
+}
+
+int
+rsock_level_arg(int family, VALUE level)
+{
+ /* convert SOL_SOCKET, IPPROTO_TCP, etc. */
+ if (IS_IP_FAMILY(family)) {
+ return constant_arg(level, rsock_ip_level_to_int, "unknown protocol level");
+ }
+ else {
+ return constant_arg(level, rsock_unknown_level_to_int, "unknown protocol level");
+ }
+}
+
+int
+rsock_optname_arg(int family, int level, VALUE optname)
+{
+ if (IS_IP_FAMILY(family)) {
+ switch (level) {
+ case SOL_SOCKET:
+ return constant_arg(optname, rsock_so_optname_to_int, "unknown socket level option name");
+ case IPPROTO_IP:
+ return constant_arg(optname, rsock_ip_optname_to_int, "unknown IP level option name");
+#ifdef IPPROTO_IPV6
+ case IPPROTO_IPV6:
+ return constant_arg(optname, rsock_ipv6_optname_to_int, "unknown IPv6 level option name");
+#endif
+ case IPPROTO_TCP:
+ return constant_arg(optname, rsock_tcp_optname_to_int, "unknown TCP level option name");
+ case IPPROTO_UDP:
+ return constant_arg(optname, rsock_udp_optname_to_int, "unknown UDP level option name");
+ default:
+ return NUM2INT(optname);
+ }
+ }
+ else {
+ switch (level) {
+ case SOL_SOCKET:
+ return constant_arg(optname, rsock_so_optname_to_int, "unknown socket level option name");
+ default:
+ return NUM2INT(optname);
+ }
+ }
+}
+
+int
+rsock_cmsg_type_arg(int family, int level, VALUE type)
+{
+ if (IS_IP_FAMILY(family)) {
+ switch (level) {
+ case SOL_SOCKET:
+ return constant_arg(type, rsock_scm_optname_to_int, "unknown UNIX control message");
+ case IPPROTO_IP:
+ return constant_arg(type, rsock_ip_optname_to_int, "unknown IP control message");
+#ifdef IPPROTO_IPV6
+ case IPPROTO_IPV6:
+ return constant_arg(type, rsock_ipv6_optname_to_int, "unknown IPv6 control message");
+#endif
+ case IPPROTO_TCP:
+ return constant_arg(type, rsock_tcp_optname_to_int, "unknown TCP control message");
+ case IPPROTO_UDP:
+ return constant_arg(type, rsock_udp_optname_to_int, "unknown UDP control message");
+ default:
+ return NUM2INT(type);
+ }
+ }
+ else {
+ switch (level) {
+ case SOL_SOCKET:
+ return constant_arg(type, rsock_scm_optname_to_int, "unknown UNIX control message");
+ default:
+ return NUM2INT(type);
+ }
+ }
+}
+
+int
+rsock_shutdown_how_arg(VALUE how)
+{
+ /* convert SHUT_RD, SHUT_WR, SHUT_RDWR. */
+ return constant_arg(how, rsock_shutdown_how_to_int, "unknown shutdown argument");
+}
+
+/*
+ * Socket::Constants module
+ */
+void
+rsock_init_socket_constants(void)
+{
+ /* constants */
+ init_constants();
+}
diff --git a/jni/ruby/ext/socket/constdefs.c b/jni/ruby/ext/socket/constdefs.c
new file mode 100644
index 0000000..18136ba
--- /dev/null
+++ b/jni/ruby/ext/socket/constdefs.c
@@ -0,0 +1,6852 @@
+/* autogenerated file */
+
+static st_table *rsock_intern_family_hash;
+static st_table *rsock_intern_family_noprefix_hash;
+static st_table *rsock_intern_protocol_family_hash;
+static st_table *rsock_intern_socktype_hash;
+static st_table *rsock_intern_ipproto_hash;
+static st_table *rsock_intern_iplevel_hash;
+static st_table *rsock_intern_so_optname_hash;
+static st_table *rsock_intern_ip_optname_hash;
+static st_table *rsock_intern_ipv6_optname_hash;
+static st_table *rsock_intern_tcp_optname_hash;
+static st_table *rsock_intern_udp_optname_hash;
+static st_table *rsock_intern_scm_optname_hash;
+static st_table *rsock_intern_local_optname_hash;
+
+#ifdef HAVE_LONG_LONG
+#define INTEGER2NUM(n) \
+ (FIXNUM_MAX < (n) ? ULL2NUM(n) : \
+ FIXNUM_MIN > (LONG_LONG)(n) ? LL2NUM(n) : \
+ LONG2FIX(n))
+#else
+#define INTEGER2NUM(n) \
+ (FIXNUM_MAX < (n) ? ULONG2NUM(n) : \
+ FIXNUM_MIN > (long)(n) ? LONG2NUM(n) : \
+ LONG2FIX(n))
+#endif
+
+static void
+init_constants(void)
+{
+ /*
+ * Document-module: Socket::Constants
+ *
+ * Socket::Constants provides socket-related constants. All possible
+ * socket constants are listed in the documentation but they may not all
+ * be present on your platform.
+ *
+ * If the underlying platform doesn't define a constant the corresponding
+ * Ruby constant is not defined.
+ *
+ */
+ rb_mSockConst = rb_define_module_under(rb_cSocket, "Constants");
+
+#if defined(SOCK_STREAM)
+ /* A stream socket provides a sequenced, reliable two-way connection for a byte stream */
+ rb_define_const(rb_cSocket, "SOCK_STREAM", INTEGER2NUM(SOCK_STREAM));
+ /* A stream socket provides a sequenced, reliable two-way connection for a byte stream */
+ rb_define_const(rb_mSockConst, "SOCK_STREAM", INTEGER2NUM(SOCK_STREAM));
+#endif
+#if defined(SOCK_DGRAM)
+ /* A datagram socket provides connectionless, unreliable messaging */
+ rb_define_const(rb_cSocket, "SOCK_DGRAM", INTEGER2NUM(SOCK_DGRAM));
+ /* A datagram socket provides connectionless, unreliable messaging */
+ rb_define_const(rb_mSockConst, "SOCK_DGRAM", INTEGER2NUM(SOCK_DGRAM));
+#endif
+#if defined(SOCK_RAW)
+ /* A raw socket provides low-level access for direct access or implementing network protocols */
+ rb_define_const(rb_cSocket, "SOCK_RAW", INTEGER2NUM(SOCK_RAW));
+ /* A raw socket provides low-level access for direct access or implementing network protocols */
+ rb_define_const(rb_mSockConst, "SOCK_RAW", INTEGER2NUM(SOCK_RAW));
+#endif
+#if defined(SOCK_RDM)
+ /* A reliable datagram socket provides reliable delivery of messages */
+ rb_define_const(rb_cSocket, "SOCK_RDM", INTEGER2NUM(SOCK_RDM));
+ /* A reliable datagram socket provides reliable delivery of messages */
+ rb_define_const(rb_mSockConst, "SOCK_RDM", INTEGER2NUM(SOCK_RDM));
+#endif
+#if defined(SOCK_SEQPACKET)
+ /* A sequential packet socket provides sequenced, reliable two-way connection for datagrams */
+ rb_define_const(rb_cSocket, "SOCK_SEQPACKET", INTEGER2NUM(SOCK_SEQPACKET));
+ /* A sequential packet socket provides sequenced, reliable two-way connection for datagrams */
+ rb_define_const(rb_mSockConst, "SOCK_SEQPACKET", INTEGER2NUM(SOCK_SEQPACKET));
+#endif
+#if defined(SOCK_PACKET)
+ /* Device-level packet access */
+ rb_define_const(rb_cSocket, "SOCK_PACKET", INTEGER2NUM(SOCK_PACKET));
+ /* Device-level packet access */
+ rb_define_const(rb_mSockConst, "SOCK_PACKET", INTEGER2NUM(SOCK_PACKET));
+#endif
+#if defined(AF_UNSPEC)
+ /* Unspecified protocol, any supported address family */
+ rb_define_const(rb_cSocket, "AF_UNSPEC", INTEGER2NUM(AF_UNSPEC));
+ /* Unspecified protocol, any supported address family */
+ rb_define_const(rb_mSockConst, "AF_UNSPEC", INTEGER2NUM(AF_UNSPEC));
+#endif
+#if defined(PF_UNSPEC)
+ /* Unspecified protocol, any supported address family */
+ rb_define_const(rb_cSocket, "PF_UNSPEC", INTEGER2NUM(PF_UNSPEC));
+ /* Unspecified protocol, any supported address family */
+ rb_define_const(rb_mSockConst, "PF_UNSPEC", INTEGER2NUM(PF_UNSPEC));
+#endif
+#if defined(AF_INET)
+ /* IPv4 protocol */
+ rb_define_const(rb_cSocket, "AF_INET", INTEGER2NUM(AF_INET));
+ /* IPv4 protocol */
+ rb_define_const(rb_mSockConst, "AF_INET", INTEGER2NUM(AF_INET));
+#endif
+#if defined(PF_INET)
+ /* IPv4 protocol */
+ rb_define_const(rb_cSocket, "PF_INET", INTEGER2NUM(PF_INET));
+ /* IPv4 protocol */
+ rb_define_const(rb_mSockConst, "PF_INET", INTEGER2NUM(PF_INET));
+#endif
+#if defined(INET6)
+#if defined(AF_INET6)
+ /* IPv6 protocol */
+ rb_define_const(rb_cSocket, "AF_INET6", INTEGER2NUM(AF_INET6));
+ /* IPv6 protocol */
+ rb_define_const(rb_mSockConst, "AF_INET6", INTEGER2NUM(AF_INET6));
+#endif
+#endif
+#if defined(INET6)
+#if defined(PF_INET6)
+ /* IPv6 protocol */
+ rb_define_const(rb_cSocket, "PF_INET6", INTEGER2NUM(PF_INET6));
+ /* IPv6 protocol */
+ rb_define_const(rb_mSockConst, "PF_INET6", INTEGER2NUM(PF_INET6));
+#endif
+#endif
+#if defined(AF_UNIX)
+ /* UNIX sockets */
+ rb_define_const(rb_cSocket, "AF_UNIX", INTEGER2NUM(AF_UNIX));
+ /* UNIX sockets */
+ rb_define_const(rb_mSockConst, "AF_UNIX", INTEGER2NUM(AF_UNIX));
+#endif
+#if defined(PF_UNIX)
+ /* UNIX sockets */
+ rb_define_const(rb_cSocket, "PF_UNIX", INTEGER2NUM(PF_UNIX));
+ /* UNIX sockets */
+ rb_define_const(rb_mSockConst, "PF_UNIX", INTEGER2NUM(PF_UNIX));
+#endif
+#if defined(AF_AX25)
+ /* AX.25 protocol */
+ rb_define_const(rb_cSocket, "AF_AX25", INTEGER2NUM(AF_AX25));
+ /* AX.25 protocol */
+ rb_define_const(rb_mSockConst, "AF_AX25", INTEGER2NUM(AF_AX25));
+#endif
+#if defined(PF_AX25)
+ /* AX.25 protocol */
+ rb_define_const(rb_cSocket, "PF_AX25", INTEGER2NUM(PF_AX25));
+ /* AX.25 protocol */
+ rb_define_const(rb_mSockConst, "PF_AX25", INTEGER2NUM(PF_AX25));
+#endif
+#if defined(AF_IPX)
+ /* IPX protocol */
+ rb_define_const(rb_cSocket, "AF_IPX", INTEGER2NUM(AF_IPX));
+ /* IPX protocol */
+ rb_define_const(rb_mSockConst, "AF_IPX", INTEGER2NUM(AF_IPX));
+#endif
+#if defined(PF_IPX)
+ /* IPX protocol */
+ rb_define_const(rb_cSocket, "PF_IPX", INTEGER2NUM(PF_IPX));
+ /* IPX protocol */
+ rb_define_const(rb_mSockConst, "PF_IPX", INTEGER2NUM(PF_IPX));
+#endif
+#if defined(AF_APPLETALK)
+ /* AppleTalk protocol */
+ rb_define_const(rb_cSocket, "AF_APPLETALK", INTEGER2NUM(AF_APPLETALK));
+ /* AppleTalk protocol */
+ rb_define_const(rb_mSockConst, "AF_APPLETALK", INTEGER2NUM(AF_APPLETALK));
+#endif
+#if defined(PF_APPLETALK)
+ /* AppleTalk protocol */
+ rb_define_const(rb_cSocket, "PF_APPLETALK", INTEGER2NUM(PF_APPLETALK));
+ /* AppleTalk protocol */
+ rb_define_const(rb_mSockConst, "PF_APPLETALK", INTEGER2NUM(PF_APPLETALK));
+#endif
+#if defined(AF_LOCAL)
+ /* Host-internal protocols */
+ rb_define_const(rb_cSocket, "AF_LOCAL", INTEGER2NUM(AF_LOCAL));
+ /* Host-internal protocols */
+ rb_define_const(rb_mSockConst, "AF_LOCAL", INTEGER2NUM(AF_LOCAL));
+#endif
+#if defined(PF_LOCAL)
+ /* Host-internal protocols */
+ rb_define_const(rb_cSocket, "PF_LOCAL", INTEGER2NUM(PF_LOCAL));
+ /* Host-internal protocols */
+ rb_define_const(rb_mSockConst, "PF_LOCAL", INTEGER2NUM(PF_LOCAL));
+#endif
+#if defined(AF_IMPLINK)
+ /* ARPANET IMP protocol */
+ rb_define_const(rb_cSocket, "AF_IMPLINK", INTEGER2NUM(AF_IMPLINK));
+ /* ARPANET IMP protocol */
+ rb_define_const(rb_mSockConst, "AF_IMPLINK", INTEGER2NUM(AF_IMPLINK));
+#endif
+#if defined(PF_IMPLINK)
+ /* ARPANET IMP protocol */
+ rb_define_const(rb_cSocket, "PF_IMPLINK", INTEGER2NUM(PF_IMPLINK));
+ /* ARPANET IMP protocol */
+ rb_define_const(rb_mSockConst, "PF_IMPLINK", INTEGER2NUM(PF_IMPLINK));
+#endif
+#if defined(AF_PUP)
+ /* PARC Universal Packet protocol */
+ rb_define_const(rb_cSocket, "AF_PUP", INTEGER2NUM(AF_PUP));
+ /* PARC Universal Packet protocol */
+ rb_define_const(rb_mSockConst, "AF_PUP", INTEGER2NUM(AF_PUP));
+#endif
+#if defined(PF_PUP)
+ /* PARC Universal Packet protocol */
+ rb_define_const(rb_cSocket, "PF_PUP", INTEGER2NUM(PF_PUP));
+ /* PARC Universal Packet protocol */
+ rb_define_const(rb_mSockConst, "PF_PUP", INTEGER2NUM(PF_PUP));
+#endif
+#if defined(AF_CHAOS)
+ /* MIT CHAOS protocols */
+ rb_define_const(rb_cSocket, "AF_CHAOS", INTEGER2NUM(AF_CHAOS));
+ /* MIT CHAOS protocols */
+ rb_define_const(rb_mSockConst, "AF_CHAOS", INTEGER2NUM(AF_CHAOS));
+#endif
+#if defined(PF_CHAOS)
+ /* MIT CHAOS protocols */
+ rb_define_const(rb_cSocket, "PF_CHAOS", INTEGER2NUM(PF_CHAOS));
+ /* MIT CHAOS protocols */
+ rb_define_const(rb_mSockConst, "PF_CHAOS", INTEGER2NUM(PF_CHAOS));
+#endif
+#if defined(AF_NS)
+ /* XEROX NS protocols */
+ rb_define_const(rb_cSocket, "AF_NS", INTEGER2NUM(AF_NS));
+ /* XEROX NS protocols */
+ rb_define_const(rb_mSockConst, "AF_NS", INTEGER2NUM(AF_NS));
+#endif
+#if defined(PF_NS)
+ /* XEROX NS protocols */
+ rb_define_const(rb_cSocket, "PF_NS", INTEGER2NUM(PF_NS));
+ /* XEROX NS protocols */
+ rb_define_const(rb_mSockConst, "PF_NS", INTEGER2NUM(PF_NS));
+#endif
+#if defined(AF_ISO)
+ /* ISO Open Systems Interconnection protocols */
+ rb_define_const(rb_cSocket, "AF_ISO", INTEGER2NUM(AF_ISO));
+ /* ISO Open Systems Interconnection protocols */
+ rb_define_const(rb_mSockConst, "AF_ISO", INTEGER2NUM(AF_ISO));
+#endif
+#if defined(PF_ISO)
+ /* ISO Open Systems Interconnection protocols */
+ rb_define_const(rb_cSocket, "PF_ISO", INTEGER2NUM(PF_ISO));
+ /* ISO Open Systems Interconnection protocols */
+ rb_define_const(rb_mSockConst, "PF_ISO", INTEGER2NUM(PF_ISO));
+#endif
+#if defined(AF_OSI)
+ /* ISO Open Systems Interconnection protocols */
+ rb_define_const(rb_cSocket, "AF_OSI", INTEGER2NUM(AF_OSI));
+ /* ISO Open Systems Interconnection protocols */
+ rb_define_const(rb_mSockConst, "AF_OSI", INTEGER2NUM(AF_OSI));
+#endif
+#if defined(PF_OSI)
+ /* ISO Open Systems Interconnection protocols */
+ rb_define_const(rb_cSocket, "PF_OSI", INTEGER2NUM(PF_OSI));
+ /* ISO Open Systems Interconnection protocols */
+ rb_define_const(rb_mSockConst, "PF_OSI", INTEGER2NUM(PF_OSI));
+#endif
+#if defined(AF_ECMA)
+ /* European Computer Manufacturers protocols */
+ rb_define_const(rb_cSocket, "AF_ECMA", INTEGER2NUM(AF_ECMA));
+ /* European Computer Manufacturers protocols */
+ rb_define_const(rb_mSockConst, "AF_ECMA", INTEGER2NUM(AF_ECMA));
+#endif
+#if defined(PF_ECMA)
+ /* European Computer Manufacturers protocols */
+ rb_define_const(rb_cSocket, "PF_ECMA", INTEGER2NUM(PF_ECMA));
+ /* European Computer Manufacturers protocols */
+ rb_define_const(rb_mSockConst, "PF_ECMA", INTEGER2NUM(PF_ECMA));
+#endif
+#if defined(AF_DATAKIT)
+ /* Datakit protocol */
+ rb_define_const(rb_cSocket, "AF_DATAKIT", INTEGER2NUM(AF_DATAKIT));
+ /* Datakit protocol */
+ rb_define_const(rb_mSockConst, "AF_DATAKIT", INTEGER2NUM(AF_DATAKIT));
+#endif
+#if defined(PF_DATAKIT)
+ /* Datakit protocol */
+ rb_define_const(rb_cSocket, "PF_DATAKIT", INTEGER2NUM(PF_DATAKIT));
+ /* Datakit protocol */
+ rb_define_const(rb_mSockConst, "PF_DATAKIT", INTEGER2NUM(PF_DATAKIT));
+#endif
+#if defined(AF_CCITT)
+ /* CCITT (now ITU-T) protocols */
+ rb_define_const(rb_cSocket, "AF_CCITT", INTEGER2NUM(AF_CCITT));
+ /* CCITT (now ITU-T) protocols */
+ rb_define_const(rb_mSockConst, "AF_CCITT", INTEGER2NUM(AF_CCITT));
+#endif
+#if defined(PF_CCITT)
+ /* CCITT (now ITU-T) protocols */
+ rb_define_const(rb_cSocket, "PF_CCITT", INTEGER2NUM(PF_CCITT));
+ /* CCITT (now ITU-T) protocols */
+ rb_define_const(rb_mSockConst, "PF_CCITT", INTEGER2NUM(PF_CCITT));
+#endif
+#if defined(AF_SNA)
+ /* IBM SNA protocol */
+ rb_define_const(rb_cSocket, "AF_SNA", INTEGER2NUM(AF_SNA));
+ /* IBM SNA protocol */
+ rb_define_const(rb_mSockConst, "AF_SNA", INTEGER2NUM(AF_SNA));
+#endif
+#if defined(PF_SNA)
+ /* IBM SNA protocol */
+ rb_define_const(rb_cSocket, "PF_SNA", INTEGER2NUM(PF_SNA));
+ /* IBM SNA protocol */
+ rb_define_const(rb_mSockConst, "PF_SNA", INTEGER2NUM(PF_SNA));
+#endif
+#if defined(AF_DEC)
+ /* DECnet protocol */
+ rb_define_const(rb_cSocket, "AF_DEC", INTEGER2NUM(AF_DEC));
+ /* DECnet protocol */
+ rb_define_const(rb_mSockConst, "AF_DEC", INTEGER2NUM(AF_DEC));
+#endif
+#if defined(PF_DEC)
+ /* DECnet protocol */
+ rb_define_const(rb_cSocket, "PF_DEC", INTEGER2NUM(PF_DEC));
+ /* DECnet protocol */
+ rb_define_const(rb_mSockConst, "PF_DEC", INTEGER2NUM(PF_DEC));
+#endif
+#if defined(AF_DLI)
+ /* DEC Direct Data Link Interface protocol */
+ rb_define_const(rb_cSocket, "AF_DLI", INTEGER2NUM(AF_DLI));
+ /* DEC Direct Data Link Interface protocol */
+ rb_define_const(rb_mSockConst, "AF_DLI", INTEGER2NUM(AF_DLI));
+#endif
+#if defined(PF_DLI)
+ /* DEC Direct Data Link Interface protocol */
+ rb_define_const(rb_cSocket, "PF_DLI", INTEGER2NUM(PF_DLI));
+ /* DEC Direct Data Link Interface protocol */
+ rb_define_const(rb_mSockConst, "PF_DLI", INTEGER2NUM(PF_DLI));
+#endif
+#if defined(AF_LAT)
+ /* Local Area Transport protocol */
+ rb_define_const(rb_cSocket, "AF_LAT", INTEGER2NUM(AF_LAT));
+ /* Local Area Transport protocol */
+ rb_define_const(rb_mSockConst, "AF_LAT", INTEGER2NUM(AF_LAT));
+#endif
+#if defined(PF_LAT)
+ /* Local Area Transport protocol */
+ rb_define_const(rb_cSocket, "PF_LAT", INTEGER2NUM(PF_LAT));
+ /* Local Area Transport protocol */
+ rb_define_const(rb_mSockConst, "PF_LAT", INTEGER2NUM(PF_LAT));
+#endif
+#if defined(AF_HYLINK)
+ /* NSC Hyperchannel protocol */
+ rb_define_const(rb_cSocket, "AF_HYLINK", INTEGER2NUM(AF_HYLINK));
+ /* NSC Hyperchannel protocol */
+ rb_define_const(rb_mSockConst, "AF_HYLINK", INTEGER2NUM(AF_HYLINK));
+#endif
+#if defined(PF_HYLINK)
+ /* NSC Hyperchannel protocol */
+ rb_define_const(rb_cSocket, "PF_HYLINK", INTEGER2NUM(PF_HYLINK));
+ /* NSC Hyperchannel protocol */
+ rb_define_const(rb_mSockConst, "PF_HYLINK", INTEGER2NUM(PF_HYLINK));
+#endif
+#if defined(AF_ROUTE)
+ /* Internal routing protocol */
+ rb_define_const(rb_cSocket, "AF_ROUTE", INTEGER2NUM(AF_ROUTE));
+ /* Internal routing protocol */
+ rb_define_const(rb_mSockConst, "AF_ROUTE", INTEGER2NUM(AF_ROUTE));
+#endif
+#if defined(PF_ROUTE)
+ /* Internal routing protocol */
+ rb_define_const(rb_cSocket, "PF_ROUTE", INTEGER2NUM(PF_ROUTE));
+ /* Internal routing protocol */
+ rb_define_const(rb_mSockConst, "PF_ROUTE", INTEGER2NUM(PF_ROUTE));
+#endif
+#if defined(AF_LINK)
+ /* Link layer interface */
+ rb_define_const(rb_cSocket, "AF_LINK", INTEGER2NUM(AF_LINK));
+ /* Link layer interface */
+ rb_define_const(rb_mSockConst, "AF_LINK", INTEGER2NUM(AF_LINK));
+#endif
+#if defined(PF_LINK)
+ /* Link layer interface */
+ rb_define_const(rb_cSocket, "PF_LINK", INTEGER2NUM(PF_LINK));
+ /* Link layer interface */
+ rb_define_const(rb_mSockConst, "PF_LINK", INTEGER2NUM(PF_LINK));
+#endif
+#if defined(AF_COIP)
+ /* Connection-oriented IP */
+ rb_define_const(rb_cSocket, "AF_COIP", INTEGER2NUM(AF_COIP));
+ /* Connection-oriented IP */
+ rb_define_const(rb_mSockConst, "AF_COIP", INTEGER2NUM(AF_COIP));
+#endif
+#if defined(PF_COIP)
+ /* Connection-oriented IP */
+ rb_define_const(rb_cSocket, "PF_COIP", INTEGER2NUM(PF_COIP));
+ /* Connection-oriented IP */
+ rb_define_const(rb_mSockConst, "PF_COIP", INTEGER2NUM(PF_COIP));
+#endif
+#if defined(AF_CNT)
+ /* Computer Network Technology */
+ rb_define_const(rb_cSocket, "AF_CNT", INTEGER2NUM(AF_CNT));
+ /* Computer Network Technology */
+ rb_define_const(rb_mSockConst, "AF_CNT", INTEGER2NUM(AF_CNT));
+#endif
+#if defined(PF_CNT)
+ /* Computer Network Technology */
+ rb_define_const(rb_cSocket, "PF_CNT", INTEGER2NUM(PF_CNT));
+ /* Computer Network Technology */
+ rb_define_const(rb_mSockConst, "PF_CNT", INTEGER2NUM(PF_CNT));
+#endif
+#if defined(AF_SIP)
+ /* Simple Internet Protocol */
+ rb_define_const(rb_cSocket, "AF_SIP", INTEGER2NUM(AF_SIP));
+ /* Simple Internet Protocol */
+ rb_define_const(rb_mSockConst, "AF_SIP", INTEGER2NUM(AF_SIP));
+#endif
+#if defined(PF_SIP)
+ /* Simple Internet Protocol */
+ rb_define_const(rb_cSocket, "PF_SIP", INTEGER2NUM(PF_SIP));
+ /* Simple Internet Protocol */
+ rb_define_const(rb_mSockConst, "PF_SIP", INTEGER2NUM(PF_SIP));
+#endif
+#if defined(AF_NDRV)
+ /* Network driver raw access */
+ rb_define_const(rb_cSocket, "AF_NDRV", INTEGER2NUM(AF_NDRV));
+ /* Network driver raw access */
+ rb_define_const(rb_mSockConst, "AF_NDRV", INTEGER2NUM(AF_NDRV));
+#endif
+#if defined(PF_NDRV)
+ /* Network driver raw access */
+ rb_define_const(rb_cSocket, "PF_NDRV", INTEGER2NUM(PF_NDRV));
+ /* Network driver raw access */
+ rb_define_const(rb_mSockConst, "PF_NDRV", INTEGER2NUM(PF_NDRV));
+#endif
+#if defined(AF_ISDN)
+ /* Integrated Services Digital Network */
+ rb_define_const(rb_cSocket, "AF_ISDN", INTEGER2NUM(AF_ISDN));
+ /* Integrated Services Digital Network */
+ rb_define_const(rb_mSockConst, "AF_ISDN", INTEGER2NUM(AF_ISDN));
+#endif
+#if defined(PF_ISDN)
+ /* Integrated Services Digital Network */
+ rb_define_const(rb_cSocket, "PF_ISDN", INTEGER2NUM(PF_ISDN));
+ /* Integrated Services Digital Network */
+ rb_define_const(rb_mSockConst, "PF_ISDN", INTEGER2NUM(PF_ISDN));
+#endif
+#if defined(AF_NATM)
+ /* Native ATM access */
+ rb_define_const(rb_cSocket, "AF_NATM", INTEGER2NUM(AF_NATM));
+ /* Native ATM access */
+ rb_define_const(rb_mSockConst, "AF_NATM", INTEGER2NUM(AF_NATM));
+#endif
+#if defined(PF_NATM)
+ /* Native ATM access */
+ rb_define_const(rb_cSocket, "PF_NATM", INTEGER2NUM(PF_NATM));
+ /* Native ATM access */
+ rb_define_const(rb_mSockConst, "PF_NATM", INTEGER2NUM(PF_NATM));
+#endif
+#if defined(AF_SYSTEM)
+ /* */
+ rb_define_const(rb_cSocket, "AF_SYSTEM", INTEGER2NUM(AF_SYSTEM));
+ /* */
+ rb_define_const(rb_mSockConst, "AF_SYSTEM", INTEGER2NUM(AF_SYSTEM));
+#endif
+#if defined(PF_SYSTEM)
+ /* */
+ rb_define_const(rb_cSocket, "PF_SYSTEM", INTEGER2NUM(PF_SYSTEM));
+ /* */
+ rb_define_const(rb_mSockConst, "PF_SYSTEM", INTEGER2NUM(PF_SYSTEM));
+#endif
+#if defined(AF_NETBIOS)
+ /* NetBIOS */
+ rb_define_const(rb_cSocket, "AF_NETBIOS", INTEGER2NUM(AF_NETBIOS));
+ /* NetBIOS */
+ rb_define_const(rb_mSockConst, "AF_NETBIOS", INTEGER2NUM(AF_NETBIOS));
+#endif
+#if defined(PF_NETBIOS)
+ /* NetBIOS */
+ rb_define_const(rb_cSocket, "PF_NETBIOS", INTEGER2NUM(PF_NETBIOS));
+ /* NetBIOS */
+ rb_define_const(rb_mSockConst, "PF_NETBIOS", INTEGER2NUM(PF_NETBIOS));
+#endif
+#if defined(AF_PPP)
+ /* Point-to-Point Protocol */
+ rb_define_const(rb_cSocket, "AF_PPP", INTEGER2NUM(AF_PPP));
+ /* Point-to-Point Protocol */
+ rb_define_const(rb_mSockConst, "AF_PPP", INTEGER2NUM(AF_PPP));
+#endif
+#if defined(PF_PPP)
+ /* Point-to-Point Protocol */
+ rb_define_const(rb_cSocket, "PF_PPP", INTEGER2NUM(PF_PPP));
+ /* Point-to-Point Protocol */
+ rb_define_const(rb_mSockConst, "PF_PPP", INTEGER2NUM(PF_PPP));
+#endif
+#if defined(AF_ATM)
+ /* Asynchronous Transfer Mode */
+ rb_define_const(rb_cSocket, "AF_ATM", INTEGER2NUM(AF_ATM));
+ /* Asynchronous Transfer Mode */
+ rb_define_const(rb_mSockConst, "AF_ATM", INTEGER2NUM(AF_ATM));
+#endif
+#if defined(PF_ATM)
+ /* Asynchronous Transfer Mode */
+ rb_define_const(rb_cSocket, "PF_ATM", INTEGER2NUM(PF_ATM));
+ /* Asynchronous Transfer Mode */
+ rb_define_const(rb_mSockConst, "PF_ATM", INTEGER2NUM(PF_ATM));
+#endif
+#if defined(AF_NETGRAPH)
+ /* Netgraph sockets */
+ rb_define_const(rb_cSocket, "AF_NETGRAPH", INTEGER2NUM(AF_NETGRAPH));
+ /* Netgraph sockets */
+ rb_define_const(rb_mSockConst, "AF_NETGRAPH", INTEGER2NUM(AF_NETGRAPH));
+#endif
+#if defined(PF_NETGRAPH)
+ /* Netgraph sockets */
+ rb_define_const(rb_cSocket, "PF_NETGRAPH", INTEGER2NUM(PF_NETGRAPH));
+ /* Netgraph sockets */
+ rb_define_const(rb_mSockConst, "PF_NETGRAPH", INTEGER2NUM(PF_NETGRAPH));
+#endif
+#if defined(AF_MAX)
+ /* Maximum address family for this platform */
+ rb_define_const(rb_cSocket, "AF_MAX", INTEGER2NUM(AF_MAX));
+ /* Maximum address family for this platform */
+ rb_define_const(rb_mSockConst, "AF_MAX", INTEGER2NUM(AF_MAX));
+#endif
+#if defined(PF_MAX)
+ /* Maximum address family for this platform */
+ rb_define_const(rb_cSocket, "PF_MAX", INTEGER2NUM(PF_MAX));
+ /* Maximum address family for this platform */
+ rb_define_const(rb_mSockConst, "PF_MAX", INTEGER2NUM(PF_MAX));
+#endif
+#if defined(AF_PACKET)
+ /* Direct link-layer access */
+ rb_define_const(rb_cSocket, "AF_PACKET", INTEGER2NUM(AF_PACKET));
+ /* Direct link-layer access */
+ rb_define_const(rb_mSockConst, "AF_PACKET", INTEGER2NUM(AF_PACKET));
+#endif
+#if defined(PF_PACKET)
+ /* Direct link-layer access */
+ rb_define_const(rb_cSocket, "PF_PACKET", INTEGER2NUM(PF_PACKET));
+ /* Direct link-layer access */
+ rb_define_const(rb_mSockConst, "PF_PACKET", INTEGER2NUM(PF_PACKET));
+#endif
+#if defined(AF_E164)
+ /* CCITT (ITU-T) E.164 recommendation */
+ rb_define_const(rb_cSocket, "AF_E164", INTEGER2NUM(AF_E164));
+ /* CCITT (ITU-T) E.164 recommendation */
+ rb_define_const(rb_mSockConst, "AF_E164", INTEGER2NUM(AF_E164));
+#endif
+#if defined(PF_XTP)
+ /* eXpress Transfer Protocol */
+ rb_define_const(rb_cSocket, "PF_XTP", INTEGER2NUM(PF_XTP));
+ /* eXpress Transfer Protocol */
+ rb_define_const(rb_mSockConst, "PF_XTP", INTEGER2NUM(PF_XTP));
+#endif
+#if defined(PF_RTIP)
+ /* */
+ rb_define_const(rb_cSocket, "PF_RTIP", INTEGER2NUM(PF_RTIP));
+ /* */
+ rb_define_const(rb_mSockConst, "PF_RTIP", INTEGER2NUM(PF_RTIP));
+#endif
+#if defined(PF_PIP)
+ /* */
+ rb_define_const(rb_cSocket, "PF_PIP", INTEGER2NUM(PF_PIP));
+ /* */
+ rb_define_const(rb_mSockConst, "PF_PIP", INTEGER2NUM(PF_PIP));
+#endif
+#if defined(PF_KEY)
+ /* */
+ rb_define_const(rb_cSocket, "PF_KEY", INTEGER2NUM(PF_KEY));
+ /* */
+ rb_define_const(rb_mSockConst, "PF_KEY", INTEGER2NUM(PF_KEY));
+#endif
+#if defined(MSG_OOB)
+ /* Process out-of-band data */
+ rb_define_const(rb_cSocket, "MSG_OOB", INTEGER2NUM(MSG_OOB));
+ /* Process out-of-band data */
+ rb_define_const(rb_mSockConst, "MSG_OOB", INTEGER2NUM(MSG_OOB));
+#endif
+#if defined(MSG_PEEK)
+ /* Peek at incoming message */
+ rb_define_const(rb_cSocket, "MSG_PEEK", INTEGER2NUM(MSG_PEEK));
+ /* Peek at incoming message */
+ rb_define_const(rb_mSockConst, "MSG_PEEK", INTEGER2NUM(MSG_PEEK));
+#endif
+#if defined(MSG_DONTROUTE)
+ /* Send without using the routing tables */
+ rb_define_const(rb_cSocket, "MSG_DONTROUTE", INTEGER2NUM(MSG_DONTROUTE));
+ /* Send without using the routing tables */
+ rb_define_const(rb_mSockConst, "MSG_DONTROUTE", INTEGER2NUM(MSG_DONTROUTE));
+#endif
+#if defined(MSG_EOR)
+ /* Data completes record */
+ rb_define_const(rb_cSocket, "MSG_EOR", INTEGER2NUM(MSG_EOR));
+ /* Data completes record */
+ rb_define_const(rb_mSockConst, "MSG_EOR", INTEGER2NUM(MSG_EOR));
+#endif
+#if defined(MSG_TRUNC)
+ /* Data discarded before delivery */
+ rb_define_const(rb_cSocket, "MSG_TRUNC", INTEGER2NUM(MSG_TRUNC));
+ /* Data discarded before delivery */
+ rb_define_const(rb_mSockConst, "MSG_TRUNC", INTEGER2NUM(MSG_TRUNC));
+#endif
+#if defined(MSG_CTRUNC)
+ /* Control data lost before delivery */
+ rb_define_const(rb_cSocket, "MSG_CTRUNC", INTEGER2NUM(MSG_CTRUNC));
+ /* Control data lost before delivery */
+ rb_define_const(rb_mSockConst, "MSG_CTRUNC", INTEGER2NUM(MSG_CTRUNC));
+#endif
+#if defined(MSG_WAITALL)
+ /* Wait for full request or error */
+ rb_define_const(rb_cSocket, "MSG_WAITALL", INTEGER2NUM(MSG_WAITALL));
+ /* Wait for full request or error */
+ rb_define_const(rb_mSockConst, "MSG_WAITALL", INTEGER2NUM(MSG_WAITALL));
+#endif
+#if defined(MSG_DONTWAIT)
+ /* This message should be non-blocking */
+ rb_define_const(rb_cSocket, "MSG_DONTWAIT", INTEGER2NUM(MSG_DONTWAIT));
+ /* This message should be non-blocking */
+ rb_define_const(rb_mSockConst, "MSG_DONTWAIT", INTEGER2NUM(MSG_DONTWAIT));
+#endif
+#if defined(MSG_EOF)
+ /* Data completes connection */
+ rb_define_const(rb_cSocket, "MSG_EOF", INTEGER2NUM(MSG_EOF));
+ /* Data completes connection */
+ rb_define_const(rb_mSockConst, "MSG_EOF", INTEGER2NUM(MSG_EOF));
+#endif
+#if defined(MSG_FLUSH)
+ /* Start of a hold sequence. Dumps to so_temp */
+ rb_define_const(rb_cSocket, "MSG_FLUSH", INTEGER2NUM(MSG_FLUSH));
+ /* Start of a hold sequence. Dumps to so_temp */
+ rb_define_const(rb_mSockConst, "MSG_FLUSH", INTEGER2NUM(MSG_FLUSH));
+#endif
+#if defined(MSG_HOLD)
+ /* Hold fragment in so_temp */
+ rb_define_const(rb_cSocket, "MSG_HOLD", INTEGER2NUM(MSG_HOLD));
+ /* Hold fragment in so_temp */
+ rb_define_const(rb_mSockConst, "MSG_HOLD", INTEGER2NUM(MSG_HOLD));
+#endif
+#if defined(MSG_SEND)
+ /* Send the packet in so_temp */
+ rb_define_const(rb_cSocket, "MSG_SEND", INTEGER2NUM(MSG_SEND));
+ /* Send the packet in so_temp */
+ rb_define_const(rb_mSockConst, "MSG_SEND", INTEGER2NUM(MSG_SEND));
+#endif
+#if defined(MSG_HAVEMORE)
+ /* Data ready to be read */
+ rb_define_const(rb_cSocket, "MSG_HAVEMORE", INTEGER2NUM(MSG_HAVEMORE));
+ /* Data ready to be read */
+ rb_define_const(rb_mSockConst, "MSG_HAVEMORE", INTEGER2NUM(MSG_HAVEMORE));
+#endif
+#if defined(MSG_RCVMORE)
+ /* Data remains in the current packet */
+ rb_define_const(rb_cSocket, "MSG_RCVMORE", INTEGER2NUM(MSG_RCVMORE));
+ /* Data remains in the current packet */
+ rb_define_const(rb_mSockConst, "MSG_RCVMORE", INTEGER2NUM(MSG_RCVMORE));
+#endif
+#if defined(MSG_COMPAT)
+ /* End of record */
+ rb_define_const(rb_cSocket, "MSG_COMPAT", INTEGER2NUM(MSG_COMPAT));
+ /* End of record */
+ rb_define_const(rb_mSockConst, "MSG_COMPAT", INTEGER2NUM(MSG_COMPAT));
+#endif
+#if defined(MSG_PROXY)
+ /* Wait for full request */
+ rb_define_const(rb_cSocket, "MSG_PROXY", INTEGER2NUM(MSG_PROXY));
+ /* Wait for full request */
+ rb_define_const(rb_mSockConst, "MSG_PROXY", INTEGER2NUM(MSG_PROXY));
+#endif
+#if defined(MSG_FIN)
+ /* */
+ rb_define_const(rb_cSocket, "MSG_FIN", INTEGER2NUM(MSG_FIN));
+ /* */
+ rb_define_const(rb_mSockConst, "MSG_FIN", INTEGER2NUM(MSG_FIN));
+#endif
+#if defined(MSG_SYN)
+ /* */
+ rb_define_const(rb_cSocket, "MSG_SYN", INTEGER2NUM(MSG_SYN));
+ /* */
+ rb_define_const(rb_mSockConst, "MSG_SYN", INTEGER2NUM(MSG_SYN));
+#endif
+#if defined(MSG_CONFIRM)
+ /* Confirm path validity */
+ rb_define_const(rb_cSocket, "MSG_CONFIRM", INTEGER2NUM(MSG_CONFIRM));
+ /* Confirm path validity */
+ rb_define_const(rb_mSockConst, "MSG_CONFIRM", INTEGER2NUM(MSG_CONFIRM));
+#endif
+#if defined(MSG_RST)
+ /* */
+ rb_define_const(rb_cSocket, "MSG_RST", INTEGER2NUM(MSG_RST));
+ /* */
+ rb_define_const(rb_mSockConst, "MSG_RST", INTEGER2NUM(MSG_RST));
+#endif
+#if defined(MSG_ERRQUEUE)
+ /* Fetch message from error queue */
+ rb_define_const(rb_cSocket, "MSG_ERRQUEUE", INTEGER2NUM(MSG_ERRQUEUE));
+ /* Fetch message from error queue */
+ rb_define_const(rb_mSockConst, "MSG_ERRQUEUE", INTEGER2NUM(MSG_ERRQUEUE));
+#endif
+#if defined(MSG_NOSIGNAL)
+ /* Do not generate SIGPIPE */
+ rb_define_const(rb_cSocket, "MSG_NOSIGNAL", INTEGER2NUM(MSG_NOSIGNAL));
+ /* Do not generate SIGPIPE */
+ rb_define_const(rb_mSockConst, "MSG_NOSIGNAL", INTEGER2NUM(MSG_NOSIGNAL));
+#endif
+#if defined(MSG_MORE)
+ /* Sender will send more */
+ rb_define_const(rb_cSocket, "MSG_MORE", INTEGER2NUM(MSG_MORE));
+ /* Sender will send more */
+ rb_define_const(rb_mSockConst, "MSG_MORE", INTEGER2NUM(MSG_MORE));
+#endif
+#if defined(MSG_FASTOPEN)
+ /* Reduce step of the handshake process */
+ rb_define_const(rb_cSocket, "MSG_FASTOPEN", INTEGER2NUM(MSG_FASTOPEN));
+ /* Reduce step of the handshake process */
+ rb_define_const(rb_mSockConst, "MSG_FASTOPEN", INTEGER2NUM(MSG_FASTOPEN));
+#endif
+#if defined(SOL_SOCKET)
+ /* Socket-level options */
+ rb_define_const(rb_cSocket, "SOL_SOCKET", INTEGER2NUM(SOL_SOCKET));
+ /* Socket-level options */
+ rb_define_const(rb_mSockConst, "SOL_SOCKET", INTEGER2NUM(SOL_SOCKET));
+#endif
+#if defined(SOL_IP)
+ /* IP socket options */
+ rb_define_const(rb_cSocket, "SOL_IP", INTEGER2NUM(SOL_IP));
+ /* IP socket options */
+ rb_define_const(rb_mSockConst, "SOL_IP", INTEGER2NUM(SOL_IP));
+#endif
+#if defined(SOL_IPX)
+ /* IPX socket options */
+ rb_define_const(rb_cSocket, "SOL_IPX", INTEGER2NUM(SOL_IPX));
+ /* IPX socket options */
+ rb_define_const(rb_mSockConst, "SOL_IPX", INTEGER2NUM(SOL_IPX));
+#endif
+#if defined(SOL_AX25)
+ /* AX.25 socket options */
+ rb_define_const(rb_cSocket, "SOL_AX25", INTEGER2NUM(SOL_AX25));
+ /* AX.25 socket options */
+ rb_define_const(rb_mSockConst, "SOL_AX25", INTEGER2NUM(SOL_AX25));
+#endif
+#if defined(SOL_ATALK)
+ /* AppleTalk socket options */
+ rb_define_const(rb_cSocket, "SOL_ATALK", INTEGER2NUM(SOL_ATALK));
+ /* AppleTalk socket options */
+ rb_define_const(rb_mSockConst, "SOL_ATALK", INTEGER2NUM(SOL_ATALK));
+#endif
+#if defined(SOL_TCP)
+ /* TCP socket options */
+ rb_define_const(rb_cSocket, "SOL_TCP", INTEGER2NUM(SOL_TCP));
+ /* TCP socket options */
+ rb_define_const(rb_mSockConst, "SOL_TCP", INTEGER2NUM(SOL_TCP));
+#endif
+#if defined(SOL_UDP)
+ /* UDP socket options */
+ rb_define_const(rb_cSocket, "SOL_UDP", INTEGER2NUM(SOL_UDP));
+ /* UDP socket options */
+ rb_define_const(rb_mSockConst, "SOL_UDP", INTEGER2NUM(SOL_UDP));
+#endif
+#if defined(IPPROTO_IP)
+ /* Dummy protocol for IP */
+ rb_define_const(rb_cSocket, "IPPROTO_IP", INTEGER2NUM(IPPROTO_IP));
+ /* Dummy protocol for IP */
+ rb_define_const(rb_mSockConst, "IPPROTO_IP", INTEGER2NUM(IPPROTO_IP));
+#endif
+#if defined(IPPROTO_ICMP)
+ /* Control message protocol */
+ rb_define_const(rb_cSocket, "IPPROTO_ICMP", INTEGER2NUM(IPPROTO_ICMP));
+ /* Control message protocol */
+ rb_define_const(rb_mSockConst, "IPPROTO_ICMP", INTEGER2NUM(IPPROTO_ICMP));
+#endif
+#if defined(IPPROTO_IGMP)
+ /* Group Management Protocol */
+ rb_define_const(rb_cSocket, "IPPROTO_IGMP", INTEGER2NUM(IPPROTO_IGMP));
+ /* Group Management Protocol */
+ rb_define_const(rb_mSockConst, "IPPROTO_IGMP", INTEGER2NUM(IPPROTO_IGMP));
+#endif
+#if defined(IPPROTO_GGP)
+ /* Gateway to Gateway Protocol */
+ rb_define_const(rb_cSocket, "IPPROTO_GGP", INTEGER2NUM(IPPROTO_GGP));
+ /* Gateway to Gateway Protocol */
+ rb_define_const(rb_mSockConst, "IPPROTO_GGP", INTEGER2NUM(IPPROTO_GGP));
+#endif
+#if defined(IPPROTO_TCP)
+ /* TCP */
+ rb_define_const(rb_cSocket, "IPPROTO_TCP", INTEGER2NUM(IPPROTO_TCP));
+ /* TCP */
+ rb_define_const(rb_mSockConst, "IPPROTO_TCP", INTEGER2NUM(IPPROTO_TCP));
+#endif
+#if defined(IPPROTO_EGP)
+ /* Exterior Gateway Protocol */
+ rb_define_const(rb_cSocket, "IPPROTO_EGP", INTEGER2NUM(IPPROTO_EGP));
+ /* Exterior Gateway Protocol */
+ rb_define_const(rb_mSockConst, "IPPROTO_EGP", INTEGER2NUM(IPPROTO_EGP));
+#endif
+#if defined(IPPROTO_PUP)
+ /* PARC Universal Packet protocol */
+ rb_define_const(rb_cSocket, "IPPROTO_PUP", INTEGER2NUM(IPPROTO_PUP));
+ /* PARC Universal Packet protocol */
+ rb_define_const(rb_mSockConst, "IPPROTO_PUP", INTEGER2NUM(IPPROTO_PUP));
+#endif
+#if defined(IPPROTO_UDP)
+ /* UDP */
+ rb_define_const(rb_cSocket, "IPPROTO_UDP", INTEGER2NUM(IPPROTO_UDP));
+ /* UDP */
+ rb_define_const(rb_mSockConst, "IPPROTO_UDP", INTEGER2NUM(IPPROTO_UDP));
+#endif
+#if defined(IPPROTO_IDP)
+ /* XNS IDP */
+ rb_define_const(rb_cSocket, "IPPROTO_IDP", INTEGER2NUM(IPPROTO_IDP));
+ /* XNS IDP */
+ rb_define_const(rb_mSockConst, "IPPROTO_IDP", INTEGER2NUM(IPPROTO_IDP));
+#endif
+#if defined(IPPROTO_HELLO)
+ /* "hello" routing protocol */
+ rb_define_const(rb_cSocket, "IPPROTO_HELLO", INTEGER2NUM(IPPROTO_HELLO));
+ /* "hello" routing protocol */
+ rb_define_const(rb_mSockConst, "IPPROTO_HELLO", INTEGER2NUM(IPPROTO_HELLO));
+#endif
+#if defined(IPPROTO_ND)
+ /* Sun net disk protocol */
+ rb_define_const(rb_cSocket, "IPPROTO_ND", INTEGER2NUM(IPPROTO_ND));
+ /* Sun net disk protocol */
+ rb_define_const(rb_mSockConst, "IPPROTO_ND", INTEGER2NUM(IPPROTO_ND));
+#endif
+#if defined(IPPROTO_TP)
+ /* ISO transport protocol class 4 */
+ rb_define_const(rb_cSocket, "IPPROTO_TP", INTEGER2NUM(IPPROTO_TP));
+ /* ISO transport protocol class 4 */
+ rb_define_const(rb_mSockConst, "IPPROTO_TP", INTEGER2NUM(IPPROTO_TP));
+#endif
+#if defined(IPPROTO_XTP)
+ /* Xpress Transport Protocol */
+ rb_define_const(rb_cSocket, "IPPROTO_XTP", INTEGER2NUM(IPPROTO_XTP));
+ /* Xpress Transport Protocol */
+ rb_define_const(rb_mSockConst, "IPPROTO_XTP", INTEGER2NUM(IPPROTO_XTP));
+#endif
+#if defined(IPPROTO_EON)
+ /* ISO cnlp */
+ rb_define_const(rb_cSocket, "IPPROTO_EON", INTEGER2NUM(IPPROTO_EON));
+ /* ISO cnlp */
+ rb_define_const(rb_mSockConst, "IPPROTO_EON", INTEGER2NUM(IPPROTO_EON));
+#endif
+#if defined(IPPROTO_BIP)
+ /* */
+ rb_define_const(rb_cSocket, "IPPROTO_BIP", INTEGER2NUM(IPPROTO_BIP));
+ /* */
+ rb_define_const(rb_mSockConst, "IPPROTO_BIP", INTEGER2NUM(IPPROTO_BIP));
+#endif
+#if defined(IPPROTO_AH)
+ /* IP6 auth header */
+ rb_define_const(rb_cSocket, "IPPROTO_AH", INTEGER2NUM(IPPROTO_AH));
+ /* IP6 auth header */
+ rb_define_const(rb_mSockConst, "IPPROTO_AH", INTEGER2NUM(IPPROTO_AH));
+#endif
+#if defined(IPPROTO_DSTOPTS)
+ /* IP6 destination option */
+ rb_define_const(rb_cSocket, "IPPROTO_DSTOPTS", INTEGER2NUM(IPPROTO_DSTOPTS));
+ /* IP6 destination option */
+ rb_define_const(rb_mSockConst, "IPPROTO_DSTOPTS", INTEGER2NUM(IPPROTO_DSTOPTS));
+#endif
+#if defined(IPPROTO_ESP)
+ /* IP6 Encapsulated Security Payload */
+ rb_define_const(rb_cSocket, "IPPROTO_ESP", INTEGER2NUM(IPPROTO_ESP));
+ /* IP6 Encapsulated Security Payload */
+ rb_define_const(rb_mSockConst, "IPPROTO_ESP", INTEGER2NUM(IPPROTO_ESP));
+#endif
+#if defined(IPPROTO_FRAGMENT)
+ /* IP6 fragmentation header */
+ rb_define_const(rb_cSocket, "IPPROTO_FRAGMENT", INTEGER2NUM(IPPROTO_FRAGMENT));
+ /* IP6 fragmentation header */
+ rb_define_const(rb_mSockConst, "IPPROTO_FRAGMENT", INTEGER2NUM(IPPROTO_FRAGMENT));
+#endif
+#if defined(IPPROTO_HOPOPTS)
+ /* IP6 hop-by-hop options */
+ rb_define_const(rb_cSocket, "IPPROTO_HOPOPTS", INTEGER2NUM(IPPROTO_HOPOPTS));
+ /* IP6 hop-by-hop options */
+ rb_define_const(rb_mSockConst, "IPPROTO_HOPOPTS", INTEGER2NUM(IPPROTO_HOPOPTS));
+#endif
+#if defined(IPPROTO_ICMPV6)
+ /* ICMP6 */
+ rb_define_const(rb_cSocket, "IPPROTO_ICMPV6", INTEGER2NUM(IPPROTO_ICMPV6));
+ /* ICMP6 */
+ rb_define_const(rb_mSockConst, "IPPROTO_ICMPV6", INTEGER2NUM(IPPROTO_ICMPV6));
+#endif
+#if defined(IPPROTO_IPV6)
+ /* IP6 header */
+ rb_define_const(rb_cSocket, "IPPROTO_IPV6", INTEGER2NUM(IPPROTO_IPV6));
+ /* IP6 header */
+ rb_define_const(rb_mSockConst, "IPPROTO_IPV6", INTEGER2NUM(IPPROTO_IPV6));
+#endif
+#if defined(IPPROTO_NONE)
+ /* IP6 no next header */
+ rb_define_const(rb_cSocket, "IPPROTO_NONE", INTEGER2NUM(IPPROTO_NONE));
+ /* IP6 no next header */
+ rb_define_const(rb_mSockConst, "IPPROTO_NONE", INTEGER2NUM(IPPROTO_NONE));
+#endif
+#if defined(IPPROTO_ROUTING)
+ /* IP6 routing header */
+ rb_define_const(rb_cSocket, "IPPROTO_ROUTING", INTEGER2NUM(IPPROTO_ROUTING));
+ /* IP6 routing header */
+ rb_define_const(rb_mSockConst, "IPPROTO_ROUTING", INTEGER2NUM(IPPROTO_ROUTING));
+#endif
+#if defined(IPPROTO_RAW)
+ /* Raw IP packet */
+ rb_define_const(rb_cSocket, "IPPROTO_RAW", INTEGER2NUM(IPPROTO_RAW));
+ /* Raw IP packet */
+ rb_define_const(rb_mSockConst, "IPPROTO_RAW", INTEGER2NUM(IPPROTO_RAW));
+#endif
+#if defined(IPPROTO_MAX)
+ /* Maximum IPPROTO constant */
+ rb_define_const(rb_cSocket, "IPPROTO_MAX", INTEGER2NUM(IPPROTO_MAX));
+ /* Maximum IPPROTO constant */
+ rb_define_const(rb_mSockConst, "IPPROTO_MAX", INTEGER2NUM(IPPROTO_MAX));
+#endif
+#if defined(IPPORT_RESERVED)
+ /* Default minimum address for bind or connect */
+ rb_define_const(rb_cSocket, "IPPORT_RESERVED", INTEGER2NUM(IPPORT_RESERVED));
+ /* Default minimum address for bind or connect */
+ rb_define_const(rb_mSockConst, "IPPORT_RESERVED", INTEGER2NUM(IPPORT_RESERVED));
+#endif
+#if defined(IPPORT_USERRESERVED)
+ /* Default maximum address for bind or connect */
+ rb_define_const(rb_cSocket, "IPPORT_USERRESERVED", INTEGER2NUM(IPPORT_USERRESERVED));
+ /* Default maximum address for bind or connect */
+ rb_define_const(rb_mSockConst, "IPPORT_USERRESERVED", INTEGER2NUM(IPPORT_USERRESERVED));
+#endif
+#if defined(INADDR_ANY)
+ /* A socket bound to INADDR_ANY receives packets from all interfaces and sends from the default IP address */
+ rb_define_const(rb_cSocket, "INADDR_ANY", INTEGER2NUM(INADDR_ANY));
+ /* A socket bound to INADDR_ANY receives packets from all interfaces and sends from the default IP address */
+ rb_define_const(rb_mSockConst, "INADDR_ANY", INTEGER2NUM(INADDR_ANY));
+#endif
+#if defined(INADDR_BROADCAST)
+ /* The network broadcast address */
+ rb_define_const(rb_cSocket, "INADDR_BROADCAST", INTEGER2NUM(INADDR_BROADCAST));
+ /* The network broadcast address */
+ rb_define_const(rb_mSockConst, "INADDR_BROADCAST", INTEGER2NUM(INADDR_BROADCAST));
+#endif
+#if defined(INADDR_LOOPBACK)
+ /* The loopback address */
+ rb_define_const(rb_cSocket, "INADDR_LOOPBACK", INTEGER2NUM(INADDR_LOOPBACK));
+ /* The loopback address */
+ rb_define_const(rb_mSockConst, "INADDR_LOOPBACK", INTEGER2NUM(INADDR_LOOPBACK));
+#endif
+#if defined(INADDR_UNSPEC_GROUP)
+ /* The reserved multicast group */
+ rb_define_const(rb_cSocket, "INADDR_UNSPEC_GROUP", INTEGER2NUM(INADDR_UNSPEC_GROUP));
+ /* The reserved multicast group */
+ rb_define_const(rb_mSockConst, "INADDR_UNSPEC_GROUP", INTEGER2NUM(INADDR_UNSPEC_GROUP));
+#endif
+#if defined(INADDR_ALLHOSTS_GROUP)
+ /* Multicast group for all systems on this subset */
+ rb_define_const(rb_cSocket, "INADDR_ALLHOSTS_GROUP", INTEGER2NUM(INADDR_ALLHOSTS_GROUP));
+ /* Multicast group for all systems on this subset */
+ rb_define_const(rb_mSockConst, "INADDR_ALLHOSTS_GROUP", INTEGER2NUM(INADDR_ALLHOSTS_GROUP));
+#endif
+#if defined(INADDR_MAX_LOCAL_GROUP)
+ /* The last local network multicast group */
+ rb_define_const(rb_cSocket, "INADDR_MAX_LOCAL_GROUP", INTEGER2NUM(INADDR_MAX_LOCAL_GROUP));
+ /* The last local network multicast group */
+ rb_define_const(rb_mSockConst, "INADDR_MAX_LOCAL_GROUP", INTEGER2NUM(INADDR_MAX_LOCAL_GROUP));
+#endif
+#if defined(INADDR_NONE)
+ /* A bitmask for matching no valid IP address */
+ rb_define_const(rb_cSocket, "INADDR_NONE", INTEGER2NUM(INADDR_NONE));
+ /* A bitmask for matching no valid IP address */
+ rb_define_const(rb_mSockConst, "INADDR_NONE", INTEGER2NUM(INADDR_NONE));
+#endif
+#if defined(IP_OPTIONS)
+ /* IP options to be included in packets */
+ rb_define_const(rb_cSocket, "IP_OPTIONS", INTEGER2NUM(IP_OPTIONS));
+ /* IP options to be included in packets */
+ rb_define_const(rb_mSockConst, "IP_OPTIONS", INTEGER2NUM(IP_OPTIONS));
+#endif
+#if defined(IP_HDRINCL)
+ /* Header is included with data */
+ rb_define_const(rb_cSocket, "IP_HDRINCL", INTEGER2NUM(IP_HDRINCL));
+ /* Header is included with data */
+ rb_define_const(rb_mSockConst, "IP_HDRINCL", INTEGER2NUM(IP_HDRINCL));
+#endif
+#if defined(IP_TOS)
+ /* IP type-of-service */
+ rb_define_const(rb_cSocket, "IP_TOS", INTEGER2NUM(IP_TOS));
+ /* IP type-of-service */
+ rb_define_const(rb_mSockConst, "IP_TOS", INTEGER2NUM(IP_TOS));
+#endif
+#if defined(IP_TTL)
+ /* IP time-to-live */
+ rb_define_const(rb_cSocket, "IP_TTL", INTEGER2NUM(IP_TTL));
+ /* IP time-to-live */
+ rb_define_const(rb_mSockConst, "IP_TTL", INTEGER2NUM(IP_TTL));
+#endif
+#if defined(IP_RECVOPTS)
+ /* Receive all IP options with datagram */
+ rb_define_const(rb_cSocket, "IP_RECVOPTS", INTEGER2NUM(IP_RECVOPTS));
+ /* Receive all IP options with datagram */
+ rb_define_const(rb_mSockConst, "IP_RECVOPTS", INTEGER2NUM(IP_RECVOPTS));
+#endif
+#if defined(IP_RECVRETOPTS)
+ /* Receive all IP options for response */
+ rb_define_const(rb_cSocket, "IP_RECVRETOPTS", INTEGER2NUM(IP_RECVRETOPTS));
+ /* Receive all IP options for response */
+ rb_define_const(rb_mSockConst, "IP_RECVRETOPTS", INTEGER2NUM(IP_RECVRETOPTS));
+#endif
+#if defined(IP_RECVDSTADDR)
+ /* Receive IP destination address with datagram */
+ rb_define_const(rb_cSocket, "IP_RECVDSTADDR", INTEGER2NUM(IP_RECVDSTADDR));
+ /* Receive IP destination address with datagram */
+ rb_define_const(rb_mSockConst, "IP_RECVDSTADDR", INTEGER2NUM(IP_RECVDSTADDR));
+#endif
+#if defined(IP_RETOPTS)
+ /* IP options to be included in datagrams */
+ rb_define_const(rb_cSocket, "IP_RETOPTS", INTEGER2NUM(IP_RETOPTS));
+ /* IP options to be included in datagrams */
+ rb_define_const(rb_mSockConst, "IP_RETOPTS", INTEGER2NUM(IP_RETOPTS));
+#endif
+#if defined(IP_MINTTL)
+ /* Minimum TTL allowed for received packets */
+ rb_define_const(rb_cSocket, "IP_MINTTL", INTEGER2NUM(IP_MINTTL));
+ /* Minimum TTL allowed for received packets */
+ rb_define_const(rb_mSockConst, "IP_MINTTL", INTEGER2NUM(IP_MINTTL));
+#endif
+#if defined(IP_DONTFRAG)
+ /* Don't fragment packets */
+ rb_define_const(rb_cSocket, "IP_DONTFRAG", INTEGER2NUM(IP_DONTFRAG));
+ /* Don't fragment packets */
+ rb_define_const(rb_mSockConst, "IP_DONTFRAG", INTEGER2NUM(IP_DONTFRAG));
+#endif
+#if defined(IP_SENDSRCADDR)
+ /* Source address for outgoing UDP datagrams */
+ rb_define_const(rb_cSocket, "IP_SENDSRCADDR", INTEGER2NUM(IP_SENDSRCADDR));
+ /* Source address for outgoing UDP datagrams */
+ rb_define_const(rb_mSockConst, "IP_SENDSRCADDR", INTEGER2NUM(IP_SENDSRCADDR));
+#endif
+#if defined(IP_ONESBCAST)
+ /* Force outgoing broadcast datagrams to have the undirected broadcast address */
+ rb_define_const(rb_cSocket, "IP_ONESBCAST", INTEGER2NUM(IP_ONESBCAST));
+ /* Force outgoing broadcast datagrams to have the undirected broadcast address */
+ rb_define_const(rb_mSockConst, "IP_ONESBCAST", INTEGER2NUM(IP_ONESBCAST));
+#endif
+#if defined(IP_RECVTTL)
+ /* Receive IP TTL with datagrams */
+ rb_define_const(rb_cSocket, "IP_RECVTTL", INTEGER2NUM(IP_RECVTTL));
+ /* Receive IP TTL with datagrams */
+ rb_define_const(rb_mSockConst, "IP_RECVTTL", INTEGER2NUM(IP_RECVTTL));
+#endif
+#if defined(IP_RECVIF)
+ /* Receive interface information with datagrams */
+ rb_define_const(rb_cSocket, "IP_RECVIF", INTEGER2NUM(IP_RECVIF));
+ /* Receive interface information with datagrams */
+ rb_define_const(rb_mSockConst, "IP_RECVIF", INTEGER2NUM(IP_RECVIF));
+#endif
+#if defined(IP_RECVSLLA)
+ /* Receive link-layer address with datagrams */
+ rb_define_const(rb_cSocket, "IP_RECVSLLA", INTEGER2NUM(IP_RECVSLLA));
+ /* Receive link-layer address with datagrams */
+ rb_define_const(rb_mSockConst, "IP_RECVSLLA", INTEGER2NUM(IP_RECVSLLA));
+#endif
+#if defined(IP_PORTRANGE)
+ /* Set the port range for sockets with unspecified port numbers */
+ rb_define_const(rb_cSocket, "IP_PORTRANGE", INTEGER2NUM(IP_PORTRANGE));
+ /* Set the port range for sockets with unspecified port numbers */
+ rb_define_const(rb_mSockConst, "IP_PORTRANGE", INTEGER2NUM(IP_PORTRANGE));
+#endif
+#if defined(IP_MULTICAST_IF)
+ /* IP multicast interface */
+ rb_define_const(rb_cSocket, "IP_MULTICAST_IF", INTEGER2NUM(IP_MULTICAST_IF));
+ /* IP multicast interface */
+ rb_define_const(rb_mSockConst, "IP_MULTICAST_IF", INTEGER2NUM(IP_MULTICAST_IF));
+#endif
+#if defined(IP_MULTICAST_TTL)
+ /* IP multicast TTL */
+ rb_define_const(rb_cSocket, "IP_MULTICAST_TTL", INTEGER2NUM(IP_MULTICAST_TTL));
+ /* IP multicast TTL */
+ rb_define_const(rb_mSockConst, "IP_MULTICAST_TTL", INTEGER2NUM(IP_MULTICAST_TTL));
+#endif
+#if defined(IP_MULTICAST_LOOP)
+ /* IP multicast loopback */
+ rb_define_const(rb_cSocket, "IP_MULTICAST_LOOP", INTEGER2NUM(IP_MULTICAST_LOOP));
+ /* IP multicast loopback */
+ rb_define_const(rb_mSockConst, "IP_MULTICAST_LOOP", INTEGER2NUM(IP_MULTICAST_LOOP));
+#endif
+#if defined(IP_ADD_MEMBERSHIP)
+ /* Add a multicast group membership */
+ rb_define_const(rb_cSocket, "IP_ADD_MEMBERSHIP", INTEGER2NUM(IP_ADD_MEMBERSHIP));
+ /* Add a multicast group membership */
+ rb_define_const(rb_mSockConst, "IP_ADD_MEMBERSHIP", INTEGER2NUM(IP_ADD_MEMBERSHIP));
+#endif
+#if defined(IP_DROP_MEMBERSHIP)
+ /* Drop a multicast group membership */
+ rb_define_const(rb_cSocket, "IP_DROP_MEMBERSHIP", INTEGER2NUM(IP_DROP_MEMBERSHIP));
+ /* Drop a multicast group membership */
+ rb_define_const(rb_mSockConst, "IP_DROP_MEMBERSHIP", INTEGER2NUM(IP_DROP_MEMBERSHIP));
+#endif
+#if defined(IP_DEFAULT_MULTICAST_TTL)
+ /* Default multicast TTL */
+ rb_define_const(rb_cSocket, "IP_DEFAULT_MULTICAST_TTL", INTEGER2NUM(IP_DEFAULT_MULTICAST_TTL));
+ /* Default multicast TTL */
+ rb_define_const(rb_mSockConst, "IP_DEFAULT_MULTICAST_TTL", INTEGER2NUM(IP_DEFAULT_MULTICAST_TTL));
+#endif
+#if defined(IP_DEFAULT_MULTICAST_LOOP)
+ /* Default multicast loopback */
+ rb_define_const(rb_cSocket, "IP_DEFAULT_MULTICAST_LOOP", INTEGER2NUM(IP_DEFAULT_MULTICAST_LOOP));
+ /* Default multicast loopback */
+ rb_define_const(rb_mSockConst, "IP_DEFAULT_MULTICAST_LOOP", INTEGER2NUM(IP_DEFAULT_MULTICAST_LOOP));
+#endif
+#if defined(IP_MAX_MEMBERSHIPS)
+ /* Maximum number multicast groups a socket can join */
+ rb_define_const(rb_cSocket, "IP_MAX_MEMBERSHIPS", INTEGER2NUM(IP_MAX_MEMBERSHIPS));
+ /* Maximum number multicast groups a socket can join */
+ rb_define_const(rb_mSockConst, "IP_MAX_MEMBERSHIPS", INTEGER2NUM(IP_MAX_MEMBERSHIPS));
+#endif
+#if defined(IP_ROUTER_ALERT)
+ /* Notify transit routers to more closely examine the contents of an IP packet */
+ rb_define_const(rb_cSocket, "IP_ROUTER_ALERT", INTEGER2NUM(IP_ROUTER_ALERT));
+ /* Notify transit routers to more closely examine the contents of an IP packet */
+ rb_define_const(rb_mSockConst, "IP_ROUTER_ALERT", INTEGER2NUM(IP_ROUTER_ALERT));
+#endif
+#if defined(IP_PKTINFO)
+ /* Receive packet information with datagrams */
+ rb_define_const(rb_cSocket, "IP_PKTINFO", INTEGER2NUM(IP_PKTINFO));
+ /* Receive packet information with datagrams */
+ rb_define_const(rb_mSockConst, "IP_PKTINFO", INTEGER2NUM(IP_PKTINFO));
+#endif
+#if defined(IP_PKTOPTIONS)
+ /* Receive packet options with datagrams */
+ rb_define_const(rb_cSocket, "IP_PKTOPTIONS", INTEGER2NUM(IP_PKTOPTIONS));
+ /* Receive packet options with datagrams */
+ rb_define_const(rb_mSockConst, "IP_PKTOPTIONS", INTEGER2NUM(IP_PKTOPTIONS));
+#endif
+#if defined(IP_MTU_DISCOVER)
+ /* Path MTU discovery */
+ rb_define_const(rb_cSocket, "IP_MTU_DISCOVER", INTEGER2NUM(IP_MTU_DISCOVER));
+ /* Path MTU discovery */
+ rb_define_const(rb_mSockConst, "IP_MTU_DISCOVER", INTEGER2NUM(IP_MTU_DISCOVER));
+#endif
+#if defined(IP_RECVERR)
+ /* Enable extended reliable error message passing */
+ rb_define_const(rb_cSocket, "IP_RECVERR", INTEGER2NUM(IP_RECVERR));
+ /* Enable extended reliable error message passing */
+ rb_define_const(rb_mSockConst, "IP_RECVERR", INTEGER2NUM(IP_RECVERR));
+#endif
+#if defined(IP_RECVTOS)
+ /* Receive TOS with incoming packets */
+ rb_define_const(rb_cSocket, "IP_RECVTOS", INTEGER2NUM(IP_RECVTOS));
+ /* Receive TOS with incoming packets */
+ rb_define_const(rb_mSockConst, "IP_RECVTOS", INTEGER2NUM(IP_RECVTOS));
+#endif
+#if defined(IP_MTU)
+ /* The Maximum Transmission Unit of the socket */
+ rb_define_const(rb_cSocket, "IP_MTU", INTEGER2NUM(IP_MTU));
+ /* The Maximum Transmission Unit of the socket */
+ rb_define_const(rb_mSockConst, "IP_MTU", INTEGER2NUM(IP_MTU));
+#endif
+#if defined(IP_FREEBIND)
+ /* Allow binding to nonexistent IP addresses */
+ rb_define_const(rb_cSocket, "IP_FREEBIND", INTEGER2NUM(IP_FREEBIND));
+ /* Allow binding to nonexistent IP addresses */
+ rb_define_const(rb_mSockConst, "IP_FREEBIND", INTEGER2NUM(IP_FREEBIND));
+#endif
+#if defined(IP_IPSEC_POLICY)
+ /* IPsec security policy */
+ rb_define_const(rb_cSocket, "IP_IPSEC_POLICY", INTEGER2NUM(IP_IPSEC_POLICY));
+ /* IPsec security policy */
+ rb_define_const(rb_mSockConst, "IP_IPSEC_POLICY", INTEGER2NUM(IP_IPSEC_POLICY));
+#endif
+#if defined(IP_XFRM_POLICY)
+ /* */
+ rb_define_const(rb_cSocket, "IP_XFRM_POLICY", INTEGER2NUM(IP_XFRM_POLICY));
+ /* */
+ rb_define_const(rb_mSockConst, "IP_XFRM_POLICY", INTEGER2NUM(IP_XFRM_POLICY));
+#endif
+#if defined(IP_PASSSEC)
+ /* Retrieve security context with datagram */
+ rb_define_const(rb_cSocket, "IP_PASSSEC", INTEGER2NUM(IP_PASSSEC));
+ /* Retrieve security context with datagram */
+ rb_define_const(rb_mSockConst, "IP_PASSSEC", INTEGER2NUM(IP_PASSSEC));
+#endif
+#if defined(IP_TRANSPARENT)
+ /* Transparent proxy */
+ rb_define_const(rb_cSocket, "IP_TRANSPARENT", INTEGER2NUM(IP_TRANSPARENT));
+ /* Transparent proxy */
+ rb_define_const(rb_mSockConst, "IP_TRANSPARENT", INTEGER2NUM(IP_TRANSPARENT));
+#endif
+#if defined(IP_PMTUDISC_DONT)
+ /* Never send DF frames */
+ rb_define_const(rb_cSocket, "IP_PMTUDISC_DONT", INTEGER2NUM(IP_PMTUDISC_DONT));
+ /* Never send DF frames */
+ rb_define_const(rb_mSockConst, "IP_PMTUDISC_DONT", INTEGER2NUM(IP_PMTUDISC_DONT));
+#endif
+#if defined(IP_PMTUDISC_WANT)
+ /* Use per-route hints */
+ rb_define_const(rb_cSocket, "IP_PMTUDISC_WANT", INTEGER2NUM(IP_PMTUDISC_WANT));
+ /* Use per-route hints */
+ rb_define_const(rb_mSockConst, "IP_PMTUDISC_WANT", INTEGER2NUM(IP_PMTUDISC_WANT));
+#endif
+#if defined(IP_PMTUDISC_DO)
+ /* Always send DF frames */
+ rb_define_const(rb_cSocket, "IP_PMTUDISC_DO", INTEGER2NUM(IP_PMTUDISC_DO));
+ /* Always send DF frames */
+ rb_define_const(rb_mSockConst, "IP_PMTUDISC_DO", INTEGER2NUM(IP_PMTUDISC_DO));
+#endif
+#if defined(IP_UNBLOCK_SOURCE)
+ /* Unblock IPv4 multicast packets with a give source address */
+ rb_define_const(rb_cSocket, "IP_UNBLOCK_SOURCE", INTEGER2NUM(IP_UNBLOCK_SOURCE));
+ /* Unblock IPv4 multicast packets with a give source address */
+ rb_define_const(rb_mSockConst, "IP_UNBLOCK_SOURCE", INTEGER2NUM(IP_UNBLOCK_SOURCE));
+#endif
+#if defined(IP_BLOCK_SOURCE)
+ /* Block IPv4 multicast packets with a give source address */
+ rb_define_const(rb_cSocket, "IP_BLOCK_SOURCE", INTEGER2NUM(IP_BLOCK_SOURCE));
+ /* Block IPv4 multicast packets with a give source address */
+ rb_define_const(rb_mSockConst, "IP_BLOCK_SOURCE", INTEGER2NUM(IP_BLOCK_SOURCE));
+#endif
+#if defined(IP_ADD_SOURCE_MEMBERSHIP)
+ /* Add a multicast group membership */
+ rb_define_const(rb_cSocket, "IP_ADD_SOURCE_MEMBERSHIP", INTEGER2NUM(IP_ADD_SOURCE_MEMBERSHIP));
+ /* Add a multicast group membership */
+ rb_define_const(rb_mSockConst, "IP_ADD_SOURCE_MEMBERSHIP", INTEGER2NUM(IP_ADD_SOURCE_MEMBERSHIP));
+#endif
+#if defined(IP_DROP_SOURCE_MEMBERSHIP)
+ /* Drop a multicast group membership */
+ rb_define_const(rb_cSocket, "IP_DROP_SOURCE_MEMBERSHIP", INTEGER2NUM(IP_DROP_SOURCE_MEMBERSHIP));
+ /* Drop a multicast group membership */
+ rb_define_const(rb_mSockConst, "IP_DROP_SOURCE_MEMBERSHIP", INTEGER2NUM(IP_DROP_SOURCE_MEMBERSHIP));
+#endif
+#if defined(IP_MSFILTER)
+ /* Multicast source filtering */
+ rb_define_const(rb_cSocket, "IP_MSFILTER", INTEGER2NUM(IP_MSFILTER));
+ /* Multicast source filtering */
+ rb_define_const(rb_mSockConst, "IP_MSFILTER", INTEGER2NUM(IP_MSFILTER));
+#endif
+#if defined(MCAST_JOIN_GROUP)
+ /* Join a multicast group */
+ rb_define_const(rb_cSocket, "MCAST_JOIN_GROUP", INTEGER2NUM(MCAST_JOIN_GROUP));
+ /* Join a multicast group */
+ rb_define_const(rb_mSockConst, "MCAST_JOIN_GROUP", INTEGER2NUM(MCAST_JOIN_GROUP));
+#endif
+#if defined(MCAST_BLOCK_SOURCE)
+ /* Block multicast packets from this source */
+ rb_define_const(rb_cSocket, "MCAST_BLOCK_SOURCE", INTEGER2NUM(MCAST_BLOCK_SOURCE));
+ /* Block multicast packets from this source */
+ rb_define_const(rb_mSockConst, "MCAST_BLOCK_SOURCE", INTEGER2NUM(MCAST_BLOCK_SOURCE));
+#endif
+#if defined(MCAST_UNBLOCK_SOURCE)
+ /* Unblock multicast packets from this source */
+ rb_define_const(rb_cSocket, "MCAST_UNBLOCK_SOURCE", INTEGER2NUM(MCAST_UNBLOCK_SOURCE));
+ /* Unblock multicast packets from this source */
+ rb_define_const(rb_mSockConst, "MCAST_UNBLOCK_SOURCE", INTEGER2NUM(MCAST_UNBLOCK_SOURCE));
+#endif
+#if defined(MCAST_LEAVE_GROUP)
+ /* Leave a multicast group */
+ rb_define_const(rb_cSocket, "MCAST_LEAVE_GROUP", INTEGER2NUM(MCAST_LEAVE_GROUP));
+ /* Leave a multicast group */
+ rb_define_const(rb_mSockConst, "MCAST_LEAVE_GROUP", INTEGER2NUM(MCAST_LEAVE_GROUP));
+#endif
+#if defined(MCAST_JOIN_SOURCE_GROUP)
+ /* Join a multicast source group */
+ rb_define_const(rb_cSocket, "MCAST_JOIN_SOURCE_GROUP", INTEGER2NUM(MCAST_JOIN_SOURCE_GROUP));
+ /* Join a multicast source group */
+ rb_define_const(rb_mSockConst, "MCAST_JOIN_SOURCE_GROUP", INTEGER2NUM(MCAST_JOIN_SOURCE_GROUP));
+#endif
+#if defined(MCAST_LEAVE_SOURCE_GROUP)
+ /* Leave a multicast source group */
+ rb_define_const(rb_cSocket, "MCAST_LEAVE_SOURCE_GROUP", INTEGER2NUM(MCAST_LEAVE_SOURCE_GROUP));
+ /* Leave a multicast source group */
+ rb_define_const(rb_mSockConst, "MCAST_LEAVE_SOURCE_GROUP", INTEGER2NUM(MCAST_LEAVE_SOURCE_GROUP));
+#endif
+#if defined(MCAST_MSFILTER)
+ /* Multicast source filtering */
+ rb_define_const(rb_cSocket, "MCAST_MSFILTER", INTEGER2NUM(MCAST_MSFILTER));
+ /* Multicast source filtering */
+ rb_define_const(rb_mSockConst, "MCAST_MSFILTER", INTEGER2NUM(MCAST_MSFILTER));
+#endif
+#if defined(MCAST_EXCLUDE)
+ /* Exclusive multicast source filter */
+ rb_define_const(rb_cSocket, "MCAST_EXCLUDE", INTEGER2NUM(MCAST_EXCLUDE));
+ /* Exclusive multicast source filter */
+ rb_define_const(rb_mSockConst, "MCAST_EXCLUDE", INTEGER2NUM(MCAST_EXCLUDE));
+#endif
+#if defined(MCAST_INCLUDE)
+ /* Inclusive multicast source filter */
+ rb_define_const(rb_cSocket, "MCAST_INCLUDE", INTEGER2NUM(MCAST_INCLUDE));
+ /* Inclusive multicast source filter */
+ rb_define_const(rb_mSockConst, "MCAST_INCLUDE", INTEGER2NUM(MCAST_INCLUDE));
+#endif
+#if defined(SO_DEBUG)
+ /* Debug info recording */
+ rb_define_const(rb_cSocket, "SO_DEBUG", INTEGER2NUM(SO_DEBUG));
+ /* Debug info recording */
+ rb_define_const(rb_mSockConst, "SO_DEBUG", INTEGER2NUM(SO_DEBUG));
+#endif
+#if defined(SO_REUSEADDR)
+ /* Allow local address reuse */
+ rb_define_const(rb_cSocket, "SO_REUSEADDR", INTEGER2NUM(SO_REUSEADDR));
+ /* Allow local address reuse */
+ rb_define_const(rb_mSockConst, "SO_REUSEADDR", INTEGER2NUM(SO_REUSEADDR));
+#endif
+#if defined(SO_REUSEPORT)
+ /* Allow local address and port reuse */
+ rb_define_const(rb_cSocket, "SO_REUSEPORT", INTEGER2NUM(SO_REUSEPORT));
+ /* Allow local address and port reuse */
+ rb_define_const(rb_mSockConst, "SO_REUSEPORT", INTEGER2NUM(SO_REUSEPORT));
+#endif
+#if defined(SO_TYPE)
+ /* Get the socket type */
+ rb_define_const(rb_cSocket, "SO_TYPE", INTEGER2NUM(SO_TYPE));
+ /* Get the socket type */
+ rb_define_const(rb_mSockConst, "SO_TYPE", INTEGER2NUM(SO_TYPE));
+#endif
+#if defined(SO_ERROR)
+ /* Get and clear the error status */
+ rb_define_const(rb_cSocket, "SO_ERROR", INTEGER2NUM(SO_ERROR));
+ /* Get and clear the error status */
+ rb_define_const(rb_mSockConst, "SO_ERROR", INTEGER2NUM(SO_ERROR));
+#endif
+#if defined(SO_DONTROUTE)
+ /* Use interface addresses */
+ rb_define_const(rb_cSocket, "SO_DONTROUTE", INTEGER2NUM(SO_DONTROUTE));
+ /* Use interface addresses */
+ rb_define_const(rb_mSockConst, "SO_DONTROUTE", INTEGER2NUM(SO_DONTROUTE));
+#endif
+#if defined(SO_BROADCAST)
+ /* Permit sending of broadcast messages */
+ rb_define_const(rb_cSocket, "SO_BROADCAST", INTEGER2NUM(SO_BROADCAST));
+ /* Permit sending of broadcast messages */
+ rb_define_const(rb_mSockConst, "SO_BROADCAST", INTEGER2NUM(SO_BROADCAST));
+#endif
+#if defined(SO_SNDBUF)
+ /* Send buffer size */
+ rb_define_const(rb_cSocket, "SO_SNDBUF", INTEGER2NUM(SO_SNDBUF));
+ /* Send buffer size */
+ rb_define_const(rb_mSockConst, "SO_SNDBUF", INTEGER2NUM(SO_SNDBUF));
+#endif
+#if defined(SO_RCVBUF)
+ /* Receive buffer size */
+ rb_define_const(rb_cSocket, "SO_RCVBUF", INTEGER2NUM(SO_RCVBUF));
+ /* Receive buffer size */
+ rb_define_const(rb_mSockConst, "SO_RCVBUF", INTEGER2NUM(SO_RCVBUF));
+#endif
+#if defined(SO_SNDBUFFORCE)
+ /* Send buffer size without wmem_max limit (Linux 2.6.14) */
+ rb_define_const(rb_cSocket, "SO_SNDBUFFORCE", INTEGER2NUM(SO_SNDBUFFORCE));
+ /* Send buffer size without wmem_max limit (Linux 2.6.14) */
+ rb_define_const(rb_mSockConst, "SO_SNDBUFFORCE", INTEGER2NUM(SO_SNDBUFFORCE));
+#endif
+#if defined(SO_RCVBUFFORCE)
+ /* Receive buffer size without rmem_max limit (Linux 2.6.14) */
+ rb_define_const(rb_cSocket, "SO_RCVBUFFORCE", INTEGER2NUM(SO_RCVBUFFORCE));
+ /* Receive buffer size without rmem_max limit (Linux 2.6.14) */
+ rb_define_const(rb_mSockConst, "SO_RCVBUFFORCE", INTEGER2NUM(SO_RCVBUFFORCE));
+#endif
+#if defined(SO_KEEPALIVE)
+ /* Keep connections alive */
+ rb_define_const(rb_cSocket, "SO_KEEPALIVE", INTEGER2NUM(SO_KEEPALIVE));
+ /* Keep connections alive */
+ rb_define_const(rb_mSockConst, "SO_KEEPALIVE", INTEGER2NUM(SO_KEEPALIVE));
+#endif
+#if defined(SO_OOBINLINE)
+ /* Leave received out-of-band data in-line */
+ rb_define_const(rb_cSocket, "SO_OOBINLINE", INTEGER2NUM(SO_OOBINLINE));
+ /* Leave received out-of-band data in-line */
+ rb_define_const(rb_mSockConst, "SO_OOBINLINE", INTEGER2NUM(SO_OOBINLINE));
+#endif
+#if defined(SO_NO_CHECK)
+ /* Disable checksums */
+ rb_define_const(rb_cSocket, "SO_NO_CHECK", INTEGER2NUM(SO_NO_CHECK));
+ /* Disable checksums */
+ rb_define_const(rb_mSockConst, "SO_NO_CHECK", INTEGER2NUM(SO_NO_CHECK));
+#endif
+#if defined(SO_PRIORITY)
+ /* The protocol-defined priority for all packets on this socket */
+ rb_define_const(rb_cSocket, "SO_PRIORITY", INTEGER2NUM(SO_PRIORITY));
+ /* The protocol-defined priority for all packets on this socket */
+ rb_define_const(rb_mSockConst, "SO_PRIORITY", INTEGER2NUM(SO_PRIORITY));
+#endif
+#if defined(SO_LINGER)
+ /* Linger on close if data is present */
+ rb_define_const(rb_cSocket, "SO_LINGER", INTEGER2NUM(SO_LINGER));
+ /* Linger on close if data is present */
+ rb_define_const(rb_mSockConst, "SO_LINGER", INTEGER2NUM(SO_LINGER));
+#endif
+#if defined(SO_PASSCRED)
+ /* Receive SCM_CREDENTIALS messages */
+ rb_define_const(rb_cSocket, "SO_PASSCRED", INTEGER2NUM(SO_PASSCRED));
+ /* Receive SCM_CREDENTIALS messages */
+ rb_define_const(rb_mSockConst, "SO_PASSCRED", INTEGER2NUM(SO_PASSCRED));
+#endif
+#if defined(SO_PEERCRED)
+ /* The credentials of the foreign process connected to this socket */
+ rb_define_const(rb_cSocket, "SO_PEERCRED", INTEGER2NUM(SO_PEERCRED));
+ /* The credentials of the foreign process connected to this socket */
+ rb_define_const(rb_mSockConst, "SO_PEERCRED", INTEGER2NUM(SO_PEERCRED));
+#endif
+#if defined(SO_RCVLOWAT)
+ /* Receive low-water mark */
+ rb_define_const(rb_cSocket, "SO_RCVLOWAT", INTEGER2NUM(SO_RCVLOWAT));
+ /* Receive low-water mark */
+ rb_define_const(rb_mSockConst, "SO_RCVLOWAT", INTEGER2NUM(SO_RCVLOWAT));
+#endif
+#if defined(SO_SNDLOWAT)
+ /* Send low-water mark */
+ rb_define_const(rb_cSocket, "SO_SNDLOWAT", INTEGER2NUM(SO_SNDLOWAT));
+ /* Send low-water mark */
+ rb_define_const(rb_mSockConst, "SO_SNDLOWAT", INTEGER2NUM(SO_SNDLOWAT));
+#endif
+#if defined(SO_RCVTIMEO)
+ /* Receive timeout */
+ rb_define_const(rb_cSocket, "SO_RCVTIMEO", INTEGER2NUM(SO_RCVTIMEO));
+ /* Receive timeout */
+ rb_define_const(rb_mSockConst, "SO_RCVTIMEO", INTEGER2NUM(SO_RCVTIMEO));
+#endif
+#if defined(SO_SNDTIMEO)
+ /* Send timeout */
+ rb_define_const(rb_cSocket, "SO_SNDTIMEO", INTEGER2NUM(SO_SNDTIMEO));
+ /* Send timeout */
+ rb_define_const(rb_mSockConst, "SO_SNDTIMEO", INTEGER2NUM(SO_SNDTIMEO));
+#endif
+#if defined(SO_ACCEPTCONN)
+ /* Socket has had listen() called on it */
+ rb_define_const(rb_cSocket, "SO_ACCEPTCONN", INTEGER2NUM(SO_ACCEPTCONN));
+ /* Socket has had listen() called on it */
+ rb_define_const(rb_mSockConst, "SO_ACCEPTCONN", INTEGER2NUM(SO_ACCEPTCONN));
+#endif
+#if defined(SO_USELOOPBACK)
+ /* Bypass hardware when possible */
+ rb_define_const(rb_cSocket, "SO_USELOOPBACK", INTEGER2NUM(SO_USELOOPBACK));
+ /* Bypass hardware when possible */
+ rb_define_const(rb_mSockConst, "SO_USELOOPBACK", INTEGER2NUM(SO_USELOOPBACK));
+#endif
+#if defined(SO_ACCEPTFILTER)
+ /* There is an accept filter */
+ rb_define_const(rb_cSocket, "SO_ACCEPTFILTER", INTEGER2NUM(SO_ACCEPTFILTER));
+ /* There is an accept filter */
+ rb_define_const(rb_mSockConst, "SO_ACCEPTFILTER", INTEGER2NUM(SO_ACCEPTFILTER));
+#endif
+#if defined(SO_DONTTRUNC)
+ /* Retain unread data */
+ rb_define_const(rb_cSocket, "SO_DONTTRUNC", INTEGER2NUM(SO_DONTTRUNC));
+ /* Retain unread data */
+ rb_define_const(rb_mSockConst, "SO_DONTTRUNC", INTEGER2NUM(SO_DONTTRUNC));
+#endif
+#if defined(SO_WANTMORE)
+ /* Give a hint when more data is ready */
+ rb_define_const(rb_cSocket, "SO_WANTMORE", INTEGER2NUM(SO_WANTMORE));
+ /* Give a hint when more data is ready */
+ rb_define_const(rb_mSockConst, "SO_WANTMORE", INTEGER2NUM(SO_WANTMORE));
+#endif
+#if defined(SO_WANTOOBFLAG)
+ /* OOB data is wanted in MSG_FLAG on receive */
+ rb_define_const(rb_cSocket, "SO_WANTOOBFLAG", INTEGER2NUM(SO_WANTOOBFLAG));
+ /* OOB data is wanted in MSG_FLAG on receive */
+ rb_define_const(rb_mSockConst, "SO_WANTOOBFLAG", INTEGER2NUM(SO_WANTOOBFLAG));
+#endif
+#if defined(SO_NREAD)
+ /* Get first packet byte count */
+ rb_define_const(rb_cSocket, "SO_NREAD", INTEGER2NUM(SO_NREAD));
+ /* Get first packet byte count */
+ rb_define_const(rb_mSockConst, "SO_NREAD", INTEGER2NUM(SO_NREAD));
+#endif
+#if defined(SO_NKE)
+ /* Install socket-level Network Kernel Extension */
+ rb_define_const(rb_cSocket, "SO_NKE", INTEGER2NUM(SO_NKE));
+ /* Install socket-level Network Kernel Extension */
+ rb_define_const(rb_mSockConst, "SO_NKE", INTEGER2NUM(SO_NKE));
+#endif
+#if defined(SO_NOSIGPIPE)
+ /* Don't SIGPIPE on EPIPE */
+ rb_define_const(rb_cSocket, "SO_NOSIGPIPE", INTEGER2NUM(SO_NOSIGPIPE));
+ /* Don't SIGPIPE on EPIPE */
+ rb_define_const(rb_mSockConst, "SO_NOSIGPIPE", INTEGER2NUM(SO_NOSIGPIPE));
+#endif
+#if defined(SO_SECURITY_AUTHENTICATION)
+ /* */
+ rb_define_const(rb_cSocket, "SO_SECURITY_AUTHENTICATION", INTEGER2NUM(SO_SECURITY_AUTHENTICATION));
+ /* */
+ rb_define_const(rb_mSockConst, "SO_SECURITY_AUTHENTICATION", INTEGER2NUM(SO_SECURITY_AUTHENTICATION));
+#endif
+#if defined(SO_SECURITY_ENCRYPTION_TRANSPORT)
+ /* */
+ rb_define_const(rb_cSocket, "SO_SECURITY_ENCRYPTION_TRANSPORT", INTEGER2NUM(SO_SECURITY_ENCRYPTION_TRANSPORT));
+ /* */
+ rb_define_const(rb_mSockConst, "SO_SECURITY_ENCRYPTION_TRANSPORT", INTEGER2NUM(SO_SECURITY_ENCRYPTION_TRANSPORT));
+#endif
+#if defined(SO_SECURITY_ENCRYPTION_NETWORK)
+ /* */
+ rb_define_const(rb_cSocket, "SO_SECURITY_ENCRYPTION_NETWORK", INTEGER2NUM(SO_SECURITY_ENCRYPTION_NETWORK));
+ /* */
+ rb_define_const(rb_mSockConst, "SO_SECURITY_ENCRYPTION_NETWORK", INTEGER2NUM(SO_SECURITY_ENCRYPTION_NETWORK));
+#endif
+#if defined(SO_BINDTODEVICE)
+ /* Only send packets from the given interface */
+ rb_define_const(rb_cSocket, "SO_BINDTODEVICE", INTEGER2NUM(SO_BINDTODEVICE));
+ /* Only send packets from the given interface */
+ rb_define_const(rb_mSockConst, "SO_BINDTODEVICE", INTEGER2NUM(SO_BINDTODEVICE));
+#endif
+#if defined(SO_ATTACH_FILTER)
+ /* Attach an accept filter */
+ rb_define_const(rb_cSocket, "SO_ATTACH_FILTER", INTEGER2NUM(SO_ATTACH_FILTER));
+ /* Attach an accept filter */
+ rb_define_const(rb_mSockConst, "SO_ATTACH_FILTER", INTEGER2NUM(SO_ATTACH_FILTER));
+#endif
+#if defined(SO_DETACH_FILTER)
+ /* Detach an accept filter */
+ rb_define_const(rb_cSocket, "SO_DETACH_FILTER", INTEGER2NUM(SO_DETACH_FILTER));
+ /* Detach an accept filter */
+ rb_define_const(rb_mSockConst, "SO_DETACH_FILTER", INTEGER2NUM(SO_DETACH_FILTER));
+#endif
+#if defined(SO_GET_FILTER)
+ /* Obtain filter set by SO_ATTACH_FILTER (Linux 3.8) */
+ rb_define_const(rb_cSocket, "SO_GET_FILTER", INTEGER2NUM(SO_GET_FILTER));
+ /* Obtain filter set by SO_ATTACH_FILTER (Linux 3.8) */
+ rb_define_const(rb_mSockConst, "SO_GET_FILTER", INTEGER2NUM(SO_GET_FILTER));
+#endif
+#if defined(SO_PEERNAME)
+ /* Name of the connecting user */
+ rb_define_const(rb_cSocket, "SO_PEERNAME", INTEGER2NUM(SO_PEERNAME));
+ /* Name of the connecting user */
+ rb_define_const(rb_mSockConst, "SO_PEERNAME", INTEGER2NUM(SO_PEERNAME));
+#endif
+#if defined(SO_TIMESTAMP)
+ /* Receive timestamp with datagrams (timeval) */
+ rb_define_const(rb_cSocket, "SO_TIMESTAMP", INTEGER2NUM(SO_TIMESTAMP));
+ /* Receive timestamp with datagrams (timeval) */
+ rb_define_const(rb_mSockConst, "SO_TIMESTAMP", INTEGER2NUM(SO_TIMESTAMP));
+#endif
+#if defined(SO_TIMESTAMPNS)
+ /* Receive nanosecond timestamp with datagrams (timespec) */
+ rb_define_const(rb_cSocket, "SO_TIMESTAMPNS", INTEGER2NUM(SO_TIMESTAMPNS));
+ /* Receive nanosecond timestamp with datagrams (timespec) */
+ rb_define_const(rb_mSockConst, "SO_TIMESTAMPNS", INTEGER2NUM(SO_TIMESTAMPNS));
+#endif
+#if defined(SO_BINTIME)
+ /* Receive timestamp with datagrams (bintime) */
+ rb_define_const(rb_cSocket, "SO_BINTIME", INTEGER2NUM(SO_BINTIME));
+ /* Receive timestamp with datagrams (bintime) */
+ rb_define_const(rb_mSockConst, "SO_BINTIME", INTEGER2NUM(SO_BINTIME));
+#endif
+#if defined(SO_RECVUCRED)
+ /* Receive user credentials with datagram */
+ rb_define_const(rb_cSocket, "SO_RECVUCRED", INTEGER2NUM(SO_RECVUCRED));
+ /* Receive user credentials with datagram */
+ rb_define_const(rb_mSockConst, "SO_RECVUCRED", INTEGER2NUM(SO_RECVUCRED));
+#endif
+#if defined(SO_MAC_EXEMPT)
+ /* Mandatory Access Control exemption for unlabeled peers */
+ rb_define_const(rb_cSocket, "SO_MAC_EXEMPT", INTEGER2NUM(SO_MAC_EXEMPT));
+ /* Mandatory Access Control exemption for unlabeled peers */
+ rb_define_const(rb_mSockConst, "SO_MAC_EXEMPT", INTEGER2NUM(SO_MAC_EXEMPT));
+#endif
+#if defined(SO_ALLZONES)
+ /* Bypass zone boundaries */
+ rb_define_const(rb_cSocket, "SO_ALLZONES", INTEGER2NUM(SO_ALLZONES));
+ /* Bypass zone boundaries */
+ rb_define_const(rb_mSockConst, "SO_ALLZONES", INTEGER2NUM(SO_ALLZONES));
+#endif
+#if defined(SO_PEERSEC)
+ /* Obtain the security credentials (Linux 2.6.2) */
+ rb_define_const(rb_cSocket, "SO_PEERSEC", INTEGER2NUM(SO_PEERSEC));
+ /* Obtain the security credentials (Linux 2.6.2) */
+ rb_define_const(rb_mSockConst, "SO_PEERSEC", INTEGER2NUM(SO_PEERSEC));
+#endif
+#if defined(SO_PASSSEC)
+ /* Toggle security context passing (Linux 2.6.18) */
+ rb_define_const(rb_cSocket, "SO_PASSSEC", INTEGER2NUM(SO_PASSSEC));
+ /* Toggle security context passing (Linux 2.6.18) */
+ rb_define_const(rb_mSockConst, "SO_PASSSEC", INTEGER2NUM(SO_PASSSEC));
+#endif
+#if defined(SO_MARK)
+ /* Set the mark for mark-based routing (Linux 2.6.25) */
+ rb_define_const(rb_cSocket, "SO_MARK", INTEGER2NUM(SO_MARK));
+ /* Set the mark for mark-based routing (Linux 2.6.25) */
+ rb_define_const(rb_mSockConst, "SO_MARK", INTEGER2NUM(SO_MARK));
+#endif
+#if defined(SO_TIMESTAMPING)
+ /* Time stamping of incoming and outgoing packets (Linux 2.6.30) */
+ rb_define_const(rb_cSocket, "SO_TIMESTAMPING", INTEGER2NUM(SO_TIMESTAMPING));
+ /* Time stamping of incoming and outgoing packets (Linux 2.6.30) */
+ rb_define_const(rb_mSockConst, "SO_TIMESTAMPING", INTEGER2NUM(SO_TIMESTAMPING));
+#endif
+#if defined(SO_PROTOCOL)
+ /* Protocol given for socket() (Linux 2.6.32) */
+ rb_define_const(rb_cSocket, "SO_PROTOCOL", INTEGER2NUM(SO_PROTOCOL));
+ /* Protocol given for socket() (Linux 2.6.32) */
+ rb_define_const(rb_mSockConst, "SO_PROTOCOL", INTEGER2NUM(SO_PROTOCOL));
+#endif
+#if defined(SO_DOMAIN)
+ /* Domain given for socket() (Linux 2.6.32) */
+ rb_define_const(rb_cSocket, "SO_DOMAIN", INTEGER2NUM(SO_DOMAIN));
+ /* Domain given for socket() (Linux 2.6.32) */
+ rb_define_const(rb_mSockConst, "SO_DOMAIN", INTEGER2NUM(SO_DOMAIN));
+#endif
+#if defined(SO_RXQ_OVFL)
+ /* Toggle cmsg for number of packets dropped (Linux 2.6.33) */
+ rb_define_const(rb_cSocket, "SO_RXQ_OVFL", INTEGER2NUM(SO_RXQ_OVFL));
+ /* Toggle cmsg for number of packets dropped (Linux 2.6.33) */
+ rb_define_const(rb_mSockConst, "SO_RXQ_OVFL", INTEGER2NUM(SO_RXQ_OVFL));
+#endif
+#if defined(SO_WIFI_STATUS)
+ /* Toggle cmsg for wifi status (Linux 3.3) */
+ rb_define_const(rb_cSocket, "SO_WIFI_STATUS", INTEGER2NUM(SO_WIFI_STATUS));
+ /* Toggle cmsg for wifi status (Linux 3.3) */
+ rb_define_const(rb_mSockConst, "SO_WIFI_STATUS", INTEGER2NUM(SO_WIFI_STATUS));
+#endif
+#if defined(SO_PEEK_OFF)
+ /* Set the peek offset (Linux 3.4) */
+ rb_define_const(rb_cSocket, "SO_PEEK_OFF", INTEGER2NUM(SO_PEEK_OFF));
+ /* Set the peek offset (Linux 3.4) */
+ rb_define_const(rb_mSockConst, "SO_PEEK_OFF", INTEGER2NUM(SO_PEEK_OFF));
+#endif
+#if defined(SO_NOFCS)
+ /* Set netns of a socket (Linux 3.4) */
+ rb_define_const(rb_cSocket, "SO_NOFCS", INTEGER2NUM(SO_NOFCS));
+ /* Set netns of a socket (Linux 3.4) */
+ rb_define_const(rb_mSockConst, "SO_NOFCS", INTEGER2NUM(SO_NOFCS));
+#endif
+#if defined(SO_LOCK_FILTER)
+ /* Lock the filter attached to a socket (Linux 3.9) */
+ rb_define_const(rb_cSocket, "SO_LOCK_FILTER", INTEGER2NUM(SO_LOCK_FILTER));
+ /* Lock the filter attached to a socket (Linux 3.9) */
+ rb_define_const(rb_mSockConst, "SO_LOCK_FILTER", INTEGER2NUM(SO_LOCK_FILTER));
+#endif
+#if defined(SO_SELECT_ERR_QUEUE)
+ /* Make select() detect socket error queue with errorfds (Linux 3.10) */
+ rb_define_const(rb_cSocket, "SO_SELECT_ERR_QUEUE", INTEGER2NUM(SO_SELECT_ERR_QUEUE));
+ /* Make select() detect socket error queue with errorfds (Linux 3.10) */
+ rb_define_const(rb_mSockConst, "SO_SELECT_ERR_QUEUE", INTEGER2NUM(SO_SELECT_ERR_QUEUE));
+#endif
+#if defined(SO_BUSY_POLL)
+ /* Set the threshold in microseconds for low latency polling (Linux 3.11) */
+ rb_define_const(rb_cSocket, "SO_BUSY_POLL", INTEGER2NUM(SO_BUSY_POLL));
+ /* Set the threshold in microseconds for low latency polling (Linux 3.11) */
+ rb_define_const(rb_mSockConst, "SO_BUSY_POLL", INTEGER2NUM(SO_BUSY_POLL));
+#endif
+#if defined(SO_MAX_PACING_RATE)
+ /* Cap the rate computed by transport layer. [bytes per second] (Linux 3.13) */
+ rb_define_const(rb_cSocket, "SO_MAX_PACING_RATE", INTEGER2NUM(SO_MAX_PACING_RATE));
+ /* Cap the rate computed by transport layer. [bytes per second] (Linux 3.13) */
+ rb_define_const(rb_mSockConst, "SO_MAX_PACING_RATE", INTEGER2NUM(SO_MAX_PACING_RATE));
+#endif
+#if defined(SO_BPF_EXTENSIONS)
+ /* Query supported BPF extensions (Linux 3.14) */
+ rb_define_const(rb_cSocket, "SO_BPF_EXTENSIONS", INTEGER2NUM(SO_BPF_EXTENSIONS));
+ /* Query supported BPF extensions (Linux 3.14) */
+ rb_define_const(rb_mSockConst, "SO_BPF_EXTENSIONS", INTEGER2NUM(SO_BPF_EXTENSIONS));
+#endif
+#if defined(SOPRI_INTERACTIVE)
+ /* Interactive socket priority */
+ rb_define_const(rb_cSocket, "SOPRI_INTERACTIVE", INTEGER2NUM(SOPRI_INTERACTIVE));
+ /* Interactive socket priority */
+ rb_define_const(rb_mSockConst, "SOPRI_INTERACTIVE", INTEGER2NUM(SOPRI_INTERACTIVE));
+#endif
+#if defined(SOPRI_NORMAL)
+ /* Normal socket priority */
+ rb_define_const(rb_cSocket, "SOPRI_NORMAL", INTEGER2NUM(SOPRI_NORMAL));
+ /* Normal socket priority */
+ rb_define_const(rb_mSockConst, "SOPRI_NORMAL", INTEGER2NUM(SOPRI_NORMAL));
+#endif
+#if defined(SOPRI_BACKGROUND)
+ /* Background socket priority */
+ rb_define_const(rb_cSocket, "SOPRI_BACKGROUND", INTEGER2NUM(SOPRI_BACKGROUND));
+ /* Background socket priority */
+ rb_define_const(rb_mSockConst, "SOPRI_BACKGROUND", INTEGER2NUM(SOPRI_BACKGROUND));
+#endif
+#if defined(IPX_TYPE)
+ /* */
+ rb_define_const(rb_cSocket, "IPX_TYPE", INTEGER2NUM(IPX_TYPE));
+ /* */
+ rb_define_const(rb_mSockConst, "IPX_TYPE", INTEGER2NUM(IPX_TYPE));
+#endif
+#if defined(TCP_NODELAY)
+ /* Don't delay sending to coalesce packets */
+ rb_define_const(rb_cSocket, "TCP_NODELAY", INTEGER2NUM(TCP_NODELAY));
+ /* Don't delay sending to coalesce packets */
+ rb_define_const(rb_mSockConst, "TCP_NODELAY", INTEGER2NUM(TCP_NODELAY));
+#endif
+#if defined(TCP_MAXSEG)
+ /* Set maximum segment size */
+ rb_define_const(rb_cSocket, "TCP_MAXSEG", INTEGER2NUM(TCP_MAXSEG));
+ /* Set maximum segment size */
+ rb_define_const(rb_mSockConst, "TCP_MAXSEG", INTEGER2NUM(TCP_MAXSEG));
+#endif
+#if defined(TCP_CORK)
+ /* Don't send partial frames (Linux 2.2, glibc 2.2) */
+ rb_define_const(rb_cSocket, "TCP_CORK", INTEGER2NUM(TCP_CORK));
+ /* Don't send partial frames (Linux 2.2, glibc 2.2) */
+ rb_define_const(rb_mSockConst, "TCP_CORK", INTEGER2NUM(TCP_CORK));
+#endif
+#if defined(TCP_DEFER_ACCEPT)
+ /* Don't notify a listening socket until data is ready (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_cSocket, "TCP_DEFER_ACCEPT", INTEGER2NUM(TCP_DEFER_ACCEPT));
+ /* Don't notify a listening socket until data is ready (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_mSockConst, "TCP_DEFER_ACCEPT", INTEGER2NUM(TCP_DEFER_ACCEPT));
+#endif
+#if defined(TCP_INFO)
+ /* Retrieve information about this socket (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_cSocket, "TCP_INFO", INTEGER2NUM(TCP_INFO));
+ /* Retrieve information about this socket (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_mSockConst, "TCP_INFO", INTEGER2NUM(TCP_INFO));
+#endif
+#if defined(TCP_KEEPCNT)
+ /* Maximum number of keepalive probes allowed before dropping a connection (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_cSocket, "TCP_KEEPCNT", INTEGER2NUM(TCP_KEEPCNT));
+ /* Maximum number of keepalive probes allowed before dropping a connection (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_mSockConst, "TCP_KEEPCNT", INTEGER2NUM(TCP_KEEPCNT));
+#endif
+#if defined(TCP_KEEPIDLE)
+ /* Idle time before keepalive probes are sent (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_cSocket, "TCP_KEEPIDLE", INTEGER2NUM(TCP_KEEPIDLE));
+ /* Idle time before keepalive probes are sent (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_mSockConst, "TCP_KEEPIDLE", INTEGER2NUM(TCP_KEEPIDLE));
+#endif
+#if defined(TCP_KEEPINTVL)
+ /* Time between keepalive probes (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_cSocket, "TCP_KEEPINTVL", INTEGER2NUM(TCP_KEEPINTVL));
+ /* Time between keepalive probes (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_mSockConst, "TCP_KEEPINTVL", INTEGER2NUM(TCP_KEEPINTVL));
+#endif
+#if defined(TCP_LINGER2)
+ /* Lifetime of orphaned FIN_WAIT2 sockets (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_cSocket, "TCP_LINGER2", INTEGER2NUM(TCP_LINGER2));
+ /* Lifetime of orphaned FIN_WAIT2 sockets (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_mSockConst, "TCP_LINGER2", INTEGER2NUM(TCP_LINGER2));
+#endif
+#if defined(TCP_MD5SIG)
+ /* Use MD5 digests (RFC2385, Linux 2.6.20, glibc 2.7) */
+ rb_define_const(rb_cSocket, "TCP_MD5SIG", INTEGER2NUM(TCP_MD5SIG));
+ /* Use MD5 digests (RFC2385, Linux 2.6.20, glibc 2.7) */
+ rb_define_const(rb_mSockConst, "TCP_MD5SIG", INTEGER2NUM(TCP_MD5SIG));
+#endif
+#if defined(TCP_NOOPT)
+ /* Don't use TCP options */
+ rb_define_const(rb_cSocket, "TCP_NOOPT", INTEGER2NUM(TCP_NOOPT));
+ /* Don't use TCP options */
+ rb_define_const(rb_mSockConst, "TCP_NOOPT", INTEGER2NUM(TCP_NOOPT));
+#endif
+#if defined(TCP_NOPUSH)
+ /* Don't push the last block of write */
+ rb_define_const(rb_cSocket, "TCP_NOPUSH", INTEGER2NUM(TCP_NOPUSH));
+ /* Don't push the last block of write */
+ rb_define_const(rb_mSockConst, "TCP_NOPUSH", INTEGER2NUM(TCP_NOPUSH));
+#endif
+#if defined(TCP_QUICKACK)
+ /* Enable quickack mode (Linux 2.4.4, glibc 2.3) */
+ rb_define_const(rb_cSocket, "TCP_QUICKACK", INTEGER2NUM(TCP_QUICKACK));
+ /* Enable quickack mode (Linux 2.4.4, glibc 2.3) */
+ rb_define_const(rb_mSockConst, "TCP_QUICKACK", INTEGER2NUM(TCP_QUICKACK));
+#endif
+#if defined(TCP_SYNCNT)
+ /* Number of SYN retransmits before a connection is dropped (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_cSocket, "TCP_SYNCNT", INTEGER2NUM(TCP_SYNCNT));
+ /* Number of SYN retransmits before a connection is dropped (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_mSockConst, "TCP_SYNCNT", INTEGER2NUM(TCP_SYNCNT));
+#endif
+#if defined(TCP_WINDOW_CLAMP)
+ /* Clamp the size of the advertised window (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_cSocket, "TCP_WINDOW_CLAMP", INTEGER2NUM(TCP_WINDOW_CLAMP));
+ /* Clamp the size of the advertised window (Linux 2.4, glibc 2.2) */
+ rb_define_const(rb_mSockConst, "TCP_WINDOW_CLAMP", INTEGER2NUM(TCP_WINDOW_CLAMP));
+#endif
+#if defined(TCP_FASTOPEN)
+ /* Reduce step of the handshake process (Linux 3.7, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_FASTOPEN", INTEGER2NUM(TCP_FASTOPEN));
+ /* Reduce step of the handshake process (Linux 3.7, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_FASTOPEN", INTEGER2NUM(TCP_FASTOPEN));
+#endif
+#if defined(TCP_CONGESTION)
+ /* TCP congestion control algorithm (Linux 2.6.13, glibc 2.6) */
+ rb_define_const(rb_cSocket, "TCP_CONGESTION", INTEGER2NUM(TCP_CONGESTION));
+ /* TCP congestion control algorithm (Linux 2.6.13, glibc 2.6) */
+ rb_define_const(rb_mSockConst, "TCP_CONGESTION", INTEGER2NUM(TCP_CONGESTION));
+#endif
+#if defined(TCP_COOKIE_TRANSACTIONS)
+ /* TCP Cookie Transactions (Linux 2.6.33, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_COOKIE_TRANSACTIONS", INTEGER2NUM(TCP_COOKIE_TRANSACTIONS));
+ /* TCP Cookie Transactions (Linux 2.6.33, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_COOKIE_TRANSACTIONS", INTEGER2NUM(TCP_COOKIE_TRANSACTIONS));
+#endif
+#if defined(TCP_QUEUE_SEQ)
+ /* Sequence of a queue for repair mode (Linux 3.5, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_QUEUE_SEQ", INTEGER2NUM(TCP_QUEUE_SEQ));
+ /* Sequence of a queue for repair mode (Linux 3.5, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_QUEUE_SEQ", INTEGER2NUM(TCP_QUEUE_SEQ));
+#endif
+#if defined(TCP_REPAIR)
+ /* Repair mode (Linux 3.5, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_REPAIR", INTEGER2NUM(TCP_REPAIR));
+ /* Repair mode (Linux 3.5, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_REPAIR", INTEGER2NUM(TCP_REPAIR));
+#endif
+#if defined(TCP_REPAIR_OPTIONS)
+ /* Options for repair mode (Linux 3.5, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_REPAIR_OPTIONS", INTEGER2NUM(TCP_REPAIR_OPTIONS));
+ /* Options for repair mode (Linux 3.5, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_REPAIR_OPTIONS", INTEGER2NUM(TCP_REPAIR_OPTIONS));
+#endif
+#if defined(TCP_REPAIR_QUEUE)
+ /* Queue for repair mode (Linux 3.5, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_REPAIR_QUEUE", INTEGER2NUM(TCP_REPAIR_QUEUE));
+ /* Queue for repair mode (Linux 3.5, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_REPAIR_QUEUE", INTEGER2NUM(TCP_REPAIR_QUEUE));
+#endif
+#if defined(TCP_THIN_DUPACK)
+ /* Duplicated acknowledgments handling for thin-streams (Linux 2.6.34, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_THIN_DUPACK", INTEGER2NUM(TCP_THIN_DUPACK));
+ /* Duplicated acknowledgments handling for thin-streams (Linux 2.6.34, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_THIN_DUPACK", INTEGER2NUM(TCP_THIN_DUPACK));
+#endif
+#if defined(TCP_THIN_LINEAR_TIMEOUTS)
+ /* Linear timeouts for thin-streams (Linux 2.6.34, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_THIN_LINEAR_TIMEOUTS", INTEGER2NUM(TCP_THIN_LINEAR_TIMEOUTS));
+ /* Linear timeouts for thin-streams (Linux 2.6.34, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_THIN_LINEAR_TIMEOUTS", INTEGER2NUM(TCP_THIN_LINEAR_TIMEOUTS));
+#endif
+#if defined(TCP_TIMESTAMP)
+ /* TCP timestamp (Linux 3.9, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_TIMESTAMP", INTEGER2NUM(TCP_TIMESTAMP));
+ /* TCP timestamp (Linux 3.9, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_TIMESTAMP", INTEGER2NUM(TCP_TIMESTAMP));
+#endif
+#if defined(TCP_USER_TIMEOUT)
+ /* Max timeout before a TCP connection is aborted (Linux 2.6.37, glibc 2.18) */
+ rb_define_const(rb_cSocket, "TCP_USER_TIMEOUT", INTEGER2NUM(TCP_USER_TIMEOUT));
+ /* Max timeout before a TCP connection is aborted (Linux 2.6.37, glibc 2.18) */
+ rb_define_const(rb_mSockConst, "TCP_USER_TIMEOUT", INTEGER2NUM(TCP_USER_TIMEOUT));
+#endif
+#if defined(UDP_CORK)
+ /* Don't send partial frames (Linux 2.5.44, glibc 2.11) */
+ rb_define_const(rb_cSocket, "UDP_CORK", INTEGER2NUM(UDP_CORK));
+ /* Don't send partial frames (Linux 2.5.44, glibc 2.11) */
+ rb_define_const(rb_mSockConst, "UDP_CORK", INTEGER2NUM(UDP_CORK));
+#endif
+#if defined(EAI_ADDRFAMILY)
+ /* Address family for hostname not supported */
+ rb_define_const(rb_cSocket, "EAI_ADDRFAMILY", INTEGER2NUM(EAI_ADDRFAMILY));
+ /* Address family for hostname not supported */
+ rb_define_const(rb_mSockConst, "EAI_ADDRFAMILY", INTEGER2NUM(EAI_ADDRFAMILY));
+#endif
+#if defined(EAI_AGAIN)
+ /* Temporary failure in name resolution */
+ rb_define_const(rb_cSocket, "EAI_AGAIN", INTEGER2NUM(EAI_AGAIN));
+ /* Temporary failure in name resolution */
+ rb_define_const(rb_mSockConst, "EAI_AGAIN", INTEGER2NUM(EAI_AGAIN));
+#endif
+#if defined(EAI_BADFLAGS)
+ /* Invalid flags */
+ rb_define_const(rb_cSocket, "EAI_BADFLAGS", INTEGER2NUM(EAI_BADFLAGS));
+ /* Invalid flags */
+ rb_define_const(rb_mSockConst, "EAI_BADFLAGS", INTEGER2NUM(EAI_BADFLAGS));
+#endif
+#if defined(EAI_FAIL)
+ /* Non-recoverable failure in name resolution */
+ rb_define_const(rb_cSocket, "EAI_FAIL", INTEGER2NUM(EAI_FAIL));
+ /* Non-recoverable failure in name resolution */
+ rb_define_const(rb_mSockConst, "EAI_FAIL", INTEGER2NUM(EAI_FAIL));
+#endif
+#if defined(EAI_FAMILY)
+ /* Address family not supported */
+ rb_define_const(rb_cSocket, "EAI_FAMILY", INTEGER2NUM(EAI_FAMILY));
+ /* Address family not supported */
+ rb_define_const(rb_mSockConst, "EAI_FAMILY", INTEGER2NUM(EAI_FAMILY));
+#endif
+#if defined(EAI_MEMORY)
+ /* Memory allocation failure */
+ rb_define_const(rb_cSocket, "EAI_MEMORY", INTEGER2NUM(EAI_MEMORY));
+ /* Memory allocation failure */
+ rb_define_const(rb_mSockConst, "EAI_MEMORY", INTEGER2NUM(EAI_MEMORY));
+#endif
+#if defined(EAI_NODATA)
+ /* No address associated with hostname */
+ rb_define_const(rb_cSocket, "EAI_NODATA", INTEGER2NUM(EAI_NODATA));
+ /* No address associated with hostname */
+ rb_define_const(rb_mSockConst, "EAI_NODATA", INTEGER2NUM(EAI_NODATA));
+#endif
+#if defined(EAI_NONAME)
+ /* Hostname nor servname, or not known */
+ rb_define_const(rb_cSocket, "EAI_NONAME", INTEGER2NUM(EAI_NONAME));
+ /* Hostname nor servname, or not known */
+ rb_define_const(rb_mSockConst, "EAI_NONAME", INTEGER2NUM(EAI_NONAME));
+#endif
+#if defined(EAI_OVERFLOW)
+ /* Argument buffer overflow */
+ rb_define_const(rb_cSocket, "EAI_OVERFLOW", INTEGER2NUM(EAI_OVERFLOW));
+ /* Argument buffer overflow */
+ rb_define_const(rb_mSockConst, "EAI_OVERFLOW", INTEGER2NUM(EAI_OVERFLOW));
+#endif
+#if defined(EAI_SERVICE)
+ /* Servname not supported for socket type */
+ rb_define_const(rb_cSocket, "EAI_SERVICE", INTEGER2NUM(EAI_SERVICE));
+ /* Servname not supported for socket type */
+ rb_define_const(rb_mSockConst, "EAI_SERVICE", INTEGER2NUM(EAI_SERVICE));
+#endif
+#if defined(EAI_SOCKTYPE)
+ /* Socket type not supported */
+ rb_define_const(rb_cSocket, "EAI_SOCKTYPE", INTEGER2NUM(EAI_SOCKTYPE));
+ /* Socket type not supported */
+ rb_define_const(rb_mSockConst, "EAI_SOCKTYPE", INTEGER2NUM(EAI_SOCKTYPE));
+#endif
+#if defined(EAI_SYSTEM)
+ /* System error returned in errno */
+ rb_define_const(rb_cSocket, "EAI_SYSTEM", INTEGER2NUM(EAI_SYSTEM));
+ /* System error returned in errno */
+ rb_define_const(rb_mSockConst, "EAI_SYSTEM", INTEGER2NUM(EAI_SYSTEM));
+#endif
+#if defined(EAI_BADHINTS)
+ /* Invalid value for hints */
+ rb_define_const(rb_cSocket, "EAI_BADHINTS", INTEGER2NUM(EAI_BADHINTS));
+ /* Invalid value for hints */
+ rb_define_const(rb_mSockConst, "EAI_BADHINTS", INTEGER2NUM(EAI_BADHINTS));
+#endif
+#if defined(EAI_PROTOCOL)
+ /* Resolved protocol is unknown */
+ rb_define_const(rb_cSocket, "EAI_PROTOCOL", INTEGER2NUM(EAI_PROTOCOL));
+ /* Resolved protocol is unknown */
+ rb_define_const(rb_mSockConst, "EAI_PROTOCOL", INTEGER2NUM(EAI_PROTOCOL));
+#endif
+#if defined(EAI_MAX)
+ /* Maximum error code from getaddrinfo */
+ rb_define_const(rb_cSocket, "EAI_MAX", INTEGER2NUM(EAI_MAX));
+ /* Maximum error code from getaddrinfo */
+ rb_define_const(rb_mSockConst, "EAI_MAX", INTEGER2NUM(EAI_MAX));
+#endif
+#if defined(AI_PASSIVE)
+ /* Get address to use with bind() */
+ rb_define_const(rb_cSocket, "AI_PASSIVE", INTEGER2NUM(AI_PASSIVE));
+ /* Get address to use with bind() */
+ rb_define_const(rb_mSockConst, "AI_PASSIVE", INTEGER2NUM(AI_PASSIVE));
+#endif
+#if defined(AI_CANONNAME)
+ /* Fill in the canonical name */
+ rb_define_const(rb_cSocket, "AI_CANONNAME", INTEGER2NUM(AI_CANONNAME));
+ /* Fill in the canonical name */
+ rb_define_const(rb_mSockConst, "AI_CANONNAME", INTEGER2NUM(AI_CANONNAME));
+#endif
+#if defined(AI_NUMERICHOST)
+ /* Prevent host name resolution */
+ rb_define_const(rb_cSocket, "AI_NUMERICHOST", INTEGER2NUM(AI_NUMERICHOST));
+ /* Prevent host name resolution */
+ rb_define_const(rb_mSockConst, "AI_NUMERICHOST", INTEGER2NUM(AI_NUMERICHOST));
+#endif
+#if defined(AI_NUMERICSERV)
+ /* Prevent service name resolution */
+ rb_define_const(rb_cSocket, "AI_NUMERICSERV", INTEGER2NUM(AI_NUMERICSERV));
+ /* Prevent service name resolution */
+ rb_define_const(rb_mSockConst, "AI_NUMERICSERV", INTEGER2NUM(AI_NUMERICSERV));
+#endif
+#if defined(AI_MASK)
+ /* Valid flag mask for getaddrinfo (not for application use) */
+ rb_define_const(rb_cSocket, "AI_MASK", INTEGER2NUM(AI_MASK));
+ /* Valid flag mask for getaddrinfo (not for application use) */
+ rb_define_const(rb_mSockConst, "AI_MASK", INTEGER2NUM(AI_MASK));
+#endif
+#if defined(AI_ALL)
+ /* Allow all addresses */
+ rb_define_const(rb_cSocket, "AI_ALL", INTEGER2NUM(AI_ALL));
+ /* Allow all addresses */
+ rb_define_const(rb_mSockConst, "AI_ALL", INTEGER2NUM(AI_ALL));
+#endif
+#if defined(AI_V4MAPPED_CFG)
+ /* Accept IPv4 mapped addresses if the kernel supports it */
+ rb_define_const(rb_cSocket, "AI_V4MAPPED_CFG", INTEGER2NUM(AI_V4MAPPED_CFG));
+ /* Accept IPv4 mapped addresses if the kernel supports it */
+ rb_define_const(rb_mSockConst, "AI_V4MAPPED_CFG", INTEGER2NUM(AI_V4MAPPED_CFG));
+#endif
+#if defined(AI_ADDRCONFIG)
+ /* Accept only if any address is assigned */
+ rb_define_const(rb_cSocket, "AI_ADDRCONFIG", INTEGER2NUM(AI_ADDRCONFIG));
+ /* Accept only if any address is assigned */
+ rb_define_const(rb_mSockConst, "AI_ADDRCONFIG", INTEGER2NUM(AI_ADDRCONFIG));
+#endif
+#if defined(AI_V4MAPPED)
+ /* Accept IPv4-mapped IPv6 addresses */
+ rb_define_const(rb_cSocket, "AI_V4MAPPED", INTEGER2NUM(AI_V4MAPPED));
+ /* Accept IPv4-mapped IPv6 addresses */
+ rb_define_const(rb_mSockConst, "AI_V4MAPPED", INTEGER2NUM(AI_V4MAPPED));
+#endif
+#if defined(AI_DEFAULT)
+ /* Default flags for getaddrinfo */
+ rb_define_const(rb_cSocket, "AI_DEFAULT", INTEGER2NUM(AI_DEFAULT));
+ /* Default flags for getaddrinfo */
+ rb_define_const(rb_mSockConst, "AI_DEFAULT", INTEGER2NUM(AI_DEFAULT));
+#endif
+#if defined(NI_MAXHOST)
+ /* Maximum length of a hostname */
+ rb_define_const(rb_cSocket, "NI_MAXHOST", INTEGER2NUM(NI_MAXHOST));
+ /* Maximum length of a hostname */
+ rb_define_const(rb_mSockConst, "NI_MAXHOST", INTEGER2NUM(NI_MAXHOST));
+#endif
+#if defined(NI_MAXSERV)
+ /* Maximum length of a service name */
+ rb_define_const(rb_cSocket, "NI_MAXSERV", INTEGER2NUM(NI_MAXSERV));
+ /* Maximum length of a service name */
+ rb_define_const(rb_mSockConst, "NI_MAXSERV", INTEGER2NUM(NI_MAXSERV));
+#endif
+#if defined(NI_NOFQDN)
+ /* An FQDN is not required for local hosts, return only the local part */
+ rb_define_const(rb_cSocket, "NI_NOFQDN", INTEGER2NUM(NI_NOFQDN));
+ /* An FQDN is not required for local hosts, return only the local part */
+ rb_define_const(rb_mSockConst, "NI_NOFQDN", INTEGER2NUM(NI_NOFQDN));
+#endif
+#if defined(NI_NUMERICHOST)
+ /* Return a numeric address */
+ rb_define_const(rb_cSocket, "NI_NUMERICHOST", INTEGER2NUM(NI_NUMERICHOST));
+ /* Return a numeric address */
+ rb_define_const(rb_mSockConst, "NI_NUMERICHOST", INTEGER2NUM(NI_NUMERICHOST));
+#endif
+#if defined(NI_NAMEREQD)
+ /* A name is required */
+ rb_define_const(rb_cSocket, "NI_NAMEREQD", INTEGER2NUM(NI_NAMEREQD));
+ /* A name is required */
+ rb_define_const(rb_mSockConst, "NI_NAMEREQD", INTEGER2NUM(NI_NAMEREQD));
+#endif
+#if defined(NI_NUMERICSERV)
+ /* Return the service name as a digit string */
+ rb_define_const(rb_cSocket, "NI_NUMERICSERV", INTEGER2NUM(NI_NUMERICSERV));
+ /* Return the service name as a digit string */
+ rb_define_const(rb_mSockConst, "NI_NUMERICSERV", INTEGER2NUM(NI_NUMERICSERV));
+#endif
+#if defined(NI_DGRAM)
+ /* The service specified is a datagram service (looks up UDP ports) */
+ rb_define_const(rb_cSocket, "NI_DGRAM", INTEGER2NUM(NI_DGRAM));
+ /* The service specified is a datagram service (looks up UDP ports) */
+ rb_define_const(rb_mSockConst, "NI_DGRAM", INTEGER2NUM(NI_DGRAM));
+#endif
+#if defined(SHUT_RD)
+ /* Shut down the reading side of the socket */
+ rb_define_const(rb_cSocket, "SHUT_RD", INTEGER2NUM(SHUT_RD));
+ /* Shut down the reading side of the socket */
+ rb_define_const(rb_mSockConst, "SHUT_RD", INTEGER2NUM(SHUT_RD));
+#endif
+#if defined(SHUT_WR)
+ /* Shut down the writing side of the socket */
+ rb_define_const(rb_cSocket, "SHUT_WR", INTEGER2NUM(SHUT_WR));
+ /* Shut down the writing side of the socket */
+ rb_define_const(rb_mSockConst, "SHUT_WR", INTEGER2NUM(SHUT_WR));
+#endif
+#if defined(SHUT_RDWR)
+ /* Shut down the both sides of the socket */
+ rb_define_const(rb_cSocket, "SHUT_RDWR", INTEGER2NUM(SHUT_RDWR));
+ /* Shut down the both sides of the socket */
+ rb_define_const(rb_mSockConst, "SHUT_RDWR", INTEGER2NUM(SHUT_RDWR));
+#endif
+#if defined(INET6)
+#if defined(IPV6_JOIN_GROUP)
+ /* Join a group membership */
+ rb_define_const(rb_cSocket, "IPV6_JOIN_GROUP", INTEGER2NUM(IPV6_JOIN_GROUP));
+ /* Join a group membership */
+ rb_define_const(rb_mSockConst, "IPV6_JOIN_GROUP", INTEGER2NUM(IPV6_JOIN_GROUP));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_LEAVE_GROUP)
+ /* Leave a group membership */
+ rb_define_const(rb_cSocket, "IPV6_LEAVE_GROUP", INTEGER2NUM(IPV6_LEAVE_GROUP));
+ /* Leave a group membership */
+ rb_define_const(rb_mSockConst, "IPV6_LEAVE_GROUP", INTEGER2NUM(IPV6_LEAVE_GROUP));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_MULTICAST_HOPS)
+ /* IP6 multicast hops */
+ rb_define_const(rb_cSocket, "IPV6_MULTICAST_HOPS", INTEGER2NUM(IPV6_MULTICAST_HOPS));
+ /* IP6 multicast hops */
+ rb_define_const(rb_mSockConst, "IPV6_MULTICAST_HOPS", INTEGER2NUM(IPV6_MULTICAST_HOPS));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_MULTICAST_IF)
+ /* IP6 multicast interface */
+ rb_define_const(rb_cSocket, "IPV6_MULTICAST_IF", INTEGER2NUM(IPV6_MULTICAST_IF));
+ /* IP6 multicast interface */
+ rb_define_const(rb_mSockConst, "IPV6_MULTICAST_IF", INTEGER2NUM(IPV6_MULTICAST_IF));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_MULTICAST_LOOP)
+ /* IP6 multicast loopback */
+ rb_define_const(rb_cSocket, "IPV6_MULTICAST_LOOP", INTEGER2NUM(IPV6_MULTICAST_LOOP));
+ /* IP6 multicast loopback */
+ rb_define_const(rb_mSockConst, "IPV6_MULTICAST_LOOP", INTEGER2NUM(IPV6_MULTICAST_LOOP));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_UNICAST_HOPS)
+ /* IP6 unicast hops */
+ rb_define_const(rb_cSocket, "IPV6_UNICAST_HOPS", INTEGER2NUM(IPV6_UNICAST_HOPS));
+ /* IP6 unicast hops */
+ rb_define_const(rb_mSockConst, "IPV6_UNICAST_HOPS", INTEGER2NUM(IPV6_UNICAST_HOPS));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_V6ONLY)
+ /* Only bind IPv6 with a wildcard bind */
+ rb_define_const(rb_cSocket, "IPV6_V6ONLY", INTEGER2NUM(IPV6_V6ONLY));
+ /* Only bind IPv6 with a wildcard bind */
+ rb_define_const(rb_mSockConst, "IPV6_V6ONLY", INTEGER2NUM(IPV6_V6ONLY));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_CHECKSUM)
+ /* Checksum offset for raw sockets */
+ rb_define_const(rb_cSocket, "IPV6_CHECKSUM", INTEGER2NUM(IPV6_CHECKSUM));
+ /* Checksum offset for raw sockets */
+ rb_define_const(rb_mSockConst, "IPV6_CHECKSUM", INTEGER2NUM(IPV6_CHECKSUM));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_DONTFRAG)
+ /* Don't fragment packets */
+ rb_define_const(rb_cSocket, "IPV6_DONTFRAG", INTEGER2NUM(IPV6_DONTFRAG));
+ /* Don't fragment packets */
+ rb_define_const(rb_mSockConst, "IPV6_DONTFRAG", INTEGER2NUM(IPV6_DONTFRAG));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_DSTOPTS)
+ /* Destination option */
+ rb_define_const(rb_cSocket, "IPV6_DSTOPTS", INTEGER2NUM(IPV6_DSTOPTS));
+ /* Destination option */
+ rb_define_const(rb_mSockConst, "IPV6_DSTOPTS", INTEGER2NUM(IPV6_DSTOPTS));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_HOPLIMIT)
+ /* Hop limit */
+ rb_define_const(rb_cSocket, "IPV6_HOPLIMIT", INTEGER2NUM(IPV6_HOPLIMIT));
+ /* Hop limit */
+ rb_define_const(rb_mSockConst, "IPV6_HOPLIMIT", INTEGER2NUM(IPV6_HOPLIMIT));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_HOPOPTS)
+ /* Hop-by-hop option */
+ rb_define_const(rb_cSocket, "IPV6_HOPOPTS", INTEGER2NUM(IPV6_HOPOPTS));
+ /* Hop-by-hop option */
+ rb_define_const(rb_mSockConst, "IPV6_HOPOPTS", INTEGER2NUM(IPV6_HOPOPTS));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_NEXTHOP)
+ /* Next hop address */
+ rb_define_const(rb_cSocket, "IPV6_NEXTHOP", INTEGER2NUM(IPV6_NEXTHOP));
+ /* Next hop address */
+ rb_define_const(rb_mSockConst, "IPV6_NEXTHOP", INTEGER2NUM(IPV6_NEXTHOP));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_PATHMTU)
+ /* Retrieve current path MTU */
+ rb_define_const(rb_cSocket, "IPV6_PATHMTU", INTEGER2NUM(IPV6_PATHMTU));
+ /* Retrieve current path MTU */
+ rb_define_const(rb_mSockConst, "IPV6_PATHMTU", INTEGER2NUM(IPV6_PATHMTU));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_PKTINFO)
+ /* Receive packet information with datagram */
+ rb_define_const(rb_cSocket, "IPV6_PKTINFO", INTEGER2NUM(IPV6_PKTINFO));
+ /* Receive packet information with datagram */
+ rb_define_const(rb_mSockConst, "IPV6_PKTINFO", INTEGER2NUM(IPV6_PKTINFO));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RECVDSTOPTS)
+ /* Receive all IP6 options for response */
+ rb_define_const(rb_cSocket, "IPV6_RECVDSTOPTS", INTEGER2NUM(IPV6_RECVDSTOPTS));
+ /* Receive all IP6 options for response */
+ rb_define_const(rb_mSockConst, "IPV6_RECVDSTOPTS", INTEGER2NUM(IPV6_RECVDSTOPTS));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RECVHOPLIMIT)
+ /* Receive hop limit with datagram */
+ rb_define_const(rb_cSocket, "IPV6_RECVHOPLIMIT", INTEGER2NUM(IPV6_RECVHOPLIMIT));
+ /* Receive hop limit with datagram */
+ rb_define_const(rb_mSockConst, "IPV6_RECVHOPLIMIT", INTEGER2NUM(IPV6_RECVHOPLIMIT));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RECVHOPOPTS)
+ /* Receive hop-by-hop options */
+ rb_define_const(rb_cSocket, "IPV6_RECVHOPOPTS", INTEGER2NUM(IPV6_RECVHOPOPTS));
+ /* Receive hop-by-hop options */
+ rb_define_const(rb_mSockConst, "IPV6_RECVHOPOPTS", INTEGER2NUM(IPV6_RECVHOPOPTS));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RECVPKTINFO)
+ /* Receive destination IP address and incoming interface */
+ rb_define_const(rb_cSocket, "IPV6_RECVPKTINFO", INTEGER2NUM(IPV6_RECVPKTINFO));
+ /* Receive destination IP address and incoming interface */
+ rb_define_const(rb_mSockConst, "IPV6_RECVPKTINFO", INTEGER2NUM(IPV6_RECVPKTINFO));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RECVRTHDR)
+ /* Receive routing header */
+ rb_define_const(rb_cSocket, "IPV6_RECVRTHDR", INTEGER2NUM(IPV6_RECVRTHDR));
+ /* Receive routing header */
+ rb_define_const(rb_mSockConst, "IPV6_RECVRTHDR", INTEGER2NUM(IPV6_RECVRTHDR));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RECVTCLASS)
+ /* Receive traffic class */
+ rb_define_const(rb_cSocket, "IPV6_RECVTCLASS", INTEGER2NUM(IPV6_RECVTCLASS));
+ /* Receive traffic class */
+ rb_define_const(rb_mSockConst, "IPV6_RECVTCLASS", INTEGER2NUM(IPV6_RECVTCLASS));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RTHDR)
+ /* Allows removal of sticky routing headers */
+ rb_define_const(rb_cSocket, "IPV6_RTHDR", INTEGER2NUM(IPV6_RTHDR));
+ /* Allows removal of sticky routing headers */
+ rb_define_const(rb_mSockConst, "IPV6_RTHDR", INTEGER2NUM(IPV6_RTHDR));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RTHDRDSTOPTS)
+ /* Allows removal of sticky destination options header */
+ rb_define_const(rb_cSocket, "IPV6_RTHDRDSTOPTS", INTEGER2NUM(IPV6_RTHDRDSTOPTS));
+ /* Allows removal of sticky destination options header */
+ rb_define_const(rb_mSockConst, "IPV6_RTHDRDSTOPTS", INTEGER2NUM(IPV6_RTHDRDSTOPTS));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RTHDR_TYPE_0)
+ /* Routing header type 0 */
+ rb_define_const(rb_cSocket, "IPV6_RTHDR_TYPE_0", INTEGER2NUM(IPV6_RTHDR_TYPE_0));
+ /* Routing header type 0 */
+ rb_define_const(rb_mSockConst, "IPV6_RTHDR_TYPE_0", INTEGER2NUM(IPV6_RTHDR_TYPE_0));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_RECVPATHMTU)
+ /* Receive current path MTU with datagram */
+ rb_define_const(rb_cSocket, "IPV6_RECVPATHMTU", INTEGER2NUM(IPV6_RECVPATHMTU));
+ /* Receive current path MTU with datagram */
+ rb_define_const(rb_mSockConst, "IPV6_RECVPATHMTU", INTEGER2NUM(IPV6_RECVPATHMTU));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_TCLASS)
+ /* Specify the traffic class */
+ rb_define_const(rb_cSocket, "IPV6_TCLASS", INTEGER2NUM(IPV6_TCLASS));
+ /* Specify the traffic class */
+ rb_define_const(rb_mSockConst, "IPV6_TCLASS", INTEGER2NUM(IPV6_TCLASS));
+#endif
+#endif
+#if defined(INET6)
+#if defined(IPV6_USE_MIN_MTU)
+ /* Use the minimum MTU size */
+ rb_define_const(rb_cSocket, "IPV6_USE_MIN_MTU", INTEGER2NUM(IPV6_USE_MIN_MTU));
+ /* Use the minimum MTU size */
+ rb_define_const(rb_mSockConst, "IPV6_USE_MIN_MTU", INTEGER2NUM(IPV6_USE_MIN_MTU));
+#endif
+#endif
+#if defined(INET_ADDRSTRLEN)
+ /* Maximum length of an IPv4 address string */
+ rb_define_const(rb_cSocket, "INET_ADDRSTRLEN", INTEGER2NUM(INET_ADDRSTRLEN));
+ /* Maximum length of an IPv4 address string */
+ rb_define_const(rb_mSockConst, "INET_ADDRSTRLEN", INTEGER2NUM(INET_ADDRSTRLEN));
+#endif
+#if defined(INET6_ADDRSTRLEN)
+ /* Maximum length of an IPv6 address string */
+ rb_define_const(rb_cSocket, "INET6_ADDRSTRLEN", INTEGER2NUM(INET6_ADDRSTRLEN));
+ /* Maximum length of an IPv6 address string */
+ rb_define_const(rb_mSockConst, "INET6_ADDRSTRLEN", INTEGER2NUM(INET6_ADDRSTRLEN));
+#endif
+#if defined(IFNAMSIZ)
+ /* Maximum interface name size */
+ rb_define_const(rb_cSocket, "IFNAMSIZ", INTEGER2NUM(IFNAMSIZ));
+ /* Maximum interface name size */
+ rb_define_const(rb_mSockConst, "IFNAMSIZ", INTEGER2NUM(IFNAMSIZ));
+#endif
+#if defined(IF_NAMESIZE)
+ /* Maximum interface name size */
+ rb_define_const(rb_cSocket, "IF_NAMESIZE", INTEGER2NUM(IF_NAMESIZE));
+ /* Maximum interface name size */
+ rb_define_const(rb_mSockConst, "IF_NAMESIZE", INTEGER2NUM(IF_NAMESIZE));
+#endif
+#if defined(SOMAXCONN)
+ /* Maximum connection requests that may be queued for a socket */
+ rb_define_const(rb_cSocket, "SOMAXCONN", INTEGER2NUM(SOMAXCONN));
+ /* Maximum connection requests that may be queued for a socket */
+ rb_define_const(rb_mSockConst, "SOMAXCONN", INTEGER2NUM(SOMAXCONN));
+#endif
+#if defined(SCM_RIGHTS)
+ /* Access rights */
+ rb_define_const(rb_cSocket, "SCM_RIGHTS", INTEGER2NUM(SCM_RIGHTS));
+ /* Access rights */
+ rb_define_const(rb_mSockConst, "SCM_RIGHTS", INTEGER2NUM(SCM_RIGHTS));
+#endif
+#if defined(SCM_TIMESTAMP)
+ /* Timestamp (timeval) */
+ rb_define_const(rb_cSocket, "SCM_TIMESTAMP", INTEGER2NUM(SCM_TIMESTAMP));
+ /* Timestamp (timeval) */
+ rb_define_const(rb_mSockConst, "SCM_TIMESTAMP", INTEGER2NUM(SCM_TIMESTAMP));
+#endif
+#if defined(SCM_TIMESTAMPNS)
+ /* Timespec (timespec) */
+ rb_define_const(rb_cSocket, "SCM_TIMESTAMPNS", INTEGER2NUM(SCM_TIMESTAMPNS));
+ /* Timespec (timespec) */
+ rb_define_const(rb_mSockConst, "SCM_TIMESTAMPNS", INTEGER2NUM(SCM_TIMESTAMPNS));
+#endif
+#if defined(SCM_TIMESTAMPING)
+ /* Timestamp (timespec list) (Linux 2.6.30) */
+ rb_define_const(rb_cSocket, "SCM_TIMESTAMPING", INTEGER2NUM(SCM_TIMESTAMPING));
+ /* Timestamp (timespec list) (Linux 2.6.30) */
+ rb_define_const(rb_mSockConst, "SCM_TIMESTAMPING", INTEGER2NUM(SCM_TIMESTAMPING));
+#endif
+#if defined(SCM_BINTIME)
+ /* Timestamp (bintime) */
+ rb_define_const(rb_cSocket, "SCM_BINTIME", INTEGER2NUM(SCM_BINTIME));
+ /* Timestamp (bintime) */
+ rb_define_const(rb_mSockConst, "SCM_BINTIME", INTEGER2NUM(SCM_BINTIME));
+#endif
+#if defined(SCM_CREDENTIALS)
+ /* The sender's credentials */
+ rb_define_const(rb_cSocket, "SCM_CREDENTIALS", INTEGER2NUM(SCM_CREDENTIALS));
+ /* The sender's credentials */
+ rb_define_const(rb_mSockConst, "SCM_CREDENTIALS", INTEGER2NUM(SCM_CREDENTIALS));
+#endif
+#if defined(SCM_CREDS)
+ /* Process credentials */
+ rb_define_const(rb_cSocket, "SCM_CREDS", INTEGER2NUM(SCM_CREDS));
+ /* Process credentials */
+ rb_define_const(rb_mSockConst, "SCM_CREDS", INTEGER2NUM(SCM_CREDS));
+#endif
+#if defined(SCM_UCRED)
+ /* User credentials */
+ rb_define_const(rb_cSocket, "SCM_UCRED", INTEGER2NUM(SCM_UCRED));
+ /* User credentials */
+ rb_define_const(rb_mSockConst, "SCM_UCRED", INTEGER2NUM(SCM_UCRED));
+#endif
+#if defined(SCM_WIFI_STATUS)
+ /* Wifi status (Linux 3.3) */
+ rb_define_const(rb_cSocket, "SCM_WIFI_STATUS", INTEGER2NUM(SCM_WIFI_STATUS));
+ /* Wifi status (Linux 3.3) */
+ rb_define_const(rb_mSockConst, "SCM_WIFI_STATUS", INTEGER2NUM(SCM_WIFI_STATUS));
+#endif
+#if defined(LOCAL_PEERCRED)
+ /* Retrieve peer credentials */
+ rb_define_const(rb_cSocket, "LOCAL_PEERCRED", INTEGER2NUM(LOCAL_PEERCRED));
+ /* Retrieve peer credentials */
+ rb_define_const(rb_mSockConst, "LOCAL_PEERCRED", INTEGER2NUM(LOCAL_PEERCRED));
+#endif
+#if defined(LOCAL_CREDS)
+ /* Pass credentials to receiver */
+ rb_define_const(rb_cSocket, "LOCAL_CREDS", INTEGER2NUM(LOCAL_CREDS));
+ /* Pass credentials to receiver */
+ rb_define_const(rb_mSockConst, "LOCAL_CREDS", INTEGER2NUM(LOCAL_CREDS));
+#endif
+#if defined(LOCAL_CONNWAIT)
+ /* Connect blocks until accepted */
+ rb_define_const(rb_cSocket, "LOCAL_CONNWAIT", INTEGER2NUM(LOCAL_CONNWAIT));
+ /* Connect blocks until accepted */
+ rb_define_const(rb_mSockConst, "LOCAL_CONNWAIT", INTEGER2NUM(LOCAL_CONNWAIT));
+#endif
+#if defined(IFF_802_1Q_VLAN)
+ /* 802.1Q VLAN device */
+ rb_define_const(rb_cSocket, "IFF_802_1Q_VLAN", INTEGER2NUM(IFF_802_1Q_VLAN));
+ /* 802.1Q VLAN device */
+ rb_define_const(rb_mSockConst, "IFF_802_1Q_VLAN", INTEGER2NUM(IFF_802_1Q_VLAN));
+#endif
+#if defined(IFF_ALLMULTI)
+ /* receive all multicast packets */
+ rb_define_const(rb_cSocket, "IFF_ALLMULTI", INTEGER2NUM(IFF_ALLMULTI));
+ /* receive all multicast packets */
+ rb_define_const(rb_mSockConst, "IFF_ALLMULTI", INTEGER2NUM(IFF_ALLMULTI));
+#endif
+#if defined(IFF_ALTPHYS)
+ /* use alternate physical connection */
+ rb_define_const(rb_cSocket, "IFF_ALTPHYS", INTEGER2NUM(IFF_ALTPHYS));
+ /* use alternate physical connection */
+ rb_define_const(rb_mSockConst, "IFF_ALTPHYS", INTEGER2NUM(IFF_ALTPHYS));
+#endif
+#if defined(IFF_AUTOMEDIA)
+ /* auto media select active */
+ rb_define_const(rb_cSocket, "IFF_AUTOMEDIA", INTEGER2NUM(IFF_AUTOMEDIA));
+ /* auto media select active */
+ rb_define_const(rb_mSockConst, "IFF_AUTOMEDIA", INTEGER2NUM(IFF_AUTOMEDIA));
+#endif
+#if defined(IFF_BONDING)
+ /* bonding master or slave */
+ rb_define_const(rb_cSocket, "IFF_BONDING", INTEGER2NUM(IFF_BONDING));
+ /* bonding master or slave */
+ rb_define_const(rb_mSockConst, "IFF_BONDING", INTEGER2NUM(IFF_BONDING));
+#endif
+#if defined(IFF_BRIDGE_PORT)
+ /* device used as bridge port */
+ rb_define_const(rb_cSocket, "IFF_BRIDGE_PORT", INTEGER2NUM(IFF_BRIDGE_PORT));
+ /* device used as bridge port */
+ rb_define_const(rb_mSockConst, "IFF_BRIDGE_PORT", INTEGER2NUM(IFF_BRIDGE_PORT));
+#endif
+#if defined(IFF_BROADCAST)
+ /* broadcast address valid */
+ rb_define_const(rb_cSocket, "IFF_BROADCAST", INTEGER2NUM(IFF_BROADCAST));
+ /* broadcast address valid */
+ rb_define_const(rb_mSockConst, "IFF_BROADCAST", INTEGER2NUM(IFF_BROADCAST));
+#endif
+#if defined(IFF_CANTCONFIG)
+ /* unconfigurable using ioctl(2) */
+ rb_define_const(rb_cSocket, "IFF_CANTCONFIG", INTEGER2NUM(IFF_CANTCONFIG));
+ /* unconfigurable using ioctl(2) */
+ rb_define_const(rb_mSockConst, "IFF_CANTCONFIG", INTEGER2NUM(IFF_CANTCONFIG));
+#endif
+#if defined(IFF_DEBUG)
+ /* turn on debugging */
+ rb_define_const(rb_cSocket, "IFF_DEBUG", INTEGER2NUM(IFF_DEBUG));
+ /* turn on debugging */
+ rb_define_const(rb_mSockConst, "IFF_DEBUG", INTEGER2NUM(IFF_DEBUG));
+#endif
+#if defined(IFF_DISABLE_NETPOLL)
+ /* disable netpoll at run-time */
+ rb_define_const(rb_cSocket, "IFF_DISABLE_NETPOLL", INTEGER2NUM(IFF_DISABLE_NETPOLL));
+ /* disable netpoll at run-time */
+ rb_define_const(rb_mSockConst, "IFF_DISABLE_NETPOLL", INTEGER2NUM(IFF_DISABLE_NETPOLL));
+#endif
+#if defined(IFF_DONT_BRIDGE)
+ /* disallow bridging this ether dev */
+ rb_define_const(rb_cSocket, "IFF_DONT_BRIDGE", INTEGER2NUM(IFF_DONT_BRIDGE));
+ /* disallow bridging this ether dev */
+ rb_define_const(rb_mSockConst, "IFF_DONT_BRIDGE", INTEGER2NUM(IFF_DONT_BRIDGE));
+#endif
+#if defined(IFF_DORMANT)
+ /* driver signals dormant */
+ rb_define_const(rb_cSocket, "IFF_DORMANT", INTEGER2NUM(IFF_DORMANT));
+ /* driver signals dormant */
+ rb_define_const(rb_mSockConst, "IFF_DORMANT", INTEGER2NUM(IFF_DORMANT));
+#endif
+#if defined(IFF_DRV_OACTIVE)
+ /* tx hardware queue is full */
+ rb_define_const(rb_cSocket, "IFF_DRV_OACTIVE", INTEGER2NUM(IFF_DRV_OACTIVE));
+ /* tx hardware queue is full */
+ rb_define_const(rb_mSockConst, "IFF_DRV_OACTIVE", INTEGER2NUM(IFF_DRV_OACTIVE));
+#endif
+#if defined(IFF_DRV_RUNNING)
+ /* resources allocated */
+ rb_define_const(rb_cSocket, "IFF_DRV_RUNNING", INTEGER2NUM(IFF_DRV_RUNNING));
+ /* resources allocated */
+ rb_define_const(rb_mSockConst, "IFF_DRV_RUNNING", INTEGER2NUM(IFF_DRV_RUNNING));
+#endif
+#if defined(IFF_DYING)
+ /* interface is winding down */
+ rb_define_const(rb_cSocket, "IFF_DYING", INTEGER2NUM(IFF_DYING));
+ /* interface is winding down */
+ rb_define_const(rb_mSockConst, "IFF_DYING", INTEGER2NUM(IFF_DYING));
+#endif
+#if defined(IFF_DYNAMIC)
+ /* dialup device with changing addresses */
+ rb_define_const(rb_cSocket, "IFF_DYNAMIC", INTEGER2NUM(IFF_DYNAMIC));
+ /* dialup device with changing addresses */
+ rb_define_const(rb_mSockConst, "IFF_DYNAMIC", INTEGER2NUM(IFF_DYNAMIC));
+#endif
+#if defined(IFF_EBRIDGE)
+ /* ethernet bridging device */
+ rb_define_const(rb_cSocket, "IFF_EBRIDGE", INTEGER2NUM(IFF_EBRIDGE));
+ /* ethernet bridging device */
+ rb_define_const(rb_mSockConst, "IFF_EBRIDGE", INTEGER2NUM(IFF_EBRIDGE));
+#endif
+#if defined(IFF_ECHO)
+ /* echo sent packets */
+ rb_define_const(rb_cSocket, "IFF_ECHO", INTEGER2NUM(IFF_ECHO));
+ /* echo sent packets */
+ rb_define_const(rb_mSockConst, "IFF_ECHO", INTEGER2NUM(IFF_ECHO));
+#endif
+#if defined(IFF_ISATAP)
+ /* ISATAP interface (RFC4214) */
+ rb_define_const(rb_cSocket, "IFF_ISATAP", INTEGER2NUM(IFF_ISATAP));
+ /* ISATAP interface (RFC4214) */
+ rb_define_const(rb_mSockConst, "IFF_ISATAP", INTEGER2NUM(IFF_ISATAP));
+#endif
+#if defined(IFF_LINK0)
+ /* per link layer defined bit 0 */
+ rb_define_const(rb_cSocket, "IFF_LINK0", INTEGER2NUM(IFF_LINK0));
+ /* per link layer defined bit 0 */
+ rb_define_const(rb_mSockConst, "IFF_LINK0", INTEGER2NUM(IFF_LINK0));
+#endif
+#if defined(IFF_LINK1)
+ /* per link layer defined bit 1 */
+ rb_define_const(rb_cSocket, "IFF_LINK1", INTEGER2NUM(IFF_LINK1));
+ /* per link layer defined bit 1 */
+ rb_define_const(rb_mSockConst, "IFF_LINK1", INTEGER2NUM(IFF_LINK1));
+#endif
+#if defined(IFF_LINK2)
+ /* per link layer defined bit 2 */
+ rb_define_const(rb_cSocket, "IFF_LINK2", INTEGER2NUM(IFF_LINK2));
+ /* per link layer defined bit 2 */
+ rb_define_const(rb_mSockConst, "IFF_LINK2", INTEGER2NUM(IFF_LINK2));
+#endif
+#if defined(IFF_LIVE_ADDR_CHANGE)
+ /* hardware address change when it's running */
+ rb_define_const(rb_cSocket, "IFF_LIVE_ADDR_CHANGE", INTEGER2NUM(IFF_LIVE_ADDR_CHANGE));
+ /* hardware address change when it's running */
+ rb_define_const(rb_mSockConst, "IFF_LIVE_ADDR_CHANGE", INTEGER2NUM(IFF_LIVE_ADDR_CHANGE));
+#endif
+#if defined(IFF_LOOPBACK)
+ /* loopback net */
+ rb_define_const(rb_cSocket, "IFF_LOOPBACK", INTEGER2NUM(IFF_LOOPBACK));
+ /* loopback net */
+ rb_define_const(rb_mSockConst, "IFF_LOOPBACK", INTEGER2NUM(IFF_LOOPBACK));
+#endif
+#if defined(IFF_LOWER_UP)
+ /* driver signals L1 up */
+ rb_define_const(rb_cSocket, "IFF_LOWER_UP", INTEGER2NUM(IFF_LOWER_UP));
+ /* driver signals L1 up */
+ rb_define_const(rb_mSockConst, "IFF_LOWER_UP", INTEGER2NUM(IFF_LOWER_UP));
+#endif
+#if defined(IFF_MACVLAN_PORT)
+ /* device used as macvlan port */
+ rb_define_const(rb_cSocket, "IFF_MACVLAN_PORT", INTEGER2NUM(IFF_MACVLAN_PORT));
+ /* device used as macvlan port */
+ rb_define_const(rb_mSockConst, "IFF_MACVLAN_PORT", INTEGER2NUM(IFF_MACVLAN_PORT));
+#endif
+#if defined(IFF_MASTER)
+ /* master of a load balancer */
+ rb_define_const(rb_cSocket, "IFF_MASTER", INTEGER2NUM(IFF_MASTER));
+ /* master of a load balancer */
+ rb_define_const(rb_mSockConst, "IFF_MASTER", INTEGER2NUM(IFF_MASTER));
+#endif
+#if defined(IFF_MASTER_8023AD)
+ /* bonding master, 802.3ad. */
+ rb_define_const(rb_cSocket, "IFF_MASTER_8023AD", INTEGER2NUM(IFF_MASTER_8023AD));
+ /* bonding master, 802.3ad. */
+ rb_define_const(rb_mSockConst, "IFF_MASTER_8023AD", INTEGER2NUM(IFF_MASTER_8023AD));
+#endif
+#if defined(IFF_MASTER_ALB)
+ /* bonding master, balance-alb. */
+ rb_define_const(rb_cSocket, "IFF_MASTER_ALB", INTEGER2NUM(IFF_MASTER_ALB));
+ /* bonding master, balance-alb. */
+ rb_define_const(rb_mSockConst, "IFF_MASTER_ALB", INTEGER2NUM(IFF_MASTER_ALB));
+#endif
+#if defined(IFF_MASTER_ARPMON)
+ /* bonding master, ARP mon in use */
+ rb_define_const(rb_cSocket, "IFF_MASTER_ARPMON", INTEGER2NUM(IFF_MASTER_ARPMON));
+ /* bonding master, ARP mon in use */
+ rb_define_const(rb_mSockConst, "IFF_MASTER_ARPMON", INTEGER2NUM(IFF_MASTER_ARPMON));
+#endif
+#if defined(IFF_MONITOR)
+ /* user-requested monitor mode */
+ rb_define_const(rb_cSocket, "IFF_MONITOR", INTEGER2NUM(IFF_MONITOR));
+ /* user-requested monitor mode */
+ rb_define_const(rb_mSockConst, "IFF_MONITOR", INTEGER2NUM(IFF_MONITOR));
+#endif
+#if defined(IFF_MULTICAST)
+ /* supports multicast */
+ rb_define_const(rb_cSocket, "IFF_MULTICAST", INTEGER2NUM(IFF_MULTICAST));
+ /* supports multicast */
+ rb_define_const(rb_mSockConst, "IFF_MULTICAST", INTEGER2NUM(IFF_MULTICAST));
+#endif
+#if defined(IFF_NOARP)
+ /* no address resolution protocol */
+ rb_define_const(rb_cSocket, "IFF_NOARP", INTEGER2NUM(IFF_NOARP));
+ /* no address resolution protocol */
+ rb_define_const(rb_mSockConst, "IFF_NOARP", INTEGER2NUM(IFF_NOARP));
+#endif
+#if defined(IFF_NOTRAILERS)
+ /* avoid use of trailers */
+ rb_define_const(rb_cSocket, "IFF_NOTRAILERS", INTEGER2NUM(IFF_NOTRAILERS));
+ /* avoid use of trailers */
+ rb_define_const(rb_mSockConst, "IFF_NOTRAILERS", INTEGER2NUM(IFF_NOTRAILERS));
+#endif
+#if defined(IFF_OACTIVE)
+ /* transmission in progress */
+ rb_define_const(rb_cSocket, "IFF_OACTIVE", INTEGER2NUM(IFF_OACTIVE));
+ /* transmission in progress */
+ rb_define_const(rb_mSockConst, "IFF_OACTIVE", INTEGER2NUM(IFF_OACTIVE));
+#endif
+#if defined(IFF_OVS_DATAPATH)
+ /* device used as Open vSwitch datapath port */
+ rb_define_const(rb_cSocket, "IFF_OVS_DATAPATH", INTEGER2NUM(IFF_OVS_DATAPATH));
+ /* device used as Open vSwitch datapath port */
+ rb_define_const(rb_mSockConst, "IFF_OVS_DATAPATH", INTEGER2NUM(IFF_OVS_DATAPATH));
+#endif
+#if defined(IFF_POINTOPOINT)
+ /* point-to-point link */
+ rb_define_const(rb_cSocket, "IFF_POINTOPOINT", INTEGER2NUM(IFF_POINTOPOINT));
+ /* point-to-point link */
+ rb_define_const(rb_mSockConst, "IFF_POINTOPOINT", INTEGER2NUM(IFF_POINTOPOINT));
+#endif
+#if defined(IFF_PORTSEL)
+ /* can set media type */
+ rb_define_const(rb_cSocket, "IFF_PORTSEL", INTEGER2NUM(IFF_PORTSEL));
+ /* can set media type */
+ rb_define_const(rb_mSockConst, "IFF_PORTSEL", INTEGER2NUM(IFF_PORTSEL));
+#endif
+#if defined(IFF_PPROMISC)
+ /* user-requested promisc mode */
+ rb_define_const(rb_cSocket, "IFF_PPROMISC", INTEGER2NUM(IFF_PPROMISC));
+ /* user-requested promisc mode */
+ rb_define_const(rb_mSockConst, "IFF_PPROMISC", INTEGER2NUM(IFF_PPROMISC));
+#endif
+#if defined(IFF_PROMISC)
+ /* receive all packets */
+ rb_define_const(rb_cSocket, "IFF_PROMISC", INTEGER2NUM(IFF_PROMISC));
+ /* receive all packets */
+ rb_define_const(rb_mSockConst, "IFF_PROMISC", INTEGER2NUM(IFF_PROMISC));
+#endif
+#if defined(IFF_RENAMING)
+ /* interface is being renamed */
+ rb_define_const(rb_cSocket, "IFF_RENAMING", INTEGER2NUM(IFF_RENAMING));
+ /* interface is being renamed */
+ rb_define_const(rb_mSockConst, "IFF_RENAMING", INTEGER2NUM(IFF_RENAMING));
+#endif
+#if defined(IFF_ROUTE)
+ /* routing entry installed */
+ rb_define_const(rb_cSocket, "IFF_ROUTE", INTEGER2NUM(IFF_ROUTE));
+ /* routing entry installed */
+ rb_define_const(rb_mSockConst, "IFF_ROUTE", INTEGER2NUM(IFF_ROUTE));
+#endif
+#if defined(IFF_RUNNING)
+ /* resources allocated */
+ rb_define_const(rb_cSocket, "IFF_RUNNING", INTEGER2NUM(IFF_RUNNING));
+ /* resources allocated */
+ rb_define_const(rb_mSockConst, "IFF_RUNNING", INTEGER2NUM(IFF_RUNNING));
+#endif
+#if defined(IFF_SIMPLEX)
+ /* can't hear own transmissions */
+ rb_define_const(rb_cSocket, "IFF_SIMPLEX", INTEGER2NUM(IFF_SIMPLEX));
+ /* can't hear own transmissions */
+ rb_define_const(rb_mSockConst, "IFF_SIMPLEX", INTEGER2NUM(IFF_SIMPLEX));
+#endif
+#if defined(IFF_SLAVE)
+ /* slave of a load balancer */
+ rb_define_const(rb_cSocket, "IFF_SLAVE", INTEGER2NUM(IFF_SLAVE));
+ /* slave of a load balancer */
+ rb_define_const(rb_mSockConst, "IFF_SLAVE", INTEGER2NUM(IFF_SLAVE));
+#endif
+#if defined(IFF_SLAVE_INACTIVE)
+ /* bonding slave not the curr. active */
+ rb_define_const(rb_cSocket, "IFF_SLAVE_INACTIVE", INTEGER2NUM(IFF_SLAVE_INACTIVE));
+ /* bonding slave not the curr. active */
+ rb_define_const(rb_mSockConst, "IFF_SLAVE_INACTIVE", INTEGER2NUM(IFF_SLAVE_INACTIVE));
+#endif
+#if defined(IFF_SLAVE_NEEDARP)
+ /* need ARPs for validation */
+ rb_define_const(rb_cSocket, "IFF_SLAVE_NEEDARP", INTEGER2NUM(IFF_SLAVE_NEEDARP));
+ /* need ARPs for validation */
+ rb_define_const(rb_mSockConst, "IFF_SLAVE_NEEDARP", INTEGER2NUM(IFF_SLAVE_NEEDARP));
+#endif
+#if defined(IFF_SMART)
+ /* interface manages own routes */
+ rb_define_const(rb_cSocket, "IFF_SMART", INTEGER2NUM(IFF_SMART));
+ /* interface manages own routes */
+ rb_define_const(rb_mSockConst, "IFF_SMART", INTEGER2NUM(IFF_SMART));
+#endif
+#if defined(IFF_STATICARP)
+ /* static ARP */
+ rb_define_const(rb_cSocket, "IFF_STATICARP", INTEGER2NUM(IFF_STATICARP));
+ /* static ARP */
+ rb_define_const(rb_mSockConst, "IFF_STATICARP", INTEGER2NUM(IFF_STATICARP));
+#endif
+#if defined(IFF_SUPP_NOFCS)
+ /* sending custom FCS */
+ rb_define_const(rb_cSocket, "IFF_SUPP_NOFCS", INTEGER2NUM(IFF_SUPP_NOFCS));
+ /* sending custom FCS */
+ rb_define_const(rb_mSockConst, "IFF_SUPP_NOFCS", INTEGER2NUM(IFF_SUPP_NOFCS));
+#endif
+#if defined(IFF_TEAM_PORT)
+ /* used as team port */
+ rb_define_const(rb_cSocket, "IFF_TEAM_PORT", INTEGER2NUM(IFF_TEAM_PORT));
+ /* used as team port */
+ rb_define_const(rb_mSockConst, "IFF_TEAM_PORT", INTEGER2NUM(IFF_TEAM_PORT));
+#endif
+#if defined(IFF_TX_SKB_SHARING)
+ /* sharing skbs on transmit */
+ rb_define_const(rb_cSocket, "IFF_TX_SKB_SHARING", INTEGER2NUM(IFF_TX_SKB_SHARING));
+ /* sharing skbs on transmit */
+ rb_define_const(rb_mSockConst, "IFF_TX_SKB_SHARING", INTEGER2NUM(IFF_TX_SKB_SHARING));
+#endif
+#if defined(IFF_UNICAST_FLT)
+ /* unicast filtering */
+ rb_define_const(rb_cSocket, "IFF_UNICAST_FLT", INTEGER2NUM(IFF_UNICAST_FLT));
+ /* unicast filtering */
+ rb_define_const(rb_mSockConst, "IFF_UNICAST_FLT", INTEGER2NUM(IFF_UNICAST_FLT));
+#endif
+#if defined(IFF_UP)
+ /* interface is up */
+ rb_define_const(rb_cSocket, "IFF_UP", INTEGER2NUM(IFF_UP));
+ /* interface is up */
+ rb_define_const(rb_mSockConst, "IFF_UP", INTEGER2NUM(IFF_UP));
+#endif
+#if defined(IFF_WAN_HDLC)
+ /* WAN HDLC device */
+ rb_define_const(rb_cSocket, "IFF_WAN_HDLC", INTEGER2NUM(IFF_WAN_HDLC));
+ /* WAN HDLC device */
+ rb_define_const(rb_mSockConst, "IFF_WAN_HDLC", INTEGER2NUM(IFF_WAN_HDLC));
+#endif
+#if defined(IFF_XMIT_DST_RELEASE)
+ /* dev_hard_start_xmit() is allowed to release skb->dst */
+ rb_define_const(rb_cSocket, "IFF_XMIT_DST_RELEASE", INTEGER2NUM(IFF_XMIT_DST_RELEASE));
+ /* dev_hard_start_xmit() is allowed to release skb->dst */
+ rb_define_const(rb_mSockConst, "IFF_XMIT_DST_RELEASE", INTEGER2NUM(IFF_XMIT_DST_RELEASE));
+#endif
+#if defined(IFF_VOLATILE)
+ /* volatile flags */
+ rb_define_const(rb_cSocket, "IFF_VOLATILE", INTEGER2NUM(IFF_VOLATILE));
+ /* volatile flags */
+ rb_define_const(rb_mSockConst, "IFF_VOLATILE", INTEGER2NUM(IFF_VOLATILE));
+#endif
+#if defined(IFF_CANTCHANGE)
+ /* flags not changeable */
+ rb_define_const(rb_cSocket, "IFF_CANTCHANGE", INTEGER2NUM(IFF_CANTCHANGE));
+ /* flags not changeable */
+ rb_define_const(rb_mSockConst, "IFF_CANTCHANGE", INTEGER2NUM(IFF_CANTCHANGE));
+#endif
+
+ rsock_intern_family_hash = st_init_numtable();
+#ifdef AF_E164
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_E164, (st_data_t)rb_intern2("AF_E164", 7));
+#endif
+#ifdef AF_PACKET
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_PACKET, (st_data_t)rb_intern2("AF_PACKET", 9));
+#endif
+#ifdef AF_MAX
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_MAX, (st_data_t)rb_intern2("AF_MAX", 6));
+#endif
+#ifdef AF_NETGRAPH
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_NETGRAPH, (st_data_t)rb_intern2("AF_NETGRAPH", 11));
+#endif
+#ifdef AF_ATM
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_ATM, (st_data_t)rb_intern2("AF_ATM", 6));
+#endif
+#ifdef AF_PPP
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_PPP, (st_data_t)rb_intern2("AF_PPP", 6));
+#endif
+#ifdef AF_NETBIOS
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_NETBIOS, (st_data_t)rb_intern2("AF_NETBIOS", 10));
+#endif
+#ifdef AF_SYSTEM
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_SYSTEM, (st_data_t)rb_intern2("AF_SYSTEM", 9));
+#endif
+#ifdef AF_NATM
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_NATM, (st_data_t)rb_intern2("AF_NATM", 7));
+#endif
+#ifdef AF_ISDN
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_ISDN, (st_data_t)rb_intern2("AF_ISDN", 7));
+#endif
+#ifdef AF_NDRV
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_NDRV, (st_data_t)rb_intern2("AF_NDRV", 7));
+#endif
+#ifdef AF_SIP
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_SIP, (st_data_t)rb_intern2("AF_SIP", 6));
+#endif
+#ifdef AF_CNT
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_CNT, (st_data_t)rb_intern2("AF_CNT", 6));
+#endif
+#ifdef AF_COIP
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_COIP, (st_data_t)rb_intern2("AF_COIP", 7));
+#endif
+#ifdef AF_LINK
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_LINK, (st_data_t)rb_intern2("AF_LINK", 7));
+#endif
+#ifdef AF_ROUTE
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_ROUTE, (st_data_t)rb_intern2("AF_ROUTE", 8));
+#endif
+#ifdef AF_HYLINK
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_HYLINK, (st_data_t)rb_intern2("AF_HYLINK", 9));
+#endif
+#ifdef AF_LAT
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_LAT, (st_data_t)rb_intern2("AF_LAT", 6));
+#endif
+#ifdef AF_DLI
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_DLI, (st_data_t)rb_intern2("AF_DLI", 6));
+#endif
+#ifdef AF_DEC
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_DEC, (st_data_t)rb_intern2("AF_DEC", 6));
+#endif
+#ifdef AF_SNA
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_SNA, (st_data_t)rb_intern2("AF_SNA", 6));
+#endif
+#ifdef AF_CCITT
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_CCITT, (st_data_t)rb_intern2("AF_CCITT", 8));
+#endif
+#ifdef AF_DATAKIT
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_DATAKIT, (st_data_t)rb_intern2("AF_DATAKIT", 10));
+#endif
+#ifdef AF_ECMA
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_ECMA, (st_data_t)rb_intern2("AF_ECMA", 7));
+#endif
+#ifdef AF_OSI
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_OSI, (st_data_t)rb_intern2("AF_OSI", 6));
+#endif
+#ifdef AF_ISO
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_ISO, (st_data_t)rb_intern2("AF_ISO", 6));
+#endif
+#ifdef AF_NS
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_NS, (st_data_t)rb_intern2("AF_NS", 5));
+#endif
+#ifdef AF_CHAOS
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_CHAOS, (st_data_t)rb_intern2("AF_CHAOS", 8));
+#endif
+#ifdef AF_PUP
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_PUP, (st_data_t)rb_intern2("AF_PUP", 6));
+#endif
+#ifdef AF_IMPLINK
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_IMPLINK, (st_data_t)rb_intern2("AF_IMPLINK", 10));
+#endif
+#ifdef AF_LOCAL
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_LOCAL, (st_data_t)rb_intern2("AF_LOCAL", 8));
+#endif
+#ifdef AF_APPLETALK
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_APPLETALK, (st_data_t)rb_intern2("AF_APPLETALK", 12));
+#endif
+#ifdef AF_IPX
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_IPX, (st_data_t)rb_intern2("AF_IPX", 6));
+#endif
+#ifdef AF_AX25
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_AX25, (st_data_t)rb_intern2("AF_AX25", 7));
+#endif
+#ifdef AF_UNIX
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_UNIX, (st_data_t)rb_intern2("AF_UNIX", 7));
+#endif
+#ifdef AF_INET6
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_INET6, (st_data_t)rb_intern2("AF_INET6", 8));
+#endif
+#ifdef AF_INET
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_INET, (st_data_t)rb_intern2("AF_INET", 7));
+#endif
+#ifdef AF_UNSPEC
+ st_insert(rsock_intern_family_hash, (st_data_t)AF_UNSPEC, (st_data_t)rb_intern2("AF_UNSPEC", 9));
+#endif
+
+ rsock_intern_family_noprefix_hash = st_init_numtable();
+#ifdef AF_E164
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_E164, (st_data_t)rb_intern2("AF_E164", 7));
+#endif
+#ifdef AF_PACKET
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_PACKET, (st_data_t)rb_intern2("AF_PACKET", 9));
+#endif
+#ifdef AF_MAX
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_MAX, (st_data_t)rb_intern2("AF_MAX", 6));
+#endif
+#ifdef AF_NETGRAPH
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NETGRAPH, (st_data_t)rb_intern2("AF_NETGRAPH", 11));
+#endif
+#ifdef AF_ATM
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ATM, (st_data_t)rb_intern2("AF_ATM", 6));
+#endif
+#ifdef AF_PPP
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_PPP, (st_data_t)rb_intern2("AF_PPP", 6));
+#endif
+#ifdef AF_NETBIOS
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NETBIOS, (st_data_t)rb_intern2("AF_NETBIOS", 10));
+#endif
+#ifdef AF_SYSTEM
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_SYSTEM, (st_data_t)rb_intern2("AF_SYSTEM", 9));
+#endif
+#ifdef AF_NATM
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NATM, (st_data_t)rb_intern2("AF_NATM", 7));
+#endif
+#ifdef AF_ISDN
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ISDN, (st_data_t)rb_intern2("AF_ISDN", 7));
+#endif
+#ifdef AF_NDRV
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NDRV, (st_data_t)rb_intern2("AF_NDRV", 7));
+#endif
+#ifdef AF_SIP
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_SIP, (st_data_t)rb_intern2("AF_SIP", 6));
+#endif
+#ifdef AF_CNT
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_CNT, (st_data_t)rb_intern2("AF_CNT", 6));
+#endif
+#ifdef AF_COIP
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_COIP, (st_data_t)rb_intern2("AF_COIP", 7));
+#endif
+#ifdef AF_LINK
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_LINK, (st_data_t)rb_intern2("AF_LINK", 7));
+#endif
+#ifdef AF_ROUTE
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ROUTE, (st_data_t)rb_intern2("AF_ROUTE", 8));
+#endif
+#ifdef AF_HYLINK
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_HYLINK, (st_data_t)rb_intern2("AF_HYLINK", 9));
+#endif
+#ifdef AF_LAT
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_LAT, (st_data_t)rb_intern2("AF_LAT", 6));
+#endif
+#ifdef AF_DLI
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_DLI, (st_data_t)rb_intern2("AF_DLI", 6));
+#endif
+#ifdef AF_DEC
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_DEC, (st_data_t)rb_intern2("AF_DEC", 6));
+#endif
+#ifdef AF_SNA
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_SNA, (st_data_t)rb_intern2("AF_SNA", 6));
+#endif
+#ifdef AF_CCITT
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_CCITT, (st_data_t)rb_intern2("AF_CCITT", 8));
+#endif
+#ifdef AF_DATAKIT
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_DATAKIT, (st_data_t)rb_intern2("AF_DATAKIT", 10));
+#endif
+#ifdef AF_ECMA
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ECMA, (st_data_t)rb_intern2("AF_ECMA", 7));
+#endif
+#ifdef AF_OSI
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_OSI, (st_data_t)rb_intern2("AF_OSI", 6));
+#endif
+#ifdef AF_ISO
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ISO, (st_data_t)rb_intern2("AF_ISO", 6));
+#endif
+#ifdef AF_NS
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NS, (st_data_t)rb_intern2("AF_NS", 5));
+#endif
+#ifdef AF_CHAOS
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_CHAOS, (st_data_t)rb_intern2("AF_CHAOS", 8));
+#endif
+#ifdef AF_PUP
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_PUP, (st_data_t)rb_intern2("AF_PUP", 6));
+#endif
+#ifdef AF_IMPLINK
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_IMPLINK, (st_data_t)rb_intern2("AF_IMPLINK", 10));
+#endif
+#ifdef AF_LOCAL
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_LOCAL, (st_data_t)rb_intern2("AF_LOCAL", 8));
+#endif
+#ifdef AF_APPLETALK
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_APPLETALK, (st_data_t)rb_intern2("AF_APPLETALK", 12));
+#endif
+#ifdef AF_IPX
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_IPX, (st_data_t)rb_intern2("AF_IPX", 6));
+#endif
+#ifdef AF_AX25
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_AX25, (st_data_t)rb_intern2("AF_AX25", 7));
+#endif
+#ifdef AF_UNIX
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_UNIX, (st_data_t)rb_intern2("AF_UNIX", 7));
+#endif
+#ifdef AF_INET6
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_INET6, (st_data_t)rb_intern2("AF_INET6", 8));
+#endif
+#ifdef AF_INET
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_INET, (st_data_t)rb_intern2("AF_INET", 7));
+#endif
+#ifdef AF_UNSPEC
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_UNSPEC, (st_data_t)rb_intern2("AF_UNSPEC", 9));
+#endif
+#ifdef AF_E164
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_E164, (st_data_t)rb_intern2("E164", 4));
+#endif
+#ifdef AF_PACKET
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_PACKET, (st_data_t)rb_intern2("PACKET", 6));
+#endif
+#ifdef AF_MAX
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_MAX, (st_data_t)rb_intern2("MAX", 3));
+#endif
+#ifdef AF_NETGRAPH
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NETGRAPH, (st_data_t)rb_intern2("NETGRAPH", 8));
+#endif
+#ifdef AF_ATM
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ATM, (st_data_t)rb_intern2("ATM", 3));
+#endif
+#ifdef AF_PPP
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_PPP, (st_data_t)rb_intern2("PPP", 3));
+#endif
+#ifdef AF_NETBIOS
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NETBIOS, (st_data_t)rb_intern2("NETBIOS", 7));
+#endif
+#ifdef AF_SYSTEM
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_SYSTEM, (st_data_t)rb_intern2("SYSTEM", 6));
+#endif
+#ifdef AF_NATM
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NATM, (st_data_t)rb_intern2("NATM", 4));
+#endif
+#ifdef AF_ISDN
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ISDN, (st_data_t)rb_intern2("ISDN", 4));
+#endif
+#ifdef AF_NDRV
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NDRV, (st_data_t)rb_intern2("NDRV", 4));
+#endif
+#ifdef AF_SIP
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_SIP, (st_data_t)rb_intern2("SIP", 3));
+#endif
+#ifdef AF_CNT
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_CNT, (st_data_t)rb_intern2("CNT", 3));
+#endif
+#ifdef AF_COIP
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_COIP, (st_data_t)rb_intern2("COIP", 4));
+#endif
+#ifdef AF_LINK
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_LINK, (st_data_t)rb_intern2("LINK", 4));
+#endif
+#ifdef AF_ROUTE
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ROUTE, (st_data_t)rb_intern2("ROUTE", 5));
+#endif
+#ifdef AF_HYLINK
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_HYLINK, (st_data_t)rb_intern2("HYLINK", 6));
+#endif
+#ifdef AF_LAT
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_LAT, (st_data_t)rb_intern2("LAT", 3));
+#endif
+#ifdef AF_DLI
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_DLI, (st_data_t)rb_intern2("DLI", 3));
+#endif
+#ifdef AF_DEC
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_DEC, (st_data_t)rb_intern2("DEC", 3));
+#endif
+#ifdef AF_SNA
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_SNA, (st_data_t)rb_intern2("SNA", 3));
+#endif
+#ifdef AF_CCITT
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_CCITT, (st_data_t)rb_intern2("CCITT", 5));
+#endif
+#ifdef AF_DATAKIT
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_DATAKIT, (st_data_t)rb_intern2("DATAKIT", 7));
+#endif
+#ifdef AF_ECMA
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ECMA, (st_data_t)rb_intern2("ECMA", 4));
+#endif
+#ifdef AF_OSI
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_OSI, (st_data_t)rb_intern2("OSI", 3));
+#endif
+#ifdef AF_ISO
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_ISO, (st_data_t)rb_intern2("ISO", 3));
+#endif
+#ifdef AF_NS
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_NS, (st_data_t)rb_intern2("NS", 2));
+#endif
+#ifdef AF_CHAOS
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_CHAOS, (st_data_t)rb_intern2("CHAOS", 5));
+#endif
+#ifdef AF_PUP
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_PUP, (st_data_t)rb_intern2("PUP", 3));
+#endif
+#ifdef AF_IMPLINK
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_IMPLINK, (st_data_t)rb_intern2("IMPLINK", 7));
+#endif
+#ifdef AF_LOCAL
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_LOCAL, (st_data_t)rb_intern2("LOCAL", 5));
+#endif
+#ifdef AF_APPLETALK
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_APPLETALK, (st_data_t)rb_intern2("APPLETALK", 9));
+#endif
+#ifdef AF_IPX
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_IPX, (st_data_t)rb_intern2("IPX", 3));
+#endif
+#ifdef AF_AX25
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_AX25, (st_data_t)rb_intern2("AX25", 4));
+#endif
+#ifdef AF_UNIX
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_UNIX, (st_data_t)rb_intern2("UNIX", 4));
+#endif
+#ifdef AF_INET6
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_INET6, (st_data_t)rb_intern2("INET6", 5));
+#endif
+#ifdef AF_INET
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_INET, (st_data_t)rb_intern2("INET", 4));
+#endif
+#ifdef AF_UNSPEC
+ st_insert(rsock_intern_family_noprefix_hash, (st_data_t)AF_UNSPEC, (st_data_t)rb_intern2("UNSPEC", 6));
+#endif
+
+ rsock_intern_protocol_family_hash = st_init_numtable();
+#ifdef PF_KEY
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_KEY, (st_data_t)rb_intern2("PF_KEY", 6));
+#endif
+#ifdef PF_PIP
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_PIP, (st_data_t)rb_intern2("PF_PIP", 6));
+#endif
+#ifdef PF_RTIP
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_RTIP, (st_data_t)rb_intern2("PF_RTIP", 7));
+#endif
+#ifdef PF_XTP
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_XTP, (st_data_t)rb_intern2("PF_XTP", 6));
+#endif
+#ifdef PF_PACKET
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_PACKET, (st_data_t)rb_intern2("PF_PACKET", 9));
+#endif
+#ifdef PF_MAX
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_MAX, (st_data_t)rb_intern2("PF_MAX", 6));
+#endif
+#ifdef PF_NETGRAPH
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_NETGRAPH, (st_data_t)rb_intern2("PF_NETGRAPH", 11));
+#endif
+#ifdef PF_ATM
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_ATM, (st_data_t)rb_intern2("PF_ATM", 6));
+#endif
+#ifdef PF_PPP
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_PPP, (st_data_t)rb_intern2("PF_PPP", 6));
+#endif
+#ifdef PF_NETBIOS
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_NETBIOS, (st_data_t)rb_intern2("PF_NETBIOS", 10));
+#endif
+#ifdef PF_SYSTEM
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_SYSTEM, (st_data_t)rb_intern2("PF_SYSTEM", 9));
+#endif
+#ifdef PF_NATM
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_NATM, (st_data_t)rb_intern2("PF_NATM", 7));
+#endif
+#ifdef PF_ISDN
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_ISDN, (st_data_t)rb_intern2("PF_ISDN", 7));
+#endif
+#ifdef PF_NDRV
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_NDRV, (st_data_t)rb_intern2("PF_NDRV", 7));
+#endif
+#ifdef PF_SIP
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_SIP, (st_data_t)rb_intern2("PF_SIP", 6));
+#endif
+#ifdef PF_CNT
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_CNT, (st_data_t)rb_intern2("PF_CNT", 6));
+#endif
+#ifdef PF_COIP
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_COIP, (st_data_t)rb_intern2("PF_COIP", 7));
+#endif
+#ifdef PF_LINK
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_LINK, (st_data_t)rb_intern2("PF_LINK", 7));
+#endif
+#ifdef PF_ROUTE
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_ROUTE, (st_data_t)rb_intern2("PF_ROUTE", 8));
+#endif
+#ifdef PF_HYLINK
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_HYLINK, (st_data_t)rb_intern2("PF_HYLINK", 9));
+#endif
+#ifdef PF_LAT
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_LAT, (st_data_t)rb_intern2("PF_LAT", 6));
+#endif
+#ifdef PF_DLI
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_DLI, (st_data_t)rb_intern2("PF_DLI", 6));
+#endif
+#ifdef PF_DEC
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_DEC, (st_data_t)rb_intern2("PF_DEC", 6));
+#endif
+#ifdef PF_SNA
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_SNA, (st_data_t)rb_intern2("PF_SNA", 6));
+#endif
+#ifdef PF_CCITT
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_CCITT, (st_data_t)rb_intern2("PF_CCITT", 8));
+#endif
+#ifdef PF_DATAKIT
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_DATAKIT, (st_data_t)rb_intern2("PF_DATAKIT", 10));
+#endif
+#ifdef PF_ECMA
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_ECMA, (st_data_t)rb_intern2("PF_ECMA", 7));
+#endif
+#ifdef PF_OSI
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_OSI, (st_data_t)rb_intern2("PF_OSI", 6));
+#endif
+#ifdef PF_ISO
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_ISO, (st_data_t)rb_intern2("PF_ISO", 6));
+#endif
+#ifdef PF_NS
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_NS, (st_data_t)rb_intern2("PF_NS", 5));
+#endif
+#ifdef PF_CHAOS
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_CHAOS, (st_data_t)rb_intern2("PF_CHAOS", 8));
+#endif
+#ifdef PF_PUP
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_PUP, (st_data_t)rb_intern2("PF_PUP", 6));
+#endif
+#ifdef PF_IMPLINK
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_IMPLINK, (st_data_t)rb_intern2("PF_IMPLINK", 10));
+#endif
+#ifdef PF_LOCAL
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_LOCAL, (st_data_t)rb_intern2("PF_LOCAL", 8));
+#endif
+#ifdef PF_APPLETALK
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_APPLETALK, (st_data_t)rb_intern2("PF_APPLETALK", 12));
+#endif
+#ifdef PF_IPX
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_IPX, (st_data_t)rb_intern2("PF_IPX", 6));
+#endif
+#ifdef PF_AX25
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_AX25, (st_data_t)rb_intern2("PF_AX25", 7));
+#endif
+#ifdef PF_UNIX
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_UNIX, (st_data_t)rb_intern2("PF_UNIX", 7));
+#endif
+#ifdef PF_INET6
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_INET6, (st_data_t)rb_intern2("PF_INET6", 8));
+#endif
+#ifdef PF_INET
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_INET, (st_data_t)rb_intern2("PF_INET", 7));
+#endif
+#ifdef PF_UNSPEC
+ st_insert(rsock_intern_protocol_family_hash, (st_data_t)PF_UNSPEC, (st_data_t)rb_intern2("PF_UNSPEC", 9));
+#endif
+
+ rsock_intern_socktype_hash = st_init_numtable();
+#ifdef SOCK_PACKET
+ st_insert(rsock_intern_socktype_hash, (st_data_t)SOCK_PACKET, (st_data_t)rb_intern2("SOCK_PACKET", 11));
+#endif
+#ifdef SOCK_SEQPACKET
+ st_insert(rsock_intern_socktype_hash, (st_data_t)SOCK_SEQPACKET, (st_data_t)rb_intern2("SOCK_SEQPACKET", 14));
+#endif
+#ifdef SOCK_RDM
+ st_insert(rsock_intern_socktype_hash, (st_data_t)SOCK_RDM, (st_data_t)rb_intern2("SOCK_RDM", 8));
+#endif
+#ifdef SOCK_RAW
+ st_insert(rsock_intern_socktype_hash, (st_data_t)SOCK_RAW, (st_data_t)rb_intern2("SOCK_RAW", 8));
+#endif
+#ifdef SOCK_DGRAM
+ st_insert(rsock_intern_socktype_hash, (st_data_t)SOCK_DGRAM, (st_data_t)rb_intern2("SOCK_DGRAM", 10));
+#endif
+#ifdef SOCK_STREAM
+ st_insert(rsock_intern_socktype_hash, (st_data_t)SOCK_STREAM, (st_data_t)rb_intern2("SOCK_STREAM", 11));
+#endif
+
+ rsock_intern_ipproto_hash = st_init_numtable();
+#ifdef IPPROTO_MAX
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_MAX, (st_data_t)rb_intern2("IPPROTO_MAX", 11));
+#endif
+#ifdef IPPROTO_RAW
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_RAW, (st_data_t)rb_intern2("IPPROTO_RAW", 11));
+#endif
+#ifdef IPPROTO_ROUTING
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_ROUTING, (st_data_t)rb_intern2("IPPROTO_ROUTING", 15));
+#endif
+#ifdef IPPROTO_NONE
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_NONE, (st_data_t)rb_intern2("IPPROTO_NONE", 12));
+#endif
+#ifdef IPPROTO_IPV6
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_IPV6, (st_data_t)rb_intern2("IPPROTO_IPV6", 12));
+#endif
+#ifdef IPPROTO_ICMPV6
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_ICMPV6, (st_data_t)rb_intern2("IPPROTO_ICMPV6", 14));
+#endif
+#ifdef IPPROTO_HOPOPTS
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_HOPOPTS, (st_data_t)rb_intern2("IPPROTO_HOPOPTS", 15));
+#endif
+#ifdef IPPROTO_FRAGMENT
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_FRAGMENT, (st_data_t)rb_intern2("IPPROTO_FRAGMENT", 16));
+#endif
+#ifdef IPPROTO_ESP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_ESP, (st_data_t)rb_intern2("IPPROTO_ESP", 11));
+#endif
+#ifdef IPPROTO_DSTOPTS
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_DSTOPTS, (st_data_t)rb_intern2("IPPROTO_DSTOPTS", 15));
+#endif
+#ifdef IPPROTO_AH
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_AH, (st_data_t)rb_intern2("IPPROTO_AH", 10));
+#endif
+#ifdef IPPROTO_BIP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_BIP, (st_data_t)rb_intern2("IPPROTO_BIP", 11));
+#endif
+#ifdef IPPROTO_EON
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_EON, (st_data_t)rb_intern2("IPPROTO_EON", 11));
+#endif
+#ifdef IPPROTO_XTP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_XTP, (st_data_t)rb_intern2("IPPROTO_XTP", 11));
+#endif
+#ifdef IPPROTO_TP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_TP, (st_data_t)rb_intern2("IPPROTO_TP", 10));
+#endif
+#ifdef IPPROTO_ND
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_ND, (st_data_t)rb_intern2("IPPROTO_ND", 10));
+#endif
+#ifdef IPPROTO_HELLO
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_HELLO, (st_data_t)rb_intern2("IPPROTO_HELLO", 13));
+#endif
+#ifdef IPPROTO_IDP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_IDP, (st_data_t)rb_intern2("IPPROTO_IDP", 11));
+#endif
+#ifdef IPPROTO_UDP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_UDP, (st_data_t)rb_intern2("IPPROTO_UDP", 11));
+#endif
+#ifdef IPPROTO_PUP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_PUP, (st_data_t)rb_intern2("IPPROTO_PUP", 11));
+#endif
+#ifdef IPPROTO_EGP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_EGP, (st_data_t)rb_intern2("IPPROTO_EGP", 11));
+#endif
+#ifdef IPPROTO_TCP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_TCP, (st_data_t)rb_intern2("IPPROTO_TCP", 11));
+#endif
+#ifdef IPPROTO_GGP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_GGP, (st_data_t)rb_intern2("IPPROTO_GGP", 11));
+#endif
+#ifdef IPPROTO_IGMP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_IGMP, (st_data_t)rb_intern2("IPPROTO_IGMP", 12));
+#endif
+#ifdef IPPROTO_ICMP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_ICMP, (st_data_t)rb_intern2("IPPROTO_ICMP", 12));
+#endif
+#ifdef IPPROTO_IP
+ st_insert(rsock_intern_ipproto_hash, (st_data_t)IPPROTO_IP, (st_data_t)rb_intern2("IPPROTO_IP", 10));
+#endif
+
+ rsock_intern_iplevel_hash = st_init_numtable();
+#ifdef IPPROTO_MAX
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_MAX, (st_data_t)rb_intern2("IPPROTO_MAX", 11));
+#endif
+#ifdef IPPROTO_RAW
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_RAW, (st_data_t)rb_intern2("IPPROTO_RAW", 11));
+#endif
+#ifdef IPPROTO_ROUTING
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ROUTING, (st_data_t)rb_intern2("IPPROTO_ROUTING", 15));
+#endif
+#ifdef IPPROTO_NONE
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_NONE, (st_data_t)rb_intern2("IPPROTO_NONE", 12));
+#endif
+#ifdef IPPROTO_IPV6
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_IPV6, (st_data_t)rb_intern2("IPPROTO_IPV6", 12));
+#endif
+#ifdef IPPROTO_ICMPV6
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ICMPV6, (st_data_t)rb_intern2("IPPROTO_ICMPV6", 14));
+#endif
+#ifdef IPPROTO_HOPOPTS
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_HOPOPTS, (st_data_t)rb_intern2("IPPROTO_HOPOPTS", 15));
+#endif
+#ifdef IPPROTO_FRAGMENT
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_FRAGMENT, (st_data_t)rb_intern2("IPPROTO_FRAGMENT", 16));
+#endif
+#ifdef IPPROTO_ESP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ESP, (st_data_t)rb_intern2("IPPROTO_ESP", 11));
+#endif
+#ifdef IPPROTO_DSTOPTS
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_DSTOPTS, (st_data_t)rb_intern2("IPPROTO_DSTOPTS", 15));
+#endif
+#ifdef IPPROTO_AH
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_AH, (st_data_t)rb_intern2("IPPROTO_AH", 10));
+#endif
+#ifdef IPPROTO_BIP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_BIP, (st_data_t)rb_intern2("IPPROTO_BIP", 11));
+#endif
+#ifdef IPPROTO_EON
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_EON, (st_data_t)rb_intern2("IPPROTO_EON", 11));
+#endif
+#ifdef IPPROTO_XTP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_XTP, (st_data_t)rb_intern2("IPPROTO_XTP", 11));
+#endif
+#ifdef IPPROTO_TP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_TP, (st_data_t)rb_intern2("IPPROTO_TP", 10));
+#endif
+#ifdef IPPROTO_ND
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ND, (st_data_t)rb_intern2("IPPROTO_ND", 10));
+#endif
+#ifdef IPPROTO_HELLO
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_HELLO, (st_data_t)rb_intern2("IPPROTO_HELLO", 13));
+#endif
+#ifdef IPPROTO_IDP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_IDP, (st_data_t)rb_intern2("IPPROTO_IDP", 11));
+#endif
+#ifdef IPPROTO_UDP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_UDP, (st_data_t)rb_intern2("IPPROTO_UDP", 11));
+#endif
+#ifdef IPPROTO_PUP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_PUP, (st_data_t)rb_intern2("IPPROTO_PUP", 11));
+#endif
+#ifdef IPPROTO_EGP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_EGP, (st_data_t)rb_intern2("IPPROTO_EGP", 11));
+#endif
+#ifdef IPPROTO_TCP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_TCP, (st_data_t)rb_intern2("IPPROTO_TCP", 11));
+#endif
+#ifdef IPPROTO_GGP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_GGP, (st_data_t)rb_intern2("IPPROTO_GGP", 11));
+#endif
+#ifdef IPPROTO_IGMP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_IGMP, (st_data_t)rb_intern2("IPPROTO_IGMP", 12));
+#endif
+#ifdef IPPROTO_ICMP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ICMP, (st_data_t)rb_intern2("IPPROTO_ICMP", 12));
+#endif
+#ifdef IPPROTO_IP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_IP, (st_data_t)rb_intern2("IPPROTO_IP", 10));
+#endif
+#ifdef SOL_SOCKET
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)SOL_SOCKET, (st_data_t)rb_intern2("SOL_SOCKET", 10));
+#endif
+#ifdef IPPROTO_MAX
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_MAX, (st_data_t)rb_intern2("MAX", 3));
+#endif
+#ifdef IPPROTO_RAW
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_RAW, (st_data_t)rb_intern2("RAW", 3));
+#endif
+#ifdef IPPROTO_ROUTING
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ROUTING, (st_data_t)rb_intern2("ROUTING", 7));
+#endif
+#ifdef IPPROTO_NONE
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_NONE, (st_data_t)rb_intern2("NONE", 4));
+#endif
+#ifdef IPPROTO_IPV6
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_IPV6, (st_data_t)rb_intern2("IPV6", 4));
+#endif
+#ifdef IPPROTO_ICMPV6
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ICMPV6, (st_data_t)rb_intern2("ICMPV6", 6));
+#endif
+#ifdef IPPROTO_HOPOPTS
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_HOPOPTS, (st_data_t)rb_intern2("HOPOPTS", 7));
+#endif
+#ifdef IPPROTO_FRAGMENT
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_FRAGMENT, (st_data_t)rb_intern2("FRAGMENT", 8));
+#endif
+#ifdef IPPROTO_ESP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ESP, (st_data_t)rb_intern2("ESP", 3));
+#endif
+#ifdef IPPROTO_DSTOPTS
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_DSTOPTS, (st_data_t)rb_intern2("DSTOPTS", 7));
+#endif
+#ifdef IPPROTO_AH
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_AH, (st_data_t)rb_intern2("AH", 2));
+#endif
+#ifdef IPPROTO_BIP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_BIP, (st_data_t)rb_intern2("BIP", 3));
+#endif
+#ifdef IPPROTO_EON
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_EON, (st_data_t)rb_intern2("EON", 3));
+#endif
+#ifdef IPPROTO_XTP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_XTP, (st_data_t)rb_intern2("XTP", 3));
+#endif
+#ifdef IPPROTO_TP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_TP, (st_data_t)rb_intern2("TP", 2));
+#endif
+#ifdef IPPROTO_ND
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ND, (st_data_t)rb_intern2("ND", 2));
+#endif
+#ifdef IPPROTO_HELLO
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_HELLO, (st_data_t)rb_intern2("HELLO", 5));
+#endif
+#ifdef IPPROTO_IDP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_IDP, (st_data_t)rb_intern2("IDP", 3));
+#endif
+#ifdef IPPROTO_UDP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_UDP, (st_data_t)rb_intern2("UDP", 3));
+#endif
+#ifdef IPPROTO_PUP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_PUP, (st_data_t)rb_intern2("PUP", 3));
+#endif
+#ifdef IPPROTO_EGP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_EGP, (st_data_t)rb_intern2("EGP", 3));
+#endif
+#ifdef IPPROTO_TCP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_TCP, (st_data_t)rb_intern2("TCP", 3));
+#endif
+#ifdef IPPROTO_GGP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_GGP, (st_data_t)rb_intern2("GGP", 3));
+#endif
+#ifdef IPPROTO_IGMP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_IGMP, (st_data_t)rb_intern2("IGMP", 4));
+#endif
+#ifdef IPPROTO_ICMP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_ICMP, (st_data_t)rb_intern2("ICMP", 4));
+#endif
+#ifdef IPPROTO_IP
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)IPPROTO_IP, (st_data_t)rb_intern2("IP", 2));
+#endif
+#ifdef SOL_SOCKET
+ st_insert(rsock_intern_iplevel_hash, (st_data_t)SOL_SOCKET, (st_data_t)rb_intern2("SOCKET", 6));
+#endif
+
+ rsock_intern_so_optname_hash = st_init_numtable();
+#ifdef SO_BPF_EXTENSIONS
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BPF_EXTENSIONS, (st_data_t)rb_intern2("SO_BPF_EXTENSIONS", 17));
+#endif
+#ifdef SO_MAX_PACING_RATE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_MAX_PACING_RATE, (st_data_t)rb_intern2("SO_MAX_PACING_RATE", 18));
+#endif
+#ifdef SO_BUSY_POLL
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BUSY_POLL, (st_data_t)rb_intern2("SO_BUSY_POLL", 12));
+#endif
+#ifdef SO_SELECT_ERR_QUEUE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SELECT_ERR_QUEUE, (st_data_t)rb_intern2("SO_SELECT_ERR_QUEUE", 19));
+#endif
+#ifdef SO_LOCK_FILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_LOCK_FILTER, (st_data_t)rb_intern2("SO_LOCK_FILTER", 14));
+#endif
+#ifdef SO_NOFCS
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NOFCS, (st_data_t)rb_intern2("SO_NOFCS", 8));
+#endif
+#ifdef SO_PEEK_OFF
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PEEK_OFF, (st_data_t)rb_intern2("SO_PEEK_OFF", 11));
+#endif
+#ifdef SO_WIFI_STATUS
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_WIFI_STATUS, (st_data_t)rb_intern2("SO_WIFI_STATUS", 14));
+#endif
+#ifdef SO_RXQ_OVFL
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RXQ_OVFL, (st_data_t)rb_intern2("SO_RXQ_OVFL", 11));
+#endif
+#ifdef SO_DOMAIN
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DOMAIN, (st_data_t)rb_intern2("SO_DOMAIN", 9));
+#endif
+#ifdef SO_PROTOCOL
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PROTOCOL, (st_data_t)rb_intern2("SO_PROTOCOL", 11));
+#endif
+#ifdef SO_TIMESTAMPING
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_TIMESTAMPING, (st_data_t)rb_intern2("SO_TIMESTAMPING", 15));
+#endif
+#ifdef SO_MARK
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_MARK, (st_data_t)rb_intern2("SO_MARK", 7));
+#endif
+#ifdef SO_PASSSEC
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PASSSEC, (st_data_t)rb_intern2("SO_PASSSEC", 10));
+#endif
+#ifdef SO_PEERSEC
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PEERSEC, (st_data_t)rb_intern2("SO_PEERSEC", 10));
+#endif
+#ifdef SO_ALLZONES
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ALLZONES, (st_data_t)rb_intern2("SO_ALLZONES", 11));
+#endif
+#ifdef SO_MAC_EXEMPT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_MAC_EXEMPT, (st_data_t)rb_intern2("SO_MAC_EXEMPT", 13));
+#endif
+#ifdef SO_RECVUCRED
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RECVUCRED, (st_data_t)rb_intern2("SO_RECVUCRED", 12));
+#endif
+#ifdef SO_BINTIME
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BINTIME, (st_data_t)rb_intern2("SO_BINTIME", 10));
+#endif
+#ifdef SO_TIMESTAMPNS
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_TIMESTAMPNS, (st_data_t)rb_intern2("SO_TIMESTAMPNS", 14));
+#endif
+#ifdef SO_TIMESTAMP
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_TIMESTAMP, (st_data_t)rb_intern2("SO_TIMESTAMP", 12));
+#endif
+#ifdef SO_PEERNAME
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PEERNAME, (st_data_t)rb_intern2("SO_PEERNAME", 11));
+#endif
+#ifdef SO_GET_FILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_GET_FILTER, (st_data_t)rb_intern2("SO_GET_FILTER", 13));
+#endif
+#ifdef SO_DETACH_FILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DETACH_FILTER, (st_data_t)rb_intern2("SO_DETACH_FILTER", 16));
+#endif
+#ifdef SO_ATTACH_FILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ATTACH_FILTER, (st_data_t)rb_intern2("SO_ATTACH_FILTER", 16));
+#endif
+#ifdef SO_BINDTODEVICE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BINDTODEVICE, (st_data_t)rb_intern2("SO_BINDTODEVICE", 15));
+#endif
+#ifdef SO_SECURITY_ENCRYPTION_NETWORK
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SECURITY_ENCRYPTION_NETWORK, (st_data_t)rb_intern2("SO_SECURITY_ENCRYPTION_NETWORK", 30));
+#endif
+#ifdef SO_SECURITY_ENCRYPTION_TRANSPORT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SECURITY_ENCRYPTION_TRANSPORT, (st_data_t)rb_intern2("SO_SECURITY_ENCRYPTION_TRANSPORT", 32));
+#endif
+#ifdef SO_SECURITY_AUTHENTICATION
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SECURITY_AUTHENTICATION, (st_data_t)rb_intern2("SO_SECURITY_AUTHENTICATION", 26));
+#endif
+#ifdef SO_NOSIGPIPE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NOSIGPIPE, (st_data_t)rb_intern2("SO_NOSIGPIPE", 12));
+#endif
+#ifdef SO_NKE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NKE, (st_data_t)rb_intern2("SO_NKE", 6));
+#endif
+#ifdef SO_NREAD
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NREAD, (st_data_t)rb_intern2("SO_NREAD", 8));
+#endif
+#ifdef SO_WANTOOBFLAG
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_WANTOOBFLAG, (st_data_t)rb_intern2("SO_WANTOOBFLAG", 14));
+#endif
+#ifdef SO_WANTMORE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_WANTMORE, (st_data_t)rb_intern2("SO_WANTMORE", 11));
+#endif
+#ifdef SO_DONTTRUNC
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DONTTRUNC, (st_data_t)rb_intern2("SO_DONTTRUNC", 12));
+#endif
+#ifdef SO_ACCEPTFILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ACCEPTFILTER, (st_data_t)rb_intern2("SO_ACCEPTFILTER", 15));
+#endif
+#ifdef SO_USELOOPBACK
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_USELOOPBACK, (st_data_t)rb_intern2("SO_USELOOPBACK", 14));
+#endif
+#ifdef SO_ACCEPTCONN
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ACCEPTCONN, (st_data_t)rb_intern2("SO_ACCEPTCONN", 13));
+#endif
+#ifdef SO_SNDTIMEO
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SNDTIMEO, (st_data_t)rb_intern2("SO_SNDTIMEO", 11));
+#endif
+#ifdef SO_RCVTIMEO
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RCVTIMEO, (st_data_t)rb_intern2("SO_RCVTIMEO", 11));
+#endif
+#ifdef SO_SNDLOWAT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SNDLOWAT, (st_data_t)rb_intern2("SO_SNDLOWAT", 11));
+#endif
+#ifdef SO_RCVLOWAT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RCVLOWAT, (st_data_t)rb_intern2("SO_RCVLOWAT", 11));
+#endif
+#ifdef SO_PEERCRED
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PEERCRED, (st_data_t)rb_intern2("SO_PEERCRED", 11));
+#endif
+#ifdef SO_PASSCRED
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PASSCRED, (st_data_t)rb_intern2("SO_PASSCRED", 11));
+#endif
+#ifdef SO_LINGER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_LINGER, (st_data_t)rb_intern2("SO_LINGER", 9));
+#endif
+#ifdef SO_PRIORITY
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PRIORITY, (st_data_t)rb_intern2("SO_PRIORITY", 11));
+#endif
+#ifdef SO_NO_CHECK
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NO_CHECK, (st_data_t)rb_intern2("SO_NO_CHECK", 11));
+#endif
+#ifdef SO_OOBINLINE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_OOBINLINE, (st_data_t)rb_intern2("SO_OOBINLINE", 12));
+#endif
+#ifdef SO_KEEPALIVE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_KEEPALIVE, (st_data_t)rb_intern2("SO_KEEPALIVE", 12));
+#endif
+#ifdef SO_RCVBUFFORCE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RCVBUFFORCE, (st_data_t)rb_intern2("SO_RCVBUFFORCE", 14));
+#endif
+#ifdef SO_SNDBUFFORCE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SNDBUFFORCE, (st_data_t)rb_intern2("SO_SNDBUFFORCE", 14));
+#endif
+#ifdef SO_RCVBUF
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RCVBUF, (st_data_t)rb_intern2("SO_RCVBUF", 9));
+#endif
+#ifdef SO_SNDBUF
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SNDBUF, (st_data_t)rb_intern2("SO_SNDBUF", 9));
+#endif
+#ifdef SO_BROADCAST
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BROADCAST, (st_data_t)rb_intern2("SO_BROADCAST", 12));
+#endif
+#ifdef SO_DONTROUTE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DONTROUTE, (st_data_t)rb_intern2("SO_DONTROUTE", 12));
+#endif
+#ifdef SO_ERROR
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ERROR, (st_data_t)rb_intern2("SO_ERROR", 8));
+#endif
+#ifdef SO_TYPE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_TYPE, (st_data_t)rb_intern2("SO_TYPE", 7));
+#endif
+#ifdef SO_REUSEPORT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_REUSEPORT, (st_data_t)rb_intern2("SO_REUSEPORT", 12));
+#endif
+#ifdef SO_REUSEADDR
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_REUSEADDR, (st_data_t)rb_intern2("SO_REUSEADDR", 12));
+#endif
+#ifdef SO_DEBUG
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DEBUG, (st_data_t)rb_intern2("SO_DEBUG", 8));
+#endif
+#ifdef SO_BPF_EXTENSIONS
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BPF_EXTENSIONS, (st_data_t)rb_intern2("BPF_EXTENSIONS", 14));
+#endif
+#ifdef SO_MAX_PACING_RATE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_MAX_PACING_RATE, (st_data_t)rb_intern2("MAX_PACING_RATE", 15));
+#endif
+#ifdef SO_BUSY_POLL
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BUSY_POLL, (st_data_t)rb_intern2("BUSY_POLL", 9));
+#endif
+#ifdef SO_SELECT_ERR_QUEUE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SELECT_ERR_QUEUE, (st_data_t)rb_intern2("SELECT_ERR_QUEUE", 16));
+#endif
+#ifdef SO_LOCK_FILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_LOCK_FILTER, (st_data_t)rb_intern2("LOCK_FILTER", 11));
+#endif
+#ifdef SO_NOFCS
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NOFCS, (st_data_t)rb_intern2("NOFCS", 5));
+#endif
+#ifdef SO_PEEK_OFF
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PEEK_OFF, (st_data_t)rb_intern2("PEEK_OFF", 8));
+#endif
+#ifdef SO_WIFI_STATUS
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_WIFI_STATUS, (st_data_t)rb_intern2("WIFI_STATUS", 11));
+#endif
+#ifdef SO_RXQ_OVFL
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RXQ_OVFL, (st_data_t)rb_intern2("RXQ_OVFL", 8));
+#endif
+#ifdef SO_DOMAIN
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DOMAIN, (st_data_t)rb_intern2("DOMAIN", 6));
+#endif
+#ifdef SO_PROTOCOL
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PROTOCOL, (st_data_t)rb_intern2("PROTOCOL", 8));
+#endif
+#ifdef SO_TIMESTAMPING
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_TIMESTAMPING, (st_data_t)rb_intern2("TIMESTAMPING", 12));
+#endif
+#ifdef SO_MARK
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_MARK, (st_data_t)rb_intern2("MARK", 4));
+#endif
+#ifdef SO_PASSSEC
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PASSSEC, (st_data_t)rb_intern2("PASSSEC", 7));
+#endif
+#ifdef SO_PEERSEC
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PEERSEC, (st_data_t)rb_intern2("PEERSEC", 7));
+#endif
+#ifdef SO_ALLZONES
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ALLZONES, (st_data_t)rb_intern2("ALLZONES", 8));
+#endif
+#ifdef SO_MAC_EXEMPT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_MAC_EXEMPT, (st_data_t)rb_intern2("MAC_EXEMPT", 10));
+#endif
+#ifdef SO_RECVUCRED
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RECVUCRED, (st_data_t)rb_intern2("RECVUCRED", 9));
+#endif
+#ifdef SO_BINTIME
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BINTIME, (st_data_t)rb_intern2("BINTIME", 7));
+#endif
+#ifdef SO_TIMESTAMPNS
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_TIMESTAMPNS, (st_data_t)rb_intern2("TIMESTAMPNS", 11));
+#endif
+#ifdef SO_TIMESTAMP
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_TIMESTAMP, (st_data_t)rb_intern2("TIMESTAMP", 9));
+#endif
+#ifdef SO_PEERNAME
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PEERNAME, (st_data_t)rb_intern2("PEERNAME", 8));
+#endif
+#ifdef SO_GET_FILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_GET_FILTER, (st_data_t)rb_intern2("GET_FILTER", 10));
+#endif
+#ifdef SO_DETACH_FILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DETACH_FILTER, (st_data_t)rb_intern2("DETACH_FILTER", 13));
+#endif
+#ifdef SO_ATTACH_FILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ATTACH_FILTER, (st_data_t)rb_intern2("ATTACH_FILTER", 13));
+#endif
+#ifdef SO_BINDTODEVICE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BINDTODEVICE, (st_data_t)rb_intern2("BINDTODEVICE", 12));
+#endif
+#ifdef SO_SECURITY_ENCRYPTION_NETWORK
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SECURITY_ENCRYPTION_NETWORK, (st_data_t)rb_intern2("SECURITY_ENCRYPTION_NETWORK", 27));
+#endif
+#ifdef SO_SECURITY_ENCRYPTION_TRANSPORT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SECURITY_ENCRYPTION_TRANSPORT, (st_data_t)rb_intern2("SECURITY_ENCRYPTION_TRANSPORT", 29));
+#endif
+#ifdef SO_SECURITY_AUTHENTICATION
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SECURITY_AUTHENTICATION, (st_data_t)rb_intern2("SECURITY_AUTHENTICATION", 23));
+#endif
+#ifdef SO_NOSIGPIPE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NOSIGPIPE, (st_data_t)rb_intern2("NOSIGPIPE", 9));
+#endif
+#ifdef SO_NKE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NKE, (st_data_t)rb_intern2("NKE", 3));
+#endif
+#ifdef SO_NREAD
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NREAD, (st_data_t)rb_intern2("NREAD", 5));
+#endif
+#ifdef SO_WANTOOBFLAG
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_WANTOOBFLAG, (st_data_t)rb_intern2("WANTOOBFLAG", 11));
+#endif
+#ifdef SO_WANTMORE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_WANTMORE, (st_data_t)rb_intern2("WANTMORE", 8));
+#endif
+#ifdef SO_DONTTRUNC
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DONTTRUNC, (st_data_t)rb_intern2("DONTTRUNC", 9));
+#endif
+#ifdef SO_ACCEPTFILTER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ACCEPTFILTER, (st_data_t)rb_intern2("ACCEPTFILTER", 12));
+#endif
+#ifdef SO_USELOOPBACK
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_USELOOPBACK, (st_data_t)rb_intern2("USELOOPBACK", 11));
+#endif
+#ifdef SO_ACCEPTCONN
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ACCEPTCONN, (st_data_t)rb_intern2("ACCEPTCONN", 10));
+#endif
+#ifdef SO_SNDTIMEO
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SNDTIMEO, (st_data_t)rb_intern2("SNDTIMEO", 8));
+#endif
+#ifdef SO_RCVTIMEO
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RCVTIMEO, (st_data_t)rb_intern2("RCVTIMEO", 8));
+#endif
+#ifdef SO_SNDLOWAT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SNDLOWAT, (st_data_t)rb_intern2("SNDLOWAT", 8));
+#endif
+#ifdef SO_RCVLOWAT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RCVLOWAT, (st_data_t)rb_intern2("RCVLOWAT", 8));
+#endif
+#ifdef SO_PEERCRED
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PEERCRED, (st_data_t)rb_intern2("PEERCRED", 8));
+#endif
+#ifdef SO_PASSCRED
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PASSCRED, (st_data_t)rb_intern2("PASSCRED", 8));
+#endif
+#ifdef SO_LINGER
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_LINGER, (st_data_t)rb_intern2("LINGER", 6));
+#endif
+#ifdef SO_PRIORITY
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_PRIORITY, (st_data_t)rb_intern2("PRIORITY", 8));
+#endif
+#ifdef SO_NO_CHECK
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_NO_CHECK, (st_data_t)rb_intern2("NO_CHECK", 8));
+#endif
+#ifdef SO_OOBINLINE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_OOBINLINE, (st_data_t)rb_intern2("OOBINLINE", 9));
+#endif
+#ifdef SO_KEEPALIVE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_KEEPALIVE, (st_data_t)rb_intern2("KEEPALIVE", 9));
+#endif
+#ifdef SO_RCVBUFFORCE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RCVBUFFORCE, (st_data_t)rb_intern2("RCVBUFFORCE", 11));
+#endif
+#ifdef SO_SNDBUFFORCE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SNDBUFFORCE, (st_data_t)rb_intern2("SNDBUFFORCE", 11));
+#endif
+#ifdef SO_RCVBUF
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_RCVBUF, (st_data_t)rb_intern2("RCVBUF", 6));
+#endif
+#ifdef SO_SNDBUF
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_SNDBUF, (st_data_t)rb_intern2("SNDBUF", 6));
+#endif
+#ifdef SO_BROADCAST
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_BROADCAST, (st_data_t)rb_intern2("BROADCAST", 9));
+#endif
+#ifdef SO_DONTROUTE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DONTROUTE, (st_data_t)rb_intern2("DONTROUTE", 9));
+#endif
+#ifdef SO_ERROR
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_ERROR, (st_data_t)rb_intern2("ERROR", 5));
+#endif
+#ifdef SO_TYPE
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_TYPE, (st_data_t)rb_intern2("TYPE", 4));
+#endif
+#ifdef SO_REUSEPORT
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_REUSEPORT, (st_data_t)rb_intern2("REUSEPORT", 9));
+#endif
+#ifdef SO_REUSEADDR
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_REUSEADDR, (st_data_t)rb_intern2("REUSEADDR", 9));
+#endif
+#ifdef SO_DEBUG
+ st_insert(rsock_intern_so_optname_hash, (st_data_t)SO_DEBUG, (st_data_t)rb_intern2("DEBUG", 5));
+#endif
+
+ rsock_intern_ip_optname_hash = st_init_numtable();
+#ifdef IP_MSFILTER
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MSFILTER, (st_data_t)rb_intern2("IP_MSFILTER", 11));
+#endif
+#ifdef IP_DROP_SOURCE_MEMBERSHIP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DROP_SOURCE_MEMBERSHIP, (st_data_t)rb_intern2("IP_DROP_SOURCE_MEMBERSHIP", 25));
+#endif
+#ifdef IP_ADD_SOURCE_MEMBERSHIP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_ADD_SOURCE_MEMBERSHIP, (st_data_t)rb_intern2("IP_ADD_SOURCE_MEMBERSHIP", 24));
+#endif
+#ifdef IP_BLOCK_SOURCE
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_BLOCK_SOURCE, (st_data_t)rb_intern2("IP_BLOCK_SOURCE", 15));
+#endif
+#ifdef IP_UNBLOCK_SOURCE
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_UNBLOCK_SOURCE, (st_data_t)rb_intern2("IP_UNBLOCK_SOURCE", 17));
+#endif
+#ifdef IP_PMTUDISC_DO
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PMTUDISC_DO, (st_data_t)rb_intern2("IP_PMTUDISC_DO", 14));
+#endif
+#ifdef IP_PMTUDISC_WANT
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PMTUDISC_WANT, (st_data_t)rb_intern2("IP_PMTUDISC_WANT", 16));
+#endif
+#ifdef IP_PMTUDISC_DONT
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PMTUDISC_DONT, (st_data_t)rb_intern2("IP_PMTUDISC_DONT", 16));
+#endif
+#ifdef IP_TRANSPARENT
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_TRANSPARENT, (st_data_t)rb_intern2("IP_TRANSPARENT", 14));
+#endif
+#ifdef IP_PASSSEC
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PASSSEC, (st_data_t)rb_intern2("IP_PASSSEC", 10));
+#endif
+#ifdef IP_XFRM_POLICY
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_XFRM_POLICY, (st_data_t)rb_intern2("IP_XFRM_POLICY", 14));
+#endif
+#ifdef IP_IPSEC_POLICY
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_IPSEC_POLICY, (st_data_t)rb_intern2("IP_IPSEC_POLICY", 15));
+#endif
+#ifdef IP_FREEBIND
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_FREEBIND, (st_data_t)rb_intern2("IP_FREEBIND", 11));
+#endif
+#ifdef IP_MTU
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MTU, (st_data_t)rb_intern2("IP_MTU", 6));
+#endif
+#ifdef IP_RECVTOS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVTOS, (st_data_t)rb_intern2("IP_RECVTOS", 10));
+#endif
+#ifdef IP_RECVERR
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVERR, (st_data_t)rb_intern2("IP_RECVERR", 10));
+#endif
+#ifdef IP_MTU_DISCOVER
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MTU_DISCOVER, (st_data_t)rb_intern2("IP_MTU_DISCOVER", 15));
+#endif
+#ifdef IP_PKTOPTIONS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PKTOPTIONS, (st_data_t)rb_intern2("IP_PKTOPTIONS", 13));
+#endif
+#ifdef IP_PKTINFO
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PKTINFO, (st_data_t)rb_intern2("IP_PKTINFO", 10));
+#endif
+#ifdef IP_ROUTER_ALERT
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_ROUTER_ALERT, (st_data_t)rb_intern2("IP_ROUTER_ALERT", 15));
+#endif
+#ifdef IP_MAX_MEMBERSHIPS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MAX_MEMBERSHIPS, (st_data_t)rb_intern2("IP_MAX_MEMBERSHIPS", 18));
+#endif
+#ifdef IP_DEFAULT_MULTICAST_LOOP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DEFAULT_MULTICAST_LOOP, (st_data_t)rb_intern2("IP_DEFAULT_MULTICAST_LOOP", 25));
+#endif
+#ifdef IP_DEFAULT_MULTICAST_TTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DEFAULT_MULTICAST_TTL, (st_data_t)rb_intern2("IP_DEFAULT_MULTICAST_TTL", 24));
+#endif
+#ifdef IP_DROP_MEMBERSHIP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DROP_MEMBERSHIP, (st_data_t)rb_intern2("IP_DROP_MEMBERSHIP", 18));
+#endif
+#ifdef IP_ADD_MEMBERSHIP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_ADD_MEMBERSHIP, (st_data_t)rb_intern2("IP_ADD_MEMBERSHIP", 17));
+#endif
+#ifdef IP_MULTICAST_LOOP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MULTICAST_LOOP, (st_data_t)rb_intern2("IP_MULTICAST_LOOP", 17));
+#endif
+#ifdef IP_MULTICAST_TTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MULTICAST_TTL, (st_data_t)rb_intern2("IP_MULTICAST_TTL", 16));
+#endif
+#ifdef IP_MULTICAST_IF
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MULTICAST_IF, (st_data_t)rb_intern2("IP_MULTICAST_IF", 15));
+#endif
+#ifdef IP_PORTRANGE
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PORTRANGE, (st_data_t)rb_intern2("IP_PORTRANGE", 12));
+#endif
+#ifdef IP_RECVSLLA
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVSLLA, (st_data_t)rb_intern2("IP_RECVSLLA", 11));
+#endif
+#ifdef IP_RECVIF
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVIF, (st_data_t)rb_intern2("IP_RECVIF", 9));
+#endif
+#ifdef IP_RECVTTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVTTL, (st_data_t)rb_intern2("IP_RECVTTL", 10));
+#endif
+#ifdef IP_ONESBCAST
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_ONESBCAST, (st_data_t)rb_intern2("IP_ONESBCAST", 12));
+#endif
+#ifdef IP_SENDSRCADDR
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_SENDSRCADDR, (st_data_t)rb_intern2("IP_SENDSRCADDR", 14));
+#endif
+#ifdef IP_DONTFRAG
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DONTFRAG, (st_data_t)rb_intern2("IP_DONTFRAG", 11));
+#endif
+#ifdef IP_MINTTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MINTTL, (st_data_t)rb_intern2("IP_MINTTL", 9));
+#endif
+#ifdef IP_RETOPTS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RETOPTS, (st_data_t)rb_intern2("IP_RETOPTS", 10));
+#endif
+#ifdef IP_RECVDSTADDR
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVDSTADDR, (st_data_t)rb_intern2("IP_RECVDSTADDR", 14));
+#endif
+#ifdef IP_RECVRETOPTS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVRETOPTS, (st_data_t)rb_intern2("IP_RECVRETOPTS", 14));
+#endif
+#ifdef IP_RECVOPTS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVOPTS, (st_data_t)rb_intern2("IP_RECVOPTS", 11));
+#endif
+#ifdef IP_TTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_TTL, (st_data_t)rb_intern2("IP_TTL", 6));
+#endif
+#ifdef IP_TOS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_TOS, (st_data_t)rb_intern2("IP_TOS", 6));
+#endif
+#ifdef IP_HDRINCL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_HDRINCL, (st_data_t)rb_intern2("IP_HDRINCL", 10));
+#endif
+#ifdef IP_OPTIONS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_OPTIONS, (st_data_t)rb_intern2("IP_OPTIONS", 10));
+#endif
+#ifdef IP_MSFILTER
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MSFILTER, (st_data_t)rb_intern2("MSFILTER", 8));
+#endif
+#ifdef IP_DROP_SOURCE_MEMBERSHIP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DROP_SOURCE_MEMBERSHIP, (st_data_t)rb_intern2("DROP_SOURCE_MEMBERSHIP", 22));
+#endif
+#ifdef IP_ADD_SOURCE_MEMBERSHIP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_ADD_SOURCE_MEMBERSHIP, (st_data_t)rb_intern2("ADD_SOURCE_MEMBERSHIP", 21));
+#endif
+#ifdef IP_BLOCK_SOURCE
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_BLOCK_SOURCE, (st_data_t)rb_intern2("BLOCK_SOURCE", 12));
+#endif
+#ifdef IP_UNBLOCK_SOURCE
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_UNBLOCK_SOURCE, (st_data_t)rb_intern2("UNBLOCK_SOURCE", 14));
+#endif
+#ifdef IP_PMTUDISC_DO
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PMTUDISC_DO, (st_data_t)rb_intern2("PMTUDISC_DO", 11));
+#endif
+#ifdef IP_PMTUDISC_WANT
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PMTUDISC_WANT, (st_data_t)rb_intern2("PMTUDISC_WANT", 13));
+#endif
+#ifdef IP_PMTUDISC_DONT
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PMTUDISC_DONT, (st_data_t)rb_intern2("PMTUDISC_DONT", 13));
+#endif
+#ifdef IP_TRANSPARENT
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_TRANSPARENT, (st_data_t)rb_intern2("TRANSPARENT", 11));
+#endif
+#ifdef IP_PASSSEC
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PASSSEC, (st_data_t)rb_intern2("PASSSEC", 7));
+#endif
+#ifdef IP_XFRM_POLICY
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_XFRM_POLICY, (st_data_t)rb_intern2("XFRM_POLICY", 11));
+#endif
+#ifdef IP_IPSEC_POLICY
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_IPSEC_POLICY, (st_data_t)rb_intern2("IPSEC_POLICY", 12));
+#endif
+#ifdef IP_FREEBIND
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_FREEBIND, (st_data_t)rb_intern2("FREEBIND", 8));
+#endif
+#ifdef IP_MTU
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MTU, (st_data_t)rb_intern2("MTU", 3));
+#endif
+#ifdef IP_RECVTOS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVTOS, (st_data_t)rb_intern2("RECVTOS", 7));
+#endif
+#ifdef IP_RECVERR
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVERR, (st_data_t)rb_intern2("RECVERR", 7));
+#endif
+#ifdef IP_MTU_DISCOVER
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MTU_DISCOVER, (st_data_t)rb_intern2("MTU_DISCOVER", 12));
+#endif
+#ifdef IP_PKTOPTIONS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PKTOPTIONS, (st_data_t)rb_intern2("PKTOPTIONS", 10));
+#endif
+#ifdef IP_PKTINFO
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PKTINFO, (st_data_t)rb_intern2("PKTINFO", 7));
+#endif
+#ifdef IP_ROUTER_ALERT
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_ROUTER_ALERT, (st_data_t)rb_intern2("ROUTER_ALERT", 12));
+#endif
+#ifdef IP_MAX_MEMBERSHIPS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MAX_MEMBERSHIPS, (st_data_t)rb_intern2("MAX_MEMBERSHIPS", 15));
+#endif
+#ifdef IP_DEFAULT_MULTICAST_LOOP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DEFAULT_MULTICAST_LOOP, (st_data_t)rb_intern2("DEFAULT_MULTICAST_LOOP", 22));
+#endif
+#ifdef IP_DEFAULT_MULTICAST_TTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DEFAULT_MULTICAST_TTL, (st_data_t)rb_intern2("DEFAULT_MULTICAST_TTL", 21));
+#endif
+#ifdef IP_DROP_MEMBERSHIP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DROP_MEMBERSHIP, (st_data_t)rb_intern2("DROP_MEMBERSHIP", 15));
+#endif
+#ifdef IP_ADD_MEMBERSHIP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_ADD_MEMBERSHIP, (st_data_t)rb_intern2("ADD_MEMBERSHIP", 14));
+#endif
+#ifdef IP_MULTICAST_LOOP
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MULTICAST_LOOP, (st_data_t)rb_intern2("MULTICAST_LOOP", 14));
+#endif
+#ifdef IP_MULTICAST_TTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MULTICAST_TTL, (st_data_t)rb_intern2("MULTICAST_TTL", 13));
+#endif
+#ifdef IP_MULTICAST_IF
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MULTICAST_IF, (st_data_t)rb_intern2("MULTICAST_IF", 12));
+#endif
+#ifdef IP_PORTRANGE
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_PORTRANGE, (st_data_t)rb_intern2("PORTRANGE", 9));
+#endif
+#ifdef IP_RECVSLLA
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVSLLA, (st_data_t)rb_intern2("RECVSLLA", 8));
+#endif
+#ifdef IP_RECVIF
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVIF, (st_data_t)rb_intern2("RECVIF", 6));
+#endif
+#ifdef IP_RECVTTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVTTL, (st_data_t)rb_intern2("RECVTTL", 7));
+#endif
+#ifdef IP_ONESBCAST
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_ONESBCAST, (st_data_t)rb_intern2("ONESBCAST", 9));
+#endif
+#ifdef IP_SENDSRCADDR
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_SENDSRCADDR, (st_data_t)rb_intern2("SENDSRCADDR", 11));
+#endif
+#ifdef IP_DONTFRAG
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_DONTFRAG, (st_data_t)rb_intern2("DONTFRAG", 8));
+#endif
+#ifdef IP_MINTTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_MINTTL, (st_data_t)rb_intern2("MINTTL", 6));
+#endif
+#ifdef IP_RETOPTS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RETOPTS, (st_data_t)rb_intern2("RETOPTS", 7));
+#endif
+#ifdef IP_RECVDSTADDR
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVDSTADDR, (st_data_t)rb_intern2("RECVDSTADDR", 11));
+#endif
+#ifdef IP_RECVRETOPTS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVRETOPTS, (st_data_t)rb_intern2("RECVRETOPTS", 11));
+#endif
+#ifdef IP_RECVOPTS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_RECVOPTS, (st_data_t)rb_intern2("RECVOPTS", 8));
+#endif
+#ifdef IP_TTL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_TTL, (st_data_t)rb_intern2("TTL", 3));
+#endif
+#ifdef IP_TOS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_TOS, (st_data_t)rb_intern2("TOS", 3));
+#endif
+#ifdef IP_HDRINCL
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_HDRINCL, (st_data_t)rb_intern2("HDRINCL", 7));
+#endif
+#ifdef IP_OPTIONS
+ st_insert(rsock_intern_ip_optname_hash, (st_data_t)IP_OPTIONS, (st_data_t)rb_intern2("OPTIONS", 7));
+#endif
+
+ rsock_intern_ipv6_optname_hash = st_init_numtable();
+#ifdef IPV6_USE_MIN_MTU
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_USE_MIN_MTU, (st_data_t)rb_intern2("IPV6_USE_MIN_MTU", 16));
+#endif
+#ifdef IPV6_TCLASS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_TCLASS, (st_data_t)rb_intern2("IPV6_TCLASS", 11));
+#endif
+#ifdef IPV6_RECVPATHMTU
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVPATHMTU, (st_data_t)rb_intern2("IPV6_RECVPATHMTU", 16));
+#endif
+#ifdef IPV6_RTHDR_TYPE_0
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RTHDR_TYPE_0, (st_data_t)rb_intern2("IPV6_RTHDR_TYPE_0", 17));
+#endif
+#ifdef IPV6_RTHDRDSTOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RTHDRDSTOPTS, (st_data_t)rb_intern2("IPV6_RTHDRDSTOPTS", 17));
+#endif
+#ifdef IPV6_RTHDR
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RTHDR, (st_data_t)rb_intern2("IPV6_RTHDR", 10));
+#endif
+#ifdef IPV6_RECVTCLASS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVTCLASS, (st_data_t)rb_intern2("IPV6_RECVTCLASS", 15));
+#endif
+#ifdef IPV6_RECVRTHDR
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVRTHDR, (st_data_t)rb_intern2("IPV6_RECVRTHDR", 14));
+#endif
+#ifdef IPV6_RECVPKTINFO
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVPKTINFO, (st_data_t)rb_intern2("IPV6_RECVPKTINFO", 16));
+#endif
+#ifdef IPV6_RECVHOPOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVHOPOPTS, (st_data_t)rb_intern2("IPV6_RECVHOPOPTS", 16));
+#endif
+#ifdef IPV6_RECVHOPLIMIT
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVHOPLIMIT, (st_data_t)rb_intern2("IPV6_RECVHOPLIMIT", 17));
+#endif
+#ifdef IPV6_RECVDSTOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVDSTOPTS, (st_data_t)rb_intern2("IPV6_RECVDSTOPTS", 16));
+#endif
+#ifdef IPV6_PKTINFO
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_PKTINFO, (st_data_t)rb_intern2("IPV6_PKTINFO", 12));
+#endif
+#ifdef IPV6_PATHMTU
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_PATHMTU, (st_data_t)rb_intern2("IPV6_PATHMTU", 12));
+#endif
+#ifdef IPV6_NEXTHOP
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_NEXTHOP, (st_data_t)rb_intern2("IPV6_NEXTHOP", 12));
+#endif
+#ifdef IPV6_HOPOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_HOPOPTS, (st_data_t)rb_intern2("IPV6_HOPOPTS", 12));
+#endif
+#ifdef IPV6_HOPLIMIT
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_HOPLIMIT, (st_data_t)rb_intern2("IPV6_HOPLIMIT", 13));
+#endif
+#ifdef IPV6_DSTOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_DSTOPTS, (st_data_t)rb_intern2("IPV6_DSTOPTS", 12));
+#endif
+#ifdef IPV6_DONTFRAG
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_DONTFRAG, (st_data_t)rb_intern2("IPV6_DONTFRAG", 13));
+#endif
+#ifdef IPV6_CHECKSUM
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_CHECKSUM, (st_data_t)rb_intern2("IPV6_CHECKSUM", 13));
+#endif
+#ifdef IPV6_V6ONLY
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_V6ONLY, (st_data_t)rb_intern2("IPV6_V6ONLY", 11));
+#endif
+#ifdef IPV6_UNICAST_HOPS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_UNICAST_HOPS, (st_data_t)rb_intern2("IPV6_UNICAST_HOPS", 17));
+#endif
+#ifdef IPV6_MULTICAST_LOOP
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_MULTICAST_LOOP, (st_data_t)rb_intern2("IPV6_MULTICAST_LOOP", 19));
+#endif
+#ifdef IPV6_MULTICAST_IF
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_MULTICAST_IF, (st_data_t)rb_intern2("IPV6_MULTICAST_IF", 17));
+#endif
+#ifdef IPV6_MULTICAST_HOPS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_MULTICAST_HOPS, (st_data_t)rb_intern2("IPV6_MULTICAST_HOPS", 19));
+#endif
+#ifdef IPV6_LEAVE_GROUP
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_LEAVE_GROUP, (st_data_t)rb_intern2("IPV6_LEAVE_GROUP", 16));
+#endif
+#ifdef IPV6_JOIN_GROUP
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_JOIN_GROUP, (st_data_t)rb_intern2("IPV6_JOIN_GROUP", 15));
+#endif
+#ifdef IPV6_USE_MIN_MTU
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_USE_MIN_MTU, (st_data_t)rb_intern2("USE_MIN_MTU", 11));
+#endif
+#ifdef IPV6_TCLASS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_TCLASS, (st_data_t)rb_intern2("TCLASS", 6));
+#endif
+#ifdef IPV6_RECVPATHMTU
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVPATHMTU, (st_data_t)rb_intern2("RECVPATHMTU", 11));
+#endif
+#ifdef IPV6_RTHDR_TYPE_0
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RTHDR_TYPE_0, (st_data_t)rb_intern2("RTHDR_TYPE_0", 12));
+#endif
+#ifdef IPV6_RTHDRDSTOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RTHDRDSTOPTS, (st_data_t)rb_intern2("RTHDRDSTOPTS", 12));
+#endif
+#ifdef IPV6_RTHDR
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RTHDR, (st_data_t)rb_intern2("RTHDR", 5));
+#endif
+#ifdef IPV6_RECVTCLASS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVTCLASS, (st_data_t)rb_intern2("RECVTCLASS", 10));
+#endif
+#ifdef IPV6_RECVRTHDR
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVRTHDR, (st_data_t)rb_intern2("RECVRTHDR", 9));
+#endif
+#ifdef IPV6_RECVPKTINFO
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVPKTINFO, (st_data_t)rb_intern2("RECVPKTINFO", 11));
+#endif
+#ifdef IPV6_RECVHOPOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVHOPOPTS, (st_data_t)rb_intern2("RECVHOPOPTS", 11));
+#endif
+#ifdef IPV6_RECVHOPLIMIT
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVHOPLIMIT, (st_data_t)rb_intern2("RECVHOPLIMIT", 12));
+#endif
+#ifdef IPV6_RECVDSTOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_RECVDSTOPTS, (st_data_t)rb_intern2("RECVDSTOPTS", 11));
+#endif
+#ifdef IPV6_PKTINFO
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_PKTINFO, (st_data_t)rb_intern2("PKTINFO", 7));
+#endif
+#ifdef IPV6_PATHMTU
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_PATHMTU, (st_data_t)rb_intern2("PATHMTU", 7));
+#endif
+#ifdef IPV6_NEXTHOP
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_NEXTHOP, (st_data_t)rb_intern2("NEXTHOP", 7));
+#endif
+#ifdef IPV6_HOPOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_HOPOPTS, (st_data_t)rb_intern2("HOPOPTS", 7));
+#endif
+#ifdef IPV6_HOPLIMIT
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_HOPLIMIT, (st_data_t)rb_intern2("HOPLIMIT", 8));
+#endif
+#ifdef IPV6_DSTOPTS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_DSTOPTS, (st_data_t)rb_intern2("DSTOPTS", 7));
+#endif
+#ifdef IPV6_DONTFRAG
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_DONTFRAG, (st_data_t)rb_intern2("DONTFRAG", 8));
+#endif
+#ifdef IPV6_CHECKSUM
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_CHECKSUM, (st_data_t)rb_intern2("CHECKSUM", 8));
+#endif
+#ifdef IPV6_V6ONLY
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_V6ONLY, (st_data_t)rb_intern2("V6ONLY", 6));
+#endif
+#ifdef IPV6_UNICAST_HOPS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_UNICAST_HOPS, (st_data_t)rb_intern2("UNICAST_HOPS", 12));
+#endif
+#ifdef IPV6_MULTICAST_LOOP
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_MULTICAST_LOOP, (st_data_t)rb_intern2("MULTICAST_LOOP", 14));
+#endif
+#ifdef IPV6_MULTICAST_IF
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_MULTICAST_IF, (st_data_t)rb_intern2("MULTICAST_IF", 12));
+#endif
+#ifdef IPV6_MULTICAST_HOPS
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_MULTICAST_HOPS, (st_data_t)rb_intern2("MULTICAST_HOPS", 14));
+#endif
+#ifdef IPV6_LEAVE_GROUP
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_LEAVE_GROUP, (st_data_t)rb_intern2("LEAVE_GROUP", 11));
+#endif
+#ifdef IPV6_JOIN_GROUP
+ st_insert(rsock_intern_ipv6_optname_hash, (st_data_t)IPV6_JOIN_GROUP, (st_data_t)rb_intern2("JOIN_GROUP", 10));
+#endif
+
+ rsock_intern_tcp_optname_hash = st_init_numtable();
+#ifdef TCP_USER_TIMEOUT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_USER_TIMEOUT, (st_data_t)rb_intern2("TCP_USER_TIMEOUT", 16));
+#endif
+#ifdef TCP_TIMESTAMP
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_TIMESTAMP, (st_data_t)rb_intern2("TCP_TIMESTAMP", 13));
+#endif
+#ifdef TCP_THIN_LINEAR_TIMEOUTS
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_THIN_LINEAR_TIMEOUTS, (st_data_t)rb_intern2("TCP_THIN_LINEAR_TIMEOUTS", 24));
+#endif
+#ifdef TCP_THIN_DUPACK
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_THIN_DUPACK, (st_data_t)rb_intern2("TCP_THIN_DUPACK", 15));
+#endif
+#ifdef TCP_REPAIR_QUEUE
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_REPAIR_QUEUE, (st_data_t)rb_intern2("TCP_REPAIR_QUEUE", 16));
+#endif
+#ifdef TCP_REPAIR_OPTIONS
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_REPAIR_OPTIONS, (st_data_t)rb_intern2("TCP_REPAIR_OPTIONS", 18));
+#endif
+#ifdef TCP_REPAIR
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_REPAIR, (st_data_t)rb_intern2("TCP_REPAIR", 10));
+#endif
+#ifdef TCP_QUEUE_SEQ
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_QUEUE_SEQ, (st_data_t)rb_intern2("TCP_QUEUE_SEQ", 13));
+#endif
+#ifdef TCP_COOKIE_TRANSACTIONS
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_COOKIE_TRANSACTIONS, (st_data_t)rb_intern2("TCP_COOKIE_TRANSACTIONS", 23));
+#endif
+#ifdef TCP_CONGESTION
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_CONGESTION, (st_data_t)rb_intern2("TCP_CONGESTION", 14));
+#endif
+#ifdef TCP_FASTOPEN
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_FASTOPEN, (st_data_t)rb_intern2("TCP_FASTOPEN", 12));
+#endif
+#ifdef TCP_WINDOW_CLAMP
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_WINDOW_CLAMP, (st_data_t)rb_intern2("TCP_WINDOW_CLAMP", 16));
+#endif
+#ifdef TCP_SYNCNT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_SYNCNT, (st_data_t)rb_intern2("TCP_SYNCNT", 10));
+#endif
+#ifdef TCP_QUICKACK
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_QUICKACK, (st_data_t)rb_intern2("TCP_QUICKACK", 12));
+#endif
+#ifdef TCP_NOPUSH
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_NOPUSH, (st_data_t)rb_intern2("TCP_NOPUSH", 10));
+#endif
+#ifdef TCP_NOOPT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_NOOPT, (st_data_t)rb_intern2("TCP_NOOPT", 9));
+#endif
+#ifdef TCP_MD5SIG
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_MD5SIG, (st_data_t)rb_intern2("TCP_MD5SIG", 10));
+#endif
+#ifdef TCP_LINGER2
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_LINGER2, (st_data_t)rb_intern2("TCP_LINGER2", 11));
+#endif
+#ifdef TCP_KEEPINTVL
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_KEEPINTVL, (st_data_t)rb_intern2("TCP_KEEPINTVL", 13));
+#endif
+#ifdef TCP_KEEPIDLE
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_KEEPIDLE, (st_data_t)rb_intern2("TCP_KEEPIDLE", 12));
+#endif
+#ifdef TCP_KEEPCNT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_KEEPCNT, (st_data_t)rb_intern2("TCP_KEEPCNT", 11));
+#endif
+#ifdef TCP_INFO
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_INFO, (st_data_t)rb_intern2("TCP_INFO", 8));
+#endif
+#ifdef TCP_DEFER_ACCEPT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_DEFER_ACCEPT, (st_data_t)rb_intern2("TCP_DEFER_ACCEPT", 16));
+#endif
+#ifdef TCP_CORK
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_CORK, (st_data_t)rb_intern2("TCP_CORK", 8));
+#endif
+#ifdef TCP_MAXSEG
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_MAXSEG, (st_data_t)rb_intern2("TCP_MAXSEG", 10));
+#endif
+#ifdef TCP_NODELAY
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_NODELAY, (st_data_t)rb_intern2("TCP_NODELAY", 11));
+#endif
+#ifdef TCP_USER_TIMEOUT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_USER_TIMEOUT, (st_data_t)rb_intern2("USER_TIMEOUT", 12));
+#endif
+#ifdef TCP_TIMESTAMP
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_TIMESTAMP, (st_data_t)rb_intern2("TIMESTAMP", 9));
+#endif
+#ifdef TCP_THIN_LINEAR_TIMEOUTS
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_THIN_LINEAR_TIMEOUTS, (st_data_t)rb_intern2("THIN_LINEAR_TIMEOUTS", 20));
+#endif
+#ifdef TCP_THIN_DUPACK
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_THIN_DUPACK, (st_data_t)rb_intern2("THIN_DUPACK", 11));
+#endif
+#ifdef TCP_REPAIR_QUEUE
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_REPAIR_QUEUE, (st_data_t)rb_intern2("REPAIR_QUEUE", 12));
+#endif
+#ifdef TCP_REPAIR_OPTIONS
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_REPAIR_OPTIONS, (st_data_t)rb_intern2("REPAIR_OPTIONS", 14));
+#endif
+#ifdef TCP_REPAIR
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_REPAIR, (st_data_t)rb_intern2("REPAIR", 6));
+#endif
+#ifdef TCP_QUEUE_SEQ
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_QUEUE_SEQ, (st_data_t)rb_intern2("QUEUE_SEQ", 9));
+#endif
+#ifdef TCP_COOKIE_TRANSACTIONS
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_COOKIE_TRANSACTIONS, (st_data_t)rb_intern2("COOKIE_TRANSACTIONS", 19));
+#endif
+#ifdef TCP_CONGESTION
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_CONGESTION, (st_data_t)rb_intern2("CONGESTION", 10));
+#endif
+#ifdef TCP_FASTOPEN
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_FASTOPEN, (st_data_t)rb_intern2("FASTOPEN", 8));
+#endif
+#ifdef TCP_WINDOW_CLAMP
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_WINDOW_CLAMP, (st_data_t)rb_intern2("WINDOW_CLAMP", 12));
+#endif
+#ifdef TCP_SYNCNT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_SYNCNT, (st_data_t)rb_intern2("SYNCNT", 6));
+#endif
+#ifdef TCP_QUICKACK
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_QUICKACK, (st_data_t)rb_intern2("QUICKACK", 8));
+#endif
+#ifdef TCP_NOPUSH
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_NOPUSH, (st_data_t)rb_intern2("NOPUSH", 6));
+#endif
+#ifdef TCP_NOOPT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_NOOPT, (st_data_t)rb_intern2("NOOPT", 5));
+#endif
+#ifdef TCP_MD5SIG
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_MD5SIG, (st_data_t)rb_intern2("MD5SIG", 6));
+#endif
+#ifdef TCP_LINGER2
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_LINGER2, (st_data_t)rb_intern2("LINGER2", 7));
+#endif
+#ifdef TCP_KEEPINTVL
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_KEEPINTVL, (st_data_t)rb_intern2("KEEPINTVL", 9));
+#endif
+#ifdef TCP_KEEPIDLE
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_KEEPIDLE, (st_data_t)rb_intern2("KEEPIDLE", 8));
+#endif
+#ifdef TCP_KEEPCNT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_KEEPCNT, (st_data_t)rb_intern2("KEEPCNT", 7));
+#endif
+#ifdef TCP_INFO
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_INFO, (st_data_t)rb_intern2("INFO", 4));
+#endif
+#ifdef TCP_DEFER_ACCEPT
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_DEFER_ACCEPT, (st_data_t)rb_intern2("DEFER_ACCEPT", 12));
+#endif
+#ifdef TCP_CORK
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_CORK, (st_data_t)rb_intern2("CORK", 4));
+#endif
+#ifdef TCP_MAXSEG
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_MAXSEG, (st_data_t)rb_intern2("MAXSEG", 6));
+#endif
+#ifdef TCP_NODELAY
+ st_insert(rsock_intern_tcp_optname_hash, (st_data_t)TCP_NODELAY, (st_data_t)rb_intern2("NODELAY", 7));
+#endif
+
+ rsock_intern_udp_optname_hash = st_init_numtable();
+#ifdef UDP_CORK
+ st_insert(rsock_intern_udp_optname_hash, (st_data_t)UDP_CORK, (st_data_t)rb_intern2("UDP_CORK", 8));
+#endif
+#ifdef UDP_CORK
+ st_insert(rsock_intern_udp_optname_hash, (st_data_t)UDP_CORK, (st_data_t)rb_intern2("CORK", 4));
+#endif
+
+ rsock_intern_scm_optname_hash = st_init_numtable();
+#ifdef SCM_WIFI_STATUS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_WIFI_STATUS, (st_data_t)rb_intern2("SCM_WIFI_STATUS", 15));
+#endif
+#ifdef SCM_UCRED
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_UCRED, (st_data_t)rb_intern2("SCM_UCRED", 9));
+#endif
+#ifdef SCM_CREDS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_CREDS, (st_data_t)rb_intern2("SCM_CREDS", 9));
+#endif
+#ifdef SCM_CREDENTIALS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_CREDENTIALS, (st_data_t)rb_intern2("SCM_CREDENTIALS", 15));
+#endif
+#ifdef SCM_BINTIME
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_BINTIME, (st_data_t)rb_intern2("SCM_BINTIME", 11));
+#endif
+#ifdef SCM_TIMESTAMPING
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_TIMESTAMPING, (st_data_t)rb_intern2("SCM_TIMESTAMPING", 16));
+#endif
+#ifdef SCM_TIMESTAMPNS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_TIMESTAMPNS, (st_data_t)rb_intern2("SCM_TIMESTAMPNS", 15));
+#endif
+#ifdef SCM_TIMESTAMP
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_TIMESTAMP, (st_data_t)rb_intern2("SCM_TIMESTAMP", 13));
+#endif
+#ifdef SCM_RIGHTS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_RIGHTS, (st_data_t)rb_intern2("SCM_RIGHTS", 10));
+#endif
+#ifdef SCM_WIFI_STATUS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_WIFI_STATUS, (st_data_t)rb_intern2("WIFI_STATUS", 11));
+#endif
+#ifdef SCM_UCRED
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_UCRED, (st_data_t)rb_intern2("UCRED", 5));
+#endif
+#ifdef SCM_CREDS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_CREDS, (st_data_t)rb_intern2("CREDS", 5));
+#endif
+#ifdef SCM_CREDENTIALS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_CREDENTIALS, (st_data_t)rb_intern2("CREDENTIALS", 11));
+#endif
+#ifdef SCM_BINTIME
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_BINTIME, (st_data_t)rb_intern2("BINTIME", 7));
+#endif
+#ifdef SCM_TIMESTAMPING
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_TIMESTAMPING, (st_data_t)rb_intern2("TIMESTAMPING", 12));
+#endif
+#ifdef SCM_TIMESTAMPNS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_TIMESTAMPNS, (st_data_t)rb_intern2("TIMESTAMPNS", 11));
+#endif
+#ifdef SCM_TIMESTAMP
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_TIMESTAMP, (st_data_t)rb_intern2("TIMESTAMP", 9));
+#endif
+#ifdef SCM_RIGHTS
+ st_insert(rsock_intern_scm_optname_hash, (st_data_t)SCM_RIGHTS, (st_data_t)rb_intern2("RIGHTS", 6));
+#endif
+
+ rsock_intern_local_optname_hash = st_init_numtable();
+#ifdef LOCAL_CONNWAIT
+ st_insert(rsock_intern_local_optname_hash, (st_data_t)LOCAL_CONNWAIT, (st_data_t)rb_intern2("LOCAL_CONNWAIT", 14));
+#endif
+#ifdef LOCAL_CREDS
+ st_insert(rsock_intern_local_optname_hash, (st_data_t)LOCAL_CREDS, (st_data_t)rb_intern2("LOCAL_CREDS", 11));
+#endif
+#ifdef LOCAL_PEERCRED
+ st_insert(rsock_intern_local_optname_hash, (st_data_t)LOCAL_PEERCRED, (st_data_t)rb_intern2("LOCAL_PEERCRED", 14));
+#endif
+#ifdef LOCAL_CONNWAIT
+ st_insert(rsock_intern_local_optname_hash, (st_data_t)LOCAL_CONNWAIT, (st_data_t)rb_intern2("CONNWAIT", 8));
+#endif
+#ifdef LOCAL_CREDS
+ st_insert(rsock_intern_local_optname_hash, (st_data_t)LOCAL_CREDS, (st_data_t)rb_intern2("CREDS", 5));
+#endif
+#ifdef LOCAL_PEERCRED
+ st_insert(rsock_intern_local_optname_hash, (st_data_t)LOCAL_PEERCRED, (st_data_t)rb_intern2("PEERCRED", 8));
+#endif
+
+}
+
+int
+rsock_family_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 2:
+#ifdef AF_NS
+ if (memcmp(str, "NS", 2) == 0) { *valp = AF_NS; return 0; }
+#endif
+ return -1;
+
+ case 3:
+#ifdef AF_IPX
+ if (memcmp(str, "IPX", 3) == 0) { *valp = AF_IPX; return 0; }
+#endif
+#ifdef AF_PUP
+ if (memcmp(str, "PUP", 3) == 0) { *valp = AF_PUP; return 0; }
+#endif
+#ifdef AF_ISO
+ if (memcmp(str, "ISO", 3) == 0) { *valp = AF_ISO; return 0; }
+#endif
+#ifdef AF_OSI
+ if (memcmp(str, "OSI", 3) == 0) { *valp = AF_OSI; return 0; }
+#endif
+#ifdef AF_SNA
+ if (memcmp(str, "SNA", 3) == 0) { *valp = AF_SNA; return 0; }
+#endif
+#ifdef AF_DEC
+ if (memcmp(str, "DEC", 3) == 0) { *valp = AF_DEC; return 0; }
+#endif
+#ifdef AF_DLI
+ if (memcmp(str, "DLI", 3) == 0) { *valp = AF_DLI; return 0; }
+#endif
+#ifdef AF_LAT
+ if (memcmp(str, "LAT", 3) == 0) { *valp = AF_LAT; return 0; }
+#endif
+#ifdef AF_CNT
+ if (memcmp(str, "CNT", 3) == 0) { *valp = AF_CNT; return 0; }
+#endif
+#ifdef AF_SIP
+ if (memcmp(str, "SIP", 3) == 0) { *valp = AF_SIP; return 0; }
+#endif
+#ifdef AF_PPP
+ if (memcmp(str, "PPP", 3) == 0) { *valp = AF_PPP; return 0; }
+#endif
+#ifdef AF_ATM
+ if (memcmp(str, "ATM", 3) == 0) { *valp = AF_ATM; return 0; }
+#endif
+#ifdef AF_MAX
+ if (memcmp(str, "MAX", 3) == 0) { *valp = AF_MAX; return 0; }
+#endif
+ return -1;
+
+ case 4:
+#ifdef AF_INET
+ if (memcmp(str, "INET", 4) == 0) { *valp = AF_INET; return 0; }
+#endif
+#ifdef AF_UNIX
+ if (memcmp(str, "UNIX", 4) == 0) { *valp = AF_UNIX; return 0; }
+#endif
+#ifdef AF_AX25
+ if (memcmp(str, "AX25", 4) == 0) { *valp = AF_AX25; return 0; }
+#endif
+#ifdef AF_ECMA
+ if (memcmp(str, "ECMA", 4) == 0) { *valp = AF_ECMA; return 0; }
+#endif
+#ifdef AF_LINK
+ if (memcmp(str, "LINK", 4) == 0) { *valp = AF_LINK; return 0; }
+#endif
+#ifdef AF_COIP
+ if (memcmp(str, "COIP", 4) == 0) { *valp = AF_COIP; return 0; }
+#endif
+#ifdef AF_NDRV
+ if (memcmp(str, "NDRV", 4) == 0) { *valp = AF_NDRV; return 0; }
+#endif
+#ifdef AF_ISDN
+ if (memcmp(str, "ISDN", 4) == 0) { *valp = AF_ISDN; return 0; }
+#endif
+#ifdef AF_NATM
+ if (memcmp(str, "NATM", 4) == 0) { *valp = AF_NATM; return 0; }
+#endif
+#ifdef AF_E164
+ if (memcmp(str, "E164", 4) == 0) { *valp = AF_E164; return 0; }
+#endif
+ return -1;
+
+ case 5:
+#ifdef AF_NS
+ if (memcmp(str, "AF_NS", 5) == 0) { *valp = AF_NS; return 0; }
+#endif
+#ifdef PF_NS
+ if (memcmp(str, "PF_NS", 5) == 0) { *valp = PF_NS; return 0; }
+#endif
+#ifdef AF_INET6
+ if (memcmp(str, "INET6", 5) == 0) { *valp = AF_INET6; return 0; }
+#endif
+#ifdef AF_LOCAL
+ if (memcmp(str, "LOCAL", 5) == 0) { *valp = AF_LOCAL; return 0; }
+#endif
+#ifdef AF_CHAOS
+ if (memcmp(str, "CHAOS", 5) == 0) { *valp = AF_CHAOS; return 0; }
+#endif
+#ifdef AF_CCITT
+ if (memcmp(str, "CCITT", 5) == 0) { *valp = AF_CCITT; return 0; }
+#endif
+#ifdef AF_ROUTE
+ if (memcmp(str, "ROUTE", 5) == 0) { *valp = AF_ROUTE; return 0; }
+#endif
+ return -1;
+
+ case 6:
+#ifdef AF_IPX
+ if (memcmp(str, "AF_IPX", 6) == 0) { *valp = AF_IPX; return 0; }
+#endif
+#ifdef PF_IPX
+ if (memcmp(str, "PF_IPX", 6) == 0) { *valp = PF_IPX; return 0; }
+#endif
+#ifdef AF_PUP
+ if (memcmp(str, "AF_PUP", 6) == 0) { *valp = AF_PUP; return 0; }
+#endif
+#ifdef PF_PUP
+ if (memcmp(str, "PF_PUP", 6) == 0) { *valp = PF_PUP; return 0; }
+#endif
+#ifdef AF_ISO
+ if (memcmp(str, "AF_ISO", 6) == 0) { *valp = AF_ISO; return 0; }
+#endif
+#ifdef PF_ISO
+ if (memcmp(str, "PF_ISO", 6) == 0) { *valp = PF_ISO; return 0; }
+#endif
+#ifdef AF_OSI
+ if (memcmp(str, "AF_OSI", 6) == 0) { *valp = AF_OSI; return 0; }
+#endif
+#ifdef PF_OSI
+ if (memcmp(str, "PF_OSI", 6) == 0) { *valp = PF_OSI; return 0; }
+#endif
+#ifdef AF_SNA
+ if (memcmp(str, "AF_SNA", 6) == 0) { *valp = AF_SNA; return 0; }
+#endif
+#ifdef PF_SNA
+ if (memcmp(str, "PF_SNA", 6) == 0) { *valp = PF_SNA; return 0; }
+#endif
+#ifdef AF_DEC
+ if (memcmp(str, "AF_DEC", 6) == 0) { *valp = AF_DEC; return 0; }
+#endif
+#ifdef PF_DEC
+ if (memcmp(str, "PF_DEC", 6) == 0) { *valp = PF_DEC; return 0; }
+#endif
+#ifdef AF_DLI
+ if (memcmp(str, "AF_DLI", 6) == 0) { *valp = AF_DLI; return 0; }
+#endif
+#ifdef PF_DLI
+ if (memcmp(str, "PF_DLI", 6) == 0) { *valp = PF_DLI; return 0; }
+#endif
+#ifdef AF_LAT
+ if (memcmp(str, "AF_LAT", 6) == 0) { *valp = AF_LAT; return 0; }
+#endif
+#ifdef PF_LAT
+ if (memcmp(str, "PF_LAT", 6) == 0) { *valp = PF_LAT; return 0; }
+#endif
+#ifdef AF_CNT
+ if (memcmp(str, "AF_CNT", 6) == 0) { *valp = AF_CNT; return 0; }
+#endif
+#ifdef PF_CNT
+ if (memcmp(str, "PF_CNT", 6) == 0) { *valp = PF_CNT; return 0; }
+#endif
+#ifdef AF_SIP
+ if (memcmp(str, "AF_SIP", 6) == 0) { *valp = AF_SIP; return 0; }
+#endif
+#ifdef PF_SIP
+ if (memcmp(str, "PF_SIP", 6) == 0) { *valp = PF_SIP; return 0; }
+#endif
+#ifdef AF_PPP
+ if (memcmp(str, "AF_PPP", 6) == 0) { *valp = AF_PPP; return 0; }
+#endif
+#ifdef PF_PPP
+ if (memcmp(str, "PF_PPP", 6) == 0) { *valp = PF_PPP; return 0; }
+#endif
+#ifdef AF_ATM
+ if (memcmp(str, "AF_ATM", 6) == 0) { *valp = AF_ATM; return 0; }
+#endif
+#ifdef PF_ATM
+ if (memcmp(str, "PF_ATM", 6) == 0) { *valp = PF_ATM; return 0; }
+#endif
+#ifdef AF_MAX
+ if (memcmp(str, "AF_MAX", 6) == 0) { *valp = AF_MAX; return 0; }
+#endif
+#ifdef PF_MAX
+ if (memcmp(str, "PF_MAX", 6) == 0) { *valp = PF_MAX; return 0; }
+#endif
+#ifdef PF_XTP
+ if (memcmp(str, "PF_XTP", 6) == 0) { *valp = PF_XTP; return 0; }
+#endif
+#ifdef PF_PIP
+ if (memcmp(str, "PF_PIP", 6) == 0) { *valp = PF_PIP; return 0; }
+#endif
+#ifdef PF_KEY
+ if (memcmp(str, "PF_KEY", 6) == 0) { *valp = PF_KEY; return 0; }
+#endif
+#ifdef AF_UNSPEC
+ if (memcmp(str, "UNSPEC", 6) == 0) { *valp = AF_UNSPEC; return 0; }
+#endif
+#ifdef AF_HYLINK
+ if (memcmp(str, "HYLINK", 6) == 0) { *valp = AF_HYLINK; return 0; }
+#endif
+#ifdef AF_SYSTEM
+ if (memcmp(str, "SYSTEM", 6) == 0) { *valp = AF_SYSTEM; return 0; }
+#endif
+#ifdef AF_PACKET
+ if (memcmp(str, "PACKET", 6) == 0) { *valp = AF_PACKET; return 0; }
+#endif
+ return -1;
+
+ case 7:
+#ifdef AF_INET
+ if (memcmp(str, "AF_INET", 7) == 0) { *valp = AF_INET; return 0; }
+#endif
+#ifdef PF_INET
+ if (memcmp(str, "PF_INET", 7) == 0) { *valp = PF_INET; return 0; }
+#endif
+#ifdef AF_UNIX
+ if (memcmp(str, "AF_UNIX", 7) == 0) { *valp = AF_UNIX; return 0; }
+#endif
+#ifdef PF_UNIX
+ if (memcmp(str, "PF_UNIX", 7) == 0) { *valp = PF_UNIX; return 0; }
+#endif
+#ifdef AF_AX25
+ if (memcmp(str, "AF_AX25", 7) == 0) { *valp = AF_AX25; return 0; }
+#endif
+#ifdef PF_AX25
+ if (memcmp(str, "PF_AX25", 7) == 0) { *valp = PF_AX25; return 0; }
+#endif
+#ifdef AF_ECMA
+ if (memcmp(str, "AF_ECMA", 7) == 0) { *valp = AF_ECMA; return 0; }
+#endif
+#ifdef PF_ECMA
+ if (memcmp(str, "PF_ECMA", 7) == 0) { *valp = PF_ECMA; return 0; }
+#endif
+#ifdef AF_LINK
+ if (memcmp(str, "AF_LINK", 7) == 0) { *valp = AF_LINK; return 0; }
+#endif
+#ifdef PF_LINK
+ if (memcmp(str, "PF_LINK", 7) == 0) { *valp = PF_LINK; return 0; }
+#endif
+#ifdef AF_COIP
+ if (memcmp(str, "AF_COIP", 7) == 0) { *valp = AF_COIP; return 0; }
+#endif
+#ifdef PF_COIP
+ if (memcmp(str, "PF_COIP", 7) == 0) { *valp = PF_COIP; return 0; }
+#endif
+#ifdef AF_NDRV
+ if (memcmp(str, "AF_NDRV", 7) == 0) { *valp = AF_NDRV; return 0; }
+#endif
+#ifdef PF_NDRV
+ if (memcmp(str, "PF_NDRV", 7) == 0) { *valp = PF_NDRV; return 0; }
+#endif
+#ifdef AF_ISDN
+ if (memcmp(str, "AF_ISDN", 7) == 0) { *valp = AF_ISDN; return 0; }
+#endif
+#ifdef PF_ISDN
+ if (memcmp(str, "PF_ISDN", 7) == 0) { *valp = PF_ISDN; return 0; }
+#endif
+#ifdef AF_NATM
+ if (memcmp(str, "AF_NATM", 7) == 0) { *valp = AF_NATM; return 0; }
+#endif
+#ifdef PF_NATM
+ if (memcmp(str, "PF_NATM", 7) == 0) { *valp = PF_NATM; return 0; }
+#endif
+#ifdef AF_E164
+ if (memcmp(str, "AF_E164", 7) == 0) { *valp = AF_E164; return 0; }
+#endif
+#ifdef PF_RTIP
+ if (memcmp(str, "PF_RTIP", 7) == 0) { *valp = PF_RTIP; return 0; }
+#endif
+#ifdef AF_IMPLINK
+ if (memcmp(str, "IMPLINK", 7) == 0) { *valp = AF_IMPLINK; return 0; }
+#endif
+#ifdef AF_DATAKIT
+ if (memcmp(str, "DATAKIT", 7) == 0) { *valp = AF_DATAKIT; return 0; }
+#endif
+#ifdef AF_NETBIOS
+ if (memcmp(str, "NETBIOS", 7) == 0) { *valp = AF_NETBIOS; return 0; }
+#endif
+ return -1;
+
+ case 8:
+#ifdef AF_INET6
+ if (memcmp(str, "AF_INET6", 8) == 0) { *valp = AF_INET6; return 0; }
+#endif
+#ifdef PF_INET6
+ if (memcmp(str, "PF_INET6", 8) == 0) { *valp = PF_INET6; return 0; }
+#endif
+#ifdef AF_LOCAL
+ if (memcmp(str, "AF_LOCAL", 8) == 0) { *valp = AF_LOCAL; return 0; }
+#endif
+#ifdef PF_LOCAL
+ if (memcmp(str, "PF_LOCAL", 8) == 0) { *valp = PF_LOCAL; return 0; }
+#endif
+#ifdef AF_CHAOS
+ if (memcmp(str, "AF_CHAOS", 8) == 0) { *valp = AF_CHAOS; return 0; }
+#endif
+#ifdef PF_CHAOS
+ if (memcmp(str, "PF_CHAOS", 8) == 0) { *valp = PF_CHAOS; return 0; }
+#endif
+#ifdef AF_CCITT
+ if (memcmp(str, "AF_CCITT", 8) == 0) { *valp = AF_CCITT; return 0; }
+#endif
+#ifdef PF_CCITT
+ if (memcmp(str, "PF_CCITT", 8) == 0) { *valp = PF_CCITT; return 0; }
+#endif
+#ifdef AF_ROUTE
+ if (memcmp(str, "AF_ROUTE", 8) == 0) { *valp = AF_ROUTE; return 0; }
+#endif
+#ifdef PF_ROUTE
+ if (memcmp(str, "PF_ROUTE", 8) == 0) { *valp = PF_ROUTE; return 0; }
+#endif
+#ifdef AF_NETGRAPH
+ if (memcmp(str, "NETGRAPH", 8) == 0) { *valp = AF_NETGRAPH; return 0; }
+#endif
+ return -1;
+
+ case 9:
+#ifdef AF_UNSPEC
+ if (memcmp(str, "AF_UNSPEC", 9) == 0) { *valp = AF_UNSPEC; return 0; }
+#endif
+#ifdef PF_UNSPEC
+ if (memcmp(str, "PF_UNSPEC", 9) == 0) { *valp = PF_UNSPEC; return 0; }
+#endif
+#ifdef AF_HYLINK
+ if (memcmp(str, "AF_HYLINK", 9) == 0) { *valp = AF_HYLINK; return 0; }
+#endif
+#ifdef PF_HYLINK
+ if (memcmp(str, "PF_HYLINK", 9) == 0) { *valp = PF_HYLINK; return 0; }
+#endif
+#ifdef AF_SYSTEM
+ if (memcmp(str, "AF_SYSTEM", 9) == 0) { *valp = AF_SYSTEM; return 0; }
+#endif
+#ifdef PF_SYSTEM
+ if (memcmp(str, "PF_SYSTEM", 9) == 0) { *valp = PF_SYSTEM; return 0; }
+#endif
+#ifdef AF_PACKET
+ if (memcmp(str, "AF_PACKET", 9) == 0) { *valp = AF_PACKET; return 0; }
+#endif
+#ifdef PF_PACKET
+ if (memcmp(str, "PF_PACKET", 9) == 0) { *valp = PF_PACKET; return 0; }
+#endif
+#ifdef AF_APPLETALK
+ if (memcmp(str, "APPLETALK", 9) == 0) { *valp = AF_APPLETALK; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef AF_IMPLINK
+ if (memcmp(str, "AF_IMPLINK", 10) == 0) { *valp = AF_IMPLINK; return 0; }
+#endif
+#ifdef PF_IMPLINK
+ if (memcmp(str, "PF_IMPLINK", 10) == 0) { *valp = PF_IMPLINK; return 0; }
+#endif
+#ifdef AF_DATAKIT
+ if (memcmp(str, "AF_DATAKIT", 10) == 0) { *valp = AF_DATAKIT; return 0; }
+#endif
+#ifdef PF_DATAKIT
+ if (memcmp(str, "PF_DATAKIT", 10) == 0) { *valp = PF_DATAKIT; return 0; }
+#endif
+#ifdef AF_NETBIOS
+ if (memcmp(str, "AF_NETBIOS", 10) == 0) { *valp = AF_NETBIOS; return 0; }
+#endif
+#ifdef PF_NETBIOS
+ if (memcmp(str, "PF_NETBIOS", 10) == 0) { *valp = PF_NETBIOS; return 0; }
+#endif
+ return -1;
+
+ case 11:
+#ifdef AF_NETGRAPH
+ if (memcmp(str, "AF_NETGRAPH", 11) == 0) { *valp = AF_NETGRAPH; return 0; }
+#endif
+#ifdef PF_NETGRAPH
+ if (memcmp(str, "PF_NETGRAPH", 11) == 0) { *valp = PF_NETGRAPH; return 0; }
+#endif
+ return -1;
+
+ case 12:
+#ifdef AF_APPLETALK
+ if (memcmp(str, "AF_APPLETALK", 12) == 0) { *valp = AF_APPLETALK; return 0; }
+#endif
+#ifdef PF_APPLETALK
+ if (memcmp(str, "PF_APPLETALK", 12) == 0) { *valp = PF_APPLETALK; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+int
+rsock_socktype_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 3:
+#ifdef SOCK_RAW
+ if (memcmp(str, "RAW", 3) == 0) { *valp = SOCK_RAW; return 0; }
+#endif
+#ifdef SOCK_RDM
+ if (memcmp(str, "RDM", 3) == 0) { *valp = SOCK_RDM; return 0; }
+#endif
+ return -1;
+
+ case 5:
+#ifdef SOCK_DGRAM
+ if (memcmp(str, "DGRAM", 5) == 0) { *valp = SOCK_DGRAM; return 0; }
+#endif
+ return -1;
+
+ case 6:
+#ifdef SOCK_STREAM
+ if (memcmp(str, "STREAM", 6) == 0) { *valp = SOCK_STREAM; return 0; }
+#endif
+#ifdef SOCK_PACKET
+ if (memcmp(str, "PACKET", 6) == 0) { *valp = SOCK_PACKET; return 0; }
+#endif
+ return -1;
+
+ case 8:
+#ifdef SOCK_RAW
+ if (memcmp(str, "SOCK_RAW", 8) == 0) { *valp = SOCK_RAW; return 0; }
+#endif
+#ifdef SOCK_RDM
+ if (memcmp(str, "SOCK_RDM", 8) == 0) { *valp = SOCK_RDM; return 0; }
+#endif
+ return -1;
+
+ case 9:
+#ifdef SOCK_SEQPACKET
+ if (memcmp(str, "SEQPACKET", 9) == 0) { *valp = SOCK_SEQPACKET; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef SOCK_DGRAM
+ if (memcmp(str, "SOCK_DGRAM", 10) == 0) { *valp = SOCK_DGRAM; return 0; }
+#endif
+ return -1;
+
+ case 11:
+#ifdef SOCK_STREAM
+ if (memcmp(str, "SOCK_STREAM", 11) == 0) { *valp = SOCK_STREAM; return 0; }
+#endif
+#ifdef SOCK_PACKET
+ if (memcmp(str, "SOCK_PACKET", 11) == 0) { *valp = SOCK_PACKET; return 0; }
+#endif
+ return -1;
+
+ case 14:
+#ifdef SOCK_SEQPACKET
+ if (memcmp(str, "SOCK_SEQPACKET", 14) == 0) { *valp = SOCK_SEQPACKET; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+int
+rsock_ipproto_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 2:
+#ifdef IPPROTO_IP
+ if (memcmp(str, "IP", 2) == 0) { *valp = IPPROTO_IP; return 0; }
+#endif
+#ifdef IPPROTO_ND
+ if (memcmp(str, "ND", 2) == 0) { *valp = IPPROTO_ND; return 0; }
+#endif
+#ifdef IPPROTO_TP
+ if (memcmp(str, "TP", 2) == 0) { *valp = IPPROTO_TP; return 0; }
+#endif
+#ifdef IPPROTO_AH
+ if (memcmp(str, "AH", 2) == 0) { *valp = IPPROTO_AH; return 0; }
+#endif
+ return -1;
+
+ case 3:
+#ifdef IPPROTO_GGP
+ if (memcmp(str, "GGP", 3) == 0) { *valp = IPPROTO_GGP; return 0; }
+#endif
+#ifdef IPPROTO_TCP
+ if (memcmp(str, "TCP", 3) == 0) { *valp = IPPROTO_TCP; return 0; }
+#endif
+#ifdef IPPROTO_EGP
+ if (memcmp(str, "EGP", 3) == 0) { *valp = IPPROTO_EGP; return 0; }
+#endif
+#ifdef IPPROTO_PUP
+ if (memcmp(str, "PUP", 3) == 0) { *valp = IPPROTO_PUP; return 0; }
+#endif
+#ifdef IPPROTO_UDP
+ if (memcmp(str, "UDP", 3) == 0) { *valp = IPPROTO_UDP; return 0; }
+#endif
+#ifdef IPPROTO_IDP
+ if (memcmp(str, "IDP", 3) == 0) { *valp = IPPROTO_IDP; return 0; }
+#endif
+#ifdef IPPROTO_XTP
+ if (memcmp(str, "XTP", 3) == 0) { *valp = IPPROTO_XTP; return 0; }
+#endif
+#ifdef IPPROTO_EON
+ if (memcmp(str, "EON", 3) == 0) { *valp = IPPROTO_EON; return 0; }
+#endif
+#ifdef IPPROTO_BIP
+ if (memcmp(str, "BIP", 3) == 0) { *valp = IPPROTO_BIP; return 0; }
+#endif
+#ifdef IPPROTO_ESP
+ if (memcmp(str, "ESP", 3) == 0) { *valp = IPPROTO_ESP; return 0; }
+#endif
+#ifdef IPPROTO_RAW
+ if (memcmp(str, "RAW", 3) == 0) { *valp = IPPROTO_RAW; return 0; }
+#endif
+#ifdef IPPROTO_MAX
+ if (memcmp(str, "MAX", 3) == 0) { *valp = IPPROTO_MAX; return 0; }
+#endif
+ return -1;
+
+ case 4:
+#ifdef IPPROTO_ICMP
+ if (memcmp(str, "ICMP", 4) == 0) { *valp = IPPROTO_ICMP; return 0; }
+#endif
+#ifdef IPPROTO_IGMP
+ if (memcmp(str, "IGMP", 4) == 0) { *valp = IPPROTO_IGMP; return 0; }
+#endif
+#ifdef IPPROTO_IPV6
+ if (memcmp(str, "IPV6", 4) == 0) { *valp = IPPROTO_IPV6; return 0; }
+#endif
+#ifdef IPPROTO_NONE
+ if (memcmp(str, "NONE", 4) == 0) { *valp = IPPROTO_NONE; return 0; }
+#endif
+ return -1;
+
+ case 5:
+#ifdef IPPROTO_HELLO
+ if (memcmp(str, "HELLO", 5) == 0) { *valp = IPPROTO_HELLO; return 0; }
+#endif
+ return -1;
+
+ case 6:
+#ifdef IPPROTO_ICMPV6
+ if (memcmp(str, "ICMPV6", 6) == 0) { *valp = IPPROTO_ICMPV6; return 0; }
+#endif
+ return -1;
+
+ case 7:
+#ifdef IPPROTO_DSTOPTS
+ if (memcmp(str, "DSTOPTS", 7) == 0) { *valp = IPPROTO_DSTOPTS; return 0; }
+#endif
+#ifdef IPPROTO_HOPOPTS
+ if (memcmp(str, "HOPOPTS", 7) == 0) { *valp = IPPROTO_HOPOPTS; return 0; }
+#endif
+#ifdef IPPROTO_ROUTING
+ if (memcmp(str, "ROUTING", 7) == 0) { *valp = IPPROTO_ROUTING; return 0; }
+#endif
+ return -1;
+
+ case 8:
+#ifdef IPPROTO_FRAGMENT
+ if (memcmp(str, "FRAGMENT", 8) == 0) { *valp = IPPROTO_FRAGMENT; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef IPPROTO_IP
+ if (memcmp(str, "IPPROTO_IP", 10) == 0) { *valp = IPPROTO_IP; return 0; }
+#endif
+#ifdef IPPROTO_ND
+ if (memcmp(str, "IPPROTO_ND", 10) == 0) { *valp = IPPROTO_ND; return 0; }
+#endif
+#ifdef IPPROTO_TP
+ if (memcmp(str, "IPPROTO_TP", 10) == 0) { *valp = IPPROTO_TP; return 0; }
+#endif
+#ifdef IPPROTO_AH
+ if (memcmp(str, "IPPROTO_AH", 10) == 0) { *valp = IPPROTO_AH; return 0; }
+#endif
+ return -1;
+
+ case 11:
+#ifdef IPPROTO_GGP
+ if (memcmp(str, "IPPROTO_GGP", 11) == 0) { *valp = IPPROTO_GGP; return 0; }
+#endif
+#ifdef IPPROTO_TCP
+ if (memcmp(str, "IPPROTO_TCP", 11) == 0) { *valp = IPPROTO_TCP; return 0; }
+#endif
+#ifdef IPPROTO_EGP
+ if (memcmp(str, "IPPROTO_EGP", 11) == 0) { *valp = IPPROTO_EGP; return 0; }
+#endif
+#ifdef IPPROTO_PUP
+ if (memcmp(str, "IPPROTO_PUP", 11) == 0) { *valp = IPPROTO_PUP; return 0; }
+#endif
+#ifdef IPPROTO_UDP
+ if (memcmp(str, "IPPROTO_UDP", 11) == 0) { *valp = IPPROTO_UDP; return 0; }
+#endif
+#ifdef IPPROTO_IDP
+ if (memcmp(str, "IPPROTO_IDP", 11) == 0) { *valp = IPPROTO_IDP; return 0; }
+#endif
+#ifdef IPPROTO_XTP
+ if (memcmp(str, "IPPROTO_XTP", 11) == 0) { *valp = IPPROTO_XTP; return 0; }
+#endif
+#ifdef IPPROTO_EON
+ if (memcmp(str, "IPPROTO_EON", 11) == 0) { *valp = IPPROTO_EON; return 0; }
+#endif
+#ifdef IPPROTO_BIP
+ if (memcmp(str, "IPPROTO_BIP", 11) == 0) { *valp = IPPROTO_BIP; return 0; }
+#endif
+#ifdef IPPROTO_ESP
+ if (memcmp(str, "IPPROTO_ESP", 11) == 0) { *valp = IPPROTO_ESP; return 0; }
+#endif
+#ifdef IPPROTO_RAW
+ if (memcmp(str, "IPPROTO_RAW", 11) == 0) { *valp = IPPROTO_RAW; return 0; }
+#endif
+#ifdef IPPROTO_MAX
+ if (memcmp(str, "IPPROTO_MAX", 11) == 0) { *valp = IPPROTO_MAX; return 0; }
+#endif
+ return -1;
+
+ case 12:
+#ifdef IPPROTO_ICMP
+ if (memcmp(str, "IPPROTO_ICMP", 12) == 0) { *valp = IPPROTO_ICMP; return 0; }
+#endif
+#ifdef IPPROTO_IGMP
+ if (memcmp(str, "IPPROTO_IGMP", 12) == 0) { *valp = IPPROTO_IGMP; return 0; }
+#endif
+#ifdef IPPROTO_IPV6
+ if (memcmp(str, "IPPROTO_IPV6", 12) == 0) { *valp = IPPROTO_IPV6; return 0; }
+#endif
+#ifdef IPPROTO_NONE
+ if (memcmp(str, "IPPROTO_NONE", 12) == 0) { *valp = IPPROTO_NONE; return 0; }
+#endif
+ return -1;
+
+ case 13:
+#ifdef IPPROTO_HELLO
+ if (memcmp(str, "IPPROTO_HELLO", 13) == 0) { *valp = IPPROTO_HELLO; return 0; }
+#endif
+ return -1;
+
+ case 14:
+#ifdef IPPROTO_ICMPV6
+ if (memcmp(str, "IPPROTO_ICMPV6", 14) == 0) { *valp = IPPROTO_ICMPV6; return 0; }
+#endif
+ return -1;
+
+ case 15:
+#ifdef IPPROTO_DSTOPTS
+ if (memcmp(str, "IPPROTO_DSTOPTS", 15) == 0) { *valp = IPPROTO_DSTOPTS; return 0; }
+#endif
+#ifdef IPPROTO_HOPOPTS
+ if (memcmp(str, "IPPROTO_HOPOPTS", 15) == 0) { *valp = IPPROTO_HOPOPTS; return 0; }
+#endif
+#ifdef IPPROTO_ROUTING
+ if (memcmp(str, "IPPROTO_ROUTING", 15) == 0) { *valp = IPPROTO_ROUTING; return 0; }
+#endif
+ return -1;
+
+ case 16:
+#ifdef IPPROTO_FRAGMENT
+ if (memcmp(str, "IPPROTO_FRAGMENT", 16) == 0) { *valp = IPPROTO_FRAGMENT; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+int
+rsock_unknown_level_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 6:
+#ifdef SOL_SOCKET
+ if (memcmp(str, "SOCKET", 6) == 0) { *valp = SOL_SOCKET; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef SOL_SOCKET
+ if (memcmp(str, "SOL_SOCKET", 10) == 0) { *valp = SOL_SOCKET; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+int
+rsock_ip_level_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 2:
+#ifdef IPPROTO_IP
+ if (memcmp(str, "IP", 2) == 0) { *valp = IPPROTO_IP; return 0; }
+#endif
+#ifdef IPPROTO_ND
+ if (memcmp(str, "ND", 2) == 0) { *valp = IPPROTO_ND; return 0; }
+#endif
+#ifdef IPPROTO_TP
+ if (memcmp(str, "TP", 2) == 0) { *valp = IPPROTO_TP; return 0; }
+#endif
+#ifdef IPPROTO_AH
+ if (memcmp(str, "AH", 2) == 0) { *valp = IPPROTO_AH; return 0; }
+#endif
+ return -1;
+
+ case 3:
+#ifdef IPPROTO_GGP
+ if (memcmp(str, "GGP", 3) == 0) { *valp = IPPROTO_GGP; return 0; }
+#endif
+#ifdef IPPROTO_TCP
+ if (memcmp(str, "TCP", 3) == 0) { *valp = IPPROTO_TCP; return 0; }
+#endif
+#ifdef IPPROTO_EGP
+ if (memcmp(str, "EGP", 3) == 0) { *valp = IPPROTO_EGP; return 0; }
+#endif
+#ifdef IPPROTO_PUP
+ if (memcmp(str, "PUP", 3) == 0) { *valp = IPPROTO_PUP; return 0; }
+#endif
+#ifdef IPPROTO_UDP
+ if (memcmp(str, "UDP", 3) == 0) { *valp = IPPROTO_UDP; return 0; }
+#endif
+#ifdef IPPROTO_IDP
+ if (memcmp(str, "IDP", 3) == 0) { *valp = IPPROTO_IDP; return 0; }
+#endif
+#ifdef IPPROTO_XTP
+ if (memcmp(str, "XTP", 3) == 0) { *valp = IPPROTO_XTP; return 0; }
+#endif
+#ifdef IPPROTO_EON
+ if (memcmp(str, "EON", 3) == 0) { *valp = IPPROTO_EON; return 0; }
+#endif
+#ifdef IPPROTO_BIP
+ if (memcmp(str, "BIP", 3) == 0) { *valp = IPPROTO_BIP; return 0; }
+#endif
+#ifdef IPPROTO_ESP
+ if (memcmp(str, "ESP", 3) == 0) { *valp = IPPROTO_ESP; return 0; }
+#endif
+#ifdef IPPROTO_RAW
+ if (memcmp(str, "RAW", 3) == 0) { *valp = IPPROTO_RAW; return 0; }
+#endif
+#ifdef IPPROTO_MAX
+ if (memcmp(str, "MAX", 3) == 0) { *valp = IPPROTO_MAX; return 0; }
+#endif
+ return -1;
+
+ case 4:
+#ifdef IPPROTO_ICMP
+ if (memcmp(str, "ICMP", 4) == 0) { *valp = IPPROTO_ICMP; return 0; }
+#endif
+#ifdef IPPROTO_IGMP
+ if (memcmp(str, "IGMP", 4) == 0) { *valp = IPPROTO_IGMP; return 0; }
+#endif
+#ifdef IPPROTO_IPV6
+ if (memcmp(str, "IPV6", 4) == 0) { *valp = IPPROTO_IPV6; return 0; }
+#endif
+#ifdef IPPROTO_NONE
+ if (memcmp(str, "NONE", 4) == 0) { *valp = IPPROTO_NONE; return 0; }
+#endif
+ return -1;
+
+ case 5:
+#ifdef IPPROTO_HELLO
+ if (memcmp(str, "HELLO", 5) == 0) { *valp = IPPROTO_HELLO; return 0; }
+#endif
+ return -1;
+
+ case 6:
+#ifdef SOL_SOCKET
+ if (memcmp(str, "SOCKET", 6) == 0) { *valp = SOL_SOCKET; return 0; }
+#endif
+#ifdef IPPROTO_ICMPV6
+ if (memcmp(str, "ICMPV6", 6) == 0) { *valp = IPPROTO_ICMPV6; return 0; }
+#endif
+ return -1;
+
+ case 7:
+#ifdef IPPROTO_DSTOPTS
+ if (memcmp(str, "DSTOPTS", 7) == 0) { *valp = IPPROTO_DSTOPTS; return 0; }
+#endif
+#ifdef IPPROTO_HOPOPTS
+ if (memcmp(str, "HOPOPTS", 7) == 0) { *valp = IPPROTO_HOPOPTS; return 0; }
+#endif
+#ifdef IPPROTO_ROUTING
+ if (memcmp(str, "ROUTING", 7) == 0) { *valp = IPPROTO_ROUTING; return 0; }
+#endif
+ return -1;
+
+ case 8:
+#ifdef IPPROTO_FRAGMENT
+ if (memcmp(str, "FRAGMENT", 8) == 0) { *valp = IPPROTO_FRAGMENT; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef SOL_SOCKET
+ if (memcmp(str, "SOL_SOCKET", 10) == 0) { *valp = SOL_SOCKET; return 0; }
+#endif
+#ifdef IPPROTO_IP
+ if (memcmp(str, "IPPROTO_IP", 10) == 0) { *valp = IPPROTO_IP; return 0; }
+#endif
+#ifdef IPPROTO_ND
+ if (memcmp(str, "IPPROTO_ND", 10) == 0) { *valp = IPPROTO_ND; return 0; }
+#endif
+#ifdef IPPROTO_TP
+ if (memcmp(str, "IPPROTO_TP", 10) == 0) { *valp = IPPROTO_TP; return 0; }
+#endif
+#ifdef IPPROTO_AH
+ if (memcmp(str, "IPPROTO_AH", 10) == 0) { *valp = IPPROTO_AH; return 0; }
+#endif
+ return -1;
+
+ case 11:
+#ifdef IPPROTO_GGP
+ if (memcmp(str, "IPPROTO_GGP", 11) == 0) { *valp = IPPROTO_GGP; return 0; }
+#endif
+#ifdef IPPROTO_TCP
+ if (memcmp(str, "IPPROTO_TCP", 11) == 0) { *valp = IPPROTO_TCP; return 0; }
+#endif
+#ifdef IPPROTO_EGP
+ if (memcmp(str, "IPPROTO_EGP", 11) == 0) { *valp = IPPROTO_EGP; return 0; }
+#endif
+#ifdef IPPROTO_PUP
+ if (memcmp(str, "IPPROTO_PUP", 11) == 0) { *valp = IPPROTO_PUP; return 0; }
+#endif
+#ifdef IPPROTO_UDP
+ if (memcmp(str, "IPPROTO_UDP", 11) == 0) { *valp = IPPROTO_UDP; return 0; }
+#endif
+#ifdef IPPROTO_IDP
+ if (memcmp(str, "IPPROTO_IDP", 11) == 0) { *valp = IPPROTO_IDP; return 0; }
+#endif
+#ifdef IPPROTO_XTP
+ if (memcmp(str, "IPPROTO_XTP", 11) == 0) { *valp = IPPROTO_XTP; return 0; }
+#endif
+#ifdef IPPROTO_EON
+ if (memcmp(str, "IPPROTO_EON", 11) == 0) { *valp = IPPROTO_EON; return 0; }
+#endif
+#ifdef IPPROTO_BIP
+ if (memcmp(str, "IPPROTO_BIP", 11) == 0) { *valp = IPPROTO_BIP; return 0; }
+#endif
+#ifdef IPPROTO_ESP
+ if (memcmp(str, "IPPROTO_ESP", 11) == 0) { *valp = IPPROTO_ESP; return 0; }
+#endif
+#ifdef IPPROTO_RAW
+ if (memcmp(str, "IPPROTO_RAW", 11) == 0) { *valp = IPPROTO_RAW; return 0; }
+#endif
+#ifdef IPPROTO_MAX
+ if (memcmp(str, "IPPROTO_MAX", 11) == 0) { *valp = IPPROTO_MAX; return 0; }
+#endif
+ return -1;
+
+ case 12:
+#ifdef IPPROTO_ICMP
+ if (memcmp(str, "IPPROTO_ICMP", 12) == 0) { *valp = IPPROTO_ICMP; return 0; }
+#endif
+#ifdef IPPROTO_IGMP
+ if (memcmp(str, "IPPROTO_IGMP", 12) == 0) { *valp = IPPROTO_IGMP; return 0; }
+#endif
+#ifdef IPPROTO_IPV6
+ if (memcmp(str, "IPPROTO_IPV6", 12) == 0) { *valp = IPPROTO_IPV6; return 0; }
+#endif
+#ifdef IPPROTO_NONE
+ if (memcmp(str, "IPPROTO_NONE", 12) == 0) { *valp = IPPROTO_NONE; return 0; }
+#endif
+ return -1;
+
+ case 13:
+#ifdef IPPROTO_HELLO
+ if (memcmp(str, "IPPROTO_HELLO", 13) == 0) { *valp = IPPROTO_HELLO; return 0; }
+#endif
+ return -1;
+
+ case 14:
+#ifdef IPPROTO_ICMPV6
+ if (memcmp(str, "IPPROTO_ICMPV6", 14) == 0) { *valp = IPPROTO_ICMPV6; return 0; }
+#endif
+ return -1;
+
+ case 15:
+#ifdef IPPROTO_DSTOPTS
+ if (memcmp(str, "IPPROTO_DSTOPTS", 15) == 0) { *valp = IPPROTO_DSTOPTS; return 0; }
+#endif
+#ifdef IPPROTO_HOPOPTS
+ if (memcmp(str, "IPPROTO_HOPOPTS", 15) == 0) { *valp = IPPROTO_HOPOPTS; return 0; }
+#endif
+#ifdef IPPROTO_ROUTING
+ if (memcmp(str, "IPPROTO_ROUTING", 15) == 0) { *valp = IPPROTO_ROUTING; return 0; }
+#endif
+ return -1;
+
+ case 16:
+#ifdef IPPROTO_FRAGMENT
+ if (memcmp(str, "IPPROTO_FRAGMENT", 16) == 0) { *valp = IPPROTO_FRAGMENT; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+int
+rsock_so_optname_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 3:
+#ifdef SO_NKE
+ if (memcmp(str, "NKE", 3) == 0) { *valp = SO_NKE; return 0; }
+#endif
+ return -1;
+
+ case 4:
+#ifdef SO_TYPE
+ if (memcmp(str, "TYPE", 4) == 0) { *valp = SO_TYPE; return 0; }
+#endif
+#ifdef SO_MARK
+ if (memcmp(str, "MARK", 4) == 0) { *valp = SO_MARK; return 0; }
+#endif
+ return -1;
+
+ case 5:
+#ifdef SO_DEBUG
+ if (memcmp(str, "DEBUG", 5) == 0) { *valp = SO_DEBUG; return 0; }
+#endif
+#ifdef SO_ERROR
+ if (memcmp(str, "ERROR", 5) == 0) { *valp = SO_ERROR; return 0; }
+#endif
+#ifdef SO_NREAD
+ if (memcmp(str, "NREAD", 5) == 0) { *valp = SO_NREAD; return 0; }
+#endif
+#ifdef SO_NOFCS
+ if (memcmp(str, "NOFCS", 5) == 0) { *valp = SO_NOFCS; return 0; }
+#endif
+ return -1;
+
+ case 6:
+#ifdef SO_NKE
+ if (memcmp(str, "SO_NKE", 6) == 0) { *valp = SO_NKE; return 0; }
+#endif
+#ifdef SO_SNDBUF
+ if (memcmp(str, "SNDBUF", 6) == 0) { *valp = SO_SNDBUF; return 0; }
+#endif
+#ifdef SO_RCVBUF
+ if (memcmp(str, "RCVBUF", 6) == 0) { *valp = SO_RCVBUF; return 0; }
+#endif
+#ifdef SO_LINGER
+ if (memcmp(str, "LINGER", 6) == 0) { *valp = SO_LINGER; return 0; }
+#endif
+#ifdef SO_DOMAIN
+ if (memcmp(str, "DOMAIN", 6) == 0) { *valp = SO_DOMAIN; return 0; }
+#endif
+ return -1;
+
+ case 7:
+#ifdef SO_TYPE
+ if (memcmp(str, "SO_TYPE", 7) == 0) { *valp = SO_TYPE; return 0; }
+#endif
+#ifdef SO_MARK
+ if (memcmp(str, "SO_MARK", 7) == 0) { *valp = SO_MARK; return 0; }
+#endif
+#ifdef SO_BINTIME
+ if (memcmp(str, "BINTIME", 7) == 0) { *valp = SO_BINTIME; return 0; }
+#endif
+#ifdef SO_PEERSEC
+ if (memcmp(str, "PEERSEC", 7) == 0) { *valp = SO_PEERSEC; return 0; }
+#endif
+#ifdef SO_PASSSEC
+ if (memcmp(str, "PASSSEC", 7) == 0) { *valp = SO_PASSSEC; return 0; }
+#endif
+ return -1;
+
+ case 8:
+#ifdef SO_DEBUG
+ if (memcmp(str, "SO_DEBUG", 8) == 0) { *valp = SO_DEBUG; return 0; }
+#endif
+#ifdef SO_ERROR
+ if (memcmp(str, "SO_ERROR", 8) == 0) { *valp = SO_ERROR; return 0; }
+#endif
+#ifdef SO_NREAD
+ if (memcmp(str, "SO_NREAD", 8) == 0) { *valp = SO_NREAD; return 0; }
+#endif
+#ifdef SO_NOFCS
+ if (memcmp(str, "SO_NOFCS", 8) == 0) { *valp = SO_NOFCS; return 0; }
+#endif
+#ifdef SO_NO_CHECK
+ if (memcmp(str, "NO_CHECK", 8) == 0) { *valp = SO_NO_CHECK; return 0; }
+#endif
+#ifdef SO_PRIORITY
+ if (memcmp(str, "PRIORITY", 8) == 0) { *valp = SO_PRIORITY; return 0; }
+#endif
+#ifdef SO_PASSCRED
+ if (memcmp(str, "PASSCRED", 8) == 0) { *valp = SO_PASSCRED; return 0; }
+#endif
+#ifdef SO_PEERCRED
+ if (memcmp(str, "PEERCRED", 8) == 0) { *valp = SO_PEERCRED; return 0; }
+#endif
+#ifdef SO_RCVLOWAT
+ if (memcmp(str, "RCVLOWAT", 8) == 0) { *valp = SO_RCVLOWAT; return 0; }
+#endif
+#ifdef SO_SNDLOWAT
+ if (memcmp(str, "SNDLOWAT", 8) == 0) { *valp = SO_SNDLOWAT; return 0; }
+#endif
+#ifdef SO_RCVTIMEO
+ if (memcmp(str, "RCVTIMEO", 8) == 0) { *valp = SO_RCVTIMEO; return 0; }
+#endif
+#ifdef SO_SNDTIMEO
+ if (memcmp(str, "SNDTIMEO", 8) == 0) { *valp = SO_SNDTIMEO; return 0; }
+#endif
+#ifdef SO_WANTMORE
+ if (memcmp(str, "WANTMORE", 8) == 0) { *valp = SO_WANTMORE; return 0; }
+#endif
+#ifdef SO_PEERNAME
+ if (memcmp(str, "PEERNAME", 8) == 0) { *valp = SO_PEERNAME; return 0; }
+#endif
+#ifdef SO_ALLZONES
+ if (memcmp(str, "ALLZONES", 8) == 0) { *valp = SO_ALLZONES; return 0; }
+#endif
+#ifdef SO_PROTOCOL
+ if (memcmp(str, "PROTOCOL", 8) == 0) { *valp = SO_PROTOCOL; return 0; }
+#endif
+#ifdef SO_RXQ_OVFL
+ if (memcmp(str, "RXQ_OVFL", 8) == 0) { *valp = SO_RXQ_OVFL; return 0; }
+#endif
+#ifdef SO_PEEK_OFF
+ if (memcmp(str, "PEEK_OFF", 8) == 0) { *valp = SO_PEEK_OFF; return 0; }
+#endif
+ return -1;
+
+ case 9:
+#ifdef SO_SNDBUF
+ if (memcmp(str, "SO_SNDBUF", 9) == 0) { *valp = SO_SNDBUF; return 0; }
+#endif
+#ifdef SO_RCVBUF
+ if (memcmp(str, "SO_RCVBUF", 9) == 0) { *valp = SO_RCVBUF; return 0; }
+#endif
+#ifdef SO_LINGER
+ if (memcmp(str, "SO_LINGER", 9) == 0) { *valp = SO_LINGER; return 0; }
+#endif
+#ifdef SO_DOMAIN
+ if (memcmp(str, "SO_DOMAIN", 9) == 0) { *valp = SO_DOMAIN; return 0; }
+#endif
+#ifdef SO_REUSEADDR
+ if (memcmp(str, "REUSEADDR", 9) == 0) { *valp = SO_REUSEADDR; return 0; }
+#endif
+#ifdef SO_REUSEPORT
+ if (memcmp(str, "REUSEPORT", 9) == 0) { *valp = SO_REUSEPORT; return 0; }
+#endif
+#ifdef SO_DONTROUTE
+ if (memcmp(str, "DONTROUTE", 9) == 0) { *valp = SO_DONTROUTE; return 0; }
+#endif
+#ifdef SO_BROADCAST
+ if (memcmp(str, "BROADCAST", 9) == 0) { *valp = SO_BROADCAST; return 0; }
+#endif
+#ifdef SO_KEEPALIVE
+ if (memcmp(str, "KEEPALIVE", 9) == 0) { *valp = SO_KEEPALIVE; return 0; }
+#endif
+#ifdef SO_OOBINLINE
+ if (memcmp(str, "OOBINLINE", 9) == 0) { *valp = SO_OOBINLINE; return 0; }
+#endif
+#ifdef SO_DONTTRUNC
+ if (memcmp(str, "DONTTRUNC", 9) == 0) { *valp = SO_DONTTRUNC; return 0; }
+#endif
+#ifdef SO_NOSIGPIPE
+ if (memcmp(str, "NOSIGPIPE", 9) == 0) { *valp = SO_NOSIGPIPE; return 0; }
+#endif
+#ifdef SO_TIMESTAMP
+ if (memcmp(str, "TIMESTAMP", 9) == 0) { *valp = SO_TIMESTAMP; return 0; }
+#endif
+#ifdef SO_RECVUCRED
+ if (memcmp(str, "RECVUCRED", 9) == 0) { *valp = SO_RECVUCRED; return 0; }
+#endif
+#ifdef SO_BUSY_POLL
+ if (memcmp(str, "BUSY_POLL", 9) == 0) { *valp = SO_BUSY_POLL; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef SO_BINTIME
+ if (memcmp(str, "SO_BINTIME", 10) == 0) { *valp = SO_BINTIME; return 0; }
+#endif
+#ifdef SO_PEERSEC
+ if (memcmp(str, "SO_PEERSEC", 10) == 0) { *valp = SO_PEERSEC; return 0; }
+#endif
+#ifdef SO_PASSSEC
+ if (memcmp(str, "SO_PASSSEC", 10) == 0) { *valp = SO_PASSSEC; return 0; }
+#endif
+#ifdef SO_ACCEPTCONN
+ if (memcmp(str, "ACCEPTCONN", 10) == 0) { *valp = SO_ACCEPTCONN; return 0; }
+#endif
+#ifdef SO_GET_FILTER
+ if (memcmp(str, "GET_FILTER", 10) == 0) { *valp = SO_GET_FILTER; return 0; }
+#endif
+#ifdef SO_MAC_EXEMPT
+ if (memcmp(str, "MAC_EXEMPT", 10) == 0) { *valp = SO_MAC_EXEMPT; return 0; }
+#endif
+ return -1;
+
+ case 11:
+#ifdef SO_NO_CHECK
+ if (memcmp(str, "SO_NO_CHECK", 11) == 0) { *valp = SO_NO_CHECK; return 0; }
+#endif
+#ifdef SO_PRIORITY
+ if (memcmp(str, "SO_PRIORITY", 11) == 0) { *valp = SO_PRIORITY; return 0; }
+#endif
+#ifdef SO_PASSCRED
+ if (memcmp(str, "SO_PASSCRED", 11) == 0) { *valp = SO_PASSCRED; return 0; }
+#endif
+#ifdef SO_PEERCRED
+ if (memcmp(str, "SO_PEERCRED", 11) == 0) { *valp = SO_PEERCRED; return 0; }
+#endif
+#ifdef SO_RCVLOWAT
+ if (memcmp(str, "SO_RCVLOWAT", 11) == 0) { *valp = SO_RCVLOWAT; return 0; }
+#endif
+#ifdef SO_SNDLOWAT
+ if (memcmp(str, "SO_SNDLOWAT", 11) == 0) { *valp = SO_SNDLOWAT; return 0; }
+#endif
+#ifdef SO_RCVTIMEO
+ if (memcmp(str, "SO_RCVTIMEO", 11) == 0) { *valp = SO_RCVTIMEO; return 0; }
+#endif
+#ifdef SO_SNDTIMEO
+ if (memcmp(str, "SO_SNDTIMEO", 11) == 0) { *valp = SO_SNDTIMEO; return 0; }
+#endif
+#ifdef SO_WANTMORE
+ if (memcmp(str, "SO_WANTMORE", 11) == 0) { *valp = SO_WANTMORE; return 0; }
+#endif
+#ifdef SO_PEERNAME
+ if (memcmp(str, "SO_PEERNAME", 11) == 0) { *valp = SO_PEERNAME; return 0; }
+#endif
+#ifdef SO_ALLZONES
+ if (memcmp(str, "SO_ALLZONES", 11) == 0) { *valp = SO_ALLZONES; return 0; }
+#endif
+#ifdef SO_PROTOCOL
+ if (memcmp(str, "SO_PROTOCOL", 11) == 0) { *valp = SO_PROTOCOL; return 0; }
+#endif
+#ifdef SO_RXQ_OVFL
+ if (memcmp(str, "SO_RXQ_OVFL", 11) == 0) { *valp = SO_RXQ_OVFL; return 0; }
+#endif
+#ifdef SO_PEEK_OFF
+ if (memcmp(str, "SO_PEEK_OFF", 11) == 0) { *valp = SO_PEEK_OFF; return 0; }
+#endif
+#ifdef SO_SNDBUFFORCE
+ if (memcmp(str, "SNDBUFFORCE", 11) == 0) { *valp = SO_SNDBUFFORCE; return 0; }
+#endif
+#ifdef SO_RCVBUFFORCE
+ if (memcmp(str, "RCVBUFFORCE", 11) == 0) { *valp = SO_RCVBUFFORCE; return 0; }
+#endif
+#ifdef SO_USELOOPBACK
+ if (memcmp(str, "USELOOPBACK", 11) == 0) { *valp = SO_USELOOPBACK; return 0; }
+#endif
+#ifdef SO_WANTOOBFLAG
+ if (memcmp(str, "WANTOOBFLAG", 11) == 0) { *valp = SO_WANTOOBFLAG; return 0; }
+#endif
+#ifdef SO_TIMESTAMPNS
+ if (memcmp(str, "TIMESTAMPNS", 11) == 0) { *valp = SO_TIMESTAMPNS; return 0; }
+#endif
+#ifdef SO_WIFI_STATUS
+ if (memcmp(str, "WIFI_STATUS", 11) == 0) { *valp = SO_WIFI_STATUS; return 0; }
+#endif
+#ifdef SO_LOCK_FILTER
+ if (memcmp(str, "LOCK_FILTER", 11) == 0) { *valp = SO_LOCK_FILTER; return 0; }
+#endif
+ return -1;
+
+ case 12:
+#ifdef SO_REUSEADDR
+ if (memcmp(str, "SO_REUSEADDR", 12) == 0) { *valp = SO_REUSEADDR; return 0; }
+#endif
+#ifdef SO_REUSEPORT
+ if (memcmp(str, "SO_REUSEPORT", 12) == 0) { *valp = SO_REUSEPORT; return 0; }
+#endif
+#ifdef SO_DONTROUTE
+ if (memcmp(str, "SO_DONTROUTE", 12) == 0) { *valp = SO_DONTROUTE; return 0; }
+#endif
+#ifdef SO_BROADCAST
+ if (memcmp(str, "SO_BROADCAST", 12) == 0) { *valp = SO_BROADCAST; return 0; }
+#endif
+#ifdef SO_KEEPALIVE
+ if (memcmp(str, "SO_KEEPALIVE", 12) == 0) { *valp = SO_KEEPALIVE; return 0; }
+#endif
+#ifdef SO_OOBINLINE
+ if (memcmp(str, "SO_OOBINLINE", 12) == 0) { *valp = SO_OOBINLINE; return 0; }
+#endif
+#ifdef SO_DONTTRUNC
+ if (memcmp(str, "SO_DONTTRUNC", 12) == 0) { *valp = SO_DONTTRUNC; return 0; }
+#endif
+#ifdef SO_NOSIGPIPE
+ if (memcmp(str, "SO_NOSIGPIPE", 12) == 0) { *valp = SO_NOSIGPIPE; return 0; }
+#endif
+#ifdef SO_TIMESTAMP
+ if (memcmp(str, "SO_TIMESTAMP", 12) == 0) { *valp = SO_TIMESTAMP; return 0; }
+#endif
+#ifdef SO_RECVUCRED
+ if (memcmp(str, "SO_RECVUCRED", 12) == 0) { *valp = SO_RECVUCRED; return 0; }
+#endif
+#ifdef SO_BUSY_POLL
+ if (memcmp(str, "SO_BUSY_POLL", 12) == 0) { *valp = SO_BUSY_POLL; return 0; }
+#endif
+#ifdef SO_ACCEPTFILTER
+ if (memcmp(str, "ACCEPTFILTER", 12) == 0) { *valp = SO_ACCEPTFILTER; return 0; }
+#endif
+#ifdef SO_BINDTODEVICE
+ if (memcmp(str, "BINDTODEVICE", 12) == 0) { *valp = SO_BINDTODEVICE; return 0; }
+#endif
+#ifdef SO_TIMESTAMPING
+ if (memcmp(str, "TIMESTAMPING", 12) == 0) { *valp = SO_TIMESTAMPING; return 0; }
+#endif
+ return -1;
+
+ case 13:
+#ifdef SO_ACCEPTCONN
+ if (memcmp(str, "SO_ACCEPTCONN", 13) == 0) { *valp = SO_ACCEPTCONN; return 0; }
+#endif
+#ifdef SO_GET_FILTER
+ if (memcmp(str, "SO_GET_FILTER", 13) == 0) { *valp = SO_GET_FILTER; return 0; }
+#endif
+#ifdef SO_MAC_EXEMPT
+ if (memcmp(str, "SO_MAC_EXEMPT", 13) == 0) { *valp = SO_MAC_EXEMPT; return 0; }
+#endif
+#ifdef SO_ATTACH_FILTER
+ if (memcmp(str, "ATTACH_FILTER", 13) == 0) { *valp = SO_ATTACH_FILTER; return 0; }
+#endif
+#ifdef SO_DETACH_FILTER
+ if (memcmp(str, "DETACH_FILTER", 13) == 0) { *valp = SO_DETACH_FILTER; return 0; }
+#endif
+ return -1;
+
+ case 14:
+#ifdef SO_SNDBUFFORCE
+ if (memcmp(str, "SO_SNDBUFFORCE", 14) == 0) { *valp = SO_SNDBUFFORCE; return 0; }
+#endif
+#ifdef SO_RCVBUFFORCE
+ if (memcmp(str, "SO_RCVBUFFORCE", 14) == 0) { *valp = SO_RCVBUFFORCE; return 0; }
+#endif
+#ifdef SO_USELOOPBACK
+ if (memcmp(str, "SO_USELOOPBACK", 14) == 0) { *valp = SO_USELOOPBACK; return 0; }
+#endif
+#ifdef SO_WANTOOBFLAG
+ if (memcmp(str, "SO_WANTOOBFLAG", 14) == 0) { *valp = SO_WANTOOBFLAG; return 0; }
+#endif
+#ifdef SO_TIMESTAMPNS
+ if (memcmp(str, "SO_TIMESTAMPNS", 14) == 0) { *valp = SO_TIMESTAMPNS; return 0; }
+#endif
+#ifdef SO_WIFI_STATUS
+ if (memcmp(str, "SO_WIFI_STATUS", 14) == 0) { *valp = SO_WIFI_STATUS; return 0; }
+#endif
+#ifdef SO_LOCK_FILTER
+ if (memcmp(str, "SO_LOCK_FILTER", 14) == 0) { *valp = SO_LOCK_FILTER; return 0; }
+#endif
+#ifdef SO_BPF_EXTENSIONS
+ if (memcmp(str, "BPF_EXTENSIONS", 14) == 0) { *valp = SO_BPF_EXTENSIONS; return 0; }
+#endif
+ return -1;
+
+ case 15:
+#ifdef SO_ACCEPTFILTER
+ if (memcmp(str, "SO_ACCEPTFILTER", 15) == 0) { *valp = SO_ACCEPTFILTER; return 0; }
+#endif
+#ifdef SO_BINDTODEVICE
+ if (memcmp(str, "SO_BINDTODEVICE", 15) == 0) { *valp = SO_BINDTODEVICE; return 0; }
+#endif
+#ifdef SO_TIMESTAMPING
+ if (memcmp(str, "SO_TIMESTAMPING", 15) == 0) { *valp = SO_TIMESTAMPING; return 0; }
+#endif
+#ifdef SO_MAX_PACING_RATE
+ if (memcmp(str, "MAX_PACING_RATE", 15) == 0) { *valp = SO_MAX_PACING_RATE; return 0; }
+#endif
+ return -1;
+
+ case 16:
+#ifdef SO_ATTACH_FILTER
+ if (memcmp(str, "SO_ATTACH_FILTER", 16) == 0) { *valp = SO_ATTACH_FILTER; return 0; }
+#endif
+#ifdef SO_DETACH_FILTER
+ if (memcmp(str, "SO_DETACH_FILTER", 16) == 0) { *valp = SO_DETACH_FILTER; return 0; }
+#endif
+#ifdef SO_SELECT_ERR_QUEUE
+ if (memcmp(str, "SELECT_ERR_QUEUE", 16) == 0) { *valp = SO_SELECT_ERR_QUEUE; return 0; }
+#endif
+ return -1;
+
+ case 17:
+#ifdef SO_BPF_EXTENSIONS
+ if (memcmp(str, "SO_BPF_EXTENSIONS", 17) == 0) { *valp = SO_BPF_EXTENSIONS; return 0; }
+#endif
+ return -1;
+
+ case 18:
+#ifdef SO_MAX_PACING_RATE
+ if (memcmp(str, "SO_MAX_PACING_RATE", 18) == 0) { *valp = SO_MAX_PACING_RATE; return 0; }
+#endif
+ return -1;
+
+ case 19:
+#ifdef SO_SELECT_ERR_QUEUE
+ if (memcmp(str, "SO_SELECT_ERR_QUEUE", 19) == 0) { *valp = SO_SELECT_ERR_QUEUE; return 0; }
+#endif
+ return -1;
+
+ case 23:
+#ifdef SO_SECURITY_AUTHENTICATION
+ if (memcmp(str, "SECURITY_AUTHENTICATION", 23) == 0) { *valp = SO_SECURITY_AUTHENTICATION; return 0; }
+#endif
+ return -1;
+
+ case 26:
+#ifdef SO_SECURITY_AUTHENTICATION
+ if (memcmp(str, "SO_SECURITY_AUTHENTICATION", 26) == 0) { *valp = SO_SECURITY_AUTHENTICATION; return 0; }
+#endif
+ return -1;
+
+ case 27:
+#ifdef SO_SECURITY_ENCRYPTION_NETWORK
+ if (memcmp(str, "SECURITY_ENCRYPTION_NETWORK", 27) == 0) { *valp = SO_SECURITY_ENCRYPTION_NETWORK; return 0; }
+#endif
+ return -1;
+
+ case 29:
+#ifdef SO_SECURITY_ENCRYPTION_TRANSPORT
+ if (memcmp(str, "SECURITY_ENCRYPTION_TRANSPORT", 29) == 0) { *valp = SO_SECURITY_ENCRYPTION_TRANSPORT; return 0; }
+#endif
+ return -1;
+
+ case 30:
+#ifdef SO_SECURITY_ENCRYPTION_NETWORK
+ if (memcmp(str, "SO_SECURITY_ENCRYPTION_NETWORK", 30) == 0) { *valp = SO_SECURITY_ENCRYPTION_NETWORK; return 0; }
+#endif
+ return -1;
+
+ case 32:
+#ifdef SO_SECURITY_ENCRYPTION_TRANSPORT
+ if (memcmp(str, "SO_SECURITY_ENCRYPTION_TRANSPORT", 32) == 0) { *valp = SO_SECURITY_ENCRYPTION_TRANSPORT; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+int
+rsock_ip_optname_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 3:
+#ifdef IP_TOS
+ if (memcmp(str, "TOS", 3) == 0) { *valp = IP_TOS; return 0; }
+#endif
+#ifdef IP_TTL
+ if (memcmp(str, "TTL", 3) == 0) { *valp = IP_TTL; return 0; }
+#endif
+#ifdef IP_MTU
+ if (memcmp(str, "MTU", 3) == 0) { *valp = IP_MTU; return 0; }
+#endif
+ return -1;
+
+ case 6:
+#ifdef IP_TOS
+ if (memcmp(str, "IP_TOS", 6) == 0) { *valp = IP_TOS; return 0; }
+#endif
+#ifdef IP_TTL
+ if (memcmp(str, "IP_TTL", 6) == 0) { *valp = IP_TTL; return 0; }
+#endif
+#ifdef IP_MTU
+ if (memcmp(str, "IP_MTU", 6) == 0) { *valp = IP_MTU; return 0; }
+#endif
+#ifdef IP_MINTTL
+ if (memcmp(str, "MINTTL", 6) == 0) { *valp = IP_MINTTL; return 0; }
+#endif
+#ifdef IP_RECVIF
+ if (memcmp(str, "RECVIF", 6) == 0) { *valp = IP_RECVIF; return 0; }
+#endif
+ return -1;
+
+ case 7:
+#ifdef IP_OPTIONS
+ if (memcmp(str, "OPTIONS", 7) == 0) { *valp = IP_OPTIONS; return 0; }
+#endif
+#ifdef IP_HDRINCL
+ if (memcmp(str, "HDRINCL", 7) == 0) { *valp = IP_HDRINCL; return 0; }
+#endif
+#ifdef IP_RETOPTS
+ if (memcmp(str, "RETOPTS", 7) == 0) { *valp = IP_RETOPTS; return 0; }
+#endif
+#ifdef IP_RECVTTL
+ if (memcmp(str, "RECVTTL", 7) == 0) { *valp = IP_RECVTTL; return 0; }
+#endif
+#ifdef IP_PKTINFO
+ if (memcmp(str, "PKTINFO", 7) == 0) { *valp = IP_PKTINFO; return 0; }
+#endif
+#ifdef IP_RECVERR
+ if (memcmp(str, "RECVERR", 7) == 0) { *valp = IP_RECVERR; return 0; }
+#endif
+#ifdef IP_RECVTOS
+ if (memcmp(str, "RECVTOS", 7) == 0) { *valp = IP_RECVTOS; return 0; }
+#endif
+#ifdef IP_PASSSEC
+ if (memcmp(str, "PASSSEC", 7) == 0) { *valp = IP_PASSSEC; return 0; }
+#endif
+ return -1;
+
+ case 8:
+#ifdef IP_RECVOPTS
+ if (memcmp(str, "RECVOPTS", 8) == 0) { *valp = IP_RECVOPTS; return 0; }
+#endif
+#ifdef IP_DONTFRAG
+ if (memcmp(str, "DONTFRAG", 8) == 0) { *valp = IP_DONTFRAG; return 0; }
+#endif
+#ifdef IP_RECVSLLA
+ if (memcmp(str, "RECVSLLA", 8) == 0) { *valp = IP_RECVSLLA; return 0; }
+#endif
+#ifdef IP_FREEBIND
+ if (memcmp(str, "FREEBIND", 8) == 0) { *valp = IP_FREEBIND; return 0; }
+#endif
+#ifdef IP_MSFILTER
+ if (memcmp(str, "MSFILTER", 8) == 0) { *valp = IP_MSFILTER; return 0; }
+#endif
+ return -1;
+
+ case 9:
+#ifdef IP_MINTTL
+ if (memcmp(str, "IP_MINTTL", 9) == 0) { *valp = IP_MINTTL; return 0; }
+#endif
+#ifdef IP_RECVIF
+ if (memcmp(str, "IP_RECVIF", 9) == 0) { *valp = IP_RECVIF; return 0; }
+#endif
+#ifdef IP_ONESBCAST
+ if (memcmp(str, "ONESBCAST", 9) == 0) { *valp = IP_ONESBCAST; return 0; }
+#endif
+#ifdef IP_PORTRANGE
+ if (memcmp(str, "PORTRANGE", 9) == 0) { *valp = IP_PORTRANGE; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef IP_OPTIONS
+ if (memcmp(str, "IP_OPTIONS", 10) == 0) { *valp = IP_OPTIONS; return 0; }
+#endif
+#ifdef IP_HDRINCL
+ if (memcmp(str, "IP_HDRINCL", 10) == 0) { *valp = IP_HDRINCL; return 0; }
+#endif
+#ifdef IP_RETOPTS
+ if (memcmp(str, "IP_RETOPTS", 10) == 0) { *valp = IP_RETOPTS; return 0; }
+#endif
+#ifdef IP_RECVTTL
+ if (memcmp(str, "IP_RECVTTL", 10) == 0) { *valp = IP_RECVTTL; return 0; }
+#endif
+#ifdef IP_PKTINFO
+ if (memcmp(str, "IP_PKTINFO", 10) == 0) { *valp = IP_PKTINFO; return 0; }
+#endif
+#ifdef IP_RECVERR
+ if (memcmp(str, "IP_RECVERR", 10) == 0) { *valp = IP_RECVERR; return 0; }
+#endif
+#ifdef IP_RECVTOS
+ if (memcmp(str, "IP_RECVTOS", 10) == 0) { *valp = IP_RECVTOS; return 0; }
+#endif
+#ifdef IP_PASSSEC
+ if (memcmp(str, "IP_PASSSEC", 10) == 0) { *valp = IP_PASSSEC; return 0; }
+#endif
+#ifdef IP_PKTOPTIONS
+ if (memcmp(str, "PKTOPTIONS", 10) == 0) { *valp = IP_PKTOPTIONS; return 0; }
+#endif
+ return -1;
+
+ case 11:
+#ifdef IP_RECVOPTS
+ if (memcmp(str, "IP_RECVOPTS", 11) == 0) { *valp = IP_RECVOPTS; return 0; }
+#endif
+#ifdef IP_DONTFRAG
+ if (memcmp(str, "IP_DONTFRAG", 11) == 0) { *valp = IP_DONTFRAG; return 0; }
+#endif
+#ifdef IP_RECVSLLA
+ if (memcmp(str, "IP_RECVSLLA", 11) == 0) { *valp = IP_RECVSLLA; return 0; }
+#endif
+#ifdef IP_FREEBIND
+ if (memcmp(str, "IP_FREEBIND", 11) == 0) { *valp = IP_FREEBIND; return 0; }
+#endif
+#ifdef IP_MSFILTER
+ if (memcmp(str, "IP_MSFILTER", 11) == 0) { *valp = IP_MSFILTER; return 0; }
+#endif
+#ifdef IP_RECVRETOPTS
+ if (memcmp(str, "RECVRETOPTS", 11) == 0) { *valp = IP_RECVRETOPTS; return 0; }
+#endif
+#ifdef IP_RECVDSTADDR
+ if (memcmp(str, "RECVDSTADDR", 11) == 0) { *valp = IP_RECVDSTADDR; return 0; }
+#endif
+#ifdef IP_SENDSRCADDR
+ if (memcmp(str, "SENDSRCADDR", 11) == 0) { *valp = IP_SENDSRCADDR; return 0; }
+#endif
+#ifdef IP_XFRM_POLICY
+ if (memcmp(str, "XFRM_POLICY", 11) == 0) { *valp = IP_XFRM_POLICY; return 0; }
+#endif
+#ifdef IP_TRANSPARENT
+ if (memcmp(str, "TRANSPARENT", 11) == 0) { *valp = IP_TRANSPARENT; return 0; }
+#endif
+#ifdef IP_PMTUDISC_DO
+ if (memcmp(str, "PMTUDISC_DO", 11) == 0) { *valp = IP_PMTUDISC_DO; return 0; }
+#endif
+ return -1;
+
+ case 12:
+#ifdef IP_ONESBCAST
+ if (memcmp(str, "IP_ONESBCAST", 12) == 0) { *valp = IP_ONESBCAST; return 0; }
+#endif
+#ifdef IP_PORTRANGE
+ if (memcmp(str, "IP_PORTRANGE", 12) == 0) { *valp = IP_PORTRANGE; return 0; }
+#endif
+#ifdef IP_MULTICAST_IF
+ if (memcmp(str, "MULTICAST_IF", 12) == 0) { *valp = IP_MULTICAST_IF; return 0; }
+#endif
+#ifdef IP_ROUTER_ALERT
+ if (memcmp(str, "ROUTER_ALERT", 12) == 0) { *valp = IP_ROUTER_ALERT; return 0; }
+#endif
+#ifdef IP_MTU_DISCOVER
+ if (memcmp(str, "MTU_DISCOVER", 12) == 0) { *valp = IP_MTU_DISCOVER; return 0; }
+#endif
+#ifdef IP_IPSEC_POLICY
+ if (memcmp(str, "IPSEC_POLICY", 12) == 0) { *valp = IP_IPSEC_POLICY; return 0; }
+#endif
+#ifdef IP_BLOCK_SOURCE
+ if (memcmp(str, "BLOCK_SOURCE", 12) == 0) { *valp = IP_BLOCK_SOURCE; return 0; }
+#endif
+ return -1;
+
+ case 13:
+#ifdef IP_PKTOPTIONS
+ if (memcmp(str, "IP_PKTOPTIONS", 13) == 0) { *valp = IP_PKTOPTIONS; return 0; }
+#endif
+#ifdef IP_MULTICAST_TTL
+ if (memcmp(str, "MULTICAST_TTL", 13) == 0) { *valp = IP_MULTICAST_TTL; return 0; }
+#endif
+#ifdef IP_PMTUDISC_DONT
+ if (memcmp(str, "PMTUDISC_DONT", 13) == 0) { *valp = IP_PMTUDISC_DONT; return 0; }
+#endif
+#ifdef IP_PMTUDISC_WANT
+ if (memcmp(str, "PMTUDISC_WANT", 13) == 0) { *valp = IP_PMTUDISC_WANT; return 0; }
+#endif
+ return -1;
+
+ case 14:
+#ifdef IP_RECVRETOPTS
+ if (memcmp(str, "IP_RECVRETOPTS", 14) == 0) { *valp = IP_RECVRETOPTS; return 0; }
+#endif
+#ifdef IP_RECVDSTADDR
+ if (memcmp(str, "IP_RECVDSTADDR", 14) == 0) { *valp = IP_RECVDSTADDR; return 0; }
+#endif
+#ifdef IP_SENDSRCADDR
+ if (memcmp(str, "IP_SENDSRCADDR", 14) == 0) { *valp = IP_SENDSRCADDR; return 0; }
+#endif
+#ifdef IP_XFRM_POLICY
+ if (memcmp(str, "IP_XFRM_POLICY", 14) == 0) { *valp = IP_XFRM_POLICY; return 0; }
+#endif
+#ifdef IP_TRANSPARENT
+ if (memcmp(str, "IP_TRANSPARENT", 14) == 0) { *valp = IP_TRANSPARENT; return 0; }
+#endif
+#ifdef IP_PMTUDISC_DO
+ if (memcmp(str, "IP_PMTUDISC_DO", 14) == 0) { *valp = IP_PMTUDISC_DO; return 0; }
+#endif
+#ifdef IP_MULTICAST_LOOP
+ if (memcmp(str, "MULTICAST_LOOP", 14) == 0) { *valp = IP_MULTICAST_LOOP; return 0; }
+#endif
+#ifdef IP_ADD_MEMBERSHIP
+ if (memcmp(str, "ADD_MEMBERSHIP", 14) == 0) { *valp = IP_ADD_MEMBERSHIP; return 0; }
+#endif
+#ifdef IP_UNBLOCK_SOURCE
+ if (memcmp(str, "UNBLOCK_SOURCE", 14) == 0) { *valp = IP_UNBLOCK_SOURCE; return 0; }
+#endif
+ return -1;
+
+ case 15:
+#ifdef IP_MULTICAST_IF
+ if (memcmp(str, "IP_MULTICAST_IF", 15) == 0) { *valp = IP_MULTICAST_IF; return 0; }
+#endif
+#ifdef IP_ROUTER_ALERT
+ if (memcmp(str, "IP_ROUTER_ALERT", 15) == 0) { *valp = IP_ROUTER_ALERT; return 0; }
+#endif
+#ifdef IP_MTU_DISCOVER
+ if (memcmp(str, "IP_MTU_DISCOVER", 15) == 0) { *valp = IP_MTU_DISCOVER; return 0; }
+#endif
+#ifdef IP_IPSEC_POLICY
+ if (memcmp(str, "IP_IPSEC_POLICY", 15) == 0) { *valp = IP_IPSEC_POLICY; return 0; }
+#endif
+#ifdef IP_BLOCK_SOURCE
+ if (memcmp(str, "IP_BLOCK_SOURCE", 15) == 0) { *valp = IP_BLOCK_SOURCE; return 0; }
+#endif
+#ifdef IP_DROP_MEMBERSHIP
+ if (memcmp(str, "DROP_MEMBERSHIP", 15) == 0) { *valp = IP_DROP_MEMBERSHIP; return 0; }
+#endif
+#ifdef IP_MAX_MEMBERSHIPS
+ if (memcmp(str, "MAX_MEMBERSHIPS", 15) == 0) { *valp = IP_MAX_MEMBERSHIPS; return 0; }
+#endif
+ return -1;
+
+ case 16:
+#ifdef IP_MULTICAST_TTL
+ if (memcmp(str, "IP_MULTICAST_TTL", 16) == 0) { *valp = IP_MULTICAST_TTL; return 0; }
+#endif
+#ifdef IP_PMTUDISC_DONT
+ if (memcmp(str, "IP_PMTUDISC_DONT", 16) == 0) { *valp = IP_PMTUDISC_DONT; return 0; }
+#endif
+#ifdef IP_PMTUDISC_WANT
+ if (memcmp(str, "IP_PMTUDISC_WANT", 16) == 0) { *valp = IP_PMTUDISC_WANT; return 0; }
+#endif
+ return -1;
+
+ case 17:
+#ifdef IP_MULTICAST_LOOP
+ if (memcmp(str, "IP_MULTICAST_LOOP", 17) == 0) { *valp = IP_MULTICAST_LOOP; return 0; }
+#endif
+#ifdef IP_ADD_MEMBERSHIP
+ if (memcmp(str, "IP_ADD_MEMBERSHIP", 17) == 0) { *valp = IP_ADD_MEMBERSHIP; return 0; }
+#endif
+#ifdef IP_UNBLOCK_SOURCE
+ if (memcmp(str, "IP_UNBLOCK_SOURCE", 17) == 0) { *valp = IP_UNBLOCK_SOURCE; return 0; }
+#endif
+ return -1;
+
+ case 18:
+#ifdef IP_DROP_MEMBERSHIP
+ if (memcmp(str, "IP_DROP_MEMBERSHIP", 18) == 0) { *valp = IP_DROP_MEMBERSHIP; return 0; }
+#endif
+#ifdef IP_MAX_MEMBERSHIPS
+ if (memcmp(str, "IP_MAX_MEMBERSHIPS", 18) == 0) { *valp = IP_MAX_MEMBERSHIPS; return 0; }
+#endif
+ return -1;
+
+ case 21:
+#ifdef IP_DEFAULT_MULTICAST_TTL
+ if (memcmp(str, "DEFAULT_MULTICAST_TTL", 21) == 0) { *valp = IP_DEFAULT_MULTICAST_TTL; return 0; }
+#endif
+#ifdef IP_ADD_SOURCE_MEMBERSHIP
+ if (memcmp(str, "ADD_SOURCE_MEMBERSHIP", 21) == 0) { *valp = IP_ADD_SOURCE_MEMBERSHIP; return 0; }
+#endif
+ return -1;
+
+ case 22:
+#ifdef IP_DEFAULT_MULTICAST_LOOP
+ if (memcmp(str, "DEFAULT_MULTICAST_LOOP", 22) == 0) { *valp = IP_DEFAULT_MULTICAST_LOOP; return 0; }
+#endif
+#ifdef IP_DROP_SOURCE_MEMBERSHIP
+ if (memcmp(str, "DROP_SOURCE_MEMBERSHIP", 22) == 0) { *valp = IP_DROP_SOURCE_MEMBERSHIP; return 0; }
+#endif
+ return -1;
+
+ case 24:
+#ifdef IP_DEFAULT_MULTICAST_TTL
+ if (memcmp(str, "IP_DEFAULT_MULTICAST_TTL", 24) == 0) { *valp = IP_DEFAULT_MULTICAST_TTL; return 0; }
+#endif
+#ifdef IP_ADD_SOURCE_MEMBERSHIP
+ if (memcmp(str, "IP_ADD_SOURCE_MEMBERSHIP", 24) == 0) { *valp = IP_ADD_SOURCE_MEMBERSHIP; return 0; }
+#endif
+ return -1;
+
+ case 25:
+#ifdef IP_DEFAULT_MULTICAST_LOOP
+ if (memcmp(str, "IP_DEFAULT_MULTICAST_LOOP", 25) == 0) { *valp = IP_DEFAULT_MULTICAST_LOOP; return 0; }
+#endif
+#ifdef IP_DROP_SOURCE_MEMBERSHIP
+ if (memcmp(str, "IP_DROP_SOURCE_MEMBERSHIP", 25) == 0) { *valp = IP_DROP_SOURCE_MEMBERSHIP; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+#ifdef IPPROTO_IPV6
+int
+rsock_ipv6_optname_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 5:
+#ifdef IPV6_RTHDR
+ if (memcmp(str, "RTHDR", 5) == 0) { *valp = IPV6_RTHDR; return 0; }
+#endif
+ return -1;
+
+ case 6:
+#ifdef IPV6_V6ONLY
+ if (memcmp(str, "V6ONLY", 6) == 0) { *valp = IPV6_V6ONLY; return 0; }
+#endif
+#ifdef IPV6_TCLASS
+ if (memcmp(str, "TCLASS", 6) == 0) { *valp = IPV6_TCLASS; return 0; }
+#endif
+ return -1;
+
+ case 7:
+#ifdef IPV6_DSTOPTS
+ if (memcmp(str, "DSTOPTS", 7) == 0) { *valp = IPV6_DSTOPTS; return 0; }
+#endif
+#ifdef IPV6_HOPOPTS
+ if (memcmp(str, "HOPOPTS", 7) == 0) { *valp = IPV6_HOPOPTS; return 0; }
+#endif
+#ifdef IPV6_NEXTHOP
+ if (memcmp(str, "NEXTHOP", 7) == 0) { *valp = IPV6_NEXTHOP; return 0; }
+#endif
+#ifdef IPV6_PATHMTU
+ if (memcmp(str, "PATHMTU", 7) == 0) { *valp = IPV6_PATHMTU; return 0; }
+#endif
+#ifdef IPV6_PKTINFO
+ if (memcmp(str, "PKTINFO", 7) == 0) { *valp = IPV6_PKTINFO; return 0; }
+#endif
+ return -1;
+
+ case 8:
+#ifdef IPV6_CHECKSUM
+ if (memcmp(str, "CHECKSUM", 8) == 0) { *valp = IPV6_CHECKSUM; return 0; }
+#endif
+#ifdef IPV6_DONTFRAG
+ if (memcmp(str, "DONTFRAG", 8) == 0) { *valp = IPV6_DONTFRAG; return 0; }
+#endif
+#ifdef IPV6_HOPLIMIT
+ if (memcmp(str, "HOPLIMIT", 8) == 0) { *valp = IPV6_HOPLIMIT; return 0; }
+#endif
+ return -1;
+
+ case 9:
+#ifdef IPV6_RECVRTHDR
+ if (memcmp(str, "RECVRTHDR", 9) == 0) { *valp = IPV6_RECVRTHDR; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef IPV6_RTHDR
+ if (memcmp(str, "IPV6_RTHDR", 10) == 0) { *valp = IPV6_RTHDR; return 0; }
+#endif
+#ifdef IPV6_JOIN_GROUP
+ if (memcmp(str, "JOIN_GROUP", 10) == 0) { *valp = IPV6_JOIN_GROUP; return 0; }
+#endif
+#ifdef IPV6_RECVTCLASS
+ if (memcmp(str, "RECVTCLASS", 10) == 0) { *valp = IPV6_RECVTCLASS; return 0; }
+#endif
+ return -1;
+
+ case 11:
+#ifdef IPV6_V6ONLY
+ if (memcmp(str, "IPV6_V6ONLY", 11) == 0) { *valp = IPV6_V6ONLY; return 0; }
+#endif
+#ifdef IPV6_TCLASS
+ if (memcmp(str, "IPV6_TCLASS", 11) == 0) { *valp = IPV6_TCLASS; return 0; }
+#endif
+#ifdef IPV6_LEAVE_GROUP
+ if (memcmp(str, "LEAVE_GROUP", 11) == 0) { *valp = IPV6_LEAVE_GROUP; return 0; }
+#endif
+#ifdef IPV6_RECVDSTOPTS
+ if (memcmp(str, "RECVDSTOPTS", 11) == 0) { *valp = IPV6_RECVDSTOPTS; return 0; }
+#endif
+#ifdef IPV6_RECVHOPOPTS
+ if (memcmp(str, "RECVHOPOPTS", 11) == 0) { *valp = IPV6_RECVHOPOPTS; return 0; }
+#endif
+#ifdef IPV6_RECVPKTINFO
+ if (memcmp(str, "RECVPKTINFO", 11) == 0) { *valp = IPV6_RECVPKTINFO; return 0; }
+#endif
+#ifdef IPV6_RECVPATHMTU
+ if (memcmp(str, "RECVPATHMTU", 11) == 0) { *valp = IPV6_RECVPATHMTU; return 0; }
+#endif
+#ifdef IPV6_USE_MIN_MTU
+ if (memcmp(str, "USE_MIN_MTU", 11) == 0) { *valp = IPV6_USE_MIN_MTU; return 0; }
+#endif
+ return -1;
+
+ case 12:
+#ifdef IPV6_DSTOPTS
+ if (memcmp(str, "IPV6_DSTOPTS", 12) == 0) { *valp = IPV6_DSTOPTS; return 0; }
+#endif
+#ifdef IPV6_HOPOPTS
+ if (memcmp(str, "IPV6_HOPOPTS", 12) == 0) { *valp = IPV6_HOPOPTS; return 0; }
+#endif
+#ifdef IPV6_NEXTHOP
+ if (memcmp(str, "IPV6_NEXTHOP", 12) == 0) { *valp = IPV6_NEXTHOP; return 0; }
+#endif
+#ifdef IPV6_PATHMTU
+ if (memcmp(str, "IPV6_PATHMTU", 12) == 0) { *valp = IPV6_PATHMTU; return 0; }
+#endif
+#ifdef IPV6_PKTINFO
+ if (memcmp(str, "IPV6_PKTINFO", 12) == 0) { *valp = IPV6_PKTINFO; return 0; }
+#endif
+#ifdef IPV6_MULTICAST_IF
+ if (memcmp(str, "MULTICAST_IF", 12) == 0) { *valp = IPV6_MULTICAST_IF; return 0; }
+#endif
+#ifdef IPV6_UNICAST_HOPS
+ if (memcmp(str, "UNICAST_HOPS", 12) == 0) { *valp = IPV6_UNICAST_HOPS; return 0; }
+#endif
+#ifdef IPV6_RECVHOPLIMIT
+ if (memcmp(str, "RECVHOPLIMIT", 12) == 0) { *valp = IPV6_RECVHOPLIMIT; return 0; }
+#endif
+#ifdef IPV6_RTHDRDSTOPTS
+ if (memcmp(str, "RTHDRDSTOPTS", 12) == 0) { *valp = IPV6_RTHDRDSTOPTS; return 0; }
+#endif
+#ifdef IPV6_RTHDR_TYPE_0
+ if (memcmp(str, "RTHDR_TYPE_0", 12) == 0) { *valp = IPV6_RTHDR_TYPE_0; return 0; }
+#endif
+ return -1;
+
+ case 13:
+#ifdef IPV6_CHECKSUM
+ if (memcmp(str, "IPV6_CHECKSUM", 13) == 0) { *valp = IPV6_CHECKSUM; return 0; }
+#endif
+#ifdef IPV6_DONTFRAG
+ if (memcmp(str, "IPV6_DONTFRAG", 13) == 0) { *valp = IPV6_DONTFRAG; return 0; }
+#endif
+#ifdef IPV6_HOPLIMIT
+ if (memcmp(str, "IPV6_HOPLIMIT", 13) == 0) { *valp = IPV6_HOPLIMIT; return 0; }
+#endif
+ return -1;
+
+ case 14:
+#ifdef IPV6_RECVRTHDR
+ if (memcmp(str, "IPV6_RECVRTHDR", 14) == 0) { *valp = IPV6_RECVRTHDR; return 0; }
+#endif
+#ifdef IPV6_MULTICAST_HOPS
+ if (memcmp(str, "MULTICAST_HOPS", 14) == 0) { *valp = IPV6_MULTICAST_HOPS; return 0; }
+#endif
+#ifdef IPV6_MULTICAST_LOOP
+ if (memcmp(str, "MULTICAST_LOOP", 14) == 0) { *valp = IPV6_MULTICAST_LOOP; return 0; }
+#endif
+ return -1;
+
+ case 15:
+#ifdef IPV6_JOIN_GROUP
+ if (memcmp(str, "IPV6_JOIN_GROUP", 15) == 0) { *valp = IPV6_JOIN_GROUP; return 0; }
+#endif
+#ifdef IPV6_RECVTCLASS
+ if (memcmp(str, "IPV6_RECVTCLASS", 15) == 0) { *valp = IPV6_RECVTCLASS; return 0; }
+#endif
+ return -1;
+
+ case 16:
+#ifdef IPV6_LEAVE_GROUP
+ if (memcmp(str, "IPV6_LEAVE_GROUP", 16) == 0) { *valp = IPV6_LEAVE_GROUP; return 0; }
+#endif
+#ifdef IPV6_RECVDSTOPTS
+ if (memcmp(str, "IPV6_RECVDSTOPTS", 16) == 0) { *valp = IPV6_RECVDSTOPTS; return 0; }
+#endif
+#ifdef IPV6_RECVHOPOPTS
+ if (memcmp(str, "IPV6_RECVHOPOPTS", 16) == 0) { *valp = IPV6_RECVHOPOPTS; return 0; }
+#endif
+#ifdef IPV6_RECVPKTINFO
+ if (memcmp(str, "IPV6_RECVPKTINFO", 16) == 0) { *valp = IPV6_RECVPKTINFO; return 0; }
+#endif
+#ifdef IPV6_RECVPATHMTU
+ if (memcmp(str, "IPV6_RECVPATHMTU", 16) == 0) { *valp = IPV6_RECVPATHMTU; return 0; }
+#endif
+#ifdef IPV6_USE_MIN_MTU
+ if (memcmp(str, "IPV6_USE_MIN_MTU", 16) == 0) { *valp = IPV6_USE_MIN_MTU; return 0; }
+#endif
+ return -1;
+
+ case 17:
+#ifdef IPV6_MULTICAST_IF
+ if (memcmp(str, "IPV6_MULTICAST_IF", 17) == 0) { *valp = IPV6_MULTICAST_IF; return 0; }
+#endif
+#ifdef IPV6_UNICAST_HOPS
+ if (memcmp(str, "IPV6_UNICAST_HOPS", 17) == 0) { *valp = IPV6_UNICAST_HOPS; return 0; }
+#endif
+#ifdef IPV6_RECVHOPLIMIT
+ if (memcmp(str, "IPV6_RECVHOPLIMIT", 17) == 0) { *valp = IPV6_RECVHOPLIMIT; return 0; }
+#endif
+#ifdef IPV6_RTHDRDSTOPTS
+ if (memcmp(str, "IPV6_RTHDRDSTOPTS", 17) == 0) { *valp = IPV6_RTHDRDSTOPTS; return 0; }
+#endif
+#ifdef IPV6_RTHDR_TYPE_0
+ if (memcmp(str, "IPV6_RTHDR_TYPE_0", 17) == 0) { *valp = IPV6_RTHDR_TYPE_0; return 0; }
+#endif
+ return -1;
+
+ case 19:
+#ifdef IPV6_MULTICAST_HOPS
+ if (memcmp(str, "IPV6_MULTICAST_HOPS", 19) == 0) { *valp = IPV6_MULTICAST_HOPS; return 0; }
+#endif
+#ifdef IPV6_MULTICAST_LOOP
+ if (memcmp(str, "IPV6_MULTICAST_LOOP", 19) == 0) { *valp = IPV6_MULTICAST_LOOP; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+#endif
+
+int
+rsock_tcp_optname_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 4:
+#ifdef TCP_CORK
+ if (memcmp(str, "CORK", 4) == 0) { *valp = TCP_CORK; return 0; }
+#endif
+#ifdef TCP_INFO
+ if (memcmp(str, "INFO", 4) == 0) { *valp = TCP_INFO; return 0; }
+#endif
+ return -1;
+
+ case 5:
+#ifdef TCP_NOOPT
+ if (memcmp(str, "NOOPT", 5) == 0) { *valp = TCP_NOOPT; return 0; }
+#endif
+ return -1;
+
+ case 6:
+#ifdef TCP_MAXSEG
+ if (memcmp(str, "MAXSEG", 6) == 0) { *valp = TCP_MAXSEG; return 0; }
+#endif
+#ifdef TCP_MD5SIG
+ if (memcmp(str, "MD5SIG", 6) == 0) { *valp = TCP_MD5SIG; return 0; }
+#endif
+#ifdef TCP_NOPUSH
+ if (memcmp(str, "NOPUSH", 6) == 0) { *valp = TCP_NOPUSH; return 0; }
+#endif
+#ifdef TCP_SYNCNT
+ if (memcmp(str, "SYNCNT", 6) == 0) { *valp = TCP_SYNCNT; return 0; }
+#endif
+#ifdef TCP_REPAIR
+ if (memcmp(str, "REPAIR", 6) == 0) { *valp = TCP_REPAIR; return 0; }
+#endif
+ return -1;
+
+ case 7:
+#ifdef TCP_NODELAY
+ if (memcmp(str, "NODELAY", 7) == 0) { *valp = TCP_NODELAY; return 0; }
+#endif
+#ifdef TCP_KEEPCNT
+ if (memcmp(str, "KEEPCNT", 7) == 0) { *valp = TCP_KEEPCNT; return 0; }
+#endif
+#ifdef TCP_LINGER2
+ if (memcmp(str, "LINGER2", 7) == 0) { *valp = TCP_LINGER2; return 0; }
+#endif
+ return -1;
+
+ case 8:
+#ifdef TCP_CORK
+ if (memcmp(str, "TCP_CORK", 8) == 0) { *valp = TCP_CORK; return 0; }
+#endif
+#ifdef TCP_INFO
+ if (memcmp(str, "TCP_INFO", 8) == 0) { *valp = TCP_INFO; return 0; }
+#endif
+#ifdef TCP_KEEPIDLE
+ if (memcmp(str, "KEEPIDLE", 8) == 0) { *valp = TCP_KEEPIDLE; return 0; }
+#endif
+#ifdef TCP_QUICKACK
+ if (memcmp(str, "QUICKACK", 8) == 0) { *valp = TCP_QUICKACK; return 0; }
+#endif
+#ifdef TCP_FASTOPEN
+ if (memcmp(str, "FASTOPEN", 8) == 0) { *valp = TCP_FASTOPEN; return 0; }
+#endif
+ return -1;
+
+ case 9:
+#ifdef TCP_NOOPT
+ if (memcmp(str, "TCP_NOOPT", 9) == 0) { *valp = TCP_NOOPT; return 0; }
+#endif
+#ifdef TCP_KEEPINTVL
+ if (memcmp(str, "KEEPINTVL", 9) == 0) { *valp = TCP_KEEPINTVL; return 0; }
+#endif
+#ifdef TCP_QUEUE_SEQ
+ if (memcmp(str, "QUEUE_SEQ", 9) == 0) { *valp = TCP_QUEUE_SEQ; return 0; }
+#endif
+#ifdef TCP_TIMESTAMP
+ if (memcmp(str, "TIMESTAMP", 9) == 0) { *valp = TCP_TIMESTAMP; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef TCP_MAXSEG
+ if (memcmp(str, "TCP_MAXSEG", 10) == 0) { *valp = TCP_MAXSEG; return 0; }
+#endif
+#ifdef TCP_MD5SIG
+ if (memcmp(str, "TCP_MD5SIG", 10) == 0) { *valp = TCP_MD5SIG; return 0; }
+#endif
+#ifdef TCP_NOPUSH
+ if (memcmp(str, "TCP_NOPUSH", 10) == 0) { *valp = TCP_NOPUSH; return 0; }
+#endif
+#ifdef TCP_SYNCNT
+ if (memcmp(str, "TCP_SYNCNT", 10) == 0) { *valp = TCP_SYNCNT; return 0; }
+#endif
+#ifdef TCP_REPAIR
+ if (memcmp(str, "TCP_REPAIR", 10) == 0) { *valp = TCP_REPAIR; return 0; }
+#endif
+#ifdef TCP_CONGESTION
+ if (memcmp(str, "CONGESTION", 10) == 0) { *valp = TCP_CONGESTION; return 0; }
+#endif
+ return -1;
+
+ case 11:
+#ifdef TCP_NODELAY
+ if (memcmp(str, "TCP_NODELAY", 11) == 0) { *valp = TCP_NODELAY; return 0; }
+#endif
+#ifdef TCP_KEEPCNT
+ if (memcmp(str, "TCP_KEEPCNT", 11) == 0) { *valp = TCP_KEEPCNT; return 0; }
+#endif
+#ifdef TCP_LINGER2
+ if (memcmp(str, "TCP_LINGER2", 11) == 0) { *valp = TCP_LINGER2; return 0; }
+#endif
+#ifdef TCP_THIN_DUPACK
+ if (memcmp(str, "THIN_DUPACK", 11) == 0) { *valp = TCP_THIN_DUPACK; return 0; }
+#endif
+ return -1;
+
+ case 12:
+#ifdef TCP_KEEPIDLE
+ if (memcmp(str, "TCP_KEEPIDLE", 12) == 0) { *valp = TCP_KEEPIDLE; return 0; }
+#endif
+#ifdef TCP_QUICKACK
+ if (memcmp(str, "TCP_QUICKACK", 12) == 0) { *valp = TCP_QUICKACK; return 0; }
+#endif
+#ifdef TCP_FASTOPEN
+ if (memcmp(str, "TCP_FASTOPEN", 12) == 0) { *valp = TCP_FASTOPEN; return 0; }
+#endif
+#ifdef TCP_DEFER_ACCEPT
+ if (memcmp(str, "DEFER_ACCEPT", 12) == 0) { *valp = TCP_DEFER_ACCEPT; return 0; }
+#endif
+#ifdef TCP_WINDOW_CLAMP
+ if (memcmp(str, "WINDOW_CLAMP", 12) == 0) { *valp = TCP_WINDOW_CLAMP; return 0; }
+#endif
+#ifdef TCP_REPAIR_QUEUE
+ if (memcmp(str, "REPAIR_QUEUE", 12) == 0) { *valp = TCP_REPAIR_QUEUE; return 0; }
+#endif
+#ifdef TCP_USER_TIMEOUT
+ if (memcmp(str, "USER_TIMEOUT", 12) == 0) { *valp = TCP_USER_TIMEOUT; return 0; }
+#endif
+ return -1;
+
+ case 13:
+#ifdef TCP_KEEPINTVL
+ if (memcmp(str, "TCP_KEEPINTVL", 13) == 0) { *valp = TCP_KEEPINTVL; return 0; }
+#endif
+#ifdef TCP_QUEUE_SEQ
+ if (memcmp(str, "TCP_QUEUE_SEQ", 13) == 0) { *valp = TCP_QUEUE_SEQ; return 0; }
+#endif
+#ifdef TCP_TIMESTAMP
+ if (memcmp(str, "TCP_TIMESTAMP", 13) == 0) { *valp = TCP_TIMESTAMP; return 0; }
+#endif
+ return -1;
+
+ case 14:
+#ifdef TCP_CONGESTION
+ if (memcmp(str, "TCP_CONGESTION", 14) == 0) { *valp = TCP_CONGESTION; return 0; }
+#endif
+#ifdef TCP_REPAIR_OPTIONS
+ if (memcmp(str, "REPAIR_OPTIONS", 14) == 0) { *valp = TCP_REPAIR_OPTIONS; return 0; }
+#endif
+ return -1;
+
+ case 15:
+#ifdef TCP_THIN_DUPACK
+ if (memcmp(str, "TCP_THIN_DUPACK", 15) == 0) { *valp = TCP_THIN_DUPACK; return 0; }
+#endif
+ return -1;
+
+ case 16:
+#ifdef TCP_DEFER_ACCEPT
+ if (memcmp(str, "TCP_DEFER_ACCEPT", 16) == 0) { *valp = TCP_DEFER_ACCEPT; return 0; }
+#endif
+#ifdef TCP_WINDOW_CLAMP
+ if (memcmp(str, "TCP_WINDOW_CLAMP", 16) == 0) { *valp = TCP_WINDOW_CLAMP; return 0; }
+#endif
+#ifdef TCP_REPAIR_QUEUE
+ if (memcmp(str, "TCP_REPAIR_QUEUE", 16) == 0) { *valp = TCP_REPAIR_QUEUE; return 0; }
+#endif
+#ifdef TCP_USER_TIMEOUT
+ if (memcmp(str, "TCP_USER_TIMEOUT", 16) == 0) { *valp = TCP_USER_TIMEOUT; return 0; }
+#endif
+ return -1;
+
+ case 18:
+#ifdef TCP_REPAIR_OPTIONS
+ if (memcmp(str, "TCP_REPAIR_OPTIONS", 18) == 0) { *valp = TCP_REPAIR_OPTIONS; return 0; }
+#endif
+ return -1;
+
+ case 19:
+#ifdef TCP_COOKIE_TRANSACTIONS
+ if (memcmp(str, "COOKIE_TRANSACTIONS", 19) == 0) { *valp = TCP_COOKIE_TRANSACTIONS; return 0; }
+#endif
+ return -1;
+
+ case 20:
+#ifdef TCP_THIN_LINEAR_TIMEOUTS
+ if (memcmp(str, "THIN_LINEAR_TIMEOUTS", 20) == 0) { *valp = TCP_THIN_LINEAR_TIMEOUTS; return 0; }
+#endif
+ return -1;
+
+ case 23:
+#ifdef TCP_COOKIE_TRANSACTIONS
+ if (memcmp(str, "TCP_COOKIE_TRANSACTIONS", 23) == 0) { *valp = TCP_COOKIE_TRANSACTIONS; return 0; }
+#endif
+ return -1;
+
+ case 24:
+#ifdef TCP_THIN_LINEAR_TIMEOUTS
+ if (memcmp(str, "TCP_THIN_LINEAR_TIMEOUTS", 24) == 0) { *valp = TCP_THIN_LINEAR_TIMEOUTS; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+int
+rsock_udp_optname_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 4:
+#ifdef UDP_CORK
+ if (memcmp(str, "CORK", 4) == 0) { *valp = UDP_CORK; return 0; }
+#endif
+ return -1;
+
+ case 8:
+#ifdef UDP_CORK
+ if (memcmp(str, "UDP_CORK", 8) == 0) { *valp = UDP_CORK; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+int
+rsock_shutdown_how_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 2:
+#ifdef SHUT_RD
+ if (memcmp(str, "RD", 2) == 0) { *valp = SHUT_RD; return 0; }
+#endif
+#ifdef SHUT_WR
+ if (memcmp(str, "WR", 2) == 0) { *valp = SHUT_WR; return 0; }
+#endif
+ return -1;
+
+ case 4:
+#ifdef SHUT_RDWR
+ if (memcmp(str, "RDWR", 4) == 0) { *valp = SHUT_RDWR; return 0; }
+#endif
+ return -1;
+
+ case 7:
+#ifdef SHUT_RD
+ if (memcmp(str, "SHUT_RD", 7) == 0) { *valp = SHUT_RD; return 0; }
+#endif
+#ifdef SHUT_WR
+ if (memcmp(str, "SHUT_WR", 7) == 0) { *valp = SHUT_WR; return 0; }
+#endif
+ return -1;
+
+ case 9:
+#ifdef SHUT_RDWR
+ if (memcmp(str, "SHUT_RDWR", 9) == 0) { *valp = SHUT_RDWR; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+int
+rsock_scm_optname_to_int(const char *str, long len, int *valp)
+{
+ switch (len) {
+ case 5:
+#ifdef SCM_CREDS
+ if (memcmp(str, "CREDS", 5) == 0) { *valp = SCM_CREDS; return 0; }
+#endif
+#ifdef SCM_UCRED
+ if (memcmp(str, "UCRED", 5) == 0) { *valp = SCM_UCRED; return 0; }
+#endif
+ return -1;
+
+ case 6:
+#ifdef SCM_RIGHTS
+ if (memcmp(str, "RIGHTS", 6) == 0) { *valp = SCM_RIGHTS; return 0; }
+#endif
+ return -1;
+
+ case 7:
+#ifdef SCM_BINTIME
+ if (memcmp(str, "BINTIME", 7) == 0) { *valp = SCM_BINTIME; return 0; }
+#endif
+ return -1;
+
+ case 9:
+#ifdef SCM_CREDS
+ if (memcmp(str, "SCM_CREDS", 9) == 0) { *valp = SCM_CREDS; return 0; }
+#endif
+#ifdef SCM_UCRED
+ if (memcmp(str, "SCM_UCRED", 9) == 0) { *valp = SCM_UCRED; return 0; }
+#endif
+#ifdef SCM_TIMESTAMP
+ if (memcmp(str, "TIMESTAMP", 9) == 0) { *valp = SCM_TIMESTAMP; return 0; }
+#endif
+ return -1;
+
+ case 10:
+#ifdef SCM_RIGHTS
+ if (memcmp(str, "SCM_RIGHTS", 10) == 0) { *valp = SCM_RIGHTS; return 0; }
+#endif
+ return -1;
+
+ case 11:
+#ifdef SCM_BINTIME
+ if (memcmp(str, "SCM_BINTIME", 11) == 0) { *valp = SCM_BINTIME; return 0; }
+#endif
+#ifdef SCM_TIMESTAMPNS
+ if (memcmp(str, "TIMESTAMPNS", 11) == 0) { *valp = SCM_TIMESTAMPNS; return 0; }
+#endif
+#ifdef SCM_CREDENTIALS
+ if (memcmp(str, "CREDENTIALS", 11) == 0) { *valp = SCM_CREDENTIALS; return 0; }
+#endif
+#ifdef SCM_WIFI_STATUS
+ if (memcmp(str, "WIFI_STATUS", 11) == 0) { *valp = SCM_WIFI_STATUS; return 0; }
+#endif
+ return -1;
+
+ case 12:
+#ifdef SCM_TIMESTAMPING
+ if (memcmp(str, "TIMESTAMPING", 12) == 0) { *valp = SCM_TIMESTAMPING; return 0; }
+#endif
+ return -1;
+
+ case 13:
+#ifdef SCM_TIMESTAMP
+ if (memcmp(str, "SCM_TIMESTAMP", 13) == 0) { *valp = SCM_TIMESTAMP; return 0; }
+#endif
+ return -1;
+
+ case 15:
+#ifdef SCM_TIMESTAMPNS
+ if (memcmp(str, "SCM_TIMESTAMPNS", 15) == 0) { *valp = SCM_TIMESTAMPNS; return 0; }
+#endif
+#ifdef SCM_CREDENTIALS
+ if (memcmp(str, "SCM_CREDENTIALS", 15) == 0) { *valp = SCM_CREDENTIALS; return 0; }
+#endif
+#ifdef SCM_WIFI_STATUS
+ if (memcmp(str, "SCM_WIFI_STATUS", 15) == 0) { *valp = SCM_WIFI_STATUS; return 0; }
+#endif
+ return -1;
+
+ case 16:
+#ifdef SCM_TIMESTAMPING
+ if (memcmp(str, "SCM_TIMESTAMPING", 16) == 0) { *valp = SCM_TIMESTAMPING; return 0; }
+#endif
+ return -1;
+
+ default:
+ return -1;
+ }
+}
+
+
+
+ID
+rsock_intern_family(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_family_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_family_noprefix(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_family_noprefix_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_protocol_family(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_protocol_family_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_socktype(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_socktype_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_ipproto(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_ipproto_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_iplevel(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_iplevel_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_so_optname(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_so_optname_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_ip_optname(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_ip_optname_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_ipv6_optname(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_ipv6_optname_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_tcp_optname(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_tcp_optname_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_udp_optname(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_udp_optname_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_scm_optname(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_scm_optname_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+ID
+rsock_intern_local_optname(int val)
+{
+ st_data_t name;
+ if (st_lookup(rsock_intern_local_optname_hash, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+
+
diff --git a/jni/ruby/ext/socket/constdefs.h b/jni/ruby/ext/socket/constdefs.h
new file mode 100644
index 0000000..b88eb84
--- /dev/null
+++ b/jni/ruby/ext/socket/constdefs.h
@@ -0,0 +1,2254 @@
+/* autogenerated file */
+#if !defined(SOCK_STREAM)
+# if defined(HAVE_CONST_SOCK_STREAM)
+# define SOCK_STREAM SOCK_STREAM
+# endif
+#endif
+#if !defined(SOCK_DGRAM)
+# if defined(HAVE_CONST_SOCK_DGRAM)
+# define SOCK_DGRAM SOCK_DGRAM
+# endif
+#endif
+#if !defined(SOCK_RAW)
+# if defined(HAVE_CONST_SOCK_RAW)
+# define SOCK_RAW SOCK_RAW
+# endif
+#endif
+#if !defined(SOCK_RDM)
+# if defined(HAVE_CONST_SOCK_RDM)
+# define SOCK_RDM SOCK_RDM
+# endif
+#endif
+#if !defined(SOCK_SEQPACKET)
+# if defined(HAVE_CONST_SOCK_SEQPACKET)
+# define SOCK_SEQPACKET SOCK_SEQPACKET
+# endif
+#endif
+#if !defined(SOCK_PACKET)
+# if defined(HAVE_CONST_SOCK_PACKET)
+# define SOCK_PACKET SOCK_PACKET
+# endif
+#endif
+#if !defined(AF_UNSPEC)
+# if defined(HAVE_CONST_AF_UNSPEC)
+# define AF_UNSPEC AF_UNSPEC
+# endif
+#endif
+#if !defined(PF_UNSPEC)
+# if defined(HAVE_CONST_PF_UNSPEC)
+# define PF_UNSPEC PF_UNSPEC
+# endif
+#endif
+#if !defined(AF_INET)
+# if defined(HAVE_CONST_AF_INET)
+# define AF_INET AF_INET
+# endif
+#endif
+#if !defined(PF_INET)
+# if defined(HAVE_CONST_PF_INET)
+# define PF_INET PF_INET
+# endif
+#endif
+#if !defined(AF_INET6)
+# if defined(HAVE_CONST_AF_INET6)
+# define AF_INET6 AF_INET6
+# endif
+#endif
+#if !defined(PF_INET6)
+# if defined(HAVE_CONST_PF_INET6)
+# define PF_INET6 PF_INET6
+# endif
+#endif
+#if !defined(AF_UNIX)
+# if defined(HAVE_CONST_AF_UNIX)
+# define AF_UNIX AF_UNIX
+# endif
+#endif
+#if !defined(PF_UNIX)
+# if defined(HAVE_CONST_PF_UNIX)
+# define PF_UNIX PF_UNIX
+# endif
+#endif
+#if !defined(AF_AX25)
+# if defined(HAVE_CONST_AF_AX25)
+# define AF_AX25 AF_AX25
+# endif
+#endif
+#if !defined(PF_AX25)
+# if defined(HAVE_CONST_PF_AX25)
+# define PF_AX25 PF_AX25
+# endif
+#endif
+#if !defined(AF_IPX)
+# if defined(HAVE_CONST_AF_IPX)
+# define AF_IPX AF_IPX
+# endif
+#endif
+#if !defined(PF_IPX)
+# if defined(HAVE_CONST_PF_IPX)
+# define PF_IPX PF_IPX
+# endif
+#endif
+#if !defined(AF_APPLETALK)
+# if defined(HAVE_CONST_AF_APPLETALK)
+# define AF_APPLETALK AF_APPLETALK
+# endif
+#endif
+#if !defined(PF_APPLETALK)
+# if defined(HAVE_CONST_PF_APPLETALK)
+# define PF_APPLETALK PF_APPLETALK
+# endif
+#endif
+#if !defined(AF_LOCAL)
+# if defined(HAVE_CONST_AF_LOCAL)
+# define AF_LOCAL AF_LOCAL
+# endif
+#endif
+#if !defined(PF_LOCAL)
+# if defined(HAVE_CONST_PF_LOCAL)
+# define PF_LOCAL PF_LOCAL
+# endif
+#endif
+#if !defined(AF_IMPLINK)
+# if defined(HAVE_CONST_AF_IMPLINK)
+# define AF_IMPLINK AF_IMPLINK
+# endif
+#endif
+#if !defined(PF_IMPLINK)
+# if defined(HAVE_CONST_PF_IMPLINK)
+# define PF_IMPLINK PF_IMPLINK
+# endif
+#endif
+#if !defined(AF_PUP)
+# if defined(HAVE_CONST_AF_PUP)
+# define AF_PUP AF_PUP
+# endif
+#endif
+#if !defined(PF_PUP)
+# if defined(HAVE_CONST_PF_PUP)
+# define PF_PUP PF_PUP
+# endif
+#endif
+#if !defined(AF_CHAOS)
+# if defined(HAVE_CONST_AF_CHAOS)
+# define AF_CHAOS AF_CHAOS
+# endif
+#endif
+#if !defined(PF_CHAOS)
+# if defined(HAVE_CONST_PF_CHAOS)
+# define PF_CHAOS PF_CHAOS
+# endif
+#endif
+#if !defined(AF_NS)
+# if defined(HAVE_CONST_AF_NS)
+# define AF_NS AF_NS
+# endif
+#endif
+#if !defined(PF_NS)
+# if defined(HAVE_CONST_PF_NS)
+# define PF_NS PF_NS
+# endif
+#endif
+#if !defined(AF_ISO)
+# if defined(HAVE_CONST_AF_ISO)
+# define AF_ISO AF_ISO
+# endif
+#endif
+#if !defined(PF_ISO)
+# if defined(HAVE_CONST_PF_ISO)
+# define PF_ISO PF_ISO
+# endif
+#endif
+#if !defined(AF_OSI)
+# if defined(HAVE_CONST_AF_OSI)
+# define AF_OSI AF_OSI
+# endif
+#endif
+#if !defined(PF_OSI)
+# if defined(HAVE_CONST_PF_OSI)
+# define PF_OSI PF_OSI
+# endif
+#endif
+#if !defined(AF_ECMA)
+# if defined(HAVE_CONST_AF_ECMA)
+# define AF_ECMA AF_ECMA
+# endif
+#endif
+#if !defined(PF_ECMA)
+# if defined(HAVE_CONST_PF_ECMA)
+# define PF_ECMA PF_ECMA
+# endif
+#endif
+#if !defined(AF_DATAKIT)
+# if defined(HAVE_CONST_AF_DATAKIT)
+# define AF_DATAKIT AF_DATAKIT
+# endif
+#endif
+#if !defined(PF_DATAKIT)
+# if defined(HAVE_CONST_PF_DATAKIT)
+# define PF_DATAKIT PF_DATAKIT
+# endif
+#endif
+#if !defined(AF_CCITT)
+# if defined(HAVE_CONST_AF_CCITT)
+# define AF_CCITT AF_CCITT
+# endif
+#endif
+#if !defined(PF_CCITT)
+# if defined(HAVE_CONST_PF_CCITT)
+# define PF_CCITT PF_CCITT
+# endif
+#endif
+#if !defined(AF_SNA)
+# if defined(HAVE_CONST_AF_SNA)
+# define AF_SNA AF_SNA
+# endif
+#endif
+#if !defined(PF_SNA)
+# if defined(HAVE_CONST_PF_SNA)
+# define PF_SNA PF_SNA
+# endif
+#endif
+#if !defined(AF_DEC)
+# if defined(HAVE_CONST_AF_DEC)
+# define AF_DEC AF_DEC
+# endif
+#endif
+#if !defined(PF_DEC)
+# if defined(HAVE_CONST_PF_DEC)
+# define PF_DEC PF_DEC
+# endif
+#endif
+#if !defined(AF_DLI)
+# if defined(HAVE_CONST_AF_DLI)
+# define AF_DLI AF_DLI
+# endif
+#endif
+#if !defined(PF_DLI)
+# if defined(HAVE_CONST_PF_DLI)
+# define PF_DLI PF_DLI
+# endif
+#endif
+#if !defined(AF_LAT)
+# if defined(HAVE_CONST_AF_LAT)
+# define AF_LAT AF_LAT
+# endif
+#endif
+#if !defined(PF_LAT)
+# if defined(HAVE_CONST_PF_LAT)
+# define PF_LAT PF_LAT
+# endif
+#endif
+#if !defined(AF_HYLINK)
+# if defined(HAVE_CONST_AF_HYLINK)
+# define AF_HYLINK AF_HYLINK
+# endif
+#endif
+#if !defined(PF_HYLINK)
+# if defined(HAVE_CONST_PF_HYLINK)
+# define PF_HYLINK PF_HYLINK
+# endif
+#endif
+#if !defined(AF_ROUTE)
+# if defined(HAVE_CONST_AF_ROUTE)
+# define AF_ROUTE AF_ROUTE
+# endif
+#endif
+#if !defined(PF_ROUTE)
+# if defined(HAVE_CONST_PF_ROUTE)
+# define PF_ROUTE PF_ROUTE
+# endif
+#endif
+#if !defined(AF_LINK)
+# if defined(HAVE_CONST_AF_LINK)
+# define AF_LINK AF_LINK
+# endif
+#endif
+#if !defined(PF_LINK)
+# if defined(HAVE_CONST_PF_LINK)
+# define PF_LINK PF_LINK
+# endif
+#endif
+#if !defined(AF_COIP)
+# if defined(HAVE_CONST_AF_COIP)
+# define AF_COIP AF_COIP
+# endif
+#endif
+#if !defined(PF_COIP)
+# if defined(HAVE_CONST_PF_COIP)
+# define PF_COIP PF_COIP
+# endif
+#endif
+#if !defined(AF_CNT)
+# if defined(HAVE_CONST_AF_CNT)
+# define AF_CNT AF_CNT
+# endif
+#endif
+#if !defined(PF_CNT)
+# if defined(HAVE_CONST_PF_CNT)
+# define PF_CNT PF_CNT
+# endif
+#endif
+#if !defined(AF_SIP)
+# if defined(HAVE_CONST_AF_SIP)
+# define AF_SIP AF_SIP
+# endif
+#endif
+#if !defined(PF_SIP)
+# if defined(HAVE_CONST_PF_SIP)
+# define PF_SIP PF_SIP
+# endif
+#endif
+#if !defined(AF_NDRV)
+# if defined(HAVE_CONST_AF_NDRV)
+# define AF_NDRV AF_NDRV
+# endif
+#endif
+#if !defined(PF_NDRV)
+# if defined(HAVE_CONST_PF_NDRV)
+# define PF_NDRV PF_NDRV
+# endif
+#endif
+#if !defined(AF_ISDN)
+# if defined(HAVE_CONST_AF_ISDN)
+# define AF_ISDN AF_ISDN
+# endif
+#endif
+#if !defined(PF_ISDN)
+# if defined(HAVE_CONST_PF_ISDN)
+# define PF_ISDN PF_ISDN
+# endif
+#endif
+#if !defined(AF_NATM)
+# if defined(HAVE_CONST_AF_NATM)
+# define AF_NATM AF_NATM
+# endif
+#endif
+#if !defined(PF_NATM)
+# if defined(HAVE_CONST_PF_NATM)
+# define PF_NATM PF_NATM
+# endif
+#endif
+#if !defined(AF_SYSTEM)
+# if defined(HAVE_CONST_AF_SYSTEM)
+# define AF_SYSTEM AF_SYSTEM
+# endif
+#endif
+#if !defined(PF_SYSTEM)
+# if defined(HAVE_CONST_PF_SYSTEM)
+# define PF_SYSTEM PF_SYSTEM
+# endif
+#endif
+#if !defined(AF_NETBIOS)
+# if defined(HAVE_CONST_AF_NETBIOS)
+# define AF_NETBIOS AF_NETBIOS
+# endif
+#endif
+#if !defined(PF_NETBIOS)
+# if defined(HAVE_CONST_PF_NETBIOS)
+# define PF_NETBIOS PF_NETBIOS
+# endif
+#endif
+#if !defined(AF_PPP)
+# if defined(HAVE_CONST_AF_PPP)
+# define AF_PPP AF_PPP
+# endif
+#endif
+#if !defined(PF_PPP)
+# if defined(HAVE_CONST_PF_PPP)
+# define PF_PPP PF_PPP
+# endif
+#endif
+#if !defined(AF_ATM)
+# if defined(HAVE_CONST_AF_ATM)
+# define AF_ATM AF_ATM
+# endif
+#endif
+#if !defined(PF_ATM)
+# if defined(HAVE_CONST_PF_ATM)
+# define PF_ATM PF_ATM
+# endif
+#endif
+#if !defined(AF_NETGRAPH)
+# if defined(HAVE_CONST_AF_NETGRAPH)
+# define AF_NETGRAPH AF_NETGRAPH
+# endif
+#endif
+#if !defined(PF_NETGRAPH)
+# if defined(HAVE_CONST_PF_NETGRAPH)
+# define PF_NETGRAPH PF_NETGRAPH
+# endif
+#endif
+#if !defined(AF_MAX)
+# if defined(HAVE_CONST_AF_MAX)
+# define AF_MAX AF_MAX
+# endif
+#endif
+#if !defined(PF_MAX)
+# if defined(HAVE_CONST_PF_MAX)
+# define PF_MAX PF_MAX
+# endif
+#endif
+#if !defined(AF_PACKET)
+# if defined(HAVE_CONST_AF_PACKET)
+# define AF_PACKET AF_PACKET
+# endif
+#endif
+#if !defined(PF_PACKET)
+# if defined(HAVE_CONST_PF_PACKET)
+# define PF_PACKET PF_PACKET
+# endif
+#endif
+#if !defined(AF_E164)
+# if defined(HAVE_CONST_AF_E164)
+# define AF_E164 AF_E164
+# endif
+#endif
+#if !defined(PF_XTP)
+# if defined(HAVE_CONST_PF_XTP)
+# define PF_XTP PF_XTP
+# endif
+#endif
+#if !defined(PF_RTIP)
+# if defined(HAVE_CONST_PF_RTIP)
+# define PF_RTIP PF_RTIP
+# endif
+#endif
+#if !defined(PF_PIP)
+# if defined(HAVE_CONST_PF_PIP)
+# define PF_PIP PF_PIP
+# endif
+#endif
+#if !defined(PF_KEY)
+# if defined(HAVE_CONST_PF_KEY)
+# define PF_KEY PF_KEY
+# endif
+#endif
+#if !defined(MSG_OOB)
+# if defined(HAVE_CONST_MSG_OOB)
+# define MSG_OOB MSG_OOB
+# endif
+#endif
+#if !defined(MSG_PEEK)
+# if defined(HAVE_CONST_MSG_PEEK)
+# define MSG_PEEK MSG_PEEK
+# endif
+#endif
+#if !defined(MSG_DONTROUTE)
+# if defined(HAVE_CONST_MSG_DONTROUTE)
+# define MSG_DONTROUTE MSG_DONTROUTE
+# endif
+#endif
+#if !defined(MSG_EOR)
+# if defined(HAVE_CONST_MSG_EOR)
+# define MSG_EOR MSG_EOR
+# endif
+#endif
+#if !defined(MSG_TRUNC)
+# if defined(HAVE_CONST_MSG_TRUNC)
+# define MSG_TRUNC MSG_TRUNC
+# endif
+#endif
+#if !defined(MSG_CTRUNC)
+# if defined(HAVE_CONST_MSG_CTRUNC)
+# define MSG_CTRUNC MSG_CTRUNC
+# endif
+#endif
+#if !defined(MSG_WAITALL)
+# if defined(HAVE_CONST_MSG_WAITALL)
+# define MSG_WAITALL MSG_WAITALL
+# endif
+#endif
+#if !defined(MSG_DONTWAIT)
+# if defined(HAVE_CONST_MSG_DONTWAIT)
+# define MSG_DONTWAIT MSG_DONTWAIT
+# endif
+#endif
+#if !defined(MSG_EOF)
+# if defined(HAVE_CONST_MSG_EOF)
+# define MSG_EOF MSG_EOF
+# endif
+#endif
+#if !defined(MSG_FLUSH)
+# if defined(HAVE_CONST_MSG_FLUSH)
+# define MSG_FLUSH MSG_FLUSH
+# endif
+#endif
+#if !defined(MSG_HOLD)
+# if defined(HAVE_CONST_MSG_HOLD)
+# define MSG_HOLD MSG_HOLD
+# endif
+#endif
+#if !defined(MSG_SEND)
+# if defined(HAVE_CONST_MSG_SEND)
+# define MSG_SEND MSG_SEND
+# endif
+#endif
+#if !defined(MSG_HAVEMORE)
+# if defined(HAVE_CONST_MSG_HAVEMORE)
+# define MSG_HAVEMORE MSG_HAVEMORE
+# endif
+#endif
+#if !defined(MSG_RCVMORE)
+# if defined(HAVE_CONST_MSG_RCVMORE)
+# define MSG_RCVMORE MSG_RCVMORE
+# endif
+#endif
+#if !defined(MSG_COMPAT)
+# if defined(HAVE_CONST_MSG_COMPAT)
+# define MSG_COMPAT MSG_COMPAT
+# endif
+#endif
+#if !defined(MSG_PROXY)
+# if defined(HAVE_CONST_MSG_PROXY)
+# define MSG_PROXY MSG_PROXY
+# endif
+#endif
+#if !defined(MSG_FIN)
+# if defined(HAVE_CONST_MSG_FIN)
+# define MSG_FIN MSG_FIN
+# endif
+#endif
+#if !defined(MSG_SYN)
+# if defined(HAVE_CONST_MSG_SYN)
+# define MSG_SYN MSG_SYN
+# endif
+#endif
+#if !defined(MSG_CONFIRM)
+# if defined(HAVE_CONST_MSG_CONFIRM)
+# define MSG_CONFIRM MSG_CONFIRM
+# endif
+#endif
+#if !defined(MSG_RST)
+# if defined(HAVE_CONST_MSG_RST)
+# define MSG_RST MSG_RST
+# endif
+#endif
+#if !defined(MSG_ERRQUEUE)
+# if defined(HAVE_CONST_MSG_ERRQUEUE)
+# define MSG_ERRQUEUE MSG_ERRQUEUE
+# endif
+#endif
+#if !defined(MSG_NOSIGNAL)
+# if defined(HAVE_CONST_MSG_NOSIGNAL)
+# define MSG_NOSIGNAL MSG_NOSIGNAL
+# endif
+#endif
+#if !defined(MSG_MORE)
+# if defined(HAVE_CONST_MSG_MORE)
+# define MSG_MORE MSG_MORE
+# endif
+#endif
+#if !defined(MSG_FASTOPEN)
+# if defined(HAVE_CONST_MSG_FASTOPEN)
+# define MSG_FASTOPEN MSG_FASTOPEN
+# endif
+#endif
+#if !defined(SOL_SOCKET)
+# if defined(HAVE_CONST_SOL_SOCKET)
+# define SOL_SOCKET SOL_SOCKET
+# endif
+#endif
+#if !defined(SOL_IP)
+# if defined(HAVE_CONST_SOL_IP)
+# define SOL_IP SOL_IP
+# endif
+#endif
+#if !defined(SOL_IPX)
+# if defined(HAVE_CONST_SOL_IPX)
+# define SOL_IPX SOL_IPX
+# endif
+#endif
+#if !defined(SOL_AX25)
+# if defined(HAVE_CONST_SOL_AX25)
+# define SOL_AX25 SOL_AX25
+# endif
+#endif
+#if !defined(SOL_ATALK)
+# if defined(HAVE_CONST_SOL_ATALK)
+# define SOL_ATALK SOL_ATALK
+# endif
+#endif
+#if !defined(SOL_TCP)
+# if defined(HAVE_CONST_SOL_TCP)
+# define SOL_TCP SOL_TCP
+# endif
+#endif
+#if !defined(SOL_UDP)
+# if defined(HAVE_CONST_SOL_UDP)
+# define SOL_UDP SOL_UDP
+# endif
+#endif
+#if !defined(IPPROTO_IP)
+# if defined(HAVE_CONST_IPPROTO_IP)
+# define IPPROTO_IP IPPROTO_IP
+# else
+# define IPPROTO_IP 0
+# endif
+#endif
+#if !defined(IPPROTO_ICMP)
+# if defined(HAVE_CONST_IPPROTO_ICMP)
+# define IPPROTO_ICMP IPPROTO_ICMP
+# else
+# define IPPROTO_ICMP 1
+# endif
+#endif
+#if !defined(IPPROTO_IGMP)
+# if defined(HAVE_CONST_IPPROTO_IGMP)
+# define IPPROTO_IGMP IPPROTO_IGMP
+# endif
+#endif
+#if !defined(IPPROTO_GGP)
+# if defined(HAVE_CONST_IPPROTO_GGP)
+# define IPPROTO_GGP IPPROTO_GGP
+# endif
+#endif
+#if !defined(IPPROTO_TCP)
+# if defined(HAVE_CONST_IPPROTO_TCP)
+# define IPPROTO_TCP IPPROTO_TCP
+# else
+# define IPPROTO_TCP 6
+# endif
+#endif
+#if !defined(IPPROTO_EGP)
+# if defined(HAVE_CONST_IPPROTO_EGP)
+# define IPPROTO_EGP IPPROTO_EGP
+# endif
+#endif
+#if !defined(IPPROTO_PUP)
+# if defined(HAVE_CONST_IPPROTO_PUP)
+# define IPPROTO_PUP IPPROTO_PUP
+# endif
+#endif
+#if !defined(IPPROTO_UDP)
+# if defined(HAVE_CONST_IPPROTO_UDP)
+# define IPPROTO_UDP IPPROTO_UDP
+# else
+# define IPPROTO_UDP 17
+# endif
+#endif
+#if !defined(IPPROTO_IDP)
+# if defined(HAVE_CONST_IPPROTO_IDP)
+# define IPPROTO_IDP IPPROTO_IDP
+# endif
+#endif
+#if !defined(IPPROTO_HELLO)
+# if defined(HAVE_CONST_IPPROTO_HELLO)
+# define IPPROTO_HELLO IPPROTO_HELLO
+# endif
+#endif
+#if !defined(IPPROTO_ND)
+# if defined(HAVE_CONST_IPPROTO_ND)
+# define IPPROTO_ND IPPROTO_ND
+# endif
+#endif
+#if !defined(IPPROTO_TP)
+# if defined(HAVE_CONST_IPPROTO_TP)
+# define IPPROTO_TP IPPROTO_TP
+# endif
+#endif
+#if !defined(IPPROTO_XTP)
+# if defined(HAVE_CONST_IPPROTO_XTP)
+# define IPPROTO_XTP IPPROTO_XTP
+# endif
+#endif
+#if !defined(IPPROTO_EON)
+# if defined(HAVE_CONST_IPPROTO_EON)
+# define IPPROTO_EON IPPROTO_EON
+# endif
+#endif
+#if !defined(IPPROTO_BIP)
+# if defined(HAVE_CONST_IPPROTO_BIP)
+# define IPPROTO_BIP IPPROTO_BIP
+# endif
+#endif
+#if !defined(IPPROTO_AH)
+# if defined(HAVE_CONST_IPPROTO_AH)
+# define IPPROTO_AH IPPROTO_AH
+# endif
+#endif
+#if !defined(IPPROTO_DSTOPTS)
+# if defined(HAVE_CONST_IPPROTO_DSTOPTS)
+# define IPPROTO_DSTOPTS IPPROTO_DSTOPTS
+# endif
+#endif
+#if !defined(IPPROTO_ESP)
+# if defined(HAVE_CONST_IPPROTO_ESP)
+# define IPPROTO_ESP IPPROTO_ESP
+# endif
+#endif
+#if !defined(IPPROTO_FRAGMENT)
+# if defined(HAVE_CONST_IPPROTO_FRAGMENT)
+# define IPPROTO_FRAGMENT IPPROTO_FRAGMENT
+# endif
+#endif
+#if !defined(IPPROTO_HOPOPTS)
+# if defined(HAVE_CONST_IPPROTO_HOPOPTS)
+# define IPPROTO_HOPOPTS IPPROTO_HOPOPTS
+# endif
+#endif
+#if !defined(IPPROTO_ICMPV6)
+# if defined(HAVE_CONST_IPPROTO_ICMPV6)
+# define IPPROTO_ICMPV6 IPPROTO_ICMPV6
+# endif
+#endif
+#if !defined(IPPROTO_IPV6)
+# if defined(HAVE_CONST_IPPROTO_IPV6)
+# define IPPROTO_IPV6 IPPROTO_IPV6
+# endif
+#endif
+#if !defined(IPPROTO_NONE)
+# if defined(HAVE_CONST_IPPROTO_NONE)
+# define IPPROTO_NONE IPPROTO_NONE
+# endif
+#endif
+#if !defined(IPPROTO_ROUTING)
+# if defined(HAVE_CONST_IPPROTO_ROUTING)
+# define IPPROTO_ROUTING IPPROTO_ROUTING
+# endif
+#endif
+#if !defined(IPPROTO_RAW)
+# if defined(HAVE_CONST_IPPROTO_RAW)
+# define IPPROTO_RAW IPPROTO_RAW
+# else
+# define IPPROTO_RAW 255
+# endif
+#endif
+#if !defined(IPPROTO_MAX)
+# if defined(HAVE_CONST_IPPROTO_MAX)
+# define IPPROTO_MAX IPPROTO_MAX
+# endif
+#endif
+#if !defined(IPPORT_RESERVED)
+# if defined(HAVE_CONST_IPPORT_RESERVED)
+# define IPPORT_RESERVED IPPORT_RESERVED
+# else
+# define IPPORT_RESERVED 1024
+# endif
+#endif
+#if !defined(IPPORT_USERRESERVED)
+# if defined(HAVE_CONST_IPPORT_USERRESERVED)
+# define IPPORT_USERRESERVED IPPORT_USERRESERVED
+# else
+# define IPPORT_USERRESERVED 5000
+# endif
+#endif
+#if !defined(INADDR_ANY)
+# if defined(HAVE_CONST_INADDR_ANY)
+# define INADDR_ANY INADDR_ANY
+# else
+# define INADDR_ANY 0x00000000
+# endif
+#endif
+#if !defined(INADDR_BROADCAST)
+# if defined(HAVE_CONST_INADDR_BROADCAST)
+# define INADDR_BROADCAST INADDR_BROADCAST
+# else
+# define INADDR_BROADCAST 0xffffffff
+# endif
+#endif
+#if !defined(INADDR_LOOPBACK)
+# if defined(HAVE_CONST_INADDR_LOOPBACK)
+# define INADDR_LOOPBACK INADDR_LOOPBACK
+# else
+# define INADDR_LOOPBACK 0x7F000001
+# endif
+#endif
+#if !defined(INADDR_UNSPEC_GROUP)
+# if defined(HAVE_CONST_INADDR_UNSPEC_GROUP)
+# define INADDR_UNSPEC_GROUP INADDR_UNSPEC_GROUP
+# else
+# define INADDR_UNSPEC_GROUP 0xe0000000
+# endif
+#endif
+#if !defined(INADDR_ALLHOSTS_GROUP)
+# if defined(HAVE_CONST_INADDR_ALLHOSTS_GROUP)
+# define INADDR_ALLHOSTS_GROUP INADDR_ALLHOSTS_GROUP
+# else
+# define INADDR_ALLHOSTS_GROUP 0xe0000001
+# endif
+#endif
+#if !defined(INADDR_MAX_LOCAL_GROUP)
+# if defined(HAVE_CONST_INADDR_MAX_LOCAL_GROUP)
+# define INADDR_MAX_LOCAL_GROUP INADDR_MAX_LOCAL_GROUP
+# else
+# define INADDR_MAX_LOCAL_GROUP 0xe00000ff
+# endif
+#endif
+#if !defined(INADDR_NONE)
+# if defined(HAVE_CONST_INADDR_NONE)
+# define INADDR_NONE INADDR_NONE
+# else
+# define INADDR_NONE 0xffffffff
+# endif
+#endif
+#if !defined(IP_OPTIONS)
+# if defined(HAVE_CONST_IP_OPTIONS)
+# define IP_OPTIONS IP_OPTIONS
+# endif
+#endif
+#if !defined(IP_HDRINCL)
+# if defined(HAVE_CONST_IP_HDRINCL)
+# define IP_HDRINCL IP_HDRINCL
+# endif
+#endif
+#if !defined(IP_TOS)
+# if defined(HAVE_CONST_IP_TOS)
+# define IP_TOS IP_TOS
+# endif
+#endif
+#if !defined(IP_TTL)
+# if defined(HAVE_CONST_IP_TTL)
+# define IP_TTL IP_TTL
+# endif
+#endif
+#if !defined(IP_RECVOPTS)
+# if defined(HAVE_CONST_IP_RECVOPTS)
+# define IP_RECVOPTS IP_RECVOPTS
+# endif
+#endif
+#if !defined(IP_RECVRETOPTS)
+# if defined(HAVE_CONST_IP_RECVRETOPTS)
+# define IP_RECVRETOPTS IP_RECVRETOPTS
+# endif
+#endif
+#if !defined(IP_RECVDSTADDR)
+# if defined(HAVE_CONST_IP_RECVDSTADDR)
+# define IP_RECVDSTADDR IP_RECVDSTADDR
+# endif
+#endif
+#if !defined(IP_RETOPTS)
+# if defined(HAVE_CONST_IP_RETOPTS)
+# define IP_RETOPTS IP_RETOPTS
+# endif
+#endif
+#if !defined(IP_MINTTL)
+# if defined(HAVE_CONST_IP_MINTTL)
+# define IP_MINTTL IP_MINTTL
+# endif
+#endif
+#if !defined(IP_DONTFRAG)
+# if defined(HAVE_CONST_IP_DONTFRAG)
+# define IP_DONTFRAG IP_DONTFRAG
+# endif
+#endif
+#if !defined(IP_SENDSRCADDR)
+# if defined(HAVE_CONST_IP_SENDSRCADDR)
+# define IP_SENDSRCADDR IP_SENDSRCADDR
+# endif
+#endif
+#if !defined(IP_ONESBCAST)
+# if defined(HAVE_CONST_IP_ONESBCAST)
+# define IP_ONESBCAST IP_ONESBCAST
+# endif
+#endif
+#if !defined(IP_RECVTTL)
+# if defined(HAVE_CONST_IP_RECVTTL)
+# define IP_RECVTTL IP_RECVTTL
+# endif
+#endif
+#if !defined(IP_RECVIF)
+# if defined(HAVE_CONST_IP_RECVIF)
+# define IP_RECVIF IP_RECVIF
+# endif
+#endif
+#if !defined(IP_RECVSLLA)
+# if defined(HAVE_CONST_IP_RECVSLLA)
+# define IP_RECVSLLA IP_RECVSLLA
+# endif
+#endif
+#if !defined(IP_PORTRANGE)
+# if defined(HAVE_CONST_IP_PORTRANGE)
+# define IP_PORTRANGE IP_PORTRANGE
+# endif
+#endif
+#if !defined(IP_MULTICAST_IF)
+# if defined(HAVE_CONST_IP_MULTICAST_IF)
+# define IP_MULTICAST_IF IP_MULTICAST_IF
+# endif
+#endif
+#if !defined(IP_MULTICAST_TTL)
+# if defined(HAVE_CONST_IP_MULTICAST_TTL)
+# define IP_MULTICAST_TTL IP_MULTICAST_TTL
+# endif
+#endif
+#if !defined(IP_MULTICAST_LOOP)
+# if defined(HAVE_CONST_IP_MULTICAST_LOOP)
+# define IP_MULTICAST_LOOP IP_MULTICAST_LOOP
+# endif
+#endif
+#if !defined(IP_ADD_MEMBERSHIP)
+# if defined(HAVE_CONST_IP_ADD_MEMBERSHIP)
+# define IP_ADD_MEMBERSHIP IP_ADD_MEMBERSHIP
+# endif
+#endif
+#if !defined(IP_DROP_MEMBERSHIP)
+# if defined(HAVE_CONST_IP_DROP_MEMBERSHIP)
+# define IP_DROP_MEMBERSHIP IP_DROP_MEMBERSHIP
+# endif
+#endif
+#if !defined(IP_DEFAULT_MULTICAST_TTL)
+# if defined(HAVE_CONST_IP_DEFAULT_MULTICAST_TTL)
+# define IP_DEFAULT_MULTICAST_TTL IP_DEFAULT_MULTICAST_TTL
+# endif
+#endif
+#if !defined(IP_DEFAULT_MULTICAST_LOOP)
+# if defined(HAVE_CONST_IP_DEFAULT_MULTICAST_LOOP)
+# define IP_DEFAULT_MULTICAST_LOOP IP_DEFAULT_MULTICAST_LOOP
+# endif
+#endif
+#if !defined(IP_MAX_MEMBERSHIPS)
+# if defined(HAVE_CONST_IP_MAX_MEMBERSHIPS)
+# define IP_MAX_MEMBERSHIPS IP_MAX_MEMBERSHIPS
+# endif
+#endif
+#if !defined(IP_ROUTER_ALERT)
+# if defined(HAVE_CONST_IP_ROUTER_ALERT)
+# define IP_ROUTER_ALERT IP_ROUTER_ALERT
+# endif
+#endif
+#if !defined(IP_PKTINFO)
+# if defined(HAVE_CONST_IP_PKTINFO)
+# define IP_PKTINFO IP_PKTINFO
+# endif
+#endif
+#if !defined(IP_PKTOPTIONS)
+# if defined(HAVE_CONST_IP_PKTOPTIONS)
+# define IP_PKTOPTIONS IP_PKTOPTIONS
+# endif
+#endif
+#if !defined(IP_MTU_DISCOVER)
+# if defined(HAVE_CONST_IP_MTU_DISCOVER)
+# define IP_MTU_DISCOVER IP_MTU_DISCOVER
+# endif
+#endif
+#if !defined(IP_RECVERR)
+# if defined(HAVE_CONST_IP_RECVERR)
+# define IP_RECVERR IP_RECVERR
+# endif
+#endif
+#if !defined(IP_RECVTOS)
+# if defined(HAVE_CONST_IP_RECVTOS)
+# define IP_RECVTOS IP_RECVTOS
+# endif
+#endif
+#if !defined(IP_MTU)
+# if defined(HAVE_CONST_IP_MTU)
+# define IP_MTU IP_MTU
+# endif
+#endif
+#if !defined(IP_FREEBIND)
+# if defined(HAVE_CONST_IP_FREEBIND)
+# define IP_FREEBIND IP_FREEBIND
+# endif
+#endif
+#if !defined(IP_IPSEC_POLICY)
+# if defined(HAVE_CONST_IP_IPSEC_POLICY)
+# define IP_IPSEC_POLICY IP_IPSEC_POLICY
+# endif
+#endif
+#if !defined(IP_XFRM_POLICY)
+# if defined(HAVE_CONST_IP_XFRM_POLICY)
+# define IP_XFRM_POLICY IP_XFRM_POLICY
+# endif
+#endif
+#if !defined(IP_PASSSEC)
+# if defined(HAVE_CONST_IP_PASSSEC)
+# define IP_PASSSEC IP_PASSSEC
+# endif
+#endif
+#if !defined(IP_TRANSPARENT)
+# if defined(HAVE_CONST_IP_TRANSPARENT)
+# define IP_TRANSPARENT IP_TRANSPARENT
+# endif
+#endif
+#if !defined(IP_PMTUDISC_DONT)
+# if defined(HAVE_CONST_IP_PMTUDISC_DONT)
+# define IP_PMTUDISC_DONT IP_PMTUDISC_DONT
+# endif
+#endif
+#if !defined(IP_PMTUDISC_WANT)
+# if defined(HAVE_CONST_IP_PMTUDISC_WANT)
+# define IP_PMTUDISC_WANT IP_PMTUDISC_WANT
+# endif
+#endif
+#if !defined(IP_PMTUDISC_DO)
+# if defined(HAVE_CONST_IP_PMTUDISC_DO)
+# define IP_PMTUDISC_DO IP_PMTUDISC_DO
+# endif
+#endif
+#if !defined(IP_UNBLOCK_SOURCE)
+# if defined(HAVE_CONST_IP_UNBLOCK_SOURCE)
+# define IP_UNBLOCK_SOURCE IP_UNBLOCK_SOURCE
+# endif
+#endif
+#if !defined(IP_BLOCK_SOURCE)
+# if defined(HAVE_CONST_IP_BLOCK_SOURCE)
+# define IP_BLOCK_SOURCE IP_BLOCK_SOURCE
+# endif
+#endif
+#if !defined(IP_ADD_SOURCE_MEMBERSHIP)
+# if defined(HAVE_CONST_IP_ADD_SOURCE_MEMBERSHIP)
+# define IP_ADD_SOURCE_MEMBERSHIP IP_ADD_SOURCE_MEMBERSHIP
+# endif
+#endif
+#if !defined(IP_DROP_SOURCE_MEMBERSHIP)
+# if defined(HAVE_CONST_IP_DROP_SOURCE_MEMBERSHIP)
+# define IP_DROP_SOURCE_MEMBERSHIP IP_DROP_SOURCE_MEMBERSHIP
+# endif
+#endif
+#if !defined(IP_MSFILTER)
+# if defined(HAVE_CONST_IP_MSFILTER)
+# define IP_MSFILTER IP_MSFILTER
+# endif
+#endif
+#if !defined(MCAST_JOIN_GROUP)
+# if defined(HAVE_CONST_MCAST_JOIN_GROUP)
+# define MCAST_JOIN_GROUP MCAST_JOIN_GROUP
+# endif
+#endif
+#if !defined(MCAST_BLOCK_SOURCE)
+# if defined(HAVE_CONST_MCAST_BLOCK_SOURCE)
+# define MCAST_BLOCK_SOURCE MCAST_BLOCK_SOURCE
+# endif
+#endif
+#if !defined(MCAST_UNBLOCK_SOURCE)
+# if defined(HAVE_CONST_MCAST_UNBLOCK_SOURCE)
+# define MCAST_UNBLOCK_SOURCE MCAST_UNBLOCK_SOURCE
+# endif
+#endif
+#if !defined(MCAST_LEAVE_GROUP)
+# if defined(HAVE_CONST_MCAST_LEAVE_GROUP)
+# define MCAST_LEAVE_GROUP MCAST_LEAVE_GROUP
+# endif
+#endif
+#if !defined(MCAST_JOIN_SOURCE_GROUP)
+# if defined(HAVE_CONST_MCAST_JOIN_SOURCE_GROUP)
+# define MCAST_JOIN_SOURCE_GROUP MCAST_JOIN_SOURCE_GROUP
+# endif
+#endif
+#if !defined(MCAST_LEAVE_SOURCE_GROUP)
+# if defined(HAVE_CONST_MCAST_LEAVE_SOURCE_GROUP)
+# define MCAST_LEAVE_SOURCE_GROUP MCAST_LEAVE_SOURCE_GROUP
+# endif
+#endif
+#if !defined(MCAST_MSFILTER)
+# if defined(HAVE_CONST_MCAST_MSFILTER)
+# define MCAST_MSFILTER MCAST_MSFILTER
+# endif
+#endif
+#if !defined(MCAST_EXCLUDE)
+# if defined(HAVE_CONST_MCAST_EXCLUDE)
+# define MCAST_EXCLUDE MCAST_EXCLUDE
+# endif
+#endif
+#if !defined(MCAST_INCLUDE)
+# if defined(HAVE_CONST_MCAST_INCLUDE)
+# define MCAST_INCLUDE MCAST_INCLUDE
+# endif
+#endif
+#if !defined(SO_DEBUG)
+# if defined(HAVE_CONST_SO_DEBUG)
+# define SO_DEBUG SO_DEBUG
+# endif
+#endif
+#if !defined(SO_REUSEADDR)
+# if defined(HAVE_CONST_SO_REUSEADDR)
+# define SO_REUSEADDR SO_REUSEADDR
+# endif
+#endif
+#if !defined(SO_REUSEPORT)
+# if defined(HAVE_CONST_SO_REUSEPORT)
+# define SO_REUSEPORT SO_REUSEPORT
+# endif
+#endif
+#if !defined(SO_TYPE)
+# if defined(HAVE_CONST_SO_TYPE)
+# define SO_TYPE SO_TYPE
+# endif
+#endif
+#if !defined(SO_ERROR)
+# if defined(HAVE_CONST_SO_ERROR)
+# define SO_ERROR SO_ERROR
+# endif
+#endif
+#if !defined(SO_DONTROUTE)
+# if defined(HAVE_CONST_SO_DONTROUTE)
+# define SO_DONTROUTE SO_DONTROUTE
+# endif
+#endif
+#if !defined(SO_BROADCAST)
+# if defined(HAVE_CONST_SO_BROADCAST)
+# define SO_BROADCAST SO_BROADCAST
+# endif
+#endif
+#if !defined(SO_SNDBUF)
+# if defined(HAVE_CONST_SO_SNDBUF)
+# define SO_SNDBUF SO_SNDBUF
+# endif
+#endif
+#if !defined(SO_RCVBUF)
+# if defined(HAVE_CONST_SO_RCVBUF)
+# define SO_RCVBUF SO_RCVBUF
+# endif
+#endif
+#if !defined(SO_SNDBUFFORCE)
+# if defined(HAVE_CONST_SO_SNDBUFFORCE)
+# define SO_SNDBUFFORCE SO_SNDBUFFORCE
+# endif
+#endif
+#if !defined(SO_RCVBUFFORCE)
+# if defined(HAVE_CONST_SO_RCVBUFFORCE)
+# define SO_RCVBUFFORCE SO_RCVBUFFORCE
+# endif
+#endif
+#if !defined(SO_KEEPALIVE)
+# if defined(HAVE_CONST_SO_KEEPALIVE)
+# define SO_KEEPALIVE SO_KEEPALIVE
+# endif
+#endif
+#if !defined(SO_OOBINLINE)
+# if defined(HAVE_CONST_SO_OOBINLINE)
+# define SO_OOBINLINE SO_OOBINLINE
+# endif
+#endif
+#if !defined(SO_NO_CHECK)
+# if defined(HAVE_CONST_SO_NO_CHECK)
+# define SO_NO_CHECK SO_NO_CHECK
+# endif
+#endif
+#if !defined(SO_PRIORITY)
+# if defined(HAVE_CONST_SO_PRIORITY)
+# define SO_PRIORITY SO_PRIORITY
+# endif
+#endif
+#if !defined(SO_LINGER)
+# if defined(HAVE_CONST_SO_LINGER)
+# define SO_LINGER SO_LINGER
+# endif
+#endif
+#if !defined(SO_PASSCRED)
+# if defined(HAVE_CONST_SO_PASSCRED)
+# define SO_PASSCRED SO_PASSCRED
+# endif
+#endif
+#if !defined(SO_PEERCRED)
+# if defined(HAVE_CONST_SO_PEERCRED)
+# define SO_PEERCRED SO_PEERCRED
+# endif
+#endif
+#if !defined(SO_RCVLOWAT)
+# if defined(HAVE_CONST_SO_RCVLOWAT)
+# define SO_RCVLOWAT SO_RCVLOWAT
+# endif
+#endif
+#if !defined(SO_SNDLOWAT)
+# if defined(HAVE_CONST_SO_SNDLOWAT)
+# define SO_SNDLOWAT SO_SNDLOWAT
+# endif
+#endif
+#if !defined(SO_RCVTIMEO)
+# if defined(HAVE_CONST_SO_RCVTIMEO)
+# define SO_RCVTIMEO SO_RCVTIMEO
+# endif
+#endif
+#if !defined(SO_SNDTIMEO)
+# if defined(HAVE_CONST_SO_SNDTIMEO)
+# define SO_SNDTIMEO SO_SNDTIMEO
+# endif
+#endif
+#if !defined(SO_ACCEPTCONN)
+# if defined(HAVE_CONST_SO_ACCEPTCONN)
+# define SO_ACCEPTCONN SO_ACCEPTCONN
+# endif
+#endif
+#if !defined(SO_USELOOPBACK)
+# if defined(HAVE_CONST_SO_USELOOPBACK)
+# define SO_USELOOPBACK SO_USELOOPBACK
+# endif
+#endif
+#if !defined(SO_ACCEPTFILTER)
+# if defined(HAVE_CONST_SO_ACCEPTFILTER)
+# define SO_ACCEPTFILTER SO_ACCEPTFILTER
+# endif
+#endif
+#if !defined(SO_DONTTRUNC)
+# if defined(HAVE_CONST_SO_DONTTRUNC)
+# define SO_DONTTRUNC SO_DONTTRUNC
+# endif
+#endif
+#if !defined(SO_WANTMORE)
+# if defined(HAVE_CONST_SO_WANTMORE)
+# define SO_WANTMORE SO_WANTMORE
+# endif
+#endif
+#if !defined(SO_WANTOOBFLAG)
+# if defined(HAVE_CONST_SO_WANTOOBFLAG)
+# define SO_WANTOOBFLAG SO_WANTOOBFLAG
+# endif
+#endif
+#if !defined(SO_NREAD)
+# if defined(HAVE_CONST_SO_NREAD)
+# define SO_NREAD SO_NREAD
+# endif
+#endif
+#if !defined(SO_NKE)
+# if defined(HAVE_CONST_SO_NKE)
+# define SO_NKE SO_NKE
+# endif
+#endif
+#if !defined(SO_NOSIGPIPE)
+# if defined(HAVE_CONST_SO_NOSIGPIPE)
+# define SO_NOSIGPIPE SO_NOSIGPIPE
+# endif
+#endif
+#if !defined(SO_SECURITY_AUTHENTICATION)
+# if defined(HAVE_CONST_SO_SECURITY_AUTHENTICATION)
+# define SO_SECURITY_AUTHENTICATION SO_SECURITY_AUTHENTICATION
+# endif
+#endif
+#if !defined(SO_SECURITY_ENCRYPTION_TRANSPORT)
+# if defined(HAVE_CONST_SO_SECURITY_ENCRYPTION_TRANSPORT)
+# define SO_SECURITY_ENCRYPTION_TRANSPORT SO_SECURITY_ENCRYPTION_TRANSPORT
+# endif
+#endif
+#if !defined(SO_SECURITY_ENCRYPTION_NETWORK)
+# if defined(HAVE_CONST_SO_SECURITY_ENCRYPTION_NETWORK)
+# define SO_SECURITY_ENCRYPTION_NETWORK SO_SECURITY_ENCRYPTION_NETWORK
+# endif
+#endif
+#if !defined(SO_BINDTODEVICE)
+# if defined(HAVE_CONST_SO_BINDTODEVICE)
+# define SO_BINDTODEVICE SO_BINDTODEVICE
+# endif
+#endif
+#if !defined(SO_ATTACH_FILTER)
+# if defined(HAVE_CONST_SO_ATTACH_FILTER)
+# define SO_ATTACH_FILTER SO_ATTACH_FILTER
+# endif
+#endif
+#if !defined(SO_DETACH_FILTER)
+# if defined(HAVE_CONST_SO_DETACH_FILTER)
+# define SO_DETACH_FILTER SO_DETACH_FILTER
+# endif
+#endif
+#if !defined(SO_GET_FILTER)
+# if defined(HAVE_CONST_SO_GET_FILTER)
+# define SO_GET_FILTER SO_GET_FILTER
+# endif
+#endif
+#if !defined(SO_PEERNAME)
+# if defined(HAVE_CONST_SO_PEERNAME)
+# define SO_PEERNAME SO_PEERNAME
+# endif
+#endif
+#if !defined(SO_TIMESTAMP)
+# if defined(HAVE_CONST_SO_TIMESTAMP)
+# define SO_TIMESTAMP SO_TIMESTAMP
+# endif
+#endif
+#if !defined(SO_TIMESTAMPNS)
+# if defined(HAVE_CONST_SO_TIMESTAMPNS)
+# define SO_TIMESTAMPNS SO_TIMESTAMPNS
+# endif
+#endif
+#if !defined(SO_BINTIME)
+# if defined(HAVE_CONST_SO_BINTIME)
+# define SO_BINTIME SO_BINTIME
+# endif
+#endif
+#if !defined(SO_RECVUCRED)
+# if defined(HAVE_CONST_SO_RECVUCRED)
+# define SO_RECVUCRED SO_RECVUCRED
+# endif
+#endif
+#if !defined(SO_MAC_EXEMPT)
+# if defined(HAVE_CONST_SO_MAC_EXEMPT)
+# define SO_MAC_EXEMPT SO_MAC_EXEMPT
+# endif
+#endif
+#if !defined(SO_ALLZONES)
+# if defined(HAVE_CONST_SO_ALLZONES)
+# define SO_ALLZONES SO_ALLZONES
+# endif
+#endif
+#if !defined(SO_PEERSEC)
+# if defined(HAVE_CONST_SO_PEERSEC)
+# define SO_PEERSEC SO_PEERSEC
+# endif
+#endif
+#if !defined(SO_PASSSEC)
+# if defined(HAVE_CONST_SO_PASSSEC)
+# define SO_PASSSEC SO_PASSSEC
+# endif
+#endif
+#if !defined(SO_MARK)
+# if defined(HAVE_CONST_SO_MARK)
+# define SO_MARK SO_MARK
+# endif
+#endif
+#if !defined(SO_TIMESTAMPING)
+# if defined(HAVE_CONST_SO_TIMESTAMPING)
+# define SO_TIMESTAMPING SO_TIMESTAMPING
+# endif
+#endif
+#if !defined(SO_PROTOCOL)
+# if defined(HAVE_CONST_SO_PROTOCOL)
+# define SO_PROTOCOL SO_PROTOCOL
+# endif
+#endif
+#if !defined(SO_DOMAIN)
+# if defined(HAVE_CONST_SO_DOMAIN)
+# define SO_DOMAIN SO_DOMAIN
+# endif
+#endif
+#if !defined(SO_RXQ_OVFL)
+# if defined(HAVE_CONST_SO_RXQ_OVFL)
+# define SO_RXQ_OVFL SO_RXQ_OVFL
+# endif
+#endif
+#if !defined(SO_WIFI_STATUS)
+# if defined(HAVE_CONST_SO_WIFI_STATUS)
+# define SO_WIFI_STATUS SO_WIFI_STATUS
+# endif
+#endif
+#if !defined(SO_PEEK_OFF)
+# if defined(HAVE_CONST_SO_PEEK_OFF)
+# define SO_PEEK_OFF SO_PEEK_OFF
+# endif
+#endif
+#if !defined(SO_NOFCS)
+# if defined(HAVE_CONST_SO_NOFCS)
+# define SO_NOFCS SO_NOFCS
+# endif
+#endif
+#if !defined(SO_LOCK_FILTER)
+# if defined(HAVE_CONST_SO_LOCK_FILTER)
+# define SO_LOCK_FILTER SO_LOCK_FILTER
+# endif
+#endif
+#if !defined(SO_SELECT_ERR_QUEUE)
+# if defined(HAVE_CONST_SO_SELECT_ERR_QUEUE)
+# define SO_SELECT_ERR_QUEUE SO_SELECT_ERR_QUEUE
+# endif
+#endif
+#if !defined(SO_BUSY_POLL)
+# if defined(HAVE_CONST_SO_BUSY_POLL)
+# define SO_BUSY_POLL SO_BUSY_POLL
+# endif
+#endif
+#if !defined(SO_MAX_PACING_RATE)
+# if defined(HAVE_CONST_SO_MAX_PACING_RATE)
+# define SO_MAX_PACING_RATE SO_MAX_PACING_RATE
+# endif
+#endif
+#if !defined(SO_BPF_EXTENSIONS)
+# if defined(HAVE_CONST_SO_BPF_EXTENSIONS)
+# define SO_BPF_EXTENSIONS SO_BPF_EXTENSIONS
+# endif
+#endif
+#if !defined(SOPRI_INTERACTIVE)
+# if defined(HAVE_CONST_SOPRI_INTERACTIVE)
+# define SOPRI_INTERACTIVE SOPRI_INTERACTIVE
+# endif
+#endif
+#if !defined(SOPRI_NORMAL)
+# if defined(HAVE_CONST_SOPRI_NORMAL)
+# define SOPRI_NORMAL SOPRI_NORMAL
+# endif
+#endif
+#if !defined(SOPRI_BACKGROUND)
+# if defined(HAVE_CONST_SOPRI_BACKGROUND)
+# define SOPRI_BACKGROUND SOPRI_BACKGROUND
+# endif
+#endif
+#if !defined(IPX_TYPE)
+# if defined(HAVE_CONST_IPX_TYPE)
+# define IPX_TYPE IPX_TYPE
+# endif
+#endif
+#if !defined(TCP_NODELAY)
+# if defined(HAVE_CONST_TCP_NODELAY)
+# define TCP_NODELAY TCP_NODELAY
+# endif
+#endif
+#if !defined(TCP_MAXSEG)
+# if defined(HAVE_CONST_TCP_MAXSEG)
+# define TCP_MAXSEG TCP_MAXSEG
+# endif
+#endif
+#if !defined(TCP_CORK)
+# if defined(HAVE_CONST_TCP_CORK)
+# define TCP_CORK TCP_CORK
+# endif
+#endif
+#if !defined(TCP_DEFER_ACCEPT)
+# if defined(HAVE_CONST_TCP_DEFER_ACCEPT)
+# define TCP_DEFER_ACCEPT TCP_DEFER_ACCEPT
+# endif
+#endif
+#if !defined(TCP_INFO)
+# if defined(HAVE_CONST_TCP_INFO)
+# define TCP_INFO TCP_INFO
+# endif
+#endif
+#if !defined(TCP_KEEPCNT)
+# if defined(HAVE_CONST_TCP_KEEPCNT)
+# define TCP_KEEPCNT TCP_KEEPCNT
+# endif
+#endif
+#if !defined(TCP_KEEPIDLE)
+# if defined(HAVE_CONST_TCP_KEEPIDLE)
+# define TCP_KEEPIDLE TCP_KEEPIDLE
+# endif
+#endif
+#if !defined(TCP_KEEPINTVL)
+# if defined(HAVE_CONST_TCP_KEEPINTVL)
+# define TCP_KEEPINTVL TCP_KEEPINTVL
+# endif
+#endif
+#if !defined(TCP_LINGER2)
+# if defined(HAVE_CONST_TCP_LINGER2)
+# define TCP_LINGER2 TCP_LINGER2
+# endif
+#endif
+#if !defined(TCP_MD5SIG)
+# if defined(HAVE_CONST_TCP_MD5SIG)
+# define TCP_MD5SIG TCP_MD5SIG
+# endif
+#endif
+#if !defined(TCP_NOOPT)
+# if defined(HAVE_CONST_TCP_NOOPT)
+# define TCP_NOOPT TCP_NOOPT
+# endif
+#endif
+#if !defined(TCP_NOPUSH)
+# if defined(HAVE_CONST_TCP_NOPUSH)
+# define TCP_NOPUSH TCP_NOPUSH
+# endif
+#endif
+#if !defined(TCP_QUICKACK)
+# if defined(HAVE_CONST_TCP_QUICKACK)
+# define TCP_QUICKACK TCP_QUICKACK
+# endif
+#endif
+#if !defined(TCP_SYNCNT)
+# if defined(HAVE_CONST_TCP_SYNCNT)
+# define TCP_SYNCNT TCP_SYNCNT
+# endif
+#endif
+#if !defined(TCP_WINDOW_CLAMP)
+# if defined(HAVE_CONST_TCP_WINDOW_CLAMP)
+# define TCP_WINDOW_CLAMP TCP_WINDOW_CLAMP
+# endif
+#endif
+#if !defined(TCP_FASTOPEN)
+# if defined(HAVE_CONST_TCP_FASTOPEN)
+# define TCP_FASTOPEN TCP_FASTOPEN
+# endif
+#endif
+#if !defined(TCP_CONGESTION)
+# if defined(HAVE_CONST_TCP_CONGESTION)
+# define TCP_CONGESTION TCP_CONGESTION
+# endif
+#endif
+#if !defined(TCP_COOKIE_TRANSACTIONS)
+# if defined(HAVE_CONST_TCP_COOKIE_TRANSACTIONS)
+# define TCP_COOKIE_TRANSACTIONS TCP_COOKIE_TRANSACTIONS
+# endif
+#endif
+#if !defined(TCP_QUEUE_SEQ)
+# if defined(HAVE_CONST_TCP_QUEUE_SEQ)
+# define TCP_QUEUE_SEQ TCP_QUEUE_SEQ
+# endif
+#endif
+#if !defined(TCP_REPAIR)
+# if defined(HAVE_CONST_TCP_REPAIR)
+# define TCP_REPAIR TCP_REPAIR
+# endif
+#endif
+#if !defined(TCP_REPAIR_OPTIONS)
+# if defined(HAVE_CONST_TCP_REPAIR_OPTIONS)
+# define TCP_REPAIR_OPTIONS TCP_REPAIR_OPTIONS
+# endif
+#endif
+#if !defined(TCP_REPAIR_QUEUE)
+# if defined(HAVE_CONST_TCP_REPAIR_QUEUE)
+# define TCP_REPAIR_QUEUE TCP_REPAIR_QUEUE
+# endif
+#endif
+#if !defined(TCP_THIN_DUPACK)
+# if defined(HAVE_CONST_TCP_THIN_DUPACK)
+# define TCP_THIN_DUPACK TCP_THIN_DUPACK
+# endif
+#endif
+#if !defined(TCP_THIN_LINEAR_TIMEOUTS)
+# if defined(HAVE_CONST_TCP_THIN_LINEAR_TIMEOUTS)
+# define TCP_THIN_LINEAR_TIMEOUTS TCP_THIN_LINEAR_TIMEOUTS
+# endif
+#endif
+#if !defined(TCP_TIMESTAMP)
+# if defined(HAVE_CONST_TCP_TIMESTAMP)
+# define TCP_TIMESTAMP TCP_TIMESTAMP
+# endif
+#endif
+#if !defined(TCP_USER_TIMEOUT)
+# if defined(HAVE_CONST_TCP_USER_TIMEOUT)
+# define TCP_USER_TIMEOUT TCP_USER_TIMEOUT
+# endif
+#endif
+#if !defined(UDP_CORK)
+# if defined(HAVE_CONST_UDP_CORK)
+# define UDP_CORK UDP_CORK
+# endif
+#endif
+#if !defined(EAI_ADDRFAMILY)
+# if defined(HAVE_CONST_EAI_ADDRFAMILY)
+# define EAI_ADDRFAMILY EAI_ADDRFAMILY
+# endif
+#endif
+#if !defined(EAI_AGAIN)
+# if defined(HAVE_CONST_EAI_AGAIN)
+# define EAI_AGAIN EAI_AGAIN
+# endif
+#endif
+#if !defined(EAI_BADFLAGS)
+# if defined(HAVE_CONST_EAI_BADFLAGS)
+# define EAI_BADFLAGS EAI_BADFLAGS
+# endif
+#endif
+#if !defined(EAI_FAIL)
+# if defined(HAVE_CONST_EAI_FAIL)
+# define EAI_FAIL EAI_FAIL
+# endif
+#endif
+#if !defined(EAI_FAMILY)
+# if defined(HAVE_CONST_EAI_FAMILY)
+# define EAI_FAMILY EAI_FAMILY
+# endif
+#endif
+#if !defined(EAI_MEMORY)
+# if defined(HAVE_CONST_EAI_MEMORY)
+# define EAI_MEMORY EAI_MEMORY
+# endif
+#endif
+#if !defined(EAI_NODATA)
+# if defined(HAVE_CONST_EAI_NODATA)
+# define EAI_NODATA EAI_NODATA
+# endif
+#endif
+#if !defined(EAI_NONAME)
+# if defined(HAVE_CONST_EAI_NONAME)
+# define EAI_NONAME EAI_NONAME
+# endif
+#endif
+#if !defined(EAI_OVERFLOW)
+# if defined(HAVE_CONST_EAI_OVERFLOW)
+# define EAI_OVERFLOW EAI_OVERFLOW
+# endif
+#endif
+#if !defined(EAI_SERVICE)
+# if defined(HAVE_CONST_EAI_SERVICE)
+# define EAI_SERVICE EAI_SERVICE
+# endif
+#endif
+#if !defined(EAI_SOCKTYPE)
+# if defined(HAVE_CONST_EAI_SOCKTYPE)
+# define EAI_SOCKTYPE EAI_SOCKTYPE
+# endif
+#endif
+#if !defined(EAI_SYSTEM)
+# if defined(HAVE_CONST_EAI_SYSTEM)
+# define EAI_SYSTEM EAI_SYSTEM
+# endif
+#endif
+#if !defined(EAI_BADHINTS)
+# if defined(HAVE_CONST_EAI_BADHINTS)
+# define EAI_BADHINTS EAI_BADHINTS
+# endif
+#endif
+#if !defined(EAI_PROTOCOL)
+# if defined(HAVE_CONST_EAI_PROTOCOL)
+# define EAI_PROTOCOL EAI_PROTOCOL
+# endif
+#endif
+#if !defined(EAI_MAX)
+# if defined(HAVE_CONST_EAI_MAX)
+# define EAI_MAX EAI_MAX
+# endif
+#endif
+#if !defined(AI_PASSIVE)
+# if defined(HAVE_CONST_AI_PASSIVE)
+# define AI_PASSIVE AI_PASSIVE
+# endif
+#endif
+#if !defined(AI_CANONNAME)
+# if defined(HAVE_CONST_AI_CANONNAME)
+# define AI_CANONNAME AI_CANONNAME
+# endif
+#endif
+#if !defined(AI_NUMERICHOST)
+# if defined(HAVE_CONST_AI_NUMERICHOST)
+# define AI_NUMERICHOST AI_NUMERICHOST
+# endif
+#endif
+#if !defined(AI_NUMERICSERV)
+# if defined(HAVE_CONST_AI_NUMERICSERV)
+# define AI_NUMERICSERV AI_NUMERICSERV
+# endif
+#endif
+#if !defined(AI_MASK)
+# if defined(HAVE_CONST_AI_MASK)
+# define AI_MASK AI_MASK
+# endif
+#endif
+#if !defined(AI_ALL)
+# if defined(HAVE_CONST_AI_ALL)
+# define AI_ALL AI_ALL
+# endif
+#endif
+#if !defined(AI_V4MAPPED_CFG)
+# if defined(HAVE_CONST_AI_V4MAPPED_CFG)
+# define AI_V4MAPPED_CFG AI_V4MAPPED_CFG
+# endif
+#endif
+#if !defined(AI_ADDRCONFIG)
+# if defined(HAVE_CONST_AI_ADDRCONFIG)
+# define AI_ADDRCONFIG AI_ADDRCONFIG
+# endif
+#endif
+#if !defined(AI_V4MAPPED)
+# if defined(HAVE_CONST_AI_V4MAPPED)
+# define AI_V4MAPPED AI_V4MAPPED
+# endif
+#endif
+#if !defined(AI_DEFAULT)
+# if defined(HAVE_CONST_AI_DEFAULT)
+# define AI_DEFAULT AI_DEFAULT
+# endif
+#endif
+#if !defined(NI_MAXHOST)
+# if defined(HAVE_CONST_NI_MAXHOST)
+# define NI_MAXHOST NI_MAXHOST
+# endif
+#endif
+#if !defined(NI_MAXSERV)
+# if defined(HAVE_CONST_NI_MAXSERV)
+# define NI_MAXSERV NI_MAXSERV
+# endif
+#endif
+#if !defined(NI_NOFQDN)
+# if defined(HAVE_CONST_NI_NOFQDN)
+# define NI_NOFQDN NI_NOFQDN
+# endif
+#endif
+#if !defined(NI_NUMERICHOST)
+# if defined(HAVE_CONST_NI_NUMERICHOST)
+# define NI_NUMERICHOST NI_NUMERICHOST
+# endif
+#endif
+#if !defined(NI_NAMEREQD)
+# if defined(HAVE_CONST_NI_NAMEREQD)
+# define NI_NAMEREQD NI_NAMEREQD
+# endif
+#endif
+#if !defined(NI_NUMERICSERV)
+# if defined(HAVE_CONST_NI_NUMERICSERV)
+# define NI_NUMERICSERV NI_NUMERICSERV
+# endif
+#endif
+#if !defined(NI_DGRAM)
+# if defined(HAVE_CONST_NI_DGRAM)
+# define NI_DGRAM NI_DGRAM
+# endif
+#endif
+#if !defined(SHUT_RD)
+# if defined(HAVE_CONST_SHUT_RD)
+# define SHUT_RD SHUT_RD
+# else
+# define SHUT_RD 0
+# endif
+#endif
+#if !defined(SHUT_WR)
+# if defined(HAVE_CONST_SHUT_WR)
+# define SHUT_WR SHUT_WR
+# else
+# define SHUT_WR 1
+# endif
+#endif
+#if !defined(SHUT_RDWR)
+# if defined(HAVE_CONST_SHUT_RDWR)
+# define SHUT_RDWR SHUT_RDWR
+# else
+# define SHUT_RDWR 2
+# endif
+#endif
+#if !defined(IPV6_JOIN_GROUP)
+# if defined(HAVE_CONST_IPV6_JOIN_GROUP)
+# define IPV6_JOIN_GROUP IPV6_JOIN_GROUP
+# endif
+#endif
+#if !defined(IPV6_LEAVE_GROUP)
+# if defined(HAVE_CONST_IPV6_LEAVE_GROUP)
+# define IPV6_LEAVE_GROUP IPV6_LEAVE_GROUP
+# endif
+#endif
+#if !defined(IPV6_MULTICAST_HOPS)
+# if defined(HAVE_CONST_IPV6_MULTICAST_HOPS)
+# define IPV6_MULTICAST_HOPS IPV6_MULTICAST_HOPS
+# endif
+#endif
+#if !defined(IPV6_MULTICAST_IF)
+# if defined(HAVE_CONST_IPV6_MULTICAST_IF)
+# define IPV6_MULTICAST_IF IPV6_MULTICAST_IF
+# endif
+#endif
+#if !defined(IPV6_MULTICAST_LOOP)
+# if defined(HAVE_CONST_IPV6_MULTICAST_LOOP)
+# define IPV6_MULTICAST_LOOP IPV6_MULTICAST_LOOP
+# endif
+#endif
+#if !defined(IPV6_UNICAST_HOPS)
+# if defined(HAVE_CONST_IPV6_UNICAST_HOPS)
+# define IPV6_UNICAST_HOPS IPV6_UNICAST_HOPS
+# endif
+#endif
+#if !defined(IPV6_V6ONLY)
+# if defined(HAVE_CONST_IPV6_V6ONLY)
+# define IPV6_V6ONLY IPV6_V6ONLY
+# endif
+#endif
+#if !defined(IPV6_CHECKSUM)
+# if defined(HAVE_CONST_IPV6_CHECKSUM)
+# define IPV6_CHECKSUM IPV6_CHECKSUM
+# endif
+#endif
+#if !defined(IPV6_DONTFRAG)
+# if defined(HAVE_CONST_IPV6_DONTFRAG)
+# define IPV6_DONTFRAG IPV6_DONTFRAG
+# endif
+#endif
+#if !defined(IPV6_DSTOPTS)
+# if defined(HAVE_CONST_IPV6_DSTOPTS)
+# define IPV6_DSTOPTS IPV6_DSTOPTS
+# endif
+#endif
+#if !defined(IPV6_HOPLIMIT)
+# if defined(HAVE_CONST_IPV6_HOPLIMIT)
+# define IPV6_HOPLIMIT IPV6_HOPLIMIT
+# endif
+#endif
+#if !defined(IPV6_HOPOPTS)
+# if defined(HAVE_CONST_IPV6_HOPOPTS)
+# define IPV6_HOPOPTS IPV6_HOPOPTS
+# endif
+#endif
+#if !defined(IPV6_NEXTHOP)
+# if defined(HAVE_CONST_IPV6_NEXTHOP)
+# define IPV6_NEXTHOP IPV6_NEXTHOP
+# endif
+#endif
+#if !defined(IPV6_PATHMTU)
+# if defined(HAVE_CONST_IPV6_PATHMTU)
+# define IPV6_PATHMTU IPV6_PATHMTU
+# endif
+#endif
+#if !defined(IPV6_PKTINFO)
+# if defined(HAVE_CONST_IPV6_PKTINFO)
+# define IPV6_PKTINFO IPV6_PKTINFO
+# endif
+#endif
+#if !defined(IPV6_RECVDSTOPTS)
+# if defined(HAVE_CONST_IPV6_RECVDSTOPTS)
+# define IPV6_RECVDSTOPTS IPV6_RECVDSTOPTS
+# endif
+#endif
+#if !defined(IPV6_RECVHOPLIMIT)
+# if defined(HAVE_CONST_IPV6_RECVHOPLIMIT)
+# define IPV6_RECVHOPLIMIT IPV6_RECVHOPLIMIT
+# endif
+#endif
+#if !defined(IPV6_RECVHOPOPTS)
+# if defined(HAVE_CONST_IPV6_RECVHOPOPTS)
+# define IPV6_RECVHOPOPTS IPV6_RECVHOPOPTS
+# endif
+#endif
+#if !defined(IPV6_RECVPKTINFO)
+# if defined(HAVE_CONST_IPV6_RECVPKTINFO)
+# define IPV6_RECVPKTINFO IPV6_RECVPKTINFO
+# endif
+#endif
+#if !defined(IPV6_RECVRTHDR)
+# if defined(HAVE_CONST_IPV6_RECVRTHDR)
+# define IPV6_RECVRTHDR IPV6_RECVRTHDR
+# endif
+#endif
+#if !defined(IPV6_RECVTCLASS)
+# if defined(HAVE_CONST_IPV6_RECVTCLASS)
+# define IPV6_RECVTCLASS IPV6_RECVTCLASS
+# endif
+#endif
+#if !defined(IPV6_RTHDR)
+# if defined(HAVE_CONST_IPV6_RTHDR)
+# define IPV6_RTHDR IPV6_RTHDR
+# endif
+#endif
+#if !defined(IPV6_RTHDRDSTOPTS)
+# if defined(HAVE_CONST_IPV6_RTHDRDSTOPTS)
+# define IPV6_RTHDRDSTOPTS IPV6_RTHDRDSTOPTS
+# endif
+#endif
+#if !defined(IPV6_RTHDR_TYPE_0)
+# if defined(HAVE_CONST_IPV6_RTHDR_TYPE_0)
+# define IPV6_RTHDR_TYPE_0 IPV6_RTHDR_TYPE_0
+# endif
+#endif
+#if !defined(IPV6_RECVPATHMTU)
+# if defined(HAVE_CONST_IPV6_RECVPATHMTU)
+# define IPV6_RECVPATHMTU IPV6_RECVPATHMTU
+# endif
+#endif
+#if !defined(IPV6_TCLASS)
+# if defined(HAVE_CONST_IPV6_TCLASS)
+# define IPV6_TCLASS IPV6_TCLASS
+# endif
+#endif
+#if !defined(IPV6_USE_MIN_MTU)
+# if defined(HAVE_CONST_IPV6_USE_MIN_MTU)
+# define IPV6_USE_MIN_MTU IPV6_USE_MIN_MTU
+# endif
+#endif
+#if !defined(INET_ADDRSTRLEN)
+# if defined(HAVE_CONST_INET_ADDRSTRLEN)
+# define INET_ADDRSTRLEN INET_ADDRSTRLEN
+# else
+# define INET_ADDRSTRLEN 16
+# endif
+#endif
+#if !defined(INET6_ADDRSTRLEN)
+# if defined(HAVE_CONST_INET6_ADDRSTRLEN)
+# define INET6_ADDRSTRLEN INET6_ADDRSTRLEN
+# else
+# define INET6_ADDRSTRLEN 46
+# endif
+#endif
+#if !defined(IFNAMSIZ)
+# if defined(HAVE_CONST_IFNAMSIZ)
+# define IFNAMSIZ IFNAMSIZ
+# endif
+#endif
+#if !defined(IF_NAMESIZE)
+# if defined(HAVE_CONST_IF_NAMESIZE)
+# define IF_NAMESIZE IF_NAMESIZE
+# endif
+#endif
+#if !defined(SOMAXCONN)
+# if defined(HAVE_CONST_SOMAXCONN)
+# define SOMAXCONN SOMAXCONN
+# else
+# define SOMAXCONN 5
+# endif
+#endif
+#if !defined(SCM_RIGHTS)
+# if defined(HAVE_CONST_SCM_RIGHTS)
+# define SCM_RIGHTS SCM_RIGHTS
+# endif
+#endif
+#if !defined(SCM_TIMESTAMP)
+# if defined(HAVE_CONST_SCM_TIMESTAMP)
+# define SCM_TIMESTAMP SCM_TIMESTAMP
+# endif
+#endif
+#if !defined(SCM_TIMESTAMPNS)
+# if defined(HAVE_CONST_SCM_TIMESTAMPNS)
+# define SCM_TIMESTAMPNS SCM_TIMESTAMPNS
+# endif
+#endif
+#if !defined(SCM_TIMESTAMPING)
+# if defined(HAVE_CONST_SCM_TIMESTAMPING)
+# define SCM_TIMESTAMPING SCM_TIMESTAMPING
+# endif
+#endif
+#if !defined(SCM_BINTIME)
+# if defined(HAVE_CONST_SCM_BINTIME)
+# define SCM_BINTIME SCM_BINTIME
+# endif
+#endif
+#if !defined(SCM_CREDENTIALS)
+# if defined(HAVE_CONST_SCM_CREDENTIALS)
+# define SCM_CREDENTIALS SCM_CREDENTIALS
+# endif
+#endif
+#if !defined(SCM_CREDS)
+# if defined(HAVE_CONST_SCM_CREDS)
+# define SCM_CREDS SCM_CREDS
+# endif
+#endif
+#if !defined(SCM_UCRED)
+# if defined(HAVE_CONST_SCM_UCRED)
+# define SCM_UCRED SCM_UCRED
+# endif
+#endif
+#if !defined(SCM_WIFI_STATUS)
+# if defined(HAVE_CONST_SCM_WIFI_STATUS)
+# define SCM_WIFI_STATUS SCM_WIFI_STATUS
+# endif
+#endif
+#if !defined(LOCAL_PEERCRED)
+# if defined(HAVE_CONST_LOCAL_PEERCRED)
+# define LOCAL_PEERCRED LOCAL_PEERCRED
+# endif
+#endif
+#if !defined(LOCAL_CREDS)
+# if defined(HAVE_CONST_LOCAL_CREDS)
+# define LOCAL_CREDS LOCAL_CREDS
+# endif
+#endif
+#if !defined(LOCAL_CONNWAIT)
+# if defined(HAVE_CONST_LOCAL_CONNWAIT)
+# define LOCAL_CONNWAIT LOCAL_CONNWAIT
+# endif
+#endif
+#if !defined(IFF_802_1Q_VLAN)
+# if defined(HAVE_CONST_IFF_802_1Q_VLAN)
+# define IFF_802_1Q_VLAN IFF_802_1Q_VLAN
+# endif
+#endif
+#if !defined(IFF_ALLMULTI)
+# if defined(HAVE_CONST_IFF_ALLMULTI)
+# define IFF_ALLMULTI IFF_ALLMULTI
+# endif
+#endif
+#if !defined(IFF_ALTPHYS)
+# if defined(HAVE_CONST_IFF_ALTPHYS)
+# define IFF_ALTPHYS IFF_ALTPHYS
+# endif
+#endif
+#if !defined(IFF_AUTOMEDIA)
+# if defined(HAVE_CONST_IFF_AUTOMEDIA)
+# define IFF_AUTOMEDIA IFF_AUTOMEDIA
+# endif
+#endif
+#if !defined(IFF_BONDING)
+# if defined(HAVE_CONST_IFF_BONDING)
+# define IFF_BONDING IFF_BONDING
+# endif
+#endif
+#if !defined(IFF_BRIDGE_PORT)
+# if defined(HAVE_CONST_IFF_BRIDGE_PORT)
+# define IFF_BRIDGE_PORT IFF_BRIDGE_PORT
+# endif
+#endif
+#if !defined(IFF_BROADCAST)
+# if defined(HAVE_CONST_IFF_BROADCAST)
+# define IFF_BROADCAST IFF_BROADCAST
+# endif
+#endif
+#if !defined(IFF_CANTCONFIG)
+# if defined(HAVE_CONST_IFF_CANTCONFIG)
+# define IFF_CANTCONFIG IFF_CANTCONFIG
+# endif
+#endif
+#if !defined(IFF_DEBUG)
+# if defined(HAVE_CONST_IFF_DEBUG)
+# define IFF_DEBUG IFF_DEBUG
+# endif
+#endif
+#if !defined(IFF_DISABLE_NETPOLL)
+# if defined(HAVE_CONST_IFF_DISABLE_NETPOLL)
+# define IFF_DISABLE_NETPOLL IFF_DISABLE_NETPOLL
+# endif
+#endif
+#if !defined(IFF_DONT_BRIDGE)
+# if defined(HAVE_CONST_IFF_DONT_BRIDGE)
+# define IFF_DONT_BRIDGE IFF_DONT_BRIDGE
+# endif
+#endif
+#if !defined(IFF_DORMANT)
+# if defined(HAVE_CONST_IFF_DORMANT)
+# define IFF_DORMANT IFF_DORMANT
+# endif
+#endif
+#if !defined(IFF_DRV_OACTIVE)
+# if defined(HAVE_CONST_IFF_DRV_OACTIVE)
+# define IFF_DRV_OACTIVE IFF_DRV_OACTIVE
+# endif
+#endif
+#if !defined(IFF_DRV_RUNNING)
+# if defined(HAVE_CONST_IFF_DRV_RUNNING)
+# define IFF_DRV_RUNNING IFF_DRV_RUNNING
+# endif
+#endif
+#if !defined(IFF_DYING)
+# if defined(HAVE_CONST_IFF_DYING)
+# define IFF_DYING IFF_DYING
+# endif
+#endif
+#if !defined(IFF_DYNAMIC)
+# if defined(HAVE_CONST_IFF_DYNAMIC)
+# define IFF_DYNAMIC IFF_DYNAMIC
+# endif
+#endif
+#if !defined(IFF_EBRIDGE)
+# if defined(HAVE_CONST_IFF_EBRIDGE)
+# define IFF_EBRIDGE IFF_EBRIDGE
+# endif
+#endif
+#if !defined(IFF_ECHO)
+# if defined(HAVE_CONST_IFF_ECHO)
+# define IFF_ECHO IFF_ECHO
+# endif
+#endif
+#if !defined(IFF_ISATAP)
+# if defined(HAVE_CONST_IFF_ISATAP)
+# define IFF_ISATAP IFF_ISATAP
+# endif
+#endif
+#if !defined(IFF_LINK0)
+# if defined(HAVE_CONST_IFF_LINK0)
+# define IFF_LINK0 IFF_LINK0
+# endif
+#endif
+#if !defined(IFF_LINK1)
+# if defined(HAVE_CONST_IFF_LINK1)
+# define IFF_LINK1 IFF_LINK1
+# endif
+#endif
+#if !defined(IFF_LINK2)
+# if defined(HAVE_CONST_IFF_LINK2)
+# define IFF_LINK2 IFF_LINK2
+# endif
+#endif
+#if !defined(IFF_LIVE_ADDR_CHANGE)
+# if defined(HAVE_CONST_IFF_LIVE_ADDR_CHANGE)
+# define IFF_LIVE_ADDR_CHANGE IFF_LIVE_ADDR_CHANGE
+# endif
+#endif
+#if !defined(IFF_LOOPBACK)
+# if defined(HAVE_CONST_IFF_LOOPBACK)
+# define IFF_LOOPBACK IFF_LOOPBACK
+# endif
+#endif
+#if !defined(IFF_LOWER_UP)
+# if defined(HAVE_CONST_IFF_LOWER_UP)
+# define IFF_LOWER_UP IFF_LOWER_UP
+# endif
+#endif
+#if !defined(IFF_MACVLAN_PORT)
+# if defined(HAVE_CONST_IFF_MACVLAN_PORT)
+# define IFF_MACVLAN_PORT IFF_MACVLAN_PORT
+# endif
+#endif
+#if !defined(IFF_MASTER)
+# if defined(HAVE_CONST_IFF_MASTER)
+# define IFF_MASTER IFF_MASTER
+# endif
+#endif
+#if !defined(IFF_MASTER_8023AD)
+# if defined(HAVE_CONST_IFF_MASTER_8023AD)
+# define IFF_MASTER_8023AD IFF_MASTER_8023AD
+# endif
+#endif
+#if !defined(IFF_MASTER_ALB)
+# if defined(HAVE_CONST_IFF_MASTER_ALB)
+# define IFF_MASTER_ALB IFF_MASTER_ALB
+# endif
+#endif
+#if !defined(IFF_MASTER_ARPMON)
+# if defined(HAVE_CONST_IFF_MASTER_ARPMON)
+# define IFF_MASTER_ARPMON IFF_MASTER_ARPMON
+# endif
+#endif
+#if !defined(IFF_MONITOR)
+# if defined(HAVE_CONST_IFF_MONITOR)
+# define IFF_MONITOR IFF_MONITOR
+# endif
+#endif
+#if !defined(IFF_MULTICAST)
+# if defined(HAVE_CONST_IFF_MULTICAST)
+# define IFF_MULTICAST IFF_MULTICAST
+# endif
+#endif
+#if !defined(IFF_NOARP)
+# if defined(HAVE_CONST_IFF_NOARP)
+# define IFF_NOARP IFF_NOARP
+# endif
+#endif
+#if !defined(IFF_NOTRAILERS)
+# if defined(HAVE_CONST_IFF_NOTRAILERS)
+# define IFF_NOTRAILERS IFF_NOTRAILERS
+# endif
+#endif
+#if !defined(IFF_OACTIVE)
+# if defined(HAVE_CONST_IFF_OACTIVE)
+# define IFF_OACTIVE IFF_OACTIVE
+# endif
+#endif
+#if !defined(IFF_OVS_DATAPATH)
+# if defined(HAVE_CONST_IFF_OVS_DATAPATH)
+# define IFF_OVS_DATAPATH IFF_OVS_DATAPATH
+# endif
+#endif
+#if !defined(IFF_POINTOPOINT)
+# if defined(HAVE_CONST_IFF_POINTOPOINT)
+# define IFF_POINTOPOINT IFF_POINTOPOINT
+# endif
+#endif
+#if !defined(IFF_PORTSEL)
+# if defined(HAVE_CONST_IFF_PORTSEL)
+# define IFF_PORTSEL IFF_PORTSEL
+# endif
+#endif
+#if !defined(IFF_PPROMISC)
+# if defined(HAVE_CONST_IFF_PPROMISC)
+# define IFF_PPROMISC IFF_PPROMISC
+# endif
+#endif
+#if !defined(IFF_PROMISC)
+# if defined(HAVE_CONST_IFF_PROMISC)
+# define IFF_PROMISC IFF_PROMISC
+# endif
+#endif
+#if !defined(IFF_RENAMING)
+# if defined(HAVE_CONST_IFF_RENAMING)
+# define IFF_RENAMING IFF_RENAMING
+# endif
+#endif
+#if !defined(IFF_ROUTE)
+# if defined(HAVE_CONST_IFF_ROUTE)
+# define IFF_ROUTE IFF_ROUTE
+# endif
+#endif
+#if !defined(IFF_RUNNING)
+# if defined(HAVE_CONST_IFF_RUNNING)
+# define IFF_RUNNING IFF_RUNNING
+# endif
+#endif
+#if !defined(IFF_SIMPLEX)
+# if defined(HAVE_CONST_IFF_SIMPLEX)
+# define IFF_SIMPLEX IFF_SIMPLEX
+# endif
+#endif
+#if !defined(IFF_SLAVE)
+# if defined(HAVE_CONST_IFF_SLAVE)
+# define IFF_SLAVE IFF_SLAVE
+# endif
+#endif
+#if !defined(IFF_SLAVE_INACTIVE)
+# if defined(HAVE_CONST_IFF_SLAVE_INACTIVE)
+# define IFF_SLAVE_INACTIVE IFF_SLAVE_INACTIVE
+# endif
+#endif
+#if !defined(IFF_SLAVE_NEEDARP)
+# if defined(HAVE_CONST_IFF_SLAVE_NEEDARP)
+# define IFF_SLAVE_NEEDARP IFF_SLAVE_NEEDARP
+# endif
+#endif
+#if !defined(IFF_SMART)
+# if defined(HAVE_CONST_IFF_SMART)
+# define IFF_SMART IFF_SMART
+# endif
+#endif
+#if !defined(IFF_STATICARP)
+# if defined(HAVE_CONST_IFF_STATICARP)
+# define IFF_STATICARP IFF_STATICARP
+# endif
+#endif
+#if !defined(IFF_SUPP_NOFCS)
+# if defined(HAVE_CONST_IFF_SUPP_NOFCS)
+# define IFF_SUPP_NOFCS IFF_SUPP_NOFCS
+# endif
+#endif
+#if !defined(IFF_TEAM_PORT)
+# if defined(HAVE_CONST_IFF_TEAM_PORT)
+# define IFF_TEAM_PORT IFF_TEAM_PORT
+# endif
+#endif
+#if !defined(IFF_TX_SKB_SHARING)
+# if defined(HAVE_CONST_IFF_TX_SKB_SHARING)
+# define IFF_TX_SKB_SHARING IFF_TX_SKB_SHARING
+# endif
+#endif
+#if !defined(IFF_UNICAST_FLT)
+# if defined(HAVE_CONST_IFF_UNICAST_FLT)
+# define IFF_UNICAST_FLT IFF_UNICAST_FLT
+# endif
+#endif
+#if !defined(IFF_UP)
+# if defined(HAVE_CONST_IFF_UP)
+# define IFF_UP IFF_UP
+# endif
+#endif
+#if !defined(IFF_WAN_HDLC)
+# if defined(HAVE_CONST_IFF_WAN_HDLC)
+# define IFF_WAN_HDLC IFF_WAN_HDLC
+# endif
+#endif
+#if !defined(IFF_XMIT_DST_RELEASE)
+# if defined(HAVE_CONST_IFF_XMIT_DST_RELEASE)
+# define IFF_XMIT_DST_RELEASE IFF_XMIT_DST_RELEASE
+# endif
+#endif
+#if !defined(IFF_VOLATILE)
+# if defined(HAVE_CONST_IFF_VOLATILE)
+# define IFF_VOLATILE IFF_VOLATILE
+# endif
+#endif
+#if !defined(IFF_CANTCHANGE)
+# if defined(HAVE_CONST_IFF_CANTCHANGE)
+# define IFF_CANTCHANGE IFF_CANTCHANGE
+# endif
+#endif
+
+int rsock_family_to_int(const char *str, long len, int *valp);
+
+int rsock_socktype_to_int(const char *str, long len, int *valp);
+
+int rsock_ipproto_to_int(const char *str, long len, int *valp);
+
+int rsock_unknown_level_to_int(const char *str, long len, int *valp);
+
+int rsock_ip_level_to_int(const char *str, long len, int *valp);
+
+int rsock_so_optname_to_int(const char *str, long len, int *valp);
+
+int rsock_ip_optname_to_int(const char *str, long len, int *valp);
+
+#ifdef IPPROTO_IPV6
+int rsock_ipv6_optname_to_int(const char *str, long len, int *valp);
+#endif
+
+int rsock_tcp_optname_to_int(const char *str, long len, int *valp);
+
+int rsock_udp_optname_to_int(const char *str, long len, int *valp);
+
+int rsock_shutdown_how_to_int(const char *str, long len, int *valp);
+
+int rsock_scm_optname_to_int(const char *str, long len, int *valp);
+
+ID rsock_intern_family(int val);
+
+ID rsock_intern_family_noprefix(int val);
+
+ID rsock_intern_protocol_family(int val);
+
+ID rsock_intern_socktype(int val);
+
+ID rsock_intern_ipproto(int val);
+
+ID rsock_intern_iplevel(int val);
+
+ID rsock_intern_so_optname(int val);
+
+ID rsock_intern_ip_optname(int val);
+
+ID rsock_intern_ipv6_optname(int val);
+
+ID rsock_intern_tcp_optname(int val);
+
+ID rsock_intern_udp_optname(int val);
+
+ID rsock_intern_scm_optname(int val);
+
+ID rsock_intern_local_optname(int val);
+
diff --git a/jni/ruby/ext/socket/depend b/jni/ruby/ext/socket/depend
new file mode 100644
index 0000000..30ecfc5
--- /dev/null
+++ b/jni/ruby/ext/socket/depend
@@ -0,0 +1,295 @@
+getnameinfo.o: getnameinfo.c $(arch_hdrdir)/ruby/config.h addrinfo.h sockport.h
+getaddrinfo.o: getaddrinfo.c $(arch_hdrdir)/ruby/config.h addrinfo.h sockport.h
+
+constdefs.h constdefs.c : $(srcdir)/mkconstants.rb
+ @echo "generating constant definitions"
+ @$(RUBY) $(srcdir)/mkconstants.rb -H constdefs.h -o constdefs.c
+
+# AUTOGENERATED DEPENDENCIES START
+ancdata.o: $(RUBY_EXTCONF_H)
+ancdata.o: $(arch_hdrdir)/ruby/config.h
+ancdata.o: $(hdrdir)/ruby/defines.h
+ancdata.o: $(hdrdir)/ruby/encoding.h
+ancdata.o: $(hdrdir)/ruby/intern.h
+ancdata.o: $(hdrdir)/ruby/io.h
+ancdata.o: $(hdrdir)/ruby/missing.h
+ancdata.o: $(hdrdir)/ruby/oniguruma.h
+ancdata.o: $(hdrdir)/ruby/ruby.h
+ancdata.o: $(hdrdir)/ruby/st.h
+ancdata.o: $(hdrdir)/ruby/subst.h
+ancdata.o: $(hdrdir)/ruby/thread.h
+ancdata.o: $(hdrdir)/ruby/util.h
+ancdata.o: $(top_srcdir)/include/ruby.h
+ancdata.o: $(top_srcdir)/internal.h
+ancdata.o: ancdata.c
+ancdata.o: constdefs.h
+ancdata.o: rubysocket.h
+ancdata.o: sockport.h
+basicsocket.o: $(RUBY_EXTCONF_H)
+basicsocket.o: $(arch_hdrdir)/ruby/config.h
+basicsocket.o: $(hdrdir)/ruby/defines.h
+basicsocket.o: $(hdrdir)/ruby/encoding.h
+basicsocket.o: $(hdrdir)/ruby/intern.h
+basicsocket.o: $(hdrdir)/ruby/io.h
+basicsocket.o: $(hdrdir)/ruby/missing.h
+basicsocket.o: $(hdrdir)/ruby/oniguruma.h
+basicsocket.o: $(hdrdir)/ruby/ruby.h
+basicsocket.o: $(hdrdir)/ruby/st.h
+basicsocket.o: $(hdrdir)/ruby/subst.h
+basicsocket.o: $(hdrdir)/ruby/thread.h
+basicsocket.o: $(hdrdir)/ruby/util.h
+basicsocket.o: $(top_srcdir)/include/ruby.h
+basicsocket.o: $(top_srcdir)/internal.h
+basicsocket.o: basicsocket.c
+basicsocket.o: constdefs.h
+basicsocket.o: rubysocket.h
+basicsocket.o: sockport.h
+constants.o: $(RUBY_EXTCONF_H)
+constants.o: $(arch_hdrdir)/ruby/config.h
+constants.o: $(hdrdir)/ruby/defines.h
+constants.o: $(hdrdir)/ruby/encoding.h
+constants.o: $(hdrdir)/ruby/intern.h
+constants.o: $(hdrdir)/ruby/io.h
+constants.o: $(hdrdir)/ruby/missing.h
+constants.o: $(hdrdir)/ruby/oniguruma.h
+constants.o: $(hdrdir)/ruby/ruby.h
+constants.o: $(hdrdir)/ruby/st.h
+constants.o: $(hdrdir)/ruby/subst.h
+constants.o: $(hdrdir)/ruby/thread.h
+constants.o: $(hdrdir)/ruby/util.h
+constants.o: $(top_srcdir)/include/ruby.h
+constants.o: $(top_srcdir)/internal.h
+constants.o: constants.c
+constants.o: constdefs.c
+constants.o: constdefs.h
+constants.o: rubysocket.h
+constants.o: sockport.h
+ifaddr.o: $(RUBY_EXTCONF_H)
+ifaddr.o: $(arch_hdrdir)/ruby/config.h
+ifaddr.o: $(hdrdir)/ruby/defines.h
+ifaddr.o: $(hdrdir)/ruby/encoding.h
+ifaddr.o: $(hdrdir)/ruby/intern.h
+ifaddr.o: $(hdrdir)/ruby/io.h
+ifaddr.o: $(hdrdir)/ruby/missing.h
+ifaddr.o: $(hdrdir)/ruby/oniguruma.h
+ifaddr.o: $(hdrdir)/ruby/ruby.h
+ifaddr.o: $(hdrdir)/ruby/st.h
+ifaddr.o: $(hdrdir)/ruby/subst.h
+ifaddr.o: $(hdrdir)/ruby/thread.h
+ifaddr.o: $(hdrdir)/ruby/util.h
+ifaddr.o: $(top_srcdir)/include/ruby.h
+ifaddr.o: $(top_srcdir)/internal.h
+ifaddr.o: constdefs.h
+ifaddr.o: ifaddr.c
+ifaddr.o: rubysocket.h
+ifaddr.o: sockport.h
+init.o: $(RUBY_EXTCONF_H)
+init.o: $(arch_hdrdir)/ruby/config.h
+init.o: $(hdrdir)/ruby/defines.h
+init.o: $(hdrdir)/ruby/encoding.h
+init.o: $(hdrdir)/ruby/intern.h
+init.o: $(hdrdir)/ruby/io.h
+init.o: $(hdrdir)/ruby/missing.h
+init.o: $(hdrdir)/ruby/oniguruma.h
+init.o: $(hdrdir)/ruby/ruby.h
+init.o: $(hdrdir)/ruby/st.h
+init.o: $(hdrdir)/ruby/subst.h
+init.o: $(hdrdir)/ruby/thread.h
+init.o: $(hdrdir)/ruby/util.h
+init.o: $(top_srcdir)/include/ruby.h
+init.o: $(top_srcdir)/internal.h
+init.o: constdefs.h
+init.o: init.c
+init.o: rubysocket.h
+init.o: sockport.h
+ipsocket.o: $(RUBY_EXTCONF_H)
+ipsocket.o: $(arch_hdrdir)/ruby/config.h
+ipsocket.o: $(hdrdir)/ruby/defines.h
+ipsocket.o: $(hdrdir)/ruby/encoding.h
+ipsocket.o: $(hdrdir)/ruby/intern.h
+ipsocket.o: $(hdrdir)/ruby/io.h
+ipsocket.o: $(hdrdir)/ruby/missing.h
+ipsocket.o: $(hdrdir)/ruby/oniguruma.h
+ipsocket.o: $(hdrdir)/ruby/ruby.h
+ipsocket.o: $(hdrdir)/ruby/st.h
+ipsocket.o: $(hdrdir)/ruby/subst.h
+ipsocket.o: $(hdrdir)/ruby/thread.h
+ipsocket.o: $(hdrdir)/ruby/util.h
+ipsocket.o: $(top_srcdir)/include/ruby.h
+ipsocket.o: $(top_srcdir)/internal.h
+ipsocket.o: constdefs.h
+ipsocket.o: ipsocket.c
+ipsocket.o: rubysocket.h
+ipsocket.o: sockport.h
+option.o: $(RUBY_EXTCONF_H)
+option.o: $(arch_hdrdir)/ruby/config.h
+option.o: $(hdrdir)/ruby/defines.h
+option.o: $(hdrdir)/ruby/encoding.h
+option.o: $(hdrdir)/ruby/intern.h
+option.o: $(hdrdir)/ruby/io.h
+option.o: $(hdrdir)/ruby/missing.h
+option.o: $(hdrdir)/ruby/oniguruma.h
+option.o: $(hdrdir)/ruby/ruby.h
+option.o: $(hdrdir)/ruby/st.h
+option.o: $(hdrdir)/ruby/subst.h
+option.o: $(hdrdir)/ruby/thread.h
+option.o: $(hdrdir)/ruby/util.h
+option.o: $(top_srcdir)/include/ruby.h
+option.o: $(top_srcdir)/internal.h
+option.o: constdefs.h
+option.o: option.c
+option.o: rubysocket.h
+option.o: sockport.h
+raddrinfo.o: $(RUBY_EXTCONF_H)
+raddrinfo.o: $(arch_hdrdir)/ruby/config.h
+raddrinfo.o: $(hdrdir)/ruby/defines.h
+raddrinfo.o: $(hdrdir)/ruby/encoding.h
+raddrinfo.o: $(hdrdir)/ruby/intern.h
+raddrinfo.o: $(hdrdir)/ruby/io.h
+raddrinfo.o: $(hdrdir)/ruby/missing.h
+raddrinfo.o: $(hdrdir)/ruby/oniguruma.h
+raddrinfo.o: $(hdrdir)/ruby/ruby.h
+raddrinfo.o: $(hdrdir)/ruby/st.h
+raddrinfo.o: $(hdrdir)/ruby/subst.h
+raddrinfo.o: $(hdrdir)/ruby/thread.h
+raddrinfo.o: $(hdrdir)/ruby/util.h
+raddrinfo.o: $(top_srcdir)/include/ruby.h
+raddrinfo.o: $(top_srcdir)/internal.h
+raddrinfo.o: constdefs.h
+raddrinfo.o: raddrinfo.c
+raddrinfo.o: rubysocket.h
+raddrinfo.o: sockport.h
+socket.o: $(RUBY_EXTCONF_H)
+socket.o: $(arch_hdrdir)/ruby/config.h
+socket.o: $(hdrdir)/ruby/defines.h
+socket.o: $(hdrdir)/ruby/encoding.h
+socket.o: $(hdrdir)/ruby/intern.h
+socket.o: $(hdrdir)/ruby/io.h
+socket.o: $(hdrdir)/ruby/missing.h
+socket.o: $(hdrdir)/ruby/oniguruma.h
+socket.o: $(hdrdir)/ruby/ruby.h
+socket.o: $(hdrdir)/ruby/st.h
+socket.o: $(hdrdir)/ruby/subst.h
+socket.o: $(hdrdir)/ruby/thread.h
+socket.o: $(hdrdir)/ruby/util.h
+socket.o: $(top_srcdir)/include/ruby.h
+socket.o: $(top_srcdir)/internal.h
+socket.o: constdefs.h
+socket.o: rubysocket.h
+socket.o: socket.c
+socket.o: sockport.h
+sockssocket.o: $(RUBY_EXTCONF_H)
+sockssocket.o: $(arch_hdrdir)/ruby/config.h
+sockssocket.o: $(hdrdir)/ruby/defines.h
+sockssocket.o: $(hdrdir)/ruby/encoding.h
+sockssocket.o: $(hdrdir)/ruby/intern.h
+sockssocket.o: $(hdrdir)/ruby/io.h
+sockssocket.o: $(hdrdir)/ruby/missing.h
+sockssocket.o: $(hdrdir)/ruby/oniguruma.h
+sockssocket.o: $(hdrdir)/ruby/ruby.h
+sockssocket.o: $(hdrdir)/ruby/st.h
+sockssocket.o: $(hdrdir)/ruby/subst.h
+sockssocket.o: $(hdrdir)/ruby/thread.h
+sockssocket.o: $(hdrdir)/ruby/util.h
+sockssocket.o: $(top_srcdir)/include/ruby.h
+sockssocket.o: $(top_srcdir)/internal.h
+sockssocket.o: constdefs.h
+sockssocket.o: rubysocket.h
+sockssocket.o: sockport.h
+sockssocket.o: sockssocket.c
+tcpserver.o: $(RUBY_EXTCONF_H)
+tcpserver.o: $(arch_hdrdir)/ruby/config.h
+tcpserver.o: $(hdrdir)/ruby/defines.h
+tcpserver.o: $(hdrdir)/ruby/encoding.h
+tcpserver.o: $(hdrdir)/ruby/intern.h
+tcpserver.o: $(hdrdir)/ruby/io.h
+tcpserver.o: $(hdrdir)/ruby/missing.h
+tcpserver.o: $(hdrdir)/ruby/oniguruma.h
+tcpserver.o: $(hdrdir)/ruby/ruby.h
+tcpserver.o: $(hdrdir)/ruby/st.h
+tcpserver.o: $(hdrdir)/ruby/subst.h
+tcpserver.o: $(hdrdir)/ruby/thread.h
+tcpserver.o: $(hdrdir)/ruby/util.h
+tcpserver.o: $(top_srcdir)/include/ruby.h
+tcpserver.o: $(top_srcdir)/internal.h
+tcpserver.o: constdefs.h
+tcpserver.o: rubysocket.h
+tcpserver.o: sockport.h
+tcpserver.o: tcpserver.c
+tcpsocket.o: $(RUBY_EXTCONF_H)
+tcpsocket.o: $(arch_hdrdir)/ruby/config.h
+tcpsocket.o: $(hdrdir)/ruby/defines.h
+tcpsocket.o: $(hdrdir)/ruby/encoding.h
+tcpsocket.o: $(hdrdir)/ruby/intern.h
+tcpsocket.o: $(hdrdir)/ruby/io.h
+tcpsocket.o: $(hdrdir)/ruby/missing.h
+tcpsocket.o: $(hdrdir)/ruby/oniguruma.h
+tcpsocket.o: $(hdrdir)/ruby/ruby.h
+tcpsocket.o: $(hdrdir)/ruby/st.h
+tcpsocket.o: $(hdrdir)/ruby/subst.h
+tcpsocket.o: $(hdrdir)/ruby/thread.h
+tcpsocket.o: $(hdrdir)/ruby/util.h
+tcpsocket.o: $(top_srcdir)/include/ruby.h
+tcpsocket.o: $(top_srcdir)/internal.h
+tcpsocket.o: constdefs.h
+tcpsocket.o: rubysocket.h
+tcpsocket.o: sockport.h
+tcpsocket.o: tcpsocket.c
+udpsocket.o: $(RUBY_EXTCONF_H)
+udpsocket.o: $(arch_hdrdir)/ruby/config.h
+udpsocket.o: $(hdrdir)/ruby/defines.h
+udpsocket.o: $(hdrdir)/ruby/encoding.h
+udpsocket.o: $(hdrdir)/ruby/intern.h
+udpsocket.o: $(hdrdir)/ruby/io.h
+udpsocket.o: $(hdrdir)/ruby/missing.h
+udpsocket.o: $(hdrdir)/ruby/oniguruma.h
+udpsocket.o: $(hdrdir)/ruby/ruby.h
+udpsocket.o: $(hdrdir)/ruby/st.h
+udpsocket.o: $(hdrdir)/ruby/subst.h
+udpsocket.o: $(hdrdir)/ruby/thread.h
+udpsocket.o: $(hdrdir)/ruby/util.h
+udpsocket.o: $(top_srcdir)/include/ruby.h
+udpsocket.o: $(top_srcdir)/internal.h
+udpsocket.o: constdefs.h
+udpsocket.o: rubysocket.h
+udpsocket.o: sockport.h
+udpsocket.o: udpsocket.c
+unixserver.o: $(RUBY_EXTCONF_H)
+unixserver.o: $(arch_hdrdir)/ruby/config.h
+unixserver.o: $(hdrdir)/ruby/defines.h
+unixserver.o: $(hdrdir)/ruby/encoding.h
+unixserver.o: $(hdrdir)/ruby/intern.h
+unixserver.o: $(hdrdir)/ruby/io.h
+unixserver.o: $(hdrdir)/ruby/missing.h
+unixserver.o: $(hdrdir)/ruby/oniguruma.h
+unixserver.o: $(hdrdir)/ruby/ruby.h
+unixserver.o: $(hdrdir)/ruby/st.h
+unixserver.o: $(hdrdir)/ruby/subst.h
+unixserver.o: $(hdrdir)/ruby/thread.h
+unixserver.o: $(hdrdir)/ruby/util.h
+unixserver.o: $(top_srcdir)/include/ruby.h
+unixserver.o: $(top_srcdir)/internal.h
+unixserver.o: constdefs.h
+unixserver.o: rubysocket.h
+unixserver.o: sockport.h
+unixserver.o: unixserver.c
+unixsocket.o: $(RUBY_EXTCONF_H)
+unixsocket.o: $(arch_hdrdir)/ruby/config.h
+unixsocket.o: $(hdrdir)/ruby/defines.h
+unixsocket.o: $(hdrdir)/ruby/encoding.h
+unixsocket.o: $(hdrdir)/ruby/intern.h
+unixsocket.o: $(hdrdir)/ruby/io.h
+unixsocket.o: $(hdrdir)/ruby/missing.h
+unixsocket.o: $(hdrdir)/ruby/oniguruma.h
+unixsocket.o: $(hdrdir)/ruby/ruby.h
+unixsocket.o: $(hdrdir)/ruby/st.h
+unixsocket.o: $(hdrdir)/ruby/subst.h
+unixsocket.o: $(hdrdir)/ruby/thread.h
+unixsocket.o: $(hdrdir)/ruby/util.h
+unixsocket.o: $(top_srcdir)/include/ruby.h
+unixsocket.o: $(top_srcdir)/internal.h
+unixsocket.o: constdefs.h
+unixsocket.o: rubysocket.h
+unixsocket.o: sockport.h
+unixsocket.o: unixsocket.c
+# AUTOGENERATED DEPENDENCIES END
diff --git a/jni/ruby/ext/socket/extconf.h b/jni/ruby/ext/socket/extconf.h
new file mode 100644
index 0000000..9386b20
--- /dev/null
+++ b/jni/ruby/ext/socket/extconf.h
@@ -0,0 +1,127 @@
+#ifndef EXTCONF_H
+#define EXTCONF_H
+#define HAVE_SYS_UIO_H 1
+#define HAVE_NETINET_IN_SYSTM_H 1
+#define HAVE_NETINET_TCP_H 1
+#define HAVE_NETINET_UDP_H 1
+#define HAVE_ARPA_INET_H 1
+#define HAVE_NETPACKET_PACKET_H 1
+#define HAVE_NET_ETHERNET_H 1
+#define HAVE_SYS_UN_H 1
+#define HAVE_IFADDRS_H 1
+#define HAVE_SYS_IOCTL_H 1
+#define HAVE_NET_IF_H 1
+#define HAVE_SYS_PARAM_H 1
+#define HAVE_ARPA_NAMESER_H 1
+#define HAVE_RESOLV_H 1
+#define HAVE_TYPE_STRUCT_SOCKADDR_UN 1
+#define HAVE_TYPE_STRUCT_SOCKADDR_STORAGE 1
+#define HAVE_TYPE_STRUCT_ADDRINFO 1
+#define HAVE_TYPE_SOCKLEN_T 1
+#define HAVE_TYPE_STRUCT_IN_PKTINFO 1
+#define HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST 1
+#define HAVE_ST_IPI_SPEC_DST 1
+#define HAVE_TYPE_STRUCT_IN6_PKTINFO 1
+#define HAVE_TYPE_STRUCT_IP_MREQ 1
+#define HAVE_TYPE_STRUCT_IP_MREQN 1
+#define HAVE_TYPE_STRUCT_IPV6_MREQ 1
+#define HAVE_STRUCT_MSGHDR_MSG_CONTROL 1
+#define HAVE_ST_MSG_CONTROL 1
+#define HAVE_TYPE_STRUCT_TCP_INFO 1
+#define HAVE_CONST_TCP_ESTABLISHED 1
+#define HAVE_CONST_TCP_SYN_SENT 1
+#define HAVE_CONST_TCP_SYN_RECV 1
+#define HAVE_CONST_TCP_FIN_WAIT1 1
+#define HAVE_CONST_TCP_FIN_WAIT2 1
+#define HAVE_CONST_TCP_TIME_WAIT 1
+#define HAVE_CONST_TCP_CLOSE 1
+#define HAVE_CONST_TCP_CLOSE_WAIT 1
+#define HAVE_CONST_TCP_LAST_ACK 1
+#define HAVE_CONST_TCP_LISTEN 1
+#define HAVE_CONST_TCP_CLOSING 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_STATE 1
+#define HAVE_ST_TCPI_STATE 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_CA_STATE 1
+#define HAVE_ST_TCPI_CA_STATE 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_RETRANSMITS 1
+#define HAVE_ST_TCPI_RETRANSMITS 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_PROBES 1
+#define HAVE_ST_TCPI_PROBES 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_BACKOFF 1
+#define HAVE_ST_TCPI_BACKOFF 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_OPTIONS 1
+#define HAVE_ST_TCPI_OPTIONS 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_RTO 1
+#define HAVE_ST_TCPI_RTO 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_ATO 1
+#define HAVE_ST_TCPI_ATO 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_SND_MSS 1
+#define HAVE_ST_TCPI_SND_MSS 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_RCV_MSS 1
+#define HAVE_ST_TCPI_RCV_MSS 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_UNACKED 1
+#define HAVE_ST_TCPI_UNACKED 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_SACKED 1
+#define HAVE_ST_TCPI_SACKED 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_LOST 1
+#define HAVE_ST_TCPI_LOST 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_RETRANS 1
+#define HAVE_ST_TCPI_RETRANS 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_FACKETS 1
+#define HAVE_ST_TCPI_FACKETS 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_LAST_DATA_SENT 1
+#define HAVE_ST_TCPI_LAST_DATA_SENT 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_LAST_ACK_SENT 1
+#define HAVE_ST_TCPI_LAST_ACK_SENT 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_LAST_DATA_RECV 1
+#define HAVE_ST_TCPI_LAST_DATA_RECV 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_LAST_ACK_RECV 1
+#define HAVE_ST_TCPI_LAST_ACK_RECV 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_PMTU 1
+#define HAVE_ST_TCPI_PMTU 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_RCV_SSTHRESH 1
+#define HAVE_ST_TCPI_RCV_SSTHRESH 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_RTT 1
+#define HAVE_ST_TCPI_RTT 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_RTTVAR 1
+#define HAVE_ST_TCPI_RTTVAR 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_SND_SSTHRESH 1
+#define HAVE_ST_TCPI_SND_SSTHRESH 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_SND_CWND 1
+#define HAVE_ST_TCPI_SND_CWND 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_ADVMSS 1
+#define HAVE_ST_TCPI_ADVMSS 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_REORDERING 1
+#define HAVE_ST_TCPI_REORDERING 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_RCV_RTT 1
+#define HAVE_ST_TCPI_RCV_RTT 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_RCV_SPACE 1
+#define HAVE_ST_TCPI_RCV_SPACE 1
+#define HAVE_STRUCT_TCP_INFO_TCPI_TOTAL_RETRANS 1
+#define HAVE_ST_TCPI_TOTAL_RETRANS 1
+#define HAVE_SOCKET 1
+#define HAVE_SENDMSG 1
+#define HAVE_RECVMSG 1
+#define HAVE_FREEADDRINFO 1
+#define HAVE_GAI_STRERROR 1
+#define GAI_STRERROR_CONST 1
+#define HAVE_ACCEPT4 1
+#define HAVE_INET_NTOP 1
+#define HAVE_INET_PTON 1
+#define HAVE_GETSERVBYPORT 1
+#define HAVE_GETIFADDRS 1
+#define HAVE_IF_INDEXTONAME 1
+#define NEED_IF_INDEXTONAME_DECL 1
+#define HAVE_IF_NAMETOINDEX 1
+#define NEED_IF_NAMETOINDEX_DECL 1
+#define HAVE_GETHOSTBYNAME2 1
+#define HAVE_SOCKETPAIR 1
+#define HAVE_GETHOSTNAME 1
+#define ENABLE_IPV6 1
+#define INET6 1
+#define HAVE_CONST_AF_UNIX 1
+#define HAVE_CONST_SCM_RIGHTS 1
+#define FD_PASSING_WORK_WITH_RECVMSG_MSG_PEEK 1
+#define HAVE_GETNAMEINFO 1
+#define HAVE_GETADDRINFO 1
+#endif
diff --git a/jni/ruby/ext/socket/extconf.rb b/jni/ruby/ext/socket/extconf.rb
new file mode 100644
index 0000000..6d15bba
--- /dev/null
+++ b/jni/ruby/ext/socket/extconf.rb
@@ -0,0 +1,695 @@
+require 'mkmf'
+
+AF_INET6_SOCKET_CREATION_TEST = <<EOF
+#include <sys/types.h>
+#ifndef _WIN32
+#include <sys/socket.h>
+#endif
+int
+main(void)
+{
+ socket(AF_INET6, SOCK_STREAM, 0);
+ return 0;
+}
+EOF
+
+GETADDRINFO_GETNAMEINFO_TEST = <<EOF
+#include <stdlib.h>
+
+#ifndef EXIT_SUCCESS
+#define EXIT_SUCCESS 0
+#endif
+#ifndef EXIT_FAILURE
+#define EXIT_FAILURE 1
+#endif
+
+#ifndef AF_LOCAL
+#define AF_LOCAL AF_UNIX
+#endif
+
+int
+main(void)
+{
+ int passive, gaierr, inet4 = 0, inet6 = 0;
+ struct addrinfo hints, *ai, *aitop;
+ char straddr[INET6_ADDRSTRLEN], strport[16];
+#ifdef _WIN32
+ WSADATA retdata;
+
+ WSAStartup(MAKEWORD(2, 0), &retdata);
+#endif
+
+ for (passive = 0; passive <= 1; passive++) {
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_protocol = IPPROTO_TCP;
+ hints.ai_flags = passive ? AI_PASSIVE : 0;
+ hints.ai_socktype = SOCK_STREAM;
+ if ((gaierr = getaddrinfo(NULL, "54321", &hints, &aitop)) != 0) {
+ (void)gai_strerror(gaierr);
+ goto bad;
+ }
+ for (ai = aitop; ai; ai = ai->ai_next) {
+ if (ai->ai_family == AF_LOCAL) continue;
+ if (ai->ai_addr == NULL)
+ goto bad;
+#if defined(_AIX)
+ if (ai->ai_family == AF_INET6 && passive) {
+ inet6++;
+ continue;
+ }
+ ai->ai_addr->sa_len = ai->ai_addrlen;
+ ai->ai_addr->sa_family = ai->ai_family;
+#endif
+ if (ai->ai_addrlen == 0 ||
+ getnameinfo(ai->ai_addr, ai->ai_addrlen,
+ straddr, sizeof(straddr), strport, sizeof(strport),
+ NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
+ goto bad;
+ }
+ if (strcmp(strport, "54321") != 0) {
+ goto bad;
+ }
+ switch (ai->ai_family) {
+ case AF_INET:
+ if (passive) {
+ if (strcmp(straddr, "0.0.0.0") != 0) {
+ goto bad;
+ }
+ } else {
+ if (strcmp(straddr, "127.0.0.1") != 0) {
+ goto bad;
+ }
+ }
+ inet4++;
+ break;
+ case AF_INET6:
+ if (passive) {
+ if (strcmp(straddr, "::") != 0) {
+ goto bad;
+ }
+ } else {
+ if (strcmp(straddr, "::1") != 0) {
+ goto bad;
+ }
+ }
+ inet6++;
+ break;
+ case AF_UNSPEC:
+ goto bad;
+ break;
+ default:
+ /* another family support? */
+ break;
+ }
+ }
+ }
+
+ if (!(inet4 == 0 || inet4 == 2))
+ goto bad;
+ if (!(inet6 == 0 || inet6 == 2))
+ goto bad;
+
+ if (aitop)
+ freeaddrinfo(aitop);
+ return EXIT_SUCCESS;
+
+ bad:
+ if (aitop)
+ freeaddrinfo(aitop);
+ return EXIT_FAILURE;
+}
+EOF
+
+RECVMSG_WITH_MSG_PEEK_ALLOCATE_FD_TEST = <<'EOF'
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+int main(int argc, char *argv[])
+{
+ int ps[2], sv[2];
+ int ret;
+ ssize_t ss;
+ int s_fd, r_fd;
+ struct msghdr s_msg, r_msg;
+ union {
+ struct cmsghdr hdr;
+ char dummy[CMSG_SPACE(sizeof(int))];
+ } s_cmsg, r_cmsg;
+ struct iovec s_iov, r_iov;
+ char s_buf[1], r_buf[1];
+ struct stat s_statbuf, r_statbuf;
+
+ ret = pipe(ps);
+ if (ret == -1) { perror("pipe"); exit(EXIT_FAILURE); }
+
+ s_fd = ps[0];
+
+ ret = socketpair(AF_UNIX, SOCK_DGRAM, 0, sv);
+ if (ret == -1) { perror("socketpair"); exit(EXIT_FAILURE); }
+
+ s_msg.msg_name = NULL;
+ s_msg.msg_namelen = 0;
+ s_msg.msg_iov = &s_iov;
+ s_msg.msg_iovlen = 1;
+ s_msg.msg_control = &s_cmsg;
+ s_msg.msg_controllen = CMSG_SPACE(sizeof(int));;
+ s_msg.msg_flags = 0;
+
+ s_iov.iov_base = &s_buf;
+ s_iov.iov_len = sizeof(s_buf);
+
+ s_buf[0] = 'a';
+
+ s_cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(int));
+ s_cmsg.hdr.cmsg_level = SOL_SOCKET;
+ s_cmsg.hdr.cmsg_type = SCM_RIGHTS;
+ memcpy(CMSG_DATA(&s_cmsg.hdr), (char *)&s_fd, sizeof(int));
+
+ ss = sendmsg(sv[0], &s_msg, 0);
+ if (ss == -1) { perror("sendmsg"); exit(EXIT_FAILURE); }
+
+ r_msg.msg_name = NULL;
+ r_msg.msg_namelen = 0;
+ r_msg.msg_iov = &r_iov;
+ r_msg.msg_iovlen = 1;
+ r_msg.msg_control = &r_cmsg;
+ r_msg.msg_controllen = CMSG_SPACE(sizeof(int));
+ r_msg.msg_flags = 0;
+
+ r_iov.iov_base = &r_buf;
+ r_iov.iov_len = sizeof(r_buf);
+
+ r_buf[0] = '0';
+
+ memset(&r_cmsg, 0xff, CMSG_SPACE(sizeof(int)));
+
+ ss = recvmsg(sv[1], &r_msg, MSG_PEEK);
+ if (ss == -1) { perror("recvmsg"); exit(EXIT_FAILURE); }
+
+ if (ss != 1) {
+ fprintf(stderr, "unexpected return value from recvmsg: %ld\n", (long)ss);
+ exit(EXIT_FAILURE);
+ }
+ if (r_buf[0] != 'a') {
+ fprintf(stderr, "unexpected return data from recvmsg: 0x%02x\n", r_buf[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ if (r_msg.msg_controllen < CMSG_LEN(sizeof(int))) {
+ fprintf(stderr, "unexpected: r_msg.msg_controllen < CMSG_LEN(sizeof(int)) not hold: %ld\n",
+ (long)r_msg.msg_controllen);
+ exit(EXIT_FAILURE);
+ }
+ if (r_cmsg.hdr.cmsg_len < CMSG_LEN(sizeof(int))) {
+ fprintf(stderr, "unexpected: r_cmsg.hdr.cmsg_len < CMSG_LEN(sizeof(int)) not hold: %ld\n",
+ (long)r_cmsg.hdr.cmsg_len);
+ exit(EXIT_FAILURE);
+ }
+ memcpy((char *)&r_fd, CMSG_DATA(&r_cmsg.hdr), sizeof(int));
+
+ if (r_fd < 0) {
+ fprintf(stderr, "negative r_fd: %d\n", r_fd);
+ exit(EXIT_FAILURE);
+ }
+
+ if (r_fd == s_fd) {
+ fprintf(stderr, "r_fd and s_fd is same: %d\n", r_fd);
+ exit(EXIT_FAILURE);
+ }
+
+ ret = fstat(s_fd, &s_statbuf);
+ if (ret == -1) { perror("fstat(s_fd)"); exit(EXIT_FAILURE); }
+
+ ret = fstat(r_fd, &r_statbuf);
+ if (ret == -1) { perror("fstat(r_fd)"); exit(EXIT_FAILURE); }
+
+ if (s_statbuf.st_dev != r_statbuf.st_dev ||
+ s_statbuf.st_ino != r_statbuf.st_ino) {
+ fprintf(stderr, "dev/ino doesn't match: s_fd:%ld/%ld r_fd:%ld/%ld\n",
+ (long)s_statbuf.st_dev, (long)s_statbuf.st_ino,
+ (long)r_statbuf.st_dev, (long)r_statbuf.st_ino);
+ exit(EXIT_FAILURE);
+ }
+
+ return EXIT_SUCCESS;
+}
+EOF
+
+def test_recvmsg_with_msg_peek_creates_fds(headers)
+ case RUBY_PLATFORM
+ when /linux/
+ # Linux 2.6.38 allocate fds by recvmsg with MSG_PEEK.
+ close_fds = true
+ when /bsd|darwin/
+ # FreeBSD 8.2.0, NetBSD 5 and MacOS X Snow Leopard doesn't
+ # allocate fds by recvmsg with MSG_PEEK.
+ # [ruby-dev:44189]
+ # http://bugs.ruby-lang.org/issues/5075
+ close_fds = false
+ when /cygwin/
+ # Cygwin doesn't support fd passing.
+ # http://cygwin.com/ml/cygwin/2003-09/msg01808.html
+ close_fds = false
+ else
+ close_fds = nil
+ end
+ if !CROSS_COMPILING
+ if checking_for("recvmsg() with MSG_PEEK allocate file descriptors") {
+ try_run(cpp_include(headers) + RECVMSG_WITH_MSG_PEEK_ALLOCATE_FD_TEST)
+ }
+ if close_fds == false
+ warn "unexpected fd-passing recvmsg() with MSG_PEEK behavor on #{RUBY_PLATFORM}: fd allocation unexpected."
+ elsif close_fds == nil
+ puts "info: #{RUBY_PLATFORM} recvmsg() with MSG_PEEK allocates fds on fd-passing."
+ end
+ close_fds = true
+ else
+ if close_fds == true
+ warn "unexpected fd-passing recvmsg() with MSG_PEEK behavor on #{RUBY_PLATFORM}: fd allocation expected."
+ elsif close_fds == nil
+ puts "info: #{RUBY_PLATFORM}: recvmsg() with MSG_PEEK doesn't allocates fds on fd-passing."
+ end
+ close_fds = false
+ end
+ end
+ if close_fds == nil
+ abort <<EOS
+Fatal: cannot test fd-passing recvmsg() with MSG_PEEK behavor
+because cross-compilation for #{RUBY_PLATFORM}.
+If recvmsg() with MSG_PEEK allocates fds on fd passing:
+--enable-close-fds-by-recvmsg-with-peek
+If recvmsg() with MSG_PEEK doesn't allocate fds on fd passing:
+--disable-close-fds-by-recvmsg-with-peek
+EOS
+ end
+ close_fds
+end
+
+$INCFLAGS << " -I$(topdir) -I$(top_srcdir)"
+
+if /darwin/ =~ RUBY_PLATFORM
+ # For IPv6 extension header access on OS X 10.7+ [Bug #8517]
+ $CFLAGS << " -D__APPLE_USE_RFC_3542"
+end
+
+headers = []
+unless $mswin or $mingw
+ headers = %w<sys/types.h netdb.h string.h sys/socket.h netinet/in.h>
+end
+
+%w[
+ sys/uio.h
+ xti.h
+ netinet/in_systm.h
+ netinet/tcp.h
+ netinet/tcp_fsm.h
+ netinet/udp.h
+ arpa/inet.h
+ netpacket/packet.h
+ net/ethernet.h
+ sys/un.h
+ ifaddrs.h
+ sys/ioctl.h
+ sys/sockio.h
+ net/if.h
+ sys/param.h
+ sys/ucred.h
+ ucred.h
+ net/if_dl.h
+ arpa/nameser.h
+ resolv.h
+].each {|h|
+ if have_header(h, headers)
+ headers << h
+ end
+}
+
+have_struct_member("struct sockaddr", "sa_len", headers) # 4.4BSD
+have_struct_member("struct sockaddr_in", "sin_len", headers) # 4.4BSD
+have_struct_member("struct sockaddr_in6", "sin6_len", headers) # 4.4BSD
+
+if have_type("struct sockaddr_un", headers) # POSIX
+ have_struct_member("struct sockaddr_un", "sun_len", headers) # 4.4BSD
+end
+
+have_type("struct sockaddr_dl", headers) # AF_LINK address. 4.4BSD since Net2
+
+have_type("struct sockaddr_storage", headers)
+
+have_type("struct addrinfo", headers)
+
+if have_type("socklen_t", headers)
+ if try_static_assert("sizeof(socklen_t) >= sizeof(long)", headers)
+ $defs << "-DRSTRING_SOCKLEN=(socklen_t)RSTRING_LEN"
+ end
+end
+
+have_type("struct in_pktinfo", headers) {|src|
+ src.sub(%r'^/\*top\*/', '\&'"\n#if defined(IPPROTO_IP) && defined(IP_PKTINFO)") <<
+ "#else\n" << "#error\n" << ">>>>>> no in_pktinfo <<<<<<\n" << "#endif\n"
+} and have_struct_member("struct in_pktinfo", "ipi_spec_dst", headers)
+have_type("struct in6_pktinfo", headers) {|src|
+ src.sub(%r'^/\*top\*/', '\&'"\n#if defined(IPPROTO_IPV6) && defined(IPV6_PKTINFO)") <<
+ "#else\n" << "#error\n" << ">>>>>> no in6_pktinfo <<<<<<\n" << "#endif\n"
+}
+
+have_type("struct sockcred", headers)
+have_type("struct cmsgcred", headers)
+
+have_type("struct ip_mreq", headers) # 4.4BSD
+have_type("struct ip_mreqn", headers) # Linux 2.4
+have_type("struct ipv6_mreq", headers) # RFC 3493
+
+have_msg_control = nil
+have_msg_control = have_struct_member('struct msghdr', 'msg_control', headers) unless $mswin or $mingw
+have_struct_member('struct msghdr', 'msg_accrights', headers)
+
+if have_type("struct tcp_info", headers)
+ have_const("TCP_ESTABLISHED", headers)
+ have_const("TCP_SYN_SENT", headers)
+ have_const("TCP_SYN_RECV", headers)
+ have_const("TCP_FIN_WAIT1", headers)
+ have_const("TCP_FIN_WAIT2", headers)
+ have_const("TCP_TIME_WAIT", headers)
+ have_const("TCP_CLOSE", headers)
+ have_const("TCP_CLOSE_WAIT", headers)
+ have_const("TCP_LAST_ACK", headers)
+ have_const("TCP_LISTEN", headers)
+ have_const("TCP_CLOSING", headers)
+ have_struct_member('struct tcp_info', 'tcpi_state', headers)
+ if /solaris/ !~ RUBY_PLATFORM
+ have_struct_member('struct tcp_info', 'tcpi_ca_state', headers)
+ end
+ have_struct_member('struct tcp_info', 'tcpi_retransmits', headers)
+ have_struct_member('struct tcp_info', 'tcpi_probes', headers)
+ have_struct_member('struct tcp_info', 'tcpi_backoff', headers)
+ have_struct_member('struct tcp_info', 'tcpi_options', headers)
+ have_struct_member('struct tcp_info', 'tcpi_snd_wscale', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rcv_wscale', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rto', headers)
+ have_struct_member('struct tcp_info', 'tcpi_ato', headers)
+ have_struct_member('struct tcp_info', 'tcpi_snd_mss', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rcv_mss', headers)
+ have_struct_member('struct tcp_info', 'tcpi_unacked', headers)
+ have_struct_member('struct tcp_info', 'tcpi_sacked', headers)
+ have_struct_member('struct tcp_info', 'tcpi_lost', headers)
+ have_struct_member('struct tcp_info', 'tcpi_retrans', headers)
+ have_struct_member('struct tcp_info', 'tcpi_fackets', headers)
+ have_struct_member('struct tcp_info', 'tcpi_last_data_sent', headers)
+ have_struct_member('struct tcp_info', 'tcpi_last_ack_sent', headers)
+ have_struct_member('struct tcp_info', 'tcpi_last_data_recv', headers)
+ have_struct_member('struct tcp_info', 'tcpi_last_ack_recv', headers)
+ have_struct_member('struct tcp_info', 'tcpi_pmtu', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rcv_ssthresh', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rtt', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rttvar', headers)
+ have_struct_member('struct tcp_info', 'tcpi_snd_ssthresh', headers)
+ have_struct_member('struct tcp_info', 'tcpi_snd_cwnd', headers)
+ have_struct_member('struct tcp_info', 'tcpi_advmss', headers)
+ have_struct_member('struct tcp_info', 'tcpi_reordering', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rcv_rtt', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rcv_space', headers)
+ have_struct_member('struct tcp_info', 'tcpi_total_retrans', headers)
+
+ # FreeBSD extension
+ have_struct_member('struct tcp_info', 'tcpi_snd_wnd', headers)
+ have_struct_member('struct tcp_info', 'tcpi_snd_bwnd', headers)
+ have_struct_member('struct tcp_info', 'tcpi_snd_nxt', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rcv_nxt', headers)
+ have_struct_member('struct tcp_info', 'tcpi_toe_tid', headers)
+ have_struct_member('struct tcp_info', 'tcpi_snd_rexmitpack', headers)
+ have_struct_member('struct tcp_info', 'tcpi_rcv_ooopack', headers)
+ have_struct_member('struct tcp_info', 'tcpi_snd_zerowin', headers)
+end
+
+case RUBY_PLATFORM
+when /mswin(32|64)|mingw/
+ test_func = "WSACleanup"
+ have_library("ws2_32", "WSACleanup", headers)
+when /cygwin/
+ test_func = "socket(0,0,0)"
+when /beos/
+ test_func = "socket(0,0,0)"
+ have_library("net", "socket(0,0,0)", headers)
+when /haiku/
+ test_func = "socket(0,0,0)"
+ have_library("network", "socket(0,0,0)", headers)
+when /i386-os2_emx/
+ test_func = "socket(0,0,0)"
+ have_library("socket", "socket(0,0,0)", headers)
+else
+ test_func = "socket(0,0,0)"
+ have_library("nsl", 't_open("", 0, (struct t_info *)NULL)', headers) # SunOS
+ have_library("socket", "socket(0,0,0)", headers) # SunOS
+end
+
+if have_func(test_func, headers)
+
+ have_func("sendmsg(0, (struct msghdr *)NULL, 0)", headers) # POSIX
+ have_recvmsg = have_func("recvmsg(0, (struct msghdr *)NULL, 0)", headers) # POSIX
+
+ have_func("freehostent((struct hostent *)NULL)", headers) # RFC 2553
+ have_func("freeaddrinfo((struct addrinfo *)NULL)", headers) # RFC 2553
+
+ if /haiku/ !~ RUBY_PLATFORM and
+ have_func("gai_strerror(0)", headers) # POSIX
+ if checking_for("gai_strerror() returns const pointer") {!try_compile(<<EOF)}
+#{cpp_include(headers)}
+#include <stdlib.h>
+void
+conftest_gai_strerror_is_const()
+{
+ *gai_strerror(0) = 0;
+}
+EOF
+ $defs << "-DGAI_STRERROR_CONST"
+ end
+ end
+
+ have_func("accept4", headers)
+
+ have_func('inet_ntop(0, (const void *)0, (char *)0, 0)', headers) or
+ have_func("inet_ntoa(*(struct in_addr *)NULL)", headers)
+ have_func('inet_pton(0, "", (void *)0)', headers) or
+ have_func('inet_aton("", (struct in_addr *)0)', headers)
+ have_func('getservbyport(0, "")', headers)
+ have_func("getifaddrs((struct ifaddrs **)NULL)", headers)
+
+ have_func("getpeereid", headers)
+
+ have_func("getpeerucred(0, (ucred_t **)NULL)", headers) # SunOS
+
+ have_func_decl = proc do |name, headers|
+ if !checking_for("declaration of #{name}()") {!%w[int void].all? {|ret| try_compile(<<EOF)}}
+#{cpp_include(headers)}
+#{ret} #{name}(void);
+EOF
+ $defs << "-DNEED_#{name.tr_cpp}_DECL"
+ end
+ end
+ if have_func('if_indextoname(0, "")', headers)
+ have_func_decl["if_indextoname"]
+ end
+ if have_func('if_nametoindex("")', headers)
+ have_func_decl["if_nametoindex"]
+ end
+
+ have_func("hsterror", headers)
+ have_func('getipnodebyname("", 0, 0, (int *)0)', headers) # RFC 2553
+ have_func('gethostbyname2("", 0)', headers) # RFC 2133
+ have_func("socketpair(0, 0, 0, 0)", headers)
+ unless have_func("gethostname((char *)0, 0)", headers)
+ have_func("uname((struct utsname *)NULL)", headers)
+ end
+
+ ipv6 = false
+ default_ipv6 = /beos|haiku/ !~ RUBY_PLATFORM
+ if enable_config("ipv6", default_ipv6)
+ if checking_for("ipv6") {try_link(AF_INET6_SOCKET_CREATION_TEST)}
+ $defs << "-DENABLE_IPV6" << "-DINET6"
+ ipv6 = true
+ end
+ end
+
+ if ipv6
+ if $mingw
+ $CPPFLAGS << " -D_WIN32_WINNT=0x501" unless $CPPFLAGS.include?("_WIN32_WINNT")
+ end
+ ipv6lib = nil
+ class << (fmt = "unknown")
+ def %(s) s || self end
+ end
+ idirs, ldirs = dir_config("inet6", %w[/usr/inet6 /usr/local/v6].find {|d| File.directory?(d)})
+ checking_for("ipv6 type", fmt) do
+ if have_macro("IPV6_INRIA_VERSION", "netinet/in.h")
+ "inria"
+ elsif have_macro("__KAME__", "netinet/in.h")
+ have_library(ipv6lib = "inet6")
+ "kame"
+ elsif have_macro("_TOSHIBA_INET6", "sys/param.h")
+ have_library(ipv6lib = "inet6") and "toshiba"
+ elsif have_macro("__V6D__", "sys/v6config.h")
+ have_library(ipv6lib = "v6") and "v6d"
+ elsif have_macro("_ZETA_MINAMI_INET6", "sys/param.h")
+ have_library(ipv6lib = "inet6") and "zeta"
+ elsif have_library("inet6")
+ "inet6"
+ end
+ end or not ipv6lib or abort <<EOS
+
+Fatal: no #{ipv6lib} library found. cannot continue.
+You need to fetch lib#{ipv6lib}.a from appropriate
+ipv6 kit and compile beforehand.
+EOS
+ end
+
+ if !have_macro("IPPROTO_IPV6", headers) && have_const("IPPROTO_IPV6", headers)
+ IO.read(File.join(File.dirname(__FILE__), "mkconstants.rb")).sub(/\A.*^__END__$/m, '').split(/\r?\n/).grep(/\AIPPROTO_\w*/){$&}.each {|name|
+ have_const(name, headers) unless $defs.include?("-DHAVE_CONST_#{name.upcase}")
+ }
+ end
+
+ if enable_config("close-fds-by-recvmsg-with-peek") {
+ have_msg_control && have_recvmsg &&
+ have_const('AF_UNIX', headers) && have_const('SCM_RIGHTS', headers) &&
+ test_recvmsg_with_msg_peek_creates_fds(headers)
+ }
+ $defs << "-DFD_PASSING_WORK_WITH_RECVMSG_MSG_PEEK"
+ end
+
+ case enable_config("wide-getaddrinfo")
+ when true
+ getaddr_info_ok = :wide
+ when nil
+ if have_func("getnameinfo", headers) and have_func("getaddrinfo", headers)
+ getaddr_info_ok = :os
+ if !CROSS_COMPILING &&
+ !checking_for("system getaddrinfo working") {
+ try_run(cpp_include(headers) + GETADDRINFO_GETNAMEINFO_TEST)
+ }
+ getaddr_info_ok = :wide
+ end
+ else
+ getaddr_info_ok = :wide
+ end
+ when false
+ if have_func("getnameinfo", headers) and have_func("getaddrinfo", headers)
+ getaddr_info_ok = :os
+ if !CROSS_COMPILING &&
+ !checking_for("system getaddrinfo working") {
+ try_run(cpp_include(headers) + GETADDRINFO_GETNAMEINFO_TEST)
+ }
+ getaddr_info_ok = nil
+ end
+ else
+ getaddr_info_ok = nil
+ end
+ else
+ raise "unexpected enable_config() value"
+ end
+
+ if ipv6 and not getaddr_info_ok
+ abort <<EOS
+
+Fatal: --enable-ipv6 is specified, and your OS seems to support IPv6 feature.
+But your getaddrinfo() and getnameinfo() are appeared to be broken. Sorry,
+you cannot compile IPv6 socket classes with broken these functions.
+You can try --enable-wide-getaddrinfo.
+EOS
+ end
+
+ case with_config("lookup-order-hack", "UNSPEC")
+ when "INET"
+ $defs << "-DLOOKUP_ORDER_HACK_INET"
+ when "INET6"
+ $defs << "-DLOOKUP_ORDER_HACK_INET6"
+ when "UNSPEC"
+ # nothing special
+ else
+ abort <<EOS
+
+Fatal: invalid value for --with-lookup-order-hack (expected INET, INET6 or UNSPEC)
+EOS
+ end
+
+ $objs = [
+ "init.#{$OBJEXT}",
+ "constants.#{$OBJEXT}",
+ "basicsocket.#{$OBJEXT}",
+ "socket.#{$OBJEXT}",
+ "ipsocket.#{$OBJEXT}",
+ "tcpsocket.#{$OBJEXT}",
+ "tcpserver.#{$OBJEXT}",
+ "sockssocket.#{$OBJEXT}",
+ "udpsocket.#{$OBJEXT}",
+ "unixsocket.#{$OBJEXT}",
+ "unixserver.#{$OBJEXT}",
+ "option.#{$OBJEXT}",
+ "ancdata.#{$OBJEXT}",
+ "raddrinfo.#{$OBJEXT}",
+ "ifaddr.#{$OBJEXT}"
+ ]
+
+ if getaddr_info_ok == :wide
+ if !have_type("struct in6_addr", headers) and have_type("struct in_addr6", headers)
+ $defs.pop(2)
+ $defs << "-Din_addr6=in6_addr"
+ end
+ if have_struct_member("struct in6_addr", "s6_addr8", headers)
+ $defs[-1] = "-Ds6_addr=s6_addr8"
+ end
+ if ipv6 == "kame" && have_struct_member("struct in6_addr", "s6_addr32", headers)
+ $defs[-1] = "-DFAITH"
+ end
+ $CPPFLAGS="-I. "+$CPPFLAGS
+ $objs += ["getaddrinfo.#{$OBJEXT}"]
+ $objs += ["getnameinfo.#{$OBJEXT}"]
+ $defs << "-DGETADDRINFO_EMU"
+ end
+
+ # workaround for recent Windows SDK
+ $defs << "-DIPPROTO_IPV6=IPPROTO_IPV6" if $defs.include?("-DHAVE_CONST_IPPROTO_IPV6") && !have_macro("IPPROTO_IPV6")
+
+ $distcleanfiles << "constants.h" << "constdefs.*"
+
+ if enable_config("socks", ENV["SOCKS_SERVER"])
+ if have_library("socks5", "SOCKSinit")
+ $defs << "-DSOCKS5" << "-DSOCKS"
+ elsif have_library("socks", "Rconnect")
+ $defs << "-DSOCKS"
+ end
+ end
+
+ hdr = "netinet6/in6.h"
+ if /darwin/ =~ RUBY_PLATFORM and !try_compile(<<"SRC", nil, :werror=>true)
+#include <netinet/in.h>
+int t(struct in6_addr *addr) {return IN6_IS_ADDR_UNSPECIFIED(addr);}
+SRC
+ print "fixing apple's netinet6/in6.rb ..."; $stdout.flush
+ in6 = File.read("/usr/include/#{hdr}")
+ if in6.gsub!(/\*\(const\s+__uint32_t\s+\*\)\(const\s+void\s+\*\)\(&(\(\w+\))->s6_addr\[(\d+)\]\)/) do
+ i, r = $2.to_i.divmod(4)
+ if r.zero?
+ "#$1->__u6_addr.__u6_addr32[#{i}]"
+ else
+ $&
+ end
+ end
+ FileUtils.mkdir_p(File.dirname(hdr))
+ open(hdr, "w") {|f| f.write(in6)}
+ $distcleanfiles << hdr
+ $distcleandirs << File.dirname(hdr)
+ puts "done"
+ else
+ puts "not needed"
+ end
+ end
+ create_makefile("socket")
+end
diff --git a/jni/ruby/ext/socket/getaddrinfo.c b/jni/ruby/ext/socket/getaddrinfo.c
new file mode 100644
index 0000000..68f610e
--- /dev/null
+++ b/jni/ruby/ext/socket/getaddrinfo.c
@@ -0,0 +1,679 @@
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
+ *
+ * Issues to be discussed:
+ * - Thread safe-ness must be checked.
+ * - Return values. There are nonstandard return values defined and used
+ * in the source code. This is because RFC2133 is silent about which error
+ * code must be returned for which situation.
+ * - PF_UNSPEC case would be handled in getipnodebyname() with the AI_ALL flag.
+ */
+
+#include "ruby/config.h"
+#ifdef RUBY_EXTCONF_H
+#include RUBY_EXTCONF_H
+#endif
+#include <sys/types.h>
+#ifndef _WIN32
+#include <sys/param.h>
+#if defined(__BEOS__) && !defined(__HAIKU__) && !defined(BONE)
+# include <net/socket.h>
+#else
+# include <sys/socket.h>
+#endif
+#include <netinet/in.h>
+#if defined(HAVE_ARPA_INET_H)
+#include <arpa/inet.h>
+#endif
+#if defined(HAVE_ARPA_NAMESER_H)
+#include <arpa/nameser.h>
+#endif
+#include <netdb.h>
+#if defined(HAVE_RESOLV_H)
+#ifdef _SX
+#include <stdio.h>
+#endif
+#include <resolv.h>
+#endif
+#include <unistd.h>
+#else
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#include <io.h>
+#endif
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <ctype.h>
+
+#ifdef SOCKS5
+#include <socks.h>
+#endif
+
+#ifndef HAVE_TYPE_SOCKLEN_T
+typedef int socklen_t;
+#endif
+
+#include "addrinfo.h"
+#include "sockport.h"
+
+#define SUCCESS 0
+#define ANY 0
+#define YES 1
+#define NO 0
+
+#ifdef FAITH
+static int translate = NO;
+static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
+#endif
+
+static const char in_addrany[] = { 0, 0, 0, 0 };
+static const char in6_addrany[] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+static const char in_loopback[] = { 127, 0, 0, 1 };
+static const char in6_loopback[] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
+};
+
+struct sockinet {
+ u_char si_len;
+ u_char si_family;
+ u_short si_port;
+};
+
+static const struct afd {
+ int a_af;
+ int a_addrlen;
+ int a_socklen;
+ int a_off;
+ const char *a_addrany;
+ const char *a_loopback;
+} afdl [] = {
+#ifdef INET6
+#define N_INET6 0
+ {PF_INET6, sizeof(struct in6_addr),
+ sizeof(struct sockaddr_in6),
+ offsetof(struct sockaddr_in6, sin6_addr),
+ in6_addrany, in6_loopback},
+#define N_INET 1
+#else
+#define N_INET 0
+#endif
+ {PF_INET, sizeof(struct in_addr),
+ sizeof(struct sockaddr_in),
+ offsetof(struct sockaddr_in, sin_addr),
+ in_addrany, in_loopback},
+ {0, 0, 0, 0, NULL, NULL},
+};
+
+#ifdef INET6
+#define PTON_MAX 16
+#else
+#define PTON_MAX 4
+#endif
+
+static int get_name __P((const char *, const struct afd *,
+ struct addrinfo **, char *, struct addrinfo *,
+ int));
+static int get_addr __P((const char *, int, struct addrinfo **,
+ struct addrinfo *, int));
+static int str_isnumber __P((const char *));
+
+static const char *const ai_errlist[] = {
+ "success.",
+ "address family for hostname not supported.", /* EAI_ADDRFAMILY */
+ "temporary failure in name resolution.", /* EAI_AGAIN */
+ "invalid value for ai_flags.", /* EAI_BADFLAGS */
+ "non-recoverable failure in name resolution.", /* EAI_FAIL */
+ "ai_family not supported.", /* EAI_FAMILY */
+ "memory allocation failure.", /* EAI_MEMORY */
+ "no address associated with hostname.", /* EAI_NODATA */
+ "hostname nor servname provided, or not known.",/* EAI_NONAME */
+ "servname not supported for ai_socktype.", /* EAI_SERVICE */
+ "ai_socktype not supported.", /* EAI_SOCKTYPE */
+ "system error returned in errno.", /* EAI_SYSTEM */
+ "invalid value for hints.", /* EAI_BADHINTS */
+ "resolved protocol is unknown.", /* EAI_PROTOCOL */
+ "unknown error.", /* EAI_MAX */
+};
+
+#define GET_CANONNAME(ai, str) \
+if (pai->ai_flags & AI_CANONNAME) {\
+ if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
+ strcpy((ai)->ai_canonname, (str));\
+ } else {\
+ error = EAI_MEMORY;\
+ goto free;\
+ }\
+}
+
+#define GET_AI(ai, afd, addr, port) {\
+ char *p;\
+ if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
+ ((afd)->a_socklen)))\
+ == NULL) {\
+ error = EAI_MEMORY;\
+ goto free;\
+ }\
+ memcpy((ai), pai, sizeof(struct addrinfo));\
+ (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
+ (ai)->ai_family = (afd)->a_af;\
+ (ai)->ai_addrlen = (afd)->a_socklen;\
+ INIT_SOCKADDR((ai)->ai_addr, (afd)->a_af, (afd)->a_socklen);\
+ ((struct sockinet *)(ai)->ai_addr)->si_port = (port);\
+ p = (char *)((ai)->ai_addr);\
+ memcpy(p + (afd)->a_off, (addr), (afd)->a_addrlen);\
+}
+
+#define ERR(err) { error = (err); goto bad; }
+
+#ifndef HAVE_GAI_STRERROR
+#ifdef GAI_STRERROR_CONST
+const
+#endif
+char *
+gai_strerror(int ecode)
+{
+ if (ecode < 0 || ecode > EAI_MAX)
+ ecode = EAI_MAX;
+ return (char *)ai_errlist[ecode];
+}
+#endif
+
+void
+freeaddrinfo(struct addrinfo *ai)
+{
+ struct addrinfo *next;
+
+ do {
+ next = ai->ai_next;
+ if (ai->ai_canonname)
+ free(ai->ai_canonname);
+ /* no need to free(ai->ai_addr) */
+ free(ai);
+ } while ((ai = next) != NULL);
+}
+
+static int
+str_isnumber(const char *p)
+{
+ char *q = (char *)p;
+ while (*q) {
+ if (! isdigit(*q))
+ return NO;
+ q++;
+ }
+ return YES;
+}
+
+#ifndef HAVE_INET_PTON
+
+static int
+inet_pton(int af, const char *hostname, void *pton)
+{
+ struct in_addr in;
+
+#ifdef HAVE_INET_ATON
+ if (!inet_aton(hostname, &in))
+ return 0;
+#else
+ int d1, d2, d3, d4;
+ char ch;
+
+ if (sscanf(hostname, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
+ 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
+ 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
+ in.s_addr = htonl(
+ ((long) d1 << 24) | ((long) d2 << 16) |
+ ((long) d3 << 8) | ((long) d4 << 0));
+ }
+ else {
+ return 0;
+ }
+#endif
+ memcpy(pton, &in, sizeof(in));
+ return 1;
+}
+#endif
+
+int
+getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res)
+{
+ struct addrinfo sentinel;
+ struct addrinfo *top = NULL;
+ struct addrinfo *cur;
+ int i, error = 0;
+ char pton[PTON_MAX];
+ struct addrinfo ai;
+ struct addrinfo *pai;
+ u_short port;
+
+#ifdef FAITH
+ static int firsttime = 1;
+
+ if (firsttime) {
+ /* translator hack */
+ {
+ char *q = getenv("GAI");
+ if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
+ translate = YES;
+ }
+ firsttime = 0;
+ }
+#endif
+
+ /* initialize file static vars */
+ sentinel.ai_next = NULL;
+ cur = &sentinel;
+ pai = &ai;
+ pai->ai_flags = 0;
+ pai->ai_family = PF_UNSPEC;
+ pai->ai_socktype = ANY;
+ pai->ai_protocol = ANY;
+ pai->ai_addrlen = 0;
+ pai->ai_canonname = NULL;
+ pai->ai_addr = NULL;
+ pai->ai_next = NULL;
+ port = ANY;
+
+ if (hostname == NULL && servname == NULL)
+ return EAI_NONAME;
+ if (hints) {
+ /* error check for hints */
+ if (hints->ai_addrlen || hints->ai_canonname ||
+ hints->ai_addr || hints->ai_next)
+ ERR(EAI_BADHINTS); /* xxx */
+ if (hints->ai_flags & ~AI_MASK)
+ ERR(EAI_BADFLAGS);
+ switch (hints->ai_family) {
+ case PF_UNSPEC:
+ case PF_INET:
+#ifdef INET6
+ case PF_INET6:
+#endif
+ break;
+ default:
+ ERR(EAI_FAMILY);
+ }
+ memcpy(pai, hints, sizeof(*pai));
+ switch (pai->ai_socktype) {
+ case ANY:
+ switch (pai->ai_protocol) {
+ case ANY:
+ break;
+ case IPPROTO_UDP:
+ pai->ai_socktype = SOCK_DGRAM;
+ break;
+ case IPPROTO_TCP:
+ pai->ai_socktype = SOCK_STREAM;
+ break;
+ default:
+#if defined(SOCK_RAW)
+ pai->ai_socktype = SOCK_RAW;
+#endif
+ break;
+ }
+ break;
+#if defined(SOCK_RAW)
+ case SOCK_RAW:
+ break;
+#endif
+ case SOCK_DGRAM:
+ if (pai->ai_protocol != IPPROTO_UDP &&
+ pai->ai_protocol != ANY)
+ ERR(EAI_BADHINTS); /*xxx*/
+ pai->ai_protocol = IPPROTO_UDP;
+ break;
+ case SOCK_STREAM:
+ if (pai->ai_protocol != IPPROTO_TCP &&
+ pai->ai_protocol != ANY)
+ ERR(EAI_BADHINTS); /*xxx*/
+ pai->ai_protocol = IPPROTO_TCP;
+ break;
+ default:
+ ERR(EAI_SOCKTYPE);
+ break;
+ }
+ }
+
+ /*
+ * service port
+ */
+ if (servname) {
+ if (str_isnumber(servname)) {
+ if (pai->ai_socktype == ANY) {
+ /* caller accept *ANY* socktype */
+ pai->ai_socktype = SOCK_DGRAM;
+ pai->ai_protocol = IPPROTO_UDP;
+ }
+ port = htons((unsigned short)atoi(servname));
+ } else if (pai->ai_flags & AI_NUMERICSERV) {
+ ERR(EAI_NONAME);
+ } else {
+ struct servent *sp;
+ const char *proto;
+
+ proto = NULL;
+ switch (pai->ai_socktype) {
+ case ANY:
+ proto = NULL;
+ break;
+ case SOCK_DGRAM:
+ proto = "udp";
+ break;
+ case SOCK_STREAM:
+ proto = "tcp";
+ break;
+ default:
+ fprintf(stderr, "panic!\n");
+ break;
+ }
+ if ((sp = getservbyname((char*)servname, proto)) == NULL)
+ ERR(EAI_SERVICE);
+ port = sp->s_port;
+ if (pai->ai_socktype == ANY)
+ if (strcmp(sp->s_proto, "udp") == 0) {
+ pai->ai_socktype = SOCK_DGRAM;
+ pai->ai_protocol = IPPROTO_UDP;
+ } else if (strcmp(sp->s_proto, "tcp") == 0) {
+ pai->ai_socktype = SOCK_STREAM;
+ pai->ai_protocol = IPPROTO_TCP;
+ } else
+ ERR(EAI_PROTOCOL); /*xxx*/
+ }
+ }
+
+ /*
+ * hostname == NULL.
+ * passive socket -> anyaddr (0.0.0.0 or ::)
+ * non-passive socket -> localhost (127.0.0.1 or ::1)
+ */
+ if (hostname == NULL) {
+ const struct afd *afd;
+ int s;
+
+ for (afd = &afdl[0]; afd->a_af; afd++) {
+ if (!(pai->ai_family == PF_UNSPEC
+ || pai->ai_family == afd->a_af)) {
+ continue;
+ }
+
+ /*
+ * filter out AFs that are not supported by the kernel
+ * XXX errno?
+ */
+ s = socket(afd->a_af, SOCK_DGRAM, 0);
+ if (s < 0)
+ continue;
+#if defined(__BEOS__)
+ closesocket(s);
+#else
+ close(s);
+#endif
+
+ if (pai->ai_flags & AI_PASSIVE) {
+ GET_AI(cur->ai_next, afd, afd->a_addrany, port);
+ /* xxx meaningless?
+ * GET_CANONNAME(cur->ai_next, "anyaddr");
+ */
+ } else {
+ GET_AI(cur->ai_next, afd, afd->a_loopback,
+ port);
+ /* xxx meaningless?
+ * GET_CANONNAME(cur->ai_next, "localhost");
+ */
+ }
+ cur = cur->ai_next;
+ }
+ top = sentinel.ai_next;
+ if (top)
+ goto good;
+ else
+ ERR(EAI_FAMILY);
+ }
+
+ /* hostname as numeric name */
+ for (i = 0; afdl[i].a_af; i++) {
+ if (inet_pton(afdl[i].a_af, hostname, pton)) {
+ u_long v4a;
+#ifdef INET6
+ u_char pfx;
+#endif
+
+ switch (afdl[i].a_af) {
+ case AF_INET:
+ v4a = ((struct in_addr *)pton)->s_addr;
+ if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
+ pai->ai_flags &= ~AI_CANONNAME;
+ v4a >>= IN_CLASSA_NSHIFT;
+ if (v4a == 0 || v4a == IN_LOOPBACKNET)
+ pai->ai_flags &= ~AI_CANONNAME;
+ break;
+#ifdef INET6
+ case AF_INET6:
+ pfx = ((struct in6_addr *)pton)->s6_addr[0];
+ if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
+ pai->ai_flags &= ~AI_CANONNAME;
+ break;
+#endif
+ }
+
+ if (pai->ai_family == afdl[i].a_af ||
+ pai->ai_family == PF_UNSPEC) {
+ if (! (pai->ai_flags & AI_CANONNAME)) {
+ GET_AI(top, &afdl[i], pton, port);
+ goto good;
+ }
+ /*
+ * if AI_CANONNAME and if reverse lookup
+ * fail, return ai anyway to pacify
+ * calling application.
+ *
+ * XXX getaddrinfo() is a name->address
+ * translation function, and it looks strange
+ * that we do addr->name translation here.
+ */
+ get_name(pton, &afdl[i], &top, pton, pai, port);
+ goto good;
+ } else
+ ERR(EAI_FAMILY); /*xxx*/
+ }
+ }
+
+ if (pai->ai_flags & AI_NUMERICHOST)
+ ERR(EAI_NONAME);
+
+ /* hostname as alphabetical name */
+ error = get_addr(hostname, pai->ai_family, &top, pai, port);
+ if (error == 0) {
+ if (top) {
+ good:
+ *res = top;
+ return SUCCESS;
+ } else
+ error = EAI_FAIL;
+ }
+ free:
+ if (top)
+ freeaddrinfo(top);
+ bad:
+ *res = NULL;
+ return error;
+}
+
+static int
+get_name(const char *addr, const struct afd *afd, struct addrinfo **res, char *numaddr, struct addrinfo *pai, int port0)
+{
+ u_short port = port0 & 0xffff;
+ struct hostent *hp;
+ struct addrinfo *cur;
+ int error = 0;
+#ifdef INET6
+ int h_error;
+#endif
+
+#ifdef INET6
+ hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
+#else
+ hp = gethostbyaddr((char*)addr, afd->a_addrlen, AF_INET);
+#endif
+ if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
+ GET_AI(cur, afd, hp->h_addr_list[0], port);
+ GET_CANONNAME(cur, hp->h_name);
+ } else
+ GET_AI(cur, afd, numaddr, port);
+
+#ifdef INET6
+ if (hp)
+ freehostent(hp);
+#endif
+ *res = cur;
+ return SUCCESS;
+ free:
+ if (cur)
+ freeaddrinfo(cur);
+#ifdef INET6
+ if (hp)
+ freehostent(hp);
+#endif
+ /* bad: */
+ *res = NULL;
+ return error;
+}
+
+static int
+get_addr(const char *hostname, int af, struct addrinfo **res, struct addrinfo *pai, int port0)
+{
+ u_short port = port0 & 0xffff;
+ struct addrinfo sentinel;
+ struct hostent *hp;
+ struct addrinfo *top, *cur;
+ const struct afd *afd;
+ int i, error = 0, h_error;
+ char *ap;
+
+ top = NULL;
+ sentinel.ai_next = NULL;
+ cur = &sentinel;
+#ifdef INET6
+ if (af == AF_UNSPEC) {
+ hp = getipnodebyname(hostname, AF_INET6,
+ AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
+ } else
+ hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
+#else
+ if (strlen(hostname) >= NI_MAXHOST) ERR(EAI_NODATA);
+ hp = gethostbyname((char*)hostname);
+ h_error = h_errno;
+#endif
+ if (hp == NULL) {
+ switch (h_error) {
+ case HOST_NOT_FOUND:
+ case NO_DATA:
+ error = EAI_NODATA;
+ break;
+ case TRY_AGAIN:
+ error = EAI_AGAIN;
+ break;
+ case NO_RECOVERY:
+ default:
+ error = EAI_FAIL;
+ break;
+ }
+ goto bad;
+ }
+
+ if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
+ (hp->h_addr_list[0] == NULL))
+ ERR(EAI_FAIL);
+
+ for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
+ switch (af) {
+#ifdef INET6
+ case AF_INET6:
+ afd = &afdl[N_INET6];
+ break;
+#endif
+#ifndef INET6
+ default: /* AF_UNSPEC */
+#endif
+ case AF_INET:
+ afd = &afdl[N_INET];
+ break;
+#ifdef INET6
+ default: /* AF_UNSPEC */
+ if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
+ ap += sizeof(struct in6_addr) -
+ sizeof(struct in_addr);
+ afd = &afdl[N_INET];
+ } else
+ afd = &afdl[N_INET6];
+ break;
+#endif
+ }
+#ifdef FAITH
+ if (translate && afd->a_af == AF_INET) {
+ struct in6_addr *in6;
+
+ GET_AI(cur->ai_next, &afdl[N_INET6], ap, port);
+ in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
+ memcpy(&in6->s6_addr, &faith_prefix,
+ sizeof(struct in6_addr) - sizeof(struct in_addr));
+ memcpy(&in6->s6_addr + sizeof(struct in_addr), ap,
+ sizeof(struct in_addr));
+ } else
+#endif /* FAITH */
+ GET_AI(cur->ai_next, afd, ap, port);
+ if (cur == &sentinel) {
+ top = cur->ai_next;
+ GET_CANONNAME(top, hp->h_name);
+ }
+ cur = cur->ai_next;
+ }
+#ifdef INET6
+ freehostent(hp);
+#endif
+ *res = top;
+ return SUCCESS;
+ free:
+ if (top)
+ freeaddrinfo(top);
+#ifdef INET6
+ if (hp)
+ freehostent(hp);
+#endif
+ bad:
+ *res = NULL;
+ return error;
+}
diff --git a/jni/ruby/ext/socket/getnameinfo.c b/jni/ruby/ext/socket/getnameinfo.c
new file mode 100644
index 0000000..4da9680
--- /dev/null
+++ b/jni/ruby/ext/socket/getnameinfo.c
@@ -0,0 +1,261 @@
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Issues to be discussed:
+ * - Thread safe-ness must be checked
+ * - Return values. There seems to be no standard for return value (RFC2133)
+ * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
+ */
+
+#include "ruby/config.h"
+#ifdef RUBY_EXTCONF_H
+#include RUBY_EXTCONF_H
+#endif
+#include <stdio.h>
+#include <sys/types.h>
+#ifndef _WIN32
+#if defined(__BEOS__) && !defined(__HAIKU__) && !defined(BONE)
+# include <net/socket.h>
+#else
+# include <sys/socket.h>
+#endif
+#include <netinet/in.h>
+#if defined(HAVE_ARPA_INET_H)
+#include <arpa/inet.h>
+#endif
+#if defined(HAVE_ARPA_NAMESER_H)
+#include <arpa/nameser.h>
+#endif
+#include <netdb.h>
+#if defined(HAVE_RESOLV_H)
+#include <resolv.h>
+#endif
+#endif
+#ifdef _WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#define snprintf _snprintf
+#endif
+
+#include <string.h>
+#include <stddef.h>
+
+#ifdef SOCKS5
+#include <socks.h>
+#endif
+
+#ifndef HAVE_TYPE_SOCKLEN_T
+typedef int socklen_t;
+#endif
+
+#include "addrinfo.h"
+#include "sockport.h"
+
+#define SUCCESS 0
+#define ANY 0
+#define YES 1
+#define NO 0
+
+struct sockinet {
+ u_char si_len;
+ u_char si_family;
+ u_short si_port;
+};
+
+static struct afd {
+ int a_af;
+ int a_addrlen;
+ int a_socklen;
+ int a_off;
+} afdl [] = {
+#ifdef INET6
+#define N_INET6 0
+ {PF_INET6, sizeof(struct in6_addr),
+ sizeof(struct sockaddr_in6),
+ offsetof(struct sockaddr_in6, sin6_addr)},
+#define N_INET 1
+#else
+#define N_INET 0
+#endif
+ {PF_INET, sizeof(struct in_addr),
+ sizeof(struct sockaddr_in),
+ offsetof(struct sockaddr_in, sin_addr)},
+ {0, 0, 0, 0},
+};
+
+#define ENI_NOSOCKET 0
+#define ENI_NOSERVNAME 1
+#define ENI_NOHOSTNAME 2
+#define ENI_MEMORY 3
+#define ENI_SYSTEM 4
+#define ENI_FAMILY 5
+#define ENI_SALEN 6
+
+#ifndef HAVE_INET_NTOP
+static const char *
+inet_ntop(int af, const void *addr, char *numaddr, size_t numaddr_len)
+{
+#ifdef HAVE_INET_NTOA
+ struct in_addr in;
+ memcpy(&in.s_addr, addr, sizeof(in.s_addr));
+ snprintf(numaddr, numaddr_len, "%s", inet_ntoa(in));
+#else
+ unsigned long x = ntohl(*(unsigned long*)addr);
+ snprintf(numaddr, numaddr_len, "%d.%d.%d.%d",
+ (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
+ (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
+#endif
+ return numaddr;
+}
+#endif
+
+int
+getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
+{
+ struct afd *afd;
+ struct hostent *hp;
+ u_short port;
+ int family, len, i;
+ char *addr, *p;
+ u_long v4a;
+#ifdef INET6
+ u_char pfx;
+#endif
+ int h_error;
+ char numserv[512];
+ char numaddr[512];
+
+ if (sa == NULL)
+ return ENI_NOSOCKET;
+
+ if (!VALIDATE_SOCKLEN(sa, salen)) return ENI_SALEN;
+ len = salen;
+
+ family = sa->sa_family;
+ for (i = 0; afdl[i].a_af; i++)
+ if (afdl[i].a_af == family) {
+ afd = &afdl[i];
+ goto found;
+ }
+ return ENI_FAMILY;
+
+ found:
+ if (len != afd->a_socklen) return ENI_SALEN;
+
+ port = ((struct sockinet *)sa)->si_port; /* network byte order */
+ addr = (char *)sa + afd->a_off;
+
+ if (serv == NULL || servlen == 0) {
+ /* what we should do? */
+ } else if (flags & NI_NUMERICSERV) {
+ snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
+ if (strlen(numserv) + 1 > servlen)
+ return ENI_MEMORY;
+ strcpy(serv, numserv);
+ } else {
+#if defined(HAVE_GETSERVBYPORT)
+ struct servent *sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
+ if (sp) {
+ if (strlen(sp->s_name) + 1 > servlen)
+ return ENI_MEMORY;
+ strcpy(serv, sp->s_name);
+ } else
+ return ENI_NOSERVNAME;
+#else
+ return ENI_NOSERVNAME;
+#endif
+ }
+
+ switch (sa->sa_family) {
+ case AF_INET:
+ v4a = ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
+ if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
+ flags |= NI_NUMERICHOST;
+ v4a >>= IN_CLASSA_NSHIFT;
+ if (v4a == 0)
+ flags |= NI_NUMERICHOST;
+ break;
+#ifdef INET6
+ case AF_INET6:
+#ifdef HAVE_ADDR8
+ pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr8[0];
+#else
+ pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
+#endif
+ if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
+ flags |= NI_NUMERICHOST;
+ break;
+#endif
+ }
+ if (host == NULL || hostlen == 0) {
+ /* what should we do? */
+ } else if (flags & NI_NUMERICHOST) {
+ if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
+ == NULL)
+ return ENI_SYSTEM;
+ if (strlen(numaddr) > hostlen)
+ return ENI_MEMORY;
+ strcpy(host, numaddr);
+ } else {
+#ifdef INET6
+ hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
+#else
+ hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
+ h_error = h_errno;
+#endif
+
+ if (hp) {
+ if (flags & NI_NOFQDN) {
+ p = strchr(hp->h_name, '.');
+ if (p) *p = '\0';
+ }
+ if (strlen(hp->h_name) + 1 > hostlen) {
+#ifdef INET6
+ freehostent(hp);
+#endif
+ return ENI_MEMORY;
+ }
+ strcpy(host, hp->h_name);
+#ifdef INET6
+ freehostent(hp);
+#endif
+ } else {
+ if (flags & NI_NAMEREQD)
+ return ENI_NOHOSTNAME;
+ if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
+ == NULL)
+ return ENI_NOHOSTNAME;
+ if (strlen(numaddr) > hostlen)
+ return ENI_MEMORY;
+ strcpy(host, numaddr);
+ }
+ }
+ return SUCCESS;
+}
diff --git a/jni/ruby/ext/socket/ifaddr.c b/jni/ruby/ext/socket/ifaddr.c
new file mode 100644
index 0000000..15395d7
--- /dev/null
+++ b/jni/ruby/ext/socket/ifaddr.c
@@ -0,0 +1,459 @@
+#include "rubysocket.h"
+
+#ifdef HAVE_GETIFADDRS
+
+/*
+ * ifa_flags is usually unsigned int.
+ * However it is uint64_t on SunOS 5.11 (OpenIndiana).
+ */
+#ifdef HAVE_LONG_LONG
+typedef unsigned LONG_LONG ifa_flags_t;
+#define PRIxIFAFLAGS PRI_LL_PREFIX"x"
+#define IFAFLAGS2NUM(flags) ULL2NUM(flags)
+#else
+typedef unsigned int ifa_flags_t;
+#define PRIxIFAFLAGS "x"
+#define IFAFLAGS2NUM(flags) UINT2NUM(flags)
+#endif
+
+VALUE rb_cSockIfaddr;
+
+typedef struct rb_ifaddr_tag rb_ifaddr_t;
+typedef struct rb_ifaddr_root_tag rb_ifaddr_root_t;
+
+struct rb_ifaddr_tag {
+ int ord;
+ struct ifaddrs *ifaddr;
+ rb_ifaddr_root_t *root;
+};
+
+struct rb_ifaddr_root_tag {
+ int refcount;
+ int numifaddrs;
+ rb_ifaddr_t ary[1];
+};
+
+static rb_ifaddr_root_t *
+get_root(const rb_ifaddr_t *ifaddr)
+{
+ return (rb_ifaddr_root_t *)((char *)&ifaddr[-ifaddr->ord] -
+ offsetof(rb_ifaddr_root_t, ary));
+}
+
+static void
+ifaddr_mark(void *ptr)
+{
+}
+
+static void
+ifaddr_free(void *ptr)
+{
+ rb_ifaddr_t *ifaddr = ptr;
+ rb_ifaddr_root_t *root = get_root(ifaddr);
+ root->refcount--;
+ if (root->refcount == 0) {
+ freeifaddrs(root->ary[0].ifaddr);
+ xfree(root);
+ }
+}
+
+static size_t
+ifaddr_memsize(const void *ptr)
+{
+ const rb_ifaddr_t *ifaddr;
+ const rb_ifaddr_root_t *root;
+ if (ptr == NULL)
+ return 0;
+ ifaddr = ptr;
+ root = get_root(ifaddr);
+ return sizeof(rb_ifaddr_root_t) + (root->numifaddrs - 1) * sizeof(rb_ifaddr_t);
+}
+
+static const rb_data_type_t ifaddr_type = {
+ "socket/ifaddr",
+ {ifaddr_mark, ifaddr_free, ifaddr_memsize,},
+};
+
+static inline rb_ifaddr_t *
+check_ifaddr(VALUE self)
+{
+ return rb_check_typeddata(self, &ifaddr_type);
+}
+
+static rb_ifaddr_t *
+get_ifaddr(VALUE self)
+{
+ rb_ifaddr_t *rifaddr = check_ifaddr(self);
+
+ if (!rifaddr) {
+ rb_raise(rb_eTypeError, "uninitialized ifaddr");
+ }
+ return rifaddr;
+}
+
+static VALUE
+rsock_getifaddrs(void)
+{
+ int ret;
+ int numifaddrs, i;
+ struct ifaddrs *ifaddrs, *ifa;
+ rb_ifaddr_root_t *root;
+ VALUE result;
+
+ ret = getifaddrs(&ifaddrs);
+ if (ret == -1)
+ rb_sys_fail("getifaddrs");
+
+ if (!ifaddrs) {
+ return rb_ary_new();
+ }
+
+ numifaddrs = 0;
+ for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next)
+ numifaddrs++;
+
+ root = xmalloc(sizeof(rb_ifaddr_root_t) + (numifaddrs-1) * sizeof(rb_ifaddr_t));
+ root->refcount = root->numifaddrs = numifaddrs;
+
+ ifa = ifaddrs;
+ for (i = 0; i < numifaddrs; i++) {
+ root->ary[i].ord = i;
+ root->ary[i].ifaddr = ifa;
+ root->ary[i].root = root;
+ ifa = ifa->ifa_next;
+ }
+
+ result = rb_ary_new2(numifaddrs);
+ for (i = 0; i < numifaddrs; i++) {
+ rb_ary_push(result, TypedData_Wrap_Struct(rb_cSockIfaddr, &ifaddr_type, &root->ary[i]));
+ }
+
+ return result;
+}
+
+/*
+ * call-seq:
+ * ifaddr.name => string
+ *
+ * Returns the interface name of _ifaddr_.
+ */
+
+static VALUE
+ifaddr_name(VALUE self)
+{
+ rb_ifaddr_t *rifaddr = get_ifaddr(self);
+ struct ifaddrs *ifa = rifaddr->ifaddr;
+ return rb_str_new_cstr(ifa->ifa_name);
+}
+
+#ifdef HAVE_IF_NAMETOINDEX
+/*
+ * call-seq:
+ * ifaddr.ifindex => integer
+ *
+ * Returns the interface index of _ifaddr_.
+ */
+
+static VALUE
+ifaddr_ifindex(VALUE self)
+{
+ rb_ifaddr_t *rifaddr = get_ifaddr(self);
+ struct ifaddrs *ifa = rifaddr->ifaddr;
+ unsigned int ifindex = if_nametoindex(ifa->ifa_name);
+ if (ifindex == 0) {
+ rb_raise(rb_eArgError, "invalid interface name: %s", ifa->ifa_name);
+ }
+ return UINT2NUM(ifindex);
+}
+#else
+#define ifaddr_ifindex rb_f_notimplement
+#endif
+
+/*
+ * call-seq:
+ * ifaddr.flags => integer
+ *
+ * Returns the flags of _ifaddr_.
+ */
+
+static VALUE
+ifaddr_flags(VALUE self)
+{
+ rb_ifaddr_t *rifaddr = get_ifaddr(self);
+ struct ifaddrs *ifa = rifaddr->ifaddr;
+ return IFAFLAGS2NUM(ifa->ifa_flags);
+}
+
+/*
+ * call-seq:
+ * ifaddr.addr => addrinfo
+ *
+ * Returns the address of _ifaddr_.
+ * nil is returned if address is not available in _ifaddr_.
+ */
+
+static VALUE
+ifaddr_addr(VALUE self)
+{
+ rb_ifaddr_t *rifaddr = get_ifaddr(self);
+ struct ifaddrs *ifa = rifaddr->ifaddr;
+ if (ifa->ifa_addr)
+ return rsock_sockaddr_obj(ifa->ifa_addr, rsock_sockaddr_len(ifa->ifa_addr));
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * ifaddr.netmask => addrinfo
+ *
+ * Returns the netmask address of _ifaddr_.
+ * nil is returned if netmask is not available in _ifaddr_.
+ */
+
+static VALUE
+ifaddr_netmask(VALUE self)
+{
+ rb_ifaddr_t *rifaddr = get_ifaddr(self);
+ struct ifaddrs *ifa = rifaddr->ifaddr;
+ if (ifa->ifa_netmask)
+ return rsock_sockaddr_obj(ifa->ifa_netmask, rsock_sockaddr_len(ifa->ifa_netmask));
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * ifaddr.broadaddr => addrinfo
+ *
+ * Returns the broadcast address of _ifaddr_.
+ * nil is returned if the flags doesn't have IFF_BROADCAST.
+ */
+
+static VALUE
+ifaddr_broadaddr(VALUE self)
+{
+ rb_ifaddr_t *rifaddr = get_ifaddr(self);
+ struct ifaddrs *ifa = rifaddr->ifaddr;
+ if ((ifa->ifa_flags & IFF_BROADCAST) && ifa->ifa_broadaddr)
+ return rsock_sockaddr_obj(ifa->ifa_broadaddr, rsock_sockaddr_len(ifa->ifa_broadaddr));
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * ifaddr.dstaddr => addrinfo
+ *
+ * Returns the destination address of _ifaddr_.
+ * nil is returned if the flags doesn't have IFF_POINTOPOINT.
+ */
+
+static VALUE
+ifaddr_dstaddr(VALUE self)
+{
+ rb_ifaddr_t *rifaddr = get_ifaddr(self);
+ struct ifaddrs *ifa = rifaddr->ifaddr;
+ if ((ifa->ifa_flags & IFF_POINTOPOINT) && ifa->ifa_dstaddr)
+ return rsock_sockaddr_obj(ifa->ifa_dstaddr, rsock_sockaddr_len(ifa->ifa_dstaddr));
+ return Qnil;
+}
+
+static void
+ifaddr_inspect_flags(ifa_flags_t flags, VALUE result)
+{
+ const char *sep = " ";
+#define INSPECT_BIT(bit, name) \
+ if (flags & (bit)) { rb_str_catf(result, "%s" name, sep); flags &= ~(ifa_flags_t)(bit); sep = ","; }
+#ifdef IFF_UP
+ INSPECT_BIT(IFF_UP, "UP")
+#endif
+#ifdef IFF_BROADCAST
+ INSPECT_BIT(IFF_BROADCAST, "BROADCAST")
+#endif
+#ifdef IFF_DEBUG
+ INSPECT_BIT(IFF_DEBUG, "DEBUG")
+#endif
+#ifdef IFF_LOOPBACK
+ INSPECT_BIT(IFF_LOOPBACK, "LOOPBACK")
+#endif
+#ifdef IFF_POINTOPOINT
+ INSPECT_BIT(IFF_POINTOPOINT, "POINTOPOINT")
+#endif
+#ifdef IFF_RUNNING
+ INSPECT_BIT(IFF_RUNNING, "RUNNING")
+#endif
+#ifdef IFF_NOARP
+ INSPECT_BIT(IFF_NOARP, "NOARP")
+#endif
+#ifdef IFF_PROMISC
+ INSPECT_BIT(IFF_PROMISC, "PROMISC")
+#endif
+#ifdef IFF_NOTRAILERS
+ INSPECT_BIT(IFF_NOTRAILERS, "NOTRAILERS")
+#endif
+#ifdef IFF_ALLMULTI
+ INSPECT_BIT(IFF_ALLMULTI, "ALLMULTI")
+#endif
+#ifdef IFF_SIMPLEX
+ INSPECT_BIT(IFF_SIMPLEX, "SIMPLEX")
+#endif
+#ifdef IFF_MASTER
+ INSPECT_BIT(IFF_MASTER, "MASTER")
+#endif
+#ifdef IFF_SLAVE
+ INSPECT_BIT(IFF_SLAVE, "SLAVE")
+#endif
+#ifdef IFF_MULTICAST
+ INSPECT_BIT(IFF_MULTICAST, "MULTICAST")
+#endif
+#ifdef IFF_PORTSEL
+ INSPECT_BIT(IFF_PORTSEL, "PORTSEL")
+#endif
+#ifdef IFF_AUTOMEDIA
+ INSPECT_BIT(IFF_AUTOMEDIA, "AUTOMEDIA")
+#endif
+#ifdef IFF_DYNAMIC
+ INSPECT_BIT(IFF_DYNAMIC, "DYNAMIC")
+#endif
+#ifdef IFF_LOWER_UP
+ INSPECT_BIT(IFF_LOWER_UP, "LOWER_UP")
+#endif
+#ifdef IFF_DORMANT
+ INSPECT_BIT(IFF_DORMANT, "DORMANT")
+#endif
+#ifdef IFF_ECHO
+ INSPECT_BIT(IFF_ECHO, "ECHO")
+#endif
+#undef INSPECT_BIT
+ if (flags) {
+ rb_str_catf(result, "%s%#"PRIxIFAFLAGS, sep, flags);
+ }
+}
+
+/*
+ * call-seq:
+ * ifaddr.inspect => string
+ *
+ * Returns a string to show contents of _ifaddr_.
+ */
+
+static VALUE
+ifaddr_inspect(VALUE self)
+{
+ rb_ifaddr_t *rifaddr = get_ifaddr(self);
+ struct ifaddrs *ifa;
+ VALUE result;
+
+ ifa = rifaddr->ifaddr;
+
+ result = rb_str_new_cstr("#<");
+
+ rb_str_append(result, rb_class_name(CLASS_OF(self)));
+ rb_str_cat2(result, " ");
+ rb_str_cat2(result, ifa->ifa_name);
+
+ if (ifa->ifa_flags)
+ ifaddr_inspect_flags(ifa->ifa_flags, result);
+
+ if (ifa->ifa_addr) {
+ rb_str_cat2(result, " ");
+ rsock_inspect_sockaddr(ifa->ifa_addr,
+ rsock_sockaddr_len(ifa->ifa_addr),
+ result);
+ }
+ if (ifa->ifa_netmask) {
+ rb_str_cat2(result, " netmask=");
+ rsock_inspect_sockaddr(ifa->ifa_netmask,
+ rsock_sockaddr_len(ifa->ifa_netmask),
+ result);
+ }
+
+ if ((ifa->ifa_flags & IFF_BROADCAST) && ifa->ifa_broadaddr) {
+ rb_str_cat2(result, " broadcast=");
+ rsock_inspect_sockaddr(ifa->ifa_broadaddr,
+ rsock_sockaddr_len(ifa->ifa_broadaddr),
+ result);
+ }
+
+ if ((ifa->ifa_flags & IFF_POINTOPOINT) && ifa->ifa_dstaddr) {
+ rb_str_cat2(result, " dstaddr=");
+ rsock_inspect_sockaddr(ifa->ifa_dstaddr,
+ rsock_sockaddr_len(ifa->ifa_dstaddr),
+ result);
+ }
+
+ rb_str_cat2(result, ">");
+ return result;
+}
+#endif
+
+#ifdef HAVE_GETIFADDRS
+/*
+ * call-seq:
+ * Socket.getifaddrs => [ifaddr1, ...]
+ *
+ * Returns an array of interface addresses.
+ * An element of the array is an instance of Socket::Ifaddr.
+ *
+ * This method can be used to find multicast-enabled interfaces:
+ *
+ * pp Socket.getifaddrs.reject {|ifaddr|
+ * !ifaddr.addr.ip? || (ifaddr.flags & Socket::IFF_MULTICAST == 0)
+ * }.map {|ifaddr| [ifaddr.name, ifaddr.ifindex, ifaddr.addr] }
+ * #=> [["eth0", 2, #<Addrinfo: 221.186.184.67>],
+ * # ["eth0", 2, #<Addrinfo: fe80::216:3eff:fe95:88bb%eth0>]]
+ *
+ * Example result on GNU/Linux:
+ * pp Socket.getifaddrs
+ * #=> [#<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 PACKET[protocol=0 lo hatype=772 HOST hwaddr=00:00:00:00:00:00]>,
+ * # #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=00:16:3e:95:88:bb] broadcast=PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=ff:ff:ff:ff:ff:ff]>,
+ * # #<Socket::Ifaddr sit0 NOARP PACKET[protocol=0 sit0 hatype=776 HOST hwaddr=00:00:00:00]>,
+ * # #<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 127.0.0.1 netmask=255.0.0.0>,
+ * # #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 221.186.184.67 netmask=255.255.255.240 broadcast=221.186.184.79>,
+ * # #<Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 ::1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>,
+ * # #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 fe80::216:3eff:fe95:88bb%eth0 netmask=ffff:ffff:ffff:ffff::>]
+ *
+ * Example result on FreeBSD:
+ * pp Socket.getifaddrs
+ * #=> [#<Socket::Ifaddr usbus0 UP,0x10000 LINK[usbus0]>,
+ * # #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 LINK[re0 3a:d0:40:9a:fe:e8]>,
+ * # #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 10.250.10.18 netmask=255.255.255.? (7 bytes for 16 bytes sockaddr_in) broadcast=10.250.10.255>,
+ * # #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 fe80:2::38d0:40ff:fe9a:fee8 netmask=ffff:ffff:ffff:ffff::>,
+ * # #<Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 2001:2e8:408:10::12 netmask=UNSPEC>,
+ * # #<Socket::Ifaddr plip0 POINTOPOINT,MULTICAST,0x800 LINK[plip0]>,
+ * # #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST LINK[lo0]>,
+ * # #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST ::1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>,
+ * # #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST fe80:4::1 netmask=ffff:ffff:ffff:ffff::>,
+ * # #<Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST 127.0.0.1 netmask=255.?.?.? (5 bytes for 16 bytes sockaddr_in)>]
+ *
+ */
+
+static VALUE
+socket_s_getifaddrs(VALUE self)
+{
+ return rsock_getifaddrs();
+}
+#else
+#define socket_s_getifaddrs rb_f_notimplement
+#endif
+
+void
+rsock_init_sockifaddr(void)
+{
+#ifdef HAVE_GETIFADDRS
+ /*
+ * Document-class: Socket::Ifaddr
+ *
+ * Socket::Ifaddr represents a result of getifaddrs() function.
+ */
+ rb_cSockIfaddr = rb_define_class_under(rb_cSocket, "Ifaddr", rb_cData);
+ rb_define_method(rb_cSockIfaddr, "inspect", ifaddr_inspect, 0);
+ rb_define_method(rb_cSockIfaddr, "name", ifaddr_name, 0);
+ rb_define_method(rb_cSockIfaddr, "ifindex", ifaddr_ifindex, 0);
+ rb_define_method(rb_cSockIfaddr, "flags", ifaddr_flags, 0);
+ rb_define_method(rb_cSockIfaddr, "addr", ifaddr_addr, 0);
+ rb_define_method(rb_cSockIfaddr, "netmask", ifaddr_netmask, 0);
+ rb_define_method(rb_cSockIfaddr, "broadaddr", ifaddr_broadaddr, 0);
+ rb_define_method(rb_cSockIfaddr, "dstaddr", ifaddr_dstaddr, 0);
+#endif
+
+ rb_define_singleton_method(rb_cSocket, "getifaddrs", socket_s_getifaddrs, 0);
+}
diff --git a/jni/ruby/ext/socket/init.c b/jni/ruby/ext/socket/init.c
new file mode 100644
index 0000000..c3f3396
--- /dev/null
+++ b/jni/ruby/ext/socket/init.c
@@ -0,0 +1,615 @@
+/************************************************
+
+ init.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+VALUE rb_cBasicSocket;
+VALUE rb_cIPSocket;
+VALUE rb_cTCPSocket;
+VALUE rb_cTCPServer;
+VALUE rb_cUDPSocket;
+#ifdef AF_UNIX
+VALUE rb_cUNIXSocket;
+VALUE rb_cUNIXServer;
+#endif
+VALUE rb_cSocket;
+VALUE rb_cAddrinfo;
+
+VALUE rb_eSocket;
+
+#ifdef SOCKS
+VALUE rb_cSOCKSSocket;
+#endif
+
+int rsock_do_not_reverse_lookup = 1;
+
+void
+rsock_raise_socket_error(const char *reason, int error)
+{
+#ifdef EAI_SYSTEM
+ if (error == EAI_SYSTEM) rb_sys_fail(reason);
+#endif
+ rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error));
+}
+
+VALUE
+rsock_init_sock(VALUE sock, int fd)
+{
+ rb_io_t *fp;
+#ifndef _WIN32
+ struct stat sbuf;
+
+ if (fstat(fd, &sbuf) < 0)
+ rb_sys_fail("fstat(2)");
+ rb_update_max_fd(fd);
+ if (!S_ISSOCK(sbuf.st_mode))
+ rb_raise(rb_eArgError, "not a socket file descriptor");
+#else
+ rb_update_max_fd(fd);
+ if (!rb_w32_is_socket(fd))
+ rb_raise(rb_eArgError, "not a socket file descriptor");
+#endif
+
+ MakeOpenFile(sock, fp);
+ fp->fd = fd;
+ fp->mode = FMODE_READWRITE|FMODE_DUPLEX;
+ rb_io_ascii8bit_binmode(sock);
+ if (rsock_do_not_reverse_lookup) {
+ fp->mode |= FMODE_NOREVLOOKUP;
+ }
+ rb_io_synchronized(fp);
+
+ return sock;
+}
+
+VALUE
+rsock_sendto_blocking(void *data)
+{
+ struct rsock_send_arg *arg = data;
+ VALUE mesg = arg->mesg;
+ return (VALUE)sendto(arg->fd, RSTRING_PTR(mesg), RSTRING_LEN(mesg),
+ arg->flags, arg->to, arg->tolen);
+}
+
+VALUE
+rsock_send_blocking(void *data)
+{
+ struct rsock_send_arg *arg = data;
+ VALUE mesg = arg->mesg;
+ return (VALUE)send(arg->fd, RSTRING_PTR(mesg), RSTRING_LEN(mesg),
+ arg->flags);
+}
+
+struct recvfrom_arg {
+ int fd, flags;
+ VALUE str;
+ socklen_t alen;
+ union_sockaddr buf;
+};
+
+static VALUE
+recvfrom_blocking(void *data)
+{
+ struct recvfrom_arg *arg = data;
+ socklen_t len0 = arg->alen;
+ ssize_t ret;
+ ret = recvfrom(arg->fd, RSTRING_PTR(arg->str), RSTRING_LEN(arg->str),
+ arg->flags, &arg->buf.addr, &arg->alen);
+ if (ret != -1 && len0 < arg->alen)
+ arg->alen = len0;
+ return (VALUE)ret;
+}
+
+VALUE
+rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
+{
+ rb_io_t *fptr;
+ VALUE str, klass;
+ struct recvfrom_arg arg;
+ VALUE len, flg;
+ long buflen;
+ long slen;
+
+ rb_scan_args(argc, argv, "11", &len, &flg);
+
+ if (flg == Qnil) arg.flags = 0;
+ else arg.flags = NUM2INT(flg);
+ buflen = NUM2INT(len);
+
+ GetOpenFile(sock, fptr);
+ if (rb_io_read_pending(fptr)) {
+ rb_raise(rb_eIOError, "recv for buffered IO");
+ }
+ arg.fd = fptr->fd;
+ arg.alen = (socklen_t)sizeof(arg.buf);
+
+ arg.str = str = rb_tainted_str_new(0, buflen);
+ klass = RBASIC(str)->klass;
+ rb_obj_hide(str);
+
+ while (rb_io_check_closed(fptr),
+ rsock_maybe_wait_fd(arg.fd),
+ (slen = BLOCKING_REGION_FD(recvfrom_blocking, &arg)) < 0) {
+ if (!rb_io_wait_readable(fptr->fd)) {
+ rb_sys_fail("recvfrom(2)");
+ }
+ if (RBASIC(str)->klass || RSTRING_LEN(str) != buflen) {
+ rb_raise(rb_eRuntimeError, "buffer string modified");
+ }
+ }
+
+ rb_obj_reveal(str, klass);
+ if (slen < RSTRING_LEN(str)) {
+ rb_str_set_len(str, slen);
+ }
+ rb_obj_taint(str);
+ switch (from) {
+ case RECV_RECV:
+ return str;
+ case RECV_IP:
+#if 0
+ if (arg.alen != sizeof(struct sockaddr_in)) {
+ rb_raise(rb_eTypeError, "sockaddr size differs - should not happen");
+ }
+#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(&arg.buf.addr, arg.alen, fptr->mode & FMODE_NOREVLOOKUP));
+ else
+ return rb_assoc_new(str, Qnil);
+
+#ifdef HAVE_SYS_UN_H
+ case RECV_UNIX:
+ return rb_assoc_new(str, rsock_unixaddr(&arg.buf.un, arg.alen));
+#endif
+ case RECV_SOCKET:
+ return rb_assoc_new(str, rsock_io_socket_addrinfo(sock, &arg.buf.addr, arg.alen));
+ default:
+ rb_bug("rsock_s_recvfrom called with bad value");
+ }
+}
+
+VALUE
+rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
+{
+ rb_io_t *fptr;
+ VALUE str;
+ union_sockaddr buf;
+ socklen_t alen = (socklen_t)sizeof buf;
+ VALUE len, flg;
+ long buflen;
+ long slen;
+ int fd, flags;
+ VALUE addr = Qnil;
+ socklen_t len0;
+
+ rb_scan_args(argc, argv, "11", &len, &flg);
+
+ if (flg == Qnil) flags = 0;
+ else flags = NUM2INT(flg);
+ buflen = NUM2INT(len);
+
+#ifdef MSG_DONTWAIT
+ /* MSG_DONTWAIT avoids the race condition between fcntl and recvfrom.
+ It is not portable, though. */
+ flags |= MSG_DONTWAIT;
+#endif
+
+ GetOpenFile(sock, fptr);
+ if (rb_io_read_pending(fptr)) {
+ rb_raise(rb_eIOError, "recvfrom for buffered IO");
+ }
+ fd = fptr->fd;
+
+ str = rb_tainted_str_new(0, buflen);
+
+ rb_io_check_closed(fptr);
+ rb_io_set_nonblock(fptr);
+ len0 = alen;
+ slen = recvfrom(fd, RSTRING_PTR(str), buflen, flags, &buf.addr, &alen);
+ if (slen != -1 && len0 < alen)
+ alen = len0;
+
+ if (slen < 0) {
+ switch (errno) {
+ case EAGAIN:
+#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
+ case EWOULDBLOCK:
+#endif
+ rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "recvfrom(2) would block");
+ }
+ rb_sys_fail("recvfrom(2)");
+ }
+ if (slen < RSTRING_LEN(str)) {
+ rb_str_set_len(str, slen);
+ }
+ rb_obj_taint(str);
+ switch (from) {
+ case RECV_RECV:
+ return str;
+
+ case RECV_IP:
+ if (alen && alen != sizeof(buf)) /* connection-oriented socket may not return a from result */
+ addr = rsock_ipaddr(&buf.addr, alen, fptr->mode & FMODE_NOREVLOOKUP);
+ break;
+
+ case RECV_SOCKET:
+ addr = rsock_io_socket_addrinfo(sock, &buf.addr, alen);
+ break;
+
+ default:
+ rb_bug("rsock_s_recvfrom_nonblock called with bad value");
+ }
+ return rb_assoc_new(str, addr);
+}
+
+/* returns true if SOCK_CLOEXEC is supported */
+int rsock_detect_cloexec(int fd)
+{
+#ifdef SOCK_CLOEXEC
+ int flags = fcntl(fd, F_GETFD);
+
+ if (flags == -1)
+ rb_bug("rsock_detect_cloexec: fcntl(%d, F_GETFD) failed: %s", fd, strerror(errno));
+
+ if (flags & FD_CLOEXEC)
+ return 1;
+#endif
+ return 0;
+}
+
+#ifdef SOCK_CLOEXEC
+static int
+rsock_socket0(int domain, int type, int proto)
+{
+ int ret;
+ static int cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
+
+ if (cloexec_state > 0) { /* common path, if SOCK_CLOEXEC is defined */
+ ret = socket(domain, type|SOCK_CLOEXEC, proto);
+ if (ret >= 0) {
+ if (ret <= 2)
+ goto fix_cloexec;
+ goto update_max_fd;
+ }
+ }
+ else if (cloexec_state < 0) { /* usually runs once only for detection */
+ ret = socket(domain, type|SOCK_CLOEXEC, proto);
+ if (ret >= 0) {
+ cloexec_state = rsock_detect_cloexec(ret);
+ if (cloexec_state == 0 || ret <= 2)
+ goto fix_cloexec;
+ goto update_max_fd;
+ }
+ else if (ret == -1 && errno == EINVAL) {
+ /* SOCK_CLOEXEC is available since Linux 2.6.27. Linux 2.6.18 fails with EINVAL */
+ ret = socket(domain, type, proto);
+ if (ret != -1) {
+ cloexec_state = 0;
+ /* fall through to fix_cloexec */
+ }
+ }
+ }
+ else { /* cloexec_state == 0 */
+ ret = socket(domain, type, proto);
+ }
+ if (ret == -1)
+ return -1;
+fix_cloexec:
+ rb_maygvl_fd_fix_cloexec(ret);
+update_max_fd:
+ rb_update_max_fd(ret);
+
+ return ret;
+}
+#else /* !SOCK_CLOEXEC */
+static int
+rsock_socket0(int domain, int type, int proto)
+{
+ int ret = socket(domain, type, proto);
+
+ if (ret == -1)
+ return -1;
+ rb_fd_fix_cloexec(ret);
+
+ return ret;
+}
+#endif /* !SOCK_CLOEXEC */
+
+int
+rsock_socket(int domain, int type, int proto)
+{
+ int fd;
+
+ fd = rsock_socket0(domain, type, proto);
+ if (fd < 0) {
+ if (errno == EMFILE || errno == ENFILE) {
+ rb_gc();
+ fd = rsock_socket0(domain, type, proto);
+ }
+ }
+ if (0 <= fd)
+ rb_update_max_fd(fd);
+ return fd;
+}
+
+/* emulate blocking connect behavior on EINTR or non-blocking socket */
+static int
+wait_connectable(int fd)
+{
+ int sockerr, revents;
+ socklen_t sockerrlen;
+
+ /* only to clear pending error */
+ sockerrlen = (socklen_t)sizeof(sockerr);
+ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr, &sockerrlen) < 0)
+ return -1;
+
+ /*
+ * Stevens book says, successful finish turn on RB_WAITFD_OUT and
+ * failure finish turn on both RB_WAITFD_IN and RB_WAITFD_OUT.
+ * So it's enough to wait only RB_WAITFD_OUT and check the pending error
+ * by getsockopt().
+ *
+ * Note: rb_wait_for_single_fd already retries on EINTR/ERESTART
+ */
+ revents = rb_wait_for_single_fd(fd, RB_WAITFD_IN|RB_WAITFD_OUT, NULL);
+
+ if (revents < 0)
+ return -1;
+
+ sockerrlen = (socklen_t)sizeof(sockerr);
+ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr, &sockerrlen) < 0)
+ return -1;
+
+ switch (sockerr) {
+ case 0:
+ /*
+ * be defensive in case some platforms set SO_ERROR on the original,
+ * interrupted connect()
+ */
+ case EINTR:
+#ifdef ERESTART
+ case ERESTART:
+#endif
+ case EAGAIN:
+#ifdef EINPROGRESS
+ case EINPROGRESS:
+#endif
+#ifdef EALREADY
+ case EALREADY:
+#endif
+#ifdef EISCONN
+ case EISCONN:
+#endif
+ return 0; /* success */
+ default:
+ /* likely (but not limited to): ECONNREFUSED, ETIMEDOUT, EHOSTUNREACH */
+ errno = sockerr;
+ return -1;
+ }
+
+ return 0;
+}
+
+struct connect_arg {
+ int fd;
+ socklen_t len;
+ const struct sockaddr *sockaddr;
+};
+
+static VALUE
+connect_blocking(void *data)
+{
+ struct connect_arg *arg = data;
+ return (VALUE)connect(arg->fd, arg->sockaddr, arg->len);
+}
+
+#if defined(SOCKS) && !defined(SOCKS5)
+static VALUE
+socks_connect_blocking(void *data)
+{
+ struct connect_arg *arg = data;
+ return (VALUE)Rconnect(arg->fd, arg->sockaddr, arg->len);
+}
+#endif
+
+int
+rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks)
+{
+ int status;
+ rb_blocking_function_t *func = connect_blocking;
+ struct connect_arg arg;
+
+ arg.fd = fd;
+ arg.sockaddr = sockaddr;
+ arg.len = len;
+#if defined(SOCKS) && !defined(SOCKS5)
+ if (socks) func = socks_connect_blocking;
+#endif
+ status = (int)BLOCKING_REGION_FD(func, &arg);
+
+ if (status < 0) {
+ switch (errno) {
+ case EINTR:
+#ifdef ERESTART
+ case ERESTART:
+#endif
+ case EAGAIN:
+#ifdef EINPROGRESS
+ case EINPROGRESS:
+#endif
+ return wait_connectable(fd);
+ }
+ }
+ return status;
+}
+
+static void
+make_fd_nonblock(int fd)
+{
+ int flags;
+#ifdef F_GETFL
+ flags = fcntl(fd, F_GETFL);
+ if (flags == -1) {
+ rb_sys_fail("fnctl(2)");
+ }
+#else
+ flags = 0;
+#endif
+ flags |= O_NONBLOCK;
+ if (fcntl(fd, F_SETFL, flags) == -1) {
+ rb_sys_fail("fnctl(2)");
+ }
+}
+
+static int
+cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len)
+{
+ int ret;
+ socklen_t len0 = 0;
+#ifdef HAVE_ACCEPT4
+ static int try_accept4 = 1;
+#endif
+ if (address_len) len0 = *address_len;
+#ifdef HAVE_ACCEPT4
+ if (try_accept4) {
+ int flags = 0;
+#ifdef SOCK_CLOEXEC
+ flags |= SOCK_CLOEXEC;
+#endif
+ ret = accept4(socket, address, address_len, flags);
+ /* accept4 is available since Linux 2.6.28, glibc 2.10. */
+ if (ret != -1) {
+ if (ret <= 2)
+ rb_maygvl_fd_fix_cloexec(ret);
+ if (address_len && len0 < *address_len) *address_len = len0;
+ return ret;
+ }
+ if (errno != ENOSYS) {
+ return -1;
+ }
+ try_accept4 = 0;
+ }
+#endif
+ ret = accept(socket, address, address_len);
+ if (ret == -1) return -1;
+ if (address_len && len0 < *address_len) *address_len = len0;
+ rb_maygvl_fd_fix_cloexec(ret);
+ return ret;
+}
+
+
+VALUE
+rsock_s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len)
+{
+ int fd2;
+
+ rb_secure(3);
+ rb_io_set_nonblock(fptr);
+ fd2 = cloexec_accept(fptr->fd, (struct sockaddr*)sockaddr, len);
+ if (fd2 < 0) {
+ switch (errno) {
+ case EAGAIN:
+#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
+ case EWOULDBLOCK:
+#endif
+ case ECONNABORTED:
+#if defined EPROTO
+ case EPROTO:
+#endif
+ rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "accept(2) would block");
+ }
+ rb_sys_fail("accept(2)");
+ }
+ rb_update_max_fd(fd2);
+ make_fd_nonblock(fd2);
+ return rsock_init_sock(rb_obj_alloc(klass), fd2);
+}
+
+struct accept_arg {
+ int fd;
+ struct sockaddr *sockaddr;
+ socklen_t *len;
+};
+
+static VALUE
+accept_blocking(void *data)
+{
+ struct accept_arg *arg = data;
+ return (VALUE)cloexec_accept(arg->fd, arg->sockaddr, arg->len);
+}
+
+VALUE
+rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
+{
+ int fd2;
+ int retry = 0;
+ struct accept_arg arg;
+
+ rb_secure(3);
+ arg.fd = fd;
+ arg.sockaddr = sockaddr;
+ arg.len = len;
+ retry:
+ rsock_maybe_wait_fd(fd);
+ fd2 = (int)BLOCKING_REGION_FD(accept_blocking, &arg);
+ if (fd2 < 0) {
+ switch (errno) {
+ case EMFILE:
+ case ENFILE:
+ if (retry) break;
+ rb_gc();
+ retry = 1;
+ goto retry;
+ default:
+ if (!rb_io_wait_readable(fd)) break;
+ retry = 0;
+ goto retry;
+ }
+ rb_sys_fail("accept(2)");
+ }
+ rb_update_max_fd(fd2);
+ if (!klass) return INT2NUM(fd2);
+ return rsock_init_sock(rb_obj_alloc(klass), fd2);
+}
+
+int
+rsock_getfamily(int sockfd)
+{
+ union_sockaddr ss;
+ socklen_t sslen = (socklen_t)sizeof(ss);
+
+ ss.addr.sa_family = AF_UNSPEC;
+ if (getsockname(sockfd, &ss.addr, &sslen) < 0)
+ return AF_UNSPEC;
+
+ return ss.addr.sa_family;
+}
+
+void
+rsock_init_socket_init(void)
+{
+ /*
+ * SocketError is the error class for socket.
+ */
+ rb_eSocket = rb_define_class("SocketError", rb_eStandardError);
+ rsock_init_ipsocket();
+ rsock_init_tcpsocket();
+ rsock_init_tcpserver();
+ rsock_init_sockssocket();
+ rsock_init_udpsocket();
+ rsock_init_unixsocket();
+ rsock_init_unixserver();
+ rsock_init_sockopt();
+ rsock_init_ancdata();
+ rsock_init_addrinfo();
+ rsock_init_sockifaddr();
+ rsock_init_socket_constants();
+}
diff --git a/jni/ruby/ext/socket/ipsocket.c b/jni/ruby/ext/socket/ipsocket.c
new file mode 100644
index 0000000..16a8373
--- /dev/null
+++ b/jni/ruby/ext/socket/ipsocket.c
@@ -0,0 +1,338 @@
+/************************************************
+
+ ipsocket.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+struct inetsock_arg
+{
+ VALUE sock;
+ struct {
+ VALUE host, serv;
+ struct rb_addrinfo *res;
+ } remote, local;
+ int type;
+ int fd;
+};
+
+static VALUE
+inetsock_cleanup(struct inetsock_arg *arg)
+{
+ if (arg->remote.res) {
+ rb_freeaddrinfo(arg->remote.res);
+ arg->remote.res = 0;
+ }
+ if (arg->local.res) {
+ rb_freeaddrinfo(arg->local.res);
+ arg->local.res = 0;
+ }
+ if (arg->fd >= 0) {
+ close(arg->fd);
+ }
+ return Qnil;
+}
+
+static VALUE
+init_inetsock_internal(struct inetsock_arg *arg)
+{
+ int error = 0;
+ int type = arg->type;
+ struct addrinfo *res, *lres;
+ int fd, status = 0, local = 0;
+ const char *syscall = 0;
+
+ arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv, SOCK_STREAM,
+ (type == INET_SERVER) ? AI_PASSIVE : 0);
+ /*
+ * Maybe also accept a local address
+ */
+
+ if (type != INET_SERVER && (!NIL_P(arg->local.host) || !NIL_P(arg->local.serv))) {
+ arg->local.res = rsock_addrinfo(arg->local.host, arg->local.serv, SOCK_STREAM, 0);
+ }
+
+ arg->fd = fd = -1;
+ for (res = arg->remote.res->ai; res; res = res->ai_next) {
+#if !defined(INET6) && defined(AF_INET6)
+ if (res->ai_family == AF_INET6)
+ continue;
+#endif
+ lres = NULL;
+ if (arg->local.res) {
+ for (lres = arg->local.res->ai; lres; lres = lres->ai_next) {
+ if (lres->ai_family == res->ai_family)
+ break;
+ }
+ if (!lres) {
+ if (res->ai_next || status < 0)
+ continue;
+ /* Use a different family local address if no choice, this
+ * will cause EAFNOSUPPORT. */
+ lres = arg->local.res->ai;
+ }
+ }
+ status = rsock_socket(res->ai_family,res->ai_socktype,res->ai_protocol);
+ syscall = "socket(2)";
+ fd = status;
+ if (fd < 0) {
+ error = errno;
+ continue;
+ }
+ arg->fd = fd;
+ if (type == INET_SERVER) {
+#if !defined(_WIN32) && !defined(__CYGWIN__)
+ status = 1;
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
+ (char*)&status, (socklen_t)sizeof(status));
+#endif
+ status = bind(fd, res->ai_addr, res->ai_addrlen);
+ syscall = "bind(2)";
+ }
+ else {
+ if (lres) {
+ status = bind(fd, lres->ai_addr, lres->ai_addrlen);
+ local = status;
+ syscall = "bind(2)";
+ }
+
+ if (status >= 0) {
+ status = rsock_connect(fd, res->ai_addr, res->ai_addrlen,
+ (type == INET_SOCKS));
+ syscall = "connect(2)";
+ }
+ }
+
+ if (status < 0) {
+ error = errno;
+ close(fd);
+ arg->fd = fd = -1;
+ continue;
+ } else
+ break;
+ }
+ if (status < 0) {
+ VALUE host, port;
+
+ if (local < 0) {
+ host = arg->local.host;
+ port = arg->local.serv;
+ } else {
+ host = arg->remote.host;
+ port = arg->remote.serv;
+ }
+
+ rsock_syserr_fail_host_port(error, syscall, host, port);
+ }
+
+ arg->fd = -1;
+
+ if (type == INET_SERVER) {
+ status = listen(fd, SOMAXCONN);
+ if (status < 0) {
+ error = errno;
+ close(fd);
+ rb_syserr_fail(error, "listen(2)");
+ }
+ }
+
+ /* create new instance */
+ return rsock_init_sock(arg->sock, fd);
+}
+
+VALUE
+rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv,
+ VALUE local_host, VALUE local_serv, int type)
+{
+ struct inetsock_arg arg;
+ arg.sock = sock;
+ arg.remote.host = remote_host;
+ arg.remote.serv = remote_serv;
+ arg.remote.res = 0;
+ arg.local.host = local_host;
+ arg.local.serv = local_serv;
+ arg.local.res = 0;
+ arg.type = type;
+ arg.fd = -1;
+ return rb_ensure(init_inetsock_internal, (VALUE)&arg,
+ inetsock_cleanup, (VALUE)&arg);
+}
+
+static ID id_numeric, id_hostname;
+
+int
+rsock_revlookup_flag(VALUE revlookup, int *norevlookup)
+{
+#define return_norevlookup(x) {*norevlookup = (x); return 1;}
+ ID id;
+
+ switch (revlookup) {
+ case Qtrue: return_norevlookup(0);
+ case Qfalse: return_norevlookup(1);
+ case Qnil: break;
+ default:
+ Check_Type(revlookup, T_SYMBOL);
+ id = SYM2ID(revlookup);
+ if (id == id_numeric) return_norevlookup(1);
+ if (id == id_hostname) return_norevlookup(0);
+ rb_raise(rb_eArgError, "invalid reverse_lookup flag: :%s", rb_id2name(id));
+ }
+ return 0;
+#undef return_norevlookup
+}
+
+/*
+ * call-seq:
+ * ipsocket.addr([reverse_lookup]) => [address_family, port, hostname, numeric_address]
+ *
+ * Returns the local address as an array which contains
+ * address_family, port, hostname and numeric_address.
+ *
+ * If +reverse_lookup+ is +true+ or +:hostname+,
+ * hostname is obtained from numeric_address using reverse lookup.
+ * Or if it is +false+, or +:numeric+,
+ * hostname is same as numeric_address.
+ * Or if it is +nil+ or ommitted, obeys to +ipsocket.do_not_reverse_lookup+.
+ * See +Socket.getaddrinfo+ also.
+ *
+ * TCPSocket.open("www.ruby-lang.org", 80) {|sock|
+ * p sock.addr #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
+ * p sock.addr(true) #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
+ * p sock.addr(false) #=> ["AF_INET", 49429, "192.168.0.128", "192.168.0.128"]
+ * p sock.addr(:hostname) #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
+ * p sock.addr(:numeric) #=> ["AF_INET", 49429, "192.168.0.128", "192.168.0.128"]
+ * }
+ *
+ */
+static VALUE
+ip_addr(int argc, VALUE *argv, VALUE sock)
+{
+ rb_io_t *fptr;
+ union_sockaddr addr;
+ socklen_t len = (socklen_t)sizeof addr;
+ int norevlookup;
+
+ GetOpenFile(sock, fptr);
+
+ if (argc < 1 || !rsock_revlookup_flag(argv[0], &norevlookup))
+ norevlookup = fptr->mode & FMODE_NOREVLOOKUP;
+ if (getsockname(fptr->fd, &addr.addr, &len) < 0)
+ rb_sys_fail("getsockname(2)");
+ return rsock_ipaddr(&addr.addr, len, norevlookup);
+}
+
+/*
+ * call-seq:
+ * ipsocket.peeraddr([reverse_lookup]) => [address_family, port, hostname, numeric_address]
+ *
+ * Returns the remote address as an array which contains
+ * address_family, port, hostname and numeric_address.
+ * It is defined for connection oriented socket such as TCPSocket.
+ *
+ * If +reverse_lookup+ is +true+ or +:hostname+,
+ * hostname is obtained from numeric_address using reverse lookup.
+ * Or if it is +false+, or +:numeric+,
+ * hostname is same as numeric_address.
+ * Or if it is +nil+ or ommitted, obeys to +ipsocket.do_not_reverse_lookup+.
+ * See +Socket.getaddrinfo+ also.
+ *
+ * TCPSocket.open("www.ruby-lang.org", 80) {|sock|
+ * p sock.peeraddr #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
+ * p sock.peeraddr(true) #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
+ * p sock.peeraddr(false) #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"]
+ * p sock.peeraddr(:hostname) #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
+ * p sock.peeraddr(:numeric) #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"]
+ * }
+ *
+ */
+static VALUE
+ip_peeraddr(int argc, VALUE *argv, VALUE sock)
+{
+ rb_io_t *fptr;
+ union_sockaddr addr;
+ socklen_t len = (socklen_t)sizeof addr;
+ int norevlookup;
+
+ GetOpenFile(sock, fptr);
+
+ if (argc < 1 || !rsock_revlookup_flag(argv[0], &norevlookup))
+ norevlookup = fptr->mode & FMODE_NOREVLOOKUP;
+ if (getpeername(fptr->fd, &addr.addr, &len) < 0)
+ rb_sys_fail("getpeername(2)");
+ return rsock_ipaddr(&addr.addr, len, norevlookup);
+}
+
+/*
+ * call-seq:
+ * ipsocket.recvfrom(maxlen) => [mesg, ipaddr]
+ * ipsocket.recvfrom(maxlen, flags) => [mesg, ipaddr]
+ *
+ * Receives a message and return the message as a string and
+ * an address which the message come from.
+ *
+ * _maxlen_ is the maximum number of bytes to receive.
+ *
+ * _flags_ should be a bitwise OR of Socket::MSG_* constants.
+ *
+ * ipaddr is same as IPSocket#{peeraddr,addr}.
+ *
+ * u1 = UDPSocket.new
+ * u1.bind("127.0.0.1", 4913)
+ * u2 = UDPSocket.new
+ * u2.send "uuuu", 0, "127.0.0.1", 4913
+ * p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
+ *
+ */
+static VALUE
+ip_recvfrom(int argc, VALUE *argv, VALUE sock)
+{
+ return rsock_s_recvfrom(sock, argc, argv, RECV_IP);
+}
+
+/*
+ * call-seq:
+ * IPSocket.getaddress(host) => ipaddress
+ *
+ * Lookups the IP address of _host_.
+ *
+ * IPSocket.getaddress("localhost") #=> "127.0.0.1"
+ * IPSocket.getaddress("ip6-localhost") #=> "::1"
+ *
+ */
+static VALUE
+ip_s_getaddress(VALUE obj, VALUE host)
+{
+ union_sockaddr addr;
+ struct rb_addrinfo *res = rsock_addrinfo(host, Qnil, SOCK_STREAM, 0);
+ socklen_t len = res->ai->ai_addrlen;
+
+ /* just take the first one */
+ memcpy(&addr, res->ai->ai_addr, len);
+ rb_freeaddrinfo(res);
+
+ return rsock_make_ipaddr(&addr.addr, len);
+}
+
+void
+rsock_init_ipsocket(void)
+{
+ /*
+ * Document-class: IPSocket < BasicSocket
+ *
+ * IPSocket is the super class of TCPSocket and UDPSocket.
+ */
+ rb_cIPSocket = rb_define_class("IPSocket", rb_cBasicSocket);
+ rb_define_method(rb_cIPSocket, "addr", ip_addr, -1);
+ rb_define_method(rb_cIPSocket, "peeraddr", ip_peeraddr, -1);
+ rb_define_method(rb_cIPSocket, "recvfrom", ip_recvfrom, -1);
+ rb_define_singleton_method(rb_cIPSocket, "getaddress", ip_s_getaddress, 1);
+ rb_undef_method(rb_cIPSocket, "getpeereid");
+
+ id_numeric = rb_intern_const("numeric");
+ id_hostname = rb_intern_const("hostname");
+}
diff --git a/jni/ruby/ext/socket/lib/socket.rb b/jni/ruby/ext/socket/lib/socket.rb
new file mode 100644
index 0000000..2cd7aea
--- /dev/null
+++ b/jni/ruby/ext/socket/lib/socket.rb
@@ -0,0 +1,871 @@
+require 'socket.so'
+
+class Addrinfo
+ # creates an Addrinfo object from the arguments.
+ #
+ # The arguments are interpreted as similar to self.
+ #
+ # Addrinfo.tcp("0.0.0.0", 4649).family_addrinfo("www.ruby-lang.org", 80)
+ # #=> #<Addrinfo: 221.186.184.68:80 TCP (www.ruby-lang.org:80)>
+ #
+ # Addrinfo.unix("/tmp/sock").family_addrinfo("/tmp/sock2")
+ # #=> #<Addrinfo: /tmp/sock2 SOCK_STREAM>
+ #
+ def family_addrinfo(*args)
+ if args.empty?
+ raise ArgumentError, "no address specified"
+ elsif Addrinfo === args.first
+ raise ArgumentError, "too many arguments" if args.length != 1
+ addrinfo = args.first
+ if (self.pfamily != addrinfo.pfamily) ||
+ (self.socktype != addrinfo.socktype)
+ raise ArgumentError, "Addrinfo type mismatch"
+ end
+ addrinfo
+ elsif self.ip?
+ raise ArgumentError, "IP address needs host and port but #{args.length} arguments given" if args.length != 2
+ host, port = args
+ Addrinfo.getaddrinfo(host, port, self.pfamily, self.socktype, self.protocol)[0]
+ elsif self.unix?
+ raise ArgumentError, "UNIX socket needs single path argument but #{args.length} arguments given" if args.length != 1
+ path, = args
+ Addrinfo.unix(path)
+ else
+ raise ArgumentError, "unexpected family"
+ end
+ end
+
+ # creates a new Socket connected to the address of +local_addrinfo+.
+ #
+ # If _local_addrinfo_ is nil, the address of the socket is not bound.
+ #
+ # The _timeout_ specify the seconds for timeout.
+ # Errno::ETIMEDOUT is raised when timeout occur.
+ #
+ # If a block is given the created socket is yielded for each address.
+ #
+ def connect_internal(local_addrinfo, timeout=nil) # :yields: socket
+ sock = Socket.new(self.pfamily, self.socktype, self.protocol)
+ begin
+ sock.ipv6only! if self.ipv6?
+ sock.bind local_addrinfo if local_addrinfo
+ if timeout
+ begin
+ sock.connect_nonblock(self)
+ rescue IO::WaitWritable
+ if !IO.select(nil, [sock], nil, timeout)
+ raise Errno::ETIMEDOUT, 'user specified timeout'
+ end
+ begin
+ sock.connect_nonblock(self) # check connection failure
+ rescue Errno::EISCONN
+ end
+ end
+ else
+ sock.connect(self)
+ end
+ rescue Exception
+ sock.close
+ raise
+ end
+ if block_given?
+ begin
+ yield sock
+ ensure
+ sock.close if !sock.closed?
+ end
+ else
+ sock
+ end
+ end
+ private :connect_internal
+
+ # :call-seq:
+ # addrinfo.connect_from([local_addr_args], [opts]) {|socket| ... }
+ # addrinfo.connect_from([local_addr_args], [opts])
+ #
+ # creates a socket connected to the address of self.
+ #
+ # If one or more arguments given as _local_addr_args_,
+ # it is used as the local address of the socket.
+ # _local_addr_args_ is given for family_addrinfo to obtain actual address.
+ #
+ # If _local_addr_args_ is not given, the local address of the socket is not bound.
+ #
+ # The optional last argument _opts_ is options represented by a hash.
+ # _opts_ may have following options:
+ #
+ # [:timeout] specify the timeout in seconds.
+ #
+ # If a block is given, it is called with the socket and the value of the block is returned.
+ # The socket is returned otherwise.
+ #
+ # Addrinfo.tcp("www.ruby-lang.org", 80).connect_from("0.0.0.0", 4649) {|s|
+ # s.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
+ # puts s.read
+ # }
+ #
+ # # Addrinfo object can be taken for the argument.
+ # Addrinfo.tcp("www.ruby-lang.org", 80).connect_from(Addrinfo.tcp("0.0.0.0", 4649)) {|s|
+ # s.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
+ # puts s.read
+ # }
+ #
+ def connect_from(*args, &block)
+ opts = Hash === args.last ? args.pop : {}
+ local_addr_args = args
+ connect_internal(family_addrinfo(*local_addr_args), opts[:timeout], &block)
+ end
+
+ # :call-seq:
+ # addrinfo.connect([opts]) {|socket| ... }
+ # addrinfo.connect([opts])
+ #
+ # creates a socket connected to the address of self.
+ #
+ # The optional argument _opts_ is options represented by a hash.
+ # _opts_ may have following options:
+ #
+ # [:timeout] specify the timeout in seconds.
+ #
+ # If a block is given, it is called with the socket and the value of the block is returned.
+ # The socket is returned otherwise.
+ #
+ # Addrinfo.tcp("www.ruby-lang.org", 80).connect {|s|
+ # s.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
+ # puts s.read
+ # }
+ #
+ def connect(opts={}, &block)
+ connect_internal(nil, opts[:timeout], &block)
+ end
+
+ # :call-seq:
+ # addrinfo.connect_to([remote_addr_args], [opts]) {|socket| ... }
+ # addrinfo.connect_to([remote_addr_args], [opts])
+ #
+ # creates a socket connected to _remote_addr_args_ and bound to self.
+ #
+ # The optional last argument _opts_ is options represented by a hash.
+ # _opts_ may have following options:
+ #
+ # [:timeout] specify the timeout in seconds.
+ #
+ # If a block is given, it is called with the socket and the value of the block is returned.
+ # The socket is returned otherwise.
+ #
+ # Addrinfo.tcp("0.0.0.0", 4649).connect_to("www.ruby-lang.org", 80) {|s|
+ # s.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
+ # puts s.read
+ # }
+ #
+ def connect_to(*args, &block)
+ opts = Hash === args.last ? args.pop : {}
+ remote_addr_args = args
+ remote_addrinfo = family_addrinfo(*remote_addr_args)
+ remote_addrinfo.send(:connect_internal, self, opts[:timeout], &block)
+ end
+
+ # creates a socket bound to self.
+ #
+ # If a block is given, it is called with the socket and the value of the block is returned.
+ # The socket is returned otherwise.
+ #
+ # Addrinfo.udp("0.0.0.0", 9981).bind {|s|
+ # s.local_address.connect {|s| s.send "hello", 0 }
+ # p s.recv(10) #=> "hello"
+ # }
+ #
+ def bind
+ sock = Socket.new(self.pfamily, self.socktype, self.protocol)
+ begin
+ sock.ipv6only! if self.ipv6?
+ sock.setsockopt(:SOCKET, :REUSEADDR, 1)
+ sock.bind(self)
+ rescue Exception
+ sock.close
+ raise
+ end
+ if block_given?
+ begin
+ yield sock
+ ensure
+ sock.close if !sock.closed?
+ end
+ else
+ sock
+ end
+ end
+
+ # creates a listening socket bound to self.
+ def listen(backlog=Socket::SOMAXCONN)
+ sock = Socket.new(self.pfamily, self.socktype, self.protocol)
+ begin
+ sock.ipv6only! if self.ipv6?
+ sock.setsockopt(:SOCKET, :REUSEADDR, 1)
+ sock.bind(self)
+ sock.listen(backlog)
+ rescue Exception
+ sock.close
+ raise
+ end
+ if block_given?
+ begin
+ yield sock
+ ensure
+ sock.close if !sock.closed?
+ end
+ else
+ sock
+ end
+ end
+
+ # iterates over the list of Addrinfo objects obtained by Addrinfo.getaddrinfo.
+ #
+ # Addrinfo.foreach(nil, 80) {|x| p x }
+ # #=> #<Addrinfo: 127.0.0.1:80 TCP (:80)>
+ # # #<Addrinfo: 127.0.0.1:80 UDP (:80)>
+ # # #<Addrinfo: [::1]:80 TCP (:80)>
+ # # #<Addrinfo: [::1]:80 UDP (:80)>
+ #
+ def self.foreach(nodename, service, family=nil, socktype=nil, protocol=nil, flags=nil, &block)
+ Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol, flags).each(&block)
+ end
+end
+
+class BasicSocket < IO
+ # Returns an address of the socket suitable for connect in the local machine.
+ #
+ # This method returns _self_.local_address, except following condition.
+ #
+ # - IPv4 unspecified address (0.0.0.0) is replaced by IPv4 loopback address (127.0.0.1).
+ # - IPv6 unspecified address (::) is replaced by IPv6 loopback address (::1).
+ #
+ # If the local address is not suitable for connect, SocketError is raised.
+ # IPv4 and IPv6 address which port is 0 is not suitable for connect.
+ # Unix domain socket which has no path is not suitable for connect.
+ #
+ # Addrinfo.tcp("0.0.0.0", 0).listen {|serv|
+ # p serv.connect_address #=> #<Addrinfo: 127.0.0.1:53660 TCP>
+ # serv.connect_address.connect {|c|
+ # s, _ = serv.accept
+ # p [c, s] #=> [#<Socket:fd 4>, #<Socket:fd 6>]
+ # }
+ # }
+ #
+ def connect_address
+ addr = local_address
+ afamily = addr.afamily
+ if afamily == Socket::AF_INET
+ raise SocketError, "unbound IPv4 socket" if addr.ip_port == 0
+ if addr.ip_address == "0.0.0.0"
+ addr = Addrinfo.new(["AF_INET", addr.ip_port, nil, "127.0.0.1"], addr.pfamily, addr.socktype, addr.protocol)
+ end
+ elsif defined?(Socket::AF_INET6) && afamily == Socket::AF_INET6
+ raise SocketError, "unbound IPv6 socket" if addr.ip_port == 0
+ if addr.ip_address == "::"
+ addr = Addrinfo.new(["AF_INET6", addr.ip_port, nil, "::1"], addr.pfamily, addr.socktype, addr.protocol)
+ elsif addr.ip_address == "0.0.0.0" # MacOS X 10.4 returns "a.b.c.d" for IPv4-mapped IPv6 address.
+ addr = Addrinfo.new(["AF_INET6", addr.ip_port, nil, "::1"], addr.pfamily, addr.socktype, addr.protocol)
+ elsif addr.ip_address == "::ffff:0.0.0.0" # MacOS X 10.6 returns "::ffff:a.b.c.d" for IPv4-mapped IPv6 address.
+ addr = Addrinfo.new(["AF_INET6", addr.ip_port, nil, "::1"], addr.pfamily, addr.socktype, addr.protocol)
+ end
+ elsif defined?(Socket::AF_UNIX) && afamily == Socket::AF_UNIX
+ raise SocketError, "unbound Unix socket" if addr.unix_path == ""
+ end
+ addr
+ end
+end
+
+class Socket < BasicSocket
+ # enable the socket option IPV6_V6ONLY if IPV6_V6ONLY is available.
+ def ipv6only!
+ if defined? Socket::IPV6_V6ONLY
+ self.setsockopt(:IPV6, :V6ONLY, 1)
+ end
+ end
+
+ # :call-seq:
+ # Socket.tcp(host, port, local_host=nil, local_port=nil, [opts]) {|socket| ... }
+ # Socket.tcp(host, port, local_host=nil, local_port=nil, [opts])
+ #
+ # creates a new socket object connected to host:port using TCP/IP.
+ #
+ # If local_host:local_port is given,
+ # the socket is bound to it.
+ #
+ # The optional last argument _opts_ is options represented by a hash.
+ # _opts_ may have following options:
+ #
+ # [:connect_timeout] specify the timeout in seconds.
+ #
+ # If a block is given, the block is called with the socket.
+ # The value of the block is returned.
+ # The socket is closed when this method returns.
+ #
+ # If no block is given, the socket is returned.
+ #
+ # Socket.tcp("www.ruby-lang.org", 80) {|sock|
+ # sock.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
+ # sock.close_write
+ # puts sock.read
+ # }
+ #
+ def self.tcp(host, port, *rest) # :yield: socket
+ opts = Hash === rest.last ? rest.pop : {}
+ raise ArgumentError, "wrong number of arguments (#{rest.length} for 2)" if 2 < rest.length
+ local_host, local_port = rest
+ last_error = nil
+ ret = nil
+
+ connect_timeout = opts[:connect_timeout]
+
+ local_addr_list = nil
+ if local_host != nil || local_port != nil
+ local_addr_list = Addrinfo.getaddrinfo(local_host, local_port, nil, :STREAM, nil)
+ end
+
+ Addrinfo.foreach(host, port, nil, :STREAM) {|ai|
+ if local_addr_list
+ local_addr = local_addr_list.find {|local_ai| local_ai.afamily == ai.afamily }
+ next if !local_addr
+ else
+ local_addr = nil
+ end
+ begin
+ sock = local_addr ?
+ ai.connect_from(local_addr, :timeout => connect_timeout) :
+ ai.connect(:timeout => connect_timeout)
+ rescue SystemCallError
+ last_error = $!
+ next
+ end
+ ret = sock
+ break
+ }
+ if !ret
+ if last_error
+ raise last_error
+ else
+ raise SocketError, "no appropriate local address"
+ end
+ end
+ if block_given?
+ begin
+ yield ret
+ ensure
+ ret.close if !ret.closed?
+ end
+ else
+ ret
+ end
+ end
+
+ # :stopdoc:
+ def self.ip_sockets_port0(ai_list, reuseaddr)
+ sockets = []
+ begin
+ sockets.clear
+ port = nil
+ ai_list.each {|ai|
+ begin
+ s = Socket.new(ai.pfamily, ai.socktype, ai.protocol)
+ rescue SystemCallError
+ next
+ end
+ sockets << s
+ s.ipv6only! if ai.ipv6?
+ if reuseaddr
+ s.setsockopt(:SOCKET, :REUSEADDR, 1)
+ end
+ if !port
+ s.bind(ai)
+ port = s.local_address.ip_port
+ else
+ s.bind(ai.family_addrinfo(ai.ip_address, port))
+ end
+ }
+ rescue Errno::EADDRINUSE
+ sockets.each {|s| s.close }
+ retry
+ rescue Exception
+ sockets.each {|s| s.close }
+ raise
+ end
+ sockets
+ end
+ class << self
+ private :ip_sockets_port0
+ end
+
+ def self.tcp_server_sockets_port0(host)
+ ai_list = Addrinfo.getaddrinfo(host, 0, nil, :STREAM, nil, Socket::AI_PASSIVE)
+ sockets = ip_sockets_port0(ai_list, true)
+ begin
+ sockets.each {|s|
+ s.listen(Socket::SOMAXCONN)
+ }
+ rescue Exception
+ sockets.each {|s| s.close }
+ raise
+ end
+ sockets
+ end
+ class << self
+ private :tcp_server_sockets_port0
+ end
+ # :startdoc:
+
+ # creates TCP/IP server sockets for _host_ and _port_.
+ # _host_ is optional.
+ #
+ # If no block given,
+ # it returns an array of listening sockets.
+ #
+ # If a block is given, the block is called with the sockets.
+ # The value of the block is returned.
+ # The socket is closed when this method returns.
+ #
+ # If _port_ is 0, actual port number is chosen dynamically.
+ # However all sockets in the result has same port number.
+ #
+ # # tcp_server_sockets returns two sockets.
+ # sockets = Socket.tcp_server_sockets(1296)
+ # p sockets #=> [#<Socket:fd 3>, #<Socket:fd 4>]
+ #
+ # # The sockets contains IPv6 and IPv4 sockets.
+ # sockets.each {|s| p s.local_address }
+ # #=> #<Addrinfo: [::]:1296 TCP>
+ # # #<Addrinfo: 0.0.0.0:1296 TCP>
+ #
+ # # IPv6 and IPv4 socket has same port number, 53114, even if it is chosen dynamically.
+ # sockets = Socket.tcp_server_sockets(0)
+ # sockets.each {|s| p s.local_address }
+ # #=> #<Addrinfo: [::]:53114 TCP>
+ # # #<Addrinfo: 0.0.0.0:53114 TCP>
+ #
+ # # The block is called with the sockets.
+ # Socket.tcp_server_sockets(0) {|sockets|
+ # p sockets #=> [#<Socket:fd 3>, #<Socket:fd 4>]
+ # }
+ #
+ def self.tcp_server_sockets(host=nil, port)
+ if port == 0
+ sockets = tcp_server_sockets_port0(host)
+ else
+ last_error = nil
+ sockets = []
+ begin
+ Addrinfo.foreach(host, port, nil, :STREAM, nil, Socket::AI_PASSIVE) {|ai|
+ begin
+ s = ai.listen
+ rescue SystemCallError
+ last_error = $!
+ next
+ end
+ sockets << s
+ }
+ if sockets.empty?
+ raise last_error
+ end
+ rescue Exception
+ sockets.each {|s| s.close }
+ raise
+ end
+ end
+ if block_given?
+ begin
+ yield sockets
+ ensure
+ sockets.each {|s| s.close if !s.closed? }
+ end
+ else
+ sockets
+ end
+ end
+
+ # yield socket and client address for each a connection accepted via given sockets.
+ #
+ # The arguments are a list of sockets.
+ # The individual argument should be a socket or an array of sockets.
+ #
+ # This method yields the block sequentially.
+ # It means that the next connection is not accepted until the block returns.
+ # So concurrent mechanism, thread for example, should be used to service multiple clients at a time.
+ #
+ def self.accept_loop(*sockets) # :yield: socket, client_addrinfo
+ sockets.flatten!(1)
+ if sockets.empty?
+ raise ArgumentError, "no sockets"
+ end
+ loop {
+ readable, _, _ = IO.select(sockets)
+ readable.each {|r|
+ begin
+ sock, addr = r.accept_nonblock
+ rescue IO::WaitReadable
+ next
+ end
+ yield sock, addr
+ }
+ }
+ end
+
+ # creates a TCP/IP server on _port_ and calls the block for each connection accepted.
+ # The block is called with a socket and a client_address as an Addrinfo object.
+ #
+ # If _host_ is specified, it is used with _port_ to determine the server addresses.
+ #
+ # The socket is *not* closed when the block returns.
+ # So application should close it explicitly.
+ #
+ # This method calls the block sequentially.
+ # It means that the next connection is not accepted until the block returns.
+ # So concurrent mechanism, thread for example, should be used to service multiple clients at a time.
+ #
+ # Note that Addrinfo.getaddrinfo is used to determine the server socket addresses.
+ # When Addrinfo.getaddrinfo returns two or more addresses,
+ # IPv4 and IPv6 address for example,
+ # all of them are used.
+ # Socket.tcp_server_loop succeeds if one socket can be used at least.
+ #
+ # # Sequential echo server.
+ # # It services only one client at a time.
+ # Socket.tcp_server_loop(16807) {|sock, client_addrinfo|
+ # begin
+ # IO.copy_stream(sock, sock)
+ # ensure
+ # sock.close
+ # end
+ # }
+ #
+ # # Threaded echo server
+ # # It services multiple clients at a time.
+ # # Note that it may accept connections too much.
+ # Socket.tcp_server_loop(16807) {|sock, client_addrinfo|
+ # Thread.new {
+ # begin
+ # IO.copy_stream(sock, sock)
+ # ensure
+ # sock.close
+ # end
+ # }
+ # }
+ #
+ def self.tcp_server_loop(host=nil, port, &b) # :yield: socket, client_addrinfo
+ tcp_server_sockets(host, port) {|sockets|
+ accept_loop(sockets, &b)
+ }
+ end
+
+ # :call-seq:
+ # Socket.udp_server_sockets([host, ] port)
+ #
+ # Creates UDP/IP sockets for a UDP server.
+ #
+ # If no block given, it returns an array of sockets.
+ #
+ # If a block is given, the block is called with the sockets.
+ # The value of the block is returned.
+ # The sockets are closed when this method returns.
+ #
+ # If _port_ is zero, some port is chosen.
+ # But the chosen port is used for the all sockets.
+ #
+ # # UDP/IP echo server
+ # Socket.udp_server_sockets(0) {|sockets|
+ # p sockets.first.local_address.ip_port #=> 32963
+ # Socket.udp_server_loop_on(sockets) {|msg, msg_src|
+ # msg_src.reply msg
+ # }
+ # }
+ #
+ def self.udp_server_sockets(host=nil, port)
+ last_error = nil
+ sockets = []
+
+ ipv6_recvpktinfo = nil
+ if defined? Socket::AncillaryData
+ if defined? Socket::IPV6_RECVPKTINFO # RFC 3542
+ ipv6_recvpktinfo = Socket::IPV6_RECVPKTINFO
+ elsif defined? Socket::IPV6_PKTINFO # RFC 2292
+ ipv6_recvpktinfo = Socket::IPV6_PKTINFO
+ end
+ end
+
+ local_addrs = Socket.ip_address_list
+
+ ip_list = []
+ Addrinfo.foreach(host, port, nil, :DGRAM, nil, Socket::AI_PASSIVE) {|ai|
+ if ai.ipv4? && ai.ip_address == "0.0.0.0"
+ local_addrs.each {|a|
+ next if !a.ipv4?
+ ip_list << Addrinfo.new(a.to_sockaddr, :INET, :DGRAM, 0);
+ }
+ elsif ai.ipv6? && ai.ip_address == "::" && !ipv6_recvpktinfo
+ local_addrs.each {|a|
+ next if !a.ipv6?
+ ip_list << Addrinfo.new(a.to_sockaddr, :INET6, :DGRAM, 0);
+ }
+ else
+ ip_list << ai
+ end
+ }
+
+ if port == 0
+ sockets = ip_sockets_port0(ip_list, false)
+ else
+ ip_list.each {|ip|
+ ai = Addrinfo.udp(ip.ip_address, port)
+ begin
+ s = ai.bind
+ rescue SystemCallError
+ last_error = $!
+ next
+ end
+ sockets << s
+ }
+ if sockets.empty?
+ raise last_error
+ end
+ end
+
+ sockets.each {|s|
+ ai = s.local_address
+ if ipv6_recvpktinfo && ai.ipv6? && ai.ip_address == "::"
+ s.setsockopt(:IPV6, ipv6_recvpktinfo, 1)
+ end
+ }
+
+ if block_given?
+ begin
+ yield sockets
+ ensure
+ sockets.each {|s| s.close if !s.closed? } if sockets
+ end
+ else
+ sockets
+ end
+ end
+
+ # :call-seq:
+ # Socket.udp_server_recv(sockets) {|msg, msg_src| ... }
+ #
+ # Receive UDP/IP packets from the given _sockets_.
+ # For each packet received, the block is called.
+ #
+ # The block receives _msg_ and _msg_src_.
+ # _msg_ is a string which is the payload of the received packet.
+ # _msg_src_ is a Socket::UDPSource object which is used for reply.
+ #
+ # Socket.udp_server_loop can be implemented using this method as follows.
+ #
+ # udp_server_sockets(host, port) {|sockets|
+ # loop {
+ # readable, _, _ = IO.select(sockets)
+ # udp_server_recv(readable) {|msg, msg_src| ... }
+ # }
+ # }
+ #
+ def self.udp_server_recv(sockets)
+ sockets.each {|r|
+ begin
+ msg, sender_addrinfo, _, *controls = r.recvmsg_nonblock
+ rescue IO::WaitReadable
+ next
+ end
+ ai = r.local_address
+ if ai.ipv6? and pktinfo = controls.find {|c| c.cmsg_is?(:IPV6, :PKTINFO) }
+ ai = Addrinfo.udp(pktinfo.ipv6_pktinfo_addr.ip_address, ai.ip_port)
+ yield msg, UDPSource.new(sender_addrinfo, ai) {|reply_msg|
+ r.sendmsg reply_msg, 0, sender_addrinfo, pktinfo
+ }
+ else
+ yield msg, UDPSource.new(sender_addrinfo, ai) {|reply_msg|
+ r.send reply_msg, 0, sender_addrinfo
+ }
+ end
+ }
+ end
+
+ # :call-seq:
+ # Socket.udp_server_loop_on(sockets) {|msg, msg_src| ... }
+ #
+ # Run UDP/IP server loop on the given sockets.
+ #
+ # The return value of Socket.udp_server_sockets is appropriate for the argument.
+ #
+ # It calls the block for each message received.
+ #
+ def self.udp_server_loop_on(sockets, &b) # :yield: msg, msg_src
+ loop {
+ readable, _, _ = IO.select(sockets)
+ udp_server_recv(readable, &b)
+ }
+ end
+
+ # :call-seq:
+ # Socket.udp_server_loop(port) {|msg, msg_src| ... }
+ # Socket.udp_server_loop(host, port) {|msg, msg_src| ... }
+ #
+ # creates a UDP/IP server on _port_ and calls the block for each message arrived.
+ # The block is called with the message and its source information.
+ #
+ # This method allocates sockets internally using _port_.
+ # If _host_ is specified, it is used conjunction with _port_ to determine the server addresses.
+ #
+ # The _msg_ is a string.
+ #
+ # The _msg_src_ is a Socket::UDPSource object.
+ # It is used for reply.
+ #
+ # # UDP/IP echo server.
+ # Socket.udp_server_loop(9261) {|msg, msg_src|
+ # msg_src.reply msg
+ # }
+ #
+ def self.udp_server_loop(host=nil, port, &b) # :yield: message, message_source
+ udp_server_sockets(host, port) {|sockets|
+ udp_server_loop_on(sockets, &b)
+ }
+ end
+
+ # UDP/IP address information used by Socket.udp_server_loop.
+ class UDPSource
+ # +remote_address+ is an Addrinfo object.
+ #
+ # +local_address+ is an Addrinfo object.
+ #
+ # +reply_proc+ is a Proc used to send reply back to the source.
+ def initialize(remote_address, local_address, &reply_proc)
+ @remote_address = remote_address
+ @local_address = local_address
+ @reply_proc = reply_proc
+ end
+
+ # Address of the source
+ attr_reader :remote_address
+
+ # Local address
+ attr_reader :local_address
+
+ def inspect # :nodoc:
+ "\#<#{self.class}: #{@remote_address.inspect_sockaddr} to #{@local_address.inspect_sockaddr}>"
+ end
+
+ # Sends the String +msg+ to the source
+ def reply(msg)
+ @reply_proc.call msg
+ end
+ end
+
+ # creates a new socket connected to path using UNIX socket socket.
+ #
+ # If a block is given, the block is called with the socket.
+ # The value of the block is returned.
+ # The socket is closed when this method returns.
+ #
+ # If no block is given, the socket is returned.
+ #
+ # # talk to /tmp/sock socket.
+ # Socket.unix("/tmp/sock") {|sock|
+ # t = Thread.new { IO.copy_stream(sock, STDOUT) }
+ # IO.copy_stream(STDIN, sock)
+ # t.join
+ # }
+ #
+ def self.unix(path) # :yield: socket
+ addr = Addrinfo.unix(path)
+ sock = addr.connect
+ if block_given?
+ begin
+ yield sock
+ ensure
+ sock.close if !sock.closed?
+ end
+ else
+ sock
+ end
+ end
+
+ # creates a UNIX server socket on _path_
+ #
+ # If no block given, it returns a listening socket.
+ #
+ # If a block is given, it is called with the socket and the block value is returned.
+ # When the block exits, the socket is closed and the socket file is removed.
+ #
+ # socket = Socket.unix_server_socket("/tmp/s")
+ # p socket #=> #<Socket:fd 3>
+ # p socket.local_address #=> #<Addrinfo: /tmp/s SOCK_STREAM>
+ #
+ # Socket.unix_server_socket("/tmp/sock") {|s|
+ # p s #=> #<Socket:fd 3>
+ # p s.local_address #=> # #<Addrinfo: /tmp/sock SOCK_STREAM>
+ # }
+ #
+ def self.unix_server_socket(path)
+ if !unix_socket_abstract_name?(path)
+ begin
+ st = File.lstat(path)
+ rescue Errno::ENOENT
+ end
+ if st && st.socket? && st.owned?
+ File.unlink path
+ end
+ end
+ s = Addrinfo.unix(path).listen
+ if block_given?
+ begin
+ yield s
+ ensure
+ s.close if !s.closed?
+ if !unix_socket_abstract_name?(path)
+ File.unlink path
+ end
+ end
+ else
+ s
+ end
+ end
+
+ class << self
+ private
+
+ def unix_socket_abstract_name?(path)
+ /linux/ =~ RUBY_PLATFORM && /\A(\0|\z)/ =~ path
+ end
+ end
+
+ # creates a UNIX socket server on _path_.
+ # It calls the block for each socket accepted.
+ #
+ # If _host_ is specified, it is used with _port_ to determine the server ports.
+ #
+ # The socket is *not* closed when the block returns.
+ # So application should close it.
+ #
+ # This method deletes the socket file pointed by _path_ at first if
+ # the file is a socket file and it is owned by the user of the application.
+ # This is safe only if the directory of _path_ is not changed by a malicious user.
+ # So don't use /tmp/malicious-users-directory/socket.
+ # Note that /tmp/socket and /tmp/your-private-directory/socket is safe assuming that /tmp has sticky bit.
+ #
+ # # Sequential echo server.
+ # # It services only one client at a time.
+ # Socket.unix_server_loop("/tmp/sock") {|sock, client_addrinfo|
+ # begin
+ # IO.copy_stream(sock, sock)
+ # ensure
+ # sock.close
+ # end
+ # }
+ #
+ def self.unix_server_loop(path, &b) # :yield: socket, client_addrinfo
+ unix_server_socket(path) {|serv|
+ accept_loop(serv, &b)
+ }
+ end
+
+end
+
diff --git a/jni/ruby/ext/socket/mkconstants.rb b/jni/ruby/ext/socket/mkconstants.rb
new file mode 100644
index 0000000..59fcb17
--- /dev/null
+++ b/jni/ruby/ext/socket/mkconstants.rb
@@ -0,0 +1,804 @@
+require 'optparse'
+require 'erb'
+
+opt = OptionParser.new
+
+opt.def_option('-h', 'help') {
+ puts opt
+ exit 0
+}
+
+opt_o = nil
+opt.def_option('-o FILE', 'specify output file') {|filename|
+ opt_o = filename
+}
+
+opt_H = nil
+opt.def_option('-H FILE', 'specify output header file') {|filename|
+ opt_H = filename
+}
+
+C_ESC = {
+ "\\" => "\\\\",
+ '"' => '\"',
+ "\n" => '\n',
+}
+
+0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\%03o" % ch }
+0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\%03o" % ch }
+C_ESC_PAT = Regexp.union(*C_ESC.keys)
+
+def c_str(str)
+ '"' + str.gsub(C_ESC_PAT) {|s| C_ESC[s]} + '"'
+end
+
+opt.parse!
+
+
+
+h = {}
+COMMENTS = Hash.new { |h, name| h[name] = name }
+
+DATA.each_line {|s|
+ name, default_value, comment = s.chomp.split(/\s+/, 3)
+ next unless name && name[0] != ?#
+
+ default_value = nil if default_value == 'nil'
+
+ if h.has_key? name
+ warn "#{$.}: warning: duplicate name: #{name}"
+ next
+ end
+ h[name] = default_value
+ COMMENTS[name] = comment
+}
+DEFS = h.to_a
+
+def each_const
+ DEFS.each {|name, default_value|
+ guard = nil
+ if /\A(AF_INET6|PF_INET6|IPV6_.*)\z/ =~ name
+ # IPv6 is not supported although AF_INET6 is defined on mingw
+ guard = "defined(INET6)"
+ end
+ yield guard, name, default_value
+ }
+end
+
+def each_name(pat)
+ DEFS.each {|name, default_value|
+ next if pat !~ name
+ yield name
+ }
+end
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_const_decls")
+% each_const {|guard, name, default_value|
+#if !defined(<%=name%>)
+# if defined(HAVE_CONST_<%=name.upcase%>)
+# define <%=name%> <%=name%>
+%if default_value
+# else
+# define <%=name%> <%=default_value%>
+%end
+# endif
+#endif
+% }
+EOS
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_const_defs_in_guard(name, default_value)")
+#if defined(<%=name%>)
+ /* <%= COMMENTS[name] %> */
+ rb_define_const(rb_cSocket, <%=c_str name%>, INTEGER2NUM(<%=name%>));
+ /* <%= COMMENTS[name] %> */
+ rb_define_const(rb_mSockConst, <%=c_str name%>, INTEGER2NUM(<%=name%>));
+#endif
+EOS
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_const_defs")
+% each_const {|guard, name, default_value|
+% if guard
+#if <%=guard%>
+<%= gen_const_defs_in_guard(name, default_value).chomp %>
+#endif
+% else
+<%= gen_const_defs_in_guard(name, default_value).chomp %>
+% end
+% }
+EOS
+
+def reverse_each_name(pat)
+ DEFS.reverse_each {|name, default_value|
+ next if pat !~ name
+ yield name
+ }
+end
+
+def each_names_with_len(pat, prefix_optional=nil)
+ h = {}
+ DEFS.each {|name, default_value|
+ next if pat !~ name
+ (h[name.length] ||= []) << [name, name]
+ }
+ if prefix_optional
+ if Regexp === prefix_optional
+ prefix_pat = prefix_optional
+ else
+ prefix_pat = /\A#{Regexp.escape prefix_optional}/
+ end
+ DEFS.each {|const, default_value|
+ next if pat !~ const
+ next if prefix_pat !~ const
+ name = $'
+ (h[name.length] ||= []) << [name, const]
+ }
+ end
+ hh = {}
+ h.each {|len, pairs|
+ pairs.each {|name, const|
+ raise "name crash: #{name}" if hh[name]
+ hh[name] = true
+ }
+ }
+ h.keys.sort.each {|len|
+ yield h[len], len
+ }
+end
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int_decl(funcname, pat, prefix_optional, guard=nil)")
+%if guard
+#ifdef <%=guard%>
+int <%=funcname%>(const char *str, long len, int *valp);
+#endif
+%else
+int <%=funcname%>(const char *str, long len, int *valp);
+%end
+EOS
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int_func_in_guard(funcname, pat, prefix_optional, guard=nil)")
+int
+<%=funcname%>(const char *str, long len, int *valp)
+{
+ switch (len) {
+% each_names_with_len(pat, prefix_optional) {|pairs, len|
+ case <%=len%>:
+% pairs.each {|name, const|
+#ifdef <%=const%>
+ if (memcmp(str, <%=c_str name%>, <%=len%>) == 0) { *valp = <%=const%>; return 0; }
+#endif
+% }
+ return -1;
+
+% }
+ default:
+ return -1;
+ }
+}
+EOS
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int_func(funcname, pat, prefix_optional, guard=nil)")
+%if guard
+#ifdef <%=guard%>
+<%=gen_name_to_int_func_in_guard(funcname, pat, prefix_optional, guard)%>
+#endif
+%else
+<%=gen_name_to_int_func_in_guard(funcname, pat, prefix_optional, guard)%>
+%end
+EOS
+
+NAME_TO_INT_DEFS = []
+def def_name_to_int(funcname, pat, prefix_optional, guard=nil)
+ decl = gen_name_to_int_decl(funcname, pat, prefix_optional, guard)
+ func = gen_name_to_int_func(funcname, pat, prefix_optional, guard)
+ NAME_TO_INT_DEFS << [decl, func]
+end
+
+def reverse_each_name_with_prefix_optional(pat, prefix_pat)
+ reverse_each_name(pat) {|n|
+ yield n, n
+ }
+ if prefix_pat
+ reverse_each_name(pat) {|n|
+ next if prefix_pat !~ n
+ yield n, $'
+ }
+ end
+end
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name_hash(hash_var, pat, prefix_pat)")
+ <%=hash_var%> = st_init_numtable();
+% reverse_each_name_with_prefix_optional(pat, prefix_pat) {|n,s|
+#ifdef <%=n%>
+ st_insert(<%=hash_var%>, (st_data_t)<%=n%>, (st_data_t)rb_intern2(<%=c_str s%>, <%=s.length%>));
+#endif
+% }
+EOS
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name_func(func_name, hash_var)")
+ID
+<%=func_name%>(int val)
+{
+ st_data_t name;
+ if (st_lookup(<%=hash_var%>, (st_data_t)val, &name))
+ return (ID)name;
+ return 0;
+}
+EOS
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name_decl(func_name, hash_var)")
+ID <%=func_name%>(int val);
+EOS
+
+INTERN_DEFS = []
+def def_intern(func_name, pat, prefix_optional=nil)
+ prefix_pat = nil
+ if prefix_optional
+ if Regexp === prefix_optional
+ prefix_pat = prefix_optional
+ else
+ prefix_pat = /\A#{Regexp.escape prefix_optional}/
+ end
+ end
+ hash_var = "#{func_name}_hash"
+ vardef = "static st_table *#{hash_var};"
+ gen_hash = gen_int_to_name_hash(hash_var, pat, prefix_pat)
+ decl = gen_int_to_name_decl(func_name, hash_var)
+ func = gen_int_to_name_func(func_name, hash_var)
+ INTERN_DEFS << [vardef, gen_hash, decl, func]
+end
+
+def_name_to_int("rsock_family_to_int", /\A(AF_|PF_)/, "AF_")
+def_name_to_int("rsock_socktype_to_int", /\ASOCK_/, "SOCK_")
+def_name_to_int("rsock_ipproto_to_int", /\AIPPROTO_/, "IPPROTO_")
+def_name_to_int("rsock_unknown_level_to_int", /\ASOL_SOCKET\z/, "SOL_")
+def_name_to_int("rsock_ip_level_to_int", /\A(SOL_SOCKET\z|IPPROTO_)/, /\A(SOL_|IPPROTO_)/)
+def_name_to_int("rsock_so_optname_to_int", /\ASO_/, "SO_")
+def_name_to_int("rsock_ip_optname_to_int", /\AIP_/, "IP_")
+def_name_to_int("rsock_ipv6_optname_to_int", /\AIPV6_/, "IPV6_", "IPPROTO_IPV6")
+def_name_to_int("rsock_tcp_optname_to_int", /\ATCP_/, "TCP_")
+def_name_to_int("rsock_udp_optname_to_int", /\AUDP_/, "UDP_")
+def_name_to_int("rsock_shutdown_how_to_int", /\ASHUT_/, "SHUT_")
+def_name_to_int("rsock_scm_optname_to_int", /\ASCM_/, "SCM_")
+
+def_intern('rsock_intern_family', /\AAF_/)
+def_intern('rsock_intern_family_noprefix', /\AAF_/, "AF_")
+def_intern('rsock_intern_protocol_family', /\APF_/)
+def_intern('rsock_intern_socktype', /\ASOCK_/)
+def_intern('rsock_intern_ipproto', /\AIPPROTO_/)
+def_intern('rsock_intern_iplevel', /\A(SOL_SOCKET\z|IPPROTO_)/, /\A(SOL_|IPPROTO_)/)
+def_intern('rsock_intern_so_optname', /\ASO_/, "SO_")
+def_intern('rsock_intern_ip_optname', /\AIP_/, "IP_")
+def_intern('rsock_intern_ipv6_optname', /\AIPV6_/, "IPV6_")
+def_intern('rsock_intern_tcp_optname', /\ATCP_/, "TCP_")
+def_intern('rsock_intern_udp_optname', /\AUDP_/, "UDP_")
+def_intern('rsock_intern_scm_optname', /\ASCM_/, "SCM_")
+def_intern('rsock_intern_local_optname', /\ALOCAL_/, "LOCAL_")
+
+result = ERB.new(<<'EOS', nil, '%').result(binding)
+/* autogenerated file */
+
+<%= INTERN_DEFS.map {|vardef, gen_hash, decl, func| vardef }.join("\n") %>
+
+#ifdef HAVE_LONG_LONG
+#define INTEGER2NUM(n) \
+ (FIXNUM_MAX < (n) ? ULL2NUM(n) : \
+ FIXNUM_MIN > (LONG_LONG)(n) ? LL2NUM(n) : \
+ LONG2FIX(n))
+#else
+#define INTEGER2NUM(n) \
+ (FIXNUM_MAX < (n) ? ULONG2NUM(n) : \
+ FIXNUM_MIN > (long)(n) ? LONG2NUM(n) : \
+ LONG2FIX(n))
+#endif
+
+static void
+init_constants(void)
+{
+ /*
+ * Document-module: Socket::Constants
+ *
+ * Socket::Constants provides socket-related constants. All possible
+ * socket constants are listed in the documentation but they may not all
+ * be present on your platform.
+ *
+ * If the underlying platform doesn't define a constant the corresponding
+ * Ruby constant is not defined.
+ *
+ */
+ rb_mSockConst = rb_define_module_under(rb_cSocket, "Constants");
+
+<%= gen_const_defs %>
+<%= INTERN_DEFS.map {|vardef, gen_hash, decl, func| gen_hash }.join("\n") %>
+}
+
+<%= NAME_TO_INT_DEFS.map {|decl, func| func }.join("\n") %>
+
+<%= INTERN_DEFS.map {|vardef, gen_hash, decl, func| func }.join("\n") %>
+
+EOS
+
+header_result = ERB.new(<<'EOS', nil, '%').result(binding)
+/* autogenerated file */
+<%= gen_const_decls %>
+<%= NAME_TO_INT_DEFS.map {|decl, func| decl }.join("\n") %>
+<%= INTERN_DEFS.map {|vardef, gen_hash, decl, func| decl }.join("\n") %>
+EOS
+
+if opt_H
+ File.open(opt_H, 'w') {|f|
+ f << header_result
+ }
+else
+ result = header_result + result
+end
+
+if opt_o
+ File.open(opt_o, 'w') {|f|
+ f << result
+ }
+else
+ $stdout << result
+end
+
+__END__
+
+SOCK_STREAM nil A stream socket provides a sequenced, reliable two-way connection for a byte stream
+SOCK_DGRAM nil A datagram socket provides connectionless, unreliable messaging
+SOCK_RAW nil A raw socket provides low-level access for direct access or implementing network protocols
+SOCK_RDM nil A reliable datagram socket provides reliable delivery of messages
+SOCK_SEQPACKET nil A sequential packet socket provides sequenced, reliable two-way connection for datagrams
+SOCK_PACKET nil Device-level packet access
+
+AF_UNSPEC nil Unspecified protocol, any supported address family
+PF_UNSPEC nil Unspecified protocol, any supported address family
+AF_INET nil IPv4 protocol
+PF_INET nil IPv4 protocol
+AF_INET6 nil IPv6 protocol
+PF_INET6 nil IPv6 protocol
+AF_UNIX nil UNIX sockets
+PF_UNIX nil UNIX sockets
+AF_AX25 nil AX.25 protocol
+PF_AX25 nil AX.25 protocol
+AF_IPX nil IPX protocol
+PF_IPX nil IPX protocol
+AF_APPLETALK nil AppleTalk protocol
+PF_APPLETALK nil AppleTalk protocol
+AF_LOCAL nil Host-internal protocols
+PF_LOCAL nil Host-internal protocols
+AF_IMPLINK nil ARPANET IMP protocol
+PF_IMPLINK nil ARPANET IMP protocol
+AF_PUP nil PARC Universal Packet protocol
+PF_PUP nil PARC Universal Packet protocol
+AF_CHAOS nil MIT CHAOS protocols
+PF_CHAOS nil MIT CHAOS protocols
+AF_NS nil XEROX NS protocols
+PF_NS nil XEROX NS protocols
+AF_ISO nil ISO Open Systems Interconnection protocols
+PF_ISO nil ISO Open Systems Interconnection protocols
+AF_OSI nil ISO Open Systems Interconnection protocols
+PF_OSI nil ISO Open Systems Interconnection protocols
+AF_ECMA nil European Computer Manufacturers protocols
+PF_ECMA nil European Computer Manufacturers protocols
+AF_DATAKIT nil Datakit protocol
+PF_DATAKIT nil Datakit protocol
+AF_CCITT nil CCITT (now ITU-T) protocols
+PF_CCITT nil CCITT (now ITU-T) protocols
+AF_SNA nil IBM SNA protocol
+PF_SNA nil IBM SNA protocol
+AF_DEC nil DECnet protocol
+PF_DEC nil DECnet protocol
+AF_DLI nil DEC Direct Data Link Interface protocol
+PF_DLI nil DEC Direct Data Link Interface protocol
+AF_LAT nil Local Area Transport protocol
+PF_LAT nil Local Area Transport protocol
+AF_HYLINK nil NSC Hyperchannel protocol
+PF_HYLINK nil NSC Hyperchannel protocol
+AF_ROUTE nil Internal routing protocol
+PF_ROUTE nil Internal routing protocol
+AF_LINK nil Link layer interface
+PF_LINK nil Link layer interface
+AF_COIP nil Connection-oriented IP
+PF_COIP nil Connection-oriented IP
+AF_CNT nil Computer Network Technology
+PF_CNT nil Computer Network Technology
+AF_SIP nil Simple Internet Protocol
+PF_SIP nil Simple Internet Protocol
+AF_NDRV nil Network driver raw access
+PF_NDRV nil Network driver raw access
+AF_ISDN nil Integrated Services Digital Network
+PF_ISDN nil Integrated Services Digital Network
+AF_NATM nil Native ATM access
+PF_NATM nil Native ATM access
+AF_SYSTEM
+PF_SYSTEM
+AF_NETBIOS nil NetBIOS
+PF_NETBIOS nil NetBIOS
+AF_PPP nil Point-to-Point Protocol
+PF_PPP nil Point-to-Point Protocol
+AF_ATM nil Asynchronous Transfer Mode
+PF_ATM nil Asynchronous Transfer Mode
+AF_NETGRAPH nil Netgraph sockets
+PF_NETGRAPH nil Netgraph sockets
+AF_MAX nil Maximum address family for this platform
+PF_MAX nil Maximum address family for this platform
+AF_PACKET nil Direct link-layer access
+PF_PACKET nil Direct link-layer access
+
+AF_E164 nil CCITT (ITU-T) E.164 recommendation
+PF_XTP nil eXpress Transfer Protocol
+PF_RTIP
+PF_PIP
+PF_KEY
+
+MSG_OOB nil Process out-of-band data
+MSG_PEEK nil Peek at incoming message
+MSG_DONTROUTE nil Send without using the routing tables
+MSG_EOR nil Data completes record
+MSG_TRUNC nil Data discarded before delivery
+MSG_CTRUNC nil Control data lost before delivery
+MSG_WAITALL nil Wait for full request or error
+MSG_DONTWAIT nil This message should be non-blocking
+MSG_EOF nil Data completes connection
+MSG_FLUSH nil Start of a hold sequence. Dumps to so_temp
+MSG_HOLD nil Hold fragment in so_temp
+MSG_SEND nil Send the packet in so_temp
+MSG_HAVEMORE nil Data ready to be read
+MSG_RCVMORE nil Data remains in the current packet
+MSG_COMPAT nil End of record
+MSG_PROXY nil Wait for full request
+MSG_FIN
+MSG_SYN
+MSG_CONFIRM nil Confirm path validity
+MSG_RST
+MSG_ERRQUEUE nil Fetch message from error queue
+MSG_NOSIGNAL nil Do not generate SIGPIPE
+MSG_MORE nil Sender will send more
+MSG_FASTOPEN nil Reduce step of the handshake process
+
+SOL_SOCKET nil Socket-level options
+SOL_IP nil IP socket options
+SOL_IPX nil IPX socket options
+SOL_AX25 nil AX.25 socket options
+SOL_ATALK nil AppleTalk socket options
+SOL_TCP nil TCP socket options
+SOL_UDP nil UDP socket options
+
+IPPROTO_IP 0 Dummy protocol for IP
+IPPROTO_ICMP 1 Control message protocol
+IPPROTO_IGMP nil Group Management Protocol
+IPPROTO_GGP nil Gateway to Gateway Protocol
+IPPROTO_TCP 6 TCP
+IPPROTO_EGP nil Exterior Gateway Protocol
+IPPROTO_PUP nil PARC Universal Packet protocol
+IPPROTO_UDP 17 UDP
+IPPROTO_IDP nil XNS IDP
+IPPROTO_HELLO nil "hello" routing protocol
+IPPROTO_ND nil Sun net disk protocol
+IPPROTO_TP nil ISO transport protocol class 4
+IPPROTO_XTP nil Xpress Transport Protocol
+IPPROTO_EON nil ISO cnlp
+IPPROTO_BIP
+IPPROTO_AH nil IP6 auth header
+IPPROTO_DSTOPTS nil IP6 destination option
+IPPROTO_ESP nil IP6 Encapsulated Security Payload
+IPPROTO_FRAGMENT nil IP6 fragmentation header
+IPPROTO_HOPOPTS nil IP6 hop-by-hop options
+IPPROTO_ICMPV6 nil ICMP6
+IPPROTO_IPV6 nil IP6 header
+IPPROTO_NONE nil IP6 no next header
+IPPROTO_ROUTING nil IP6 routing header
+
+IPPROTO_RAW 255 Raw IP packet
+IPPROTO_MAX nil Maximum IPPROTO constant
+
+# Some port configuration
+IPPORT_RESERVED 1024 Default minimum address for bind or connect
+IPPORT_USERRESERVED 5000 Default maximum address for bind or connect
+
+# Some reserved IP v.4 addresses
+INADDR_ANY 0x00000000 A socket bound to INADDR_ANY receives packets from all interfaces and sends from the default IP address
+INADDR_BROADCAST 0xffffffff The network broadcast address
+INADDR_LOOPBACK 0x7F000001 The loopback address
+INADDR_UNSPEC_GROUP 0xe0000000 The reserved multicast group
+INADDR_ALLHOSTS_GROUP 0xe0000001 Multicast group for all systems on this subset
+INADDR_MAX_LOCAL_GROUP 0xe00000ff The last local network multicast group
+INADDR_NONE 0xffffffff A bitmask for matching no valid IP address
+
+# IP [gs]etsockopt options
+IP_OPTIONS nil IP options to be included in packets
+IP_HDRINCL nil Header is included with data
+IP_TOS nil IP type-of-service
+IP_TTL nil IP time-to-live
+IP_RECVOPTS nil Receive all IP options with datagram
+IP_RECVRETOPTS nil Receive all IP options for response
+IP_RECVDSTADDR nil Receive IP destination address with datagram
+IP_RETOPTS nil IP options to be included in datagrams
+IP_MINTTL nil Minimum TTL allowed for received packets
+IP_DONTFRAG nil Don't fragment packets
+IP_SENDSRCADDR nil Source address for outgoing UDP datagrams
+IP_ONESBCAST nil Force outgoing broadcast datagrams to have the undirected broadcast address
+IP_RECVTTL nil Receive IP TTL with datagrams
+IP_RECVIF nil Receive interface information with datagrams
+IP_RECVSLLA nil Receive link-layer address with datagrams
+IP_PORTRANGE nil Set the port range for sockets with unspecified port numbers
+IP_MULTICAST_IF nil IP multicast interface
+IP_MULTICAST_TTL nil IP multicast TTL
+IP_MULTICAST_LOOP nil IP multicast loopback
+IP_ADD_MEMBERSHIP nil Add a multicast group membership
+IP_DROP_MEMBERSHIP nil Drop a multicast group membership
+IP_DEFAULT_MULTICAST_TTL nil Default multicast TTL
+IP_DEFAULT_MULTICAST_LOOP nil Default multicast loopback
+IP_MAX_MEMBERSHIPS nil Maximum number multicast groups a socket can join
+IP_ROUTER_ALERT nil Notify transit routers to more closely examine the contents of an IP packet
+IP_PKTINFO nil Receive packet information with datagrams
+IP_PKTOPTIONS nil Receive packet options with datagrams
+IP_MTU_DISCOVER nil Path MTU discovery
+IP_RECVERR nil Enable extended reliable error message passing
+IP_RECVTOS nil Receive TOS with incoming packets
+IP_MTU nil The Maximum Transmission Unit of the socket
+IP_FREEBIND nil Allow binding to nonexistent IP addresses
+IP_IPSEC_POLICY nil IPsec security policy
+IP_XFRM_POLICY
+IP_PASSSEC nil Retrieve security context with datagram
+IP_TRANSPARENT nil Transparent proxy
+IP_PMTUDISC_DONT nil Never send DF frames
+IP_PMTUDISC_WANT nil Use per-route hints
+IP_PMTUDISC_DO nil Always send DF frames
+IP_UNBLOCK_SOURCE nil Unblock IPv4 multicast packets with a give source address
+IP_BLOCK_SOURCE nil Block IPv4 multicast packets with a give source address
+IP_ADD_SOURCE_MEMBERSHIP nil Add a multicast group membership
+IP_DROP_SOURCE_MEMBERSHIP nil Drop a multicast group membership
+IP_MSFILTER nil Multicast source filtering
+
+MCAST_JOIN_GROUP nil Join a multicast group
+MCAST_BLOCK_SOURCE nil Block multicast packets from this source
+MCAST_UNBLOCK_SOURCE nil Unblock multicast packets from this source
+MCAST_LEAVE_GROUP nil Leave a multicast group
+MCAST_JOIN_SOURCE_GROUP nil Join a multicast source group
+MCAST_LEAVE_SOURCE_GROUP nil Leave a multicast source group
+MCAST_MSFILTER nil Multicast source filtering
+MCAST_EXCLUDE nil Exclusive multicast source filter
+MCAST_INCLUDE nil Inclusive multicast source filter
+
+SO_DEBUG nil Debug info recording
+SO_REUSEADDR nil Allow local address reuse
+SO_REUSEPORT nil Allow local address and port reuse
+SO_TYPE nil Get the socket type
+SO_ERROR nil Get and clear the error status
+SO_DONTROUTE nil Use interface addresses
+SO_BROADCAST nil Permit sending of broadcast messages
+SO_SNDBUF nil Send buffer size
+SO_RCVBUF nil Receive buffer size
+SO_SNDBUFFORCE nil Send buffer size without wmem_max limit (Linux 2.6.14)
+SO_RCVBUFFORCE nil Receive buffer size without rmem_max limit (Linux 2.6.14)
+SO_KEEPALIVE nil Keep connections alive
+SO_OOBINLINE nil Leave received out-of-band data in-line
+SO_NO_CHECK nil Disable checksums
+SO_PRIORITY nil The protocol-defined priority for all packets on this socket
+SO_LINGER nil Linger on close if data is present
+SO_PASSCRED nil Receive SCM_CREDENTIALS messages
+SO_PEERCRED nil The credentials of the foreign process connected to this socket
+SO_RCVLOWAT nil Receive low-water mark
+SO_SNDLOWAT nil Send low-water mark
+SO_RCVTIMEO nil Receive timeout
+SO_SNDTIMEO nil Send timeout
+SO_ACCEPTCONN nil Socket has had listen() called on it
+SO_USELOOPBACK nil Bypass hardware when possible
+SO_ACCEPTFILTER nil There is an accept filter
+SO_DONTTRUNC nil Retain unread data
+SO_WANTMORE nil Give a hint when more data is ready
+SO_WANTOOBFLAG nil OOB data is wanted in MSG_FLAG on receive
+SO_NREAD nil Get first packet byte count
+SO_NKE nil Install socket-level Network Kernel Extension
+SO_NOSIGPIPE nil Don't SIGPIPE on EPIPE
+SO_SECURITY_AUTHENTICATION
+SO_SECURITY_ENCRYPTION_TRANSPORT
+SO_SECURITY_ENCRYPTION_NETWORK
+SO_BINDTODEVICE nil Only send packets from the given interface
+SO_ATTACH_FILTER nil Attach an accept filter
+SO_DETACH_FILTER nil Detach an accept filter
+SO_GET_FILTER nil Obtain filter set by SO_ATTACH_FILTER (Linux 3.8)
+SO_PEERNAME nil Name of the connecting user
+SO_TIMESTAMP nil Receive timestamp with datagrams (timeval)
+SO_TIMESTAMPNS nil Receive nanosecond timestamp with datagrams (timespec)
+SO_BINTIME nil Receive timestamp with datagrams (bintime)
+SO_RECVUCRED nil Receive user credentials with datagram
+SO_MAC_EXEMPT nil Mandatory Access Control exemption for unlabeled peers
+SO_ALLZONES nil Bypass zone boundaries
+SO_PEERSEC nil Obtain the security credentials (Linux 2.6.2)
+SO_PASSSEC nil Toggle security context passing (Linux 2.6.18)
+SO_MARK nil Set the mark for mark-based routing (Linux 2.6.25)
+SO_TIMESTAMPING nil Time stamping of incoming and outgoing packets (Linux 2.6.30)
+SO_PROTOCOL nil Protocol given for socket() (Linux 2.6.32)
+SO_DOMAIN nil Domain given for socket() (Linux 2.6.32)
+SO_RXQ_OVFL nil Toggle cmsg for number of packets dropped (Linux 2.6.33)
+SO_WIFI_STATUS nil Toggle cmsg for wifi status (Linux 3.3)
+SO_PEEK_OFF nil Set the peek offset (Linux 3.4)
+SO_NOFCS nil Set netns of a socket (Linux 3.4)
+SO_LOCK_FILTER nil Lock the filter attached to a socket (Linux 3.9)
+SO_SELECT_ERR_QUEUE nil Make select() detect socket error queue with errorfds (Linux 3.10)
+SO_BUSY_POLL nil Set the threshold in microseconds for low latency polling (Linux 3.11)
+SO_MAX_PACING_RATE nil Cap the rate computed by transport layer. [bytes per second] (Linux 3.13)
+SO_BPF_EXTENSIONS nil Query supported BPF extensions (Linux 3.14)
+
+SOPRI_INTERACTIVE nil Interactive socket priority
+SOPRI_NORMAL nil Normal socket priority
+SOPRI_BACKGROUND nil Background socket priority
+
+IPX_TYPE
+
+TCP_NODELAY nil Don't delay sending to coalesce packets
+TCP_MAXSEG nil Set maximum segment size
+TCP_CORK nil Don't send partial frames (Linux 2.2, glibc 2.2)
+TCP_DEFER_ACCEPT nil Don't notify a listening socket until data is ready (Linux 2.4, glibc 2.2)
+TCP_INFO nil Retrieve information about this socket (Linux 2.4, glibc 2.2)
+TCP_KEEPCNT nil Maximum number of keepalive probes allowed before dropping a connection (Linux 2.4, glibc 2.2)
+TCP_KEEPIDLE nil Idle time before keepalive probes are sent (Linux 2.4, glibc 2.2)
+TCP_KEEPINTVL nil Time between keepalive probes (Linux 2.4, glibc 2.2)
+TCP_LINGER2 nil Lifetime of orphaned FIN_WAIT2 sockets (Linux 2.4, glibc 2.2)
+TCP_MD5SIG nil Use MD5 digests (RFC2385, Linux 2.6.20, glibc 2.7)
+TCP_NOOPT nil Don't use TCP options
+TCP_NOPUSH nil Don't push the last block of write
+TCP_QUICKACK nil Enable quickack mode (Linux 2.4.4, glibc 2.3)
+TCP_SYNCNT nil Number of SYN retransmits before a connection is dropped (Linux 2.4, glibc 2.2)
+TCP_WINDOW_CLAMP nil Clamp the size of the advertised window (Linux 2.4, glibc 2.2)
+TCP_FASTOPEN nil Reduce step of the handshake process (Linux 3.7, glibc 2.18)
+TCP_CONGESTION nil TCP congestion control algorithm (Linux 2.6.13, glibc 2.6)
+TCP_COOKIE_TRANSACTIONS nil TCP Cookie Transactions (Linux 2.6.33, glibc 2.18)
+TCP_QUEUE_SEQ nil Sequence of a queue for repair mode (Linux 3.5, glibc 2.18)
+TCP_REPAIR nil Repair mode (Linux 3.5, glibc 2.18)
+TCP_REPAIR_OPTIONS nil Options for repair mode (Linux 3.5, glibc 2.18)
+TCP_REPAIR_QUEUE nil Queue for repair mode (Linux 3.5, glibc 2.18)
+TCP_THIN_DUPACK nil Duplicated acknowledgments handling for thin-streams (Linux 2.6.34, glibc 2.18)
+TCP_THIN_LINEAR_TIMEOUTS nil Linear timeouts for thin-streams (Linux 2.6.34, glibc 2.18)
+TCP_TIMESTAMP nil TCP timestamp (Linux 3.9, glibc 2.18)
+TCP_USER_TIMEOUT nil Max timeout before a TCP connection is aborted (Linux 2.6.37, glibc 2.18)
+
+UDP_CORK nil Don't send partial frames (Linux 2.5.44, glibc 2.11)
+
+EAI_ADDRFAMILY nil Address family for hostname not supported
+EAI_AGAIN nil Temporary failure in name resolution
+EAI_BADFLAGS nil Invalid flags
+EAI_FAIL nil Non-recoverable failure in name resolution
+EAI_FAMILY nil Address family not supported
+EAI_MEMORY nil Memory allocation failure
+EAI_NODATA nil No address associated with hostname
+EAI_NONAME nil Hostname nor servname, or not known
+EAI_OVERFLOW nil Argument buffer overflow
+EAI_SERVICE nil Servname not supported for socket type
+EAI_SOCKTYPE nil Socket type not supported
+EAI_SYSTEM nil System error returned in errno
+EAI_BADHINTS nil Invalid value for hints
+EAI_PROTOCOL nil Resolved protocol is unknown
+EAI_MAX nil Maximum error code from getaddrinfo
+
+AI_PASSIVE nil Get address to use with bind()
+AI_CANONNAME nil Fill in the canonical name
+AI_NUMERICHOST nil Prevent host name resolution
+AI_NUMERICSERV nil Prevent service name resolution
+AI_MASK nil Valid flag mask for getaddrinfo (not for application use)
+AI_ALL nil Allow all addresses
+AI_V4MAPPED_CFG nil Accept IPv4 mapped addresses if the kernel supports it
+AI_ADDRCONFIG nil Accept only if any address is assigned
+AI_V4MAPPED nil Accept IPv4-mapped IPv6 addresses
+AI_DEFAULT nil Default flags for getaddrinfo
+
+NI_MAXHOST nil Maximum length of a hostname
+NI_MAXSERV nil Maximum length of a service name
+NI_NOFQDN nil An FQDN is not required for local hosts, return only the local part
+NI_NUMERICHOST nil Return a numeric address
+NI_NAMEREQD nil A name is required
+NI_NUMERICSERV nil Return the service name as a digit string
+NI_DGRAM nil The service specified is a datagram service (looks up UDP ports)
+
+SHUT_RD 0 Shut down the reading side of the socket
+SHUT_WR 1 Shut down the writing side of the socket
+SHUT_RDWR 2 Shut down the both sides of the socket
+
+IPV6_JOIN_GROUP nil Join a group membership
+IPV6_LEAVE_GROUP nil Leave a group membership
+IPV6_MULTICAST_HOPS nil IP6 multicast hops
+IPV6_MULTICAST_IF nil IP6 multicast interface
+IPV6_MULTICAST_LOOP nil IP6 multicast loopback
+IPV6_UNICAST_HOPS nil IP6 unicast hops
+IPV6_V6ONLY nil Only bind IPv6 with a wildcard bind
+IPV6_CHECKSUM nil Checksum offset for raw sockets
+IPV6_DONTFRAG nil Don't fragment packets
+IPV6_DSTOPTS nil Destination option
+IPV6_HOPLIMIT nil Hop limit
+IPV6_HOPOPTS nil Hop-by-hop option
+IPV6_NEXTHOP nil Next hop address
+IPV6_PATHMTU nil Retrieve current path MTU
+IPV6_PKTINFO nil Receive packet information with datagram
+IPV6_RECVDSTOPTS nil Receive all IP6 options for response
+IPV6_RECVHOPLIMIT nil Receive hop limit with datagram
+IPV6_RECVHOPOPTS nil Receive hop-by-hop options
+IPV6_RECVPKTINFO nil Receive destination IP address and incoming interface
+IPV6_RECVRTHDR nil Receive routing header
+IPV6_RECVTCLASS nil Receive traffic class
+IPV6_RTHDR nil Allows removal of sticky routing headers
+IPV6_RTHDRDSTOPTS nil Allows removal of sticky destination options header
+IPV6_RTHDR_TYPE_0 nil Routing header type 0
+IPV6_RECVPATHMTU nil Receive current path MTU with datagram
+IPV6_TCLASS nil Specify the traffic class
+IPV6_USE_MIN_MTU nil Use the minimum MTU size
+
+INET_ADDRSTRLEN 16 Maximum length of an IPv4 address string
+INET6_ADDRSTRLEN 46 Maximum length of an IPv6 address string
+IFNAMSIZ nil Maximum interface name size
+IF_NAMESIZE nil Maximum interface name size
+
+SOMAXCONN 5 Maximum connection requests that may be queued for a socket
+
+SCM_RIGHTS nil Access rights
+SCM_TIMESTAMP nil Timestamp (timeval)
+SCM_TIMESTAMPNS nil Timespec (timespec)
+SCM_TIMESTAMPING nil Timestamp (timespec list) (Linux 2.6.30)
+SCM_BINTIME nil Timestamp (bintime)
+SCM_CREDENTIALS nil The sender's credentials
+SCM_CREDS nil Process credentials
+SCM_UCRED nil User credentials
+SCM_WIFI_STATUS nil Wifi status (Linux 3.3)
+
+LOCAL_PEERCRED nil Retrieve peer credentials
+LOCAL_CREDS nil Pass credentials to receiver
+LOCAL_CONNWAIT nil Connect blocks until accepted
+
+IFF_802_1Q_VLAN nil 802.1Q VLAN device
+IFF_ALLMULTI nil receive all multicast packets
+IFF_ALTPHYS nil use alternate physical connection
+IFF_AUTOMEDIA nil auto media select active
+IFF_BONDING nil bonding master or slave
+IFF_BRIDGE_PORT nil device used as bridge port
+IFF_BROADCAST nil broadcast address valid
+IFF_CANTCONFIG nil unconfigurable using ioctl(2)
+IFF_DEBUG nil turn on debugging
+IFF_DISABLE_NETPOLL nil disable netpoll at run-time
+IFF_DONT_BRIDGE nil disallow bridging this ether dev
+IFF_DORMANT nil driver signals dormant
+IFF_DRV_OACTIVE nil tx hardware queue is full
+IFF_DRV_RUNNING nil resources allocated
+IFF_DYING nil interface is winding down
+IFF_DYNAMIC nil dialup device with changing addresses
+IFF_EBRIDGE nil ethernet bridging device
+IFF_ECHO nil echo sent packets
+IFF_ISATAP nil ISATAP interface (RFC4214)
+IFF_LINK0 nil per link layer defined bit 0
+IFF_LINK1 nil per link layer defined bit 1
+IFF_LINK2 nil per link layer defined bit 2
+IFF_LIVE_ADDR_CHANGE nil hardware address change when it's running
+IFF_LOOPBACK nil loopback net
+IFF_LOWER_UP nil driver signals L1 up
+IFF_MACVLAN_PORT nil device used as macvlan port
+IFF_MASTER nil master of a load balancer
+IFF_MASTER_8023AD nil bonding master, 802.3ad.
+IFF_MASTER_ALB nil bonding master, balance-alb.
+IFF_MASTER_ARPMON nil bonding master, ARP mon in use
+IFF_MONITOR nil user-requested monitor mode
+IFF_MULTICAST nil supports multicast
+IFF_NOARP nil no address resolution protocol
+IFF_NOTRAILERS nil avoid use of trailers
+IFF_OACTIVE nil transmission in progress
+IFF_OVS_DATAPATH nil device used as Open vSwitch datapath port
+IFF_POINTOPOINT nil point-to-point link
+IFF_PORTSEL nil can set media type
+IFF_PPROMISC nil user-requested promisc mode
+IFF_PROMISC nil receive all packets
+IFF_RENAMING nil interface is being renamed
+IFF_ROUTE nil routing entry installed
+IFF_RUNNING nil resources allocated
+IFF_SIMPLEX nil can't hear own transmissions
+IFF_SLAVE nil slave of a load balancer
+IFF_SLAVE_INACTIVE nil bonding slave not the curr. active
+IFF_SLAVE_NEEDARP nil need ARPs for validation
+IFF_SMART nil interface manages own routes
+IFF_STATICARP nil static ARP
+IFF_SUPP_NOFCS nil sending custom FCS
+IFF_TEAM_PORT nil used as team port
+IFF_TX_SKB_SHARING nil sharing skbs on transmit
+IFF_UNICAST_FLT nil unicast filtering
+IFF_UP nil interface is up
+IFF_WAN_HDLC nil WAN HDLC device
+IFF_XMIT_DST_RELEASE nil dev_hard_start_xmit() is allowed to release skb->dst
+IFF_VOLATILE nil volatile flags
+IFF_CANTCHANGE nil flags not changeable
diff --git a/jni/ruby/ext/socket/option.c b/jni/ruby/ext/socket/option.c
new file mode 100644
index 0000000..b00ffb9
--- /dev/null
+++ b/jni/ruby/ext/socket/option.c
@@ -0,0 +1,1473 @@
+#include "rubysocket.h"
+
+VALUE rb_cSockOpt;
+
+#define pack_var(v) rb_str_new((const char *)&(v), sizeof(v))
+
+#define CAT(x,y) x##y
+#define XCAT(x,y) CAT(x,y)
+
+#if defined(__linux__) || \
+ defined(__GNU__) /* GNU/Hurd */ || \
+ defined(__FreeBSD__) || \
+ defined(__DragonFly__) || \
+ defined(__APPLE__) || \
+ defined(_WIN32) || \
+ defined(__CYGWIN__)
+# define TYPE_IP_MULTICAST_LOOP int
+# define TYPE_IP_MULTICAST_TTL int
+#else
+/* The original IP multicast implementation by Steve Deering
+ * NetBSD
+ * OpenBSD
+ * SunOS
+ */
+# define TYPE_IP_MULTICAST_LOOP byte
+# define TYPE_IP_MULTICAST_TTL byte
+# define USE_INSPECT_BYTE 1
+#endif
+
+static VALUE
+sockopt_pack_byte(VALUE value)
+{
+ char i = NUM2CHR(rb_to_int(value));
+ return pack_var(i);
+}
+
+static VALUE
+sockopt_pack_int(VALUE value)
+{
+ int i = NUM2INT(rb_to_int(value));
+ return pack_var(i);
+}
+
+static VALUE
+constant_to_sym(int constant, ID (*intern_const)(int))
+{
+ ID name = intern_const(constant);
+ if (name) {
+ return ID2SYM(name);
+ }
+
+ return INT2NUM(constant);
+}
+
+static VALUE
+optname_to_sym(int level, int optname)
+{
+ switch (level) {
+ case SOL_SOCKET:
+ return constant_to_sym(optname, rsock_intern_so_optname);
+ case IPPROTO_IP:
+ return constant_to_sym(optname, rsock_intern_ip_optname);
+#ifdef IPPROTO_IPV6
+ case IPPROTO_IPV6:
+ return constant_to_sym(optname, rsock_intern_ipv6_optname);
+#endif
+ case IPPROTO_TCP:
+ return constant_to_sym(optname, rsock_intern_tcp_optname);
+ case IPPROTO_UDP:
+ return constant_to_sym(optname, rsock_intern_udp_optname);
+ default:
+ return INT2NUM(optname);
+ }
+}
+
+/*
+ * call-seq:
+ * Socket::Option.new(family, level, optname, data) => sockopt
+ *
+ * Returns a new Socket::Option object.
+ *
+ * sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i"))
+ * p sockopt #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
+ *
+ */
+static VALUE
+sockopt_initialize(VALUE self, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE data)
+{
+ int family = rsock_family_arg(vfamily);
+ int level = rsock_level_arg(family, vlevel);
+ int optname = rsock_optname_arg(family, level, voptname);
+ StringValue(data);
+ rb_ivar_set(self, rb_intern("family"), INT2NUM(family));
+ rb_ivar_set(self, rb_intern("level"), INT2NUM(level));
+ rb_ivar_set(self, rb_intern("optname"), INT2NUM(optname));
+ rb_ivar_set(self, rb_intern("data"), data);
+ return self;
+}
+
+VALUE
+rsock_sockopt_new(int family, int level, int optname, VALUE data)
+{
+ NEWOBJ_OF(obj, struct RObject, rb_cSockOpt, T_OBJECT);
+ StringValue(data);
+ sockopt_initialize((VALUE)obj, INT2NUM(family), INT2NUM(level), INT2NUM(optname), data);
+ return (VALUE)obj;
+}
+
+/*
+ * call-seq:
+ * sockopt.family => integer
+ *
+ * returns the socket family as an integer.
+ *
+ * p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).family
+ * #=> 10
+ */
+static VALUE
+sockopt_family_m(VALUE self)
+{
+ return rb_attr_get(self, rb_intern("family"));
+}
+
+static int
+sockopt_level(VALUE self)
+{
+ return NUM2INT(rb_attr_get(self, rb_intern("level")));
+}
+
+/*
+ * call-seq:
+ * sockopt.level => integer
+ *
+ * returns the socket level as an integer.
+ *
+ * p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).level
+ * #=> 41
+ */
+static VALUE
+sockopt_level_m(VALUE self)
+{
+ return INT2NUM(sockopt_level(self));
+}
+
+static int
+sockopt_optname(VALUE self)
+{
+ return NUM2INT(rb_attr_get(self, rb_intern("optname")));
+}
+
+/*
+ * call-seq:
+ * sockopt.optname => integer
+ *
+ * returns the socket option name as an integer.
+ *
+ * p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).optname
+ * #=> 2
+ */
+static VALUE
+sockopt_optname_m(VALUE self)
+{
+ return INT2NUM(sockopt_optname(self));
+}
+
+/*
+ * call-seq:
+ * sockopt.data => string
+ * sockopt.to_s => string
+ *
+ * returns the socket option data as a string.
+ *
+ * p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).data
+ * #=> "\x01\x00\x00\x00"
+ */
+static VALUE
+sockopt_data(VALUE self)
+{
+ VALUE v = rb_attr_get(self, rb_intern("data"));
+ StringValue(v);
+ return v;
+}
+
+/*
+ * call-seq:
+ * Socket::Option.byte(family, level, optname, integer) => sockopt
+ *
+ * Creates a new Socket::Option object which contains a byte as data.
+ *
+ * p Socket::Option.byte(:INET, :SOCKET, :KEEPALIVE, 1)
+ * #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
+ */
+static VALUE
+sockopt_s_byte(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE vint)
+{
+ int family = rsock_family_arg(vfamily);
+ int level = rsock_level_arg(family, vlevel);
+ int optname = rsock_optname_arg(family, level, voptname);
+ return rsock_sockopt_new(family, level, optname, sockopt_pack_byte(vint));
+}
+
+/*
+ * call-seq:
+ * sockopt.byte => integer
+ *
+ * Returns the data in _sockopt_ as an byte.
+ *
+ * sockopt = Socket::Option.byte(:INET, :SOCKET, :KEEPALIVE, 1)
+ * p sockopt.byte => 1
+ */
+static VALUE
+sockopt_byte(VALUE self)
+{
+ unsigned char i;
+ VALUE data = sockopt_data(self);
+ StringValue(data);
+ if (RSTRING_LEN(data) != sizeof(i))
+ rb_raise(rb_eTypeError, "size differ. expected as sizeof(int)=%d but %ld",
+ (int)sizeof(i), (long)RSTRING_LEN(data));
+ return CHR2FIX(*RSTRING_PTR(data));
+}
+
+/*
+ * call-seq:
+ * Socket::Option.int(family, level, optname, integer) => sockopt
+ *
+ * Creates a new Socket::Option object which contains an int as data.
+ *
+ * The size and endian is dependent on the platform.
+ *
+ * p Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
+ * #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
+ */
+static VALUE
+sockopt_s_int(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE vint)
+{
+ int family = rsock_family_arg(vfamily);
+ int level = rsock_level_arg(family, vlevel);
+ int optname = rsock_optname_arg(family, level, voptname);
+ return rsock_sockopt_new(family, level, optname, sockopt_pack_int(vint));
+}
+
+/*
+ * call-seq:
+ * sockopt.int => integer
+ *
+ * Returns the data in _sockopt_ as an int.
+ *
+ * The size and endian is dependent on the platform.
+ *
+ * sockopt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
+ * p sockopt.int => 1
+ */
+static VALUE
+sockopt_int(VALUE self)
+{
+ int i;
+ VALUE data = sockopt_data(self);
+ StringValue(data);
+ if (RSTRING_LEN(data) != sizeof(int))
+ rb_raise(rb_eTypeError, "size differ. expected as sizeof(int)=%d but %ld",
+ (int)sizeof(int), (long)RSTRING_LEN(data));
+ memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
+ return INT2NUM(i);
+}
+
+/*
+ * call-seq:
+ * Socket::Option.bool(family, level, optname, bool) => sockopt
+ *
+ * Creates a new Socket::Option object which contains boolean as data.
+ * Actually 0 or 1 as int is used.
+ *
+ * p Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true)
+ * #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
+ *
+ * p Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false)
+ * #=> #<Socket::Option: AF_INET SOCKET KEEPALIVE 0>
+ *
+ */
+static VALUE
+sockopt_s_bool(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE vbool)
+{
+ int family = rsock_family_arg(vfamily);
+ int level = rsock_level_arg(family, vlevel);
+ int optname = rsock_optname_arg(family, level, voptname);
+ int i = RTEST(vbool) ? 1 : 0;
+ return rsock_sockopt_new(family, level, optname, pack_var(i));
+}
+
+/*
+ * call-seq:
+ * sockopt.bool => true or false
+ *
+ * Returns the data in _sockopt_ as an boolean value.
+ *
+ * sockopt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
+ * p sockopt.bool => true
+ */
+static VALUE
+sockopt_bool(VALUE self)
+{
+ int i;
+ VALUE data = sockopt_data(self);
+ StringValue(data);
+ if (RSTRING_LEN(data) != sizeof(int))
+ rb_raise(rb_eTypeError, "size differ. expected as sizeof(int)=%d but %ld",
+ (int)sizeof(int), (long)RSTRING_LEN(data));
+ memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
+ return i == 0 ? Qfalse : Qtrue;
+}
+
+/*
+ * call-seq:
+ * Socket::Option.linger(onoff, secs) => sockopt
+ *
+ * Creates a new Socket::Option object for SOL_SOCKET/SO_LINGER.
+ *
+ * _onoff_ should be an integer or a boolean.
+ *
+ * _secs_ should be the number of seconds.
+ *
+ * p Socket::Option.linger(true, 10)
+ * #=> #<Socket::Option: UNSPEC SOCKET LINGER on 10sec>
+ *
+ */
+static VALUE
+sockopt_s_linger(VALUE klass, VALUE vonoff, VALUE vsecs)
+{
+ VALUE tmp;
+ struct linger l;
+ memset(&l, 0, sizeof(l));
+ if (!NIL_P(tmp = rb_check_to_integer(vonoff, "to_int")))
+ l.l_onoff = NUM2INT(tmp);
+ else
+ l.l_onoff = RTEST(vonoff) ? 1 : 0;
+ l.l_linger = NUM2INT(vsecs);
+ return rsock_sockopt_new(AF_UNSPEC, SOL_SOCKET, SO_LINGER, pack_var(l));
+}
+
+/*
+ * call-seq:
+ * sockopt.linger => [bool, seconds]
+ *
+ * Returns the linger data in _sockopt_ as a pair of boolean and integer.
+ *
+ * sockopt = Socket::Option.linger(true, 10)
+ * p sockopt.linger => [true, 10]
+ */
+static VALUE
+sockopt_linger(VALUE self)
+{
+ int level = sockopt_level(self);
+ int optname = sockopt_optname(self);
+ VALUE data = sockopt_data(self);
+ struct linger l;
+ VALUE vonoff, vsecs;
+
+ if (level != SOL_SOCKET || optname != SO_LINGER)
+ rb_raise(rb_eTypeError, "linger socket option expected");
+ if (RSTRING_LEN(data) != sizeof(l))
+ rb_raise(rb_eTypeError, "size differ. expected as sizeof(struct linger)=%d but %ld",
+ (int)sizeof(struct linger), (long)RSTRING_LEN(data));
+ memcpy((char*)&l, RSTRING_PTR(data), sizeof(struct linger));
+ switch (l.l_onoff) {
+ case 0: vonoff = Qfalse; break;
+ case 1: vonoff = Qtrue; break;
+ default: vonoff = INT2NUM(l.l_onoff); break;
+ }
+ vsecs = INT2NUM(l.l_linger);
+ return rb_assoc_new(vonoff, vsecs);
+}
+
+/*
+ * call-seq:
+ * Socket::Option.ipv4_multicast_loop(integer) => sockopt
+ *
+ * Creates a new Socket::Option object for IP_MULTICAST_LOOP.
+ *
+ * The size is dependent on the platform.
+ *
+ * sockopt = Socket::Option.int(:INET, :IPPROTO_IP, :IP_MULTICAST_LOOP, 1)
+ * p sockopt.int => 1
+ *
+ * p Socket::Option.ipv4_multicast_loop(10)
+ * #=> #<Socket::Option: INET IP MULTICAST_LOOP 10>
+ *
+ */
+static VALUE
+sockopt_s_ipv4_multicast_loop(VALUE klass, VALUE value)
+{
+
+#if defined(IPPROTO_IP) && defined(IP_MULTICAST_LOOP)
+ VALUE o = XCAT(sockopt_pack_,TYPE_IP_MULTICAST_LOOP)(value);
+ return rsock_sockopt_new(AF_INET, IPPROTO_IP, IP_MULTICAST_LOOP, o);
+#else
+# error IPPROTO_IP or IP_MULTICAST_LOOP is not implemented
+#endif
+}
+
+/*
+ * call-seq:
+ * sockopt.ipv4_multicast_loop => integer
+ *
+ * Returns the ipv4_multicast_loop data in _sockopt_ as a integer.
+ *
+ * sockopt = Socket::Option.ipv4_multicast_loop(10)
+ * p sockopt.ipv4_multicast_loop => 10
+ */
+static VALUE
+sockopt_ipv4_multicast_loop(VALUE self)
+{
+ int family = NUM2INT(sockopt_family_m(self));
+ int level = sockopt_level(self);
+ int optname = sockopt_optname(self);
+
+#if defined(IPPROTO_IP) && defined(IP_MULTICAST_LOOP)
+ if (family == AF_INET && level == IPPROTO_IP && optname == IP_MULTICAST_LOOP) {
+ return XCAT(sockopt_,TYPE_IP_MULTICAST_LOOP)(self);
+ }
+#endif
+ rb_raise(rb_eTypeError, "ipv4_multicast_loop socket option expected");
+ UNREACHABLE;
+}
+
+#define inspect_ipv4_multicast_loop(a,b,c,d) \
+ XCAT(inspect_,TYPE_IP_MULTICAST_LOOP)(a,b,c,d)
+
+/*
+ * call-seq:
+ * Socket::Option.ipv4_multicast_ttl(integer) => sockopt
+ *
+ * Creates a new Socket::Option object for IP_MULTICAST_TTL.
+ *
+ * The size is dependent on the platform.
+ *
+ * p Socket::Option.ipv4_multicast_ttl(10)
+ * #=> #<Socket::Option: INET IP MULTICAST_TTL 10>
+ *
+ */
+static VALUE
+sockopt_s_ipv4_multicast_ttl(VALUE klass, VALUE value)
+{
+#if defined(IPPROTO_IP) && defined(IP_MULTICAST_TTL)
+ VALUE o = XCAT(sockopt_pack_,TYPE_IP_MULTICAST_TTL)(value);
+ return rsock_sockopt_new(AF_INET, IPPROTO_IP, IP_MULTICAST_TTL, o);
+#else
+# error IPPROTO_IP or IP_MULTICAST_TTL is not implemented
+#endif
+}
+
+/*
+ * call-seq:
+ * sockopt.ipv4_multicast_ttl => integer
+ *
+ * Returns the ipv4_multicast_ttl data in _sockopt_ as a integer.
+ *
+ * sockopt = Socket::Option.ipv4_multicast_ttl(10)
+ * p sockopt.ipv4_multicast_ttl => 10
+ */
+static VALUE
+sockopt_ipv4_multicast_ttl(VALUE self)
+{
+ int family = NUM2INT(sockopt_family_m(self));
+ int level = sockopt_level(self);
+ int optname = sockopt_optname(self);
+
+#if defined(IPPROTO_IP) && defined(IP_MULTICAST_TTL)
+ if (family == AF_INET && level == IPPROTO_IP && optname == IP_MULTICAST_TTL) {
+ return XCAT(sockopt_,TYPE_IP_MULTICAST_TTL)(self);
+ }
+#endif
+ rb_raise(rb_eTypeError, "ipv4_multicast_ttl socket option expected");
+ UNREACHABLE;
+}
+
+#define inspect_ipv4_multicast_ttl(a,b,c,d) \
+ XCAT(inspect_,TYPE_IP_MULTICAST_TTL)(a,b,c,d)
+
+static int
+inspect_int(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(int)) {
+ int i;
+ memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
+ rb_str_catf(ret, " %d", i);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+
+#ifdef USE_INSPECT_BYTE
+static int
+inspect_byte(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(unsigned char)) {
+ rb_str_catf(ret, " %d", (unsigned char)*RSTRING_PTR(data));
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+static int
+inspect_errno(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(int)) {
+ int i;
+ char *err;
+ memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
+ err = strerror(i);
+ rb_str_catf(ret, " %s (%d)", err, i);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+
+#if defined(IPV6_MULTICAST_LOOP)
+static int
+inspect_uint(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(int)) {
+ unsigned int i;
+ memcpy((char*)&i, RSTRING_PTR(data), sizeof(unsigned int));
+ rb_str_catf(ret, " %u", i);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(SOL_SOCKET) && defined(SO_LINGER) /* POSIX */
+static int
+inspect_linger(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct linger)) {
+ struct linger s;
+ memcpy((char*)&s, RSTRING_PTR(data), sizeof(s));
+ switch (s.l_onoff) {
+ case 0: rb_str_cat2(ret, " off"); break;
+ case 1: rb_str_cat2(ret, " on"); break;
+ default: rb_str_catf(ret, " on(%d)", s.l_onoff); break;
+ }
+ rb_str_catf(ret, " %dsec", s.l_linger);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(SOL_SOCKET) && defined(SO_TYPE) /* POSIX */
+static int
+inspect_socktype(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(int)) {
+ int i;
+ ID id;
+ memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
+ id = rsock_intern_socktype(i);
+ if (id)
+ rb_str_catf(ret, " %s", rb_id2name(id));
+ else
+ rb_str_catf(ret, " %d", i);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+static int
+inspect_timeval_as_interval(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct timeval)) {
+ struct timeval s;
+ memcpy((char*)&s, RSTRING_PTR(data), sizeof(s));
+ rb_str_catf(ret, " %ld.%06ldsec", (long)s.tv_sec, (long)s.tv_usec);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+
+/*
+ * socket option for IPv4 multicast is bit confusing.
+ *
+ * IP Multicast is implemented by Steve Deering at first:
+ * IP Multicast Extensions for 4.3BSD UNIX and related systems
+ * (MULTICAST 1.2 Release)
+ * http://www.kohala.com/start/mcast.api.txt
+ *
+ * There are 2 socket options which takes a u_char (unsigned char).
+ *
+ * IP_MULTICAST_TTL
+ * IP_MULTICAST_LOOP
+ *
+ * However Linux and FreeBSD setsockname accepts int argument
+ * as well as u_char.
+ * Their getsockname returns int.
+ *
+ * There are 3 socket options which takes a struct.
+ *
+ * IP_MULTICAST_IF: struct in_addr
+ * IP_ADD_MEMBERSHIP: struct ip_mreq
+ * IP_DROP_MEMBERSHIP: struct ip_mreq
+ *
+ * But they uses an IP address to specify an interface.
+ * This means the API cannot specify an unnumbered interface.
+ *
+ * Linux 2.4 introduces struct ip_mreqn to fix this problem.
+ * struct ip_mreqn has imr_ifindex field to specify interface index.
+ *
+ * IP_MULTICAST_IF: struct ip_mreqn
+ * IP_ADD_MEMBERSHIP: struct ip_mreqn
+ * IP_DROP_MEMBERSHIP: struct ip_mreqn
+ *
+ * FreeBSD 7 obtained struct ip_mreqn for IP_MULTICAST_IF.
+ * http://www.FreeBSD.org/cgi/cvsweb.cgi/src/sys/netinet/in.h.diff?r1=1.99;r2=1.100
+ *
+ * Another hackish workaround is "RFC 1724 hack".
+ * RFC 1724 section 3.3 suggests unnumbered interfaces
+ * specified by pseudo address 0.0.0.0/8.
+ * NetBSD 4 and FreeBSD 5 documented it.
+ * http://cvsweb.netbsd.org/cgi-bin/cvsweb.cgi/src/share/man/man4/ip.4.diff?r1=1.16&r2=1.17
+ * http://www.FreeBSD.org/cgi/cvsweb.cgi/src/share/man/man4/ip.4.diff?r1=1.37;r2=1.38
+ * FreeBSD 7.0 removed it.
+ * http://www.FreeBSD.org/cgi/cvsweb.cgi/src/share/man/man4/ip.4.diff?r1=1.49;r2=1.50
+ *
+ * RFC 1724 hack is not supported by Socket::Option#inspect because
+ * it is not distinguishable by the size.
+ */
+
+#if !defined HAVE_INET_NTOP && ! defined _WIN32
+static const char *
+inet_ntop(int af, const void *addr, char *numaddr, size_t numaddr_len)
+{
+#ifdef HAVE_INET_NTOA
+ struct in_addr in;
+ memcpy(&in.s_addr, addr, sizeof(in.s_addr));
+ snprintf(numaddr, numaddr_len, "%s", inet_ntoa(in));
+#else
+ unsigned long x = ntohl(*(unsigned long*)addr);
+ snprintf(numaddr, numaddr_len, "%d.%d.%d.%d",
+ (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
+ (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
+#endif
+ return numaddr;
+}
+#elif defined __MINGW32__
+# define inet_ntop(f,a,n,l) rb_w32_inet_ntop(f,a,n,l)
+#elif defined _MSC_VER && RUBY_MSVCRT_VERSION < 90
+const char *WSAAPI inet_ntop(int, const void *, char *, size_t);
+#endif
+
+/* Although the buffer size needed depends on the prefixes, "%u" may generate "4294967295". */
+static int
+rb_if_indextoname(const char *succ_prefix, const char *fail_prefix, unsigned int ifindex, char *buf, size_t len)
+{
+#if defined(HAVE_IF_INDEXTONAME)
+ char ifbuf[IFNAMSIZ];
+ if (if_indextoname(ifindex, ifbuf) == NULL)
+ return snprintf(buf, len, "%s%u", fail_prefix, ifindex);
+ else
+ return snprintf(buf, len, "%s%s", succ_prefix, ifbuf);
+#else
+# ifndef IFNAMSIZ
+# define IFNAMSIZ (sizeof(unsigned int)*3+1)
+# endif
+ return snprintf(buf, len, "%s%u", fail_prefix, ifindex);
+#endif
+}
+
+#if defined(IPPROTO_IP) && defined(HAVE_TYPE_STRUCT_IP_MREQ) /* 4.4BSD, GNU/Linux */
+static int
+inspect_ipv4_mreq(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct ip_mreq)) {
+ struct ip_mreq s;
+ char addrbuf[INET_ADDRSTRLEN];
+ memcpy((char*)&s, RSTRING_PTR(data), sizeof(s));
+ if (inet_ntop(AF_INET, &s.imr_multiaddr, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
+ rb_str_cat2(ret, " invalid-address");
+ else
+ rb_str_catf(ret, " %s", addrbuf);
+ if (inet_ntop(AF_INET, &s.imr_interface, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
+ rb_str_catf(ret, " invalid-address");
+ else
+ rb_str_catf(ret, " %s", addrbuf);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(IPPROTO_IP) && defined(HAVE_TYPE_STRUCT_IP_MREQN) /* GNU/Linux, FreeBSD 7 */
+static int
+inspect_ipv4_mreqn(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct ip_mreqn)) {
+ struct ip_mreqn s;
+ char addrbuf[INET_ADDRSTRLEN], ifbuf[32+IFNAMSIZ];
+ memcpy((char*)&s, RSTRING_PTR(data), sizeof(s));
+ if (inet_ntop(AF_INET, &s.imr_multiaddr, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
+ rb_str_cat2(ret, " invalid-address");
+ else
+ rb_str_catf(ret, " %s", addrbuf);
+ if (inet_ntop(AF_INET, &s.imr_address, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
+ rb_str_catf(ret, " invalid-address");
+ else
+ rb_str_catf(ret, " %s", addrbuf);
+ rb_if_indextoname(" ", " ifindex:", s.imr_ifindex, ifbuf, sizeof(ifbuf));
+ rb_str_cat2(ret, ifbuf);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(IPPROTO_IP) && defined(HAVE_TYPE_STRUCT_IP_MREQ) /* 4.4BSD, GNU/Linux */
+static int
+inspect_ipv4_add_drop_membership(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct ip_mreq))
+ return inspect_ipv4_mreq(level, optname, data, ret);
+# if defined(HAVE_TYPE_STRUCT_IP_MREQN)
+ else if (RSTRING_LEN(data) == sizeof(struct ip_mreqn))
+ return inspect_ipv4_mreqn(level, optname, data, ret);
+# endif
+ else
+ return 0;
+}
+#endif
+
+#if defined(IPPROTO_IP) && defined(IP_MULTICAST_IF) && defined(HAVE_TYPE_STRUCT_IP_MREQN) /* 4.4BSD, GNU/Linux */
+static int
+inspect_ipv4_multicast_if(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct in_addr)) {
+ struct in_addr s;
+ char addrbuf[INET_ADDRSTRLEN];
+ memcpy((char*)&s, RSTRING_PTR(data), sizeof(s));
+ if (inet_ntop(AF_INET, &s, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
+ rb_str_cat2(ret, " invalid-address");
+ else
+ rb_str_catf(ret, " %s", addrbuf);
+ return 1;
+ }
+ else if (RSTRING_LEN(data) == sizeof(struct ip_mreqn)) {
+ return inspect_ipv4_mreqn(level, optname, data, ret);
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(IPV6_MULTICAST_IF) /* POSIX, RFC 3493 */
+static int
+inspect_ipv6_multicast_if(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(int)) {
+ char ifbuf[32+IFNAMSIZ];
+ unsigned int ifindex;
+ memcpy((char*)&ifindex, RSTRING_PTR(data), sizeof(unsigned int));
+ rb_if_indextoname(" ", " ", ifindex, ifbuf, sizeof(ifbuf));
+ rb_str_cat2(ret, ifbuf);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(IPPROTO_IPV6) && defined(HAVE_TYPE_STRUCT_IPV6_MREQ) /* POSIX, RFC 3493 */
+static int
+inspect_ipv6_mreq(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct ipv6_mreq)) {
+ struct ipv6_mreq s;
+ char addrbuf[INET6_ADDRSTRLEN], ifbuf[32+IFNAMSIZ];
+ memcpy((char*)&s, RSTRING_PTR(data), sizeof(s));
+ if (inet_ntop(AF_INET6, &s.ipv6mr_multiaddr, addrbuf, (socklen_t)sizeof(addrbuf)) == NULL)
+ rb_str_cat2(ret, " invalid-address");
+ else
+ rb_str_catf(ret, " %s", addrbuf);
+ rb_if_indextoname(" ", " interface:", s.ipv6mr_interface, ifbuf, sizeof(ifbuf));
+ rb_str_cat2(ret, ifbuf);
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(IPPROTO_TCP) && defined(TCP_INFO) && defined(HAVE_TYPE_STRUCT_TCP_INFO)
+
+#ifdef __FreeBSD__
+# ifndef HAVE_CONST_TCP_ESTABLISHED
+# define TCP_ESTABLISHED TCPS_ESTABLISHED
+# endif
+# ifndef HAVE_CONST_TCP_SYN_SENT
+# define TCP_SYN_SENT TCPS_SYN_SENT
+# endif
+# ifndef HAVE_CONST_TCP_SYN_RECV
+# define TCP_SYN_RECV TCPS_SYN_RECEIVED
+# endif
+# ifndef HAVE_CONST_TCP_FIN_WAIT1
+# define TCP_FIN_WAIT1 TCPS_FIN_WAIT_1
+# endif
+# ifndef HAVE_CONST_TCP_FIN_WAIT2
+# define TCP_FIN_WAIT2 TCPS_FIN_WAIT_2
+# endif
+# ifndef HAVE_CONST_TCP_TIME_WAIT
+# define TCP_TIME_WAIT TCPS_TIME_WAIT
+# endif
+# ifndef HAVE_CONST_TCP_CLOSE
+# define TCP_CLOSE TCPS_CLOSED
+# endif
+# ifndef HAVE_CONST_TCP_CLOSE_WAIT
+# define TCP_CLOSE_WAIT TCPS_CLOSE_WAIT
+# endif
+# ifndef HAVE_CONST_TCP_LAST_ACK
+# define TCP_LAST_ACK TCPS_LAST_ACK
+# endif
+# ifndef HAVE_CONST_TCP_LISTEN
+# define TCP_LISTEN TCPS_LISTEN
+# endif
+# ifndef HAVE_CONST_TCP_CLOSING
+# define TCP_CLOSING TCPS_CLOSING
+# endif
+#endif
+
+#if defined(HAVE_CONST_TCP_ESTABLISHED) && !defined(TCP_ESTABLISHED)
+# define TCP_ESTABLISHED TCP_ESTABLISHED
+#endif
+#if defined(HAVE_CONST_TCP_SYN_SENT) && !defined(TCP_SYN_SENT)
+# define TCP_SYN_SENT TCP_SYN_SENT
+#endif
+#if defined(HAVE_CONST_TCP_SYN_RECV) && !defined(TCP_SYN_RECV)
+# define TCP_SYN_RECV TCP_SYN_RECV
+#endif
+#if defined(HAVE_CONST_TCP_FIN_WAIT1) && !defined(TCP_FIN_WAIT1)
+# define TCP_FIN_WAIT1 TCP_FIN_WAIT1
+#endif
+#if defined(HAVE_CONST_TCP_FIN_WAIT2) && !defined(TCP_FIN_WAIT2)
+# define TCP_FIN_WAIT2 TCP_FIN_WAIT2
+#endif
+#if defined(HAVE_CONST_TCP_TIME_WAIT) && !defined(TCP_TIME_WAIT)
+# define TCP_TIME_WAIT TCP_TIME_WAIT
+#endif
+#if defined(HAVE_CONST_TCP_CLOSE) && !defined(TCP_CLOSE)
+# define TCP_CLOSE TCP_CLOSE
+#endif
+#if defined(HAVE_CONST_TCP_CLOSE_WAIT) && !defined(TCP_CLOSE_WAIT)
+# define TCP_CLOSE_WAIT TCP_CLOSE_WAIT
+#endif
+#if defined(HAVE_CONST_TCP_LAST_ACK) && !defined(TCP_LAST_ACK)
+# define TCP_LAST_ACK TCP_LAST_ACK
+#endif
+#if defined(HAVE_CONST_TCP_LISTEN) && !defined(TCP_LISTEN)
+# define TCP_LISTEN TCP_LISTEN
+#endif
+#if defined(HAVE_CONST_TCP_CLOSING) && !defined(TCP_CLOSING)
+# define TCP_CLOSING TCP_CLOSING
+#endif
+
+static void
+inspect_tcpi_options(VALUE ret, uint8_t options)
+{
+ int sep = '=';
+
+ rb_str_cat2(ret, " options");
+#define INSPECT_TCPI_OPTION(optval, name) \
+ if (options & (optval)) { \
+ options &= ~(uint8_t)(optval); \
+ rb_str_catf(ret, "%c%s", sep, name); \
+ sep = ','; \
+ }
+#ifdef TCPI_OPT_TIMESTAMPS /* GNU/Linux, FreeBSD */
+ INSPECT_TCPI_OPTION(TCPI_OPT_TIMESTAMPS, "TIMESTAMPS");
+#endif
+#ifdef TCPI_OPT_SACK /* GNU/Linux, FreeBSD */
+ INSPECT_TCPI_OPTION(TCPI_OPT_SACK, "SACK");
+#endif
+#ifdef TCPI_OPT_WSCALE /* GNU/Linux, FreeBSD */
+ INSPECT_TCPI_OPTION(TCPI_OPT_WSCALE, "WSCALE");
+#endif
+#ifdef TCPI_OPT_ECN /* GNU/Linux, FreeBSD */
+ INSPECT_TCPI_OPTION(TCPI_OPT_ECN, "ECN");
+#endif
+#ifdef TCPI_OPT_ECN_SEEN /* GNU/Linux */
+ INSPECT_TCPI_OPTION(TCPI_OPT_ECN_SEEN, "ECN_SEEN");
+#endif
+#ifdef TCPI_OPT_SYN_DATA /* GNU/Linux */
+ INSPECT_TCPI_OPTION(TCPI_OPT_SYN_DATA, "SYN_DATA");
+#endif
+#ifdef TCPI_OPT_TOE /* FreeBSD */
+ INSPECT_TCPI_OPTION(TCPI_OPT_TOE, "TOE");
+#endif
+#undef INSPECT_TCPI_OPTION
+
+ if (options || sep == '=') {
+ rb_str_catf(ret, "%c%u", sep, options);
+ }
+}
+
+static void
+inspect_tcpi_usec(VALUE ret, const char *prefix, uint32_t t)
+{
+ rb_str_catf(ret, "%s%u.%06us", prefix, t / 1000000, t % 1000000);
+}
+
+#if defined(__linux__) || defined(__sun)
+static void
+inspect_tcpi_msec(VALUE ret, const char *prefix, uint32_t t)
+{
+ rb_str_catf(ret, "%s%u.%03us", prefix, t / 1000, t % 1000);
+}
+#endif
+
+#ifdef __FreeBSD__
+# define inspect_tcpi_rto(ret, t) inspect_tcpi_usec(ret, " rto=", t)
+# define inspect_tcpi_last_data_recv(ret, t) inspect_tcpi_usec(ret, " last_data_recv=", t)
+# define inspect_tcpi_rtt(ret, t) inspect_tcpi_usec(ret, " rtt=", t)
+# define inspect_tcpi_rttvar(ret, t) inspect_tcpi_usec(ret, " rttvar=", t)
+#else
+# define inspect_tcpi_rto(ret, t) inspect_tcpi_usec(ret, " rto=", t)
+# define inspect_tcpi_ato(ret, t) inspect_tcpi_usec(ret, " ato=", t)
+# define inspect_tcpi_last_data_sent(ret, t) inspect_tcpi_msec(ret, " last_data_sent=", t)
+# define inspect_tcpi_last_data_recv(ret, t) inspect_tcpi_msec(ret, " last_data_recv=", t)
+# define inspect_tcpi_last_ack_sent(ret, t) inspect_tcpi_msec(ret, " last_ack_sent=", t)
+# define inspect_tcpi_last_ack_recv(ret, t) inspect_tcpi_msec(ret, " last_ack_recv=", t)
+# define inspect_tcpi_rtt(ret, t) inspect_tcpi_usec(ret, " rtt=", t)
+# define inspect_tcpi_rttvar(ret, t) inspect_tcpi_usec(ret, " rttvar=", t)
+# define inspect_tcpi_rcv_rtt(ret, t) inspect_tcpi_usec(ret, " rcv_rtt=", t)
+#endif
+
+static int
+inspect_tcp_info(int level, int optname, VALUE data, VALUE ret)
+{
+ size_t actual_size = RSTRING_LEN(data);
+ if (sizeof(struct tcp_info) <= actual_size) {
+ struct tcp_info s;
+ memcpy((char*)&s, RSTRING_PTR(data), sizeof(s));
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_STATE
+ switch (s.tcpi_state) {
+# ifdef TCP_ESTABLISHED
+ case TCP_ESTABLISHED: rb_str_cat_cstr(ret, " state=ESTABLISHED"); break;
+# endif
+# ifdef TCP_SYN_SENT
+ case TCP_SYN_SENT: rb_str_cat_cstr(ret, " state=SYN_SENT"); break;
+# endif
+# ifdef TCP_SYN_RECV
+ case TCP_SYN_RECV: rb_str_cat_cstr(ret, " state=SYN_RECV"); break;
+# endif
+# ifdef TCP_FIN_WAIT1
+ case TCP_FIN_WAIT1: rb_str_cat_cstr(ret, " state=FIN_WAIT1"); break;
+# endif
+# ifdef TCP_FIN_WAIT2
+ case TCP_FIN_WAIT2: rb_str_cat_cstr(ret, " state=FIN_WAIT2"); break;
+# endif
+# ifdef TCP_TIME_WAIT
+ case TCP_TIME_WAIT: rb_str_cat_cstr(ret, " state=TIME_WAIT"); break;
+# endif
+# ifdef TCP_CLOSE
+ case TCP_CLOSE: rb_str_cat_cstr(ret, " state=CLOSED"); break; /* RFC 793 uses "CLOSED", not "CLOSE" */
+# endif
+# ifdef TCP_CLOSE_WAIT
+ case TCP_CLOSE_WAIT: rb_str_cat_cstr(ret, " state=CLOSE_WAIT"); break;
+# endif
+# ifdef TCP_LAST_ACK
+ case TCP_LAST_ACK: rb_str_cat_cstr(ret, " state=LAST_ACK"); break;
+# endif
+# ifdef TCP_LISTEN
+ case TCP_LISTEN: rb_str_cat_cstr(ret, " state=LISTEN"); break;
+# endif
+# ifdef TCP_CLOSING
+ case TCP_CLOSING: rb_str_cat_cstr(ret, " state=CLOSING"); break;
+# endif
+ default: rb_str_catf(ret, " state=%u", s.tcpi_state); break;
+ }
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_CA_STATE
+ switch (s.tcpi_ca_state) {
+ case TCP_CA_Open: rb_str_cat_cstr(ret, " ca_state=Open"); break;
+ case TCP_CA_Disorder: rb_str_cat_cstr(ret, " ca_state=Disorder"); break;
+ case TCP_CA_CWR: rb_str_cat_cstr(ret, " ca_state=CWR"); break;
+ case TCP_CA_Recovery: rb_str_cat_cstr(ret, " ca_state=Recovery"); break;
+ case TCP_CA_Loss: rb_str_cat_cstr(ret, " ca_state=Loss"); break;
+ default: rb_str_catf(ret, " ca_state=%u", s.tcpi_ca_state); break;
+ }
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RETRANSMITS
+ rb_str_catf(ret, " retransmits=%u", s.tcpi_retransmits);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_PROBES
+ rb_str_catf(ret, " probes=%u", s.tcpi_probes);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_BACKOFF
+ rb_str_catf(ret, " backoff=%u", s.tcpi_backoff);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_OPTIONS
+ inspect_tcpi_options(ret, s.tcpi_options);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SND_WSCALE
+ rb_str_catf(ret, " snd_wscale=%u", s.tcpi_snd_wscale);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RCV_WSCALE
+ rb_str_catf(ret, " rcv_wscale=%u", s.tcpi_rcv_wscale);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RTO
+ inspect_tcpi_rto(ret, s.tcpi_rto);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_ATO
+ inspect_tcpi_ato(ret, s.tcpi_ato);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SND_MSS
+ rb_str_catf(ret, " snd_mss=%u", s.tcpi_snd_mss);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RCV_MSS
+ rb_str_catf(ret, " rcv_mss=%u", s.tcpi_rcv_mss);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_UNACKED
+ rb_str_catf(ret, " unacked=%u", s.tcpi_unacked);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SACKED
+ rb_str_catf(ret, " sacked=%u", s.tcpi_sacked);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_LOST
+ rb_str_catf(ret, " lost=%u", s.tcpi_lost);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RETRANS
+ rb_str_catf(ret, " retrans=%u", s.tcpi_retrans);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_FACKETS
+ rb_str_catf(ret, " fackets=%u", s.tcpi_fackets);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_LAST_DATA_SENT
+ inspect_tcpi_last_data_sent(ret, s.tcpi_last_data_sent);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_LAST_ACK_SENT
+ inspect_tcpi_last_ack_sent(ret, s.tcpi_last_ack_sent);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_LAST_DATA_RECV
+ inspect_tcpi_last_data_recv(ret, s.tcpi_last_data_recv);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_LAST_ACK_RECV
+ inspect_tcpi_last_ack_recv(ret, s.tcpi_last_ack_recv);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_PMTU
+ rb_str_catf(ret, " pmtu=%u", s.tcpi_pmtu);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RCV_SSTHRESH
+ rb_str_catf(ret, " rcv_ssthresh=%u", s.tcpi_rcv_ssthresh);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RTT
+ inspect_tcpi_rtt(ret, s.tcpi_rtt);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RTTVAR
+ inspect_tcpi_rttvar(ret, s.tcpi_rttvar);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SND_SSTHRESH
+ rb_str_catf(ret, " snd_ssthresh=%u", s.tcpi_snd_ssthresh);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SND_CWND
+ rb_str_catf(ret, " snd_cwnd=%u", s.tcpi_snd_cwnd);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_ADVMSS
+ rb_str_catf(ret, " advmss=%u", s.tcpi_advmss);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_REORDERING
+ rb_str_catf(ret, " reordering=%u", s.tcpi_reordering);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RCV_RTT
+ inspect_tcpi_rcv_rtt(ret, s.tcpi_rcv_rtt);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RCV_SPACE
+ rb_str_catf(ret, " rcv_space=%u", s.tcpi_rcv_space);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_TOTAL_RETRANS
+ rb_str_catf(ret, " total_retrans=%u", s.tcpi_total_retrans);
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SND_WND
+ rb_str_catf(ret, " snd_wnd=%u", s.tcpi_snd_wnd); /* FreeBSD */
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SND_BWND
+ rb_str_catf(ret, " snd_bwnd=%u", s.tcpi_snd_bwnd); /* FreeBSD */
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SND_NXT
+ rb_str_catf(ret, " snd_nxt=%u", s.tcpi_snd_nxt); /* FreeBSD */
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RCV_NXT
+ rb_str_catf(ret, " rcv_nxt=%u", s.tcpi_rcv_nxt); /* FreeBSD */
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_TOE_TID
+ rb_str_catf(ret, " toe_tid=%u", s.tcpi_toe_tid); /* FreeBSD */
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SND_REXMITPACK
+ rb_str_catf(ret, " snd_rexmitpack=%u", s.tcpi_snd_rexmitpack); /* FreeBSD */
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_RCV_OOOPACK
+ rb_str_catf(ret, " rcv_ooopack=%u", s.tcpi_rcv_ooopack); /* FreeBSD */
+#endif
+#ifdef HAVE_STRUCT_TCP_INFO_TCPI_SND_ZEROWIN
+ rb_str_catf(ret, " snd_zerowin=%u", s.tcpi_snd_zerowin); /* FreeBSD */
+#endif
+ if (sizeof(struct tcp_info) < actual_size)
+ rb_str_catf(ret, " (%u bytes too long)", (unsigned)(actual_size - sizeof(struct tcp_info)));
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(SOL_SOCKET) && defined(SO_PEERCRED) /* GNU/Linux, OpenBSD */
+#if defined(__OpenBSD__)
+#define RUBY_SOCK_PEERCRED struct sockpeercred
+#else
+#define RUBY_SOCK_PEERCRED struct ucred
+#endif
+static int
+inspect_peercred(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(RUBY_SOCK_PEERCRED)) {
+ RUBY_SOCK_PEERCRED cred;
+ memcpy(&cred, RSTRING_PTR(data), sizeof(RUBY_SOCK_PEERCRED));
+ rb_str_catf(ret, " pid=%u euid=%u egid=%u",
+ (unsigned)cred.pid, (unsigned)cred.uid, (unsigned)cred.gid);
+ rb_str_cat2(ret, " (ucred)");
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+#if defined(LOCAL_PEERCRED) /* FreeBSD, MacOS X */
+static int
+inspect_local_peercred(int level, int optname, VALUE data, VALUE ret)
+{
+ if (RSTRING_LEN(data) == sizeof(struct xucred)) {
+ struct xucred cred;
+ memcpy(&cred, RSTRING_PTR(data), sizeof(struct xucred));
+ if (cred.cr_version != XUCRED_VERSION)
+ return 0;
+ rb_str_catf(ret, " version=%u", cred.cr_version);
+ rb_str_catf(ret, " euid=%u", cred.cr_uid);
+ if (cred.cr_ngroups) {
+ int i;
+ const char *sep = " groups=";
+ for (i = 0; i < cred.cr_ngroups; i++) {
+ rb_str_catf(ret, "%s%u", sep, cred.cr_groups[i]);
+ sep = ",";
+ }
+ }
+ rb_str_cat2(ret, " (xucred)");
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+#endif
+
+
+/*
+ * call-seq:
+ * sockopt.inspect => string
+ *
+ * Returns a string which shows sockopt in human-readable form.
+ *
+ * p Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i")).inspect
+ * #=> "#<Socket::Option: INET SOCKET KEEPALIVE 1>"
+ *
+ */
+static VALUE
+sockopt_inspect(VALUE self)
+{
+ int family = NUM2INT(sockopt_family_m(self));
+ int level = NUM2INT(sockopt_level_m(self));
+ int optname = NUM2INT(sockopt_optname_m(self));
+ VALUE data = sockopt_data(self);
+ VALUE v, ret;
+ ID family_id, level_id, optname_id;
+ int inspected;
+
+ StringValue(data);
+
+ ret = rb_sprintf("#<%s:", rb_obj_classname(self));
+
+ family_id = rsock_intern_family_noprefix(family);
+ if (family_id)
+ rb_str_catf(ret, " %s", rb_id2name(family_id));
+ else
+ rb_str_catf(ret, " family:%d", family);
+
+ if (level == SOL_SOCKET) {
+ rb_str_cat2(ret, " SOCKET");
+
+ optname_id = rsock_intern_so_optname(optname);
+ if (optname_id)
+ rb_str_catf(ret, " %s", rb_id2name(optname_id));
+ else
+ rb_str_catf(ret, " optname:%d", optname);
+ }
+#ifdef HAVE_SYS_UN_H
+ else if (family == AF_UNIX) {
+ rb_str_catf(ret, " level:%d", level);
+
+ optname_id = rsock_intern_local_optname(optname);
+ if (optname_id)
+ rb_str_catf(ret, " %s", rb_id2name(optname_id));
+ else
+ rb_str_catf(ret, " optname:%d", optname);
+ }
+#endif
+ else if (IS_IP_FAMILY(family)) {
+ level_id = rsock_intern_iplevel(level);
+ if (level_id)
+ rb_str_catf(ret, " %s", rb_id2name(level_id));
+ else
+ rb_str_catf(ret, " level:%d", level);
+
+ v = optname_to_sym(level, optname);
+ if (SYMBOL_P(v))
+ rb_str_catf(ret, " %s", rb_id2name(SYM2ID(v)));
+ else
+ rb_str_catf(ret, " optname:%d", optname);
+ }
+ else {
+ rb_str_catf(ret, " level:%d", level);
+ rb_str_catf(ret, " optname:%d", optname);
+ }
+
+ inspected = 0;
+
+ if (level == SOL_SOCKET)
+ family = AF_UNSPEC;
+ switch (family) {
+ case AF_UNSPEC:
+ switch (level) {
+ case SOL_SOCKET:
+ switch (optname) {
+# if defined(SO_DEBUG) /* POSIX */
+ case SO_DEBUG: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_ERROR) /* POSIX */
+ case SO_ERROR: inspected = inspect_errno(level, optname, data, ret); break;
+# endif
+# if defined(SO_TYPE) /* POSIX */
+ case SO_TYPE: inspected = inspect_socktype(level, optname, data, ret); break;
+# endif
+# if defined(SO_ACCEPTCONN) /* POSIX */
+ case SO_ACCEPTCONN: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_BROADCAST) /* POSIX */
+ case SO_BROADCAST: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_REUSEADDR) /* POSIX */
+ case SO_REUSEADDR: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_KEEPALIVE) /* POSIX */
+ case SO_KEEPALIVE: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_OOBINLINE) /* POSIX */
+ case SO_OOBINLINE: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_SNDBUF) /* POSIX */
+ case SO_SNDBUF: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_RCVBUF) /* POSIX */
+ case SO_RCVBUF: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_DONTROUTE) /* POSIX */
+ case SO_DONTROUTE: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_RCVLOWAT) /* POSIX */
+ case SO_RCVLOWAT: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_SNDLOWAT) /* POSIX */
+ case SO_SNDLOWAT: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(SO_LINGER) /* POSIX */
+ case SO_LINGER: inspected = inspect_linger(level, optname, data, ret); break;
+# endif
+# if defined(SO_RCVTIMEO) /* POSIX */
+ case SO_RCVTIMEO: inspected = inspect_timeval_as_interval(level, optname, data, ret); break;
+# endif
+# if defined(SO_SNDTIMEO) /* POSIX */
+ case SO_SNDTIMEO: inspected = inspect_timeval_as_interval(level, optname, data, ret); break;
+# endif
+# if defined(SO_PEERCRED) /* GNU/Linux, OpenBSD */
+ case SO_PEERCRED: inspected = inspect_peercred(level, optname, data, ret); break;
+# endif
+ }
+ break;
+ }
+ break;
+
+ case AF_INET:
+#ifdef INET6
+ case AF_INET6:
+#endif
+ switch (level) {
+# if defined(IPPROTO_IP)
+ case IPPROTO_IP:
+ switch (optname) {
+# if defined(IP_MULTICAST_IF) && defined(HAVE_TYPE_STRUCT_IP_MREQN) /* 4.4BSD, GNU/Linux */
+ case IP_MULTICAST_IF: inspected = inspect_ipv4_multicast_if(level, optname, data, ret); break;
+# endif
+# if defined(IP_ADD_MEMBERSHIP) /* 4.4BSD, GNU/Linux */
+ case IP_ADD_MEMBERSHIP: inspected = inspect_ipv4_add_drop_membership(level, optname, data, ret); break;
+# endif
+# if defined(IP_DROP_MEMBERSHIP) /* 4.4BSD, GNU/Linux */
+ case IP_DROP_MEMBERSHIP: inspected = inspect_ipv4_add_drop_membership(level, optname, data, ret); break;
+# endif
+# if defined(IP_MULTICAST_LOOP) /* 4.4BSD, GNU/Linux */
+ case IP_MULTICAST_LOOP: inspected = inspect_ipv4_multicast_loop(level, optname, data, ret); break;
+# endif
+# if defined(IP_MULTICAST_TTL) /* 4.4BSD, GNU/Linux */
+ case IP_MULTICAST_TTL: inspected = inspect_ipv4_multicast_ttl(level, optname, data, ret); break;
+# endif
+ }
+ break;
+# endif
+
+# if defined(IPPROTO_IPV6)
+ case IPPROTO_IPV6:
+ switch (optname) {
+# if defined(IPV6_MULTICAST_HOPS) /* POSIX */
+ case IPV6_MULTICAST_HOPS: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(IPV6_MULTICAST_IF) /* POSIX */
+ case IPV6_MULTICAST_IF: inspected = inspect_ipv6_multicast_if(level, optname, data, ret); break;
+# endif
+# if defined(IPV6_MULTICAST_LOOP) /* POSIX */
+ case IPV6_MULTICAST_LOOP: inspected = inspect_uint(level, optname, data, ret); break;
+# endif
+# if defined(IPV6_JOIN_GROUP) /* POSIX */
+ case IPV6_JOIN_GROUP: inspected = inspect_ipv6_mreq(level, optname, data, ret); break;
+# endif
+# if defined(IPV6_LEAVE_GROUP) /* POSIX */
+ case IPV6_LEAVE_GROUP: inspected = inspect_ipv6_mreq(level, optname, data, ret); break;
+# endif
+# if defined(IPV6_UNICAST_HOPS) /* POSIX */
+ case IPV6_UNICAST_HOPS: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(IPV6_V6ONLY) /* POSIX */
+ case IPV6_V6ONLY: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+ }
+ break;
+# endif
+
+# if defined(IPPROTO_TCP)
+ case IPPROTO_TCP:
+ switch (optname) {
+# if defined(TCP_NODELAY) /* POSIX */
+ case TCP_NODELAY: inspected = inspect_int(level, optname, data, ret); break;
+# endif
+# if defined(TCP_INFO) && defined(HAVE_TYPE_STRUCT_TCP_INFO) /* Linux, FreeBSD */
+ case TCP_INFO: inspected = inspect_tcp_info(level, optname, data, ret); break;
+# endif
+ }
+ break;
+# endif
+ }
+ break;
+
+#ifdef HAVE_SYS_UN_H
+ case AF_UNIX:
+ switch (level) {
+ case 0:
+ switch (optname) {
+# if defined(LOCAL_PEERCRED)
+ case LOCAL_PEERCRED: inspected = inspect_local_peercred(level, optname, data, ret); break;
+# endif
+ }
+ break;
+ }
+ break;
+#endif
+ }
+
+ if (!inspected) {
+ rb_str_cat2(ret, " ");
+ rb_str_append(ret, rb_str_dump(data));
+ }
+
+ rb_str_cat2(ret, ">");
+
+ return ret;
+}
+
+/*
+ * call-seq:
+ * sockopt.unpack(template) => array
+ *
+ * Calls String#unpack on sockopt.data.
+ *
+ * sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i"))
+ * p sockopt.unpack("i") #=> [1]
+ * p sockopt.data.unpack("i") #=> [1]
+ */
+static VALUE
+sockopt_unpack(VALUE self, VALUE template)
+{
+ return rb_funcall(sockopt_data(self), rb_intern("unpack"), 1, template);
+}
+
+void
+rsock_init_sockopt(void)
+{
+ /*
+ * Document-class: Socket::Option
+ *
+ * Socket::Option represents a socket option used by
+ * BasicSocket#getsockopt and BasicSocket#setsockopt. A socket option
+ * contains the socket #family, protocol #level, option name #optname and
+ * option value #data.
+ */
+ rb_cSockOpt = rb_define_class_under(rb_cSocket, "Option", rb_cObject);
+ rb_define_method(rb_cSockOpt, "initialize", sockopt_initialize, 4);
+ rb_define_method(rb_cSockOpt, "family", sockopt_family_m, 0);
+ rb_define_method(rb_cSockOpt, "level", sockopt_level_m, 0);
+ rb_define_method(rb_cSockOpt, "optname", sockopt_optname_m, 0);
+ rb_define_method(rb_cSockOpt, "data", sockopt_data, 0);
+ rb_define_method(rb_cSockOpt, "inspect", sockopt_inspect, 0);
+
+ rb_define_singleton_method(rb_cSockOpt, "int", sockopt_s_int, 4);
+ rb_define_method(rb_cSockOpt, "int", sockopt_int, 0);
+
+ rb_define_singleton_method(rb_cSockOpt, "byte", sockopt_s_byte, 4);
+ rb_define_method(rb_cSockOpt, "byte", sockopt_byte, 0);
+
+ rb_define_singleton_method(rb_cSockOpt, "bool", sockopt_s_bool, 4);
+ rb_define_method(rb_cSockOpt, "bool", sockopt_bool, 0);
+
+ rb_define_singleton_method(rb_cSockOpt, "linger", sockopt_s_linger, 2);
+ rb_define_method(rb_cSockOpt, "linger", sockopt_linger, 0);
+
+ rb_define_singleton_method(rb_cSockOpt, "ipv4_multicast_ttl", sockopt_s_ipv4_multicast_ttl, 1);
+ rb_define_method(rb_cSockOpt, "ipv4_multicast_ttl", sockopt_ipv4_multicast_ttl, 0);
+
+ rb_define_singleton_method(rb_cSockOpt, "ipv4_multicast_loop", sockopt_s_ipv4_multicast_loop, 1);
+ rb_define_method(rb_cSockOpt, "ipv4_multicast_loop", sockopt_ipv4_multicast_loop, 0);
+
+ rb_define_method(rb_cSockOpt, "unpack", sockopt_unpack, 1);
+
+ rb_define_method(rb_cSockOpt, "to_s", sockopt_data, 0); /* compatibility for ruby before 1.9.2 */
+}
+
diff --git a/jni/ruby/ext/socket/raddrinfo.c b/jni/ruby/ext/socket/raddrinfo.c
new file mode 100644
index 0000000..caa08fe
--- /dev/null
+++ b/jni/ruby/ext/socket/raddrinfo.c
@@ -0,0 +1,2575 @@
+/************************************************
+
+ raddrinfo.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+#if defined(INET6) && (defined(LOOKUP_ORDER_HACK_INET) || defined(LOOKUP_ORDER_HACK_INET6))
+#define LOOKUP_ORDERS (sizeof(lookup_order_table) / sizeof(lookup_order_table[0]))
+static const int lookup_order_table[] = {
+#if defined(LOOKUP_ORDER_HACK_INET)
+ PF_INET, PF_INET6, PF_UNSPEC,
+#elif defined(LOOKUP_ORDER_HACK_INET6)
+ PF_INET6, PF_INET, PF_UNSPEC,
+#else
+ /* should not happen */
+#endif
+};
+
+static int
+ruby_getaddrinfo(const char *nodename, const char *servname,
+ const struct addrinfo *hints, struct addrinfo **res)
+{
+ struct addrinfo tmp_hints;
+ int i, af, error;
+
+ if (hints->ai_family != PF_UNSPEC) {
+ return getaddrinfo(nodename, servname, hints, res);
+ }
+
+ for (i = 0; i < LOOKUP_ORDERS; i++) {
+ af = lookup_order_table[i];
+ MEMCPY(&tmp_hints, hints, struct addrinfo, 1);
+ tmp_hints.ai_family = af;
+ error = getaddrinfo(nodename, servname, &tmp_hints, res);
+ if (error) {
+ if (tmp_hints.ai_family == PF_UNSPEC) {
+ break;
+ }
+ }
+ else {
+ break;
+ }
+ }
+
+ return error;
+}
+#define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo((node),(serv),(hints),(res))
+#endif
+
+#if defined(_AIX)
+static int
+ruby_getaddrinfo__aix(const char *nodename, const char *servname,
+ const struct addrinfo *hints, struct addrinfo **res)
+{
+ int error = getaddrinfo(nodename, servname, hints, res);
+ struct addrinfo *r;
+ if (error)
+ return error;
+ for (r = *res; r != NULL; r = r->ai_next) {
+ if (r->ai_addr->sa_family == 0)
+ r->ai_addr->sa_family = r->ai_family;
+ if (r->ai_addr->sa_len == 0)
+ r->ai_addr->sa_len = r->ai_addrlen;
+ }
+ return 0;
+}
+#undef getaddrinfo
+#define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo__aix((node),(serv),(hints),(res))
+static int
+ruby_getnameinfo__aix(const struct sockaddr *sa, size_t salen,
+ char *host, size_t hostlen,
+ char *serv, size_t servlen, int flags)
+{
+ struct sockaddr_in6 *sa6;
+ u_int32_t *a6;
+
+ if (sa->sa_family == AF_INET6) {
+ sa6 = (struct sockaddr_in6 *)sa;
+ a6 = sa6->sin6_addr.u6_addr.u6_addr32;
+
+ if (a6[0] == 0 && a6[1] == 0 && a6[2] == 0 && a6[3] == 0) {
+ strncpy(host, "::", hostlen);
+ snprintf(serv, servlen, "%d", sa6->sin6_port);
+ return 0;
+ }
+ }
+ return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
+}
+#undef getnameinfo
+#define getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) \
+ ruby_getnameinfo__aix((sa), (salen), (host), (hostlen), (serv), (servlen), (flags))
+#endif
+
+static int str_is_number(const char *);
+
+#if defined(__APPLE__)
+static int
+ruby_getaddrinfo__darwin(const char *nodename, const char *servname,
+ const struct addrinfo *hints, struct addrinfo **res)
+{
+ /* fix [ruby-core:29427] */
+ const char *tmp_servname;
+ struct addrinfo tmp_hints;
+ int error;
+
+ tmp_servname = servname;
+ MEMCPY(&tmp_hints, hints, struct addrinfo, 1);
+ if (nodename && servname) {
+ if (str_is_number(tmp_servname) && atoi(servname) == 0) {
+ tmp_servname = NULL;
+#ifdef AI_NUMERICSERV
+ if (tmp_hints.ai_flags) tmp_hints.ai_flags &= ~AI_NUMERICSERV;
+#endif
+ }
+ }
+
+ error = getaddrinfo(nodename, tmp_servname, &tmp_hints, res);
+ if (error == 0) {
+ /* [ruby-dev:23164] */
+ struct addrinfo *r;
+ r = *res;
+ while (r) {
+ if (! r->ai_socktype) r->ai_socktype = hints->ai_socktype;
+ if (! r->ai_protocol) {
+ if (r->ai_socktype == SOCK_DGRAM) {
+ r->ai_protocol = IPPROTO_UDP;
+ }
+ else if (r->ai_socktype == SOCK_STREAM) {
+ r->ai_protocol = IPPROTO_TCP;
+ }
+ }
+ r = r->ai_next;
+ }
+ }
+
+ return error;
+}
+#undef getaddrinfo
+#define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo__darwin((node),(serv),(hints),(res))
+#endif
+
+#ifndef GETADDRINFO_EMU
+struct getaddrinfo_arg
+{
+ const char *node;
+ const char *service;
+ const struct addrinfo *hints;
+ struct addrinfo **res;
+};
+
+static void *
+nogvl_getaddrinfo(void *arg)
+{
+ int ret;
+ struct getaddrinfo_arg *ptr = arg;
+ ret = getaddrinfo(ptr->node, ptr->service, ptr->hints, ptr->res);
+#ifdef __linux__
+ /* On Linux (mainly Ubuntu 13.04) /etc/nsswitch.conf has mdns4 and
+ * it cause getaddrinfo to return EAI_SYSTEM/ENOENT. [ruby-list:49420]
+ */
+ if (ret == EAI_SYSTEM && errno == ENOENT)
+ ret = EAI_NONAME;
+#endif
+ return (void *)(VALUE)ret;
+}
+#endif
+
+static int
+numeric_getaddrinfo(const char *node, const char *service,
+ const struct addrinfo *hints,
+ struct addrinfo **res)
+{
+#ifdef HAVE_INET_PTON
+# if defined __MINGW64__
+# define inet_pton(f,s,d) rb_w32_inet_pton(f,s,d)
+# endif
+
+ if (node && (!service || strspn(service, "0123456789") == strlen(service))) {
+ static const struct {
+ int socktype;
+ int protocol;
+ } list[] = {
+ { SOCK_STREAM, IPPROTO_TCP },
+ { SOCK_DGRAM, IPPROTO_UDP },
+ { SOCK_RAW, 0 }
+ };
+ struct addrinfo *ai = NULL;
+ int port = service ? (unsigned short)atoi(service): 0;
+ int hint_family = hints ? hints->ai_family : PF_UNSPEC;
+ int hint_socktype = hints ? hints->ai_socktype : 0;
+ int hint_protocol = hints ? hints->ai_protocol : 0;
+ char ipv4addr[4];
+#ifdef AF_INET6
+ char ipv6addr[16];
+ if ((hint_family == PF_UNSPEC || hint_family == PF_INET6) &&
+ strspn(node, "0123456789abcdefABCDEF.:") == strlen(node) &&
+ inet_pton(AF_INET6, node, ipv6addr)) {
+ int i;
+ for (i = numberof(list)-1; 0 <= i; i--) {
+ if ((hint_socktype == 0 || hint_socktype == list[i].socktype) &&
+ (hint_protocol == 0 || list[i].protocol == 0 || hint_protocol == list[i].protocol)) {
+ struct addrinfo *ai0 = xcalloc(1, sizeof(struct addrinfo));
+ struct sockaddr_in6 *sa = xmalloc(sizeof(struct sockaddr_in6));
+ INIT_SOCKADDR_IN6(sa, sizeof(struct sockaddr_in6));
+ memcpy(&sa->sin6_addr, ipv6addr, sizeof(ipv6addr));
+ sa->sin6_port = htons(port);
+ ai0->ai_family = PF_INET6;
+ ai0->ai_socktype = list[i].socktype;
+ ai0->ai_protocol = hint_protocol ? hint_protocol : list[i].protocol;
+ ai0->ai_addrlen = sizeof(struct sockaddr_in6);
+ ai0->ai_addr = (struct sockaddr *)sa;
+ ai0->ai_canonname = NULL;
+ ai0->ai_next = ai;
+ ai = ai0;
+ }
+ }
+ }
+ else
+#endif
+ if ((hint_family == PF_UNSPEC || hint_family == PF_INET) &&
+ strspn(node, "0123456789.") == strlen(node) &&
+ inet_pton(AF_INET, node, ipv4addr)) {
+ int i;
+ for (i = numberof(list)-1; 0 <= i; i--) {
+ if ((hint_socktype == 0 || hint_socktype == list[i].socktype) &&
+ (hint_protocol == 0 || list[i].protocol == 0 || hint_protocol == list[i].protocol)) {
+ struct addrinfo *ai0 = xcalloc(1, sizeof(struct addrinfo));
+ struct sockaddr_in *sa = xmalloc(sizeof(struct sockaddr_in));
+ INIT_SOCKADDR_IN(sa, sizeof(struct sockaddr_in));
+ memcpy(&sa->sin_addr, ipv4addr, sizeof(ipv4addr));
+ sa->sin_port = htons(port);
+ ai0->ai_family = PF_INET;
+ ai0->ai_socktype = list[i].socktype;
+ ai0->ai_protocol = hint_protocol ? hint_protocol : list[i].protocol;
+ ai0->ai_addrlen = sizeof(struct sockaddr_in);
+ ai0->ai_addr = (struct sockaddr *)sa;
+ ai0->ai_canonname = NULL;
+ ai0->ai_next = ai;
+ ai = ai0;
+ }
+ }
+ }
+ if (ai) {
+ *res = ai;
+ return 0;
+ }
+ }
+#endif
+ return EAI_FAIL;
+}
+
+int
+rb_getaddrinfo(const char *node, const char *service,
+ const struct addrinfo *hints,
+ struct rb_addrinfo **res)
+{
+ struct addrinfo *ai;
+ int ret;
+ int allocated_by_malloc = 0;
+
+ ret = numeric_getaddrinfo(node, service, hints, &ai);
+ if (ret == 0)
+ allocated_by_malloc = 1;
+ else {
+#ifdef GETADDRINFO_EMU
+ ret = getaddrinfo(node, service, hints, &ai);
+#else
+ struct getaddrinfo_arg arg;
+ MEMZERO(&arg, struct getaddrinfo_arg, 1);
+ arg.node = node;
+ arg.service = service;
+ arg.hints = hints;
+ arg.res = &ai;
+ ret = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getaddrinfo, &arg, RUBY_UBF_IO, 0);
+#endif
+ }
+
+ if (ret == 0) {
+ *res = (struct rb_addrinfo *)xmalloc(sizeof(struct rb_addrinfo));
+ (*res)->allocated_by_malloc = allocated_by_malloc;
+ (*res)->ai = ai;
+ }
+ return ret;
+}
+
+void
+rb_freeaddrinfo(struct rb_addrinfo *ai)
+{
+ if (!ai->allocated_by_malloc)
+ freeaddrinfo(ai->ai);
+ else {
+ struct addrinfo *ai1, *ai2;
+ ai1 = ai->ai;
+ while (ai1) {
+ ai2 = ai1->ai_next;
+ xfree(ai1->ai_addr);
+ xfree(ai1);
+ ai1 = ai2;
+ }
+ }
+ xfree(ai);
+}
+
+#ifndef GETADDRINFO_EMU
+struct getnameinfo_arg
+{
+ const struct sockaddr *sa;
+ socklen_t salen;
+ int flags;
+ char *host;
+ size_t hostlen;
+ char *serv;
+ size_t servlen;
+};
+
+static void *
+nogvl_getnameinfo(void *arg)
+{
+ struct getnameinfo_arg *ptr = arg;
+ return (void *)(VALUE)getnameinfo(ptr->sa, ptr->salen,
+ ptr->host, (socklen_t)ptr->hostlen,
+ ptr->serv, (socklen_t)ptr->servlen,
+ ptr->flags);
+}
+#endif
+
+int
+rb_getnameinfo(const struct sockaddr *sa, socklen_t salen,
+ char *host, size_t hostlen,
+ char *serv, size_t servlen, int flags)
+{
+#ifdef GETADDRINFO_EMU
+ return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
+#else
+ struct getnameinfo_arg arg;
+ int ret;
+ arg.sa = sa;
+ arg.salen = salen;
+ arg.host = host;
+ arg.hostlen = hostlen;
+ arg.serv = serv;
+ arg.servlen = servlen;
+ arg.flags = flags;
+ ret = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getnameinfo, &arg, RUBY_UBF_IO, 0);
+ return ret;
+#endif
+}
+
+static void
+make_ipaddr0(struct sockaddr *addr, socklen_t addrlen, char *buf, size_t buflen)
+{
+ int error;
+
+ 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, socklen_t addrlen)
+{
+ char hbuf[1024];
+
+ make_ipaddr0(addr, addrlen, hbuf, sizeof(hbuf));
+ return rb_str_new2(hbuf);
+}
+
+static void
+make_inetaddr(unsigned int host, char *buf, size_t buflen)
+{
+ struct sockaddr_in sin;
+
+ INIT_SOCKADDR_IN(&sin, sizeof(sin));
+ sin.sin_addr.s_addr = host;
+ make_ipaddr0((struct sockaddr*)&sin, sizeof(sin), buf, buflen);
+}
+
+static int
+str_is_number(const char *p)
+{
+ char *ep;
+
+ if (!p || *p == '\0')
+ return 0;
+ ep = NULL;
+ (void)STRTOUL(p, &ep, 10);
+ if (ep && *ep == '\0')
+ return 1;
+ else
+ return 0;
+}
+
+static char*
+host_str(VALUE host, char *hbuf, size_t hbuflen, int *flags_ptr)
+{
+ if (NIL_P(host)) {
+ return NULL;
+ }
+ else if (rb_obj_is_kind_of(host, rb_cInteger)) {
+ unsigned int i = NUM2UINT(host);
+
+ make_inetaddr(htonl(i), hbuf, hbuflen);
+ if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
+ return hbuf;
+ }
+ else {
+ char *name;
+
+ SafeStringValue(host);
+ name = RSTRING_PTR(host);
+ if (!name || *name == 0 || (name[0] == '<' && strcmp(name, "<any>") == 0)) {
+ 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, hbuflen);
+ if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
+ }
+ else if (strlen(name) >= hbuflen) {
+ rb_raise(rb_eArgError, "hostname too long (%"PRIuSIZE")",
+ strlen(name));
+ }
+ else {
+ strcpy(hbuf, name);
+ }
+ return hbuf;
+ }
+}
+
+static char*
+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, pbuflen, "%ld", FIX2LONG(port));
+#ifdef AI_NUMERICSERV
+ if (flags_ptr) *flags_ptr |= AI_NUMERICSERV;
+#endif
+ return pbuf;
+ }
+ else {
+ char *serv;
+
+ SafeStringValue(port);
+ serv = RSTRING_PTR(port);
+ if (strlen(serv) >= pbuflen) {
+ rb_raise(rb_eArgError, "service name too long (%"PRIuSIZE")",
+ strlen(serv));
+ }
+ strcpy(pbuf, serv);
+ return pbuf;
+ }
+}
+
+struct rb_addrinfo*
+rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack)
+{
+ struct rb_addrinfo* res = NULL;
+ char *hostp, *portp;
+ int error;
+ char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
+ int additional_flags = 0;
+
+ hostp = host_str(host, hbuf, sizeof(hbuf), &additional_flags);
+ portp = port_str(port, pbuf, sizeof(pbuf), &additional_flags);
+
+ if (socktype_hack && hints->ai_socktype == 0 && str_is_number(portp)) {
+ hints->ai_socktype = SOCK_DGRAM;
+ }
+ hints->ai_flags |= additional_flags;
+
+ error = rb_getaddrinfo(hostp, portp, hints, &res);
+ if (error) {
+ if (hostp && hostp[strlen(hostp)-1] == '\n') {
+ rb_raise(rb_eSocket, "newline at the end of hostname");
+ }
+ rsock_raise_socket_error("getaddrinfo", error);
+ }
+
+ return res;
+}
+
+struct rb_addrinfo*
+rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags)
+{
+ struct addrinfo hints;
+
+ MEMZERO(&hints, struct addrinfo, 1);
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = socktype;
+ hints.ai_flags = flags;
+ return rsock_getaddrinfo(host, port, &hints, 1);
+}
+
+VALUE
+rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
+{
+ VALUE family, port, addr1, addr2;
+ VALUE ary;
+ int error;
+ char hbuf[1024], pbuf[1024];
+ ID id;
+
+ id = rsock_intern_family(sockaddr->sa_family);
+ if (id) {
+ family = rb_str_dup(rb_id2str(id));
+ }
+ else {
+ sprintf(pbuf, "unknown:%d", sockaddr->sa_family);
+ family = rb_str_new2(pbuf);
+ }
+
+ addr1 = Qnil;
+ if (!norevlookup) {
+ error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf),
+ NULL, 0, 0);
+ if (! error) {
+ addr1 = rb_str_new2(hbuf);
+ }
+ }
+ error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf),
+ pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV);
+ if (error) {
+ rsock_raise_socket_error("getnameinfo", error);
+ }
+ addr2 = rb_str_new2(hbuf);
+ if (addr1 == Qnil) {
+ addr1 = addr2;
+ }
+ port = INT2FIX(atoi(pbuf));
+ ary = rb_ary_new3(4, family, port, addr1, addr2);
+
+ return ary;
+}
+
+#ifdef HAVE_SYS_UN_H
+VALUE
+rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len)
+{
+ char *s, *e;
+ s = sockaddr->sun_path;
+ e = (char *)sockaddr + len;
+ while (s < e && *(e-1) == '\0')
+ e--;
+ if (s <= e)
+ return rb_str_new(s, e-s);
+ else
+ return rb_str_new2("");
+}
+
+VALUE
+rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len)
+{
+ return rb_assoc_new(rb_str_new2("AF_UNIX"),
+ rsock_unixpath_str(sockaddr, len));
+}
+
+socklen_t
+rsock_unix_sockaddr_len(VALUE path)
+{
+#ifdef __linux__
+ if (RSTRING_LEN(path) == 0) {
+ /* autobind; see unix(7) for details. */
+ return (socklen_t) sizeof(sa_family_t);
+ }
+ else if (RSTRING_PTR(path)[0] == '\0') {
+ /* abstract namespace; see unix(7) for details. */
+ if (SOCKLEN_MAX - offsetof(struct sockaddr_un, sun_path) < (size_t)RSTRING_LEN(path))
+ rb_raise(rb_eArgError, "Linux abstract socket too long");
+ return (socklen_t) offsetof(struct sockaddr_un, sun_path) +
+ RSTRING_SOCKLEN(path);
+ }
+ else {
+#endif
+ return (socklen_t) sizeof(struct sockaddr_un);
+#ifdef __linux__
+ }
+#endif
+}
+#endif
+
+struct hostent_arg {
+ VALUE host;
+ struct rb_addrinfo* addr;
+ VALUE (*ipaddr)(struct sockaddr*, socklen_t);
+};
+
+static VALUE
+make_hostent_internal(struct hostent_arg *arg)
+{
+ VALUE host = arg->host;
+ struct addrinfo* addr = arg->addr->ai;
+ VALUE (*ipaddr)(struct sockaddr*, socklen_t) = arg->ipaddr;
+
+ struct addrinfo *ai;
+ struct hostent *h;
+ VALUE ary, names;
+ char **pch;
+ const char* hostp;
+ char hbuf[NI_MAXHOST];
+
+ ary = rb_ary_new();
+ if (addr->ai_canonname) {
+ hostp = addr->ai_canonname;
+ }
+ else {
+ hostp = host_str(host, hbuf, sizeof(hbuf), NULL);
+ }
+ rb_ary_push(ary, rb_str_new2(hostp));
+
+ if (addr->ai_canonname && strlen(addr->ai_canonname) < NI_MAXHOST &&
+ (h = gethostbyname(addr->ai_canonname))) {
+ names = rb_ary_new();
+ if (h->h_aliases != NULL) {
+ for (pch = h->h_aliases; *pch; pch++) {
+ rb_ary_push(names, rb_str_new2(*pch));
+ }
+ }
+ }
+ else {
+ names = rb_ary_new2(0);
+ }
+ rb_ary_push(ary, names);
+ rb_ary_push(ary, INT2NUM(addr->ai_family));
+ for (ai = addr; ai; ai = ai->ai_next) {
+ rb_ary_push(ary, (*ipaddr)(ai->ai_addr, ai->ai_addrlen));
+ }
+
+ return ary;
+}
+
+VALUE
+rsock_freeaddrinfo(VALUE arg)
+{
+ struct rb_addrinfo *addr = (struct rb_addrinfo *)arg;
+ rb_freeaddrinfo(addr);
+ return Qnil;
+}
+
+VALUE
+rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t))
+{
+ struct hostent_arg arg;
+
+ arg.host = host;
+ arg.addr = addr;
+ arg.ipaddr = ipaddr;
+ return rb_ensure(make_hostent_internal, (VALUE)&arg,
+ rsock_freeaddrinfo, (VALUE)addr);
+}
+
+typedef struct {
+ VALUE inspectname;
+ VALUE canonname;
+ int pfamily;
+ int socktype;
+ int protocol;
+ socklen_t sockaddr_len;
+ union_sockaddr addr;
+} rb_addrinfo_t;
+
+static void
+addrinfo_mark(void *ptr)
+{
+ rb_addrinfo_t *rai = ptr;
+ if (rai) {
+ rb_gc_mark(rai->inspectname);
+ rb_gc_mark(rai->canonname);
+ }
+}
+
+#define addrinfo_free RUBY_TYPED_DEFAULT_FREE
+
+static size_t
+addrinfo_memsize(const void *ptr)
+{
+ return ptr ? sizeof(rb_addrinfo_t) : 0;
+}
+
+static const rb_data_type_t addrinfo_type = {
+ "socket/addrinfo",
+ {addrinfo_mark, addrinfo_free, addrinfo_memsize,},
+};
+
+static VALUE
+addrinfo_s_allocate(VALUE klass)
+{
+ return TypedData_Wrap_Struct(klass, &addrinfo_type, 0);
+}
+
+#define IS_ADDRINFO(obj) rb_typeddata_is_kind_of((obj), &addrinfo_type)
+static inline rb_addrinfo_t *
+check_addrinfo(VALUE self)
+{
+ return rb_check_typeddata(self, &addrinfo_type);
+}
+
+static rb_addrinfo_t *
+get_addrinfo(VALUE self)
+{
+ rb_addrinfo_t *rai = check_addrinfo(self);
+
+ if (!rai) {
+ rb_raise(rb_eTypeError, "uninitialized socket address");
+ }
+ return rai;
+}
+
+
+static rb_addrinfo_t *
+alloc_addrinfo(void)
+{
+ rb_addrinfo_t *rai = ZALLOC(rb_addrinfo_t);
+ rai->inspectname = Qnil;
+ rai->canonname = Qnil;
+ return rai;
+}
+
+static void
+init_addrinfo(rb_addrinfo_t *rai, struct sockaddr *sa, socklen_t len,
+ int pfamily, int socktype, int protocol,
+ VALUE canonname, VALUE inspectname)
+{
+ if ((socklen_t)sizeof(rai->addr) < len)
+ rb_raise(rb_eArgError, "sockaddr string too big");
+ memcpy((void *)&rai->addr, (void *)sa, len);
+ rai->sockaddr_len = len;
+
+ rai->pfamily = pfamily;
+ rai->socktype = socktype;
+ rai->protocol = protocol;
+ rai->canonname = canonname;
+ rai->inspectname = inspectname;
+}
+
+VALUE
+rsock_addrinfo_new(struct sockaddr *addr, socklen_t len,
+ int family, int socktype, int protocol,
+ VALUE canonname, VALUE inspectname)
+{
+ VALUE a;
+ rb_addrinfo_t *rai;
+
+ a = addrinfo_s_allocate(rb_cAddrinfo);
+ DATA_PTR(a) = rai = alloc_addrinfo();
+ init_addrinfo(rai, addr, len, family, socktype, protocol, canonname, inspectname);
+ return a;
+}
+
+static struct rb_addrinfo *
+call_getaddrinfo(VALUE node, VALUE service,
+ VALUE family, VALUE socktype, VALUE protocol, VALUE flags,
+ int socktype_hack)
+{
+ struct addrinfo hints;
+ struct rb_addrinfo *res;
+
+ MEMZERO(&hints, struct addrinfo, 1);
+ hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family);
+
+ if (!NIL_P(socktype)) {
+ hints.ai_socktype = rsock_socktype_arg(socktype);
+ }
+ if (!NIL_P(protocol)) {
+ hints.ai_protocol = NUM2INT(protocol);
+ }
+ if (!NIL_P(flags)) {
+ hints.ai_flags = NUM2INT(flags);
+ }
+ res = rsock_getaddrinfo(node, service, &hints, socktype_hack);
+
+ if (res == NULL)
+ rb_raise(rb_eSocket, "host not found");
+ return res;
+}
+
+static VALUE make_inspectname(VALUE node, VALUE service, struct addrinfo *res);
+
+static void
+init_addrinfo_getaddrinfo(rb_addrinfo_t *rai, VALUE node, VALUE service,
+ VALUE family, VALUE socktype, VALUE protocol, VALUE flags,
+ VALUE inspectnode, VALUE inspectservice)
+{
+ struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 1);
+ VALUE canonname;
+ VALUE inspectname = rb_str_equal(node, inspectnode) ? Qnil : make_inspectname(inspectnode, inspectservice, res->ai);
+
+ canonname = Qnil;
+ if (res->ai->ai_canonname) {
+ canonname = rb_tainted_str_new_cstr(res->ai->ai_canonname);
+ OBJ_FREEZE(canonname);
+ }
+
+ init_addrinfo(rai, res->ai->ai_addr, res->ai->ai_addrlen,
+ NUM2INT(family), NUM2INT(socktype), NUM2INT(protocol),
+ canonname, inspectname);
+
+ rb_freeaddrinfo(res);
+}
+
+static VALUE
+make_inspectname(VALUE node, VALUE service, struct addrinfo *res)
+{
+ VALUE inspectname = Qnil;
+
+ if (res) {
+ /* drop redundant information which also shown in address:port part. */
+ char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
+ int ret;
+ ret = rb_getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
+ sizeof(hbuf), pbuf, sizeof(pbuf),
+ NI_NUMERICHOST|NI_NUMERICSERV);
+ if (ret == 0) {
+ if (RB_TYPE_P(node, T_STRING) && strcmp(hbuf, RSTRING_PTR(node)) == 0)
+ node = Qnil;
+ if (RB_TYPE_P(service, T_STRING) && strcmp(pbuf, RSTRING_PTR(service)) == 0)
+ service = Qnil;
+ else if (RB_TYPE_P(service, T_FIXNUM) && atoi(pbuf) == FIX2INT(service))
+ service = Qnil;
+ }
+ }
+
+ if (RB_TYPE_P(node, T_STRING)) {
+ inspectname = rb_str_dup(node);
+ }
+ if (RB_TYPE_P(service, T_STRING)) {
+ if (NIL_P(inspectname))
+ inspectname = rb_sprintf(":%s", StringValueCStr(service));
+ else
+ rb_str_catf(inspectname, ":%s", StringValueCStr(service));
+ }
+ else if (RB_TYPE_P(service, T_FIXNUM) && FIX2INT(service) != 0)
+ {
+ if (NIL_P(inspectname))
+ inspectname = rb_sprintf(":%d", FIX2INT(service));
+ else
+ rb_str_catf(inspectname, ":%d", FIX2INT(service));
+ }
+ if (!NIL_P(inspectname)) {
+ OBJ_INFECT(inspectname, node);
+ OBJ_INFECT(inspectname, service);
+ OBJ_FREEZE(inspectname);
+ }
+ return inspectname;
+}
+
+static VALUE
+addrinfo_firstonly_new(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags)
+{
+ VALUE ret;
+ VALUE canonname;
+ VALUE inspectname;
+
+ struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0);
+
+ inspectname = make_inspectname(node, service, res->ai);
+
+ canonname = Qnil;
+ if (res->ai->ai_canonname) {
+ canonname = rb_tainted_str_new_cstr(res->ai->ai_canonname);
+ OBJ_FREEZE(canonname);
+ }
+
+ ret = rsock_addrinfo_new(res->ai->ai_addr, res->ai->ai_addrlen,
+ res->ai->ai_family, res->ai->ai_socktype,
+ res->ai->ai_protocol,
+ canonname, inspectname);
+
+ rb_freeaddrinfo(res);
+ return ret;
+}
+
+static VALUE
+addrinfo_list_new(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags)
+{
+ VALUE ret;
+ struct addrinfo *r;
+ VALUE inspectname;
+
+ struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0);
+
+ inspectname = make_inspectname(node, service, res->ai);
+
+ ret = rb_ary_new();
+ for (r = res->ai; r; r = r->ai_next) {
+ VALUE addr;
+ VALUE canonname = Qnil;
+
+ if (r->ai_canonname) {
+ canonname = rb_tainted_str_new_cstr(r->ai_canonname);
+ OBJ_FREEZE(canonname);
+ }
+
+ addr = rsock_addrinfo_new(r->ai_addr, r->ai_addrlen,
+ r->ai_family, r->ai_socktype, r->ai_protocol,
+ canonname, inspectname);
+
+ rb_ary_push(ret, addr);
+ }
+
+ rb_freeaddrinfo(res);
+ return ret;
+}
+
+
+#ifdef HAVE_SYS_UN_H
+static void
+init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype)
+{
+ struct sockaddr_un un;
+ socklen_t len;
+
+ StringValue(path);
+
+ if (sizeof(un.sun_path) < (size_t)RSTRING_LEN(path))
+ rb_raise(rb_eArgError,
+ "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
+ (size_t)RSTRING_LEN(path), sizeof(un.sun_path));
+
+ INIT_SOCKADDR_UN(&un, sizeof(struct sockaddr_un));
+ memcpy((void*)&un.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
+
+ len = rsock_unix_sockaddr_len(path);
+ init_addrinfo(rai, (struct sockaddr *)&un, len,
+ PF_UNIX, socktype, 0, Qnil, Qnil);
+}
+#endif
+
+/*
+ * call-seq:
+ * Addrinfo.new(sockaddr) => addrinfo
+ * Addrinfo.new(sockaddr, family) => addrinfo
+ * Addrinfo.new(sockaddr, family, socktype) => addrinfo
+ * Addrinfo.new(sockaddr, family, socktype, protocol) => addrinfo
+ *
+ * returns a new instance of Addrinfo.
+ * The instance contains sockaddr, family, socktype, protocol.
+ * sockaddr means struct sockaddr which can be used for connect(2), etc.
+ * family, socktype and protocol are integers which is used for arguments of socket(2).
+ *
+ * sockaddr is specified as an array or a string.
+ * The array should be compatible to the value of IPSocket#addr or UNIXSocket#addr.
+ * The string should be struct sockaddr as generated by
+ * Socket.sockaddr_in or Socket.unpack_sockaddr_un.
+ *
+ * sockaddr examples:
+ * - ["AF_INET", 46102, "localhost.localdomain", "127.0.0.1"]
+ * - ["AF_INET6", 42304, "ip6-localhost", "::1"]
+ * - ["AF_UNIX", "/tmp/sock"]
+ * - Socket.sockaddr_in("smtp", "2001:DB8::1")
+ * - Socket.sockaddr_in(80, "172.18.22.42")
+ * - Socket.sockaddr_in(80, "www.ruby-lang.org")
+ * - Socket.sockaddr_un("/tmp/sock")
+ *
+ * In an AF_INET/AF_INET6 sockaddr array, the 4th element,
+ * numeric IP address, is used to construct socket address in the Addrinfo instance.
+ * If the 3rd element, textual host name, is non-nil, it is also recorded but used only for Addrinfo#inspect.
+ *
+ * family is specified as an integer to specify the protocol family such as Socket::PF_INET.
+ * It can be a symbol or a string which is the constant name
+ * with or without PF_ prefix such as :INET, :INET6, :UNIX, "PF_INET", etc.
+ * If omitted, PF_UNSPEC is assumed.
+ *
+ * socktype is specified as an integer to specify the socket type such as Socket::SOCK_STREAM.
+ * It can be a symbol or a string which is the constant name
+ * with or without SOCK_ prefix such as :STREAM, :DGRAM, :RAW, "SOCK_STREAM", etc.
+ * If omitted, 0 is assumed.
+ *
+ * protocol is specified as an integer to specify the protocol such as Socket::IPPROTO_TCP.
+ * It must be an integer, unlike family and socktype.
+ * If omitted, 0 is assumed.
+ * Note that 0 is reasonable value for most protocols, except raw socket.
+ *
+ */
+static VALUE
+addrinfo_initialize(int argc, VALUE *argv, VALUE self)
+{
+ rb_addrinfo_t *rai;
+ VALUE sockaddr_arg, sockaddr_ary, pfamily, socktype, protocol;
+ int i_pfamily, i_socktype, i_protocol;
+ struct sockaddr *sockaddr_ptr;
+ socklen_t sockaddr_len;
+ VALUE canonname = Qnil, inspectname = Qnil;
+
+ if (check_addrinfo(self))
+ rb_raise(rb_eTypeError, "already initialized socket address");
+ DATA_PTR(self) = rai = alloc_addrinfo();
+
+ rb_scan_args(argc, argv, "13", &sockaddr_arg, &pfamily, &socktype, &protocol);
+
+ i_pfamily = NIL_P(pfamily) ? PF_UNSPEC : rsock_family_arg(pfamily);
+ i_socktype = NIL_P(socktype) ? 0 : rsock_socktype_arg(socktype);
+ i_protocol = NIL_P(protocol) ? 0 : NUM2INT(protocol);
+
+ sockaddr_ary = rb_check_array_type(sockaddr_arg);
+ if (!NIL_P(sockaddr_ary)) {
+ VALUE afamily = rb_ary_entry(sockaddr_ary, 0);
+ int af;
+ StringValue(afamily);
+ if (rsock_family_to_int(RSTRING_PTR(afamily), RSTRING_LEN(afamily), &af) == -1)
+ rb_raise(rb_eSocket, "unknown address family: %s", StringValueCStr(afamily));
+ switch (af) {
+ case AF_INET: /* ["AF_INET", 46102, "localhost.localdomain", "127.0.0.1"] */
+#ifdef INET6
+ case AF_INET6: /* ["AF_INET6", 42304, "ip6-localhost", "::1"] */
+#endif
+ {
+ VALUE service = rb_ary_entry(sockaddr_ary, 1);
+ VALUE nodename = rb_ary_entry(sockaddr_ary, 2);
+ VALUE numericnode = rb_ary_entry(sockaddr_ary, 3);
+ int flags;
+
+ service = INT2NUM(NUM2INT(service));
+ if (!NIL_P(nodename))
+ StringValue(nodename);
+ StringValue(numericnode);
+ flags = AI_NUMERICHOST;
+#ifdef AI_NUMERICSERV
+ flags |= AI_NUMERICSERV;
+#endif
+
+ init_addrinfo_getaddrinfo(rai, numericnode, service,
+ INT2NUM(i_pfamily ? i_pfamily : af), INT2NUM(i_socktype), INT2NUM(i_protocol),
+ INT2NUM(flags),
+ nodename, service);
+ break;
+ }
+
+#ifdef HAVE_SYS_UN_H
+ case AF_UNIX: /* ["AF_UNIX", "/tmp/sock"] */
+ {
+ VALUE path = rb_ary_entry(sockaddr_ary, 1);
+ StringValue(path);
+ init_unix_addrinfo(rai, path, SOCK_STREAM);
+ break;
+ }
+#endif
+
+ default:
+ rb_raise(rb_eSocket, "unexpected address family");
+ }
+ }
+ else {
+ StringValue(sockaddr_arg);
+ sockaddr_ptr = (struct sockaddr *)RSTRING_PTR(sockaddr_arg);
+ sockaddr_len = RSTRING_SOCKLEN(sockaddr_arg);
+ init_addrinfo(rai, sockaddr_ptr, sockaddr_len,
+ i_pfamily, i_socktype, i_protocol,
+ canonname, inspectname);
+ }
+
+ return self;
+}
+
+static int
+get_afamily(struct sockaddr *addr, socklen_t len)
+{
+ if ((socklen_t)((char*)&addr->sa_family + sizeof(addr->sa_family) - (char*)addr) <= len)
+ return addr->sa_family;
+ else
+ return AF_UNSPEC;
+}
+
+static int
+ai_get_afamily(rb_addrinfo_t *rai)
+{
+ return get_afamily(&rai->addr.addr, rai->sockaddr_len);
+}
+
+static VALUE
+inspect_sockaddr(VALUE addrinfo, VALUE ret)
+{
+ rb_addrinfo_t *rai = get_addrinfo(addrinfo);
+ union_sockaddr *sockaddr = &rai->addr;
+ socklen_t socklen = rai->sockaddr_len;
+ return rsock_inspect_sockaddr((struct sockaddr *)sockaddr, socklen, ret);
+}
+
+VALUE
+rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE ret)
+{
+ union_sockaddr *sockaddr = (union_sockaddr *)sockaddr_arg;
+ if (socklen == 0) {
+ rb_str_cat2(ret, "empty-sockaddr");
+ }
+ else if ((long)socklen < ((char*)&sockaddr->addr.sa_family + sizeof(sockaddr->addr.sa_family)) - (char*)sockaddr)
+ rb_str_cat2(ret, "too-short-sockaddr");
+ else {
+ switch (sockaddr->addr.sa_family) {
+ case AF_UNSPEC:
+ {
+ rb_str_cat2(ret, "UNSPEC");
+ break;
+ }
+
+ case AF_INET:
+ {
+ struct sockaddr_in *addr;
+ int port;
+ addr = &sockaddr->in;
+ if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+0+1) <= socklen)
+ rb_str_catf(ret, "%d", ((unsigned char*)&addr->sin_addr)[0]);
+ else
+ rb_str_cat2(ret, "?");
+ if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+1+1) <= socklen)
+ rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[1]);
+ else
+ rb_str_cat2(ret, ".?");
+ if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+2+1) <= socklen)
+ rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[2]);
+ else
+ rb_str_cat2(ret, ".?");
+ if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+3+1) <= socklen)
+ rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[3]);
+ else
+ rb_str_cat2(ret, ".?");
+
+ if ((socklen_t)(((char*)&addr->sin_port)-(char*)addr+(int)sizeof(addr->sin_port)) < socklen) {
+ port = ntohs(addr->sin_port);
+ if (port)
+ rb_str_catf(ret, ":%d", port);
+ }
+ else {
+ rb_str_cat2(ret, ":?");
+ }
+ if ((socklen_t)sizeof(struct sockaddr_in) != socklen)
+ rb_str_catf(ret, " (%d bytes for %d bytes sockaddr_in)",
+ (int)socklen,
+ (int)sizeof(struct sockaddr_in));
+ break;
+ }
+
+#ifdef AF_INET6
+ case AF_INET6:
+ {
+ struct sockaddr_in6 *addr;
+ char hbuf[1024];
+ int port;
+ int error;
+ if (socklen < (socklen_t)sizeof(struct sockaddr_in6)) {
+ rb_str_catf(ret, "too-short-AF_INET6-sockaddr %d bytes", (int)socklen);
+ }
+ else {
+ addr = &sockaddr->in6;
+ /* use getnameinfo for scope_id.
+ * RFC 4007: IPv6 Scoped Address Architecture
+ * draft-ietf-ipv6-scope-api-00.txt: Scoped Address Extensions to the IPv6 Basic Socket API
+ */
+ error = getnameinfo(&sockaddr->addr, socklen,
+ hbuf, (socklen_t)sizeof(hbuf), NULL, 0,
+ NI_NUMERICHOST|NI_NUMERICSERV);
+ if (error) {
+ rsock_raise_socket_error("getnameinfo", error);
+ }
+ if (addr->sin6_port == 0) {
+ rb_str_cat2(ret, hbuf);
+ }
+ else {
+ port = ntohs(addr->sin6_port);
+ rb_str_catf(ret, "[%s]:%d", hbuf, port);
+ }
+ if ((socklen_t)sizeof(struct sockaddr_in6) < socklen)
+ rb_str_catf(ret, "(sockaddr %d bytes too long)", (int)(socklen - sizeof(struct sockaddr_in6)));
+ }
+ break;
+ }
+#endif
+
+#ifdef HAVE_SYS_UN_H
+ case AF_UNIX:
+ {
+ struct sockaddr_un *addr = &sockaddr->un;
+ char *p, *s, *e;
+ s = addr->sun_path;
+ e = (char*)addr + socklen;
+ while (s < e && *(e-1) == '\0')
+ e--;
+ if (e < s)
+ rb_str_cat2(ret, "too-short-AF_UNIX-sockaddr");
+ else if (s == e)
+ rb_str_cat2(ret, "empty-path-AF_UNIX-sockaddr");
+ else {
+ int printable_only = 1;
+ p = s;
+ while (p < e) {
+ printable_only = printable_only && ISPRINT(*p) && !ISSPACE(*p);
+ p++;
+ }
+ if (printable_only) { /* only printable, no space */
+ if (s[0] != '/') /* relative path */
+ rb_str_cat2(ret, "UNIX ");
+ rb_str_cat(ret, s, p - s);
+ }
+ else {
+ rb_str_cat2(ret, "UNIX");
+ while (s < e)
+ rb_str_catf(ret, ":%02x", (unsigned char)*s++);
+ }
+ }
+ break;
+ }
+#endif
+
+#if defined(AF_PACKET) && defined(__linux__)
+ /* GNU/Linux */
+ case AF_PACKET:
+ {
+ struct sockaddr_ll *addr;
+ const char *sep = "[";
+#define CATSEP do { rb_str_cat2(ret, sep); sep = " "; } while (0);
+
+ addr = (struct sockaddr_ll *)sockaddr;
+
+ rb_str_cat2(ret, "PACKET");
+
+ if (offsetof(struct sockaddr_ll, sll_protocol) + sizeof(addr->sll_protocol) <= (size_t)socklen) {
+ CATSEP;
+ rb_str_catf(ret, "protocol=%d", ntohs(addr->sll_protocol));
+ }
+ if (offsetof(struct sockaddr_ll, sll_ifindex) + sizeof(addr->sll_ifindex) <= (size_t)socklen) {
+ char buf[IFNAMSIZ];
+ CATSEP;
+ if (if_indextoname(addr->sll_ifindex, buf) == NULL)
+ rb_str_catf(ret, "ifindex=%d", addr->sll_ifindex);
+ else
+ rb_str_catf(ret, "%s", buf);
+ }
+ if (offsetof(struct sockaddr_ll, sll_hatype) + sizeof(addr->sll_hatype) <= (size_t)socklen) {
+ CATSEP;
+ rb_str_catf(ret, "hatype=%d", addr->sll_hatype);
+ }
+ if (offsetof(struct sockaddr_ll, sll_pkttype) + sizeof(addr->sll_pkttype) <= (size_t)socklen) {
+ CATSEP;
+ if (addr->sll_pkttype == PACKET_HOST)
+ rb_str_cat2(ret, "HOST");
+ else if (addr->sll_pkttype == PACKET_BROADCAST)
+ rb_str_cat2(ret, "BROADCAST");
+ else if (addr->sll_pkttype == PACKET_MULTICAST)
+ rb_str_cat2(ret, "MULTICAST");
+ else if (addr->sll_pkttype == PACKET_OTHERHOST)
+ rb_str_cat2(ret, "OTHERHOST");
+ else if (addr->sll_pkttype == PACKET_OUTGOING)
+ rb_str_cat2(ret, "OUTGOING");
+ else
+ rb_str_catf(ret, "pkttype=%d", addr->sll_pkttype);
+ }
+ if (socklen != (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + addr->sll_halen)) {
+ CATSEP;
+ if (offsetof(struct sockaddr_ll, sll_halen) + sizeof(addr->sll_halen) <= (size_t)socklen) {
+ rb_str_catf(ret, "halen=%d", addr->sll_halen);
+ }
+ }
+ if (offsetof(struct sockaddr_ll, sll_addr) < (size_t)socklen) {
+ socklen_t len, i;
+ CATSEP;
+ rb_str_cat2(ret, "hwaddr");
+ len = addr->sll_halen;
+ if ((size_t)socklen < offsetof(struct sockaddr_ll, sll_addr) + len)
+ len = socklen - offsetof(struct sockaddr_ll, sll_addr);
+ for (i = 0; i < len; i++) {
+ rb_str_cat2(ret, i == 0 ? "=" : ":");
+ rb_str_catf(ret, "%02x", addr->sll_addr[i]);
+ }
+ }
+
+ if (socklen < (socklen_t)(offsetof(struct sockaddr_ll, sll_halen) + sizeof(addr->sll_halen)) ||
+ (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + addr->sll_halen) != socklen) {
+ CATSEP;
+ rb_str_catf(ret, "(%d bytes for %d bytes sockaddr_ll)",
+ (int)socklen, (int)sizeof(struct sockaddr_ll));
+ }
+
+ rb_str_cat2(ret, "]");
+#undef CATSEP
+
+ break;
+ }
+#endif
+
+#if defined(AF_LINK) && defined(HAVE_TYPE_STRUCT_SOCKADDR_DL)
+ /* AF_LINK is defined in 4.4BSD derivations since Net2.
+ link_ntoa is also defined at Net2.
+ However Debian GNU/kFreeBSD defines AF_LINK but
+ don't have link_ntoa. */
+ case AF_LINK:
+ {
+ /*
+ * Simple implementation using link_ntoa():
+ * This doesn't work on Debian GNU/kFreeBSD 6.0.7 (squeeze).
+ * Also, the format is bit different.
+ *
+ * rb_str_catf(ret, "LINK %s", link_ntoa(&sockaddr->dl));
+ * break;
+ */
+ struct sockaddr_dl *addr = &sockaddr->dl;
+ char *np = NULL, *ap = NULL, *endp;
+ int nlen = 0, alen = 0;
+ int i, off;
+ const char *sep = "[";
+#define CATSEP do { rb_str_cat2(ret, sep); sep = " "; } while (0);
+
+ rb_str_cat2(ret, "LINK");
+
+ endp = ((char *)addr) + socklen;
+
+ if (offsetof(struct sockaddr_dl, sdl_data) < socklen) {
+ np = addr->sdl_data;
+ nlen = addr->sdl_nlen;
+ if (endp - np < nlen)
+ nlen = (int)(endp - np);
+ }
+ off = addr->sdl_nlen;
+
+ if (offsetof(struct sockaddr_dl, sdl_data) + off < socklen) {
+ ap = addr->sdl_data + off;
+ alen = addr->sdl_alen;
+ if (endp - ap < alen)
+ alen = (int)(endp - ap);
+ }
+
+ CATSEP;
+ if (np)
+ rb_str_catf(ret, "%.*s", nlen, np);
+ else
+ rb_str_cat2(ret, "?");
+
+ if (ap && 0 < alen) {
+ CATSEP;
+ for (i = 0; i < alen; i++)
+ rb_str_catf(ret, "%s%02x", i == 0 ? "" : ":", (unsigned char)ap[i]);
+ }
+
+ if (socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_nlen) + sizeof(addr->sdl_nlen)) ||
+ socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_alen) + sizeof(addr->sdl_alen)) ||
+ socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_slen) + sizeof(addr->sdl_slen)) ||
+ /* longer length is possible behavior because struct sockaddr_dl has "minimum work area, can be larger" as the last field.
+ * cf. Net2:/usr/src/sys/net/if_dl.h. */
+ socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_data) + addr->sdl_nlen + addr->sdl_alen + addr->sdl_slen)) {
+ CATSEP;
+ rb_str_catf(ret, "(%d bytes for %d bytes sockaddr_dl)",
+ (int)socklen, (int)sizeof(struct sockaddr_dl));
+ }
+
+ rb_str_cat2(ret, "]");
+#undef CATSEP
+ break;
+ }
+#endif
+
+ default:
+ {
+ ID id = rsock_intern_family(sockaddr->addr.sa_family);
+ if (id == 0)
+ rb_str_catf(ret, "unknown address family %d", sockaddr->addr.sa_family);
+ else
+ rb_str_catf(ret, "%s address format unknown", rb_id2name(id));
+ break;
+ }
+ }
+ }
+
+ return ret;
+}
+
+/*
+ * call-seq:
+ * addrinfo.inspect => string
+ *
+ * returns a string which shows addrinfo in human-readable form.
+ *
+ * Addrinfo.tcp("localhost", 80).inspect #=> "#<Addrinfo: 127.0.0.1:80 TCP (localhost)>"
+ * Addrinfo.unix("/tmp/sock").inspect #=> "#<Addrinfo: /tmp/sock SOCK_STREAM>"
+ *
+ */
+static VALUE
+addrinfo_inspect(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ int internet_p;
+ VALUE ret;
+
+ ret = rb_sprintf("#<%s: ", rb_obj_classname(self));
+
+ inspect_sockaddr(self, ret);
+
+ if (rai->pfamily && ai_get_afamily(rai) != rai->pfamily) {
+ ID id = rsock_intern_protocol_family(rai->pfamily);
+ if (id)
+ rb_str_catf(ret, " %s", rb_id2name(id));
+ else
+ rb_str_catf(ret, " PF_\?\?\?(%d)", rai->pfamily);
+ }
+
+ internet_p = rai->pfamily == PF_INET;
+#ifdef INET6
+ internet_p = internet_p || rai->pfamily == PF_INET6;
+#endif
+ if (internet_p && rai->socktype == SOCK_STREAM &&
+ (rai->protocol == 0 || rai->protocol == IPPROTO_TCP)) {
+ rb_str_cat2(ret, " TCP");
+ }
+ else if (internet_p && rai->socktype == SOCK_DGRAM &&
+ (rai->protocol == 0 || rai->protocol == IPPROTO_UDP)) {
+ rb_str_cat2(ret, " UDP");
+ }
+ else {
+ if (rai->socktype) {
+ ID id = rsock_intern_socktype(rai->socktype);
+ if (id)
+ rb_str_catf(ret, " %s", rb_id2name(id));
+ else
+ rb_str_catf(ret, " SOCK_\?\?\?(%d)", rai->socktype);
+ }
+
+ if (rai->protocol) {
+ if (internet_p) {
+ ID id = rsock_intern_ipproto(rai->protocol);
+ if (id)
+ rb_str_catf(ret, " %s", rb_id2name(id));
+ else
+ goto unknown_protocol;
+ }
+ else {
+ unknown_protocol:
+ rb_str_catf(ret, " UNKNOWN_PROTOCOL(%d)", rai->protocol);
+ }
+ }
+ }
+
+ if (!NIL_P(rai->canonname)) {
+ VALUE name = rai->canonname;
+ rb_str_catf(ret, " %s", StringValueCStr(name));
+ }
+
+ if (!NIL_P(rai->inspectname)) {
+ VALUE name = rai->inspectname;
+ rb_str_catf(ret, " (%s)", StringValueCStr(name));
+ }
+
+ rb_str_buf_cat2(ret, ">");
+ return ret;
+}
+
+/*
+ * call-seq:
+ * addrinfo.inspect_sockaddr => string
+ *
+ * returns a string which shows the sockaddr in _addrinfo_ with human-readable form.
+ *
+ * Addrinfo.tcp("localhost", 80).inspect_sockaddr #=> "127.0.0.1:80"
+ * Addrinfo.tcp("ip6-localhost", 80).inspect_sockaddr #=> "[::1]:80"
+ * Addrinfo.unix("/tmp/sock").inspect_sockaddr #=> "/tmp/sock"
+ *
+ */
+VALUE
+rsock_addrinfo_inspect_sockaddr(VALUE self)
+{
+ return inspect_sockaddr(self, rb_str_new("", 0));
+}
+
+/* :nodoc: */
+static VALUE
+addrinfo_mdump(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ VALUE sockaddr, afamily, pfamily, socktype, protocol, canonname, inspectname;
+ int afamily_int = ai_get_afamily(rai);
+ ID id;
+
+ id = rsock_intern_protocol_family(rai->pfamily);
+ if (id == 0)
+ rb_raise(rb_eSocket, "unknown protocol family: %d", rai->pfamily);
+ pfamily = rb_id2str(id);
+
+ if (rai->socktype == 0)
+ socktype = INT2FIX(0);
+ else {
+ id = rsock_intern_socktype(rai->socktype);
+ if (id == 0)
+ rb_raise(rb_eSocket, "unknown socktype: %d", rai->socktype);
+ socktype = rb_id2str(id);
+ }
+
+ if (rai->protocol == 0)
+ protocol = INT2FIX(0);
+ else if (IS_IP_FAMILY(afamily_int)) {
+ id = rsock_intern_ipproto(rai->protocol);
+ if (id == 0)
+ rb_raise(rb_eSocket, "unknown IP protocol: %d", rai->protocol);
+ protocol = rb_id2str(id);
+ }
+ else {
+ rb_raise(rb_eSocket, "unknown protocol: %d", rai->protocol);
+ }
+
+ canonname = rai->canonname;
+
+ inspectname = rai->inspectname;
+
+ id = rsock_intern_family(afamily_int);
+ if (id == 0)
+ rb_raise(rb_eSocket, "unknown address family: %d", afamily_int);
+ afamily = rb_id2str(id);
+
+ switch(afamily_int) {
+#ifdef HAVE_SYS_UN_H
+ case AF_UNIX:
+ {
+ struct sockaddr_un *su = &rai->addr.un;
+ char *s, *e;
+ s = su->sun_path;
+ e = (char*)su + rai->sockaddr_len;
+ while (s < e && *(e-1) == '\0')
+ e--;
+ sockaddr = rb_str_new(s, e-s);
+ break;
+ }
+#endif
+
+ default:
+ {
+ char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
+ int error;
+ error = getnameinfo(&rai->addr.addr, rai->sockaddr_len,
+ hbuf, (socklen_t)sizeof(hbuf), pbuf, (socklen_t)sizeof(pbuf),
+ NI_NUMERICHOST|NI_NUMERICSERV);
+ if (error) {
+ rsock_raise_socket_error("getnameinfo", error);
+ }
+ sockaddr = rb_assoc_new(rb_str_new_cstr(hbuf), rb_str_new_cstr(pbuf));
+ break;
+ }
+ }
+
+ return rb_ary_new3(7, afamily, sockaddr, pfamily, socktype, protocol, canonname, inspectname);
+}
+
+/* :nodoc: */
+static VALUE
+addrinfo_mload(VALUE self, VALUE ary)
+{
+ VALUE v;
+ VALUE canonname, inspectname;
+ int afamily, pfamily, socktype, protocol;
+ union_sockaddr ss;
+ socklen_t len;
+ rb_addrinfo_t *rai;
+
+ if (check_addrinfo(self))
+ rb_raise(rb_eTypeError, "already initialized socket address");
+
+ ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
+
+ v = rb_ary_entry(ary, 0);
+ StringValue(v);
+ if (rsock_family_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &afamily) == -1)
+ rb_raise(rb_eTypeError, "unexpected address family");
+
+ v = rb_ary_entry(ary, 2);
+ StringValue(v);
+ if (rsock_family_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &pfamily) == -1)
+ rb_raise(rb_eTypeError, "unexpected protocol family");
+
+ v = rb_ary_entry(ary, 3);
+ if (v == INT2FIX(0))
+ socktype = 0;
+ else {
+ StringValue(v);
+ if (rsock_socktype_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &socktype) == -1)
+ rb_raise(rb_eTypeError, "unexpected socktype");
+ }
+
+ v = rb_ary_entry(ary, 4);
+ if (v == INT2FIX(0))
+ protocol = 0;
+ else {
+ StringValue(v);
+ if (IS_IP_FAMILY(afamily)) {
+ if (rsock_ipproto_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &protocol) == -1)
+ rb_raise(rb_eTypeError, "unexpected protocol");
+ }
+ else {
+ rb_raise(rb_eTypeError, "unexpected protocol");
+ }
+ }
+
+ v = rb_ary_entry(ary, 5);
+ if (NIL_P(v))
+ canonname = Qnil;
+ else {
+ StringValue(v);
+ canonname = v;
+ }
+
+ v = rb_ary_entry(ary, 6);
+ if (NIL_P(v))
+ inspectname = Qnil;
+ else {
+ StringValue(v);
+ inspectname = v;
+ }
+
+ v = rb_ary_entry(ary, 1);
+ switch(afamily) {
+#ifdef HAVE_SYS_UN_H
+ case AF_UNIX:
+ {
+ struct sockaddr_un uaddr;
+ INIT_SOCKADDR_UN(&uaddr, sizeof(struct sockaddr_un));
+
+ StringValue(v);
+ if (sizeof(uaddr.sun_path) < (size_t)RSTRING_LEN(v))
+ rb_raise(rb_eSocket,
+ "too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
+ (size_t)RSTRING_LEN(v), sizeof(uaddr.sun_path));
+ memcpy(uaddr.sun_path, RSTRING_PTR(v), RSTRING_LEN(v));
+ len = (socklen_t)sizeof(uaddr);
+ memcpy(&ss, &uaddr, len);
+ break;
+ }
+#endif
+
+ default:
+ {
+ VALUE pair = rb_convert_type(v, T_ARRAY, "Array", "to_ary");
+ struct rb_addrinfo *res;
+ int flags = AI_NUMERICHOST;
+#ifdef AI_NUMERICSERV
+ flags |= AI_NUMERICSERV;
+#endif
+ res = call_getaddrinfo(rb_ary_entry(pair, 0), rb_ary_entry(pair, 1),
+ INT2NUM(pfamily), INT2NUM(socktype), INT2NUM(protocol),
+ INT2NUM(flags), 1);
+
+ len = res->ai->ai_addrlen;
+ memcpy(&ss, res->ai->ai_addr, res->ai->ai_addrlen);
+ rb_freeaddrinfo(res);
+ break;
+ }
+ }
+
+ DATA_PTR(self) = rai = alloc_addrinfo();
+ init_addrinfo(rai, &ss.addr, len,
+ pfamily, socktype, protocol,
+ canonname, inspectname);
+ return self;
+}
+
+/*
+ * call-seq:
+ * addrinfo.afamily => integer
+ *
+ * returns the address family as an integer.
+ *
+ * Addrinfo.tcp("localhost", 80).afamily == Socket::AF_INET #=> true
+ *
+ */
+static VALUE
+addrinfo_afamily(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ return INT2NUM(ai_get_afamily(rai));
+}
+
+/*
+ * call-seq:
+ * addrinfo.pfamily => integer
+ *
+ * returns the protocol family as an integer.
+ *
+ * Addrinfo.tcp("localhost", 80).pfamily == Socket::PF_INET #=> true
+ *
+ */
+static VALUE
+addrinfo_pfamily(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ return INT2NUM(rai->pfamily);
+}
+
+/*
+ * call-seq:
+ * addrinfo.socktype => integer
+ *
+ * returns the socket type as an integer.
+ *
+ * Addrinfo.tcp("localhost", 80).socktype == Socket::SOCK_STREAM #=> true
+ *
+ */
+static VALUE
+addrinfo_socktype(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ return INT2NUM(rai->socktype);
+}
+
+/*
+ * call-seq:
+ * addrinfo.protocol => integer
+ *
+ * returns the socket type as an integer.
+ *
+ * Addrinfo.tcp("localhost", 80).protocol == Socket::IPPROTO_TCP #=> true
+ *
+ */
+static VALUE
+addrinfo_protocol(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ return INT2NUM(rai->protocol);
+}
+
+/*
+ * call-seq:
+ * addrinfo.to_sockaddr => string
+ * addrinfo.to_s => string
+ *
+ * returns the socket address as packed struct sockaddr string.
+ *
+ * Addrinfo.tcp("localhost", 80).to_sockaddr
+ * #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
+ *
+ */
+static VALUE
+addrinfo_to_sockaddr(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ VALUE ret;
+ ret = rb_str_new((char*)&rai->addr, rai->sockaddr_len);
+ OBJ_INFECT(ret, self);
+ return ret;
+}
+
+/*
+ * call-seq:
+ * addrinfo.canonname => string or nil
+ *
+ * returns the canonical name as an string.
+ *
+ * nil is returned if no canonical name.
+ *
+ * The canonical name is set by Addrinfo.getaddrinfo when AI_CANONNAME is specified.
+ *
+ * list = Addrinfo.getaddrinfo("www.ruby-lang.org", 80, :INET, :STREAM, nil, Socket::AI_CANONNAME)
+ * p list[0] #=> #<Addrinfo: 221.186.184.68:80 TCP carbon.ruby-lang.org (www.ruby-lang.org)>
+ * p list[0].canonname #=> "carbon.ruby-lang.org"
+ *
+ */
+static VALUE
+addrinfo_canonname(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ return rai->canonname;
+}
+
+/*
+ * call-seq:
+ * addrinfo.ip? => true or false
+ *
+ * returns true if addrinfo is internet (IPv4/IPv6) address.
+ * returns false otherwise.
+ *
+ * Addrinfo.tcp("127.0.0.1", 80).ip? #=> true
+ * Addrinfo.tcp("::1", 80).ip? #=> true
+ * Addrinfo.unix("/tmp/sock").ip? #=> false
+ *
+ */
+static VALUE
+addrinfo_ip_p(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ int family = ai_get_afamily(rai);
+ return IS_IP_FAMILY(family) ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ * addrinfo.ipv4? => true or false
+ *
+ * returns true if addrinfo is IPv4 address.
+ * returns false otherwise.
+ *
+ * Addrinfo.tcp("127.0.0.1", 80).ipv4? #=> true
+ * Addrinfo.tcp("::1", 80).ipv4? #=> false
+ * Addrinfo.unix("/tmp/sock").ipv4? #=> false
+ *
+ */
+static VALUE
+addrinfo_ipv4_p(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ return ai_get_afamily(rai) == AF_INET ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ * addrinfo.ipv6? => true or false
+ *
+ * returns true if addrinfo is IPv6 address.
+ * returns false otherwise.
+ *
+ * Addrinfo.tcp("127.0.0.1", 80).ipv6? #=> false
+ * Addrinfo.tcp("::1", 80).ipv6? #=> true
+ * Addrinfo.unix("/tmp/sock").ipv6? #=> false
+ *
+ */
+static VALUE
+addrinfo_ipv6_p(VALUE self)
+{
+#ifdef AF_INET6
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ return ai_get_afamily(rai) == AF_INET6 ? Qtrue : Qfalse;
+#else
+ return Qfalse;
+#endif
+}
+
+/*
+ * call-seq:
+ * addrinfo.unix? => true or false
+ *
+ * returns true if addrinfo is UNIX address.
+ * returns false otherwise.
+ *
+ * Addrinfo.tcp("127.0.0.1", 80).unix? #=> false
+ * Addrinfo.tcp("::1", 80).unix? #=> false
+ * Addrinfo.unix("/tmp/sock").unix? #=> true
+ *
+ */
+static VALUE
+addrinfo_unix_p(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+#ifdef AF_UNIX
+ return ai_get_afamily(rai) == AF_UNIX ? Qtrue : Qfalse;
+#else
+ return Qfalse;
+#endif
+}
+
+/*
+ * call-seq:
+ * addrinfo.getnameinfo => [nodename, service]
+ * addrinfo.getnameinfo(flags) => [nodename, service]
+ *
+ * returns nodename and service as a pair of strings.
+ * This converts struct sockaddr in addrinfo to textual representation.
+ *
+ * flags should be bitwise OR of Socket::NI_??? constants.
+ *
+ * Addrinfo.tcp("127.0.0.1", 80).getnameinfo #=> ["localhost", "www"]
+ *
+ * Addrinfo.tcp("127.0.0.1", 80).getnameinfo(Socket::NI_NUMERICSERV)
+ * #=> ["localhost", "80"]
+ */
+static VALUE
+addrinfo_getnameinfo(int argc, VALUE *argv, VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ VALUE vflags;
+ char hbuf[1024], pbuf[1024];
+ int flags, error;
+
+ rb_scan_args(argc, argv, "01", &vflags);
+
+ flags = NIL_P(vflags) ? 0 : NUM2INT(vflags);
+
+ if (rai->socktype == SOCK_DGRAM)
+ flags |= NI_DGRAM;
+
+ error = getnameinfo(&rai->addr.addr, rai->sockaddr_len,
+ hbuf, (socklen_t)sizeof(hbuf), pbuf, (socklen_t)sizeof(pbuf),
+ flags);
+ if (error) {
+ rsock_raise_socket_error("getnameinfo", error);
+ }
+
+ return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
+}
+
+/*
+ * call-seq:
+ * addrinfo.ip_unpack => [addr, port]
+ *
+ * Returns the IP address and port number as 2-element array.
+ *
+ * Addrinfo.tcp("127.0.0.1", 80).ip_unpack #=> ["127.0.0.1", 80]
+ * Addrinfo.tcp("::1", 80).ip_unpack #=> ["::1", 80]
+ */
+static VALUE
+addrinfo_ip_unpack(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ int family = ai_get_afamily(rai);
+ VALUE vflags;
+ VALUE ret, portstr;
+
+ if (!IS_IP_FAMILY(family))
+ rb_raise(rb_eSocket, "need IPv4 or IPv6 address");
+
+ vflags = INT2NUM(NI_NUMERICHOST|NI_NUMERICSERV);
+ ret = addrinfo_getnameinfo(1, &vflags, self);
+ portstr = rb_ary_entry(ret, 1);
+ rb_ary_store(ret, 1, INT2NUM(atoi(StringValueCStr(portstr))));
+ return ret;
+}
+
+/*
+ * call-seq:
+ * addrinfo.ip_address => string
+ *
+ * Returns the IP address as a string.
+ *
+ * Addrinfo.tcp("127.0.0.1", 80).ip_address #=> "127.0.0.1"
+ * Addrinfo.tcp("::1", 80).ip_address #=> "::1"
+ */
+static VALUE
+addrinfo_ip_address(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ int family = ai_get_afamily(rai);
+ VALUE vflags;
+ VALUE ret;
+
+ if (!IS_IP_FAMILY(family))
+ rb_raise(rb_eSocket, "need IPv4 or IPv6 address");
+
+ vflags = INT2NUM(NI_NUMERICHOST|NI_NUMERICSERV);
+ ret = addrinfo_getnameinfo(1, &vflags, self);
+ return rb_ary_entry(ret, 0);
+}
+
+/*
+ * call-seq:
+ * addrinfo.ip_port => port
+ *
+ * Returns the port number as an integer.
+ *
+ * Addrinfo.tcp("127.0.0.1", 80).ip_port #=> 80
+ * Addrinfo.tcp("::1", 80).ip_port #=> 80
+ */
+static VALUE
+addrinfo_ip_port(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ int family = ai_get_afamily(rai);
+ int port;
+
+ if (!IS_IP_FAMILY(family)) {
+ bad_family:
+#ifdef AF_INET6
+ rb_raise(rb_eSocket, "need IPv4 or IPv6 address");
+#else
+ rb_raise(rb_eSocket, "need IPv4 address");
+#endif
+ }
+
+ switch (family) {
+ case AF_INET:
+ if (rai->sockaddr_len != sizeof(struct sockaddr_in))
+ rb_raise(rb_eSocket, "unexpected sockaddr size for IPv4");
+ port = ntohs(rai->addr.in.sin_port);
+ break;
+
+#ifdef AF_INET6
+ case AF_INET6:
+ if (rai->sockaddr_len != sizeof(struct sockaddr_in6))
+ rb_raise(rb_eSocket, "unexpected sockaddr size for IPv6");
+ port = ntohs(rai->addr.in6.sin6_port);
+ break;
+#endif
+
+ default:
+ goto bad_family;
+ }
+
+ return INT2NUM(port);
+}
+
+static int
+extract_in_addr(VALUE self, uint32_t *addrp)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ int family = ai_get_afamily(rai);
+ if (family != AF_INET) return 0;
+ *addrp = ntohl(rai->addr.in.sin_addr.s_addr);
+ return 1;
+}
+
+/*
+ * Returns true for IPv4 private address (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv4_private_p(VALUE self)
+{
+ uint32_t a;
+ if (!extract_in_addr(self, &a)) return Qfalse;
+ if ((a & 0xff000000) == 0x0a000000 || /* 10.0.0.0/8 */
+ (a & 0xfff00000) == 0xac100000 || /* 172.16.0.0/12 */
+ (a & 0xffff0000) == 0xc0a80000) /* 192.168.0.0/16 */
+ return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv4 loopback address (127.0.0.0/8).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv4_loopback_p(VALUE self)
+{
+ uint32_t a;
+ if (!extract_in_addr(self, &a)) return Qfalse;
+ if ((a & 0xff000000) == 0x7f000000) /* 127.0.0.0/8 */
+ return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv4 multicast address (224.0.0.0/4).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv4_multicast_p(VALUE self)
+{
+ uint32_t a;
+ if (!extract_in_addr(self, &a)) return Qfalse;
+ if ((a & 0xf0000000) == 0xe0000000) /* 224.0.0.0/4 */
+ return Qtrue;
+ return Qfalse;
+}
+
+#ifdef INET6
+
+static struct in6_addr *
+extract_in6_addr(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ int family = ai_get_afamily(rai);
+ if (family != AF_INET6) return NULL;
+ return &rai->addr.in6.sin6_addr;
+}
+
+/*
+ * Returns true for IPv6 unspecified address (::).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_unspecified_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_UNSPECIFIED(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 loopback address (::1).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_loopback_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_LOOPBACK(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 multicast address (ff00::/8).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_multicast_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_MULTICAST(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 link local address (ff80::/10).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_linklocal_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_LINKLOCAL(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 site local address (ffc0::/10).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_sitelocal_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_SITELOCAL(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 unique local address (fc00::/7, RFC4193).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_unique_local_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_UNIQUE_LOCAL(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv4-mapped IPv6 address (::ffff:0:0/80).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_v4mapped_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_V4MAPPED(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv4-compatible IPv6 address (::/80).
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_v4compat_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_V4COMPAT(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 multicast node-local scope address.
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_mc_nodelocal_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_MC_NODELOCAL(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 multicast link-local scope address.
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_mc_linklocal_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_MC_LINKLOCAL(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 multicast site-local scope address.
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_mc_sitelocal_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_MC_SITELOCAL(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 multicast organization-local scope address.
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_mc_orglocal_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_MC_ORGLOCAL(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns true for IPv6 multicast global scope address.
+ * It returns false otherwise.
+ */
+static VALUE
+addrinfo_ipv6_mc_global_p(VALUE self)
+{
+ struct in6_addr *addr = extract_in6_addr(self);
+ if (addr && IN6_IS_ADDR_MC_GLOBAL(addr)) return Qtrue;
+ return Qfalse;
+}
+
+/*
+ * Returns IPv4 address of IPv4 mapped/compatible IPv6 address.
+ * It returns nil if +self+ is not IPv4 mapped/compatible IPv6 address.
+ *
+ * Addrinfo.ip("::192.0.2.3").ipv6_to_ipv4 #=> #<Addrinfo: 192.0.2.3>
+ * Addrinfo.ip("::ffff:192.0.2.3").ipv6_to_ipv4 #=> #<Addrinfo: 192.0.2.3>
+ * Addrinfo.ip("::1").ipv6_to_ipv4 #=> nil
+ * Addrinfo.ip("192.0.2.3").ipv6_to_ipv4 #=> nil
+ * Addrinfo.unix("/tmp/sock").ipv6_to_ipv4 #=> nil
+ */
+static VALUE
+addrinfo_ipv6_to_ipv4(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ struct in6_addr *addr;
+ int family = ai_get_afamily(rai);
+ if (family != AF_INET6) return Qnil;
+ addr = &rai->addr.in6.sin6_addr;
+ if (IN6_IS_ADDR_V4MAPPED(addr) || IN6_IS_ADDR_V4COMPAT(addr)) {
+ struct sockaddr_in sin4;
+ INIT_SOCKADDR_IN(&sin4, sizeof(sin4));
+ memcpy(&sin4.sin_addr, (char*)addr + sizeof(*addr) - sizeof(sin4.sin_addr), sizeof(sin4.sin_addr));
+ return rsock_addrinfo_new((struct sockaddr *)&sin4, (socklen_t)sizeof(sin4),
+ PF_INET, rai->socktype, rai->protocol,
+ rai->canonname, rai->inspectname);
+ }
+ else {
+ return Qnil;
+ }
+}
+
+#endif
+
+#ifdef HAVE_SYS_UN_H
+/*
+ * call-seq:
+ * addrinfo.unix_path => path
+ *
+ * Returns the socket path as a string.
+ *
+ * Addrinfo.unix("/tmp/sock").unix_path #=> "/tmp/sock"
+ */
+static VALUE
+addrinfo_unix_path(VALUE self)
+{
+ rb_addrinfo_t *rai = get_addrinfo(self);
+ int family = ai_get_afamily(rai);
+ struct sockaddr_un *addr;
+ char *s, *e;
+
+ if (family != AF_UNIX)
+ rb_raise(rb_eSocket, "need AF_UNIX address");
+
+ addr = &rai->addr.un;
+
+ s = addr->sun_path;
+ e = (char*)addr + rai->sockaddr_len;
+ if (e < s)
+ rb_raise(rb_eSocket, "too short AF_UNIX address: %"PRIuSIZE" bytes given for minimum %"PRIuSIZE" bytes.",
+ (size_t)rai->sockaddr_len, (size_t)(s - (char *)addr));
+ if (addr->sun_path + sizeof(addr->sun_path) < e)
+ rb_raise(rb_eSocket,
+ "too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
+ (size_t)(e - addr->sun_path), sizeof(addr->sun_path));
+ while (s < e && *(e-1) == '\0')
+ e--;
+ return rb_str_new(s, e-s);
+}
+#endif
+
+/*
+ * call-seq:
+ * Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol, flags) => [addrinfo, ...]
+ * Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol) => [addrinfo, ...]
+ * Addrinfo.getaddrinfo(nodename, service, family, socktype) => [addrinfo, ...]
+ * Addrinfo.getaddrinfo(nodename, service, family) => [addrinfo, ...]
+ * Addrinfo.getaddrinfo(nodename, service) => [addrinfo, ...]
+ *
+ * returns a list of addrinfo objects as an array.
+ *
+ * This method converts nodename (hostname) and service (port) to addrinfo.
+ * Since the conversion is not unique, the result is a list of addrinfo objects.
+ *
+ * nodename or service can be nil if no conversion intended.
+ *
+ * family, socktype and protocol are hint for preferred protocol.
+ * If the result will be used for a socket with SOCK_STREAM,
+ * SOCK_STREAM should be specified as socktype.
+ * If so, Addrinfo.getaddrinfo returns addrinfo list appropriate for SOCK_STREAM.
+ * If they are omitted or nil is given, the result is not restricted.
+ *
+ * Similarly, PF_INET6 as family restricts for IPv6.
+ *
+ * flags should be bitwise OR of Socket::AI_??? constants such as follows.
+ * Note that the exact list of the constants depends on OS.
+ *
+ * AI_PASSIVE Get address to use with bind()
+ * AI_CANONNAME Fill in the canonical name
+ * AI_NUMERICHOST Prevent host name resolution
+ * AI_NUMERICSERV Prevent service name resolution
+ * AI_V4MAPPED Accept IPv4-mapped IPv6 addresses
+ * AI_ALL Allow all addresses
+ * AI_ADDRCONFIG Accept only if any address is assigned
+ *
+ * Note that socktype should be specified whenever application knows the usage of the address.
+ * Some platform causes an error when socktype is omitted and servname is specified as an integer
+ * because some port numbers, 512 for example, are ambiguous without socktype.
+ *
+ * Addrinfo.getaddrinfo("www.kame.net", 80, nil, :STREAM)
+ * #=> [#<Addrinfo: 203.178.141.194:80 TCP (www.kame.net)>,
+ * # #<Addrinfo: [2001:200:dff:fff1:216:3eff:feb1:44d7]:80 TCP (www.kame.net)>]
+ *
+ */
+static VALUE
+addrinfo_s_getaddrinfo(int argc, VALUE *argv, VALUE self)
+{
+ VALUE node, service, family, socktype, protocol, flags;
+
+ rb_scan_args(argc, argv, "24", &node, &service, &family, &socktype, &protocol, &flags);
+ return addrinfo_list_new(node, service, family, socktype, protocol, flags);
+}
+
+/*
+ * call-seq:
+ * Addrinfo.ip(host) => addrinfo
+ *
+ * returns an addrinfo object for IP address.
+ *
+ * The port, socktype, protocol of the result is filled by zero.
+ * So, it is not appropriate to create a socket.
+ *
+ * Addrinfo.ip("localhost") #=> #<Addrinfo: 127.0.0.1 (localhost)>
+ */
+static VALUE
+addrinfo_s_ip(VALUE self, VALUE host)
+{
+ VALUE ret;
+ rb_addrinfo_t *rai;
+ ret = addrinfo_firstonly_new(host, Qnil,
+ INT2NUM(PF_UNSPEC), INT2FIX(0), INT2FIX(0), INT2FIX(0));
+ rai = get_addrinfo(ret);
+ rai->socktype = 0;
+ rai->protocol = 0;
+ return ret;
+}
+
+/*
+ * call-seq:
+ * Addrinfo.tcp(host, port) => addrinfo
+ *
+ * returns an addrinfo object for TCP address.
+ *
+ * Addrinfo.tcp("localhost", "smtp") #=> #<Addrinfo: 127.0.0.1:25 TCP (localhost:smtp)>
+ */
+static VALUE
+addrinfo_s_tcp(VALUE self, VALUE host, VALUE port)
+{
+ return addrinfo_firstonly_new(host, port,
+ INT2NUM(PF_UNSPEC), INT2NUM(SOCK_STREAM), INT2NUM(IPPROTO_TCP), INT2FIX(0));
+}
+
+/*
+ * call-seq:
+ * Addrinfo.udp(host, port) => addrinfo
+ *
+ * returns an addrinfo object for UDP address.
+ *
+ * Addrinfo.udp("localhost", "daytime") #=> #<Addrinfo: 127.0.0.1:13 UDP (localhost:daytime)>
+ */
+static VALUE
+addrinfo_s_udp(VALUE self, VALUE host, VALUE port)
+{
+ return addrinfo_firstonly_new(host, port,
+ INT2NUM(PF_UNSPEC), INT2NUM(SOCK_DGRAM), INT2NUM(IPPROTO_UDP), INT2FIX(0));
+}
+
+#ifdef HAVE_SYS_UN_H
+
+/*
+ * call-seq:
+ * Addrinfo.unix(path [, socktype]) => addrinfo
+ *
+ * returns an addrinfo object for UNIX socket address.
+ *
+ * _socktype_ specifies the socket type.
+ * If it is omitted, :STREAM is used.
+ *
+ * Addrinfo.unix("/tmp/sock") #=> #<Addrinfo: /tmp/sock SOCK_STREAM>
+ * Addrinfo.unix("/tmp/sock", :DGRAM) #=> #<Addrinfo: /tmp/sock SOCK_DGRAM>
+ */
+static VALUE
+addrinfo_s_unix(int argc, VALUE *argv, VALUE self)
+{
+ VALUE path, vsocktype, addr;
+ int socktype;
+ rb_addrinfo_t *rai;
+
+ rb_scan_args(argc, argv, "11", &path, &vsocktype);
+
+ if (NIL_P(vsocktype))
+ socktype = SOCK_STREAM;
+ else
+ socktype = rsock_socktype_arg(vsocktype);
+
+ addr = addrinfo_s_allocate(rb_cAddrinfo);
+ DATA_PTR(addr) = rai = alloc_addrinfo();
+ init_unix_addrinfo(rai, path, socktype);
+ OBJ_INFECT(addr, path);
+ return addr;
+}
+
+#endif
+
+VALUE
+rsock_sockaddr_string_value(volatile VALUE *v)
+{
+ VALUE val = *v;
+ if (IS_ADDRINFO(val)) {
+ *v = addrinfo_to_sockaddr(val);
+ }
+ StringValue(*v);
+ return *v;
+}
+
+VALUE
+rsock_sockaddr_string_value_with_addrinfo(volatile VALUE *v, VALUE *rai_ret)
+{
+ VALUE val = *v;
+ *rai_ret = Qnil;
+ if (IS_ADDRINFO(val)) {
+ *v = addrinfo_to_sockaddr(val);
+ *rai_ret = val;
+ }
+ StringValue(*v);
+ return *v;
+}
+
+char *
+rsock_sockaddr_string_value_ptr(volatile VALUE *v)
+{
+ rsock_sockaddr_string_value(v);
+ return RSTRING_PTR(*v);
+}
+
+VALUE
+rb_check_sockaddr_string_type(VALUE val)
+{
+ if (IS_ADDRINFO(val))
+ return addrinfo_to_sockaddr(val);
+ return rb_check_string_type(val);
+}
+
+VALUE
+rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len)
+{
+ int family;
+ int socktype;
+ int ret;
+ socklen_t optlen = (socklen_t)sizeof(socktype);
+
+ /* assumes protocol family and address family are identical */
+ family = get_afamily(addr, len);
+
+ ret = getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&socktype, &optlen);
+ if (ret == -1) {
+ rb_sys_fail("getsockopt(SO_TYPE)");
+ }
+
+ return rsock_addrinfo_new(addr, len, family, socktype, 0, Qnil, Qnil);
+}
+
+VALUE
+rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
+{
+ rb_io_t *fptr;
+
+ switch (TYPE(io)) {
+ case T_FIXNUM:
+ return rsock_fd_socket_addrinfo(FIX2INT(io), addr, len);
+
+ case T_BIGNUM:
+ return rsock_fd_socket_addrinfo(NUM2INT(io), addr, len);
+
+ case T_FILE:
+ GetOpenFile(io, fptr);
+ return rsock_fd_socket_addrinfo(fptr->fd, addr, len);
+
+ default:
+ rb_raise(rb_eTypeError, "neither IO nor file descriptor");
+ }
+
+ UNREACHABLE;
+}
+
+/*
+ * Addrinfo class
+ */
+void
+rsock_init_addrinfo(void)
+{
+ /*
+ * The Addrinfo class maps <tt>struct addrinfo</tt> to ruby. This
+ * structure identifies an Internet host and a service.
+ */
+ rb_cAddrinfo = rb_define_class("Addrinfo", rb_cData);
+ rb_define_alloc_func(rb_cAddrinfo, addrinfo_s_allocate);
+ rb_define_method(rb_cAddrinfo, "initialize", addrinfo_initialize, -1);
+ rb_define_method(rb_cAddrinfo, "inspect", addrinfo_inspect, 0);
+ rb_define_method(rb_cAddrinfo, "inspect_sockaddr", rsock_addrinfo_inspect_sockaddr, 0);
+ rb_define_singleton_method(rb_cAddrinfo, "getaddrinfo", addrinfo_s_getaddrinfo, -1);
+ rb_define_singleton_method(rb_cAddrinfo, "ip", addrinfo_s_ip, 1);
+ rb_define_singleton_method(rb_cAddrinfo, "tcp", addrinfo_s_tcp, 2);
+ rb_define_singleton_method(rb_cAddrinfo, "udp", addrinfo_s_udp, 2);
+#ifdef HAVE_SYS_UN_H
+ rb_define_singleton_method(rb_cAddrinfo, "unix", addrinfo_s_unix, -1);
+#endif
+
+ rb_define_method(rb_cAddrinfo, "afamily", addrinfo_afamily, 0);
+ rb_define_method(rb_cAddrinfo, "pfamily", addrinfo_pfamily, 0);
+ rb_define_method(rb_cAddrinfo, "socktype", addrinfo_socktype, 0);
+ rb_define_method(rb_cAddrinfo, "protocol", addrinfo_protocol, 0);
+ rb_define_method(rb_cAddrinfo, "canonname", addrinfo_canonname, 0);
+
+ rb_define_method(rb_cAddrinfo, "ipv4?", addrinfo_ipv4_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6?", addrinfo_ipv6_p, 0);
+ rb_define_method(rb_cAddrinfo, "unix?", addrinfo_unix_p, 0);
+
+ rb_define_method(rb_cAddrinfo, "ip?", addrinfo_ip_p, 0);
+ rb_define_method(rb_cAddrinfo, "ip_unpack", addrinfo_ip_unpack, 0);
+ rb_define_method(rb_cAddrinfo, "ip_address", addrinfo_ip_address, 0);
+ rb_define_method(rb_cAddrinfo, "ip_port", addrinfo_ip_port, 0);
+
+ rb_define_method(rb_cAddrinfo, "ipv4_private?", addrinfo_ipv4_private_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv4_loopback?", addrinfo_ipv4_loopback_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv4_multicast?", addrinfo_ipv4_multicast_p, 0);
+
+#ifdef INET6
+ rb_define_method(rb_cAddrinfo, "ipv6_unspecified?", addrinfo_ipv6_unspecified_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_loopback?", addrinfo_ipv6_loopback_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_multicast?", addrinfo_ipv6_multicast_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_linklocal?", addrinfo_ipv6_linklocal_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_sitelocal?", addrinfo_ipv6_sitelocal_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_unique_local?", addrinfo_ipv6_unique_local_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_v4mapped?", addrinfo_ipv6_v4mapped_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_v4compat?", addrinfo_ipv6_v4compat_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_mc_nodelocal?", addrinfo_ipv6_mc_nodelocal_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_mc_linklocal?", addrinfo_ipv6_mc_linklocal_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_mc_sitelocal?", addrinfo_ipv6_mc_sitelocal_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_mc_orglocal?", addrinfo_ipv6_mc_orglocal_p, 0);
+ rb_define_method(rb_cAddrinfo, "ipv6_mc_global?", addrinfo_ipv6_mc_global_p, 0);
+
+ rb_define_method(rb_cAddrinfo, "ipv6_to_ipv4", addrinfo_ipv6_to_ipv4, 0);
+#endif
+
+#ifdef HAVE_SYS_UN_H
+ rb_define_method(rb_cAddrinfo, "unix_path", addrinfo_unix_path, 0);
+#endif
+
+ rb_define_method(rb_cAddrinfo, "to_sockaddr", addrinfo_to_sockaddr, 0);
+ rb_define_method(rb_cAddrinfo, "to_s", addrinfo_to_sockaddr, 0); /* compatibility for ruby before 1.9.2 */
+
+ rb_define_method(rb_cAddrinfo, "getnameinfo", addrinfo_getnameinfo, -1);
+
+ rb_define_method(rb_cAddrinfo, "marshal_dump", addrinfo_mdump, 0);
+ rb_define_method(rb_cAddrinfo, "marshal_load", addrinfo_mload, 1);
+}
diff --git a/jni/ruby/ext/socket/rubysocket.h b/jni/ruby/ext/socket/rubysocket.h
new file mode 100644
index 0000000..8beffd2
--- /dev/null
+++ b/jni/ruby/ext/socket/rubysocket.h
@@ -0,0 +1,415 @@
+#ifndef RUBY_SOCKET_H
+#define RUBY_SOCKET_H 1
+
+#include "ruby/ruby.h"
+#include "ruby/io.h"
+#include "ruby/thread.h"
+#include "ruby/util.h"
+#include "internal.h"
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef HAVE_SYS_UIO_H
+# include <sys/uio.h>
+#endif
+
+#ifdef HAVE_XTI_H
+# include <xti.h>
+#endif
+
+#ifdef _WIN32
+# if defined(_MSC_VER)
+# undef HAVE_TYPE_STRUCT_SOCKADDR_DL
+# endif
+#else
+# if defined(__BEOS__) && !defined(__HAIKU__) && !defined(BONE)
+# include <net/socket.h>
+# else
+# include <sys/socket.h>
+# endif
+# include <netinet/in.h>
+# ifdef HAVE_NETINET_IN_SYSTM_H
+# include <netinet/in_systm.h>
+# endif
+# ifdef HAVE_NETINET_TCP_H
+# include <netinet/tcp.h>
+# endif
+# ifdef HAVE_NETINET_TCP_FSM_H
+# include <netinet/tcp_fsm.h>
+# endif
+# ifdef HAVE_NETINET_UDP_H
+# include <netinet/udp.h>
+# endif
+# ifdef HAVE_ARPA_INET_H
+# include <arpa/inet.h>
+# endif
+# include <netdb.h>
+#endif
+
+#ifdef HAVE_NETPACKET_PACKET_H
+# include <netpacket/packet.h>
+#endif
+#ifdef HAVE_NET_ETHERNET_H
+# include <net/ethernet.h>
+#endif
+
+#include <errno.h>
+
+#ifdef HAVE_SYS_UN_H
+# include <sys/un.h>
+#endif
+
+#if defined(HAVE_FCNTL)
+# ifdef HAVE_SYS_SELECT_H
+# include <sys/select.h>
+# endif
+# ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+# endif
+# ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+# endif
+# ifdef HAVE_FCNTL_H
+# include <fcntl.h>
+# endif
+#endif
+
+#ifdef HAVE_IFADDRS_H
+# include <ifaddrs.h>
+#endif
+#ifdef HAVE_SYS_IOCTL_H
+# include <sys/ioctl.h>
+#endif
+#ifdef HAVE_SYS_SOCKIO_H
+# include <sys/sockio.h>
+#endif
+#ifdef HAVE_NET_IF_H
+# include <net/if.h>
+#endif
+
+#ifdef HAVE_SYS_PARAM_H
+# include <sys/param.h>
+#endif
+#ifdef HAVE_SYS_UCRED_H
+# include <sys/ucred.h>
+#endif
+#ifdef HAVE_UCRED_H
+# include <ucred.h>
+#endif
+#ifdef HAVE_NET_IF_DL_H
+# include <net/if_dl.h>
+#endif
+
+#ifndef HAVE_TYPE_SOCKLEN_T
+typedef int socklen_t;
+#endif
+
+#ifdef NEED_IF_INDEXTONAME_DECL
+char *if_indextoname(unsigned int, char *);
+#endif
+#ifdef NEED_IF_NAMETOINDEX_DECL
+unsigned int if_nametoindex(const char *);
+#endif
+
+#define SOCKLEN_MAX \
+ (0 < (socklen_t)-1 ? \
+ ~(socklen_t)0 : \
+ (((((socklen_t)1) << (sizeof(socklen_t) * CHAR_BIT - 2)) - 1) * 2 + 1))
+
+#ifndef RSTRING_SOCKLEN
+# define RSTRING_SOCKLEN (socklen_t)RSTRING_LENINT
+#endif
+
+#ifndef EWOULDBLOCK
+# define EWOULDBLOCK EAGAIN
+#endif
+
+/*
+ * workaround for NetBSD, OpenBSD and etc.
+ * The problem is since 4.4BSD-Lite.
+ * FreeBSD fix the problem at FreeBSD 2.2.0.
+ * NetBSD fix the problem at NetBSD 3.0 by kern/29624.
+ * OpenBSD fix the problem at OpenBSD 3.8.
+ */
+#define pseudo_AF_FTIP pseudo_AF_RTIP
+
+#ifndef HAVE_GETADDRINFO
+# include "addrinfo.h"
+#endif
+
+#include "sockport.h"
+
+#ifndef NI_MAXHOST
+# define NI_MAXHOST 1025
+#endif
+#ifndef NI_MAXSERV
+# define NI_MAXSERV 32
+#endif
+
+#ifdef AF_INET6
+# define IS_IP_FAMILY(af) ((af) == AF_INET || (af) == AF_INET6)
+#else
+# define IS_IP_FAMILY(af) ((af) == AF_INET)
+#endif
+
+#ifndef IN6_IS_ADDR_UNIQUE_LOCAL
+# define IN6_IS_ADDR_UNIQUE_LOCAL(a) (((a)->s6_addr[0] == 0xfc) || ((a)->s6_addr[0] == 0xfd))
+#endif
+
+#ifndef HAVE_TYPE_STRUCT_SOCKADDR_STORAGE
+/*
+ * RFC 2553: protocol-independent placeholder for socket addresses
+ */
+# define _SS_MAXSIZE 128
+# define _SS_ALIGNSIZE (sizeof(double))
+# define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(unsigned char) * 2)
+# define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(unsigned char) * 2 - \
+ _SS_PAD1SIZE - _SS_ALIGNSIZE)
+
+struct sockaddr_storage {
+# ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
+ unsigned char ss_len; /* address length */
+ unsigned char ss_family; /* address family */
+# else
+ unsigned short ss_family;
+# endif
+ char __ss_pad1[_SS_PAD1SIZE];
+ double __ss_align; /* force desired structure storage alignment */
+ char __ss_pad2[_SS_PAD2SIZE];
+};
+#endif
+
+typedef union {
+ struct sockaddr addr;
+ struct sockaddr_in in;
+#ifdef AF_INET6
+ struct sockaddr_in6 in6;
+#endif
+#ifdef HAVE_TYPE_STRUCT_SOCKADDR_UN
+ struct sockaddr_un un;
+#endif
+#ifdef HAVE_TYPE_STRUCT_SOCKADDR_DL
+ struct sockaddr_dl dl; /* AF_LINK */
+#endif
+ struct sockaddr_storage storage;
+ char place_holder[2048]; /* sockaddr_storage is not enough for Unix domain sockets on SunOS and Darwin. */
+} union_sockaddr;
+
+#ifdef __APPLE__
+/*
+ * CMSG_ macros are broken on 64bit darwin, because __DARWIN_ALIGN
+ * aligns up to __darwin_size_t which is 64bit, but CMSG_DATA is
+ * 32bit-aligned.
+ */
+# undef __DARWIN_ALIGNBYTES
+# define __DARWIN_ALIGNBYTES (sizeof(unsigned int) - 1)
+#endif
+
+#if defined(_AIX)
+# ifndef CMSG_SPACE
+# define CMSG_SPACE(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
+# endif
+# ifndef CMSG_LEN
+# define CMSG_LEN(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
+# endif
+#endif
+
+#ifdef __BEOS__
+# undef close
+# define close closesocket
+#endif
+
+#define INET_CLIENT 0
+#define INET_SERVER 1
+#define INET_SOCKS 2
+
+extern int rsock_do_not_reverse_lookup;
+extern int rsock_cmsg_cloexec_state;
+#define FMODE_NOREVLOOKUP 0x100
+
+extern VALUE rb_cBasicSocket;
+extern VALUE rb_cIPSocket;
+extern VALUE rb_cTCPSocket;
+extern VALUE rb_cTCPServer;
+extern VALUE rb_cUDPSocket;
+#ifdef HAVE_SYS_UN_H
+extern VALUE rb_cUNIXSocket;
+extern VALUE rb_cUNIXServer;
+#endif
+extern VALUE rb_cSocket;
+extern VALUE rb_cAddrinfo;
+extern VALUE rb_cSockOpt;
+
+extern VALUE rb_eSocket;
+
+#ifdef SOCKS
+extern VALUE rb_cSOCKSSocket;
+# ifdef SOCKS5
+# include <socks.h>
+# else
+void SOCKSinit();
+int Rconnect();
+# endif
+#endif
+
+#include "constdefs.h"
+
+#define BLOCKING_REGION_FD(func, arg) (long)rb_thread_io_blocking_region((func), (arg), (arg)->fd)
+
+#define SockAddrStringValue(v) rsock_sockaddr_string_value(&(v))
+#define SockAddrStringValuePtr(v) rsock_sockaddr_string_value_ptr(&(v))
+#define SockAddrStringValueWithAddrinfo(v, rai_ret) rsock_sockaddr_string_value_with_addrinfo(&(v), &(rai_ret))
+VALUE rsock_sockaddr_string_value(volatile VALUE *);
+char *rsock_sockaddr_string_value_ptr(volatile VALUE *);
+VALUE rsock_sockaddr_string_value_with_addrinfo(volatile VALUE *v, VALUE *ai_ret);
+
+VALUE rb_check_sockaddr_string_type(VALUE);
+
+NORETURN(void rsock_raise_socket_error(const char *, int));
+
+int rsock_family_arg(VALUE domain);
+int rsock_socktype_arg(VALUE type);
+int rsock_level_arg(int family, VALUE level);
+int rsock_optname_arg(int family, int level, VALUE optname);
+int rsock_cmsg_type_arg(int family, int level, VALUE type);
+int rsock_shutdown_how_arg(VALUE how);
+
+int rsock_getfamily(int sockfd);
+
+struct rb_addrinfo {
+ struct addrinfo *ai;
+ int allocated_by_malloc;
+};
+int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct rb_addrinfo **res);
+void rb_freeaddrinfo(struct rb_addrinfo *ai);
+VALUE rsock_freeaddrinfo(VALUE arg);
+int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);
+struct rb_addrinfo *rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags);
+struct rb_addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack);
+VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len);
+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_addrinfo_inspect_sockaddr(VALUE rai);
+
+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 rb_addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t));
+VALUE rsock_inspect_sockaddr(struct sockaddr *addr, socklen_t socklen, VALUE ret);
+socklen_t rsock_sockaddr_len(struct sockaddr *addr);
+VALUE rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len);
+
+int rsock_revlookup_flag(VALUE revlookup, int *norevlookup);
+
+#ifdef HAVE_SYS_UN_H
+VALUE rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len);
+VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len);
+socklen_t rsock_unix_sockaddr_len(VALUE path);
+#endif
+
+int rsock_socket(int domain, int type, int proto);
+int rsock_detect_cloexec(int fd);
+VALUE rsock_init_sock(VALUE sock, int fd);
+VALUE rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass);
+VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type);
+VALUE rsock_init_unixsock(VALUE sock, VALUE path, int server);
+
+struct rsock_send_arg {
+ int fd, flags;
+ VALUE mesg;
+ struct sockaddr *to;
+ socklen_t tolen;
+};
+
+VALUE rsock_sendto_blocking(void *data);
+VALUE rsock_send_blocking(void *data);
+VALUE rsock_bsock_send(int argc, VALUE *argv, VALUE sock);
+
+enum sock_recv_type {
+ RECV_RECV, /* BasicSocket#recv(no from) */
+ RECV_IP, /* IPSocket#recvfrom */
+ RECV_UNIX, /* UNIXSocket#recvfrom */
+ RECV_SOCKET /* Socket#recvfrom */
+};
+
+VALUE rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from);
+VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from);
+
+int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks);
+
+VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len);
+VALUE rsock_s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len);
+VALUE rsock_sock_listen(VALUE sock, VALUE log);
+
+VALUE rsock_sockopt_new(int family, int level, int optname, VALUE data);
+
+#if defined(HAVE_SENDMSG)
+VALUE rsock_bsock_sendmsg(int argc, VALUE *argv, VALUE sock);
+VALUE rsock_bsock_sendmsg_nonblock(int argc, VALUE *argv, VALUE sock);
+#else
+#define rsock_bsock_sendmsg rb_f_notimplement
+#define rsock_bsock_sendmsg_nonblock rb_f_notimplement
+#endif
+
+#if defined(HAVE_RECVMSG)
+VALUE rsock_bsock_recvmsg(int argc, VALUE *argv, VALUE sock);
+VALUE rsock_bsock_recvmsg_nonblock(int argc, VALUE *argv, VALUE sock);
+ssize_t rsock_recvmsg(int socket, struct msghdr *message, int flags);
+#else
+#define rsock_bsock_recvmsg rb_f_notimplement
+#define rsock_bsock_recvmsg_nonblock rb_f_notimplement
+#endif
+
+#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
+void rsock_discard_cmsg_resource(struct msghdr *mh, int msg_peek_p);
+#endif
+
+void rsock_init_basicsocket(void);
+void rsock_init_ipsocket(void);
+void rsock_init_tcpsocket(void);
+void rsock_init_tcpserver(void);
+void rsock_init_sockssocket(void);
+void rsock_init_udpsocket(void);
+void rsock_init_unixsocket(void);
+void rsock_init_unixserver(void);
+void rsock_init_socket_constants(void);
+void rsock_init_ancdata(void);
+void rsock_init_addrinfo(void);
+void rsock_init_sockopt(void);
+void rsock_init_sockifaddr(void);
+void rsock_init_socket_init(void);
+
+NORETURN(void rsock_syserr_fail_host_port(int err, const char *, VALUE, VALUE));
+NORETURN(void rsock_syserr_fail_path(int err, const char *, VALUE));
+NORETURN(void rsock_syserr_fail_sockaddr(int err, const char *mesg, struct sockaddr *addr, socklen_t len));
+NORETURN(void rsock_syserr_fail_raddrinfo(int err, const char *mesg, VALUE rai));
+NORETURN(void rsock_syserr_fail_raddrinfo_or_sockaddr(int err, const char *mesg, VALUE addr, VALUE rai));
+
+NORETURN(void rsock_sys_fail_host_port(const char *, VALUE, VALUE));
+NORETURN(void rsock_sys_fail_path(const char *, VALUE));
+NORETURN(void rsock_sys_fail_sockaddr(const char *, struct sockaddr *addr, socklen_t len));
+NORETURN(void rsock_sys_fail_raddrinfo(const char *, VALUE rai));
+NORETURN(void rsock_sys_fail_raddrinfo_or_sockaddr(const char *, VALUE addr, VALUE rai));
+
+/*
+ * It is safe on Linux to attempt using a socket without waiting on it in
+ * all cases. For some syscalls (e.g. accept/accept4), blocking on the
+ * syscall instead of relying on select/poll allows the kernel to use
+ * "wake-one" behavior and avoid the thundering herd problem.
+ * This is likely safe on all other *nix-like systems, so this whitelist
+ * can be expanded by interested parties.
+ */
+#if defined(__linux__)
+static inline int rsock_maybe_fd_writable(int fd) { return 1; }
+static inline void rsock_maybe_wait_fd(int fd) { }
+#else /* some systems (mswin/mingw) need these. ref: r36946 */
+# define rsock_maybe_fd_writable(fd) rb_thread_fd_writable((fd))
+# define rsock_maybe_wait_fd(fd) rb_thread_wait_fd((fd))
+#endif
+
+#endif
diff --git a/jni/ruby/ext/socket/socket.c b/jni/ruby/ext/socket/socket.c
new file mode 100644
index 0000000..81028c5
--- /dev/null
+++ b/jni/ruby/ext/socket/socket.c
@@ -0,0 +1,2182 @@
+/************************************************
+
+ socket.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+static VALUE sock_s_unpack_sockaddr_in(VALUE, VALUE);
+
+void
+rsock_sys_fail_host_port(const char *mesg, VALUE host, VALUE port)
+{
+ rsock_syserr_fail_host_port(errno, mesg, host, port);
+}
+
+void
+rsock_syserr_fail_host_port(int err, const char *mesg, VALUE host, VALUE port)
+{
+ VALUE message;
+
+ message = rb_sprintf("%s for %+"PRIsVALUE" port % "PRIsVALUE"",
+ mesg, host, port);
+
+ rb_syserr_fail_str(err, message);
+}
+
+void
+rsock_sys_fail_path(const char *mesg, VALUE path)
+{
+ rsock_syserr_fail_path(errno, mesg, path);
+}
+
+void
+rsock_syserr_fail_path(int err, const char *mesg, VALUE path)
+{
+ VALUE message;
+
+ if (RB_TYPE_P(path, T_STRING)) {
+ message = rb_sprintf("%s for % "PRIsVALUE"", mesg, path);
+ rb_syserr_fail_str(err, message);
+ }
+ else {
+ rb_syserr_fail(err, mesg);
+ }
+}
+
+void
+rsock_sys_fail_sockaddr(const char *mesg, struct sockaddr *addr, socklen_t len)
+{
+ rsock_syserr_fail_sockaddr(errno, mesg, addr, len);
+}
+
+void
+rsock_syserr_fail_sockaddr(int err, const char *mesg, struct sockaddr *addr, socklen_t len)
+{
+ VALUE rai;
+
+ rai = rsock_addrinfo_new(addr, len, PF_UNSPEC, 0, 0, Qnil, Qnil);
+
+ rsock_syserr_fail_raddrinfo(err, mesg, rai);
+}
+
+void
+rsock_sys_fail_raddrinfo(const char *mesg, VALUE rai)
+{
+ rsock_syserr_fail_raddrinfo(errno, mesg, rai);
+}
+
+void
+rsock_syserr_fail_raddrinfo(int err, const char *mesg, VALUE rai)
+{
+ VALUE str, message;
+
+ str = rsock_addrinfo_inspect_sockaddr(rai);
+ message = rb_sprintf("%s for %"PRIsVALUE"", mesg, str);
+
+ rb_syserr_fail_str(err, message);
+}
+
+void
+rsock_sys_fail_raddrinfo_or_sockaddr(const char *mesg, VALUE addr, VALUE rai)
+{
+ rsock_syserr_fail_raddrinfo_or_sockaddr(errno, mesg, addr, rai);
+}
+
+void
+rsock_syserr_fail_raddrinfo_or_sockaddr(int err, const char *mesg, VALUE addr, VALUE rai)
+{
+ if (NIL_P(rai)) {
+ StringValue(addr);
+
+ rsock_syserr_fail_sockaddr(err, mesg,
+ (struct sockaddr *)RSTRING_PTR(addr),
+ (socklen_t)RSTRING_LEN(addr)); /* overflow should be checked already */
+ }
+ else
+ rsock_syserr_fail_raddrinfo(err, mesg, rai);
+}
+
+static void
+setup_domain_and_type(VALUE domain, int *dv, VALUE type, int *tv)
+{
+ *dv = rsock_family_arg(domain);
+ *tv = rsock_socktype_arg(type);
+}
+
+/*
+ * call-seq:
+ * Socket.new(domain, socktype [, protocol]) => socket
+ *
+ * Creates a new socket object.
+ *
+ * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
+ *
+ * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
+ *
+ * _protocol_ is optional and should be a protocol defined in the domain.
+ * If protocol is not given, 0 is used internally.
+ *
+ * Socket.new(:INET, :STREAM) # TCP socket
+ * Socket.new(:INET, :DGRAM) # UDP socket
+ * Socket.new(:UNIX, :STREAM) # UNIX stream socket
+ * Socket.new(:UNIX, :DGRAM) # UNIX datagram socket
+ */
+static VALUE
+sock_initialize(int argc, VALUE *argv, VALUE sock)
+{
+ VALUE domain, type, protocol;
+ int fd;
+ int d, t;
+
+ rb_scan_args(argc, argv, "21", &domain, &type, &protocol);
+ if (NIL_P(protocol))
+ protocol = INT2FIX(0);
+
+ rb_secure(3);
+ setup_domain_and_type(domain, &d, type, &t);
+ fd = rsock_socket(d, t, NUM2INT(protocol));
+ if (fd < 0) rb_sys_fail("socket(2)");
+
+ return rsock_init_sock(sock, fd);
+}
+
+#if defined HAVE_SOCKETPAIR
+static VALUE
+io_call_close(VALUE io)
+{
+ return rb_funcall(io, rb_intern("close"), 0, 0);
+}
+
+static VALUE
+io_close(VALUE io)
+{
+ return rb_rescue(io_call_close, io, 0, 0);
+}
+
+static VALUE
+pair_yield(VALUE pair)
+{
+ return rb_ensure(rb_yield, pair, io_close, rb_ary_entry(pair, 1));
+}
+#endif
+
+#if defined HAVE_SOCKETPAIR
+
+#ifdef SOCK_CLOEXEC
+static int
+rsock_socketpair0(int domain, int type, int protocol, int sv[2])
+{
+ int ret;
+ static int cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
+
+ if (cloexec_state > 0) { /* common path, if SOCK_CLOEXEC is defined */
+ ret = socketpair(domain, type|SOCK_CLOEXEC, protocol, sv);
+ if (ret == 0 && (sv[0] <= 2 || sv[1] <= 2)) {
+ goto fix_cloexec; /* highly unlikely */
+ }
+ goto update_max_fd;
+ }
+ else if (cloexec_state < 0) { /* usually runs once only for detection */
+ ret = socketpair(domain, type|SOCK_CLOEXEC, protocol, sv);
+ if (ret == 0) {
+ cloexec_state = rsock_detect_cloexec(sv[0]);
+ if ((cloexec_state == 0) || (sv[0] <= 2 || sv[1] <= 2))
+ goto fix_cloexec;
+ goto update_max_fd;
+ }
+ else if (ret == -1 && errno == EINVAL) {
+ /* SOCK_CLOEXEC is available since Linux 2.6.27. Linux 2.6.18 fails with EINVAL */
+ ret = socketpair(domain, type, protocol, sv);
+ if (ret != -1) {
+ /* The reason of EINVAL may be other than SOCK_CLOEXEC.
+ * So disable SOCK_CLOEXEC only if socketpair() succeeds without SOCK_CLOEXEC.
+ * Ex. Socket.pair(:UNIX, 0xff) fails with EINVAL.
+ */
+ cloexec_state = 0;
+ }
+ }
+ }
+ else { /* cloexec_state == 0 */
+ ret = socketpair(domain, type, protocol, sv);
+ }
+ if (ret == -1) {
+ return -1;
+ }
+
+fix_cloexec:
+ rb_maygvl_fd_fix_cloexec(sv[0]);
+ rb_maygvl_fd_fix_cloexec(sv[1]);
+
+update_max_fd:
+ rb_update_max_fd(sv[0]);
+ rb_update_max_fd(sv[1]);
+
+ return ret;
+}
+#else /* !SOCK_CLOEXEC */
+static int
+rsock_socketpair0(int domain, int type, int protocol, int sv[2])
+{
+ int ret = socketpair(domain, type, protocol, sv);
+
+ if (ret == -1)
+ return -1;
+
+ rb_fd_fix_cloexec(sv[0]);
+ rb_fd_fix_cloexec(sv[1]);
+ return ret;
+}
+#endif /* !SOCK_CLOEXEC */
+
+static int
+rsock_socketpair(int domain, int type, int protocol, int sv[2])
+{
+ int ret;
+
+ ret = rsock_socketpair0(domain, type, protocol, sv);
+ if (ret < 0 && (errno == EMFILE || errno == ENFILE)) {
+ rb_gc();
+ ret = rsock_socketpair0(domain, type, protocol, sv);
+ }
+
+ return ret;
+}
+
+/*
+ * call-seq:
+ * Socket.pair(domain, type, protocol) => [socket1, socket2]
+ * Socket.socketpair(domain, type, protocol) => [socket1, socket2]
+ *
+ * Creates a pair of sockets connected each other.
+ *
+ * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
+ *
+ * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
+ *
+ * _protocol_ should be a protocol defined in the domain,
+ * defaults to 0 for the domain.
+ *
+ * s1, s2 = Socket.pair(:UNIX, :STREAM, 0)
+ * s1.send "a", 0
+ * s1.send "b", 0
+ * s1.close
+ * p s2.recv(10) #=> "ab"
+ * p s2.recv(10) #=> ""
+ * p s2.recv(10) #=> ""
+ *
+ * s1, s2 = Socket.pair(:UNIX, :DGRAM, 0)
+ * s1.send "a", 0
+ * s1.send "b", 0
+ * p s2.recv(10) #=> "a"
+ * p s2.recv(10) #=> "b"
+ *
+ */
+VALUE
+rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass)
+{
+ VALUE domain, type, protocol;
+ int d, t, p, sp[2];
+ int ret;
+ VALUE s1, s2, r;
+
+ rb_scan_args(argc, argv, "21", &domain, &type, &protocol);
+ if (NIL_P(protocol))
+ protocol = INT2FIX(0);
+
+ setup_domain_and_type(domain, &d, type, &t);
+ p = NUM2INT(protocol);
+ ret = rsock_socketpair(d, t, p, sp);
+ if (ret < 0) {
+ rb_sys_fail("socketpair(2)");
+ }
+
+ s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
+ s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
+ r = rb_assoc_new(s1, s2);
+ if (rb_block_given_p()) {
+ return rb_ensure(pair_yield, r, io_close, s1);
+ }
+ return r;
+}
+#else
+#define rsock_sock_s_socketpair rb_f_notimplement
+#endif
+
+/*
+ * call-seq:
+ * socket.connect(remote_sockaddr) => 0
+ *
+ * Requests a connection to be made on the given +remote_sockaddr+. Returns 0 if
+ * successful, otherwise an exception is raised.
+ *
+ * === Parameter
+ * * +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
+ *
+ * === Example:
+ * # Pull down Google's web page
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
+ * sockaddr = Socket.pack_sockaddr_in( 80, 'www.google.com' )
+ * socket.connect( sockaddr )
+ * socket.write( "GET / HTTP/1.0\r\n\r\n" )
+ * results = socket.read
+ *
+ * === Unix-based Exceptions
+ * On unix-based systems the following system exceptions may be raised if
+ * the call to _connect_ fails:
+ * * Errno::EACCES - search permission is denied for a component of the prefix
+ * path or write access to the +socket+ is denied
+ * * Errno::EADDRINUSE - the _sockaddr_ is already in use
+ * * Errno::EADDRNOTAVAIL - the specified _sockaddr_ is not available from the
+ * local machine
+ * * Errno::EAFNOSUPPORT - the specified _sockaddr_ is not a valid address for
+ * the address family of the specified +socket+
+ * * Errno::EALREADY - a connection is already in progress for the specified
+ * socket
+ * * Errno::EBADF - the +socket+ is not a valid file descriptor
+ * * Errno::ECONNREFUSED - the target _sockaddr_ was not listening for connections
+ * refused the connection request
+ * * Errno::ECONNRESET - the remote host reset the connection request
+ * * Errno::EFAULT - the _sockaddr_ cannot be accessed
+ * * Errno::EHOSTUNREACH - the destination host cannot be reached (probably
+ * because the host is down or a remote router cannot reach it)
+ * * Errno::EINPROGRESS - the O_NONBLOCK is set for the +socket+ and the
+ * connection cannot be immediately established; the connection will be
+ * established asynchronously
+ * * Errno::EINTR - the attempt to establish the connection was interrupted by
+ * delivery of a signal that was caught; the connection will be established
+ * asynchronously
+ * * Errno::EISCONN - the specified +socket+ is already connected
+ * * Errno::EINVAL - the address length used for the _sockaddr_ is not a valid
+ * length for the address family or there is an invalid family in _sockaddr_
+ * * Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
+ * PATH_MAX
+ * * Errno::ENETDOWN - the local interface used to reach the destination is down
+ * * Errno::ENETUNREACH - no route to the network is present
+ * * Errno::ENOBUFS - no buffer space is available
+ * * Errno::ENOSR - there were insufficient STREAMS resources available to
+ * complete the operation
+ * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
+ * * Errno::EOPNOTSUPP - the calling +socket+ is listening and cannot be connected
+ * * Errno::EPROTOTYPE - the _sockaddr_ has a different type than the socket
+ * bound to the specified peer address
+ * * Errno::ETIMEDOUT - the attempt to connect time out before a connection
+ * was made.
+ *
+ * On unix-based systems if the address family of the calling +socket+ is
+ * AF_UNIX the follow exceptions may be raised if the call to _connect_
+ * fails:
+ * * Errno::EIO - an i/o error occurred while reading from or writing to the
+ * file system
+ * * Errno::ELOOP - too many symbolic links were encountered in translating
+ * the pathname in _sockaddr_
+ * * Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
+ * characters, or an entire pathname exceeded PATH_MAX characters
+ * * Errno::ENOENT - a component of the pathname does not name an existing file
+ * or the pathname is an empty string
+ * * Errno::ENOTDIR - a component of the path prefix of the pathname in _sockaddr_
+ * is not a directory
+ *
+ * === Windows Exceptions
+ * On Windows systems the following system exceptions may be raised if
+ * the call to _connect_ fails:
+ * * Errno::ENETDOWN - the network is down
+ * * Errno::EADDRINUSE - the socket's local address is already in use
+ * * Errno::EINTR - the socket was cancelled
+ * * Errno::EINPROGRESS - a blocking socket is in progress or the service provider
+ * is still processing a callback function. Or a nonblocking connect call is
+ * in progress on the +socket+.
+ * * Errno::EALREADY - see Errno::EINVAL
+ * * Errno::EADDRNOTAVAIL - the remote address is not a valid address, such as
+ * ADDR_ANY TODO check ADDRANY TO INADDR_ANY
+ * * Errno::EAFNOSUPPORT - addresses in the specified family cannot be used with
+ * with this +socket+
+ * * Errno::ECONNREFUSED - the target _sockaddr_ was not listening for connections
+ * refused the connection request
+ * * Errno::EFAULT - the socket's internal address or address length parameter
+ * is too small or is not a valid part of the user space address
+ * * Errno::EINVAL - the +socket+ is a listening socket
+ * * Errno::EISCONN - the +socket+ is already connected
+ * * Errno::ENETUNREACH - the network cannot be reached from this host at this time
+ * * Errno::EHOSTUNREACH - no route to the network is present
+ * * Errno::ENOBUFS - no buffer space is available
+ * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
+ * * Errno::ETIMEDOUT - the attempt to connect time out before a connection
+ * was made.
+ * * Errno::EWOULDBLOCK - the socket is marked as nonblocking and the
+ * connection cannot be completed immediately
+ * * Errno::EACCES - the attempt to connect the datagram socket to the
+ * broadcast address failed
+ *
+ * === See
+ * * connect manual pages on unix-based systems
+ * * connect function in Microsoft's Winsock functions reference
+ */
+static VALUE
+sock_connect(VALUE sock, VALUE addr)
+{
+ VALUE rai;
+ rb_io_t *fptr;
+ int fd, n;
+
+ SockAddrStringValueWithAddrinfo(addr, rai);
+ addr = rb_str_new4(addr);
+ GetOpenFile(sock, fptr);
+ fd = fptr->fd;
+ n = rsock_connect(fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr), 0);
+ if (n < 0) {
+ rsock_sys_fail_raddrinfo_or_sockaddr("connect(2)", addr, rai);
+ }
+
+ return INT2FIX(n);
+}
+
+/*
+ * call-seq:
+ * socket.connect_nonblock(remote_sockaddr) => 0
+ *
+ * Requests a connection to be made on the given +remote_sockaddr+ after
+ * O_NONBLOCK is set for the underlying file descriptor.
+ * Returns 0 if successful, otherwise an exception is raised.
+ *
+ * === Parameter
+ * * +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
+ *
+ * === Example:
+ * # Pull down Google's web page
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
+ * sockaddr = Socket.sockaddr_in(80, 'www.google.com')
+ * begin # emulate blocking connect
+ * socket.connect_nonblock(sockaddr)
+ * rescue IO::WaitWritable
+ * IO.select(nil, [socket]) # wait 3-way handshake completion
+ * begin
+ * socket.connect_nonblock(sockaddr) # check connection failure
+ * rescue Errno::EISCONN
+ * end
+ * end
+ * socket.write("GET / HTTP/1.0\r\n\r\n")
+ * results = socket.read
+ *
+ * Refer to Socket#connect for the exceptions that may be thrown if the call
+ * to _connect_nonblock_ fails.
+ *
+ * Socket#connect_nonblock may raise any error corresponding to connect(2) failure,
+ * including Errno::EINPROGRESS.
+ *
+ * If the exception is Errno::EINPROGRESS,
+ * it is extended by IO::WaitWritable.
+ * So IO::WaitWritable can be used to rescue the exceptions for retrying connect_nonblock.
+ *
+ * === See
+ * * Socket#connect
+ */
+static VALUE
+sock_connect_nonblock(VALUE sock, VALUE addr)
+{
+ VALUE rai;
+ rb_io_t *fptr;
+ int n;
+
+ SockAddrStringValueWithAddrinfo(addr, rai);
+ addr = rb_str_new4(addr);
+ GetOpenFile(sock, fptr);
+ rb_io_set_nonblock(fptr);
+ n = connect(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr));
+ if (n < 0) {
+ if (errno == EINPROGRESS)
+ rb_readwrite_sys_fail(RB_IO_WAIT_WRITABLE, "connect(2) would block");
+ rsock_sys_fail_raddrinfo_or_sockaddr("connect(2)", addr, rai);
+ }
+
+ return INT2FIX(n);
+}
+
+/*
+ * call-seq:
+ * socket.bind(local_sockaddr) => 0
+ *
+ * Binds to the given local address.
+ *
+ * === Parameter
+ * * +local_sockaddr+ - the +struct+ sockaddr contained in a string or an Addrinfo object
+ *
+ * === Example
+ * require 'socket'
+ *
+ * # use Addrinfo
+ * socket = Socket.new(:INET, :STREAM, 0)
+ * socket.bind(Addrinfo.tcp("127.0.0.1", 2222))
+ * p socket.local_address #=> #<Addrinfo: 127.0.0.1:2222 TCP>
+ *
+ * # use struct sockaddr
+ * include Socket::Constants
+ * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
+ * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
+ * socket.bind( sockaddr )
+ *
+ * === Unix-based Exceptions
+ * On unix-based based systems the following system exceptions may be raised if
+ * the call to _bind_ fails:
+ * * Errno::EACCES - the specified _sockaddr_ is protected and the current
+ * user does not have permission to bind to it
+ * * Errno::EADDRINUSE - the specified _sockaddr_ is already in use
+ * * Errno::EADDRNOTAVAIL - the specified _sockaddr_ is not available from the
+ * local machine
+ * * Errno::EAFNOSUPPORT - the specified _sockaddr_ is not a valid address for
+ * the family of the calling +socket+
+ * * Errno::EBADF - the _sockaddr_ specified is not a valid file descriptor
+ * * Errno::EFAULT - the _sockaddr_ argument cannot be accessed
+ * * Errno::EINVAL - the +socket+ is already bound to an address, and the
+ * protocol does not support binding to the new _sockaddr_ or the +socket+
+ * has been shut down.
+ * * Errno::EINVAL - the address length is not a valid length for the address
+ * family
+ * * Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
+ * PATH_MAX
+ * * Errno::ENOBUFS - no buffer space is available
+ * * Errno::ENOSR - there were insufficient STREAMS resources available to
+ * complete the operation
+ * * Errno::ENOTSOCK - the +socket+ does not refer to a socket
+ * * Errno::EOPNOTSUPP - the socket type of the +socket+ does not support
+ * binding to an address
+ *
+ * On unix-based based systems if the address family of the calling +socket+ is
+ * Socket::AF_UNIX the follow exceptions may be raised if the call to _bind_
+ * fails:
+ * * Errno::EACCES - search permission is denied for a component of the prefix
+ * path or write access to the +socket+ is denied
+ * * Errno::EDESTADDRREQ - the _sockaddr_ argument is a null pointer
+ * * Errno::EISDIR - same as Errno::EDESTADDRREQ
+ * * Errno::EIO - an i/o error occurred
+ * * Errno::ELOOP - too many symbolic links were encountered in translating
+ * the pathname in _sockaddr_
+ * * Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
+ * characters, or an entire pathname exceeded PATH_MAX characters
+ * * Errno::ENOENT - a component of the pathname does not name an existing file
+ * or the pathname is an empty string
+ * * Errno::ENOTDIR - a component of the path prefix of the pathname in _sockaddr_
+ * is not a directory
+ * * Errno::EROFS - the name would reside on a read only filesystem
+ *
+ * === Windows Exceptions
+ * On Windows systems the following system exceptions may be raised if
+ * the call to _bind_ fails:
+ * * Errno::ENETDOWN-- the network is down
+ * * Errno::EACCES - the attempt to connect the datagram socket to the
+ * broadcast address failed
+ * * Errno::EADDRINUSE - the socket's local address is already in use
+ * * Errno::EADDRNOTAVAIL - the specified address is not a valid address for this
+ * computer
+ * * Errno::EFAULT - the socket's internal address or address length parameter
+ * is too small or is not a valid part of the user space addressed
+ * * Errno::EINVAL - the +socket+ is already bound to an address
+ * * Errno::ENOBUFS - no buffer space is available
+ * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
+ *
+ * === See
+ * * bind manual pages on unix-based systems
+ * * bind function in Microsoft's Winsock functions reference
+ */
+static VALUE
+sock_bind(VALUE sock, VALUE addr)
+{
+ VALUE rai;
+ rb_io_t *fptr;
+
+ SockAddrStringValueWithAddrinfo(addr, rai);
+ GetOpenFile(sock, fptr);
+ if (bind(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr)) < 0)
+ rsock_sys_fail_raddrinfo_or_sockaddr("bind(2)", addr, rai);
+
+ return INT2FIX(0);
+}
+
+/*
+ * call-seq:
+ * socket.listen( int ) => 0
+ *
+ * Listens for connections, using the specified +int+ as the backlog. A call
+ * to _listen_ only applies if the +socket+ is of type SOCK_STREAM or
+ * SOCK_SEQPACKET.
+ *
+ * === Parameter
+ * * +backlog+ - the maximum length of the queue for pending connections.
+ *
+ * === Example 1
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
+ * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
+ * socket.bind( sockaddr )
+ * socket.listen( 5 )
+ *
+ * === Example 2 (listening on an arbitrary port, unix-based systems only):
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
+ * socket.listen( 1 )
+ *
+ * === Unix-based Exceptions
+ * On unix based systems the above will work because a new +sockaddr+ struct
+ * is created on the address ADDR_ANY, for an arbitrary port number as handed
+ * off by the kernel. It will not work on Windows, because Windows requires that
+ * the +socket+ is bound by calling _bind_ before it can _listen_.
+ *
+ * If the _backlog_ amount exceeds the implementation-dependent maximum
+ * queue length, the implementation's maximum queue length will be used.
+ *
+ * On unix-based based systems the following system exceptions may be raised if the
+ * call to _listen_ fails:
+ * * Errno::EBADF - the _socket_ argument is not a valid file descriptor
+ * * Errno::EDESTADDRREQ - the _socket_ is not bound to a local address, and
+ * the protocol does not support listening on an unbound socket
+ * * Errno::EINVAL - the _socket_ is already connected
+ * * Errno::ENOTSOCK - the _socket_ argument does not refer to a socket
+ * * Errno::EOPNOTSUPP - the _socket_ protocol does not support listen
+ * * Errno::EACCES - the calling process does not have appropriate privileges
+ * * Errno::EINVAL - the _socket_ has been shut down
+ * * Errno::ENOBUFS - insufficient resources are available in the system to
+ * complete the call
+ *
+ * === Windows Exceptions
+ * On Windows systems the following system exceptions may be raised if
+ * the call to _listen_ fails:
+ * * Errno::ENETDOWN - the network is down
+ * * Errno::EADDRINUSE - the socket's local address is already in use. This
+ * usually occurs during the execution of _bind_ but could be delayed
+ * if the call to _bind_ was to a partially wildcard address (involving
+ * ADDR_ANY) and if a specific address needs to be committed at the
+ * time of the call to _listen_
+ * * Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the
+ * service provider is still processing a callback function
+ * * Errno::EINVAL - the +socket+ has not been bound with a call to _bind_.
+ * * Errno::EISCONN - the +socket+ is already connected
+ * * Errno::EMFILE - no more socket descriptors are available
+ * * Errno::ENOBUFS - no buffer space is available
+ * * Errno::ENOTSOC - +socket+ is not a socket
+ * * Errno::EOPNOTSUPP - the referenced +socket+ is not a type that supports
+ * the _listen_ method
+ *
+ * === See
+ * * listen manual pages on unix-based systems
+ * * listen function in Microsoft's Winsock functions reference
+ */
+VALUE
+rsock_sock_listen(VALUE sock, VALUE log)
+{
+ rb_io_t *fptr;
+ int backlog;
+
+ backlog = NUM2INT(log);
+ GetOpenFile(sock, fptr);
+ if (listen(fptr->fd, backlog) < 0)
+ rb_sys_fail("listen(2)");
+
+ return INT2FIX(0);
+}
+
+/*
+ * call-seq:
+ * socket.recvfrom(maxlen) => [mesg, sender_addrinfo]
+ * socket.recvfrom(maxlen, flags) => [mesg, sender_addrinfo]
+ *
+ * Receives up to _maxlen_ bytes from +socket+. _flags_ is zero or more
+ * of the +MSG_+ options. The first element of the results, _mesg_, is the data
+ * received. The second element, _sender_addrinfo_, contains protocol-specific
+ * address information of the sender.
+ *
+ * === Parameters
+ * * +maxlen+ - the maximum number of bytes to receive from the socket
+ * * +flags+ - zero or more of the +MSG_+ options
+ *
+ * === Example
+ * # In one file, start this first
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
+ * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
+ * socket.bind( sockaddr )
+ * socket.listen( 5 )
+ * client, client_addrinfo = socket.accept
+ * data = client.recvfrom( 20 )[0].chomp
+ * puts "I only received 20 bytes '#{data}'"
+ * sleep 1
+ * socket.close
+ *
+ * # In another file, start this second
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
+ * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
+ * socket.connect( sockaddr )
+ * socket.puts "Watch this get cut short!"
+ * socket.close
+ *
+ * === Unix-based Exceptions
+ * On unix-based based systems the following system exceptions may be raised if the
+ * call to _recvfrom_ fails:
+ * * Errno::EAGAIN - the +socket+ file descriptor is marked as O_NONBLOCK and no
+ * data is waiting to be received; or MSG_OOB is set and no out-of-band data
+ * is available and either the +socket+ file descriptor is marked as
+ * O_NONBLOCK or the +socket+ does not support blocking to wait for
+ * out-of-band-data
+ * * Errno::EWOULDBLOCK - see Errno::EAGAIN
+ * * Errno::EBADF - the +socket+ is not a valid file descriptor
+ * * Errno::ECONNRESET - a connection was forcibly closed by a peer
+ * * Errno::EFAULT - the socket's internal buffer, address or address length
+ * cannot be accessed or written
+ * * Errno::EINTR - a signal interrupted _recvfrom_ before any data was available
+ * * Errno::EINVAL - the MSG_OOB flag is set and no out-of-band data is available
+ * * Errno::EIO - an i/o error occurred while reading from or writing to the
+ * filesystem
+ * * Errno::ENOBUFS - insufficient resources were available in the system to
+ * perform the operation
+ * * Errno::ENOMEM - insufficient memory was available to fulfill the request
+ * * Errno::ENOSR - there were insufficient STREAMS resources available to
+ * complete the operation
+ * * Errno::ENOTCONN - a receive is attempted on a connection-mode socket that
+ * is not connected
+ * * Errno::ENOTSOCK - the +socket+ does not refer to a socket
+ * * Errno::EOPNOTSUPP - the specified flags are not supported for this socket type
+ * * Errno::ETIMEDOUT - the connection timed out during connection establishment
+ * or due to a transmission timeout on an active connection
+ *
+ * === Windows Exceptions
+ * On Windows systems the following system exceptions may be raised if
+ * the call to _recvfrom_ fails:
+ * * Errno::ENETDOWN - the network is down
+ * * Errno::EFAULT - the internal buffer and from parameters on +socket+ are not
+ * part of the user address space, or the internal fromlen parameter is
+ * too small to accommodate the peer address
+ * * Errno::EINTR - the (blocking) call was cancelled by an internal call to
+ * the WinSock function WSACancelBlockingCall
+ * * Errno::EINPROGRESS - a blocking Windows Sockets 1.1 call is in progress or
+ * the service provider is still processing a callback function
+ * * Errno::EINVAL - +socket+ has not been bound with a call to _bind_, or an
+ * unknown flag was specified, or MSG_OOB was specified for a socket with
+ * SO_OOBINLINE enabled, or (for byte stream-style sockets only) the internal
+ * len parameter on +socket+ was zero or negative
+ * * Errno::EISCONN - +socket+ is already connected. The call to _recvfrom_ is
+ * not permitted with a connected socket on a socket that is connection
+ * oriented or connectionless.
+ * * Errno::ENETRESET - the connection has been broken due to the keep-alive
+ * activity detecting a failure while the operation was in progress.
+ * * Errno::EOPNOTSUPP - MSG_OOB was specified, but +socket+ is not stream-style
+ * such as type SOCK_STREAM. OOB data is not supported in the communication
+ * domain associated with +socket+, or +socket+ is unidirectional and
+ * supports only send operations
+ * * Errno::ESHUTDOWN - +socket+ has been shutdown. It is not possible to
+ * call _recvfrom_ on a socket after _shutdown_ has been invoked.
+ * * Errno::EWOULDBLOCK - +socket+ is marked as nonblocking and a call to
+ * _recvfrom_ would block.
+ * * Errno::EMSGSIZE - the message was too large to fit into the specified buffer
+ * and was truncated.
+ * * Errno::ETIMEDOUT - the connection has been dropped, because of a network
+ * failure or because the system on the other end went down without
+ * notice
+ * * Errno::ECONNRESET - the virtual circuit was reset by the remote side
+ * executing a hard or abortive close. The application should close the
+ * socket; it is no longer usable. On a UDP-datagram socket this error
+ * indicates a previous send operation resulted in an ICMP Port Unreachable
+ * message.
+ */
+static VALUE
+sock_recvfrom(int argc, VALUE *argv, VALUE sock)
+{
+ return rsock_s_recvfrom(sock, argc, argv, RECV_SOCKET);
+}
+
+/*
+ * call-seq:
+ * socket.recvfrom_nonblock(maxlen) => [mesg, sender_addrinfo]
+ * socket.recvfrom_nonblock(maxlen, flags) => [mesg, sender_addrinfo]
+ *
+ * Receives up to _maxlen_ bytes from +socket+ using recvfrom(2) after
+ * O_NONBLOCK is set for the underlying file descriptor.
+ * _flags_ is zero or more of the +MSG_+ options.
+ * The first element of the results, _mesg_, is the data received.
+ * The second element, _sender_addrinfo_, contains protocol-specific address
+ * information of the sender.
+ *
+ * When recvfrom(2) returns 0, Socket#recvfrom_nonblock returns
+ * an empty string as data.
+ * The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.
+ *
+ * === Parameters
+ * * +maxlen+ - the maximum number of bytes to receive from the socket
+ * * +flags+ - zero or more of the +MSG_+ options
+ *
+ * === Example
+ * # In one file, start this first
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
+ * sockaddr = Socket.sockaddr_in(2200, 'localhost')
+ * socket.bind(sockaddr)
+ * socket.listen(5)
+ * client, client_addrinfo = socket.accept
+ * begin # emulate blocking recvfrom
+ * pair = client.recvfrom_nonblock(20)
+ * rescue IO::WaitReadable
+ * IO.select([client])
+ * retry
+ * end
+ * data = pair[0].chomp
+ * puts "I only received 20 bytes '#{data}'"
+ * sleep 1
+ * socket.close
+ *
+ * # In another file, start this second
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
+ * sockaddr = Socket.sockaddr_in(2200, 'localhost')
+ * socket.connect(sockaddr)
+ * socket.puts "Watch this get cut short!"
+ * socket.close
+ *
+ * Refer to Socket#recvfrom for the exceptions that may be thrown if the call
+ * to _recvfrom_nonblock_ fails.
+ *
+ * Socket#recvfrom_nonblock may raise any error corresponding to recvfrom(2) failure,
+ * including Errno::EWOULDBLOCK.
+ *
+ * If the exception is Errno::EWOULDBLOCK or Errno::AGAIN,
+ * it is extended by IO::WaitReadable.
+ * So IO::WaitReadable can be used to rescue the exceptions for retrying recvfrom_nonblock.
+ *
+ * === See
+ * * Socket#recvfrom
+ */
+static VALUE
+sock_recvfrom_nonblock(int argc, VALUE *argv, VALUE sock)
+{
+ return rsock_s_recvfrom_nonblock(sock, argc, argv, RECV_SOCKET);
+}
+
+/*
+ * call-seq:
+ * socket.accept => [client_socket, client_addrinfo]
+ *
+ * Accepts a next connection.
+ * Returns a new Socket object and Addrinfo object.
+ *
+ * serv = Socket.new(:INET, :STREAM, 0)
+ * serv.listen(5)
+ * c = Socket.new(:INET, :STREAM, 0)
+ * c.connect(serv.connect_address)
+ * p serv.accept #=> [#<Socket:fd 6>, #<Addrinfo: 127.0.0.1:48555 TCP>]
+ *
+ */
+static VALUE
+sock_accept(VALUE sock)
+{
+ rb_io_t *fptr;
+ VALUE sock2;
+ union_sockaddr buf;
+ socklen_t len = (socklen_t)sizeof buf;
+
+ GetOpenFile(sock, fptr);
+ sock2 = rsock_s_accept(rb_cSocket,fptr->fd,&buf.addr,&len);
+
+ return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, &buf.addr, len));
+}
+
+/*
+ * call-seq:
+ * socket.accept_nonblock => [client_socket, client_addrinfo]
+ *
+ * Accepts an incoming connection using accept(2) after
+ * O_NONBLOCK is set for the underlying file descriptor.
+ * It returns an array containing the accepted socket
+ * for the incoming connection, _client_socket_,
+ * and an Addrinfo, _client_addrinfo_.
+ *
+ * === Example
+ * # In one script, start this first
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
+ * sockaddr = Socket.sockaddr_in(2200, 'localhost')
+ * socket.bind(sockaddr)
+ * socket.listen(5)
+ * begin # emulate blocking accept
+ * client_socket, client_addrinfo = socket.accept_nonblock
+ * rescue IO::WaitReadable, Errno::EINTR
+ * IO.select([socket])
+ * retry
+ * end
+ * puts "The client said, '#{client_socket.readline.chomp}'"
+ * client_socket.puts "Hello from script one!"
+ * socket.close
+ *
+ * # In another script, start this second
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
+ * sockaddr = Socket.sockaddr_in(2200, 'localhost')
+ * socket.connect(sockaddr)
+ * socket.puts "Hello from script 2."
+ * puts "The server said, '#{socket.readline.chomp}'"
+ * socket.close
+ *
+ * Refer to Socket#accept for the exceptions that may be thrown if the call
+ * to _accept_nonblock_ fails.
+ *
+ * Socket#accept_nonblock may raise any error corresponding to accept(2) failure,
+ * including Errno::EWOULDBLOCK.
+ *
+ * If the exception is Errno::EWOULDBLOCK, Errno::AGAIN, Errno::ECONNABORTED or Errno::EPROTO,
+ * it is extended by IO::WaitReadable.
+ * So IO::WaitReadable can be used to rescue the exceptions for retrying accept_nonblock.
+ *
+ * === See
+ * * Socket#accept
+ */
+static VALUE
+sock_accept_nonblock(VALUE sock)
+{
+ rb_io_t *fptr;
+ VALUE sock2;
+ union_sockaddr buf;
+ socklen_t len = (socklen_t)sizeof buf;
+
+ GetOpenFile(sock, fptr);
+ sock2 = rsock_s_accept_nonblock(rb_cSocket, fptr, &buf.addr, &len);
+ return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, &buf.addr, len));
+}
+
+/*
+ * call-seq:
+ * socket.sysaccept => [client_socket_fd, client_addrinfo]
+ *
+ * Accepts an incoming connection returning an array containing the (integer)
+ * file descriptor for the incoming connection, _client_socket_fd_,
+ * and an Addrinfo, _client_addrinfo_.
+ *
+ * === Example
+ * # In one script, start this first
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
+ * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
+ * socket.bind( sockaddr )
+ * socket.listen( 5 )
+ * client_fd, client_addrinfo = socket.sysaccept
+ * client_socket = Socket.for_fd( client_fd )
+ * puts "The client said, '#{client_socket.readline.chomp}'"
+ * client_socket.puts "Hello from script one!"
+ * socket.close
+ *
+ * # In another script, start this second
+ * require 'socket'
+ * include Socket::Constants
+ * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
+ * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
+ * socket.connect( sockaddr )
+ * socket.puts "Hello from script 2."
+ * puts "The server said, '#{socket.readline.chomp}'"
+ * socket.close
+ *
+ * Refer to Socket#accept for the exceptions that may be thrown if the call
+ * to _sysaccept_ fails.
+ *
+ * === See
+ * * Socket#accept
+ */
+static VALUE
+sock_sysaccept(VALUE sock)
+{
+ rb_io_t *fptr;
+ VALUE sock2;
+ union_sockaddr buf;
+ socklen_t len = (socklen_t)sizeof buf;
+
+ GetOpenFile(sock, fptr);
+ sock2 = rsock_s_accept(0,fptr->fd,&buf.addr,&len);
+
+ return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, &buf.addr, len));
+}
+
+#ifdef HAVE_GETHOSTNAME
+/*
+ * call-seq:
+ * Socket.gethostname => hostname
+ *
+ * Returns the hostname.
+ *
+ * p Socket.gethostname #=> "hal"
+ *
+ * Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc.
+ * If you need local IP address, use Socket.ip_address_list.
+ */
+static VALUE
+sock_gethostname(VALUE obj)
+{
+#if defined(NI_MAXHOST)
+# define RUBY_MAX_HOST_NAME_LEN NI_MAXHOST
+#elif defined(HOST_NAME_MAX)
+# define RUBY_MAX_HOST_NAME_LEN HOST_NAME_MAX
+#else
+# define RUBY_MAX_HOST_NAME_LEN 1024
+#endif
+
+ char buf[RUBY_MAX_HOST_NAME_LEN+1];
+
+ rb_secure(3);
+ if (gethostname(buf, (int)sizeof buf - 1) < 0)
+ rb_sys_fail("gethostname(3)");
+
+ buf[sizeof buf - 1] = '\0';
+ return rb_str_new2(buf);
+}
+#else
+#ifdef HAVE_UNAME
+
+#include <sys/utsname.h>
+
+static VALUE
+sock_gethostname(VALUE obj)
+{
+ struct utsname un;
+
+ rb_secure(3);
+ uname(&un);
+ return rb_str_new2(un.nodename);
+}
+#else
+#define sock_gethostname rb_f_notimplement
+#endif
+#endif
+
+static VALUE
+make_addrinfo(struct rb_addrinfo *res0, int norevlookup)
+{
+ VALUE base, ary;
+ struct addrinfo *res;
+
+ if (res0 == NULL) {
+ rb_raise(rb_eSocket, "host not found");
+ }
+ base = rb_ary_new();
+ for (res = res0->ai; res; res = res->ai_next) {
+ ary = rsock_ipaddr(res->ai_addr, res->ai_addrlen, norevlookup);
+ if (res->ai_canonname) {
+ RARRAY_PTR(ary)[2] = rb_str_new2(res->ai_canonname);
+ }
+ rb_ary_push(ary, INT2FIX(res->ai_family));
+ rb_ary_push(ary, INT2FIX(res->ai_socktype));
+ rb_ary_push(ary, INT2FIX(res->ai_protocol));
+ rb_ary_push(base, ary);
+ }
+ return base;
+}
+
+static VALUE
+sock_sockaddr(struct sockaddr *addr, socklen_t len)
+{
+ char *ptr;
+
+ switch (addr->sa_family) {
+ case AF_INET:
+ ptr = (char*)&((struct sockaddr_in*)addr)->sin_addr.s_addr;
+ len = (socklen_t)sizeof(((struct sockaddr_in*)addr)->sin_addr.s_addr);
+ break;
+#ifdef AF_INET6
+ case AF_INET6:
+ ptr = (char*)&((struct sockaddr_in6*)addr)->sin6_addr.s6_addr;
+ len = (socklen_t)sizeof(((struct sockaddr_in6*)addr)->sin6_addr.s6_addr);
+ break;
+#endif
+ default:
+ rb_raise(rb_eSocket, "unknown socket family:%d", addr->sa_family);
+ break;
+ }
+ return rb_str_new(ptr, len);
+}
+
+/*
+ * call-seq:
+ * Socket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list]
+ *
+ * Obtains the host information for _hostname_.
+ *
+ * p Socket.gethostbyname("hal") #=> ["localhost", ["hal"], 2, "\x7F\x00\x00\x01"]
+ *
+ */
+static VALUE
+sock_s_gethostbyname(VALUE obj, VALUE host)
+{
+ rb_secure(3);
+ return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME), sock_sockaddr);
+}
+
+/*
+ * call-seq:
+ * Socket.gethostbyaddr(address_string [, address_family]) => hostent
+ *
+ * Obtains the host information for _address_.
+ *
+ * p Socket.gethostbyaddr([221,186,184,68].pack("CCCC"))
+ * #=> ["carbon.ruby-lang.org", [], 2, "\xDD\xBA\xB8D"]
+ */
+static VALUE
+sock_s_gethostbyaddr(int argc, VALUE *argv)
+{
+ VALUE addr, family;
+ struct hostent *h;
+ char **pch;
+ VALUE ary, names;
+ int t = AF_INET;
+
+ rb_scan_args(argc, argv, "11", &addr, &family);
+ StringValue(addr);
+ if (!NIL_P(family)) {
+ t = rsock_family_arg(family);
+ }
+#ifdef AF_INET6
+ else if (RSTRING_LEN(addr) == 16) {
+ t = AF_INET6;
+ }
+#endif
+ h = gethostbyaddr(RSTRING_PTR(addr), RSTRING_SOCKLEN(addr), t);
+ if (h == NULL) {
+#ifdef HAVE_HSTRERROR
+ extern int h_errno;
+ rb_raise(rb_eSocket, "%s", (char*)hstrerror(h_errno));
+#else
+ rb_raise(rb_eSocket, "host not found");
+#endif
+ }
+ ary = rb_ary_new();
+ rb_ary_push(ary, rb_str_new2(h->h_name));
+ names = rb_ary_new();
+ rb_ary_push(ary, names);
+ if (h->h_aliases != NULL) {
+ for (pch = h->h_aliases; *pch; pch++) {
+ rb_ary_push(names, rb_str_new2(*pch));
+ }
+ }
+ rb_ary_push(ary, INT2NUM(h->h_addrtype));
+#ifdef h_addr
+ for (pch = h->h_addr_list; *pch; pch++) {
+ rb_ary_push(ary, rb_str_new(*pch, h->h_length));
+ }
+#else
+ rb_ary_push(ary, rb_str_new(h->h_addr, h->h_length));
+#endif
+
+ return ary;
+}
+
+/*
+ * call-seq:
+ * Socket.getservbyname(service_name) => port_number
+ * Socket.getservbyname(service_name, protocol_name) => port_number
+ *
+ * Obtains the port number for _service_name_.
+ *
+ * If _protocol_name_ is not given, "tcp" is assumed.
+ *
+ * Socket.getservbyname("smtp") #=> 25
+ * Socket.getservbyname("shell") #=> 514
+ * Socket.getservbyname("syslog", "udp") #=> 514
+ */
+static VALUE
+sock_s_getservbyname(int argc, VALUE *argv)
+{
+ VALUE service, proto;
+ struct servent *sp;
+ long port;
+ const char *servicename, *protoname = "tcp";
+
+ rb_scan_args(argc, argv, "11", &service, &proto);
+ StringValue(service);
+ if (!NIL_P(proto)) StringValue(proto);
+ servicename = StringValueCStr(service);
+ if (!NIL_P(proto)) protoname = StringValueCStr(proto);
+ sp = getservbyname(servicename, protoname);
+ if (sp) {
+ port = ntohs(sp->s_port);
+ }
+ else {
+ char *end;
+
+ port = STRTOUL(servicename, &end, 0);
+ if (*end != '\0') {
+ rb_raise(rb_eSocket, "no such service %s/%s", servicename, protoname);
+ }
+ }
+ return INT2FIX(port);
+}
+
+/*
+ * call-seq:
+ * Socket.getservbyport(port [, protocol_name]) => service
+ *
+ * Obtains the port number for _port_.
+ *
+ * If _protocol_name_ is not given, "tcp" is assumed.
+ *
+ * Socket.getservbyport(80) #=> "www"
+ * Socket.getservbyport(514, "tcp") #=> "shell"
+ * Socket.getservbyport(514, "udp") #=> "syslog"
+ *
+ */
+static VALUE
+sock_s_getservbyport(int argc, VALUE *argv)
+{
+ VALUE port, proto;
+ struct servent *sp;
+ long portnum;
+ const char *protoname = "tcp";
+
+ rb_scan_args(argc, argv, "11", &port, &proto);
+ portnum = NUM2LONG(port);
+ if (portnum != (uint16_t)portnum) {
+ const char *s = portnum > 0 ? "big" : "small";
+ rb_raise(rb_eRangeError, "integer %ld too %s to convert into `int16_t'", portnum, s);
+ }
+ if (!NIL_P(proto)) protoname = StringValueCStr(proto);
+
+ sp = getservbyport((int)htons((uint16_t)portnum), protoname);
+ if (!sp) {
+ rb_raise(rb_eSocket, "no such service for port %d/%s", (int)portnum, protoname);
+ }
+ return rb_tainted_str_new2(sp->s_name);
+}
+
+/*
+ * call-seq:
+ * Socket.getaddrinfo(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) => array
+ *
+ * Obtains address information for _nodename_:_servname_.
+ *
+ * _family_ should be an address family such as: :INET, :INET6, :UNIX, etc.
+ *
+ * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
+ *
+ * _protocol_ should be a protocol defined in the family,
+ * and defaults to 0 for the family.
+ *
+ * _flags_ should be bitwise OR of Socket::AI_* constants.
+ *
+ * Socket.getaddrinfo("www.ruby-lang.org", "http", nil, :STREAM)
+ * #=> [["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68", 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP
+ *
+ * Socket.getaddrinfo("localhost", nil)
+ * #=> [["AF_INET", 0, "localhost", "127.0.0.1", 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP
+ * # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP
+ * # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP
+ *
+ * _reverse_lookup_ directs the form of the third element, and has to
+ * be one of below. If _reverse_lookup_ is omitted, the default value is +nil+.
+ *
+ * +true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time.
+ * +false+, +:numeric+: hostname is same as numeric address.
+ * +nil+: obey to the current +do_not_reverse_lookup+ flag.
+ *
+ * If Addrinfo object is preferred, use Addrinfo.getaddrinfo.
+ */
+static VALUE
+sock_s_getaddrinfo(int argc, VALUE *argv)
+{
+ VALUE host, port, family, socktype, protocol, flags, ret, revlookup;
+ struct addrinfo hints;
+ struct rb_addrinfo *res;
+ int norevlookup;
+
+ rb_scan_args(argc, argv, "25", &host, &port, &family, &socktype, &protocol, &flags, &revlookup);
+
+ MEMZERO(&hints, struct addrinfo, 1);
+ hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family);
+
+ if (!NIL_P(socktype)) {
+ hints.ai_socktype = rsock_socktype_arg(socktype);
+ }
+ if (!NIL_P(protocol)) {
+ hints.ai_protocol = NUM2INT(protocol);
+ }
+ if (!NIL_P(flags)) {
+ hints.ai_flags = NUM2INT(flags);
+ }
+ if (NIL_P(revlookup) || !rsock_revlookup_flag(revlookup, &norevlookup)) {
+ norevlookup = rsock_do_not_reverse_lookup;
+ }
+ res = rsock_getaddrinfo(host, port, &hints, 0);
+
+ ret = make_addrinfo(res, norevlookup);
+ rb_freeaddrinfo(res);
+ return ret;
+}
+
+/*
+ * call-seq:
+ * Socket.getnameinfo(sockaddr [, flags]) => [hostname, servicename]
+ *
+ * Obtains name information for _sockaddr_.
+ *
+ * _sockaddr_ should be one of follows.
+ * - packed sockaddr string such as Socket.sockaddr_in(80, "127.0.0.1")
+ * - 3-elements array such as ["AF_INET", 80, "127.0.0.1"]
+ * - 4-elements array such as ["AF_INET", 80, ignored, "127.0.0.1"]
+ *
+ * _flags_ should be bitwise OR of Socket::NI_* constants.
+ *
+ * Note:
+ * The last form is compatible with IPSocket#addr and IPSocket#peeraddr.
+ *
+ * Socket.getnameinfo(Socket.sockaddr_in(80, "127.0.0.1")) #=> ["localhost", "www"]
+ * Socket.getnameinfo(["AF_INET", 80, "127.0.0.1"]) #=> ["localhost", "www"]
+ * Socket.getnameinfo(["AF_INET", 80, "localhost", "127.0.0.1"]) #=> ["localhost", "www"]
+ *
+ * If Addrinfo object is preferred, use Addrinfo#getnameinfo.
+ */
+static VALUE
+sock_s_getnameinfo(int argc, VALUE *argv)
+{
+ VALUE sa, af = Qnil, host = Qnil, port = Qnil, flags, tmp;
+ char *hptr, *pptr;
+ char hbuf[1024], pbuf[1024];
+ int fl;
+ struct rb_addrinfo *res = NULL;
+ struct addrinfo hints, *r;
+ int error, saved_errno;
+ union_sockaddr ss;
+ struct sockaddr *sap;
+ socklen_t salen;
+
+ sa = flags = Qnil;
+ rb_scan_args(argc, argv, "11", &sa, &flags);
+
+ fl = 0;
+ if (!NIL_P(flags)) {
+ fl = NUM2INT(flags);
+ }
+ tmp = rb_check_sockaddr_string_type(sa);
+ if (!NIL_P(tmp)) {
+ sa = tmp;
+ if (sizeof(ss) < (size_t)RSTRING_LEN(sa)) {
+ rb_raise(rb_eTypeError, "sockaddr length too big");
+ }
+ memcpy(&ss, RSTRING_PTR(sa), RSTRING_LEN(sa));
+ if (!VALIDATE_SOCKLEN(&ss.addr, RSTRING_LEN(sa))) {
+ rb_raise(rb_eTypeError, "sockaddr size differs - should not happen");
+ }
+ sap = &ss.addr;
+ salen = RSTRING_SOCKLEN(sa);
+ goto call_nameinfo;
+ }
+ tmp = rb_check_array_type(sa);
+ if (!NIL_P(tmp)) {
+ sa = tmp;
+ MEMZERO(&hints, struct addrinfo, 1);
+ if (RARRAY_LEN(sa) == 3) {
+ af = RARRAY_PTR(sa)[0];
+ port = RARRAY_PTR(sa)[1];
+ host = RARRAY_PTR(sa)[2];
+ }
+ else if (RARRAY_LEN(sa) >= 4) {
+ af = RARRAY_PTR(sa)[0];
+ port = RARRAY_PTR(sa)[1];
+ host = RARRAY_PTR(sa)[3];
+ if (NIL_P(host)) {
+ host = RARRAY_PTR(sa)[2];
+ }
+ else {
+ /*
+ * 4th element holds numeric form, don't resolve.
+ * see rsock_ipaddr().
+ */
+#ifdef AI_NUMERICHOST /* AIX 4.3.3 doesn't have AI_NUMERICHOST. */
+ hints.ai_flags |= AI_NUMERICHOST;
+#endif
+ }
+ }
+ else {
+ rb_raise(rb_eArgError, "array size should be 3 or 4, %ld given",
+ RARRAY_LEN(sa));
+ }
+ /* host */
+ if (NIL_P(host)) {
+ hptr = NULL;
+ }
+ else {
+ strncpy(hbuf, StringValuePtr(host), sizeof(hbuf));
+ hbuf[sizeof(hbuf) - 1] = '\0';
+ hptr = hbuf;
+ }
+ /* port */
+ if (NIL_P(port)) {
+ strcpy(pbuf, "0");
+ pptr = NULL;
+ }
+ else if (FIXNUM_P(port)) {
+ snprintf(pbuf, sizeof(pbuf), "%ld", NUM2LONG(port));
+ pptr = pbuf;
+ }
+ else {
+ strncpy(pbuf, StringValuePtr(port), sizeof(pbuf));
+ pbuf[sizeof(pbuf) - 1] = '\0';
+ pptr = pbuf;
+ }
+ hints.ai_socktype = (fl & NI_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
+ /* af */
+ hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af);
+ error = rb_getaddrinfo(hptr, pptr, &hints, &res);
+ if (error) goto error_exit_addr;
+ sap = res->ai->ai_addr;
+ salen = res->ai->ai_addrlen;
+ }
+ else {
+ rb_raise(rb_eTypeError, "expecting String or Array");
+ }
+
+ call_nameinfo:
+ error = rb_getnameinfo(sap, salen, hbuf, sizeof(hbuf),
+ pbuf, sizeof(pbuf), fl);
+ if (error) goto error_exit_name;
+ if (res) {
+ for (r = res->ai->ai_next; r; r = r->ai_next) {
+ char hbuf2[1024], pbuf2[1024];
+
+ sap = r->ai_addr;
+ 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) {
+ rb_freeaddrinfo(res);
+ rb_raise(rb_eSocket, "sockaddr resolved to multiple nodename");
+ }
+ }
+ rb_freeaddrinfo(res);
+ }
+ return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
+
+ error_exit_addr:
+ saved_errno = errno;
+ if (res) rb_freeaddrinfo(res);
+ errno = saved_errno;
+ rsock_raise_socket_error("getaddrinfo", error);
+
+ error_exit_name:
+ saved_errno = errno;
+ if (res) rb_freeaddrinfo(res);
+ errno = saved_errno;
+ rsock_raise_socket_error("getnameinfo", error);
+
+ UNREACHABLE;
+}
+
+/*
+ * call-seq:
+ * Socket.sockaddr_in(port, host) => sockaddr
+ * Socket.pack_sockaddr_in(port, host) => sockaddr
+ *
+ * Packs _port_ and _host_ as an AF_INET/AF_INET6 sockaddr string.
+ *
+ * Socket.sockaddr_in(80, "127.0.0.1")
+ * #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
+ *
+ * Socket.sockaddr_in(80, "::1")
+ * #=> "\n\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00"
+ *
+ */
+static VALUE
+sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
+{
+ struct rb_addrinfo *res = rsock_addrinfo(host, port, 0, 0);
+ VALUE addr = rb_str_new((char*)res->ai->ai_addr, res->ai->ai_addrlen);
+
+ rb_freeaddrinfo(res);
+ OBJ_INFECT(addr, port);
+ OBJ_INFECT(addr, host);
+
+ return addr;
+}
+
+/*
+ * call-seq:
+ * Socket.unpack_sockaddr_in(sockaddr) => [port, ip_address]
+ *
+ * Unpacks _sockaddr_ into port and ip_address.
+ *
+ * _sockaddr_ should be a string or an addrinfo for AF_INET/AF_INET6.
+ *
+ * sockaddr = Socket.sockaddr_in(80, "127.0.0.1")
+ * p sockaddr #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
+ * p Socket.unpack_sockaddr_in(sockaddr) #=> [80, "127.0.0.1"]
+ *
+ */
+static VALUE
+sock_s_unpack_sockaddr_in(VALUE self, VALUE addr)
+{
+ struct sockaddr_in * sockaddr;
+ VALUE host;
+
+ sockaddr = (struct sockaddr_in*)SockAddrStringValuePtr(addr);
+ if (RSTRING_LEN(addr) <
+ (char*)&((struct sockaddr *)sockaddr)->sa_family +
+ sizeof(((struct sockaddr *)sockaddr)->sa_family) -
+ (char*)sockaddr)
+ rb_raise(rb_eArgError, "too short sockaddr");
+ if (((struct sockaddr *)sockaddr)->sa_family != AF_INET
+#ifdef INET6
+ && ((struct sockaddr *)sockaddr)->sa_family != AF_INET6
+#endif
+ ) {
+#ifdef INET6
+ rb_raise(rb_eArgError, "not an AF_INET/AF_INET6 sockaddr");
+#else
+ rb_raise(rb_eArgError, "not an AF_INET sockaddr");
+#endif
+ }
+ host = rsock_make_ipaddr((struct sockaddr*)sockaddr, RSTRING_SOCKLEN(addr));
+ OBJ_INFECT(host, addr);
+ return rb_assoc_new(INT2NUM(ntohs(sockaddr->sin_port)), host);
+}
+
+#ifdef HAVE_SYS_UN_H
+
+/*
+ * call-seq:
+ * Socket.sockaddr_un(path) => sockaddr
+ * Socket.pack_sockaddr_un(path) => sockaddr
+ *
+ * Packs _path_ as an AF_UNIX sockaddr string.
+ *
+ * Socket.sockaddr_un("/tmp/sock") #=> "\x01\x00/tmp/sock\x00\x00..."
+ *
+ */
+static VALUE
+sock_s_pack_sockaddr_un(VALUE self, VALUE path)
+{
+ struct sockaddr_un sockaddr;
+ VALUE addr;
+
+ StringValue(path);
+ INIT_SOCKADDR_UN(&sockaddr, sizeof(struct sockaddr_un));
+ if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) {
+ rb_raise(rb_eArgError, "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
+ (size_t)RSTRING_LEN(path), sizeof(sockaddr.sun_path));
+ }
+ memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
+ addr = rb_str_new((char*)&sockaddr, rsock_unix_sockaddr_len(path));
+ OBJ_INFECT(addr, path);
+
+ return addr;
+}
+
+/*
+ * call-seq:
+ * Socket.unpack_sockaddr_un(sockaddr) => path
+ *
+ * Unpacks _sockaddr_ into path.
+ *
+ * _sockaddr_ should be a string or an addrinfo for AF_UNIX.
+ *
+ * sockaddr = Socket.sockaddr_un("/tmp/sock")
+ * p Socket.unpack_sockaddr_un(sockaddr) #=> "/tmp/sock"
+ *
+ */
+static VALUE
+sock_s_unpack_sockaddr_un(VALUE self, VALUE addr)
+{
+ struct sockaddr_un * sockaddr;
+ VALUE path;
+
+ sockaddr = (struct sockaddr_un*)SockAddrStringValuePtr(addr);
+ if (RSTRING_LEN(addr) <
+ (char*)&((struct sockaddr *)sockaddr)->sa_family +
+ sizeof(((struct sockaddr *)sockaddr)->sa_family) -
+ (char*)sockaddr)
+ rb_raise(rb_eArgError, "too short sockaddr");
+ if (((struct sockaddr *)sockaddr)->sa_family != AF_UNIX) {
+ rb_raise(rb_eArgError, "not an AF_UNIX sockaddr");
+ }
+ if (sizeof(struct sockaddr_un) < (size_t)RSTRING_LEN(addr)) {
+ rb_raise(rb_eTypeError, "too long sockaddr_un - %ld longer than %d",
+ RSTRING_LEN(addr), (int)sizeof(struct sockaddr_un));
+ }
+ path = rsock_unixpath_str(sockaddr, RSTRING_SOCKLEN(addr));
+ OBJ_INFECT(path, addr);
+ return path;
+}
+#endif
+
+#if defined(HAVE_GETIFADDRS) || defined(SIOCGLIFCONF) || defined(SIOCGIFCONF) || defined(_WIN32)
+
+static socklen_t
+sockaddr_len(struct sockaddr *addr)
+{
+ if (addr == NULL)
+ return 0;
+
+#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
+ if (addr->sa_len != 0)
+ return addr->sa_len;
+#endif
+
+ switch (addr->sa_family) {
+ case AF_INET:
+ return (socklen_t)sizeof(struct sockaddr_in);
+
+#ifdef AF_INET6
+ case AF_INET6:
+ return (socklen_t)sizeof(struct sockaddr_in6);
+#endif
+
+#ifdef HAVE_SYS_UN_H
+ case AF_UNIX:
+ return (socklen_t)sizeof(struct sockaddr_un);
+#endif
+
+#ifdef AF_PACKET
+ case AF_PACKET:
+ return (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + ((struct sockaddr_ll *)addr)->sll_halen);
+#endif
+
+ default:
+ return (socklen_t)(offsetof(struct sockaddr, sa_family) + sizeof(addr->sa_family));
+ }
+}
+
+socklen_t
+rsock_sockaddr_len(struct sockaddr *addr)
+{
+ return sockaddr_len(addr);
+}
+
+static VALUE
+sockaddr_obj(struct sockaddr *addr, socklen_t len)
+{
+#if defined(AF_INET6) && defined(__KAME__)
+ struct sockaddr_in6 addr6;
+#endif
+
+ if (addr == NULL)
+ return Qnil;
+
+ len = sockaddr_len(addr);
+
+#if defined(__KAME__) && defined(AF_INET6)
+ if (addr->sa_family == AF_INET6) {
+ /* KAME uses the 2nd 16bit word of link local IPv6 address as interface index internally */
+ /* http://orange.kame.net/dev/cvsweb.cgi/kame/IMPLEMENTATION */
+ /* convert fe80:1::1 to fe80::1%1 */
+ len = (socklen_t)sizeof(struct sockaddr_in6);
+ memcpy(&addr6, addr, len);
+ addr = (struct sockaddr *)&addr6;
+ if (IN6_IS_ADDR_LINKLOCAL(&addr6.sin6_addr) &&
+ addr6.sin6_scope_id == 0 &&
+ (addr6.sin6_addr.s6_addr[2] || addr6.sin6_addr.s6_addr[3])) {
+ addr6.sin6_scope_id = (addr6.sin6_addr.s6_addr[2] << 8) | addr6.sin6_addr.s6_addr[3];
+ addr6.sin6_addr.s6_addr[2] = 0;
+ addr6.sin6_addr.s6_addr[3] = 0;
+ }
+ }
+#endif
+
+ return rsock_addrinfo_new(addr, len, addr->sa_family, 0, 0, Qnil, Qnil);
+}
+
+VALUE
+rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len)
+{
+ return sockaddr_obj(addr, len);
+}
+
+#endif
+
+#if defined(HAVE_GETIFADDRS) || (defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && !defined(__hpux)) || defined(SIOCGIFCONF) || defined(_WIN32)
+/*
+ * call-seq:
+ * Socket.ip_address_list => array
+ *
+ * Returns local IP addresses as an array.
+ *
+ * The array contains Addrinfo objects.
+ *
+ * pp Socket.ip_address_list
+ * #=> [#<Addrinfo: 127.0.0.1>,
+ * #<Addrinfo: 192.168.0.128>,
+ * #<Addrinfo: ::1>,
+ * ...]
+ *
+ */
+static VALUE
+socket_s_ip_address_list(VALUE self)
+{
+#if defined(HAVE_GETIFADDRS)
+ struct ifaddrs *ifp = NULL;
+ struct ifaddrs *p;
+ int ret;
+ VALUE list;
+
+ ret = getifaddrs(&ifp);
+ if (ret == -1) {
+ rb_sys_fail("getifaddrs");
+ }
+
+ list = rb_ary_new();
+ for (p = ifp; p; p = p->ifa_next) {
+ if (p->ifa_addr != NULL && IS_IP_FAMILY(p->ifa_addr->sa_family)) {
+ struct sockaddr *addr = p->ifa_addr;
+#if defined(AF_INET6) && defined(__sun)
+ /*
+ * OpenIndiana SunOS 5.11 getifaddrs() returns IPv6 link local
+ * address with sin6_scope_id == 0.
+ * So fill it from the interface name (ifa_name).
+ */
+ struct sockaddr_in6 addr6;
+ if (addr->sa_family == AF_INET6) {
+ socklen_t len = (socklen_t)sizeof(struct sockaddr_in6);
+ memcpy(&addr6, addr, len);
+ addr = (struct sockaddr *)&addr6;
+ if (IN6_IS_ADDR_LINKLOCAL(&addr6.sin6_addr) &&
+ addr6.sin6_scope_id == 0) {
+ unsigned int ifindex = if_nametoindex(p->ifa_name);
+ if (ifindex != 0) {
+ addr6.sin6_scope_id = ifindex;
+ }
+ }
+ }
+#endif
+ rb_ary_push(list, sockaddr_obj(addr, sockaddr_len(addr)));
+ }
+ }
+
+ freeifaddrs(ifp);
+
+ return list;
+#elif defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && !defined(__hpux)
+ /* Solaris if_tcp(7P) */
+ /* HP-UX has SIOCGLIFCONF too. But it uses different struct */
+ int fd = -1;
+ int ret;
+ struct lifnum ln;
+ struct lifconf lc;
+ char *reason = NULL;
+ int save_errno;
+ int i;
+ VALUE list = Qnil;
+
+ lc.lifc_buf = NULL;
+
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (fd == -1)
+ rb_sys_fail("socket(2)");
+
+ memset(&ln, 0, sizeof(ln));
+ ln.lifn_family = AF_UNSPEC;
+
+ ret = ioctl(fd, SIOCGLIFNUM, &ln);
+ if (ret == -1) {
+ reason = "SIOCGLIFNUM";
+ goto finish;
+ }
+
+ memset(&lc, 0, sizeof(lc));
+ lc.lifc_family = AF_UNSPEC;
+ lc.lifc_flags = 0;
+ lc.lifc_len = sizeof(struct lifreq) * ln.lifn_count;
+ lc.lifc_req = xmalloc(lc.lifc_len);
+
+ ret = ioctl(fd, SIOCGLIFCONF, &lc);
+ if (ret == -1) {
+ reason = "SIOCGLIFCONF";
+ goto finish;
+ }
+
+ list = rb_ary_new();
+ for (i = 0; i < ln.lifn_count; i++) {
+ struct lifreq *req = &lc.lifc_req[i];
+ if (IS_IP_FAMILY(req->lifr_addr.ss_family)) {
+ if (req->lifr_addr.ss_family == AF_INET6 &&
+ IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)(&req->lifr_addr))->sin6_addr) &&
+ ((struct sockaddr_in6 *)(&req->lifr_addr))->sin6_scope_id == 0) {
+ struct lifreq req2;
+ memcpy(req2.lifr_name, req->lifr_name, LIFNAMSIZ);
+ ret = ioctl(fd, SIOCGLIFINDEX, &req2);
+ if (ret == -1) {
+ reason = "SIOCGLIFINDEX";
+ goto finish;
+ }
+ ((struct sockaddr_in6 *)(&req->lifr_addr))->sin6_scope_id = req2.lifr_index;
+ }
+ rb_ary_push(list, sockaddr_obj((struct sockaddr *)&req->lifr_addr, req->lifr_addrlen));
+ }
+ }
+
+ finish:
+ save_errno = errno;
+ if (lc.lifc_buf != NULL)
+ xfree(lc.lifc_req);
+ if (fd != -1)
+ close(fd);
+ errno = save_errno;
+
+ if (reason)
+ rb_sys_fail(reason);
+ return list;
+
+#elif defined(SIOCGIFCONF)
+ int fd = -1;
+ int ret;
+#define EXTRA_SPACE ((int)(sizeof(struct ifconf) + sizeof(union_sockaddr)))
+ char initbuf[4096+EXTRA_SPACE];
+ char *buf = initbuf;
+ int bufsize;
+ struct ifconf conf;
+ struct ifreq *req;
+ VALUE list = Qnil;
+ const char *reason = NULL;
+ int save_errno;
+
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (fd == -1)
+ rb_sys_fail("socket(2)");
+
+ bufsize = sizeof(initbuf);
+ buf = initbuf;
+
+ retry:
+ conf.ifc_len = bufsize;
+ conf.ifc_req = (struct ifreq *)buf;
+
+ /* fprintf(stderr, "bufsize: %d\n", bufsize); */
+
+ ret = ioctl(fd, SIOCGIFCONF, &conf);
+ if (ret == -1) {
+ reason = "SIOCGIFCONF";
+ goto finish;
+ }
+
+ /* fprintf(stderr, "conf.ifc_len: %d\n", conf.ifc_len); */
+
+ if (bufsize - EXTRA_SPACE < conf.ifc_len) {
+ if (bufsize < conf.ifc_len) {
+ /* NetBSD returns required size for all interfaces. */
+ bufsize = conf.ifc_len + EXTRA_SPACE;
+ }
+ else {
+ bufsize = bufsize << 1;
+ }
+ if (buf == initbuf)
+ buf = NULL;
+ buf = xrealloc(buf, bufsize);
+ goto retry;
+ }
+
+ close(fd);
+ fd = -1;
+
+ list = rb_ary_new();
+ req = conf.ifc_req;
+ while ((char*)req < (char*)conf.ifc_req + conf.ifc_len) {
+ struct sockaddr *addr = &req->ifr_addr;
+ if (IS_IP_FAMILY(addr->sa_family)) {
+ rb_ary_push(list, sockaddr_obj(addr, sockaddr_len(addr)));
+ }
+#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
+# ifndef _SIZEOF_ADDR_IFREQ
+# define _SIZEOF_ADDR_IFREQ(r) \
+ (sizeof(struct ifreq) + \
+ (sizeof(struct sockaddr) < (r).ifr_addr.sa_len ? \
+ (r).ifr_addr.sa_len - sizeof(struct sockaddr) : \
+ 0))
+# endif
+ req = (struct ifreq *)((char*)req + _SIZEOF_ADDR_IFREQ(*req));
+#else
+ req = (struct ifreq *)((char*)req + sizeof(struct ifreq));
+#endif
+ }
+
+ finish:
+
+ save_errno = errno;
+ if (buf != initbuf)
+ xfree(buf);
+ if (fd != -1)
+ close(fd);
+ errno = save_errno;
+
+ if (reason)
+ rb_sys_fail(reason);
+ return list;
+
+#undef EXTRA_SPACE
+#elif defined(_WIN32)
+ typedef struct ip_adapter_unicast_address_st {
+ unsigned LONG_LONG dummy0;
+ struct ip_adapter_unicast_address_st *Next;
+ struct {
+ struct sockaddr *lpSockaddr;
+ int iSockaddrLength;
+ } Address;
+ int dummy1;
+ int dummy2;
+ int dummy3;
+ long dummy4;
+ long dummy5;
+ long dummy6;
+ } ip_adapter_unicast_address_t;
+ typedef struct ip_adapter_anycast_address_st {
+ unsigned LONG_LONG dummy0;
+ struct ip_adapter_anycast_address_st *Next;
+ struct {
+ struct sockaddr *lpSockaddr;
+ int iSockaddrLength;
+ } Address;
+ } ip_adapter_anycast_address_t;
+ typedef struct ip_adapter_addresses_st {
+ unsigned LONG_LONG dummy0;
+ struct ip_adapter_addresses_st *Next;
+ void *dummy1;
+ ip_adapter_unicast_address_t *FirstUnicastAddress;
+ ip_adapter_anycast_address_t *FirstAnycastAddress;
+ void *dummy2;
+ void *dummy3;
+ void *dummy4;
+ void *dummy5;
+ void *dummy6;
+ BYTE dummy7[8];
+ DWORD dummy8;
+ DWORD dummy9;
+ DWORD dummy10;
+ DWORD IfType;
+ int OperStatus;
+ DWORD dummy12;
+ DWORD dummy13[16];
+ void *dummy14;
+ } ip_adapter_addresses_t;
+ typedef ULONG (WINAPI *GetAdaptersAddresses_t)(ULONG, ULONG, PVOID, ip_adapter_addresses_t *, PULONG);
+ HMODULE h;
+ GetAdaptersAddresses_t pGetAdaptersAddresses;
+ ULONG len;
+ DWORD ret;
+ ip_adapter_addresses_t *adapters;
+ VALUE list;
+
+ h = LoadLibrary("iphlpapi.dll");
+ if (!h)
+ rb_notimplement();
+ pGetAdaptersAddresses = (GetAdaptersAddresses_t)GetProcAddress(h, "GetAdaptersAddresses");
+ if (!pGetAdaptersAddresses) {
+ FreeLibrary(h);
+ rb_notimplement();
+ }
+
+ ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &len);
+ if (ret != ERROR_SUCCESS && ret != ERROR_BUFFER_OVERFLOW) {
+ errno = rb_w32_map_errno(ret);
+ FreeLibrary(h);
+ rb_sys_fail("GetAdaptersAddresses");
+ }
+ adapters = (ip_adapter_addresses_t *)ALLOCA_N(BYTE, len);
+ ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, adapters, &len);
+ if (ret != ERROR_SUCCESS) {
+ errno = rb_w32_map_errno(ret);
+ FreeLibrary(h);
+ rb_sys_fail("GetAdaptersAddresses");
+ }
+
+ list = rb_ary_new();
+ for (; adapters; adapters = adapters->Next) {
+ ip_adapter_unicast_address_t *uni;
+ ip_adapter_anycast_address_t *any;
+ if (adapters->OperStatus != 1) /* 1 means IfOperStatusUp */
+ continue;
+ for (uni = adapters->FirstUnicastAddress; uni; uni = uni->Next) {
+#ifndef INET6
+ if (uni->Address.lpSockaddr->sa_family == AF_INET)
+#else
+ if (IS_IP_FAMILY(uni->Address.lpSockaddr->sa_family))
+#endif
+ rb_ary_push(list, sockaddr_obj(uni->Address.lpSockaddr, uni->Address.iSockaddrLength));
+ }
+ for (any = adapters->FirstAnycastAddress; any; any = any->Next) {
+#ifndef INET6
+ if (any->Address.lpSockaddr->sa_family == AF_INET)
+#else
+ if (IS_IP_FAMILY(any->Address.lpSockaddr->sa_family))
+#endif
+ rb_ary_push(list, sockaddr_obj(any->Address.lpSockaddr, any->Address.iSockaddrLength));
+ }
+ }
+
+ FreeLibrary(h);
+ return list;
+#endif
+}
+#else
+#define socket_s_ip_address_list rb_f_notimplement
+#endif
+
+void
+Init_socket(void)
+{
+ rsock_init_basicsocket();
+
+ /*
+ * Document-class: Socket < BasicSocket
+ *
+ * Class +Socket+ provides access to the underlying operating system
+ * socket implementations. It can be used to provide more operating system
+ * specific functionality than the protocol-specific socket classes.
+ *
+ * The constants defined under Socket::Constants are also defined under
+ * Socket. For example, Socket::AF_INET is usable as well as
+ * Socket::Constants::AF_INET. See Socket::Constants for the list of
+ * constants.
+ *
+ * === What's a socket?
+ *
+ * Sockets are endpoints of a bidirectional communication channel.
+ * Sockets can communicate within a process, between processes on the same
+ * machine or between different machines. There are many types of socket:
+ * TCPSocket, UDPSocket or UNIXSocket for example.
+ *
+ * Sockets have their own vocabulary:
+ *
+ * *domain:*
+ * The family of protocols:
+ * * Socket::PF_INET
+ * * Socket::PF_INET6
+ * * Socket::PF_UNIX
+ * * etc.
+ *
+ * *type:*
+ * The type of communications between the two endpoints, typically
+ * * Socket::SOCK_STREAM
+ * * Socket::SOCK_DGRAM.
+ *
+ * *protocol:*
+ * Typically _zero_.
+ * This may be used to identify a variant of a protocol.
+ *
+ * *hostname:*
+ * The identifier of a network interface:
+ * * a string (hostname, IPv4 or IPv6 address or +broadcast+
+ * which specifies a broadcast address)
+ * * a zero-length string which specifies INADDR_ANY
+ * * an integer (interpreted as binary address in host byte order).
+ *
+ * === Quick start
+ *
+ * Many of the classes, such as TCPSocket, UDPSocket or UNIXSocket,
+ * ease the use of sockets comparatively to the equivalent C programming interface.
+ *
+ * Let's create an internet socket using the IPv4 protocol in a C-like manner:
+ *
+ * s = Socket.new Socket::AF_INET, Socket::SOCK_STREAM
+ * s.connect Socket.pack_sockaddr_in(80, 'example.com')
+ *
+ * You could also use the TCPSocket class:
+ *
+ * s = TCPSocket.new 'example.com', 80
+ *
+ * A simple server might look like this:
+ *
+ * require 'socket'
+ *
+ * server = TCPServer.new 2000 # Server bound to port 2000
+ *
+ * loop do
+ * client = server.accept # Wait for a client to connect
+ * client.puts "Hello !"
+ * client.puts "Time is #{Time.now}"
+ * client.close
+ * end
+ *
+ * A simple client may look like this:
+ *
+ * require 'socket'
+ *
+ * s = TCPSocket.new 'localhost', 2000
+ *
+ * while line = s.gets # Read lines from socket
+ * puts line # and print them
+ * end
+ *
+ * s.close # close socket when done
+ *
+ * === Exception Handling
+ *
+ * Ruby's Socket implementation raises exceptions based on the error
+ * generated by the system dependent implementation. This is why the
+ * methods are documented in a way that isolate Unix-based system
+ * exceptions from Windows based exceptions. If more information on a
+ * particular exception is needed, please refer to the Unix manual pages or
+ * the Windows WinSock reference.
+ *
+ * === Convenience methods
+ *
+ * Although the general way to create socket is Socket.new,
+ * there are several methods of socket creation for most cases.
+ *
+ * TCP client socket::
+ * Socket.tcp, TCPSocket.open
+ * TCP server socket::
+ * Socket.tcp_server_loop, TCPServer.open
+ * UNIX client socket::
+ * Socket.unix, UNIXSocket.open
+ * UNIX server socket::
+ * Socket.unix_server_loop, UNIXServer.open
+ *
+ * === Documentation by
+ *
+ * * Zach Dennis
+ * * Sam Roberts
+ * * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
+ *
+ * Much material in this documentation is taken with permission from
+ * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
+ */
+ rb_cSocket = rb_define_class("Socket", rb_cBasicSocket);
+
+ rsock_init_socket_init();
+
+ rb_define_method(rb_cSocket, "initialize", sock_initialize, -1);
+ rb_define_method(rb_cSocket, "connect", sock_connect, 1);
+ rb_define_method(rb_cSocket, "connect_nonblock", sock_connect_nonblock, 1);
+ rb_define_method(rb_cSocket, "bind", sock_bind, 1);
+ rb_define_method(rb_cSocket, "listen", rsock_sock_listen, 1);
+ rb_define_method(rb_cSocket, "accept", sock_accept, 0);
+ rb_define_method(rb_cSocket, "accept_nonblock", sock_accept_nonblock, 0);
+ rb_define_method(rb_cSocket, "sysaccept", sock_sysaccept, 0);
+
+ rb_define_method(rb_cSocket, "recvfrom", sock_recvfrom, -1);
+ rb_define_method(rb_cSocket, "recvfrom_nonblock", sock_recvfrom_nonblock, -1);
+
+ rb_define_singleton_method(rb_cSocket, "socketpair", rsock_sock_s_socketpair, -1);
+ rb_define_singleton_method(rb_cSocket, "pair", rsock_sock_s_socketpair, -1);
+ rb_define_singleton_method(rb_cSocket, "gethostname", sock_gethostname, 0);
+ rb_define_singleton_method(rb_cSocket, "gethostbyname", sock_s_gethostbyname, 1);
+ rb_define_singleton_method(rb_cSocket, "gethostbyaddr", sock_s_gethostbyaddr, -1);
+ rb_define_singleton_method(rb_cSocket, "getservbyname", sock_s_getservbyname, -1);
+ rb_define_singleton_method(rb_cSocket, "getservbyport", sock_s_getservbyport, -1);
+ rb_define_singleton_method(rb_cSocket, "getaddrinfo", sock_s_getaddrinfo, -1);
+ rb_define_singleton_method(rb_cSocket, "getnameinfo", sock_s_getnameinfo, -1);
+ rb_define_singleton_method(rb_cSocket, "sockaddr_in", sock_s_pack_sockaddr_in, 2);
+ rb_define_singleton_method(rb_cSocket, "pack_sockaddr_in", sock_s_pack_sockaddr_in, 2);
+ rb_define_singleton_method(rb_cSocket, "unpack_sockaddr_in", sock_s_unpack_sockaddr_in, 1);
+#ifdef HAVE_SYS_UN_H
+ rb_define_singleton_method(rb_cSocket, "sockaddr_un", sock_s_pack_sockaddr_un, 1);
+ rb_define_singleton_method(rb_cSocket, "pack_sockaddr_un", sock_s_pack_sockaddr_un, 1);
+ rb_define_singleton_method(rb_cSocket, "unpack_sockaddr_un", sock_s_unpack_sockaddr_un, 1);
+#endif
+
+ rb_define_singleton_method(rb_cSocket, "ip_address_list", socket_s_ip_address_list, 0);
+}
diff --git a/jni/ruby/ext/socket/sockport.h b/jni/ruby/ext/socket/sockport.h
new file mode 100644
index 0000000..7689e5e
--- /dev/null
+++ b/jni/ruby/ext/socket/sockport.h
@@ -0,0 +1,116 @@
+/************************************************
+
+ sockport.h -
+
+ $Author: akr $
+ created at: Fri Apr 30 23:19:34 JST 1999
+
+************************************************/
+
+#ifndef SOCKPORT_H
+#define SOCKPORT_H
+
+#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
+# define VALIDATE_SOCKLEN(addr, len) ((addr)->sa_len == (len))
+#else
+# define VALIDATE_SOCKLEN(addr, len) ((void)(addr), (void)(len), 1)
+#endif
+
+#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
+# define SET_SA_LEN(sa, len) (void)((sa)->sa_len = (len))
+#else
+# define SET_SA_LEN(sa, len) (void)(len)
+#endif
+
+/* for strict-aliasing rule */
+#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
+# define SET_SIN_LEN(sa, len) (void)((sa)->sin_len = (len))
+#else
+# define SET_SIN_LEN(sa, len) SET_SA_LEN((struct sockaddr *)(sa), (len))
+#endif
+
+#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
+# define SET_SIN6_LEN(sa, len) (void)((sa)->sin6_len = (len))
+#else
+# define SET_SIN6_LEN(sa, len) SET_SA_LEN((struct sockaddr *)(sa), (len))
+#endif
+
+#define INIT_SOCKADDR(addr, family, len) \
+ do { \
+ struct sockaddr *init_sockaddr_ptr = (addr); \
+ socklen_t init_sockaddr_len = (len); \
+ memset(init_sockaddr_ptr, 0, init_sockaddr_len); \
+ init_sockaddr_ptr->sa_family = (family); \
+ SET_SA_LEN(init_sockaddr_ptr, init_sockaddr_len); \
+ } while (0)
+
+#define INIT_SOCKADDR_IN(addr, len) \
+ do { \
+ struct sockaddr_in *init_sockaddr_ptr = (addr); \
+ socklen_t init_sockaddr_len = (len); \
+ memset(init_sockaddr_ptr, 0, init_sockaddr_len); \
+ init_sockaddr_ptr->sin_family = AF_INET; \
+ SET_SIN_LEN(init_sockaddr_ptr, init_sockaddr_len); \
+ } while (0)
+
+#define INIT_SOCKADDR_IN6(addr, len) \
+ do { \
+ struct sockaddr_in6 *init_sockaddr_ptr = (addr); \
+ socklen_t init_sockaddr_len = (len); \
+ memset(init_sockaddr_ptr, 0, init_sockaddr_len); \
+ init_sockaddr_ptr->sin6_family = AF_INET6; \
+ SET_SIN6_LEN(init_sockaddr_ptr, init_sockaddr_len); \
+ } while (0)
+
+
+/* for strict-aliasing rule */
+#ifdef HAVE_TYPE_STRUCT_SOCKADDR_UN
+# ifdef HAVE_STRUCT_SOCKADDR_IN_SUN_LEN
+# define SET_SUN_LEN(sa, len) (void)((sa)->sun_len = (len))
+# else
+# define SET_SUN_LEN(sa, len) SET_SA_LEN((struct sockaddr *)(sa), (len))
+# endif
+# define INIT_SOCKADDR_UN(addr, len) \
+ do { \
+ struct sockaddr_un *init_sockaddr_ptr = (addr); \
+ socklen_t init_sockaddr_len = (len); \
+ memset(init_sockaddr_ptr, 0, init_sockaddr_len); \
+ init_sockaddr_ptr->sun_family = AF_UNIX; \
+ SET_SUN_LEN(init_sockaddr_ptr, init_sockaddr_len); \
+ } while (0)
+#endif
+
+#ifndef IN_MULTICAST
+# define IN_CLASSD(i) (((long)(i) & 0xf0000000) == 0xe0000000)
+# define IN_MULTICAST(i) IN_CLASSD(i)
+#endif
+
+#ifndef IN_EXPERIMENTAL
+# define IN_EXPERIMENTAL(i) ((((long)(i)) & 0xe0000000) == 0xe0000000)
+#endif
+
+#ifndef IN_CLASSA_NSHIFT
+# define IN_CLASSA_NSHIFT 24
+#endif
+
+#ifndef IN_LOOPBACKNET
+# define IN_LOOPBACKNET 127
+#endif
+
+#ifndef AF_UNSPEC
+# define AF_UNSPEC 0
+#endif
+
+#ifndef PF_UNSPEC
+# define PF_UNSPEC AF_UNSPEC
+#endif
+
+#ifndef PF_INET
+# define PF_INET AF_INET
+#endif
+
+#if defined(HOST_NOT_FOUND) && !defined(h_errno) && !defined(__CYGWIN__)
+extern int h_errno;
+#endif
+
+#endif
diff --git a/jni/ruby/ext/socket/sockssocket.c b/jni/ruby/ext/socket/sockssocket.c
new file mode 100644
index 0000000..81f77a6
--- /dev/null
+++ b/jni/ruby/ext/socket/sockssocket.c
@@ -0,0 +1,68 @@
+/************************************************
+
+ sockssocket.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+#ifdef SOCKS
+/*
+ * call-seq:
+ * SOCKSSocket.new(host, serv) => socket
+ *
+ * Opens a SOCKS connection to +host+ via the SOCKS server +serv+.
+ *
+ */
+static VALUE
+socks_init(VALUE sock, VALUE host, VALUE serv)
+{
+ static int init = 0;
+
+ if (init == 0) {
+ SOCKSinit("ruby");
+ init = 1;
+ }
+
+ return rsock_init_inetsock(sock, host, serv, Qnil, Qnil, INET_SOCKS);
+}
+
+#ifdef SOCKS5
+/*
+ * Closes the SOCKS connection.
+ *
+ */
+static VALUE
+socks_s_close(VALUE sock)
+{
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ shutdown(fptr->fd, 2);
+ return rb_io_close(sock);
+}
+#endif
+#endif
+
+void
+rsock_init_sockssocket(void)
+{
+#ifdef SOCKS
+ /*
+ * Document-class: SOCKSSocket < TCPSocket
+ *
+ * SOCKS is an Internet protocol that routes packets between a client and
+ * a server through a proxy server. SOCKS5, if supported, additionally
+ * provides authentication so only authorized users may access a server.
+ */
+ rb_cSOCKSSocket = rb_define_class("SOCKSSocket", rb_cTCPSocket);
+ rb_define_method(rb_cSOCKSSocket, "initialize", socks_init, 2);
+#ifdef SOCKS5
+ rb_define_method(rb_cSOCKSSocket, "close", socks_s_close, 0);
+#endif
+#endif
+}
diff --git a/jni/ruby/ext/socket/tcpserver.c b/jni/ruby/ext/socket/tcpserver.c
new file mode 100644
index 0000000..2245a06
--- /dev/null
+++ b/jni/ruby/ext/socket/tcpserver.c
@@ -0,0 +1,178 @@
+/************************************************
+
+ tcpserver.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+/*
+ * call-seq:
+ * TCPServer.new([hostname,] port) => tcpserver
+ *
+ * Creates a new server socket bound to _port_.
+ *
+ * If _hostname_ is given, the socket is bound to it.
+ *
+ * serv = TCPServer.new("127.0.0.1", 28561)
+ * s = serv.accept
+ * s.puts Time.now
+ * s.close
+ *
+ * Internally, TCPServer.new calls getaddrinfo() function to
+ * obtain addresses.
+ * If getaddrinfo() returns multiple addresses,
+ * TCPServer.new tries to create a server socket for each address
+ * and returns first one that is successful.
+ *
+ */
+static VALUE
+tcp_svr_init(int argc, VALUE *argv, VALUE sock)
+{
+ VALUE hostname, port;
+
+ rb_scan_args(argc, argv, "011", &hostname, &port);
+ return rsock_init_inetsock(sock, hostname, port, Qnil, Qnil, INET_SERVER);
+}
+
+/*
+ * call-seq:
+ * tcpserver.accept => tcpsocket
+ *
+ * Accepts an incoming connection. It returns a new TCPSocket object.
+ *
+ * TCPServer.open("127.0.0.1", 14641) {|serv|
+ * s = serv.accept
+ * s.puts Time.now
+ * s.close
+ * }
+ *
+ */
+static VALUE
+tcp_accept(VALUE sock)
+{
+ rb_io_t *fptr;
+ union_sockaddr from;
+ socklen_t fromlen;
+
+ GetOpenFile(sock, fptr);
+ fromlen = (socklen_t)sizeof(from);
+ return rsock_s_accept(rb_cTCPSocket, fptr->fd, &from.addr, &fromlen);
+}
+
+/*
+ * call-seq:
+ * tcpserver.accept_nonblock => tcpsocket
+ *
+ * Accepts an incoming connection using accept(2) after
+ * O_NONBLOCK is set for the underlying file descriptor.
+ * It returns an accepted TCPSocket for the incoming connection.
+ *
+ * === Example
+ * require 'socket'
+ * serv = TCPServer.new(2202)
+ * begin # emulate blocking accept
+ * sock = serv.accept_nonblock
+ * rescue IO::WaitReadable, Errno::EINTR
+ * IO.select([serv])
+ * retry
+ * end
+ * # sock is an accepted socket.
+ *
+ * Refer to Socket#accept for the exceptions that may be thrown if the call
+ * to TCPServer#accept_nonblock fails.
+ *
+ * TCPServer#accept_nonblock may raise any error corresponding to accept(2) failure,
+ * including Errno::EWOULDBLOCK.
+ *
+ * If the exception is Errno::EWOULDBLOCK, Errno::AGAIN, Errno::ECONNABORTED, Errno::EPROTO,
+ * it is extended by IO::WaitReadable.
+ * So IO::WaitReadable can be used to rescue the exceptions for retrying accept_nonblock.
+ *
+ * === See
+ * * TCPServer#accept
+ * * Socket#accept
+ */
+static VALUE
+tcp_accept_nonblock(VALUE sock)
+{
+ rb_io_t *fptr;
+ union_sockaddr from;
+ socklen_t fromlen;
+
+ GetOpenFile(sock, fptr);
+ fromlen = (socklen_t)sizeof(from);
+ return rsock_s_accept_nonblock(rb_cTCPSocket, fptr, &from.addr, &fromlen);
+}
+
+/*
+ * call-seq:
+ * tcpserver.sysaccept => file_descriptor
+ *
+ * Returns a file descriptor of a accepted connection.
+ *
+ * TCPServer.open("127.0.0.1", 28561) {|serv|
+ * fd = serv.sysaccept
+ * s = IO.for_fd(fd)
+ * s.puts Time.now
+ * s.close
+ * }
+ *
+ */
+static VALUE
+tcp_sysaccept(VALUE sock)
+{
+ rb_io_t *fptr;
+ union_sockaddr from;
+ socklen_t fromlen;
+
+ GetOpenFile(sock, fptr);
+ fromlen = (socklen_t)sizeof(from);
+ return rsock_s_accept(0, fptr->fd, &from.addr, &fromlen);
+}
+
+void
+rsock_init_tcpserver(void)
+{
+ /*
+ * Document-class: TCPServer < TCPSocket
+ *
+ * TCPServer represents a TCP/IP server socket.
+ *
+ * A simple TCP server may look like:
+ *
+ * require 'socket'
+ *
+ * server = TCPServer.new 2000 # Server bind to port 2000
+ * loop do
+ * client = server.accept # Wait for a client to connect
+ * client.puts "Hello !"
+ * client.puts "Time is #{Time.now}"
+ * client.close
+ * end
+ *
+ * A more usable server (serving multiple clients):
+ *
+ * require 'socket'
+ *
+ * server = TCPServer.new 2000
+ * loop do
+ * Thread.start(server.accept) do |client|
+ * client.puts "Hello !"
+ * client.puts "Time is #{Time.now}"
+ * client.close
+ * end
+ * end
+ *
+ */
+ rb_cTCPServer = rb_define_class("TCPServer", rb_cTCPSocket);
+ rb_define_method(rb_cTCPServer, "accept", tcp_accept, 0);
+ rb_define_method(rb_cTCPServer, "accept_nonblock", tcp_accept_nonblock, 0);
+ rb_define_method(rb_cTCPServer, "sysaccept", tcp_sysaccept, 0);
+ rb_define_method(rb_cTCPServer, "initialize", tcp_svr_init, -1);
+ rb_define_method(rb_cTCPServer, "listen", rsock_sock_listen, 1); /* in socket.c */
+}
diff --git a/jni/ruby/ext/socket/tcpsocket.c b/jni/ruby/ext/socket/tcpsocket.c
new file mode 100644
index 0000000..6217e42
--- /dev/null
+++ b/jni/ruby/ext/socket/tcpsocket.c
@@ -0,0 +1,82 @@
+/************************************************
+
+ tcpsocket.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+/*
+ * call-seq:
+ * TCPSocket.new(remote_host, remote_port, local_host=nil, local_port=nil)
+ *
+ * Opens a TCP connection to +remote_host+ on +remote_port+. If +local_host+
+ * and +local_port+ are specified, then those parameters are used on the local
+ * end to establish the connection.
+ */
+static VALUE
+tcp_init(int argc, VALUE *argv, VALUE sock)
+{
+ VALUE remote_host, remote_serv;
+ VALUE local_host, local_serv;
+
+ rb_scan_args(argc, argv, "22", &remote_host, &remote_serv,
+ &local_host, &local_serv);
+
+ return rsock_init_inetsock(sock, remote_host, remote_serv,
+ local_host, local_serv, INET_CLIENT);
+}
+
+static VALUE
+tcp_sockaddr(struct sockaddr *addr, socklen_t len)
+{
+ return rsock_make_ipaddr(addr, len);
+}
+
+/*
+ * call-seq:
+ * TCPSocket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list]
+ *
+ * Lookups host information by _hostname_.
+ *
+ * TCPSocket.gethostbyname("localhost")
+ * #=> ["localhost", ["hal"], 2, "127.0.0.1"]
+ *
+ */
+static VALUE
+tcp_s_gethostbyname(VALUE obj, VALUE host)
+{
+ rb_secure(3);
+ return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME),
+ tcp_sockaddr);
+}
+
+void
+rsock_init_tcpsocket(void)
+{
+ /*
+ * Document-class: TCPSocket < IPSocket
+ *
+ * TCPSocket represents a TCP/IP client socket.
+ *
+ * A simple client may look like:
+ *
+ * require 'socket'
+ *
+ * s = TCPSocket.new 'localhost', 2000
+ *
+ * while line = s.gets # Read lines from socket
+ * puts line # and print them
+ * end
+ *
+ * s.close # close socket when done
+ *
+ */
+ rb_cTCPSocket = rb_define_class("TCPSocket", rb_cIPSocket);
+ rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1);
+ rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1);
+}
diff --git a/jni/ruby/ext/socket/udpsocket.c b/jni/ruby/ext/socket/udpsocket.c
new file mode 100644
index 0000000..eb605ca
--- /dev/null
+++ b/jni/ruby/ext/socket/udpsocket.c
@@ -0,0 +1,266 @@
+/************************************************
+
+ udpsocket.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+/*
+ * call-seq:
+ * UDPSocket.new([address_family]) => socket
+ *
+ * Creates a new UDPSocket object.
+ *
+ * _address_family_ should be an integer, a string or a symbol:
+ * Socket::AF_INET, "AF_INET", :INET, etc.
+ *
+ * UDPSocket.new #=> #<UDPSocket:fd 3>
+ * UDPSocket.new(Socket::AF_INET6) #=> #<UDPSocket:fd 4>
+ *
+ */
+static VALUE
+udp_init(int argc, VALUE *argv, VALUE sock)
+{
+ VALUE arg;
+ int family = AF_INET;
+ int fd;
+
+ rb_secure(3);
+ if (rb_scan_args(argc, argv, "01", &arg) == 1) {
+ family = rsock_family_arg(arg);
+ }
+ fd = rsock_socket(family, SOCK_DGRAM, 0);
+ if (fd < 0) {
+ rb_sys_fail("socket(2) - udp");
+ }
+
+ return rsock_init_sock(sock, fd);
+}
+
+struct udp_arg
+{
+ struct rb_addrinfo *res;
+ int fd;
+};
+
+static VALUE
+udp_connect_internal(struct udp_arg *arg)
+{
+ int fd = arg->fd;
+ struct addrinfo *res;
+
+ for (res = arg->res->ai; res; res = res->ai_next) {
+ if (rsock_connect(fd, res->ai_addr, res->ai_addrlen, 0) >= 0) {
+ return Qtrue;
+ }
+ }
+ return Qfalse;
+}
+
+/*
+ * call-seq:
+ * udpsocket.connect(host, port) => 0
+ *
+ * Connects _udpsocket_ to _host_:_port_.
+ *
+ * This makes possible to send without destination address.
+ *
+ * u1 = UDPSocket.new
+ * u1.bind("127.0.0.1", 4913)
+ * u2 = UDPSocket.new
+ * u2.connect("127.0.0.1", 4913)
+ * u2.send "uuuu", 0
+ * p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
+ *
+ */
+static VALUE
+udp_connect(VALUE sock, VALUE host, VALUE port)
+{
+ rb_io_t *fptr;
+ struct udp_arg arg;
+ VALUE ret;
+
+ rb_secure(3);
+ arg.res = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
+ GetOpenFile(sock, fptr);
+ arg.fd = fptr->fd;
+ ret = rb_ensure(udp_connect_internal, (VALUE)&arg,
+ rsock_freeaddrinfo, (VALUE)arg.res);
+ if (!ret) rsock_sys_fail_host_port("connect(2)", host, port);
+ return INT2FIX(0);
+}
+
+/*
+ * call-seq:
+ * udpsocket.bind(host, port) #=> 0
+ *
+ * Binds _udpsocket_ to _host_:_port_.
+ *
+ * u1 = UDPSocket.new
+ * u1.bind("127.0.0.1", 4913)
+ * u1.send "message-to-self", 0, "127.0.0.1", 4913
+ * p u1.recvfrom(10) #=> ["message-to", ["AF_INET", 4913, "localhost", "127.0.0.1"]]
+ *
+ */
+static VALUE
+udp_bind(VALUE sock, VALUE host, VALUE port)
+{
+ rb_io_t *fptr;
+ struct rb_addrinfo *res0;
+ struct addrinfo *res;
+
+ rb_secure(3);
+ res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
+ GetOpenFile(sock, fptr);
+ for (res = res0->ai; res; res = res->ai_next) {
+ if (bind(fptr->fd, res->ai_addr, res->ai_addrlen) < 0) {
+ continue;
+ }
+ rb_freeaddrinfo(res0);
+ return INT2FIX(0);
+ }
+ rb_freeaddrinfo(res0);
+
+ rsock_sys_fail_host_port("bind(2)", host, port);
+
+ return INT2FIX(0);
+}
+
+/*
+ * call-seq:
+ * udpsocket.send(mesg, flags, host, port) => numbytes_sent
+ * udpsocket.send(mesg, flags, sockaddr_to) => numbytes_sent
+ * udpsocket.send(mesg, flags) => numbytes_sent
+ *
+ * Sends _mesg_ via _udpsocket_.
+ *
+ * _flags_ should be a bitwise OR of Socket::MSG_* constants.
+ *
+ * u1 = UDPSocket.new
+ * u1.bind("127.0.0.1", 4913)
+ *
+ * u2 = UDPSocket.new
+ * u2.send "hi", 0, "127.0.0.1", 4913
+ *
+ * mesg, addr = u1.recvfrom(10)
+ * u1.send mesg, 0, addr[3], addr[1]
+ *
+ * p u2.recv(100) #=> "hi"
+ *
+ */
+static VALUE
+udp_send(int argc, VALUE *argv, VALUE sock)
+{
+ VALUE flags, host, port;
+ rb_io_t *fptr;
+ int n;
+ struct rb_addrinfo *res0;
+ struct addrinfo *res;
+ struct rsock_send_arg arg;
+
+ if (argc == 2 || argc == 3) {
+ return rsock_bsock_send(argc, argv, sock);
+ }
+ rb_scan_args(argc, argv, "4", &arg.mesg, &flags, &host, &port);
+
+ StringValue(arg.mesg);
+ res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
+ GetOpenFile(sock, fptr);
+ arg.fd = fptr->fd;
+ arg.flags = NUM2INT(flags);
+ for (res = res0->ai; res; res = res->ai_next) {
+ retry:
+ arg.to = res->ai_addr;
+ arg.tolen = res->ai_addrlen;
+ rsock_maybe_fd_writable(arg.fd);
+ n = (int)BLOCKING_REGION_FD(rsock_sendto_blocking, &arg);
+ if (n >= 0) {
+ rb_freeaddrinfo(res0);
+ return INT2FIX(n);
+ }
+ if (rb_io_wait_writable(fptr->fd)) {
+ goto retry;
+ }
+ }
+ rb_freeaddrinfo(res0);
+ rsock_sys_fail_host_port("sendto(2)", host, port);
+ return INT2FIX(n);
+}
+
+/*
+ * call-seq:
+ * udpsocket.recvfrom_nonblock(maxlen) => [mesg, sender_inet_addr]
+ * udpsocket.recvfrom_nonblock(maxlen, flags) => [mesg, sender_inet_addr]
+ *
+ * Receives up to _maxlen_ bytes from +udpsocket+ using recvfrom(2) after
+ * O_NONBLOCK is set for the underlying file descriptor.
+ * If _maxlen_ is omitted, its default value is 65536.
+ * _flags_ is zero or more of the +MSG_+ options.
+ * The first element of the results, _mesg_, is the data received.
+ * The second element, _sender_inet_addr_, is an array to represent the sender address.
+ *
+ * When recvfrom(2) returns 0,
+ * Socket#recvfrom_nonblock returns an empty string as data.
+ * It means an empty packet.
+ *
+ * === Parameters
+ * * +maxlen+ - the number of bytes to receive from the socket
+ * * +flags+ - zero or more of the +MSG_+ options
+ *
+ * === Example
+ * require 'socket'
+ * s1 = UDPSocket.new
+ * s1.bind("127.0.0.1", 0)
+ * s2 = UDPSocket.new
+ * s2.bind("127.0.0.1", 0)
+ * s2.connect(*s1.addr.values_at(3,1))
+ * s1.connect(*s2.addr.values_at(3,1))
+ * s1.send "aaa", 0
+ * begin # emulate blocking recvfrom
+ * p s2.recvfrom_nonblock(10) #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]]
+ * rescue IO::WaitReadable
+ * IO.select([s2])
+ * retry
+ * end
+ *
+ * Refer to Socket#recvfrom for the exceptions that may be thrown if the call
+ * to _recvfrom_nonblock_ fails.
+ *
+ * UDPSocket#recvfrom_nonblock may raise any error corresponding to recvfrom(2) failure,
+ * including Errno::EWOULDBLOCK.
+ *
+ * If the exception is Errno::EWOULDBLOCK or Errno::AGAIN,
+ * it is extended by IO::WaitReadable.
+ * So IO::WaitReadable can be used to rescue the exceptions for retrying recvfrom_nonblock.
+ *
+ * === See
+ * * Socket#recvfrom
+ */
+static VALUE
+udp_recvfrom_nonblock(int argc, VALUE *argv, VALUE sock)
+{
+ return rsock_s_recvfrom_nonblock(sock, argc, argv, RECV_IP);
+}
+
+void
+rsock_init_udpsocket(void)
+{
+ /*
+ * Document-class: UDPSocket < IPSocket
+ *
+ * UDPSocket represents a UDP/IP socket.
+ *
+ */
+ rb_cUDPSocket = rb_define_class("UDPSocket", rb_cIPSocket);
+ rb_define_method(rb_cUDPSocket, "initialize", udp_init, -1);
+ rb_define_method(rb_cUDPSocket, "connect", udp_connect, 2);
+ rb_define_method(rb_cUDPSocket, "bind", udp_bind, 2);
+ rb_define_method(rb_cUDPSocket, "send", udp_send, -1);
+ rb_define_method(rb_cUDPSocket, "recvfrom_nonblock", udp_recvfrom_nonblock, -1);
+}
+
diff --git a/jni/ruby/ext/socket/unixserver.c b/jni/ruby/ext/socket/unixserver.c
new file mode 100644
index 0000000..df98497
--- /dev/null
+++ b/jni/ruby/ext/socket/unixserver.c
@@ -0,0 +1,155 @@
+/************************************************
+
+ unixserver.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+#ifdef HAVE_SYS_UN_H
+/*
+ * call-seq:
+ * UNIXServer.new(path) => unixserver
+ *
+ * Creates a new UNIX server socket bound to _path_.
+ *
+ * serv = UNIXServer.new("/tmp/sock")
+ * s = serv.accept
+ * p s.read
+ */
+static VALUE
+unix_svr_init(VALUE sock, VALUE path)
+{
+ return rsock_init_unixsock(sock, path, 1);
+}
+
+/*
+ * call-seq:
+ * unixserver.accept => unixsocket
+ *
+ * Accepts an incoming connection.
+ * It returns a new UNIXSocket object.
+ *
+ * UNIXServer.open("/tmp/sock") {|serv|
+ * UNIXSocket.open("/tmp/sock") {|c|
+ * s = serv.accept
+ * s.puts "hi"
+ * s.close
+ * p c.read #=> "hi\n"
+ * }
+ * }
+ *
+ */
+static VALUE
+unix_accept(VALUE sock)
+{
+ rb_io_t *fptr;
+ struct sockaddr_un from;
+ socklen_t fromlen;
+
+ GetOpenFile(sock, fptr);
+ fromlen = (socklen_t)sizeof(struct sockaddr_un);
+ return rsock_s_accept(rb_cUNIXSocket, fptr->fd,
+ (struct sockaddr*)&from, &fromlen);
+}
+
+/*
+ * call-seq:
+ * unixserver.accept_nonblock => unixsocket
+ *
+ * Accepts an incoming connection using accept(2) after
+ * O_NONBLOCK is set for the underlying file descriptor.
+ * It returns an accepted UNIXSocket for the incoming connection.
+ *
+ * === Example
+ * require 'socket'
+ * serv = UNIXServer.new("/tmp/sock")
+ * begin # emulate blocking accept
+ * sock = serv.accept_nonblock
+ * rescue IO::WaitReadable, Errno::EINTR
+ * IO.select([serv])
+ * retry
+ * end
+ * # sock is an accepted socket.
+ *
+ * Refer to Socket#accept for the exceptions that may be thrown if the call
+ * to UNIXServer#accept_nonblock fails.
+ *
+ * UNIXServer#accept_nonblock may raise any error corresponding to accept(2) failure,
+ * including Errno::EWOULDBLOCK.
+ *
+ * If the exception is Errno::EWOULDBLOCK, Errno::AGAIN, Errno::ECONNABORTED or Errno::EPROTO,
+ * it is extended by IO::WaitReadable.
+ * So IO::WaitReadable can be used to rescue the exceptions for retrying accept_nonblock.
+ *
+ * === See
+ * * UNIXServer#accept
+ * * Socket#accept
+ */
+static VALUE
+unix_accept_nonblock(VALUE sock)
+{
+ rb_io_t *fptr;
+ struct sockaddr_un from;
+ socklen_t fromlen;
+
+ GetOpenFile(sock, fptr);
+ fromlen = (socklen_t)sizeof(from);
+ return rsock_s_accept_nonblock(rb_cUNIXSocket, fptr,
+ (struct sockaddr *)&from, &fromlen);
+}
+
+/*
+ * call-seq:
+ * unixserver.sysaccept => file_descriptor
+ *
+ * Accepts a new connection.
+ * It returns the new file descriptor which is an integer.
+ *
+ * UNIXServer.open("/tmp/sock") {|serv|
+ * UNIXSocket.open("/tmp/sock") {|c|
+ * fd = serv.sysaccept
+ * s = IO.new(fd)
+ * s.puts "hi"
+ * s.close
+ * p c.read #=> "hi\n"
+ * }
+ * }
+ *
+ */
+static VALUE
+unix_sysaccept(VALUE sock)
+{
+ rb_io_t *fptr;
+ struct sockaddr_un from;
+ socklen_t fromlen;
+
+ GetOpenFile(sock, fptr);
+ fromlen = (socklen_t)sizeof(struct sockaddr_un);
+ return rsock_s_accept(0, fptr->fd, (struct sockaddr*)&from, &fromlen);
+}
+
+#endif
+
+void
+rsock_init_unixserver(void)
+{
+#ifdef HAVE_SYS_UN_H
+ /*
+ * Document-class: UNIXServer < UNIXSocket
+ *
+ * UNIXServer represents a UNIX domain stream server socket.
+ *
+ */
+ rb_cUNIXServer = rb_define_class("UNIXServer", rb_cUNIXSocket);
+ rb_define_method(rb_cUNIXServer, "initialize", unix_svr_init, 1);
+ rb_define_method(rb_cUNIXServer, "accept", unix_accept, 0);
+ rb_define_method(rb_cUNIXServer, "accept_nonblock", unix_accept_nonblock, 0);
+ rb_define_method(rb_cUNIXServer, "sysaccept", unix_sysaccept, 0);
+ rb_define_method(rb_cUNIXServer, "listen", rsock_sock_listen, 1); /* in socket.c */
+#endif
+}
diff --git a/jni/ruby/ext/socket/unixsocket.c b/jni/ruby/ext/socket/unixsocket.c
new file mode 100644
index 0000000..9a4c2cf
--- /dev/null
+++ b/jni/ruby/ext/socket/unixsocket.c
@@ -0,0 +1,534 @@
+/************************************************
+
+ unixsocket.c -
+
+ created at: Thu Mar 31 12:21:29 JST 1994
+
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
+
+************************************************/
+
+#include "rubysocket.h"
+
+#ifdef HAVE_SYS_UN_H
+struct unixsock_arg {
+ struct sockaddr_un *sockaddr;
+ socklen_t sockaddrlen;
+ int fd;
+};
+
+static VALUE
+unixsock_connect_internal(VALUE a)
+{
+ struct unixsock_arg *arg = (struct unixsock_arg *)a;
+ return (VALUE)rsock_connect(arg->fd, (struct sockaddr*)arg->sockaddr,
+ arg->sockaddrlen, 0);
+}
+
+VALUE
+rsock_init_unixsock(VALUE sock, VALUE path, int server)
+{
+ struct sockaddr_un sockaddr;
+ socklen_t sockaddrlen;
+ int fd, status;
+ rb_io_t *fptr;
+
+ SafeStringValue(path);
+
+ INIT_SOCKADDR_UN(&sockaddr, sizeof(struct sockaddr_un));
+ if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) {
+ rb_raise(rb_eArgError, "too long unix socket path (%ldbytes given but %dbytes max)",
+ RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path));
+ }
+ memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
+ sockaddrlen = rsock_unix_sockaddr_len(path);
+
+ fd = rsock_socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd < 0) {
+ rsock_sys_fail_path("socket(2)", path);
+ }
+
+ if (server) {
+ status = bind(fd, (struct sockaddr*)&sockaddr, sockaddrlen);
+ }
+ else {
+ int prot;
+ struct unixsock_arg arg;
+ arg.sockaddr = &sockaddr;
+ arg.sockaddrlen = sockaddrlen;
+ arg.fd = fd;
+ status = (int)rb_protect(unixsock_connect_internal, (VALUE)&arg, &prot);
+ if (prot) {
+ close(fd);
+ rb_jump_tag(prot);
+ }
+ }
+
+ if (status < 0) {
+ close(fd);
+ rsock_sys_fail_path("connect(2)", path);
+ }
+
+ if (server) {
+ if (listen(fd, SOMAXCONN) < 0) {
+ close(fd);
+ rsock_sys_fail_path("listen(2)", path);
+ }
+ }
+
+ rsock_init_sock(sock, fd);
+ if (server) {
+ GetOpenFile(sock, fptr);
+ fptr->pathv = rb_str_new_frozen(path);
+ }
+
+ return sock;
+}
+
+/*
+ * call-seq:
+ * UNIXSocket.new(path) => unixsocket
+ *
+ * Creates a new UNIX client socket connected to _path_.
+ *
+ * s = UNIXSocket.new("/tmp/sock")
+ * s.send "hello", 0
+ *
+ */
+static VALUE
+unix_init(VALUE sock, VALUE path)
+{
+ return rsock_init_unixsock(sock, path, 0);
+}
+
+/*
+ * call-seq:
+ * unixsocket.path => path
+ *
+ * Returns the path of the local address of unixsocket.
+ *
+ * s = UNIXServer.new("/tmp/sock")
+ * p s.path #=> "/tmp/sock"
+ *
+ */
+static VALUE
+unix_path(VALUE sock)
+{
+ rb_io_t *fptr;
+
+ GetOpenFile(sock, fptr);
+ if (NIL_P(fptr->pathv)) {
+ struct sockaddr_un addr;
+ socklen_t len = (socklen_t)sizeof(addr);
+ socklen_t len0 = len;
+ if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
+ rsock_sys_fail_path("getsockname(2)", fptr->pathv);
+ if (len0 < len) len = len0;
+ fptr->pathv = rb_obj_freeze(rsock_unixpath_str(&addr, len));
+ }
+ return rb_str_dup(fptr->pathv);
+}
+
+/*
+ * call-seq:
+ * unixsocket.recvfrom(maxlen [, flags]) => [mesg, unixaddress]
+ *
+ * Receives a message via _unixsocket_.
+ *
+ * _maxlen_ is the maximum number of bytes to receive.
+ *
+ * _flags_ should be a bitwise OR of Socket::MSG_* constants.
+ *
+ * s1 = Socket.new(:UNIX, :DGRAM, 0)
+ * s1_ai = Addrinfo.unix("/tmp/sock1")
+ * s1.bind(s1_ai)
+ *
+ * s2 = Socket.new(:UNIX, :DGRAM, 0)
+ * s2_ai = Addrinfo.unix("/tmp/sock2")
+ * s2.bind(s2_ai)
+ * s3 = UNIXSocket.for_fd(s2.fileno)
+ *
+ * s1.send "a", 0, s2_ai
+ * p s3.recvfrom(10) #=> ["a", ["AF_UNIX", "/tmp/sock1"]]
+ *
+ */
+static VALUE
+unix_recvfrom(int argc, VALUE *argv, VALUE sock)
+{
+ return rsock_s_recvfrom(sock, argc, argv, RECV_UNIX);
+}
+
+#if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && defined(SCM_RIGHTS)
+#define FD_PASSING_BY_MSG_CONTROL 1
+#else
+#define FD_PASSING_BY_MSG_CONTROL 0
+#endif
+
+#if defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
+#define FD_PASSING_BY_MSG_ACCRIGHTS 1
+#else
+#define FD_PASSING_BY_MSG_ACCRIGHTS 0
+#endif
+
+struct iomsg_arg {
+ int fd;
+ struct msghdr msg;
+};
+
+#if defined(HAVE_SENDMSG) && (FD_PASSING_BY_MSG_CONTROL || FD_PASSING_BY_MSG_ACCRIGHTS)
+static VALUE
+sendmsg_blocking(void *data)
+{
+ struct iomsg_arg *arg = data;
+ return sendmsg(arg->fd, &arg->msg, 0);
+}
+
+/*
+ * call-seq:
+ * unixsocket.send_io(io) => nil
+ *
+ * Sends _io_ as file descriptor passing.
+ *
+ * s1, s2 = UNIXSocket.pair
+ *
+ * s1.send_io STDOUT
+ * stdout = s2.recv_io
+ *
+ * p STDOUT.fileno #=> 1
+ * p stdout.fileno #=> 6
+ *
+ * stdout.puts "hello" # outputs "hello\n" to standard output.
+ */
+static VALUE
+unix_send_io(VALUE sock, VALUE val)
+{
+ int fd;
+ rb_io_t *fptr;
+ struct iomsg_arg arg;
+ struct iovec vec[1];
+ char buf[1];
+
+#if FD_PASSING_BY_MSG_CONTROL
+ union {
+ struct cmsghdr hdr;
+ char pad[sizeof(struct cmsghdr)+8+sizeof(int)+8];
+ } cmsg;
+#endif
+
+ if (rb_obj_is_kind_of(val, rb_cIO)) {
+ rb_io_t *valfptr;
+ GetOpenFile(val, valfptr);
+ fd = valfptr->fd;
+ }
+ else if (FIXNUM_P(val)) {
+ fd = FIX2INT(val);
+ }
+ else {
+ rb_raise(rb_eTypeError, "neither IO nor file descriptor");
+ }
+
+ GetOpenFile(sock, fptr);
+
+ arg.msg.msg_name = NULL;
+ arg.msg.msg_namelen = 0;
+
+ /* Linux and Solaris doesn't work if msg_iov is NULL. */
+ buf[0] = '\0';
+ vec[0].iov_base = buf;
+ vec[0].iov_len = 1;
+ arg.msg.msg_iov = vec;
+ arg.msg.msg_iovlen = 1;
+
+#if FD_PASSING_BY_MSG_CONTROL
+ arg.msg.msg_control = (caddr_t)&cmsg;
+ arg.msg.msg_controllen = (socklen_t)CMSG_LEN(sizeof(int));
+ arg.msg.msg_flags = 0;
+ MEMZERO((char*)&cmsg, char, sizeof(cmsg));
+ cmsg.hdr.cmsg_len = (socklen_t)CMSG_LEN(sizeof(int));
+ cmsg.hdr.cmsg_level = SOL_SOCKET;
+ cmsg.hdr.cmsg_type = SCM_RIGHTS;
+ memcpy(CMSG_DATA(&cmsg.hdr), &fd, sizeof(int));
+#else
+ arg.msg.msg_accrights = (caddr_t)&fd;
+ arg.msg.msg_accrightslen = sizeof(fd);
+#endif
+
+ arg.fd = fptr->fd;
+ while ((int)BLOCKING_REGION_FD(sendmsg_blocking, &arg) == -1) {
+ if (!rb_io_wait_writable(arg.fd))
+ rsock_sys_fail_path("sendmsg(2)", fptr->pathv);
+ }
+
+ return Qnil;
+}
+#else
+#define unix_send_io rb_f_notimplement
+#endif
+
+#if defined(HAVE_RECVMSG) && (FD_PASSING_BY_MSG_CONTROL || FD_PASSING_BY_MSG_ACCRIGHTS)
+static VALUE
+recvmsg_blocking(void *data)
+{
+ struct iomsg_arg *arg = data;
+ int flags = 0;
+ return rsock_recvmsg(arg->fd, &arg->msg, flags);
+}
+
+/*
+ * call-seq:
+ * unixsocket.recv_io([klass [, mode]]) => io
+ *
+ * Example
+ *
+ * UNIXServer.open("/tmp/sock") {|serv|
+ * UNIXSocket.open("/tmp/sock") {|c|
+ * s = serv.accept
+ *
+ * c.send_io STDOUT
+ * stdout = s.recv_io
+ *
+ * p STDOUT.fileno #=> 1
+ * p stdout.fileno #=> 7
+ *
+ * stdout.puts "hello" # outputs "hello\n" to standard output.
+ * }
+ * }
+ *
+ */
+static VALUE
+unix_recv_io(int argc, VALUE *argv, VALUE sock)
+{
+ VALUE klass, mode;
+ rb_io_t *fptr;
+ struct iomsg_arg arg;
+ struct iovec vec[2];
+ char buf[1];
+
+ int fd;
+#if FD_PASSING_BY_MSG_CONTROL
+ union {
+ struct cmsghdr hdr;
+ char pad[sizeof(struct cmsghdr)+8+sizeof(int)+8];
+ } cmsg;
+#endif
+
+ rb_scan_args(argc, argv, "02", &klass, &mode);
+ if (argc == 0)
+ klass = rb_cIO;
+ if (argc <= 1)
+ mode = Qnil;
+
+ GetOpenFile(sock, fptr);
+
+ arg.msg.msg_name = NULL;
+ arg.msg.msg_namelen = 0;
+
+ vec[0].iov_base = buf;
+ vec[0].iov_len = sizeof(buf);
+ arg.msg.msg_iov = vec;
+ arg.msg.msg_iovlen = 1;
+
+#if FD_PASSING_BY_MSG_CONTROL
+ arg.msg.msg_control = (caddr_t)&cmsg;
+ arg.msg.msg_controllen = (socklen_t)CMSG_SPACE(sizeof(int));
+ arg.msg.msg_flags = 0;
+ cmsg.hdr.cmsg_len = (socklen_t)CMSG_LEN(sizeof(int));
+ cmsg.hdr.cmsg_level = SOL_SOCKET;
+ cmsg.hdr.cmsg_type = SCM_RIGHTS;
+ fd = -1;
+ memcpy(CMSG_DATA(&cmsg.hdr), &fd, sizeof(int));
+#else
+ arg.msg.msg_accrights = (caddr_t)&fd;
+ arg.msg.msg_accrightslen = sizeof(fd);
+ fd = -1;
+#endif
+
+ arg.fd = fptr->fd;
+ while ((int)BLOCKING_REGION_FD(recvmsg_blocking, &arg) == -1) {
+ if (!rb_io_wait_readable(arg.fd))
+ rsock_sys_fail_path("recvmsg(2)", fptr->pathv);
+ }
+
+#if FD_PASSING_BY_MSG_CONTROL
+ if (arg.msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr)) {
+ rb_raise(rb_eSocket,
+ "file descriptor was not passed (msg_controllen=%d smaller than sizeof(struct cmsghdr)=%d)",
+ (int)arg.msg.msg_controllen, (int)sizeof(struct cmsghdr));
+ }
+ if (cmsg.hdr.cmsg_level != SOL_SOCKET) {
+ rb_raise(rb_eSocket,
+ "file descriptor was not passed (cmsg_level=%d, %d expected)",
+ cmsg.hdr.cmsg_level, SOL_SOCKET);
+ }
+ if (cmsg.hdr.cmsg_type != SCM_RIGHTS) {
+ rb_raise(rb_eSocket,
+ "file descriptor was not passed (cmsg_type=%d, %d expected)",
+ cmsg.hdr.cmsg_type, SCM_RIGHTS);
+ }
+ if (arg.msg.msg_controllen < (socklen_t)CMSG_LEN(sizeof(int))) {
+ rb_raise(rb_eSocket,
+ "file descriptor was not passed (msg_controllen=%d smaller than CMSG_LEN(sizeof(int))=%d)",
+ (int)arg.msg.msg_controllen, (int)CMSG_LEN(sizeof(int)));
+ }
+ if ((socklen_t)CMSG_SPACE(sizeof(int)) < arg.msg.msg_controllen) {
+ rb_raise(rb_eSocket,
+ "file descriptor was not passed (msg_controllen=%d bigger than CMSG_SPACE(sizeof(int))=%d)",
+ (int)arg.msg.msg_controllen, (int)CMSG_SPACE(sizeof(int)));
+ }
+ if (cmsg.hdr.cmsg_len != CMSG_LEN(sizeof(int))) {
+ rsock_discard_cmsg_resource(&arg.msg, 0);
+ rb_raise(rb_eSocket,
+ "file descriptor was not passed (cmsg_len=%d, %d expected)",
+ (int)cmsg.hdr.cmsg_len, (int)CMSG_LEN(sizeof(int)));
+ }
+#else
+ if (arg.msg.msg_accrightslen != sizeof(fd)) {
+ rb_raise(rb_eSocket,
+ "file descriptor was not passed (accrightslen=%d, %d expected)",
+ arg.msg.msg_accrightslen, (int)sizeof(fd));
+ }
+#endif
+
+#if FD_PASSING_BY_MSG_CONTROL
+ memcpy(&fd, CMSG_DATA(&cmsg.hdr), sizeof(int));
+#endif
+
+ rb_update_max_fd(fd);
+
+ if (rsock_cmsg_cloexec_state < 0)
+ rsock_cmsg_cloexec_state = rsock_detect_cloexec(fd);
+ if (rsock_cmsg_cloexec_state == 0 || fd <= 2)
+ rb_maygvl_fd_fix_cloexec(fd);
+
+ if (klass == Qnil)
+ return INT2FIX(fd);
+ else {
+ ID for_fd;
+ int ff_argc;
+ VALUE ff_argv[2];
+ CONST_ID(for_fd, "for_fd");
+ ff_argc = mode == Qnil ? 1 : 2;
+ ff_argv[0] = INT2FIX(fd);
+ ff_argv[1] = mode;
+ return rb_funcall2(klass, for_fd, ff_argc, ff_argv);
+ }
+}
+#else
+#define unix_recv_io rb_f_notimplement
+#endif
+
+/*
+ * call-seq:
+ * unixsocket.addr => [address_family, unix_path]
+ *
+ * Returns the local address as an array which contains
+ * address_family and unix_path.
+ *
+ * Example
+ * serv = UNIXServer.new("/tmp/sock")
+ * p serv.addr #=> ["AF_UNIX", "/tmp/sock"]
+ */
+static VALUE
+unix_addr(VALUE sock)
+{
+ rb_io_t *fptr;
+ struct sockaddr_un addr;
+ socklen_t len = (socklen_t)sizeof addr;
+ socklen_t len0 = len;
+
+ GetOpenFile(sock, fptr);
+
+ if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
+ rsock_sys_fail_path("getsockname(2)", fptr->pathv);
+ if (len0 < len) len = len0;
+ return rsock_unixaddr(&addr, len);
+}
+
+/*
+ * call-seq:
+ * unixsocket.peeraddr => [address_family, unix_path]
+ *
+ * Returns the remote address as an array which contains
+ * address_family and unix_path.
+ *
+ * Example
+ * serv = UNIXServer.new("/tmp/sock")
+ * c = UNIXSocket.new("/tmp/sock")
+ * p c.peeraddr #=> ["AF_UNIX", "/tmp/sock"]
+ */
+static VALUE
+unix_peeraddr(VALUE sock)
+{
+ rb_io_t *fptr;
+ struct sockaddr_un addr;
+ socklen_t len = (socklen_t)sizeof addr;
+ socklen_t len0 = len;
+
+ GetOpenFile(sock, fptr);
+
+ if (getpeername(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
+ rsock_sys_fail_path("getpeername(2)", fptr->pathv);
+ if (len0 < len) len = len0;
+ return rsock_unixaddr(&addr, len);
+}
+
+/*
+ * call-seq:
+ * UNIXSocket.pair([type [, protocol]]) => [unixsocket1, unixsocket2]
+ * UNIXSocket.socketpair([type [, protocol]]) => [unixsocket1, unixsocket2]
+ *
+ * Creates a pair of sockets connected to each other.
+ *
+ * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
+ *
+ * _protocol_ should be a protocol defined in the domain.
+ * 0 is default protocol for the domain.
+ *
+ * s1, s2 = UNIXSocket.pair
+ * s1.send "a", 0
+ * s1.send "b", 0
+ * p s2.recv(10) #=> "ab"
+ *
+ */
+static VALUE
+unix_s_socketpair(int argc, VALUE *argv, VALUE klass)
+{
+ VALUE domain, type, protocol;
+ VALUE args[3];
+
+ domain = INT2FIX(PF_UNIX);
+ rb_scan_args(argc, argv, "02", &type, &protocol);
+ if (argc == 0)
+ type = INT2FIX(SOCK_STREAM);
+ if (argc <= 1)
+ protocol = INT2FIX(0);
+
+ args[0] = domain;
+ args[1] = type;
+ args[2] = protocol;
+
+ return rsock_sock_s_socketpair(3, args, klass);
+}
+#endif
+
+void
+rsock_init_unixsocket(void)
+{
+#ifdef HAVE_SYS_UN_H
+ /*
+ * Document-class: UNIXSocket < BasicSocket
+ *
+ * UNIXSocket represents a UNIX domain stream client socket.
+ */
+ rb_cUNIXSocket = rb_define_class("UNIXSocket", rb_cBasicSocket);
+ rb_define_method(rb_cUNIXSocket, "initialize", unix_init, 1);
+ rb_define_method(rb_cUNIXSocket, "path", unix_path, 0);
+ rb_define_method(rb_cUNIXSocket, "addr", unix_addr, 0);
+ rb_define_method(rb_cUNIXSocket, "peeraddr", unix_peeraddr, 0);
+ rb_define_method(rb_cUNIXSocket, "recvfrom", unix_recvfrom, -1);
+ rb_define_method(rb_cUNIXSocket, "send_io", unix_send_io, 1);
+ rb_define_method(rb_cUNIXSocket, "recv_io", unix_recv_io, -1);
+ rb_define_singleton_method(rb_cUNIXSocket, "socketpair", unix_s_socketpair, -1);
+ rb_define_singleton_method(rb_cUNIXSocket, "pair", unix_s_socketpair, -1);
+#endif
+}