Changeset 0b749a3 in mainline for uspace/app


Ignore:
Timestamp:
2010-11-22T15:39:53Z (15 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0eddb76, aae339e9
Parents:
9a1d8ab (diff), 8cd1aa5e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge development/ changes

Location:
uspace/app
Files:
13 added
25 edited
2 moved

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/builtins/builtins.h

    r9a1d8ab r0b749a3  
    1010#include "cd/cd_def.h"
    1111#include "exit/exit_def.h"
    12         {NULL, NULL, NULL, NULL, NULL}
     12        {NULL, NULL, NULL, NULL, 0}
    1313};
    1414
  • uspace/app/bdsh/cmds/modules/bdd/bdd.c

    r9a1d8ab r0b749a3  
    6868        unsigned int argc;
    6969        unsigned int i, j;
    70         dev_handle_t handle;
     70        devmap_handle_t handle;
    7171        uint8_t *blk;
    7272        size_t size, bytes, rows;
  • uspace/app/bdsh/cmds/modules/help/help.c

    r9a1d8ab r0b749a3  
    4545extern const char *progname;
    4646
    47 #define HELP_IS_MODULE   1
    48 #define HELP_IS_BUILTIN  0
    49 #define HELP_IS_RUBBISH  -1
     47#define HELP_IS_COMMANDS        2
     48#define HELP_IS_MODULE          1
     49#define HELP_IS_BUILTIN         0
     50#define HELP_IS_RUBBISH         -1
    5051
    5152volatile int mod_switch = -1;
     
    5556{
    5657        int rc = HELP_IS_RUBBISH;
     58
     59        if (str_cmp(cmd, "commands") == 0)
     60                return HELP_IS_COMMANDS;
    5761
    5862        rc = is_builtin(cmd);
     
    9094}
    9195
    92 int cmd_help(char *argv[])
     96static void help_commands(void)
    9397{
     98        builtin_t *cmd;
    9499        module_t *mod;
    95         builtin_t *cmd;
    96         unsigned int i = 0;
    97         int rc = 0;
    98         int argc;
    99         int level = HELP_SHORT;
     100        unsigned int i;
    100101
    101         argc = cli_count_args(argv);
    102 
    103         if (argc > 3) {
    104                 printf("\nToo many arguments to `%s', try:\n", cmdname);
    105                 help_cmd_help(HELP_SHORT);
    106                 return CMD_FAILURE;
    107         }
    108 
    109         if (argc == 3) {
    110                 if (!str_cmp("extended", argv[2]))
    111                         level = HELP_LONG;
    112                 else
    113                         level = HELP_SHORT;
    114         }
    115 
    116         if (argc > 1) {
    117                 rc = is_mod_or_builtin(argv[1]);
    118                 switch (rc) {
    119                 case HELP_IS_RUBBISH:
    120                         printf("Invalid command %s\n", argv[1]);
    121                         return CMD_FAILURE;
    122                 case HELP_IS_MODULE:
    123                         help_module(mod_switch, level);
    124                         return CMD_SUCCESS;
    125                 case HELP_IS_BUILTIN:
    126                         help_builtin(mod_switch, level);
    127                         return CMD_SUCCESS;
    128                 }
    129         }
    130 
    131         printf("\n  Available commands are:\n");
     102        printf("\n  Bdsh built-in commands:\n");
    132103        printf("  ------------------------------------------------------------\n");
    133104
     
    154125        printf("\n  Try %s %s for more information on how `%s' works.\n\n",
    155126                cmdname, cmdname, cmdname);
     127}
     128
     129/** Display survival tips. ('help' without arguments) */
     130static void help_survival(void)
     131{
     132        printf("Don't panic!\n\n");
     133
     134        printf("This is Bdsh, the Brain dead shell, currently "
     135            "the primary user interface to HelenOS. Bdsh allows you to enter "
     136            "commands and supports history (Up, Down arrow keys), "
     137            "line editing (Left Arrow, Right Arrow, Home, End, Backspace), "
     138            "selection (Shift + movement keys), copy and paste (Ctrl-C, "
     139            "Ctrl-V), similar to common desktop environments.\n\n");
     140
     141        printf("The most basic filesystem commands are Bdsh builtins. Type "
     142            "'help commands' [Enter] to see the list of Bdsh builtin commands. "
     143            "Other commands are external executables located in the /app and "
     144            "/srv directories. Type 'ls /app' [Enter] and 'ls /srv' [Enter] "
     145            "to see their list. You can execute an external command simply "
     146            "by entering its name (e.g. type 'tetris' [Enter]).\n\n");
     147
     148        printf("HelenOS has virtual consoles (VCs). You can switch between "
     149            "these using the F1-F11 keys.\n\n");
     150
     151        printf("This is but a small glimpse of what you can do with HelenOS. "
     152            "To learn more please point your browser to the HelenOS User's "
     153            "Guide: http://trac.helenos.org/trac.fcgi/wiki/UsersGuide\n\n");
     154}
     155
     156int cmd_help(char *argv[])
     157{
     158        int rc = 0;
     159        int argc;
     160        int level = HELP_SHORT;
     161
     162        argc = cli_count_args(argv);
     163
     164        if (argc > 3) {
     165                printf("\nToo many arguments to `%s', try:\n", cmdname);
     166                help_cmd_help(HELP_SHORT);
     167                return CMD_FAILURE;
     168        }
     169
     170        if (argc == 3) {
     171                if (!str_cmp("extended", argv[2]))
     172                        level = HELP_LONG;
     173                else
     174                        level = HELP_SHORT;
     175        }
     176
     177        if (argc > 1) {
     178                rc = is_mod_or_builtin(argv[1]);
     179                switch (rc) {
     180                case HELP_IS_RUBBISH:
     181                        printf("Invalid topic %s\n", argv[1]);
     182                        return CMD_FAILURE;
     183                case HELP_IS_COMMANDS:
     184                        help_commands();
     185                        return CMD_SUCCESS;
     186                case HELP_IS_MODULE:
     187                        help_module(mod_switch, level);
     188                        return CMD_SUCCESS;
     189                case HELP_IS_BUILTIN:
     190                        help_builtin(mod_switch, level);
     191                        return CMD_SUCCESS;
     192                }
     193        }
     194
     195        help_survival();
    156196
    157197        return CMD_SUCCESS;
  • uspace/app/bdsh/exec.c

    r9a1d8ab r0b749a3  
    4141#include <fcntl.h>
    4242#include <str_error.h>
     43#include <errno.h>
    4344
    4445#include "config.h"
     
    116117        task_exit_t texit;
    117118        char *tmp;
    118         int retval;
     119        int rc, retval;
    119120
    120121        tmp = str_dup(find_command(cmd));
    121122        free(found);
    122123
    123         tid = task_spawn(tmp, (const char **) argv, &retval);
     124        rc = task_spawnv(&tid, tmp, (const char **) argv);
    124125        free(tmp);
    125126
    126         if (tid == 0) {
     127        if (rc != 0) {
    127128                cli_error(CL_EEXEC, "%s: Cannot spawn `%s' (%s)", progname, cmd,
    128                     str_error(retval));
     129                    str_error(rc));
    129130                return 1;
    130131        }
    131132       
    132         task_wait(tid, &texit, &retval);
    133         if (texit != TASK_EXIT_NORMAL) {
     133        rc = task_wait(tid, &texit, &retval);
     134        if (rc != EOK) {
     135                printf("%s: Failed waiting for command (%s)\n", str_error(rc));
     136        } else if (texit != TASK_EXIT_NORMAL) {
    134137                printf("%s: Command failed (unexpectedly terminated)\n", progname);
    135138        } else if (retval != 0) {
    136                 printf("%s: Command failed (%s)\n",
    137                     progname, str_error(retval));
     139                printf("%s: Command failed (exit code %d)\n",
     140                    progname, retval);
    138141        }
    139142
  • uspace/app/bdsh/scli.c

    r9a1d8ab r0b749a3  
    8989                exit(EXIT_FAILURE);
    9090
    91         printf("Welcome to %s - %s\nType `help' at any time for usage information.\n",
    92                 progname, PACKAGE_STRING);
    93 
    9491        while (!cli_quit) {
    9592                get_input(&usr);
  • uspace/app/getterm/Makefile

    r9a1d8ab r0b749a3  
    3434SOURCES = \
    3535        getterm.c \
    36         version.c
     36        version.c \
     37        welcome.c
    3738
    3839include $(USPACE_PREFIX)/Makefile.common
  • uspace/app/getterm/getterm.c

    r9a1d8ab r0b749a3  
    4141#include <task.h>
    4242#include <str_error.h>
     43#include <errno.h>
    4344#include "version.h"
     45#include "welcome.h"
    4446
    4547#define APP_NAME  "getterm"
     
    4749static void usage(void)
    4850{
    49         printf("Usage: %s <terminal> <path>\n", APP_NAME);
     51        printf("Usage: %s <terminal> [-w] <command> [<arguments...>]\n", APP_NAME);
    5052}
    5153
     
    7274}
    7375
    74 static task_id_t spawn(const char *fname)
    75 {
    76         const char *args[2];
    77        
    78         args[0] = fname;
    79         args[1] = NULL;
    80        
    81         int err;
    82         task_id_t id = task_spawn(fname, args, &err);
    83        
    84         if (id == 0)
    85                 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,
    86                     str_error(err));
    87        
    88         return id;
    89 }
    90 
    9176int main(int argc, char *argv[])
    9277{
    93         if (argc < 3) {
     78        int rc;
     79        task_exit_t texit;
     80        int retval;
     81        task_id_t id;
     82        char *fname, *term;
     83        char **cmd_args;
     84        bool print_wmsg;
     85
     86        argv++;
     87        argc--;
     88        if (argc < 1) {
    9489                usage();
    9590                return -1;
    9691        }
     92
     93        if (str_cmp(*argv, "-w") == 0) {
     94                print_wmsg = true;
     95                argv++;
     96                argc--;
     97        } else {
     98                print_wmsg = false;
     99        }
     100
     101        if (argc < 2) {
     102                usage();
     103                return -1;
     104        }
     105
     106        term = *argv++;
     107        fname = *argv;
     108        cmd_args = argv;
    97109       
    98         reopen(&stdin, 0, argv[1], O_RDONLY, "r");
    99         reopen(&stdout, 1, argv[1], O_WRONLY, "w");
    100         reopen(&stderr, 2, argv[1], O_WRONLY, "w");
     110        reopen(&stdin, 0, term, O_RDONLY, "r");
     111        reopen(&stdout, 1, term, O_WRONLY, "w");
     112        reopen(&stderr, 2, term, O_WRONLY, "w");
    101113       
    102114        /*
     
    115127                return -4;
    116128       
    117         version_print(argv[1]);
    118         task_id_t id = spawn(argv[2]);
    119        
    120         if (id != 0) {
    121                 task_exit_t texit;
    122                 int retval;
    123                 task_wait(id, &texit, &retval);
    124                
    125                 return 0;
     129        version_print(term);
     130        if (print_wmsg)
     131                welcome_msg_print();
     132
     133        rc = task_spawnv(&id, fname, (const char * const *) cmd_args);
     134        if (rc != EOK) {
     135                printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,
     136                    str_error(rc));
     137                return -5;
    126138        }
    127        
    128         return -5;
     139
     140        rc = task_wait(id, &texit, &retval);
     141        if (rc != EOK) {
     142                printf("%s: Error waiting for %s (%s)\n", APP_NAME, fname,
     143                    str_error(rc));
     144                return -6;
     145        }
     146
     147        return 0;
    129148}
    130149
  • uspace/app/getterm/version.c

    r9a1d8ab r0b749a3  
    6161        printf("HelenOS release %s (%s)%s%s\n", release, name, revision, timestamp);
    6262        printf("Running on %s (%s)\n", arch, term);
    63         printf("Copyright (c) 2001-2009 HelenOS project\n\n");
     63        printf("Copyright (c) 2001-2010 HelenOS project\n\n");
    6464}
    6565
  • uspace/app/getterm/welcome.h

    r9a1d8ab r0b749a3  
    11/*
    2  * Copyright (c) 2009 Lukas Mejdrech
     2 * Copyright (c) 2010 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
    29 /** @addtogroup net
     29/** @addtogroup getterm
    3030 * @{
    3131 */
     32/**
     33 * @file
     34 */
    3235
    33 #ifndef __SELF_TEST_H__
    34 #define __SELF_TEST_H__
     36#ifndef WELCOME_H__
     37#define WELCOME_H__
    3538
    36 extern int self_test(void);
     39extern void welcome_msg_print(void);
    3740
    3841#endif
  • uspace/app/init/init.c

    r9a1d8ab r0b749a3  
    124124static void spawn(const char *fname)
    125125{
    126         const char *argv[2];
     126        int rc;
    127127        struct stat s;
    128128       
     
    131131       
    132132        printf("%s: Spawning %s\n", NAME, fname);
    133        
    134         argv[0] = fname;
    135         argv[1] = NULL;
    136        
    137         int err;
    138         if (!task_spawn(fname, argv, &err))
     133        rc = task_spawnl(NULL, fname, fname, NULL);
     134        if (rc != EOK) {
    139135                printf("%s: Error spawning %s (%s)\n", NAME, fname,
    140                     str_error(err));
     136                    str_error(rc));
     137        }
    141138}
    142139
    143140static void srv_start(const char *fname)
    144141{
    145         const char *argv[2];
    146142        task_id_t id;
    147143        task_exit_t texit;
     
    153149       
    154150        printf("%s: Starting %s\n", NAME, fname);
    155        
    156         argv[0] = fname;
    157         argv[1] = NULL;
    158        
    159         id = task_spawn(fname, argv, &retval);
     151        rc = task_spawnl(&id, fname, fname, NULL);
    160152        if (!id) {
    161153                printf("%s: Error spawning %s (%s)\n", NAME, fname,
    162                     str_error(retval));
     154                    str_error(rc));
    163155                return;
    164156        }
     
    167159        if (rc != EOK) {
    168160                printf("%s: Error waiting for %s (%s(\n", NAME, fname,
    169                     str_error(retval));
    170                 return;
    171         }
    172        
    173         if ((texit != TASK_EXIT_NORMAL) || (retval != 0)) {
    174                 printf("%s: Server %s failed to start (%s)\n", NAME,
    175                         fname, str_error(retval));
     161                    str_error(rc));
     162                return;
     163        }
     164       
     165        if (texit != TASK_EXIT_NORMAL) {
     166                printf("%s: Server %s failed to start (unexpectedly "
     167                    "terminated)\n", NAME, fname);
     168                return;
     169        }
     170
     171        if (retval != 0) {
     172                printf("%s: Server %s failed to start (exit code %d)\n", NAME,
     173                        fname, retval);
    176174        }
    177175}
     
    179177static void console(const char *dev)
    180178{
    181         const char *argv[3];
    182179        char hid_in[DEVMAP_NAME_MAXLEN];
    183180        int rc;
     
    188185       
    189186        /* Wait for the input device to be ready */
    190         dev_handle_t handle;
     187        devmap_handle_t handle;
    191188        rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING);
    192        
    193         if (rc == EOK) {
    194                 argv[0] = SRV_CONSOLE;
    195                 argv[1] = hid_in;
    196                 argv[2] = NULL;
    197                
    198                 if (!task_spawn(SRV_CONSOLE, argv, &rc))
    199                         printf("%s: Error spawning %s %s (%s)\n", NAME, SRV_CONSOLE,
    200                             hid_in, str_error(rc));
    201         } else
     189        if (rc != EOK) {
    202190                printf("%s: Error waiting on %s (%s)\n", NAME, hid_in,
    203191                    str_error(rc));
    204 }
    205 
    206 static void getterm(const char *dev, const char *app)
    207 {
    208         const char *argv[4];
     192                return;
     193        }
     194       
     195        rc = task_spawnl(NULL, SRV_CONSOLE, SRV_CONSOLE, hid_in, NULL);
     196        if (rc != EOK) {
     197                printf("%s: Error spawning %s %s (%s)\n", NAME, SRV_CONSOLE,
     198                    hid_in, str_error(rc));
     199        }
     200}
     201
     202static void getterm(const char *dev, const char *app, bool wmsg)
     203{
    209204        char term[DEVMAP_NAME_MAXLEN];
    210205        int rc;
     
    215210       
    216211        /* Wait for the terminal device to be ready */
    217         dev_handle_t handle;
     212        devmap_handle_t handle;
    218213        rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING);
    219        
    220         if (rc == EOK) {
    221                 argv[0] = APP_GETTERM;
    222                 argv[1] = term;
    223                 argv[2] = app;
    224                 argv[3] = NULL;
    225                
    226                 if (!task_spawn(APP_GETTERM, argv, &rc))
    227                         printf("%s: Error spawning %s %s %s (%s)\n", NAME, APP_GETTERM,
    228                             term, app, str_error(rc));
    229         } else
     214        if (rc != EOK) {
    230215                printf("%s: Error waiting on %s (%s)\n", NAME, term,
    231216                    str_error(rc));
     217                return;
     218        }
     219       
     220        if (wmsg) {
     221                rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, "-w", term,
     222                    app, NULL);
     223                if (rc != EOK) {
     224                        printf("%s: Error spawning %s -w %s %s (%s)\n", NAME,
     225                            APP_GETTERM, term, app, str_error(rc));
     226                }
     227        } else {
     228                rc = task_spawnl(NULL, APP_GETTERM, APP_GETTERM, term, app,
     229                    NULL);
     230                if (rc != EOK) {
     231                        printf("%s: Error spawning %s %s %s (%s)\n", NAME,
     232                            APP_GETTERM, term, app, str_error(rc));
     233                }
     234        }
    232235}
    233236
     
    277280        srv_start("/srv/adb_ms");
    278281        srv_start("/srv/char_ms");
     282        srv_start("/srv/s3c24ts");
    279283       
    280284        spawn("/srv/fb");
     
    301305#endif
    302306       
    303         getterm("term/vc0", "/app/bdsh");
    304         getterm("term/vc1", "/app/bdsh");
    305         getterm("term/vc2", "/app/bdsh");
    306         getterm("term/vc3", "/app/bdsh");
    307         getterm("term/vc4", "/app/bdsh");
    308         getterm("term/vc5", "/app/bdsh");
    309         getterm("term/vc6", "/app/klog");
     307        getterm("term/vc0", "/app/bdsh", true);
     308        getterm("term/vc1", "/app/bdsh", false);
     309        getterm("term/vc2", "/app/bdsh", false);
     310        getterm("term/vc3", "/app/bdsh", false);
     311        getterm("term/vc4", "/app/bdsh", false);
     312        getterm("term/vc5", "/app/bdsh", false);
     313        getterm("term/vc6", "/app/klog", false);
     314        getterm("term/vc7", "/srv/devman", false);
    310315       
    311316        return 0;
  • uspace/app/mkfat/mkfat.c

    r9a1d8ab r0b749a3  
    8585    struct fat_params *par);
    8686static int fat_blocks_write(struct fat_params const *par,
    87     dev_handle_t handle);
     87    devmap_handle_t handle);
    8888static void fat_bootsec_create(struct fat_params const *par, struct fat_bs *bs);
    8989
     
    9595        int rc;
    9696        char *dev_path;
    97         dev_handle_t handle;
     97        devmap_handle_t handle;
    9898        size_t block_size;
    9999        char *endptr;
     
    234234
    235235/** Create file system with the given parameters. */
    236 static int fat_blocks_write(struct fat_params const *par, dev_handle_t handle)
     236static int fat_blocks_write(struct fat_params const *par, devmap_handle_t handle)
    237237{
    238238        aoff64_t addr;
  • uspace/app/netecho/Makefile

    r9a1d8ab r0b749a3  
    2929
    3030USPACE_PREFIX = ../..
    31 LIBS = $(LIBSOCKET_PREFIX)/libsocket.a
    32 EXTRA_CFLAGS = -I$(LIBSOCKET_PREFIX)/include
     31LIBS =
     32EXTRA_CFLAGS =
    3333BINARY = netecho
    3434
  • uspace/app/netecho/netecho.c

    r9a1d8ab r0b749a3  
    2828
    2929/** @addtogroup netecho
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Network echo application.
    35  *  Answers received packets.
     34 * Network echo application.
     35 * Answers received packets.
    3636 */
    3737
     
    4242#include <arg_parse.h>
    4343
    44 #include <in.h>
    45 #include <in6.h>
    46 #include <inet.h>
    47 #include <socket.h>
    48 #include <net_err.h>
    49 #include <socket_parse.h>
     44#include <net/in.h>
     45#include <net/in6.h>
     46#include <net/inet.h>
     47#include <net/socket.h>
     48#include <net/socket_parse.h>
    5049
    5150#include "print_error.h"
    5251
    53 /** Network echo module name.
    54  */
     52/** Network echo module name. */
    5553#define NAME    "Network Echo"
    5654
    57 /** Prints the application help.
    58  */
    59 void echo_print_help(void);
    60 
    61 /** Module entry point.
    62  *  Reads command line parameters and starts listenning.
    63  *  @param[in] argc The number of command line parameters.
    64  *  @param[in] argv The command line parameters.
    65  *  @returns EOK on success.
    66  */
    67 int main(int argc, char * argv[]);
    68 
    69 void echo_print_help(void){
     55static void echo_print_help(void)
     56{
    7057        printf(
    7158                "Network Echo aplication\n" \
     
    10188}
    10289
    103 int main(int argc, char * argv[]){
    104         ERROR_DECLARE;
    105 
    106         size_t size                     = 1024;
    107         int verbose                     = 0;
    108         char * reply            = NULL;
    109         sock_type_t type        = SOCK_DGRAM;
    110         int count                       = -1;
    111         int family                      = PF_INET;
    112         uint16_t port           = 7;
    113         int backlog                     = 3;
    114 
    115         socklen_t max_length                            = sizeof(struct sockaddr_in6);
     90int main(int argc, char *argv[])
     91{
     92        size_t size = 1024;
     93        int verbose = 0;
     94        char *reply = NULL;
     95        sock_type_t type = SOCK_DGRAM;
     96        int count = -1;
     97        int family = PF_INET;
     98        uint16_t port = 7;
     99        int backlog = 3;
     100
     101        socklen_t max_length = sizeof(struct sockaddr_in6);
    116102        uint8_t address_data[max_length];
    117         struct sockaddr * address                       = (struct sockaddr *) address_data;
    118         struct sockaddr_in * address_in         = (struct sockaddr_in *) address;
    119         struct sockaddr_in6 * address_in6       = (struct sockaddr_in6 *) address;
     103        struct sockaddr *address = (struct sockaddr *) address_data;
     104        struct sockaddr_in *address_in = (struct sockaddr_in *) address;
     105        struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address;
    120106        socklen_t addrlen;
    121107        char address_string[INET6_ADDRSTRLEN];
    122         uint8_t * address_start;
     108        uint8_t *address_start;
    123109        int socket_id;
    124110        int listening_id;
    125         char * data;
     111        char *data;
    126112        size_t length;
    127113        int index;
    128114        size_t reply_length;
    129115        int value;
     116        int rc;
    130117
    131118        // parse the command line arguments
    132         for(index = 1; index < argc; ++ index){
    133                 if(argv[index][0] == '-'){
    134                         switch(argv[index][1]){
    135                                 case 'b':
    136                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 0));
    137                                         break;
    138                                 case 'c':
    139                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 0));
    140                                         break;
    141                                 case 'f':
    142                                         ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family));
    143                                         break;
    144                                 case 'h':
     119        for (index = 1; index < argc; ++ index) {
     120                if (argv[index][0] == '-') {
     121                        switch (argv[index][1]) {
     122                        case 'b':
     123                                rc = arg_parse_int(argc, argv, &index, &backlog, 0);
     124                                if (rc != EOK)
     125                                        return rc;
     126                                break;
     127                        case 'c':
     128                                rc = arg_parse_int(argc, argv, &index, &count, 0);
     129                                if (rc != EOK)
     130                                        return rc;
     131                                break;
     132                        case 'f':
     133                                rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family);
     134                                if (rc != EOK)
     135                                        return rc;
     136                                break;
     137                        case 'h':
     138                                echo_print_help();
     139                                return EOK;
     140                                break;
     141                        case 'p':
     142                                rc = arg_parse_int(argc, argv, &index, &value, 0);
     143                                if (rc != EOK)
     144                                        return rc;
     145                                port = (uint16_t) value;
     146                                break;
     147                        case 'r':
     148                                rc = arg_parse_string(argc, argv, &index, &reply, 0);
     149                                if (rc != EOK)
     150                                        return rc;
     151                                break;
     152                        case 's':
     153                                rc = arg_parse_int(argc, argv, &index, &value, 0);
     154                                if (rc != EOK)
     155                                        return rc;
     156                                size = (value >= 0) ? (size_t) value : 0;
     157                                break;
     158                        case 't':
     159                                rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type);
     160                                if (rc != EOK)
     161                                        return rc;
     162                                type = (sock_type_t) value;
     163                                break;
     164                        case 'v':
     165                                verbose = 1;
     166                                break;
     167                        // long options with the double minus sign ('-')
     168                        case '-':
     169                                if (str_lcmp(argv[index] + 2, "backlog=", 6) == 0) {
     170                                        rc = arg_parse_int(argc, argv, &index, &backlog, 8);
     171                                        if (rc != EOK)
     172                                                return rc;
     173                                } else if (str_lcmp(argv[index] + 2, "count=", 6) == 0) {
     174                                        rc = arg_parse_int(argc, argv, &index, &count, 8);
     175                                        if (rc != EOK)
     176                                                return rc;
     177                                } else if (str_lcmp(argv[index] + 2, "family=", 7) == 0) {
     178                                        rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family);
     179                                        if (rc != EOK)
     180                                                return rc;
     181                                } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) {
    145182                                        echo_print_help();
    146183                                        return EOK;
    147                                         break;
    148                                 case 'p':
    149                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0));
     184                                } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) {
     185                                        rc = arg_parse_int(argc, argv, &index, &value, 7);
     186                                        if (rc != EOK)
     187                                                return rc;
    150188                                        port = (uint16_t) value;
    151                                         break;
    152                                 case 'r':
    153                                         ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 0));
    154                                         break;
    155                                 case 's':
    156                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0));
     189                                } else if (str_lcmp(argv[index] + 2, "reply=", 6) == 0) {
     190                                        rc = arg_parse_string(argc, argv, &index, &reply, 8);
     191                                        if (rc != EOK)
     192                                                return rc;
     193                                } else if (str_lcmp(argv[index] + 2, "size=", 5) == 0) {
     194                                        rc = arg_parse_int(argc, argv, &index, &value, 7);
     195                                        if (rc != EOK)
     196                                                return rc;
    157197                                        size = (value >= 0) ? (size_t) value : 0;
    158                                         break;
    159                                 case 't':
    160                                         ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type));
     198                                } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) {
     199                                        rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type);
     200                                        if (rc != EOK)
     201                                                return rc;
    161202                                        type = (sock_type_t) value;
    162                                         break;
    163                                 case 'v':
     203                                } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) {
    164204                                        verbose = 1;
    165                                         break;
    166                                 // long options with the double minus sign ('-')
    167                                 case '-':
    168                                         if(str_lcmp(argv[index] + 2, "backlog=", 6) == 0){
    169                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &backlog, 8));
    170                                         }else if(str_lcmp(argv[index] + 2, "count=", 6) == 0){
    171                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &count, 8));
    172                                         }else if(str_lcmp(argv[index] + 2, "family=", 7) == 0){
    173                                                 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family));
    174                                         }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){
    175                                                 echo_print_help();
    176                                                 return EOK;
    177                                         }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){
    178                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7));
    179                                                 port = (uint16_t) value;
    180                                         }else if(str_lcmp(argv[index] + 2, "reply=", 6) == 0){
    181                                                 ERROR_PROPAGATE(arg_parse_string(argc, argv, &index, &reply, 8));
    182                                         }else if(str_lcmp(argv[index] + 2, "size=", 5) == 0){
    183                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7));
    184                                                 size = (value >= 0) ? (size_t) value : 0;
    185                                         }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){
    186                                                 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type));
    187                                                 type = (sock_type_t) value;
    188                                         }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){
    189                                                 verbose = 1;
    190                                         }else{
    191                                                 echo_print_help();
    192                                                 return EINVAL;
    193                                         }
    194                                         break;
    195                                 default:
     205                                } else {
    196206                                        echo_print_help();
    197207                                        return EINVAL;
     208                                }
     209                                break;
     210                        default:
     211                                echo_print_help();
     212                                return EINVAL;
    198213                        }
    199                 }else{
     214                } else {
    200215                        echo_print_help();
    201216                        return EINVAL;
     
    204219
    205220        // check the buffer size
    206         if(size <= 0){
     221        if (size <= 0) {
    207222                fprintf(stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size);
    208223                size = 1024;
     
    210225        // size plus the terminating null (\0)
    211226        data = (char *) malloc(size + 1);
    212         if(! data){
     227        if (!data) {
    213228                fprintf(stderr, "Failed to allocate receive buffer.\n");
    214229                return ENOMEM;
     
    220235        // prepare the address buffer
    221236        bzero(address_data, max_length);
    222         switch(family){
    223                 case PF_INET:
    224                         address_in->sin_family = AF_INET;
    225                         address_in->sin_port = htons(port);
    226                         addrlen = sizeof(struct sockaddr_in);
    227                         break;
    228                 case PF_INET6:
    229                         address_in6->sin6_family = AF_INET6;
    230                         address_in6->sin6_port = htons(port);
    231                         addrlen = sizeof(struct sockaddr_in6);
    232                         break;
    233                 default:
    234                         fprintf(stderr, "Protocol family is not supported\n");
    235                         return EAFNOSUPPORT;
     237        switch (family) {
     238        case PF_INET:
     239                address_in->sin_family = AF_INET;
     240                address_in->sin_port = htons(port);
     241                addrlen = sizeof(struct sockaddr_in);
     242                break;
     243        case PF_INET6:
     244                address_in6->sin6_family = AF_INET6;
     245                address_in6->sin6_port = htons(port);
     246                addrlen = sizeof(struct sockaddr_in6);
     247                break;
     248        default:
     249                fprintf(stderr, "Protocol family is not supported\n");
     250                return EAFNOSUPPORT;
    236251        }
    237252
    238253        // get a listening socket
    239254        listening_id = socket(family, type, 0);
    240         if(listening_id < 0){
     255        if (listening_id < 0) {
    241256                socket_print_error(stderr, listening_id, "Socket create: ", "\n");
    242257                return listening_id;
     
    244259
    245260        // if the stream socket is used
    246         if(type == SOCK_STREAM){
     261        if (type == SOCK_STREAM) {
    247262                // check the backlog
    248                 if(backlog <= 0){
     263                if (backlog <= 0) {
    249264                        fprintf(stderr, "Accepted sockets queue size too small (%d). Using 3 instead.\n", size);
    250265                        backlog = 3;
    251266                }
    252267                // set the backlog
    253                 if(ERROR_OCCURRED(listen(listening_id, backlog))){
    254                         socket_print_error(stderr, ERROR_CODE, "Socket listen: ", "\n");
    255                         return ERROR_CODE;
     268                rc = listen(listening_id, backlog);
     269                if (rc != EOK) {
     270                        socket_print_error(stderr, rc, "Socket listen: ", "\n");
     271                        return rc;
    256272                }
    257273        }
    258274
    259275        // bind the listenning socket
    260         if(ERROR_OCCURRED(bind(listening_id, address, addrlen))){
    261                 socket_print_error(stderr, ERROR_CODE, "Socket bind: ", "\n");
    262                 return ERROR_CODE;
    263         }
    264 
    265         if(verbose){
     276        rc = bind(listening_id, address, addrlen);
     277        if (rc != EOK) {
     278                socket_print_error(stderr, rc, "Socket bind: ", "\n");
     279                return rc;
     280        }
     281
     282        if (verbose)
    266283                printf("Socket %d listenning at %d\n", listening_id, port);
    267         }
    268284
    269285        socket_id = listening_id;
     
    271287        // do count times
    272288        // or indefinitely if set to a negative value
    273         while(count){
     289        while (count) {
    274290
    275291                addrlen = max_length;
    276                 if(type == SOCK_STREAM){
     292                if (type == SOCK_STREAM) {
    277293                        // acceept a socket if the stream socket is used
    278294                        socket_id = accept(listening_id, address, &addrlen);
    279                         if(socket_id <= 0){
     295                        if (socket_id <= 0) {
    280296                                socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
    281                         }else{
    282                                 if(verbose){
     297                        } else {
     298                                if (verbose)
    283299                                        printf("Socket %d accepted\n", socket_id);
    284                                 }
    285300                        }
    286301                }
    287302
    288303                // if the datagram socket is used or the stream socked was accepted
    289                 if(socket_id > 0){
     304                if (socket_id > 0) {
    290305
    291306                        // receive an echo request
    292307                        value = recvfrom(socket_id, data, size, 0, address, &addrlen);
    293                         if(value < 0){
     308                        if (value < 0) {
    294309                                socket_print_error(stderr, value, "Socket receive: ", "\n");
    295                         }else{
     310                        } else {
    296311                                length = (size_t) value;
    297                                 if(verbose){
     312                                if (verbose) {
    298313                                        // print the header
    299314
    300315                                        // get the source port and prepare the address buffer
    301316                                        address_start = NULL;
    302                                         switch(address->sa_family){
    303                                                 case AF_INET:
    304                                                         port = ntohs(address_in->sin_port);
    305                                                         address_start = (uint8_t *) &address_in->sin_addr.s_addr;
    306                                                         break;
    307                                                 case AF_INET6:
    308                                                         port = ntohs(address_in6->sin6_port);
    309                                                         address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
    310                                                         break;
    311                                                 default:
    312                                                         fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);
     317                                        switch (address->sa_family) {
     318                                        case AF_INET:
     319                                                port = ntohs(address_in->sin_port);
     320                                                address_start = (uint8_t *) &address_in->sin_addr.s_addr;
     321                                                break;
     322                                        case AF_INET6:
     323                                                port = ntohs(address_in6->sin6_port);
     324                                                address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
     325                                                break;
     326                                        default:
     327                                                fprintf(stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family);
    313328                                        }
    314329                                        // parse the source address
    315                                         if(address_start){
    316                                                 if(ERROR_OCCURRED(inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string)))){
    317                                                         fprintf(stderr, "Received address error %d\n", ERROR_CODE);
    318                                                 }else{
     330                                        if (address_start) {
     331                                                rc = inet_ntop(address->sa_family, address_start, address_string, sizeof(address_string));
     332                                                if (rc != EOK) {
     333                                                        fprintf(stderr, "Received address error %d\n", rc);
     334                                                } else {
    319335                                                        data[length] = '\0';
    320336                                                        printf("Socket %d received %d bytes from %s:%d\n%s\n", socket_id, length, address_string, port, data);
     
    324340
    325341                                // answer the request either with the static reply or the original data
    326                                 if(ERROR_OCCURRED(sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen))){
    327                                         socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
    328                                 }
    329 
     342                                rc = sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen);
     343                                if (rc != EOK)
     344                                        socket_print_error(stderr, rc, "Socket send: ", "\n");
    330345                        }
    331346
    332347                        // close the accepted stream socket
    333                         if(type == SOCK_STREAM){
    334                                 if(ERROR_OCCURRED(closesocket(socket_id))){
    335                                         socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
    336                                 }
     348                        if (type == SOCK_STREAM) {
     349                                rc = closesocket(socket_id);
     350                                if (rc != EOK)
     351                                        socket_print_error(stderr, rc, "Close socket: ", "\n");
    337352                        }
    338353
     
    340355
    341356                // decrease the count if positive
    342                 if(count > 0){
    343                         -- count;
    344                         if(verbose){
     357                if (count > 0) {
     358                        count--;
     359                        if (verbose)
    345360                                printf("Waiting for next %d packet(s)\n", count);
    346                         }
    347                 }
    348         }
    349 
    350         if(verbose){
     361                }
     362        }
     363
     364        if (verbose)
    351365                printf("Closing the socket\n");
    352         }
    353366
    354367        // close the listenning socket
    355         if(ERROR_OCCURRED(closesocket(listening_id))){
    356                 socket_print_error(stderr, ERROR_CODE, "Close socket: ", "\n");
    357                 return ERROR_CODE;
    358         }
    359 
    360         if(verbose){
     368        rc = closesocket(listening_id);
     369        if (rc != EOK) {
     370                socket_print_error(stderr, rc, "Close socket: ", "\n");
     371                return rc;
     372        }
     373
     374        if (verbose)
    361375                printf("Exiting\n");
    362         }
    363376
    364377        return EOK;
  • uspace/app/netecho/print_error.c

    r9a1d8ab r0b749a3  
    2828
    2929/** @addtogroup net_app
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Generic application error printing functions implementation.
     34 * Generic application error printing functions implementation.
    3535 */
    36 
    37 #include <stdio.h>
    38 
    39 #include <icmp_codes.h>
    40 #include <socket_errno.h>
    4136
    4237#include "print_error.h"
    4338
    44 void icmp_print_error(FILE * output, int error_code, const char * prefix, const char * suffix){
    45         if(output){
    46                 if(prefix){
    47                         fprintf(output, "%s", prefix);
    48                 }
    49                 switch(error_code){
    50                         case ICMP_DEST_UNREACH:
    51                                 fprintf(output, "ICMP Destination Unreachable (%d) error", error_code);
    52                                 break;
    53                         case ICMP_SOURCE_QUENCH:
    54                                 fprintf(output, "ICMP Source Quench (%d) error", error_code);
    55                                 break;
    56                         case ICMP_REDIRECT:
    57                                 fprintf(output, "ICMP Redirect (%d) error", error_code);
    58                                 break;
    59                         case ICMP_ALTERNATE_ADDR:
    60                                 fprintf(output, "ICMP Alternate Host Address (%d) error", error_code);
    61                                 break;
    62                         case ICMP_ROUTER_ADV:
    63                                 fprintf(output, "ICMP Router Advertisement (%d) error", error_code);
    64                                 break;
    65                         case ICMP_ROUTER_SOL:
    66                                 fprintf(output, "ICMP Router Solicitation (%d) error", error_code);
    67                                 break;
    68                         case ICMP_TIME_EXCEEDED:
    69                                 fprintf(output, "ICMP Time Exceeded (%d) error", error_code);
    70                                 break;
    71                         case ICMP_PARAMETERPROB:
    72                                 fprintf(output, "ICMP Paramenter Problem (%d) error", error_code);
    73                                 break;
    74                         case ICMP_CONVERSION_ERROR:
    75                                 fprintf(output, "ICMP Datagram Conversion Error (%d) error", error_code);
    76                                 break;
    77                         case ICMP_REDIRECT_MOBILE:
    78                                 fprintf(output, "ICMP Mobile Host Redirect (%d) error", error_code);
    79                                 break;
    80                         case ICMP_SKIP:
    81                                 fprintf(output, "ICMP SKIP (%d) error", error_code);
    82                                 break;
    83                         case ICMP_PHOTURIS:
    84                                 fprintf(output, "ICMP Photuris (%d) error", error_code);
    85                                 break;
    86                         default:
    87                                 fprintf(output, "Other (%d) error", error_code);
    88                 }
    89                 if(suffix){
    90                         fprintf(output, "%s", suffix);
    91                 }
     39#include <stdio.h>
     40#include <errno.h>
     41
     42#include <net/icmp_codes.h>
     43
     44/** Prints the specific ICMP error description.
     45 *
     46 * @param[in] output The description output stream. May be NULL.
     47 * @param[in] error_code The ICMP error code.
     48 * @param[in] prefix The error description prefix. May be NULL.
     49 * @param[in] suffix The error description suffix. May be NULL.
     50 */
     51void icmp_print_error(FILE *output, int error_code, const char *prefix, const char *suffix)
     52{
     53        if (!output)
     54                return;
     55       
     56        if (prefix)
     57                fprintf(output, "%s", prefix);
     58               
     59        switch (error_code) {
     60        case ICMP_DEST_UNREACH:
     61                fprintf(output, "ICMP Destination Unreachable (%d) error", error_code);
     62                break;
     63        case ICMP_SOURCE_QUENCH:
     64                fprintf(output, "ICMP Source Quench (%d) error", error_code);
     65                break;
     66        case ICMP_REDIRECT:
     67                fprintf(output, "ICMP Redirect (%d) error", error_code);
     68                break;
     69        case ICMP_ALTERNATE_ADDR:
     70                fprintf(output, "ICMP Alternate Host Address (%d) error", error_code);
     71                break;
     72        case ICMP_ROUTER_ADV:
     73                fprintf(output, "ICMP Router Advertisement (%d) error", error_code);
     74                break;
     75        case ICMP_ROUTER_SOL:
     76                fprintf(output, "ICMP Router Solicitation (%d) error", error_code);
     77                break;
     78        case ICMP_TIME_EXCEEDED:
     79                fprintf(output, "ICMP Time Exceeded (%d) error", error_code);
     80                break;
     81        case ICMP_PARAMETERPROB:
     82                fprintf(output, "ICMP Paramenter Problem (%d) error", error_code);
     83                break;
     84        case ICMP_CONVERSION_ERROR:
     85                fprintf(output, "ICMP Datagram Conversion Error (%d) error", error_code);
     86                break;
     87        case ICMP_REDIRECT_MOBILE:
     88                fprintf(output, "ICMP Mobile Host Redirect (%d) error", error_code);
     89                break;
     90        case ICMP_SKIP:
     91                fprintf(output, "ICMP SKIP (%d) error", error_code);
     92                break;
     93        case ICMP_PHOTURIS:
     94                fprintf(output, "ICMP Photuris (%d) error", error_code);
     95                break;
     96        default:
     97                fprintf(output, "Other (%d) error", error_code);
    9298        }
     99
     100        if (suffix)
     101                fprintf(output, "%s", suffix);
    93102}
    94103
    95 void print_error(FILE * output, int error_code, const char * prefix, const char * suffix){
    96         if(IS_ICMP_ERROR(error_code)){
     104/** Prints the error description.
     105 *
     106 * Supports ICMP and socket error codes.
     107 *
     108 * @param[in] output The description output stream. May be NULL.
     109 * @param[in] error_code The error code.
     110 * @param[in] prefix The error description prefix. May be NULL.
     111 * @param[in] suffix The error description suffix. May be NULL.
     112 */
     113void print_error(FILE *output, int error_code, const char *prefix, const char *suffix)
     114{
     115        if (IS_ICMP_ERROR(error_code))
    97116                icmp_print_error(output, error_code, prefix, suffix);
    98         }else if(IS_SOCKET_ERROR(error_code)){
     117        else if(IS_SOCKET_ERROR(error_code))
    99118                socket_print_error(output, error_code, prefix, suffix);
    100         }
    101119}
    102120
    103 void socket_print_error(FILE * output, int error_code, const char * prefix, const char * suffix){
    104         if(output){
    105                 if(prefix){
    106                         fprintf(output, "%s", prefix);
    107                 }
    108                 switch(error_code){
    109                         case ENOTSOCK:
    110                                 fprintf(output, "Not a socket (%d) error", error_code);
    111                                 break;
    112                         case EPROTONOSUPPORT:
    113                                 fprintf(output, "Protocol not supported (%d) error", error_code);
    114                                 break;
    115                         case ESOCKTNOSUPPORT:
    116                                 fprintf(output, "Socket type not supported (%d) error", error_code);
    117                                 break;
    118                         case EPFNOSUPPORT:
    119                                 fprintf(output, "Protocol family not supported (%d) error", error_code);
    120                                 break;
    121                         case EAFNOSUPPORT:
    122                                 fprintf(output, "Address family not supported (%d) error", error_code);
    123                                 break;
    124                         case EADDRINUSE:
    125                                 fprintf(output, "Address already in use (%d) error", error_code);
    126                                 break;
    127                         case ENOTCONN:
    128                                 fprintf(output, "Socket not connected (%d) error", error_code);
    129                                 break;
    130                         case NO_DATA:
    131                                 fprintf(output, "No data (%d) error", error_code);
    132                                 break;
    133                         case EINPROGRESS:
    134                                 fprintf(output, "Another operation in progress (%d) error", error_code);
    135                                 break;
    136                         case EDESTADDRREQ:
    137                                 fprintf(output, "Destination address required (%d) error", error_code);
    138                         case TRY_AGAIN:
    139                                 fprintf(output, "Try again (%d) error", error_code);
    140                         default:
    141                                 fprintf(output, "Other (%d) error", error_code);
    142                 }
    143                 if(suffix){
    144                         fprintf(output, "%s", suffix);
    145                 }
     121/** Prints the specific socket error description.
     122 *
     123 * @param[in] output The description output stream. May be NULL.
     124 * @param[in] error_code The socket error code.
     125 * @param[in] prefix The error description prefix. May be NULL.
     126 * @param[in] suffix The error description suffix. May be NULL.
     127 */
     128void socket_print_error(FILE *output, int error_code, const char *prefix, const char *suffix)
     129{
     130        if (!output)
     131                return;
     132
     133        if (prefix)
     134                fprintf(output, "%s", prefix);
     135
     136        switch (error_code) {
     137        case ENOTSOCK:
     138                fprintf(output, "Not a socket (%d) error", error_code);
     139                break;
     140        case EPROTONOSUPPORT:
     141                fprintf(output, "Protocol not supported (%d) error", error_code);
     142                break;
     143        case ESOCKTNOSUPPORT:
     144                fprintf(output, "Socket type not supported (%d) error", error_code);
     145                break;
     146        case EPFNOSUPPORT:
     147                fprintf(output, "Protocol family not supported (%d) error", error_code);
     148                break;
     149        case EAFNOSUPPORT:
     150                fprintf(output, "Address family not supported (%d) error", error_code);
     151                break;
     152        case EADDRINUSE:
     153                fprintf(output, "Address already in use (%d) error", error_code);
     154                break;
     155        case ENOTCONN:
     156                fprintf(output, "Socket not connected (%d) error", error_code);
     157                break;
     158        case NO_DATA:
     159                fprintf(output, "No data (%d) error", error_code);
     160                break;
     161        case EINPROGRESS:
     162                fprintf(output, "Another operation in progress (%d) error", error_code);
     163                break;
     164        case EDESTADDRREQ:
     165                fprintf(output, "Destination address required (%d) error", error_code);
     166        case TRY_AGAIN:
     167                fprintf(output, "Try again (%d) error", error_code);
     168        default:
     169                fprintf(output, "Other (%d) error", error_code);
    146170        }
     171
     172        if (suffix)
     173                fprintf(output, "%s", suffix);
    147174}
    148175
    149176/** @}
    150177 */
     178
  • uspace/app/netecho/print_error.h

    r9a1d8ab r0b749a3  
    2828
    2929/** @addtogroup net_app
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Generic application error printing functions.
     34 * Generic application error printing functions.
    3535 */
    3636
    37 #ifndef __NET_APP_PRINT__
    38 #define __NET_APP_PRINT__
     37#ifndef NET_APP_PRINT_
     38#define NET_APP_PRINT_
     39
     40#include <stdio.h>
    3941
    4042/** Returns whether the error code may be an ICMP error code.
    41  *  @param[in] error_code The error code.
    42  *  @returns A value indicating whether the error code may be an ICMP error code.
     43 *
     44 * @param[in] error_code The error code.
     45 * @return A value indicating whether the error code may be an ICMP error code.
    4346 */
    44 #define IS_ICMP_ERROR(error_code)               ((error_code) > 0)
     47#define IS_ICMP_ERROR(error_code)       ((error_code) > 0)
    4548
    4649/** Returns whether the error code may be socket error code.
    47  *  @param[in] error_code The error code.
    48  *  @returns A value indicating whether the error code may be a socket error code.
     50 *
     51 * @param[in] error_code The error code.
     52 * @return A value indicating whether the error code may be a socket error code.
    4953 */
    5054#define IS_SOCKET_ERROR(error_code)     ((error_code) < 0)
    5155
    52 /** Prints the specific ICMP error description.
    53  *  @param[in] output The description output stream. May be NULL.
    54  *  @param[in] error_code The ICMP error code.
    55  *  @param[in] prefix The error description prefix. May be NULL.
    56  *  @param[in] suffix The error description suffix. May be NULL.
    57  */
    58 extern void icmp_print_error(FILE * output, int error_code, const char * prefix, const char * suffix);
    59 
    60 /** Prints the error description.
    61  *  Supports ICMP and socket error codes.
    62  *  @param[in] output The description output stream. May be NULL.
    63  *  @param[in] error_code The error code.
    64  *  @param[in] prefix The error description prefix. May be NULL.
    65  *  @param[in] suffix The error description suffix. May be NULL.
    66  */
    67 extern void print_error(FILE * output, int error_code, const char * prefix, const char * suffix);
    68 
    69 /** Prints the specific socket error description.
    70  *  @param[in] output The description output stream. May be NULL.
    71  *  @param[in] error_code The socket error code.
    72  *  @param[in] prefix The error description prefix. May be NULL.
    73  *  @param[in] suffix The error description suffix. May be NULL.
    74  */
    75 extern void socket_print_error(FILE * output, int error_code, const char * prefix, const char * suffix);
     56extern void icmp_print_error(FILE *, int, const char *, const char *);
     57extern void print_error(FILE *, int, const char *, const char *);
     58extern void socket_print_error(FILE *, int, const char *, const char *);
    7659
    7760#endif
  • uspace/app/nettest1/Makefile

    r9a1d8ab r0b749a3  
    2929
    3030USPACE_PREFIX = ../..
    31 LIBS = $(LIBSOCKET_PREFIX)/libsocket.a
    32 EXTRA_CFLAGS = -I$(LIBSOCKET_PREFIX)/include
     31LIBS =
     32EXTRA_CFLAGS =
    3333BINARY = nettest1
    3434
  • uspace/app/nettest1/nettest.c

    r9a1d8ab r0b749a3  
    2828
    2929/** @addtogroup nettest
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Networking test support functions implementation.
     34 * Networking test support functions implementation.
    3535 */
    3636
    3737#include <stdio.h>
    38 
    39 #include <socket.h>
    40 #include <net_err.h>
     38#include <net/socket.h>
    4139
    4240#include "nettest.h"
    4341#include "print_error.h"
    4442
    45 int sockets_create(int verbose, int * socket_ids, int sockets, int family, sock_type_t type){
    46         int index;
    47 
    48         if(verbose){
     43
     44/** Creates new sockets.
     45 *
     46 * @param[in] verbose A value indicating whether to print out verbose information.
     47 * @param[out] socket_ids A field to store the socket identifiers.
     48 * @param[in] sockets The number of sockets to create. Should be at most the size of the field.
     49 * @param[in] family The socket address family.
     50 * @param[in] type The socket type.
     51 * @return EOK on success.
     52 * @return Other error codes as defined for the socket() function.
     53 */
     54int sockets_create(int verbose, int *socket_ids, int sockets, int family, sock_type_t type)
     55{
     56        int index;
     57
     58        if (verbose)
    4959                printf("Create\t");
    50         }
    51         fflush(stdout);
    52         for(index = 0; index < sockets; ++ index){
     60               
     61        fflush(stdout);
     62       
     63        for (index = 0; index < sockets; index++) {
    5364                socket_ids[index] = socket(family, type, 0);
    54                 if(socket_ids[index] < 0){
     65                if (socket_ids[index] < 0) {
    5566                        printf("Socket %d (%d) error:\n", index, socket_ids[index]);
    5667                        socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n");
    5768                        return socket_ids[index];
    5869                }
    59                 if(verbose){
    60                         print_mark(index);
    61                 }
    62         }
    63         return EOK;
    64 }
    65 
    66 int sockets_close(int verbose, int * socket_ids, int sockets){
    67         ERROR_DECLARE;
    68 
    69         int index;
    70 
    71         if(verbose){
     70                if (verbose)
     71                        print_mark(index);
     72        }
     73       
     74        return EOK;
     75}
     76
     77/** Closes sockets.
     78 *
     79 * @param[in] verbose A value indicating whether to print out verbose information.
     80 * @param[in] socket_ids A field of stored socket identifiers.
     81 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
     82 * @return EOK on success.
     83 * @return Other error codes as defined for the closesocket() function.
     84 */
     85int sockets_close(int verbose, int *socket_ids, int sockets)
     86{
     87        int index;
     88        int rc;
     89
     90        if (verbose)
    7291                printf("\tClose\t");
    73         }
    74         fflush(stdout);
    75         for(index = 0; index < sockets; ++ index){
    76                 if(ERROR_OCCURRED(closesocket(socket_ids[index]))){
     92
     93        fflush(stdout);
     94       
     95        for (index = 0; index < sockets; index++) {
     96                rc = closesocket(socket_ids[index]);
     97                if (rc != EOK) {
    7798                        printf("Socket %d (%d) error:\n", index, socket_ids[index]);
    78                         socket_print_error(stderr, ERROR_CODE, "Socket close: ", "\n");
    79                         return ERROR_CODE;
    80                 }
    81                 if(verbose){
    82                         print_mark(index);
    83                 }
    84         }
    85         return EOK;
    86 }
    87 
    88 int sockets_connect(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen){
    89         ERROR_DECLARE;
    90 
    91         int index;
    92 
    93         if(verbose){
     99                        socket_print_error(stderr, rc, "Socket close: ", "\n");
     100                        return rc;
     101                }
     102                if (verbose)
     103                        print_mark(index);
     104        }
     105       
     106        return EOK;
     107}
     108
     109/** Connects sockets.
     110 *
     111 * @param[in] verbose A value indicating whether to print out verbose information.
     112 * @param[in] socket_ids A field of stored socket identifiers.
     113 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
     114 * @param[in] address The destination host address to connect to.
     115 * @param[in] addrlen The length of the destination address in bytes.
     116 * @return EOK on success.
     117 * @return Other error codes as defined for the connect() function.
     118 */
     119int sockets_connect(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen)
     120{
     121        int index;
     122        int rc;
     123
     124        if (verbose)
    94125                printf("\tConnect\t");
    95         }
    96         fflush(stdout);
    97         for(index = 0; index < sockets; ++ index){
    98                 if(ERROR_OCCURRED(connect(socket_ids[index], address, addrlen))){
    99                         socket_print_error(stderr, ERROR_CODE, "Socket connect: ", "\n");
    100                         return ERROR_CODE;
    101                 }
    102                 if(verbose){
    103                         print_mark(index);
    104                 }
    105         }
    106         return EOK;
    107 }
    108 
    109 int sockets_sendto(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages){
    110         ERROR_DECLARE;
    111 
     126       
     127        fflush(stdout);
     128       
     129        for (index = 0; index < sockets; index++) {
     130                rc = connect(socket_ids[index], address, addrlen);
     131                if (rc != EOK) {
     132                        socket_print_error(stderr, rc, "Socket connect: ", "\n");
     133                        return rc;
     134                }
     135                if (verbose)
     136                        print_mark(index);
     137        }
     138       
     139        return EOK;
     140}
     141
     142/** Sends data via sockets.
     143 *
     144 * @param[in] verbose A value indicating whether to print out verbose information.
     145 * @param[in] socket_ids A field of stored socket identifiers.
     146 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
     147 * @param[in] address The destination host address to send data to.
     148 * @param[in] addrlen The length of the destination address in bytes.
     149 * @param[in] data The data to be sent.
     150 * @param[in] size The data size in bytes.
     151 * @param[in] messages The number of datagrams per socket to be sent.
     152 * @return EOK on success.
     153 * @return Other error codes as defined for the sendto() function.
     154 */
     155int sockets_sendto(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen, char *data, int size, int messages)
     156{
    112157        int index;
    113158        int message;
    114 
    115         if(verbose){
     159        int rc;
     160
     161        if (verbose)
    116162                printf("\tSendto\t");
    117         }
    118         fflush(stdout);
    119         for(index = 0; index < sockets; ++ index){
    120                 for(message = 0; message < messages; ++ message){
    121                         if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, addrlen))){
    122                                 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
    123                                 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
    124                                 return ERROR_CODE;
    125                         }
    126                 }
    127                 if(verbose){
    128                         print_mark(index);
    129                 }
    130         }
    131         return EOK;
    132 }
    133 
    134 int sockets_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){
     163
     164        fflush(stdout);
     165       
     166        for (index = 0; index < sockets; index++) {
     167                for (message = 0; message < messages; message++) {
     168                        rc = sendto(socket_ids[index], data, size, 0, address, addrlen);
     169                        if (rc != EOK) {
     170                                printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
     171                                socket_print_error(stderr, rc, "Socket send: ", "\n");
     172                                return rc;
     173                        }
     174                }
     175                if (verbose)
     176                        print_mark(index);
     177        }
     178       
     179        return EOK;
     180}
     181
     182/** Receives data via sockets.
     183 *
     184 * @param[in] verbose A value indicating whether to print out verbose information.
     185 * @param[in] socket_ids A field of stored socket identifiers.
     186 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
     187 * @param[in] address The source host address of received datagrams.
     188 * @param[in,out] addrlen The maximum length of the source address in bytes. The actual size of the source address is set instead.
     189 * @param[out] data The received data.
     190 * @param[in] size The maximum data size in bytes.
     191 * @param[in] messages The number of datagrams per socket to be received.
     192 * @return EOK on success.
     193 * @return Other error codes as defined for the recvfrom() function.
     194 */
     195int sockets_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages)
     196{
    135197        int value;
    136198        int index;
    137199        int message;
    138200
    139         if(verbose){
     201        if (verbose)
    140202                printf("\tRecvfrom\t");
    141         }
    142         fflush(stdout);
    143         for(index = 0; index < sockets; ++ index){
    144                 for(message = 0; message < messages; ++ message){
     203       
     204        fflush(stdout);
     205       
     206        for (index = 0; index < sockets; index++) {
     207                for (message = 0; message < messages; message++) {
    145208                        value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
    146                         if(value < 0){
     209                        if (value < 0) {
    147210                                printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
    148211                                socket_print_error(stderr, value, "Socket receive: ", "\n");
     
    150213                        }
    151214                }
    152                 if(verbose){
    153                         print_mark(index);
    154                 }
    155         }
    156         return EOK;
    157 }
    158 
    159 int sockets_sendto_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){
    160         ERROR_DECLARE;
    161 
     215                if (verbose)
     216                        print_mark(index);
     217        }
     218        return EOK;
     219}
     220
     221/** Sends and receives data via sockets.
     222 *
     223 * Each datagram is sent and a reply read consequently.
     224 * The next datagram is sent after the reply is received.
     225 *
     226 * @param[in] verbose A value indicating whether to print out verbose information.
     227 * @param[in] socket_ids A field of stored socket identifiers.
     228 * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
     229 * @param[in,out] address The destination host address to send data to. The source host address of received datagrams is set instead.
     230 * @param[in] addrlen The length of the destination address in bytes.
     231 * @param[in,out] data The data to be sent. The received data are set instead.
     232 * @param[in] size The data size in bytes.
     233 * @param[in] messages The number of datagrams per socket to be received.
     234 * @return EOK on success.
     235 * @return Other error codes as defined for the recvfrom() function.
     236 */
     237int sockets_sendto_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages)
     238{
    162239        int value;
    163240        int index;
    164241        int message;
    165 
    166         if(verbose){
     242        int rc;
     243
     244        if (verbose)
    167245                printf("\tSendto and recvfrom\t");
    168         }
    169         fflush(stdout);
    170         for(index = 0; index < sockets; ++ index){
    171                 for(message = 0; message < messages; ++ message){
    172                         if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, * addrlen))){
    173                                 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
    174                                 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
    175                                 return ERROR_CODE;
     246
     247        fflush(stdout);
     248       
     249        for (index = 0; index < sockets; index++) {
     250                for (message = 0; message < messages; message++) {
     251                        rc = sendto(socket_ids[index], data, size, 0, address, *addrlen);
     252                        if (rc != EOK) {
     253                                printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
     254                                socket_print_error(stderr, rc, "Socket send: ", "\n");
     255                                return rc;
    176256                        }
    177257                        value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
    178                         if(value < 0){
     258                        if (value < 0) {
    179259                                printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
    180260                                socket_print_error(stderr, value, "Socket receive: ", "\n");
     
    182262                        }
    183263                }
    184                 if(verbose){
    185                         print_mark(index);
    186                 }
    187         }
    188         return EOK;
    189 }
    190 
    191 void print_mark(int index){
    192         if((index + 1) % 10){
     264                if (verbose)
     265                        print_mark(index);
     266        }
     267       
     268        return EOK;
     269}
     270
     271/** Prints a mark.
     272 *
     273 * If the index is a multiple of ten, a different mark is printed.
     274 *
     275 * @param[in] index The index of the mark to be printed.
     276 */
     277void print_mark(int index)
     278{
     279        if ((index + 1) % 10)
    193280                printf("*");
    194         }else{
     281        else
    195282                printf("|");
    196         }
    197283        fflush(stdout);
    198284}
  • uspace/app/nettest1/nettest.h

    r9a1d8ab r0b749a3  
    2828
    2929/** @addtogroup nettest
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Networking test support functions.
     34 * Networking test support functions.
    3535 */
    3636
    37 #ifndef __NET_TEST__
    38 #define __NET_TEST__
     37#ifndef NET_TEST_
     38#define NET_TEST_
    3939
    40 #include <socket.h>
     40#include <net/socket.h>
    4141
    42 /** Prints a mark.
    43  *  If the index is a multiple of ten, a different mark is printed.
    44  *  @param[in] index The index of the mark to be printed.
    45  */
    46 extern void print_mark(int index);
    47 
    48 /** Creates new sockets.
    49  *  @param[in] verbose A value indicating whether to print out verbose information.
    50  *  @param[out] socket_ids A field to store the socket identifiers.
    51  *  @param[in] sockets The number of sockets to create. Should be at most the size of the field.
    52  *  @param[in] family The socket address family.
    53  *  @param[in] type The socket type.
    54  *  @returns EOK on success.
    55  *  @returns Other error codes as defined for the socket() function.
    56  */
    57 extern int sockets_create(int verbose, int * socket_ids, int sockets, int family, sock_type_t type);
    58 
    59 /** Closes sockets.
    60  *  @param[in] verbose A value indicating whether to print out verbose information.
    61  *  @param[in] socket_ids A field of stored socket identifiers.
    62  *  @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
    63  *  @returns EOK on success.
    64  *  @returns Other error codes as defined for the closesocket() function.
    65  */
    66 extern int sockets_close(int verbose, int * socket_ids, int sockets);
    67 
    68 /** Connects sockets.
    69  *  @param[in] verbose A value indicating whether to print out verbose information.
    70  *  @param[in] socket_ids A field of stored socket identifiers.
    71  *  @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
    72  *  @param[in] address The destination host address to connect to.
    73  *  @param[in] addrlen The length of the destination address in bytes.
    74  *  @returns EOK on success.
    75  *  @returns Other error codes as defined for the connect() function.
    76  */
    77 extern int sockets_connect(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen);
    78 
    79 /** Sends data via sockets.
    80  *  @param[in] verbose A value indicating whether to print out verbose information.
    81  *  @param[in] socket_ids A field of stored socket identifiers.
    82  *  @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
    83  *  @param[in] address The destination host address to send data to.
    84  *  @param[in] addrlen The length of the destination address in bytes.
    85  *  @param[in] data The data to be sent.
    86  *  @param[in] size The data size in bytes.
    87  *  @param[in] messages The number of datagrams per socket to be sent.
    88  *  @returns EOK on success.
    89  *  @returns Other error codes as defined for the sendto() function.
    90  */
    91 extern int sockets_sendto(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages);
    92 
    93 /** Receives data via sockets.
    94  *  @param[in] verbose A value indicating whether to print out verbose information.
    95  *  @param[in] socket_ids A field of stored socket identifiers.
    96  *  @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
    97  *  @param[in] address The source host address of received datagrams.
    98  *  @param[in,out] addrlen The maximum length of the source address in bytes. The actual size of the source address is set instead.
    99  *  @param[out] data The received data.
    100  *  @param[in] size The maximum data size in bytes.
    101  *  @param[in] messages The number of datagrams per socket to be received.
    102  *  @returns EOK on success.
    103  *  @returns Other error codes as defined for the recvfrom() function.
    104  */
    105 extern int sockets_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages);
    106 
    107 /** Sends and receives data via sockets.
    108  *  Each datagram is sent and a reply read consequently.
    109  *  The next datagram is sent after the reply is received.
    110  *  @param[in] verbose A value indicating whether to print out verbose information.
    111  *  @param[in] socket_ids A field of stored socket identifiers.
    112  *  @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
    113  *  @param[in,out] address The destination host address to send data to. The source host address of received datagrams is set instead.
    114  *  @param[in] addrlen The length of the destination address in bytes.
    115  *  @param[in,out] data The data to be sent. The received data are set instead.
    116  *  @param[in] size The data size in bytes.
    117  *  @param[in] messages The number of datagrams per socket to be received.
    118  *  @returns EOK on success.
    119  *  @returns Other error codes as defined for the recvfrom() function.
    120  */
    121 extern int sockets_sendto_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages);
     42extern void print_mark(int);
     43extern int sockets_create(int, int *, int, int, sock_type_t);
     44extern int sockets_close(int, int *, int);
     45extern int sockets_connect(int, int *, int, struct sockaddr *, socklen_t);
     46extern int sockets_sendto(int, int *, int, struct sockaddr *, socklen_t, char *, int, int);
     47extern int sockets_recvfrom(int, int *, int, struct sockaddr *, socklen_t *, char *, int, int);
     48extern int sockets_sendto_recvfrom(int, int *, int, struct sockaddr *, socklen_t *, char *, int, int);
    12249
    12350#endif
     
    12552/** @}
    12653 */
     54
  • uspace/app/nettest1/nettest1.c

    r9a1d8ab r0b749a3  
    2828
    2929/** @addtogroup nettest
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Networking test 1 application - sockets.
    35  */
     34 * Networking test 1 application - sockets.
     35 */
     36
     37#include "nettest.h"
     38#include "print_error.h"
    3639
    3740#include <malloc.h>
     
    4245#include <arg_parse.h>
    4346
    44 #include <in.h>
    45 #include <in6.h>
    46 #include <inet.h>
    47 #include <socket.h>
    48 #include <net_err.h>
    49 #include <socket_parse.h>
    50 
    51 #include "nettest.h"
    52 #include "print_error.h"
    53 
    54 /** Echo module name.
    55  */
     47#include <net/in.h>
     48#include <net/in6.h>
     49#include <net/inet.h>
     50#include <net/socket.h>
     51#include <net/socket_parse.h>
     52
     53/** Echo module name. */
    5654#define NAME    "Nettest1"
    5755
    58 /** Packet data pattern.
    59  */
     56/** Packet data pattern. */
    6057#define NETTEST1_TEXT   "Networking test 1 - sockets"
    6158
    62 /** Module entry point.
    63  *  Starts testing.
    64  *  @param[in] argc The number of command line parameters.
    65  *  @param[in] argv The command line parameters.
    66  *  @returns EOK on success.
    67  */
    68 int main(int argc, char * argv[]);
    69 
    70 /** Prints the application help.
    71  */
    72 void nettest1_print_help(void);
    73 
    74 /** Refreshes the data.
    75  *  Fills the data block with the NETTEST1_TEXT pattern.
    76  *  @param[out] data The data block.
    77  *  @param[in] size The data block size in bytes.
    78  */
    79 void nettest1_refresh_data(char * data, size_t size);
    80 
    81 int main(int argc, char * argv[]){
    82         ERROR_DECLARE;
    83 
    84         size_t size                     = 27;
    85         int verbose                     = 0;
    86         sock_type_t type        = SOCK_DGRAM;
    87         int sockets                     = 10;
    88         int messages            = 10;
    89         int family                      = PF_INET;
    90         uint16_t port           = 7;
    91 
    92         socklen_t max_length                            = sizeof(struct sockaddr_in6);
    93         uint8_t address_data[max_length];
    94         struct sockaddr * address                       = (struct sockaddr *) address_data;
    95         struct sockaddr_in * address_in         = (struct sockaddr_in *) address;
    96         struct sockaddr_in6 * address_in6       = (struct sockaddr_in6 *) address;
    97         socklen_t addrlen;
    98 //      char address_string[INET6_ADDRSTRLEN];
    99         uint8_t * address_start;
    100 
    101         int * socket_ids;
    102         char * data;
     59static int family = PF_INET;
     60static sock_type_t type = SOCK_DGRAM;
     61static char *data;
     62static size_t size = 27;
     63static int verbose = 0;
     64
     65static struct sockaddr *address;
     66static socklen_t addrlen;
     67
     68static int sockets;
     69static int messages;
     70static uint16_t port;
     71
     72static void nettest1_print_help(void)
     73{
     74        printf(
     75            "Network Networking test 1 aplication - sockets\n"
     76            "Usage: echo [options] numeric_address\n"
     77            "Where options are:\n"
     78            "-f protocol_family | --family=protocol_family\n"
     79            "\tThe listenning socket protocol family. Only the PF_INET and "
     80            "PF_INET6 are supported.\n"
     81            "\n"
     82            "-h | --help\n"
     83            "\tShow this application help.\n"
     84            "\n"
     85            "-m count | --messages=count\n"
     86            "\tThe number of messages to send and receive per socket. The "
     87            "default is 10.\n"
     88            "\n"
     89            "-n sockets | --sockets=count\n"
     90            "\tThe number of sockets to use. The default is 10.\n"
     91            "\n"
     92            "-p port_number | --port=port_number\n"
     93            "\tThe port number the application should send messages to. The "
     94            "default is 7.\n"
     95            "\n"
     96            "-s packet_size | --size=packet_size\n"
     97            "\tThe packet data size the application sends. The default is "
     98            "28 bytes.\n"
     99            "\n"
     100            "-v | --verbose\n"
     101            "\tShow all output messages.\n");
     102}
     103
     104/** Parse one command-line option.
     105 *
     106 * @param argc          Number of all command-line arguments.
     107 * @param argv          All command-line arguments.
     108 * @param index         Current argument index (in, out).
     109 */
     110static int nettest1_parse_opt(int argc, char *argv[], int *index)
     111{
    103112        int value;
     113        int rc;
     114
     115        switch (argv[*index][1]) {
     116        /*
     117         * Short options with only one letter
     118         */
     119        case 'f':
     120                rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family);
     121                if (rc != EOK)
     122                        return rc;
     123                break;
     124        case 'h':
     125                nettest1_print_help();
     126                return EOK;
     127        case 'm':
     128                rc = arg_parse_int(argc, argv, index, &messages, 0);
     129                if (rc != EOK)
     130                        return rc;
     131                break;
     132        case 'n':
     133                rc = arg_parse_int(argc, argv, index, &sockets, 0);
     134                if (rc != EOK)
     135                        return rc;
     136                break;
     137        case 'p':
     138                rc = arg_parse_int(argc, argv, index, &value, 0);
     139                if (rc != EOK)
     140                        return rc;
     141                port = (uint16_t) value;
     142                break;
     143        case 's':
     144                rc = arg_parse_int(argc, argv, index, &value, 0);
     145                if (rc != EOK)
     146                        return rc;
     147                size = (value >= 0) ? (size_t) value : 0;
     148                break;
     149        case 't':
     150                rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type);
     151                if (rc != EOK)
     152                        return rc;
     153                type = (sock_type_t) value;
     154                break;
     155        case 'v':
     156                verbose = 1;
     157                break;
     158        /*
     159         * Long options with double dash ('-')
     160         */
     161        case '-':
     162                if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
     163                        rc = arg_parse_name_int(argc, argv, index, &family, 9,
     164                            socket_parse_protocol_family);
     165                        if (rc != EOK)
     166                                return rc;
     167                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
     168                        nettest1_print_help();
     169                        return EOK;
     170                } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) {
     171                        rc = arg_parse_int(argc, argv, index, &messages, 8);
     172                        if (rc != EOK)
     173                                return rc;
     174                } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) {
     175                        rc = arg_parse_int(argc, argv, index, &sockets, 8);
     176                        if (rc != EOK)
     177                                return rc;
     178                } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
     179                        rc = arg_parse_int(argc, argv, index, &value, 7);
     180                        if (rc != EOK)
     181                                return rc;
     182                        port = (uint16_t) value;
     183                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     184                        rc = arg_parse_name_int(argc, argv, index, &value, 7,
     185                            socket_parse_socket_type);
     186                        if (rc != EOK)
     187                                return rc;
     188                        type = (sock_type_t) value;
     189                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     190                        verbose = 1;
     191                } else {
     192                        nettest1_print_help();
     193                        return EINVAL;
     194                }
     195                break;
     196        default:
     197                nettest1_print_help();
     198                return EINVAL;
     199        }
     200
     201        return EOK;
     202}
     203
     204/** Fill buffer with the NETTEST1_TEXT pattern.
     205 *
     206 * @param buffer        Data buffer.
     207 * @param size          Buffer size in bytes.
     208 */
     209static void nettest1_fill_buffer(char *buffer, size_t size)
     210{
     211        size_t length;
     212
     213        length = 0;
     214        while (size > length + sizeof(NETTEST1_TEXT) - 1) {
     215                memcpy(buffer + length, NETTEST1_TEXT,
     216                    sizeof(NETTEST1_TEXT) - 1);
     217                length += sizeof(NETTEST1_TEXT) - 1;
     218        }
     219
     220        memcpy(buffer + length, NETTEST1_TEXT, size - length);
     221        buffer[size] = '\0';
     222}
     223
     224static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
     225{
     226        int rc;
     227
     228        if (verbose)
     229                printf("%d sockets, %d messages\n", nsockets, nmessages);
     230
     231        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
     232        if (rc != EOK)
     233                return rc;
     234
     235        if (type == SOCK_STREAM) {
     236                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     237                    addrlen);
     238                if (rc != EOK)
     239                        return rc;
     240        }
     241
     242        rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
     243            &addrlen, data, size, nmessages);
     244        if (rc != EOK)
     245                return rc;
     246
     247        rc = sockets_close(verbose, socket_ids, nsockets);
     248        if (rc != EOK)
     249                return rc;
     250
     251        if (verbose)
     252                printf("\tOK\n");
     253
     254        /****/
     255
     256        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
     257        if (rc != EOK)
     258                return rc;
     259
     260        if (type == SOCK_STREAM) {
     261                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     262                    addrlen);
     263                if (rc != EOK)
     264                        return rc;
     265        }
     266
     267        rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
     268            data, size, nmessages);
     269        if (rc != EOK)
     270                return rc;
     271
     272        rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
     273            data, size, nmessages);
     274        if (rc != EOK)
     275                return rc;
     276
     277        rc = sockets_close(verbose, socket_ids, nsockets);
     278        if (rc != EOK)
     279                return rc;
     280
     281        if (verbose)
     282                printf("\tOK\n");
     283
     284        return EOK;
     285}
     286
     287int main(int argc, char *argv[])
     288{
     289        struct sockaddr_in address_in;
     290        struct sockaddr_in6 address_in6;
     291        uint8_t *address_start;
     292
     293        int *socket_ids;
    104294        int index;
    105295        struct timeval time_before;
    106296        struct timeval time_after;
    107297
    108         // parse the command line arguments
    109         // stop before the last argument if it does not start with the minus sign ('-')
    110         for(index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index){
    111                 // options should start with the minus sign ('-')
    112                 if(argv[index][0] == '-'){
    113                         switch(argv[index][1]){
    114                                 // short options with only one letter
    115                                 case 'f':
    116                                         ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family));
    117                                         break;
    118                                 case 'h':
    119                                         nettest1_print_help();
    120                                         return EOK;
    121                                         break;
    122                                 case 'm':
    123                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 0));
    124                                         break;
    125                                 case 'n':
    126                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 0));
    127                                         break;
    128                                 case 'p':
    129                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0));
    130                                         port = (uint16_t) value;
    131                                         break;
    132                                 case 's':
    133                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0));
    134                                         size = (value >= 0) ? (size_t) value : 0;
    135                                         break;
    136                                 case 't':
    137                                         ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type));
    138                                         type = (sock_type_t) value;
    139                                         break;
    140                                 case 'v':
    141                                         verbose = 1;
    142                                         break;
    143                                 // long options with the double minus sign ('-')
    144                                 case '-':
    145                                         if(str_lcmp(argv[index] + 2, "family=", 7) == 0){
    146                                                 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family));
    147                                         }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){
    148                                                 nettest1_print_help();
    149                                                 return EOK;
    150                                         }else if(str_lcmp(argv[index] + 2, "messages=", 6) == 0){
    151                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 8));
    152                                         }else if(str_lcmp(argv[index] + 2, "sockets=", 6) == 0){
    153                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 8));
    154                                         }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){
    155                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7));
    156                                                 port = (uint16_t) value;
    157                                         }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){
    158                                                 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type));
    159                                                 type = (sock_type_t) value;
    160                                         }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){
    161                                                 verbose = 1;
    162                                         }else{
    163                                                 nettest1_print_help();
    164                                                 return EINVAL;
    165                                         }
    166                                         break;
    167                                 default:
    168                                         nettest1_print_help();
    169                                         return EINVAL;
    170                         }
    171                 }else{
     298        int rc;
     299
     300        sockets = 10;
     301        messages = 10;
     302        port = 7;
     303
     304        /*
     305         * Parse the command line arguments. Stop before the last argument
     306         * if it does not start with dash ('-')
     307         */
     308        for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) {
     309                /* Options should start with dash ('-') */
     310                if (argv[index][0] == '-') {
     311                        rc = nettest1_parse_opt(argc, argv, &index);
     312                        if (rc != EOK)
     313                                return rc;
     314                } else {
    172315                        nettest1_print_help();
    173316                        return EINVAL;
     
    175318        }
    176319
    177         // if not before the last argument containing the address
    178         if(index >= argc){
     320        /* If not before the last argument containing the address */
     321        if (index >= argc) {
    179322                printf("Command line error: missing address\n");
    180323                nettest1_print_help();
     
    182325        }
    183326
    184         // prepare the address buffer
    185         bzero(address_data, max_length);
    186         switch(family){
    187                 case PF_INET:
    188                         address_in->sin_family = AF_INET;
    189                         address_in->sin_port = htons(port);
    190                         address_start = (uint8_t *) &address_in->sin_addr.s_addr;
    191                         addrlen = sizeof(struct sockaddr_in);
    192                         break;
    193                 case PF_INET6:
    194                         address_in6->sin6_family = AF_INET6;
    195                         address_in6->sin6_port = htons(port);
    196                         address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
    197                         addrlen = sizeof(struct sockaddr_in6);
    198                         break;
    199                 default:
    200                         fprintf(stderr, "Address family is not supported\n");
    201                         return EAFNOSUPPORT;
    202         }
    203 
    204         // parse the last argument which should contain the address
    205         if(ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))){
    206                 fprintf(stderr, "Address parse error %d\n", ERROR_CODE);
    207                 return ERROR_CODE;
    208         }
    209 
    210         // check the buffer size
    211         if(size <= 0){
    212                 fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size);
     327        /* Prepare the address buffer */
     328
     329        switch (family) {
     330        case PF_INET:
     331                address_in.sin_family = AF_INET;
     332                address_in.sin_port = htons(port);
     333                address = (struct sockaddr *) &address_in;
     334                addrlen = sizeof(address_in);
     335                address_start = (uint8_t *) &address_in.sin_addr.s_addr;
     336                break;
     337        case PF_INET6:
     338                address_in6.sin6_family = AF_INET6;
     339                address_in6.sin6_port = htons(port);
     340                address = (struct sockaddr *) &address_in6;
     341                addrlen = sizeof(address_in6);
     342                address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
     343                break;
     344        default:
     345                fprintf(stderr, "Address family is not supported\n");
     346                return EAFNOSUPPORT;
     347        }
     348
     349        /* Parse the last argument which should contain the address */
     350        rc = inet_pton(family, argv[argc - 1], address_start);
     351        if (rc != EOK) {
     352                fprintf(stderr, "Address parse error %d\n", rc);
     353                return rc;
     354        }
     355
     356        /* Check data buffer size */
     357        if (size <= 0) {
     358                fprintf(stderr, "Data buffer size too small (%d). Using 1024 "
     359                    "bytes instead.\n", size);
    213360                size = 1024;
    214361        }
    215362
    216         // prepare the buffer
    217         // size plus the terminating null (\0)
     363        /*
     364         * Prepare data buffer. Allocate size bytes plus one for the
     365         * trailing null character.
     366         */
    218367        data = (char *) malloc(size + 1);
    219         if(! data){
     368        if (!data) {
    220369                fprintf(stderr, "Failed to allocate data buffer.\n");
    221370                return ENOMEM;
    222371        }
    223         nettest1_refresh_data(data, size);
    224 
    225         // check the socket count
    226         if(sockets <= 0){
    227                 fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets);
     372        nettest1_fill_buffer(data, size);
     373
     374        /* Check socket count */
     375        if (sockets <= 0) {
     376                fprintf(stderr, "Socket count too small (%d). Using "
     377                    "2 instead.\n", sockets);
    228378                sockets = 2;
    229379        }
    230380
    231         // prepare the socket buffer
    232         // count plus the terminating null (\0)
     381        /*
     382         * Prepare socket buffer. Allocate count fields plus the terminating
     383         * null (\0).
     384         */
    233385        socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    234         if(! socket_ids){
     386        if (!socket_ids) {
    235387                fprintf(stderr, "Failed to allocate receive buffer.\n");
    236388                return ENOMEM;
    237389        }
    238         socket_ids[sockets] = NULL;
    239 
    240         if(verbose){
     390        socket_ids[sockets] = 0;
     391
     392        if (verbose)
    241393                printf("Starting tests\n");
    242         }
    243 
    244         if(verbose){
    245                 printf("1 socket, 1 message\n");
    246         }
    247 
    248         if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){
    249                 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
    250                 return ERROR_CODE;
    251         }
    252 
    253         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type));
    254         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1));
    255         if(verbose){
    256                 printf("\tOK\n");
    257         }
    258 
    259         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type));
    260         if(type == SOCK_STREAM){
    261                 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen));
    262         }
    263         ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1));
    264         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1));
    265         if(verbose){
    266                 printf("\tOK\n");
    267         }
    268 
    269         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type));
    270         if(type == SOCK_STREAM){
    271                 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen));
    272         }
    273         ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, 1));
    274         ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, 1));
    275         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1));
    276         if(verbose){
    277                 printf("\tOK\n");
    278         }
    279 
    280         if(verbose){
    281                 printf("1 socket, %d messages\n", messages);
    282         }
    283 
    284         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type));
    285         if(type == SOCK_STREAM){
    286                 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen));
    287         }
    288         ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages));
    289         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1));
    290         if(verbose){
    291                 printf("\tOK\n");
    292         }
    293 
    294         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, 1, family, type));
    295         if(type == SOCK_STREAM){
    296                 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, 1, address, addrlen));
    297         }
    298         ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, 1, address, addrlen, data, size, messages));
    299         ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, 1, address, &addrlen, data, size, messages));
    300         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, 1));
    301         if(verbose){
    302                 printf("\tOK\n");
    303         }
    304 
    305         if(verbose){
    306                 printf("%d sockets, 1 message\n", sockets);
    307         }
    308 
    309         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type));
    310         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets));
    311         if(verbose){
    312                 printf("\tOK\n");
    313         }
    314 
    315         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type));
    316         if(type == SOCK_STREAM){
    317                 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen));
    318         }
    319         ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1));
    320         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets));
    321         if(verbose){
    322                 printf("\tOK\n");
    323         }
    324 
    325         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type));
    326         if(type == SOCK_STREAM){
    327                 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen));
    328         }
    329         ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, 1));
    330         ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, 1));
    331         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets));
    332         if(verbose){
    333                 printf("\tOK\n");
    334         }
    335 
    336         if(verbose){
    337                 printf("%d sockets, %d messages\n", sockets, messages);
    338         }
    339 
    340         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type));
    341         if(type == SOCK_STREAM){
    342                 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen));
    343         }
    344         ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages));
    345         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets));
    346         if(verbose){
    347                 printf("\tOK\n");
    348         }
    349 
    350         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type));
    351         if(type == SOCK_STREAM){
    352                 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen));
    353         }
    354         ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages));
    355         ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages));
    356         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets));
    357 
    358         if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){
    359                 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
    360                 return ERROR_CODE;
    361         }
    362 
    363         if(verbose){
    364                 printf("\tOK\n");
    365         }
    366 
    367         printf("Tested in %d microseconds\n", tv_sub(&time_after, &time_before));
    368 
    369         if(verbose){
     394
     395        rc = gettimeofday(&time_before, NULL);
     396        if (rc != EOK) {
     397                fprintf(stderr, "Get time of day error %d\n", rc);
     398                return rc;
     399        }
     400
     401        nettest1_test(socket_ids,       1,        1);
     402        nettest1_test(socket_ids,       1, messages);
     403        nettest1_test(socket_ids, sockets,        1);
     404        nettest1_test(socket_ids, sockets, messages);
     405
     406        rc = gettimeofday(&time_after, NULL);
     407        if (rc != EOK) {
     408                fprintf(stderr, "Get time of day error %d\n", rc);
     409                return rc;
     410        }
     411
     412        printf("Tested in %d microseconds\n", tv_sub(&time_after,
     413            &time_before));
     414
     415        if (verbose)
    370416                printf("Exiting\n");
    371         }
    372417
    373418        return EOK;
    374419}
    375420
    376 void nettest1_print_help(void){
    377         printf(
    378                 "Network Networking test 1 aplication - sockets\n" \
    379                 "Usage: echo [options] numeric_address\n" \
    380                 "Where options are:\n" \
    381                 "-f protocol_family | --family=protocol_family\n" \
    382                 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
    383                 "\n" \
    384                 "-h | --help\n" \
    385                 "\tShow this application help.\n"
    386                 "\n" \
    387                 "-m count | --messages=count\n" \
    388                 "\tThe number of messages to send and receive per socket. The default is 10.\n" \
    389                 "\n" \
    390                 "-n sockets | --sockets=count\n" \
    391                 "\tThe number of sockets to use. The default is 10.\n" \
    392                 "\n" \
    393                 "-p port_number | --port=port_number\n" \
    394                 "\tThe port number the application should send messages to. The default is 7.\n" \
    395                 "\n" \
    396                 "-s packet_size | --size=packet_size\n" \
    397                 "\tThe packet data size the application sends. The default is 28 bytes.\n" \
    398                 "\n" \
    399                 "-v | --verbose\n" \
    400                 "\tShow all output messages.\n"
    401         );
    402 }
    403 
    404 void nettest1_refresh_data(char * data, size_t size){
    405         size_t length;
    406 
    407         // fill the data
    408         length = 0;
    409         while(size > length + sizeof(NETTEST1_TEXT) - 1){
    410                 memcpy(data + length, NETTEST1_TEXT, sizeof(NETTEST1_TEXT) - 1);
    411                 length += sizeof(NETTEST1_TEXT) - 1;
    412         }
    413         memcpy(data + length, NETTEST1_TEXT, size - length);
    414         data[size] = '\0';
    415 }
    416421
    417422/** @}
  • uspace/app/nettest2/Makefile

    r9a1d8ab r0b749a3  
    2929
    3030USPACE_PREFIX = ../..
    31 LIBS = $(LIBSOCKET_PREFIX)/libsocket.a
    32 EXTRA_CFLAGS = -I$(LIBSOCKET_PREFIX)/include
     31LIBS =
     32EXTRA_CFLAGS =
    3333BINARY = nettest2
    3434
  • uspace/app/nettest2/nettest2.c

    r9a1d8ab r0b749a3  
    2828
    2929/** @addtogroup nettest
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Networking test 2 application - transfer.
    35  */
     34 * Networking test 2 application - transfer.
     35 */
     36
     37#include "nettest.h"
     38#include "print_error.h"
    3639
    3740#include <malloc.h>
     
    4144#include <time.h>
    4245#include <arg_parse.h>
    43 
    44 #include <in.h>
    45 #include <in6.h>
    46 #include <inet.h>
    47 #include <socket.h>
    48 #include <net_err.h>
    49 #include <socket_parse.h>
    50 
    51 #include "nettest.h"
    52 #include "print_error.h"
    53 
    54 /** Echo module name.
    55  */
     46#include <bool.h>
     47
     48#include <net/in.h>
     49#include <net/in6.h>
     50#include <net/inet.h>
     51#include <net/socket.h>
     52#include <net/socket_parse.h>
     53
     54/** Echo module name. */
    5655#define NAME    "Nettest2"
    5756
    58 /** Packet data pattern.
    59  */
     57/** Packet data pattern. */
    6058#define NETTEST2_TEXT   "Networking test 2 - transfer"
    6159
    62 /** Module entry point.
    63  *  Starts testing.
    64  *  @param[in] argc The number of command line parameters.
    65  *  @param[in] argv The command line parameters.
    66  *  @returns EOK on success.
    67  */
    68 int main(int argc, char * argv[]);
    69 
    70 /** Prints the application help.
    71  */
    72 void nettest2_print_help(void);
    73 
    74 /** Refreshes the data.
    75  *  Fills the data block with the NETTEST1_TEXT pattern.
    76  *  @param[out] data The data block.
    77  *  @param[in] size The data block size in bytes.
    78  */
    79 void nettest2_refresh_data(char * data, size_t size);
    80 
    81 int main(int argc, char * argv[]){
    82         ERROR_DECLARE;
    83 
    84         size_t size                     = 28;
    85         int verbose                     = 0;
    86         sock_type_t type        = SOCK_DGRAM;
    87         int sockets                     = 10;
    88         int messages            = 10;
    89         int family                      = PF_INET;
    90         uint16_t port           = 7;
    91 
    92         socklen_t max_length                            = sizeof(struct sockaddr_in6);
    93         uint8_t address_data[max_length];
    94         struct sockaddr * address                       = (struct sockaddr *) address_data;
    95         struct sockaddr_in * address_in         = (struct sockaddr_in *) address;
    96         struct sockaddr_in6 * address_in6       = (struct sockaddr_in6 *) address;
     60static size_t size;
     61static bool verbose;
     62static sock_type_t type;
     63static int sockets;
     64static int messages;
     65static int family;
     66static uint16_t port;
     67
     68static void nettest2_print_help(void)
     69{
     70        printf(
     71            "Network Networking test 2 aplication - UDP transfer\n"
     72            "Usage: echo [options] address\n"
     73            "Where options are:\n"
     74            "-f protocol_family | --family=protocol_family\n"
     75            "\tThe listenning socket protocol family. Only the PF_INET and "
     76            "PF_INET6 are supported.\n"
     77            "\n"
     78            "-h | --help\n"
     79            "\tShow this application help.\n"
     80            "\n"
     81            "-m count | --messages=count\n"
     82            "\tThe number of messages to send and receive per socket. The "
     83            "default is 10.\n"
     84            "\n"
     85            "-n sockets | --sockets=count\n"
     86            "\tThe number of sockets to use. The default is 10.\n"
     87            "\n"
     88            "-p port_number | --port=port_number\n"
     89            "\tThe port number the application should send messages to. The "
     90            "default is 7.\n"
     91            "\n"
     92            "-s packet_size | --size=packet_size\n"
     93            "\tThe packet data size the application sends. The default is 29 "
     94            "bytes.\n"
     95            "\n"
     96            "-v | --verbose\n"
     97            "\tShow all output messages.\n");
     98}
     99
     100/** Fill buffer with the NETTEST2_TEXT pattern.
     101 *
     102 * @param buffer        Data buffer.
     103 * @param size          Buffer size in bytes.
     104 */
     105static void nettest2_fill_buffer(char *buffer, size_t size)
     106{
     107        size_t length;
     108
     109        length = 0;
     110        while (size > length + sizeof(NETTEST2_TEXT) - 1) {
     111                memcpy(buffer + length, NETTEST2_TEXT,
     112                    sizeof(NETTEST2_TEXT) - 1);
     113                length += sizeof(NETTEST2_TEXT) - 1;
     114        }
     115
     116        memcpy(buffer + length, NETTEST2_TEXT, size - length);
     117        buffer[size] = '\0';
     118}
     119
     120/** Parse one command-line option.
     121 *
     122 * @param argc          Number of all command-line arguments.
     123 * @param argv          All command-line arguments.
     124 * @param index         Current argument index (in, out).
     125 */
     126static int nettest2_parse_opt(int argc, char *argv[], int *index)
     127{
     128        int value;
     129        int rc;
     130
     131        switch (argv[*index][1]) {
     132        /*
     133         * Short options with only one letter
     134         */
     135        case 'f':
     136                rc = arg_parse_name_int(argc, argv, index, &family, 0,
     137                    socket_parse_protocol_family);
     138                if (rc != EOK)
     139                        return rc;
     140                break;
     141        case 'h':
     142                nettest2_print_help();
     143                return EOK;
     144                break;
     145        case 'm':
     146                rc = arg_parse_int(argc, argv, index, &messages, 0);
     147                if (rc != EOK)
     148                        return rc;
     149                break;
     150        case 'n':
     151                rc = arg_parse_int(argc, argv, index, &sockets, 0);
     152                if (rc != EOK)
     153                        return rc;
     154                break;
     155        case 'p':
     156                rc = arg_parse_int(argc, argv, index, &value, 0);
     157                if (rc != EOK)
     158                        return rc;
     159                port = (uint16_t) value;
     160                break;
     161        case 's':
     162                rc = arg_parse_int(argc, argv, index, &value, 0);
     163                if (rc != EOK)
     164                        return rc;
     165                size = (value >= 0) ? (size_t) value : 0;
     166                break;
     167        case 't':
     168                rc = arg_parse_name_int(argc, argv, index, &value, 0,
     169                    socket_parse_socket_type);
     170                if (rc != EOK)
     171                        return rc;
     172                type = (sock_type_t) value;
     173                break;
     174        case 'v':
     175                verbose = true;
     176                break;
     177        /*
     178         * Long options with double dash ('-')
     179         */
     180        case '-':
     181                if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
     182                        rc = arg_parse_name_int(argc, argv, index, &family, 9,
     183                            socket_parse_protocol_family);
     184                        if (rc != EOK)
     185                                return rc;
     186                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
     187                        nettest2_print_help();
     188                        return EOK;
     189                } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) {
     190                        rc = arg_parse_int(argc, argv, index, &messages, 8);
     191                        if (rc != EOK)
     192                                return rc;
     193                } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) {
     194                        rc = arg_parse_int(argc, argv, index, &sockets, 8);
     195                        if (rc != EOK)
     196                                return rc;
     197                } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
     198                        rc = arg_parse_int(argc, argv, index, &value, 7);
     199                        if (rc != EOK)
     200                                return rc;
     201                        port = (uint16_t) value;
     202                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     203                        rc = arg_parse_name_int(argc, argv, index, &value, 7,
     204                            socket_parse_socket_type);
     205                        if (rc != EOK)
     206                                return rc;
     207                        type = (sock_type_t) value;
     208                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     209                        verbose = 1;
     210                } else {
     211                        nettest2_print_help();
     212                        return EINVAL;
     213                }
     214                break;
     215        default:
     216                nettest2_print_help();
     217                return EINVAL;
     218        }
     219
     220        return EOK;
     221}
     222
     223int main(int argc, char *argv[])
     224{
     225        struct sockaddr *address;
     226        struct sockaddr_in address_in;
     227        struct sockaddr_in6 address_in6;
    97228        socklen_t addrlen;
    98 //      char address_string[INET6_ADDRSTRLEN];
    99         uint8_t * address_start;
    100 
    101         int * socket_ids;
    102         char * data;
    103         int value;
     229        uint8_t *address_start;
     230
     231        int *socket_ids;
     232        char *data;
    104233        int index;
    105234        struct timeval time_before;
    106235        struct timeval time_after;
    107236
    108         // parse the command line arguments
    109         // stop before the last argument if it does not start with the minus sign ('-')
    110         for(index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); ++ index){
    111                 // options should start with the minus sign ('-')
    112                 if(argv[index][0] == '-'){
    113                         switch(argv[index][1]){
    114                                 // short options with only one letter
    115                                 case 'f':
    116                                         ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family));
    117                                         break;
    118                                 case 'h':
    119                                         nettest2_print_help();
    120                                         return EOK;
    121                                         break;
    122                                 case 'm':
    123                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 0));
    124                                         break;
    125                                 case 'n':
    126                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 0));
    127                                         break;
    128                                 case 'p':
    129                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0));
    130                                         port = (uint16_t) value;
    131                                         break;
    132                                 case 's':
    133                                         ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 0));
    134                                         size = (value >= 0) ? (size_t) value : 0;
    135                                         break;
    136                                 case 't':
    137                                         ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type));
    138                                         type = (sock_type_t) value;
    139                                         break;
    140                                 case 'v':
    141                                         verbose = 1;
    142                                         break;
    143                                 // long options with the double minus sign ('-')
    144                                 case '-':
    145                                         if(str_lcmp(argv[index] + 2, "family=", 7) == 0){
    146                                                 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family));
    147                                         }else if(str_lcmp(argv[index] + 2, "help", 5) == 0){
    148                                                 nettest2_print_help();
    149                                                 return EOK;
    150                                         }else if(str_lcmp(argv[index] + 2, "messages=", 6) == 0){
    151                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &messages, 8));
    152                                         }else if(str_lcmp(argv[index] + 2, "sockets=", 6) == 0){
    153                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &sockets, 8));
    154                                         }else if(str_lcmp(argv[index] + 2, "port=", 5) == 0){
    155                                                 ERROR_PROPAGATE(arg_parse_int(argc, argv, &index, &value, 7));
    156                                                 port = (uint16_t) value;
    157                                         }else if(str_lcmp(argv[index] + 2, "type=", 5) == 0){
    158                                                 ERROR_PROPAGATE(arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type));
    159                                                 type = (sock_type_t) value;
    160                                         }else if(str_lcmp(argv[index] + 2, "verbose", 8) == 0){
    161                                                 verbose = 1;
    162                                         }else{
    163                                                 nettest2_print_help();
    164                                                 return EINVAL;
    165                                         }
    166                                         break;
    167                                 default:
    168                                         nettest2_print_help();
    169                                         return EINVAL;
    170                         }
    171                 }else{
     237        int rc;
     238
     239        size = 28;
     240        verbose = false;
     241        type = SOCK_DGRAM;
     242        sockets = 10;
     243        messages = 10;
     244        family = PF_INET;
     245        port = 7;
     246
     247        /*
     248         * Parse the command line arguments.
     249         *
     250         * Stop before the last argument if it does not start with dash ('-')
     251         */
     252        for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
     253            (argv[index][0] == '-')); ++index) {
     254
     255                /* Options should start with dash ('-') */
     256                if (argv[index][0] == '-') {
     257                        rc = nettest2_parse_opt(argc, argv, &index);
     258                        if (rc != EOK)
     259                                return rc;
     260                } else {
    172261                        nettest2_print_help();
    173262                        return EINVAL;
     
    175264        }
    176265
    177         // if not before the last argument containing the address
    178         if(index >= argc){
     266        /* If not before the last argument containing the address */
     267        if (index >= argc) {
    179268                printf("Command line error: missing address\n");
    180269                nettest2_print_help();
     
    182271        }
    183272
    184         // prepare the address buffer
    185         bzero(address_data, max_length);
    186         switch(family){
    187                 case PF_INET:
    188                         address_in->sin_family = AF_INET;
    189                         address_in->sin_port = htons(port);
    190                         address_start = (uint8_t *) &address_in->sin_addr.s_addr;
    191                         addrlen = sizeof(struct sockaddr_in);
    192                         break;
    193                 case PF_INET6:
    194                         address_in6->sin6_family = AF_INET6;
    195                         address_in6->sin6_port = htons(port);
    196                         address_start = (uint8_t *) &address_in6->sin6_addr.s6_addr;
    197                         addrlen = sizeof(struct sockaddr_in6);
    198                         break;
    199                 default:
    200                         fprintf(stderr, "Address family is not supported\n");
    201                         return EAFNOSUPPORT;
    202         }
    203 
    204         // parse the last argument which should contain the address
    205         if(ERROR_OCCURRED(inet_pton(family, argv[argc - 1], address_start))){
    206                 fprintf(stderr, "Address parse error %d\n", ERROR_CODE);
    207                 return ERROR_CODE;
    208         }
    209 
    210         // check the buffer size
    211         if(size <= 0){
    212                 fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size);
     273        /* Prepare the address buffer */
     274
     275        switch (family) {
     276        case PF_INET:
     277                address_in.sin_family = AF_INET;
     278                address_in.sin_port = htons(port);
     279                address = (struct sockaddr *) &address_in;
     280                addrlen = sizeof(address_in);
     281                address_start = (uint8_t *) &address_in.sin_addr.s_addr;
     282                break;
     283        case PF_INET6:
     284                address_in6.sin6_family = AF_INET6;
     285                address_in6.sin6_port = htons(port);
     286                address = (struct sockaddr *) &address_in6;
     287                addrlen = sizeof(address_in6);
     288                address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
     289                break;
     290        default:
     291                fprintf(stderr, "Address family is not supported\n");
     292                return EAFNOSUPPORT;
     293        }
     294
     295        /* Parse the last argument which should contain the address. */
     296        rc = inet_pton(family, argv[argc - 1], address_start);
     297        if (rc != EOK) {
     298                fprintf(stderr, "Address parse error %d\n", rc);
     299                return rc;
     300        }
     301
     302        /* Check data buffer size. */
     303        if (size <= 0) {
     304                fprintf(stderr, "Data buffer size too small (%d). Using 1024 "
     305                    "bytes instead.\n", size);
    213306                size = 1024;
    214307        }
    215308
    216         // prepare the buffer
    217         // size plus terminating null (\0)
     309        /*
     310         * Prepare the buffer. Allocate size bytes plus one for terminating
     311         * null character.
     312         */
    218313        data = (char *) malloc(size + 1);
    219         if(! data){
     314        if (!data) {
    220315                fprintf(stderr, "Failed to allocate data buffer.\n");
    221316                return ENOMEM;
    222317        }
    223         nettest2_refresh_data(data, size);
    224 
    225         // check the socket count
    226         if(sockets <= 0){
    227                 fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets);
     318
     319        /* Fill buffer with a pattern. */
     320        nettest2_fill_buffer(data, size);
     321
     322        /* Check socket count. */
     323        if (sockets <= 0) {
     324                fprintf(stderr, "Socket count too small (%d). Using "
     325                    "2 instead.\n", sockets);
    228326                sockets = 2;
    229327        }
    230328
    231         // prepare the socket buffer
    232         // count plus the terminating null (\0)
     329        /*
     330         * Prepare the socket buffer.
     331         * Allocate count entries plus the terminating null (\0)
     332         */
    233333        socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    234         if(! socket_ids){
     334        if (!socket_ids) {
    235335                fprintf(stderr, "Failed to allocate receive buffer.\n");
    236336                return ENOMEM;
    237337        }
    238         socket_ids[sockets] = NULL;
    239 
    240         if(verbose){
     338        socket_ids[sockets] = 0;
     339
     340        if (verbose)
    241341                printf("Starting tests\n");
    242         }
    243 
    244         ERROR_PROPAGATE(sockets_create(verbose, socket_ids, sockets, family, type));
    245 
    246         if(type == SOCK_STREAM){
    247                 ERROR_PROPAGATE(sockets_connect(verbose, socket_ids, sockets, address, addrlen));
    248         }
    249 
    250         if(verbose){
     342
     343        rc = sockets_create(verbose, socket_ids, sockets, family, type);
     344        if (rc != EOK)
     345                return rc;
     346
     347        if (type == SOCK_STREAM) {
     348                rc = sockets_connect(verbose, socket_ids, sockets,
     349                    address, addrlen);
     350                if (rc != EOK)
     351                        return rc;
     352        }
     353
     354        if (verbose)
    251355                printf("\n");
    252         }
    253 
    254         if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){
    255                 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
    256                 return ERROR_CODE;
    257         }
    258 
    259         ERROR_PROPAGATE(sockets_sendto_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages));
    260 
    261         if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){
    262                 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
    263                 return ERROR_CODE;
    264         }
    265 
    266         if(verbose){
     356
     357        rc = gettimeofday(&time_before, NULL);
     358        if (rc != EOK) {
     359                fprintf(stderr, "Get time of day error %d\n", rc);
     360                return rc;
     361        }
     362
     363        rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address,
     364            &addrlen, data, size, messages);
     365        if (rc != EOK)
     366                return rc;
     367
     368        rc = gettimeofday(&time_after, NULL);
     369        if (rc != EOK) {
     370                fprintf(stderr, "Get time of day error %d\n", rc);
     371                return rc;
     372        }
     373
     374        if (verbose)
    267375                printf("\tOK\n");
    268         }
    269 
    270         printf("sendto + recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before));
    271 
    272         if(ERROR_OCCURRED(gettimeofday(&time_before, NULL))){
    273                 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
    274                 return ERROR_CODE;
    275         }
    276 
    277         ERROR_PROPAGATE(sockets_sendto(verbose, socket_ids, sockets, address, addrlen, data, size, messages));
    278         ERROR_PROPAGATE(sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen, data, size, messages));
    279 
    280         if(ERROR_OCCURRED(gettimeofday(&time_after, NULL))){
    281                 fprintf(stderr, "Get time of day error %d\n", ERROR_CODE);
    282                 return ERROR_CODE;
    283         }
    284 
    285         if(verbose){
     376
     377        printf("sendto + recvfrom tested in %d microseconds\n",
     378            tv_sub(&time_after, &time_before));
     379
     380        rc = gettimeofday(&time_before, NULL);
     381        if (rc != EOK) {
     382                fprintf(stderr, "Get time of day error %d\n", rc);
     383                return rc;
     384        }
     385
     386        rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen,
     387            data, size, messages);
     388        if (rc != EOK)
     389                return rc;
     390
     391        rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen,
     392            data, size, messages);
     393        if (rc != EOK)
     394                return rc;
     395
     396        rc = gettimeofday(&time_after, NULL);
     397        if (rc != EOK) {
     398                fprintf(stderr, "Get time of day error %d\n", rc);
     399                return rc;
     400        }
     401
     402        if (verbose)
    286403                printf("\tOK\n");
    287         }
    288 
    289         printf("sendto, recvfrom tested in %d microseconds\n", tv_sub(&time_after, &time_before));
    290 
    291         ERROR_PROPAGATE(sockets_close(verbose, socket_ids, sockets));
    292 
    293         if(verbose){
     404
     405        printf("sendto, recvfrom tested in %d microseconds\n",
     406            tv_sub(&time_after, &time_before));
     407
     408        rc = sockets_close(verbose, socket_ids, sockets);
     409        if (rc != EOK)
     410                return rc;
     411
     412        if (verbose)
    294413                printf("\nExiting\n");
    295         }
    296414
    297415        return EOK;
    298416}
    299417
    300 void nettest2_print_help(void){
    301         printf(
    302                 "Network Networking test 2 aplication - UDP transfer\n" \
    303                 "Usage: echo [options] numeric_address\n" \
    304                 "Where options are:\n" \
    305                 "-f protocol_family | --family=protocol_family\n" \
    306                 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
    307                 "\n" \
    308                 "-h | --help\n" \
    309                 "\tShow this application help.\n"
    310                 "\n" \
    311                 "-m count | --messages=count\n" \
    312                 "\tThe number of messages to send and receive per socket. The default is 10.\n" \
    313                 "\n" \
    314                 "-n sockets | --sockets=count\n" \
    315                 "\tThe number of sockets to use. The default is 10.\n" \
    316                 "\n" \
    317                 "-p port_number | --port=port_number\n" \
    318                 "\tThe port number the application should send messages to. The default is 7.\n" \
    319                 "\n" \
    320                 "-s packet_size | --size=packet_size\n" \
    321                 "\tThe packet data size the application sends. The default is 29 bytes.\n" \
    322                 "\n" \
    323                 "-v | --verbose\n" \
    324                 "\tShow all output messages.\n"
    325         );
    326 }
    327 
    328 void nettest2_refresh_data(char * data, size_t size){
    329         size_t length;
    330 
    331         // fill the data
    332         length = 0;
    333         while(size > length + sizeof(NETTEST2_TEXT) - 1){
    334                 memcpy(data + length, NETTEST2_TEXT, sizeof(NETTEST2_TEXT) - 1);
    335                 length += sizeof(NETTEST2_TEXT) - 1;
    336         }
    337         memcpy(data + length, NETTEST2_TEXT, size - length);
    338         data[size] = '\0';
    339 }
    340 
    341418/** @}
    342419 */
  • uspace/app/ping/Makefile

    r9a1d8ab r0b749a3  
    2929
    3030USPACE_PREFIX = ../..
    31 LIBS = $(LIBSOCKET_PREFIX)/libsocket.a
    32 EXTRA_CFLAGS = -I$(LIBSOCKET_PREFIX)/include
     31LIBS =
     32EXTRA_CFLAGS =
    3333BINARY = ping
    3434
  • uspace/app/ping/ping.c

    r9a1d8ab r0b749a3  
    4242#include <ipc/services.h>
    4343#include <str_error.h>
     44#include <errno.h>
    4445#include <arg_parse.h>
    4546
    46 #include <icmp_api.h>
    47 #include <in.h>
    48 #include <in6.h>
    49 #include <inet.h>
    50 #include <ip_codes.h>
    51 #include <socket_errno.h>
    52 #include <socket_parse.h>
     47#include <net/icmp_api.h>
     48#include <net/in.h>
     49#include <net/in6.h>
     50#include <net/inet.h>
     51#include <net/socket_parse.h>
     52#include <net/ip_codes.h>
    5353
    5454#include "print_error.h"
  • uspace/app/redir/redir.c

    r9a1d8ab r0b749a3  
    4343#include <task.h>
    4444#include <str_error.h>
     45#include <errno.h>
    4546
    4647#define NAME  "redir"
     
    7677static task_id_t spawn(int argc, char *argv[])
    7778{
    78         const char **args = (const char **) calloc(argc + 1, sizeof(char *));
     79        const char **args;
     80        task_id_t id;
     81        int rc;
     82
     83        args = (const char **) calloc(argc + 1, sizeof(char *));
    7984        if (!args) {
    8085                printf("No memory available\n");
     
    8893        args[argc] = NULL;
    8994       
    90         int err;
    91         task_id_t id = task_spawn(argv[0], args, &err);
     95        rc = task_spawnv(&id, argv[0], args);
    9296       
    9397        free(args);
    9498       
    95         if (id == 0)
     99        if (rc != EOK) {
    96100                printf("%s: Error spawning %s (%s)\n", NAME, argv[0],
    97                     str_error(err));
     101                    str_error(rc));
     102        }
    98103       
    99104        return id;
  • uspace/app/sbi/src/os/helenos.c

    r9a1d8ab r0b749a3  
    249249        task_id_t tid;
    250250        task_exit_t texit;
    251         int retval;
    252 
    253         tid = task_spawn(cmd[0], (char const * const *) cmd, &retval);
    254         if (tid == 0) {
     251        int rc, retval;
     252
     253        rc = task_spawnv(&tid, cmd[0], (char const * const *) cmd);
     254        if (rc != EOK) {
    255255                printf("Error: Failed spawning '%s' (%s).\n", cmd[0],
    256                     str_error(retval));
     256                    str_error(rc));
    257257                exit(1);
    258258        }
    259259
    260260        /* XXX Handle exit status and return value. */
    261         task_wait(tid, &texit, &retval);
     261        rc = task_wait(tid, &texit, &retval);
     262        (void) rc;
    262263
    263264        return EOK;
  • uspace/app/test_serial/Makefile

    r9a1d8ab r0b749a3  
    2929
    3030USPACE_PREFIX = ../..
    31 EXTRA_CFLAGS = -Iinclude
    32 LIBRARY = libsocket
     31BINARY = test_serial
    3332
    3433SOURCES = \
    35         generic/socket_client.c \
    36         generic/socket_core.c \
    37         generic/socket_parse.c \
    38         generic/inet.c \
    39         generic/net_modules.c \
    40         generic/icmp_common.c \
    41         generic/icmp_api.c \
    42         packet/packet.c \
    43         packet/packet_client.c \
    44         packet/packet_server.c \
    45         adt/dynamic_fifo.c \
    46         adt/measured_strings.c \
    47         adt/char_map.c
     34        test_serial.c
    4835
    4936include $(USPACE_PREFIX)/Makefile.common
  • uspace/app/trace/syscalls.c

    r9a1d8ab r0b749a3  
    7373    [SYS_PHYSMEM_MAP] = { "physmem_map",                4,      V_ERRNO },
    7474    [SYS_IOSPACE_ENABLE] = { "iospace_enable",          1,      V_ERRNO },
    75     [SYS_PREEMPT_CONTROL] = { "preempt_control",        1,      V_ERRNO },
     75    [SYS_INTERRUPT_ENABLE] = { "interrupt_enable",      2,      V_ERRNO },
    7676
    7777    [SYS_SYSINFO_GET_TAG] = { "sysinfo_get_tag",                2,      V_INTEGER },
Note: See TracChangeset for help on using the changeset viewer.