Changeset b40bfac in mainline for uspace/app
- Timestamp:
- 2010-11-08T07:13:25Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 63a1e60
- Parents:
- d70a463 (diff), 3da12d74 (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:
-
- 12 edited
- 4 moved
-
bdsh/exec.c (modified) (2 diffs)
-
getterm/getterm.c (modified) (2 diffs)
-
init/init.c (modified) (7 diffs)
-
netecho/netecho.c (modified) (11 diffs)
-
netecho/print_error.c (modified) (2 diffs)
-
netecho/print_error.h (modified) (1 diff)
-
netstart/Makefile (moved) (moved from uspace/srv/net/netstart/Makefile ) (2 diffs)
-
netstart/netstart.c (moved) (moved from uspace/srv/net/netstart/netstart.c ) (5 diffs)
-
netstart/self_test.c (moved) (moved from uspace/srv/net/netstart/self_test.c )
-
netstart/self_test.h (moved) (moved from uspace/srv/net/netstart/self_test.h )
-
nettest1/nettest.c (modified) (4 diffs)
-
nettest1/nettest.h (modified) (2 diffs)
-
nettest1/nettest1.c (modified) (6 diffs)
-
nettest2/nettest2.c (modified) (6 diffs)
-
redir/redir.c (modified) (3 diffs)
-
sbi/src/os/helenos.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/exec.c
rd70a463 rb40bfac 41 41 #include <fcntl.h> 42 42 #include <str_error.h> 43 #include <errno.h> 43 44 44 45 #include "config.h" … … 116 117 task_exit_t texit; 117 118 char *tmp; 118 int r etval;119 int rc, retval; 119 120 120 121 tmp = str_dup(find_command(cmd)); 121 122 free(found); 122 123 123 tid = task_spawn(tmp, (const char **) argv, &retval);124 rc = task_spawnv(&tid, tmp, (const char **) argv); 124 125 free(tmp); 125 126 126 if ( tid == 0) {127 if (rc != 0) { 127 128 cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd, 128 str_error(r etval));129 str_error(rc)); 129 130 return 1; 130 131 } 131 132 132 task_wait(tid, &texit, &retval); 133 if (texit != TASK_EXIT_NORMAL) { 133 rc = task_wait(tid, &texit, &retval); 134 if (rc != EOK) { 135 printf("%s: Failed waiting for command (%s)\n", str_error(rc)); 136 } else if (texit != TASK_EXIT_NORMAL) { 134 137 printf("%s: Command failed (unexpectedly terminated)\n", progname); 135 138 } else if (retval != 0) { 136 printf("%s: Command failed ( %s)\n",137 progname, str_error(retval));139 printf("%s: Command failed (exit code %d)\n", 140 progname, retval); 138 141 } 139 142 -
uspace/app/getterm/getterm.c
rd70a463 rb40bfac 41 41 #include <task.h> 42 42 #include <str_error.h> 43 #include <errno.h> 43 44 #include "version.h" 44 45 … … 74 75 static task_id_t spawn(const char *fname) 75 76 { 76 const char *args[2]; 77 task_id_t id; 78 int rc; 77 79 78 args[0] = fname; 79 args[1] = NULL; 80 81 int err; 82 task_id_t id = task_spawn(fname, args, &err); 83 84 if (id == 0) 80 rc = task_spawnl(&id, fname, fname, NULL); 81 if (rc != EOK) { 85 82 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname, 86 str_error(err)); 83 str_error(rc)); 84 return 0; 85 } 87 86 88 87 return id; -
uspace/app/init/init.c
rd70a463 rb40bfac 124 124 static void spawn(const char *fname) 125 125 { 126 const char *argv[2];126 int rc; 127 127 struct stat s; 128 128 … … 131 131 132 132 printf("%s: Spawning %s\n", NAME, fname); 133 134 argv[0] = fname; 135 argv[1] = NULL; 136 137 int err; 138 if (!task_spawn(fname, argv, &err)) 133 rc = task_spawnl(NULL, fname, fname, NULL); 134 if (rc != EOK) { 139 135 printf("%s: Error spawning %s (%s)\n", NAME, fname, 140 str_error(err)); 136 str_error(rc)); 137 } 141 138 } 142 139 143 140 static void srv_start(const char *fname) 144 141 { 145 const char *argv[2];146 142 task_id_t id; 147 143 task_exit_t texit; … … 153 149 154 150 printf("%s: Starting %s\n", NAME, fname); 155 156 argv[0] = fname; 157 argv[1] = NULL; 158 159 id = task_spawn(fname, argv, &retval); 151 rc = task_spawnl(&id, fname, fname, NULL); 160 152 if (!id) { 161 153 printf("%s: Error spawning %s (%s)\n", NAME, fname, 162 str_error(r etval));154 str_error(rc)); 163 155 return; 164 156 } … … 167 159 if (rc != EOK) { 168 160 printf("%s: Error waiting for %s (%s(\n", NAME, fname, 169 str_error(retval)); 170 return; 171 } 172 173 if ((texit != TASK_EXIT_NORMAL) || (retval != 0)) { 174 printf("%s: Server %s failed to start (%s)\n", NAME, 175 fname, str_error(retval)); 161 str_error(rc)); 162 return; 163 } 164 165 if (texit != TASK_EXIT_NORMAL) { 166 printf("%s: Server %s failed to start (unexpectedly " 167 "terminated)\n", NAME, fname); 168 return; 169 } 170 171 if (retval != 0) { 172 printf("%s: Server %s failed to start (exit code %d)\n", NAME, 173 fname, retval); 176 174 } 177 175 } … … 179 177 static void console(const char *dev) 180 178 { 181 const char *argv[3];182 179 char hid_in[DEVMAP_NAME_MAXLEN]; 183 180 int rc; … … 190 187 dev_handle_t handle; 191 188 rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING); 192 193 if (rc == EOK) { 194 argv[0] = SRV_CONSOLE; 195 argv[1] = hid_in; 196 argv[2] = NULL; 197 198 if (!task_spawn(SRV_CONSOLE, argv, &rc)) 199 printf("%s: Error spawning %s %s (%s)\n", NAME, SRV_CONSOLE, 200 hid_in, str_error(rc)); 201 } else 189 if (rc != EOK) { 202 190 printf("%s: Error waiting on %s (%s)\n", NAME, hid_in, 203 191 str_error(rc)); 192 return; 193 } 194 195 rc = task_spawnl(NULL, SRV_CONSOLE, SRV_CONSOLE, hid_in, NULL); 196 if (rc != EOK) { 197 printf("%s: Error spawning %s %s (%s)\n", NAME, SRV_CONSOLE, 198 hid_in, str_error(rc)); 199 } 204 200 } 205 201 206 202 static void getterm(const char *dev, const char *app) 207 203 { 208 const char *argv[4];209 204 char term[DEVMAP_NAME_MAXLEN]; 210 205 int rc; … … 217 212 dev_handle_t handle; 218 213 rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING); 219 220 if (rc == EOK) { 221 argv[0] = APP_GETTERM; 222 argv[1] = term; 223 argv[2] = app; 224 argv[3] = NULL; 225 226 if (!task_spawn(APP_GETTERM, argv, &rc)) 227 printf("%s: Error spawning %s %s %s (%s)\n", NAME, APP_GETTERM, 228 term, app, str_error(rc)); 229 } else 214 if (rc != EOK) { 230 215 printf("%s: Error waiting on %s (%s)\n", NAME, term, 231 216 str_error(rc)); 217 return; 218 } 219 220 rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, term, app, NULL); 221 if (rc != EOK) { 222 printf("%s: Error spawning %s %s %s (%s)\n", NAME, 223 APP_GETTERM, term, app, str_error(rc)); 224 } 232 225 } 233 226 -
uspace/app/netecho/netecho.c
rd70a463 rb40bfac 28 28 29 29 /** @addtogroup netecho 30 * @{30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Network echo application.35 * Answers received packets.34 * Network echo application. 35 * Answers received packets. 36 36 */ 37 37 … … 41 41 #include <task.h> 42 42 #include <arg_parse.h> 43 #include <err.h>44 43 45 44 #include <net/in.h> … … 51 50 #include "print_error.h" 52 51 53 /** Network echo module name. 54 */ 52 /** Network echo module name. */ 55 53 #define NAME "Network Echo" 56 54 57 /** Prints the application help. 58 */ 59 void echo_print_help(void); 60 61 /** Module entry point. 62 * Reads command line parameters and starts listenning. 63 * @param[in] argc The number of command line parameters. 64 * @param[in] argv The command line parameters. 65 * @returns EOK on success. 66 */ 67 int main(int argc, char * argv[]); 68 69 void echo_print_help(void){ 55 static void echo_print_help(void) 56 { 70 57 printf( 71 58 "Network Echo aplication\n" \ … … 101 88 } 102 89 103 int main(int argc, char * argv[]){ 104 ERROR_DECLARE; 105 106 size_t size = 1024; 107 int verbose = 0; 108 char * reply = NULL; 109 sock_type_t type = SOCK_DGRAM; 110 int count = -1; 111 int family = PF_INET; 112 uint16_t port = 7; 113 int backlog = 3; 114 115 socklen_t max_length = sizeof(struct sockaddr_in6); 90 int main(int argc, char *argv[]) 91 { 92 size_t size = 1024; 93 int verbose = 0; 94 char *reply = NULL; 95 sock_type_t type = SOCK_DGRAM; 96 int count = -1; 97 int family = PF_INET; 98 uint16_t port = 7; 99 int backlog = 3; 100 101 socklen_t max_length = sizeof(struct sockaddr_in6); 116 102 uint8_t address_data[max_length]; 117 struct sockaddr * address= (struct sockaddr *) address_data;118 struct sockaddr_in * address_in= (struct sockaddr_in *) address;119 struct sockaddr_in6 * address_in6= (struct sockaddr_in6 *) address;103 struct sockaddr *address = (struct sockaddr *) address_data; 104 struct sockaddr_in *address_in = (struct sockaddr_in *) address; 105 struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address; 120 106 socklen_t addrlen; 121 107 char address_string[INET6_ADDRSTRLEN]; 122 uint8_t * address_start;108 uint8_t *address_start; 123 109 int socket_id; 124 110 int listening_id; 125 char * data;111 char *data; 126 112 size_t length; 127 113 int index; 128 114 size_t reply_length; 129 115 int value; 116 int rc; 130 117 131 118 // parse the command line arguments 132 for(index = 1; index < argc; ++ index){ 133 if(argv[index][0] == '-'){ 134 switch(argv[index][1]){ 135 case 'b': 136 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 0)); 137 break; 138 case 'c': 139 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 0)); 140 break; 141 case 'f': 142 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 143 break; 144 case 'h': 119 for (index = 1; index < argc; ++ index) { 120 if (argv[index][0] == '-') { 121 switch (argv[index][1]) { 122 case 'b': 123 rc = arg_parse_int(argc, argv, &index, &backlog, 0); 124 if (rc != EOK) 125 return rc; 126 break; 127 case 'c': 128 rc = arg_parse_int(argc, argv, &index, &count, 0); 129 if (rc != EOK) 130 return rc; 131 break; 132 case 'f': 133 rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family); 134 if (rc != EOK) 135 return rc; 136 break; 137 case 'h': 138 echo_print_help(); 139 return EOK; 140 break; 141 case 'p': 142 rc = arg_parse_int(argc, argv, &index, &value, 0); 143 if (rc != EOK) 144 return rc; 145 port = (uint16_t) value; 146 break; 147 case 'r': 148 rc = arg_parse_string(argc, argv, &index, &reply, 0); 149 if (rc != EOK) 150 return rc; 151 break; 152 case 's': 153 rc = arg_parse_int(argc, argv, &index, &value, 0); 154 if (rc != EOK) 155 return rc; 156 size = (value >= 0) ? (size_t) value : 0; 157 break; 158 case 't': 159 rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type); 160 if (rc != EOK) 161 return rc; 162 type = (sock_type_t) value; 163 break; 164 case 'v': 165 verbose = 1; 166 break; 167 // long options with the double minus sign ('-') 168 case '-': 169 if (str_lcmp(argv[index] + 2, "backlog=", 6) == 0) { 170 rc = arg_parse_int(argc, argv, &index, &backlog, 8); 171 if (rc != EOK) 172 return rc; 173 } else if (str_lcmp(argv[index] + 2, "count=", 6) == 0) { 174 rc = arg_parse_int(argc, argv, &index, &count, 8); 175 } else if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 176 rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family); 177 if (rc != EOK) 178 return rc; 179 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 145 180 echo_print_help(); 146 181 return EOK; 147 break; 148 case 'p': 149 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 182 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 183 rc = arg_parse_int(argc, argv, &index, &value, 7); 184 if (rc != EOK) 185 return rc; 150 186 port = (uint16_t) value; 151 break; 152 case 'r': 153 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 0)); 154 break; 155 case 's': 156 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 187 } else if (str_lcmp(argv[index] + 2, "reply=", 6) == 0) { 188 rc = arg_parse_string(argc, argv, &index, &reply, 8); 189 if (rc != EOK) 190 return rc; 191 } else if (str_lcmp(argv[index] + 2, "size=", 5) == 0) { 192 rc = arg_parse_int(argc, argv, &index, &value, 7); 193 if (rc != EOK) 194 return rc; 157 195 size = (value >= 0) ? (size_t) value : 0; 158 break; 159 case 't': 160 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 196 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 197 rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type); 198 if (rc != EOK) 199 return rc; 161 200 type = (sock_type_t) value; 162 break; 163 case 'v': 201 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { 164 202 verbose = 1; 165 break; 166 // long options with the double minus sign ('-') 167 case '-': 168 if(str_lcmp(argv[index] + 2, "backlog=", 6) == 0){ 169 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 8)); 170 }else if(str_lcmp(argv[index] + 2, "count=", 6) == 0){ 171 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 8)); 172 }else if(str_lcmp(argv[index] + 2, "family=", 7) == 0){ 173 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 174 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){ 175 echo_print_help(); 176 return EOK; 177 }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){ 178 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 179 port = (uint16_t) value; 180 }else if(str_lcmp(argv[index] + 2, "reply=", 6) == 0){ 181 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 8)); 182 }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){ 183 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 184 size = (value >= 0) ? (size_t) value : 0; 185 }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){ 186 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 187 type = (sock_type_t) value; 188 }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){ 189 verbose = 1; 190 }else{ 191 echo_print_help(); 192 return EINVAL; 193 } 194 break; 195 default: 203 } else { 196 204 echo_print_help(); 197 205 return EINVAL; 206 } 207 break; 208 default: 209 echo_print_help(); 210 return EINVAL; 198 211 } 199 } else{212 } else { 200 213 echo_print_help(); 201 214 return EINVAL; … … 204 217 205 218 // check the buffer size 206 if (size <= 0){219 if (size <= 0) { 207 220 fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size); 208 221 size = 1024; … … 210 223 // size plus the terminating null (\0) 211 224 data = (char *) malloc(size + 1); 212 if (! data){225 if (!data) { 213 226 fprintf(stderr, "Failed to allocate receive buffer.\n"); 214 227 return ENOMEM; … … 220 233 // prepare the address buffer 221 234 bzero(address_data, max_length); 222 switch (family){223 case PF_INET:224 address_in->sin_family = AF_INET;225 address_in->sin_port = htons(port);226 addrlen = sizeof(struct sockaddr_in);227 break;228 case PF_INET6:229 address_in6->sin6_family = AF_INET6;230 address_in6->sin6_port = htons(port);231 addrlen = sizeof(struct sockaddr_in6);232 break;233 default:234 fprintf(stderr, "Protocol family is not supported\n");235 return EAFNOSUPPORT;235 switch (family) { 236 case PF_INET: 237 address_in->sin_family = AF_INET; 238 address_in->sin_port = htons(port); 239 addrlen = sizeof(struct sockaddr_in); 240 break; 241 case PF_INET6: 242 address_in6->sin6_family = AF_INET6; 243 address_in6->sin6_port = htons(port); 244 addrlen = sizeof(struct sockaddr_in6); 245 break; 246 default: 247 fprintf(stderr, "Protocol family is not supported\n"); 248 return EAFNOSUPPORT; 236 249 } 237 250 238 251 // get a listening socket 239 252 listening_id = socket(family, type, 0); 240 if (listening_id < 0){253 if (listening_id < 0) { 241 254 socket_print_error(stderr, listening_id, "Socket create: ", "\n"); 242 255 return listening_id; … … 244 257 245 258 // if the stream socket is used 246 if (type == SOCK_STREAM){259 if (type == SOCK_STREAM) { 247 260 // check the backlog 248 if (backlog <= 0){261 if (backlog <= 0) { 249 262 fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size); 250 263 backlog = 3; 251 264 } 252 265 // set the backlog 253 if(ERROR_OCCURRED(listen(listening_id, backlog))){ 254 socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n"); 255 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; 256 270 } 257 271 } 258 272 259 273 // bind the listenning socket 260 if(ERROR_OCCURRED(bind(listening_id, address, addrlen))){ 261 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n"); 262 return ERROR_CODE; 263 } 264 265 if(verbose){ 274 rc = bind(listening_id, address, addrlen); 275 if (rc != EOK) { 276 socket_print_error(stderr, rc, "Socket bind: ", "\n"); 277 return rc; 278 } 279 280 if (verbose) 266 281 printf("Socket %d listenning at %d\n", listening_id, port); 267 }268 282 269 283 socket_id = listening_id; … … 271 285 // do count times 272 286 // or indefinitely if set to a negative value 273 while (count){287 while (count) { 274 288 275 289 addrlen = max_length; 276 if (type == SOCK_STREAM){290 if (type == SOCK_STREAM) { 277 291 // acceept a socket if the stream socket is used 278 292 socket_id = accept(listening_id, address, &addrlen); 279 if (socket_id <= 0){293 if (socket_id <= 0) { 280 294 socket_print_error(stderr, socket_id, "Socket accept: ", "\n"); 281 } else{282 if (verbose){295 } else { 296 if (verbose) 283 297 printf("Socket %d accepted\n", socket_id); 284 }285 298 } 286 299 } 287 300 288 301 // if the datagram socket is used or the stream socked was accepted 289 if (socket_id > 0){302 if (socket_id > 0) { 290 303 291 304 // receive an echo request 292 305 value = recvfrom(socket_id, data, size, 0, address, &addrlen); 293 if (value < 0){306 if (value < 0) { 294 307 socket_print_error(stderr, value, "Socket receive: ", "\n"); 295 } else{308 } else { 296 309 length = (size_t) value; 297 if (verbose){310 if (verbose) { 298 311 // print the header 299 312 300 313 // get the source port and prepare the address buffer 301 314 address_start = NULL; 302 switch (address->sa_family){303 case AF_INET:304 port = ntohs(address_in->sin_port);305 address_start = (uint8_t *) &address_in->sin_addr.s_addr;306 break;307 case AF_INET6:308 port = ntohs(address_in6->sin6_port);309 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;310 break;311 default:312 fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);315 switch (address->sa_family) { 316 case AF_INET: 317 port = ntohs(address_in->sin_port); 318 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 319 break; 320 case AF_INET6: 321 port = ntohs(address_in6->sin6_port); 322 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 323 break; 324 default: 325 fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family); 313 326 } 314 327 // parse the source address 315 if(address_start){ 316 if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){ 317 fprintf(stderr, "Received address error %d\n", ERROR_CODE); 318 }else{ 328 if (address_start) { 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); 332 } else { 319 333 data[length] = '\0'; 320 334 printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data); … … 324 338 325 339 // answer the request either with the static reply or the original data 326 if(ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))){ 327 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 328 } 329 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"); 330 343 } 331 344 332 345 // close the accepted stream socket 333 if (type == SOCK_STREAM){334 if(ERROR_OCCURRED(closesocket(socket_id))){335 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");336 }346 if (type == SOCK_STREAM) { 347 rc = closesocket(socket_id); 348 if (rc != EOK) 349 socket_print_error(stderr, rc, "Close socket: ", "\n"); 337 350 } 338 351 … … 340 353 341 354 // decrease the count if positive 342 if (count > 0){343 -- count;344 if (verbose){355 if (count > 0) { 356 count--; 357 if (verbose) 345 358 printf("Waiting for next %d packet(s)\n", count); 346 } 347 } 348 } 349 350 if(verbose){ 359 } 360 } 361 362 if (verbose) 351 363 printf("Closing the socket\n"); 352 }353 364 354 365 // close the listenning socket 355 if(ERROR_OCCURRED(closesocket(listening_id))){ 356 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n"); 357 return ERROR_CODE; 358 } 359 360 if(verbose){ 366 rc = closesocket(listening_id); 367 if (rc != EOK) { 368 socket_print_error(stderr, rc, "Close socket: ", "\n"); 369 return rc; 370 } 371 372 if (verbose) 361 373 printf("Exiting\n"); 362 }363 374 364 375 return EOK; -
uspace/app/netecho/print_error.c
rd70a463 rb40bfac 28 28 29 29 /** @addtogroup net_app 30 * @{30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Generic application error printing functions implementation.34 * Generic application error printing functions implementation. 35 35 */ 36 37 #include "print_error.h" 36 38 37 39 #include <stdio.h> … … 40 42 #include <net/icmp_codes.h> 41 43 42 #include "print_error.h" 44 /** Prints the specific ICMP error description. 45 * 46 * @param[in] output The description output stream. May be NULL. 47 * @param[in] error_code The ICMP error code. 48 * @param[in] prefix The error description prefix. May be NULL. 49 * @param[in] suffix The error description suffix. May be NULL. 50 */ 51 void icmp_print_error(FILE *output, int error_code, const char *prefix, const char *suffix) 52 { 53 if (!output) 54 return; 55 56 if (prefix) 57 fprintf(output, "%s", prefix); 58 59 switch (error_code) { 60 case ICMP_DEST_UNREACH: 61 fprintf(output, "ICMP Destination Unreachable (%d) error", error_code); 62 break; 63 case ICMP_SOURCE_QUENCH: 64 fprintf(output, "ICMP Source Quench (%d) error", error_code); 65 break; 66 case ICMP_REDIRECT: 67 fprintf(output, "ICMP Redirect (%d) error", error_code); 68 break; 69 case ICMP_ALTERNATE_ADDR: 70 fprintf(output, "ICMP Alternate Host Address (%d) error", error_code); 71 break; 72 case ICMP_ROUTER_ADV: 73 fprintf(output, "ICMP Router Advertisement (%d) error", error_code); 74 break; 75 case ICMP_ROUTER_SOL: 76 fprintf(output, "ICMP Router Solicitation (%d) error", error_code); 77 break; 78 case ICMP_TIME_EXCEEDED: 79 fprintf(output, "ICMP Time Exceeded (%d) error", error_code); 80 break; 81 case ICMP_PARAMETERPROB: 82 fprintf(output, "ICMP Paramenter Problem (%d) error", error_code); 83 break; 84 case ICMP_CONVERSION_ERROR: 85 fprintf(output, "ICMP Datagram Conversion Error (%d) error", error_code); 86 break; 87 case ICMP_REDIRECT_MOBILE: 88 fprintf(output, "ICMP Mobile Host Redirect (%d) error", error_code); 89 break; 90 case ICMP_SKIP: 91 fprintf(output, "ICMP SKIP (%d) error", error_code); 92 break; 93 case ICMP_PHOTURIS: 94 fprintf(output, "ICMP Photuris (%d) error", error_code); 95 break; 96 default: 97 fprintf(output, "Other (%d) error", error_code); 98 } 43 99 44 void icmp_print_error(FILE * output, int error_code, const char * prefix, const char * suffix){ 45 if(output){ 46 if(prefix){ 47 fprintf(output, "%s", prefix); 48 } 49 switch(error_code){ 50 case ICMP_DEST_UNREACH: 51 fprintf(output, "ICMP Destination Unreachable (%d) error", error_code); 52 break; 53 case ICMP_SOURCE_QUENCH: 54 fprintf(output, "ICMP Source Quench (%d) error", error_code); 55 break; 56 case ICMP_REDIRECT: 57 fprintf(output, "ICMP Redirect (%d) error", error_code); 58 break; 59 case ICMP_ALTERNATE_ADDR: 60 fprintf(output, "ICMP Alternate Host Address (%d) error", error_code); 61 break; 62 case ICMP_ROUTER_ADV: 63 fprintf(output, "ICMP Router Advertisement (%d) error", error_code); 64 break; 65 case ICMP_ROUTER_SOL: 66 fprintf(output, "ICMP Router Solicitation (%d) error", error_code); 67 break; 68 case ICMP_TIME_EXCEEDED: 69 fprintf(output, "ICMP Time Exceeded (%d) error", error_code); 70 break; 71 case ICMP_PARAMETERPROB: 72 fprintf(output, "ICMP Paramenter Problem (%d) error", error_code); 73 break; 74 case ICMP_CONVERSION_ERROR: 75 fprintf(output, "ICMP Datagram Conversion Error (%d) error", error_code); 76 break; 77 case ICMP_REDIRECT_MOBILE: 78 fprintf(output, "ICMP Mobile Host Redirect (%d) error", error_code); 79 break; 80 case ICMP_SKIP: 81 fprintf(output, "ICMP SKIP (%d) error", error_code); 82 break; 83 case ICMP_PHOTURIS: 84 fprintf(output, "ICMP Photuris (%d) error", error_code); 85 break; 86 default: 87 fprintf(output, "Other (%d) error", error_code); 88 } 89 if(suffix){ 90 fprintf(output, "%s", suffix); 91 } 92 } 100 if (suffix) 101 fprintf(output, "%s", suffix); 93 102 } 94 103 95 void print_error(FILE * output, int error_code, const char * prefix, const char * suffix){ 96 if(IS_ICMP_ERROR(error_code)){ 104 /** Prints the error description. 105 * 106 * Supports ICMP and socket error codes. 107 * 108 * @param[in] output The description output stream. May be NULL. 109 * @param[in] error_code The error code. 110 * @param[in] prefix The error description prefix. May be NULL. 111 * @param[in] suffix The error description suffix. May be NULL. 112 */ 113 void print_error(FILE *output, int error_code, const char *prefix, const char *suffix) 114 { 115 if (IS_ICMP_ERROR(error_code)) 97 116 icmp_print_error(output, error_code, prefix, suffix); 98 }else if(IS_SOCKET_ERROR(error_code)){117 else if(IS_SOCKET_ERROR(error_code)) 99 118 socket_print_error(output, error_code, prefix, suffix); 100 }101 119 } 102 120 103 void socket_print_error(FILE * output, int error_code, const char * prefix, const char * suffix){ 104 if(output){ 105 if(prefix){ 106 fprintf(output, "%s", prefix); 107 } 108 switch(error_code){ 109 case ENOTSOCK: 110 fprintf(output, "Not a socket (%d) error", error_code); 111 break; 112 case EPROTONOSUPPORT: 113 fprintf(output, "Protocol not supported (%d) error", error_code); 114 break; 115 case ESOCKTNOSUPPORT: 116 fprintf(output, "Socket type not supported (%d) error", error_code); 117 break; 118 case EPFNOSUPPORT: 119 fprintf(output, "Protocol family not supported (%d) error", error_code); 120 break; 121 case EAFNOSUPPORT: 122 fprintf(output, "Address family not supported (%d) error", error_code); 123 break; 124 case EADDRINUSE: 125 fprintf(output, "Address already in use (%d) error", error_code); 126 break; 127 case ENOTCONN: 128 fprintf(output, "Socket not connected (%d) error", error_code); 129 break; 130 case NO_DATA: 131 fprintf(output, "No data (%d) error", error_code); 132 break; 133 case EINPROGRESS: 134 fprintf(output, "Another operation in progress (%d) error", error_code); 135 break; 136 case EDESTADDRREQ: 137 fprintf(output, "Destination address required (%d) error", error_code); 138 case TRY_AGAIN: 139 fprintf(output, "Try again (%d) error", error_code); 140 default: 141 fprintf(output, "Other (%d) error", error_code); 142 } 143 if(suffix){ 144 fprintf(output, "%s", suffix); 145 } 121 /** Prints the specific socket error description. 122 * 123 * @param[in] output The description output stream. May be NULL. 124 * @param[in] error_code The socket error code. 125 * @param[in] prefix The error description prefix. May be NULL. 126 * @param[in] suffix The error description suffix. May be NULL. 127 */ 128 void socket_print_error(FILE *output, int error_code, const char *prefix, const char *suffix) 129 { 130 if (!output) 131 return; 132 133 if (prefix) 134 fprintf(output, "%s", prefix); 135 136 switch (error_code) { 137 case ENOTSOCK: 138 fprintf(output, "Not a socket (%d) error", error_code); 139 break; 140 case EPROTONOSUPPORT: 141 fprintf(output, "Protocol not supported (%d) error", error_code); 142 break; 143 case ESOCKTNOSUPPORT: 144 fprintf(output, "Socket type not supported (%d) error", error_code); 145 break; 146 case EPFNOSUPPORT: 147 fprintf(output, "Protocol family not supported (%d) error", error_code); 148 break; 149 case EAFNOSUPPORT: 150 fprintf(output, "Address family not supported (%d) error", error_code); 151 break; 152 case EADDRINUSE: 153 fprintf(output, "Address already in use (%d) error", error_code); 154 break; 155 case ENOTCONN: 156 fprintf(output, "Socket not connected (%d) error", error_code); 157 break; 158 case NO_DATA: 159 fprintf(output, "No data (%d) error", error_code); 160 break; 161 case EINPROGRESS: 162 fprintf(output, "Another operation in progress (%d) error", error_code); 163 break; 164 case EDESTADDRREQ: 165 fprintf(output, "Destination address required (%d) error", error_code); 166 case TRY_AGAIN: 167 fprintf(output, "Try again (%d) error", error_code); 168 default: 169 fprintf(output, "Other (%d) error", error_code); 146 170 } 171 172 if (suffix) 173 fprintf(output, "%s", suffix); 147 174 } 148 175 149 176 /** @} 150 177 */ 178 -
uspace/app/netecho/print_error.h
rd70a463 rb40bfac 28 28 29 29 /** @addtogroup net_app 30 * @{30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Generic application error printing functions.34 * Generic application error printing functions. 35 35 */ 36 36 37 #ifndef __NET_APP_PRINT__ 38 #define __NET_APP_PRINT__ 37 #ifndef NET_APP_PRINT_ 38 #define NET_APP_PRINT_ 39 40 #include <stdio.h> 39 41 40 42 /** Returns whether the error code may be an ICMP error code. 41 * @param[in] error_code The error code. 42 * @returns A value indicating whether the error code may be an ICMP error code. 43 * 44 * @param[in] error_code The error code. 45 * @returns A value indicating whether the error code may be an ICMP error code. 43 46 */ 44 #define IS_ICMP_ERROR(error_code) ((error_code) > 0)47 #define IS_ICMP_ERROR(error_code) ((error_code) > 0) 45 48 46 49 /** Returns whether the error code may be socket error code. 47 * @param[in] error_code The error code. 48 * @returns A value indicating whether the error code may be a socket error code. 50 * 51 * @param[in] error_code The error code. 52 * @returns A value indicating whether the error code may be a socket error code. 49 53 */ 50 54 #define IS_SOCKET_ERROR(error_code) ((error_code) < 0) 51 55 52 /** Prints the specific ICMP error description. 53 * @param[in] output The description output stream. May be NULL. 54 * @param[in] error_code The ICMP error code. 55 * @param[in] prefix The error description prefix. May be NULL. 56 * @param[in] suffix The error description suffix. May be NULL. 57 */ 58 extern void icmp_print_error(FILE * output, int error_code, const char * prefix, const char * suffix); 59 60 /** Prints the error description. 61 * Supports ICMP and socket error codes. 62 * @param[in] output The description output stream. May be NULL. 63 * @param[in] error_code The error code. 64 * @param[in] prefix The error description prefix. May be NULL. 65 * @param[in] suffix The error description suffix. May be NULL. 66 */ 67 extern void print_error(FILE * output, int error_code, const char * prefix, const char * suffix); 68 69 /** Prints the specific socket error description. 70 * @param[in] output The description output stream. May be NULL. 71 * @param[in] error_code The socket error code. 72 * @param[in] prefix The error description prefix. May be NULL. 73 * @param[in] suffix The error description suffix. May be NULL. 74 */ 75 extern void socket_print_error(FILE * output, int error_code, const char * prefix, const char * suffix); 56 extern void icmp_print_error(FILE *, int, const char *, const char *); 57 extern void print_error(FILE *, int, const char *, const char *); 58 extern void socket_print_error(FILE *, int, const char *, const char *); 76 59 77 60 #endif -
uspace/app/netstart/Makefile
rd70a463 rb40bfac 28 28 # 29 29 30 USPACE_PREFIX = ../.. /..30 USPACE_PREFIX = ../.. 31 31 LIBS = $(LIBNET_PREFIX)/libnet.a 32 32 EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include … … 35 35 36 36 SOURCES = \ 37 netstart.c \ 38 self_test.c 37 netstart.c 39 38 40 39 include $(USPACE_PREFIX)/Makefile.common -
uspace/app/netstart/netstart.c
rd70a463 rb40bfac 32 32 33 33 /** @file 34 *35 34 * Start the networking subsystem. 36 * Perform networking self-test if executed37 * with the -s argument.38 *39 35 */ 40 36 … … 45 41 #include <task.h> 46 42 #include <str_error.h> 47 #include <err.h>48 43 #include <ipc/ipc.h> 49 44 #include <ipc/services.h> … … 52 47 #include <net/modules.h> 53 48 54 #include "self_test.h"55 56 49 /** Start a module. 57 50 * 58 * @param[in] desc The module description 59 * @param[in] path The module absolute path. 60 * 61 * @returns true on succesful spanwning 62 * @returns false on failure 63 * 51 * @param[in] desc The module description 52 * @param[in] path The module absolute path. 53 * @returns True on succesful spanwning. 54 * @returns False on failure 64 55 */ 65 56 static bool spawn(const char *desc, const char *path) 66 57 { 58 int rc; 59 67 60 printf("%s: Spawning %s (%s)\n", NAME, desc, path); 68 69 const char *argv[2]; 70 71 argv[0] = path; 72 argv[1] = NULL; 73 74 int err; 75 if (task_spawn(path, argv, &err) == 0) { 61 rc = task_spawnl(NULL, path, path, NULL); 62 if (rc != EOK) { 76 63 fprintf(stderr, "%s: Error spawning %s (%s)\n", NAME, path, 77 str_error( err));64 str_error(rc)); 78 65 return false; 79 66 } … … 84 71 int main(int argc, char *argv[]) 85 72 { 86 ERROR_DECLARE; 87 88 /* Run self-tests */ 89 if ((argc > 1) && (str_cmp(argv[1], "-s") == 0)) 90 ERROR_PROPAGATE(self_test()); 73 int rc; 91 74 92 75 if (!spawn("networking service", "/srv/net")) … … 96 79 97 80 int net_phone = connect_to_service(SERVICE_NETWORKING); 98 if (ERROR_OCCURRED(ipc_call_sync_0_0(net_phone, NET_NET_STARTUP))) { 99 fprintf(stderr, "%s: Startup error %d\n", NAME, ERROR_CODE); 100 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; 101 85 } 102 86 -
uspace/app/nettest1/nettest.c
rd70a463 rb40bfac 28 28 29 29 /** @addtogroup nettest 30 * @{30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Networking test support functions implementation.34 * Networking test support functions implementation. 35 35 */ 36 36 37 37 #include <stdio.h> 38 #include <err.h>39 40 38 #include <net/socket.h> 41 39 … … 43 41 #include "print_error.h" 44 42 45 int sockets_create(int verbose, int * socket_ids, int sockets, int family, sock_type_t type){ 46 int index; 47 48 if(verbose){ 43 44 /** Creates new sockets. 45 * 46 * @param[in] verbose A value indicating whether to print out verbose information. 47 * @param[out] socket_ids A field to store the socket identifiers. 48 * @param[in] sockets The number of sockets to create. Should be at most the size of the field. 49 * @param[in] family The socket address family. 50 * @param[in] type The socket type. 51 * @returns EOK on success. 52 * @returns Other error codes as defined for the socket() function. 53 */ 54 int sockets_create(int verbose, int *socket_ids, int sockets, int family, sock_type_t type) 55 { 56 int index; 57 58 if (verbose) 49 59 printf("Create\t"); 50 } 51 fflush(stdout); 52 for(index = 0; index < sockets; ++ index){ 60 61 fflush(stdout); 62 63 for (index = 0; index < sockets; index++) { 53 64 socket_ids[index] = socket(family, type, 0); 54 if (socket_ids[index] < 0){65 if (socket_ids[index] < 0) { 55 66 printf("Socket %d (%d) error:\n", index, socket_ids[index]); 56 67 socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n"); 57 68 return socket_ids[index]; 58 69 } 59 if(verbose){ 60 print_mark(index); 61 } 62 } 63 return EOK; 64 } 65 66 int sockets_close(int verbose, int * socket_ids, int sockets){ 67 ERROR_DECLARE; 68 69 int index; 70 71 if(verbose){ 70 if (verbose) 71 print_mark(index); 72 } 73 74 return EOK; 75 } 76 77 /** Closes sockets. 78 * 79 * @param[in] verbose A value indicating whether to print out verbose information. 80 * @param[in] socket_ids A field of stored socket identifiers. 81 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 82 * @returns EOK on success. 83 * @returns Other error codes as defined for the closesocket() function. 84 */ 85 int sockets_close(int verbose, int *socket_ids, int sockets) 86 { 87 int index; 88 int rc; 89 90 if (verbose) 72 91 printf("\tClose\t"); 73 } 74 fflush(stdout); 75 for(index = 0; index < sockets; ++ index){ 76 if(ERROR_OCCURRED(closesocket(socket_ids[index]))){ 92 93 fflush(stdout); 94 95 for (index = 0; index < sockets; index++) { 96 rc = closesocket(socket_ids[index]); 97 if (rc != EOK) { 77 98 printf("Socket %d (%d) error:\n", index, socket_ids[index]); 78 socket_print_error(stderr, ERROR_CODE, "Socket close: ", "\n"); 79 return ERROR_CODE; 80 } 81 if(verbose){ 82 print_mark(index); 83 } 84 } 85 return EOK; 86 } 87 88 int sockets_connect(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen){ 89 ERROR_DECLARE; 90 91 int index; 92 93 if(verbose){ 99 socket_print_error(stderr, rc, "Socket close: ", "\n"); 100 return rc; 101 } 102 if (verbose) 103 print_mark(index); 104 } 105 106 return EOK; 107 } 108 109 /** Connects sockets. 110 * 111 * @param[in] verbose A value indicating whether to print out verbose information. 112 * @param[in] socket_ids A field of stored socket identifiers. 113 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 114 * @param[in] address The destination host address to connect to. 115 * @param[in] addrlen The length of the destination address in bytes. 116 * @returns EOK on success. 117 * @returns Other error codes as defined for the connect() function. 118 */ 119 int sockets_connect(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen) 120 { 121 int index; 122 int rc; 123 124 if (verbose) 94 125 printf("\tConnect\t"); 95 } 96 fflush(stdout); 97 for(index = 0; index < sockets; ++ index){ 98 if(ERROR_OCCURRED(connect(socket_ids[index], address, addrlen))){ 99 socket_print_error(stderr, ERROR_CODE, "Socket connect: ", "\n"); 100 return ERROR_CODE; 101 } 102 if(verbose){ 103 print_mark(index); 104 } 105 } 106 return EOK; 107 } 108 109 int sockets_sendto(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages){ 110 ERROR_DECLARE; 111 126 127 fflush(stdout); 128 129 for (index = 0; index < sockets; index++) { 130 rc = connect(socket_ids[index], address, addrlen); 131 if (rc != EOK) { 132 socket_print_error(stderr, rc, "Socket connect: ", "\n"); 133 return rc; 134 } 135 if (verbose) 136 print_mark(index); 137 } 138 139 return EOK; 140 } 141 142 /** Sends data via sockets. 143 * 144 * @param[in] verbose A value indicating whether to print out verbose information. 145 * @param[in] socket_ids A field of stored socket identifiers. 146 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 147 * @param[in] address The destination host address to send data to. 148 * @param[in] addrlen The length of the destination address in bytes. 149 * @param[in] data The data to be sent. 150 * @param[in] size The data size in bytes. 151 * @param[in] messages The number of datagrams per socket to be sent. 152 * @returns EOK on success. 153 * @returns Other error codes as defined for the sendto() function. 154 */ 155 int sockets_sendto(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen, char *data, int size, int messages) 156 { 112 157 int index; 113 158 int message; 114 115 if(verbose){ 159 int rc; 160 161 if (verbose) 116 162 printf("\tSendto\t"); 117 } 118 fflush(stdout); 119 for(index = 0; index < sockets; ++ index){ 120 for(message = 0; message < messages; ++ message){ 121 if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, addrlen))){ 122 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 123 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 124 return ERROR_CODE; 125 } 126 } 127 if(verbose){ 128 print_mark(index); 129 } 130 } 131 return EOK; 132 } 133 134 int sockets_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){ 163 164 fflush(stdout); 165 166 for (index = 0; index < sockets; index++) { 167 for (message = 0; message < messages; message++) { 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; 173 } 174 } 175 if (verbose) 176 print_mark(index); 177 } 178 179 return EOK; 180 } 181 182 /** Receives data via sockets. 183 * 184 * @param[in] verbose A value indicating whether to print out verbose information. 185 * @param[in] socket_ids A field of stored socket identifiers. 186 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 187 * @param[in] address The source host address of received datagrams. 188 * @param[in,out] addrlen The maximum length of the source address in bytes. The actual size of the source address is set instead. 189 * @param[out] data The received data. 190 * @param[in] size The maximum data size in bytes. 191 * @param[in] messages The number of datagrams per socket to be received. 192 * @returns EOK on success. 193 * @returns Other error codes as defined for the recvfrom() function. 194 */ 195 int sockets_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages) 196 { 135 197 int value; 136 198 int index; 137 199 int message; 138 200 139 if (verbose){201 if (verbose) 140 202 printf("\tRecvfrom\t"); 141 } 142 fflush(stdout); 143 for(index = 0; index < sockets; ++ index){ 144 for(message = 0; message < messages; ++ message){ 203 204 fflush(stdout); 205 206 for (index = 0; index < sockets; index++) { 207 for (message = 0; message < messages; message++) { 145 208 value = recvfrom(socket_ids[index], data, size, 0, address, addrlen); 146 if (value < 0){209 if (value < 0) { 147 210 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 148 211 socket_print_error(stderr, value, "Socket receive: ", "\n"); … … 150 213 } 151 214 } 152 if(verbose){ 153 print_mark(index); 154 } 155 } 156 return EOK; 157 } 158 159 int sockets_sendto_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){ 160 ERROR_DECLARE; 161 215 if (verbose) 216 print_mark(index); 217 } 218 return EOK; 219 } 220 221 /** Sends and receives data via sockets. 222 * 223 * Each datagram is sent and a reply read consequently. 224 * The next datagram is sent after the reply is received. 225 * 226 * @param[in] verbose A value indicating whether to print out verbose information. 227 * @param[in] socket_ids A field of stored socket identifiers. 228 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 229 * @param[in,out] address The destination host address to send data to. The source host address of received datagrams is set instead. 230 * @param[in] addrlen The length of the destination address in bytes. 231 * @param[in,out] data The data to be sent. The received data are set instead. 232 * @param[in] size The data size in bytes. 233 * @param[in] messages The number of datagrams per socket to be received. 234 * @returns EOK on success. 235 * @returns Other error codes as defined for the recvfrom() function. 236 */ 237 int sockets_sendto_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages) 238 { 162 239 int value; 163 240 int index; 164 241 int message; 165 166 if(verbose){ 242 int rc; 243 244 if (verbose) 167 245 printf("\tSendto and recvfrom\t"); 168 } 169 fflush(stdout); 170 for(index = 0; index < sockets; ++ index){ 171 for(message = 0; message < messages; ++ message){ 172 if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, * addrlen))){ 173 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 174 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n"); 175 return ERROR_CODE; 246 247 fflush(stdout); 248 249 for (index = 0; index < sockets; index++) { 250 for (message = 0; message < messages; message++) { 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; 176 256 } 177 257 value = recvfrom(socket_ids[index], data, size, 0, address, addrlen); 178 if (value < 0){258 if (value < 0) { 179 259 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 180 260 socket_print_error(stderr, value, "Socket receive: ", "\n"); … … 182 262 } 183 263 } 184 if(verbose){ 185 print_mark(index); 186 } 187 } 188 return EOK; 189 } 190 191 void print_mark(int index){ 192 if((index + 1) % 10){ 264 if (verbose) 265 print_mark(index); 266 } 267 268 return EOK; 269 } 270 271 /** Prints a mark. 272 * 273 * If the index is a multiple of ten, a different mark is printed. 274 * 275 * @param[in] index The index of the mark to be printed. 276 */ 277 void print_mark(int index) 278 { 279 if ((index + 1) % 10) 193 280 printf("*"); 194 }else{281 else 195 282 printf("|"); 196 }197 283 fflush(stdout); 198 284 } -
uspace/app/nettest1/nettest.h
rd70a463 rb40bfac 28 28 29 29 /** @addtogroup nettest 30 * @{30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Networking test support functions.34 * Networking test support functions. 35 35 */ 36 36 37 #ifndef __NET_TEST__38 #define __NET_TEST__37 #ifndef NET_TEST_ 38 #define NET_TEST_ 39 39 40 40 #include <net/socket.h> 41 41 42 /** Prints a mark. 43 * If the index is a multiple of ten, a different mark is printed. 44 * @param[in] index The index of the mark to be printed. 45 */ 46 extern void print_mark(int index); 47 48 /** Creates new sockets. 49 * @param[in] verbose A value indicating whether to print out verbose information. 50 * @param[out] socket_ids A field to store the socket identifiers. 51 * @param[in] sockets The number of sockets to create. Should be at most the size of the field. 52 * @param[in] family The socket address family. 53 * @param[in] type The socket type. 54 * @returns EOK on success. 55 * @returns Other error codes as defined for the socket() function. 56 */ 57 extern int sockets_create(int verbose, int * socket_ids, int sockets, int family, sock_type_t type); 58 59 /** Closes sockets. 60 * @param[in] verbose A value indicating whether to print out verbose information. 61 * @param[in] socket_ids A field of stored socket identifiers. 62 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 63 * @returns EOK on success. 64 * @returns Other error codes as defined for the closesocket() function. 65 */ 66 extern int sockets_close(int verbose, int * socket_ids, int sockets); 67 68 /** Connects sockets. 69 * @param[in] verbose A value indicating whether to print out verbose information. 70 * @param[in] socket_ids A field of stored socket identifiers. 71 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 72 * @param[in] address The destination host address to connect to. 73 * @param[in] addrlen The length of the destination address in bytes. 74 * @returns EOK on success. 75 * @returns Other error codes as defined for the connect() function. 76 */ 77 extern int sockets_connect(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen); 78 79 /** Sends data via sockets. 80 * @param[in] verbose A value indicating whether to print out verbose information. 81 * @param[in] socket_ids A field of stored socket identifiers. 82 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 83 * @param[in] address The destination host address to send data to. 84 * @param[in] addrlen The length of the destination address in bytes. 85 * @param[in] data The data to be sent. 86 * @param[in] size The data size in bytes. 87 * @param[in] messages The number of datagrams per socket to be sent. 88 * @returns EOK on success. 89 * @returns Other error codes as defined for the sendto() function. 90 */ 91 extern int sockets_sendto(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages); 92 93 /** Receives data via sockets. 94 * @param[in] verbose A value indicating whether to print out verbose information. 95 * @param[in] socket_ids A field of stored socket identifiers. 96 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 97 * @param[in] address The source host address of received datagrams. 98 * @param[in,out] addrlen The maximum length of the source address in bytes. The actual size of the source address is set instead. 99 * @param[out] data The received data. 100 * @param[in] size The maximum data size in bytes. 101 * @param[in] messages The number of datagrams per socket to be received. 102 * @returns EOK on success. 103 * @returns Other error codes as defined for the recvfrom() function. 104 */ 105 extern int sockets_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages); 106 107 /** Sends and receives data via sockets. 108 * Each datagram is sent and a reply read consequently. 109 * The next datagram is sent after the reply is received. 110 * @param[in] verbose A value indicating whether to print out verbose information. 111 * @param[in] socket_ids A field of stored socket identifiers. 112 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field. 113 * @param[in,out] address The destination host address to send data to. The source host address of received datagrams is set instead. 114 * @param[in] addrlen The length of the destination address in bytes. 115 * @param[in,out] data The data to be sent. The received data are set instead. 116 * @param[in] size The data size in bytes. 117 * @param[in] messages The number of datagrams per socket to be received. 118 * @returns EOK on success. 119 * @returns Other error codes as defined for the recvfrom() function. 120 */ 121 extern int sockets_sendto_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages); 42 extern void print_mark(int); 43 extern int sockets_create(int, int *, int, int, sock_type_t); 44 extern int sockets_close(int, int *, int); 45 extern int sockets_connect(int, int *, int, struct sockaddr *, socklen_t); 46 extern int sockets_sendto(int, int *, int, struct sockaddr *, socklen_t, char *, int, int); 47 extern int sockets_recvfrom(int, int *, int, struct sockaddr *, socklen_t *, char *, int, int); 48 extern int sockets_sendto_recvfrom(int, int *, int, struct sockaddr *, socklen_t *, char *, int, int); 122 49 123 50 #endif … … 125 52 /** @} 126 53 */ 54 -
uspace/app/nettest1/nettest1.c
rd70a463 rb40bfac 28 28 29 29 /** @addtogroup nettest 30 * @{30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Networking test 1 application - sockets. 35 */ 34 * Networking test 1 application - sockets. 35 */ 36 37 #include "nettest.h" 38 #include "print_error.h" 36 39 37 40 #include <malloc.h> … … 41 44 #include <time.h> 42 45 #include <arg_parse.h> 43 #include <err.h>44 46 45 47 #include <net/in.h> … … 49 51 #include <net/socket_parse.h> 50 52 51 #include "nettest.h" 52 #include "print_error.h" 53 54 /** Echo module name. 55 */ 53 /** Echo module name. */ 56 54 #define NAME "Nettest1" 57 55 58 /** Packet data pattern. 59 */ 56 /** Packet data pattern. */ 60 57 #define NETTEST1_TEXT "Networking test 1 - sockets" 61 58 62 /** Module entry point. 63 * Starts testing. 64 * @param[in] argc The number of command line parameters. 65 * @param[in] argv The command line parameters. 66 * @returns EOK on success. 67 */ 68 int main(int argc, char * argv[]); 69 70 /** Prints the application help. 71 */ 72 void nettest1_print_help(void); 73 74 /** Refreshes the data. 75 * Fills the data block with the NETTEST1_TEXT pattern. 76 * @param[out] data The data block. 77 * @param[in] size The data block size in bytes. 78 */ 79 void nettest1_refresh_data(char * data, size_t size); 80 81 int main(int argc, char * argv[]){ 82 ERROR_DECLARE; 83 84 size_t size = 27; 85 int verbose = 0; 86 sock_type_t type = SOCK_DGRAM; 87 int sockets = 10; 88 int messages = 10; 89 int family = PF_INET; 90 uint16_t port = 7; 91 92 socklen_t max_length = sizeof(struct sockaddr_in6); 93 uint8_t address_data[max_length]; 94 struct sockaddr * address = (struct sockaddr *) address_data; 95 struct sockaddr_in * address_in = (struct sockaddr_in *) address; 96 struct sockaddr_in6 * address_in6 = (struct sockaddr_in6 *) address; 97 socklen_t addrlen; 98 // char address_string[INET6_ADDRSTRLEN]; 99 uint8_t * address_start; 100 101 int * socket_ids; 102 char * data; 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 72 static void nettest1_print_help(void) 73 { 74 printf( 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"); 102 } 103 104 /** Parse one command-line option. 105 * 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 { 103 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. 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; 104 297 int index; 105 298 struct timeval time_before; 106 299 struct timeval time_after; 107 300 108 // parse the command line arguments 109 // stop before the last argument if it does not start with the minus sign ('-') 110 for(index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index){ 111 // options should start with the minus sign ('-') 112 if(argv[index][0] == '-'){ 113 switch(argv[index][1]){ 114 // short options with only one letter 115 case 'f': 116 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 117 break; 118 case 'h': 119 nettest1_print_help(); 120 return EOK; 121 break; 122 case 'm': 123 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 0)); 124 break; 125 case 'n': 126 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 0)); 127 break; 128 case 'p': 129 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 130 port = (uint16_t) value; 131 break; 132 case 's': 133 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 134 size = (value >= 0) ? (size_t) value : 0; 135 break; 136 case 't': 137 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 138 type = (sock_type_t) value; 139 break; 140 case 'v': 141 verbose = 1; 142 break; 143 // long options with the double minus sign ('-') 144 case '-': 145 if(str_lcmp(argv[index] + 2, "family=", 7) == 0){ 146 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 147 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){ 148 nettest1_print_help(); 149 return EOK; 150 }else if(str_lcmp(argv[index] + 2, "messages=", 6) == 0){ 151 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 8)); 152 }else if(str_lcmp(argv[index] + 2, "sockets=", 6) == 0){ 153 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 8)); 154 }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){ 155 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 156 port = (uint16_t) value; 157 }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){ 158 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 159 type = (sock_type_t) value; 160 }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){ 161 verbose = 1; 162 }else{ 163 nettest1_print_help(); 164 return EINVAL; 165 } 166 break; 167 default: 168 nettest1_print_help(); 169 return EINVAL; 170 } 171 }else{ 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 { 172 323 nettest1_print_help(); 173 324 return EINVAL; … … 175 326 } 176 327 177 / / if not before the last argument containing the address178 if (index >= argc){328 /* If not before the last argument containing the address */ 329 if (index >= argc) { 179 330 printf("Command line error: missing address\n"); 180 331 nettest1_print_help(); … … 182 333 } 183 334 184 / / prepare the address buffer335 /* Prepare the address buffer */ 185 336 bzero(address_data, max_length); 186 switch(family){ 187 case PF_INET: 188 address_in->sin_family = AF_INET; 189 address_in->sin_port = htons(port); 190 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 191 addrlen = sizeof(struct sockaddr_in); 192 break; 193 case PF_INET6: 194 address_in6->sin6_family = AF_INET6; 195 address_in6->sin6_port = htons(port); 196 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 197 addrlen = sizeof(struct sockaddr_in6); 198 break; 199 default: 200 fprintf(stderr, "Address family is not supported\n"); 201 return EAFNOSUPPORT; 202 } 203 204 // parse the last argument which should contain the address 205 if(ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))){ 206 fprintf(stderr, "Address parse error %d\n", ERROR_CODE); 207 return ERROR_CODE; 208 } 209 210 // check the buffer size 211 if(size <= 0){ 212 fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size); 337 338 switch (family) { 339 case PF_INET: 340 address_in->sin_family = AF_INET; 341 address_in->sin_port = htons(port); 342 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 343 addrlen = sizeof(struct sockaddr_in); 344 break; 345 case PF_INET6: 346 address_in6->sin6_family = AF_INET6; 347 address_in6->sin6_port = htons(port); 348 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 349 addrlen = sizeof(struct sockaddr_in6); 350 break; 351 default: 352 fprintf(stderr, "Address family is not supported\n"); 353 return EAFNOSUPPORT; 354 } 355 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 */ 364 if (size <= 0) { 365 fprintf(stderr, "Data buffer size too small (%d). Using 1024 " 366 "bytes instead.\n", size); 213 367 size = 1024; 214 368 } 215 369 216 // prepare the buffer 217 // size plus the terminating null (\0) 370 /* 371 * Prepare data buffer. Allocate size bytes plus one for the 372 * trailing null character. 373 */ 218 374 data = (char *) malloc(size + 1); 219 if (! data){375 if (!data) { 220 376 fprintf(stderr, "Failed to allocate data buffer.\n"); 221 377 return ENOMEM; 222 378 } 223 nettest1_refresh_data(data, size); 224 225 // check the socket count 226 if(sockets <= 0){ 227 fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets); 379 nettest1_fill_buffer(data, size); 380 381 /* Check socket count */ 382 if (sockets <= 0) { 383 fprintf(stderr, "Socket count too small (%d). Using " 384 "2 instead.\n", sockets); 228 385 sockets = 2; 229 386 } 230 387 231 // prepare the socket buffer 232 // count plus the terminating null (\0) 388 /* 389 * Prepare socket buffer. Allocate count fields plus the terminating 390 * null (\0). 391 */ 233 392 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 234 if (! socket_ids){393 if (!socket_ids) { 235 394 fprintf(stderr, "Failed to allocate receive buffer.\n"); 236 395 return ENOMEM; … … 238 397 socket_ids[sockets] = NULL; 239 398 240 if (verbose){399 if (verbose) 241 400 printf("Starting tests\n"); 242 } 243 244 if(verbose){ 245 printf("1 socket, 1 message\n"); 246 } 247 248 if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){ 249 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 250 return ERROR_CODE; 251 } 252 253 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 254 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 255 if(verbose){ 256 printf("\tOK\n"); 257 } 258 259 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 260 if(type == SOCK_STREAM){ 261 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 262 } 263 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1)); 264 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 265 if(verbose){ 266 printf("\tOK\n"); 267 } 268 269 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 270 if(type == SOCK_STREAM){ 271 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 272 } 273 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, 1)); 274 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1)); 275 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 276 if(verbose){ 277 printf("\tOK\n"); 278 } 279 280 if(verbose){ 281 printf("1 socket, %d messages\n", messages); 282 } 283 284 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type)); 285 if(type == SOCK_STREAM){ 286 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen)); 287 } 288 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages)); 289 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 290 if(verbose){ 291 printf("\tOK\n"); 292 } 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 } 298 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, messages)); 299 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages)); 300 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1)); 301 if(verbose){ 302 printf("\tOK\n"); 303 } 304 305 if(verbose){ 306 printf("%d sockets, 1 message\n", sockets); 307 } 308 309 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 310 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 311 if(verbose){ 312 printf("\tOK\n"); 313 } 314 315 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 316 if(type == SOCK_STREAM){ 317 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 318 } 319 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1)); 320 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 321 if(verbose){ 322 printf("\tOK\n"); 323 } 324 325 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 326 if(type == SOCK_STREAM){ 327 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 328 } 329 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, 1)); 330 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1)); 331 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 332 if(verbose){ 333 printf("\tOK\n"); 334 } 335 336 if(verbose){ 337 printf("%d sockets, %d messages\n", sockets, messages); 338 } 339 340 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 341 if(type == SOCK_STREAM){ 342 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 343 } 344 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 345 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 346 if(verbose){ 347 printf("\tOK\n"); 348 } 349 350 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 351 if(type == SOCK_STREAM){ 352 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 353 } 354 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages)); 355 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 356 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 357 358 if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){ 359 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 360 return ERROR_CODE; 361 } 362 363 if(verbose){ 364 printf("\tOK\n"); 365 } 366 367 printf("Tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 368 369 if(verbose){ 401 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)); 421 422 if (verbose) 370 423 printf("Exiting\n"); 371 }372 424 373 425 return EOK; 374 426 } 375 427 376 void nettest1_print_help(void){377 printf(378 "Network Networking test 1 aplication - sockets\n" \379 "Usage: echo [options] numeric_address\n" \380 "Where options are:\n" \381 "-f protocol_family | --family=protocol_family\n" \382 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"383 "\n" \384 "-h | --help\n" \385 "\tShow this application help.\n"386 "\n" \387 "-m count | --messages=count\n" \388 "\tThe number of messages to send and receive per socket. The default is 10.\n" \389 "\n" \390 "-n sockets | --sockets=count\n" \391 "\tThe number of sockets to use. The default is 10.\n" \392 "\n" \393 "-p port_number | --port=port_number\n" \394 "\tThe port number the application should send messages to. The default is 7.\n" \395 "\n" \396 "-s packet_size | --size=packet_size\n" \397 "\tThe packet data size the application sends. The default is 28 bytes.\n" \398 "\n" \399 "-v | --verbose\n" \400 "\tShow all output messages.\n"401 );402 }403 404 void nettest1_refresh_data(char * data, size_t size){405 size_t length;406 407 // fill the data408 length = 0;409 while(size > length + sizeof(NETTEST1_TEXT) - 1){410 memcpy(data + length, NETTEST1_TEXT, sizeof(NETTEST1_TEXT) - 1);411 length += sizeof(NETTEST1_TEXT) - 1;412 }413 memcpy(data + length, NETTEST1_TEXT, size - length);414 data[size] = '\0';415 }416 428 417 429 /** @} -
uspace/app/nettest2/nettest2.c
rd70a463 rb40bfac 28 28 29 29 /** @addtogroup nettest 30 * @{30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * Networking test 2 application - transfer. 35 */ 34 * Networking test 2 application - transfer. 35 */ 36 37 #include "nettest.h" 38 #include "print_error.h" 36 39 37 40 #include <malloc.h> … … 41 44 #include <time.h> 42 45 #include <arg_parse.h> 43 #include < err.h>46 #include <bool.h> 44 47 45 48 #include <net/in.h> … … 49 52 #include <net/socket_parse.h> 50 53 51 #include "nettest.h" 52 #include "print_error.h" 53 54 /** Echo module name. 55 */ 54 /** Echo module name. */ 56 55 #define NAME "Nettest2" 57 56 58 /** Packet data pattern. 59 */ 57 /** Packet data pattern. */ 60 58 #define NETTEST2_TEXT "Networking test 2 - transfer" 61 59 62 /** Module entry point. 63 * Starts testing. 64 * @param[in] argc The number of command line parameters. 65 * @param[in] argv The command line parameters. 66 * @returns EOK on success. 67 */ 68 int main(int argc, char * argv[]); 69 70 /** Prints the application help. 71 */ 72 void nettest2_print_help(void); 73 74 /** Refreshes the data. 75 * Fills the data block with the NETTEST1_TEXT pattern. 76 * @param[out] data The data block. 77 * @param[in] size The data block size in bytes. 78 */ 79 void nettest2_refresh_data(char * data, size_t size); 80 81 int main(int argc, char * argv[]){ 82 ERROR_DECLARE; 83 84 size_t size = 28; 85 int verbose = 0; 86 sock_type_t type = SOCK_DGRAM; 87 int sockets = 10; 88 int messages = 10; 89 int family = PF_INET; 90 uint16_t port = 7; 91 92 socklen_t max_length = sizeof(struct sockaddr_in6); 93 uint8_t address_data[max_length]; 94 struct sockaddr * address = (struct sockaddr *) address_data; 95 struct sockaddr_in * address_in = (struct sockaddr_in *) address; 96 struct sockaddr_in6 * address_in6 = (struct sockaddr_in6 *) address; 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 68 static void nettest2_print_help(void) 69 { 70 printf( 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"); 98 } 99 100 /** Fill buffer with the NETTEST2_TEXT pattern. 101 * 102 * @param buffer Data buffer. 103 * @param size Buffer size in bytes. 104 */ 105 static void nettest2_fill_buffer(char *buffer, size_t size) 106 { 107 size_t length; 108 109 length = 0; 110 while (size > length + sizeof(NETTEST2_TEXT) - 1) { 111 memcpy(buffer + length, NETTEST2_TEXT, 112 sizeof(NETTEST2_TEXT) - 1); 113 length += sizeof(NETTEST2_TEXT) - 1; 114 } 115 116 memcpy(buffer + length, NETTEST2_TEXT, size - length); 117 buffer[size] = '\0'; 118 } 119 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) 127 { 128 int value; 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; 210 } else { 211 nettest2_print_help(); 212 return EINVAL; 213 } 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; 97 230 socklen_t addrlen; 98 // char address_string[INET6_ADDRSTRLEN]; 99 uint8_t * address_start; 100 101 int * socket_ids; 102 char * data; 103 int value; 231 uint8_t *address_start; 232 233 int *socket_ids; 234 char *data; 104 235 int index; 105 236 struct timeval time_before; 106 237 struct timeval time_after; 107 238 108 // parse the command line arguments 109 // stop before the last argument if it does not start with the minus sign ('-') 110 for(index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index){ 111 // options should start with the minus sign ('-') 112 if(argv[index][0] == '-'){ 113 switch(argv[index][1]){ 114 // short options with only one letter 115 case 'f': 116 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family)); 117 break; 118 case 'h': 119 nettest2_print_help(); 120 return EOK; 121 break; 122 case 'm': 123 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 0)); 124 break; 125 case 'n': 126 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 0)); 127 break; 128 case 'p': 129 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 130 port = (uint16_t) value; 131 break; 132 case 's': 133 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 134 size = (value >= 0) ? (size_t) value : 0; 135 break; 136 case 't': 137 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type)); 138 type = (sock_type_t) value; 139 break; 140 case 'v': 141 verbose = 1; 142 break; 143 // long options with the double minus sign ('-') 144 case '-': 145 if(str_lcmp(argv[index] + 2, "family=", 7) == 0){ 146 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family)); 147 }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){ 148 nettest2_print_help(); 149 return EOK; 150 }else if(str_lcmp(argv[index] + 2, "messages=", 6) == 0){ 151 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 8)); 152 }else if(str_lcmp(argv[index] + 2, "sockets=", 6) == 0){ 153 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 8)); 154 }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){ 155 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7)); 156 port = (uint16_t) value; 157 }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){ 158 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type)); 159 type = (sock_type_t) value; 160 }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){ 161 verbose = 1; 162 }else{ 163 nettest2_print_help(); 164 return EINVAL; 165 } 166 break; 167 default: 168 nettest2_print_help(); 169 return EINVAL; 170 } 171 }else{ 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 { 172 268 nettest2_print_help(); 173 269 return EINVAL; … … 175 271 } 176 272 177 / / if not before the last argument containing the address178 if (index >= argc){273 /* If not before the last argument containing the address */ 274 if (index >= argc) { 179 275 printf("Command line error: missing address\n"); 180 276 nettest2_print_help(); … … 182 278 } 183 279 184 / / prepare the address buffer280 /* Prepare the address buffer */ 185 281 bzero(address_data, max_length); 186 switch(family){ 187 case PF_INET: 188 address_in->sin_family = AF_INET; 189 address_in->sin_port = htons(port); 190 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 191 addrlen = sizeof(struct sockaddr_in); 192 break; 193 case PF_INET6: 194 address_in6->sin6_family = AF_INET6; 195 address_in6->sin6_port = htons(port); 196 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 197 addrlen = sizeof(struct sockaddr_in6); 198 break; 199 default: 200 fprintf(stderr, "Address family is not supported\n"); 201 return EAFNOSUPPORT; 202 } 203 204 // parse the last argument which should contain the address 205 if(ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))){ 206 fprintf(stderr, "Address parse error %d\n", ERROR_CODE); 207 return ERROR_CODE; 208 } 209 210 // check the buffer size 211 if(size <= 0){ 212 fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size); 282 283 switch (family) { 284 case PF_INET: 285 address_in->sin_family = AF_INET; 286 address_in->sin_port = htons(port); 287 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 288 addrlen = sizeof(struct sockaddr_in); 289 break; 290 case PF_INET6: 291 address_in6->sin6_family = AF_INET6; 292 address_in6->sin6_port = htons(port); 293 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 294 addrlen = sizeof(struct sockaddr_in6); 295 break; 296 default: 297 fprintf(stderr, "Address family is not supported\n"); 298 return EAFNOSUPPORT; 299 } 300 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. */ 309 if (size <= 0) { 310 fprintf(stderr, "Data buffer size too small (%d). Using 1024 " 311 "bytes instead.\n", size); 213 312 size = 1024; 214 313 } 215 314 216 // prepare the buffer 217 // size plus terminating null (\0) 315 /* 316 * Prepare the buffer. Allocate size bytes plus one for terminating 317 * null character. 318 */ 218 319 data = (char *) malloc(size + 1); 219 if (! data){320 if (!data) { 220 321 fprintf(stderr, "Failed to allocate data buffer.\n"); 221 322 return ENOMEM; 222 323 } 223 nettest2_refresh_data(data, size); 224 225 // check the socket count 226 if(sockets <= 0){ 227 fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets); 324 325 /* Fill buffer with a pattern. */ 326 nettest2_fill_buffer(data, size); 327 328 /* Check socket count. */ 329 if (sockets <= 0) { 330 fprintf(stderr, "Socket count too small (%d). Using " 331 "2 instead.\n", sockets); 228 332 sockets = 2; 229 333 } 230 334 231 // prepare the socket buffer 232 // count plus the terminating null (\0) 335 /* 336 * Prepare the socket buffer. 337 * Allocate count entries plus the terminating null (\0) 338 */ 233 339 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 234 if (! socket_ids){340 if (!socket_ids) { 235 341 fprintf(stderr, "Failed to allocate receive buffer.\n"); 236 342 return ENOMEM; … … 238 344 socket_ids[sockets] = NULL; 239 345 240 if (verbose){346 if (verbose) 241 347 printf("Starting tests\n"); 242 } 243 244 ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type)); 245 246 if(type == SOCK_STREAM){ 247 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen)); 248 } 249 250 if(verbose){ 348 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 } 359 360 if (verbose) 251 361 printf("\n"); 252 } 253 254 if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){ 255 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 256 return ERROR_CODE; 257 } 258 259 ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 260 261 if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){ 262 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 263 return ERROR_CODE; 264 } 265 266 if(verbose){ 362 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; 378 } 379 380 if (verbose) 267 381 printf("\tOK\n"); 268 } 269 270 printf("sendto + recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 271 272 if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){ 273 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 274 return ERROR_CODE; 275 } 276 277 ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages)); 278 ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages)); 279 280 if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){ 281 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE); 282 return ERROR_CODE; 283 } 284 285 if(verbose){ 382 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; 406 } 407 408 if (verbose) 286 409 printf("\tOK\n"); 287 } 288 289 printf("sendto, recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before)); 290 291 ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets)); 292 293 if(verbose){ 410 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; 417 418 if (verbose) 294 419 printf("\nExiting\n"); 295 }296 420 297 421 return EOK; 298 422 } 299 423 300 void nettest2_print_help(void){301 printf(302 "Network Networking test 2 aplication - UDP transfer\n" \303 "Usage: echo [options] numeric_address\n" \304 "Where options are:\n" \305 "-f protocol_family | --family=protocol_family\n" \306 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"307 "\n" \308 "-h | --help\n" \309 "\tShow this application help.\n"310 "\n" \311 "-m count | --messages=count\n" \312 "\tThe number of messages to send and receive per socket. The default is 10.\n" \313 "\n" \314 "-n sockets | --sockets=count\n" \315 "\tThe number of sockets to use. The default is 10.\n" \316 "\n" \317 "-p port_number | --port=port_number\n" \318 "\tThe port number the application should send messages to. The default is 7.\n" \319 "\n" \320 "-s packet_size | --size=packet_size\n" \321 "\tThe packet data size the application sends. The default is 29 bytes.\n" \322 "\n" \323 "-v | --verbose\n" \324 "\tShow all output messages.\n"325 );326 }327 328 void nettest2_refresh_data(char * data, size_t size){329 size_t length;330 331 // fill the data332 length = 0;333 while(size > length + sizeof(NETTEST2_TEXT) - 1){334 memcpy(data + length, NETTEST2_TEXT, sizeof(NETTEST2_TEXT) - 1);335 length += sizeof(NETTEST2_TEXT) - 1;336 }337 memcpy(data + length, NETTEST2_TEXT, size - length);338 data[size] = '\0';339 }340 341 424 /** @} 342 425 */ -
uspace/app/redir/redir.c
rd70a463 rb40bfac 43 43 #include <task.h> 44 44 #include <str_error.h> 45 #include <errno.h> 45 46 46 47 #define NAME "redir" … … 76 77 static task_id_t spawn(int argc, char *argv[]) 77 78 { 78 const char **args = (const char **) calloc(argc + 1, sizeof(char *)); 79 const char **args; 80 task_id_t id; 81 int rc; 82 83 args = (const char **) calloc(argc + 1, sizeof(char *)); 79 84 if (!args) { 80 85 printf("No memory available\n"); … … 88 93 args[argc] = NULL; 89 94 90 int err; 91 task_id_t id = task_spawn(argv[0], args, &err); 95 rc = task_spawnv(&id, argv[0], args); 92 96 93 97 free(args); 94 98 95 if ( id == 0)99 if (rc != EOK) { 96 100 printf("%s: Error spawning %s (%s)\n", NAME, argv[0], 97 str_error(err)); 101 str_error(rc)); 102 } 98 103 99 104 return id; -
uspace/app/sbi/src/os/helenos.c
rd70a463 rb40bfac 249 249 task_id_t tid; 250 250 task_exit_t texit; 251 int r etval;252 253 tid = task_spawn(cmd[0], (char const * const *) cmd, &retval);254 if ( tid == 0) {251 int rc, retval; 252 253 rc = task_spawnv(&tid, cmd[0], (char const * const *) cmd); 254 if (rc != EOK) { 255 255 printf("Error: Failed spawning '%s' (%s).\n", cmd[0], 256 str_error(r etval));256 str_error(rc)); 257 257 exit(1); 258 258 } 259 259 260 260 /* XXX Handle exit status and return value. */ 261 task_wait(tid, &texit, &retval); 261 rc = task_wait(tid, &texit, &retval); 262 (void) rc; 262 263 263 264 return EOK;
Note:
See TracChangeset
for help on using the changeset viewer.
