Changeset 0b749a3 in mainline for uspace/app
- Timestamp:
- 2010-11-22T15:39:53Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 0eddb76, aae339e9
- Parents:
- 9a1d8ab (diff), 8cd1aa5e (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:
-
- 13 added
- 25 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/builtins/builtins.h
r9a1d8ab r0b749a3 10 10 #include "cd/cd_def.h" 11 11 #include "exit/exit_def.h" 12 {NULL, NULL, NULL, NULL, NULL}12 {NULL, NULL, NULL, NULL, 0} 13 13 }; 14 14 -
uspace/app/bdsh/cmds/modules/bdd/bdd.c
r9a1d8ab r0b749a3 68 68 unsigned int argc; 69 69 unsigned int i, j; 70 dev _handle_t handle;70 devmap_handle_t handle; 71 71 uint8_t *blk; 72 72 size_t size, bytes, rows; -
uspace/app/bdsh/cmds/modules/help/help.c
r9a1d8ab r0b749a3 45 45 extern const char *progname; 46 46 47 #define HELP_IS_MODULE 1 48 #define HELP_IS_BUILTIN 0 49 #define HELP_IS_RUBBISH -1 47 #define HELP_IS_COMMANDS 2 48 #define HELP_IS_MODULE 1 49 #define HELP_IS_BUILTIN 0 50 #define HELP_IS_RUBBISH -1 50 51 51 52 volatile int mod_switch = -1; … … 55 56 { 56 57 int rc = HELP_IS_RUBBISH; 58 59 if (str_cmp(cmd, "commands") == 0) 60 return HELP_IS_COMMANDS; 57 61 58 62 rc = is_builtin(cmd); … … 90 94 } 91 95 92 int cmd_help(char *argv[])96 static void help_commands(void) 93 97 { 98 builtin_t *cmd; 94 99 module_t *mod; 95 builtin_t *cmd; 96 unsigned int i = 0; 97 int rc = 0; 98 int argc; 99 int level = HELP_SHORT; 100 unsigned int i; 100 101 101 argc = cli_count_args(argv); 102 103 if (argc > 3) { 104 printf("\nToo many arguments to `%s', try:\n", cmdname); 105 help_cmd_help(HELP_SHORT); 106 return CMD_FAILURE; 107 } 108 109 if (argc == 3) { 110 if (!str_cmp("extended", argv[2])) 111 level = HELP_LONG; 112 else 113 level = HELP_SHORT; 114 } 115 116 if (argc > 1) { 117 rc = is_mod_or_builtin(argv[1]); 118 switch (rc) { 119 case HELP_IS_RUBBISH: 120 printf("Invalid command %s\n", argv[1]); 121 return CMD_FAILURE; 122 case HELP_IS_MODULE: 123 help_module(mod_switch, level); 124 return CMD_SUCCESS; 125 case HELP_IS_BUILTIN: 126 help_builtin(mod_switch, level); 127 return CMD_SUCCESS; 128 } 129 } 130 131 printf("\n Available commands are:\n"); 102 printf("\n Bdsh built-in commands:\n"); 132 103 printf(" ------------------------------------------------------------\n"); 133 104 … … 154 125 printf("\n Try %s %s for more information on how `%s' works.\n\n", 155 126 cmdname, cmdname, cmdname); 127 } 128 129 /** Display survival tips. ('help' without arguments) */ 130 static void help_survival(void) 131 { 132 printf("Don't panic!\n\n"); 133 134 printf("This is Bdsh, the Brain dead shell, currently " 135 "the primary user interface to HelenOS. Bdsh allows you to enter " 136 "commands and supports history (Up, Down arrow keys), " 137 "line editing (Left Arrow, Right Arrow, Home, End, Backspace), " 138 "selection (Shift + movement keys), copy and paste (Ctrl-C, " 139 "Ctrl-V), similar to common desktop environments.\n\n"); 140 141 printf("The most basic filesystem commands are Bdsh builtins. Type " 142 "'help commands' [Enter] to see the list of Bdsh builtin commands. " 143 "Other commands are external executables located in the /app and " 144 "/srv directories. Type 'ls /app' [Enter] and 'ls /srv' [Enter] " 145 "to see their list. You can execute an external command simply " 146 "by entering its name (e.g. type 'tetris' [Enter]).\n\n"); 147 148 printf("HelenOS has virtual consoles (VCs). You can switch between " 149 "these using the F1-F11 keys.\n\n"); 150 151 printf("This is but a small glimpse of what you can do with HelenOS. " 152 "To learn more please point your browser to the HelenOS User's " 153 "Guide: http://trac.helenos.org/trac.fcgi/wiki/UsersGuide\n\n"); 154 } 155 156 int cmd_help(char *argv[]) 157 { 158 int rc = 0; 159 int argc; 160 int level = HELP_SHORT; 161 162 argc = cli_count_args(argv); 163 164 if (argc > 3) { 165 printf("\nToo many arguments to `%s', try:\n", cmdname); 166 help_cmd_help(HELP_SHORT); 167 return CMD_FAILURE; 168 } 169 170 if (argc == 3) { 171 if (!str_cmp("extended", argv[2])) 172 level = HELP_LONG; 173 else 174 level = HELP_SHORT; 175 } 176 177 if (argc > 1) { 178 rc = is_mod_or_builtin(argv[1]); 179 switch (rc) { 180 case HELP_IS_RUBBISH: 181 printf("Invalid topic %s\n", argv[1]); 182 return CMD_FAILURE; 183 case HELP_IS_COMMANDS: 184 help_commands(); 185 return CMD_SUCCESS; 186 case HELP_IS_MODULE: 187 help_module(mod_switch, level); 188 return CMD_SUCCESS; 189 case HELP_IS_BUILTIN: 190 help_builtin(mod_switch, level); 191 return CMD_SUCCESS; 192 } 193 } 194 195 help_survival(); 156 196 157 197 return CMD_SUCCESS; -
uspace/app/bdsh/exec.c
r9a1d8ab r0b749a3 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/bdsh/scli.c
r9a1d8ab r0b749a3 89 89 exit(EXIT_FAILURE); 90 90 91 printf("Welcome to %s - %s\nType `help' at any time for usage information.\n",92 progname, PACKAGE_STRING);93 94 91 while (!cli_quit) { 95 92 get_input(&usr); -
uspace/app/getterm/Makefile
r9a1d8ab r0b749a3 34 34 SOURCES = \ 35 35 getterm.c \ 36 version.c 36 version.c \ 37 welcome.c 37 38 38 39 include $(USPACE_PREFIX)/Makefile.common -
uspace/app/getterm/getterm.c
r9a1d8ab r0b749a3 41 41 #include <task.h> 42 42 #include <str_error.h> 43 #include <errno.h> 43 44 #include "version.h" 45 #include "welcome.h" 44 46 45 47 #define APP_NAME "getterm" … … 47 49 static void usage(void) 48 50 { 49 printf("Usage: %s <terminal> <path>\n", APP_NAME);51 printf("Usage: %s <terminal> [-w] <command> [<arguments...>]\n", APP_NAME); 50 52 } 51 53 … … 72 74 } 73 75 74 static task_id_t spawn(const char *fname)75 {76 const char *args[2];77 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)85 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,86 str_error(err));87 88 return id;89 }90 91 76 int main(int argc, char *argv[]) 92 77 { 93 if (argc < 3) { 78 int rc; 79 task_exit_t texit; 80 int retval; 81 task_id_t id; 82 char *fname, *term; 83 char **cmd_args; 84 bool print_wmsg; 85 86 argv++; 87 argc--; 88 if (argc < 1) { 94 89 usage(); 95 90 return -1; 96 91 } 92 93 if (str_cmp(*argv, "-w") == 0) { 94 print_wmsg = true; 95 argv++; 96 argc--; 97 } else { 98 print_wmsg = false; 99 } 100 101 if (argc < 2) { 102 usage(); 103 return -1; 104 } 105 106 term = *argv++; 107 fname = *argv; 108 cmd_args = argv; 97 109 98 reopen(&stdin, 0, argv[1], O_RDONLY, "r");99 reopen(&stdout, 1, argv[1], O_WRONLY, "w");100 reopen(&stderr, 2, argv[1], O_WRONLY, "w");110 reopen(&stdin, 0, term, O_RDONLY, "r"); 111 reopen(&stdout, 1, term, O_WRONLY, "w"); 112 reopen(&stderr, 2, term, O_WRONLY, "w"); 101 113 102 114 /* … … 115 127 return -4; 116 128 117 version_print( argv[1]);118 task_id_t id = spawn(argv[2]);119 120 if (id != 0) { 121 task_exit_t texit;122 int retval;123 task_wait(id, &texit, &retval);124 125 return 0;129 version_print(term); 130 if (print_wmsg) 131 welcome_msg_print(); 132 133 rc = task_spawnv(&id, fname, (const char * const *) cmd_args); 134 if (rc != EOK) { 135 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname, 136 str_error(rc)); 137 return -5; 126 138 } 127 128 return -5; 139 140 rc = task_wait(id, &texit, &retval); 141 if (rc != EOK) { 142 printf("%s: Error waiting for %s (%s)\n", APP_NAME, fname, 143 str_error(rc)); 144 return -6; 145 } 146 147 return 0; 129 148 } 130 149 -
uspace/app/getterm/version.c
r9a1d8ab r0b749a3 61 61 printf("HelenOS release %s (%s)%s%s\n", release, name, revision, timestamp); 62 62 printf("Running on %s (%s)\n", arch, term); 63 printf("Copyright (c) 2001-20 09HelenOS project\n\n");63 printf("Copyright (c) 2001-2010 HelenOS project\n\n"); 64 64 } 65 65 -
uspace/app/getterm/welcome.h
r9a1d8ab r0b749a3 1 1 /* 2 * Copyright (c) 20 09 Lukas Mejdrech2 * Copyright (c) 2010 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 /** @addtogroup net29 /** @addtogroup getterm 30 30 * @{ 31 31 */ 32 /** 33 * @file 34 */ 32 35 33 #ifndef __SELF_TEST_H__34 #define __SELF_TEST_H__36 #ifndef WELCOME_H__ 37 #define WELCOME_H__ 35 38 36 extern int self_test(void);39 extern void welcome_msg_print(void); 37 40 38 41 #endif -
uspace/app/init/init.c
r9a1d8ab r0b749a3 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; … … 188 185 189 186 /* Wait for the input device to be ready */ 190 dev _handle_t handle;187 devmap_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)); 204 } 205 206 static void getterm(const char *dev, const char *app) 207 { 208 const char *argv[4]; 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 } 200 } 201 202 static void getterm(const char *dev, const char *app, bool wmsg) 203 { 209 204 char term[DEVMAP_NAME_MAXLEN]; 210 205 int rc; … … 215 210 216 211 /* Wait for the terminal device to be ready */ 217 dev _handle_t handle;212 devmap_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 if (wmsg) { 221 rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, "-w", term, 222 app, NULL); 223 if (rc != EOK) { 224 printf("%s: Error spawning %s -w %s %s (%s)\n", NAME, 225 APP_GETTERM, term, app, str_error(rc)); 226 } 227 } else { 228 rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, term, app, 229 NULL); 230 if (rc != EOK) { 231 printf("%s: Error spawning %s %s %s (%s)\n", NAME, 232 APP_GETTERM, term, app, str_error(rc)); 233 } 234 } 232 235 } 233 236 … … 277 280 srv_start("/srv/adb_ms"); 278 281 srv_start("/srv/char_ms"); 282 srv_start("/srv/s3c24ts"); 279 283 280 284 spawn("/srv/fb"); … … 301 305 #endif 302 306 303 getterm("term/vc0", "/app/bdsh"); 304 getterm("term/vc1", "/app/bdsh"); 305 getterm("term/vc2", "/app/bdsh"); 306 getterm("term/vc3", "/app/bdsh"); 307 getterm("term/vc4", "/app/bdsh"); 308 getterm("term/vc5", "/app/bdsh"); 309 getterm("term/vc6", "/app/klog"); 307 getterm("term/vc0", "/app/bdsh", true); 308 getterm("term/vc1", "/app/bdsh", false); 309 getterm("term/vc2", "/app/bdsh", false); 310 getterm("term/vc3", "/app/bdsh", false); 311 getterm("term/vc4", "/app/bdsh", false); 312 getterm("term/vc5", "/app/bdsh", false); 313 getterm("term/vc6", "/app/klog", false); 314 getterm("term/vc7", "/srv/devman", false); 310 315 311 316 return 0; -
uspace/app/mkfat/mkfat.c
r9a1d8ab r0b749a3 85 85 struct fat_params *par); 86 86 static int fat_blocks_write(struct fat_params const *par, 87 dev _handle_t handle);87 devmap_handle_t handle); 88 88 static void fat_bootsec_create(struct fat_params const *par, struct fat_bs *bs); 89 89 … … 95 95 int rc; 96 96 char *dev_path; 97 dev _handle_t handle;97 devmap_handle_t handle; 98 98 size_t block_size; 99 99 char *endptr; … … 234 234 235 235 /** Create file system with the given parameters. */ 236 static int fat_blocks_write(struct fat_params const *par, dev _handle_t handle)236 static int fat_blocks_write(struct fat_params const *par, devmap_handle_t handle) 237 237 { 238 238 aoff64_t addr; -
uspace/app/netecho/Makefile
r9a1d8ab r0b749a3 29 29 30 30 USPACE_PREFIX = ../.. 31 LIBS = $(LIBSOCKET_PREFIX)/libsocket.a32 EXTRA_CFLAGS = -I$(LIBSOCKET_PREFIX)/include31 LIBS = 32 EXTRA_CFLAGS = 33 33 BINARY = netecho 34 34 -
uspace/app/netecho/netecho.c
r9a1d8ab r0b749a3 28 28 29 29 /** @addtogroup netecho 30 * 30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * 35 * 34 * Network echo application. 35 * Answers received packets. 36 36 */ 37 37 … … 42 42 #include <arg_parse.h> 43 43 44 #include <in.h> 45 #include <in6.h> 46 #include <inet.h> 47 #include <socket.h> 48 #include <net_err.h> 49 #include <socket_parse.h> 44 #include <net/in.h> 45 #include <net/in6.h> 46 #include <net/inet.h> 47 #include <net/socket.h> 48 #include <net/socket_parse.h> 50 49 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 * 108 uint8_t *address_start; 123 109 int socket_id; 124 110 int listening_id; 125 char * 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 if (rc != EOK) 176 return rc; 177 } else if (str_lcmp(argv[index] + 2, "family=", 7) == 0) { 178 rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family); 179 if (rc != EOK) 180 return rc; 181 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) { 145 182 echo_print_help(); 146 183 return EOK; 147 break; 148 case 'p': 149 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0)); 184 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) { 185 rc = arg_parse_int(argc, argv, &index, &value, 7); 186 if (rc != EOK) 187 return rc; 150 188 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)); 189 } else if (str_lcmp(argv[index] + 2, "reply=", 6) == 0) { 190 rc = arg_parse_string(argc, argv, &index, &reply, 8); 191 if (rc != EOK) 192 return rc; 193 } else if (str_lcmp(argv[index] + 2, "size=", 5) == 0) { 194 rc = arg_parse_int(argc, argv, &index, &value, 7); 195 if (rc != EOK) 196 return rc; 157 197 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)); 198 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) { 199 rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type); 200 if (rc != EOK) 201 return rc; 161 202 type = (sock_type_t) value; 162 break; 163 case 'v': 203 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) { 164 204 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: 205 } else { 196 206 echo_print_help(); 197 207 return EINVAL; 208 } 209 break; 210 default: 211 echo_print_help(); 212 return EINVAL; 198 213 } 199 } else{214 } else { 200 215 echo_print_help(); 201 216 return EINVAL; … … 204 219 205 220 // check the buffer size 206 if (size <= 0){221 if (size <= 0) { 207 222 fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size); 208 223 size = 1024; … … 210 225 // size plus the terminating null (\0) 211 226 data = (char *) malloc(size + 1); 212 if (! data){227 if (!data) { 213 228 fprintf(stderr, "Failed to allocate receive buffer.\n"); 214 229 return ENOMEM; … … 220 235 // prepare the address buffer 221 236 bzero(address_data, max_length); 222 switch (family){223 224 225 226 227 228 229 230 231 232 233 234 235 237 switch (family) { 238 case PF_INET: 239 address_in->sin_family = AF_INET; 240 address_in->sin_port = htons(port); 241 addrlen = sizeof(struct sockaddr_in); 242 break; 243 case PF_INET6: 244 address_in6->sin6_family = AF_INET6; 245 address_in6->sin6_port = htons(port); 246 addrlen = sizeof(struct sockaddr_in6); 247 break; 248 default: 249 fprintf(stderr, "Protocol family is not supported\n"); 250 return EAFNOSUPPORT; 236 251 } 237 252 238 253 // get a listening socket 239 254 listening_id = socket(family, type, 0); 240 if (listening_id < 0){255 if (listening_id < 0) { 241 256 socket_print_error(stderr, listening_id, "Socket create: ", "\n"); 242 257 return listening_id; … … 244 259 245 260 // if the stream socket is used 246 if (type == SOCK_STREAM){261 if (type == SOCK_STREAM) { 247 262 // check the backlog 248 if (backlog <= 0){263 if (backlog <= 0) { 249 264 fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size); 250 265 backlog = 3; 251 266 } 252 267 // 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; 268 rc = listen(listening_id, backlog); 269 if (rc != EOK) { 270 socket_print_error(stderr, rc, "Socket listen: ", "\n"); 271 return rc; 256 272 } 257 273 } 258 274 259 275 // 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){ 276 rc = bind(listening_id, address, addrlen); 277 if (rc != EOK) { 278 socket_print_error(stderr, rc, "Socket bind: ", "\n"); 279 return rc; 280 } 281 282 if (verbose) 266 283 printf("Socket %d listenning at %d\n", listening_id, port); 267 }268 284 269 285 socket_id = listening_id; … … 271 287 // do count times 272 288 // or indefinitely if set to a negative value 273 while (count){289 while (count) { 274 290 275 291 addrlen = max_length; 276 if (type == SOCK_STREAM){292 if (type == SOCK_STREAM) { 277 293 // acceept a socket if the stream socket is used 278 294 socket_id = accept(listening_id, address, &addrlen); 279 if (socket_id <= 0){295 if (socket_id <= 0) { 280 296 socket_print_error(stderr, socket_id, "Socket accept: ", "\n"); 281 } else{282 if (verbose){297 } else { 298 if (verbose) 283 299 printf("Socket %d accepted\n", socket_id); 284 }285 300 } 286 301 } 287 302 288 303 // if the datagram socket is used or the stream socked was accepted 289 if (socket_id > 0){304 if (socket_id > 0) { 290 305 291 306 // receive an echo request 292 307 value = recvfrom(socket_id, data, size, 0, address, &addrlen); 293 if (value < 0){308 if (value < 0) { 294 309 socket_print_error(stderr, value, "Socket receive: ", "\n"); 295 } else{310 } else { 296 311 length = (size_t) value; 297 if (verbose){312 if (verbose) { 298 313 // print the header 299 314 300 315 // get the source port and prepare the address buffer 301 316 address_start = NULL; 302 switch (address->sa_family){303 304 305 306 307 308 309 310 311 312 317 switch (address->sa_family) { 318 case AF_INET: 319 port = ntohs(address_in->sin_port); 320 address_start = (uint8_t *) &address_in->sin_addr.s_addr; 321 break; 322 case AF_INET6: 323 port = ntohs(address_in6->sin6_port); 324 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr; 325 break; 326 default: 327 fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family); 313 328 } 314 329 // 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{ 330 if (address_start) { 331 rc = inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)); 332 if (rc != EOK) { 333 fprintf(stderr, "Received address error %d\n", rc); 334 } else { 319 335 data[length] = '\0'; 320 336 printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data); … … 324 340 325 341 // 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 342 rc = sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen); 343 if (rc != EOK) 344 socket_print_error(stderr, rc, "Socket send: ", "\n"); 330 345 } 331 346 332 347 // 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 }348 if (type == SOCK_STREAM) { 349 rc = closesocket(socket_id); 350 if (rc != EOK) 351 socket_print_error(stderr, rc, "Close socket: ", "\n"); 337 352 } 338 353 … … 340 355 341 356 // decrease the count if positive 342 if (count > 0){343 -- count;344 if (verbose){357 if (count > 0) { 358 count--; 359 if (verbose) 345 360 printf("Waiting for next %d packet(s)\n", count); 346 } 347 } 348 } 349 350 if(verbose){ 361 } 362 } 363 364 if (verbose) 351 365 printf("Closing the socket\n"); 352 }353 366 354 367 // 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){ 368 rc = closesocket(listening_id); 369 if (rc != EOK) { 370 socket_print_error(stderr, rc, "Close socket: ", "\n"); 371 return rc; 372 } 373 374 if (verbose) 361 375 printf("Exiting\n"); 362 }363 376 364 377 return EOK; -
uspace/app/netecho/print_error.c
r9a1d8ab r0b749a3 28 28 29 29 /** @addtogroup net_app 30 * 30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * 34 * Generic application error printing functions implementation. 35 35 */ 36 37 #include <stdio.h>38 39 #include <icmp_codes.h>40 #include <socket_errno.h>41 36 42 37 #include "print_error.h" 43 38 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 } 39 #include <stdio.h> 40 #include <errno.h> 41 42 #include <net/icmp_codes.h> 43 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); 92 98 } 99 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
r9a1d8ab r0b749a3 28 28 29 29 /** @addtogroup net_app 30 * 30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * 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 * @return A value indicating whether the error code may be an ICMP error code. 43 46 */ 44 #define IS_ICMP_ERROR(error_code) 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 * @return 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/nettest1/Makefile
r9a1d8ab r0b749a3 29 29 30 30 USPACE_PREFIX = ../.. 31 LIBS = $(LIBSOCKET_PREFIX)/libsocket.a32 EXTRA_CFLAGS = -I$(LIBSOCKET_PREFIX)/include31 LIBS = 32 EXTRA_CFLAGS = 33 33 BINARY = nettest1 34 34 -
uspace/app/nettest1/nettest.c
r9a1d8ab r0b749a3 28 28 29 29 /** @addtogroup nettest 30 * 30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * 34 * Networking test support functions implementation. 35 35 */ 36 36 37 37 #include <stdio.h> 38 39 #include <socket.h> 40 #include <net_err.h> 38 #include <net/socket.h> 41 39 42 40 #include "nettest.h" 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 * @return EOK on success. 52 * @return 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 * @return EOK on success. 83 * @return 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 * @return EOK on success. 117 * @return 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 * @return EOK on success. 153 * @return 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 * @return EOK on success. 193 * @return 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 * @return EOK on success. 235 * @return 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
r9a1d8ab r0b749a3 28 28 29 29 /** @addtogroup nettest 30 * 30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * 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 #include < socket.h>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
r9a1d8ab r0b749a3 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> … … 42 45 #include <arg_parse.h> 43 46 44 #include <in.h> 45 #include <in6.h> 46 #include <inet.h> 47 #include <socket.h> 48 #include <net_err.h> 49 #include <socket_parse.h> 50 51 #include "nettest.h" 52 #include "print_error.h" 53 54 /** Echo module name. 55 */ 47 #include <net/in.h> 48 #include <net/in6.h> 49 #include <net/inet.h> 50 #include <net/socket.h> 51 #include <net/socket_parse.h> 52 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 struct sockaddr_in address_in; 290 struct sockaddr_in6 address_in6; 291 uint8_t *address_start; 292 293 int *socket_ids; 104 294 int index; 105 295 struct timeval time_before; 106 296 struct timeval time_after; 107 297 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{ 298 int rc; 299 300 sockets = 10; 301 messages = 10; 302 port = 7; 303 304 /* 305 * Parse the command line arguments. Stop before the last argument 306 * if it does not start with dash ('-') 307 */ 308 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) { 309 /* Options should start with dash ('-') */ 310 if (argv[index][0] == '-') { 311 rc = nettest1_parse_opt(argc, argv, &index); 312 if (rc != EOK) 313 return rc; 314 } else { 172 315 nettest1_print_help(); 173 316 return EINVAL; … … 175 318 } 176 319 177 / / if not before the last argument containing the address178 if (index >= argc){320 /* If not before the last argument containing the address */ 321 if (index >= argc) { 179 322 printf("Command line error: missing address\n"); 180 323 nettest1_print_help(); … … 182 325 } 183 326 184 // prepare the address buffer 185 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); 327 /* Prepare the address buffer */ 328 329 switch (family) { 330 case PF_INET: 331 address_in.sin_family = AF_INET; 332 address_in.sin_port = htons(port); 333 address = (struct sockaddr *) &address_in; 334 addrlen = sizeof(address_in); 335 address_start = (uint8_t *) &address_in.sin_addr.s_addr; 336 break; 337 case PF_INET6: 338 address_in6.sin6_family = AF_INET6; 339 address_in6.sin6_port = htons(port); 340 address = (struct sockaddr *) &address_in6; 341 addrlen = sizeof(address_in6); 342 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr; 343 break; 344 default: 345 fprintf(stderr, "Address family is not supported\n"); 346 return EAFNOSUPPORT; 347 } 348 349 /* Parse the last argument which should contain the address */ 350 rc = inet_pton(family, argv[argc - 1], address_start); 351 if (rc != EOK) { 352 fprintf(stderr, "Address parse error %d\n", rc); 353 return rc; 354 } 355 356 /* Check data buffer size */ 357 if (size <= 0) { 358 fprintf(stderr, "Data buffer size too small (%d). Using 1024 " 359 "bytes instead.\n", size); 213 360 size = 1024; 214 361 } 215 362 216 // prepare the buffer 217 // size plus the terminating null (\0) 363 /* 364 * Prepare data buffer. Allocate size bytes plus one for the 365 * trailing null character. 366 */ 218 367 data = (char *) malloc(size + 1); 219 if (! data){368 if (!data) { 220 369 fprintf(stderr, "Failed to allocate data buffer.\n"); 221 370 return ENOMEM; 222 371 } 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); 372 nettest1_fill_buffer(data, size); 373 374 /* Check socket count */ 375 if (sockets <= 0) { 376 fprintf(stderr, "Socket count too small (%d). Using " 377 "2 instead.\n", sockets); 228 378 sockets = 2; 229 379 } 230 380 231 // prepare the socket buffer 232 // count plus the terminating null (\0) 381 /* 382 * Prepare socket buffer. Allocate count fields plus the terminating 383 * null (\0). 384 */ 233 385 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 234 if (! socket_ids){386 if (!socket_ids) { 235 387 fprintf(stderr, "Failed to allocate receive buffer.\n"); 236 388 return ENOMEM; 237 389 } 238 socket_ids[sockets] = NULL;239 240 if (verbose){390 socket_ids[sockets] = 0; 391 392 if (verbose) 241 393 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){ 394 395 rc = gettimeofday(&time_before, NULL); 396 if (rc != EOK) { 397 fprintf(stderr, "Get time of day error %d\n", rc); 398 return rc; 399 } 400 401 nettest1_test(socket_ids, 1, 1); 402 nettest1_test(socket_ids, 1, messages); 403 nettest1_test(socket_ids, sockets, 1); 404 nettest1_test(socket_ids, sockets, messages); 405 406 rc = gettimeofday(&time_after, NULL); 407 if (rc != EOK) { 408 fprintf(stderr, "Get time of day error %d\n", rc); 409 return rc; 410 } 411 412 printf("Tested in %d microseconds\n", tv_sub(&time_after, 413 &time_before)); 414 415 if (verbose) 370 416 printf("Exiting\n"); 371 }372 417 373 418 return EOK; 374 419 } 375 420 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 421 417 422 /** @} -
uspace/app/nettest2/Makefile
r9a1d8ab r0b749a3 29 29 30 30 USPACE_PREFIX = ../.. 31 LIBS = $(LIBSOCKET_PREFIX)/libsocket.a32 EXTRA_CFLAGS = -I$(LIBSOCKET_PREFIX)/include31 LIBS = 32 EXTRA_CFLAGS = 33 33 BINARY = nettest2 34 34 -
uspace/app/nettest2/nettest2.c
r9a1d8ab r0b749a3 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 44 #include <in.h> 45 #include <in6.h> 46 #include <inet.h> 47 #include <socket.h> 48 #include <net_err.h> 49 #include <socket_parse.h> 50 51 #include "nettest.h" 52 #include "print_error.h" 53 54 /** Echo module name. 55 */ 46 #include <bool.h> 47 48 #include <net/in.h> 49 #include <net/in6.h> 50 #include <net/inet.h> 51 #include <net/socket.h> 52 #include <net/socket_parse.h> 53 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 struct sockaddr *address; 226 struct sockaddr_in address_in; 227 struct sockaddr_in6 address_in6; 97 228 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; 229 uint8_t *address_start; 230 231 int *socket_ids; 232 char *data; 104 233 int index; 105 234 struct timeval time_before; 106 235 struct timeval time_after; 107 236 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{ 237 int rc; 238 239 size = 28; 240 verbose = false; 241 type = SOCK_DGRAM; 242 sockets = 10; 243 messages = 10; 244 family = PF_INET; 245 port = 7; 246 247 /* 248 * Parse the command line arguments. 249 * 250 * Stop before the last argument if it does not start with dash ('-') 251 */ 252 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 253 (argv[index][0] == '-')); ++index) { 254 255 /* Options should start with dash ('-') */ 256 if (argv[index][0] == '-') { 257 rc = nettest2_parse_opt(argc, argv, &index); 258 if (rc != EOK) 259 return rc; 260 } else { 172 261 nettest2_print_help(); 173 262 return EINVAL; … … 175 264 } 176 265 177 / / if not before the last argument containing the address178 if (index >= argc){266 /* If not before the last argument containing the address */ 267 if (index >= argc) { 179 268 printf("Command line error: missing address\n"); 180 269 nettest2_print_help(); … … 182 271 } 183 272 184 // prepare the address buffer 185 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); 273 /* Prepare the address buffer */ 274 275 switch (family) { 276 case PF_INET: 277 address_in.sin_family = AF_INET; 278 address_in.sin_port = htons(port); 279 address = (struct sockaddr *) &address_in; 280 addrlen = sizeof(address_in); 281 address_start = (uint8_t *) &address_in.sin_addr.s_addr; 282 break; 283 case PF_INET6: 284 address_in6.sin6_family = AF_INET6; 285 address_in6.sin6_port = htons(port); 286 address = (struct sockaddr *) &address_in6; 287 addrlen = sizeof(address_in6); 288 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr; 289 break; 290 default: 291 fprintf(stderr, "Address family is not supported\n"); 292 return EAFNOSUPPORT; 293 } 294 295 /* Parse the last argument which should contain the address. */ 296 rc = inet_pton(family, argv[argc - 1], address_start); 297 if (rc != EOK) { 298 fprintf(stderr, "Address parse error %d\n", rc); 299 return rc; 300 } 301 302 /* Check data buffer size. */ 303 if (size <= 0) { 304 fprintf(stderr, "Data buffer size too small (%d). Using 1024 " 305 "bytes instead.\n", size); 213 306 size = 1024; 214 307 } 215 308 216 // prepare the buffer 217 // size plus terminating null (\0) 309 /* 310 * Prepare the buffer. Allocate size bytes plus one for terminating 311 * null character. 312 */ 218 313 data = (char *) malloc(size + 1); 219 if (! data){314 if (!data) { 220 315 fprintf(stderr, "Failed to allocate data buffer.\n"); 221 316 return ENOMEM; 222 317 } 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); 318 319 /* Fill buffer with a pattern. */ 320 nettest2_fill_buffer(data, size); 321 322 /* Check socket count. */ 323 if (sockets <= 0) { 324 fprintf(stderr, "Socket count too small (%d). Using " 325 "2 instead.\n", sockets); 228 326 sockets = 2; 229 327 } 230 328 231 // prepare the socket buffer 232 // count plus the terminating null (\0) 329 /* 330 * Prepare the socket buffer. 331 * Allocate count entries plus the terminating null (\0) 332 */ 233 333 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 234 if (! socket_ids){334 if (!socket_ids) { 235 335 fprintf(stderr, "Failed to allocate receive buffer.\n"); 236 336 return ENOMEM; 237 337 } 238 socket_ids[sockets] = NULL;239 240 if (verbose){338 socket_ids[sockets] = 0; 339 340 if (verbose) 241 341 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){ 342 343 rc = sockets_create(verbose, socket_ids, sockets, family, type); 344 if (rc != EOK) 345 return rc; 346 347 if (type == SOCK_STREAM) { 348 rc = sockets_connect(verbose, socket_ids, sockets, 349 address, addrlen); 350 if (rc != EOK) 351 return rc; 352 } 353 354 if (verbose) 251 355 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){ 356 357 rc = gettimeofday(&time_before, NULL); 358 if (rc != EOK) { 359 fprintf(stderr, "Get time of day error %d\n", rc); 360 return rc; 361 } 362 363 rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, 364 &addrlen, data, size, messages); 365 if (rc != EOK) 366 return rc; 367 368 rc = gettimeofday(&time_after, NULL); 369 if (rc != EOK) { 370 fprintf(stderr, "Get time of day error %d\n", rc); 371 return rc; 372 } 373 374 if (verbose) 267 375 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){ 376 377 printf("sendto + recvfrom tested in %d microseconds\n", 378 tv_sub(&time_after, &time_before)); 379 380 rc = gettimeofday(&time_before, NULL); 381 if (rc != EOK) { 382 fprintf(stderr, "Get time of day error %d\n", rc); 383 return rc; 384 } 385 386 rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen, 387 data, size, messages); 388 if (rc != EOK) 389 return rc; 390 391 rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, 392 data, size, messages); 393 if (rc != EOK) 394 return rc; 395 396 rc = gettimeofday(&time_after, NULL); 397 if (rc != EOK) { 398 fprintf(stderr, "Get time of day error %d\n", rc); 399 return rc; 400 } 401 402 if (verbose) 286 403 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){ 404 405 printf("sendto, recvfrom tested in %d microseconds\n", 406 tv_sub(&time_after, &time_before)); 407 408 rc = sockets_close(verbose, socket_ids, sockets); 409 if (rc != EOK) 410 return rc; 411 412 if (verbose) 294 413 printf("\nExiting\n"); 295 }296 414 297 415 return EOK; 298 416 } 299 417 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 418 /** @} 342 419 */ -
uspace/app/ping/Makefile
r9a1d8ab r0b749a3 29 29 30 30 USPACE_PREFIX = ../.. 31 LIBS = $(LIBSOCKET_PREFIX)/libsocket.a32 EXTRA_CFLAGS = -I$(LIBSOCKET_PREFIX)/include31 LIBS = 32 EXTRA_CFLAGS = 33 33 BINARY = ping 34 34 -
uspace/app/ping/ping.c
r9a1d8ab r0b749a3 42 42 #include <ipc/services.h> 43 43 #include <str_error.h> 44 #include <errno.h> 44 45 #include <arg_parse.h> 45 46 46 #include <icmp_api.h> 47 #include <in.h> 48 #include <in6.h> 49 #include <inet.h> 50 #include <ip_codes.h> 51 #include <socket_errno.h> 52 #include <socket_parse.h> 47 #include <net/icmp_api.h> 48 #include <net/in.h> 49 #include <net/in6.h> 50 #include <net/inet.h> 51 #include <net/socket_parse.h> 52 #include <net/ip_codes.h> 53 53 54 54 #include "print_error.h" -
uspace/app/redir/redir.c
r9a1d8ab r0b749a3 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
r9a1d8ab r0b749a3 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; -
uspace/app/test_serial/Makefile
r9a1d8ab r0b749a3 29 29 30 30 USPACE_PREFIX = ../.. 31 EXTRA_CFLAGS = -Iinclude 32 LIBRARY = libsocket 31 BINARY = test_serial 33 32 34 33 SOURCES = \ 35 generic/socket_client.c \ 36 generic/socket_core.c \ 37 generic/socket_parse.c \ 38 generic/inet.c \ 39 generic/net_modules.c \ 40 generic/icmp_common.c \ 41 generic/icmp_api.c \ 42 packet/packet.c \ 43 packet/packet_client.c \ 44 packet/packet_server.c \ 45 adt/dynamic_fifo.c \ 46 adt/measured_strings.c \ 47 adt/char_map.c 34 test_serial.c 48 35 49 36 include $(USPACE_PREFIX)/Makefile.common -
uspace/app/trace/syscalls.c
r9a1d8ab r0b749a3 73 73 [SYS_PHYSMEM_MAP] = { "physmem_map", 4, V_ERRNO }, 74 74 [SYS_IOSPACE_ENABLE] = { "iospace_enable", 1, V_ERRNO }, 75 [SYS_ PREEMPT_CONTROL] = { "preempt_control", 1, V_ERRNO },75 [SYS_INTERRUPT_ENABLE] = { "interrupt_enable", 2, V_ERRNO }, 76 76 77 77 [SYS_SYSINFO_GET_TAG] = { "sysinfo_get_tag", 2, V_INTEGER },
Note:
See TracChangeset
for help on using the changeset viewer.