Skip to content

Commit

Permalink
add socket flags for zeroconf
Browse files Browse the repository at this point in the history
  • Loading branch information
ahorek committed Dec 21, 2023
1 parent 627d8d1 commit 3814c47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
27 changes: 24 additions & 3 deletions core/src/main/java/org/jruby/ext/socket/Ifaddr.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.net.SocketException;
import java.net.UnknownHostException;

import jnr.constants.platform.InterfaceInfo;

/**
*
* @author Lucas Allan Amorim
Expand All @@ -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;

Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

}
11 changes: 3 additions & 8 deletions core/src/main/java/org/jruby/ext/socket/RubySocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit 3814c47

Please sign in to comment.