Changeset 02a09ed in mainline for uspace/app/nettest2/nettest2.c
- Timestamp:
- 2013-06-28T20:20:03Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1d24ad3
- Parents:
- edf0d27
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nettest2/nettest2.c
redf0d27 r02a09ed 60 60 #define NETTEST2_TEXT "Networking test 2 - transfer" 61 61 62 static size_t size;63 static bool verbose;64 static sock_type_t type;65 static int sockets;66 static int messages;67 static int family;68 static uint16_t port ;62 static uint16_t family = PF_INET; 63 static size_t size = 28; 64 static bool verbose = false; 65 static sock_type_t type = SOCK_DGRAM; 66 static int sockets = 10; 67 static int messages = 10; 68 static uint16_t port = 7; 69 69 70 70 static void nettest2_print_help(void) … … 234 234 int main(int argc, char *argv[]) 235 235 { 236 struct sockaddr *address;237 struct sockaddr_in address_in;238 struct sockaddr_in6 address_in6;239 dnsr_hostinfo_t *hinfo;240 socklen_t addrlen;241 uint8_t *address_start;242 243 int *socket_ids;244 char *data;245 236 int index; 246 struct timeval time_before;247 struct timeval time_after;248 249 237 int rc; 250 251 size = 28; 252 verbose = false; 253 type = SOCK_DGRAM; 254 sockets = 10; 255 messages = 10; 256 family = PF_INET; 257 port = 7; 258 238 259 239 /* 260 240 * Parse the command line arguments. … … 264 244 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 265 245 (argv[index][0] == '-')); ++index) { 266 267 246 /* Options should start with dash ('-') */ 268 247 if (argv[index][0] == '-') { … … 276 255 } 277 256 278 /* If not before the last argument containing the host */257 /* The last argument containing the host */ 279 258 if (index >= argc) { 280 printf(" Command line error: missing host name\n");259 printf("Host name missing.\n"); 281 260 nettest2_print_help(); 282 261 return EINVAL; 283 262 } 284 263 264 char *addr_s = argv[argc - 1]; 265 266 /* Interpret as address */ 267 inet_addr_t addr_addr; 268 rc = inet_addr_parse(addr_s, &addr_addr); 269 270 if (rc != EOK) { 271 /* Interpret as a host name */ 272 dnsr_hostinfo_t *hinfo = NULL; 273 rc = dnsr_name2host(addr_s, &hinfo); 274 275 if (rc != EOK) { 276 printf("Error resolving host '%s'.\n", addr_s); 277 return EINVAL; 278 } 279 280 addr_addr = hinfo->addr; 281 } 282 283 struct sockaddr_in addr; 284 struct sockaddr_in6 addr6; 285 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 286 287 if (af != family) { 288 printf("Address family does not match explicitly set family.\n"); 289 return EINVAL; 290 } 291 285 292 /* Prepare the address buffer */ 286 287 switch (family) { 288 case PF_INET: 289 address_in.sin_family = AF_INET; 290 address_in.sin_port = htons(port); 291 address = (struct sockaddr *) &address_in; 292 addrlen = sizeof(address_in); 293 address_start = (uint8_t *) &address_in.sin_addr.s_addr; 294 break; 295 case PF_INET6: 296 address_in6.sin6_family = AF_INET6; 297 address_in6.sin6_port = htons(port); 298 address = (struct sockaddr *) &address_in6; 299 addrlen = sizeof(address_in6); 300 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr; 293 294 struct sockaddr *address; 295 socklen_t addrlen; 296 297 switch (af) { 298 case AF_INET: 299 addr.sin_port = htons(port); 300 address = (struct sockaddr *) &addr; 301 addrlen = sizeof(addr); 302 break; 303 case AF_INET6: 304 addr6.sin6_port = htons(port); 305 address = (struct sockaddr *) &addr6; 306 addrlen = sizeof(addr6); 301 307 break; 302 308 default: 303 309 fprintf(stderr, "Address family is not supported\n"); 304 310 return EAFNOSUPPORT; 305 }306 307 /* Parse the last argument which should contain the host/address */308 rc = inet_pton(family, argv[argc - 1], address_start);309 if (rc != EOK) {310 /* Try interpreting as a host name */311 rc = dnsr_name2host(argv[argc - 1], &hinfo);312 if (rc != EOK) {313 printf("Error resolving host '%s'.\n", argv[argc - 1]);314 return rc;315 }316 317 rc = inet_addr_sockaddr_in(&hinfo->addr, &address_in);318 if (rc != EOK) {319 printf("Host '%s' not resolved as IPv4 address.\n", argv[argc - 1]);320 return rc;321 }322 311 } 323 312 … … 333 322 * null character. 334 323 */ 335 data = (char *) malloc(size + 1);324 char *data = (char *) malloc(size + 1); 336 325 if (!data) { 337 326 fprintf(stderr, "Failed to allocate data buffer.\n"); … … 353 342 * Allocate count entries plus the terminating null (\0) 354 343 */ 355 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));344 int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 356 345 if (!socket_ids) { 357 346 fprintf(stderr, "Failed to allocate receive buffer.\n"); 358 347 return ENOMEM; 359 348 } 349 360 350 socket_ids[sockets] = 0; 361 351 … … 377 367 printf("\n"); 378 368 369 struct timeval time_before; 379 370 rc = gettimeofday(&time_before, NULL); 380 371 if (rc != EOK) { … … 388 379 return rc; 389 380 381 struct timeval time_after; 390 382 rc = gettimeofday(&time_after, NULL); 391 383 if (rc != EOK) {
Note:
See TracChangeset
for help on using the changeset viewer.