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