/** * Opens a server TCP connection to clients. Creates, binds, and listens * * @param port local TCP port to listen on * @param backlog listen backlog. * @return a native handle to the network connection. * @throws IOException */ public int openServer(int port, int backlog) throws IOException { int fd = -1; fd = sockets.socket(Socket.AF_INET, Socket.SOCK_STREAM, 0); if (DEBUG) { System.err.println("Socket.socket() = " + fd); } if (fd < 0) { throw newError(fd, "socket create"); } set_blocking_flags(fd, /*is_blocking*/ false); setSockOpt(fd, Socket.SO_REUSEADDR, 1); setSockOpt(fd, Socket.IPPROTO_TCP, Socket.TCP_NODELAY, 1); Socket.sockaddr_in local_sin = new Socket.sockaddr_in(); local_sin.sin_family = Socket.AF_INET; local_sin.sin_port = Inet.htons((short) port); local_sin.sin_addr = Socket.INADDR_ANY; if (DEBUG) { System.err.println("Socket.bind(" + fd + ", " + local_sin + ")"); } if (sockets.bind(fd, local_sin, local_sin.size()) < 0) { throw newError(fd, "bind"); } if (sockets.listen(fd, backlog) < 0) { throw newError(fd, "listen"); } return fd; }
/** * set a socket option * * @param socket socket descriptor * @param level Socket.SOL_SOCKET, etc. * @param option_name * @param option_value new value * @throws IOException on error */ public void setSockOpt(int socket, int level, int option_name, int option_value) throws IOException { IntByReference value = new IntByReference(option_value); if (false) Assert.that(option_value == value.getValue()); if (DEBUG) { System.out.println( "setSockOpt(" + socket + ", " + level + ", " + option_name + ", " + option_value + ")"); } int err = sockets.setsockopt(socket, level, option_name, value, 4); if (false) Assert.that(option_value == value.getValue()); value.free(); LibCUtil.errCheckNeg(err); if (DEBUG) { int newValue = getSockOpt(socket, level, option_name); if (option_value != newValue) { System.out.println( "FAILED: setSockOpt(" + socket + ", " + level + ", " + option_name + ", " + option_value + ")"); System.err.println(" Ended with: " + newValue); } } }
/** * Takes an IPv4 Internet address and returns string representing the address in `.' notation * * @param in the opaque bytes of an IPv4 "struct in_addr" * @return String */ public String inet_ntop(int in) { Pointer charBuf = new Pointer(Socket.INET_ADDRSTRLEN); IntByReference addrBuf = new IntByReference(in); // the addr is passed by value (to handle IPv6) String result = sockets.inet_ntop(Socket.AF_INET, addrBuf, charBuf, Socket.INET_ADDRSTRLEN); addrBuf.free(); charBuf.free(); return result; }
/** @inheritDoc */ public void close(int fd) throws IOException { // NOTE: this would block the VM. A real implementation should // make this a async native method. sockets.shutdown(fd, 2); libc.close(fd); if (DEBUG) { System.out.println("close(" + fd + ")"); } }
/** * Accept client connections on server socket fd. Blocks until a client connects. * * @param fd open server socket. See {@link #openServer}. * @return a native handle to the network connection. * @throws IOException */ public int accept(int fd) throws IOException { Socket.sockaddr_in remote_sin = new Socket.sockaddr_in(); IntByReference address_len = new IntByReference(4); int newSocket; try { if (DEBUG) { System.err.println("Socket.accept(" + fd + ", " + remote_sin + ")..."); } newSocket = sockets.accept(fd, remote_sin, address_len); if (newSocket < 0) { if (DEBUG) { System.err.println("Need to block for accept..."); } int err_code = LibCUtil.errno(); if (err_code == LibC.EAGAIN || err_code == LibC.EWOULDBLOCK) { VMThread.getSystemEvents().waitForReadEvent(fd); newSocket = sockets.accept(fd, remote_sin, address_len); if (newSocket < 0) { throw newError(fd, "accept"); } } else { // BUG! throw newError(fd, "accept"); } } } finally { address_len.free(); } set_blocking_flags(newSocket, /*is_blocking*/ false); // we could read info about client from remote_sin, but don't need to. if (DEBUG) { System.err.println(" Socket.accept(...) = " + newSocket); } return newSocket; }
/** Read errno, try to clean up fd, and create exception. */ private IOException newError(int fd, String msg) { if (DEBUG) { VM.print(msg); VM.print(": errno: "); } int err_code = LibCUtil.errno(); if (DEBUG) { VM.print(err_code); VM.println(); } sockets.shutdown(fd, 2); libc.close(fd); return new IOException(" errno: " + err_code + " on fd: " + fd + " during " + msg); }
/** * get a socket option * * @param socket socket descriptor * @param level Socket.SOL_SOCKET, etc. * @param option_name * @return socket option value * @throws IOException on error */ public int getSockOpt(int socket, int level, int option_name) throws IOException { IntByReference value = new IntByReference(0); IntByReference opt_len = new IntByReference(4); if (DEBUG) { System.out.println("getsockopt(" + socket + ", " + level + ", " + option_name + ")"); } int err = sockets.getsockopt(socket, level, option_name, value, opt_len); if (DEBUG) { System.out.println( " returned value: " + value.getValue() + ", size: " + opt_len.getValue()); } int result = value.getValue(); value.free(); if (false) Assert.that(opt_len.getValue() == 4); opt_len.free(); LibCUtil.errCheckNeg(err); return result; }
/** @inheritDoc */ public int open(String hostname, int port, int mode) throws IOException { // init_sockets(); win32 only int fd = -1; fd = sockets.socket(Socket.AF_INET, Socket.SOCK_STREAM, 0); if (DEBUG) { System.err.println("Socket.socket() = " + fd); } if (fd < 0) { throw newError(fd, "socket create"); } set_blocking_flags(fd, /*is_blocking*/ false); NetDB.hostent phostent; // hostname is always NUL terminated. See socket/Protocol.java for detail. phostent = NetDB.INSTANCE.gethostbyname(hostname); if (phostent == null) { throw newError(fd, "gethostbyname (herrono = " + NetDB.INSTANCE.h_errno() + ")"); } Socket.sockaddr_in destination_sin = new Socket.sockaddr_in(); destination_sin.sin_family = Socket.AF_INET; destination_sin.sin_port = Inet.htons((short) port); destination_sin.sin_addr = phostent.h_addr_list[0]; if (DEBUG) { System.err.println("Socket.sockaddr_in: " + destination_sin); System.err.println("connect: hostname: " + hostname + " port: " + port + " mode: " + mode); } if (sockets.connect(fd, destination_sin, destination_sin.size()) < 0) { int err_code = LibCUtil.errno(); if (err_code == LibC.EINPROGRESS || err_code == LibC.EWOULDBLOCK) { // When the socket is ready for connect, it becomes *writable* // (according to BSD socket spec of select()) VMThread.getSystemEvents().waitForWriteEvent(fd); int errno = getSockOpt(fd, Socket.SO_ERROR); if (errno != 0) { String msg; if (errno == LibC.ECONNREFUSED) { msg = "connect refused"; } else { msg = String.valueOf(errno); } throw new IOException("ConnectException: " + msg); } if (DEBUG) { System.err.println("connect back fd = " + fd); } } else { throw newError(fd, "connect"); } } try { setSockOpt(fd, Socket.IPPROTO_TCP, Socket.TCP_NODELAY, 1); } catch (IOException ex) { } return fd; }