ruby-changes:28587
From: akr <ko1@a...>
Date: Sat, 11 May 2013 17:32:40 +0900 (JST)
Subject: [ruby-changes:28587] akr:r40639 (trunk): * ext/socket: New method, Socket.getifaddrs, implemented.
akr 2013-05-11 17:32:26 +0900 (Sat, 11 May 2013) New Revision: 40639 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40639 Log: * ext/socket: New method, Socket.getifaddrs, implemented. [ruby-core:54777] [Feature #8368] Added files: trunk/ext/socket/ifaddr.c Modified files: trunk/ChangeLog trunk/ext/socket/extconf.rb trunk/ext/socket/init.c trunk/ext/socket/mkconstants.rb trunk/ext/socket/raddrinfo.c trunk/ext/socket/rubysocket.h trunk/ext/socket/socket.c trunk/test/socket/test_socket.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 40638) +++ ChangeLog (revision 40639) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat May 11 17:28:51 2013 Tanaka Akira <akr@f...> + + * ext/socket: New method, Socket.getifaddrs, implemented. + [ruby-core:54777] [Feature #8368] + Sat May 11 00:47:22 2013 Tanaka Akira <akr@f...> * gc.h (SET_MACHINE_STACK_END): Add !defined(_ILP32) to a defining Index: ext/socket/rubysocket.h =================================================================== --- ext/socket/rubysocket.h (revision 40638) +++ ext/socket/rubysocket.h (revision 40639) @@ -44,6 +44,13 @@ https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L44 # 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 @@ -87,6 +94,9 @@ https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L94 #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; @@ -169,6 +179,9 @@ typedef union { https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L179 #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; @@ -267,6 +280,9 @@ VALUE rsock_addrinfo_inspect_sockaddr(VA https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L280 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 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); @@ -344,6 +360,7 @@ void rsock_init_socket_constants(void); https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L360 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_sys_fail_host_port(const char *, VALUE, VALUE)); Index: ext/socket/init.c =================================================================== --- ext/socket/init.c (revision 40638) +++ ext/socket/init.c (revision 40639) @@ -627,5 +627,6 @@ rsock_init_socket_init() https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L627 rsock_init_sockopt(); rsock_init_ancdata(); rsock_init_addrinfo(); + rsock_init_sockifaddr(); rsock_init_socket_constants(); } Index: ext/socket/raddrinfo.c =================================================================== --- ext/socket/raddrinfo.c (revision 40638) +++ ext/socket/raddrinfo.c (revision 40639) @@ -953,34 +953,62 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L953 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); +} - if (rai->sockaddr_len == 0) { +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)rai->sockaddr_len < ((char*)&rai->addr.addr.sa_family + sizeof(rai->addr.addr.sa_family)) - (char*)&rai->addr) + 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 (rai->addr.addr.sa_family) { + switch (sockaddr->addr.sa_family) { + case AF_UNSPEC: + { + rb_str_cat2(ret, "UNSPEC"); + break; + } + case AF_INET: { struct sockaddr_in *addr; int port; - if (rai->sockaddr_len < (socklen_t)sizeof(struct sockaddr_in)) { - rb_str_cat2(ret, "too-short-AF_INET-sockaddr"); - } - else { - addr = &rai->addr.in; - rb_str_catf(ret, "%d.%d.%d.%d", - ((unsigned char*)&addr->sin_addr)[0], - ((unsigned char*)&addr->sin_addr)[1], - ((unsigned char*)&addr->sin_addr)[2], - ((unsigned char*)&addr->sin_addr)[3]); - port = ntohs(addr->sin_port); - if (port) - rb_str_catf(ret, ":%d", port); - if ((socklen_t)sizeof(struct sockaddr_in) < rai->sockaddr_len) - rb_str_catf(ret, "(sockaddr %d bytes too long)", (int)(rai->sockaddr_len - sizeof(struct sockaddr_in))); - } + addr = &sockaddr->in; + if (((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 (((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 (((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 (((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 (((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; } @@ -991,16 +1019,16 @@ inspect_sockaddr(VALUE addrinfo, VALUE r https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1019 char hbuf[1024]; int port; int error; - if (rai->sockaddr_len < (socklen_t)sizeof(struct sockaddr_in6)) { - rb_str_cat2(ret, "too-short-AF_INET6-sockaddr"); + if (socklen < (socklen_t)sizeof(struct sockaddr_in6)) { + rb_str_catf(ret, "too-short-AF_INET6-sockaddr %d bytes", (int)socklen); } else { - addr = &rai->addr.in6; + 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(&rai->addr.addr, rai->sockaddr_len, + error = getnameinfo(&sockaddr->addr, socklen, hbuf, (socklen_t)sizeof(hbuf), NULL, 0, NI_NUMERICHOST|NI_NUMERICSERV); if (error) { @@ -1013,8 +1041,8 @@ inspect_sockaddr(VALUE addrinfo, VALUE r https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1041 port = ntohs(addr->sin6_port); rb_str_catf(ret, "[%s]:%d", hbuf, port); } - if ((socklen_t)sizeof(struct sockaddr_in6) < rai->sockaddr_len) - rb_str_catf(ret, "(sockaddr %d bytes too long)", (int)(rai->sockaddr_len - sizeof(struct sockaddr_in6))); + if ((socklen_t)sizeof(struct sockaddr_in6) < socklen) + rb_str_catf(ret, "(sockaddr %d bytes too long)", (int)(socklen - sizeof(struct sockaddr_in6))); } break; } @@ -1023,10 +1051,10 @@ inspect_sockaddr(VALUE addrinfo, VALUE r https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1051 #ifdef HAVE_SYS_UN_H case AF_UNIX: { - struct sockaddr_un *addr = &rai->addr.un; + struct sockaddr_un *addr = &sockaddr->un; char *p, *s, *e; s = addr->sun_path; - e = (char*)addr + rai->sockaddr_len; + e = (char*)addr + socklen; while (s < e && *(e-1) == '\0') e--; if (e < s) @@ -1042,11 +1070,11 @@ inspect_sockaddr(VALUE addrinfo, VALUE r https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1070 } if (printable_only) { /* only printable, no space */ if (s[0] != '/') /* relative path */ - rb_str_cat2(ret, "AF_UNIX "); + rb_str_cat2(ret, "UNIX "); rb_str_cat(ret, s, p - s); } else { - rb_str_cat2(ret, "AF_UNIX"); + rb_str_cat2(ret, "UNIX"); while (s < e) rb_str_catf(ret, ":%02x", (unsigned char)*s++); } @@ -1055,11 +1083,157 @@ inspect_sockaddr(VALUE addrinfo, VALUE r https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1083 } #endif +#ifdef AF_PACKET + /* 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) <= socklen) { + CATSEP; + rb_str_catf(ret, "protocol=%d", ntohs(addr->sll_protocol)); + } + if (offsetof(struct sockaddr_ll, sll_ifindex) + sizeof(addr->sll_ifindex) <= 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) <= socklen) { + CATSEP; + rb_str_catf(ret, "hatype=%d", addr->sll_hatype); + } + if (offsetof(struct sockaddr_ll, sll_pkttype) + sizeof(addr->sll_pkttype) <= 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) <= socklen) { + rb_str_catf(ret, "halen=%d", addr->sll_halen); + } + } + if (offsetof(struct sockaddr_ll, sll_addr) < socklen) { + socklen_t len, i; + CATSEP; + rb_str_cat2(ret, "hwaddr"); + len = addr->sll_halen; + if (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 + +#ifdef AF_LINK + /* 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 = 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 = 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(rai->addr.addr.sa_family); + ID id = rsock_intern_family(sockaddr->addr.sa_family); if (id == 0) - rb_str_catf(ret, "unknown address family %d", rai->addr.addr.sa_family); + 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; Index: ext/socket/mkconstants.rb =================================================================== --- ext/socket/mkconstants.rb (revision 40638) +++ ext/socket/mkconstants.rb (revision 40639) @@ -703,3 +703,62 @@ SCM_UCRED nil User credentials https://github.com/ruby/ruby/blob/trunk/ext/socket/mkconstants.rb#L703 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 Index: ext/socket/extconf.rb =================================================================== --- ext/socket/extconf.rb (revision 40638) +++ ext/socket/extconf.rb (re (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/