Changeset 0bbef9b in mainline
- Timestamp:
- 2010-11-06T22:54:25Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5f69504
- Parents:
- 1712f87
- Location:
- uspace/app/nettest1
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nettest1/nettest.c
r1712f87 r0bbef9b 36 36 37 37 #include <stdio.h> 38 #include <err.h>39 40 38 #include <net/socket.h> 41 39 … … 87 85 int sockets_close(int verbose, int *socket_ids, int sockets) 88 86 { 89 ERROR_DECLARE; 90 91 int index; 87 int index; 88 int rc; 92 89 93 90 if (verbose) … … 97 94 98 95 for (index = 0; index < sockets; index++) { 99 if (ERROR_OCCURRED(closesocket(socket_ids[index]))) { 96 rc = closesocket(socket_ids[index]); 97 if (rc != EOK) { 100 98 printf("Socket %d (%d) error:\n", index, socket_ids[index]); 101 socket_print_error(stderr, ERROR_CODE, "Socket close: ", "\n");102 return ERROR_CODE;99 socket_print_error(stderr, rc, "Socket close: ", "\n"); 100 return rc; 103 101 } 104 102 if (verbose) … … 121 119 int sockets_connect(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen) 122 120 { 123 ERROR_DECLARE; 124 125 int index; 121 int index; 122 int rc; 126 123 127 124 if (verbose) … … 131 128 132 129 for (index = 0; index < sockets; index++) { 133 if (ERROR_OCCURRED(connect(socket_ids[index], address, addrlen))) { 134 socket_print_error(stderr, ERROR_CODE, "Socket connect: ", "\n"); 135 return ERROR_CODE; 130 rc = connect(socket_ids[index], address, addrlen); 131 if (rc != EOK) { 132 socket_print_error(stderr, rc, "Socket connect: ", "\n"); 133 return rc; 136 134 } 137 135 if (verbose) … … 157 155 int sockets_sendto(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen, char *data, int size, int messages) 158 156 { 159 ERROR_DECLARE;160 161 157 int index; 162 158 int message; 159 int rc; 163 160 164 161 if (verbose) … … 169 166 for (index = 0; index < sockets; index++) { 170 167 for (message = 0; message < messages; message++) { 171 if (ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, addrlen))) { 172 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 173 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 174 return ERROR_CODE; 168 rc = sendto(socket_ids[index], data, size, 0, address, addrlen); 169 if (rc != EOK) { 170 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 171 socket_print_error(stderr, rc, "Socket send: ", "\n"); 172 return rc; 175 173 } 176 174 } … … 239 237 int sockets_sendto_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages) 240 238 { 241 ERROR_DECLARE;242 243 239 int value; 244 240 int index; 245 241 int message; 242 int rc; 246 243 247 244 if (verbose) … … 252 249 for (index = 0; index < sockets; index++) { 253 250 for (message = 0; message < messages; message++) { 254 if (ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, *addrlen))) { 255 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 256 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 257 return ERROR_CODE; 251 rc = sendto(socket_ids[index], data, size, 0, address, *addrlen); 252 if (rc != EOK) { 253 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 254 socket_print_error(stderr, rc, "Socket send: ", "\n"); 255 return rc; 258 256 } 259 257 value = recvfrom(socket_ids[index], data, size, 0, address, addrlen); -
uspace/app/nettest1/nettest1.c
r1712f87 r0bbef9b 44 44 #include <time.h> 45 45 #include <arg_parse.h> 46 #include <err.h>47 46 48 47 #include <net/in.h> … … 57 56 /** Packet data pattern. */ 58 57 #define NETTEST1_TEXT "Networking test 1 - sockets" 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 67 60 68 static void nettest1_print_help(void) … … 108 116 } 109 117 118 static int nettest1_test(int *socket_ids, int nsockets, int nmessages) 119 { 120 int rc; 121 122 if (verbose) 123 printf("%d sockets, %d messages\n", nsockets, nmessages); 124 125 rc = sockets_create(verbose, socket_ids, nsockets, family, type); 126 if (rc != EOK) 127 return rc; 128 129 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); 136 if (rc != EOK) 137 return rc; 138 139 rc = sockets_close(verbose, socket_ids, nsockets); 140 if (rc != EOK) 141 return rc; 142 143 if (verbose) 144 printf("\tOK\n"); 145 146 /****/ 147 148 rc = sockets_create(verbose, socket_ids, nsockets, family, type); 149 if (rc != EOK) 150 return rc; 151 152 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); 163 if (rc != EOK) 164 return rc; 165 166 rc = sockets_close(verbose, socket_ids, nsockets); 167 if (rc != EOK) 168 return rc; 169 170 if (verbose) 171 printf("\tOK\n"); 172 173 return EOK; 174 } 110 175 111 176 int main(int argc, char *argv[]) 112 177 { 113 ERROR_DECLARE;114 115 size_t size = 27;116 int verbose = 0;117 sock_type_t type = SOCK_DGRAM;118 178 int sockets = 10; 119 179 int messages = 10; 120 int family = PF_INET;121 180 uint16_t port = 7; 122 181 123 socklen_t max_length = sizeof(struct sockaddr_in6); 124 uint8_t address_data[max_length]; 125 struct sockaddr *address = (struct sockaddr *) address_data; 126 struct sockaddr_in *address_in = (struct sockaddr_in *) address; 127 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address; 128 socklen_t addrlen; 182 socklen_t max_length; 183 uint8_t *address_data[sizeof(struct sockaddr_in6)]; 184 struct sockaddr_in *address_in; 185 struct sockaddr_in6 *address_in6; 129 186 uint8_t *address_start; 130 187 131 188 int *socket_ids; 132 char *data;133 189 int value; 134 190 int index; 135 191 struct timeval time_before; 136 192 struct timeval time_after; 193 194 int rc; 195 196 max_length = sizeof(address_data); 197 address = (struct sockaddr *) address_data; 198 address_in = (struct sockaddr_in *) address; 199 address_in6 = (struct sockaddr_in6 *) address; 137 200 138 201 // parse the command line arguments … … 144 207 // short options with only one letter 145 208 case 'f': 146 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 209 rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family); 210 if (rc != EOK) 211 return rc; 147 212 break; 148 213 case 'h': 149 214 nettest1_print_help(); 150 215 return EOK; 151 break;152 216 case 'm': 153 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 0)); 217 rc = arg_parse_int(argc, argv, &index, &messages, 0); 218 if (rc != EOK) 219 return rc; 154 220 break; 155 221 case 'n': 156 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 0)); 222 rc = arg_parse_int(argc, argv, &index, &sockets, 0); 223 if (rc != EOK) 224 return rc; 157 225 break; 158 226 case 'p': 159 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 227 rc = arg_parse_int(argc, argv, &index, &value, 0); 228 if (rc != EOK) 229 return rc; 160 230 port = (uint16_t) value; 161 231 break; 162 232 case 's': 163 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 233 rc = arg_parse_int(argc, argv, &index, &value, 0); 234 if (rc != EOK) 235 return rc; 164 236 size = (value >= 0) ? (size_t) value : 0; 165 237 break; 166 238 case 't': 167 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 239 rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type); 240 if (rc != EOK) 241 return rc; 168 242 type = (sock_type_t) value; 169 243 break; … … 174 248 case '-': 175 249 if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 176 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 250 rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family); 251 if (rc != EOK) 252 return rc; 177 253 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 178 254 nettest1_print_help(); 179 255 return EOK; 180 256 } else if (str_lcmp(argv[index] + 2, "messages=", 6) == 0) { 181 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 8)); 257 rc = arg_parse_int(argc, argv, &index, &messages, 8); 258 if (rc != EOK) 259 return rc; 182 260 } else if (str_lcmp(argv[index] + 2, "sockets=", 6) == 0) { 183 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 8)); 261 rc = arg_parse_int(argc, argv, &index, &sockets, 8); 262 if (rc != EOK) 263 return rc; 184 264 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 185 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 265 rc = arg_parse_int(argc, argv, &index, &value, 7); 266 if (rc != EOK) 267 return rc; 186 268 port = (uint16_t) value; 187 269 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 188 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 270 rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type); 271 if (rc != EOK) 272 return rc; 189 273 type = (sock_type_t) value; 190 274 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { … … 233 317 234 318 // parse the last argument which should contain the address 235 if (ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))) { 236 fprintf(stderr, "Address parse error %d\n", ERROR_CODE); 237 return ERROR_CODE; 319 rc = inet_pton(family, argv[argc - 1], address_start); 320 if (rc != EOK) { 321 fprintf(stderr, "Address parse error %d\n", rc); 322 return rc; 238 323 } 239 324 … … 271 356 printf("Starting tests\n"); 272 357 273 if (verbose) 274 printf("1 socket, 1 message\n"); 275 276 if (ERROR_OCCURRED(gettimeofday(&time_before, NULL))) { 277 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 278 return ERROR_CODE; 279 } 280 281 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 282 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 283 if (verbose) 284 printf("\tOK\n"); 285 286 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 287 if (type == SOCK_STREAM) 288 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 289 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1)); 290 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 291 if (verbose) 292 printf("\tOK\n"); 293 294 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 295 if (type == SOCK_STREAM) 296 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 297 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, 1)); 298 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1)); 299 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 300 if (verbose) 301 printf("\tOK\n"); 302 303 if (verbose) 304 printf("1 socket, %d messages\n", messages); 305 306 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 307 if (type == SOCK_STREAM) 308 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 309 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages)); 310 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 311 if (verbose) 312 printf("\tOK\n"); 313 314 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 315 if (type == SOCK_STREAM) 316 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 317 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, messages)); 318 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages)); 319 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 320 if (verbose) 321 printf("\tOK\n"); 322 323 if (verbose) 324 printf("%d sockets, 1 message\n", sockets); 325 326 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 327 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 328 if (verbose) 329 printf("\tOK\n"); 330 331 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 332 if (type == SOCK_STREAM) 333 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 334 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1)); 335 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 336 if (verbose) 337 printf("\tOK\n"); 338 339 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 340 if (type == SOCK_STREAM) 341 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 342 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, 1)); 343 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1)); 344 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 345 if (verbose) 346 printf("\tOK\n"); 347 348 if (verbose) 349 printf("%d sockets, %d messages\n", sockets, messages); 350 351 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 352 if (type == SOCK_STREAM) 353 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 354 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 355 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 356 if (verbose) 357 printf("\tOK\n"); 358 359 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 360 if (type == SOCK_STREAM) 361 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 362 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages)); 363 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 364 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 365 366 if (ERROR_OCCURRED(gettimeofday(&time_after, NULL))) { 367 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 368 return ERROR_CODE; 369 } 370 371 if (verbose) 372 printf("\tOK\n"); 358 rc = gettimeofday(&time_before, NULL); 359 if (rc != EOK) { 360 fprintf(stderr, "Get time of day error %d\n", rc); 361 return rc; 362 } 363 364 nettest1_test(socket_ids, 1, 1); 365 nettest1_test(socket_ids, 1, messages); 366 nettest1_test(socket_ids, sockets, 1); 367 nettest1_test(socket_ids, sockets, messages); 368 369 rc = gettimeofday(&time_after, NULL); 370 if (rc != EOK) { 371 fprintf(stderr, "Get time of day error %d\n", rc); 372 return rc; 373 } 373 374 374 375 printf("Tested in %d microseconds\n", tv_sub(&time_after, &time_before));
Note:
See TracChangeset
for help on using the changeset viewer.