예제 #1
0
  protected IRubyObject getSocknameCommon(ThreadContext context, String caller) {
    try {
      InetSocketAddress sock = getSocketAddress();

      if (null == sock) {
        return Sockaddr.pack_sockaddr_in(context, 0, "0.0.0.0");

      } else {
        return Sockaddr.pack_sockaddr_in(context, sock);
      }

    } catch (BadDescriptorException e) {
      throw context.runtime.newErrnoEBADFError();
    }
  }
예제 #2
0
  public static IRubyObject doAcceptNonblock(RubySocket sock, ThreadContext context, boolean ex) {
    try {
      Channel channel = sock.getChannel();
      if (channel instanceof SelectableChannel) {
        SelectableChannel selectable = (SelectableChannel) channel;

        synchronized (selectable.blockingLock()) {
          boolean oldBlocking = selectable.isBlocking();

          try {
            selectable.configureBlocking(false);

            IRubyObject socket = doAccept(sock, context, ex);
            if (!(socket instanceof RubySocket)) return socket;
            SocketChannel socketChannel = (SocketChannel) ((RubySocket) socket).getChannel();
            InetSocketAddress addr =
                (InetSocketAddress) socketChannel.socket().getRemoteSocketAddress();

            return context.runtime.newArray(
                socket, Sockaddr.packSockaddrFromAddress(context, addr));
          } finally {
            selectable.configureBlocking(oldBlocking);
          }
        }
      } else {
        throw context.runtime.newErrnoENOPROTOOPTError();
      }
    } catch (IOException e) {
      throw sockerr(context.runtime, e.getLocalizedMessage(), e);
    }
  }
예제 #3
0
  public static IRubyObject pack_sockaddr_in(
      ThreadContext context, IRubyObject port, IRubyObject host) {
    int portNum =
        port instanceof RubyString
            ? Integer.parseInt(port.convertToString().toString())
            : RubyNumeric.fix2int(port);

    return Sockaddr.pack_sockaddr_in(
        context, portNum, host.isNil() ? null : host.convertToString().toString());
  }
예제 #4
0
  public static IRubyObject gethostbyaddr(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;
    IRubyObject[] ret = new IRubyObject[4];

    ret[0] =
        runtime.newString(
            Sockaddr.addressFromString(runtime, args[0].convertToString().toString())
                .getCanonicalHostName());
    ret[1] = runtime.newArray();
    ret[2] = runtime.newFixnum(2); // AF_INET
    ret[3] = args[0];

    return runtime.newArrayNoCopy(ret);
  }
예제 #5
0
  @JRubyMethod()
  public IRubyObject bind(ThreadContext context, IRubyObject addr, IRubyObject backlog) {
    final InetSocketAddress iaddr;

    if (addr instanceof Addrinfo) {
      Addrinfo addrInfo = (Addrinfo) addr;
      if (!addrInfo.ip_p(context).isTrue()) {
        throw context.runtime.newTypeError("not an INET or INET6 address: " + addrInfo);
      }
      iaddr = new InetSocketAddress(addrInfo.getInetAddress().getHostAddress(), addrInfo.getPort());
    } else {
      iaddr = Sockaddr.addressFromSockaddr_in(context, addr);
    }
    doBind(context, getChannel(), iaddr, RubyFixnum.fix2int(backlog));

    return RubyFixnum.zero(context.runtime);
  }
예제 #6
0
 public static IRubyObject unpack_sockaddr_in(ThreadContext context, IRubyObject addr) {
   return Sockaddr.unpack_sockaddr_in(context, addr);
 }