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: 71a086cecc3e
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 837cb739713e
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Mar 20, 2015

  1. Add reverse_lookup param to Socket.getaddrinfo.

    Adding reverse_lookup parameter support to Socket.getaddrinfo.
    This parameter was added in Ruby 1.9.
    kpamidon committed Mar 20, 2015
    Copy the full SHA
    d0cec55 View commit details

Commits on May 8, 2015

  1. Merge pull request #2724 from kpamidon/socket_getaddrinfo_reverse_lookup

    Socket.getaddrinfo reverse_lookup
    headius committed May 8, 2015
    Copy the full SHA
    837cb73 View commit details
Showing with 42 additions and 9 deletions.
  1. +1 −1 core/src/main/java/org/jruby/ext/socket/RubySocket.java
  2. +41 −8 core/src/main/java/org/jruby/ext/socket/SocketUtils.java
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ext/socket/RubySocket.java
Original file line number Diff line number Diff line change
@@ -287,7 +287,7 @@ public static IRubyObject gethostbyname(ThreadContext context, IRubyObject recv,
return SocketUtils.gethostbyname(context, hostname);
}

@JRubyMethod(required = 2, optional = 4, meta = true)
@JRubyMethod(required = 2, optional = 5, meta = true)
public static IRubyObject getaddrinfo(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
return SocketUtils.getaddrinfo(context, args);
}
49 changes: 41 additions & 8 deletions core/src/main/java/org/jruby/ext/socket/SocketUtils.java
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
import jnr.netdb.Service;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyInteger;
@@ -182,15 +183,15 @@ public static IRubyObject gethostbyname(ThreadContext context, IRubyObject hostn
/**
* Ruby definition would look like:
*
* def self.getaddrinfo(host, port, family = nil, socktype = nil, protocol = nil, flags = nil)
* def self.getaddrinfo(host, port, family = nil, socktype = nil, protocol = nil, flags = nil, reverse_lookup = nil)
*/
public static IRubyObject getaddrinfo(final ThreadContext context, IRubyObject[] args) {
final Ruby runtime = context.runtime;
final List<IRubyObject> l = new ArrayList<IRubyObject>();

buildAddrinfoList(context, args, new AddrinfoCallback() {
@Override
public void addrinfo(InetAddress address, int port, Sock sock) {
public void addrinfo(InetAddress address, int port, Sock sock, Boolean reverse) {
boolean is_ipv6 = address instanceof Inet6Address;
boolean sock_stream = true;
boolean sock_dgram = true;
@@ -211,7 +212,7 @@ public void addrinfo(InetAddress address, int port, Sock sock) {
c = new IRubyObject[7];
c[0] = runtime.newString(is_ipv6 ? "AF_INET6" : "AF_INET");
c[1] = runtime.newFixnum(port);
c[2] = runtime.newString(getHostAddress(context, address));
c[2] = runtime.newString(getHostAddress(context, address, reverse));
c[3] = runtime.newString(address.getHostAddress());
c[4] = runtime.newFixnum(is_ipv6 ? PF_INET6 : PF_INET);
c[5] = runtime.newFixnum(SOCK_DGRAM);
@@ -223,7 +224,7 @@ public void addrinfo(InetAddress address, int port, Sock sock) {
c = new IRubyObject[7];
c[0] = runtime.newString(is_ipv6 ? "AF_INET6" : "AF_INET");
c[1] = runtime.newFixnum(port);
c[2] = runtime.newString(getHostAddress(context, address));
c[2] = runtime.newString(getHostAddress(context, address, reverse));
c[3] = runtime.newString(address.getHostAddress());
c[4] = runtime.newFixnum(is_ipv6 ? PF_INET6 : PF_INET);
c[5] = runtime.newFixnum(SOCK_STREAM);
@@ -242,7 +243,7 @@ public static List<Addrinfo> getaddrinfoList(ThreadContext context, IRubyObject[

buildAddrinfoList(context, args, new AddrinfoCallback() {
@Override
public void addrinfo(InetAddress address, int port, Sock sock) {
public void addrinfo(InetAddress address, int port, Sock sock, Boolean reverse) {
boolean sock_stream = true;
boolean sock_dgram = true;

@@ -279,7 +280,8 @@ interface AddrinfoCallback {
void addrinfo(
InetAddress address,
int port,
Sock sock);
Sock sock,
Boolean reverse);
}

public static void buildAddrinfoList(ThreadContext context, IRubyObject[] args, AddrinfoCallback callback) {
@@ -297,6 +299,24 @@ public static void buildAddrinfoList(ThreadContext context, IRubyObject[] args,
IRubyObject socktype = args.length > 3 ? args[3] : context.nil;
//IRubyObject protocol = args[4];
IRubyObject flags = args.length > 5 ? args[5] : context.nil;
IRubyObject reverseArg = args.length > 6 ? args[6] : context.nil;

// The Ruby Socket.getaddrinfo function supports boolean/nil/Symbol values for the
// reverse_lookup parameter. We need to massage all valid inputs to true/false/null.
Boolean reverseLookup = null;
if (reverseArg instanceof RubyBoolean) {
reverseLookup = reverseArg.isTrue();
} else if (reverseArg instanceof RubySymbol) {
String reverseString = reverseArg.toString();
if ("hostname".equals(reverseString)) {
reverseLookup = true;
} else if ("numeric".equals(reverseString)) {
reverseLookup = false;
} else {
throw runtime.newArgumentError("invalid reverse_lookup flag: :" +
reverseString);
}
}

AddressFamily addressFamily = AF_INET;
if (!family.isNil()) {
@@ -339,7 +359,7 @@ public static void buildAddrinfoList(ThreadContext context, IRubyObject[] args,

for(int i = 0; i < addrs.length; i++) {
int p = port.isNil() ? 0 : (int)port.convertToInteger().getLongValue();
callback.addrinfo(addrs[i], p, sock);
callback.addrinfo(addrs[i], p, sock, reverseLookup);
}

} catch(UnknownHostException e) {
@@ -517,8 +537,21 @@ public static int getPortFrom(ThreadContext context, IRubyObject _port) {
return port;
}

private static String getHostAddress(ThreadContext context, InetAddress addr, Boolean reverse) {
String ret;
if (reverse == null) {
ret = context.runtime.isDoNotReverseLookupEnabled() ?
addr.getHostAddress() : addr.getCanonicalHostName();
} else if (reverse) {
ret = addr.getCanonicalHostName();
} else {
ret = addr.getHostAddress();
}
return ret;
}

private static String getHostAddress(ThreadContext context, InetAddress addr) {
return context.runtime.isDoNotReverseLookupEnabled() ? addr.getHostAddress() : addr.getCanonicalHostName();
return getHostAddress(context, addr, null);
}

private static final Pattern STRING_IPV4_ADDRESS_PATTERN =