/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup echo
* @{
*/
/** @file
*/
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <task.h>
#include "../../include/in.h"
#include "../../include/inet.h"
#include "../../include/socket.h"
#include "../../err.h"
/** Echo module name.
*/
#define NAME "Echo"
/** Module entry point.
* Reads command line parameters and starts listenning.
* @param argc The number of command line parameters. Input parameter.
* @param argv The command line parameters. Input parameter.
* @returns EOK on success.
*/
int main( int argc, char * argv[] );
/** Prints the application help.
*/
void print_help( void );
/** Prints the parameter unrecognized message and the application help.
* @param index The index of the parameter. Input parameter.
* @param parameter The parameter name. Input parameter.
*/
void print_unrecognized( int index, const char * parameter );
/** Parses the next parameter as an integral number.
* Uses the offseted actual parameter if the offset is set or the next one if not.
* @param argc The total number of the parameters. Input parameter.
* @param argv The parameters. Input parameter.
* @param index The actual parameter index. Input/output parameter.
* @param value The parsed parameter value. Output parameter.
* @param name The parameter name to be printed on errors. Input parameter.
* @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the parameter is missing.
* @returns EINVAL if the parameter is in wrong format.
*/
int parse_parameter_int( int argc, char ** argv, int * index, int * value, const char * name, int offset );
/** Parses the next parameter as a character string.
* Uses the offseted actual parameter if the offset is set or the next one if not.
* @param argc The total number of the parameters. Input parameter.
* @param argv The parameters. Input parameter.
* @param index The actual parameter index. Input/output parameter.
* @param value The parsed parameter value. Output parameter.
* @param name The parameter name to be printed on errors. Input parameter.
* @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the parameter is missing.
*/
int parse_parameter_string( int argc, char ** argv, int * index, char ** value, const char * name, int offset );
/** Parses the next named parameter as an integral number.
* Uses the offseted actual parameter if the offset is set or the next one if not.
* Translates the parameter using the parse_value function.
* @param argc The total number of the parameters. Input parameter.
* @param argv The parameters. Input parameter.
* @param index The actual parameter index. Input/output parameter.
* @param value The parsed parameter value. Output parameter.
* @param name The parameter name to be printed on errors. Input parameter.
* @param offset The value offset in the actual parameter. If not set, the next parameter is parsed instead. Input parameter.
* @param parse_value The translation function to parse the named value.
* @returns EOK on success.
* @returns EINVAL if the parameter is missing.
* @returns ENOENT if the parameter name has not been found.
*/
int parse_parameter_name_int( int argc, char ** argv, int * index, int * value, const char * name, int offset, int ( * parse_value )( const char * value ));
/** Translates the character string to the protocol family number.
* @param name The protocol family name. Input parameter.
* @returns The corresponding protocol family number.
*/
int parse_protocol_family( const char * name );
/** Translates the character string to the socket type number.
* @param name The socket type name. Input parameter.
* @returns The corresponding socket type number.
*/
int parse_socket_type( const char * name );
void print_help( void ){
"Network Echo aplication\n" \
"Usage: echo [options]\n" \
"Where options are:\n" \
"-p port_number | --port=port_number\n" \
"\tThe port number the application should listen at. The default is 7.\n" \
"\n" \
"-s receive_size | --size=receive_size\n" \
"\tThe maximum receive data size the application should accept. The default is 1024 bytes.\n" \
"\n" \
"-c count | --count\n" \
"\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\n" \
"\n" \
"-r reply_string | --reply=reply_string\n" \
"\tThe constant reply string. The default is the original data received.\n" \
"\n" \
"-f protocol_family | --family=protocol_family\n" \
"\tThe listenning socket protocol family. Only the PF_INET is supported.\n"
"\n" \
"-h | --help\n" \
"\tShow this application help.\n"
"\n" \
"-t socket_type | --type=socket_type\n" \
"\tThe listenning socket type. Only the SOCK_DGRAM is supported.\n" \
"\n" \
"-v | --verbose\n" \
"\tShow all output messages.\n"
);
}
int parse_parameter_int( int argc, char ** argv, int * index, int * value, const char * name, int offset ){
char * rest;
if( offset ){
* value
= strtol( argv
[ * index
] + offset
, & rest
, 10 );
}else if(( * index ) + 1 < argc ){
++ ( * index );
* value
= strtol( argv
[ * index
], & rest
, 10 );
}else{
fprintf( stderr
, "Command line error: missing %s\n", name
);
return EINVAL;
}
if( rest && ( * rest )){
fprintf( stderr
, "Command line error: %s unrecognized (%d: %s)\n", name
, * index
, argv
[ * index
] );
return EINVAL;
}
return EOK;
}
int parse_parameter_string( int argc, char ** argv, int * index, char ** value, const char * name, int offset ){
if( offset ){
* value = argv[ * index ] + offset;
}else if(( * index ) + 1 < argc ){
++ ( * index );
* value = argv[ * index ];
}else{
fprintf( stderr
, "Command line error: missing %s\n", name
);
return EINVAL;
}
return EOK;
}
int parse_parameter_name_int( int argc, char ** argv, int * index, int * value, const char * name, int offset, int ( * parse_value )( const char * value )){
ERROR_DECLARE;
char * parameter;
ERROR_PROPAGATE( parse_parameter_string( argc, argv, index, & parameter, name, offset ));
* value = ( * parse_value )( parameter );
if(( * value ) == ENOENT ){
fprintf( stderr
, "Command line error: unrecognized %s value (%d: %s)\n", name
, * index
, parameter
);
return ENOENT;
}
return EOK;
}
int parse_protocol_family( const char * name ){
if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
return PF_INET;
}
return ENOENT;
}
int parse_socket_type( const char * name ){
if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
return SOCK_DGRAM;
}
return ENOENT;
}
void print_unrecognized( int index, const char * parameter ){
fprintf( stderr
, "Command line error - unrecognized parameter (%d: %s)\n", index
, parameter
);
print_help();
}
int main( int argc, char * argv[] ){
ERROR_DECLARE;
int size = 1024;
int verbose = 0;
char * reply = NULL;
sock_type_t type = SOCK_DGRAM;
int count = -1;
struct sockaddr_in address = { .sin_family = PF_INET, .sin_port = 7 };
int socket_id;
int address_length;
char address_string[ INET_ADDRSTRLEN ];
char * data;
int length;
int index;
size_t reply_length;
int value;
printf( "Task %d - ", task_get_id
());
for( index = 1; index < argc; ++ index ){
if( argv[ index ][ 0 ] == '-' ){
switch( argv[ index ][ 1 ] ){
case 'c': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "count", 0 ));
break;
case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 0, parse_protocol_family ));
address.sin_family = ( uint16_t ) value;
break;
case 'h': print_help();
return EOK;
break;
case 'p': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 0 ));
address.sin_port = value;
break;
case 'r': ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 ));
break;
case 's': ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & size, "receive size", 0 ));
break;
case 't': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, ( int * ) & type, "socket_type", 0, parse_socket_type ));
break;
case 'v': verbose = 1;
break;
case '-': if( str_lcmp( argv[ index ] + 2, "count=", 6 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & count, "received count", 8 ))
}else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "protocol family", 9, parse_protocol_family ));
address.sin_family = value;
}else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
print_help();
return EOK;
}else if( str_lcmp( argv[ index ] + 2, "port=", 5 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & value, "port number", 7 ));
address.sin_port = value;
}else if( str_lcmp( argv[ index ] + 2, "reply=", 6 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 8 ));
}else if( str_lcmp( argv[ index ] + 2, "size=", 5 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_int( argc, argv, & index, & size, "receive size", 7 ));
}else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, ( int * ) & type, "socket_type", 7, parse_socket_type ));
}else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
verbose = 1;
}else{
print_unrecognized( index, argv[ index ] + 2 );
return EINVAL;
}
break;
default:
print_unrecognized( index, argv[ index ] + 1 );
return EINVAL;
}
}else{
print_unrecognized( index, argv[ index ] );
return EINVAL;
}
}
if( size <= 0 ){
fprintf( stderr
, "Receive size too small (%d). Using 1024 bytes instead.\n", size
);
size = 1024;
}
data
= ( char * ) malloc( size
+ 1 );
if( ! data ){
fprintf( stderr
, "Failed to allocate receive buffer.\n" );
return ENOMEM;
}
reply_length = reply ? str_length( reply ) : 0;
socket_id = socket( address.sin_family, type, 0 );
if( socket_id < 0 ){
fprintf( stderr
, "Socket create error %d\n", socket_id
);
return socket_id;
}
if( ERROR_OCCURRED( bind( socket_id, ( struct sockaddr * ) & address, sizeof( address )))){
fprintf( stderr
, "Socket bind error %d\n", ERROR_CODE
);
return ERROR_CODE;
}
if( verbose
) printf( "Listenning at %d\n", address.
sin_port );
while( count ){
address_length = sizeof( address );
length = recvfrom( socket_id, data, size, 0, ( struct sockaddr * ) & address, & address_length );
if( length < 0 ){
fprintf( stderr
, "Socket receive error %d\n", length
);
}else{
if( verbose ){
if( ERROR_OCCURRED( inet_ntop( address.sin_family, ( uint8_t * ) & address.sin_addr.s_addr, address_string, sizeof( address_string )))){
fprintf( stderr
, "Received address error %d\n", ERROR_CODE
);
continue;
}else{
data[ length ] = '\0';
printf( "Received from %s:%d\n%s\n", address_string
, address.
sin_port, data
);
}
}
if( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : ( size_t ) length, 0, ( struct sockaddr * ) & address, sizeof( address )))){
fprintf( stderr
, "Socket send error %d\n", ERROR_CODE
);
}
}
if( count > 0 ){
-- count;
if( verbose
) printf( "Waiting for next %d packet(s)\n", count
);
}
}
if( verbose
) printf( "Closing the socket\n" );
if( ERROR_OCCURRED( closesocket( socket_id ))){
fprintf( stderr
, "Close socket error %d\n", ERROR_CODE
);
return ERROR_CODE;
}
if( verbose
) printf( "Exiting\n" );
return EOK;
}
/** @}
*/