ソケットの操作
Socket
参考:
Socket::Constants
アドレスファミリー (AF_*)
Socket::Constants.constants.grep(/^AF_/).reduce({}) do |result, name| result.merge(name => Socket.const_get(name)) end
ソケットタイプ (SOCK_*)
Socket::Constants.constants.grep(/^SOCK_/).reduce({}) do |result, name| result.merge(name => Socket.const_get(name)) end
プロトコル (IPPROTO_*)
Socket::Constants.constants.grep(/^IPPROTO_/).reduce({}) do |result, name| result.merge(name => Socket.const_get(name)) end
addrinfo フラグ (AI_*)
Socket::Constants.constants.grep(/^AI_/).reduce({}) do |result, name| result.merge(name => Socket.const_get(name)) end
定義されている定数のカウント
[:AF, :SOCK, :IPPROTO, :AI].reduce({}) do |result, type| result.merge( type => Socket::Constants.constants.grep(/^#{type}_/).count ) end # => {:AF=>33, :SOCK=>5, :IPPROTO=>25, :AI=>10}
定数名と値を列挙する
[:AF, :SOCK, :IPPROTO, :AI].reduce({}) do |result, type| result.merge( type => Socket::Constants.constants.grep(/^#{type}_/).map do |name| [name, Socket.const_get(name)] end ) end # => {:AF=>[[:AF_INET, 2],[:AF_INET6, 30],[:AF_UNIX, 1],[:AF_UNSPEC, 0],[:AF_IPX, 23],[:AF_APPLETALK, 16],[:AF_LOCAL, 1],[:AF_IMPLINK, 3],[:AF_PUP, 4],[:AF_CHAOS, 5],[:AF_NS, 6],[:AF_ISO, 7],[:AF_OSI, 7],[:AF_ECMA, 8],[:AF_DATAKIT, 9],[:AF_CCITT, 10],[:AF_SNA, 11],[:AF_DLI, 13],[:AF_LAT, 14],[:AF_HYLINK, 15],[:AF_ROUTE, 17],[:AF_LINK, 18],[:AF_COIP, 20],[:AF_CNT, 21],[:AF_SIP, 24],[:AF_NDRV, 27],[:AF_ISDN, 28],[:AF_NATM, 31],[:AF_SYSTEM, 32],[:AF_NETBIOS, 33],[:AF_PPP, 34],[:AF_MAX, 40],[:AF_E164, 28]],:SOCK=>[[:SOCK_STREAM, 1], [:SOCK_DGRAM, 2], [:SOCK_RAW, 3], [:SOCK_RDM, 4], [:SOCK_SEQPACKET, 5]],:IPPROTO=>[[:IPPROTO_IP, 0],[:IPPROTO_ICMP, 1],[:IPPROTO_IGMP, 2],[:IPPROTO_GGP, 3],[:IPPROTO_TCP, 6],[:IPPROTO_EGP, 8],[:IPPROTO_PUP, 12],[:IPPROTO_UDP, 17],[:IPPROTO_IDP, 22],[:IPPROTO_HELLO, 63],[:IPPROTO_ND, 77],[:IPPROTO_TP, 29],[:IPPROTO_XTP, 36],[:IPPROTO_EON, 80],[:IPPROTO_AH, 51],[:IPPROTO_DSTOPTS, 60],[:IPPROTO_ESP, 50],[:IPPROTO_FRAGMENT, 44],[:IPPROTO_HOPOPTS, 0],[:IPPROTO_ICMPV6, 58],[:IPPROTO_IPV6, 41],[:IPPROTO_NONE, 59],[:IPPROTO_ROUTING, 43],[:IPPROTO_RAW, 255],[:IPPROTO_MAX, 256]],:AI=>[[:AI_PASSIVE, 1],[:AI_CANONNAME, 2],[:AI_NUMERICHOST, 4],[:AI_NUMERICSERV, 4096],[:AI_MASK, 5127],[:AI_ALL, 256],[:AI_V4MAPPED_CFG, 512],[:AI_ADDRCONFIG, 1024],[:AI_V4MAPPED, 2048],[:AI_DEFAULT, 1536]]}
値から定数名を逆引きする
Socket::Constants.constants.grep( /^IPPROTO_/ ).reduce({}) do |result ,name| result.merge(Socket.const_get(name) => name) end[6] # => :IPPROTO_TCP
参考: