Subversion Repositories HelenOS

Compare Revisions

No changes between revisions

Ignore whitespace Rev 4719 → Rev 4720

/branches/network/uspace/srv/net/app/print_error.c
0,0 → 1,143
/*
* 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 net_app
* @{
*/
 
/** @file
* Generic application error printing functions implementation.
*/
 
#include <stdio.h>
 
#include "../include/icmp_codes.h"
#include "../include/socket_errno.h"
 
#include "print_error.h"
 
void print_error( FILE * output, int error_code, const char * prefix, const char * suffix ){
if( IS_ICMP_ERROR( error_code )){
icmp_print_error( output, error_code, prefix, suffix );
}else if( IS_SOCKET_ERROR( error_code )){
socket_print_error( output, error_code, prefix, suffix );
}
}
 
void icmp_print_error( FILE * output, int error_code, const char * prefix, const char * suffix ){
if( output ){
if( prefix ){
fprintf( output, "%s", prefix );
}
switch( error_code ){
case ICMP_DEST_UNREACH:
fprintf( output, "ICMP Destination Unreachable (%d) error", error_code );
break;
case ICMP_SOURCE_QUENCH:
fprintf( output, "ICMP Source Quench (%d) error", error_code );
break;
case ICMP_REDIRECT:
fprintf( output, "ICMP Redirect (%d) error", error_code );
break;
case ICMP_ALTERNATE_ADDR:
fprintf( output, "ICMP Alternate Host Address (%d) error", error_code );
break;
case ICMP_ROUTER_ADV:
fprintf( output, "ICMP Router Advertisement (%d) error", error_code );
break;
case ICMP_ROUTER_SOL:
fprintf( output, "ICMP Router Solicitation (%d) error", error_code );
break;
case ICMP_TIME_EXCEEDED:
fprintf( output, "ICMP Time Exceeded (%d) error", error_code );
break;
case ICMP_PARAMETERPROB:
fprintf( output, "ICMP Paramenter Problem (%d) error", error_code );
break;
case ICMP_CONVERSION_ERROR:
fprintf( output, "ICMP Datagram Conversion Error (%d) error", error_code );
break;
case ICMP_REDIRECT_MOBILE:
fprintf( output, "ICMP Mobile Host Redirect (%d) error", error_code );
break;
case ICMP_SKIP:
fprintf( output, "ICMP SKIP (%d) error", error_code );
break;
case ICMP_PHOTURIS:
fprintf( output, "ICMP Photuris (%d) error", error_code );
break;
default:
fprintf( output, "Other (%d) error", error_code );
}
if( suffix ){
fprintf( output, "%s", suffix );
}
}
}
 
void socket_print_error( FILE * output, int error_code, const char * prefix, const char * suffix ){
if( output ){
if( prefix ){
fprintf( output, "%s", prefix );
}
switch( error_code ){
case ENOTSOCK:
fprintf( output, "Not a socket (%d) error", error_code );
break;
case EPROTONOSUPPORT:
fprintf( output, "Protocol not supported (%d) error", error_code );
break;
case ESOCKTNOSUPPORT:
fprintf( output, "Socket type not supported (%d) error", error_code );
break;
case EPFNOSUPPORT:
fprintf( output, "Protocol family not supported (%d) error", error_code );
break;
case EAFNOSUPPORT:
fprintf( output, "Address family not supported (%d) error", error_code );
break;
case EADDRINUSE:
fprintf( output, "Address already in use (%d) error", error_code );
break;
case ENOTCONN:
fprintf( output, "Socket not connected (%d) error", error_code );
break;
case NO_DATA:
fprintf( output, "No data (%d) error", error_code );
break;
default:
fprintf( output, "Other (%d) error", error_code );
}
if( suffix ){
fprintf( output, "%s", suffix );
}
}
}
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/app/print_error.h
0,0 → 1,80
/*
* 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 net_app
* @{
*/
 
/** @file
* Generic application error printing functions.
*/
 
#ifndef __NET_APP_PRINT__
#define __NET_APP_PRINT__
 
/** Returns whether the error code may be an ICMP error code.
* @param error_code The error code. Input parameter.
* @returns A value indicating whether the error code may be an ICMP error code.
*/
#define IS_ICMP_ERROR( error_code ) (( error_code ) > 0 )
 
/** Returns whether the error code may be socket error code.
* @param error_code The error code. Input parameter.
* @returns A value indicating whether the error code may be a socket error code.
*/
#define IS_SOCKET_ERROR( error_code ) (( error_code ) < 0 )
 
/** Prints the error description.
* Supports ICMP and socket error codes.
* @param output The description output stream. May be NULL. Input parameter.
* @param error_code The error code. Input parameter.
* @param prefix The error description prefix. May be NULL. Input parameter.
* @param suffix The error description suffix. May be NULL. Input parameter.
*/
void print_error( FILE * output, int error_code, const char * prefix, const char * suffix );
 
/** Prints the specific ICMP error description.
* @param output The description output stream. May be NULL. Input parameter.
* @param error_code The ICMP error code. Input parameter.
* @param prefix The error description prefix. May be NULL. Input parameter.
* @param suffix The error description suffix. May be NULL. Input parameter.
*/
void icmp_print_error( FILE * output, int error_code, const char * prefix, const char * suffix );
 
/** Prints the specific socket error description.
* @param output The description output stream. May be NULL. Input parameter.
* @param error_code The socket error code. Input parameter.
* @param prefix The error description prefix. May be NULL. Input parameter.
* @param suffix The error description suffix. May be NULL. Input parameter.
*/
void socket_print_error( FILE * output, int error_code, const char * prefix, const char * suffix );
 
#endif
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/app/ping/Makefile
40,6 → 40,7
SOURCES = \
$(NAME).c \
$(NET_BASE)app/parse.c \
$(NET_BASE)app/print_error.c \
$(NET_BASE)inet.c \
$(NET_BASE)modules.c \
$(NET_BASE)tl/icmp/icmp_api.c \
/branches/network/uspace/srv/net/app/ping/ping.c
42,13 → 42,15
 
#include "../../include/icmp_api.h"
#include "../../include/in.h"
#include "../../include/in6.h"
#include "../../include/inet.h"
#include "../../include/ip_codes.h"
#include "../../include/socket_codes.h"
#include "../../include/socket_errno.h"
 
#include "../../err.h"
 
#include "../parse.h"
#include "../print_error.h"
 
/** Echo module name.
*/
69,8 → 71,9
/** Translates the character string to the address family number.
* @param name The address family name. Input parameter.
* @returns The corresponding address family number.
* @returns EAFNOSUPPORTED if the address family is not supported.
*/
int parse_address_family( const char * name );
int parse_address_family( const char * name );
 
void print_help( void ){
printf(
85,7 → 88,7
"\tDisable packet fragmentation.\n"
"\n" \
"-f address_family | --family=address_family\n" \
"\tThe given address family. Only the AF_INET is supported.\n"
"\tThe given address family. Only the AF_INET and AF_INET6 are supported.\n"
"\n" \
"-h | --help\n" \
"\tShow this application help.\n"
110,8 → 113,10
int parse_address_family( const char * name ){
if( str_lcmp( name, "AF_INET", 7 ) == 0 ){
return AF_INET;
}else if( str_lcmp( name, "AF_INET6", 8 ) == 0 ){
return AF_INET6;
}
return ENOENT;
return EAFNOSUPPORT;
}
 
int main( int argc, char * argv[] ){
124,8 → 129,16
ip_tos_t tos = 0;
int count = 3;
suseconds_t timeout = 3000;
struct sockaddr_in address = { .sin_family = AF_INET, .sin_port = 7 };
int family = AF_INET;
 
socklen_t max_length = sizeof( struct sockaddr_in6 );
uint8_t address_data[ max_length ];
struct sockaddr * address = ( struct sockaddr * ) address_data;
struct sockaddr_in * address_in = ( struct sockaddr_in * ) address;
struct sockaddr_in6 * address_in6 = ( struct sockaddr_in6 * ) address;
socklen_t addrlen;
char address_string[ INET6_ADDRSTRLEN ];
uint8_t * address_start;
int icmp_phone;
struct timeval time_before;
struct timeval time_after;
141,8 → 154,7
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, "address family", 0, parse_address_family ));
address.sin_family = ( uint16_t ) value;
case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "address family", 0, parse_address_family ));
break;
case 'h': print_help();
return EOK;
160,8 → 172,7
}else if( str_lcmp( argv[ index ] + 2, "dont_fragment", 13 ) == 0 ){
dont_fragment = 1;
}else if( str_lcmp( argv[ index ] + 2, "family=", 7 ) == 0 ){
ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & value, "address family", 9, parse_address_family ));
address.sin_family = ( uint16_t ) value;
ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "address family", 9, parse_address_family ));
}else if( str_lcmp( argv[ index ] + 2, "help", 5 ) == 0 ){
print_help();
return EOK;
197,7 → 208,24
}
}
 
if( ERROR_OCCURRED( inet_pton( address.sin_family, argv[ argc - 1 ], ( uint8_t * ) & address.sin_addr ))){
bzero( address_data, max_length );
switch( family ){
case AF_INET:
address_in->sin_family = AF_INET;
address_start = ( uint8_t * ) & address_in->sin_addr.s_addr;
addrlen = sizeof( struct sockaddr_in );
break;
case AF_INET6:
address_in6->sin6_family = AF_INET6;
address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr;
addrlen = sizeof( struct sockaddr_in6 );
break;
default:
fprintf( stderr, "Protocol family is not supported\n" );
return EAFNOSUPPORT;
}
 
if( ERROR_OCCURRED( inet_pton( family, argv[ argc - 1 ], address_start ))){
fprintf( stderr, "Address parse error %d\n", ERROR_CODE );
return ERROR_CODE;
}
208,6 → 236,11
}
 
printf( "PING %d bytes of data\n", size );
if( ERROR_OCCURRED( inet_ntop( address->sa_family, address_start, address_string, sizeof( address_string )))){
fprintf( stderr, "Address error %d\n", ERROR_CODE );
}else{
printf( "Address %s:\n", address_string );
}
 
while( count > 0 ){
if( ERROR_OCCURRED( gettimeofday( & time_before, NULL ))){
214,7 → 247,7
fprintf( stderr, "Get time of day error %d\n", ERROR_CODE );
return ERROR_CODE;
}
result = icmp_echo_msg( icmp_phone, size, timeout, ttl, tos, dont_fragment, ( struct sockaddr * ) & address, sizeof( address ));
result = icmp_echo_msg( icmp_phone, size, timeout, ttl, tos, dont_fragment, address, addrlen );
if( ERROR_OCCURRED( gettimeofday( & time_after, NULL ))){
fprintf( stderr, "Get time of day error %d\n", ERROR_CODE );
return ERROR_CODE;
223,11 → 256,11
case ICMP_ECHO:
printf( "Ping round trip time %d microseconds\n", tv_sub( & time_after, & time_before ));
break;
case ELIMIT:
case ETIMEOUT:
printf( "Timeouted.\n" );
break;
default:
printf( "Error %d.\n", result );
print_error( stdout, result, NULL, "\n" );
}
-- count;
}
/branches/network/uspace/srv/net/app/echo/echo.c
41,6 → 41,7
#include <task.h>
 
#include "../../include/in.h"
#include "../../include/in6.h"
#include "../../include/inet.h"
#include "../../include/socket.h"
 
47,6 → 48,7
#include "../../err.h"
 
#include "../parse.h"
#include "../print_error.h"
 
/** Echo module name.
*/
67,14 → 69,16
/** Translates the character string to the protocol family number.
* @param name The protocol family name. Input parameter.
* @returns The corresponding protocol family number.
* @returns EPFNOSUPPORTED if the protocol family is not supported.
*/
int parse_protocol_family( const char * name );
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.
* @returns ESOCKNOSUPPORTED if the socket type is not supported.
*/
int parse_socket_type( const char * name );
int parse_socket_type( const char * name );
 
void print_help( void ){
printf(
85,7 → 89,7
"\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"
"\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
"\n" \
"-h | --help\n" \
"\tShow this application help.\n"
110,8 → 114,10
int parse_protocol_family( const char * name ){
if( str_lcmp( name, "PF_INET", 7 ) == 0 ){
return PF_INET;
}else if( str_lcmp( name, "PF_INET6", 8 ) == 0 ){
return PF_INET6;
}
return ENOENT;
return EPFNOSUPPORT;
}
 
int parse_socket_type( const char * name ){
118,7 → 124,7
if( str_lcmp( name, "SOCK_DGRAM", 11 ) == 0 ){
return SOCK_DGRAM;
}
return ENOENT;
return ESOCKTNOSUPPORT;
}
 
int main( int argc, char * argv[] ){
129,12 → 135,18
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;
uint16_t port = 7;
 
socklen_t max_length = sizeof( struct sockaddr_in6 );
uint8_t address_data[ max_length ];
struct sockaddr * address = ( struct sockaddr * ) address_data;
struct sockaddr_in * address_in = ( struct sockaddr_in * ) address;
struct sockaddr_in6 * address_in6 = ( struct sockaddr_in6 * ) address;
socklen_t addrlen;
char address_string[ INET6_ADDRSTRLEN ];
uint8_t * address_start;
int socket_id;
int address_length;
char address_string[ INET_ADDRSTRLEN ];
char * data;
size_t length;
int index;
149,21 → 161,13
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;
}
case 'f': ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 0, parse_protocol_family ));
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;
port = ( uint16_t ) value;
break;
case 'r': ERROR_PROPAGATE( parse_parameter_string( argc, argv, & index, & reply, "reply string", 0 ));
break;
178,22 → 182,13
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;
ERROR_PROPAGATE( parse_parameter_name_int( argc, argv, & index, & family, "protocol family", 9, parse_protocol_family ));
}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;
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 ){
239,31 → 234,62
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 );
 
bzero( address_data, max_length );
switch( family ){
case PF_INET:
address_in->sin_family = AF_INET;
address_in->sin_port = port;
addrlen = sizeof( struct sockaddr_in );
break;
case PF_INET6:
address_in6->sin6_family = AF_INET6;
address_in6->sin6_port = port;
addrlen = sizeof( struct sockaddr_in6 );
break;
default:
fprintf( stderr, "Protocol family is not supported\n" );
return EAFNOSUPPORT;
}
if( ERROR_OCCURRED( bind( socket_id, address, addrlen ))){
socket_print_error( stderr, ERROR_CODE, "Socket bind: ", "\n" );
return ERROR_CODE;
}
 
if( verbose ) printf( "Listenning at %d\n", address.sin_port );
if( verbose ) printf( "Listenning at %d\n", port );
 
while( count ){
address_length = sizeof( address );
value = recvfrom( socket_id, data, size, 0, ( struct sockaddr * ) & address, & address_length );
addrlen = max_length;
value = recvfrom( socket_id, data, size, 0, address, & addrlen );
if( value < 0 ){
fprintf( stderr, "Socket receive error %d\n", value );
socket_print_error( stderr, value, "Socket receive: ", "\n" );
}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 );
address_start = NULL;
switch( address->sa_family ){
case AF_INET:
port = address_in->sin_port;
address_start = ( uint8_t * ) & address_in->sin_addr.s_addr;
break;
case AF_INET6:
port = address_in6->sin6_port;
address_start = ( uint8_t * ) & address_in6->sin6_addr.s6_addr;
break;
default:
fprintf( stderr, "Address family %d (0x%X) is not supported.\n", address->sa_family );
}
if( address_start ){
if( ERROR_OCCURRED( inet_ntop( address->sa_family, address_start, address_string, sizeof( address_string )))){
fprintf( stderr, "Received address error %d\n", ERROR_CODE );
}else{
data[ length ] = '\0';
printf( "Received from %s:%d\n%s\n", address_string, 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( ERROR_OCCURRED( sendto( socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen ))){
socket_print_error( stderr, ERROR_CODE, "Socket send: ", "\n" );
}
}
if( count > 0 ){
275,7 → 301,7
if( verbose ) printf( "Closing the socket\n" );
 
if( ERROR_OCCURRED( closesocket( socket_id ))){
fprintf( stderr, "Close socket error %d\n", ERROR_CODE );
socket_print_error( stderr, ERROR_CODE, "Close socket: ", "\n" );
return ERROR_CODE;
}
 
/branches/network/uspace/srv/net/app/echo/Makefile
39,7 → 39,8
OUTPUT = $(NAME)
SOURCES = \
$(NAME).c \
$(NET_BASE)app/parse.c
$(NET_BASE)app/parse.c \
$(NET_BASE)app/print_error.c
 
LIBS += ../../socket/libsocket.a