Changeset 60ab6c3 in mainline for uspace/srv/net/app/ping/ping.c
- Timestamp:
- 2010-03-10T05:46:54Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5782081
- Parents:
- 71b00dcc (diff), b48ebd19 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/app/ping/ping.c
r71b00dcc r60ab6c3 39 39 #include <task.h> 40 40 #include <time.h> 41 #include <ipc/ipc.h> 41 42 #include <ipc/services.h> 42 43 … … 67 68 /** Prints the application help. 68 69 */ 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 } 70 void ping_print_help(void); 121 71 122 72 int main(int argc, char * argv[]){ … … 126 76 int verbose = 0; 127 77 int dont_fragment = 0; 128 ip_ttl_t ttl 129 ip_tos_t tos 78 ip_ttl_t ttl = 0; 79 ip_tos_t tos = 0; 130 80 int count = 3; 131 suseconds_t timeout 81 suseconds_t timeout = 3000; 132 82 int family = AF_INET; 133 83 134 socklen_t max_length = sizeof(struct sockaddr_in6);84 socklen_t max_length = sizeof(struct sockaddr_in6); 135 85 uint8_t address_data[max_length]; 136 struct sockaddr * address = (struct sockaddr *) address_data;86 struct sockaddr * address = (struct sockaddr *) address_data; 137 87 struct sockaddr_in * address_in = (struct sockaddr_in *) address; 138 88 struct sockaddr_in6 * address_in6 = (struct sockaddr_in6 *) address; … … 147 97 int index; 148 98 99 // print the program label 149 100 printf("Task %d - ", task_get_id()); 150 101 printf("%s\n", NAME); 151 102 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){ 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 ('-') 158 107 if(argv[index][0] == '-'){ 159 108 switch(argv[index][1]){ 109 // short options with only one letter 160 110 case 'c': 161 111 ERROR_PROPAGATE(parse_parameter_int(argc, argv, &index, &count, "count", 0)); … … 165 115 break; 166 116 case 'h': 167 p rint_help();117 ping_print_help(); 168 118 return EOK; 169 119 break; … … 179 129 verbose = 1; 180 130 break; 131 // long options with the double minus sign ('-') 181 132 case '-': 182 133 if(str_lcmp(argv[index] + 2, "count=", 6) == 0){ … … 187 138 ERROR_PROPAGATE(parse_parameter_name_int(argc, argv, &index, &family, "address family", 9, parse_address_family)); 188 139 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){ 189 p rint_help();140 ping_print_help(); 190 141 return EOK; 191 142 }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){ … … 205 156 }else{ 206 157 print_unrecognized(index, argv[index] + 2); 207 p rint_help();158 ping_print_help(); 208 159 return EINVAL; 209 160 } … … 211 162 default: 212 163 print_unrecognized(index, argv[index] + 1); 213 p rint_help();164 ping_print_help(); 214 165 return EINVAL; 215 166 } 216 167 }else{ 217 168 print_unrecognized(index, argv[index]); 218 p rint_help();169 ping_print_help(); 219 170 return EINVAL; 220 171 } 221 172 } 222 173 174 // if not before the last argument containing the address 175 if(index >= argc){ 176 printf("Command line error: missing address\n"); 177 ping_print_help(); 178 return EINVAL; 179 } 180 181 // prepare the address buffer 223 182 bzero(address_data, max_length); 224 183 switch(family){ … … 238 197 } 239 198 199 // parse the last argument which should contain the address 240 200 if(ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))){ 241 201 fprintf(stderr, "Address parse error %d\n", ERROR_CODE); … … 243 203 } 244 204 205 // connect to the ICMP module 245 206 icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT); 246 207 if(icmp_phone < 0){ … … 249 210 } 250 211 212 // print the ping header 251 213 printf("PING %d bytes of data\n", size); 252 214 if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){ … … 256 218 } 257 219 220 // do count times 258 221 while(count > 0){ 222 223 // get the starting time 259 224 if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){ 260 225 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 226 // release the ICMP phone 227 ipc_hangup(icmp_phone); 261 228 return ERROR_CODE; 262 229 } 230 231 // request the ping 263 232 result = icmp_echo_msg(icmp_phone, size, timeout, ttl, tos, dont_fragment, address, addrlen); 233 234 // get the ending time 264 235 if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){ 265 236 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 237 // release the ICMP phone 238 ipc_hangup(icmp_phone); 266 239 return ERROR_CODE; 267 240 } 241 242 // print the result 268 243 switch(result){ 269 244 case ICMP_ECHO: … … 283 258 } 284 259 260 // release the ICMP phone 261 ipc_hangup(icmp_phone); 262 285 263 return EOK; 286 264 } 287 265 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 288 301 /** @} 289 302 */
Note:
See TracChangeset
for help on using the changeset viewer.