49,6 → 49,7 |
#include "../../structures/dynamic_fifo.h" |
#include "../../structures/packet/packet_client.h" |
|
#include "../../include/crc.h" |
#include "../../include/in.h" |
#include "../../include/in6.h" |
#include "../../include/inet.h" |
57,6 → 58,7 |
#include "../../include/ip_protocols.h" |
#include "../../include/icmp_client.h" |
#include "../../include/icmp_interface.h" |
#include "../../include/net_interface.h" |
#include "../../include/socket_codes.h" |
#include "../../include/socket_errno.h" |
|
69,6 → 71,10 |
#include "udp_header.h" |
#include "udp_module.h" |
|
/** Default UDP checksum computing. |
*/ |
#define NET_DEFAULT_UDP_CHECKSUM_COMPUTING true |
|
/** Maximum UDP fragment size. |
*/ |
#define MAX_UDP_FRAGMENT_SIZE 65535 |
163,24 → 169,12 |
* @returns ENOMEM if there is not enough memory left. |
* @returns EINVAL if the received address is not an IP address. |
* @returns Other error codes as defined for the packet_translate() function. |
* @returns Other error codes as defined for the socket_write_data() function. |
* @returns Other error codes as defined for the data_reply() function. |
*/ |
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ); |
|
/*@}*/ |
|
/** Receives data from the socket. |
* The received data buffer is allocated and returned. |
* @param data The data buffer to be filled. Output parameter. |
* @param length The buffer length. Output parameter. |
* @returns EOK on success. |
* @returns EBADMEM if the data or the length parameter is NULL. |
* @returns EINVAL if the client does not send data. |
* @returns ENOMEM if there is not enough memory left. |
* @returns Other error codes as defined for the ipc_data_write_finalize() function. |
*/ |
int socket_read_data( void ** data, size_t * length ); |
|
/** Receives data from the socket into a packet. |
* @param packet The new created packet. Output parameter. |
* @param prefix Reserved packet data prefix length. Input parameter. |
193,15 → 187,6 |
*/ |
int socket_read_packet_data( packet_ref packet, size_t prefix, const struct sockaddr * addr, socklen_t addrlen ); |
|
/** Replies the data to the socket. |
* @param data The data buffer to be sent. Input parameter. |
* @param data_length The buffer length. Input parameter. |
* @returns EOK on success. |
* @returns EINVAL if the client does not expect all the data. |
* @returns Other error codes as defined for the ipc_data_read_finalize() function. |
*/ |
int socket_write_data( void * data, size_t data_length ); |
|
/** Sets the address port. |
* Supports AF_INET and AF_INET6 address families. |
* @param addr The address to be updated. Input/output parameter. |
220,6 → 205,11 |
int udp_initialize( async_client_conn_t client_connection ){ |
ERROR_DECLARE; |
|
measured_string_t names[] = {{ "UDP_CHECKSUM_COMPUTING", 22 }}; |
measured_string_ref configuration; |
size_t count = sizeof( names ) / sizeof( measured_string_t ); |
char * data; |
|
fibril_rwlock_initialize( & udp_globals.lock ); |
fibril_rwlock_write_lock( & udp_globals.lock ); |
udp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP ); |
235,6 → 225,16 |
udp_globals.prefix += sizeof( udp_header_t ); |
udp_globals.content -= sizeof( udp_header_t ); |
udp_globals.last_used_port = UDP_FREE_PORTS_START - 1; |
// get configuration |
udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING; |
configuration = & names[ 0 ]; |
ERROR_PROPAGATE( net_get_conf_req( udp_globals.net_phone, & configuration, count, & data )); |
if( configuration ){ |
if( configuration[ 0 ].value ){ |
udp_globals.checksum_computing = ( configuration[ 0 ].value[ 0 ] == 'y' ); |
} |
net_free_settings( configuration, data ); |
} |
fibril_rwlock_write_unlock( & udp_globals.lock ); |
return EOK; |
} |
250,11 → 250,14 |
socket_core_ref * socket; |
packet_t next_packet; |
size_t total_length; |
// uint16_t checksum; |
uint32_t checksum; |
int fragments; |
packet_t tmp_packet; |
icmp_type_t type; |
icmp_code_t code; |
ip_pseudo_header_ref ip_header; |
struct sockaddr * src; |
struct sockaddr * dest; |
|
if( error ){ |
switch( error ){ |
308,17 → 311,37 |
next_packet = packet; |
fragments = 0; |
total_length = ntohs( header->len ); |
// compute header checksum if set |
if( header->check && ( ! error )){ |
result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest ); |
if( result <= 0 ){ |
return udp_release_and_return( packet, result ); |
} |
if( ERROR_OCCURRED( ip_client_get_pseudo_header( IPPROTO_UDP, src, result, dest, result, total_length, & ip_header, & length ))){ |
return udp_release_and_return( packet, ERROR_CODE ); |
}else{ |
checksum = compute_checksum( 0, ip_header, length ); |
// the udp header checksum will be added with the first fragment later |
free( ip_header ); |
} |
}else{ |
header->check = 0; |
checksum = 0; |
} |
do{ |
++ fragments; |
length = packet_get_data_length( packet ); |
length = packet_get_data_length( next_packet ); |
if( length <= 0 ){ |
return udp_release_and_return( packet, NO_DATA ); |
} |
if( total_length < length ){ |
// cut of the suffix if too long |
if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - total_length ))){ |
return udp_release_and_return( packet, ERROR_CODE ); |
} |
// add partial checksum if set |
if( header->check ){ |
checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet )); |
} |
// relese the rest of the packet fragments |
tmp_packet = pq_next( next_packet ); |
while( tmp_packet ){ |
326,13 → 349,24 |
pq_release( udp_globals.net_phone, packet_get_id( tmp_packet )); |
tmp_packet = next_packet; |
} |
// exit the loop |
break; |
} |
total_length -= length; |
/* if( header->header_checksum ){ |
// add partial checksum if set |
if( header->check ){ |
checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet )); |
} |
*/ |
}while(( next_packet = pq_next( next_packet )) && ( total_length > 0 )); |
// check checksum |
if( header->check ){ |
if( flip_checksum( compact_checksum( checksum ))){ |
// TODO checksum error ICMP? |
// TODO remove debug dump |
printf("udp check failed %x => %x\n", header->check, flip_checksum( compact_checksum( checksum ))); |
return udp_release_and_return( packet, EINVAL ); |
} |
} |
// queue the received packet |
if( ERROR_OCCURRED( dyn_fifo_push( &( ** socket ).received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){ |
return udp_release_and_return( packet, ERROR_CODE ); |
404,7 → 438,7 |
answer_count = 3; |
break; |
case NET_SOCKET_BIND: |
res = socket_read_data(( void ** ) & addr, & addrlen ); |
res = data_receive(( void ** ) & addr, & addrlen ); |
if( res == EOK ){ |
fibril_rwlock_write_lock( & lock ); |
fibril_rwlock_write_lock( & udp_globals.lock ); |
415,7 → 449,7 |
} |
break; |
case NET_SOCKET_SENDTO: |
res = socket_read_data(( void ** ) & addr, & addrlen ); |
res = data_receive(( void ** ) & addr, & addrlen ); |
if( res == EOK ){ |
fibril_rwlock_read_lock( & lock ); |
fibril_rwlock_read_lock( & udp_globals.lock ); |
475,6 → 509,10 |
size_t total_length; |
int result; |
uint16_t dest_port; |
uint32_t checksum; |
ip_pseudo_header_ref ip_header; |
size_t headerlen; |
device_id_t device_id; |
|
if( addrlen < sizeof( struct sockaddr )) return EINVAL; |
switch( addr->sa_family ){ |
510,18 → 548,23 |
} |
|
// 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 )); |
ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, socket->device_id, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix )); |
|
// read the first packet fragment |
result = socket_read_packet_data( & packet, sizeof( udp_header_t ), addr, addrlen ); |
if( result < 0 ) return result; |
total_length = ( size_t ) result; |
if( udp_globals.checksum_computing ){ |
checksum = compute_checksum( 0, packet_get_data( packet ), packet_get_data_length( packet )); |
}else{ |
checksum = 0; |
} |
// prefix the udp header |
header = PACKET_PREFIX( packet, udp_header_t ); |
if( ! header ){ |
pq_release( udp_globals.net_phone, packet_get_id( packet )); |
return ENOMEM; |
return udp_release_and_return( packet, ENOMEM ); |
} |
bzero( header, sizeof( * header )); |
// read the rest of the packet fragments |
for( index = 1; index < fragments; ++ index ){ |
result = socket_read_packet_data( & next_packet, 0, addr, addrlen ); |
530,20 → 573,40 |
} |
packet = pq_add( packet, next_packet, index, 0 ); |
total_length += ( size_t ) result; |
if( udp_globals.checksum_computing ){ |
checksum = compute_checksum( checksum, packet_get_data( next_packet ), packet_get_data_length( next_packet )); |
} |
} |
// set the udp header |
header->source = htons( socket->port ); |
header->dest = htons( dest_port ); |
header->len = htons( total_length + sizeof( udp_header_t )); |
// TODO my ip address for the pseudo header checksum |
header->check = 0; |
if( udp_globals.checksum_computing ){ |
if( ERROR_OCCURRED( ip_get_route_req( udp_globals.ip_phone, IPPROTO_UDP, addr, addrlen, & device_id, & ip_header, & headerlen ))){ |
return udp_release_and_return( packet, ERROR_CODE ); |
} |
if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( ip_header, headerlen, total_length + sizeof( udp_header_t )))){ |
free( ip_header ); |
return udp_release_and_return( packet, ERROR_CODE ); |
} |
/*// TODO remove debug dump: |
uint8_t * data; |
data = ip_header; |
printf( "ip_header:\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", headerlen, data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ] ); |
*/ checksum = compute_checksum( checksum, ip_header, headerlen ); |
checksum = compute_checksum( checksum, ( uint8_t * ) header, sizeof( * header )); |
header->check = htons( flip_checksum( compact_checksum( checksum ))); |
free( ip_header ); |
}else{ |
device_id = socket->device_id; |
} |
// prepare the first packet fragment |
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 udp_release_and_return( packet, ERROR_CODE ); |
} |
// send the packet |
return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP, 0 ); |
return ip_send_msg( udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0 ); |
} |
|
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){ |
585,12 → 648,12 |
} |
* addrlen = ( size_t ) result; |
// send the source address |
ERROR_PROPAGATE( socket_write_data( addr, * addrlen )); |
ERROR_PROPAGATE( data_reply( addr, * addrlen )); |
|
next_packet = pq_next( packet ); |
if( ! next_packet ){ |
// write all if only one fragment |
ERROR_PROPAGATE( socket_write_data( data + sizeof( udp_header_t ), packet_get_data_length( packet ) - sizeof( udp_header_t ))); |
ERROR_PROPAGATE( data_reply( data + sizeof( udp_header_t ), packet_get_data_length( packet ) - sizeof( udp_header_t ))); |
// store the total length |
length = packet_get_data_length( packet ) - sizeof( udp_header_t ); |
}else{ |
612,13 → 675,13 |
next_packet = pq_next( packet ); |
}while( next_packet ); |
// write the fragment lengths |
ERROR_PROPAGATE( socket_write_data( lengths, sizeof( int ) * ( fragments + 1 ))); |
ERROR_PROPAGATE( data_reply( lengths, sizeof( int ) * ( fragments + 1 ))); |
// write the first fragment |
ERROR_PROPAGATE( socket_write_data( data + sizeof( udp_header_t ), lengths[ 0 ] )); |
ERROR_PROPAGATE( data_reply( data + sizeof( udp_header_t ), lengths[ 0 ] )); |
next_packet = pq_next( packet ); |
// write the rest of the fragments |
for( index = 1; index < fragments; ++ index ){ |
ERROR_PROPAGATE( socket_write_data( packet_get_data( next_packet ), lengths[ index ] )); |
ERROR_PROPAGATE( data_reply( packet_get_data( next_packet ), lengths[ index ] )); |
next_packet = pq_next( packet ); |
}while( next_packet ); |
// store the total length |
632,33 → 695,6 |
return ( int ) length; |
} |
|
int socket_write_data( void * data, size_t data_length ){ |
size_t length; |
ipc_callid_t callid; |
|
if(( ! ipc_data_read_receive( & callid, & length )) |
|| ( length < data_length )){ |
return EINVAL; |
} |
return ipc_data_read_finalize( callid, data, data_length ); |
} |
|
int socket_read_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; |
} |
|
int socket_read_packet_data( packet_ref packet, size_t prefix, const struct sockaddr * addr, socklen_t addrlen ){ |
ERROR_DECLARE; |
|
674,15 → 710,13 |
// allocate space in the packet |
data = packet_suffix( * packet, length ); |
if( ! data ){ |
pq_release( udp_globals.net_phone, packet_get_id( * packet )); |
return ENOMEM; |
return udp_release_and_return( * packet, ENOMEM ); |
} |
// read the data into the packet |
if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length )) |
// set the packet destination address |
|| ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){ |
pq_release( udp_globals.net_phone, packet_get_id( * packet )); |
return ERROR_CODE; |
return udp_release_and_return( * packet, ERROR_CODE ); |
} |
return ( int ) length; |
} |