//def self.getaddrinfo(host, port, family = nil, socktype = nil, protocol = nil, flags = nil)
@JRubyMethod(required = 2, optional = 4, meta = true)
public static IRubyObject getaddrinfo(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
args = Arity.scanArgs(context.getRuntime(),args,2,4);
try {
Ruby r = context.getRuntime();
IRubyObject host = args[0];
IRubyObject port = args[1];
if(port instanceof RubyString) {
port = getservbyname(context, recv, new IRubyObject[]{port});
}
//IRubyObject family = args[2];
IRubyObject socktype = args[3];
//IRubyObject protocol = args[4];
//IRubyObject flags = args[5];
boolean sock_stream = true;
boolean sock_dgram = true;
if(!socktype.isNil()) {
int val = RubyNumeric.fix2int(socktype);
if(val == 1) {
sock_dgram = false;
} else if(val == 2) {
sock_stream = false;
}
}
InetAddress[] addrs = InetAddress.getAllByName(host.isNil() ? null : host.convertToString().toString());
List<IRubyObject> l = new ArrayList<IRubyObject>();
for(int i=0;i<addrs.length;i++) {
IRubyObject[] c;
if(sock_dgram) {
c = new IRubyObject[7];
c[0] = r.newString("AF_INET");
c[1] = port;
c[2] = r.newString(addrs[i].getCanonicalHostName());
c[3] = r.newString(addrs[i].getHostAddress());
c[4] = r.newFixnum(PF_INET);
c[5] = r.newFixnum(SOCK_DGRAM);
c[6] = r.newFixnum(IPPROTO_UDP);
l.add(r.newArrayNoCopy(c));
}
if(sock_stream) {
c = new IRubyObject[7];
c[0] = r.newString("AF_INET");
c[1] = port;
c[2] = r.newString(addrs[i].getCanonicalHostName());
c[3] = r.newString(addrs[i].getHostAddress());
c[4] = r.newFixnum(PF_INET);
c[5] = r.newFixnum(SOCK_STREAM);
c[6] = r.newFixnum(IPPROTO_TCP);
l.add(r.newArrayNoCopy(c));
}
}
return r.newArray(l);
} catch(UnknownHostException e) {
throw sockerr(context.getRuntime(), "getaddrinfo: name or service not known");
}
}