Index: uspace/lib/net/generic/socket_core.c
===================================================================
--- uspace/lib/net/generic/socket_core.c	(revision 25d2de69b358d56541d2ba684af54fba71d44efe)
+++ uspace/lib/net/generic/socket_core.c	(revision 137f8aa0da58c7057d098c654bc621728bd90d5f)
@@ -27,15 +27,21 @@
  */
 
-/** @addtogroup socket
- *  @{
+/** @addtogroup libnet
+ * @{
  */
 
 /** @file
- *  Socket common core implementation.
- */
+ * Socket common core implementation.
+ */
+
+#include <socket_core.h>
+#include <packet_client.h>
+#include <packet_remote.h>
 
 #include <net/socket_codes.h>
 #include <net/in.h>
 #include <net/inet.h>
+#include <net/packet.h>
+#include <net/modules.h>
 
 #include <stdint.h>
@@ -46,22 +52,16 @@
 #include <adt/dynamic_fifo.h>
 #include <adt/int_map.h>
-#include <net/packet.h>
-#include <packet_client.h>
-#include <packet_remote.h>
-#include <net/modules.h>
-#include <socket_core.h>
-
-/** Maximum number of random attempts to find a new socket identifier before switching to the sequence.
- */
-#define SOCKET_ID_TRIES					100
-
-/** Bound port sockets.
- */
-struct socket_port{
-	/** The bound sockets map.
-	 */
+
+/**
+ * Maximum number of random attempts to find a new socket identifier before
+ * switching to the sequence.
+ */
+#define SOCKET_ID_TRIES	100
+
+/** Bound port sockets.*/
+struct socket_port {
+	/** The bound sockets map. */
 	socket_port_map_t map;
-	/** The bound sockets count.
-	 */
+	/** The bound sockets count. */
 	int count;
 };
@@ -74,91 +74,134 @@
 
 /** Destroys the socket.
- *  If the socket is bound, the port is released.
- *  Releases all buffered packets, calls the release function and removes the socket from the local sockets.
- *  @param[in] packet_phone The packet server phone to release buffered packets.
- *  @param[in] socket The socket to be destroyed.
- *  @param[in,out] local_sockets The local sockets to be updated.
- *  @param[in,out] global_sockets The global sockets to be updated.
- *  @param[in] socket_release The client release callback function.
- */
-static void socket_destroy_core(int packet_phone, socket_core_ref socket, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket)){
+ *
+ * If the socket is bound, the port is released.
+ * Releases all buffered packets, calls the release function and removes the
+ * socket from the local sockets.
+ *
+ * @param[in] packet_phone The packet server phone to release buffered packets.
+ * @param[in] socket	The socket to be destroyed.
+ * @param[in,out] local_sockets The local sockets to be updated.
+ * @param[in,out] global_sockets The global sockets to be updated.
+ * @param[in] socket_release The client release callback function.
+ */
+static void
+socket_destroy_core(int packet_phone, socket_core_ref socket,
+    socket_cores_ref local_sockets, socket_ports_ref global_sockets,
+    void (* socket_release)(socket_core_ref socket))
+{
 	int packet_id;
 
 	// if bound
-	if(socket->port){
+	if (socket->port) {
 		// release the port
 		socket_port_release(global_sockets, socket);
 	}
+	
 	// release all received packets
-	while((packet_id = dyn_fifo_pop(&socket->received)) >= 0){
+	while ((packet_id = dyn_fifo_pop(&socket->received)) >= 0)
 		pq_release_remote(packet_phone, packet_id);
-	}
+
 	dyn_fifo_destroy(&socket->received);
 	dyn_fifo_destroy(&socket->accepted);
-	if(socket_release){
+
+	if (socket_release)
 		socket_release(socket);
-	}
+
 	socket_cores_exclude(local_sockets, socket->socket_id);
 }
 
-void socket_cores_release(int packet_phone, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket)){
-	if(socket_cores_is_valid(local_sockets)){
-		int index;
-
-		local_sockets->magic = 0;
-		for(index = 0; index < local_sockets->next; ++ index){
-			if(socket_cores_item_is_valid(&(local_sockets->items[index]))){
-				local_sockets->items[index].magic = 0;
-				if(local_sockets->items[index].value){
-					socket_destroy_core(packet_phone, local_sockets->items[index].value, local_sockets, global_sockets, socket_release);
-					free(local_sockets->items[index].value);
-					local_sockets->items[index].value = NULL;
-				}
+/** Destroys local sockets.
+ *
+ * Releases all buffered packets and calls the release function for each of the
+ * sockets.
+ *
+ * @param[in] packet_phone The packet server phone to release buffered packets.
+ * @param[in] local_sockets The local sockets to be destroyed.
+ * @param[in,out] global_sockets The global sockets to be updated.
+ * @param[in] socket_release The client release callback function.
+ */
+void
+socket_cores_release(int packet_phone, socket_cores_ref local_sockets,
+    socket_ports_ref global_sockets,
+    void (* socket_release)(socket_core_ref socket))
+{
+	int index;
+
+	if (!socket_cores_is_valid(local_sockets))
+		return;
+
+	local_sockets->magic = 0;
+
+	for (index = 0; index < local_sockets->next; ++index) {
+		if (socket_cores_item_is_valid(&local_sockets->items[index])) {
+			local_sockets->items[index].magic = 0;
+
+			if (local_sockets->items[index].value) {
+				socket_destroy_core(packet_phone,
+				    local_sockets->items[index].value,
+				    local_sockets, global_sockets,
+				    socket_release);
+				free(local_sockets->items[index].value);
+				local_sockets->items[index].value = NULL;
 			}
 		}
-		free(local_sockets->items);
-	}
+	}
+
+	free(local_sockets->items);
 }
 
 /** Adds the socket to a socket port.
- *  @param[in,out] socket_port The socket port structure.
- *  @param[in] socket The socket to be added.
- *  @param[in] key The socket key identifier.
- *  @param[in] key_length The socket key length.
- *  @returns EOK on success.
- *  @returns ENOMEM if there is not enough memory left.
- */
-static int socket_port_add_core(socket_port_ref socket_port, socket_core_ref socket, const char * key, size_t key_length){
+ *
+ * @param[in,out] socket_port The socket port structure.
+ * @param[in] socket	The socket to be added.
+ * @param[in] key	The socket key identifier.
+ * @param[in] key_length The socket key length.
+ * @returns		EOK on success.
+ * @returns		ENOMEM if there is not enough memory left.
+ */
+static int
+socket_port_add_core(socket_port_ref socket_port, socket_core_ref socket,
+    const char *key, size_t key_length)
+{
 	ERROR_DECLARE;
 
-	socket_core_ref * socket_ref;
+	socket_core_ref *socket_ref;
 
 	// create a wrapper
 	socket_ref = malloc(sizeof(*socket_ref));
-	if(! socket_ref){
+	if (!socket_ref)
 		return ENOMEM;
-	}
+
 	*socket_ref = socket;
 	// add the wrapper
-	if(ERROR_OCCURRED(socket_port_map_add(&socket_port->map, key, key_length, socket_ref))){
+	if (ERROR_OCCURRED(socket_port_map_add(&socket_port->map, key,
+	    key_length, socket_ref))) {
 		free(socket_ref);
 		return ERROR_CODE;
 	}
-	++ socket_port->count;
+	
+	++socket_port->count;
 	socket->key = key;
 	socket->key_length = key_length;
+	
 	return EOK;
 }
 
 /** Binds the socket to the port.
- *  The SOCKET_MAP_KEY_LISTENING key identifier is used.
- *  @param[in] global_sockets The global sockets to be updated.
- *  @param[in] socket The socket to be added.
- *  @param[in] port The port number to be bound to.
- *  @returns EOK on success.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns Other error codes as defined for the socket_ports_add() function.
- */
-static int socket_bind_insert(socket_ports_ref global_sockets, socket_core_ref socket, int port){
+ *
+ * The SOCKET_MAP_KEY_LISTENING key identifier is used.
+ *
+ * @param[in] global_sockets The global sockets to be updated.
+ * @param[in] socket	The socket to be added.
+ * @param[in] port	The port number to be bound to.
+ * @returns		EOK on success.
+ * @returns		ENOMEM if there is not enough memory left.
+ * @returns		Other error codes as defined for the
+ *			 socket_ports_add() function.
+ */
+static int
+socket_bind_insert(socket_ports_ref global_sockets, socket_core_ref socket,
+    int port)
+{
 	ERROR_DECLARE;
 
@@ -167,102 +210,164 @@
 	// create a wrapper
 	socket_port = malloc(sizeof(*socket_port));
-	if(! socket_port){
+	if (!socket_port)
 		return ENOMEM;
-	}
+
 	socket_port->count = 0;
-	if(ERROR_OCCURRED(socket_port_map_initialize(&socket_port->map))
-		|| ERROR_OCCURRED(socket_port_add_core(socket_port, socket, SOCKET_MAP_KEY_LISTENING, 0))){
+	if (ERROR_OCCURRED(socket_port_map_initialize(&socket_port->map)) ||
+	    ERROR_OCCURRED(socket_port_add_core(socket_port, socket,
+	    SOCKET_MAP_KEY_LISTENING, 0))) {
 		socket_port_map_destroy(&socket_port->map);
 		free(socket_port);
 		return ERROR_CODE;
 	}
+	
 	// register the incomming port
 	ERROR_CODE = socket_ports_add(global_sockets, port, socket_port);
-	if(ERROR_CODE < 0){
+	if (ERROR_CODE < 0) {
 		socket_port_map_destroy(&socket_port->map);
 		free(socket_port);
 		return ERROR_CODE;
 	}
+	
 	socket->port = port;
 	return EOK;
 }
 
-int socket_bind(socket_cores_ref local_sockets, socket_ports_ref global_sockets, int socket_id, void * addr, size_t addrlen, int free_ports_start, int free_ports_end, int last_used_port){
+/** Binds the socket to the port.
+ *
+ * The address port is used if set, a free port is used if not.
+ *
+ * @param[in] local_sockets The local sockets to be searched.
+ * @param[in,out] global_sockets The global sockets to be updated.
+ * @param[in] socket_id	The new socket identifier.
+ * @param[in] addr	The address to be bound to.
+ * @param[in] addrlen	The address length.
+ * @param[in] free_ports_start The minimum free port.
+ * @param[in] free_ports_end The maximum free port.
+ * @param[in] last_used_port The last used free port.
+ * @returns		EOK on success.
+ * @returns		ENOTSOCK if the socket was not found.
+ * @returns		EAFNOSUPPORT if the address family is not supported.
+ * @returns		EADDRINUSE if the port is already in use.
+ * @returns		Other error codes as defined for the
+ *			socket_bind_free_port() function.
+ * @returns		Other error codes as defined for the
+ *			socket_bind_insert() function.
+ */
+int
+socket_bind(socket_cores_ref local_sockets, socket_ports_ref global_sockets,
+    int socket_id, void *addr, size_t addrlen, int free_ports_start,
+    int free_ports_end, int last_used_port)
+{
 	socket_core_ref socket;
 	socket_port_ref socket_port;
-	struct sockaddr * address;
-	struct sockaddr_in * address_in;
-
-	if(addrlen < sizeof(struct sockaddr)){
+	struct sockaddr *address;
+	struct sockaddr_in *address_in;
+
+	if (addrlen < sizeof(struct sockaddr))
 		return EINVAL;
-	}
+
 	address = (struct sockaddr *) addr;
-	switch(address->sa_family){
-		case AF_INET:
-			if(addrlen != sizeof(struct sockaddr_in)){
-				return EINVAL;
-			}
-			address_in = (struct sockaddr_in *) addr;
-			// find the socket
-			socket = socket_cores_find(local_sockets, socket_id);
-			if(! socket){
-				return ENOTSOCK;
-			}
-			// bind a free port?
-			if(address_in->sin_port <= 0){
-				return socket_bind_free_port(global_sockets, socket, free_ports_start, free_ports_end, last_used_port);
-			}
-			// try to find the port
-			socket_port = socket_ports_find(global_sockets, ntohs(address_in->sin_port));
-			if(socket_port){
-				// already used
-				return EADDRINUSE;
-			}
-			// if bound
-			if(socket->port){
-				// release the port
-				socket_port_release(global_sockets, socket);
-			}
-			socket->port = -1;
-			return socket_bind_insert(global_sockets, socket, ntohs(address_in->sin_port));
-			break;
+	switch (address->sa_family) {
+	case AF_INET:
+		if (addrlen != sizeof(struct sockaddr_in))
+			return EINVAL;
+		
+		address_in = (struct sockaddr_in *) addr;
+		// find the socket
+		socket = socket_cores_find(local_sockets, socket_id);
+		if (!socket)
+			return ENOTSOCK;
+		
+		// bind a free port?
+		if (address_in->sin_port <= 0)
+			return socket_bind_free_port(global_sockets, socket,
+			     free_ports_start, free_ports_end, last_used_port);
+		
+		// try to find the port
+		socket_port = socket_ports_find(global_sockets,
+		    ntohs(address_in->sin_port));
+		if (socket_port) {
+			// already used
+			return EADDRINUSE;
+		}
+		
+		// if bound
+		if (socket->port) {
+			// release the port
+			socket_port_release(global_sockets, socket);
+		}
+		socket->port = -1;
+		
+		return socket_bind_insert(global_sockets, socket,
+		    ntohs(address_in->sin_port));
+		
+	case AF_INET6:
 		// TODO IPv6
-	}
+		break;
+	}
+	
 	return EAFNOSUPPORT;
 }
 
-int socket_bind_free_port(socket_ports_ref global_sockets, socket_core_ref socket, int free_ports_start, int free_ports_end, int last_used_port){
+/** Binds the socket to a free port.
+ *
+ * The first free port is used.
+ *
+ * @param[in,out] global_sockets The global sockets to be updated.
+ * @param[in,out] socket The socket to be bound.
+ * @param[in] free_ports_start The minimum free port.
+ * @param[in] free_ports_end The maximum free port.
+ * @param[in] last_used_port The last used free port.
+ * @returns		EOK on success.
+ * @returns		ENOTCONN if no free port was found.
+ * @returns		Other error codes as defined for the
+ *			socket_bind_insert() function.
+ */
+int
+socket_bind_free_port(socket_ports_ref global_sockets, socket_core_ref socket,
+    int free_ports_start, int free_ports_end, int last_used_port)
+{
 	int index;
 
 	// from the last used one
 	index = last_used_port;
-	do{
-		++ index;
+	
+	do {
+		++index;
+		
 		// til the range end
-		if(index >= free_ports_end){
+		if (index >= free_ports_end) {
 			// start from the range beginning
 			index = free_ports_start - 1;
-			do{
-				++ index;
+			do {
+				++index;
 				// til the last used one
-				if(index >= last_used_port){
+				if (index >= last_used_port) {
 					// none found
 					return ENOTCONN;
 				}
-			}while(socket_ports_find(global_sockets, index) != NULL);
+			} while (socket_ports_find(global_sockets, index));
+			
 			// found, break immediately
 			break;
 		}
-	}while(socket_ports_find(global_sockets, index) != NULL);
+		
+	} while (socket_ports_find(global_sockets, index));
+	
 	return socket_bind_insert(global_sockets, socket, index);
 }
 
 /** Tries to find a new free socket identifier.
- *  @param[in] local_sockets The local sockets to be searched.
- *  @param[in] positive A value indicating whether a positive identifier is requested. A negative identifier is requested if set to false.
- *	@returns The new socket identifier.
- *  @returns ELIMIT if there is no socket identifier available.
- */
-static int socket_generate_new_id(socket_cores_ref local_sockets, int positive){
+ *
+ * @param[in] local_sockets The local sockets to be searched.
+ * @param[in] positive	A value indicating whether a positive identifier is
+ *			requested. A negative identifier is requested if set to
+ *			false.
+ * @returns		The new socket identifier.
+ * @returns		ELIMIT if there is no socket identifier available.
+ */
+static int socket_generate_new_id(socket_cores_ref local_sockets, int positive)
+{
 	int socket_id;
 	int count;
@@ -270,28 +375,45 @@
 	count = 0;
 //	socket_id = socket_globals.last_id;
-	do{
-		if(count < SOCKET_ID_TRIES){
+	do {
+		if (count < SOCKET_ID_TRIES) {
 			socket_id = rand() % INT_MAX;
-			++ count;
-		}else if(count == SOCKET_ID_TRIES){
+			++count;
+		} else if (count == SOCKET_ID_TRIES) {
 			socket_id = 1;
-			++ count;
+			++count;
 		// only this branch for last_id
-		}else{
-			if(socket_id < INT_MAX){
+		} else {
+			if (socket_id < INT_MAX) {
 				++ socket_id;
-/*			}else if(socket_globals.last_id){
+/*			} else if(socket_globals.last_id) {
 *				socket_globals.last_id = 0;
 *				socket_id = 1;
-*/			}else{
+*/			} else {
 				return ELIMIT;
 			}
 		}
-	}while(socket_cores_find(local_sockets, ((positive ? 1 : -1) * socket_id)));
+	} while (socket_cores_find(local_sockets,
+	    ((positive ? 1 : -1) * socket_id)));
+	
 //	last_id = socket_id
 	return socket_id;
 }
 
-int socket_create(socket_cores_ref local_sockets, int app_phone, void * specific_data, int * socket_id){
+/** Creates a new socket.
+ *
+ * @param[in,out] local_sockets The local sockets to be updated.
+ * @param[in] app_phone	The application phone.
+ * @param[in] specific_data The socket specific data.
+ * @param[in,out] socket_id The new socket identifier. A new identifier is
+ *			chosen if set to zero or negative. A negative identifier
+ *			is chosen if set to negative.
+ * @returns		EOK on success.
+ * @returns		EINVAL if the socket_id parameter is NULL.
+ * @returns		ENOMEM if there is not enough memory left.
+ */
+int
+socket_create(socket_cores_ref local_sockets, int app_phone,
+    void *specific_data, int *socket_id)
+{
 	ERROR_DECLARE;
 
@@ -300,24 +422,23 @@
 	int positive;
 
-	if(! socket_id){
+	if (!socket_id)
 		return EINVAL;
-	}
+	
 	// store the socket
-	if(*socket_id <= 0){
+	if (*socket_id <= 0) {
 		positive = (*socket_id == 0);
 		*socket_id = socket_generate_new_id(local_sockets, positive);
-		if(*socket_id <= 0){
-			return * socket_id;
-		}
-		if(! positive){
+		if (*socket_id <= 0)
+			return *socket_id;
+		if (!positive)
 			*socket_id *= -1;
-		}
-	}else if(socket_cores_find(local_sockets, * socket_id)){
+	} else if(socket_cores_find(local_sockets, *socket_id)) {
 		return EEXIST;
 	}
+	
 	socket = (socket_core_ref) malloc(sizeof(*socket));
-	if(! socket){
+	if (!socket)
 		return ENOMEM;
-	}
+	
 	// initialize
 	socket->phone = app_phone;
@@ -326,16 +447,18 @@
 	socket->key_length = 0;
 	socket->specific_data = specific_data;
-	if(ERROR_OCCURRED(dyn_fifo_initialize(&socket->received, SOCKET_INITIAL_RECEIVED_SIZE))){
+	if (ERROR_OCCURRED(dyn_fifo_initialize(&socket->received,
+	    SOCKET_INITIAL_RECEIVED_SIZE))) {
 		free(socket);
 		return ERROR_CODE;
 	}
-	if(ERROR_OCCURRED(dyn_fifo_initialize(&socket->accepted, SOCKET_INITIAL_ACCEPTED_SIZE))){
+	if (ERROR_OCCURRED(dyn_fifo_initialize(&socket->accepted,
+	    SOCKET_INITIAL_ACCEPTED_SIZE))) {
 		dyn_fifo_destroy(&socket->received);
 		free(socket);
 		return ERROR_CODE;
 	}
-	socket->socket_id = * socket_id;
+	socket->socket_id = *socket_id;
 	res = socket_cores_add(local_sockets, socket->socket_id, socket);
-	if(res < 0){
+	if (res < 0) {
 		dyn_fifo_destroy(&socket->received);
 		dyn_fifo_destroy(&socket->accepted);
@@ -343,8 +466,27 @@
 		return res;
 	}
+	
 	return EOK;
 }
 
-int socket_destroy(int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket)){
+/** Destroys the socket.
+ *
+ * If the socket is bound, the port is released.
+ * Releases all buffered packets, calls the release function and removes the
+ * socket from the local sockets.
+ *
+ * @param[in] packet_phone The packet server phone to release buffered packets.
+ * @param[in] socket_id	The socket identifier.
+ * @param[in,out] local_sockets The local sockets to be updated.
+ * @param[in,out] global_sockets The global sockets to be updated.
+ * @param[in] socket_release The client release callback function.
+ * @returns		EOK on success.
+ * @returns		ENOTSOCK if the socket is not found.
+ */
+int
+socket_destroy(int packet_phone, int socket_id, socket_cores_ref local_sockets,
+    socket_ports_ref global_sockets,
+    void (*socket_release)(socket_core_ref socket))
+{
 	socket_core_ref socket;
 	int accepted_id;
@@ -352,112 +494,183 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+	
 	// destroy all accepted sockets
-	while((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0){
-		socket_destroy(packet_phone, accepted_id, local_sockets, global_sockets, socket_release);
-	}
-	socket_destroy_core(packet_phone, socket, local_sockets, global_sockets, socket_release);
+	while ((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0)
+		socket_destroy(packet_phone, accepted_id, local_sockets,
+		    global_sockets, socket_release);
+	
+	socket_destroy_core(packet_phone, socket, local_sockets, global_sockets,
+	    socket_release);
+	
 	return EOK;
 }
 
-int socket_reply_packets(packet_t packet, size_t * length){
+/** Replies the packet or the packet queue data to the application via the
+ * socket.
+ *
+ * Uses the current message processing fibril.
+ *
+ * @param[in] packet	The packet to be transfered.
+ * @param[out] length	The total data length.
+ * @returns		EOK on success.
+ * @returns		EBADMEM if the length parameter is NULL.
+ * @returns		ENOMEM if there is not enough memory left.
+ * @returns		Other error codes as defined for the data_reply()
+ *			function.
+ */
+int socket_reply_packets(packet_t packet, size_t *length)
+{
 	ERROR_DECLARE;
 
 	packet_t next_packet;
 	size_t fragments;
-	size_t * lengths;
+	size_t *lengths;
 	size_t index;
 
-	if(! length){
+	if (!length)
 		return EBADMEM;
-	}
+
 	next_packet = pq_next(packet);
-	if(! next_packet){
+	if (!next_packet) {
 		// write all if only one fragment
-		ERROR_PROPAGATE(data_reply(packet_get_data(packet), packet_get_data_length(packet)));
+		ERROR_PROPAGATE(data_reply(packet_get_data(packet),
+		    packet_get_data_length(packet)));
 		// store the total length
 		*length = packet_get_data_length(packet);
-	}else{
+	} else {
 		// count the packet fragments
 		fragments = 1;
 		next_packet = pq_next(packet);
-		while((next_packet = pq_next(next_packet))){
-			++ fragments;
-		}
+		while ((next_packet = pq_next(next_packet)))
+			++fragments;
+		
 		// compute and store the fragment lengths
-		lengths = (size_t *) malloc(sizeof(size_t) * fragments + sizeof(size_t));
-		if(! lengths){
+		lengths = (size_t *) malloc(sizeof(size_t) * fragments +
+		    sizeof(size_t));
+		if (!lengths)
 			return ENOMEM;
-		}
+		
 		lengths[0] = packet_get_data_length(packet);
 		lengths[fragments] = lengths[0];
 		next_packet = pq_next(packet);
-		for(index = 1; index < fragments; ++ index){
+		
+		for (index = 1; index < fragments; ++index) {
 			lengths[index] = packet_get_data_length(next_packet);
 			lengths[fragments] += lengths[index];
 			next_packet = pq_next(packet);
-		}while(next_packet);
+		}
+		
 		// write the fragment lengths
-		ERROR_PROPAGATE(data_reply(lengths, sizeof(int) * (fragments + 1)));
+		ERROR_PROPAGATE(data_reply(lengths,
+		    sizeof(int) * (fragments + 1)));
 		next_packet = packet;
+		
 		// write the fragments
-		for(index = 0; index < fragments; ++ index){
-			ERROR_PROPAGATE(data_reply(packet_get_data(next_packet), lengths[index]));
+		for (index = 0; index < fragments; ++index) {
+			ERROR_PROPAGATE(data_reply(packet_get_data(next_packet),
+			    lengths[index]));
 			next_packet = pq_next(next_packet);
-		}while(next_packet);
+		}
+		
 		// store the total length
 		*length = lengths[fragments];
 		free(lengths);
 	}
+	
 	return EOK;
 }
 
-socket_core_ref socket_port_find(socket_ports_ref global_sockets, int port, const char * key, size_t key_length){
+/** Finds the bound port socket.
+ *
+ * @param[in] global_sockets The global sockets to be searched.
+ * @param[in] port	The port number.
+ * @param[in] key	The socket key identifier.
+ * @param[in] key_length The socket key length.
+ * @returns		The found socket.
+ * @returns		NULL if no socket was found.
+ */
+socket_core_ref
+socket_port_find(socket_ports_ref global_sockets, int port, const char *key,
+    size_t key_length)
+{
 	socket_port_ref socket_port;
-	socket_core_ref * socket_ref;
+	socket_core_ref *socket_ref;
 
 	socket_port = socket_ports_find(global_sockets, port);
-	if(socket_port && (socket_port->count > 0)){
-		socket_ref = socket_port_map_find(&socket_port->map, key, key_length);
-		if(socket_ref){
-			return * socket_ref;
-		}
-	}
+	if (socket_port && (socket_port->count > 0)) {
+		socket_ref = socket_port_map_find(&socket_port->map, key,
+		    key_length);
+		if (socket_ref)
+			return *socket_ref;
+	}
+	
 	return NULL;
 }
 
-void socket_port_release(socket_ports_ref global_sockets, socket_core_ref socket){
+/** Releases the socket port.
+ *
+ * If the socket is bound the port entry is released.
+ * If there are no more port entries the port is release.
+ *
+ * @param[in] global_sockets The global sockets to be updated.
+ * @param[in] socket	The socket to be unbound.
+ */
+void
+socket_port_release(socket_ports_ref global_sockets, socket_core_ref socket)
+{
 	socket_port_ref socket_port;
-	socket_core_ref * socket_ref;
-
-	if(socket->port){
-		// find ports
-		socket_port = socket_ports_find(global_sockets, socket->port);
-		if(socket_port){
-			// find the socket
-			socket_ref = socket_port_map_find(&socket_port->map, socket->key, socket->key_length);
-			if(socket_ref){
-				-- socket_port->count;
-				// release if empty
-				if(socket_port->count <= 0){
-					// destroy the map
-					socket_port_map_destroy(&socket_port->map);
-					// release the port
-					socket_ports_exclude(global_sockets, socket->port);
-				}else{
-					// remove
-					socket_port_map_exclude(&socket_port->map, socket->key, socket->key_length);
-				}
+	socket_core_ref *socket_ref;
+
+	if (!socket->port)
+		return;
+	
+	// find ports
+	socket_port = socket_ports_find(global_sockets, socket->port);
+	if (socket_port) {
+		// find the socket
+		socket_ref = socket_port_map_find(&socket_port->map,
+		    socket->key, socket->key_length);
+		
+		if (socket_ref) {
+			--socket_port->count;
+			
+			// release if empty
+			if (socket_port->count <= 0) {
+				// destroy the map
+				socket_port_map_destroy(&socket_port->map);
+				// release the port
+				socket_ports_exclude(global_sockets,
+				    socket->port);
+			} else {
+				// remove
+				socket_port_map_exclude(&socket_port->map,
+				    socket->key, socket->key_length);
 			}
 		}
-		socket->port = 0;
-		socket->key = NULL;
-		socket->key_length = 0;
-	}
-}
-
-int socket_port_add(socket_ports_ref global_sockets, int port, socket_core_ref socket, const char * key, size_t key_length){
+	}
+	
+	socket->port = 0;
+	socket->key = NULL;
+	socket->key_length = 0;
+}
+
+/** Adds the socket to an already bound port.
+ *
+ * @param[in] global_sockets The global sockets to be updated.
+ * @param[in] port	The port number to be bound to.
+ * @param[in] socket	The socket to be added.
+ * @param[in] key	The socket key identifier.
+ * @param[in] key_length The socket key length.
+ * @returns		EOK on success.
+ * @returns		ENOENT if the port is not already used.
+ * @returns		Other error codes as defined for the
+ *			socket_port_add_core() function.
+ */
+int
+socket_port_add(socket_ports_ref global_sockets, int port,
+    socket_core_ref socket, const char *key, size_t key_length)
+{
 	ERROR_DECLARE;
 
@@ -466,9 +679,11 @@
 	// find ports
 	socket_port = socket_ports_find(global_sockets, port);
-	if(! socket_port){
+	if (!socket_port)
 		return ENOENT;
-	}
+	
 	// add the socket
-	ERROR_PROPAGATE(socket_port_add_core(socket_port, socket, key, key_length));
+	ERROR_PROPAGATE(socket_port_add_core(socket_port, socket, key,
+	    key_length));
+	
 	socket->port = port;
 	return EOK;
Index: uspace/lib/net/include/socket_core.h
===================================================================
--- uspace/lib/net/include/socket_core.h	(revision 25d2de69b358d56541d2ba684af54fba71d44efe)
+++ uspace/lib/net/include/socket_core.h	(revision 137f8aa0da58c7057d098c654bc621728bd90d5f)
@@ -27,213 +27,110 @@
  */
 
-/** @addtogroup socket
+/** @addtogroup libnet 
  *  @{
  */
 
 /** @file
- *  Socket common core.
+ * Socket common core.
  */
 
-#ifndef __NET_SOCKET_CORE_H__
-#define __NET_SOCKET_CORE_H__
+#ifndef LIBNET_SOCKET_CORE_H_
+#define LIBNET_SOCKET_CORE_H_
 
 #include <sys/types.h>
-
-#include <net/in.h>
-#include <net/device.h>
 #include <adt/generic_char_map.h>
 #include <adt/dynamic_fifo.h>
 #include <adt/int_map.h>
+#include <net/in.h>
+#include <net/device.h>
 #include <net/packet.h>
 
-/** Initial size of the received packet queue.
- */
+/** Initial size of the received packet queue. */
 #define SOCKET_INITIAL_RECEIVED_SIZE	4
 
-/** Maximum size of the received packet queue.
- */
-#define SOCKET_MAX_RECEIVED_SIZE		0
+/** Maximum size of the received packet queue. */
+#define SOCKET_MAX_RECEIVED_SIZE	0
 
-/** Initial size of the sockets for acceptance queue.
- */
+/** Initial size of the sockets for acceptance queue. */
 #define SOCKET_INITIAL_ACCEPTED_SIZE	1
 
-/** Maximum size of the sockets for acceptance queue.
- */
-#define SOCKET_MAX_ACCEPTEDED_SIZE		0
+/** Maximum size of the sockets for acceptance queue. */
+#define SOCKET_MAX_ACCEPTEDED_SIZE	0
 
-/** Listening sockets' port map key.
- */
+/** Listening sockets' port map key. */
 #define SOCKET_MAP_KEY_LISTENING	"L"
 
 /** Type definition of the socket core.
- *  @see socket_core
+ * @see socket_core
  */
-typedef struct socket_core	socket_core_t;
+typedef struct socket_core socket_core_t;
 
 /** Type definition of the socket core pointer.
- *  @see socket_core
+ * @see socket_core
  */
-typedef socket_core_t *	socket_core_ref;
+typedef socket_core_t *socket_core_ref;
 
 /** Type definition of the socket port.
- *  @see socket_port
+ * @see socket_port
  */
-typedef struct socket_port	socket_port_t;
+typedef struct socket_port socket_port_t;
 
 /** Type definition of the socket port pointer.
- *  @see socket_port
+ * @see socket_port
  */
-typedef socket_port_t *	socket_port_ref;
+typedef socket_port_t *socket_port_ref;
 
-/** Socket core.
- */
-struct socket_core{
-	/** Socket identifier.
-	 */
+/** Socket core. */
+struct socket_core {
+	/** Socket identifier. */
 	int socket_id;
-	/** Client application phone.
-	 */
+	/** Client application phone. */
 	int phone;
-	/** Bound port.
-	 */
+	/** Bound port. */
 	int port;
-	/** Received packets queue.
-	 */
+	/** Received packets queue. */
 	dyn_fifo_t received;
-	/** Sockets for acceptance queue.
-	 */
+	/** Sockets for acceptance queue. */
 	dyn_fifo_t accepted;
-	/** Protocol specific data.
-	 */
-	void * specific_data;
-	/** Socket ports map key.
-	 */
-	const char * key;
-	/** Length of the Socket ports map key.
-	 */
+	/** Protocol specific data. */
+	void *specific_data;
+	/** Socket ports map key. */
+	const char *key;
+	/** Length of the Socket ports map key. */
 	size_t key_length;
 };
 
 /** Sockets map.
- *  The key is the socket identifier.
+ * The key is the socket identifier.
  */
 INT_MAP_DECLARE(socket_cores, socket_core_t);
 
 /** Bount port sockets map.
- *  The listening socket has the SOCKET_MAP_KEY_LISTENING key identifier whereas the other use the remote addresses.
+ *
+ * The listening socket has the SOCKET_MAP_KEY_LISTENING key identifier whereas
+ * the other use the remote addresses.
  */
 GENERIC_CHAR_MAP_DECLARE(socket_port_map, socket_core_ref);
 
 /** Ports map.
- *  The key is the port number.
+ * The key is the port number.
  */
 INT_MAP_DECLARE(socket_ports, socket_port_t);
 
-/** Destroys local sockets.
- *  Releases all buffered packets and calls the release function for each of the sockets.
- *  @param[in] packet_phone The packet server phone to release buffered packets.
- *  @param[in] local_sockets The local sockets to be destroyed.
- *  @param[in,out] global_sockets The global sockets to be updated.
- *  @param[in] socket_release The client release callback function.
- */
-extern void socket_cores_release(int packet_phone, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket));
-
-/** Binds the socket to the port.
- *  The address port is used if set, a free port is used if not.
- *  @param[in] local_sockets The local sockets to be searched.
- *  @param[in,out] global_sockets The global sockets to be updated.
- *  @param[in] socket_id The new socket identifier.
- *  @param[in] addr The address to be bound to.
- *  @param[in] addrlen The address length.
- *  @param[in] free_ports_start The minimum free port.
- *  @param[in] free_ports_end The maximum free port.
- *  @param[in] last_used_port The last used free port.
- *  @returns EOK on success.
- *  @returns ENOTSOCK if the socket was not found.
- *  @returns EAFNOSUPPORT if the address family is not supported.
- *  @returns EADDRINUSE if the port is already in use.
- *  @returns Other error codes as defined for the socket_bind_free_port() function.
- *  @returns Other error codes as defined for the socket_bind_insert() function.
- */
-extern int socket_bind(socket_cores_ref local_sockets, socket_ports_ref global_sockets, int socket_id, void * addr, size_t addrlen, int free_ports_start, int free_ports_end, int last_used_port);
-
-/** Binds the socket to a free port.
- *  The first free port is used.
- *  @param[in,out] global_sockets The global sockets to be updated.
- *  @param[in,out] socket The socket to be bound.
- *  @param[in] free_ports_start The minimum free port.
- *  @param[in] free_ports_end The maximum free port.
- *  @param[in] last_used_port The last used free port.
- *  @returns EOK on success.
- *  @returns ENOTCONN if no free port was found.
- *  @returns Other error codes as defined for the socket_bind_insert() function.
- */
-extern int socket_bind_free_port(socket_ports_ref global_sockets, socket_core_ref socket, int free_ports_start, int free_ports_end, int last_used_port);
-
-/** Creates a new socket.
- *  @param[in,out] local_sockets The local sockets to be updated.
- *  @param[in] app_phone The application phone.
- *  @param[in] specific_data The socket specific data.
- *  @param[in,out] socket_id The new socket identifier. A new identifier is chosen if set to zero (0) or negative. A negative identifier is chosen if set to negative.
- *  @returns EOK on success.
- *  @returns EINVAL if the socket_id parameter is NULL.
- *  @returns ENOMEM if there is not enough memory left.
- */
-extern int socket_create(socket_cores_ref local_sockets, int app_phone, void * specific_data, int * socket_id);
-
-/** Destroys the socket.
- *  If the socket is bound, the port is released.
- *  Releases all buffered packets, calls the release function and removes the socket from the local sockets.
- *  @param[in] packet_phone The packet server phone to release buffered packets.
- *  @param[in] socket_id The socket identifier.
- *  @param[in,out] local_sockets The local sockets to be updated.
- *  @param[in,out] global_sockets The global sockets to be updated.
- *  @param[in] socket_release The client release callback function.
- *  @returns EOK on success.
- *  @returns ENOTSOCK if the socket is not found.
- */
-extern int socket_destroy(int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket));
-
-/** Replies the packet or the packet queue data to the application via the socket.
- *  Uses the current message processing fibril.
- *  @param[in] packet The packet to be transfered.
- *  @param[out] length The total data length.
- *  @returns EOK on success.
- *  @returns EBADMEM if the length parameter is NULL.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns Other error codes as defined for the data_reply() function.
- */
-extern int socket_reply_packets(packet_t packet, size_t * length);
-
-/** Finds the bound port socket.
- *  @param[in] global_sockets The global sockets to be searched.
- *  @param[in] port The port number.
- *  @param[in] key The socket key identifier.
- *  @param[in] key_length The socket key length.
- *  @returns The found socket.
- *  @returns NULL if no socket was found.
- */
-extern socket_core_ref socket_port_find(socket_ports_ref global_sockets, int port, const char * key, size_t key_length);
-
-/** Releases the socket port.
- *  If the socket is bound the port entry is released.
- *  If there are no more port entries the port is release.
- *  @param[in] global_sockets The global sockets to be updated.
- *  @param[in] socket The socket to be unbound.
- */
-extern void socket_port_release(socket_ports_ref global_sockets, socket_core_ref socket);
-
-/** Adds the socket to an already bound port.
- *  @param[in] global_sockets The global sockets to be updated.
- *  @param[in] port The port number to be bound to.
- *  @param[in] socket The socket to be added.
- *  @param[in] key The socket key identifier.
- *  @param[in] key_length The socket key length.
- *  @returns EOK on success.
- *  @returns ENOENT if the port is not already used.
- *  @returns Other error codes as defined for the socket_port_add_core() function.
- */
-extern int socket_port_add(socket_ports_ref global_sockets, int port, socket_core_ref socket, const char * key, size_t key_length);
+extern void socket_cores_release(int, socket_cores_ref, socket_ports_ref,
+    void (*)(socket_core_ref));
+extern int socket_bind(socket_cores_ref, socket_ports_ref, int, void *, size_t,
+    int, int, int);
+extern int socket_bind_free_port(socket_ports_ref, socket_core_ref, int, int,
+    int);
+extern int socket_create(socket_cores_ref, int, void *, int *);
+extern int socket_destroy(int, int, socket_cores_ref, socket_ports_ref,
+    void (*)(socket_core_ref));
+extern int socket_reply_packets(packet_t, size_t *);
+extern socket_core_ref socket_port_find(socket_ports_ref, int, const char *,
+    size_t);
+extern void socket_port_release(socket_ports_ref, socket_core_ref);
+extern int socket_port_add(socket_ports_ref, int, socket_core_ref,
+    const char *, size_t);
 
 #endif
