Subversion Repositories HelenOS

Rev

Rev 4708 | Rev 4720 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Lukas Mejdrech
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup echo
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  Echo application.
  35.  *  Answers received packets.
  36.  */
  37.  
  38. #include <malloc.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <task.h>
  42.  
  43. #include "../../include/in.h"
  44. #include "../../include/inet.h"
  45. #include "../../include/socket.h"
  46.  
  47. #include "../../err.h"
  48.  
  49. #include "../parse.h"
  50.  
  51. /** Echo module name.
  52.  */
  53. #define NAME    "Echo"
  54.  
  55. /** Module entry point.
  56.  *  Reads command line parameters and starts listenning.
  57.  *  @param argc The number of command line parameters. Input parameter.
  58.  *  @param argv The command line parameters. Input parameter.
  59.  *  @returns EOK on success.
  60.  */
  61. int     main( int argc, char * argv[] );
  62.  
  63. /** Prints the application help.
  64.  */
  65. void    print_help( void );
  66.  
  67. /** Translates the character string to the protocol family number.
  68.  *  @param name The protocol family name. Input parameter.
  69.  *  @returns The corresponding protocol family number.
  70.  */
  71. int parse_protocol_family( const char * name );
  72.  
  73. /** Translates the character string to the socket type number.
  74.  *  @param name The socket type name. Input parameter.
  75.  *  @returns The corresponding socket type number.
  76.  */
  77. int parse_socket_type( const char * name );
  78.  
  79. void print_help( void ){
  80.     printf(
  81.         "Network Echo aplication\n" \
  82.         "Usage: echo [options]\n" \
  83.         "Where options are:\n" \
  84.         "-c count | --count=count\n" \
  85.         "\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \
  86.         "\n" \
  87.         "-f protocol_family | --family=protocol_family\n" \
  88.         "\tThe listenning socket protocol family. Only the PF_INET is supported.\n"
  89.         "\n" \
  90.         "-h | --help\n" \
  91.         "\tShow this application help.\n"
  92.         "\n" \
  93.         "-p port_number | --port=port_number\n" \
  94.         "\tThe port number the application should listen at. The default is 7.\n" \
  95.         "\n" \
  96.         "-r reply_string | --reply=reply_string\n" \
  97.         "\tThe constant reply string. The default is the original data received.\n" \
  98.         "\n" \
  99.         "-s receive_size | --size=receive_size\n" \
  100.         "\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \
  101.         "\n" \
  102.         "-t socket_type | --type=socket_type\n" \
  103.         "\tThe listenning socket type. Only the SOCK_DGRAM is supported.\n" \
  104.         "\n" \
  105.         "-v | --verbose\n" \
  106.         "\tShow all output messages.\n"
  107.     );
  108. }
  109.  
  110. int parse_protocol_family( const char * name ){
  111.     if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
  112.         return PF_INET;
  113.     }
  114.     return ENOENT;
  115. }
  116.  
  117. int parse_socket_type( const char * name ){
  118.     if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
  119.         return SOCK_DGRAM;
  120.     }
  121.     return ENOENT;
  122. }
  123.  
  124. int main( int argc, char * argv[] ){
  125.     ERROR_DECLARE;
  126.  
  127.     size_t              size            = 1024;
  128.     int                 verbose         = 0;
  129.     char *              reply           = NULL;
  130.     sock_type_t         type            = SOCK_DGRAM;
  131.     int                 count           = -1;
  132.     struct sockaddr_in  address         = { .sin_family = AF_INET, .sin_port = 7 };
  133.     int                 family          = PF_INET;
  134.  
  135.     int                 socket_id;
  136.     int                 address_length;
  137.     char                address_string[ INET_ADDRSTRLEN ];
  138.     char *              data;
  139.     size_t              length;
  140.     int                 index;
  141.     size_t              reply_length;
  142.     int                 value;
  143.  
  144.     printf( "Task %d - ", task_get_id());
  145.     printf( "%s\n", NAME );
  146.  
  147.     for( index = 1; index < argc; ++ index ){
  148.         if( argv[ index ][ 0 ] == '-' ){
  149.             switch( argv[ index ][ 1 ] ){
  150.                 case 'c':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 ));
  151.                             break;
  152.                 case 'f':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 0, parse_protocol_family ));
  153.                             family = value;
  154.                             switch( family ){
  155.                                 case PF_INET:
  156.                                     address.sin_family = AF_INET;
  157.                                     break;
  158.                                 default:
  159.                                     return ENOENT;
  160.                             }
  161.                             break;
  162.                 case 'h':   print_help();
  163.                             return EOK;
  164.                             break;
  165.                 case 'p':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
  166.                             address.sin_port = ( uint16_t ) value;
  167.                             break;
  168.                 case 'r':   ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 ));
  169.                             break;
  170.                 case 's':   ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 0 ));
  171.                             size = (value >= 0 ) ? ( size_t ) value : 0;
  172.                             break;
  173.                 case 't':   ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket_type", 0, parse_socket_type ));
  174.                             type = ( sock_type_t ) value;
  175.                             break;
  176.                 case 'v':   verbose = 1;
  177.                             break;
  178.                 case '-':   if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){
  179.                                 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 ))
  180.                             }else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
  181.                                 ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 9, parse_protocol_family ));
  182.                                 family = value;
  183.                                 switch( family ){
  184.                                     case PF_INET:
  185.                                         address.sin_family = AF_INET;
  186.                                         break;
  187.                                     default:
  188.                                         return ENOENT;
  189.                                 }
  190.                                 break;
  191.                             }else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
  192.                                 print_help();
  193.                                 return EOK;
  194.                             }else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
  195.                                 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
  196.                                 address.sin_port = ( uint16_t ) value;
  197.                             }else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){
  198.                                 ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 ));
  199.                             }else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){
  200.                                 ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "receive size", 7 ));
  201.                                 size = (value >= 0 ) ? ( size_t ) value : 0;
  202.                             }else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
  203.                                 ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket_type", 7, parse_socket_type ));
  204.                                 type = ( sock_type_t ) value;
  205.                             }else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
  206.                                 verbose = 1;
  207.                             }else{
  208.                                 print_unrecognized( index, argv[ index ] + 2 );
  209.                                 print_help();
  210.                                 return EINVAL;
  211.                             }
  212.                             break;
  213.                 default:
  214.                     print_unrecognized( index, argv[ index ] + 1 );
  215.                     print_help();
  216.                     return EINVAL;
  217.             }
  218.         }else{
  219.             print_unrecognized( index, argv[ index ] );
  220.             print_help();
  221.             return EINVAL;
  222.         }
  223.     }
  224.  
  225.     if( size <= 0 ){
  226.         fprintf( stderr, "Receive size too small (%d). Using 1024 bytes instead.\n", size );
  227.         size = 1024;
  228.     }
  229.     data = ( char * ) malloc( size + 1 );
  230.     if( ! data ){
  231.         fprintf( stderr, "Failed to allocate receive buffer.\n" );
  232.         return ENOMEM;
  233.     }
  234.  
  235.     reply_length = reply ? str_length( reply ) : 0;
  236.  
  237.     socket_id = socket( family, type, 0 );
  238.     if( socket_id < 0 ){
  239.         fprintf( stderr, "Socket create error %d\n", socket_id );
  240.         return socket_id;
  241.     }
  242.     if( ERROR_OCCURRED( bind( socket_id, ( struct sockaddr * ) & address, sizeof( address )))){
  243.         fprintf( stderr, "Socket bind error %d\n", ERROR_CODE );
  244.         return ERROR_CODE;
  245.     }
  246.  
  247.     if( verbose ) printf( "Listenning at %d\n", address.sin_port );
  248.  
  249.     while( count ){
  250.         address_length = sizeof( address );
  251.         value = recvfrom( socket_id, data, size, 0, ( struct sockaddr * ) & address, & address_length );
  252.         if( value < 0 ){
  253.             fprintf( stderr, "Socket receive error %d\n", value );
  254.         }else{
  255.             length = ( size_t ) value;
  256.             if( verbose ){
  257.                 if( ERROR_OCCURRED( inet_ntop( address.sin_family, ( uint8_t * ) & address.sin_addr.s_addr, address_string, sizeof( address_string )))){
  258.                     fprintf( stderr, "Received address error %d\n", ERROR_CODE );
  259.                     continue;
  260.                 }else{
  261.                     data[ length ] = '\0';
  262.                     printf( "Received from %s:%d\n%s\n", address_string, address.sin_port, data );
  263.                 }
  264.             }
  265.             if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, ( struct sockaddr * ) & address, sizeof( address )))){
  266.                 fprintf( stderr, "Socket send error %d\n", ERROR_CODE );
  267.             }
  268.         }
  269.         if( count > 0 ){
  270.             -- count;
  271.             if( verbose ) printf( "Waiting for next %d packet(s)\n", count );
  272.         }
  273.     }
  274.  
  275.     if( verbose ) printf( "Closing the socket\n" );
  276.  
  277.     if( ERROR_OCCURRED( closesocket( socket_id ))){
  278.         fprintf( stderr, "Close socket error %d\n", ERROR_CODE );
  279.         return ERROR_CODE;
  280.     }
  281.  
  282.     if( verbose ) printf( "Exiting\n" );
  283.  
  284.     return EOK;
  285. }
  286.  
  287. /** @}
  288.  */
  289.