/*
* 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
* Echo application.
* Answers received packets.
*/
#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"
#include "../parse.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 );
/** 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" \
"-c count | --count=count\n" \
"\tThe number of received messages to handle. A negative number means infinity. The default is infinity.\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" \
"-p port_number | --port=port_number\n" \
"\tThe port number the application should listen at. The default is 7.\n" \
"\n" \
"-r reply_string | --reply=reply_string\n" \
"\tThe constant reply string. The default is the original data received.\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" \
"-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_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;
}
int main( int argc, char * argv[] ){
ERROR_DECLARE;
size_t size = 1024;
int verbose = 0;
char * reply = NULL;
sock_type_t type = SOCK_DGRAM;
int count = -1;
struct sockaddr_in address = { .sin_family = AF_INET, .sin_port = 7 };
int family = PF_INET;
int socket_id;
int address_length;
char address_string[ INET_ADDRSTRLEN ];
char * data;
size_t 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 ));
family = value;
switch( family ){
case PF_INET:
address.sin_family = AF_INET;
break;
default:
return ENOENT;
}
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 = ( uint16_t ) 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, & value, "receive size", 0 ));
size = (value >= 0 ) ? ( size_t ) value : 0;
break;
case 't': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket_type", 0, parse_socket_type ));
type = ( sock_type_t ) value;
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 ));
family = value;
switch( family ){
case PF_INET:
address.sin_family = AF_INET;
break;
default:
return ENOENT;
}
break;
}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 = ( uint16_t ) 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, & value, "receive size", 7 ));
size = (value >= 0 ) ? ( size_t ) value : 0;
}else if( str_lcmp( argv[ index ] + 2, "type=", 5 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "socket_type", 7, parse_socket_type ));
type = ( sock_type_t ) value;
}else if( str_lcmp( argv[ index ] + 2, "verbose", 8 ) == 0 ){
verbose = 1;
}else{
print_unrecognized( index, argv[ index ] + 2 );
print_help();
return EINVAL;
}
break;
default:
print_unrecognized( index, argv[ index ] + 1 );
print_help();
return EINVAL;
}
}else{
print_unrecognized( index, argv[ index ] );
print_help();
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( 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 );
value = recvfrom( socket_id, data, size, 0, ( struct sockaddr * ) & address, & address_length );
if( value < 0 ){
fprintf( stderr
, "Socket receive error %d\n", value
);
}else{
length = ( size_t ) value;
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 : 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;
}
/** @}
*/