Skip to content

Commit

Permalink
Showing 2 changed files with 42 additions and 9 deletions.
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
@@ -313,7 +313,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) {
@@ -527,8 +547,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 =

0 comments on commit 5da3695

Please sign in to comment.