Changeset 03c971f in mainline for uspace/app
- Timestamp:
- 2013-08-15T14:20:16Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- bb2a5b2
- Parents:
- f2c19b0 (diff), 2921602 (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:
-
- 2 added
- 4 deleted
- 27 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/Makefile
rf2c19b0 r03c971f 29 29 30 30 USPACE_PREFIX = ../.. 31 LIBS = $(LIBBLOCK_PREFIX)/libblock.a $(LIBCLUI_PREFIX)/libclui.a \ 32 $(LIBFMTUTIL_PREFIX)/libfmtutil.a 33 EXTRA_CFLAGS = -I$(LIBBLOCK_PREFIX) -I$(LIBCLUI_PREFIX) \ 34 -I$(LIBFMTUTIL_PREFIX) -I. -Icmds/ -Icmds/builtins -Icmds/modules 31 LIBS = $(LIBCLUI_PREFIX)/libclui.a $(LIBFMTUTIL_PREFIX)/libfmtutil.a 32 EXTRA_CFLAGS = -I$(LIBCLUI_PREFIX) -I$(LIBFMTUTIL_PREFIX) \ 33 -I. -Icmds/ -Icmds/builtins -Icmds/modules 35 34 BINARY = bdsh 36 35 … … 40 39 cmds/modules/mkfile/mkfile.c \ 41 40 cmds/modules/rm/rm.c \ 42 cmds/modules/bdd/bdd.c \43 41 cmds/modules/cat/cat.c \ 44 42 cmds/modules/touch/touch.c \ -
uspace/app/bdsh/cmds/builtins/batch/batch.c
rf2c19b0 r03c971f 29 29 #include <stdio.h> 30 30 #include <stdlib.h> 31 #include <stdbool.h> 32 #include <errno.h> 31 33 #include "config.h" 32 34 #include "util.h" … … 44 46 if (level == HELP_SHORT) { 45 47 printf( 46 "\n batch [filename] \n"48 "\n batch [filename] [-c]\n" 47 49 " Issues commands stored in the file.\n" 48 50 " Each command must correspond to the single line in the file.\n\n"); … … 54 56 " separate groups of commands. There is no support for comments,\n" 55 57 " variables, recursion or other programming constructs - the `batch'\n" 56 " command is indeed very trivial.\n\n"); 58 " command is indeed very trivial.\n" 59 " If the filename is followed by -c, execution continues even if some\n" 60 " of the commands failed.\n\n"); 57 61 } 58 62 … … 65 69 { 66 70 unsigned int argc; 71 bool continue_despite_errors = false; 67 72 68 73 /* Count the arguments */ … … 72 77 printf("%s - no input file provided.\n", cmdname); 73 78 return CMD_FAILURE; 79 } 80 81 if (argc > 2) { 82 if (str_cmp(argv[2], "-c") == 0) 83 continue_despite_errors = true; 74 84 } 75 85 … … 99 109 rc = process_input(&fusr); 100 110 /* fusr->line was freed by process_input() */ 111 if ((rc != EOK) && continue_despite_errors) { 112 /* Mute the error. */ 113 rc = EOK; 114 } 101 115 } 102 116 if (rc == 0 && c != EOF) { -
uspace/app/bdsh/cmds/modules/cat/cat.c
rf2c19b0 r03c971f 63 63 static bool should_quit = false; 64 64 static bool dash_represents_stdin = false; 65 static unsigned int lineno = 0; 66 static bool number = false; 67 static bool last_char_was_newline = false; 65 68 66 69 static console_ctrl_t *console = NULL; … … 75 78 { "hex", no_argument, 0, 'x' }, 76 79 { "stdin", no_argument, 0, 's' }, 80 { "number", no_argument, 0, 'n' }, 77 81 { 0, 0, 0, 0 } 78 82 }; … … 95 99 " -m, --more Pause after each screen full\n" 96 100 " -x, --hex Print bytes as hex values\n" 97 " -s --stdin Treat `-' in file list as standard input\n" 101 " -s, --stdin Treat `-' in file list as standard input\n" 102 " -n, --number Number all output lines\n" 98 103 "Currently, %s is under development, some options don't work.\n", 99 104 cmdname, cmdname); … … 153 158 static void paged_char(wchar_t c) 154 159 { 160 if (last_char_was_newline && number) { 161 lineno++; 162 printf("%6u ", lineno); 163 } 155 164 putchar(c); 165 last_char_was_newline = c == '\n'; 156 166 if (paging_enabled) { 157 167 chars_remaining--; … … 306 316 should_quit = false; 307 317 console = console_init(stdin, stdout); 318 number = false; 319 lineno = 0; 320 /* This enables printing of the first number. */ 321 last_char_was_newline = true; 322 308 323 309 324 argc = cli_count_args(argv); 310 325 311 326 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 312 c = getopt_long(argc, argv, "xhvmH:t:b:s ", long_options, &opt_ind);327 c = getopt_long(argc, argv, "xhvmH:t:b:s:n", long_options, &opt_ind); 313 328 switch (c) { 314 329 case 'h': … … 347 362 dash_represents_stdin = true; 348 363 break; 364 case 'n': 365 number = true; 366 break; 349 367 } 350 368 } -
uspace/app/bdsh/cmds/modules/help/help.c
rf2c19b0 r03c971f 98 98 builtin_t *cmd; 99 99 module_t *mod; 100 unsigned int i;101 100 102 101 printf("\n Bdsh built-in commands:\n"); … … 104 103 105 104 /* First, show a list of built in commands that are available in this mode */ 106 for (cmd = builtins; cmd->name != NULL; cmd++ , i++) {105 for (cmd = builtins; cmd->name != NULL; cmd++) { 107 106 if (is_builtin_alias(cmd->name)) 108 107 printf(" %-16s\tAlias for `%s'\n", cmd->name, … … 112 111 } 113 112 114 i = 0;115 116 113 /* Now, show a list of module commands that are available in this mode */ 117 for (mod = modules; mod->name != NULL; mod++ , i++) {114 for (mod = modules; mod->name != NULL; mod++) { 118 115 if (is_module_alias(mod->name)) 119 116 printf(" %-16s\tAlias for `%s'\n", mod->name, -
uspace/app/bdsh/cmds/modules/modules.h
rf2c19b0 r03c971f 50 50 #include "mkfile/entry.h" 51 51 #include "rm/entry.h" 52 #include "bdd/entry.h"53 52 #include "cat/entry.h" 54 53 #include "touch/entry.h" … … 74 73 #include "mkfile/mkfile_def.h" 75 74 #include "rm/rm_def.h" 76 #include "bdd/bdd_def.h"77 75 #include "cat/cat_def.h" 78 76 #include "touch/touch_def.h" -
uspace/app/bdsh/cmds/modules/pwd/pwd.c
rf2c19b0 r03c971f 55 55 } 56 56 57 memset(buff, 0, sizeof(buff));57 memset(buff, 0, PATH_MAX); 58 58 getcwd(buff, PATH_MAX); 59 59 -
uspace/app/bdsh/cmds/modules/rm/rm.c
rf2c19b0 r03c971f 99 99 if (NULL == (rm->nwd = (char *) malloc(PATH_MAX))) 100 100 return 0; 101 memset(rm->nwd, 0, sizeof(rm->nwd));101 memset(rm->nwd, 0, PATH_MAX); 102 102 103 103 if (NULL == (rm->owd = (char *) malloc(PATH_MAX))) 104 104 return 0; 105 memset(rm->owd, 0, sizeof(rm->owd));105 memset(rm->owd, 0, PATH_MAX); 106 106 107 107 if (NULL == (rm->cwd = (char *) malloc(PATH_MAX))) 108 108 return 0; 109 memset(rm->cwd, 0, sizeof(rm->cwd));109 memset(rm->cwd, 0, PATH_MAX); 110 110 111 111 chdir("."); … … 298 298 break; 299 299 } 300 memset(buff, 0, sizeof(buff));300 memset(buff, 0, len); 301 301 snprintf(buff, len, "%s", argv[i]); 302 302 -
uspace/app/bdsh/cmds/modules/sleep/sleep.c
rf2c19b0 r03c971f 67 67 uint64_t whole_seconds; 68 68 uint64_t frac_seconds; 69 c har *endptr;69 const char *endptr; 70 70 71 71 /* Check for whole seconds */ -
uspace/app/bdsh/exec.c
rf2c19b0 r03c971f 83 83 /* We now have n places to look for the command */ 84 84 for (i = 0; search_dir[i] != NULL; i++) { 85 memset(found, 0, sizeof(found));85 memset(found, 0, PATH_MAX); 86 86 snprintf(found, PATH_MAX, "%s/%s", search_dir[i], cmd); 87 87 if (-1 != try_access(found)) { -
uspace/app/binutils/intrusive.sh
rf2c19b0 r03c971f 96 96 # libposix function name redefinitons in one of the arm-specific files. 97 97 # 98 # Patch 9 99 # Libiberty does not trust our strncmp that is provided in libposix. 100 # That is because when cross-compiling, it cannot check how it behaves 101 # and assumes the worst. But then it clashes when linking (due to 102 # multiple definitions of ...) and this patch prevents compilation of 103 # liberty strncmp. 98 104 99 105 case "$1" in … … 157 163 # Patch libiberty configure script. 158 164 cat "$2/libiberty/configure.backup" | \ 159 # See Patch 1. 160 sed 's/^cross_compiling=no/cross_compiling=yes/g' \ 165 # See Patch 1 and 9. 166 sed -e 's/^cross_compiling=no/cross_compiling=yes/g' \ 167 -e 's/ac_cv_func_strncmp_works=no/ac_cv_func_strncmp_works=yes/g' \ 161 168 > "$2/libiberty/configure" 162 169 -
uspace/app/binutils/toolchain.sh
rf2c19b0 r03c971f 72 72 echo ' ld $LD_ARGS' 73 73 echo 'else' 74 CFLAGS="`echo " $3 " | \74 CFLAGS="`echo " $3 " | \ 75 75 sed 's/ -O[^ ]*//g' | \ 76 76 sed 's/ -W[^ ]*//g' | \ 77 77 sed 's/ -pipe//g' | \ 78 sed 's/ -g //g' | \78 sed 's/ -g[^ ]*//g' | \ 79 79 sed 's/ [ ]*/ /g'`" 80 80 echo ' CONFTEST="`echo \"$*\" | grep '\' conftest \''`"' -
uspace/app/blkdump/blkdump.c
rf2c19b0 r03c971f 1 1 /* 2 2 * Copyright (c) 2011 Martin Sucha 3 * Copyright (c) 201 0Jiri Svoboda3 * Copyright (c) 2013 Jiri Svoboda 4 4 * All rights reserved. 5 5 * … … 52 52 53 53 static void syntax_print(void); 54 static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size); 55 static int print_toc(void); 54 56 static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row); 57 58 static bool relative = false; 59 static service_id_t service_id; 55 60 56 61 int main(int argc, char **argv) … … 59 64 int rc; 60 65 char *dev_path; 61 service_id_t service_id;62 66 size_t block_size; 63 67 char *endptr; … … 65 69 aoff64_t block_count = 1; 66 70 aoff64_t dev_nblocks; 67 uint8_t *data; 68 size_t data_offset; 69 aoff64_t current; 70 aoff64_t limit; 71 bool relative = false; 71 bool toc = false; 72 72 73 73 if (argc < 2) { … … 79 79 --argc; ++argv; 80 80 81 if (str_cmp(*argv, "--toc") == 0) { 82 --argc; ++argv; 83 toc = true; 84 goto devname; 85 } 86 81 87 if (str_cmp(*argv, "--relative") == 0) { 82 88 --argc; ++argv; … … 120 126 } 121 127 128 devname: 122 129 if (argc != 1) { 123 130 printf(NAME ": Error, unexpected argument.\n"); … … 153 160 printf("Device %s has %" PRIuOFF64 " blocks, %" PRIuOFF64 " bytes each\n", dev_path, dev_nblocks, (aoff64_t) block_size); 154 161 162 if (toc) 163 rc = print_toc(); 164 else 165 rc = print_blocks(block_offset, block_count, block_size); 166 167 block_fini(service_id); 168 169 return rc; 170 } 171 172 static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size) 173 { 174 uint8_t *data; 175 aoff64_t current; 176 aoff64_t limit; 177 size_t data_offset; 178 int rc; 179 155 180 data = malloc(block_size); 156 181 if (data == NULL) { 157 182 printf(NAME ": Error allocating data buffer of %" PRIuOFF64 " bytes", (aoff64_t) block_size); 158 block_fini(service_id);159 183 return 3; 160 184 } 161 185 162 186 limit = block_offset + block_count; 163 187 for (current = block_offset; current < limit; current++) { … … 168 192 return 3; 169 193 } 170 194 171 195 printf("---- Block %" PRIuOFF64 " (at %" PRIuOFF64 ") ----\n", current, current*block_size); 172 196 173 197 for (data_offset = 0; data_offset < block_size; data_offset += 16) { 174 198 if (relative) { … … 183 207 printf("\n"); 184 208 } 185 209 186 210 free(data); 187 188 block_fini(service_id); 211 return 0; 212 } 213 214 static int print_toc(void) 215 { 216 toc_block_t *toc; 217 218 toc = block_get_toc(service_id, 0); 219 if (toc == NULL) 220 return 1; 221 222 printf("TOC size: %" PRIu16 " bytes\n", toc->size); 223 printf("First session: %" PRIu8 "\n", toc->first_session); 224 printf("Last_session: %" PRIu8 "\n", toc->last_session); 189 225 190 226 return 0; -
uspace/app/dnscfg/dnscfg.c
rf2c19b0 r03c971f 49 49 static void print_syntax(void) 50 50 { 51 printf("syntax:\n"); 52 printf("\t" NAME " set-ns <server-addr>\n"); 53 printf("\t" NAME " unset-ns\n"); 51 printf("Syntax:\n"); 52 printf("\t%s get-ns\n", NAME); 53 printf("\t%s set-ns <server-addr>\n", NAME); 54 printf("\t%s unset-ns\n", NAME); 54 55 } 55 56 56 57 static int dnscfg_set_ns(int argc, char *argv[]) 57 58 { 58 char *srv_addr;59 inet_addr_t addr;60 int rc;61 62 59 if (argc < 1) { 63 printf( NAME ": Missing arguments.\n");60 printf("%s: Missing arguments.\n", NAME); 64 61 print_syntax(); 65 62 return EINVAL; 66 63 } 67 64 68 65 if (argc > 1) { 69 printf( NAME ": Too many arguments.\n");66 printf("%s: Too many arguments.\n", NAME); 70 67 print_syntax(); 71 68 return EINVAL; 72 69 } 73 74 srv_addr = argv[0]; 75 76 rc = inet_addr_parse(srv_addr, &addr); 70 71 char *srv_addr = argv[0]; 72 73 inet_addr_t addr; 74 int rc = inet_addr_parse(srv_addr, &addr); 75 77 76 if (rc != EOK) { 78 printf( NAME ": Invalid address format '%s'.\n", srv_addr);79 return EINVAL;77 printf("%s: Invalid address format '%s'.\n", NAME, srv_addr); 78 return rc; 80 79 } 81 80 82 81 rc = dnsr_set_srvaddr(&addr); 83 82 if (rc != EOK) { 84 printf( NAME ": Failed settingserver address '%s' (%s)\n",85 srv_addr, str_error(rc));86 return EIO;83 printf("%s: Failed setting nameserver address '%s' (%s)\n", 84 NAME, srv_addr, str_error(rc)); 85 return rc; 87 86 } 88 87 89 88 return EOK; 90 89 } 91 90 92 static int dnscfg_unset_ns( int argc, char *argv[])91 static int dnscfg_unset_ns(void) 93 92 { 94 93 inet_addr_t addr; 95 int rc; 96 97 if (argc > 0) { 98 printf(NAME ": Too many arguments.\n"); 99 print_syntax(); 100 return EINVAL; 94 inet_addr_any(&addr); 95 96 int rc = dnsr_set_srvaddr(&addr); 97 if (rc != EOK) { 98 printf("%s: Failed unsetting server address (%s)\n", 99 NAME, str_error(rc)); 100 return rc; 101 101 } 102 103 addr.ipv4 = 0; 104 rc = dnsr_set_srvaddr(&addr); 105 if (rc != EOK) { 106 printf(NAME ": Failed unsetting server address (%s)\n", 107 str_error(rc)); 108 return EIO; 109 } 110 102 111 103 return EOK; 112 104 } … … 115 107 { 116 108 inet_addr_t addr; 117 char *addr_str; 118 int rc; 119 120 rc = dnsr_get_srvaddr(&addr); 109 int rc = dnsr_get_srvaddr(&addr); 121 110 if (rc != EOK) { 122 printf( NAME ": Failed getting DNS server address.\n");111 printf("%s: Failed getting DNS server address.\n", NAME); 123 112 return rc; 124 113 } 125 114 115 char *addr_str; 126 116 rc = inet_addr_format(&addr, &addr_str); 127 117 if (rc != EOK) { 128 printf( NAME ": Out of memory.\n");118 printf("%s: Out of memory.\n", NAME); 129 119 return rc; 130 120 } 131 132 printf(" Server: %s\n", addr_str);121 122 printf("Nameserver: %s\n", addr_str); 133 123 free(addr_str); 134 124 return EOK; … … 137 127 int main(int argc, char *argv[]) 138 128 { 139 int rc; 140 141 if (argc < 2) { 142 rc = dnscfg_print(); 143 if (rc != EOK) 144 return 1; 145 return 0; 146 } 147 148 if (str_cmp(argv[1], "set-ns") == 0) { 149 rc = dnscfg_set_ns(argc - 2, argv + 2); 150 if (rc != EOK) 151 return 1; 152 } else if (str_cmp(argv[1], "unset-ns") == 0) { 153 rc = dnscfg_unset_ns(argc - 2, argv + 2); 154 if (rc != EOK) 155 return 1; 156 } else { 157 printf(NAME ": Unknown command '%s'.\n", argv[1]); 129 if ((argc < 2) || (str_cmp(argv[1], "get-ns") == 0)) 130 return dnscfg_print(); 131 else if (str_cmp(argv[1], "set-ns") == 0) 132 return dnscfg_set_ns(argc - 2, argv + 2); 133 else if (str_cmp(argv[1], "unset-ns") == 0) 134 return dnscfg_unset_ns(); 135 else { 136 printf("%s: Unknown command '%s'.\n", NAME, argv[1]); 158 137 print_syntax(); 159 138 return 1; 160 139 } 161 162 140 163 141 return 0; 164 142 } -
uspace/app/dnsres/dnsres.c
rf2c19b0 r03c971f 36 36 #include <inet/addr.h> 37 37 #include <inet/dnsr.h> 38 #include <net/socket_codes.h> 38 39 #include <stdio.h> 39 40 #include <stdlib.h> 40 41 41 #define NAME "dnsres"42 #define NAME "dnsres" 42 43 43 44 static void print_syntax(void) 44 45 { 45 printf(" syntax: " NAME " <host-name>\n");46 printf("Syntax: %s [-4|-6] <host-name>\n", NAME); 46 47 } 47 48 48 49 int main(int argc, char *argv[]) 49 50 { 50 int rc; 51 dnsr_hostinfo_t *hinfo; 52 char *hname; 53 char *saddr; 54 55 if (argc != 2) { 51 if ((argc < 2) || (argc > 3)) { 56 52 print_syntax(); 57 53 return 1; 58 54 } 59 60 hname = argv[1]; 61 62 rc = dnsr_name2host(hname, &hinfo); 55 56 uint16_t af; 57 char *hname; 58 59 if (str_cmp(argv[1], "-4") == 0) { 60 if (argc < 3) { 61 print_syntax(); 62 return 1; 63 } 64 65 af = AF_INET; 66 hname = argv[2]; 67 } else if (str_cmp(argv[1], "-6") == 0) { 68 if (argc < 3) { 69 print_syntax(); 70 return 1; 71 } 72 73 af = AF_INET6; 74 hname = argv[2]; 75 } else { 76 af = 0; 77 hname = argv[1]; 78 } 79 80 dnsr_hostinfo_t *hinfo; 81 int rc = dnsr_name2host(hname, &hinfo, af); 63 82 if (rc != EOK) { 64 printf( NAME ": Error resolving '%s'.\n", argv[1]);65 return 1;83 printf("%s: Error resolving '%s'.\n", NAME, hname); 84 return rc; 66 85 } 67 86 87 char *saddr; 68 88 rc = inet_addr_format(&hinfo->addr, &saddr); 69 89 if (rc != EOK) { 70 90 dnsr_hostinfo_destroy(hinfo); 71 printf( NAME ": Out of memory.\n");72 return 1;91 printf("%s: Error formatting address.\n", NAME); 92 return rc; 73 93 } 74 94 75 95 printf("Host name: %s\n", hname); 96 76 97 if (str_cmp(hname, hinfo->cname) != 0) 77 98 printf("Canonical name: %s\n", hinfo->cname); 99 78 100 printf("Address: %s\n", saddr); 79 101 80 102 dnsr_hostinfo_destroy(hinfo); 81 103 free(saddr); 82 104 83 105 return 0; 84 106 } -
uspace/app/edit/search_impl.h
rf2c19b0 r03c971f 40 40 41 41 /** Search state */ 42 typedefstruct search {42 struct search { 43 43 /* Note: This structure is opaque for the user. */ 44 44 … … 49 49 void *client_data; 50 50 search_ops_t ops; 51 } search_t;51 }; 52 52 53 53 #endif -
uspace/app/edit/sheet_impl.h
rf2c19b0 r03c971f 40 40 41 41 /** Sheet */ 42 typedefstruct sheet {42 struct sheet { 43 43 /* Note: This structure is opaque for the user. */ 44 44 … … 48 48 49 49 list_t tags; 50 } sheet_t;50 }; 51 51 52 52 #endif -
uspace/app/inet/inet.c
rf2c19b0 r03c971f 44 44 #include <sys/types.h> 45 45 46 #define NAME "inet"46 #define NAME "inet" 47 47 48 48 static void print_syntax(void) … … 257 257 if (count > 0) 258 258 printf(" [Addr/Width] [Link-Name] [Addr-Name] [Def-MTU]\n"); 259 ainfo.name = linfo.name = astr = NULL; 259 ainfo.name = NULL; 260 linfo.name = NULL; 261 astr = NULL; 260 262 261 263 for (i = 0; i < count; i++) { … … 290 292 free(astr); 291 293 292 ainfo.name = linfo.name = astr = NULL; 294 ainfo.name = NULL; 295 linfo.name = NULL; 296 astr = NULL; 293 297 } 294 298 … … 329 333 printf(" [Dest/Width] [Router-Addr] [Route-Name]\n"); 330 334 331 srinfo.name = dest_str = router_str = NULL; 335 srinfo.name = NULL; 336 dest_str = NULL; 337 router_str = NULL; 332 338 333 339 for (i = 0; i < count; i++) { … … 360 366 free(router_str); 361 367 362 router_str = srinfo.name = dest_str = NULL; 368 router_str = NULL; 369 srinfo.name = NULL; 370 dest_str = NULL; 363 371 } 364 372 -
uspace/app/mkmfs/mkmfs.c
rf2c19b0 r03c971f 649 649 mark_bmap(ibmap_buf, i, FREE); 650 650 651 for (i = sb->first_data_zone + 1; i < sb->n_zones; ++i)651 for (i = 2; i < sb->n_zones - sb->first_data_zone; ++i) 652 652 mark_bmap(zbmap_buf, i, FREE); 653 653 -
uspace/app/netecho/netecho.c
rf2c19b0 r03c971f 225 225 uint8_t address_buf[sizeof(struct sockaddr_in6)]; 226 226 227 socklen_t addrlen ;227 socklen_t addrlen = sizeof(struct sockaddr_in6); 228 228 int socket_id; 229 229 ssize_t rcv_size; … … 240 240 if (type == SOCK_STREAM) { 241 241 /* Accept a socket if a stream socket is used */ 242 addrlen = sizeof(address_buf);243 242 if (verbose) 244 243 printf("accept()\n"); … … 280 279 case AF_INET6: 281 280 port = ntohs(address_in6->sin6_port); 282 address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;281 address_start = (uint8_t *) address_in6->sin6_addr.s6_addr; 283 282 break; 284 283 default: … … 380 379 address_in6.sin6_family = AF_INET6; 381 380 address_in6.sin6_port = htons(port); 381 address_in6.sin6_addr = in6addr_any; 382 382 address = (struct sockaddr *) &address_in6; 383 383 addrlen = sizeof(address_in6); -
uspace/app/nettest1/nettest.c
rf2c19b0 r03c971f 41 41 #include "print_error.h" 42 42 43 44 43 /** Creates new sockets. 45 44 * … … 52 51 * @return Other error codes as defined for the socket() function. 53 52 */ 54 int sockets_create(int verbose, int *socket_ids, int sockets, int family, sock_type_t type) 55 { 56 int index; 57 53 int sockets_create(int verbose, int *socket_ids, unsigned int sockets, 54 uint16_t family, sock_type_t type) 55 { 58 56 if (verbose) 59 57 printf("Create\t"); 60 61 fflush(stdout); 62 63 for ( index = 0; index < sockets; index++) {58 59 fflush(stdout); 60 61 for (unsigned int index = 0; index < sockets; index++) { 64 62 socket_ids[index] = socket(family, type, 0); 65 63 if (socket_ids[index] < 0) { 66 printf("Socket % d(%d) error:\n", index, socket_ids[index]);64 printf("Socket %u (%d) error:\n", index, socket_ids[index]); 67 65 socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n"); 68 66 return socket_ids[index]; 69 67 } 68 70 69 if (verbose) 71 70 print_mark(index); … … 83 82 * @return Other error codes as defined for the closesocket() function. 84 83 */ 85 int sockets_close(int verbose, int *socket_ids, int sockets) 86 { 87 int index; 88 int rc; 89 84 int sockets_close(int verbose, int *socket_ids, unsigned int sockets) 85 { 90 86 if (verbose) 91 87 printf("\tClose\t"); 92 93 fflush(stdout); 94 95 for ( index = 0; index < sockets; index++) {96 rc = closesocket(socket_ids[index]);88 89 fflush(stdout); 90 91 for (unsigned int index = 0; index < sockets; index++) { 92 int rc = closesocket(socket_ids[index]); 97 93 if (rc != EOK) { 98 printf("Socket % d(%d) error:\n", index, socket_ids[index]);94 printf("Socket %u (%d) error:\n", index, socket_ids[index]); 99 95 socket_print_error(stderr, rc, "Socket close: ", "\n"); 100 96 return rc; 101 97 } 98 102 99 if (verbose) 103 100 print_mark(index); … … 117 114 * @return Other error codes as defined for the connect() function. 118 115 */ 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 116 int sockets_connect(int verbose, int *socket_ids, unsigned int sockets, 117 struct sockaddr *address, socklen_t addrlen) 118 { 124 119 if (verbose) 125 120 printf("\tConnect\t"); … … 127 122 fflush(stdout); 128 123 129 for ( index = 0; index < sockets; index++) {130 rc = connect(socket_ids[index], address, addrlen);124 for (unsigned int index = 0; index < sockets; index++) { 125 int rc = connect(socket_ids[index], address, addrlen); 131 126 if (rc != EOK) { 132 127 socket_print_error(stderr, rc, "Socket connect: ", "\n"); 133 128 return rc; 134 129 } 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. 130 131 if (verbose) 132 print_mark(index); 133 } 134 135 return EOK; 136 } 137 138 /** Send data via sockets. 139 * 140 * @param[in] verbose Print out verbose information. 141 * @param[in] socket_ids Stored socket identifiers. 142 * @param[in] sockets Number of sockets. 143 * @param[in] address Destination host address to send data to. 144 * @param[in] addrlen Length of the destination address in bytes. 145 * @param[in] data Data to be sent. 146 * @param[in] size Size of the data in bytes. 147 * @param[in] messages Number of datagrams per socket to be sent. 148 * @param[in] type Socket type. 149 * 152 150 * @return EOK on success. 153 151 * @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 { 157 int index; 158 int message; 159 int rc; 160 152 * 153 */ 154 int sockets_sendto(int verbose, int *socket_ids, unsigned int sockets, 155 struct sockaddr *address, socklen_t addrlen, char *data, size_t size, 156 unsigned int messages, sock_type_t type) 157 { 161 158 if (verbose) 162 159 printf("\tSendto\t"); 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); 160 161 fflush(stdout); 162 163 for (unsigned int index = 0; index < sockets; index++) { 164 for (unsigned int message = 0; message < messages; message++) { 165 int rc; 166 167 switch (type) { 168 case SOCK_STREAM: 169 rc = send(socket_ids[index], data, size, 0); 170 break; 171 case SOCK_DGRAM: 172 rc = sendto(socket_ids[index], data, size, 0, address, addrlen); 173 break; 174 default: 175 rc = EINVAL; 176 } 177 169 178 if (rc != EOK) { 170 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 179 printf("Socket %u (%d), message %u error:\n", 180 index, socket_ids[index], message); 171 181 socket_print_error(stderr, rc, "Socket send: ", "\n"); 172 182 return rc; 173 183 } 174 184 } 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. 185 186 if (verbose) 187 print_mark(index); 188 } 189 190 return EOK; 191 } 192 193 /** Receive data via sockets. 194 * 195 * @param[in] verbose Print out verbose information. 196 * @param[in] socket_ids Stored socket identifiers. 197 * @param[in] sockets Number of sockets. 198 * @param[in] address Source host address of received datagrams. 199 * @param[in,out] addrlen Maximum length of the source address in bytes. 200 * The actual size of the source address is set. 201 * @param[out] data Received data. 202 * @param[in] size Maximum data size in bytes. 203 * @param[in] messages Number of datagrams per socket to be received. 204 * 192 205 * @return EOK on success. 193 206 * @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 { 197 int value; 198 int index; 199 int message; 200 207 * 208 */ 209 int sockets_recvfrom(int verbose, int *socket_ids, unsigned int sockets, 210 struct sockaddr *address, socklen_t *addrlen, char *data, size_t size, 211 unsigned int messages) 212 { 201 213 if (verbose) 202 214 printf("\tRecvfrom\t"); … … 204 216 fflush(stdout); 205 217 206 for (index = 0; index < sockets; index++) { 207 for (message = 0; message < messages; message++) { 208 value = recvfrom(socket_ids[index], data, size, 0, address, addrlen); 209 if (value < 0) { 210 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 211 socket_print_error(stderr, value, "Socket receive: ", "\n"); 212 return value; 213 } 214 } 215 if (verbose) 216 print_mark(index); 217 } 218 return EOK; 219 } 220 221 /** Sends and receives data via sockets. 218 for (unsigned int index = 0; index < sockets; index++) { 219 for (unsigned int message = 0; message < messages; message++) { 220 int rc = recvfrom(socket_ids[index], data, size, 0, address, addrlen); 221 if (rc < 0) { 222 printf("Socket %u (%d), message %u error:\n", 223 index, socket_ids[index], message); 224 socket_print_error(stderr, rc, "Socket receive: ", "\n"); 225 return rc; 226 } 227 } 228 229 if (verbose) 230 print_mark(index); 231 } 232 233 return EOK; 234 } 235 236 /** Send and receive data via sockets. 222 237 * 223 238 * Each datagram is sent and a reply read consequently. 224 239 * The next datagram is sent after the reply is received. 225 240 * 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. 241 * @param[in] verbose Print out verbose information. 242 * @param[in] socket_ids Stored socket identifiers. 243 * @param[in] sockets Number of sockets. 244 * @param[in,out] address Destination host address to send data to. 245 * The source host address of received datagrams is set. 246 * @param[in] addrlen Length of the destination address in bytes. 247 * @param[in,out] data Data to be sent. The received data are set. 248 * @param[in] size Size of the data in bytes. 249 * @param[in] messages Number of datagrams per socket to be received. 250 * @param[in] type Socket type. 251 * 234 252 * @return EOK on success. 235 253 * @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 { 239 int value; 240 int index; 241 int message; 242 int rc; 243 254 * 255 */ 256 int sockets_sendto_recvfrom(int verbose, int *socket_ids, unsigned int sockets, 257 struct sockaddr *address, socklen_t *addrlen, char *data, 258 size_t size, unsigned int messages, sock_type_t type) 259 { 244 260 if (verbose) 245 261 printf("\tSendto and recvfrom\t"); 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); 262 263 fflush(stdout); 264 265 for (unsigned int index = 0; index < sockets; index++) { 266 for (unsigned int message = 0; message < messages; message++) { 267 int rc; 268 269 switch (type) { 270 case SOCK_STREAM: 271 rc = send(socket_ids[index], data, size, 0); 272 break; 273 case SOCK_DGRAM: 274 rc = sendto(socket_ids[index], data, size, 0, address, *addrlen); 275 break; 276 default: 277 rc = EINVAL; 278 } 279 252 280 if (rc != EOK) { 253 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 281 printf("Socket %u (%d), message %u error:\n", 282 index, socket_ids[index], message); 254 283 socket_print_error(stderr, rc, "Socket send: ", "\n"); 255 284 return rc; 256 285 } 257 value = recvfrom(socket_ids[index], data, size, 0, address, addrlen); 258 if (value < 0) { 259 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); 260 socket_print_error(stderr, value, "Socket receive: ", "\n"); 261 return value; 262 } 263 } 286 287 rc = recvfrom(socket_ids[index], data, size, 0, address, addrlen); 288 if (rc < 0) { 289 printf("Socket %u (%d), message %u error:\n", 290 index, socket_ids[index], message); 291 socket_print_error(stderr, rc, "Socket receive: ", "\n"); 292 return rc; 293 } 294 } 295 264 296 if (verbose) 265 297 print_mark(index); … … 274 306 * 275 307 * @param[in] index The index of the mark to be printed. 276 */ 277 void print_mark(int index) 308 * 309 */ 310 void print_mark(unsigned int index) 278 311 { 279 312 if ((index + 1) % 10) … … 281 314 else 282 315 printf("|"); 316 283 317 fflush(stdout); 284 318 } -
uspace/app/nettest1/nettest.h
rf2c19b0 r03c971f 40 40 #include <net/socket.h> 41 41 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); 42 extern void print_mark(unsigned int); 43 extern int sockets_create(int, int *, unsigned int, uint16_t, sock_type_t); 44 extern int sockets_close(int, int *, unsigned int); 45 extern int sockets_connect(int, int *, unsigned int, struct sockaddr *, 46 socklen_t); 47 extern int sockets_sendto(int, int *, unsigned int, struct sockaddr *, 48 socklen_t, char *, size_t, unsigned int, sock_type_t); 49 extern int sockets_recvfrom(int, int *, unsigned int, struct sockaddr *, 50 socklen_t *, char *, size_t, unsigned int); 51 extern int sockets_sendto_recvfrom(int, int *, unsigned int, struct sockaddr *, 52 socklen_t *, char *, size_t, unsigned int, sock_type_t); 49 53 50 54 #endif -
uspace/app/nettest1/nettest1.c
rf2c19b0 r03c971f 54 54 55 55 /** Echo module name. */ 56 #define NAME "Nettest1"56 #define NAME "nettest1" 57 57 58 58 /** Packet data pattern. */ 59 #define NETTEST1_TEXT 60 61 static int family = PF_INET;59 #define NETTEST1_TEXT "Networking test 1 - sockets" 60 61 static uint16_t family = AF_INET; 62 62 static sock_type_t type = SOCK_DGRAM; 63 static char *data;64 63 static size_t size = 27; 65 static int verbose = 0; 64 static bool verbose = false; 65 static int sockets = 10; 66 static int messages = 10; 67 static uint16_t port = 7; 66 68 67 69 static struct sockaddr *address; 68 70 static socklen_t addrlen; 69 71 70 static int sockets; 71 static int messages; 72 static uint16_t port; 72 static char *data; 73 73 74 74 static void nettest1_print_help(void) … … 114 114 int value; 115 115 int rc; 116 116 117 117 switch (argv[*index][1]) { 118 118 /* … … 120 120 */ 121 121 case 'f': 122 rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family); 123 if (rc != EOK) 124 return rc; 122 rc = arg_parse_name_int(argc, argv, index, &value, 0, 123 socket_parse_protocol_family); 124 if (rc != EOK) 125 return rc; 126 127 family = (uint16_t) value; 125 128 break; 126 129 case 'h': … … 131 134 if (rc != EOK) 132 135 return rc; 136 133 137 break; 134 138 case 'n': … … 136 140 if (rc != EOK) 137 141 return rc; 142 138 143 break; 139 144 case 'p': … … 141 146 if (rc != EOK) 142 147 return rc; 148 143 149 port = (uint16_t) value; 144 150 break; … … 147 153 if (rc != EOK) 148 154 return rc; 155 149 156 size = (value >= 0) ? (size_t) value : 0; 150 157 break; 151 158 case 't': 152 rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type); 153 if (rc != EOK) 154 return rc; 159 rc = arg_parse_name_int(argc, argv, index, &value, 0, 160 socket_parse_socket_type); 161 if (rc != EOK) 162 return rc; 163 155 164 type = (sock_type_t) value; 156 165 break; … … 158 167 verbose = 1; 159 168 break; 169 160 170 /* 161 171 * Long options with double dash ('-') … … 163 173 case '-': 164 174 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { 165 rc = arg_parse_name_int(argc, argv, index, & family, 9,175 rc = arg_parse_name_int(argc, argv, index, &value, 9, 166 176 socket_parse_protocol_family); 167 177 if (rc != EOK) 168 178 return rc; 179 180 family = (uint16_t) value; 169 181 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { 170 182 nettest1_print_help(); … … 182 194 if (rc != EOK) 183 195 return rc; 196 184 197 port = (uint16_t) value; 185 198 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { … … 188 201 if (rc != EOK) 189 202 return rc; 203 190 204 type = (sock_type_t) value; 191 205 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { … … 200 214 return EINVAL; 201 215 } 202 216 203 217 return EOK; 204 218 } … … 211 225 static void nettest1_fill_buffer(char *buffer, size_t size) 212 226 { 213 size_t length; 214 215 length = 0; 227 size_t length = 0; 216 228 while (size > length + sizeof(NETTEST1_TEXT) - 1) { 217 229 memcpy(buffer + length, NETTEST1_TEXT, … … 219 231 length += sizeof(NETTEST1_TEXT) - 1; 220 232 } 221 233 222 234 memcpy(buffer + length, NETTEST1_TEXT, size - length); 223 235 buffer[size] = '\0'; … … 226 238 static int nettest1_test(int *socket_ids, int nsockets, int nmessages) 227 239 { 228 int rc;229 230 240 if (verbose) 231 241 printf("%d sockets, %d messages\n", nsockets, nmessages); 232 233 rc = sockets_create(verbose, socket_ids, nsockets, family, type);234 if (rc != EOK) 235 return rc; 236 242 243 int rc = sockets_create(verbose, socket_ids, nsockets, family, type); 244 if (rc != EOK) 245 return rc; 246 237 247 if (type == SOCK_STREAM) { 238 248 rc = sockets_connect(verbose, socket_ids, nsockets, address, … … 241 251 return rc; 242 252 } 243 253 244 254 rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address, 245 &addrlen, data, size, nmessages );246 if (rc != EOK) 247 return rc; 248 255 &addrlen, data, size, nmessages, type); 256 if (rc != EOK) 257 return rc; 258 249 259 rc = sockets_close(verbose, socket_ids, nsockets); 250 260 if (rc != EOK) 251 261 return rc; 252 262 253 263 if (verbose) 254 264 printf("\tOK\n"); 255 265 256 266 /****/ 257 267 258 268 rc = sockets_create(verbose, socket_ids, nsockets, family, type); 259 269 if (rc != EOK) 260 270 return rc; 261 271 262 272 if (type == SOCK_STREAM) { 263 273 rc = sockets_connect(verbose, socket_ids, nsockets, address, … … 266 276 return rc; 267 277 } 268 278 269 279 rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen, 270 data, size, nmessages );271 if (rc != EOK) 272 return rc; 273 280 data, size, nmessages, type); 281 if (rc != EOK) 282 return rc; 283 274 284 rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen, 275 285 data, size, nmessages); 276 286 if (rc != EOK) 277 287 return rc; 278 288 279 289 rc = sockets_close(verbose, socket_ids, nsockets); 280 290 if (rc != EOK) 281 291 return rc; 282 292 283 293 if (verbose) 284 294 printf("\tOK\n"); 285 295 286 296 return EOK; 287 297 } … … 289 299 int main(int argc, char *argv[]) 290 300 { 291 struct sockaddr_in address_in;292 struct sockaddr_in6 address_in6;293 dnsr_hostinfo_t *hinfo;294 uint8_t *address_start;295 296 int *socket_ids;297 int index;298 struct timeval time_before;299 struct timeval time_after;300 301 int rc;302 303 sockets = 10;304 messages = 10;305 port = 7;306 307 301 /* 308 302 * Parse the command line arguments. Stop before the last argument 309 303 * if it does not start with dash ('-') 310 304 */ 311 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) { 305 int index; 306 int rc; 307 308 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 309 (argv[index][0] == '-')); index++) { 312 310 /* Options should start with dash ('-') */ 313 311 if (argv[index][0] == '-') { … … 320 318 } 321 319 } 322 323 /* If not before the last argument containing the host */320 321 /* The last argument containing the host */ 324 322 if (index >= argc) { 325 printf(" Command line error: missing host name\n");323 printf("Host name missing.\n"); 326 324 nettest1_print_help(); 327 325 return EINVAL; 328 326 } 329 327 328 char *addr_s = argv[argc - 1]; 329 330 /* Interpret as address */ 331 inet_addr_t addr_addr; 332 rc = inet_addr_parse(addr_s, &addr_addr); 333 334 if (rc != EOK) { 335 /* Interpret as a host name */ 336 dnsr_hostinfo_t *hinfo = NULL; 337 rc = dnsr_name2host(addr_s, &hinfo, family); 338 339 if (rc != EOK) { 340 printf("Error resolving host '%s'.\n", addr_s); 341 return EINVAL; 342 } 343 344 addr_addr = hinfo->addr; 345 } 346 347 struct sockaddr_in addr; 348 struct sockaddr_in6 addr6; 349 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 350 351 if (af != family) { 352 printf("Address family does not match explicitly set family.\n"); 353 return EINVAL; 354 } 355 330 356 /* Prepare the address buffer */ 331 332 switch (family) { 333 case PF_INET: 334 address_in.sin_family = AF_INET; 335 address_in.sin_port = htons(port); 336 address = (struct sockaddr *) &address_in; 337 addrlen = sizeof(address_in); 338 address_start = (uint8_t *) &address_in.sin_addr.s_addr; 339 break; 340 case PF_INET6: 341 address_in6.sin6_family = AF_INET6; 342 address_in6.sin6_port = htons(port); 343 address = (struct sockaddr *) &address_in6; 344 addrlen = sizeof(address_in6); 345 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr; 357 358 switch (af) { 359 case AF_INET: 360 addr.sin_port = htons(port); 361 address = (struct sockaddr *) &addr; 362 addrlen = sizeof(addr); 363 break; 364 case AF_INET6: 365 addr6.sin6_port = htons(port); 366 address = (struct sockaddr *) &addr6; 367 addrlen = sizeof(addr6); 346 368 break; 347 369 default: … … 349 371 return EAFNOSUPPORT; 350 372 } 351 352 /* Parse the last argument which should contain the host/address */ 353 rc = inet_pton(family, argv[argc - 1], address_start); 354 if (rc != EOK) { 355 /* Try interpreting as a host name */ 356 rc = dnsr_name2host(argv[argc - 1], &hinfo); 357 if (rc != EOK) { 358 printf("Error resolving host '%s'.\n", argv[argc - 1]); 359 return rc; 360 } 361 362 address_in.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4); 363 } 364 373 365 374 /* Check data buffer size */ 366 375 if (size <= 0) { … … 369 378 size = 1024; 370 379 } 371 380 372 381 /* 373 382 * Prepare data buffer. Allocate size bytes plus one for the … … 380 389 } 381 390 nettest1_fill_buffer(data, size); 382 391 383 392 /* Check socket count */ 384 393 if (sockets <= 0) { … … 387 396 sockets = 2; 388 397 } 389 398 390 399 /* 391 400 * Prepare socket buffer. Allocate count fields plus the terminating 392 401 * null (\0). 393 402 */ 394 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));403 int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 395 404 if (!socket_ids) { 396 405 fprintf(stderr, "Failed to allocate receive buffer.\n"); 397 406 return ENOMEM; 398 407 } 408 399 409 socket_ids[sockets] = 0; 400 410 401 411 if (verbose) 402 412 printf("Starting tests\n"); 403 413 414 struct timeval time_before; 404 415 rc = gettimeofday(&time_before, NULL); 405 416 if (rc != EOK) { … … 407 418 return rc; 408 419 } 409 420 410 421 nettest1_test(socket_ids, 1, 1); 411 422 nettest1_test(socket_ids, 1, messages); 412 423 nettest1_test(socket_ids, sockets, 1); 413 424 nettest1_test(socket_ids, sockets, messages); 414 425 426 struct timeval time_after; 415 427 rc = gettimeofday(&time_after, NULL); 416 428 if (rc != EOK) { … … 418 430 return rc; 419 431 } 420 432 421 433 printf("Tested in %ld microseconds\n", tv_sub(&time_after, 422 434 &time_before)); 423 435 424 436 if (verbose) 425 437 printf("Exiting\n"); 426 438 427 439 return EOK; 428 440 } 429 441 430 431 442 /** @} 432 443 */ -
uspace/app/nettest2/nettest2.c
rf2c19b0 r03c971f 55 55 56 56 /** Echo module name. */ 57 #define NAME "Nettest2"57 #define NAME "nettest2" 58 58 59 59 /** Packet data pattern. */ 60 #define NETTEST2_TEXT 61 62 static size_t size;63 static bool verbose;64 static sock_type_t type;65 static int sockets;66 static int messages;67 static int family;68 static uint16_t port ;60 #define NETTEST2_TEXT "Networking test 2 - transfer" 61 62 static uint16_t family = PF_INET; 63 static size_t size = 28; 64 static bool verbose = false; 65 static sock_type_t type = SOCK_DGRAM; 66 static int sockets = 10; 67 static int messages = 10; 68 static uint16_t port = 7; 69 69 70 70 static void nettest2_print_help(void) … … 107 107 static void nettest2_fill_buffer(char *buffer, size_t size) 108 108 { 109 size_t length; 110 111 length = 0; 109 size_t length = 0; 112 110 while (size > length + sizeof(NETTEST2_TEXT) - 1) { 113 111 memcpy(buffer + length, NETTEST2_TEXT, … … 115 113 length += sizeof(NETTEST2_TEXT) - 1; 116 114 } 117 115 118 116 memcpy(buffer + length, NETTEST2_TEXT, size - length); 119 117 buffer[size] = '\0'; … … 130 128 int value; 131 129 int rc; 132 130 133 131 switch (argv[*index][1]) { 134 132 /* … … 136 134 */ 137 135 case 'f': 138 rc = arg_parse_name_int(argc, argv, index, & family, 0,136 rc = arg_parse_name_int(argc, argv, index, &value, 0, 139 137 socket_parse_protocol_family); 140 138 if (rc != EOK) 141 139 return rc; 140 141 family = (uint16_t) value; 142 142 break; 143 143 case 'h': 144 144 nettest2_print_help(); 145 145 return EOK; 146 break;147 146 case 'm': 148 147 rc = arg_parse_int(argc, argv, index, &messages, 0); 149 148 if (rc != EOK) 150 149 return rc; 150 151 151 break; 152 152 case 'n': … … 154 154 if (rc != EOK) 155 155 return rc; 156 156 157 break; 157 158 case 'p': … … 159 160 if (rc != EOK) 160 161 return rc; 162 161 163 port = (uint16_t) value; 162 164 break; … … 165 167 if (rc != EOK) 166 168 return rc; 169 167 170 size = (value >= 0) ? (size_t) value : 0; 168 171 break; … … 172 175 if (rc != EOK) 173 176 return rc; 177 174 178 type = (sock_type_t) value; 175 179 break; … … 177 181 verbose = true; 178 182 break; 183 179 184 /* 180 185 * Long options with double dash ('-') … … 182 187 case '-': 183 188 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) { 184 rc = arg_parse_name_int(argc, argv, index, & family, 9,189 rc = arg_parse_name_int(argc, argv, index, &value, 9, 185 190 socket_parse_protocol_family); 186 191 if (rc != EOK) 187 192 return rc; 193 194 family = (uint16_t) value; 188 195 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) { 189 196 nettest2_print_help(); … … 201 208 if (rc != EOK) 202 209 return rc; 210 203 211 port = (uint16_t) value; 204 212 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) { … … 207 215 if (rc != EOK) 208 216 return rc; 217 209 218 type = (sock_type_t) value; 210 219 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) { … … 219 228 return EINVAL; 220 229 } 221 230 222 231 return EOK; 223 232 } … … 225 234 int main(int argc, char *argv[]) 226 235 { 227 struct sockaddr *address;228 struct sockaddr_in address_in;229 struct sockaddr_in6 address_in6;230 dnsr_hostinfo_t *hinfo;231 socklen_t addrlen;232 uint8_t *address_start;233 234 int *socket_ids;235 char *data;236 236 int index; 237 struct timeval time_before;238 struct timeval time_after;239 240 237 int rc; 241 242 size = 28; 243 verbose = false; 244 type = SOCK_DGRAM; 245 sockets = 10; 246 messages = 10; 247 family = PF_INET; 248 port = 7; 249 238 250 239 /* 251 240 * Parse the command line arguments. … … 255 244 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 256 245 (argv[index][0] == '-')); ++index) { 257 258 246 /* Options should start with dash ('-') */ 259 247 if (argv[index][0] == '-') { … … 266 254 } 267 255 } 268 269 /* If not before the last argument containing the host */256 257 /* The last argument containing the host */ 270 258 if (index >= argc) { 271 printf(" Command line error: missing host name\n");259 printf("Host name missing.\n"); 272 260 nettest2_print_help(); 273 261 return EINVAL; 274 262 } 275 263 264 char *addr_s = argv[argc - 1]; 265 266 /* Interpret as address */ 267 inet_addr_t addr_addr; 268 rc = inet_addr_parse(addr_s, &addr_addr); 269 270 if (rc != EOK) { 271 /* Interpret as a host name */ 272 dnsr_hostinfo_t *hinfo = NULL; 273 rc = dnsr_name2host(addr_s, &hinfo, family); 274 275 if (rc != EOK) { 276 printf("Error resolving host '%s'.\n", addr_s); 277 return EINVAL; 278 } 279 280 addr_addr = hinfo->addr; 281 } 282 283 struct sockaddr_in addr; 284 struct sockaddr_in6 addr6; 285 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 286 287 if (af != family) { 288 printf("Address family does not match explicitly set family.\n"); 289 return EINVAL; 290 } 291 276 292 /* Prepare the address buffer */ 277 278 switch (family) { 279 case PF_INET: 280 address_in.sin_family = AF_INET; 281 address_in.sin_port = htons(port); 282 address = (struct sockaddr *) &address_in; 283 addrlen = sizeof(address_in); 284 address_start = (uint8_t *) &address_in.sin_addr.s_addr; 285 break; 286 case PF_INET6: 287 address_in6.sin6_family = AF_INET6; 288 address_in6.sin6_port = htons(port); 289 address = (struct sockaddr *) &address_in6; 290 addrlen = sizeof(address_in6); 291 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr; 293 294 struct sockaddr *address; 295 socklen_t addrlen; 296 297 switch (af) { 298 case AF_INET: 299 addr.sin_port = htons(port); 300 address = (struct sockaddr *) &addr; 301 addrlen = sizeof(addr); 302 break; 303 case AF_INET6: 304 addr6.sin6_port = htons(port); 305 address = (struct sockaddr *) &addr6; 306 addrlen = sizeof(addr6); 292 307 break; 293 308 default: … … 295 310 return EAFNOSUPPORT; 296 311 } 297 298 /* Parse the last argument which should contain the host/address */ 299 rc = inet_pton(family, argv[argc - 1], address_start); 300 if (rc != EOK) { 301 /* Try interpreting as a host name */ 302 rc = dnsr_name2host(argv[argc - 1], &hinfo); 303 if (rc != EOK) { 304 printf("Error resolving host '%s'.\n", argv[argc - 1]); 305 return rc; 306 } 307 308 address_in.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4); 309 } 310 312 311 313 /* Check data buffer size. */ 312 314 if (size <= 0) { … … 315 317 size = 1024; 316 318 } 317 319 318 320 /* 319 321 * Prepare the buffer. Allocate size bytes plus one for terminating 320 322 * null character. 321 323 */ 322 data = (char *) malloc(size + 1);324 char *data = (char *) malloc(size + 1); 323 325 if (!data) { 324 326 fprintf(stderr, "Failed to allocate data buffer.\n"); 325 327 return ENOMEM; 326 328 } 327 329 328 330 /* Fill buffer with a pattern. */ 329 331 nettest2_fill_buffer(data, size); 330 332 331 333 /* Check socket count. */ 332 334 if (sockets <= 0) { … … 335 337 sockets = 2; 336 338 } 337 339 338 340 /* 339 341 * Prepare the socket buffer. 340 342 * Allocate count entries plus the terminating null (\0) 341 343 */ 342 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));344 int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 343 345 if (!socket_ids) { 344 346 fprintf(stderr, "Failed to allocate receive buffer.\n"); 345 347 return ENOMEM; 346 348 } 349 347 350 socket_ids[sockets] = 0; 348 351 349 352 if (verbose) 350 353 printf("Starting tests\n"); 351 354 352 355 rc = sockets_create(verbose, socket_ids, sockets, family, type); 353 356 if (rc != EOK) 354 357 return rc; 355 358 356 359 if (type == SOCK_STREAM) { 357 360 rc = sockets_connect(verbose, socket_ids, sockets, … … 360 363 return rc; 361 364 } 362 365 363 366 if (verbose) 364 367 printf("\n"); 365 368 369 struct timeval time_before; 366 370 rc = gettimeofday(&time_before, NULL); 367 371 if (rc != EOK) { … … 369 373 return rc; 370 374 } 371 375 372 376 rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, 373 &addrlen, data, size, messages );377 &addrlen, data, size, messages, type); 374 378 if (rc != EOK) 375 379 return rc; 376 380 381 struct timeval time_after; 377 382 rc = gettimeofday(&time_after, NULL); 378 383 if (rc != EOK) { … … 380 385 return rc; 381 386 } 382 387 383 388 if (verbose) 384 389 printf("\tOK\n"); 385 390 386 391 printf("sendto + recvfrom tested in %ld microseconds\n", 387 392 tv_sub(&time_after, &time_before)); 388 393 389 394 rc = gettimeofday(&time_before, NULL); 390 395 if (rc != EOK) { … … 392 397 return rc; 393 398 } 394 399 395 400 rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen, 396 data, size, messages );401 data, size, messages, type); 397 402 if (rc != EOK) 398 403 return rc; 399 404 400 405 rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, 401 406 data, size, messages); 402 407 if (rc != EOK) 403 408 return rc; 404 409 405 410 rc = gettimeofday(&time_after, NULL); 406 411 if (rc != EOK) { … … 408 413 return rc; 409 414 } 410 415 411 416 if (verbose) 412 417 printf("\tOK\n"); 413 418 414 419 printf("sendto, recvfrom tested in %ld microseconds\n", 415 420 tv_sub(&time_after, &time_before)); 416 421 417 422 rc = sockets_close(verbose, socket_ids, sockets); 418 423 if (rc != EOK) 419 424 return rc; 420 425 421 426 if (verbose) 422 427 printf("\nExiting\n"); 423 428 424 429 return EOK; 425 430 } -
uspace/app/nettest3/nettest3.c
rf2c19b0 r03c971f 78 78 if (rc != EOK) { 79 79 /* Try interpreting as a host name */ 80 rc = dnsr_name2host(argv[1], &hinfo );80 rc = dnsr_name2host(argv[1], &hinfo, AF_INET); 81 81 if (rc != EOK) { 82 82 printf("Error resolving host '%s'.\n", argv[1]); 83 83 return rc; 84 84 } 85 86 addr.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4); 87 addr.sin_family = AF_INET; 85 86 uint16_t af = inet_addr_sockaddr_in(&hinfo->addr, &addr, NULL); 87 if (af != AF_INET) { 88 printf("Host '%s' not resolved as IPv4 address.\n", argv[1]); 89 return rc; 90 } 88 91 } 89 92 printf("result: rc=%d, family=%d, addr=%x\n", rc, -
uspace/app/nterm/conn.c
rf2c19b0 r03c971f 75 75 int conn_open(const char *addr_s, const char *port_s) 76 76 { 77 struct sockaddr_in addr; 78 dnsr_hostinfo_t *hinfo = NULL; 79 int rc; 80 char *endptr; 81 82 addr.sin_family = AF_INET; 83 84 rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr); 77 int conn_fd = -1; 78 79 /* Interpret as address */ 80 inet_addr_t addr_addr; 81 int rc = inet_addr_parse(addr_s, &addr_addr); 82 85 83 if (rc != EOK) { 86 /* Try interpreting as a host name */ 87 rc = dnsr_name2host(addr_s, &hinfo); 84 /* Interpret as a host name */ 85 dnsr_hostinfo_t *hinfo = NULL; 86 rc = dnsr_name2host(addr_s, &hinfo, 0); 87 88 88 if (rc != EOK) { 89 89 printf("Error resolving host '%s'.\n", addr_s); 90 90 goto error; 91 91 } 92 93 addr .sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);92 93 addr_addr = hinfo->addr; 94 94 } 95 96 addr.sin_port = htons(strtol(port_s, &endptr, 10)); 95 96 struct sockaddr_in addr; 97 struct sockaddr_in6 addr6; 98 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 99 100 char *endptr; 101 uint16_t port = strtol(port_s, &endptr, 10); 97 102 if (*endptr != '\0') { 98 103 printf("Invalid port number %s\n", port_s); 99 104 goto error; 100 105 } 101 106 107 printf("Connecting to host %s port %u\n", addr_s, port); 108 102 109 conn_fd = socket(PF_INET, SOCK_STREAM, 0); 103 110 if (conn_fd < 0) 104 111 goto error; 105 106 printf("Connecting to host %s port %u\n", addr_s, ntohs(addr.sin_port)); 107 108 rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr)); 112 113 switch (af) { 114 case AF_INET: 115 addr.sin_port = htons(port); 116 rc = connect(conn_fd, (struct sockaddr *) &addr, sizeof(addr)); 117 break; 118 case AF_INET6: 119 addr6.sin6_port = htons(port); 120 rc = connect(conn_fd, (struct sockaddr *) &addr6, sizeof(addr6)); 121 break; 122 default: 123 printf("Unknown address family.\n"); 124 goto error; 125 } 126 109 127 if (rc != EOK) 110 128 goto error; 111 129 112 130 rcv_fid = fibril_create(rcv_fibril, NULL); 113 131 if (rcv_fid == 0) 114 132 goto error; 115 133 116 134 fibril_add_ready(rcv_fid); 117 135 118 136 return EOK; 119 137 120 138 error: 121 139 if (conn_fd >= 0) { … … 123 141 conn_fd = -1; 124 142 } 125 143 126 144 return EIO; 127 145 } … … 129 147 int conn_send(void *data, size_t size) 130 148 { 131 int rc; 132 133 rc = send(conn_fd, data, size, 0); 149 int rc = send(conn_fd, data, size, 0); 134 150 if (rc != EOK) 135 151 return EIO; 136 152 137 153 return EOK; 138 154 } -
uspace/app/pcc/config.h
rf2c19b0 r03c971f 50 50 51 51 /* Define to 1 if you have the `mkstemp' function. */ 52 //#define HAVE_MKSTEMP 152 #define HAVE_MKSTEMP 1 53 53 54 54 /* Define to 1 if you have the `snprintf' function. */ -
uspace/app/ping/ping.c
rf2c19b0 r03c971f 37 37 #include <errno.h> 38 38 #include <fibril_synch.h> 39 #include <net/socket_codes.h> 39 40 #include <inet/dnsr.h> 40 41 #include <inet/addr.h> 41 42 #include <inet/inetping.h> 42 43 #include <io/console.h> 44 #include <getopt.h> 43 45 #include <stdio.h> 44 46 #include <stdlib.h> 47 #include <str.h> 48 #include <str_error.h> 45 49 #include <sys/types.h> 46 50 … … 53 57 #define PING_TIMEOUT (1000 * 1000) 54 58 59 typedef enum { 60 RECEIVED_NONE, 61 RECEIVED_SUCCESS, 62 RECEIVED_INTERRUPT 63 } received_t; 64 65 static received_t received; 66 static FIBRIL_CONDVAR_INITIALIZE(received_cv); 67 static FIBRIL_MUTEX_INITIALIZE(received_lock); 68 69 static bool quit = false; 70 static FIBRIL_CONDVAR_INITIALIZE(quit_cv); 71 static FIBRIL_MUTEX_INITIALIZE(quit_lock); 72 55 73 static int ping_ev_recv(inetping_sdu_t *); 56 57 static bool done = false;58 static FIBRIL_CONDVAR_INITIALIZE(done_cv);59 static FIBRIL_MUTEX_INITIALIZE(done_lock);60 74 61 75 static inetping_ev_ops_t ev_ops = { … … 63 77 }; 64 78 65 static inet_addr_t src_addr; 66 static inet_addr_t dest_addr; 67 68 static bool ping_repeat = false; 79 static addr32_t src; 80 static addr32_t dest; 81 82 static bool repeat_forever = false; 83 static size_t repeat_count = 1; 84 85 static const char *short_options = "rn:"; 69 86 70 87 static void print_syntax(void) 71 88 { 72 printf("syntax: " NAME " [-r] <host>\n"); 73 } 74 75 static void ping_signal_done(void) 76 { 77 fibril_mutex_lock(&done_lock); 78 done = true; 79 fibril_mutex_unlock(&done_lock); 80 fibril_condvar_broadcast(&done_cv); 89 printf("Syntax: %s [-n <count>|-r] <host>\n", NAME); 90 } 91 92 static void ping_signal_received(received_t value) 93 { 94 fibril_mutex_lock(&received_lock); 95 received = value; 96 fibril_mutex_unlock(&received_lock); 97 fibril_condvar_broadcast(&received_cv); 98 } 99 100 static void ping_signal_quit(void) 101 { 102 fibril_mutex_lock(&quit_lock); 103 quit = true; 104 fibril_mutex_unlock(&quit_lock); 105 fibril_condvar_broadcast(&quit_cv); 81 106 } 82 107 83 108 static int ping_ev_recv(inetping_sdu_t *sdu) 84 109 { 85 char *asrc, *adest; 86 int rc; 87 88 rc = inet_addr_format(&sdu->src, &asrc); 110 inet_addr_t src_addr; 111 inet_addr_set(sdu->src, &src_addr); 112 113 inet_addr_t dest_addr; 114 inet_addr_set(sdu->dest, &dest_addr); 115 116 char *asrc; 117 int rc = inet_addr_format(&src_addr, &asrc); 89 118 if (rc != EOK) 90 119 return ENOMEM; 91 92 rc = inet_addr_format(&sdu->dest, &adest); 120 121 char *adest; 122 rc = inet_addr_format(&dest_addr, &adest); 93 123 if (rc != EOK) { 94 124 free(asrc); 95 125 return ENOMEM; 96 126 } 127 97 128 printf("Received ICMP echo reply: from %s to %s, seq. no %u, " 98 129 "payload size %zu\n", asrc, adest, sdu->seq_no, sdu->size); 99 100 if (!ping_repeat) { 101 ping_signal_done(); 102 } 103 130 131 ping_signal_received(RECEIVED_SUCCESS); 132 104 133 free(asrc); 105 134 free(adest); … … 110 139 { 111 140 inetping_sdu_t sdu; 112 int rc; 113 114 sdu.src = src_addr; 115 sdu.dest = dest_addr; 141 142 sdu.src = src; 143 sdu.dest = dest; 116 144 sdu.seq_no = seq_no; 117 145 sdu.data = (void *) "foo"; 118 146 sdu.size = 3; 119 120 rc = inetping_send(&sdu); 121 if (rc != EOK) { 122 printf(NAME ": Failed sending echo request (%d).\n", rc); 123 return rc; 124 } 125 126 return EOK; 147 148 int rc = inetping_send(&sdu); 149 if (rc != EOK) 150 printf("Failed sending echo request: %s (%d).\n", 151 str_error(rc), rc); 152 153 return rc; 127 154 } 128 155 … … 130 157 { 131 158 uint16_t seq_no = 0; 132 159 160 while ((repeat_count--) || (repeat_forever)) { 161 fibril_mutex_lock(&received_lock); 162 received = RECEIVED_NONE; 163 fibril_mutex_unlock(&received_lock); 164 165 (void) ping_send(++seq_no); 166 167 fibril_mutex_lock(&received_lock); 168 int rc = fibril_condvar_wait_timeout(&received_cv, &received_lock, 169 PING_TIMEOUT); 170 received_t recv = received; 171 fibril_mutex_unlock(&received_lock); 172 173 if ((rc == ETIMEOUT) || (recv == RECEIVED_NONE)) 174 printf("Echo request timed out (seq. no %u)\n", seq_no); 175 176 if (recv == RECEIVED_INTERRUPT) 177 break; 178 179 if ((repeat_count > 0) || (repeat_forever)) { 180 fibril_mutex_lock(&received_lock); 181 rc = fibril_condvar_wait_timeout(&received_cv, &received_lock, 182 PING_DELAY); 183 recv = received; 184 fibril_mutex_unlock(&received_lock); 185 186 if (recv == RECEIVED_INTERRUPT) 187 break; 188 } 189 } 190 191 ping_signal_quit(); 192 return 0; 193 } 194 195 static int input_fibril(void *arg) 196 { 197 console_ctrl_t *con = console_init(stdin, stdout); 198 133 199 while (true) { 134 fibril_mutex_lock(&done_lock); 135 if (done) { 136 fibril_mutex_unlock(&done_lock); 137 return 0; 138 } 139 fibril_mutex_unlock(&done_lock); 140 141 (void) ping_send(++seq_no); 142 async_usleep(PING_DELAY); 143 } 144 145 return 0; 146 } 147 148 static int input_fibril(void *arg) 149 { 150 console_ctrl_t *con; 151 cons_event_t ev; 152 153 con = console_init(stdin, stdout); 154 printf("[Press Ctrl-Q to quit]\n"); 155 156 while (true) { 200 cons_event_t ev; 157 201 if (!console_get_event(con, &ev)) 158 202 break; 159 160 if ( ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS&&161 ( ev.ev.key.mods & (KM_ALT | KM_SHIFT)) ==162 0 && (ev.ev.key.mods & KM_CTRL) != 0) {203 204 if ((ev.type == CEV_KEY) && (ev.ev.key.type == KEY_PRESS) && 205 ((ev.ev.key.mods & (KM_ALT | KM_SHIFT)) == 0) && 206 ((ev.ev.key.mods & KM_CTRL) != 0)) { 163 207 /* Ctrl+key */ 164 208 if (ev.ev.key.key == KC_Q) { 165 ping_signal_ done();166 return 0;209 ping_signal_received(RECEIVED_INTERRUPT); 210 break; 167 211 } 168 212 } 169 213 } 170 214 171 215 return 0; 172 216 } … … 178 222 char *adest = NULL; 179 223 char *sdest = NULL; 180 int rc; 181 int argi; 182 183 rc = inetping_init(&ev_ops); 184 if (rc != EOK) { 185 printf(NAME ": Failed connecting to internet ping service " 186 "(%d).\n", rc); 187 goto error; 188 } 189 190 argi = 1; 191 if (argi < argc && str_cmp(argv[argi], "-r") == 0) { 192 ping_repeat = true; 193 ++argi; 194 } else { 195 ping_repeat = false; 196 } 197 198 if (argc - argi != 1) { 224 225 int rc = inetping_init(&ev_ops); 226 if (rc != EOK) { 227 printf("Failed connecting to internet ping service: " 228 "%s (%d).\n", str_error(rc), rc); 229 goto error; 230 } 231 232 int c; 233 while ((c = getopt(argc, argv, short_options)) != -1) { 234 switch (c) { 235 case 'r': 236 repeat_forever = true; 237 break; 238 case 'n': 239 rc = str_size_t(optarg, NULL, 10, true, &repeat_count); 240 if (rc != EOK) { 241 printf("Invalid repeat count.\n"); 242 print_syntax(); 243 goto error; 244 } 245 break; 246 default: 247 printf("Unknown option passed.\n"); 248 print_syntax(); 249 goto error; 250 } 251 } 252 253 if (optind >= argc) { 254 printf("IP address or host name not supplied.\n"); 199 255 print_syntax(); 200 256 goto error; 201 257 } 202 258 203 259 /* Parse destination address */ 204 rc = inet_addr_parse(argv[argi], &dest_addr); 260 inet_addr_t dest_addr; 261 rc = inet_addr_parse(argv[optind], &dest_addr); 205 262 if (rc != EOK) { 206 263 /* Try interpreting as a host name */ 207 rc = dnsr_name2host(argv[ argi], &hinfo);264 rc = dnsr_name2host(argv[optind], &hinfo, AF_INET); 208 265 if (rc != EOK) { 209 printf( NAME ": Error resolving host '%s'.\n", argv[argi]);266 printf("Error resolving host '%s'.\n", argv[optind]); 210 267 goto error; 211 268 } 212 269 213 270 dest_addr = hinfo->addr; 214 271 } 215 272 273 uint16_t af = inet_addr_get(&dest_addr, &dest, NULL); 274 if (af != AF_INET) { 275 printf("Destination '%s' is not an IPv4 address.\n", 276 argv[optind]); 277 goto error; 278 } 279 216 280 /* Determine source address */ 217 rc = inetping_get_srcaddr(&dest_addr, &src_addr); 218 if (rc != EOK) { 219 printf(NAME ": Failed determining source address.\n"); 220 goto error; 221 } 222 281 rc = inetping_get_srcaddr(dest, &src); 282 if (rc != EOK) { 283 printf("Failed determining source address.\n"); 284 goto error; 285 } 286 287 inet_addr_t src_addr; 288 inet_addr_set(src, &src_addr); 289 223 290 rc = inet_addr_format(&src_addr, &asrc); 224 291 if (rc != EOK) { 225 printf( NAME ":Out of memory.\n");226 goto error; 227 } 228 292 printf("Out of memory.\n"); 293 goto error; 294 } 295 229 296 rc = inet_addr_format(&dest_addr, &adest); 230 297 if (rc != EOK) { 231 printf( NAME ":Out of memory.\n");232 goto error; 233 } 234 298 printf("Out of memory.\n"); 299 goto error; 300 } 301 235 302 if (hinfo != NULL) { 236 303 rc = asprintf(&sdest, "%s (%s)", hinfo->cname, adest); 237 304 if (rc < 0) { 238 printf( NAME ":Out of memory.\n");305 printf("Out of memory.\n"); 239 306 goto error; 240 307 } … … 243 310 adest = NULL; 244 311 } 245 246 printf("Sending ICMP echo request from %s to %s .\n",312 313 printf("Sending ICMP echo request from %s to %s (Ctrl+Q to quit)\n", 247 314 asrc, sdest); 248 249 fid_t fid; 250 251 if (ping_repeat) { 252 fid = fibril_create(transmit_fibril, NULL); 253 if (fid == 0) { 254 printf(NAME ": Failed creating transmit fibril.\n"); 255 goto error; 256 } 257 258 fibril_add_ready(fid); 259 260 fid = fibril_create(input_fibril, NULL); 261 if (fid == 0) { 262 printf(NAME ": Failed creating input fibril.\n"); 263 goto error; 264 } 265 266 fibril_add_ready(fid); 267 } else { 268 ping_send(1); 269 } 270 271 fibril_mutex_lock(&done_lock); 272 rc = EOK; 273 while (!done && rc != ETIMEOUT) { 274 rc = fibril_condvar_wait_timeout(&done_cv, &done_lock, 275 ping_repeat ? 0 : PING_TIMEOUT); 276 } 277 fibril_mutex_unlock(&done_lock); 278 279 if (rc == ETIMEOUT) { 280 printf(NAME ": Echo request timed out.\n"); 281 goto error; 282 } 283 315 316 fid_t fid = fibril_create(transmit_fibril, NULL); 317 if (fid == 0) { 318 printf("Failed creating transmit fibril.\n"); 319 goto error; 320 } 321 322 fibril_add_ready(fid); 323 324 fid = fibril_create(input_fibril, NULL); 325 if (fid == 0) { 326 printf("Failed creating input fibril.\n"); 327 goto error; 328 } 329 330 fibril_add_ready(fid); 331 332 fibril_mutex_lock(&quit_lock); 333 while (!quit) 334 fibril_condvar_wait(&quit_cv, &quit_lock); 335 fibril_mutex_unlock(&quit_lock); 336 284 337 free(asrc); 285 338 free(adest); … … 287 340 dnsr_hostinfo_destroy(hinfo); 288 341 return 0; 342 289 343 error: 290 344 free(asrc);
Note:
See TracChangeset
for help on using the changeset viewer.