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