package org.jruby.ext.socket;
import static jnr.constants.platform.AddressFamily.*;
import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NoRouteToHostException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
public class RubyTCPSocket extends RubyIPSocket {
static void createTCPSocket(Ruby runtime) {
RubyClass rb_cTCPSocket = runtime.defineClass("TCPSocket", runtime.getClass("IPSocket"), TCPSOCKET_ALLOCATOR);
rb_cTCPSocket.defineAnnotatedMethods(RubyTCPSocket.class);
runtime.getObject().setConstant("TCPsocket",rb_cTCPSocket);
}
private static ObjectAllocator TCPSOCKET_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new RubyTCPSocket(runtime, klass);
}
};
public RubyTCPSocket(Ruby runtime, RubyClass type) {
super(runtime, type);
}
private SocketChannel attemptConnect(ThreadContext context, IRubyObject host, String localHost, int localPort,
String remoteHost, int remotePort) throws IOException {
for (InetAddress address: InetAddress.getAllByName(remoteHost)) {
SocketChannel channel = SocketChannel.open();
Socket socket = channel.socket();
openFile = null;
initSocket(newChannelFD(context.runtime, channel));
channel.configureBlocking(false);
if (localHost != null) {
socket.setReuseAddress(true);
socket.bind( new InetSocketAddress(InetAddress.getByName(localHost), localPort) );
}
try {
channel.connect(new InetSocketAddress(address, remotePort));
while (!context.getThread().select(channel, this, SelectionKey.OP_CONNECT)) {
context.pollThreadEvents();
}
while (!channel.finishConnect()) {
context.pollThreadEvents();
}
channel.configureBlocking(true);
return channel;
} catch (ConnectException e) {
}
}
throw context.runtime.newErrnoECONNREFUSEDError("connect(2) for " + host.inspect() + " port " + remotePort);
}
@JRubyMethod(required = 2, optional = 2, visibility = Visibility.PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.runtime;
IRubyObject host = args[0];
IRubyObject port = args[1];
final String remoteHost = host.isNil() ? "localhost" : host.convertToString().toString();
final int remotePort = SocketUtils.getPortFrom(context, port);
String localHost = (args.length >= 3 && !args[2].isNil()) ? args[2].convertToString().toString() : null;
int localPort = (args.length == 4 && !args[3].isNil()) ? SocketUtils.getPortFrom(context, args[3]) : 0;
boolean success = false;
SocketChannel channel = null;
try {
try {
channel = attemptConnect(context, host, localHost, localPort, remoteHost, remotePort);
success = true;
} catch (BindException e) {
throw runtime.newErrnoEADDRFromBindException(e, " to: " + remoteHost + ':' + remotePort);
} catch (NoRouteToHostException e) {
throw runtime.newErrnoEHOSTUNREACHError("SocketChannel.connect");
} catch (UnknownHostException e) {
throw SocketUtils.sockerr(runtime, "initialize: name or service not known");
}
} catch (ClosedChannelException e) {
throw runtime.newErrnoECONNREFUSEDError();
} catch (BindException e) {
throw runtime.newErrnoEADDRFromBindException(e, " on: " + localHost + ':' + localPort);
} catch (IOException e) {
throw runtime.newIOErrorFromException(e);
} catch (IllegalArgumentException e) {
throw sockerr(runtime, e.getMessage(), e);
} finally {
if (!success && channel != null) try { channel.close(); } catch (IOException ioe) {}
}
return context.nil;
}
@JRubyMethod(meta = true)
public static IRubyObject gethostbyname(ThreadContext context, IRubyObject recv, IRubyObject hostname) {
Ruby runtime = context.runtime;
IRubyObject ret0, ret1, ret2, ret3;
String hostString = hostname.convertToString().toString();
try {
InetAddress addr = InetAddress.getByName(hostString);
ret0 = runtime.newString(do_not_reverse_lookup(context, recv).isTrue() ? addr.getHostAddress() : addr.getCanonicalHostName());
ret1 = runtime.newArray();
if (addr instanceof Inet4Address) {
ret2 = runtime.newFixnum(AF_INET);
} else {
ret2 = runtime.newFixnum(AF_INET6);
}
ret3 = runtime.newString(addr.getHostAddress());
return RubyArray.newArray(runtime, ret0, ret1, ret2, ret3);
}
catch(UnknownHostException e) {
throw SocketUtils.sockerr(runtime, "gethostbyname: name or service not known");
}
}
@Deprecated
public static IRubyObject open(IRubyObject recv, IRubyObject[] args, Block block) {
return open(recv.getRuntime().getCurrentContext(), recv, args, block);
}
@Deprecated
public static IRubyObject gethostbyname(IRubyObject recv, IRubyObject hostname) {
return gethostbyname(recv.getRuntime().getCurrentContext(), recv, hostname);
}
}