Refine search
@Override public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { if (!(socket instanceof UnixSocket)) { throw new AssertionError("Unexpected socket: " + socket); } socket.setSoTimeout(connectTimeout); try { socket.getChannel().connect(new UnixSocketAddress(socketFile)); } catch (SocketTimeoutException e) { throw new ConnectTimeoutException(e, null, remoteAddress.getAddress()); } return socket; } }
@JRubyMethod public IRubyObject inspect_sockaddr(ThreadContext context) { if (socketAddress instanceof UnixSocketAddress) { String path = getUnixSocketAddress().path(); return context.runtime.newString(path.startsWith("/") ? path : "UNIX " + path); } int port = getInetSocketAddress().getPort(); if (getInetAddress() instanceof Inet6Address) { String host = ipv6_ip(); String hostPort = port == 0 ? host : "[" + host + "]:" + port; return context.runtime.newString(hostPort); } String portString = port == 0 ? "" : ":" + port; String host = getInetSocketAddress().getAddress().getHostAddress(); return context.runtime.newString(host + portString); }
socketAddress = new UnixSocketAddress(new File(path.toString())); this.socketType = SocketType.UNIX; this.sock = Sock.SOCK_STREAM; if (inetAddress == null) inetAddress = getRubyInetAddress(numericnode); this.socketAddress = new InetSocketAddress(inetAddress, _port); throw runtime.newRaiseException(runtime.getClass("SocketError"), "unknown address family: " + family.toString()); this.socketAddress = new InetSocketAddress(inetAddress, _port); } catch (IOException ioe) { ioe.printStackTrace(); throw runtime.newIOErrorFromException(ioe);
public static SocketAddress sockaddrFromBytes(Ruby runtime, byte[] val) throws IOException { AddressFamily afamily = AddressFamily.valueOf(uint16(val[0], val[1])); if (afamily == null || afamily == AddressFamily.__UNKNOWN_CONSTANT__) { throw runtime.newArgumentError("can't resolve socket address of wrong type"); } int port; switch (afamily) { case AF_INET: port = uint16(val[2], val[3]); Inet4Address inet4Address = (Inet4Address)InetAddress.getByAddress(Helpers.subseq(val, 4, 4)); return new InetSocketAddress(inet4Address, port); case AF_INET6: port = uint16(val[2], val[3]); Inet6Address inet6Address = (Inet6Address)InetAddress.getByAddress(Helpers.subseq(val, 4, 16)); return new InetSocketAddress(inet6Address, port); case AF_UNIX: String path = new String(val, 2, val.length - 2); return new UnixSocketAddress(new File(path)); default: throw runtime.newArgumentError("can't resolve socket address of wrong type"); } }
@Override public SocketAddress call() throws UnknownHostException { if (port == 0) { // Hostname is a file path to the socket return new UnixSocketAddress(hostname); } else { return new InetSocketAddress(InetAddress.getByName(hostname), port); } } };
InetAddress inet = address.getAddress(); if (!inet.isLoopbackAddress() && !inet.isLinkLocalAddress() && !inet.isSiteLocalAddress()) throw new IOException("UnixSocket cannot connect to "+address.getHostString()); UnixSocketAddress unixAddress = new UnixSocketAddress( this._unixSocket ); channel = UnixSocketChannel.open( unixAddress );
@JRubyMethod public IRubyObject canonname(ThreadContext context) { if (socketAddress instanceof InetSocketAddress) { return context.runtime.newString(getInetSocketAddress().getAddress().getCanonicalHostName()); } else if (socketAddress instanceof UnixSocketAddress) { return context.runtime.newString(getUnixSocketAddress().path()); } throw context.runtime.newNotImplementedError("canonname not implemented for socket address: " + socketAddress); }
@JRubyMethod(optional = 1) public IRubyObject getnameinfo(ThreadContext context, IRubyObject[] args) { Ruby runtime = context.runtime; RubyString hostname; InetSocketAddress inet = getInetSocketAddress(); if (inet != null) { hostname = runtime.newString(inet.getHostName()); } else { UnixSocketAddress unix = getUnixSocketAddress(); hostname = runtime.newString(unix.path()); } RubyString rubyService = null; if (args.length > 0) { int flags = args[0].convertToInteger().getIntValue(); if ((flags & NameInfo.NI_NUMERICSERV.intValue()) != 0) { rubyService = runtime.newString(Integer.toString(getPort())); } } if (rubyService == null) { Service service = Service.getServiceByPort(getPort(), protocol.getName()); rubyService = runtime.newString(service.getName()); } return runtime.newArray(hostname, rubyService); }
@Override public void connect(SocketAddress endpoint, Integer timeout) throws IOException { this.inetSocketAddress = (InetSocketAddress) endpoint; super.connect(new UnixSocketAddress(path), timeout); }
protected void init_unixsock(Ruby runtime, IRubyObject _path, boolean server) { ByteList path = _path.convertToString().getByteList(); String fpath = Helpers.decodeByteList(runtime, path); int maxSize = 103; // Max size from Darwin, lowest common value we know of if (fpath.length() > 103) { throw runtime.newArgumentError("too long unix socket path (max: " + maxSize + "bytes)"); } try { if (server) { UnixServerSocketChannel channel = UnixServerSocketChannel.open(); UnixServerSocket socket = channel.socket(); // TODO: listen backlog socket.bind(new UnixSocketAddress(new File(fpath))); init_sock(runtime, channel, fpath); } else { File fpathFile = new File(fpath); if (!fpathFile.exists()) { throw runtime.newErrnoENOENTError("unix socket"); } UnixSocketChannel channel = UnixSocketChannel.open(); channel.connect(new UnixSocketAddress(fpathFile)); init_sock(runtime, channel); } } catch (IOException ioe) { throw runtime.newIOErrorFromException(ioe); } }
@Override public UnixSocketAddress receive(ByteBuffer src) throws IOException { UnixSocketAddress remote = new UnixSocketAddress(); int n = Native.recvfrom(getFD(), src, remote.getStruct()); if (n < 0) { throw new IOException(Native.getLastErrorString()); } return remote; }
@JRubyMethod public IRubyObject remote_address(ThreadContext context) { Ruby runtime = context.runtime; InetSocketAddress address = getInetRemoteSocket(); if (address != null) { SocketType socketType = SocketType.forChannel(getChannel()); return new Addrinfo(runtime, runtime.getClass("Addrinfo"), address, socketType.getSocketType(), socketType); } UnixSocketAddress unix = getUnixRemoteSocket(); if (unix != null) { return Addrinfo.unix(context, runtime.getClass("Addrinfo"), runtime.newString(unix.path())); } throw runtime.newErrnoENOTCONNError(); }
@JRubyMethod public IRubyObject peeraddr(ThreadContext context) { final Ruby runtime = context.runtime; final String _path = getUnixRemoteSocket().path(); final RubyString path = (_path == null) ? RubyString.newEmptyString(runtime) : runtime.newString(_path); return runtime.newArray( runtime.newString("AF_UNIX"), path ); }
@JRubyMethod public IRubyObject local_address(ThreadContext context) { Ruby runtime = context.runtime; InetSocketAddress address = getInetSocketAddress(); if (address != null) { SocketType socketType = SocketType.forChannel(getChannel()); return new Addrinfo(runtime, runtime.getClass("Addrinfo"), address, socketType.getSocketType(), socketType); } UnixSocketAddress unix = getUnixSocketAddress(); return Addrinfo.unix(context, runtime.getClass("Addrinfo"), runtime.newString(unix.path())); }
private void writeObject(ObjectOutputStream o) throws IOException { o.defaultWriteObject(); o.writeObject(path()); }
public boolean connect(UnixSocketAddress remote) throws IOException { remoteAddress = remote; if (!doConnect(remoteAddress.getStruct())) { state = State.CONNECTING; return false; } else { state = State.CONNECTED; return true; } }
public void bind(SocketAddress endpoint, int backlog) throws java.io.IOException { if (!(endpoint instanceof UnixSocketAddress)) { throw new IOException("Invalid address"); } UnixSocketAddress addr = (UnixSocketAddress) endpoint; if (Native.bind(fd, addr.getStruct(), addr.length()) < 0) { throw new IOException("bind failed: " + Native.getLastErrorString()); } if (Native.listen(fd, backlog) < 0) { throw new IOException("listen failed: " + Native.getLastErrorString()); } }
@JRubyMethod public IRubyObject inspect_sockaddr(ThreadContext context) { if (socketAddress instanceof UnixSocketAddress) { String path = getUnixSocketAddress().path(); return context.runtime.newString(path.startsWith("/") ? path : "UNIX " + path); } int port = getInetSocketAddress().getPort(); if (getInetAddress() instanceof Inet6Address) { String host = ipv6_ip(); String hostPort = port == 0 ? host : "[" + host + "]:" + port; return context.runtime.newString(hostPort); } String portString = port == 0 ? "" : ":" + port; String host = getInetSocketAddress().getAddress().getHostAddress(); return context.runtime.newString(host + portString); }
socketAddress = new UnixSocketAddress(new File(path.toString())); this.socketType = SocketType.UNIX; this.sock = Sock.SOCK_STREAM; if (inetAddress == null) inetAddress = getRubyInetAddress(numericnode); this.socketAddress = new InetSocketAddress(inetAddress, _port); throw runtime.newRaiseException(runtime.getClass("SocketError"), "unknown address family: " + family.toString()); this.socketAddress = new InetSocketAddress(inetAddress, _port); } catch (IOException ioe) { ioe.printStackTrace(); throw runtime.newIOErrorFromException(ioe);