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