Index: uspace/srv/net/tl/tcp/tcp.c
===================================================================
--- uspace/srv/net/tl/tcp/tcp.c	(revision 472020fcce0d34f10b66ca0bd38c3b005abb9a4c)
+++ uspace/srv/net/tl/tcp/tcp.c	(revision 49d819b4cb965ac133a41b34c95dbb8bba13ee94)
@@ -28,11 +28,15 @@
 
 /** @addtogroup tcp
- *  @{
+ * @{
  */
 
 /** @file
- *  TCP module implementation.
- *  @see tcp.h
+ * TCP module implementation.
+ * @see tcp.h
  */
+
+#include "tcp.h"
+#include "tcp_header.h"
+#include "tcp_module.h"
 
 #include <assert.h>
@@ -72,8 +76,4 @@
 #include <tl_interface.h>
 
-#include "tcp.h"
-#include "tcp_header.h"
-#include "tcp_module.h"
-
 /** TCP module name. */
 #define NAME	"TCP protocol"
@@ -110,10 +110,11 @@
 
 /** Returns a value indicating whether the value is in the interval respecting
- *  the possible overflow.
+ * the possible overflow.
  *
- *  The high end and/or the value may overflow, be lower than the low value.
- *  @param[in] lower The last value before the interval.
- *  @param[in] value The value to be checked.
- *  @param[in] higher_equal The last value in the interval.
+ * The high end and/or the value may overflow, be lower than the low value.
+ *
+ * @param[in] lower	The last value before the interval.
+ * @param[in] value	The value to be checked.
+ * @param[in] higher_equal The last value in the interval.
  */
 #define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) \
@@ -165,86 +166,71 @@
 };
 
-/** Releases the packet and returns the result.
- *  @param[in] packet The packet queue to be released.
- *  @param[in] result The result to be returned.
- *  @return The result parameter.
- */
-int tcp_release_and_return(packet_t packet, int result);
-
-void tcp_prepare_operation_header(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
-    int finalize);
-int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
-    socket_core_ref socket, tcp_socket_data_ref socket_data,
-    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
-    int globals_read_only);
-void tcp_free_socket_data(socket_core_ref socket);
-
-int tcp_timeout(void *data);
-
-int tcp_release_after_timeout(void *data);
-
-int tcp_process_packet(device_id_t device_id, packet_t packet,
-    services_t error);
-int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
-    struct sockaddr *addr, socklen_t addrlen);
-int tcp_queue_prepare_packet(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
-int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
-    packet_t packet, size_t data_length);
-packet_t tcp_get_packets_to_send(socket_core_ref socket,
-    tcp_socket_data_ref socket_data);
-void tcp_send_packets(device_id_t device_id, packet_t packet);
-
-void tcp_process_acknowledgement(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, tcp_header_ref header);
-packet_t tcp_send_prepare_packet(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length,
-    size_t sequence_number);
-packet_t tcp_prepare_copy(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length,
-    size_t sequence_number);
-void tcp_retransmit_packet(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, size_t sequence_number);
-int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket,
-    tcp_socket_data_ref socket_data, int synchronize, int finalize);
-void tcp_refresh_socket_data(tcp_socket_data_ref socket_data);
-
-void tcp_initialize_socket_data(tcp_socket_data_ref socket_data);
-
-int tcp_process_listen(socket_core_ref listening_socket,
-    tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
-    packet_t packet, struct sockaddr *src, struct sockaddr *dest,
-    size_t addrlen);
-int tcp_process_syn_sent(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
-int tcp_process_syn_received(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
-int tcp_process_established(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet,
-    int fragments, size_t total_length);
-int tcp_queue_received_packet(socket_core_ref socket,
-    tcp_socket_data_ref socket_data, packet_t packet, int fragments,
-    size_t total_length);
-
-int tcp_received_msg(device_id_t device_id, packet_t packet,
-    services_t receiver, services_t error);
-int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
-
-int tcp_listen_message(socket_cores_ref local_sockets, int socket_id,
-    int backlog);
-int tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
-    struct sockaddr *addr, socklen_t addrlen);
-int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id,
-    int flags, size_t * addrlen);
-int tcp_send_message(socket_cores_ref local_sockets, int socket_id,
-    int fragments, size_t * data_fragment_size, int flags);
-int tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
-    int new_socket_id, size_t * data_fragment_size, size_t * addrlen);
-int tcp_close_message(socket_cores_ref local_sockets, int socket_id);
+static int tcp_release_and_return(packet_t, int);
+static void tcp_prepare_operation_header(socket_core_ref, tcp_socket_data_ref,
+    tcp_header_ref, int synchronize, int);
+static int tcp_prepare_timeout(int (*)(void *), socket_core_ref,
+    tcp_socket_data_ref, size_t, tcp_socket_state_t, suseconds_t, int);
+static void tcp_free_socket_data(socket_core_ref);
+
+static int tcp_timeout(void *);
+
+static int tcp_release_after_timeout(void *);
+
+static int tcp_process_packet(device_id_t, packet_t, services_t);
+static int tcp_connect_core(socket_core_ref, socket_cores_ref,
+    struct sockaddr *, socklen_t);
+static int tcp_queue_prepare_packet(socket_core_ref, tcp_socket_data_ref,
+    packet_t, size_t);
+static int tcp_queue_packet(socket_core_ref, tcp_socket_data_ref, packet_t,
+    size_t);
+static packet_t tcp_get_packets_to_send(socket_core_ref, tcp_socket_data_ref);
+static void tcp_send_packets(device_id_t, packet_t);
+
+static void tcp_process_acknowledgement(socket_core_ref, tcp_socket_data_ref,
+    tcp_header_ref);
+static packet_t tcp_send_prepare_packet(socket_core_ref, tcp_socket_data_ref,
+    packet_t, size_t, size_t);
+static packet_t tcp_prepare_copy(socket_core_ref, tcp_socket_data_ref, packet_t,
+    size_t, size_t);
+/* static */ void tcp_retransmit_packet(socket_core_ref, tcp_socket_data_ref,
+    size_t);
+static int tcp_create_notification_packet(packet_t *, socket_core_ref,
+    tcp_socket_data_ref, int, int);
+static void tcp_refresh_socket_data(tcp_socket_data_ref);
+
+static void tcp_initialize_socket_data(tcp_socket_data_ref);
+
+static int tcp_process_listen(socket_core_ref, tcp_socket_data_ref,
+    tcp_header_ref, packet_t, struct sockaddr *, struct sockaddr *, size_t);
+static int tcp_process_syn_sent(socket_core_ref, tcp_socket_data_ref,
+    tcp_header_ref, packet_t);
+static int tcp_process_syn_received(socket_core_ref, tcp_socket_data_ref,
+    tcp_header_ref, packet_t);
+static int tcp_process_established(socket_core_ref, tcp_socket_data_ref,
+    tcp_header_ref, packet_t, int, size_t);
+static int tcp_queue_received_packet(socket_core_ref, tcp_socket_data_ref,
+    packet_t, int, size_t);
+
+static int tcp_received_msg(device_id_t, packet_t, services_t, services_t);
+static int tcp_process_client_messages(ipc_callid_t, ipc_call_t);
+
+static int tcp_listen_message(socket_cores_ref, int, int);
+static int tcp_connect_message(socket_cores_ref, int, struct sockaddr *,
+    socklen_t);
+static int tcp_recvfrom_message(socket_cores_ref, int, int, size_t *);
+static int tcp_send_message(socket_cores_ref, int, int, size_t *, int);
+static int tcp_accept_message(socket_cores_ref, int, int, size_t *, size_t *);
+static int tcp_close_message(socket_cores_ref, int);
 
 /** TCP global data. */
 tcp_globals_t tcp_globals;
 
+/** Initializes the TCP module.
+ *
+ * @param[in] client_connection The client connection processing function. The
+ *			module skeleton propagates its own one.
+ * @returns		EOK on success.
+ * @returns		ENOMEM if there is not enough memory left.
+ */
 int tcp_initialize(async_client_conn_t client_connection)
 {
@@ -314,23 +300,20 @@
 	size_t addrlen;
 
-	if (error) {
-		switch (error) {
-		case SERVICE_ICMP:
-			// process error
-			result = icmp_client_process_packet(packet, &type,
-			    &code, NULL, NULL);
-			if (result < 0)
-				return tcp_release_and_return(packet, result);
-
-			length = (size_t) result;
-			if (ERROR_OCCURRED(packet_trim(packet, length, 0))) {
-				return tcp_release_and_return(packet,
-				    ERROR_CODE);
-			}
-			break;
-
-		default:
-			return tcp_release_and_return(packet, ENOTSUP);
-		}
+	switch (error) {
+	case SERVICE_NONE:
+		break;
+	case SERVICE_ICMP:
+		// process error
+		result = icmp_client_process_packet(packet, &type, &code, NULL,
+		    NULL);
+		if (result < 0)
+			return tcp_release_and_return(packet, result);
+
+		length = (size_t) result;
+		if (ERROR_OCCURRED(packet_trim(packet, length, 0)))
+			return tcp_release_and_return(packet, ERROR_CODE);
+		break;
+	default:
+		return tcp_release_and_return(packet, ENOTSUP);
 	}
 
@@ -379,14 +362,14 @@
 		    ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,
 		    0);
-		if (!socket) {
-			if (tl_prepare_icmp_packet(tcp_globals.net_phone,
-			    tcp_globals.icmp_phone, packet, error) == EOK) {
-				icmp_destination_unreachable_msg(
-				    tcp_globals.icmp_phone, ICMP_PORT_UNREACH,
-				    0, packet);
-			}
-			return EADDRNOTAVAIL;
-		}
-	}
+	}
+	if (!socket) {
+		if (tl_prepare_icmp_packet(tcp_globals.net_phone,
+		    tcp_globals.icmp_phone, packet, error) == EOK) {
+			icmp_destination_unreachable_msg(tcp_globals.icmp_phone,
+			    ICMP_PORT_UNREACH, 0, packet);
+		}
+		return EADDRNOTAVAIL;
+	}
+
 	printf("socket id %d\n", socket->socket_id);
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
@@ -402,5 +385,5 @@
 	total_length = 0;
 	do {
-		++fragments;
+		fragments++;
 		length = packet_get_data_length(next_packet);
 		if (length <= 0)
@@ -421,8 +404,7 @@
 
 	if (error)
-		goto error;
+		goto has_error_service;
 	
 	if (socket_data->state == TCP_SOCKET_LISTEN) {
-
 		if (socket_data->pseudo_header) {
 			free(socket_data->pseudo_header);
@@ -464,5 +446,5 @@
 	}
 
-error:
+has_error_service:
 	fibril_rwlock_read_unlock(&tcp_globals.lock);
 
@@ -507,6 +489,5 @@
 int
 tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data,
-    tcp_header_ref header, packet_t packet, int fragments,
-    size_t total_length)
+    tcp_header_ref header, packet_t packet, int fragments, size_t total_length)
 {
 	ERROR_DECLARE;
@@ -684,7 +665,7 @@
 					continue;
 					// at least partly following data?
-				} else if (IS_IN_INTERVAL_OVERFLOW(
-				    sequence_number, socket_data->next_incoming,
-				    new_sequence_number)) {
+				}
+				if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
+				    socket_data->next_incoming, new_sequence_number)) {
 					if (socket_data->next_incoming <
 					    new_sequence_number) {
@@ -961,5 +942,5 @@
 	listening_socket = socket_port_find(&tcp_globals.sockets,
 	    listening_port, SOCKET_MAP_KEY_LISTENING, 0);
-	if ((!listening_socket) ||
+	if (!listening_socket ||
 	    (listening_socket->socket_id != listening_socket_id)) {
 		fibril_rwlock_write_unlock(&tcp_globals.lock);
@@ -1191,5 +1172,5 @@
 	if (number == socket_data->expected) {
 		// increase the counter
-		++socket_data->expected_count;
+		socket_data->expected_count++;
 		if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
 			socket_data->expected_count = 1;
@@ -1200,6 +1181,20 @@
 }
 
-int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call,
-    ipc_call_t * answer, int *answer_count)
+/** Processes the TCP message.
+ *
+ * @param[in] callid	The message identifier.
+ * @param[in] call	The message parameters.
+ * @param[out] answer	The message answer parameters.
+ * @param[out] answer_count The last parameter for the actual answer in the
+ *			answer parameter.
+ * @returns		EOK on success.
+ * @returns		ENOTSUP if the message is not known.
+ *
+ * @see tcp_interface.h
+ * @see IS_NET_TCP_MESSAGE()
+ */
+int
+tcp_message_standalone(ipc_callid_t callid, ipc_call_t *call,
+    ipc_call_t *answer, int *answer_count)
 {
 	ERROR_DECLARE;
@@ -1521,5 +1516,5 @@
 	socket = socket_port_find(&tcp_globals.sockets, timeout->port,
 	    timeout->key, timeout->key_length);
-	if (!(socket && (socket->socket_id == timeout->socket_id)))
+	if (!socket || (socket->socket_id != timeout->socket_id))
 		goto out;
 	
@@ -1532,5 +1527,5 @@
 	if (timeout->sequence_number) {
 		// increase the timeout counter;
-		++socket_data->timeout_count;
+		socket_data->timeout_count++;
 		if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
 			// TODO release as connection lost
@@ -1744,11 +1739,8 @@
 	    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;
 		fibril_rwlock_write_lock(&tcp_globals.lock);
-
 	} else {
-
 		packet = tcp_get_packets_to_send(socket, socket_data);
 		if (packet) {
@@ -1876,5 +1868,5 @@
 		packet = pq_next(packet);
 		// overflow occurred ?
-		if ((!packet) &&
+		if (!packet &&
 		    (socket_data->last_outgoing > socket_data->next_outgoing)) {
 			printf("gpts overflow\n");
@@ -2044,5 +2036,5 @@
 int
 tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
-    size_t * addrlen)
+    size_t *addrlen)
 {
 	ERROR_DECLARE;
@@ -2099,5 +2091,5 @@
 int
 tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments,
-    size_t * data_fragment_size, int flags)
+    size_t *data_fragment_size, int flags)
 {
 	ERROR_DECLARE;
@@ -2138,5 +2130,5 @@
 	    packet_dimension->content : socket_data->data_fragment_size);
 
-	for (index = 0; index < fragments; ++index) {
+	for (index = 0; index < fragments; index++) {
 		// read the data fragment
 		result = tl_socket_read_packet_data(tcp_globals.net_phone,
@@ -2235,5 +2227,5 @@
 
 int
-tcp_create_notification_packet(packet_t * packet, socket_core_ref socket,
+tcp_create_notification_packet(packet_t *packet, socket_core_ref socket,
     tcp_socket_data_ref socket_data, int synchronize, int finalize)
 {
@@ -2271,5 +2263,5 @@
 int
 tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
-    int new_socket_id, size_t * data_fragment_size, size_t * addrlen)
+    int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
 {
 	ERROR_DECLARE;
@@ -2373,4 +2365,10 @@
 }
 
+/** Releases the packet and returns the result.
+ *
+ * @param[in] packet	The packet queue to be released.
+ * @param[in] result	The result to be returned.
+ * @return		The result parameter.
+ */
 int tcp_release_and_return(packet_t packet, int result)
 {
@@ -2381,6 +2379,6 @@
 /** Default thread for new connections.
  *
- *  @param[in] iid The initial message identifier.
- *  @param[in] icall The initial message call structure.
+ * @param[in] iid	The initial message identifier.
+ * @param[in] icall	The initial message call structure.
  *
  */
@@ -2397,23 +2395,18 @@
 		int answer_count;
 
-		/*
-		   Clear the answer structure 
-		 */
+		/* Clear the answer structure */
 		refresh_answer(&answer, &answer_count);
 
-		/*
-		   Fetch the next message 
-		 */
+		/* Fetch the next message */
 		ipc_call_t call;
 		ipc_callid_t callid = async_get_call(&call);
 
-		/*
-		   Process the message 
-		 */
+		/* Process the message */
 		int res = tl_module_message_standalone(callid, &call, &answer,
 		    &answer_count);
 
 		/*
-		   End if said to either by the message or the processing result 
+		 * End if told to either by the message or the processing
+		 * result.
 		 */
 		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
@@ -2430,10 +2423,7 @@
 /** Starts the module.
  *
- *  @param argc The count of the command line arguments. Ignored parameter.
- *  @param argv The command line parameters. Ignored parameter.
- *
- *  @returns EOK on success.
- *  @returns Other error codes as defined for each specific module start function.
- *
+ * @returns		EOK on success.
+ * @returns		Other error codes as defined for each specific module
+ *			start function.
  */
 int
