Changes in uspace/srv/net/app/ping/ping.c [3be62bc:aadf01e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/app/ping/ping.c
r3be62bc raadf01e 39 39 #include <task.h> 40 40 #include <time.h> 41 #include <ipc/ipc.h>42 41 #include <ipc/services.h> 43 42 … … 68 67 /** Prints the application help. 69 68 */ 70 void ping_print_help(void); 69 void print_help(void); 70 71 /** Translates the character string to the address family number. 72 * @param[in] name The address family name. 73 * @returns The corresponding address family number. 74 * @returns EAFNOSUPPORTED if the address family is not supported. 75 */ 76 int parse_address_family(const char * name); 77 78 void print_help(void){ 79 printf( 80 "Network Ping aplication\n" \ 81 "Usage: ping [options] numeric_address\n" \ 82 "Where options are:\n" \ 83 "\n" \ 84 "-c request_count | --count=request_count\n" \ 85 "\tThe number of packets the application sends. The default is three (3).\n" \ 86 "\n" \ 87 "--dont_fragment\n" \ 88 "\tDisable packet fragmentation.\n" 89 "\n" \ 90 "-f address_family | --family=address_family\n" \ 91 "\tThe given address family. Only the AF_INET and AF_INET6 are supported.\n" 92 "\n" \ 93 "-h | --help\n" \ 94 "\tShow this application help.\n" 95 "\n" \ 96 "-s packet_size | --size=packet_size\n" \ 97 "\tThe packet data size the application sends. The default is 38 bytes.\n" \ 98 "\n" \ 99 "-t timeout | --timeout=timeout\n" \ 100 "\tThe number of miliseconds the application waits for a reply. The default is three thousands (3 000).\n" \ 101 "\n" \ 102 "--tos=tos\n" \ 103 "\tThe type of service to be used.\n" \ 104 "\n" \ 105 "--ttl=ttl\n" \ 106 "\tThe time to live to be used.\n" \ 107 "\n" \ 108 "-v | --verbose\n" \ 109 "\tShow all output messages.\n" 110 ); 111 } 112 113 int parse_address_family(const char * name){ 114 if(str_lcmp(name, "AF_INET", 7) == 0){ 115 return AF_INET; 116 }else if(str_lcmp(name, "AF_INET6", 8) == 0){ 117 return AF_INET6; 118 } 119 return EAFNOSUPPORT; 120 } 71 121 72 122 int main(int argc, char * argv[]){ … … 76 126 int verbose = 0; 77 127 int dont_fragment = 0; 78 ip_ttl_t ttl = 0;79 ip_tos_t tos = 0;128 ip_ttl_t ttl = 0; 129 ip_tos_t tos = 0; 80 130 int count = 3; 81 suseconds_t timeout = 3000;131 suseconds_t timeout = 3000; 82 132 int family = AF_INET; 83 133 84 socklen_t max_length 134 socklen_t max_length = sizeof(struct sockaddr_in6); 85 135 uint8_t address_data[max_length]; 86 struct sockaddr * address 136 struct sockaddr * address = (struct sockaddr *) address_data; 87 137 struct sockaddr_in * address_in = (struct sockaddr_in *) address; 88 138 struct sockaddr_in6 * address_in6 = (struct sockaddr_in6 *) address; … … 97 147 int index; 98 148 99 // print the program label100 149 printf("Task %d - ", task_get_id()); 101 150 printf("%s\n", NAME); 102 151 103 // parse the command line arguments 104 // stop before the last argument if it does not start with the minus sign ('-') 105 for(index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index){ 106 // options should start with the minus sign ('-') 152 if(argc <= 1){ 153 print_help(); 154 return EINVAL; 155 } 156 157 for(index = 1; (index < argc - 1) || ((index == argc) && (argv[index][0] == '-')); ++ index){ 107 158 if(argv[index][0] == '-'){ 108 159 switch(argv[index][1]){ 109 // short options with only one letter110 160 case 'c': 111 161 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "count", 0)); … … 115 165 break; 116 166 case 'h': 117 p ing_print_help();167 print_help(); 118 168 return EOK; 119 169 break; … … 129 179 verbose = 1; 130 180 break; 131 // long options with the double minus sign ('-')132 181 case '-': 133 182 if(str_lcmp(argv[index] + 2, "count=", 6) == 0){ … … 138 187 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "address family", 9, parse_address_family)); 139 188 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){ 140 p ing_print_help();189 print_help(); 141 190 return EOK; 142 191 }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){ … … 156 205 }else{ 157 206 print_unrecognized(index, argv[index] + 2); 158 p ing_print_help();207 print_help(); 159 208 return EINVAL; 160 209 } … … 162 211 default: 163 212 print_unrecognized(index, argv[index] + 1); 164 p ing_print_help();213 print_help(); 165 214 return EINVAL; 166 215 } 167 216 }else{ 168 217 print_unrecognized(index, argv[index]); 169 p ing_print_help();218 print_help(); 170 219 return EINVAL; 171 220 } 172 221 } 173 222 174 // if not before the last argument containing the address175 if(index >= argc){176 printf("Command line error: missing address\n");177 ping_print_help();178 return EINVAL;179 }180 181 // prepare the address buffer182 223 bzero(address_data, max_length); 183 224 switch(family){ … … 197 238 } 198 239 199 // parse the last argument which should contain the address200 240 if(ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))){ 201 241 fprintf(stderr, "Address parse error %d\n", ERROR_CODE); … … 203 243 } 204 244 205 // connect to the ICMP module206 245 icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT); 207 246 if(icmp_phone < 0){ … … 210 249 } 211 250 212 // print the ping header213 251 printf("PING %d bytes of data\n", size); 214 252 if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){ … … 218 256 } 219 257 220 // do count times221 258 while(count > 0){ 222 223 // get the starting time224 259 if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){ 225 260 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 226 // release the ICMP phone227 ipc_hangup(icmp_phone);228 261 return ERROR_CODE; 229 262 } 230 231 // request the ping232 263 result = icmp_echo_msg(icmp_phone, size, timeout, ttl, tos, dont_fragment, address, addrlen); 233 234 // get the ending time235 264 if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){ 236 265 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 237 // release the ICMP phone238 ipc_hangup(icmp_phone);239 266 return ERROR_CODE; 240 267 } 241 242 // print the result243 268 switch(result){ 244 269 case ICMP_ECHO: … … 258 283 } 259 284 260 // release the ICMP phone261 ipc_hangup(icmp_phone);262 263 285 return EOK; 264 286 } 265 287 266 void ping_print_help(void){267 printf(268 "Network Ping aplication\n" \269 "Usage: ping [options] numeric_address\n" \270 "Where options are:\n" \271 "\n" \272 "-c request_count | --count=request_count\n" \273 "\tThe number of packets the application sends. The default is three (3).\n" \274 "\n" \275 "--dont_fragment\n" \276 "\tDisable packet fragmentation.\n"277 "\n" \278 "-f address_family | --family=address_family\n" \279 "\tThe given address family. Only the AF_INET and AF_INET6 are supported.\n"280 "\n" \281 "-h | --help\n" \282 "\tShow this application help.\n"283 "\n" \284 "-s packet_size | --size=packet_size\n" \285 "\tThe packet data size the application sends. The default is 38 bytes.\n" \286 "\n" \287 "-t timeout | --timeout=timeout\n" \288 "\tThe number of miliseconds the application waits for a reply. The default is three thousands (3 000).\n" \289 "\n" \290 "--tos=tos\n" \291 "\tThe type of service to be used.\n" \292 "\n" \293 "--ttl=ttl\n" \294 "\tThe time to live to be used.\n" \295 "\n" \296 "-v | --verbose\n" \297 "\tShow all output messages.\n"298 );299 }300 301 288 /** @} 302 289 */
Note:
See TracChangeset
for help on using the changeset viewer.