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