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