diff --git a/core/src/main/java/org/jruby/ext/socket/Ifaddr.java b/core/src/main/java/org/jruby/ext/socket/Ifaddr.java index e411473e6da..3d6a077b046 100644 --- a/core/src/main/java/org/jruby/ext/socket/Ifaddr.java +++ b/core/src/main/java/org/jruby/ext/socket/Ifaddr.java @@ -15,6 +15,8 @@ import java.net.SocketException; import java.net.UnknownHostException; +import jnr.constants.platform.InterfaceInfo; + /** * * @author Lucas Allan Amorim @@ -32,6 +34,7 @@ public class Ifaddr extends RubyObject { private boolean isLink; private String netmask; private int index; + private int flags; private String flagStatus; private Addrinfo addr; @@ -56,12 +59,14 @@ public Ifaddr(Ruby runtime, RubyClass metaClass, NetworkInterface ni, InterfaceA this.index = ni.getIndex(); this.networkInterface = ni; this.isLink = false; + this.flags = 0; this.address = it.getAddress(); this.broadcast = it.getBroadcast(); this.interfaceAddress = it; setAddr(runtime); setNetmask(it); setInspectString(ni); + setInterfaceFlags(ni); } public Ifaddr(Ruby runtime, RubyClass metaClass, NetworkInterface ni) throws SocketException { @@ -71,10 +76,12 @@ public Ifaddr(Ruby runtime, RubyClass metaClass, NetworkInterface ni) throws Soc this.isUp = ni.isUp(); this.isPointToPoint = ni.isPointToPoint(); this.index = ni.getIndex(); + this.flags = 0; this.networkInterface = ni; this.isLink = true; setAddr(runtime); setInspectString(ni); + setInterfaceFlags(ni); } @JRubyMethod @@ -111,10 +118,9 @@ public IRubyObject ifindex(ThreadContext context) { return context.runtime.newFixnum(index); } - @JRubyMethod(notImplemented = true) + @JRubyMethod public IRubyObject flags(ThreadContext context) { - // not implemented yet - return context.nil; + return context.runtime.newFixnum(flags); } @JRubyMethod @@ -212,4 +218,19 @@ private String getBroadcastAsString() { return broadcast.toString().substring(1); } + private void setInterfaceFlags(NetworkInterface nif) throws SocketException { + if (isUp) { + flags |= InterfaceInfo.IFF_UP.value(); + } + if (nif.supportsMulticast()) { + flags |= InterfaceInfo.IFF_MULTICAST.value(); + } + if (isLoopback) { + flags |= InterfaceInfo.IFF_LOOPBACK.value(); + } + if (isPointToPoint) { + flags |= InterfaceInfo.IFF_POINTOPOINT.value(); + } + } + } diff --git a/core/src/main/java/org/jruby/ext/socket/RubySocket.java b/core/src/main/java/org/jruby/ext/socket/RubySocket.java index eefa175d8b4..59f80b26cde 100644 --- a/core/src/main/java/org/jruby/ext/socket/RubySocket.java +++ b/core/src/main/java/org/jruby/ext/socket/RubySocket.java @@ -42,6 +42,7 @@ import jnr.constants.platform.SocketMessage; import jnr.constants.platform.SocketOption; import jnr.constants.platform.TCP; +import jnr.constants.platform.InterfaceInfo; import jnr.netdb.Protocol; import jnr.unixsocket.UnixSocketAddress; import jnr.unixsocket.UnixSocketChannel; @@ -109,6 +110,8 @@ static void createSocket(Ruby runtime) { runtime.loadConstantSet(rb_mConstants, IPProto.class); runtime.loadConstantSet(rb_mConstants, Shutdown.class); runtime.loadConstantSet(rb_mConstants, TCP.class); + runtime.loadConstantSet(rb_mConstants, IP.class); + runtime.loadConstantSet(rb_mConstants, InterfaceInfo.class); runtime.loadConstantSet(rb_mConstants, NameInfo.class); runtime.loadConstantSet(rb_mConstants, SocketMessage.class); @@ -133,14 +136,6 @@ static void createSocket(Ruby runtime) { rb_mConstants.setConstant("AI_DEFAULT", runtime.newFixnum(AddressInfo.AI_DEFAULT)); rb_mConstants.setConstant("AI_MASK", runtime.newFixnum(AddressInfo.AI_MASK)); - // More constants needed by specs - rb_mConstants.setConstant("IP_MULTICAST_TTL", runtime.newFixnum(IP.IP_MULTICAST_TTL.value())); - rb_mConstants.setConstant("IP_MULTICAST_LOOP", runtime.newFixnum(IP.IP_MULTICAST_LOOP.value())); - rb_mConstants.setConstant("IP_ADD_MEMBERSHIP", runtime.newFixnum(IP.IP_ADD_MEMBERSHIP.value())); - rb_mConstants.setConstant("IP_MAX_MEMBERSHIPS", runtime.newFixnum(IP.IP_MAX_MEMBERSHIPS.value())); - rb_mConstants.setConstant("IP_DEFAULT_MULTICAST_LOOP", runtime.newFixnum(IP.IP_DEFAULT_MULTICAST_LOOP)); - rb_mConstants.setConstant("IP_DEFAULT_MULTICAST_TTL", runtime.newFixnum(IP.IP_DEFAULT_MULTICAST_TTL)); - rb_cSocket.includeModule(rb_mConstants); rb_cSocket.defineAnnotatedMethods(RubySocket.class);