@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 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(name = {"to_sockaddr", "to_s"}) public IRubyObject to_sockaddr(ThreadContext context) { switch (getAddressFamily()) { case AF_INET: case AF_INET6: InetAddress inetAddress = ((InetSocketAddress) socketAddress).getAddress(); int port = ((InetSocketAddress) socketAddress).getPort(); return Sockaddr.pack_sockaddr_in(context, port, inetAddress.getHostAddress()); case AF_UNIX: return Sockaddr.pack_sockaddr_un(context, getUnixSocketAddress().path()); case AF_UNSPEC: ByteArrayOutputStream bufS = new ByteArrayOutputStream(); DataOutputStream ds = new DataOutputStream(bufS); try { // struct sockaddr_ll { (see: man 7 packet) ds.writeShort(swapShortEndian(AF_PACKET)); // unsigned short sll_family; /* Always AF_PACKET */ ds.writeShort(0); // unsigned short sll_protocol; /* Physical layer protocol */ ds.writeInt(swapIntEndian(networkInterface.getIndex())); // int sll_ifindex; /* Interface number */ ds.writeShort(swapShortEndian(hatype())); // unsigned short sll_hatype; /* ARP hardware type */ ds.writeByte(PACKET_HOST); // unsigned char sll_pkttype; /* Packet type */ byte[] hw = hwaddr(); ds.writeByte(hw.length); // unsigned char sll_halen; /* Length of address */ ds.write(hw); // unsigned char sll_addr[8]; /* Physical layer address */ } catch (IOException e) { throw SocketUtils.sockerr(context.runtime, "to_sockaddr: " + e.getMessage()); } return context.runtime.newString(new ByteList(bufS.toByteArray(), false)); } return context.nil; }
@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); }
@JRubyMethod(name = {"to_sockaddr", "to_s"}) public IRubyObject to_sockaddr(ThreadContext context) { switch (getAddressFamily()) { case AF_INET: case AF_INET6: InetAddress inetAddress = ((InetSocketAddress) socketAddress).getAddress(); int port = ((InetSocketAddress) socketAddress).getPort(); return Sockaddr.pack_sockaddr_in(context, port, inetAddress.getHostAddress()); case AF_UNIX: return Sockaddr.pack_sockaddr_un(context, getUnixSocketAddress().path()); case AF_UNSPEC: ByteArrayOutputStream bufS = new ByteArrayOutputStream(); DataOutputStream ds = new DataOutputStream(bufS); try { // struct sockaddr_ll { (see: man 7 packet) ds.writeShort(swapShortEndian(AF_PACKET)); // unsigned short sll_family; /* Always AF_PACKET */ ds.writeShort(0); // unsigned short sll_protocol; /* Physical layer protocol */ ds.writeInt(swapIntEndian(networkInterface.getIndex())); // int sll_ifindex; /* Interface number */ ds.writeShort(swapShortEndian(hatype())); // unsigned short sll_hatype; /* ARP hardware type */ ds.writeByte(PACKET_HOST); // unsigned char sll_pkttype; /* Packet type */ byte[] hw = hwaddr(); ds.writeByte(hw.length); // unsigned char sll_halen; /* Length of address */ ds.write(hw); // unsigned char sll_addr[8]; /* Physical layer address */ } catch (IOException e) { throw SocketUtils.sockerr(context.runtime, "to_sockaddr: " + e.getMessage()); } return context.runtime.newString(new ByteList(bufS.toByteArray(), false)); } return context.nil; }
@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); }
@JRubyMethod public IRubyObject unix_path(ThreadContext context) { if (getAddressFamily() != AF_UNIX) { throw SocketUtils.sockerr(context.runtime, "need AF_UNIX address"); } return context.runtime.newString(getUnixSocketAddress().path()); }
@JRubyMethod public IRubyObject unix_path(ThreadContext context) { if (getAddressFamily() != AF_UNIX) { throw SocketUtils.sockerr(context.runtime, "need AF_UNIX address"); } return context.runtime.newString(getUnixSocketAddress().path()); }
@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); }
@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); }