Skip to content

Commit e15f74e

Browse files
committedMar 26, 2018
Handle error when attempting to connect to IP6 with default INET4.
Fixes #5112.
1 parent 877c609 commit e15f74e

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/ext/socket/RubyUDPSocket.java

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.net.SocketAddress;
4141
import java.net.SocketException;
4242
import java.net.MulticastSocket;
43+
import java.net.SocketOption;
4344
import java.net.StandardProtocolFamily;
4445
import java.net.UnknownHostException;
4546
import java.net.DatagramPacket;
@@ -48,6 +49,7 @@
4849
import java.nio.channels.DatagramChannel;
4950
import java.nio.channels.IllegalBlockingModeException;
5051
import java.nio.channels.NotYetConnectedException;
52+
import java.nio.channels.UnsupportedAddressTypeException;
5153

5254
import jnr.constants.platform.AddressFamily;
5355
import jnr.netdb.Service;
@@ -123,6 +125,7 @@ public IRubyObject initialize(ThreadContext context, ProtocolFamily family) {
123125
Ruby runtime = context.runtime;
124126

125127
try {
128+
this.family = family;
126129
DatagramChannel channel = DatagramChannel.open(family);
127130
initSocket(newChannelFD(runtime, channel));
128131
} catch (ConnectException e) {
@@ -188,6 +191,11 @@ else if (host instanceof RubyFixnum) {
188191

189192
return RubyFixnum.zero(runtime);
190193
}
194+
catch (UnsupportedAddressTypeException e) {
195+
// This may not be the appropriate message for all such exceptions
196+
ProtocolFamily family = this.family == null ? StandardProtocolFamily.INET : this.family;
197+
throw SocketUtils.sockerr(runtime, "bind: unsupported address " + host.inspect() + " for protocol family " + family);
198+
}
191199
catch (UnknownHostException e) {
192200
throw SocketUtils.sockerr(runtime, "bind: name or service not known");
193201
}
@@ -630,6 +638,7 @@ private static IRubyObject doReceiveMulticast(RubyBasicSocket socket, final Ruby
630638
}
631639

632640
private volatile Class<? extends InetAddress> explicitFamily;
641+
private volatile ProtocolFamily family;
633642

634643
@Deprecated
635644
public IRubyObject bind(IRubyObject host, IRubyObject port) {

0 commit comments

Comments
 (0)
Please sign in to comment.