Changeset 987930f in mainline
- Timestamp:
- 2010-11-07T13:21:35Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 69e0d6d
- Parents:
- 5d0f1bc
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nettest2/nettest2.c
r5d0f1bc r987930f 44 44 #include <time.h> 45 45 #include <arg_parse.h> 46 #include <bool.h> 46 47 47 48 #include <net/in.h> … … 57 58 #define NETTEST2_TEXT "Networking test 2 - transfer" 58 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; 67 59 68 static void nettest2_print_help(void) 60 69 { 61 70 printf( 62 "Network Networking test 2 aplication - UDP transfer\n" \ 63 "Usage: echo [options] numeric_address\n" \ 64 "Where options are:\n" \ 65 "-f protocol_family | --family=protocol_family\n" \ 66 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n" 67 "\n" \ 68 "-h | --help\n" \ 69 "\tShow this application help.\n" 70 "\n" \ 71 "-m count | --messages=count\n" \ 72 "\tThe number of messages to send and receive per socket. The default is 10.\n" \ 73 "\n" \ 74 "-n sockets | --sockets=count\n" \ 75 "\tThe number of sockets to use. The default is 10.\n" \ 76 "\n" \ 77 "-p port_number | --port=port_number\n" \ 78 "\tThe port number the application should send messages to. The default is 7.\n" \ 79 "\n" \ 80 "-s packet_size | --size=packet_size\n" \ 81 "\tThe packet data size the application sends. The default is 29 bytes.\n" \ 82 "\n" \ 83 "-v | --verbose\n" \ 84 "\tShow all output messages.\n" 85 ); 71 "Network Networking test 2 aplication - UDP transfer\n" 72 "Usage: echo [options] address\n" 73 "Where options are:\n" 74 "-f protocol_family | --family=protocol_family\n" 75 "\tThe listenning socket protocol family. Only the PF_INET and " 76 "PF_INET6 are supported.\n" 77 "\n" 78 "-h | --help\n" 79 "\tShow this application help.\n" 80 "\n" 81 "-m count | --messages=count\n" 82 "\tThe number of messages to send and receive per socket. The " 83 "default is 10.\n" 84 "\n" 85 "-n sockets | --sockets=count\n" 86 "\tThe number of sockets to use. The default is 10.\n" 87 "\n" 88 "-p port_number | --port=port_number\n" 89 "\tThe port number the application should send messages to. The " 90 "default is 7.\n" 91 "\n" 92 "-s packet_size | --size=packet_size\n" 93 "\tThe packet data size the application sends. The default is 29 " 94 "bytes.\n" 95 "\n" 96 "-v | --verbose\n" 97 "\tShow all output messages.\n"); 86 98 } 87 99 88 /** Refreshes the data.100 /** Fill buffer with the NETTEST1_TEXT pattern. 89 101 * 90 * Fills the data block with the NETTEST1_TEXT pattern. 91 * 92 * @param[out] data The data block. 93 * @param[in] size The data block size in bytes. 94 */ 95 static void nettest2_refresh_data(char *data, size_t size) 102 * @param buffer Data buffer. 103 * @param size Buffer size in bytes. 104 */ 105 static void nettest2_fill_buffer(char *buffer, size_t size) 96 106 { 97 107 size_t length; 98 108 99 // fill the data100 109 length = 0; 101 110 while (size > length + sizeof(NETTEST2_TEXT) - 1) { 102 memcpy(data + length, NETTEST2_TEXT, sizeof(NETTEST2_TEXT) - 1); 111 memcpy(buffer + length, NETTEST2_TEXT, 112 sizeof(NETTEST2_TEXT) - 1); 103 113 length += sizeof(NETTEST2_TEXT) - 1; 104 114 } 105 memcpy(data + length, NETTEST2_TEXT, size - length); 106 data[size] = '\0'; 115 116 memcpy(buffer + length, NETTEST2_TEXT, size - length); 117 buffer[size] = '\0'; 107 118 } 108 119 109 110 int main(int argc, char *argv[]) 120 /** Parse one command-line option. 121 * 122 * @param argc Number of all command-line arguments. 123 * @param argv All command-line arguments. 124 * @param index Current argument index (in, out). 125 */ 126 static int nettest2_parse_opt(int argc, char *argv[], int *index) 111 127 { 112 size_t size = 28;113 int verbose = 0;114 sock_type_t type = SOCK_DGRAM;115 int sockets = 10;116 int messages = 10;117 int family = PF_INET;118 uint16_t port = 7;119 120 socklen_t max_length = sizeof(struct sockaddr_in6);121 uint8_t address_data[max_length];122 struct sockaddr *address = (struct sockaddr *) address_data;123 struct sockaddr_in *address_in = (struct sockaddr_in *) address;124 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address;125 socklen_t addrlen;126 uint8_t *address_start;127 128 int *socket_ids;129 char *data;130 128 int value; 131 int index;132 struct timeval time_before;133 struct timeval time_after;134 135 129 int rc; 136 130 137 // parse the command line arguments 138 // stop before the last argument if it does not start with the minus sign ('-') 139 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index) { 140 // options should start with the minus sign ('-') 141 if (argv[index][0] == '-') { 142 switch(argv[index][1]){ 143 // short options with only one letter 144 case 'f': 145 rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family); 146 if (rc != EOK) 147 return rc; 148 break; 149 case 'h': 150 nettest2_print_help(); 151 return EOK; 152 break; 153 case 'm': 154 rc = arg_parse_int(argc, argv, &index, &messages, 0); 155 if (rc != EOK) 156 return rc; 157 break; 158 case 'n': 159 rc = arg_parse_int(argc, argv, &index, &sockets, 0); 160 if (rc != EOK) 161 return rc; 162 break; 163 case 'p': 164 rc = arg_parse_int(argc, argv, &index, &value, 0); 165 if (rc != EOK) 166 return rc; 167 port = (uint16_t) value; 168 break; 169 case 's': 170 rc = arg_parse_int(argc, argv, &index, &value, 0); 171 if (rc != EOK) 172 return rc; 173 size = (value >= 0) ? (size_t) value : 0; 174 break; 175 case 't': 176 rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type); 177 if (rc != EOK) 178 return rc; 179 type = (sock_type_t) value; 180 break; 181 case 'v': 182 verbose = 1; 183 break; 184 // long options with the double minus sign ('-') 185 case '-': 186 if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 187 rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family); 188 if (rc != EOK) 189 return rc; 190 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 191 nettest2_print_help(); 192 return EOK; 193 } else if (str_lcmp(argv[index] + 2, "messages=", 6) == 0) { 194 rc = arg_parse_int(argc, argv, &index, &messages, 8); 195 if (rc != EOK) 196 return rc; 197 } else if (str_lcmp(argv[index] + 2, "sockets=", 6) == 0) { 198 rc = arg_parse_int(argc, argv, &index, &sockets, 8); 199 if (rc != EOK) 200 return rc; 201 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 202 rc = arg_parse_int(argc, argv, &index, &value, 7); 203 if (rc != EOK) 204 return rc; 205 port = (uint16_t) value; 206 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 207 rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type); 208 if (rc != EOK) 209 return rc; 210 type = (sock_type_t) value; 211 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { 212 verbose = 1; 213 } else { 214 nettest2_print_help(); 215 return EINVAL; 216 } 217 break; 218 default: 219 nettest2_print_help(); 220 return EINVAL; 221 } 131 rc = EOK; 132 133 switch (argv[*index][1]) { 134 /* 135 * Short options with only one letter 136 */ 137 case 'f': 138 rc = arg_parse_name_int(argc, argv, index, &family, 0, 139 socket_parse_protocol_family); 140 if (rc != EOK) 141 return rc; 142 break; 143 case 'h': 144 nettest2_print_help(); 145 return EOK; 146 break; 147 case 'm': 148 rc = arg_parse_int(argc, argv, index, &messages, 0); 149 if (rc != EOK) 150 return rc; 151 break; 152 case 'n': 153 rc = arg_parse_int(argc, argv, index, &sockets, 0); 154 if (rc != EOK) 155 return rc; 156 break; 157 case 'p': 158 rc = arg_parse_int(argc, argv, index, &value, 0); 159 if (rc != EOK) 160 return rc; 161 port = (uint16_t) value; 162 break; 163 case 's': 164 rc = arg_parse_int(argc, argv, index, &value, 0); 165 if (rc != EOK) 166 return rc; 167 size = (value >= 0) ? (size_t) value : 0; 168 break; 169 case 't': 170 rc = arg_parse_name_int(argc, argv, index, &value, 0, 171 socket_parse_socket_type); 172 if (rc != EOK) 173 return rc; 174 type = (sock_type_t) value; 175 break; 176 case 'v': 177 verbose = true; 178 break; 179 /* 180 * Long options with double dash ('-') 181 */ 182 case '-': 183 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { 184 rc = arg_parse_name_int(argc, argv, index, &family, 9, 185 socket_parse_protocol_family); 186 if (rc != EOK) 187 return rc; 188 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { 189 nettest2_print_help(); 190 return EOK; 191 } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) { 192 rc = arg_parse_int(argc, argv, index, &messages, 8); 193 if (rc != EOK) 194 return rc; 195 } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) { 196 rc = arg_parse_int(argc, argv, index, &sockets, 8); 197 if (rc != EOK) 198 return rc; 199 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) { 200 rc = arg_parse_int(argc, argv, index, &value, 7); 201 if (rc != EOK) 202 return rc; 203 port = (uint16_t) value; 204 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { 205 rc = arg_parse_name_int(argc, argv, index, &value, 7, 206 socket_parse_socket_type); 207 if (rc != EOK) 208 return rc; 209 type = (sock_type_t) value; 210 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { 211 verbose = 1; 222 212 } else { 223 213 nettest2_print_help(); 224 214 return EINVAL; 225 215 } 226 } 227 228 // if not before the last argument containing the address 216 break; 217 default: 218 nettest2_print_help(); 219 return EINVAL; 220 } 221 222 return EOK; 223 } 224 225 int main(int argc, char *argv[]) 226 { 227 socklen_t max_length; 228 uint8_t address_data[sizeof(struct sockaddr_in6)]; 229 struct sockaddr *address; 230 struct sockaddr_in *address_in; 231 struct sockaddr_in6 *address_in6; 232 socklen_t addrlen; 233 uint8_t *address_start; 234 235 int *socket_ids; 236 char *data; 237 int index; 238 struct timeval time_before; 239 struct timeval time_after; 240 241 int rc; 242 243 size = 28; 244 verbose = false; 245 type = SOCK_DGRAM; 246 sockets = 10; 247 messages = 10; 248 family = PF_INET; 249 port = 7; 250 251 max_length = sizeof(address_data); 252 address = (struct sockaddr *) address_data; 253 address_in = (struct sockaddr_in *) address; 254 address_in6 = (struct sockaddr_in6 *) address; 255 256 /* 257 * Parse the command line arguments. 258 * 259 * Stop before the last argument if it does not start with dash ('-') 260 */ 261 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 262 (argv[index][0] == '-')); ++index) { 263 264 /* Options should start with dash ('-') */ 265 if (argv[index][0] == '-') { 266 rc = nettest2_parse_opt(argc, argv, &index); 267 if (rc != EOK) 268 return rc; 269 } else { 270 nettest2_print_help(); 271 return EINVAL; 272 } 273 } 274 275 /* If not before the last argument containing the address */ 229 276 if (index >= argc) { 230 277 printf("Command line error: missing address\n"); … … 233 280 } 234 281 235 / / prepare the address buffer282 /* Prepare the address buffer */ 236 283 bzero(address_data, max_length); 284 237 285 switch (family) { 238 286 case PF_INET: … … 253 301 } 254 302 255 / / parse the last argument which should contain the address303 /* Parse the last argument which should contain the address. */ 256 304 rc = inet_pton(family, argv[argc - 1], address_start); 257 305 if (rc != EOK) { … … 260 308 } 261 309 262 / / check the buffer size310 /* Check data buffer size. */ 263 311 if (size <= 0) { 264 fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size); 312 fprintf(stderr, "Data buffer size too small (%d). Using 1024 " 313 "bytes instead.\n", size); 265 314 size = 1024; 266 315 } 267 316 268 // prepare the buffer 269 // size plus terminating null (\0) 317 /* 318 * Prepare the buffer. Allocate size bytes plus one for terminating 319 * null character. 320 */ 270 321 data = (char *) malloc(size + 1); 271 322 if (!data) { … … 273 324 return ENOMEM; 274 325 } 275 nettest2_refresh_data(data, size); 276 277 // check the socket count 326 327 /* Fill buffer with a pattern. */ 328 nettest2_fill_buffer(data, size); 329 330 /* Check socket count. */ 278 331 if (sockets <= 0) { 279 fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets); 332 fprintf(stderr, "Socket count too small (%d). Using " 333 "2 instead.\n", sockets); 280 334 sockets = 2; 281 335 } 282 336 283 // prepare the socket buffer 284 // count plus the terminating null (\0) 337 /* 338 * Prepare the socket buffer. 339 * Allocate count entries plus the terminating null (\0) 340 */ 285 341 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 286 342 if (!socket_ids) { … … 298 354 299 355 if (type == SOCK_STREAM) { 300 rc = sockets_connect(verbose, socket_ids, sockets, address, addrlen); 356 rc = sockets_connect(verbose, socket_ids, sockets, 357 address, addrlen); 301 358 if (rc != EOK) 302 359 return rc; … … 312 369 } 313 370 314 rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages); 371 rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, 372 &addrlen, data, size, messages); 315 373 if (rc != EOK) 316 374 return rc; … … 325 383 printf("\tOK\n"); 326 384 327 printf("sendto + recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 385 printf("sendto + recvfrom tested in %d microseconds\n", 386 tv_sub(&time_after, &time_before)); 328 387 329 388 rc = gettimeofday(&time_before, NULL); … … 333 392 } 334 393 335 rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages); 394 rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen, 395 data, size, messages); 336 396 if (rc != EOK) 337 397 return rc; 338 398 339 rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages); 399 rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, 400 data, size, messages); 340 401 if (rc != EOK) 341 402 return rc; … … 350 411 printf("\tOK\n"); 351 412 352 printf("sendto, recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 413 printf("sendto, recvfrom tested in %d microseconds\n", 414 tv_sub(&time_after, &time_before)); 353 415 354 416 rc = sockets_close(verbose, socket_ids, sockets);
Note:
See TracChangeset
for help on using the changeset viewer.