2009-01-01 01:08:41 -05:00
|
|
|
require 'optparse'
|
2009-01-01 01:28:34 -05:00
|
|
|
require 'erb'
|
2009-01-01 01:08:41 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2009-01-01 01:28:34 -05:00
|
|
|
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, C_ESC) + '"'
|
2009-01-01 01:08:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.parse!
|
2005-02-28 01:29:52 -05:00
|
|
|
|
2009-01-01 01:28:34 -05:00
|
|
|
result = ''
|
|
|
|
|
2005-03-07 23:41:51 -05:00
|
|
|
# workaround for NetBSD, OpenBSD and etc.
|
2009-01-01 01:28:34 -05:00
|
|
|
result << "#define pseudo_AF_FTIP pseudo_AF_RTIP\n"
|
2005-03-07 23:41:51 -05:00
|
|
|
|
2009-01-01 02:49:31 -05:00
|
|
|
DEFS = []
|
|
|
|
DATA.each_line {|s|
|
|
|
|
name, default_value = s.scan(/\S+/)
|
|
|
|
next unless name && name[0] != ?#
|
|
|
|
DEFS << [name, default_value]
|
|
|
|
}
|
|
|
|
|
|
|
|
def each_const
|
|
|
|
DEFS.each {|name, default_value|
|
2008-12-30 07:19:40 -05:00
|
|
|
if name =~ /\AINADDR_/
|
|
|
|
define = "sock_define_uconst"
|
|
|
|
else
|
|
|
|
define = "sock_define_const"
|
|
|
|
end
|
2009-01-01 02:04:31 -05:00
|
|
|
guard = nil
|
|
|
|
if /\A(AF_INET6|PF_INET6)\z/ =~ name
|
|
|
|
# IPv6 is not supported although AF_INET6 is defined on bcc32/mingw
|
|
|
|
guard = "defined(INET6)"
|
|
|
|
end
|
|
|
|
yield guard, define, name, default_value
|
2009-01-01 01:28:34 -05:00
|
|
|
}
|
2005-02-28 01:29:52 -05:00
|
|
|
end
|
|
|
|
|
2009-01-01 02:49:31 -05:00
|
|
|
def each_name(pat)
|
|
|
|
DEFS.each {|name, default_value|
|
|
|
|
next if pat !~ name
|
|
|
|
yield name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-01 03:03:36 -05:00
|
|
|
def each_names_with_len(pat)
|
|
|
|
h = {}
|
|
|
|
DEFS.each {|name, default_value|
|
|
|
|
next if pat !~ name
|
|
|
|
(h[name.length] ||= []) << name
|
|
|
|
}
|
|
|
|
h.keys.sort.each {|len|
|
|
|
|
yield h[len], len
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-01 05:54:38 -05:00
|
|
|
ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int(str_var, len_var, retp_var, pat)")
|
2009-01-01 03:38:14 -05:00
|
|
|
switch (<%=len_var%>) {
|
|
|
|
% each_names_with_len(pat) {|names, len|
|
|
|
|
case <%=len%>:
|
|
|
|
% names.each {|name|
|
|
|
|
#ifdef <%=name%>
|
|
|
|
% size = name.bytesize
|
2009-01-01 05:54:38 -05:00
|
|
|
if (memcmp(<%=str_var%>, <%=c_str name%>, <%=size%>) == 0) { *<%=retp_var%> = <%=name%>; return 0; }
|
2009-01-01 03:38:14 -05:00
|
|
|
#endif
|
|
|
|
% }
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
% }
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
|
2009-01-01 05:37:41 -05:00
|
|
|
MAYBE_ALIAS = [
|
2009-01-01 06:55:05 -05:00
|
|
|
["AF_UNIX", "AF_LOCAL"],
|
|
|
|
["AF_ISO", "AF_OSI"]
|
2009-01-01 05:37:41 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
def each_alias(pat)
|
|
|
|
h = {}
|
|
|
|
each_name(pat) {|name|
|
|
|
|
h[name] = [name]
|
|
|
|
}
|
|
|
|
MAYBE_ALIAS.each {|names|
|
|
|
|
a = []
|
|
|
|
names.each {|n|
|
|
|
|
a << n if h.delete n
|
|
|
|
}
|
|
|
|
h[a.first] = a
|
|
|
|
}
|
|
|
|
h.each_value {|names|
|
|
|
|
yield names
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name(int_var, pat)")
|
|
|
|
switch (<%=int_var%>) {
|
|
|
|
% each_alias(pat) {|names|
|
|
|
|
% names.each_with_index {|n, i|
|
|
|
|
% cond = ["defined(#{n})"]
|
|
|
|
% (0...i).each {|j| cond << "(!defined(#{names[j]}) || #{n} != #{names[j]})" }
|
|
|
|
#if <%=cond.join(" && ")%>
|
|
|
|
case <%=n%>: return <%=c_str n%>;
|
|
|
|
#endif
|
|
|
|
% }
|
|
|
|
% }
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
|
2009-01-01 01:28:34 -05:00
|
|
|
result << ERB.new(<<'EOS', nil, '%').result(binding)
|
2009-01-01 02:09:19 -05:00
|
|
|
static void
|
|
|
|
init_constants(VALUE mConst)
|
|
|
|
{
|
2009-01-01 02:49:31 -05:00
|
|
|
% each_const {|guard, define, name, default_value|
|
2009-01-01 02:04:31 -05:00
|
|
|
% if guard
|
|
|
|
#if <%=guard%>
|
|
|
|
% end
|
|
|
|
#if defined(<%=name%>)
|
2009-01-01 01:28:34 -05:00
|
|
|
<%=define%>(<%=c_str name%>, <%=name%>);
|
|
|
|
% if default_value
|
|
|
|
#else
|
|
|
|
<%=define%>(<%=c_str name%>, <%=default_value%>);
|
|
|
|
% end
|
|
|
|
#endif
|
2009-01-01 02:04:31 -05:00
|
|
|
% if guard
|
|
|
|
#endif
|
|
|
|
% end
|
2009-01-01 01:28:34 -05:00
|
|
|
|
|
|
|
% }
|
2009-01-01 02:09:19 -05:00
|
|
|
}
|
2009-01-01 02:49:31 -05:00
|
|
|
|
|
|
|
static int
|
2009-01-01 05:54:38 -05:00
|
|
|
family_to_int(char *str, int len, int *valp)
|
2009-01-01 02:49:31 -05:00
|
|
|
{
|
2009-01-01 05:54:38 -05:00
|
|
|
<%= gen_name_to_int("str", "len", "valp", /\A[AP]F_/) %>
|
2009-01-01 03:38:14 -05:00
|
|
|
}
|
2009-01-01 03:03:36 -05:00
|
|
|
|
2009-01-01 03:38:14 -05:00
|
|
|
static int
|
2009-01-01 05:54:38 -05:00
|
|
|
socktype_to_int(char *str, int len, int *valp)
|
2009-01-01 03:38:14 -05:00
|
|
|
{
|
2009-01-01 05:54:38 -05:00
|
|
|
<%= gen_name_to_int("str", "len", "valp", /\ASOCK_/) %>
|
2009-01-01 02:49:31 -05:00
|
|
|
}
|
|
|
|
|
2009-01-01 05:37:41 -05:00
|
|
|
static char *
|
|
|
|
family_to_str(int val)
|
|
|
|
{
|
|
|
|
<%= gen_int_to_name("val", /\AAF_/) %>
|
|
|
|
}
|
|
|
|
|
2009-01-01 01:28:34 -05:00
|
|
|
EOS
|
|
|
|
|
2009-01-01 01:08:41 -05:00
|
|
|
if opt_o
|
|
|
|
File.open(opt_o, 'w') {|f|
|
2009-01-01 01:28:34 -05:00
|
|
|
f << result
|
2009-01-01 01:08:41 -05:00
|
|
|
}
|
|
|
|
else
|
2009-01-01 01:28:34 -05:00
|
|
|
$stdout << result
|
2009-01-01 01:08:41 -05:00
|
|
|
end
|
|
|
|
|
2005-02-28 01:29:52 -05:00
|
|
|
__END__
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XTP
|
|
|
|
IPPROTO_EON
|
|
|
|
IPPROTO_BIP
|
2008-09-15 23:13:14 -04:00
|
|
|
IPPROTO_AH
|
|
|
|
IPPROTO_DSTOPTS
|
|
|
|
IPPROTO_ESP
|
|
|
|
IPPROTO_FRAGMENT
|
|
|
|
IPPROTO_HOPOPTS
|
|
|
|
IPPROTO_ICMPV6
|
|
|
|
IPPROTO_IPV6
|
|
|
|
IPPROTO_NONE
|
|
|
|
IPPROTO_ROUTING
|
|
|
|
|
2005-02-28 01:29:52 -05:00
|
|
|
IPPROTO_RAW 255
|
|
|
|
IPPROTO_MAX
|
|
|
|
|
|
|
|
# Some port configuration
|
|
|
|
IPPORT_RESERVED 1024
|
|
|
|
IPPORT_USERRESERVED 5000
|
|
|
|
|
|
|
|
# Some reserved IP v.4 addresses
|
|
|
|
INADDR_ANY 0x00000000
|
|
|
|
INADDR_BROADCAST 0xffffffff
|
|
|
|
INADDR_LOOPBACK 0x7F000001
|
|
|
|
INADDR_UNSPEC_GROUP 0xe0000000
|
|
|
|
INADDR_ALLHOSTS_GROUP 0xe0000001
|
|
|
|
INADDR_MAX_LOCAL_GROUP 0xe00000ff
|
|
|
|
INADDR_NONE 0xffffffff
|
|
|
|
|
|
|
|
# IP [gs]etsockopt options
|
|
|
|
IP_OPTIONS
|
|
|
|
IP_HDRINCL
|
|
|
|
IP_TOS
|
|
|
|
IP_TTL
|
|
|
|
IP_RECVOPTS
|
|
|
|
IP_RECVRETOPTS
|
|
|
|
IP_RECVDSTADDR
|
|
|
|
IP_RETOPTS
|
|
|
|
IP_MULTICAST_IF
|
|
|
|
IP_MULTICAST_TTL
|
|
|
|
IP_MULTICAST_LOOP
|
|
|
|
IP_ADD_MEMBERSHIP
|
|
|
|
IP_DROP_MEMBERSHIP
|
|
|
|
IP_DEFAULT_MULTICAST_TTL
|
|
|
|
IP_DEFAULT_MULTICAST_LOOP
|
|
|
|
IP_MAX_MEMBERSHIPS
|
2005-02-28 09:18:41 -05:00
|
|
|
|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
|
EAI_NODATA
|
|
|
|
EAI_NONAME
|
2008-09-15 23:13:14 -04:00
|
|
|
EAI_OVERFLOW
|
2005-02-28 01:29:52 -05:00
|
|
|
EAI_SERVICE
|
|
|
|
EAI_SOCKTYPE
|
|
|
|
EAI_SYSTEM
|
|
|
|
EAI_BADHINTS
|
|
|
|
EAI_PROTOCOL
|
|
|
|
EAI_MAX
|
2005-02-28 09:18:41 -05:00
|
|
|
|
2005-02-28 01:29:52 -05:00
|
|
|
AI_PASSIVE
|
|
|
|
AI_CANONNAME
|
|
|
|
AI_NUMERICHOST
|
2008-09-15 23:13:14 -04:00
|
|
|
AI_NUMERICSERV
|
2005-02-28 01:29:52 -05:00
|
|
|
AI_MASK
|
|
|
|
AI_ALL
|
|
|
|
AI_V4MAPPED_CFG
|
|
|
|
AI_ADDRCONFIG
|
|
|
|
AI_V4MAPPED
|
|
|
|
AI_DEFAULT
|
2005-02-28 09:18:41 -05:00
|
|
|
|
2005-02-28 01:29:52 -05:00
|
|
|
NI_MAXHOST
|
|
|
|
NI_MAXSERV
|
|
|
|
NI_NOFQDN
|
|
|
|
NI_NUMERICHOST
|
|
|
|
NI_NAMEREQD
|
|
|
|
NI_NUMERICSERV
|
|
|
|
NI_DGRAM
|
2005-02-28 09:18:41 -05:00
|
|
|
|
2005-02-28 01:29:52 -05:00
|
|
|
SHUT_RD 0
|
|
|
|
SHUT_WR 1
|
|
|
|
SHUT_RDWR 2
|
2008-09-06 08:38:59 -04:00
|
|
|
|
|
|
|
IPV6_JOIN_GROUP
|
|
|
|
IPV6_LEAVE_GROUP
|
|
|
|
IPV6_MULTICAST_HOPS
|
|
|
|
IPV6_MULTICAST_IF
|
|
|
|
IPV6_MULTICAST_LOOP
|
|
|
|
IPV6_UNICAST_HOPS
|
|
|
|
IPV6_V6ONLY
|
|
|
|
IPV6_CHECKSUM
|
|
|
|
IPV6_DONTFRAG
|
|
|
|
IPV6_DSTOPTS
|
|
|
|
IPV6_HOPLIMIT
|
|
|
|
IP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