Index: uspace/srv/net/il/arp/arp.c
===================================================================
--- uspace/srv/net/il/arp/arp.c	(revision fe8dfa6849229e829003d0fb83de58148a0aa45d)
+++ uspace/srv/net/il/arp/arp.c	(revision fe5a9fc4b7984d760b15c9c1dd964a5b0f138bed)
@@ -72,5 +72,5 @@
 
 /** Number of microseconds to wait for an ARP reply. */
-#define ARP_TRANS_WAIT	1000000
+#define ARP_TRANS_WAIT  1000000
 
 /** ARP global data. */
@@ -87,4 +87,5 @@
 		trans->hw_addr = NULL;
 	}
+	
 	fibril_condvar_broadcast(&trans->cv);
 }
@@ -93,8 +94,8 @@
 {
 	int count;
-	arp_trans_t *trans;
-
+	
 	for (count = arp_addr_count(addresses) - 1; count >= 0; count--) {
-		trans = arp_addr_items_get_index(&addresses->values, count);
+		arp_trans_t *trans = arp_addr_items_get_index(&addresses->values,
+		    count);
 		if (trans)
 			arp_clear_trans(trans);
@@ -102,26 +103,29 @@
 }
 
-
-/** Clears the device specific data.
- *
- * @param[in] device	The device specific data.
+/** Clear the device specific data.
+ *
+ * @param[in] device Device specific data.
  */
 static void arp_clear_device(arp_device_t *device)
 {
 	int count;
-	arp_proto_t *proto;
-
+	
 	for (count = arp_protos_count(&device->protos) - 1; count >= 0;
 	    count--) {
-		proto = arp_protos_get_index(&device->protos, count);
+		arp_proto_t *proto = arp_protos_get_index(&device->protos,
+		    count);
+		
 		if (proto) {
 			if (proto->addr)
 				free(proto->addr);
+			
 			if (proto->addr_data)
 				free(proto->addr_data);
+			
 			arp_clear_addr(&proto->addresses);
 			arp_addr_destroy(&proto->addresses);
 		}
 	}
+	
 	arp_protos_clear(&device->protos);
 }
@@ -130,21 +134,24 @@
 {
 	int count;
-	arp_device_t *device;
-
+	
 	fibril_mutex_lock(&arp_globals.lock);
 	for (count = arp_cache_count(&arp_globals.cache) - 1; count >= 0;
 	    count--) {
-		device = arp_cache_get_index(&arp_globals.cache, count);
+		arp_device_t *device = arp_cache_get_index(&arp_globals.cache,
+		    count);
+		
 		if (device) {
 			arp_clear_device(device);
 			if (device->addr_data)
 				free(device->addr_data);
+			
 			if (device->broadcast_data)
 				free(device->broadcast_data);
 		}
 	}
+	
 	arp_cache_clear(&arp_globals.cache);
 	fibril_mutex_unlock(&arp_globals.lock);
-	printf("Cache cleaned\n");
+	
 	return EOK;
 }
@@ -153,59 +160,60 @@
     services_t protocol, measured_string_t *address)
 {
-	arp_device_t *device;
-	arp_proto_t *proto;
-	arp_trans_t *trans;
-
 	fibril_mutex_lock(&arp_globals.lock);
-	device = arp_cache_find(&arp_globals.cache, device_id);
+	
+	arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
 	if (!device) {
 		fibril_mutex_unlock(&arp_globals.lock);
 		return ENOENT;
 	}
-	proto = arp_protos_find(&device->protos, protocol);
+	
+	arp_proto_t *proto = arp_protos_find(&device->protos, protocol);
 	if (!proto) {
 		fibril_mutex_unlock(&arp_globals.lock);
 		return ENOENT;
 	}
-	trans = arp_addr_find(&proto->addresses, address->value, address->length);
+	
+	arp_trans_t *trans = arp_addr_find(&proto->addresses, address->value,
+	    address->length);
 	if (trans)
 		arp_clear_trans(trans);
+	
 	arp_addr_exclude(&proto->addresses, address->value, address->length);
+	
 	fibril_mutex_unlock(&arp_globals.lock);
 	return EOK;
 }
 
-
 static int arp_clear_device_req(int arp_phone, device_id_t device_id)
 {
-	arp_device_t *device;
-
 	fibril_mutex_lock(&arp_globals.lock);
-	device = arp_cache_find(&arp_globals.cache, device_id);
+	
+	arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
 	if (!device) {
 		fibril_mutex_unlock(&arp_globals.lock);
 		return ENOENT;
 	}
+	
 	arp_clear_device(device);
-	printf("Device %d cleared\n", device_id);
+	
 	fibril_mutex_unlock(&arp_globals.lock);
 	return EOK;
 }
 
-/** Creates new protocol specific data.
- *
- * Allocates and returns the needed memory block as the proto parameter.
- *
- * @param[out] proto	The allocated protocol specific data.
- * @param[in] service	The protocol module service.
- * @param[in] address	The actual protocol device address.
- * @return		EOK on success.
- * @return		ENOMEM if there is not enough memory left.
+/** Create new protocol specific data.
+ *
+ * Allocate and return the needed memory block as the proto parameter.
+ *
+ * @param[out] proto   Allocated protocol specific data.
+ * @param[in]  service Protocol module service.
+ * @param[in]  address Actual protocol device address.
+ *
+ * @return EOK on success.
+ * @return ENOMEM if there is not enough memory left.
+ *
  */
 static int arp_proto_create(arp_proto_t **proto, services_t service,
     measured_string_t *address)
 {
-	int rc;
-
 	*proto = (arp_proto_t *) malloc(sizeof(arp_proto_t));
 	if (!*proto)
@@ -216,5 +224,5 @@
 	(*proto)->addr_data = address->value;
 	
-	rc = arp_addr_initialize(&(*proto)->addresses);
+	int rc = arp_addr_initialize(&(*proto)->addresses);
 	if (rc != EOK) {
 		free(*proto);
@@ -225,41 +233,41 @@
 }
 
-/** Registers the device.
- *
- * Creates new device entry in the cache or updates the protocol address if the
+/** Register the device.
+ *
+ * Create new device entry in the cache or update the protocol address if the
  * device with the device identifier and the driver service exists.
  *
- * @param[in] device_id	The device identifier.
- * @param[in] service	The device driver service.
- * @param[in] protocol	The protocol service.
- * @param[in] address	The actual device protocol address.
- * @return		EOK on success.
- * @return		EEXIST if another device with the same device identifier
- *			and different driver service exists.
- * @return		ENOMEM if there is not enough memory left.
- * @return		Other error codes as defined for the
- *			measured_strings_return() function.
+ * @param[in] device_id Device identifier.
+ * @param[in] service   Device driver service.
+ * @param[in] protocol  Protocol service.
+ * @param[in] address   Actual device protocol address.
+ *
+ * @return EOK on success.
+ * @return EEXIST if another device with the same device identifier
+ *         and different driver service exists.
+ * @return ENOMEM if there is not enough memory left.
+ * @return Other error codes as defined for the
+ *         measured_strings_return() function.
+ *
  */
 static int arp_device_message(device_id_t device_id, services_t service,
     services_t protocol, measured_string_t *address)
 {
-	arp_device_t *device;
-	arp_proto_t *proto;
-	hw_type_t hardware;
 	int index;
 	int rc;
-
+	
 	fibril_mutex_lock(&arp_globals.lock);
-
+	
 	/* An existing device? */
-	device = arp_cache_find(&arp_globals.cache, device_id);
-
+	arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
 	if (device) {
 		if (device->service != service) {
-			printf("Device %d already exists\n", device->device_id);
+			printf("%s: Device %d already exists\n", NAME,
+			    device->device_id);
 			fibril_mutex_unlock(&arp_globals.lock);
 			return EEXIST;
 		}
-		proto = arp_protos_find(&device->protos, protocol);
+		
+		arp_proto_t *proto = arp_protos_find(&device->protos, protocol);
 		if (proto) {
 			free(proto->addr);
@@ -273,4 +281,5 @@
 				return rc;
 			}
+			
 			index = arp_protos_add(&device->protos, proto->service,
 			    proto);
@@ -280,13 +289,14 @@
 				return index;
 			}
-			printf("New protocol added:\n\tdevice id\t= "
-			    "%d\n\tproto\t= %d", device_id, protocol);
+			
+			printf("%s: New protocol added (id: %d, proto: %d)\n", NAME,
+			    device_id, protocol);
 		}
 	} else {
-		hardware = hardware_map(service);
+		hw_type_t hardware = hardware_map(service);
 		if (!hardware)
 			return ENOENT;
 		
-		/* Create a new device */
+		/* Create new device */
 		device = (arp_device_t *) malloc(sizeof(arp_device_t));
 		if (!device) {
@@ -294,4 +304,5 @@
 			return ENOMEM;
 		}
+		
 		device->hardware = hardware;
 		device->device_id = device_id;
@@ -302,4 +313,6 @@
 			return rc;
 		}
+		
+		arp_proto_t *proto;
 		rc = arp_proto_create(&proto, protocol, address);
 		if (rc != EOK) {
@@ -308,4 +321,5 @@
 			return rc;
 		}
+		
 		index = arp_protos_add(&device->protos, proto->service, proto);
 		if (index < 0) {
@@ -315,7 +329,8 @@
 			return index;
 		}
+		
 		device->service = service;
 		
-		/* Bind the new one */
+		/* Bind */
 		device->phone = nil_bind_service(device->service,
 		    (sysarg_t) device->device_id, SERVICE_ARP,
@@ -376,24 +391,25 @@
 		    device->service, protocol);
 	}
+	
 	fibril_mutex_unlock(&arp_globals.lock);
-	
 	return EOK;
 }
 
-/** Initializes the ARP module.
+/** Initialize the ARP module.
  *
  *  @param[in] client_connection The client connection processing function.
- *			The module skeleton propagates its own one.
- *  @return		EOK on success.
- *  @return		ENOMEM if there is not enough memory left.
+ *                               The module skeleton propagates its own one.
+ *
+ *  @return EOK on success.
+ *  @return ENOMEM if there is not enough memory left.
+ *
  */
 int arp_initialize(async_client_conn_t client_connection)
 {
-	int rc;
-
 	fibril_mutex_initialize(&arp_globals.lock);
+	
 	fibril_mutex_lock(&arp_globals.lock);
 	arp_globals.client_connection = client_connection;
-	rc = arp_cache_initialize(&arp_globals.cache);
+	int rc = arp_cache_initialize(&arp_globals.cache);
 	fibril_mutex_unlock(&arp_globals.lock);
 	
@@ -401,69 +417,68 @@
 }
 
-/** Updates the device content length according to the new MTU value.
- *
- * @param[in] device_id	The device identifier.
- * @param[in] mtu	The new mtu value.
- * @return		ENOENT if device is not found.
- * @return		EOK on success.
+/** Update the device content length according to the new MTU value.
+ *
+ * @param[in] device_id Device identifier.
+ * @param[in] mtu       New MTU value.
+ *
+ * @return ENOENT if device is not found.
+ * @return EOK on success.
+ *
  */
 static int arp_mtu_changed_message(device_id_t device_id, size_t mtu)
 {
-	arp_device_t *device;
-
 	fibril_mutex_lock(&arp_globals.lock);
-	device = arp_cache_find(&arp_globals.cache, device_id);
+	
+	arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
 	if (!device) {
 		fibril_mutex_unlock(&arp_globals.lock);
 		return ENOENT;
 	}
+	
 	device->packet_dimension.content = mtu;
+	
 	fibril_mutex_unlock(&arp_globals.lock);
-	printf("arp - device %d changed mtu to %zu\n\n", device_id, mtu);
+	
+	printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu);
+	
 	return EOK;
 }
 
-/** Processes the received ARP packet.
- *
- * Updates the source hardware address if the source entry exists or the packet
+/** Process the received ARP packet.
+ *
+ * Update the source hardware address if the source entry exists or the packet
  * is targeted to my protocol address.
- * Responses to the ARP request if the packet is the ARP request and is
+ *
+ * Respond to the ARP request if the packet is the ARP request and is
  * targeted to my address.
  *
- * @param[in] device_id	The source device identifier.
- * @param[in,out] packet The received packet.
- * @return		EOK on success and the packet is no longer needed.
- * @return		One on success and the packet has been reused.
- * @return		EINVAL if the packet is too small to carry an ARP
- *			packet.
- * @return		EINVAL if the received address lengths differs from
- *			the registered values.
- * @return		ENOENT if the device is not found in the cache.
- * @return		ENOENT if the protocol for the device is not found in
- *			the cache.
- * @return		ENOMEM if there is not enough memory left.
+ * @param[in]     device_id Source device identifier.
+ * @param[in,out] packet    Received packet.
+ *
+ * @return EOK on success and the packet is no longer needed.
+ * @return One on success and the packet has been reused.
+ * @return EINVAL if the packet is too small to carry an ARP
+ *         packet.
+ * @return EINVAL if the received address lengths differs from
+ *         the registered values.
+ * @return ENOENT if the device is not found in the cache.
+ * @return ENOENT if the protocol for the device is not found in
+ *         the cache.
+ * @return ENOMEM if there is not enough memory left.
+ *
  */
 static int arp_receive_message(device_id_t device_id, packet_t *packet)
 {
-	size_t length;
-	arp_header_t *header;
-	arp_device_t *device;
-	arp_proto_t *proto;
-	arp_trans_t *trans;
-	uint8_t *src_hw;
-	uint8_t *src_proto;
-	uint8_t *des_hw;
-	uint8_t *des_proto;
 	int rc;
 	
-	length = packet_get_data_length(packet);
+	size_t length = packet_get_data_length(packet);
 	if (length <= sizeof(arp_header_t))
 		return EINVAL;
-
-	device = arp_cache_find(&arp_globals.cache, device_id);
+	
+	arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
 	if (!device)
 		return ENOENT;
-
-	header = (arp_header_t *) packet_get_data(packet);
+	
+	arp_header_t *header = (arp_header_t *) packet_get_data(packet);
 	if ((ntohs(header->hardware) != device->hardware) ||
 	    (length < sizeof(arp_header_t) + header->hardware_length * 2U +
@@ -471,22 +486,26 @@
 		return EINVAL;
 	}
-
-	proto = arp_protos_find(&device->protos,
+	
+	arp_proto_t *proto = arp_protos_find(&device->protos,
 	    protocol_unmap(device->service, ntohs(header->protocol)));
 	if (!proto)
 		return ENOENT;
-
-	src_hw = ((uint8_t *) header) + sizeof(arp_header_t);
-	src_proto = src_hw + header->hardware_length;
-	des_hw = src_proto + header->protocol_length;
-	des_proto = des_hw + header->hardware_length;
-	trans = arp_addr_find(&proto->addresses, src_proto,
+	
+	uint8_t *src_hw = ((uint8_t *) header) + sizeof(arp_header_t);
+	uint8_t *src_proto = src_hw + header->hardware_length;
+	uint8_t *des_hw = src_proto + header->protocol_length;
+	uint8_t *des_proto = des_hw + header->hardware_length;
+	
+	arp_trans_t *trans = arp_addr_find(&proto->addresses, src_proto,
 	    header->protocol_length);
-	/* Exists? */
-	if (trans && trans->hw_addr) {
+	
+	if ((trans) && (trans->hw_addr)) {
+		/* Translation exists */
 		if (trans->hw_addr->length != header->hardware_length)
 			return EINVAL;
+		
 		memcpy(trans->hw_addr->value, src_hw, trans->hw_addr->length);
 	}
+	
 	/* Is my protocol address? */
 	if (proto->addr->length != header->protocol_length)
@@ -494,9 +513,10 @@
 	
 	if (!bcmp(proto->addr->value, des_proto, proto->addr->length)) {
-		/* Not already updated? */
 		if (!trans) {
+			/* Update the translation */
 			trans = (arp_trans_t *) malloc(sizeof(arp_trans_t));
 			if (!trans)
 				return ENOMEM;
+			
 			trans->hw_addr = NULL;
 			fibril_condvar_initialize(&trans->cv);
@@ -508,4 +528,5 @@
 			}
 		}
+		
 		if (!trans->hw_addr) {
 			trans->hw_addr = measured_string_create_bulk(src_hw,
@@ -517,4 +538,5 @@
 			fibril_condvar_broadcast(&trans->cv);
 		}
+		
 		if (ntohs(header->operation) == ARPOP_REQUEST) {
 			header->operation = htons(ARPOP_REPLY);
@@ -537,5 +559,5 @@
 		}
 	}
-
+	
 	return EOK;
 }
@@ -566,5 +588,7 @@
 	header->protocol_length = (uint8_t) proto->addr->length;
 	header->operation = htons(ARPOP_REQUEST);
+	
 	length = sizeof(arp_header_t);
+	
 	memcpy(((uint8_t *) header) + length, device->addr->value,
 	    device->addr->length);
@@ -603,11 +627,7 @@
  *
  */
-static int
-arp_translate_message(device_id_t device_id, services_t protocol,
+static int arp_translate_message(device_id_t device_id, services_t protocol,
     measured_string_t *target, measured_string_t **translation)
 {
-	arp_device_t *device;
-	arp_proto_t *proto;
-	arp_trans_t *trans;
 	bool retry = false;
 	int rc;
@@ -617,13 +637,14 @@
 		return EBADMEM;
 	
-	device = arp_cache_find(&arp_globals.cache, device_id);
+	arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id);
 	if (!device)
 		return ENOENT;
 	
-	proto = arp_protos_find(&device->protos, protocol);
+	arp_proto_t *proto = arp_protos_find(&device->protos, protocol);
 	if ((!proto) || (proto->addr->length != target->length))
 		return ENOENT;
 	
-	trans = arp_addr_find(&proto->addresses, target->value, target->length);
+	arp_trans_t *trans = arp_addr_find(&proto->addresses, target->value,
+	    target->length);
 	if (trans) {
 		if (trans->hw_addr) {
@@ -632,10 +653,11 @@
 		}
 		
-		if (retry)
+		if (retry) {
+			/* Remove the translation from the map */
+			arp_clear_trans(trans);
+			arp_addr_exclude(&proto->addresses, target->value,
+			    target->length);
 			return EAGAIN;
-		
-		rc = arp_send_request(device_id, protocol, target, device, proto);
-		if (rc != EOK)
-			return rc;
+		}
 		
 		rc = fibril_condvar_wait_timeout(&trans->cv, &arp_globals.lock,
@@ -654,6 +676,8 @@
 	if (!trans)
 		return ENOMEM;
+	
 	trans->hw_addr = NULL;
 	fibril_condvar_initialize(&trans->cv);
+	
 	rc = arp_addr_add(&proto->addresses, target->value, target->length,
 	    trans);
@@ -676,21 +700,20 @@
 }
 
-
-/** Processes the ARP message.
- *
- * @param[in] callid	The message identifier.
- * @param[in] call	The message parameters.
- * @param[out] answer	The message answer parameters.
- * @param[out] answer_count The last parameter for the actual answer in the
- *			answer parameter.
- * @return		EOK on success.
- * @return		ENOTSUP if the message is not known.
+/** Process the ARP message.
+ *
+ * @param[in]  callid Message identifier.
+ * @param[in]  call   Message parameters.
+ * @param[out] answer Answer.
+ * @param[out] count  Number of arguments of the answer.
+ *
+ * @return EOK on success.
+ * @return ENOTSUP if the message is not known.
  *
  * @see arp_interface.h
  * @see IS_NET_ARP_MESSAGE()
- */
-int
-arp_message_standalone(ipc_callid_t callid, ipc_call_t *call,
-    ipc_call_t *answer, size_t *answer_count)
+ *
+ */
+int arp_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
+    size_t *count)
 {
 	measured_string_t *address;
@@ -701,5 +724,5 @@
 	int rc;
 	
-	*answer_count = 0;
+	*count = 0;
 	switch (IPC_GET_IMETHOD(*call)) {
 	case IPC_M_PHONE_HUNGUP:
@@ -717,4 +740,5 @@
 			free(data);
 		}
+		
 		return rc;
 	
@@ -729,19 +753,22 @@
 		free(address);
 		free(data);
+		
 		if (rc != EOK) {
 			fibril_mutex_unlock(&arp_globals.lock);
 			return rc;
 		}
+		
 		if (!translation) {
 			fibril_mutex_unlock(&arp_globals.lock);
 			return ENOENT;
 		}
+		
 		rc = measured_strings_reply(translation, 1);
 		fibril_mutex_unlock(&arp_globals.lock);
 		return rc;
-
+	
 	case NET_ARP_CLEAR_DEVICE:
 		return arp_clear_device_req(0, IPC_GET_DEVICE(*call));
-
+	
 	case NET_ARP_CLEAR_ADDRESS:
 		rc = measured_strings_receive(&address, &data, 1);
@@ -793,6 +820,6 @@
 /** Default thread for new connections.
  *
- * @param[in] iid	The initial message identifier.
- * @param[in] icall	The initial message call structure.
+ * @param[in] iid   Initial message identifier.
+ * @param[in] icall Initial message call structure.
  */
 static void il_client_connection(ipc_callid_t iid, ipc_call_t *icall)
@@ -816,5 +843,5 @@
 		
 		/* Process the message */
-		int res = il_module_message_standalone(callid, &call, &answer,
+		int res = il_module_message(callid, &call, &answer,
 		    &count);
 		
@@ -832,20 +859,10 @@
 }
 
-/** Starts the module.
- *
- * @return		EOK on success.
- * @return		Other error codes as defined for each specific module
- *			start function.
- */
 int main(int argc, char *argv[])
 {
-	int rc;
-	
 	/* Start the module */
-	rc = il_module_start_standalone(il_client_connection);
-	return rc;
+	return il_module_start(il_client_connection);
 }
 
 /** @}
  */
-
