Index: uspace/srv/net/app/ping/ping.c
===================================================================
--- uspace/srv/net/app/ping/ping.c	(revision aadf01ebd2c032bc8dd43a6e06ae5715e6c2710f)
+++ uspace/srv/net/app/ping/ping.c	(revision 3be62bc8cd08888b403d5c582327281b0683ccc5)
@@ -39,4 +39,5 @@
 #include <task.h>
 #include <time.h>
+#include <ipc/ipc.h>
 #include <ipc/services.h>
 
@@ -67,56 +68,5 @@
 /** Prints the application help.
  */
-void print_help(void);
-
-/** Translates the character string to the address family number.
- *  @param[in] name The address family name.
- *  @returns The corresponding address family number.
- *  @returns EAFNOSUPPORTED if the address family is not supported.
- */
-int parse_address_family(const char * name);
-
-void print_help(void){
-	printf(
-		"Network Ping aplication\n" \
-		"Usage: ping [options] numeric_address\n" \
-		"Where options are:\n" \
-		"\n" \
-		"-c request_count | --count=request_count\n" \
-		"\tThe number of packets the application sends. The default is three (3).\n" \
-		"\n" \
-		"--dont_fragment\n" \
-		"\tDisable packet fragmentation.\n"
-		"\n" \
-		"-f address_family | --family=address_family\n" \
-		"\tThe given address family. Only the AF_INET and AF_INET6 are supported.\n"
-		"\n" \
-		"-h | --help\n" \
-		"\tShow this application help.\n"
-		"\n" \
-		"-s packet_size | --size=packet_size\n" \
-		"\tThe packet data size the application sends. The default is 38 bytes.\n" \
-		"\n" \
-		"-t timeout | --timeout=timeout\n" \
-		"\tThe number of miliseconds the application waits for a reply. The default is three thousands (3 000).\n" \
-		"\n" \
-		"--tos=tos\n" \
-		"\tThe type of service to be used.\n" \
-		"\n" \
-		"--ttl=ttl\n" \
-		"\tThe time to live to be used.\n" \
-		"\n" \
-		"-v | --verbose\n" \
-		"\tShow all output messages.\n"
-	);
-}
-
-int parse_address_family(const char * name){
-	if(str_lcmp(name, "AF_INET", 7) == 0){
-		return AF_INET;
-	}else if(str_lcmp(name, "AF_INET6", 8) == 0){
-		return AF_INET6;
-	}
-	return EAFNOSUPPORT;
-}
+void ping_print_help(void);
 
 int main(int argc, char * argv[]){
@@ -126,13 +76,13 @@
 	int verbose			= 0;
 	int dont_fragment	= 0;
-	ip_ttl_t ttl				= 0;
-	ip_tos_t tos				= 0;
+	ip_ttl_t ttl		= 0;
+	ip_tos_t tos		= 0;
 	int count			= 3;
-	suseconds_t timeout			= 3000;
+	suseconds_t timeout	= 3000;
 	int family			= AF_INET;
 
-	socklen_t max_length		= sizeof(struct sockaddr_in6);
+	socklen_t max_length				= sizeof(struct sockaddr_in6);
 	uint8_t address_data[max_length];
-	struct sockaddr * address		= (struct sockaddr *) address_data;
+	struct sockaddr * address			= (struct sockaddr *) address_data;
 	struct sockaddr_in * address_in		= (struct sockaddr_in *) address;
 	struct sockaddr_in6 * address_in6	= (struct sockaddr_in6 *) address;
@@ -147,15 +97,15 @@
 	int index;
 
+	// print the program label
 	printf("Task %d - ", task_get_id());
 	printf("%s\n", NAME);
 
-	if(argc <= 1){
-		print_help();
-		return EINVAL;
-	}
-
-	for(index = 1; (index < argc - 1) || ((index == argc) && (argv[index][0] == '-')); ++ index){
+	// parse the command line arguments
+	// stop before the last argument if it does not start with the minus sign ('-')
+	for(index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index){
+		// options should start with the minus sign ('-')
 		if(argv[index][0] == '-'){
 			switch(argv[index][1]){
+				// short options with only one letter
 				case 'c':
 					ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "count", 0));
@@ -165,5 +115,5 @@
 					break;
 				case 'h':
-					print_help();
+					ping_print_help();
 					return EOK;
 					break;
@@ -179,4 +129,5 @@
 					verbose = 1;
 					break;
+				// long options with the double minus sign ('-')
 				case '-':
 					if(str_lcmp(argv[index] + 2, "count=", 6) == 0){
@@ -187,5 +138,5 @@
 						ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "address family", 9, parse_address_family));
 					}else if(str_lcmp(argv[index] + 2, "help", 5) == 0){
-						print_help();
+						ping_print_help();
 						return EOK;
 					}else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){
@@ -205,5 +156,5 @@
 					}else{
 						print_unrecognized(index, argv[index] + 2);
-						print_help();
+						ping_print_help();
 						return EINVAL;
 					}
@@ -211,14 +162,22 @@
 				default:
 					print_unrecognized(index, argv[index] + 1);
-					print_help();
+					ping_print_help();
 					return EINVAL;
 			}
 		}else{
 			print_unrecognized(index, argv[index]);
-			print_help();
+			ping_print_help();
 			return EINVAL;
 		}
 	}
 
+	// if not before the last argument containing the address
+	if(index >= argc){
+		printf("Command line error: missing address\n");
+		ping_print_help();
+		return EINVAL;
+	}
+
+	// prepare the address buffer
 	bzero(address_data, max_length);
 	switch(family){
@@ -238,4 +197,5 @@
 	}
 
+	// parse the last argument which should contain the address
 	if(ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))){
 		fprintf(stderr, "Address parse error %d\n", ERROR_CODE);
@@ -243,4 +203,5 @@
 	}
 
+	// connect to the ICMP module
 	icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT);
 	if(icmp_phone < 0){
@@ -249,4 +210,5 @@
 	}
 
+	// print the ping header
 	printf("PING %d bytes of data\n", size);
 	if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){
@@ -256,14 +218,27 @@
 	}
 
+	// do count times
 	while(count > 0){
+
+		// get the starting time
 		if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){
 			fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
+			// release the ICMP phone
+			ipc_hangup(icmp_phone);
 			return ERROR_CODE;
 		}
+
+		// request the ping
 		result = icmp_echo_msg(icmp_phone, size, timeout, ttl, tos, dont_fragment, address, addrlen);
+
+		// get the ending time
 		if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){
 			fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
+			// release the ICMP phone
+			ipc_hangup(icmp_phone);
 			return ERROR_CODE;
 		}
+
+		// print the result
 		switch(result){
 			case ICMP_ECHO:
@@ -283,7 +258,45 @@
 	}
 
+	// release the ICMP phone
+	ipc_hangup(icmp_phone);
+
 	return EOK;
 }
 
+void ping_print_help(void){
+	printf(
+		"Network Ping aplication\n" \
+		"Usage: ping [options] numeric_address\n" \
+		"Where options are:\n" \
+		"\n" \
+		"-c request_count | --count=request_count\n" \
+		"\tThe number of packets the application sends. The default is three (3).\n" \
+		"\n" \
+		"--dont_fragment\n" \
+		"\tDisable packet fragmentation.\n"
+		"\n" \
+		"-f address_family | --family=address_family\n" \
+		"\tThe given address family. Only the AF_INET and AF_INET6 are supported.\n"
+		"\n" \
+		"-h | --help\n" \
+		"\tShow this application help.\n"
+		"\n" \
+		"-s packet_size | --size=packet_size\n" \
+		"\tThe packet data size the application sends. The default is 38 bytes.\n" \
+		"\n" \
+		"-t timeout | --timeout=timeout\n" \
+		"\tThe number of miliseconds the application waits for a reply. The default is three thousands (3 000).\n" \
+		"\n" \
+		"--tos=tos\n" \
+		"\tThe type of service to be used.\n" \
+		"\n" \
+		"--ttl=ttl\n" \
+		"\tThe time to live to be used.\n" \
+		"\n" \
+		"-v | --verbose\n" \
+		"\tShow all output messages.\n"
+	);
+}
+
 /** @}
  */
