Subversion Repositories HelenOS

Compare Revisions

No changes between revisions

Ignore whitespace Rev 4578 → Rev 4579

/branches/network/uspace/srv/net/tl/udp/udp_header.h
0,0 → 1,79
/*
* 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 udp
* @{
*/
 
/** @file
* User datagram protocol header and options definitions.
* Names according to the linux src/include/linux/udp.h header file.
*/
 
#ifndef __NET_UDP_HEADER_H__
#define __NET_UDP_HEADER_H__
 
#include <sys/types.h>
 
/** Type definition of the user datagram header.
* @see udp_header
*/
typedef struct udp_header udp_header_t;
 
/** Type definition of the user datagram header pointer.
* @see udp_header
*/
typedef udp_header_t * udp_header_ref;
 
/** User datagram header.
*/
struct udp_header{
/** Source Port is an optional field, when meaningful, it indicates the port of the sending process, and may be assumed to be the port to which a reply should be addressed in the absence of any other information.
* If not used, a value of zero is inserted.
*/
uint16_t source;
/** Destination port has a meaning within the context of a particular internet destination address.
*/
uint16_t dest;
/** Length is the length in octets of this user datagram including this header and the data.
* This means the minimum value of the length is eight.
*/
uint16_t len;
/** Checksum is the 16-bit one's complement of the one's complement sum of a pseudo header of information from the IP header, the UDP header, and the data, padded with zero octets at the end (if necessary) to make a multiple of two octets.
* The pseudo header conceptually prefixed to the UDP header contains the source address, the destination address, the protocol, and the UDP length.
* This information gives protection against misrouted datagrams.
* If the computed checksum is zero, it is transmitted as all ones (the equivalent in one's complement arithmetic).
* An all zero transmitted checksum value means that the transmitter generated no checksum (for debugging or for higher level protocols that don't care).
*/
uint16_t check;
} __attribute__ ((packed));
 
#endif
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/tl/udp/udp.c
34,6 → 34,7
*/
 
#include <async.h>
#include <malloc.h>
#include <stdio.h>
 
#include <ipc/ipc.h>
44,17 → 45,28
#include "../../modules.h"
#include "../../structures/packet/packet_client.h"
 
#include "../../include/in.h"
#include "../../include/inet.h"
#include "../../include/ip_client.h"
#include "../../include/ip_interface.h"
#include "../../include/ip_protocols.h"
#include "../../include/socket.h"
#include "../../include/socket_errno.h"
//#include "../../include/udp_interface.h"
 
#include "../../socket/socket_core.h"
#include "../../socket/socket_messages.h"
 
#include "../tl_messages.h"
 
#include "udp.h"
#include "udp_header.h"
#include "udp_module.h"
 
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver );
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, void * data, size_t length, int flags );
int socket_get_data( void ** data, size_t * length );
 
udp_globals_t udp_globals;
 
61,25 → 73,18
/** Initializes the module.
*/
int udp_initialize( async_client_conn_t client_connection ){
// ERROR_DECLARE;
ERROR_DECLARE;
 
// ipcarg_t arg1, arg2;
// packet_t packet;
 
/* printf( "UDP - testing to send to IP:\n" );
ERROR_PROPAGATE( ip_echo( udp_globals.ip_phone, 12, 34, 0, 0, 0, & arg1, & arg2, NULL, NULL, NULL ));
if(( arg1 != 12 ) || ( arg2 != 34 )) return EINVAL;
printf( "OK\n" );
*/
udp_globals.port_search_start = 1025;
udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
 
/* printf( "UDP - testing to send packet to IP:\n" );
packet = packet_get_4( udp_globals.net_phone, 6, 20, 30, 20 );
if( ! packet ) return ENOMEM;
packet_copy_data( packet, "Hi, this is UDP", 16 );
ip_send_msg( udp_globals.ip_phone, -1, packet, SERVICE_UDP );
printf( "OK\n" );
*/ return EOK;
if( udp_globals.ip_phone < 0 ){
return udp_globals.ip_phone;
}
ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
ERROR_PROPAGATE( socket_ports_initialize( & udp_globals.sockets ));
udp_globals.prefix += sizeof( udp_header_t );
udp_globals.content -= sizeof( udp_header_t );
return EOK;
}
 
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver ){
100,14 → 105,192
 
* answer_count = 0;
switch( IPC_GET_METHOD( * call )){
case IPC_M_PHONE_HUNGUP:
return EOK;
case NET_TL_RECEIVED:
ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, IPC_GET_PACKET( call )));
return udp_received_msg( IPC_GET_DEVICE( call ), packet, 0 );
case IPC_M_CONNECT_TO_ME:
return process_client_messages( callid, call, answer, answer_count );
}
return ENOTSUP;
}
 
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
ERROR_DECLARE;
 
int res;
bool keep_on_going = true;
socket_cores_t local_sockets;
int app_phone = IPC_GET_PHONE( call );
void * addr;
size_t addrlen;
void * data;
size_t length;
 
/*
* Accept the connection
* - Answer the first IPC_M_CONNECT_ME_TO call.
*/
ipc_answer_0( callid, EOK );
 
socket_cores_initialize( & local_sockets );
 
while( keep_on_going ){
// refresh data
* answer_count = 0;
IPC_SET_RETVAL( * answer, 0 );
// just to be precize
IPC_SET_METHOD( * answer, 0 );
IPC_SET_ARG1( * answer, 0 );
IPC_SET_ARG2( * answer, 0 );
IPC_SET_ARG3( * answer, 0 );
IPC_SET_ARG4( * answer, 0 );
IPC_SET_ARG5( * answer, 0 );
 
callid = async_get_call( call );
printf( "message %d\n", IPC_GET_METHOD( * call ));
 
switch( IPC_GET_METHOD( * call )){
case IPC_M_PHONE_HUNGUP:
keep_on_going = false;
res = EOK;
break;
case NET_SOCKET:
res = socket_create( & local_sockets, app_phone );
break;
case NET_SOCKET_BIND:
if( ERROR_OCCURRED( socket_get_data( & addr, & addrlen ))){
res = ERROR_CODE;
break;
}
res = socket_bind( & local_sockets, & udp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen );
free( addr );
break;
case NET_SOCKET_SENDTO:
if( ERROR_OCCURRED( socket_get_data( & addr, & addrlen ))){
res = ERROR_CODE;
break;
}
if( ERROR_OCCURRED( socket_get_data( & data, & length ))){
free( addr );
res = ERROR_CODE;
break;
}
res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, data, length, SOCKET_GET_FLAGS( call ));
free( addr );
free( data );
break;
case NET_SOCKET_RECVFROM:
// TODO read first received packet queue data continuesly
res = ENOTSUP;
break;
case NET_SOCKET_CLOSE:
res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets );
break;
case NET_SOCKET_GETSOCKOPT:
case NET_SOCKET_SETSOCKOPT:
default:
res = ENOTSUP;
break;
}
 
// TODO debug
printf( "res = %d\n", res );
 
switch( * answer_count ){
case 0: ipc_answer_0( callid, res );
continue;
case 1: ipc_answer_1( callid, res, IPC_GET_ARG1( * answer ));
continue;
case 2: ipc_answer_2( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ));
continue;
case 3: ipc_answer_3( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ));
continue;
case 4: ipc_answer_4( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ), IPC_GET_ARG4( * answer ));
continue;
default: ipc_answer_5( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ), IPC_GET_ARG4( * answer ), IPC_GET_ARG5( * answer ));
continue;
}
}
 
socket_cores_destroy( & local_sockets );
 
return EOK;
}
 
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, void * data, size_t length, int flags ){
ERROR_DECLARE;
 
socket_core_ref socket;
struct sockaddr * address;
struct sockaddr_in * address_in;
packet_t packet;
udp_header_ref header;
 
if( addrlen < sizeof( struct sockaddr )) return EINVAL;
address = ( struct sockaddr * ) addr;
switch( address->sa_family ){
case AF_INET:
if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
address_in = ( struct sockaddr_in * ) addr;
socket = socket_cores_find( local_sockets, socket_id );
if( ! socket ) return ENOTSOCK;
/* if( socket->port < 0 ){
return ENOTCONN;
socket->port = udp_globals.port_search_start;
while( socket_ports_find( global_sockets, socket->port ) >= 0 ){
socket_port = ( socket->port + 1 ) % ( 65535 - 1025 ) + 1025;
}
ERROR_PROPAGATE( socket_ports_add( global_sockets, socket->port );
udp_globals.port_search_start = socket->port + 1;
}
*/ // TODO create and send fragmented packets
// TODO do not ask all the time
ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
packet = packet_get_4( udp_globals.net_phone, length, udp_globals.addr_len, udp_globals.prefix, udp_globals.suffix );
if( ! packet ) return ENOMEM;
if( ERROR_OCCURRED( packet_copy_data( packet, data, length ))
|| ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){
pq_release( udp_globals.net_phone, packet_get_id( packet ));
return ERROR_CODE;
}
header = PACKET_PREFIX( packet, udp_header_t );
if( ! header ){
pq_release( udp_globals.net_phone, packet_get_id( packet ));
return ENOMEM;
}
header->source = ( socket->port < 0 ) ? 0 : htons( socket->port );
header->dest = htons( address_in->sin_port );
header->len = htons( length );
// TODO my ip address for the pseudo header checksum
header->check = 0;
if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
pq_release( udp_globals.net_phone, packet_get_id( packet ));
return ERROR_CODE;
}
return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP );
break;
// TODO IPv6
default:
return EAFNOSUPPORT;
}
return EOK;
}
 
int socket_get_data( void ** data, size_t * length ){
ERROR_DECLARE;
 
ipc_callid_t callid;
 
if( !( data && length )) return EBADMEM;
if( ! ipc_data_write_receive( & callid, length )) return EINVAL;
* data = malloc( * length );
if( ! data ) return ENOMEM;
if( ERROR_OCCURRED( ipc_data_write_finalize( callid, * data, * length ))){
free( data );
return ERROR_CODE;
}
return EOK;
}
 
/** @}
*/
/branches/network/uspace/srv/net/tl/udp/udp.h
36,11 → 36,19
#ifndef __NET_UDP_H__
#define __NET_UDP_H__
 
#include "../../socket/socket_core.h"
 
typedef struct udp_globals udp_globals_t;
 
struct udp_globals{
int ip_phone;
int net_phone;
int ip_phone;
size_t addr_len;
size_t prefix;
size_t content;
size_t suffix;
int net_phone;
int port_search_start;
socket_ports_t sockets;
};
 
#endif
/branches/network/uspace/srv/net/tl/udp/udp_module.c
68,6 → 68,9
 
async_set_client_connection( client_connection );
udp_globals.net_phone = net_connect_module( SERVICE_NETWORKING );
if( udp_globals.net_phone < 0 ){
return udp_globals.net_phone;
}
ERROR_PROPAGATE( pm_init());
if( ERROR_OCCURRED( udp_initialize( client_connection ))
|| ERROR_OCCURRED( REGISTER_ME( SERVICE_UDP, & phonehash ))){