Changes in uspace/app/nettest2/nettest2.c [c442f63:3e6a98c5] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nettest2/nettest2.c
rc442f63 r3e6a98c5 47 47 #include <stdbool.h> 48 48 49 #include <inet/dnsr.h>50 49 #include <net/in.h> 51 50 #include <net/in6.h> … … 55 54 56 55 /** Echo module name. */ 57 #define NAME "nettest2"56 #define NAME "Nettest2" 58 57 59 58 /** Packet data pattern. */ 60 #define NETTEST2_TEXT 61 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;59 #define NETTEST2_TEXT "Networking test 2 - transfer" 60 61 static size_t size; 62 static bool verbose; 63 static sock_type_t type; 64 static int sockets; 65 static int messages; 66 static int family; 67 static uint16_t port; 69 68 70 69 static void nettest2_print_help(void) … … 72 71 printf( 73 72 "Network Networking test 2 aplication - UDP transfer\n" 74 "Usage: nettest2 [options] host\n"73 "Usage: echo [options] address\n" 75 74 "Where options are:\n" 76 75 "-f protocol_family | --family=protocol_family\n" … … 107 106 static void nettest2_fill_buffer(char *buffer, size_t size) 108 107 { 109 size_t length = 0; 108 size_t length; 109 110 length = 0; 110 111 while (size > length + sizeof(NETTEST2_TEXT) - 1) { 111 112 memcpy(buffer + length, NETTEST2_TEXT, … … 113 114 length += sizeof(NETTEST2_TEXT) - 1; 114 115 } 115 116 116 117 memcpy(buffer + length, NETTEST2_TEXT, size - length); 117 118 buffer[size] = '\0'; … … 128 129 int value; 129 130 int rc; 130 131 131 132 switch (argv[*index][1]) { 132 133 /* … … 134 135 */ 135 136 case 'f': 136 rc = arg_parse_name_int(argc, argv, index, & value, 0,137 rc = arg_parse_name_int(argc, argv, index, &family, 0, 137 138 socket_parse_protocol_family); 138 139 if (rc != EOK) 139 140 return rc; 140 141 family = (uint16_t) value;142 141 break; 143 142 case 'h': 144 143 nettest2_print_help(); 145 144 return EOK; 145 break; 146 146 case 'm': 147 147 rc = arg_parse_int(argc, argv, index, &messages, 0); 148 148 if (rc != EOK) 149 149 return rc; 150 151 150 break; 152 151 case 'n': … … 154 153 if (rc != EOK) 155 154 return rc; 156 157 155 break; 158 156 case 'p': … … 160 158 if (rc != EOK) 161 159 return rc; 162 163 160 port = (uint16_t) value; 164 161 break; … … 167 164 if (rc != EOK) 168 165 return rc; 169 170 166 size = (value >= 0) ? (size_t) value : 0; 171 167 break; … … 175 171 if (rc != EOK) 176 172 return rc; 177 178 173 type = (sock_type_t) value; 179 174 break; … … 181 176 verbose = true; 182 177 break; 183 184 178 /* 185 179 * Long options with double dash ('-') … … 187 181 case '-': 188 182 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { 189 rc = arg_parse_name_int(argc, argv, index, & value, 9,183 rc = arg_parse_name_int(argc, argv, index, &family, 9, 190 184 socket_parse_protocol_family); 191 185 if (rc != EOK) 192 186 return rc; 193 194 family = (uint16_t) value;195 187 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { 196 188 nettest2_print_help(); … … 208 200 if (rc != EOK) 209 201 return rc; 210 211 202 port = (uint16_t) value; 212 203 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { … … 215 206 if (rc != EOK) 216 207 return rc; 217 218 208 type = (sock_type_t) value; 219 209 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { … … 228 218 return EINVAL; 229 219 } 230 220 231 221 return EOK; 232 222 } … … 234 224 int main(int argc, char *argv[]) 235 225 { 226 struct sockaddr *address; 227 struct sockaddr_in address_in; 228 struct sockaddr_in6 address_in6; 229 socklen_t addrlen; 230 uint8_t *address_start; 231 232 int *socket_ids; 233 char *data; 236 234 int index; 235 struct timeval time_before; 236 struct timeval time_after; 237 237 238 int rc; 238 239 240 size = 28; 241 verbose = false; 242 type = SOCK_DGRAM; 243 sockets = 10; 244 messages = 10; 245 family = PF_INET; 246 port = 7; 247 239 248 /* 240 249 * Parse the command line arguments. … … 244 253 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 245 254 (argv[index][0] == '-')); ++index) { 255 246 256 /* Options should start with dash ('-') */ 247 257 if (argv[index][0] == '-') { … … 254 264 } 255 265 } 256 257 /* The last argument containing the host*/266 267 /* If not before the last argument containing the address */ 258 268 if (index >= argc) { 259 printf(" Host name missing.\n");269 printf("Command line error: missing address\n"); 260 270 nettest2_print_help(); 261 271 return EINVAL; 262 272 } 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, family); 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 273 292 274 /* Prepare the address buffer */ 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); 275 276 switch (family) { 277 case PF_INET: 278 address_in.sin_family = AF_INET; 279 address_in.sin_port = htons(port); 280 address = (struct sockaddr *) &address_in; 281 addrlen = sizeof(address_in); 282 address_start = (uint8_t *) &address_in.sin_addr.s_addr; 283 break; 284 case PF_INET6: 285 address_in6.sin6_family = AF_INET6; 286 address_in6.sin6_port = htons(port); 287 address = (struct sockaddr *) &address_in6; 288 addrlen = sizeof(address_in6); 289 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr; 307 290 break; 308 291 default: … … 310 293 return EAFNOSUPPORT; 311 294 } 312 295 296 /* Parse the last argument which should contain the address. */ 297 rc = inet_pton(family, argv[argc - 1], address_start); 298 if (rc != EOK) { 299 fprintf(stderr, "Address parse error %d\n", rc); 300 return rc; 301 } 302 313 303 /* Check data buffer size. */ 314 304 if (size <= 0) { … … 317 307 size = 1024; 318 308 } 319 309 320 310 /* 321 311 * Prepare the buffer. Allocate size bytes plus one for terminating 322 312 * null character. 323 313 */ 324 char *data = (char *) malloc(size + 1);314 data = (char *) malloc(size + 1); 325 315 if (!data) { 326 316 fprintf(stderr, "Failed to allocate data buffer.\n"); 327 317 return ENOMEM; 328 318 } 329 319 330 320 /* Fill buffer with a pattern. */ 331 321 nettest2_fill_buffer(data, size); 332 322 333 323 /* Check socket count. */ 334 324 if (sockets <= 0) { … … 337 327 sockets = 2; 338 328 } 339 329 340 330 /* 341 331 * Prepare the socket buffer. 342 332 * Allocate count entries plus the terminating null (\0) 343 333 */ 344 int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));334 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 345 335 if (!socket_ids) { 346 336 fprintf(stderr, "Failed to allocate receive buffer.\n"); 347 337 return ENOMEM; 348 338 } 349 350 339 socket_ids[sockets] = 0; 351 340 352 341 if (verbose) 353 342 printf("Starting tests\n"); 354 343 355 344 rc = sockets_create(verbose, socket_ids, sockets, family, type); 356 345 if (rc != EOK) 357 346 return rc; 358 347 359 348 if (type == SOCK_STREAM) { 360 349 rc = sockets_connect(verbose, socket_ids, sockets, … … 363 352 return rc; 364 353 } 365 354 366 355 if (verbose) 367 356 printf("\n"); 368 369 struct timeval time_before; 357 370 358 rc = gettimeofday(&time_before, NULL); 371 359 if (rc != EOK) { … … 373 361 return rc; 374 362 } 375 363 376 364 rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, 377 &addrlen, data, size, messages , type);365 &addrlen, data, size, messages); 378 366 if (rc != EOK) 379 367 return rc; 380 381 struct timeval time_after; 368 382 369 rc = gettimeofday(&time_after, NULL); 383 370 if (rc != EOK) { … … 385 372 return rc; 386 373 } 387 374 388 375 if (verbose) 389 376 printf("\tOK\n"); 390 377 391 378 printf("sendto + recvfrom tested in %ld microseconds\n", 392 379 tv_sub(&time_after, &time_before)); 393 380 394 381 rc = gettimeofday(&time_before, NULL); 395 382 if (rc != EOK) { … … 397 384 return rc; 398 385 } 399 386 400 387 rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen, 401 data, size, messages , type);388 data, size, messages); 402 389 if (rc != EOK) 403 390 return rc; 404 391 405 392 rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, 406 393 data, size, messages); 407 394 if (rc != EOK) 408 395 return rc; 409 396 410 397 rc = gettimeofday(&time_after, NULL); 411 398 if (rc != EOK) { … … 413 400 return rc; 414 401 } 415 402 416 403 if (verbose) 417 404 printf("\tOK\n"); 418 405 419 406 printf("sendto, recvfrom tested in %ld microseconds\n", 420 407 tv_sub(&time_after, &time_before)); 421 408 422 409 rc = sockets_close(verbose, socket_ids, sockets); 423 410 if (rc != EOK) 424 411 return rc; 425 412 426 413 if (verbose) 427 414 printf("\nExiting\n"); 428 415 429 416 return EOK; 430 417 }
Note:
See TracChangeset
for help on using the changeset viewer.