Changeset 3da12d74 in mainline
- Timestamp:
- 2010-11-07T13:41:35Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 9ce7eb5, 9fd39d6, b40bfac
- Parents:
- 1712f87 (diff), 69e0d6d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- uspace/app
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/netecho/netecho.c
r1712f87 r3da12d74 41 41 #include <task.h> 42 42 #include <arg_parse.h> 43 #include <err.h>44 43 45 44 #include <net/in.h> … … 91 90 int main(int argc, char *argv[]) 92 91 { 93 ERROR_DECLARE;94 95 92 size_t size = 1024; 96 93 int verbose = 0; … … 117 114 size_t reply_length; 118 115 int value; 116 int rc; 119 117 120 118 // parse the command line arguments … … 123 121 switch (argv[index][1]) { 124 122 case 'b': 125 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 0)); 123 rc = arg_parse_int(argc, argv, &index, &backlog, 0); 124 if (rc != EOK) 125 return rc; 126 126 break; 127 127 case 'c': 128 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 0)); 128 rc = arg_parse_int(argc, argv, &index, &count, 0); 129 if (rc != EOK) 130 return rc; 129 131 break; 130 132 case 'f': 131 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 133 rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family); 134 if (rc != EOK) 135 return rc; 132 136 break; 133 137 case 'h': … … 136 140 break; 137 141 case 'p': 138 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 142 rc = arg_parse_int(argc, argv, &index, &value, 0); 143 if (rc != EOK) 144 return rc; 139 145 port = (uint16_t) value; 140 146 break; 141 147 case 'r': 142 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 0)); 148 rc = arg_parse_string(argc, argv, &index, &reply, 0); 149 if (rc != EOK) 150 return rc; 143 151 break; 144 152 case 's': 145 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 153 rc = arg_parse_int(argc, argv, &index, &value, 0); 154 if (rc != EOK) 155 return rc; 146 156 size = (value >= 0) ? (size_t) value : 0; 147 157 break; 148 158 case 't': 149 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 159 rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type); 160 if (rc != EOK) 161 return rc; 150 162 type = (sock_type_t) value; 151 163 break; … … 156 168 case '-': 157 169 if (str_lcmp(argv[index] + 2, "backlog=", 6) == 0) { 158 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 8)); 170 rc = arg_parse_int(argc, argv, &index, &backlog, 8); 171 if (rc != EOK) 172 return rc; 159 173 } else if (str_lcmp(argv[index] + 2, "count=", 6) == 0) { 160 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 8));174 rc = arg_parse_int(argc, argv, &index, &count, 8); 161 175 } else if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 162 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 176 rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family); 177 if (rc != EOK) 178 return rc; 163 179 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 164 180 echo_print_help(); 165 181 return EOK; 166 182 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 167 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 183 rc = arg_parse_int(argc, argv, &index, &value, 7); 184 if (rc != EOK) 185 return rc; 168 186 port = (uint16_t) value; 169 187 } else if (str_lcmp(argv[index] + 2, "reply=", 6) == 0) { 170 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 8)); 188 rc = arg_parse_string(argc, argv, &index, &reply, 8); 189 if (rc != EOK) 190 return rc; 171 191 } else if (str_lcmp(argv[index] + 2, "size=", 5) == 0) { 172 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 192 rc = arg_parse_int(argc, argv, &index, &value, 7); 193 if (rc != EOK) 194 return rc; 173 195 size = (value >= 0) ? (size_t) value : 0; 174 196 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 175 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 197 rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type); 198 if (rc != EOK) 199 return rc; 176 200 type = (sock_type_t) value; 177 201 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { … … 240 264 } 241 265 // set the backlog 242 if (ERROR_OCCURRED(listen(listening_id, backlog))) { 243 socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n"); 244 return ERROR_CODE; 266 rc = listen(listening_id, backlog); 267 if (rc != EOK) { 268 socket_print_error(stderr, rc, "Socket listen: ", "\n"); 269 return rc; 245 270 } 246 271 } 247 272 248 273 // bind the listenning socket 249 if (ERROR_OCCURRED(bind(listening_id, address, addrlen))) { 250 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n"); 251 return ERROR_CODE; 274 rc = bind(listening_id, address, addrlen); 275 if (rc != EOK) { 276 socket_print_error(stderr, rc, "Socket bind: ", "\n"); 277 return rc; 252 278 } 253 279 … … 301 327 // parse the source address 302 328 if (address_start) { 303 if (ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))) { 304 fprintf(stderr, "Received address error %d\n", ERROR_CODE); 329 rc = inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)); 330 if (rc != EOK) { 331 fprintf(stderr, "Received address error %d\n", rc); 305 332 } else { 306 333 data[length] = '\0'; … … 311 338 312 339 // answer the request either with the static reply or the original data 313 if (ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))) 314 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 340 rc = sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen); 341 if (rc != EOK) 342 socket_print_error(stderr, rc, "Socket send: ", "\n"); 315 343 } 316 344 317 345 // close the accepted stream socket 318 346 if (type == SOCK_STREAM) { 319 if (ERROR_OCCURRED(closesocket(socket_id))) 320 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n"); 347 rc = closesocket(socket_id); 348 if (rc != EOK) 349 socket_print_error(stderr, rc, "Close socket: ", "\n"); 321 350 } 322 351 … … 335 364 336 365 // close the listenning socket 337 if (ERROR_OCCURRED(closesocket(listening_id))) { 338 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n"); 339 return ERROR_CODE; 366 rc = closesocket(listening_id); 367 if (rc != EOK) { 368 socket_print_error(stderr, rc, "Close socket: ", "\n"); 369 return rc; 340 370 } 341 371 -
uspace/app/netstart/netstart.c
r1712f87 r3da12d74 41 41 #include <task.h> 42 42 #include <str_error.h> 43 #include <err.h>44 43 #include <ipc/ipc.h> 45 44 #include <ipc/services.h> … … 72 71 int main(int argc, char *argv[]) 73 72 { 74 ERROR_DECLARE;73 int rc; 75 74 76 75 if (!spawn("networking service", "/srv/net")) … … 80 79 81 80 int net_phone = connect_to_service(SERVICE_NETWORKING); 82 if (ERROR_OCCURRED(ipc_call_sync_0_0(net_phone, NET_NET_STARTUP))) { 83 fprintf(stderr, "%s: Startup error %d\n", NAME, ERROR_CODE); 84 return ERROR_CODE; 81 rc = ipc_call_sync_0_0(net_phone, NET_NET_STARTUP); 82 if (rc != EOK) { 83 fprintf(stderr, "%s: Startup error %d\n", NAME, rc); 84 return rc; 85 85 } 86 86 -
uspace/app/nettest1/nettest.c
r1712f87 r3da12d74 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 r3da12d74 44 44 #include <time.h> 45 45 #include <arg_parse.h> 46 #include <err.h>47 46 48 47 #include <net/in.h> … … 58 57 #define NETTEST1_TEXT "Networking test 1 - sockets" 59 58 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; 71 60 72 static void nettest1_print_help(void) 61 73 { 62 74 printf( 63 "Network Networking test 1 aplication - sockets\n" \ 64 "Usage: echo [options] numeric_address\n" \ 65 "Where options are:\n" \ 66 "-f protocol_family | --family=protocol_family\n" \ 67 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n" 68 "\n" \ 69 "-h | --help\n" \ 70 "\tShow this application help.\n" 71 "\n" \ 72 "-m count | --messages=count\n" \ 73 "\tThe number of messages to send and receive per socket. The default is 10.\n" \ 74 "\n" \ 75 "-n sockets | --sockets=count\n" \ 76 "\tThe number of sockets to use. The default is 10.\n" \ 77 "\n" \ 78 "-p port_number | --port=port_number\n" \ 79 "\tThe port number the application should send messages to. The default is 7.\n" \ 80 "\n" \ 81 "-s packet_size | --size=packet_size\n" \ 82 "\tThe packet data size the application sends. The default is 28 bytes.\n" \ 83 "\n" \ 84 "-v | --verbose\n" \ 85 "\tShow all output messages.\n" 86 ); 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"); 87 102 } 88 103 89 /** Refreshes the data.104 /** Parse one command-line option. 90 105 * 91 * Fills the data block with the NETTEST1_TEXT pattern. 92 * 93 * @param[out] data The data block. 94 * @param[in] size The data block size in bytes. 95 */ 96 static void nettest1_refresh_data(char *data, size_t size) 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) 97 111 { 98 size_t length;99 100 // fill the data101 length = 0;102 while (size > length + sizeof(NETTEST1_TEXT) - 1) {103 memcpy(data + length, NETTEST1_TEXT, sizeof(NETTEST1_TEXT) - 1);104 length += sizeof(NETTEST1_TEXT) - 1;105 }106 memcpy(data + length, NETTEST1_TEXT, size - length);107 data[size] = '\0';108 }109 110 111 int main(int argc, char *argv[])112 {113 ERROR_DECLARE;114 115 size_t size = 27;116 int verbose = 0;117 sock_type_t type = SOCK_DGRAM;118 int sockets = 10;119 int messages = 10;120 int family = PF_INET;121 uint16_t port = 7;122 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;129 uint8_t *address_start;130 131 int *socket_ids;132 char *data;133 112 int value; 134 int index; 135 struct timeval time_before; 136 struct timeval time_after; 137 138 // parse the command line arguments 139 // stop before the last argument if it does not start with the minus sign ('-') 140 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) { 141 // options should start with the minus sign ('-') 142 if (argv[index][0] == '-') { 143 switch (argv[index][1]) { 144 // short options with only one letter 145 case 'f': 146 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 147 break; 148 case 'h': 149 nettest1_print_help(); 150 return EOK; 151 break; 152 case 'm': 153 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 0)); 154 break; 155 case 'n': 156 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 0)); 157 break; 158 case 'p': 159 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 160 port = (uint16_t) value; 161 break; 162 case 's': 163 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 164 size = (value >= 0) ? (size_t) value : 0; 165 break; 166 case 't': 167 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 168 type = (sock_type_t) value; 169 break; 170 case 'v': 171 verbose = 1; 172 break; 173 // long options with the double minus sign ('-') 174 case '-': 175 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)); 177 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 178 nettest1_print_help(); 179 return EOK; 180 } else if (str_lcmp(argv[index] + 2, "messages=", 6) == 0) { 181 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 8)); 182 } else if (str_lcmp(argv[index] + 2, "sockets=", 6) == 0) { 183 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 8)); 184 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 185 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 186 port = (uint16_t) value; 187 } 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)); 189 type = (sock_type_t) value; 190 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { 191 verbose = 1; 192 } else { 193 nettest1_print_help(); 194 return EINVAL; 195 } 196 break; 197 default: 198 nettest1_print_help(); 199 return EINVAL; 200 } 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; 201 191 } else { 202 192 nettest1_print_help(); 203 193 return EINVAL; 204 194 } 205 } 206 207 // if not before the last argument containing the address 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. 205 * 206 * @param buffer Data buffer. 207 * @param size Buffer size in bytes. 208 */ 209 static void nettest1_fill_buffer(char *buffer, size_t size) 210 { 211 size_t length; 212 213 length = 0; 214 while (size > length + sizeof(NETTEST1_TEXT) - 1) { 215 memcpy(buffer + length, NETTEST1_TEXT, 216 sizeof(NETTEST1_TEXT) - 1); 217 length += sizeof(NETTEST1_TEXT) - 1; 218 } 219 220 memcpy(buffer + length, NETTEST1_TEXT, size - length); 221 buffer[size] = '\0'; 222 } 223 224 static int nettest1_test(int *socket_ids, int nsockets, int nmessages) 225 { 226 int rc; 227 228 if (verbose) 229 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 235 if (type == SOCK_STREAM) { 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); 244 if (rc != EOK) 245 return rc; 246 247 rc = sockets_close(verbose, socket_ids, nsockets); 248 if (rc != EOK) 249 return rc; 250 251 if (verbose) 252 printf("\tOK\n"); 253 254 /****/ 255 256 rc = sockets_create(verbose, socket_ids, nsockets, family, type); 257 if (rc != EOK) 258 return rc; 259 260 if (type == SOCK_STREAM) { 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); 274 if (rc != EOK) 275 return rc; 276 277 rc = sockets_close(verbose, socket_ids, nsockets); 278 if (rc != EOK) 279 return rc; 280 281 if (verbose) 282 printf("\tOK\n"); 283 284 return EOK; 285 } 286 287 int main(int argc, char *argv[]) 288 { 289 290 socklen_t max_length; 291 uint8_t *address_data[sizeof(struct sockaddr_in6)]; 292 struct sockaddr_in *address_in; 293 struct sockaddr_in6 *address_in6; 294 uint8_t *address_start; 295 296 int *socket_ids; 297 int index; 298 struct timeval time_before; 299 struct timeval time_after; 300 301 int rc; 302 303 max_length = sizeof(address_data); 304 address = (struct sockaddr *) address_data; 305 address_in = (struct sockaddr_in *) address; 306 address_in6 = (struct sockaddr_in6 *) address; 307 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 */ 316 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) { 317 /* Options should start with dash ('-') */ 318 if (argv[index][0] == '-') { 319 rc = nettest1_parse_opt(argc, argv, &index); 320 if (rc != EOK) 321 return rc; 322 } else { 323 nettest1_print_help(); 324 return EINVAL; 325 } 326 } 327 328 /* If not before the last argument containing the address */ 208 329 if (index >= argc) { 209 330 printf("Command line error: missing address\n"); … … 212 333 } 213 334 214 / / prepare the address buffer335 /* Prepare the address buffer */ 215 336 bzero(address_data, max_length); 337 216 338 switch (family) { 217 339 case PF_INET: … … 232 354 } 233 355 234 // 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; 238 } 239 240 // check the buffer size 356 /* Parse the last argument which should contain the address */ 357 rc = inet_pton(family, argv[argc - 1], address_start); 358 if (rc != EOK) { 359 fprintf(stderr, "Address parse error %d\n", rc); 360 return rc; 361 } 362 363 /* Check data buffer size */ 241 364 if (size <= 0) { 242 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); 243 367 size = 1024; 244 368 } 245 369 246 // prepare the buffer 247 // size plus the terminating null (\0) 370 /* 371 * Prepare data buffer. Allocate size bytes plus one for the 372 * trailing null character. 373 */ 248 374 data = (char *) malloc(size + 1); 249 375 if (!data) { … … 251 377 return ENOMEM; 252 378 } 253 nettest1_ refresh_data(data, size);254 255 / / check the socket count379 nettest1_fill_buffer(data, size); 380 381 /* Check socket count */ 256 382 if (sockets <= 0) { 257 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); 258 385 sockets = 2; 259 386 } 260 387 261 // prepare the socket buffer 262 // count plus the terminating null (\0) 388 /* 389 * Prepare socket buffer. Allocate count fields plus the terminating 390 * null (\0). 391 */ 263 392 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 264 393 if (!socket_ids) { … … 271 400 printf("Starting tests\n"); 272 401 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"); 373 374 printf("Tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 402 rc = gettimeofday(&time_before, NULL); 403 if (rc != EOK) { 404 fprintf(stderr, "Get time of day error %d\n", rc); 405 return rc; 406 } 407 408 nettest1_test(socket_ids, 1, 1); 409 nettest1_test(socket_ids, 1, messages); 410 nettest1_test(socket_ids, sockets, 1); 411 nettest1_test(socket_ids, sockets, messages); 412 413 rc = gettimeofday(&time_after, NULL); 414 if (rc != EOK) { 415 fprintf(stderr, "Get time of day error %d\n", rc); 416 return rc; 417 } 418 419 printf("Tested in %d microseconds\n", tv_sub(&time_after, 420 &time_before)); 375 421 376 422 if (verbose) -
uspace/app/nettest2/nettest2.c
r1712f87 r3da12d74 44 44 #include <time.h> 45 45 #include <arg_parse.h> 46 #include < err.h>46 #include <bool.h> 47 47 48 48 #include <net/in.h> … … 58 58 #define NETTEST2_TEXT "Networking test 2 - transfer" 59 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 60 68 static void nettest2_print_help(void) 61 69 { 62 70 printf( 63 "Network Networking test 2 aplication - UDP transfer\n" \ 64 "Usage: echo [options] numeric_address\n" \ 65 "Where options are:\n" \ 66 "-f protocol_family | --family=protocol_family\n" \ 67 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n" 68 "\n" \ 69 "-h | --help\n" \ 70 "\tShow this application help.\n" 71 "\n" \ 72 "-m count | --messages=count\n" \ 73 "\tThe number of messages to send and receive per socket. The default is 10.\n" \ 74 "\n" \ 75 "-n sockets | --sockets=count\n" \ 76 "\tThe number of sockets to use. The default is 10.\n" \ 77 "\n" \ 78 "-p port_number | --port=port_number\n" \ 79 "\tThe port number the application should send messages to. The default is 7.\n" \ 80 "\n" \ 81 "-s packet_size | --size=packet_size\n" \ 82 "\tThe packet data size the application sends. The default is 29 bytes.\n" \ 83 "\n" \ 84 "-v | --verbose\n" \ 85 "\tShow all output messages.\n" 86 ); 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"); 87 98 } 88 99 89 /** Refreshes the data.100 /** Fill buffer with the NETTEST2_TEXT pattern. 90 101 * 91 * Fills the data block with the NETTEST1_TEXT pattern. 92 * 93 * @param[out] data The data block. 94 * @param[in] size The data block size in bytes. 95 */ 96 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) 97 106 { 98 107 size_t length; 99 108 100 // fill the data101 109 length = 0; 102 110 while (size > length + sizeof(NETTEST2_TEXT) - 1) { 103 memcpy(data + length, NETTEST2_TEXT, sizeof(NETTEST2_TEXT) - 1); 111 memcpy(buffer + length, NETTEST2_TEXT, 112 sizeof(NETTEST2_TEXT) - 1); 104 113 length += sizeof(NETTEST2_TEXT) - 1; 105 114 } 106 memcpy(data + length, NETTEST2_TEXT, size - length); 107 data[size] = '\0'; 115 116 memcpy(buffer + length, NETTEST2_TEXT, size - length); 117 buffer[size] = '\0'; 108 118 } 109 119 110 111 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) 112 127 { 113 ERROR_DECLARE;114 115 size_t size = 28;116 int verbose = 0;117 sock_type_t type = SOCK_DGRAM;118 int sockets = 10;119 int messages = 10;120 int family = PF_INET;121 uint16_t port = 7;122 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;129 uint8_t *address_start;130 131 int *socket_ids;132 char *data;133 128 int value; 134 int index; 135 struct timeval time_before; 136 struct timeval time_after; 137 138 // parse the command line arguments 139 // stop before the last argument if it does not start with the minus sign ('-') 140 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index) { 141 // options should start with the minus sign ('-') 142 if (argv[index][0] == '-') { 143 switch(argv[index][1]){ 144 // short options with only one letter 145 case 'f': 146 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 147 break; 148 case 'h': 149 nettest2_print_help(); 150 return EOK; 151 break; 152 case 'm': 153 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 0)); 154 break; 155 case 'n': 156 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 0)); 157 break; 158 case 'p': 159 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 160 port = (uint16_t) value; 161 break; 162 case 's': 163 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 164 size = (value >= 0) ? (size_t) value : 0; 165 break; 166 case 't': 167 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 168 type = (sock_type_t) value; 169 break; 170 case 'v': 171 verbose = 1; 172 break; 173 // long options with the double minus sign ('-') 174 case '-': 175 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)); 177 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 178 nettest2_print_help(); 179 return EOK; 180 } else if (str_lcmp(argv[index] + 2, "messages=", 6) == 0) { 181 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 8)); 182 } else if (str_lcmp(argv[index] + 2, "sockets=", 6) == 0) { 183 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 8)); 184 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 185 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 186 port = (uint16_t) value; 187 } 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)); 189 type = (sock_type_t) value; 190 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { 191 verbose = 1; 192 } else { 193 nettest2_print_help(); 194 return EINVAL; 195 } 196 break; 197 default: 198 nettest2_print_help(); 199 return EINVAL; 200 } 129 int rc; 130 131 switch (argv[*index][1]) { 132 /* 133 * Short options with only one letter 134 */ 135 case 'f': 136 rc = arg_parse_name_int(argc, argv, index, &family, 0, 137 socket_parse_protocol_family); 138 if (rc != EOK) 139 return rc; 140 break; 141 case 'h': 142 nettest2_print_help(); 143 return EOK; 144 break; 145 case 'm': 146 rc = arg_parse_int(argc, argv, index, &messages, 0); 147 if (rc != EOK) 148 return rc; 149 break; 150 case 'n': 151 rc = arg_parse_int(argc, argv, index, &sockets, 0); 152 if (rc != EOK) 153 return rc; 154 break; 155 case 'p': 156 rc = arg_parse_int(argc, argv, index, &value, 0); 157 if (rc != EOK) 158 return rc; 159 port = (uint16_t) value; 160 break; 161 case 's': 162 rc = arg_parse_int(argc, argv, index, &value, 0); 163 if (rc != EOK) 164 return rc; 165 size = (value >= 0) ? (size_t) value : 0; 166 break; 167 case 't': 168 rc = arg_parse_name_int(argc, argv, index, &value, 0, 169 socket_parse_socket_type); 170 if (rc != EOK) 171 return rc; 172 type = (sock_type_t) value; 173 break; 174 case 'v': 175 verbose = true; 176 break; 177 /* 178 * Long options with double dash ('-') 179 */ 180 case '-': 181 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { 182 rc = arg_parse_name_int(argc, argv, index, &family, 9, 183 socket_parse_protocol_family); 184 if (rc != EOK) 185 return rc; 186 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { 187 nettest2_print_help(); 188 return EOK; 189 } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) { 190 rc = arg_parse_int(argc, argv, index, &messages, 8); 191 if (rc != EOK) 192 return rc; 193 } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) { 194 rc = arg_parse_int(argc, argv, index, &sockets, 8); 195 if (rc != EOK) 196 return rc; 197 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) { 198 rc = arg_parse_int(argc, argv, index, &value, 7); 199 if (rc != EOK) 200 return rc; 201 port = (uint16_t) value; 202 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { 203 rc = arg_parse_name_int(argc, argv, index, &value, 7, 204 socket_parse_socket_type); 205 if (rc != EOK) 206 return rc; 207 type = (sock_type_t) value; 208 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { 209 verbose = 1; 201 210 } else { 202 211 nettest2_print_help(); 203 212 return EINVAL; 204 213 } 205 } 206 207 // if not before the last argument containing the address 214 break; 215 default: 216 nettest2_print_help(); 217 return EINVAL; 218 } 219 220 return EOK; 221 } 222 223 int main(int argc, char *argv[]) 224 { 225 socklen_t max_length; 226 uint8_t address_data[sizeof(struct sockaddr_in6)]; 227 struct sockaddr *address; 228 struct sockaddr_in *address_in; 229 struct sockaddr_in6 *address_in6; 230 socklen_t addrlen; 231 uint8_t *address_start; 232 233 int *socket_ids; 234 char *data; 235 int index; 236 struct timeval time_before; 237 struct timeval time_after; 238 239 int rc; 240 241 size = 28; 242 verbose = false; 243 type = SOCK_DGRAM; 244 sockets = 10; 245 messages = 10; 246 family = PF_INET; 247 port = 7; 248 249 max_length = sizeof(address_data); 250 address = (struct sockaddr *) address_data; 251 address_in = (struct sockaddr_in *) address; 252 address_in6 = (struct sockaddr_in6 *) address; 253 254 /* 255 * Parse the command line arguments. 256 * 257 * Stop before the last argument if it does not start with dash ('-') 258 */ 259 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 260 (argv[index][0] == '-')); ++index) { 261 262 /* Options should start with dash ('-') */ 263 if (argv[index][0] == '-') { 264 rc = nettest2_parse_opt(argc, argv, &index); 265 if (rc != EOK) 266 return rc; 267 } else { 268 nettest2_print_help(); 269 return EINVAL; 270 } 271 } 272 273 /* If not before the last argument containing the address */ 208 274 if (index >= argc) { 209 275 printf("Command line error: missing address\n"); … … 212 278 } 213 279 214 / / prepare the address buffer280 /* Prepare the address buffer */ 215 281 bzero(address_data, max_length); 282 216 283 switch (family) { 217 284 case PF_INET: … … 232 299 } 233 300 234 // 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; 238 } 239 240 // check the buffer size 301 /* Parse the last argument which should contain the address. */ 302 rc = inet_pton(family, argv[argc - 1], address_start); 303 if (rc != EOK) { 304 fprintf(stderr, "Address parse error %d\n", rc); 305 return rc; 306 } 307 308 /* Check data buffer size. */ 241 309 if (size <= 0) { 242 fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size); 310 fprintf(stderr, "Data buffer size too small (%d). Using 1024 " 311 "bytes instead.\n", size); 243 312 size = 1024; 244 313 } 245 314 246 // prepare the buffer 247 // size plus terminating null (\0) 315 /* 316 * Prepare the buffer. Allocate size bytes plus one for terminating 317 * null character. 318 */ 248 319 data = (char *) malloc(size + 1); 249 320 if (!data) { … … 251 322 return ENOMEM; 252 323 } 253 nettest2_refresh_data(data, size); 254 255 // check the socket count 324 325 /* Fill buffer with a pattern. */ 326 nettest2_fill_buffer(data, size); 327 328 /* Check socket count. */ 256 329 if (sockets <= 0) { 257 fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets); 330 fprintf(stderr, "Socket count too small (%d). Using " 331 "2 instead.\n", sockets); 258 332 sockets = 2; 259 333 } 260 334 261 // prepare the socket buffer 262 // count plus the terminating null (\0) 335 /* 336 * Prepare the socket buffer. 337 * Allocate count entries plus the terminating null (\0) 338 */ 263 339 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 264 340 if (!socket_ids) { … … 271 347 printf("Starting tests\n"); 272 348 273 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 274 275 if (type == SOCK_STREAM) 276 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 349 rc = sockets_create(verbose, socket_ids, sockets, family, type); 350 if (rc != EOK) 351 return rc; 352 353 if (type == SOCK_STREAM) { 354 rc = sockets_connect(verbose, socket_ids, sockets, 355 address, addrlen); 356 if (rc != EOK) 357 return rc; 358 } 277 359 278 360 if (verbose) 279 361 printf("\n"); 280 362 281 if (ERROR_OCCURRED(gettimeofday(&time_before, NULL))) { 282 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 283 return ERROR_CODE; 284 } 285 286 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 287 288 if (ERROR_OCCURRED(gettimeofday(&time_after, NULL))) { 289 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 290 return ERROR_CODE; 363 rc = gettimeofday(&time_before, NULL); 364 if (rc != EOK) { 365 fprintf(stderr, "Get time of day error %d\n", rc); 366 return rc; 367 } 368 369 rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, 370 &addrlen, data, size, messages); 371 if (rc != EOK) 372 return rc; 373 374 rc = gettimeofday(&time_after, NULL); 375 if (rc != EOK) { 376 fprintf(stderr, "Get time of day error %d\n", rc); 377 return rc; 291 378 } 292 379 … … 294 381 printf("\tOK\n"); 295 382 296 printf("sendto + recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 297 298 if (ERROR_OCCURRED(gettimeofday(&time_before, NULL))) { 299 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 300 return ERROR_CODE; 301 } 302 303 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages)); 304 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 305 306 if (ERROR_OCCURRED(gettimeofday(&time_after, NULL))) { 307 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 308 return ERROR_CODE; 383 printf("sendto + recvfrom tested in %d microseconds\n", 384 tv_sub(&time_after, &time_before)); 385 386 rc = gettimeofday(&time_before, NULL); 387 if (rc != EOK) { 388 fprintf(stderr, "Get time of day error %d\n", rc); 389 return rc; 390 } 391 392 rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen, 393 data, size, messages); 394 if (rc != EOK) 395 return rc; 396 397 rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, 398 data, size, messages); 399 if (rc != EOK) 400 return rc; 401 402 rc = gettimeofday(&time_after, NULL); 403 if (rc != EOK) { 404 fprintf(stderr, "Get time of day error %d\n", rc); 405 return rc; 309 406 } 310 407 … … 312 409 printf("\tOK\n"); 313 410 314 printf("sendto, recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 315 316 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 411 printf("sendto, recvfrom tested in %d microseconds\n", 412 tv_sub(&time_after, &time_before)); 413 414 rc = sockets_close(verbose, socket_ids, sockets); 415 if (rc != EOK) 416 return rc; 317 417 318 418 if (verbose)
Note:
See TracChangeset
for help on using the changeset viewer.