Index: uspace/srv/net/il/ip/ip.c
===================================================================
--- uspace/srv/net/il/ip/ip.c	(revision a000878c0dee83a08f032207ffd800c201fca95b)
+++ uspace/srv/net/il/ip/ip.c	(revision aadf01ebd2c032bc8dd43a6e06ae5715e6c2710f)
@@ -108,9 +108,9 @@
 /** IP packet address length.
  */
-#define IP_ADDR							sizeof( struct sockaddr_in6 )
+#define IP_ADDR							sizeof(struct sockaddr_in6)
 
 /** IP packet prefix length.
  */
-#define IP_PREFIX						sizeof( ip_header_t )
+#define IP_PREFIX						sizeof(ip_header_t)
 
 /** IP packet suffix length.
@@ -124,5 +124,5 @@
 /** The IP localhost address.
  */
-#define IPV4_LOCALHOST_ADDRESS	htonl(( 127 << 24 ) + 1 )
+#define IPV4_LOCALHOST_ADDRESS	htonl((127 << 24) + 1)
 
 /** IP global data.
@@ -130,9 +130,9 @@
 ip_globals_t	ip_globals;
 
-DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
-
-INT_MAP_IMPLEMENT( ip_protos, ip_proto_t )
-
-GENERIC_FIELD_IMPLEMENT( ip_routes, ip_route_t )
+DEVICE_MAP_IMPLEMENT(ip_netifs, ip_netif_t)
+
+INT_MAP_IMPLEMENT(ip_protos, ip_proto_t)
+
+GENERIC_FIELD_IMPLEMENT(ip_routes, ip_route_t)
 
 /** Updates the device content length according to the new MTU value.
@@ -142,5 +142,5 @@
  *  @returns ENOENT if device is not found.
  */
-int	ip_mtu_changed_message( device_id_t device_id, size_t mtu );
+int ip_mtu_changed_message(device_id_t device_id, size_t mtu);
 
 /** Updates the device state.
@@ -150,5 +150,5 @@
  *  @returns ENOENT if device is not found.
  */
-int	ip_device_state_message( device_id_t device_id, device_state_t state );
+int ip_device_state_message(device_id_t device_id, device_state_t state);
 
 /** Returns the device packet dimensions for sending.
@@ -162,5 +162,5 @@
  *  @returns EOK on success.
  */
-int	ip_packet_size_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
+int ip_packet_size_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix);
 
 /** Registers the transport layer protocol.
@@ -175,5 +175,5 @@
  *  @returns ENOMEM if there is not enough memory left.
  */
-int	ip_register( int protocol, services_t service, int phone, tl_received_msg_t tl_received_msg );
+int ip_register(int protocol, services_t service, int phone, tl_received_msg_t tl_received_msg);
 
 /** Initializes a new network interface specific data.
@@ -192,5 +192,5 @@
  *  @returns Other error codes as defined for the nil_packet_size_req() function.
  */
-int	ip_netif_initialize( ip_netif_ref ip_netif );
+int ip_netif_initialize(ip_netif_ref ip_netif);
 
 /** Sends the packet or the packet queue via the specified route.
@@ -206,5 +206,5 @@
  *  @returns Other error codes as defined for the ip_prepare_packet() function.
  */
-int	ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest, services_t error );
+int ip_send_route(packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest, services_t error);
 
 /** Prepares the outgoing packet or the packet queue.
@@ -222,5 +222,5 @@
  *  @returns Other error codes as defined for the packet_set_addr() function.
  */
-int	ip_prepare_packet( in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination );
+int ip_prepare_packet(in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination);
 
 /** Checks the packet queue lengths and fragments the packets if needed.
@@ -235,5 +235,5 @@
  *  @returns NULL if there are no packets left.
  */
-packet_t	ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, socklen_t addr_len, services_t error );
+packet_t ip_split_packet(packet_t packet, size_t prefix, size_t content, size_t suffix, socklen_t addr_len, services_t error);
 
 /** Checks the packet length and fragments it if needed.
@@ -255,5 +255,5 @@
  *  @returns Other error codes as defined for the ip_fragment_packet_data() function.
  */
-int	ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, socklen_t addr_len );
+int ip_fragment_packet(packet_t packet, size_t length, size_t prefix, size_t suffix, socklen_t addr_len);
 
 /** Fragments the packet from the end.
@@ -271,5 +271,5 @@
  *  @returns Other error codes as defined for the pq_insert_after() function.
  */
-int	ip_fragment_packet_data( packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, const struct sockaddr * src, const struct sockaddr * dest, socklen_t addrlen );
+int ip_fragment_packet_data(packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, const struct sockaddr * src, const struct sockaddr * dest, socklen_t addrlen);
 
 /** Prefixes a middle fragment header based on the last fragment header to the packet.
@@ -279,5 +279,5 @@
  *  @returns NULL on error.
  */
-ip_header_ref	ip_create_middle_header( packet_t packet, ip_header_ref last );
+ip_header_ref ip_create_middle_header(packet_t packet, ip_header_ref last);
 
 /** Copies the fragment header.
@@ -286,5 +286,5 @@
  *  @param[in] first The original header to be copied.
  */
-void ip_create_last_header( ip_header_ref last, ip_header_ref first );
+void ip_create_last_header(ip_header_ref last, ip_header_ref first);
 
 /** Returns the network interface's IP address.
@@ -293,5 +293,5 @@
  *  @returns NULL if no IP address was found.
  */
-in_addr_t *	ip_netif_address( ip_netif_ref netif );
+in_addr_t * ip_netif_address(ip_netif_ref netif);
 
 /** Searches all network interfaces if there is a suitable route.
@@ -300,5 +300,5 @@
  *  @returns NULL if no route was found.
  */
-ip_route_ref	ip_find_route( in_addr_t destination );
+ip_route_ref ip_find_route(in_addr_t destination);
 
 /** Searches the network interfaces if there is a suitable route.
@@ -308,5 +308,5 @@
  *  @returns NULL if no route was found.
  */
-ip_route_ref	ip_netif_find_route( ip_netif_ref netif, in_addr_t destination );
+ip_route_ref ip_netif_find_route(ip_netif_ref netif, in_addr_t destination);
 
 /** Processes the received IP packet or the packet queue one by one.
@@ -321,5 +321,5 @@
  *  @returns ENOMEM if there is not enough memory left.
  */
-int	ip_receive_message( device_id_t device_id, packet_t packet );
+int ip_receive_message(device_id_t device_id, packet_t packet);
 
 /** Processes the received packet.
@@ -338,5 +338,5 @@
  *  @returns ENOENT if the packet is for another host and the routing is disabled.
  */
-int	ip_process_packet( device_id_t device_id, packet_t packet );
+int ip_process_packet(device_id_t device_id, packet_t packet);
 
 /** Returns the packet destination address from the IP header.
@@ -344,5 +344,5 @@
  *  @returns The packet destination address.
  */
-in_addr_t	ip_get_destination( ip_header_ref header );
+in_addr_t ip_get_destination(ip_header_ref header);
 
 /** Delivers the packet to the local host.
@@ -361,5 +361,5 @@
  *  @returns Other error codes as defined for the protocol specific tl_received_msg function.
  */
-int	ip_deliver_local( device_id_t device_id, packet_t packet, ip_header_ref header, services_t error );
+int ip_deliver_local(device_id_t device_id, packet_t packet, ip_header_ref header, services_t error);
 
 /** Prepares the ICMP notification packet.
@@ -374,5 +374,5 @@
  *  @returns EINVAL if the ip_prepare_icmp() fails.
  */
-int	ip_prepare_icmp_and_get_phone( services_t error, packet_t packet, ip_header_ref header );
+int ip_prepare_icmp_and_get_phone(services_t error, packet_t packet, ip_header_ref header);
 
 /** Returns the ICMP phone.
@@ -381,5 +381,5 @@
  *  @returns ENOENT if the ICMP is not registered.
  */
-int	ip_get_icmp_phone( void );
+int ip_get_icmp_phone(void);
 
 /** Prepares the ICMP notification packet.
@@ -395,5 +395,5 @@
  *  @returns Other error codes as defined for the packet_set_addr().
  */
-int	ip_prepare_icmp( packet_t packet, ip_header_ref header );
+int ip_prepare_icmp(packet_t packet, ip_header_ref header);
 
 /** Releases the packet and returns the result.
@@ -402,13 +402,13 @@
  *  @return The result parameter.
  */
-int	ip_release_and_return( packet_t packet, int result );
-
-int ip_initialize( async_client_conn_t client_connection ){
+int ip_release_and_return(packet_t packet, int result);
+
+int ip_initialize(async_client_conn_t client_connection){
 	ERROR_DECLARE;
 
-	fibril_rwlock_initialize( & ip_globals.lock );
-	fibril_rwlock_write_lock( & ip_globals.lock );
-	fibril_rwlock_initialize( & ip_globals.protos_lock );
-	fibril_rwlock_initialize( & ip_globals.netifs_lock );
+	fibril_rwlock_initialize(&ip_globals.lock);
+	fibril_rwlock_write_lock(&ip_globals.lock);
+	fibril_rwlock_initialize(&ip_globals.protos_lock);
+	fibril_rwlock_initialize(&ip_globals.netifs_lock);
 	ip_globals.packet_counter = 0;
 	ip_globals.gateway.address.s_addr = 0;
@@ -416,25 +416,27 @@
 	ip_globals.gateway.gateway.s_addr = 0;
 	ip_globals.gateway.netif = NULL;
-	ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs ));
-	ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos ));
+	ERROR_PROPAGATE(ip_netifs_initialize(&ip_globals.netifs));
+	ERROR_PROPAGATE(ip_protos_initialize(&ip_globals.protos));
 	ip_globals.client_connection = client_connection;
-	ERROR_PROPAGATE( modules_initialize( & ip_globals.modules ));
-	ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, arp_task_get_id(), arp_connect_module ));
-	fibril_rwlock_write_unlock( & ip_globals.lock );
+	ERROR_PROPAGATE(modules_initialize(&ip_globals.modules));
+	ERROR_PROPAGATE(add_module(NULL, &ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, arp_task_get_id(), arp_connect_module));
+	fibril_rwlock_write_unlock(&ip_globals.lock);
 	return EOK;
 }
 
-int ip_device_req( int il_phone, device_id_t device_id, services_t netif ){
+int ip_device_req(int il_phone, device_id_t device_id, services_t netif){
 	ERROR_DECLARE;
 
-	ip_netif_ref	ip_netif;
-	ip_route_ref	route;
-	int				index;
-	char *			data;
-
-	ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
-	if( ! ip_netif ) return ENOMEM;
-	if( ERROR_OCCURRED( ip_routes_initialize( & ip_netif->routes ))){
-		free( ip_netif );
+	ip_netif_ref ip_netif;
+	ip_route_ref route;
+	int index;
+	char * data;
+
+	ip_netif = (ip_netif_ref) malloc(sizeof(ip_netif_t));
+	if(! ip_netif){
+		return ENOMEM;
+	}
+	if(ERROR_OCCURRED(ip_routes_initialize(&ip_netif->routes))){
+		free(ip_netif);
 		return ERROR_CODE;
 	}
@@ -442,49 +444,51 @@
 	ip_netif->service = netif;
 	ip_netif->state = NETIF_STOPPED;
-	fibril_rwlock_write_lock( & ip_globals.netifs_lock );
-	if( ERROR_OCCURRED( ip_netif_initialize( ip_netif ))){
-		fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
-		ip_routes_destroy( & ip_netif->routes );
-		free( ip_netif );
+	fibril_rwlock_write_lock(&ip_globals.netifs_lock);
+	if(ERROR_OCCURRED(ip_netif_initialize(ip_netif))){
+		fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
+		ip_routes_destroy(&ip_netif->routes);
+		free(ip_netif);
 		return ERROR_CODE;
 	}
-	if( ip_netif->arp ) ++ ip_netif->arp->usage;
+	if(ip_netif->arp){
+		++ ip_netif->arp->usage;
+	}
 	// print the settings
-	printf( "New device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d\n", ip_netif->device_id, ip_netif->phone, ip_netif->ipv );
-	printf( "\tconfiguration\t= %s\n", ip_netif->dhcp ? "dhcp" : "static" );
+	printf("New device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d\n", ip_netif->device_id, ip_netif->phone, ip_netif->ipv);
+	printf("\tconfiguration\t= %s\n", ip_netif->dhcp ? "dhcp" : "static");
 	// TODO ipv6 addresses
-	data = ( char * ) malloc( INET_ADDRSTRLEN );
-	if( data ){
-		for( index = 0; index < ip_routes_count( & ip_netif->routes ); ++ index ){
-			route = ip_routes_get_index( & ip_netif->routes, index );
-			if( route ){
-				printf( "\tRouting %d:\n", index );
-				inet_ntop( AF_INET, ( uint8_t * ) & route->address.s_addr, data, INET_ADDRSTRLEN );
-				printf( "\t\taddress\t= %s\n", data );
-				inet_ntop( AF_INET, ( uint8_t * ) & route->netmask.s_addr, data, INET_ADDRSTRLEN );
-				printf( "\t\tnetmask\t= %s\n", data );
-				inet_ntop( AF_INET, ( uint8_t * ) & route->gateway.s_addr, data, INET_ADDRSTRLEN );
-				printf( "\t\tgateway\t= %s\n", data );
-			}
-		}
-		inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast.s_addr, data, INET_ADDRSTRLEN );
-		printf( "\t\tbroadcast\t= %s\n", data );
-		free( data );
-	}
-	fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+	data = (char *) malloc(INET_ADDRSTRLEN);
+	if(data){
+		for(index = 0; index < ip_routes_count(&ip_netif->routes); ++ index){
+			route = ip_routes_get_index(&ip_netif->routes, index);
+			if(route){
+				printf("\tRouting %d:\n", index);
+				inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr, data, INET_ADDRSTRLEN);
+				printf("\t\taddress\t= %s\n", data);
+				inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr, data, INET_ADDRSTRLEN);
+				printf("\t\tnetmask\t= %s\n", data);
+				inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr, data, INET_ADDRSTRLEN);
+				printf("\t\tgateway\t= %s\n", data);
+			}
+		}
+		inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, data, INET_ADDRSTRLEN);
+		printf("\t\tbroadcast\t= %s\n", data);
+		free(data);
+	}
+	fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 	return EOK;
 }
 
-int ip_netif_initialize( ip_netif_ref ip_netif ){
+int ip_netif_initialize(ip_netif_ref ip_netif){
 	ERROR_DECLARE;
 
-	measured_string_t	names[] = {{ str_dup("IPV"), 3 }, { str_dup("IP_CONFIG"), 9 }, { str_dup("IP_ADDR"), 7 }, { str_dup("IP_NETMASK"), 10 }, { str_dup("IP_GATEWAY"), 10 }, { str_dup("IP_BROADCAST"), 12 }, { str_dup("ARP"), 3 }, { str_dup("IP_ROUTING"), 10 }};
-	measured_string_ref	configuration;
-	size_t				count = sizeof( names ) / sizeof( measured_string_t );
-	char *				data;
-	measured_string_t	address;
-	int					index;
-	ip_route_ref		route;
-	in_addr_t			gateway;
+	measured_string_t names[] = {{str_dup("IPV"), 3}, {str_dup("IP_CONFIG"), 9}, {str_dup("IP_ADDR"), 7}, {str_dup("IP_NETMASK"), 10}, {str_dup("IP_GATEWAY"), 10}, {str_dup("IP_BROADCAST"), 12}, {str_dup("ARP"), 3}, {str_dup("IP_ROUTING"), 10}};
+	measured_string_ref configuration;
+	size_t count = sizeof(names) / sizeof(measured_string_t);
+	char * data;
+	measured_string_t address;
+	int index;
+	ip_route_ref route;
+	in_addr_t gateway;
 
 	ip_netif->arp = NULL;
@@ -493,20 +497,20 @@
 	ip_netif->dhcp = false;
 	ip_netif->routing = NET_DEFAULT_IP_ROUTING;
-	configuration = & names[ 0 ];
+	configuration = &names[0];
 	// get configuration
-	ERROR_PROPAGATE( net_get_device_conf_req( ip_globals.net_phone, ip_netif->device_id, & configuration, count, & data ));
-	if( configuration ){
-		if( configuration[ 0 ].value ){
-			ip_netif->ipv = strtol( configuration[ 0 ].value, NULL, 0 );
-		}
-		ip_netif->dhcp = ! str_lcmp( configuration[ 1 ].value, "dhcp", configuration[ 1 ].length );
-		if( ip_netif->dhcp ){
+	ERROR_PROPAGATE(net_get_device_conf_req(ip_globals.net_phone, ip_netif->device_id, &configuration, count, &data));
+	if(configuration){
+		if(configuration[0].value){
+			ip_netif->ipv = strtol(configuration[0].value, NULL, 0);
+		}
+		ip_netif->dhcp = ! str_lcmp(configuration[1].value, "dhcp", configuration[1].length);
+		if(ip_netif->dhcp){
 			// TODO dhcp
-			net_free_settings( configuration, data );
+			net_free_settings(configuration, data);
 			return ENOTSUP;
-		}else if( ip_netif->ipv == IPV4 ){
-			route = ( ip_route_ref ) malloc( sizeof( ip_route_t ));
-			if( ! route ){
-				net_free_settings( configuration, data );
+		}else if(ip_netif->ipv == IPV4){
+			route = (ip_route_ref) malloc(sizeof(ip_route_t));
+			if(! route){
+				net_free_settings(configuration, data);
 				return ENOMEM;
 			}
@@ -515,47 +519,47 @@
 			route->gateway.s_addr = 0;
 			route->netif = ip_netif;
-			index = ip_routes_add( & ip_netif->routes, route );
-			if( index < 0 ){
-				net_free_settings( configuration, data );
-				free( route );
+			index = ip_routes_add(&ip_netif->routes, route);
+			if(index < 0){
+				net_free_settings(configuration, data);
+				free(route);
 				return index;
 			}
-			if( ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 2 ].value, ( uint8_t * ) & route->address.s_addr ))
-			|| ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 3 ].value, ( uint8_t * ) & route->netmask.s_addr ))
-			|| ( inet_pton( AF_INET, configuration[ 4 ].value, ( uint8_t * ) & gateway.s_addr ) == EINVAL )
-			|| ( inet_pton( AF_INET, configuration[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast.s_addr ) == EINVAL )){
-				net_free_settings( configuration, data );
+			if(ERROR_OCCURRED(inet_pton(AF_INET, configuration[2].value, (uint8_t *) &route->address.s_addr))
+				|| ERROR_OCCURRED(inet_pton(AF_INET, configuration[3].value, (uint8_t *) &route->netmask.s_addr))
+				|| (inet_pton(AF_INET, configuration[4].value, (uint8_t *) &gateway.s_addr) == EINVAL)
+				|| (inet_pton(AF_INET, configuration[5].value, (uint8_t *) &ip_netif->broadcast.s_addr) == EINVAL)){
+				net_free_settings(configuration, data);
 				return EINVAL;
 			}
 		}else{
 			// TODO ipv6 in separate module
-			net_free_settings( configuration, data );
+			net_free_settings(configuration, data);
 			return ENOTSUP;
 		}
-		if( configuration[ 6 ].value ){
-			ip_netif->arp = get_running_module( & ip_globals.modules, configuration[ 6 ].value );
-			if( ! ip_netif->arp ){
-				printf( "Failed to start the arp %s\n", configuration[ 6 ].value );
-				net_free_settings( configuration, data );
+		if(configuration[6].value){
+			ip_netif->arp = get_running_module(&ip_globals.modules, configuration[6].value);
+			if(! ip_netif->arp){
+				printf("Failed to start the arp %s\n", configuration[6].value);
+				net_free_settings(configuration, data);
 				return EINVAL;
 			}
 		}
-		if( configuration[ 7 ].value ){
-			ip_netif->routing = ( configuration[ 7 ].value[ 0 ] == 'y' );
-		}
-		net_free_settings( configuration, data );
+		if(configuration[7].value){
+			ip_netif->routing = (configuration[7].value[0] == 'y');
+		}
+		net_free_settings(configuration, data);
 	}
 	// binds the netif service which also initializes the device
-	ip_netif->phone = nil_bind_service( ip_netif->service, ( ipcarg_t ) ip_netif->device_id, SERVICE_IP, ip_globals.client_connection );
-	if( ip_netif->phone < 0 ){
-		printf( "Failed to contact the nil service %d\n", ip_netif->service );
+	ip_netif->phone = nil_bind_service(ip_netif->service, (ipcarg_t) ip_netif->device_id, SERVICE_IP, ip_globals.client_connection);
+	if(ip_netif->phone < 0){
+		printf("Failed to contact the nil service %d\n", ip_netif->service);
 		return ip_netif->phone;
 	}
 	// has to be after the device netif module initialization
-	if( ip_netif->arp ){
-		if( route ){
-			address.value = ( char * ) & route->address.s_addr;
-			address.length = CONVERT_SIZE( in_addr_t, char, 1 );
-			ERROR_PROPAGATE( arp_device_req( ip_netif->arp->phone, ip_netif->device_id, SERVICE_IP, ip_netif->service, & address ));
+	if(ip_netif->arp){
+		if(route){
+			address.value = (char *) &route->address.s_addr;
+			address.length = CONVERT_SIZE(in_addr_t, char, 1);
+			ERROR_PROPAGATE(arp_device_req(ip_netif->arp->phone, ip_netif->device_id, SERVICE_IP, ip_netif->service, &address));
 		}else{
 			ip_netif->arp = 0;
@@ -563,12 +567,14 @@
 	}
 	// get packet dimensions
-	ERROR_PROPAGATE( nil_packet_size_req( ip_netif->phone, ip_netif->device_id, & ip_netif->packet_dimension ));
-	if( ip_netif->packet_dimension.content < IP_MIN_CONTENT ){
-		printf( "Maximum transmission unit %d bytes is too small, at least %d bytes are needed\n", ip_netif->packet_dimension.content, IP_MIN_CONTENT );
+	ERROR_PROPAGATE(nil_packet_size_req(ip_netif->phone, ip_netif->device_id, &ip_netif->packet_dimension));
+	if(ip_netif->packet_dimension.content < IP_MIN_CONTENT){
+		printf("Maximum transmission unit %d bytes is too small, at least %d bytes are needed\n", ip_netif->packet_dimension.content, IP_MIN_CONTENT);
 		ip_netif->packet_dimension.content = IP_MIN_CONTENT;
 	}
-	index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif );
-	if( index < 0 ) return index;
-	if( gateway.s_addr ){
+	index = ip_netifs_add(&ip_globals.netifs, ip_netif->device_id, ip_netif);
+	if(index < 0){
+		return index;
+	}
+	if(gateway.s_addr){
 		// the default gateway
 		ip_globals.gateway.address.s_addr = 0;
@@ -580,96 +586,100 @@
 }
 
-int ip_mtu_changed_message( device_id_t device_id, size_t mtu ){
-	ip_netif_ref	netif;
-
-	fibril_rwlock_write_lock( & ip_globals.netifs_lock );
-	netif = ip_netifs_find( & ip_globals.netifs, device_id );
-	if( ! netif ){
-		fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+int ip_mtu_changed_message(device_id_t device_id, size_t mtu){
+	ip_netif_ref netif;
+
+	fibril_rwlock_write_lock(&ip_globals.netifs_lock);
+	netif = ip_netifs_find(&ip_globals.netifs, device_id);
+	if(! netif){
+		fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 		return ENOENT;
 	}
 	netif->packet_dimension.content = mtu;
-	printf( "ip - device %d changed mtu to %d\n\n", device_id, mtu );
-	fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+	printf("ip - device %d changed mtu to %d\n\n", device_id, mtu);
+	fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 	return EOK;
 }
 
-int ip_device_state_message( device_id_t device_id, device_state_t state ){
-	ip_netif_ref	netif;
-
-	fibril_rwlock_write_lock( & ip_globals.netifs_lock );
+int ip_device_state_message(device_id_t device_id, device_state_t state){
+	ip_netif_ref netif;
+
+	fibril_rwlock_write_lock(&ip_globals.netifs_lock);
 	// find the device
-	netif = ip_netifs_find( & ip_globals.netifs, device_id );
-	if( ! netif ){
-		fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+	netif = ip_netifs_find(&ip_globals.netifs, device_id);
+	if(! netif){
+		fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 		return ENOENT;
 	}
 	netif->state = state;
-	printf( "ip - device %d changed state to %d\n\n", device_id, state );
-	fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+	printf("ip - device %d changed state to %d\n\n", device_id, state);
+	fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 	return EOK;
 }
 
-int ip_connect_module( services_t service ){
+int ip_connect_module(services_t service){
 	return EOK;
 }
 
-int ip_bind_service( services_t service, int protocol, services_t me, async_client_conn_t receiver, tl_received_msg_t received_msg ){
-	return ip_register( protocol, me, 0, received_msg );
-}
-
-int ip_register( int protocol, services_t service, int phone, tl_received_msg_t received_msg ){
-	ip_proto_ref	proto;
-	int				index;
-
-	if( !( protocol && service && (( phone > 0 ) || ( received_msg )))) return EINVAL;
-	proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t ));
-	if( ! proto ) return ENOMEM;
+int ip_bind_service(services_t service, int protocol, services_t me, async_client_conn_t receiver, tl_received_msg_t received_msg){
+	return ip_register(protocol, me, 0, received_msg);
+}
+
+int ip_register(int protocol, services_t service, int phone, tl_received_msg_t received_msg){
+	ip_proto_ref proto;
+	int index;
+
+	if(!(protocol && service && ((phone > 0) || (received_msg)))){
+		return EINVAL;
+	}
+	proto = (ip_proto_ref) malloc(sizeof(ip_protos_t));
+	if(! proto){
+		return ENOMEM;
+	}
 	proto->protocol = protocol;
 	proto->service = service;
 	proto->phone = phone;
 	proto->received_msg = received_msg;
-	fibril_rwlock_write_lock( & ip_globals.protos_lock );
-	index = ip_protos_add( & ip_globals.protos, proto->protocol, proto );
-	if( index < 0 ){
-		fibril_rwlock_write_unlock( & ip_globals.protos_lock );
-		free( proto );
+	fibril_rwlock_write_lock(&ip_globals.protos_lock);
+	index = ip_protos_add(&ip_globals.protos, proto->protocol, proto);
+	if(index < 0){
+		fibril_rwlock_write_unlock(&ip_globals.protos_lock);
+		free(proto);
 		return index;
 	}
-	printf( "New protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d\n", proto->protocol, proto->phone );
-	fibril_rwlock_write_unlock( & ip_globals.protos_lock );
+	printf("New protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d\n", proto->protocol, proto->phone);
+	fibril_rwlock_write_unlock(&ip_globals.protos_lock);
 	return EOK;
 }
 
-int ip_send_msg( int il_phone, device_id_t device_id, packet_t packet, services_t sender, services_t error ){
+int ip_send_msg(int il_phone, device_id_t device_id, packet_t packet, services_t sender, services_t error){
 	ERROR_DECLARE;
 
-	int					addrlen;
-	ip_netif_ref		netif;
-	ip_route_ref		route;
-	struct sockaddr *		addr;
-	struct sockaddr_in *	address_in;
+	int addrlen;
+	ip_netif_ref netif;
+	ip_route_ref route;
+	struct sockaddr * addr;
+	struct sockaddr_in * address_in;
 //	struct sockaddr_in6 *	address_in6;
-	in_addr_t *			dest;
-	in_addr_t *			src;
-	int					phone;
+	in_addr_t * dest;
+	in_addr_t * src;
+	int phone;
 
 	// addresses in the host byte order
 	// should be the next hop address or the target destination address
-	addrlen = packet_get_addr( packet, NULL, ( uint8_t ** ) & addr );
-	if( addrlen < 0 ){
-		return ip_release_and_return( packet, addrlen );
-	}
-	if(( size_t ) addrlen < sizeof( struct sockaddr )){
-		return ip_release_and_return( packet, EINVAL );
-	}
-	switch( addr->sa_family ){
+	addrlen = packet_get_addr(packet, NULL, (uint8_t **) &addr);
+	if(addrlen < 0){
+		return ip_release_and_return(packet, addrlen);
+	}
+	if((size_t) addrlen < sizeof(struct sockaddr)){
+		return ip_release_and_return(packet, EINVAL);
+	}
+	switch(addr->sa_family){
 		case AF_INET:
-			if( addrlen != sizeof( struct sockaddr_in )){
-				return ip_release_and_return( packet, EINVAL );
-			}
-			address_in = ( struct sockaddr_in * ) addr;
-			dest = & address_in->sin_addr;
-			if( ! dest->s_addr ){
+			if(addrlen != sizeof(struct sockaddr_in)){
+				return ip_release_and_return(packet, EINVAL);
+			}
+			address_in = (struct sockaddr_in *) addr;
+			dest = &address_in->sin_addr;
+			if(! dest->s_addr){
 				dest->s_addr = IPV4_LOCALHOST_ADDRESS;
 			}
@@ -677,135 +687,139 @@
 		// TODO IPv6
 /*		case AF_INET6:
-			if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
-			address_in6 = ( struct sockaddr_in6 * ) dest;
+			if(addrlen != sizeof(struct sockaddr_in6)){
+				return EINVAL;
+			}
+			address_in6 = (struct sockaddr_in6 *) dest;
 			address_in6.sin6_addr.s6_addr;
 			IPV6_LOCALHOST_ADDRESS;
 */		default:
-			return ip_release_and_return( packet, EAFNOSUPPORT );
-	}
-	fibril_rwlock_read_lock( & ip_globals.netifs_lock );
+			return ip_release_and_return(packet, EAFNOSUPPORT);
+	}
+	fibril_rwlock_read_lock(&ip_globals.netifs_lock);
 	// device specified?
-	if( device_id > 0 ){
-		netif = ip_netifs_find( & ip_globals.netifs, device_id );
-		route = ip_netif_find_route( netif, * dest );
-		if( netif && ( ! route ) && ( ip_globals.gateway.netif == netif )){
-			route = & ip_globals.gateway;
+	if(device_id > 0){
+		netif = ip_netifs_find(&ip_globals.netifs, device_id);
+		route = ip_netif_find_route(netif, * dest);
+		if(netif && (! route) && (ip_globals.gateway.netif == netif)){
+			route = &ip_globals.gateway;
 		}
 	}else{
-		route = ip_find_route( * dest );
+		route = ip_find_route(*dest);
 		netif = route ? route->netif : NULL;
 	}
-	if( !( netif && route )){
-		fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
-		phone = ip_prepare_icmp_and_get_phone( error, packet, NULL );
-		if( phone >= 0 ){
+	if(!(netif && route)){
+		fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
+		phone = ip_prepare_icmp_and_get_phone(error, packet, NULL);
+		if(phone >= 0){
 			// unreachable ICMP if no routing
-			icmp_destination_unreachable_msg( phone, ICMP_NET_UNREACH, 0, packet );
+			icmp_destination_unreachable_msg(phone, ICMP_NET_UNREACH, 0, packet);
 		}
 		return ENOENT;
 	}
-	if( error ){
+	if(error){
 		// do not send for broadcast, anycast packets or network broadcast
-		if(( ! dest->s_addr )
-		|| ( !( ~ dest->s_addr ))
-		|| ( !( ~(( dest->s_addr & ( ~ route->netmask.s_addr )) | route->netmask.s_addr )))
-		|| ( !( dest->s_addr & ( ~ route->netmask.s_addr )))){
-			return ip_release_and_return( packet, EINVAL );
-		}
-	}
-	if( route->address.s_addr == dest->s_addr ){
+		if((! dest->s_addr)
+			|| (!(~ dest->s_addr))
+			|| (!(~((dest->s_addr &(~ route->netmask.s_addr)) | route->netmask.s_addr)))
+			|| (!(dest->s_addr &(~ route->netmask.s_addr)))){
+			return ip_release_and_return(packet, EINVAL);
+		}
+	}
+	if(route->address.s_addr == dest->s_addr){
 		// find the loopback device to deliver
 		dest->s_addr = IPV4_LOCALHOST_ADDRESS;
-		route = ip_find_route( * dest );
+		route = ip_find_route(*dest);
 		netif = route ? route->netif : NULL;
-		if( !( netif && route )){
-			fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
-			phone = ip_prepare_icmp_and_get_phone( error, packet, NULL );
-			if( phone >= 0 ){
+		if(!(netif && route)){
+			fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
+			phone = ip_prepare_icmp_and_get_phone(error, packet, NULL);
+			if(phone >= 0){
 				// unreachable ICMP if no routing
-				icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
+				icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0, packet);
 			}
 			return ENOENT;
 		}
 	}
-	src = ip_netif_address( netif );
-	if( ! src ){
-		fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
-		return ip_release_and_return( packet, ENOENT );
-	}
-	ERROR_CODE = ip_send_route( packet, netif, route, src, * dest, error );
-	fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
+	src = ip_netif_address(netif);
+	if(! src){
+		fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
+		return ip_release_and_return(packet, ENOENT);
+	}
+	ERROR_CODE = ip_send_route(packet, netif, route, src, * dest, error);
+	fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
 	return ERROR_CODE;
 }
 
-in_addr_t * ip_netif_address( ip_netif_ref netif ){
-	ip_route_ref	route;
-
-	route = ip_routes_get_index( & netif->routes, 0 );
-	return route ? & route->address : NULL;
-}
-
-int ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest, services_t error ){
+in_addr_t * ip_netif_address(ip_netif_ref netif){
+	ip_route_ref route;
+
+	route = ip_routes_get_index(&netif->routes, 0);
+	return route ? &route->address : NULL;
+}
+
+int ip_send_route(packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest, services_t error){
 	ERROR_DECLARE;
 
-	measured_string_t	destination;
-	measured_string_ref	translation;
-	char *				data;
-	int					phone;
+	measured_string_t destination;
+	measured_string_ref translation;
+	char * data;
+	int phone;
 
 	// get destination hardware address
-	if( netif->arp && ( route->address.s_addr != dest.s_addr )){
-		destination.value = route->gateway.s_addr ? ( char * ) & route->gateway.s_addr : ( char * ) & dest.s_addr;
-		destination.length = CONVERT_SIZE( dest.s_addr, char, 1 );
-		if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ))){
-//			sleep( 1 );
-//			ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ));
-			pq_release( ip_globals.net_phone, packet_get_id( packet ));
+	if(netif->arp && (route->address.s_addr != dest.s_addr)){
+		destination.value = route->gateway.s_addr ? (char *) &route->gateway.s_addr : (char *) &dest.s_addr;
+		destination.length = CONVERT_SIZE(dest.s_addr, char, 1);
+		if(ERROR_OCCURRED(arp_translate_req(netif->arp->phone, netif->device_id, SERVICE_IP, &destination, &translation, &data))){
+//			sleep(1);
+//			ERROR_PROPAGATE(arp_translate_req(netif->arp->phone, netif->device_id, SERVICE_IP, &destination, &translation, &data));
+			pq_release(ip_globals.net_phone, packet_get_id(packet));
 			return ERROR_CODE;
 		}
-		if( !( translation && translation->value )){
-			if( translation ){
-				free( translation );
-				free( data );
-			}
-			phone = ip_prepare_icmp_and_get_phone( error, packet, NULL );
-			if( phone >= 0 ){
+		if(!(translation && translation->value)){
+			if(translation){
+				free(translation);
+				free(data);
+			}
+			phone = ip_prepare_icmp_and_get_phone(error, packet, NULL);
+			if(phone >= 0){
 				// unreachable ICMP if no routing
-				icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
+				icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0, packet);
 			}
 			return EINVAL;
 		}
 	}else translation = NULL;
-	if( ERROR_OCCURRED( ip_prepare_packet( src, dest, packet, translation ))){
-		pq_release( ip_globals.net_phone, packet_get_id( packet ));
+	if(ERROR_OCCURRED(ip_prepare_packet(src, dest, packet, translation))){
+		pq_release(ip_globals.net_phone, packet_get_id(packet));
 	}else{
-		packet = ip_split_packet( packet, netif->packet_dimension.prefix, netif->packet_dimension.content, netif->packet_dimension.suffix, netif->packet_dimension.addr_len, error );
-		if( packet ){
-			nil_send_msg( netif->phone, netif->device_id, packet, SERVICE_IP );
-		}
-	}
-	if( translation ){
-		free( translation );
-		free( data );
+		packet = ip_split_packet(packet, netif->packet_dimension.prefix, netif->packet_dimension.content, netif->packet_dimension.suffix, netif->packet_dimension.addr_len, error);
+		if(packet){
+			nil_send_msg(netif->phone, netif->device_id, packet, SERVICE_IP);
+		}
+	}
+	if(translation){
+		free(translation);
+		free(data);
 	}
 	return ERROR_CODE;
 }
 
-int ip_prepare_packet( in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination ){
+int ip_prepare_packet(in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination){
 	ERROR_DECLARE;
 
-	size_t				length;
-	ip_header_ref		header;
-	ip_header_ref		last_header;
-	ip_header_ref		middle_header;
-	packet_t			next;
-
-	length = packet_get_data_length( packet );
-	if(( length < sizeof( ip_header_t )) || ( length > IP_MAX_CONTENT )) return EINVAL;
-	header = ( ip_header_ref ) packet_get_data( packet );
-	if( destination ){
-		ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
+	size_t length;
+	ip_header_ref header;
+	ip_header_ref last_header;
+	ip_header_ref middle_header;
+	packet_t next;
+
+	length = packet_get_data_length(packet);
+	if((length < sizeof(ip_header_t)) || (length > IP_MAX_CONTENT)){
+		return EINVAL;
+	}
+	header = (ip_header_ref) packet_get_data(packet);
+	if(destination){
+		ERROR_PROPAGATE(packet_set_addr(packet, NULL, (uint8_t *) destination->value, CONVERT_SIZE(char, uint8_t, destination->length)));
 	}else{
-		ERROR_PROPAGATE( packet_set_addr( packet, NULL, NULL, 0 ));
+		ERROR_PROPAGATE(packet_set_addr(packet, NULL, NULL, 0));
 	}
 	header->version = IPV4;
@@ -813,157 +827,175 @@
 	header->fragment_offset_low = 0;
 	header->header_checksum = 0;
-	if( source ) header->source_address = source->s_addr;
+	if(source){
+		header->source_address = source->s_addr;
+	}
 	header->destination_address = dest.s_addr;
-	fibril_rwlock_write_lock( & ip_globals.lock );
+	fibril_rwlock_write_lock(&ip_globals.lock);
 	++ ip_globals.packet_counter;
-	header->identification = htons( ip_globals.packet_counter );
-	fibril_rwlock_write_unlock( & ip_globals.lock );
-//	length = packet_get_data_length( packet );
-	if( pq_next( packet )){
-		last_header = ( ip_header_ref ) malloc( IP_HEADER_LENGTH( header ));
-		if( ! last_header ) return ENOMEM;
-		ip_create_last_header( last_header, header );
-		next = pq_next( packet );
-		while( pq_next( next )){
-			middle_header = ( ip_header_ref ) packet_prefix( next, IP_HEADER_LENGTH( last_header ));
-			if( ! middle_header ) return ENOMEM;
-			memcpy( middle_header, last_header, IP_HEADER_LENGTH( last_header ));
+	header->identification = htons(ip_globals.packet_counter);
+	fibril_rwlock_write_unlock(&ip_globals.lock);
+//	length = packet_get_data_length(packet);
+	if(pq_next(packet)){
+		last_header = (ip_header_ref) malloc(IP_HEADER_LENGTH(header));
+		if(! last_header){
+			return ENOMEM;
+		}
+		ip_create_last_header(last_header, header);
+		next = pq_next(packet);
+		while(pq_next(next)){
+			middle_header = (ip_header_ref) packet_prefix(next, IP_HEADER_LENGTH(last_header));
+			if(! middle_header){
+				return ENOMEM;
+			}
+			memcpy(middle_header, last_header, IP_HEADER_LENGTH(last_header));
 			header->flags |= IPFLAG_MORE_FRAGMENTS;
-			middle_header->total_length = htons( packet_get_data_length( next ));
-			middle_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH( length );
-			middle_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW( length );
-			middle_header->header_checksum = IP_HEADER_CHECKSUM( middle_header );
-			if( destination ){
-				ERROR_PROPAGATE( packet_set_addr( next, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
-			}
-			length += packet_get_data_length( next );
-			next = pq_next( next );
-		}
-		middle_header = ( ip_header_ref ) packet_prefix( next, IP_HEADER_LENGTH( last_header ));
-		if( ! middle_header ) return ENOMEM;
-		memcpy( middle_header, last_header, IP_HEADER_LENGTH( last_header ));
-		middle_header->total_length = htons( packet_get_data_length( next ));
-		middle_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH( length );
-		middle_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW( length );
-		middle_header->header_checksum = IP_HEADER_CHECKSUM( middle_header );
-		if( destination ){
-			ERROR_PROPAGATE( packet_set_addr( next, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
-		}
-		length += packet_get_data_length( next );
-		free( last_header );
+			middle_header->total_length = htons(packet_get_data_length(next));
+			middle_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length);
+			middle_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW(length);
+			middle_header->header_checksum = IP_HEADER_CHECKSUM(middle_header);
+			if(destination){
+				ERROR_PROPAGATE(packet_set_addr(next, NULL, (uint8_t *) destination->value, CONVERT_SIZE(char, uint8_t, destination->length)));
+			}
+			length += packet_get_data_length(next);
+			next = pq_next(next);
+		}
+		middle_header = (ip_header_ref) packet_prefix(next, IP_HEADER_LENGTH(last_header));
+		if(! middle_header){
+			return ENOMEM;
+		}
+		memcpy(middle_header, last_header, IP_HEADER_LENGTH(last_header));
+		middle_header->total_length = htons(packet_get_data_length(next));
+		middle_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length);
+		middle_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW(length);
+		middle_header->header_checksum = IP_HEADER_CHECKSUM(middle_header);
+		if(destination){
+			ERROR_PROPAGATE(packet_set_addr(next, NULL, (uint8_t *) destination->value, CONVERT_SIZE(char, uint8_t, destination->length)));
+		}
+		length += packet_get_data_length(next);
+		free(last_header);
 		header->flags |= IPFLAG_MORE_FRAGMENTS;
 	}
-	header->total_length = htons( length );
+	header->total_length = htons(length);
 	// unnecessary for all protocols
-	header->header_checksum = IP_HEADER_CHECKSUM( header );
+	header->header_checksum = IP_HEADER_CHECKSUM(header);
 	return EOK;
 }
 
-int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
+int ip_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
 	ERROR_DECLARE;
 
-	packet_t				packet;
-	struct sockaddr *		addr;
-	size_t					addrlen;
-	ip_pseudo_header_ref	header;
-	size_t					headerlen;
-
-	* answer_count = 0;
-	switch( IPC_GET_METHOD( * call )){
+	packet_t packet;
+	struct sockaddr * addr;
+	size_t addrlen;
+	ip_pseudo_header_ref header;
+	size_t headerlen;
+
+	*answer_count = 0;
+	switch(IPC_GET_METHOD(*call)){
 		case IPC_M_PHONE_HUNGUP:
 			return EOK;
 		case NET_IL_DEVICE:
-			return ip_device_req( 0, IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
+			return ip_device_req(0, IPC_GET_DEVICE(call), IPC_GET_SERVICE(call));
 		case IPC_M_CONNECT_TO_ME:
-			return ip_register( IL_GET_PROTO( call ), IL_GET_SERVICE( call ), IPC_GET_PHONE( call ), NULL );
+			return ip_register(IL_GET_PROTO(call), IL_GET_SERVICE(call), IPC_GET_PHONE(call), NULL);
 		case NET_IL_SEND:
-			ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
-			return ip_send_msg( 0, IPC_GET_DEVICE( call ), packet, 0, IPC_GET_ERROR( call ));
+			ERROR_PROPAGATE(packet_translate(ip_globals.net_phone, &packet, IPC_GET_PACKET(call)));
+			return ip_send_msg(0, IPC_GET_DEVICE(call), packet, 0, IPC_GET_ERROR(call));
 		case NET_IL_DEVICE_STATE:
-			return ip_device_state_message( IPC_GET_DEVICE( call ), IPC_GET_STATE( call ));
+			return ip_device_state_message(IPC_GET_DEVICE(call), IPC_GET_STATE(call));
 		case NET_IL_RECEIVED:
-			ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
-			return ip_receive_message( IPC_GET_DEVICE( call ), packet );
+			ERROR_PROPAGATE(packet_translate(ip_globals.net_phone, &packet, IPC_GET_PACKET(call)));
+			return ip_receive_message(IPC_GET_DEVICE(call), packet);
 		case NET_IP_RECEIVED_ERROR:
-			ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
-			return ip_received_error_msg( 0, IPC_GET_DEVICE( call ), packet, IPC_GET_TARGET( call ), IPC_GET_ERROR( call ));
+			ERROR_PROPAGATE(packet_translate(ip_globals.net_phone, &packet, IPC_GET_PACKET(call)));
+			return ip_received_error_msg(0, IPC_GET_DEVICE(call), packet, IPC_GET_TARGET(call), IPC_GET_ERROR(call));
 		case NET_IP_ADD_ROUTE:
-			return ip_add_route_req( 0, IPC_GET_DEVICE( call ), IP_GET_ADDRESS( call ), IP_GET_NETMASK( call ), IP_GET_GATEWAY( call ));
+			return ip_add_route_req(0, IPC_GET_DEVICE(call), IP_GET_ADDRESS(call), IP_GET_NETMASK(call), IP_GET_GATEWAY(call));
 		case NET_IP_SET_GATEWAY:
-			return ip_set_gateway_req( 0, IPC_GET_DEVICE( call ), IP_GET_GATEWAY( call ));
+			return ip_set_gateway_req(0, IPC_GET_DEVICE(call), IP_GET_GATEWAY(call));
 		case NET_IP_GET_ROUTE:
-			ERROR_PROPAGATE( data_receive(( void ** ) & addr, & addrlen ));
-			ERROR_PROPAGATE( ip_get_route_req( 0, IP_GET_PROTOCOL( call ), addr, ( socklen_t ) addrlen, IPC_SET_DEVICE( answer ), & header, & headerlen ));
-			* IP_SET_HEADERLEN( answer ) = headerlen;
-			* answer_count = 2;
-			if( ! ERROR_OCCURRED( data_reply( & headerlen, sizeof( headerlen )))){
-				ERROR_CODE = data_reply( header, headerlen );
-			}
-			free( header );
+			ERROR_PROPAGATE(data_receive((void **) &addr, &addrlen));
+			ERROR_PROPAGATE(ip_get_route_req(0, IP_GET_PROTOCOL(call), addr, (socklen_t) addrlen, IPC_SET_DEVICE(answer), &header, &headerlen));
+			*IP_SET_HEADERLEN(answer) = headerlen;
+			*answer_count = 2;
+			if(! ERROR_OCCURRED(data_reply(&headerlen, sizeof(headerlen)))){
+				ERROR_CODE = data_reply(header, headerlen);
+			}
+			free(header);
 			return ERROR_CODE;
 		case NET_IL_PACKET_SPACE:
-			ERROR_PROPAGATE( ip_packet_size_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
-			* answer_count = 3;
+			ERROR_PROPAGATE(ip_packet_size_message(IPC_GET_DEVICE(call), IPC_SET_ADDR(answer), IPC_SET_PREFIX(answer), IPC_SET_CONTENT(answer), IPC_SET_SUFFIX(answer)));
+			*answer_count = 3;
 			return EOK;
 		case NET_IL_MTU_CHANGED:
-			return ip_mtu_changed_message( IPC_GET_DEVICE( call ), IPC_GET_MTU( call ));
+			return ip_mtu_changed_message(IPC_GET_DEVICE(call), IPC_GET_MTU(call));
 	}
 	return ENOTSUP;
 }
 
-int ip_packet_size_req( int ip_phone, device_id_t device_id, packet_dimension_ref packet_dimension ){
-	if( ! packet_dimension ) return EBADMEM;
-	return ip_packet_size_message( device_id, & packet_dimension->addr_len, & packet_dimension->prefix, & packet_dimension->content, & packet_dimension->suffix );
-}
-
-int ip_packet_size_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
-	ip_netif_ref	netif;
-	int				index;
-
-	if( !( addr_len && prefix && content && suffix )) return EBADMEM;
-	* content = IP_MAX_CONTENT - IP_PREFIX;
-	fibril_rwlock_read_lock( & ip_globals.netifs_lock );
-	if( device_id < 0 ){
-		* addr_len = IP_ADDR;
-		* prefix = 0;
-		* suffix = 0;
-		for( index = ip_netifs_count( & ip_globals.netifs ) - 1; index >= 0; -- index ){
-			netif = ip_netifs_get_index( & ip_globals.netifs, index );
-			if( netif ){
-				if( netif->packet_dimension.addr_len > * addr_len ) * addr_len = netif->packet_dimension.addr_len;
-				if( netif->packet_dimension.prefix > * prefix ) * prefix = netif->packet_dimension.prefix;
-				if( netif->packet_dimension.suffix > * suffix ) * suffix = netif->packet_dimension.suffix;
-			}
-		}
-		* prefix = * prefix + IP_PREFIX;
-		* suffix = * suffix + IP_SUFFIX;
+int ip_packet_size_req(int ip_phone, device_id_t device_id, packet_dimension_ref packet_dimension){
+	if(! packet_dimension){
+		return EBADMEM;
+	}
+	return ip_packet_size_message(device_id, &packet_dimension->addr_len, &packet_dimension->prefix, &packet_dimension->content, &packet_dimension->suffix);
+}
+
+int ip_packet_size_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix){
+	ip_netif_ref netif;
+	int index;
+
+	if(!(addr_len && prefix && content && suffix)){
+		return EBADMEM;
+	}
+	*content = IP_MAX_CONTENT - IP_PREFIX;
+	fibril_rwlock_read_lock(&ip_globals.netifs_lock);
+	if(device_id < 0){
+		*addr_len = IP_ADDR;
+		*prefix = 0;
+		*suffix = 0;
+		for(index = ip_netifs_count(&ip_globals.netifs) - 1; index >= 0; -- index){
+			netif = ip_netifs_get_index(&ip_globals.netifs, index);
+			if(netif){
+				if(netif->packet_dimension.addr_len > * addr_len){
+					*addr_len = netif->packet_dimension.addr_len;
+				}
+				if(netif->packet_dimension.prefix > * prefix){
+					*prefix = netif->packet_dimension.prefix;
+				}
+				if(netif->packet_dimension.suffix > * suffix){
+					*suffix = netif->packet_dimension.suffix;
+				}
+			}
+		}
+		*prefix = * prefix + IP_PREFIX;
+		*suffix = * suffix + IP_SUFFIX;
 	}else{
-		netif = ip_netifs_find( & ip_globals.netifs, device_id );
-		if( ! netif ){
-			fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
+		netif = ip_netifs_find(&ip_globals.netifs, device_id);
+		if(! netif){
+			fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
 			return ENOENT;
 		}
-		* addr_len = ( netif->packet_dimension.addr_len > IP_ADDR ) ? netif->packet_dimension.addr_len : IP_ADDR;
-		* prefix = netif->packet_dimension.prefix + IP_PREFIX;
-		* suffix = netif->packet_dimension.suffix + IP_SUFFIX;
-	}
-	fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
+		*addr_len = (netif->packet_dimension.addr_len > IP_ADDR) ? netif->packet_dimension.addr_len : IP_ADDR;
+		*prefix = netif->packet_dimension.prefix + IP_PREFIX;
+		*suffix = netif->packet_dimension.suffix + IP_SUFFIX;
+	}
+	fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
 	return EOK;
 }
 
-int ip_add_route_req( int ip_phone, device_id_t device_id, in_addr_t address, in_addr_t netmask, in_addr_t gateway ){
-	ip_route_ref	route;
-	ip_netif_ref	netif;
-	int				index;
-
-	fibril_rwlock_write_lock( & ip_globals.netifs_lock );
-	netif = ip_netifs_find( & ip_globals.netifs, device_id );
-	if( ! netif ){
-		fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+int ip_add_route_req(int ip_phone, device_id_t device_id, in_addr_t address, in_addr_t netmask, in_addr_t gateway){
+	ip_route_ref route;
+	ip_netif_ref netif;
+	int index;
+
+	fibril_rwlock_write_lock(&ip_globals.netifs_lock);
+	netif = ip_netifs_find(&ip_globals.netifs, device_id);
+	if(! netif){
+		fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 		return ENOENT;
 	}
-	route = ( ip_route_ref ) malloc( sizeof( ip_route_t ));
-	if( ! route ){
-		fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+	route = (ip_route_ref) malloc(sizeof(ip_route_t));
+	if(! route){
+		fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 		return ENOMEM;
 	}
@@ -972,37 +1004,41 @@
 	route->gateway.s_addr = gateway.s_addr;
 	route->netif = netif;
-	index = ip_routes_add( & netif->routes, route );
-	if( index < 0 ) free( route );
-	fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+	index = ip_routes_add(&netif->routes, route);
+	if(index < 0){
+		free(route);
+	}
+	fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 	return index;
 }
 
-ip_route_ref ip_find_route( in_addr_t destination ){
-	int				index;
-	ip_route_ref	route;
-	ip_netif_ref	netif;
+ip_route_ref ip_find_route(in_addr_t destination){
+	int index;
+	ip_route_ref route;
+	ip_netif_ref netif;
 
 	// start with the last netif - the newest one
-	index = ip_netifs_count( & ip_globals.netifs ) - 1;
-	while( index >= 0 ){
-		netif = ip_netifs_get_index( & ip_globals.netifs, index );
-		if( netif && ( netif->state == NETIF_ACTIVE )){
-			route = ip_netif_find_route( netif, destination );
-			if( route ) return route;
+	index = ip_netifs_count(&ip_globals.netifs) - 1;
+	while(index >= 0){
+		netif = ip_netifs_get_index(&ip_globals.netifs, index);
+		if(netif && (netif->state == NETIF_ACTIVE)){
+			route = ip_netif_find_route(netif, destination);
+			if(route){
+				return route;
+			}
 		}
 		-- index;
 	}
-	return & ip_globals.gateway;
-}
-
-ip_route_ref ip_netif_find_route( ip_netif_ref netif, in_addr_t destination ){
-	int				index;
-	ip_route_ref	route;
-
-	if( netif ){
+	return &ip_globals.gateway;
+}
+
+ip_route_ref ip_netif_find_route(ip_netif_ref netif, in_addr_t destination){
+	int index;
+	ip_route_ref route;
+
+	if(netif){
 		// start with the first one - the direct route
-		for( index = 0; index < ip_routes_count( & netif->routes ); ++ index ){
-			route = ip_routes_get_index( & netif->routes, index );
-			if( route && (( route->address.s_addr & route->netmask.s_addr ) == ( destination.s_addr & route->netmask.s_addr ))){
+		for(index = 0; index < ip_routes_count(&netif->routes); ++ index){
+			route = ip_routes_get_index(&netif->routes, index);
+			if(route && ((route->address.s_addr &route->netmask.s_addr) == (destination.s_addr &route->netmask.s_addr))){
 				return route;
 			}
@@ -1012,11 +1048,11 @@
 }
 
-int ip_set_gateway_req( int ip_phone, device_id_t device_id, in_addr_t gateway ){
-	ip_netif_ref	netif;
-
-	fibril_rwlock_write_lock( & ip_globals.netifs_lock );
-	netif = ip_netifs_find( & ip_globals.netifs, device_id );
-	if( ! netif ){
-		fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+int ip_set_gateway_req(int ip_phone, device_id_t device_id, in_addr_t gateway){
+	ip_netif_ref netif;
+
+	fibril_rwlock_write_lock(&ip_globals.netifs_lock);
+	netif = ip_netifs_find(&ip_globals.netifs, device_id);
+	if(! netif){
+		fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 		return ENOENT;
 	}
@@ -1025,37 +1061,37 @@
 	ip_globals.gateway.gateway.s_addr = gateway.s_addr;
 	ip_globals.gateway.netif = netif;
-	fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
+	fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
 	return EOK;
 }
 
-packet_t ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, socklen_t addr_len, services_t error ){
-	size_t			length;
-	packet_t		next;
-	packet_t		new_packet;
-	int				result;
-	int				phone;
+packet_t ip_split_packet(packet_t packet, size_t prefix, size_t content, size_t suffix, socklen_t addr_len, services_t error){
+	size_t length;
+	packet_t next;
+	packet_t new_packet;
+	int result;
+	int phone;
 
 	next = packet;
 	// check all packets
-	while( next ){
-		length = packet_get_data_length( next );
+	while(next){
+		length = packet_get_data_length(next);
 		// too long?
-		if( length > content ){
-			result = ip_fragment_packet( next, content, prefix, suffix, addr_len );
-			if( result != EOK ){
-				new_packet = pq_detach( next );
-				if( next == packet ){
+		if(length > content){
+			result = ip_fragment_packet(next, content, prefix, suffix, addr_len);
+			if(result != EOK){
+				new_packet = pq_detach(next);
+				if(next == packet){
 					// the new first packet of the queue
 					packet = new_packet;
 				}
 				// fragmentation needed?
-				if( result == EPERM ){
-					phone = ip_prepare_icmp_and_get_phone( error, next, NULL );
-					if( phone >= 0 ){
+				if(result == EPERM){
+					phone = ip_prepare_icmp_and_get_phone(error, next, NULL);
+					if(phone >= 0){
 						// fragmentation necessary ICMP
-						icmp_destination_unreachable_msg( phone, ICMP_FRAG_NEEDED, content, next );
+						icmp_destination_unreachable_msg(phone, ICMP_FRAG_NEEDED, content, next);
 					}
 				}else{
-					pq_release( ip_globals.net_phone, packet_get_id( next ));
+					pq_release(ip_globals.net_phone, packet_get_id(next));
 				}
 				next = new_packet;
@@ -1063,119 +1099,133 @@
 			}
 		}
-		next = pq_next( next );
+		next = pq_next(next);
 	}
 	return packet;
 }
 
-int ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, socklen_t addr_len ){
+int ip_fragment_packet(packet_t packet, size_t length, size_t prefix, size_t suffix, socklen_t addr_len){
 	ERROR_DECLARE;
 
-	packet_t		new_packet;
-	ip_header_ref	header;
-	ip_header_ref	middle_header;
-	ip_header_ref	last_header;
-	struct sockaddr *		src;
-	struct sockaddr *		dest;
-	socklen_t		addrlen;
-	int				result;
-
-	result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest );
-	if( result <= 0 ) return EINVAL;
-	addrlen = ( socklen_t ) result;
-	if( packet_get_data_length( packet ) <= sizeof( ip_header_t )) return ENOMEM;
+	packet_t new_packet;
+	ip_header_ref header;
+	ip_header_ref middle_header;
+	ip_header_ref last_header;
+	struct sockaddr * src;
+	struct sockaddr * dest;
+	socklen_t addrlen;
+	int result;
+
+	result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
+	if(result <= 0){
+		return EINVAL;
+	}
+	addrlen = (socklen_t) result;
+	if(packet_get_data_length(packet) <= sizeof(ip_header_t)){
+		return ENOMEM;
+	}
 	// get header
-	header = ( ip_header_ref ) packet_get_data( packet );
-	if( ! header ) return EINVAL;
+	header = (ip_header_ref) packet_get_data(packet);
+	if(! header){
+		return EINVAL;
+	}
 	// fragmentation forbidden?
-	if( header->flags & IPFLAG_DONT_FRAGMENT ){
+	if(header->flags &IPFLAG_DONT_FRAGMENT){
 		return EPERM;
 	}
 	// create the last fragment
-	new_packet = packet_get_4( ip_globals.net_phone, prefix, length, suffix, (( addrlen > addr_len ) ? addrlen : addr_len ));
-	if( ! new_packet ) return ENOMEM;
+	new_packet = packet_get_4(ip_globals.net_phone, prefix, length, suffix, ((addrlen > addr_len) ? addrlen : addr_len));
+	if(! new_packet){
+		return ENOMEM;
+	}
 	// allocate as much as originally
-	last_header = ( ip_header_ref ) packet_suffix( new_packet, IP_HEADER_LENGTH( header ));
-	if( ! last_header ){
-		return ip_release_and_return( packet, ENOMEM );
-	}
-	ip_create_last_header( last_header, header );
+	last_header = (ip_header_ref) packet_suffix(new_packet, IP_HEADER_LENGTH(header));
+	if(! last_header){
+		return ip_release_and_return(packet, ENOMEM);
+	}
+	ip_create_last_header(last_header, header);
 	// trim the unused space
-	if( ERROR_OCCURRED( packet_trim( new_packet, 0, IP_HEADER_LENGTH( header ) - IP_HEADER_LENGTH( last_header )))){
-		return ip_release_and_return( packet, ERROR_CODE );
+	if(ERROR_OCCURRED(packet_trim(new_packet, 0, IP_HEADER_LENGTH(header) - IP_HEADER_LENGTH(last_header)))){
+		return ip_release_and_return(packet, ERROR_CODE);
 	}
 	// biggest multiple of 8 lower than content
 	// TODO even fragmentation?
-	length = length & ( ~ 0x7 );// ( content / 8 ) * 8
-	if( ERROR_OCCURRED( ip_fragment_packet_data( packet, new_packet, header, last_header, (( IP_HEADER_DATA_LENGTH( header ) - (( length - IP_HEADER_LENGTH( header )) & ( ~ 0x7 ))) % (( length - IP_HEADER_LENGTH( last_header )) & ( ~ 0x7 ))), src, dest, addrlen ))){
-		return ip_release_and_return( packet, ERROR_CODE );
+	length = length &(~ 0x7);// (content / 8) * 8
+	if(ERROR_OCCURRED(ip_fragment_packet_data(packet, new_packet, header, last_header, ((IP_HEADER_DATA_LENGTH(header) - ((length - IP_HEADER_LENGTH(header)) &(~ 0x7))) % ((length - IP_HEADER_LENGTH(last_header)) &(~ 0x7))), src, dest, addrlen))){
+		return ip_release_and_return(packet, ERROR_CODE);
 	}
 	// mark the first as fragmented
 	header->flags |= IPFLAG_MORE_FRAGMENTS;
 	// create middle framgents
-	while( IP_TOTAL_LENGTH( header ) > length ){
-		new_packet = packet_get_4( ip_globals.net_phone, prefix, length, suffix, (( addrlen >= addr_len ) ? addrlen : addr_len ));
-		if( ! new_packet ) return ENOMEM;
-		middle_header = ip_create_middle_header( new_packet, last_header );
-		if( ! middle_header ){
-			return ip_release_and_return( packet, ENOMEM );
-		}
-		if( ERROR_OCCURRED( ip_fragment_packet_data( packet, new_packet, header, middle_header, ( length - IP_HEADER_LENGTH( middle_header )) & ( ~ 0x7 ), src, dest, addrlen ))){
-			return ip_release_and_return( packet, ERROR_CODE );
+	while(IP_TOTAL_LENGTH(header) > length){
+		new_packet = packet_get_4(ip_globals.net_phone, prefix, length, suffix, ((addrlen >= addr_len) ? addrlen : addr_len));
+		if(! new_packet){
+			return ENOMEM;
+		}
+		middle_header = ip_create_middle_header(new_packet, last_header);
+		if(! middle_header){
+			return ip_release_and_return(packet, ENOMEM);
+		}
+		if(ERROR_OCCURRED(ip_fragment_packet_data(packet, new_packet, header, middle_header, (length - IP_HEADER_LENGTH(middle_header)) &(~ 0x7), src, dest, addrlen))){
+			return ip_release_and_return(packet, ERROR_CODE);
 		}
 	}
 	// finish the first fragment
-	header->header_checksum = IP_HEADER_CHECKSUM( header );
+	header->header_checksum = IP_HEADER_CHECKSUM(header);
 	return EOK;
 }
 
-int ip_fragment_packet_data( packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, const struct sockaddr * src, const struct sockaddr * dest, socklen_t addrlen ){
+int ip_fragment_packet_data(packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, const struct sockaddr * src, const struct sockaddr * dest, socklen_t addrlen){
 	ERROR_DECLARE;
 
-	void *			data;
-	size_t			offset;
-
-	data = packet_suffix( new_packet, length );
-	if( ! data ) return ENOMEM;
-	memcpy( data, (( void * ) header ) + IP_TOTAL_LENGTH( header ) - length, length );
-	ERROR_PROPAGATE( packet_trim( packet, 0, length ));
-	header->total_length = htons( IP_TOTAL_LENGTH( header ) - length );
-	new_header->total_length = htons( IP_HEADER_LENGTH( new_header ) + length );
-	offset = IP_FRAGMENT_OFFSET( header ) + IP_HEADER_DATA_LENGTH( header );
-	new_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH( offset );
-	new_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW( offset );
-	new_header->header_checksum = IP_HEADER_CHECKSUM( new_header );
-	ERROR_PROPAGATE( packet_set_addr( new_packet, ( const uint8_t * ) src, ( const uint8_t * ) dest, addrlen ));
-	return pq_insert_after( packet, new_packet );
-}
-
-ip_header_ref ip_create_middle_header( packet_t packet, ip_header_ref last ){
-	ip_header_ref	middle;
-
-	middle = ( ip_header_ref ) packet_suffix( packet, IP_HEADER_LENGTH( last ));
-	if( ! middle ) return NULL;
-	memcpy( middle, last, IP_HEADER_LENGTH( last ));
+	void * data;
+	size_t offset;
+
+	data = packet_suffix(new_packet, length);
+	if(! data){
+		return ENOMEM;
+	}
+	memcpy(data, ((void *) header) + IP_TOTAL_LENGTH(header) - length, length);
+	ERROR_PROPAGATE(packet_trim(packet, 0, length));
+	header->total_length = htons(IP_TOTAL_LENGTH(header) - length);
+	new_header->total_length = htons(IP_HEADER_LENGTH(new_header) + length);
+	offset = IP_FRAGMENT_OFFSET(header) + IP_HEADER_DATA_LENGTH(header);
+	new_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH(offset);
+	new_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW(offset);
+	new_header->header_checksum = IP_HEADER_CHECKSUM(new_header);
+	ERROR_PROPAGATE(packet_set_addr(new_packet, (const uint8_t *) src, (const uint8_t *) dest, addrlen));
+	return pq_insert_after(packet, new_packet);
+}
+
+ip_header_ref ip_create_middle_header(packet_t packet, ip_header_ref last){
+	ip_header_ref middle;
+
+	middle = (ip_header_ref) packet_suffix(packet, IP_HEADER_LENGTH(last));
+	if(! middle){
+		return NULL;
+	}
+	memcpy(middle, last, IP_HEADER_LENGTH(last));
 	middle->flags |= IPFLAG_MORE_FRAGMENTS;
 	return middle;
 }
 
-void ip_create_last_header( ip_header_ref last, ip_header_ref first ){
-	ip_option_ref	option;
-	size_t			next;
-	size_t			length;
+void ip_create_last_header(ip_header_ref last, ip_header_ref first){
+	ip_option_ref option;
+	size_t next;
+	size_t length;
 
 	// copy first itself
-	memcpy( last, first, sizeof( ip_header_t ));
-	length = sizeof( ip_header_t );
-	next = sizeof( ip_header_t );
+	memcpy(last, first, sizeof(ip_header_t));
+	length = sizeof(ip_header_t);
+	next = sizeof(ip_header_t);
 	// process all ip options
-	while( next < first->header_length ){
-		option = ( ip_option_ref ) ((( uint8_t * ) first ) + next );
+	while(next < first->header_length){
+		option = (ip_option_ref) (((uint8_t *) first) + next);
 		// skip end or noop
-		if(( option->type == IPOPT_END ) || ( option->type == IPOPT_NOOP )){
+		if((option->type == IPOPT_END) || (option->type == IPOPT_NOOP)){
 			++ next;
 		}else{
 			// copy if said so or skip
-			if( IPOPT_COPIED( option->type )){
-				memcpy((( uint8_t * ) last ) + length, (( uint8_t * ) first ) + next, option->length );
+			if(IPOPT_COPIED(option->type)){
+				memcpy(((uint8_t *) last) + length, ((uint8_t *) first) + next, option->length);
 				length += option->length;
 			}
@@ -1185,6 +1235,6 @@
 	}
 	// align 4 byte boundary
-	if( length % 4 ){
-		bzero((( uint8_t * ) last ) + length, 4 - ( length % 4 ));
+	if(length % 4){
+		bzero(((uint8_t *) last) + length, 4 - (length % 4));
 		last->header_length = length / 4 + 1;
 	}else{
@@ -1194,92 +1244,92 @@
 }
 
-int ip_receive_message( device_id_t device_id, packet_t packet ){
-	packet_t		next;
+int ip_receive_message(device_id_t device_id, packet_t packet){
+	packet_t next;
 
 	do{
-		next = pq_detach( packet );
-		ip_process_packet( device_id, packet );
+		next = pq_detach(packet);
+		ip_process_packet(device_id, packet);
 		packet = next;
-	}while( packet );
+	}while(packet);
 	return EOK;
 }
 
-int ip_process_packet( device_id_t device_id, packet_t packet ){
+int ip_process_packet(device_id_t device_id, packet_t packet){
 	ERROR_DECLARE;
 
-	ip_header_ref	header;
-	in_addr_t		dest;
-	ip_route_ref	route;
-	int				phone;
-	struct sockaddr *	addr;
-	struct sockaddr_in	addr_in;
+	ip_header_ref header;
+	in_addr_t dest;
+	ip_route_ref route;
+	int phone;
+	struct sockaddr * addr;
+	struct sockaddr_in addr_in;
 //	struct sockaddr_in	addr_in6;
-	socklen_t		addrlen;
-
-	header = ( ip_header_ref ) packet_get_data( packet );
-	if( ! header ){
-		return ip_release_and_return( packet, ENOMEM );
+	socklen_t addrlen;
+
+	header = (ip_header_ref) packet_get_data(packet);
+	if(! header){
+		return ip_release_and_return(packet, ENOMEM);
 	}
 	// checksum
-	if(( header->header_checksum ) && ( IP_HEADER_CHECKSUM( header ) != IP_CHECKSUM_ZERO )){
-		phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
-		if( phone >= 0 ){
+	if((header->header_checksum) && (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)){
+		phone = ip_prepare_icmp_and_get_phone(0, packet, header);
+		if(phone >= 0){
 			// checksum error ICMP
-			icmp_parameter_problem_msg( phone, ICMP_PARAM_POINTER, (( size_t ) (( void * ) & header->header_checksum )) - (( size_t ) (( void * ) header )), packet );
+			icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->header_checksum)) - ((size_t) ((void *) header)), packet);
 		}
 		return EINVAL;
 	}
-	if( header->ttl <= 1 ){
-		phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
-		if( phone >= 0 ){
+	if(header->ttl <= 1){
+		phone = ip_prepare_icmp_and_get_phone(0, packet, header);
+		if(phone >= 0){
 			// ttl oxceeded ICMP
-			icmp_time_exceeded_msg( phone, ICMP_EXC_TTL, packet );
+			icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet);
 		}
 		return EINVAL;
 	}
 	// process ipopt and get destination
-	dest = ip_get_destination( header );
+	dest = ip_get_destination(header);
 	// set the addrination address
-	switch( header->version ){
+	switch(header->version){
 		case IPVERSION:
-			addrlen = sizeof( addr_in );
-			bzero( & addr_in, addrlen );
+			addrlen = sizeof(addr_in);
+			bzero(&addr_in, addrlen);
 			addr_in.sin_family = AF_INET;
-			memcpy( & addr_in.sin_addr.s_addr, & dest, sizeof( dest ));
-			addr = ( struct sockaddr * ) & addr_in;
+			memcpy(&addr_in.sin_addr.s_addr, &dest, sizeof(dest));
+			addr = (struct sockaddr *) &addr_in;
 			break;
 /*		case IPv6VERSION:
-			addrlen = sizeof( dest_in6 );
-			bzero( & dest_in6, addrlen );
+			addrlen = sizeof(dest_in6);
+			bzero(&dest_in6, addrlen);
 			dest_in6.sin6_family = AF_INET6;
-			memcpy( & dest_in6.sin6_addr.s6_addr, );
-			dest = ( struct sockaddr * ) & dest_in;
+			memcpy(&dest_in6.sin6_addr.s6_addr,);
+			dest = (struct sockaddr *) &dest_in;
 			break;
 */		default:
-			return ip_release_and_return( packet, EAFNOSUPPORT );
-	}
-	ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) & addr, addrlen ));
-	route = ip_find_route( dest );
-	if( ! route ){
-		phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
-		if( phone >= 0 ){
+			return ip_release_and_return(packet, EAFNOSUPPORT);
+	}
+	ERROR_PROPAGATE(packet_set_addr(packet, NULL, (uint8_t *) &addr, addrlen));
+	route = ip_find_route(dest);
+	if(! route){
+		phone = ip_prepare_icmp_and_get_phone(0, packet, header);
+		if(phone >= 0){
 			// unreachable ICMP
-			icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
+			icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0, packet);
 		}
 		return ENOENT;
 	}
-	if( route->address.s_addr == dest.s_addr ){
+	if(route->address.s_addr == dest.s_addr){
 		// local delivery
-		return ip_deliver_local( device_id, packet, header, 0 );
+		return ip_deliver_local(device_id, packet, header, 0);
 	}else{
 		// only if routing enabled
-		if( route->netif->routing ){
+		if(route->netif->routing){
 			-- header->ttl;
-			return ip_send_route( packet, route->netif, route, NULL, dest, 0 );
+			return ip_send_route(packet, route->netif, route, NULL, dest, 0);
 		}else{
-			phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
-			if( phone >= 0 ){
+			phone = ip_prepare_icmp_and_get_phone(0, packet, header);
+			if(phone >= 0){
 				// unreachable ICMP if no routing
-				icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
+				icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0, packet);
 			}
 			return ENOENT;
@@ -1288,116 +1338,116 @@
 }
 
-int ip_received_error_msg( int ip_phone, device_id_t device_id, packet_t packet, services_t target, services_t error ){
-	uint8_t *			data;
-	int					offset;
-	icmp_type_t			type;
-	icmp_code_t			code;
-	ip_netif_ref		netif;
-	measured_string_t	address;
-	ip_route_ref		route;
-	ip_header_ref		header;
-
-	switch( error ){
+int ip_received_error_msg(int ip_phone, device_id_t device_id, packet_t packet, services_t target, services_t error){
+	uint8_t * data;
+	int offset;
+	icmp_type_t type;
+	icmp_code_t code;
+	ip_netif_ref netif;
+	measured_string_t address;
+	ip_route_ref route;
+	ip_header_ref header;
+
+	switch(error){
 		case SERVICE_ICMP:
-			offset = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
-			if( offset < 0 ){
-				return ip_release_and_return( packet, ENOMEM );
-			}
-			data = packet_get_data( packet );
-			header = ( ip_header_ref )( data + offset );
+			offset = icmp_client_process_packet(packet, &type, &code, NULL, NULL);
+			if(offset < 0){
+				return ip_release_and_return(packet, ENOMEM);
+			}
+			data = packet_get_data(packet);
+			header = (ip_header_ref)(data + offset);
 			// destination host unreachable?
-			if(( type == ICMP_DEST_UNREACH ) && ( code == ICMP_HOST_UNREACH )){
-				fibril_rwlock_read_lock( & ip_globals.netifs_lock );
-				netif = ip_netifs_find( & ip_globals.netifs, device_id );
-				if( netif && netif->arp ){
-					route = ip_routes_get_index( & netif->routes, 0 );
+			if((type == ICMP_DEST_UNREACH) && (code == ICMP_HOST_UNREACH)){
+				fibril_rwlock_read_lock(&ip_globals.netifs_lock);
+				netif = ip_netifs_find(&ip_globals.netifs, device_id);
+				if(netif && netif->arp){
+					route = ip_routes_get_index(&netif->routes, 0);
 					// from the same network?
-					if( route && (( route->address.s_addr & route->netmask.s_addr ) == ( header->destination_address & route->netmask.s_addr ))){
+					if(route && ((route->address.s_addr &route->netmask.s_addr) == (header->destination_address &route->netmask.s_addr))){
 						// clear the ARP mapping if any
-						address.value = ( char * ) & header->destination_address;
-						address.length = CONVERT_SIZE( uint8_t, char, sizeof( header->destination_address ));
-						arp_clear_address_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address );
+						address.value = (char *) &header->destination_address;
+						address.length = CONVERT_SIZE(uint8_t, char, sizeof(header->destination_address));
+						arp_clear_address_req(netif->arp->phone, netif->device_id, SERVICE_IP, &address);
 					}
 				}
-				fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
+				fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
 			}
 			break;
 		default:
-			return ip_release_and_return( packet, ENOTSUP );
-	}
-	return ip_deliver_local( device_id, packet, header, error );
-}
-
-int ip_deliver_local( device_id_t device_id, packet_t packet, ip_header_ref header, services_t error ){
+			return ip_release_and_return(packet, ENOTSUP);
+	}
+	return ip_deliver_local(device_id, packet, header, error);
+}
+
+int ip_deliver_local(device_id_t device_id, packet_t packet, ip_header_ref header, services_t error){
 	ERROR_DECLARE;
 
-	ip_proto_ref	proto;
-	int				phone;
-	services_t		service;
-	tl_received_msg_t	received_msg;
-	struct sockaddr *	src;
-	struct sockaddr *	dest;
-	struct sockaddr_in	src_in;
-	struct sockaddr_in	dest_in;
+	ip_proto_ref proto;
+	int phone;
+	services_t service;
+	tl_received_msg_t received_msg;
+	struct sockaddr * src;
+	struct sockaddr * dest;
+	struct sockaddr_in src_in;
+	struct sockaddr_in dest_in;
 //	struct sockaddr_in	src_in6;
 //	struct sockaddr_in	dest_in6;
-	socklen_t		addrlen;
-
-	if(( header->flags & IPFLAG_MORE_FRAGMENTS ) || IP_FRAGMENT_OFFSET( header )){
+	socklen_t addrlen;
+
+	if((header->flags &IPFLAG_MORE_FRAGMENTS) || IP_FRAGMENT_OFFSET(header)){
 		// TODO fragmented
 		return ENOTSUP;
 	}else{
-		switch( header->version ){
+		switch(header->version){
 			case IPVERSION:
-				addrlen = sizeof( src_in );
-				bzero( & src_in, addrlen );
+				addrlen = sizeof(src_in);
+				bzero(&src_in, addrlen);
 				src_in.sin_family = AF_INET;
-				memcpy( & dest_in, & src_in, addrlen );
-				memcpy( & src_in.sin_addr.s_addr, & header->source_address, sizeof( header->source_address ));
-				memcpy( & dest_in.sin_addr.s_addr, & header->destination_address, sizeof( header->destination_address ));
-				src = ( struct sockaddr * ) & src_in;
-				dest = ( struct sockaddr * ) & dest_in;
+				memcpy(&dest_in, &src_in, addrlen);
+				memcpy(&src_in.sin_addr.s_addr, &header->source_address, sizeof(header->source_address));
+				memcpy(&dest_in.sin_addr.s_addr, &header->destination_address, sizeof(header->destination_address));
+				src = (struct sockaddr *) &src_in;
+				dest = (struct sockaddr *) &dest_in;
 				break;
 /*			case IPv6VERSION:
-				addrlen = sizeof( src_in6 );
-				bzero( & src_in6, addrlen );
+				addrlen = sizeof(src_in6);
+				bzero(&src_in6, addrlen);
 				src_in6.sin6_family = AF_INET6;
-				memcpy( & dest_in6, & src_in6, addrlen );
-				memcpy( & src_in6.sin6_addr.s6_addr, );
-				memcpy( & dest_in6.sin6_addr.s6_addr, );
-				src = ( struct sockaddr * ) & src_in;
-				dest = ( struct sockaddr * ) & dest_in;
+				memcpy(&dest_in6, &src_in6, addrlen);
+				memcpy(&src_in6.sin6_addr.s6_addr,);
+				memcpy(&dest_in6.sin6_addr.s6_addr,);
+				src = (struct sockaddr *) &src_in;
+				dest = (struct sockaddr *) &dest_in;
 				break;
 */			default:
-				return ip_release_and_return( packet, EAFNOSUPPORT );
-		}
-		if( ERROR_OCCURRED( packet_set_addr( packet, ( uint8_t * ) src, ( uint8_t * ) dest, addrlen ))){
-			return ip_release_and_return( packet, ERROR_CODE );
+				return ip_release_and_return(packet, EAFNOSUPPORT);
+		}
+		if(ERROR_OCCURRED(packet_set_addr(packet, (uint8_t *) src, (uint8_t *) dest, addrlen))){
+			return ip_release_and_return(packet, ERROR_CODE);
 		}
 		// trim padding if present
-		if(( ! error ) && ( IP_TOTAL_LENGTH( header ) < packet_get_data_length( packet ))){
-			if( ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - IP_TOTAL_LENGTH( header )))){
-				return ip_release_and_return( packet, ERROR_CODE );
-			}
-		}
-		fibril_rwlock_read_lock( & ip_globals.protos_lock );
-		proto = ip_protos_find( & ip_globals.protos, header->protocol );
-		if( ! proto ){
-			fibril_rwlock_read_unlock( & ip_globals.protos_lock );
-			phone = ip_prepare_icmp_and_get_phone( error, packet, header );
-			if( phone >= 0 ){
+		if((! error) && (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))){
+			if(ERROR_OCCURRED(packet_trim(packet, 0, packet_get_data_length(packet) - IP_TOTAL_LENGTH(header)))){
+				return ip_release_and_return(packet, ERROR_CODE);
+			}
+		}
+		fibril_rwlock_read_lock(&ip_globals.protos_lock);
+		proto = ip_protos_find(&ip_globals.protos, header->protocol);
+		if(! proto){
+			fibril_rwlock_read_unlock(&ip_globals.protos_lock);
+			phone = ip_prepare_icmp_and_get_phone(error, packet, header);
+			if(phone >= 0){
 				// unreachable ICMP
-				icmp_destination_unreachable_msg( phone, ICMP_PROT_UNREACH, 0, packet );
+				icmp_destination_unreachable_msg(phone, ICMP_PROT_UNREACH, 0, packet);
 			}
 			return ENOENT;
 		}
-		if( proto->received_msg ){
+		if(proto->received_msg){
 			service = proto->service;
 			received_msg = proto->received_msg;
-			fibril_rwlock_read_unlock( & ip_globals.protos_lock );
-			ERROR_CODE = received_msg( device_id, packet, service, error );
+			fibril_rwlock_read_unlock(&ip_globals.protos_lock);
+			ERROR_CODE = received_msg(device_id, packet, service, error);
 		}else{
-			ERROR_CODE = tl_received_msg( proto->phone, device_id, packet, proto->service, error );
-			fibril_rwlock_read_unlock( & ip_globals.protos_lock );
+			ERROR_CODE = tl_received_msg(proto->phone, device_id, packet, proto->service, error);
+			fibril_rwlock_read_unlock(&ip_globals.protos_lock);
 		}
 		return ERROR_CODE;
@@ -1405,6 +1455,6 @@
 }
 
-in_addr_t ip_get_destination( ip_header_ref header ){
-	in_addr_t	destination;
+in_addr_t ip_get_destination(ip_header_ref header){
+	in_addr_t destination;
 
 	// TODO search set ipopt route?
@@ -1413,97 +1463,107 @@
 }
 
-int ip_prepare_icmp( packet_t packet, ip_header_ref header ){
-	packet_t	next;
-	struct sockaddr *	dest;
-	struct sockaddr_in	dest_in;
+int ip_prepare_icmp(packet_t packet, ip_header_ref header){
+	packet_t next;
+	struct sockaddr * dest;
+	struct sockaddr_in dest_in;
 //	struct sockaddr_in	dest_in6;
-	socklen_t		addrlen;
+	socklen_t addrlen;
 
 	// detach the first packet and release the others
-	next = pq_detach( packet );
-	if( next ){
-		pq_release( ip_globals.net_phone, packet_get_id( next ));
-	}
-	if( ! header ){
-		if( packet_get_data_length( packet ) <= sizeof( ip_header_t )) return ENOMEM;
+	next = pq_detach(packet);
+	if(next){
+		pq_release(ip_globals.net_phone, packet_get_id(next));
+	}
+	if(! header){
+		if(packet_get_data_length(packet) <= sizeof(ip_header_t)){
+			return ENOMEM;
+		}
 		// get header
-		header = ( ip_header_ref ) packet_get_data( packet );
-		if( ! header ) return EINVAL;
+		header = (ip_header_ref) packet_get_data(packet);
+		if(! header){
+			return EINVAL;
+		}
 	}
 	// only for the first fragment
-	if( IP_FRAGMENT_OFFSET( header )) return EINVAL;
+	if(IP_FRAGMENT_OFFSET(header)){
+		return EINVAL;
+	}
 	// not for the ICMP protocol
-	if( header->protocol == IPPROTO_ICMP ){
+	if(header->protocol == IPPROTO_ICMP){
 		return EPERM;
 	}
 	// set the destination address
-	switch( header->version ){
+	switch(header->version){
 		case IPVERSION:
-			addrlen = sizeof( dest_in );
-			bzero( & dest_in, addrlen );
+			addrlen = sizeof(dest_in);
+			bzero(&dest_in, addrlen);
 			dest_in.sin_family = AF_INET;
-			memcpy( & dest_in.sin_addr.s_addr, & header->source_address, sizeof( header->source_address ));
-			dest = ( struct sockaddr * ) & dest_in;
+			memcpy(&dest_in.sin_addr.s_addr, &header->source_address, sizeof(header->source_address));
+			dest = (struct sockaddr *) &dest_in;
 			break;
 /*		case IPv6VERSION:
-			addrlen = sizeof( dest_in6 );
-			bzero( & dest_in6, addrlen );
+			addrlen = sizeof(dest_in6);
+			bzero(&dest_in6, addrlen);
 			dest_in6.sin6_family = AF_INET6;
-			memcpy( & dest_in6.sin6_addr.s6_addr, );
-			dest = ( struct sockaddr * ) & dest_in;
+			memcpy(&dest_in6.sin6_addr.s6_addr,);
+			dest = (struct sockaddr *) &dest_in;
 			break;
 */		default:
 			return EAFNOSUPPORT;
 	}
-	return packet_set_addr( packet, NULL, ( uint8_t * ) dest, addrlen );
-}
-
-int ip_get_icmp_phone( void ){
-	ip_proto_ref	proto;
-	int				phone;
-
-	fibril_rwlock_read_lock( & ip_globals.protos_lock );
-	proto = ip_protos_find( & ip_globals.protos, IPPROTO_ICMP );
+	return packet_set_addr(packet, NULL, (uint8_t *) dest, addrlen);
+}
+
+int ip_get_icmp_phone(void){
+	ip_proto_ref proto;
+	int phone;
+
+	fibril_rwlock_read_lock(&ip_globals.protos_lock);
+	proto = ip_protos_find(&ip_globals.protos, IPPROTO_ICMP);
 	phone = proto ? proto->phone : ENOENT;
-	fibril_rwlock_read_unlock( & ip_globals.protos_lock );
+	fibril_rwlock_read_unlock(&ip_globals.protos_lock);
 	return phone;
 }
 
-int ip_prepare_icmp_and_get_phone( services_t error, packet_t packet, ip_header_ref header ){
-	int	phone;
+int ip_prepare_icmp_and_get_phone(services_t error, packet_t packet, ip_header_ref header){
+	int phone;
 
 	phone = ip_get_icmp_phone();
-	if( error || ( phone < 0 ) || ip_prepare_icmp( packet, header )){
-		return ip_release_and_return( packet, EINVAL );
+	if(error || (phone < 0) || ip_prepare_icmp(packet, header)){
+		return ip_release_and_return(packet, EINVAL);
 	}
 	return phone;
 }
 
-int	ip_release_and_return( packet_t packet, int result ){
-	pq_release( ip_globals.net_phone, packet_get_id( packet ));
+int ip_release_and_return(packet_t packet, int result){
+	pq_release(ip_globals.net_phone, packet_get_id(packet));
 	return result;
 }
 
-int ip_get_route_req( int ip_phone, ip_protocol_t protocol, const struct sockaddr * destination, socklen_t addrlen, device_id_t * device_id, ip_pseudo_header_ref * header, size_t * headerlen ){
-	struct sockaddr_in *	address_in;
+int ip_get_route_req(int ip_phone, ip_protocol_t protocol, const struct sockaddr * destination, socklen_t addrlen, device_id_t * device_id, ip_pseudo_header_ref * header, size_t * headerlen){
+	struct sockaddr_in * address_in;
 //	struct sockaddr_in6 *	address_in6;
-	in_addr_t *				dest;
-	in_addr_t *				src;
-	ip_route_ref			route;
-	ipv4_pseudo_header_ref	header_in;
-
-	if( !( destination && ( addrlen > 0 ))) return EINVAL;
-	if( !( device_id && header && headerlen )) return EBADMEM;
-	if(( size_t ) addrlen < sizeof( struct sockaddr )){
+	in_addr_t * dest;
+	in_addr_t * src;
+	ip_route_ref route;
+	ipv4_pseudo_header_ref header_in;
+
+	if(!(destination && (addrlen > 0))){
 		return EINVAL;
 	}
-	switch( destination->sa_family ){
+	if(!(device_id && header && headerlen)){
+		return EBADMEM;
+	}
+	if((size_t) addrlen < sizeof(struct sockaddr)){
+		return EINVAL;
+	}
+	switch(destination->sa_family){
 		case AF_INET:
-			if( addrlen != sizeof( struct sockaddr_in )){
+			if(addrlen != sizeof(struct sockaddr_in)){
 				return EINVAL;
 			}
-			address_in = ( struct sockaddr_in * ) destination;
-			dest = & address_in->sin_addr;
-			if( ! dest->s_addr ){
+			address_in = (struct sockaddr_in *) destination;
+			dest = &address_in->sin_addr;
+			if(! dest->s_addr){
 				dest->s_addr = IPV4_LOCALHOST_ADDRESS;
 			}
@@ -1511,28 +1571,32 @@
 		// TODO IPv6
 /*		case AF_INET6:
-			if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
-			address_in6 = ( struct sockaddr_in6 * ) dest;
+			if(addrlen != sizeof(struct sockaddr_in6)){
+				return EINVAL;
+			}
+			address_in6 = (struct sockaddr_in6 *) dest;
 			address_in6.sin6_addr.s6_addr;
 */		default:
 			return EAFNOSUPPORT;
 	}
-	fibril_rwlock_read_lock( & ip_globals.lock );
-	route = ip_find_route( * dest );
-	if( !( route && route->netif )){
-		fibril_rwlock_read_unlock( & ip_globals.lock );
+	fibril_rwlock_read_lock(&ip_globals.lock);
+	route = ip_find_route(*dest);
+	if(!(route && route->netif)){
+		fibril_rwlock_read_unlock(&ip_globals.lock);
 		return ENOENT;
 	}
-	* device_id = route->netif->device_id;
-	src = ip_netif_address( route->netif );
-	fibril_rwlock_read_unlock( & ip_globals.lock );
-	* headerlen = sizeof( * header_in );
-	header_in = ( ipv4_pseudo_header_ref ) malloc( * headerlen );
-	if( ! header_in ) return ENOMEM;
-	bzero( header_in, * headerlen );
+	*device_id = route->netif->device_id;
+	src = ip_netif_address(route->netif);
+	fibril_rwlock_read_unlock(&ip_globals.lock);
+	*headerlen = sizeof(*header_in);
+	header_in = (ipv4_pseudo_header_ref) malloc(*headerlen);
+	if(! header_in){
+		return ENOMEM;
+	}
+	bzero(header_in, * headerlen);
 	header_in->destination_address = dest->s_addr;
 	header_in->source_address = src->s_addr;
 	header_in->protocol = protocol;
 	header_in->data_length = 0;
-	* header = ( ip_pseudo_header_ref ) header_in;
+	*header = (ip_pseudo_header_ref) header_in;
 	return EOK;
 }
