Subversion Repositories HelenOS

Compare Revisions

No changes between revisions

Ignore whitespace Rev 4602 → Rev 4603

/branches/network/contrib/conf/qemu.sh
1,4 → 1,4
#!/bin/sh
 
#qemu 0.10.2 only (NOT qemu 0.9.1!)
qemu $@ -vga std -no-kqemu -M isapc -net nic,model=ne2k_isa -net user -boot d -cdrom image.iso -redir tcp:8080:10.0.2.15:8080
qemu $@ -vga std -no-kqemu -M isapc -net nic,model=ne2k_isa -net user -boot d -cdrom image.iso -redir udp:8080::8080 -redir udp:8081::8081
/branches/network/uspace/srv/net/tl/udp/udp.c
255,7 → 255,8
res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ));
if( res > 0 ){
* SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
* answer_count = 1;
* SOCKET_SET_ADDRESS_LENGTH( answer ) = sizeof( struct sockaddr_in );
* answer_count = 2;
res = EOK;
}
break;
388,7 → 389,7
header = ( udp_header_ref ) data;
// set the source address
address.sin_family = PF_INET;
address.sin_port = ntohs( header->dest );
address.sin_port = ntohs( header->source );
length = packet_get_addr( packet, & addr, NULL );
if( length != sizeof( address.sin_addr.s_addr )){
pq_release( udp_globals.net_phone, packet_id );
/branches/network/uspace/srv/net/app/echo/echo.c
0,0 → 1,357
/*
* 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 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 The socket type name. Input parameter.
* @returns The corresponding socket type number.
*/
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" \
"\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());
printf( "%s\n", NAME );
 
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( "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;
}
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/app/echo/Makefile
0,0 → 1,45
#
# 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.
#
 
NAME = echo
 
NET_BASE = ../../
STRUCTURES = $(NET_BASE)structures/
 
include ../../../../../Makefile.config
 
## Sources
#
 
OUTPUT = $(NAME)
SOURCES = \
$(NAME).c
 
LIBS += ../../socket/libsocket.a
 
include $(NET_BASE)Makefile.module
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/include/socket.h
41,6 → 41,7
 
#include <sys/types.h>
 
#include "byteorder.h"
#include "in.h"
#include "inet.h"
 
/branches/network/uspace/srv/net/net/start/netstart.c
80,7 → 80,7
ERROR_PROPAGATE( self_test());
// start net service
if( ! spawn( "/srv/net" )){
printf( "Could not spawn net\n" );
fprintf( stderr, "Could not spawn net\n" );
return EINVAL;
}
// start net
/branches/network/uspace/srv/net/socket/socket_core.c
143,11 → 143,11
if( ! socket ) return ENOTSOCK;
socket_ports_exclude( global_sockets, socket->port );
// destroy all accepted sockets
while(( accepted_id = dyn_fifo_pop( & socket->accepted ))){
while(( accepted_id = dyn_fifo_pop( & socket->accepted )) >= 0 ){
socket_destroy( packet_phone, accepted_id, local_sockets, global_sockets );
}
// release all received packets
while(( packet_id = dyn_fifo_pop( & socket->received ))){
while(( packet_id = dyn_fifo_pop( & socket->received )) >= 0 ){
pq_release( packet_phone, packet_id );
}
dyn_fifo_destroy( & socket->received );
/branches/network/uspace/srv/net/socket/socket_messages.h
69,6 → 69,8
#define SOCKET_GET_OPT_LEVEL( call ) ( int ) IPC_GET_ARG2( * call )
#define SOCKET_SET_DATA_FRAGMENTS( call ) ( int * ) & IPC_GET_ARG2( * call )
#define SOCKET_GET_DATA_FRAGMENTS( call ) ( int ) IPC_GET_ARG2( * call )
#define SOCKET_SET_ADDRESS_LENGTH( call ) ( size_t * ) & IPC_GET_ARG2( * call )
#define SOCKET_GET_ADDRESS_LENGTH( call ) ( size_t ) IPC_GET_ARG2( * call )
 
#define SOCKET_SET_DATA_FRAGMENT_SIZE( call ) ( int * ) & IPC_GET_ARG3( * call )
#define SOCKET_GET_DATA_FRAGMENT_SIZE( call ) ( int ) IPC_GET_ARG3( * call )
/branches/network/uspace/srv/net/socket/socket_client.c
454,6 → 454,7
 
if( ! data ) return EBADMEM;
if( ! datalength ) return NO_DATA;
if( fromaddr && (( ! addrlen ) || ( * addrlen < sizeof( struct sockaddr_in )))) return EINVAL;
// find the socket
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
473,9 → 474,6
// request packet data
message_id = async_send_4( socket->phone, message, socket->socket_id, 0, socket->service, flags, & answer );
// read the address if desired
if( fromaddr ){
* addrlen = sizeof( struct sockaddr_in );
}
if(( ! fromaddr ) || ( ipc_data_read_start( socket->phone, fromaddr, * addrlen ) == EOK )){
if( fragments == 1 ){
// read all if only one fragment
503,6 → 501,8
dyn_fifo_pop( & socket->received );
// return read data length
result = SOCKET_GET_READ_DATA_LENGTH( & answer );
// set address length
if( fromaddr && addrlen ) * addrlen = SOCKET_GET_ADDRESS_LENGTH( & answer );
}
fibril_mutex_unlock( & socket->receive_lock );
return result;
/branches/network/uspace/srv/net/socket/Makefile
37,6 → 37,7
LIB = lib$(NAME).a
SOURCES = \
$(NAME)_client.c \
$(NET_BASE)modules.c \
$(NET_BASE)inet.c \
$(STRUCTURES)dynamic_fifo.c
 
/branches/network/uspace/srv/net/Makefile
34,7 → 34,8
nil/eth \
net \
net/start \
socket
socket \
app/echo
 
DIRS_MODULAR = il/ip \
il/arp \
/branches/network/boot/arch/ia32/Makefile.inc
48,6 → 48,7
$(USPACEDIR)/srv/net/netif/lo/lo \
$(USPACEDIR)/srv/net/netif/dp8390/dp8390 \
$(USPACEDIR)/srv/net/nil/eth/eth \
$(USPACEDIR)/srv/net/app/echo/echo \
$(USPACEDIR)/srv/pci/pci \
$(USPACEDIR)/srv/fb/fb \
$(USPACEDIR)/srv/kbd/kbd \