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