Index: uspace/srv/hid/rfb/rfb.c
===================================================================
--- uspace/srv/hid/rfb/rfb.c	(revision 870f78cb5edbf47ff1c7b5d88fdcddd374690118)
+++ uspace/srv/hid/rfb/rfb.c	(revision fab2746a5541d35bc79cbed917f66153231e4a4b)
@@ -31,4 +31,7 @@
 #include <stdlib.h>
 #include <fibril_synch.h>
+#include <inet/addr.h>
+#include <inet/endpoint.h>
+#include <inet/tcp.h>
 #include <inttypes.h>
 #include <str.h>
@@ -38,9 +41,15 @@
 #include <io/log.h>
 
-#include <net/in.h>
-#include <net/inet.h>
-#include <net/socket.h>
-
 #include "rfb.h"
+
+static void rfb_new_conn(tcp_listener_t *, tcp_conn_t *);
+
+static tcp_listen_cb_t listen_cb = {
+	.new_conn = rfb_new_conn
+};
+
+static tcp_cb_t conn_cb = {
+	.connected = NULL
+};
 
 /** Buffer for receiving the request. */
@@ -53,15 +62,18 @@
 
 /** Receive one character (with buffering) */
-static int recv_char(int fd, char *c)
-{
+static int recv_char(tcp_conn_t *conn, char *c)
+{
+	size_t nrecv;
+	int rc;
+
 	if (rbuf_out == rbuf_in) {
 		rbuf_out = 0;
 		rbuf_in = 0;
 		
-		ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);
-		if (rc <= 0)
+		rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv);
+		if (rc != EOK)
 			return rc;
 		
-		rbuf_in = rc;
+		rbuf_in = nrecv;
 	}
 	
@@ -71,8 +83,8 @@
 
 /** Receive count characters (with buffering) */
-static int recv_chars(int fd, char *c, size_t count)
+static int recv_chars(tcp_conn_t *conn, char *c, size_t count)
 {
 	for (size_t i = 0; i < count; i++) {
-		int rc = recv_char(fd, c);
+		int rc = recv_char(conn, c);
 		if (rc != EOK)
 			return rc;
@@ -82,9 +94,9 @@
 }
 
-static int recv_skip_chars(int fd, size_t count)
+static int recv_skip_chars(tcp_conn_t *conn, size_t count)
 {
 	for (size_t i = 0; i < count; i++) {
 		char c;
-		int rc = recv_char(fd, &c);
+		int rc = recv_char(conn, &c);
 		if (rc != EOK)
 			return rc;
@@ -204,8 +216,8 @@
 }
 
-static int recv_message(int conn_sd, char type, void *buf, size_t size)
+static int recv_message(tcp_conn_t *conn, char type, void *buf, size_t size)
 {
 	memcpy(buf, &type, 1);
-	return recv_chars(conn_sd, ((char *) buf) + 1, size -1);
+	return recv_chars(conn, ((char *) buf) + 1, size -1);
 }
 
@@ -477,5 +489,6 @@
 }
 
-static int rfb_send_framebuffer_update(rfb_t *rfb, int conn_sd, bool incremental)
+static int rfb_send_framebuffer_update(rfb_t *rfb, tcp_conn_t *conn,
+    bool incremental)
 {
 	fibril_mutex_lock(&rfb->lock);
@@ -540,5 +553,5 @@
 	
 	if (!rfb->pixel_format.true_color) {
-		int rc = send(conn_sd, send_palette, send_palette_size, 0);
+		int rc = tcp_conn_send(conn, send_palette, send_palette_size);
 		if (rc != EOK) {
 			free(buf);
@@ -547,5 +560,5 @@
 	}
 	
-	int rc = send(conn_sd, buf, buf_size, 0);
+	int rc = tcp_conn_send(conn, buf, buf_size);
 	free(buf);
 	
@@ -580,8 +593,8 @@
 }
 
-static void rfb_socket_connection(rfb_t *rfb, int conn_sd)
+static void rfb_socket_connection(rfb_t *rfb, tcp_conn_t *conn)
 {
 	/* Version handshake */
-	int rc = send(conn_sd, "RFB 003.008\n", 12, 0);
+	int rc = tcp_conn_send(conn, "RFB 003.008\n", 12);
 	if (rc != EOK) {
 		log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server version %d", rc);
@@ -590,5 +603,5 @@
 	
 	char client_version[12];
-	rc = recv_chars(conn_sd, client_version, 12);
+	rc = recv_chars(conn, client_version, 12);
 	if (rc != EOK) {
 		log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client version: %d", rc);
@@ -607,5 +620,5 @@
 	sec_types[0] = 1; /* length */
 	sec_types[1] = RFB_SECURITY_NONE;
-	rc = send(conn_sd, sec_types, 2, 0);
+	rc = tcp_conn_send(conn, sec_types, 2);
 	if (rc != EOK) {
 		log_msg(LOG_DEFAULT, LVL_WARN,
@@ -615,5 +628,5 @@
 	
 	char selected_sec_type = 0;
-	rc = recv_char(conn_sd, &selected_sec_type);
+	rc = recv_char(conn, &selected_sec_type);
 	if (rc != EOK) {
 		log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving security type: %d", rc);
@@ -626,5 +639,5 @@
 	}
 	uint32_t security_result = RFB_SECURITY_HANDSHAKE_OK;
-	rc = send(conn_sd, &security_result, sizeof(uint32_t), 0);
+	rc = tcp_conn_send(conn, &security_result, sizeof(uint32_t));
 	if (rc != EOK) {
 		log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending security result: %d", rc);
@@ -634,5 +647,5 @@
 	/* Client init */
 	char shared_flag;
-	rc = recv_char(conn_sd, &shared_flag);
+	rc = recv_char(conn, &shared_flag);
 	if (rc != EOK) {
 		log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client init: %d", rc);
@@ -657,5 +670,5 @@
 	memcpy(server_init->name, rfb->name, name_length);
 	fibril_mutex_unlock(&rfb->lock);
-	rc = send(conn_sd, server_init, msg_length, 0);
+	rc = tcp_conn_send(conn, server_init, msg_length);
 	if (rc != EOK) {
 		log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server init: %d", rc);
@@ -665,5 +678,5 @@
 	while (true) {
 		char message_type = 0;
-		rc = recv_char(conn_sd, &message_type);
+		rc = recv_char(conn, &message_type);
 		if (rc != EOK) {
 			log_msg(LOG_DEFAULT, LVL_WARN,
@@ -680,5 +693,5 @@
 		switch (message_type) {
 		case RFB_CMSG_SET_PIXEL_FORMAT:
-			recv_message(conn_sd, message_type, &spf, sizeof(spf));
+			recv_message(conn, message_type, &spf, sizeof(spf));
 			rfb_pixel_format_to_host(&spf.pixel_format, &spf.pixel_format);
 			log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetPixelFormat message");
@@ -690,10 +703,10 @@
 			break;
 		case RFB_CMSG_SET_ENCODINGS:
-			recv_message(conn_sd, message_type, &se, sizeof(se));
+			recv_message(conn, message_type, &se, sizeof(se));
 			rfb_set_encodings_to_host(&se, &se);
 			log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetEncodings message");
 			for (uint16_t i = 0; i < se.count; i++) {
 				int32_t encoding = 0;
-				rc = recv_chars(conn_sd, (char *) &encoding, sizeof(int32_t));
+				rc = recv_chars(conn, (char *) &encoding, sizeof(int32_t));
 				if (rc != EOK)
 					return;
@@ -707,25 +720,25 @@
 			break;
 		case RFB_CMSG_FRAMEBUFFER_UPDATE_REQUEST:
-			recv_message(conn_sd, message_type, &fbur, sizeof(fbur));
+			recv_message(conn, message_type, &fbur, sizeof(fbur));
 			rfb_framebuffer_update_request_to_host(&fbur, &fbur);
 			log_msg(LOG_DEFAULT, LVL_DEBUG2,
 			    "Received FramebufferUpdateRequest message");
-			rfb_send_framebuffer_update(rfb, conn_sd, fbur.incremental);
+			rfb_send_framebuffer_update(rfb, conn, fbur.incremental);
 			break;
 		case RFB_CMSG_KEY_EVENT:
-			recv_message(conn_sd, message_type, &ke, sizeof(ke));
+			recv_message(conn, message_type, &ke, sizeof(ke));
 			rfb_key_event_to_host(&ke, &ke);
 			log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received KeyEvent message");
 			break;
 		case RFB_CMSG_POINTER_EVENT:
-			recv_message(conn_sd, message_type, &pe, sizeof(pe));
+			recv_message(conn, message_type, &pe, sizeof(pe));
 			rfb_pointer_event_to_host(&pe, &pe);
 			log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received PointerEvent message");
 			break;
 		case RFB_CMSG_CLIENT_CUT_TEXT:
-			recv_message(conn_sd, message_type, &cct, sizeof(cct));
+			recv_message(conn, message_type, &cct, sizeof(cct));
 			rfb_client_cut_text_to_host(&cct, &cct);
 			log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received ClientCutText message");
-			recv_skip_chars(conn_sd, cct.length);
+			recv_skip_chars(conn, cct.length);
 			break;
 		default:
@@ -737,64 +750,39 @@
 }
 
-int rfb_listen(rfb_t *rfb, uint16_t port) {
-	struct sockaddr_in addr;
-	
-	addr.sin_family = AF_INET;
-	addr.sin_port = htons(port);
-	
-	int rc = inet_pton(AF_INET, "127.0.0.1", (void *)
-	    &addr.sin_addr.s_addr);
+int rfb_listen(rfb_t *rfb, uint16_t port)
+{
+	tcp_t *tcp = NULL;
+	tcp_listener_t *lst = NULL;
+	inet_ep_t ep;
+	int rc;
+	
+	inet_ep_init(&ep);
+	ep.port = port;
+	
+	rc = tcp_listener_create(tcp, &ep, &listen_cb, rfb, &conn_cb, rfb,
+	    &lst);
 	if (rc != EOK) {
-		log_msg(LOG_DEFAULT, LVL_ERROR, "Error parsing network address (%s)",
-		    str_error(rc));
-		return rc;
-	}
-
-	int listen_sd = socket(PF_INET, SOCK_STREAM, 0);
-	if (listen_sd < 0) {
-		log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating listening socket (%s)",
-		    str_error(listen_sd));
-		return rc;
-	}
-	
-	rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));
-	if (rc != EOK) {
-		log_msg(LOG_DEFAULT, LVL_ERROR, "Error binding socket (%s)",
-		    str_error(rc));
-		return rc;
-	}
-	
-	rc = listen(listen_sd, 2);
-	if (rc != EOK) {
-		log_msg(LOG_DEFAULT, LVL_ERROR, "listen() failed (%s)", str_error(rc));
-		return rc;
-	}
-	
-	rfb->listen_sd = listen_sd;
+		log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating listener.\n");
+		goto error;
+	}
+	
+	rfb->tcp = tcp;
+	rfb->lst = lst;
 	
 	return EOK;
-}
-
-void rfb_accept(rfb_t *rfb)
-{
-	while (true) {
-		struct sockaddr_in raddr;
-		socklen_t raddr_len = sizeof(raddr);
-		int conn_sd = accept(rfb->listen_sd, (struct sockaddr *) &raddr,
-		    &raddr_len);
-		
-		if (conn_sd < 0) {
-			log_msg(LOG_DEFAULT, LVL_WARN, "accept() failed (%s)",
-			    str_error(conn_sd));
-			continue;
-		}
-		
-		log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection accepted");
-		
-		rbuf_out = 0;
-		rbuf_in = 0;
-		
-		rfb_socket_connection(rfb, conn_sd);
-		closesocket(conn_sd);
-	}
-}
+error:
+	tcp_listener_destroy(lst);
+	tcp_destroy(tcp);
+	return rc;
+}
+
+static void rfb_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
+{
+	rfb_t *rfb = (rfb_t *)tcp_listener_userptr(lst);
+	log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection accepted");
+
+	rbuf_out = 0;
+	rbuf_in = 0;
+
+	rfb_socket_connection(rfb, conn);
+}
