Example #1
0
  /**
   * Ruby definition would look like:
   *
   * <p>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, Boolean reverse) {
            boolean is_ipv6 = address instanceof Inet6Address;
            boolean sock_stream = true;
            boolean sock_dgram = true;

            if (sock != null) {
              if (sock == SOCK_STREAM) {
                sock_dgram = false;

              } else if (sock == SOCK_DGRAM) {
                sock_stream = false;
              }
            }

            IRubyObject[] c;

            if (sock_dgram) {
              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, reverse));
              c[3] = runtime.newString(address.getHostAddress());
              c[4] = runtime.newFixnum(is_ipv6 ? PF_INET6 : PF_INET);
              c[5] = runtime.newFixnum(SOCK_DGRAM);
              c[6] = runtime.newFixnum(IPPROTO_UDP);
              l.add(runtime.newArrayNoCopy(c));
            }

            if (sock_stream) {
              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, reverse));
              c[3] = runtime.newString(address.getHostAddress());
              c[4] = runtime.newFixnum(is_ipv6 ? PF_INET6 : PF_INET);
              c[5] = runtime.newFixnum(SOCK_STREAM);
              c[6] = runtime.newFixnum(IPPROTO_TCP);
              l.add(runtime.newArrayNoCopy(c));
            }
          }
        });

    return runtime.newArray(l);
  }
Example #2
0
  public static List<Addrinfo> getaddrinfoList(ThreadContext context, IRubyObject[] args) {
    final Ruby runtime = context.runtime;
    final List<Addrinfo> l = new ArrayList<Addrinfo>();

    buildAddrinfoList(
        context,
        args,
        new AddrinfoCallback() {
          @Override
          public void addrinfo(InetAddress address, int port, Sock sock, Boolean reverse) {
            boolean sock_stream = true;
            boolean sock_dgram = true;

            if (sock != null) {
              if (sock == SOCK_STREAM) {
                sock_dgram = false;

              } else if (sock == SOCK_DGRAM) {
                sock_stream = false;
              }
            }

            if (sock_dgram) {
              l.add(
                  new Addrinfo(
                      runtime, runtime.getClass("Addrinfo"), address, port, SocketType.DATAGRAM));
            }

            if (sock_stream) {
              l.add(
                  new Addrinfo(
                      runtime, runtime.getClass("Addrinfo"), address, port, SocketType.SOCKET));
            }
          }
        });

    return l;
  }