Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6d5dfa5ceb6b
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 78c8657a6326
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Jan 2, 2016

  1. More robust conversion and value-checking.

    Covers address family, protocol family, socket type, and protocol.
    headius committed Jan 2, 2016
    Copy the full SHA
    0d68727 View commit details
  2. Copy the full SHA
    78c8657 View commit details
Showing with 93 additions and 25 deletions.
  1. +27 −1 core/src/main/java/org/jruby/ext/socket/RubySocket.java
  2. +66 −24 core/src/main/java/org/jruby/ext/socket/SocketUtils.java
28 changes: 27 additions & 1 deletion core/src/main/java/org/jruby/ext/socket/RubySocket.java
Original file line number Diff line number Diff line change
@@ -400,7 +400,33 @@ public static IRubyObject socketpair(ThreadContext context, IRubyObject recv, IR
throw context.runtime.newErrnoEOPNOTSUPPError("Socket.socketpair only supports streaming UNIX sockets");
}

return RubyUNIXSocket.socketpair(context, recv, arrayOf(domain, type));
final Ruby runtime = context.runtime;

// TODO: type and protocol

UnixSocketChannel[] sp;

try {
sp = UnixSocketChannel.pair();

final RubyClass socketClass = runtime.getClass("Socket");
RubySocket sock = new RubySocket(runtime, socketClass);
sock.soDomain = AddressFamily.AF_UNIX;
sock.soType = Sock.SOCK_STREAM;
sock.soProtocolFamily = ProtocolFamily.PF_UNIX;
sock.initSocket(newChannelFD(runtime, sp[0]));

RubySocket sock2 = new RubySocket(runtime, socketClass);
sock2.soDomain = AddressFamily.AF_UNIX;
sock2.soType = Sock.SOCK_STREAM;
sock2.soProtocolFamily = ProtocolFamily.PF_UNIX;
sock.initSocket(newChannelFD(runtime, sp[1]));

return runtime.newArray(sock, sock2);

} catch (IOException ioe) {
throw runtime.newIOErrorFromException(ioe);
}
}

private void initFieldsFromDescriptor(Ruby runtime, ChannelFD fd) {
90 changes: 66 additions & 24 deletions core/src/main/java/org/jruby/ext/socket/SocketUtils.java
Original file line number Diff line number Diff line change
@@ -579,48 +579,90 @@ private static String getHostAddress(ThreadContext context, InetAddress addr, Bo

// MRI: address family part of rsock_family_to_int
static AddressFamily addressFamilyFromArg(IRubyObject domain) {
if(domain instanceof RubyString || domain instanceof RubySymbol) {
String domainString = domain.toString();
if (domainString.startsWith("AF_")) return AddressFamily.valueOf(domainString);
return AddressFamily.valueOf("AF_" + domainString);
IRubyObject maybeString = TypeConverter.checkStringType(domain.getRuntime(), domain);

if (!maybeString.isNil()) {
domain = maybeString;
}

int domainInt = RubyNumeric.fix2int(domain);
return AddressFamily.valueOf(domainInt);
try {
if (domain instanceof RubyString || domain instanceof RubySymbol) {
String domainString = domain.toString();
if (domainString.startsWith("AF_")) return AddressFamily.valueOf(domainString);
if (domainString.startsWith("PF_"))
return AddressFamily.valueOf(ProtocolFamily.valueOf(domainString).intValue());
return AddressFamily.valueOf("AF_" + domainString);
}

int domainInt = RubyNumeric.fix2int(domain);
return AddressFamily.valueOf(domainInt);
} catch (IllegalArgumentException iae) {
throw SocketUtils.sockerr(domain.getRuntime(), "invalid address family: " + domain);
}
}

static Sock sockFromArg(IRubyObject type) {
if(type instanceof RubyString || type instanceof RubySymbol) {
String typeString = type.toString();
if (typeString.startsWith("SOCK_")) return Sock.valueOf(typeString.toString());
return Sock.valueOf("SOCK_" + typeString);
IRubyObject maybeString = TypeConverter.checkStringType(type.getRuntime(), type);

if (!maybeString.isNil()) {
type = maybeString;
}

int typeInt = RubyNumeric.fix2int(type);
return Sock.valueOf(typeInt);
try {
if(type instanceof RubyString || type instanceof RubySymbol) {
String typeString = type.toString();
if (typeString.startsWith("SOCK_")) return Sock.valueOf(typeString.toString());
return Sock.valueOf("SOCK_" + typeString);
}

int typeInt = RubyNumeric.fix2int(type);
return Sock.valueOf(typeInt);
} catch (IllegalArgumentException iae) {
throw SocketUtils.sockerr(type.getRuntime(), "invalid socket type: " + type);
}
}

// MRI: protocol family part of rsock_family_to_int
static ProtocolFamily protocolFamilyFromArg(IRubyObject protocol) {
if (protocol instanceof RubyString || protocol instanceof RubySymbol) {
String protocolString = protocol.toString();
if (protocolString.startsWith("PF_")) return ProtocolFamily.valueOf(protocolString);
if (protocolString.startsWith("AF_")) return ProtocolFamily.valueOf(AddressFamily.valueOf(protocolString).intValue());
return ProtocolFamily.valueOf("PF_" + protocolString);
IRubyObject maybeString = TypeConverter.checkStringType(protocol.getRuntime(), protocol);

if (!maybeString.isNil()) {
protocol = maybeString;
}

int protocolInt = RubyNumeric.fix2int(protocol);
return ProtocolFamily.valueOf(protocolInt);
try {
if (protocol instanceof RubyString || protocol instanceof RubySymbol) {
String protocolString = protocol.toString();
if (protocolString.startsWith("PF_")) return ProtocolFamily.valueOf(protocolString);
if (protocolString.startsWith("AF_")) return ProtocolFamily.valueOf(AddressFamily.valueOf(protocolString).intValue());
return ProtocolFamily.valueOf("PF_" + protocolString);
}

int protocolInt = RubyNumeric.fix2int(protocol);
return ProtocolFamily.valueOf(protocolInt);
} catch (IllegalArgumentException iae) {
throw SocketUtils.sockerr(protocol.getRuntime(), "invalid protocol family: " + protocol);
}
}

static Protocol protocolFromArg(IRubyObject protocol) {
if(protocol instanceof RubyString || protocol instanceof RubySymbol) {
String protocolString = protocol.toString();
return Protocol.getProtocolByName(protocolString);
IRubyObject maybeString = TypeConverter.checkStringType(protocol.getRuntime(), protocol);

if (!maybeString.isNil()) {
protocol = maybeString;
}

int protocolInt = RubyNumeric.fix2int(protocol);
return Protocol.getProtocolByNumber(protocolInt);
try {
if(protocol instanceof RubyString || protocol instanceof RubySymbol) {
String protocolString = protocol.toString();
return Protocol.getProtocolByName(protocolString);
}

int protocolInt = RubyNumeric.fix2int(protocol);
return Protocol.getProtocolByNumber(protocolInt);
} catch (IllegalArgumentException iae) {
throw SocketUtils.sockerr(protocol.getRuntime(), "invalid protocol: " + protocol);
}
}

public static int portToInt(IRubyObject port) {