Changes in uspace/app/nettest1/nettest1.c [c442f63:d4d74dc] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nettest1/nettest1.c
rc442f63 rd4d74dc 46 46 #include <arg_parse.h> 47 47 48 #include <inet/dnsr.h>49 48 #include <net/in.h> 50 49 #include <net/in6.h> … … 54 53 55 54 /** Echo module name. */ 56 #define NAME "nettest1"55 #define NAME "Nettest1" 57 56 58 57 /** Packet data pattern. */ 59 #define NETTEST1_TEXT 60 61 static uint16_t family = AF_INET;58 #define NETTEST1_TEXT "Networking test 1 - sockets" 59 60 static int family = PF_INET; 62 61 static sock_type_t type = SOCK_DGRAM; 62 static char *data; 63 63 static size_t size = 27; 64 static bool verbose = false; 65 static int sockets = 10; 66 static int messages = 10; 67 static uint16_t port = 7; 64 static int verbose = 0; 68 65 69 66 static struct sockaddr *address; 70 67 static socklen_t addrlen; 71 68 72 static char *data; 69 static int sockets; 70 static int messages; 71 static uint16_t port; 73 72 74 73 static void nettest1_print_help(void) … … 76 75 printf( 77 76 "Network Networking test 1 aplication - sockets\n" 78 "Usage: nettest1 [options] host\n"77 "Usage: echo [options] numeric_address\n" 79 78 "Where options are:\n" 80 79 "-f protocol_family | --family=protocol_family\n" … … 114 113 int value; 115 114 int rc; 116 115 117 116 switch (argv[*index][1]) { 118 117 /* … … 120 119 */ 121 120 case 'f': 122 rc = arg_parse_name_int(argc, argv, index, &value, 0, 123 socket_parse_protocol_family); 124 if (rc != EOK) 125 return rc; 126 127 family = (uint16_t) value; 121 rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family); 122 if (rc != EOK) 123 return rc; 128 124 break; 129 125 case 'h': … … 134 130 if (rc != EOK) 135 131 return rc; 136 137 132 break; 138 133 case 'n': … … 140 135 if (rc != EOK) 141 136 return rc; 142 143 137 break; 144 138 case 'p': … … 146 140 if (rc != EOK) 147 141 return rc; 148 149 142 port = (uint16_t) value; 150 143 break; … … 153 146 if (rc != EOK) 154 147 return rc; 155 156 148 size = (value >= 0) ? (size_t) value : 0; 157 149 break; 158 150 case 't': 159 rc = arg_parse_name_int(argc, argv, index, &value, 0, 160 socket_parse_socket_type); 161 if (rc != EOK) 162 return rc; 163 151 rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type); 152 if (rc != EOK) 153 return rc; 164 154 type = (sock_type_t) value; 165 155 break; … … 167 157 verbose = 1; 168 158 break; 169 170 159 /* 171 160 * Long options with double dash ('-') … … 173 162 case '-': 174 163 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { 175 rc = arg_parse_name_int(argc, argv, index, & value, 9,164 rc = arg_parse_name_int(argc, argv, index, &family, 9, 176 165 socket_parse_protocol_family); 177 166 if (rc != EOK) 178 167 return rc; 179 180 family = (uint16_t) value;181 168 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { 182 169 nettest1_print_help(); … … 194 181 if (rc != EOK) 195 182 return rc; 196 197 183 port = (uint16_t) value; 198 184 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { … … 201 187 if (rc != EOK) 202 188 return rc; 203 204 189 type = (sock_type_t) value; 205 190 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { … … 214 199 return EINVAL; 215 200 } 216 201 217 202 return EOK; 218 203 } … … 225 210 static void nettest1_fill_buffer(char *buffer, size_t size) 226 211 { 227 size_t length = 0; 212 size_t length; 213 214 length = 0; 228 215 while (size > length + sizeof(NETTEST1_TEXT) - 1) { 229 216 memcpy(buffer + length, NETTEST1_TEXT, … … 231 218 length += sizeof(NETTEST1_TEXT) - 1; 232 219 } 233 220 234 221 memcpy(buffer + length, NETTEST1_TEXT, size - length); 235 222 buffer[size] = '\0'; … … 238 225 static int nettest1_test(int *socket_ids, int nsockets, int nmessages) 239 226 { 227 int rc; 228 240 229 if (verbose) 241 230 printf("%d sockets, %d messages\n", nsockets, nmessages); 242 243 intrc = sockets_create(verbose, socket_ids, nsockets, family, type);244 if (rc != EOK) 245 return rc; 246 231 232 rc = sockets_create(verbose, socket_ids, nsockets, family, type); 233 if (rc != EOK) 234 return rc; 235 247 236 if (type == SOCK_STREAM) { 248 237 rc = sockets_connect(verbose, socket_ids, nsockets, address, … … 251 240 return rc; 252 241 } 253 242 254 243 rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address, 255 &addrlen, data, size, nmessages , type);256 if (rc != EOK) 257 return rc; 258 244 &addrlen, data, size, nmessages); 245 if (rc != EOK) 246 return rc; 247 259 248 rc = sockets_close(verbose, socket_ids, nsockets); 260 249 if (rc != EOK) 261 250 return rc; 262 251 263 252 if (verbose) 264 253 printf("\tOK\n"); 265 254 266 255 /****/ 267 256 268 257 rc = sockets_create(verbose, socket_ids, nsockets, family, type); 269 258 if (rc != EOK) 270 259 return rc; 271 260 272 261 if (type == SOCK_STREAM) { 273 262 rc = sockets_connect(verbose, socket_ids, nsockets, address, … … 276 265 return rc; 277 266 } 278 267 279 268 rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen, 280 data, size, nmessages , type);281 if (rc != EOK) 282 return rc; 283 269 data, size, nmessages); 270 if (rc != EOK) 271 return rc; 272 284 273 rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen, 285 274 data, size, nmessages); 286 275 if (rc != EOK) 287 276 return rc; 288 277 289 278 rc = sockets_close(verbose, socket_ids, nsockets); 290 279 if (rc != EOK) 291 280 return rc; 292 281 293 282 if (verbose) 294 283 printf("\tOK\n"); 295 284 296 285 return EOK; 297 286 } … … 299 288 int main(int argc, char *argv[]) 300 289 { 290 struct sockaddr_in address_in; 291 struct sockaddr_in6 address_in6; 292 uint8_t *address_start; 293 294 int *socket_ids; 295 int index; 296 struct timeval time_before; 297 struct timeval time_after; 298 299 int rc; 300 301 sockets = 10; 302 messages = 10; 303 port = 7; 304 301 305 /* 302 306 * Parse the command line arguments. Stop before the last argument 303 307 * if it does not start with dash ('-') 304 308 */ 305 int index; 306 int rc; 307 308 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 309 (argv[index][0] == '-')); index++) { 309 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) { 310 310 /* Options should start with dash ('-') */ 311 311 if (argv[index][0] == '-') { … … 318 318 } 319 319 } 320 321 /* The last argument containing the host*/320 321 /* If not before the last argument containing the address */ 322 322 if (index >= argc) { 323 printf(" Host name missing.\n");323 printf("Command line error: missing address\n"); 324 324 nettest1_print_help(); 325 325 return EINVAL; 326 326 } 327 328 char *addr_s = argv[argc - 1]; 329 330 /* Interpret as address */ 331 inet_addr_t addr_addr; 332 rc = inet_addr_parse(addr_s, &addr_addr); 333 334 if (rc != EOK) { 335 /* Interpret as a host name */ 336 dnsr_hostinfo_t *hinfo = NULL; 337 rc = dnsr_name2host(addr_s, &hinfo, family); 338 339 if (rc != EOK) { 340 printf("Error resolving host '%s'.\n", addr_s); 341 return EINVAL; 342 } 343 344 addr_addr = hinfo->addr; 345 } 346 347 struct sockaddr_in addr; 348 struct sockaddr_in6 addr6; 349 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 350 351 if (af != family) { 352 printf("Address family does not match explicitly set family.\n"); 353 return EINVAL; 354 } 355 327 356 328 /* Prepare the address buffer */ 357 358 switch (af) { 359 case AF_INET: 360 addr.sin_port = htons(port); 361 address = (struct sockaddr *) &addr; 362 addrlen = sizeof(addr); 363 break; 364 case AF_INET6: 365 addr6.sin6_port = htons(port); 366 address = (struct sockaddr *) &addr6; 367 addrlen = sizeof(addr6); 329 330 switch (family) { 331 case PF_INET: 332 address_in.sin_family = AF_INET; 333 address_in.sin_port = htons(port); 334 address = (struct sockaddr *) &address_in; 335 addrlen = sizeof(address_in); 336 address_start = (uint8_t *) &address_in.sin_addr.s_addr; 337 break; 338 case PF_INET6: 339 address_in6.sin6_family = AF_INET6; 340 address_in6.sin6_port = htons(port); 341 address = (struct sockaddr *) &address_in6; 342 addrlen = sizeof(address_in6); 343 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr; 368 344 break; 369 345 default: … … 371 347 return EAFNOSUPPORT; 372 348 } 373 349 350 /* Parse the last argument which should contain the address */ 351 rc = inet_pton(family, argv[argc - 1], address_start); 352 if (rc != EOK) { 353 fprintf(stderr, "Address parse error %d\n", rc); 354 return rc; 355 } 356 374 357 /* Check data buffer size */ 375 358 if (size <= 0) { … … 378 361 size = 1024; 379 362 } 380 363 381 364 /* 382 365 * Prepare data buffer. Allocate size bytes plus one for the … … 389 372 } 390 373 nettest1_fill_buffer(data, size); 391 374 392 375 /* Check socket count */ 393 376 if (sockets <= 0) { … … 396 379 sockets = 2; 397 380 } 398 381 399 382 /* 400 383 * Prepare socket buffer. Allocate count fields plus the terminating 401 384 * null (\0). 402 385 */ 403 int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));386 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 404 387 if (!socket_ids) { 405 388 fprintf(stderr, "Failed to allocate receive buffer.\n"); 406 389 return ENOMEM; 407 390 } 408 409 391 socket_ids[sockets] = 0; 410 392 411 393 if (verbose) 412 394 printf("Starting tests\n"); 413 414 struct timeval time_before; 395 415 396 rc = gettimeofday(&time_before, NULL); 416 397 if (rc != EOK) { … … 418 399 return rc; 419 400 } 420 401 421 402 nettest1_test(socket_ids, 1, 1); 422 403 nettest1_test(socket_ids, 1, messages); 423 404 nettest1_test(socket_ids, sockets, 1); 424 405 nettest1_test(socket_ids, sockets, messages); 425 426 struct timeval time_after; 406 427 407 rc = gettimeofday(&time_after, NULL); 428 408 if (rc != EOK) { … … 430 410 return rc; 431 411 } 432 412 433 413 printf("Tested in %ld microseconds\n", tv_sub(&time_after, 434 414 &time_before)); 435 415 436 416 if (verbose) 437 417 printf("Exiting\n"); 438 418 439 419 return EOK; 440 420 } 441 421 422 442 423 /** @} 443 424 */
Note:
See TracChangeset
for help on using the changeset viewer.