46,6 → 46,8 |
|
#include "../../err.h" |
|
#include "../parse.h" |
|
/** Echo module name. |
*/ |
#define NAME "Echo" |
58,69 → 60,10 |
*/ |
int main( int argc, char * argv[] ); |
|
/** @name Output printing functions |
*/ |
/*@{*/ |
|
/** 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 ); |
|
/*@}*/ |
|
/** @name Command line argumets parsing functions |
*/ |
/*@{*/ |
|
/** 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. |
133,25 → 76,14 |
*/ |
int parse_socket_type( const char * name ); |
|
/*@}*/ |
|
void print_help( void ){ |
printf( |
"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" \ |
"-c count | --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" \ |
158,6 → 90,15 |
"-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" \ |
166,52 → 107,6 |
); |
} |
|
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; |
226,11 → 121,6 |
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; |
|
239,7 → 129,8 |
char * reply = NULL; |
sock_type_t type = SOCK_DGRAM; |
int count = -1; |
struct sockaddr_in address = { .sin_family = PF_INET, .sin_port = 7 }; |
struct sockaddr_in address = { .sin_family = AF_INET, .sin_port = 7 }; |
int family = PF_INET; |
|
int socket_id; |
int address_length; |
259,7 → 150,14 |
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; |
family = value; |
switch( family ){ |
case PF_INET: |
address.sin_family = AF_INET; |
break; |
default: |
return ENOENT; |
} |
break; |
case 'h': print_help(); |
return EOK; |
281,7 → 179,15 |
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 = ( uint16_t ) value; |
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; |
300,15 → 206,18 |
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; |
} |
} |
325,7 → 234,7 |
|
reply_length = reply ? str_length( reply ) : 0; |
|
socket_id = socket( address.sin_family, type, 0 ); |
socket_id = socket( family, type, 0 ); |
if( socket_id < 0 ){ |
fprintf( stderr, "Socket create error %d\n", socket_id ); |
return socket_id; |