Changeset 03c971f in mainline for uspace/app


Ignore:
Timestamp:
2013-08-15T14:20:16Z (12 years ago)
Author:
Jan Vesely <jano.vesely@…>
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.
Message:

Mainline changes.

Location:
uspace/app
Files:
2 added
4 deleted
27 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/Makefile

    rf2c19b0 r03c971f  
    2929
    3030USPACE_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
     31LIBS = $(LIBCLUI_PREFIX)/libclui.a $(LIBFMTUTIL_PREFIX)/libfmtutil.a
     32EXTRA_CFLAGS = -I$(LIBCLUI_PREFIX) -I$(LIBFMTUTIL_PREFIX) \
     33        -I. -Icmds/ -Icmds/builtins -Icmds/modules
    3534BINARY = bdsh
    3635
     
    4039        cmds/modules/mkfile/mkfile.c \
    4140        cmds/modules/rm/rm.c \
    42         cmds/modules/bdd/bdd.c \
    4341        cmds/modules/cat/cat.c \
    4442        cmds/modules/touch/touch.c \
  • uspace/app/bdsh/cmds/builtins/batch/batch.c

    rf2c19b0 r03c971f  
    2929#include <stdio.h>
    3030#include <stdlib.h>
     31#include <stdbool.h>
     32#include <errno.h>
    3133#include "config.h"
    3234#include "util.h"
     
    4446        if (level == HELP_SHORT) {
    4547                printf(
    46                 "\n  batch [filename]\n"
     48                "\n  batch [filename] [-c]\n"
    4749                "  Issues commands stored in the file.\n"
    4850                "  Each command must correspond to the single line in the file.\n\n");
     
    5456                "  separate groups of commands. There is no support for comments,\n"
    5557                "  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");
    5761        }
    5862
     
    6569{
    6670        unsigned int argc;
     71        bool continue_despite_errors = false;
    6772
    6873        /* Count the arguments */
     
    7277                printf("%s - no input file provided.\n", cmdname);
    7378                return CMD_FAILURE;
     79        }
     80
     81        if (argc > 2) {
     82                if (str_cmp(argv[2], "-c") == 0)
     83                        continue_despite_errors = true;
    7484        }
    7585
     
    99109                                        rc = process_input(&fusr);
    100110                                        /* fusr->line was freed by process_input() */
     111                                        if ((rc != EOK) && continue_despite_errors) {
     112                                                /* Mute the error. */
     113                                                rc = EOK;
     114                                        }
    101115                                }
    102116                                if (rc == 0 && c != EOF) {
  • uspace/app/bdsh/cmds/modules/cat/cat.c

    rf2c19b0 r03c971f  
    6363static bool should_quit = false;
    6464static bool dash_represents_stdin = false;
     65static unsigned int lineno = 0;
     66static bool number = false;
     67static bool last_char_was_newline = false;
    6568
    6669static console_ctrl_t *console = NULL;
     
    7578        { "hex", no_argument, 0, 'x' },
    7679        { "stdin", no_argument, 0, 's' },
     80        { "number", no_argument, 0, 'n' },
    7781        { 0, 0, 0, 0 }
    7882};
     
    9599                "  -m, --more       Pause after each screen full\n"
    96100                "  -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"
    98103                "Currently, %s is under development, some options don't work.\n",
    99104                cmdname, cmdname);
     
    153158static void paged_char(wchar_t c)
    154159{
     160        if (last_char_was_newline && number) {
     161                lineno++;
     162                printf("%6u  ", lineno);
     163        }
    155164        putchar(c);
     165        last_char_was_newline = c == '\n';
    156166        if (paging_enabled) {
    157167                chars_remaining--;
     
    306316        should_quit = false;
    307317        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
    308323
    309324        argc = cli_count_args(argv);
    310325
    311326        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);
    313328                switch (c) {
    314329                case 'h':
     
    347362                        dash_represents_stdin = true;
    348363                        break;
     364                case 'n':
     365                        number = true;
     366                        break;
    349367                }
    350368        }
  • uspace/app/bdsh/cmds/modules/help/help.c

    rf2c19b0 r03c971f  
    9898        builtin_t *cmd;
    9999        module_t *mod;
    100         unsigned int i;
    101100
    102101        printf("\n  Bdsh built-in commands:\n");
     
    104103
    105104        /* 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++) {
    107106                        if (is_builtin_alias(cmd->name))
    108107                                printf("   %-16s\tAlias for `%s'\n", cmd->name,
     
    112111        }
    113112
    114         i = 0;
    115 
    116113        /* 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++) {
    118115                        if (is_module_alias(mod->name))
    119116                                printf("   %-16s\tAlias for `%s'\n", mod->name,
  • uspace/app/bdsh/cmds/modules/modules.h

    rf2c19b0 r03c971f  
    5050#include "mkfile/entry.h"
    5151#include "rm/entry.h"
    52 #include "bdd/entry.h"
    5352#include "cat/entry.h"
    5453#include "touch/entry.h"
     
    7473#include "mkfile/mkfile_def.h"
    7574#include "rm/rm_def.h"
    76 #include "bdd/bdd_def.h"
    7775#include "cat/cat_def.h"
    7876#include "touch/touch_def.h"
  • uspace/app/bdsh/cmds/modules/pwd/pwd.c

    rf2c19b0 r03c971f  
    5555        }
    5656
    57         memset(buff, 0, sizeof(buff));
     57        memset(buff, 0, PATH_MAX);
    5858        getcwd(buff, PATH_MAX);
    5959
  • uspace/app/bdsh/cmds/modules/rm/rm.c

    rf2c19b0 r03c971f  
    9999        if (NULL == (rm->nwd = (char *) malloc(PATH_MAX)))
    100100                return 0;
    101         memset(rm->nwd, 0, sizeof(rm->nwd));
     101        memset(rm->nwd, 0, PATH_MAX);
    102102
    103103        if (NULL == (rm->owd = (char *) malloc(PATH_MAX)))
    104104                return 0;
    105         memset(rm->owd, 0, sizeof(rm->owd));
     105        memset(rm->owd, 0, PATH_MAX);
    106106
    107107        if (NULL == (rm->cwd = (char *) malloc(PATH_MAX)))
    108108                return 0;
    109         memset(rm->cwd, 0, sizeof(rm->cwd));
     109        memset(rm->cwd, 0, PATH_MAX);
    110110
    111111        chdir(".");
     
    298298                        break;
    299299                }
    300                 memset(buff, 0, sizeof(buff));
     300                memset(buff, 0, len);
    301301                snprintf(buff, len, "%s", argv[i]);
    302302
  • uspace/app/bdsh/cmds/modules/sleep/sleep.c

    rf2c19b0 r03c971f  
    6767        uint64_t whole_seconds;
    6868        uint64_t frac_seconds;
    69         char *endptr;
     69        const char *endptr;
    7070
    7171        /* Check for whole seconds */
  • uspace/app/bdsh/exec.c

    rf2c19b0 r03c971f  
    8383        /* We now have n places to look for the command */
    8484        for (i = 0; search_dir[i] != NULL; i++) {
    85                 memset(found, 0, sizeof(found));
     85                memset(found, 0, PATH_MAX);
    8686                snprintf(found, PATH_MAX, "%s/%s", search_dir[i], cmd);
    8787                if (-1 != try_access(found)) {
  • uspace/app/binutils/intrusive.sh

    rf2c19b0 r03c971f  
    9696# libposix function name redefinitons in one of the arm-specific files.
    9797#
     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.
    98104
    99105case "$1" in
     
    157163                # Patch libiberty configure script.
    158164                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' \
    161168                > "$2/libiberty/configure"
    162169
  • uspace/app/binutils/toolchain.sh

    rf2c19b0 r03c971f  
    7272                echo '  ld $LD_ARGS'
    7373                echo 'else'
    74                 CFLAGS="`echo " $3" | \
     74                CFLAGS="`echo " $3 " | \
    7575                        sed 's/ -O[^ ]*//g' | \
    7676                        sed 's/ -W[^ ]*//g' | \
    7777                        sed 's/ -pipe//g' | \
    78                         sed 's/ -g//g' | \
     78                        sed 's/ -g[^ ]*//g' | \
    7979                        sed 's/ [ ]*/ /g'`"
    8080                echo '  CONFTEST="`echo \"$*\" | grep '\' conftest \''`"'
  • uspace/app/blkdump/blkdump.c

    rf2c19b0 r03c971f  
    11/*
    22 * Copyright (c) 2011 Martin Sucha
    3  * Copyright (c) 2010 Jiri Svoboda
     3 * Copyright (c) 2013 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    5252
    5353static void syntax_print(void);
     54static int print_blocks(aoff64_t block_offset, aoff64_t block_count, size_t block_size);
     55static int print_toc(void);
    5456static void print_hex_row(uint8_t *data, size_t length, size_t bytes_per_row);
     57
     58static bool relative = false;
     59static service_id_t service_id;
    5560
    5661int main(int argc, char **argv)
     
    5964        int rc;
    6065        char *dev_path;
    61         service_id_t service_id;
    6266        size_t block_size;
    6367        char *endptr;
     
    6569        aoff64_t block_count = 1;
    6670        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;
    7272       
    7373        if (argc < 2) {
     
    7979        --argc; ++argv;
    8080
     81        if (str_cmp(*argv, "--toc") == 0) {
     82                --argc; ++argv;
     83                toc = true;
     84                goto devname;
     85        }
     86
    8187        if (str_cmp(*argv, "--relative") == 0) {
    8288                --argc; ++argv;
     
    120126        }
    121127
     128devname:
    122129        if (argc != 1) {
    123130                printf(NAME ": Error, unexpected argument.\n");
     
    153160        printf("Device %s has %" PRIuOFF64 " blocks, %" PRIuOFF64 " bytes each\n", dev_path, dev_nblocks, (aoff64_t) block_size);
    154161
     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
     172static 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
    155180        data = malloc(block_size);
    156181        if (data == NULL) {
    157182                printf(NAME ": Error allocating data buffer of %" PRIuOFF64 " bytes", (aoff64_t) block_size);
    158                 block_fini(service_id);
    159183                return 3;
    160184        }
    161        
     185
    162186        limit = block_offset + block_count;
    163187        for (current = block_offset; current < limit; current++) {
     
    168192                        return 3;
    169193                }
    170                
     194
    171195                printf("---- Block %" PRIuOFF64 " (at %" PRIuOFF64 ") ----\n", current, current*block_size);
    172                
     196
    173197                for (data_offset = 0; data_offset < block_size; data_offset += 16) {
    174198                        if (relative) {
     
    183207                printf("\n");
    184208        }
    185        
     209
    186210        free(data);
    187 
    188         block_fini(service_id);
     211        return 0;
     212}
     213
     214static 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);
    189225
    190226        return 0;
  • uspace/app/dnscfg/dnscfg.c

    rf2c19b0 r03c971f  
    4949static void print_syntax(void)
    5050{
    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);
    5455}
    5556
    5657static int dnscfg_set_ns(int argc, char *argv[])
    5758{
    58         char *srv_addr;
    59         inet_addr_t addr;
    60         int rc;
    61 
    6259        if (argc < 1) {
    63                 printf(NAME ": Missing arguments.\n");
     60                printf("%s: Missing arguments.\n", NAME);
    6461                print_syntax();
    6562                return EINVAL;
    6663        }
    67 
     64       
    6865        if (argc > 1) {
    69                 printf(NAME ": Too many arguments.\n");
     66                printf("%s: Too many arguments.\n", NAME);
    7067                print_syntax();
    7168                return EINVAL;
    7269        }
    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       
    7776        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;
    8079        }
    81 
     80       
    8281        rc = dnsr_set_srvaddr(&addr);
    8382        if (rc != EOK) {
    84                 printf(NAME ": Failed setting server 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;
    8786        }
    88 
     87       
    8988        return EOK;
    9089}
    9190
    92 static int dnscfg_unset_ns(int argc, char *argv[])
     91static int dnscfg_unset_ns(void)
    9392{
    9493        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;
    101101        }
    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       
    111103        return EOK;
    112104}
     
    115107{
    116108        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);
    121110        if (rc != EOK) {
    122                 printf(NAME ": Failed getting DNS server address.\n");
     111                printf("%s: Failed getting DNS server address.\n", NAME);
    123112                return rc;
    124113        }
    125 
     114       
     115        char *addr_str;
    126116        rc = inet_addr_format(&addr, &addr_str);
    127117        if (rc != EOK) {
    128                 printf(NAME ": Out of memory.\n");
     118                printf("%s: Out of memory.\n", NAME);
    129119                return rc;
    130120        }
    131 
    132         printf("Server: %s\n", addr_str);
     121       
     122        printf("Nameserver: %s\n", addr_str);
    133123        free(addr_str);
    134124        return EOK;
     
    137127int main(int argc, char *argv[])
    138128{
    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]);
    158137                print_syntax();
    159138                return 1;
    160139        }
    161 
    162 
     140       
    163141        return 0;
    164142}
  • uspace/app/dnsres/dnsres.c

    rf2c19b0 r03c971f  
    3636#include <inet/addr.h>
    3737#include <inet/dnsr.h>
     38#include <net/socket_codes.h>
    3839#include <stdio.h>
    3940#include <stdlib.h>
    4041
    41 #define NAME "dnsres"
     42#define NAME  "dnsres"
    4243
    4344static void print_syntax(void)
    4445{
    45         printf("syntax: " NAME " <host-name>\n");
     46        printf("Syntax: %s [-4|-6] <host-name>\n", NAME);
    4647}
    4748
    4849int main(int argc, char *argv[])
    4950{
    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)) {
    5652                print_syntax();
    5753                return 1;
    5854        }
    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);
    6382        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;
    6685        }
    67 
     86       
     87        char *saddr;
    6888        rc = inet_addr_format(&hinfo->addr, &saddr);
    6989        if (rc != EOK) {
    7090                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;
    7393        }
    74 
     94       
    7595        printf("Host name: %s\n", hname);
     96       
    7697        if (str_cmp(hname, hinfo->cname) != 0)
    7798                printf("Canonical name: %s\n", hinfo->cname);
     99       
    78100        printf("Address: %s\n", saddr);
    79 
     101       
    80102        dnsr_hostinfo_destroy(hinfo);
    81103        free(saddr);
    82 
     104       
    83105        return 0;
    84106}
  • uspace/app/edit/search_impl.h

    rf2c19b0 r03c971f  
    4040
    4141/** Search state */
    42 typedef struct search {
     42struct search {
    4343        /* Note: This structure is opaque for the user. */
    4444
     
    4949        void *client_data;
    5050        search_ops_t ops;
    51 } search_t;
     51};
    5252
    5353#endif
  • uspace/app/edit/sheet_impl.h

    rf2c19b0 r03c971f  
    4040
    4141/** Sheet */
    42 typedef struct sheet {
     42struct sheet {
    4343        /* Note: This structure is opaque for the user. */
    4444
     
    4848
    4949        list_t tags;
    50 } sheet_t;
     50};
    5151
    5252#endif
  • uspace/app/inet/inet.c

    rf2c19b0 r03c971f  
    4444#include <sys/types.h>
    4545
    46 #define NAME "inet"
     46#define NAME  "inet"
    4747
    4848static void print_syntax(void)
     
    257257        if (count > 0)
    258258                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;
    260262
    261263        for (i = 0; i < count; i++) {
     
    290292                free(astr);
    291293
    292                 ainfo.name = linfo.name = astr = NULL;
     294                ainfo.name = NULL;
     295                linfo.name = NULL;
     296                astr = NULL;
    293297        }
    294298
     
    329333                printf("    [Dest/Width] [Router-Addr] [Route-Name]\n");
    330334
    331         srinfo.name = dest_str = router_str = NULL;
     335        srinfo.name = NULL;
     336        dest_str = NULL;
     337        router_str = NULL;
    332338
    333339        for (i = 0; i < count; i++) {
     
    360366                free(router_str);
    361367
    362                 router_str = srinfo.name = dest_str = NULL;
     368                router_str = NULL;
     369                srinfo.name = NULL;
     370                dest_str = NULL;
    363371        }
    364372
  • uspace/app/mkmfs/mkmfs.c

    rf2c19b0 r03c971f  
    649649                mark_bmap(ibmap_buf, i, FREE);
    650650
    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)
    652652                mark_bmap(zbmap_buf, i, FREE);
    653653
  • uspace/app/netecho/netecho.c

    rf2c19b0 r03c971f  
    225225        uint8_t address_buf[sizeof(struct sockaddr_in6)];
    226226
    227         socklen_t addrlen;
     227        socklen_t addrlen = sizeof(struct sockaddr_in6);
    228228        int socket_id;
    229229        ssize_t rcv_size;
     
    240240        if (type == SOCK_STREAM) {
    241241                /* Accept a socket if a stream socket is used */
    242                 addrlen = sizeof(address_buf);
    243242                if (verbose)
    244243                        printf("accept()\n");
     
    280279                                case AF_INET6:
    281280                                        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;
    283282                                        break;
    284283                                default:
     
    380379                address_in6.sin6_family = AF_INET6;
    381380                address_in6.sin6_port = htons(port);
     381                address_in6.sin6_addr = in6addr_any;
    382382                address = (struct sockaddr *) &address_in6;
    383383                addrlen = sizeof(address_in6);
  • uspace/app/nettest1/nettest.c

    rf2c19b0 r03c971f  
    4141#include "print_error.h"
    4242
    43 
    4443/** Creates new sockets.
    4544 *
     
    5251 * @return Other error codes as defined for the socket() function.
    5352 */
    54 int sockets_create(int verbose, int *socket_ids, int sockets, int family, sock_type_t type)
    55 {
    56         int index;
    57 
     53int sockets_create(int verbose, int *socket_ids, unsigned int sockets,
     54    uint16_t family, sock_type_t type)
     55{
    5856        if (verbose)
    5957                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++) {
    6462                socket_ids[index] = socket(family, type, 0);
    6563                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]);
    6765                        socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n");
    6866                        return socket_ids[index];
    6967                }
     68               
    7069                if (verbose)
    7170                        print_mark(index);
     
    8382 * @return Other error codes as defined for the closesocket() function.
    8483 */
    85 int sockets_close(int verbose, int *socket_ids, int sockets)
    86 {
    87         int index;
    88         int rc;
    89 
     84int sockets_close(int verbose, int *socket_ids, unsigned int sockets)
     85{
    9086        if (verbose)
    9187                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]);
    9793                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]);
    9995                        socket_print_error(stderr, rc, "Socket close: ", "\n");
    10096                        return rc;
    10197                }
     98               
    10299                if (verbose)
    103100                        print_mark(index);
     
    117114 * @return Other error codes as defined for the connect() function.
    118115 */
    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 
     116int sockets_connect(int verbose, int *socket_ids, unsigned int sockets,
     117    struct sockaddr *address, socklen_t addrlen)
     118{
    124119        if (verbose)
    125120                printf("\tConnect\t");
     
    127122        fflush(stdout);
    128123       
    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);
    131126                if (rc != EOK) {
    132127                        socket_print_error(stderr, rc, "Socket connect: ", "\n");
    133128                        return rc;
    134129                }
    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 *
    152150 * @return EOK on success.
    153151 * @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 */
     154int 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{
    161158        if (verbose)
    162159                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                       
    169178                        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);
    171181                                socket_print_error(stderr, rc, "Socket send: ", "\n");
    172182                                return rc;
    173183                        }
    174184                }
    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 *
    192205 * @return EOK on success.
    193206 * @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 */
     209int 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{
    201213        if (verbose)
    202214                printf("\tRecvfrom\t");
     
    204216        fflush(stdout);
    205217       
    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.
    222237 *
    223238 * Each datagram is sent and a reply read consequently.
    224239 * The next datagram is sent after the reply is received.
    225240 *
    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 *
    234252 * @return EOK on success.
    235253 * @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 */
     256int 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{
    244260        if (verbose)
    245261                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                       
    252280                        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);
    254283                                socket_print_error(stderr, rc, "Socket send: ", "\n");
    255284                                return rc;
    256285                        }
    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               
    264296                if (verbose)
    265297                        print_mark(index);
     
    274306 *
    275307 * @param[in] index The index of the mark to be printed.
    276  */
    277 void print_mark(int index)
     308 *
     309 */
     310void print_mark(unsigned int index)
    278311{
    279312        if ((index + 1) % 10)
     
    281314        else
    282315                printf("|");
     316       
    283317        fflush(stdout);
    284318}
  • uspace/app/nettest1/nettest.h

    rf2c19b0 r03c971f  
    4040#include <net/socket.h>
    4141
    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);
     42extern void print_mark(unsigned int);
     43extern int sockets_create(int, int *, unsigned int, uint16_t, sock_type_t);
     44extern int sockets_close(int, int *, unsigned int);
     45extern int sockets_connect(int, int *, unsigned int, struct sockaddr *,
     46    socklen_t);
     47extern int sockets_sendto(int, int *, unsigned int, struct sockaddr *,
     48    socklen_t, char *, size_t, unsigned int, sock_type_t);
     49extern int sockets_recvfrom(int, int *, unsigned int, struct sockaddr *,
     50    socklen_t *, char *, size_t, unsigned int);
     51extern int sockets_sendto_recvfrom(int, int *, unsigned int, struct sockaddr *,
     52    socklen_t *, char *, size_t, unsigned int, sock_type_t);
    4953
    5054#endif
  • uspace/app/nettest1/nettest1.c

    rf2c19b0 r03c971f  
    5454
    5555/** Echo module name. */
    56 #define NAME    "Nettest1"
     56#define NAME  "nettest1"
    5757
    5858/** Packet data pattern. */
    59 #define NETTEST1_TEXT   "Networking test 1 - sockets"
    60 
    61 static int family = PF_INET;
     59#define NETTEST1_TEXT  "Networking test 1 - sockets"
     60
     61static uint16_t family = AF_INET;
    6262static sock_type_t type = SOCK_DGRAM;
    63 static char *data;
    6463static size_t size = 27;
    65 static int verbose = 0;
     64static bool verbose = false;
     65static int sockets = 10;
     66static int messages = 10;
     67static uint16_t port = 7;
    6668
    6769static struct sockaddr *address;
    6870static socklen_t addrlen;
    6971
    70 static int sockets;
    71 static int messages;
    72 static uint16_t port;
     72static char *data;
    7373
    7474static void nettest1_print_help(void)
     
    114114        int value;
    115115        int rc;
    116 
     116       
    117117        switch (argv[*index][1]) {
    118118        /*
     
    120120         */
    121121        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;
    125128                break;
    126129        case 'h':
     
    131134                if (rc != EOK)
    132135                        return rc;
     136               
    133137                break;
    134138        case 'n':
     
    136140                if (rc != EOK)
    137141                        return rc;
     142               
    138143                break;
    139144        case 'p':
     
    141146                if (rc != EOK)
    142147                        return rc;
     148               
    143149                port = (uint16_t) value;
    144150                break;
     
    147153                if (rc != EOK)
    148154                        return rc;
     155               
    149156                size = (value >= 0) ? (size_t) value : 0;
    150157                break;
    151158        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               
    155164                type = (sock_type_t) value;
    156165                break;
     
    158167                verbose = 1;
    159168                break;
     169       
    160170        /*
    161171         * Long options with double dash ('-')
     
    163173        case '-':
    164174                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,
    166176                            socket_parse_protocol_family);
    167177                        if (rc != EOK)
    168178                                return rc;
     179                       
     180                        family = (uint16_t) value;
    169181                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
    170182                        nettest1_print_help();
     
    182194                        if (rc != EOK)
    183195                                return rc;
     196                       
    184197                        port = (uint16_t) value;
    185198                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     
    188201                        if (rc != EOK)
    189202                                return rc;
     203                       
    190204                        type = (sock_type_t) value;
    191205                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     
    200214                return EINVAL;
    201215        }
    202 
     216       
    203217        return EOK;
    204218}
     
    211225static void nettest1_fill_buffer(char *buffer, size_t size)
    212226{
    213         size_t length;
    214 
    215         length = 0;
     227        size_t length = 0;
    216228        while (size > length + sizeof(NETTEST1_TEXT) - 1) {
    217229                memcpy(buffer + length, NETTEST1_TEXT,
     
    219231                length += sizeof(NETTEST1_TEXT) - 1;
    220232        }
    221 
     233       
    222234        memcpy(buffer + length, NETTEST1_TEXT, size - length);
    223235        buffer[size] = '\0';
     
    226238static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
    227239{
    228         int rc;
    229 
    230240        if (verbose)
    231241                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       
    237247        if (type == SOCK_STREAM) {
    238248                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    241251                        return rc;
    242252        }
    243 
     253       
    244254        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       
    249259        rc = sockets_close(verbose, socket_ids, nsockets);
    250260        if (rc != EOK)
    251261                return rc;
    252 
     262       
    253263        if (verbose)
    254264                printf("\tOK\n");
    255 
     265       
    256266        /****/
    257 
     267       
    258268        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    259269        if (rc != EOK)
    260270                return rc;
    261 
     271       
    262272        if (type == SOCK_STREAM) {
    263273                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    266276                        return rc;
    267277        }
    268 
     278       
    269279        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       
    274284        rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
    275285            data, size, nmessages);
    276286        if (rc != EOK)
    277287                return rc;
    278 
     288       
    279289        rc = sockets_close(verbose, socket_ids, nsockets);
    280290        if (rc != EOK)
    281291                return rc;
    282 
     292       
    283293        if (verbose)
    284294                printf("\tOK\n");
    285 
     295       
    286296        return EOK;
    287297}
     
    289299int main(int argc, char *argv[])
    290300{
    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 
    307301        /*
    308302         * Parse the command line arguments. Stop before the last argument
    309303         * if it does not start with dash ('-')
    310304         */
    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++) {
    312310                /* Options should start with dash ('-') */
    313311                if (argv[index][0] == '-') {
     
    320318                }
    321319        }
    322 
    323         /* If not before the last argument containing the host */
     320       
     321        /* The last argument containing the host */
    324322        if (index >= argc) {
    325                 printf("Command line error: missing host name\n");
     323                printf("Host name missing.\n");
    326324                nettest1_print_help();
    327325                return EINVAL;
    328326        }
    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       
    330356        /* 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);
    346368                break;
    347369        default:
     
    349371                return EAFNOSUPPORT;
    350372        }
    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       
    365374        /* Check data buffer size */
    366375        if (size <= 0) {
     
    369378                size = 1024;
    370379        }
    371 
     380       
    372381        /*
    373382         * Prepare data buffer. Allocate size bytes plus one for the
     
    380389        }
    381390        nettest1_fill_buffer(data, size);
    382 
     391       
    383392        /* Check socket count */
    384393        if (sockets <= 0) {
     
    387396                sockets = 2;
    388397        }
    389 
     398       
    390399        /*
    391400         * Prepare socket buffer. Allocate count fields plus the terminating
    392401         * null (\0).
    393402         */
    394         socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
     403        int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    395404        if (!socket_ids) {
    396405                fprintf(stderr, "Failed to allocate receive buffer.\n");
    397406                return ENOMEM;
    398407        }
     408       
    399409        socket_ids[sockets] = 0;
    400 
     410       
    401411        if (verbose)
    402412                printf("Starting tests\n");
    403 
     413       
     414        struct timeval time_before;
    404415        rc = gettimeofday(&time_before, NULL);
    405416        if (rc != EOK) {
     
    407418                return rc;
    408419        }
    409 
     420       
    410421        nettest1_test(socket_ids,       1,        1);
    411422        nettest1_test(socket_ids,       1, messages);
    412423        nettest1_test(socket_ids, sockets,        1);
    413424        nettest1_test(socket_ids, sockets, messages);
    414 
     425       
     426        struct timeval time_after;
    415427        rc = gettimeofday(&time_after, NULL);
    416428        if (rc != EOK) {
     
    418430                return rc;
    419431        }
    420 
     432       
    421433        printf("Tested in %ld microseconds\n", tv_sub(&time_after,
    422434            &time_before));
    423 
     435       
    424436        if (verbose)
    425437                printf("Exiting\n");
    426 
     438       
    427439        return EOK;
    428440}
    429441
    430 
    431442/** @}
    432443 */
  • uspace/app/nettest2/nettest2.c

    rf2c19b0 r03c971f  
    5555
    5656/** Echo module name. */
    57 #define NAME    "Nettest2"
     57#define NAME  "nettest2"
    5858
    5959/** Packet data pattern. */
    60 #define NETTEST2_TEXT   "Networking test 2 - transfer"
    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
     62static uint16_t family = PF_INET;
     63static size_t size = 28;
     64static bool verbose = false;
     65static sock_type_t type = SOCK_DGRAM;
     66static int sockets = 10;
     67static int messages = 10;
     68static uint16_t port = 7;
    6969
    7070static void nettest2_print_help(void)
     
    107107static void nettest2_fill_buffer(char *buffer, size_t size)
    108108{
    109         size_t length;
    110 
    111         length = 0;
     109        size_t length = 0;
    112110        while (size > length + sizeof(NETTEST2_TEXT) - 1) {
    113111                memcpy(buffer + length, NETTEST2_TEXT,
     
    115113                length += sizeof(NETTEST2_TEXT) - 1;
    116114        }
    117 
     115       
    118116        memcpy(buffer + length, NETTEST2_TEXT, size - length);
    119117        buffer[size] = '\0';
     
    130128        int value;
    131129        int rc;
    132 
     130       
    133131        switch (argv[*index][1]) {
    134132        /*
     
    136134         */
    137135        case 'f':
    138                 rc = arg_parse_name_int(argc, argv, index, &family, 0,
     136                rc = arg_parse_name_int(argc, argv, index, &value, 0,
    139137                    socket_parse_protocol_family);
    140138                if (rc != EOK)
    141139                        return rc;
     140               
     141                family = (uint16_t) value;
    142142                break;
    143143        case 'h':
    144144                nettest2_print_help();
    145145                return EOK;
    146                 break;
    147146        case 'm':
    148147                rc = arg_parse_int(argc, argv, index, &messages, 0);
    149148                if (rc != EOK)
    150149                        return rc;
     150               
    151151                break;
    152152        case 'n':
     
    154154                if (rc != EOK)
    155155                        return rc;
     156               
    156157                break;
    157158        case 'p':
     
    159160                if (rc != EOK)
    160161                        return rc;
     162               
    161163                port = (uint16_t) value;
    162164                break;
     
    165167                if (rc != EOK)
    166168                        return rc;
     169               
    167170                size = (value >= 0) ? (size_t) value : 0;
    168171                break;
     
    172175                if (rc != EOK)
    173176                        return rc;
     177               
    174178                type = (sock_type_t) value;
    175179                break;
     
    177181                verbose = true;
    178182                break;
     183       
    179184        /*
    180185         * Long options with double dash ('-')
     
    182187        case '-':
    183188                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,
    185190                            socket_parse_protocol_family);
    186191                        if (rc != EOK)
    187192                                return rc;
     193                       
     194                        family = (uint16_t) value;
    188195                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
    189196                        nettest2_print_help();
     
    201208                        if (rc != EOK)
    202209                                return rc;
     210                       
    203211                        port = (uint16_t) value;
    204212                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     
    207215                        if (rc != EOK)
    208216                                return rc;
     217                       
    209218                        type = (sock_type_t) value;
    210219                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     
    219228                return EINVAL;
    220229        }
    221 
     230       
    222231        return EOK;
    223232}
     
    225234int main(int argc, char *argv[])
    226235{
    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;
    236236        int index;
    237         struct timeval time_before;
    238         struct timeval time_after;
    239 
    240237        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       
    250239        /*
    251240         * Parse the command line arguments.
     
    255244        for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
    256245            (argv[index][0] == '-')); ++index) {
    257 
    258246                /* Options should start with dash ('-') */
    259247                if (argv[index][0] == '-') {
     
    266254                }
    267255        }
    268 
    269         /* If not before the last argument containing the host */
     256       
     257        /* The last argument containing the host */
    270258        if (index >= argc) {
    271                 printf("Command line error: missing host name\n");
     259                printf("Host name missing.\n");
    272260                nettest2_print_help();
    273261                return EINVAL;
    274262        }
    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       
    276292        /* 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);
    292307                break;
    293308        default:
     
    295310                return EAFNOSUPPORT;
    296311        }
    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       
    311313        /* Check data buffer size. */
    312314        if (size <= 0) {
     
    315317                size = 1024;
    316318        }
    317 
     319       
    318320        /*
    319321         * Prepare the buffer. Allocate size bytes plus one for terminating
    320322         * null character.
    321323         */
    322         data = (char *) malloc(size + 1);
     324        char *data = (char *) malloc(size + 1);
    323325        if (!data) {
    324326                fprintf(stderr, "Failed to allocate data buffer.\n");
    325327                return ENOMEM;
    326328        }
    327 
     329       
    328330        /* Fill buffer with a pattern. */
    329331        nettest2_fill_buffer(data, size);
    330 
     332       
    331333        /* Check socket count. */
    332334        if (sockets <= 0) {
     
    335337                sockets = 2;
    336338        }
    337 
     339       
    338340        /*
    339341         * Prepare the socket buffer.
    340342         * Allocate count entries plus the terminating null (\0)
    341343         */
    342         socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
     344        int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    343345        if (!socket_ids) {
    344346                fprintf(stderr, "Failed to allocate receive buffer.\n");
    345347                return ENOMEM;
    346348        }
     349       
    347350        socket_ids[sockets] = 0;
    348 
     351       
    349352        if (verbose)
    350353                printf("Starting tests\n");
    351 
     354       
    352355        rc = sockets_create(verbose, socket_ids, sockets, family, type);
    353356        if (rc != EOK)
    354357                return rc;
    355 
     358       
    356359        if (type == SOCK_STREAM) {
    357360                rc = sockets_connect(verbose, socket_ids, sockets,
     
    360363                        return rc;
    361364        }
    362 
     365       
    363366        if (verbose)
    364367                printf("\n");
    365 
     368       
     369        struct timeval time_before;
    366370        rc = gettimeofday(&time_before, NULL);
    367371        if (rc != EOK) {
     
    369373                return rc;
    370374        }
    371 
     375       
    372376        rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address,
    373             &addrlen, data, size, messages);
     377            &addrlen, data, size, messages, type);
    374378        if (rc != EOK)
    375379                return rc;
    376 
     380       
     381        struct timeval time_after;
    377382        rc = gettimeofday(&time_after, NULL);
    378383        if (rc != EOK) {
     
    380385                return rc;
    381386        }
    382 
     387       
    383388        if (verbose)
    384389                printf("\tOK\n");
    385 
     390       
    386391        printf("sendto + recvfrom tested in %ld microseconds\n",
    387392            tv_sub(&time_after, &time_before));
    388 
     393       
    389394        rc = gettimeofday(&time_before, NULL);
    390395        if (rc != EOK) {
     
    392397                return rc;
    393398        }
    394 
     399       
    395400        rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen,
    396             data, size, messages);
     401            data, size, messages, type);
    397402        if (rc != EOK)
    398403                return rc;
    399 
     404       
    400405        rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen,
    401406            data, size, messages);
    402407        if (rc != EOK)
    403408                return rc;
    404 
     409       
    405410        rc = gettimeofday(&time_after, NULL);
    406411        if (rc != EOK) {
     
    408413                return rc;
    409414        }
    410 
     415       
    411416        if (verbose)
    412417                printf("\tOK\n");
    413 
     418       
    414419        printf("sendto, recvfrom tested in %ld microseconds\n",
    415420            tv_sub(&time_after, &time_before));
    416 
     421       
    417422        rc = sockets_close(verbose, socket_ids, sockets);
    418423        if (rc != EOK)
    419424                return rc;
    420 
     425       
    421426        if (verbose)
    422427                printf("\nExiting\n");
    423 
     428       
    424429        return EOK;
    425430}
  • uspace/app/nettest3/nettest3.c

    rf2c19b0 r03c971f  
    7878                if (rc != EOK) {
    7979                        /* Try interpreting as a host name */
    80                         rc = dnsr_name2host(argv[1], &hinfo);
     80                        rc = dnsr_name2host(argv[1], &hinfo, AF_INET);
    8181                        if (rc != EOK) {
    8282                                printf("Error resolving host '%s'.\n", argv[1]);
    8383                                return rc;
    8484                        }
    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                        }
    8891                }
    8992                printf("result: rc=%d, family=%d, addr=%x\n", rc,
  • uspace/app/nterm/conn.c

    rf2c19b0 r03c971f  
    7575int conn_open(const char *addr_s, const char *port_s)
    7676{
    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       
    8583        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               
    8888                if (rc != EOK) {
    8989                        printf("Error resolving host '%s'.\n", addr_s);
    9090                        goto error;
    9191                }
    92 
    93                 addr.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);
     92               
     93                addr_addr = hinfo->addr;
    9494        }
    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);
    97102        if (*endptr != '\0') {
    98103                printf("Invalid port number %s\n", port_s);
    99104                goto error;
    100105        }
    101 
     106       
     107        printf("Connecting to host %s port %u\n", addr_s, port);
     108       
    102109        conn_fd = socket(PF_INET, SOCK_STREAM, 0);
    103110        if (conn_fd < 0)
    104111                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       
    109127        if (rc != EOK)
    110128                goto error;
    111 
     129       
    112130        rcv_fid = fibril_create(rcv_fibril, NULL);
    113131        if (rcv_fid == 0)
    114132                goto error;
    115 
     133       
    116134        fibril_add_ready(rcv_fid);
    117 
     135       
    118136        return EOK;
    119 
     137       
    120138error:
    121139        if (conn_fd >= 0) {
     
    123141                conn_fd = -1;
    124142        }
    125 
     143       
    126144        return EIO;
    127145}
     
    129147int conn_send(void *data, size_t size)
    130148{
    131         int rc;
    132 
    133         rc = send(conn_fd, data, size, 0);
     149        int rc = send(conn_fd, data, size, 0);
    134150        if (rc != EOK)
    135151                return EIO;
    136 
     152       
    137153        return EOK;
    138154}
  • uspace/app/pcc/config.h

    rf2c19b0 r03c971f  
    5050
    5151/* Define to 1 if you have the `mkstemp' function. */
    52 //#define HAVE_MKSTEMP 1
     52#define HAVE_MKSTEMP 1
    5353
    5454/* Define to 1 if you have the `snprintf' function. */
  • uspace/app/ping/ping.c

    rf2c19b0 r03c971f  
    3737#include <errno.h>
    3838#include <fibril_synch.h>
     39#include <net/socket_codes.h>
    3940#include <inet/dnsr.h>
    4041#include <inet/addr.h>
    4142#include <inet/inetping.h>
    4243#include <io/console.h>
     44#include <getopt.h>
    4345#include <stdio.h>
    4446#include <stdlib.h>
     47#include <str.h>
     48#include <str_error.h>
    4549#include <sys/types.h>
    4650
     
    5357#define PING_TIMEOUT (1000 * 1000)
    5458
     59typedef enum {
     60        RECEIVED_NONE,
     61        RECEIVED_SUCCESS,
     62        RECEIVED_INTERRUPT
     63} received_t;
     64
     65static received_t received;
     66static FIBRIL_CONDVAR_INITIALIZE(received_cv);
     67static FIBRIL_MUTEX_INITIALIZE(received_lock);
     68
     69static bool quit = false;
     70static FIBRIL_CONDVAR_INITIALIZE(quit_cv);
     71static FIBRIL_MUTEX_INITIALIZE(quit_lock);
     72
    5573static 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);
    6074
    6175static inetping_ev_ops_t ev_ops = {
     
    6377};
    6478
    65 static inet_addr_t src_addr;
    66 static inet_addr_t dest_addr;
    67 
    68 static bool ping_repeat = false;
     79static addr32_t src;
     80static addr32_t dest;
     81
     82static bool repeat_forever = false;
     83static size_t repeat_count = 1;
     84
     85static const char *short_options = "rn:";
    6986
    7087static void print_syntax(void)
    7188{
    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
     92static 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
     100static 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);
    81106}
    82107
    83108static int ping_ev_recv(inetping_sdu_t *sdu)
    84109{
    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);
    89118        if (rc != EOK)
    90119                return ENOMEM;
    91 
    92         rc = inet_addr_format(&sdu->dest, &adest);
     120       
     121        char *adest;
     122        rc = inet_addr_format(&dest_addr, &adest);
    93123        if (rc != EOK) {
    94124                free(asrc);
    95125                return ENOMEM;
    96126        }
     127       
    97128        printf("Received ICMP echo reply: from %s to %s, seq. no %u, "
    98129            "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       
    104133        free(asrc);
    105134        free(adest);
     
    110139{
    111140        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;
    116144        sdu.seq_no = seq_no;
    117145        sdu.data = (void *) "foo";
    118146        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;
    127154}
    128155
     
    130157{
    131158        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
     195static int input_fibril(void *arg)
     196{
     197        console_ctrl_t *con = console_init(stdin, stdout);
     198       
    133199        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;
    157201                if (!console_get_event(con, &ev))
    158202                        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)) {
    163207                        /* Ctrl+key */
    164208                        if (ev.ev.key.key == KC_Q) {
    165                                 ping_signal_done();
    166                                 return 0;
     209                                ping_signal_received(RECEIVED_INTERRUPT);
     210                                break;
    167211                        }
    168212                }
    169213        }
    170 
     214       
    171215        return 0;
    172216}
     
    178222        char *adest = NULL;
    179223        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");
    199255                print_syntax();
    200256                goto error;
    201257        }
    202 
     258       
    203259        /* 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);
    205262        if (rc != EOK) {
    206263                /* Try interpreting as a host name */
    207                 rc = dnsr_name2host(argv[argi], &hinfo);
     264                rc = dnsr_name2host(argv[optind], &hinfo, AF_INET);
    208265                if (rc != EOK) {
    209                         printf(NAME ": Error resolving host '%s'.\n", argv[argi]);
     266                        printf("Error resolving host '%s'.\n", argv[optind]);
    210267                        goto error;
    211268                }
    212 
     269               
    213270                dest_addr = hinfo->addr;
    214271        }
    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       
    216280        /* 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       
    223290        rc = inet_addr_format(&src_addr, &asrc);
    224291        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       
    229296        rc = inet_addr_format(&dest_addr, &adest);
    230297        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       
    235302        if (hinfo != NULL) {
    236303                rc = asprintf(&sdest, "%s (%s)", hinfo->cname, adest);
    237304                if (rc < 0) {
    238                         printf(NAME ": Out of memory.\n");
     305                        printf("Out of memory.\n");
    239306                        goto error;
    240307                }
     
    243310                adest = NULL;
    244311        }
    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",
    247314            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       
    284337        free(asrc);
    285338        free(adest);
     
    287340        dnsr_hostinfo_destroy(hinfo);
    288341        return 0;
     342       
    289343error:
    290344        free(asrc);
Note: See TracChangeset for help on using the changeset viewer.