Index: uspace/srv/net/tl/tcp/tcp.c
===================================================================
--- uspace/srv/net/tl/tcp/tcp.c	(revision 0578271269331fb048f182ef5c3d26dae37c84ea)
+++ uspace/srv/net/tl/tcp/tcp.c	(revision 89e57ceef2a2fce4e3518b2109ffcd674896bf36)
@@ -47,4 +47,5 @@
 #include <stdio.h>
 #include <errno.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
@@ -234,5 +235,5 @@
 int tcp_initialize(async_client_conn_t client_connection)
 {
-	int rc;
+	ERROR_DECLARE;
 
 	assert(client_connection);
@@ -245,24 +246,18 @@
 	tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
 	    SERVICE_TCP, client_connection);
-	if (tcp_globals.ip_phone < 0) {
-		fibril_rwlock_write_unlock(&tcp_globals.lock);
+	if (tcp_globals.ip_phone < 0)
 		return tcp_globals.ip_phone;
-	}
 	
-	rc = socket_ports_initialize(&tcp_globals.sockets);
-	if (rc != EOK)
-		goto out;
-
-	rc = packet_dimensions_initialize(&tcp_globals.dimensions);
-	if (rc != EOK) {
+	ERROR_PROPAGATE(socket_ports_initialize(&tcp_globals.sockets));
+	if (ERROR_OCCURRED(packet_dimensions_initialize(
+	    &tcp_globals.dimensions))) {
 		socket_ports_destroy(&tcp_globals.sockets);
-		goto out;
+		return ERROR_CODE;
 	}
 
 	tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
-
-out:
 	fibril_rwlock_write_unlock(&tcp_globals.lock);
-	return rc;
+
+	return EOK;
 }
 
@@ -271,5 +266,5 @@
     services_t error)
 {
-	int rc;
+	ERROR_DECLARE;
 
 	if (receiver != SERVICE_TCP)
@@ -277,15 +272,16 @@
 
 	fibril_rwlock_write_lock(&tcp_globals.lock);
-	rc = tcp_process_packet(device_id, packet, error);
-	if (rc != EOK)
+	if (ERROR_OCCURRED(tcp_process_packet(device_id, packet, error)))
 		fibril_rwlock_write_unlock(&tcp_globals.lock);
 
-	printf("receive %d \n", rc);
-
-	return rc;
+	printf("receive %d \n", ERROR_CODE);
+
+	return ERROR_CODE;
 }
 
 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error)
 {
+	ERROR_DECLARE;
+
 	size_t length;
 	size_t offset;
@@ -303,5 +299,4 @@
 	struct sockaddr *dest;
 	size_t addrlen;
-	int rc;
 
 	switch (error) {
@@ -316,7 +311,6 @@
 
 		length = (size_t) result;
-		rc = packet_trim(packet, length, 0);
-		if (rc != EOK)
-			return tcp_release_and_return(packet, rc);
+		if (ERROR_OCCURRED(packet_trim(packet, length, 0)))
+			return tcp_release_and_return(packet, ERROR_CODE);
 		break;
 	default:
@@ -339,7 +333,6 @@
 
 	// trim all but TCP header
-	rc = packet_trim(packet, offset, 0);
-	if (rc != EOK)
-		return tcp_release_and_return(packet, rc);
+	if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
+		return tcp_release_and_return(packet, ERROR_CODE);
 
 	// get tcp header
@@ -357,7 +350,7 @@
 	addrlen = (size_t) result;
 
-	rc = tl_set_address_port(src, addrlen, ntohs(header->source_port));
-	if (rc != EOK)
-		return tcp_release_and_return(packet, rc);
+	if (ERROR_OCCURRED(tl_set_address_port(src, addrlen,
+	    ntohs(header->source_port)))) 
+		return tcp_release_and_return(packet, ERROR_CODE);
 	
 	// find the destination socket
@@ -420,19 +413,16 @@
 		}
 
-		rc = ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen,
-		    dest, addrlen, total_length, &socket_data->pseudo_header,
-		    &socket_data->headerlen);
-		if (rc != EOK) {
+		if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src,
+		    addrlen, dest, addrlen, total_length,
+		    &socket_data->pseudo_header, &socket_data->headerlen))) {
 			fibril_rwlock_write_unlock(socket_data->local_lock);
-			return tcp_release_and_return(packet, rc);
-		}
-	} else {
-		rc = ip_client_set_pseudo_header_data_length(
-		    socket_data->pseudo_header, socket_data->headerlen,
-		    total_length);
-		if (rc != EOK) {
-			fibril_rwlock_write_unlock(socket_data->local_lock);
-			return tcp_release_and_return(packet, rc);
-		}
+			return tcp_release_and_return(packet, ERROR_CODE);
+		}
+
+	} else if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
+	    socket_data->pseudo_header, socket_data->headerlen,
+	    total_length))) {
+		fibril_rwlock_write_unlock(socket_data->local_lock);
+		return tcp_release_and_return(packet, ERROR_CODE);
 	}
 	
@@ -444,7 +434,6 @@
 		fibril_rwlock_write_unlock(socket_data->local_lock);
 
-		rc = tl_prepare_icmp_packet(tcp_globals.net_phone,
-		    tcp_globals.icmp_phone, packet, error);
-		if (rc == EOK) {
+		if (ERROR_NONE(tl_prepare_icmp_packet(tcp_globals.net_phone,
+		    tcp_globals.icmp_phone, packet, error))) {
 			// checksum error ICMP
 			icmp_parameter_problem_msg(tcp_globals.icmp_phone,
@@ -463,13 +452,14 @@
 	switch (socket_data->state) {
 	case TCP_SOCKET_LISTEN:
-		rc = tcp_process_listen(socket, socket_data, header, packet,
-		    src, dest, addrlen);
+		ERROR_CODE = tcp_process_listen(socket, socket_data, header,
+		    packet, src, dest, addrlen);
 		break;
 	case TCP_SOCKET_SYN_RECEIVED:
-		rc = tcp_process_syn_received(socket, socket_data, header,
-		    packet);
+		ERROR_CODE = tcp_process_syn_received(socket, socket_data,
+		    header, packet);
 		break;
 	case TCP_SOCKET_SYN_SENT:
-		rc = tcp_process_syn_sent(socket, socket_data, header, packet);
+		ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header,
+		    packet);
 		break;
 	case TCP_SOCKET_FIN_WAIT_1:
@@ -482,6 +472,6 @@
 		// ack releasing the socket gets processed later
 	case TCP_SOCKET_ESTABLISHED:
-		rc = tcp_process_established(socket, socket_data, header,
-		    packet, fragments, total_length);
+		ERROR_CODE = tcp_process_established(socket, socket_data,
+		    header, packet, fragments, total_length);
 		break;
 	default:
@@ -489,7 +479,7 @@
 	}
 
-	if (rc != EOK) {
+	if (ERROR_CODE != EOK) {
+		printf("process %d\n", ERROR_CODE);
 		fibril_rwlock_write_unlock(socket_data->local_lock);
-		printf("process %d\n", rc);
 	}
 
@@ -501,4 +491,6 @@
     tcp_header_ref header, packet_t packet, int fragments, size_t total_length)
 {
+	ERROR_DECLARE;
+
 	packet_t next_packet;
 	packet_t tmp_packet;
@@ -509,5 +501,4 @@
 	size_t offset;
 	uint32_t new_sequence_number;
-	int rc;
 
 	assert(socket);
@@ -550,9 +541,7 @@
 		}
 
-		if (offset > 0) {
-			rc = packet_trim(packet, offset, 0);
-			if (rc != EOK)
-				return tcp_release_and_return(packet, rc);
-		}
+		if ((offset > 0) && (ERROR_OCCURRED(packet_trim(packet,
+		    offset, 0))))
+			return tcp_release_and_return(packet, ERROR_CODE);
 
 		assert(new_sequence_number == socket_data->next_incoming);
@@ -586,11 +575,8 @@
 			if (length <= offset)
 				next_packet = pq_next(next_packet);
-			else {
-				rc = packet_trim(next_packet, 0,
-				    length - offset));
-				if (rc != EOK)
-					return tcp_release_and_return(packet,
-					    rc);
-			}
+			else if (ERROR_OCCURRED(packet_trim(next_packet, 0,
+			    length - offset)))
+				return tcp_release_and_return(packet,
+				    ERROR_CODE);
 			offset -= length;
 			total_length -= length - offset;
@@ -617,13 +603,11 @@
 		// remove the header
 		total_length -= TCP_HEADER_LENGTH(header);
-		rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
-		if (rc != EOK)
-			return tcp_release_and_return(packet, rc);
+		if (ERROR_OCCURRED(packet_trim(packet,
+		    TCP_HEADER_LENGTH(header), 0)))
+			return tcp_release_and_return(packet, ERROR_CODE);
 
 		if (total_length) {
-			rc = tcp_queue_received_packet(socket, socket_data,
-			    packet, fragments, total_length);
-			if (rc != EOK)
-				return rc;
+			ERROR_PROPAGATE(tcp_queue_received_packet(socket,
+			    socket_data, packet, fragments, total_length));
 		} else {
 			total_length = 1;
@@ -633,6 +617,7 @@
 		packet = socket_data->incoming;
 		while (packet) {
-			rc = pq_get_order(socket_data->incoming, &order, NULL);
-			if (rc != EOK) {
+
+			if (ERROR_OCCURRED(pq_get_order(socket_data->incoming,
+			    &order, NULL))) {
 				// remove the corrupted packet
 				next_packet = pq_detach(packet);
@@ -671,9 +656,8 @@
 				    socket_data->next_incoming) {
 					// queue received data
-					rc = tcp_queue_received_packet(socket,
+					ERROR_PROPAGATE(
+					    tcp_queue_received_packet(socket,
 					    socket_data, packet, 1,
-					    packet_get_data_length(packet));
-					if (rc != EOK)
-						return rc;
+					    packet_get_data_length(packet)));
 					socket_data->next_incoming =
 					    new_sequence_number;
@@ -693,13 +677,12 @@
 						    new_sequence_number;
 					}
-					rc = packet_trim(packet,length, 0);
-					if (rc == EOK) {
+					if (ERROR_NONE(packet_trim(packet,
+					    length, 0))) {
 						// queue received data
-						rc = tcp_queue_received_packet(
+						ERROR_PROPAGATE(
+						    tcp_queue_received_packet(
 						    socket, socket_data, packet,
 						    1, packet_get_data_length(
-						    packet));
-						if (rc != EOK)
-							return rc;
+						    packet)));
 						socket_data->next_incoming =
 						    new_sequence_number;
@@ -726,13 +709,12 @@
 		// remove the header
 		total_length -= TCP_HEADER_LENGTH(header);
-		rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
-		if (rc != EOK)
-			return tcp_release_and_return(packet, rc);
+		if (ERROR_OCCURRED(packet_trim(packet,
+		    TCP_HEADER_LENGTH(header), 0)))
+			return tcp_release_and_return(packet, ERROR_CODE);
 
 		next_packet = pq_detach(packet);
 		length = packet_get_data_length(packet);
-		rc = pq_add(&socket_data->incoming, packet, new_sequence_number,
-		    length);
-		if (rc != EOK) {
+		if (ERROR_OCCURRED(pq_add(&socket_data->incoming, packet,
+		    new_sequence_number, length))) {
 			// remove the corrupted packets
 			pq_release_remote(tcp_globals.net_phone,
@@ -745,13 +727,8 @@
 				tmp_packet = pq_detach(next_packet);
 				length = packet_get_data_length(next_packet);
-
-				rc = pq_set_order(next_packet,
-				    new_sequence_number, length);
-				if (rc != EOK) {
-					pq_release_remote(tcp_globals.net_phone,
-					    packet_get_id(next_packet));
-				}
-				rc = pq_insert_after(packet, next_packet);
-				if (rc != EOK) {
+				if (ERROR_OCCURRED(pq_set_order(next_packet,
+				    new_sequence_number, length)) ||
+				    ERROR_OCCURRED(pq_insert_after(packet,
+				    next_packet))) {
 					pq_release_remote(tcp_globals.net_phone,
 					    packet_get_id(next_packet));
@@ -785,11 +762,8 @@
 	if (!packet) {
 		// create the notification packet
-		rc = tcp_create_notification_packet(&packet, socket,
-		    socket_data, 0, 0);
-		if (rc != EOK)
-			return rc;
-		rc = tcp_queue_prepare_packet(socket, socket_data, packet, 1);
-		if (rc != EOK)
-			return rc;
+		ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+		    socket_data, 0, 0));
+		ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data,
+		    packet, 1));
 		packet = tcp_send_prepare_packet(socket, socket_data, packet, 1,
 		    socket_data->last_outgoing + 1);
@@ -809,6 +783,7 @@
     size_t total_length)
 {
+	ERROR_DECLARE;
+
 	packet_dimension_ref packet_dimension;
-	int rc;
 
 	assert(socket);
@@ -820,12 +795,11 @@
 
 	// queue the received packet
-	rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
-	    SOCKET_MAX_RECEIVED_SIZE);
-	if (rc != EOK)
-		return tcp_release_and_return(packet, rc);
-	rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
-	    &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
-	if (rc != EOK)
-		return tcp_release_and_return(packet, rc);
+	if (ERROR_OCCURRED(dyn_fifo_push(&socket->received,
+	    packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) ||
+	    ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	    &tcp_globals.dimensions, socket_data->device_id,
+	    &packet_dimension))) {
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
 
 	// decrease the window size
@@ -846,6 +820,7 @@
     tcp_header_ref header, packet_t packet)
 {
+	ERROR_DECLARE;
+
 	packet_t next_packet;
-	int rc;
 
 	assert(socket);
@@ -869,9 +844,8 @@
 	}
 	// trim if longer than the header
-	if (packet_get_data_length(packet) > sizeof(*header)) {
-		rc = packet_trim(packet, 0,
-		    packet_get_data_length(packet) - sizeof(*header));
-		if (rc != EOK)
-			return tcp_release_and_return(packet, rc);
+	if ((packet_get_data_length(packet) > sizeof(*header)) &&
+	    ERROR_OCCURRED(packet_trim(packet, 0,
+	    packet_get_data_length(packet) - sizeof(*header)))) {
+		return tcp_release_and_return(packet, ERROR_CODE);
 	}
 	tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
@@ -902,4 +876,6 @@
     size_t addrlen)
 {
+	ERROR_DECLARE;
+
 	packet_t next_packet;
 	socket_core_ref socket;
@@ -908,5 +884,4 @@
 	int listening_socket_id = listening_socket->socket_id;
 	int listening_port = listening_socket->port;
-	int rc;
 
 	assert(listening_socket);
@@ -938,20 +913,19 @@
 	memcpy(socket_data->addr, src, socket_data->addrlen);
 	socket_data->dest_port = ntohs(header->source_port);
-	rc = tl_set_address_port(socket_data->addr, socket_data->addrlen,
-	    socket_data->dest_port);
-	if (rc != EOK) {
+	if (ERROR_OCCURRED(tl_set_address_port(socket_data->addr,
+	    socket_data->addrlen, socket_data->dest_port))) {
 		free(socket_data->addr);
 		free(socket_data);
-		return tcp_release_and_return(packet, rc);
+		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+		return ERROR_CODE;
 	}
 
 	// create a socket
 	socket_id = -1;
-	rc = socket_create(socket_data->local_sockets, listening_socket->phone,
-	    socket_data, &socket_id);
-	if (rc != EOK) {
+	if (ERROR_OCCURRED(socket_create(socket_data->local_sockets,
+	    listening_socket->phone, socket_data, &socket_id))) {
 		free(socket_data->addr);
 		free(socket_data);
-		return tcp_release_and_return(packet, rc);
+		return tcp_release_and_return(packet, ERROR_CODE);
 	}
 
@@ -990,19 +964,19 @@
 	assert(socket_data);
 
-	rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,
-	    (const char *) socket_data->addr, socket_data->addrlen);
+	ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port,
+	    socket, (const char *) socket_data->addr, socket_data->addrlen);
 	assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
 	    (const char *) socket_data->addr, socket_data->addrlen));
 
-//	rc = socket_bind_free_port(&tcp_globals.sockets, socket,
+//	ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket,
 //	    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
 //	    tcp_globals.last_used_port);
 //	tcp_globals.last_used_port = socket->port;
 	fibril_rwlock_write_unlock(&tcp_globals.lock);
-	if (rc != EOK) {
+	if (ERROR_CODE != EOK) {
 		socket_destroy(tcp_globals.net_phone, socket->socket_id,
 		    socket_data->local_sockets, &tcp_globals.sockets,
 		    tcp_free_socket_data);
-		return tcp_release_and_return(packet, rc);
+		return tcp_release_and_return(packet, ERROR_CODE);
 	}
 
@@ -1018,23 +992,20 @@
 
 	// trim if longer than the header
-	if (packet_get_data_length(packet) > sizeof(*header)) {
-		rc = packet_trim(packet, 0,
-		    packet_get_data_length(packet) - sizeof(*header));
-		if (rc != EOK) {
-			socket_destroy(tcp_globals.net_phone, socket->socket_id,
-			    socket_data->local_sockets, &tcp_globals.sockets,
-			    tcp_free_socket_data);
-			return tcp_release_and_return(packet, rc);
-		}
-	}
-
-	tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
-
-	rc = tcp_queue_packet(socket, socket_data, packet, 1);
-	if (rc != EOK) {
+	if ((packet_get_data_length(packet) > sizeof(*header)) &&
+	    ERROR_OCCURRED(packet_trim(packet, 0,
+	    packet_get_data_length(packet) - sizeof(*header)))) {
 		socket_destroy(tcp_globals.net_phone, socket->socket_id,
 		    socket_data->local_sockets, &tcp_globals.sockets,
 		    tcp_free_socket_data);
-		return rc;
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+
+	tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
+
+	if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))) {
+		socket_destroy(tcp_globals.net_phone, socket->socket_id,
+		    socket_data->local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data);
+		return ERROR_CODE;
 	}
 
@@ -1060,7 +1031,8 @@
     tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet)
 {
+	ERROR_DECLARE;
+
 	socket_core_ref listening_socket;
 	tcp_socket_data_ref listening_socket_data;
-	int rc;
 
 	assert(socket);
@@ -1087,7 +1059,8 @@
 
 		// queue the received packet
-		rc = dyn_fifo_push(&listening_socket->accepted,
-		    (-1 * socket->socket_id), listening_socket_data->backlog);
-		if (rc == EOK) {
+		if (ERROR_NONE(dyn_fifo_push(&listening_socket->accepted,
+		    (-1 * socket->socket_id),
+		    listening_socket_data->backlog))) {
+
 			// notify the destination socket
 			async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
@@ -1104,12 +1077,9 @@
 
 	// create the notification packet
-	rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
-	if (rc != EOK)
-		return rc;
+	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+	    socket_data, 0, 1));
 
 	// send the packet
-	rc = tcp_queue_packet(socket, socket_data, packet, 1);
-	if (rc != EOK)
-		return rc;
+	ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
 
 	// flush packets
@@ -1228,6 +1198,7 @@
     ipc_call_t *answer, int *answer_count)
 {
+	ERROR_DECLARE;
+
 	packet_t packet;
-	int rc;
 
 	assert(call);
@@ -1238,15 +1209,13 @@
 	switch (IPC_GET_METHOD(*call)) {
 	case NET_TL_RECEIVED:
-//		fibril_rwlock_read_lock(&tcp_globals.lock);
-		rc = packet_translate_remote(tcp_globals.net_phone, &packet,
-		    IPC_GET_PACKET(call));
-		if (rc != EOK) {
-//			fibril_rwlock_read_unlock(&tcp_globals.lock);
-			return rc;
-		}
-		rc = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP,
-		    IPC_GET_ERROR(call));
-//		fibril_rwlock_read_unlock(&tcp_globals.lock);
-		return rc;
+		//fibril_rwlock_read_lock(&tcp_globals.lock);
+		if (ERROR_NONE(packet_translate_remote(tcp_globals.net_phone,
+		    &packet, IPC_GET_PACKET(call)))) {
+			ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call),
+			    packet, SERVICE_TCP, IPC_GET_ERROR(call));
+		}
+		//fibril_rwlock_read_unlock(&tcp_globals.lock);
+		return ERROR_CODE;
+
 	case IPC_M_CONNECT_TO_ME:
 		return tcp_process_client_messages(callid, *call);
@@ -1693,6 +1662,7 @@
     struct sockaddr *addr, socklen_t addrlen)
 {
+	ERROR_DECLARE;
+
 	socket_core_ref socket;
-	int rc;
 
 	assert(local_sockets);
@@ -1705,6 +1675,6 @@
 		return ENOTSOCK;
 	
-	rc = tcp_connect_core(socket, local_sockets, addr, addrlen);
-	if (rc != EOK) {
+	if (ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr,
+	    addrlen))) {
 		tcp_free_socket_data(socket);
 		// unbind if bound
@@ -1715,5 +1685,5 @@
 		}
 	}
-	return rc;
+	return ERROR_CODE;
 }
 
@@ -1722,7 +1692,8 @@
     struct sockaddr *addr, socklen_t addrlen)
 {
+	ERROR_DECLARE;
+
 	tcp_socket_data_ref socket_data;
 	packet_t packet;
-	int rc;
 
 	assert(socket);
@@ -1740,29 +1711,22 @@
 
 	// get the destination port
-	rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port);
-	if (rc != EOK)
-		return rc;
-	
+	ERROR_PROPAGATE(tl_get_address_port(addr, addrlen,
+	    &socket_data->dest_port));
 	if (socket->port <= 0) {
 		// try to find a free port
-		rc = socket_bind_free_port(&tcp_globals.sockets, socket,
-		    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
-		    tcp_globals.last_used_port);
-		if (rc != EOK)
-			return rc;
+		ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets,
+		    socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+		    tcp_globals.last_used_port));
 		// set the next port as the search starting port number
 		tcp_globals.last_used_port = socket->port;
 	}
 
-	rc = ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
+	ERROR_PROPAGATE(ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
 	    addr, addrlen, &socket_data->device_id,
-	    &socket_data->pseudo_header, &socket_data->headerlen);
-	if (rc != EOK)
-		return rc;
+	    &socket_data->pseudo_header, &socket_data->headerlen));
 
 	// create the notification packet
-	rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0);
-	if (rc != EOK)
-		return rc;
+	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+	    socket_data, 1, 0));
 
 	// unlock the globals and wait for an operation
@@ -1772,9 +1736,7 @@
 	socket_data->addrlen = addrlen;
 	// send the packet
-
-	if (((rc = tcp_queue_packet(socket, socket_data, packet, 1)) != EOK) ||
-	    ((rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0,
-	    TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false)) !=
-	    EOK)) {
+	if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1)) ||
+	    ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data,
+	    0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))) {
 		socket_data->addr = NULL;
 		socket_data->addrlen = 0;
@@ -1792,6 +1754,6 @@
 			fibril_condvar_wait(&socket_data->operation.condvar,
 			    &socket_data->operation.mutex);
-			rc = socket_data->operation.result;
-			if (rc != EOK) {
+			ERROR_CODE = socket_data->operation.result;
+			if (ERROR_CODE != EOK) {
 				socket_data->addr = NULL;
 				socket_data->addrlen = 0;
@@ -1800,5 +1762,5 @@
 			socket_data->addr = NULL;
 			socket_data->addrlen = 0;
-			rc = EINTR;
+			ERROR_CODE = EINTR;
 		}
 	}
@@ -1807,5 +1769,5 @@
 
 	// return the result
-	return rc;
+	return ERROR_CODE;
 }
 
@@ -1814,6 +1776,7 @@
     tcp_socket_data_ref socket_data, packet_t packet, size_t data_length)
 {
+	ERROR_DECLARE;
+
 	tcp_header_ref header;
-	int rc;
 
 	assert(socket);
@@ -1830,7 +1793,6 @@
 	header->sequence_number = htonl(socket_data->next_outgoing);
 
-	rc = packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr,
-	    socket_data->addrlen);
-	if (rc != EOK)
+	if (ERROR_OCCURRED(packet_set_addr(packet, NULL,
+	    (uint8_t *) socket_data->addr, socket_data->addrlen)))
 		return tcp_release_and_return(packet, EINVAL);
 
@@ -1846,5 +1808,5 @@
     packet_t packet, size_t data_length)
 {
-	int rc;
+	ERROR_DECLARE;
 
 	assert(socket);
@@ -1852,12 +1814,10 @@
 	assert(socket->specific_data == socket_data);
 
-	rc = tcp_queue_prepare_packet(socket, socket_data, packet, data_length);
-	if (rc != EOK)
-		return rc;
-
-	rc = pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing,
-	    data_length);
-	if (rc != EOK)
-		return tcp_release_and_return(packet, rc);
+	ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet,
+	    data_length));
+
+	if (ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet,
+	    socket_data->next_outgoing, data_length)))
+		return tcp_release_and_return(packet, ERROR_CODE);
 
 	socket_data->next_outgoing += data_length;
@@ -1868,4 +1828,6 @@
 tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data)
 {
+	ERROR_DECLARE;
+
 	packet_t packet;
 	packet_t copy;
@@ -1873,5 +1835,4 @@
 	packet_t previous = NULL;
 	size_t data_length;
-	int rc;
 
 	assert(socket);
@@ -1898,11 +1859,8 @@
 		if (!sending) {
 			sending = copy;
-		} else {
-			rc = pq_insert_after(previous, copy);
-			if (rc != EOK) {
-				pq_release_remote(tcp_globals.net_phone,
-				    packet_get_id(copy));
-				return sending;
-			}
+		} else if (ERROR_OCCURRED(pq_insert_after(previous, copy))) {
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(copy));
+			return sending;
 		}
 
@@ -1926,7 +1884,8 @@
     packet_t packet, size_t data_length, size_t sequence_number)
 {
+	ERROR_DECLARE;
+
 	tcp_header_ref header;
 	uint32_t checksum;
-	int rc;
 
 	assert(socket);
@@ -1935,7 +1894,7 @@
 
 	// adjust the pseudo header
-	rc = ip_client_set_pseudo_header_data_length(socket_data->pseudo_header,
-	    socket_data->headerlen, packet_get_data_length(packet));
-	if (rc != EOK) {
+	if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
+	    socket_data->pseudo_header, socket_data->headerlen,
+	    packet_get_data_length(packet)))) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
@@ -1962,18 +1921,13 @@
 	checksum = compute_checksum(0, socket_data->pseudo_header,
 	    socket_data->headerlen);
-	checksum = compute_checksum(checksum,
-	    (uint8_t *) packet_get_data(packet),
+	checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet),
 	    packet_get_data_length(packet));
 	header->checksum = htons(flip_checksum(compact_checksum(checksum)));
 
 	// prepare the packet
-	rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
-	if (rc != EOK) {
-		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-		return NULL;
-	}
-	rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,
-	    sequence_number, socket_data->state, socket_data->timeout, true);
-	if (rc != EOK) {
+	if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0,
+	    0, 0)) || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket,
+	    socket_data, sequence_number, socket_data->state,
+	    socket_data->timeout, true))) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
@@ -2084,4 +2038,6 @@
     size_t *addrlen)
 {
+	ERROR_DECLARE;
+
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
@@ -2089,5 +2045,4 @@
 	packet_t packet;
 	size_t length;
-	int rc;
 
 	assert(local_sockets);
@@ -2111,7 +2066,6 @@
 	// send the source address if desired
 	if (addrlen) {
-		rc = data_reply(socket_data->addr, socket_data->addrlen);
-		if (rc != EOK)
-			return rc;
+		ERROR_PROPAGATE(data_reply(socket_data->addr,
+		    socket_data->addrlen));
 		*addrlen = socket_data->addrlen;
 	}
@@ -2122,12 +2076,9 @@
 		return NO_DATA;
 
-	rc = packet_translate_remote(tcp_globals.net_phone, &packet, packet_id);
-	if (rc != EOK)
-		return rc;
+	ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet,
+	    packet_id));
 
 	// reply the packets
-	rc = socket_reply_packets(packet, &length);
-	if (rc != EOK)
-		return rc;
+	ERROR_PROPAGATE(socket_reply_packets(packet, &length));
 
 	// release the packet
@@ -2142,4 +2093,6 @@
     size_t *data_fragment_size, int flags)
 {
+	ERROR_DECLARE;
+
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
@@ -2150,5 +2103,4 @@
 	int index;
 	int result;
-	int rc;
 
 	assert(local_sockets);
@@ -2171,8 +2123,6 @@
 		return ENOTCONN;
 
-	rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
-	    &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
-	if (rc != EOK)
-		return rc;
+	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	   &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
 
 	*data_fragment_size =
@@ -2195,7 +2145,6 @@
 
 		tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
-		rc = tcp_queue_packet(socket, socket_data, packet, 0);
-		if (rc != EOK)
-			return rc;
+		ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet,
+		    0));
 	}
 
@@ -2216,8 +2165,9 @@
 tcp_close_message(socket_cores_ref local_sockets, int socket_id)
 {
+	ERROR_DECLARE;
+
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
 	packet_t packet;
-	int rc;
 
 	// find the socket
@@ -2244,12 +2194,11 @@
 	default:
 		// just destroy
-		rc = socket_destroy(tcp_globals.net_phone, socket_id,
+		if (ERROR_NONE(socket_destroy(tcp_globals.net_phone, socket_id,
 		    local_sockets, &tcp_globals.sockets,
-		    tcp_free_socket_data);
-		if (rc == EOK) {
+		    tcp_free_socket_data))) {
 			fibril_rwlock_write_unlock(socket_data->local_lock);
 			fibril_rwlock_write_unlock(&tcp_globals.lock);
 		}
-		return rc;
+		return ERROR_CODE;
 	}
 
@@ -2258,12 +2207,9 @@
 
 	// create the notification packet
-	rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
-	if (rc != EOK)
-		return rc;
+	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+	    socket_data, 0, 1));
 
 	// send the packet
-	rc = tcp_queue_packet(socket, socket_data, packet, 1);
-	if (rc != EOK)
-		return rc;
+	ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
 
 	// flush packets
@@ -2284,15 +2230,15 @@
     tcp_socket_data_ref socket_data, int synchronize, int finalize)
 {
+	ERROR_DECLARE;
+
 	packet_dimension_ref packet_dimension;
 	tcp_header_ref header;
-	int rc;
 
 	assert(packet);
 
 	// get the device packet dimension
-	rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
-	    &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
-	if (rc != EOK)
-		return rc;
+	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	    &tcp_globals.dimensions, socket_data->device_id,
+	    &packet_dimension));
 
 	// get a new packet
@@ -2319,9 +2265,10 @@
     int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
 {
+	ERROR_DECLARE;
+
 	socket_core_ref accepted;
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
 	packet_dimension_ref packet_dimension;
-	int rc;
 
 	assert(local_sockets);
@@ -2357,13 +2304,9 @@
 		// TODO can it be in another state?
 		if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
-			rc = data_reply(socket_data->addr,
-			    socket_data->addrlen);
-			if (rc != EOK)
-				return rc;
-			rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
-			    &tcp_globals.dimensions, socket_data->device_id,
-			    &packet_dimension);
-			if (rc != EOK)
-				return rc;
+			ERROR_PROPAGATE(data_reply(socket_data->addr,
+			    socket_data->addrlen));
+			ERROR_PROPAGATE(tl_get_ip_packet_dimension(
+			    tcp_globals.ip_phone, &tcp_globals.dimensions,
+			    socket_data->device_id, &packet_dimension));
 			*addrlen = socket_data->addrlen;
 
@@ -2375,8 +2318,7 @@
 	
 			if (new_socket_id > 0) {
-				rc = socket_cores_update(local_sockets,
-				    accepted->socket_id, new_socket_id);
-				if (rc != EOK)
-					return rc;
+				ERROR_PROPAGATE(socket_cores_update(
+				    local_sockets, accepted->socket_id,
+				    new_socket_id));
 				accepted->socket_id = new_socket_id;
 			}
@@ -2488,8 +2430,13 @@
 main(int argc, char *argv[])
 {
-	int rc;
-
-	rc = tl_module_start_standalone(tl_client_connection);
-	return rc;
+	ERROR_DECLARE;
+
+	/*
+	   Start the module 
+	 */
+	if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection)))
+		return ERROR_CODE;
+
+	return EOK;
 }
 
