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