Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4719 → Rev 4720

/branches/network/uspace/srv/net/tl/udp/udp.c
50,6 → 50,7
#include "../../structures/packet/packet_client.h"
 
#include "../../include/in.h"
#include "../../include/in6.h"
#include "../../include/inet.h"
#include "../../include/ip_client.h"
#include "../../include/ip_interface.h"
130,6 → 131,7
/** Sends data from the socket to the remote address.
* Binds the socket to a free port if not already connected/bound.
* Handles the NET_SOCKET_SENDTO message.
* Supports AF_INET and AF_INET6 address families.
* @param local_sockets The application local sockets. Input/output parameter.
* @param socket_id Socket identifier. Input parameter.
* @param addr The destination address. Input parameter.
146,13 → 148,15
* @returns Other error codes as defined for the ip_client_prepare_packet() function.
* @returns Other error codes as defined for the ip_send_msg() function.
*/
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, int fragments, int flags );
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, int flags );
 
/** Receives data to the socket.
* Handles the NET_SOCKET_RECVFROM message.
* Replies the source address as well.
* @param local_sockets The application local sockets. Input parameter.
* @param socket_id Socket identifier. Input parameter.
* @param flags Various receive flags. Input parameter.
* @param addrlen The source address length. Output parameter.
* @returns The number of bytes received.
* @returns ENOTSOCK if the socket is not found.
* @returns NO_DATA if there are no received packets or data.
161,7 → 165,7
* @returns Other error codes as defined for the packet_translate() function.
* @returns Other error codes as defined for the socket_write_data() function.
*/
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags );
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen );
 
/*@}*/
 
180,13 → 184,14
/** 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.
* @param address_in The destination address to be set. Input parameter.
* @param addr The destination address. Input parameter.
* @param addrlen The address length. Input parameter.
* @returns Number of bytes received.
* @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_read_finalize() function.
*/
int socket_read_packet_data( packet_ref packet, size_t prefix, struct sockaddr_in * address_in );
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.
197,6 → 202,17
*/
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.
* @param addrlen The address length. Input parameter.
* @param port The port to be set. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the address length does not match the address family.
* @returns EAFNOSUPPORT if the address family is not supported.
*/
int udp_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port );
 
/** UDP global data.
*/
udp_globals_t udp_globals;
352,7 → 368,7
bool keep_on_going = true;
socket_cores_t local_sockets;
int app_phone = IPC_GET_PHONE( & call );
void * addr;
struct sockaddr * addr;
size_t addrlen;
fibril_rwlock_t lock;
ipc_call_t answer;
388,7 → 404,7
answer_count = 3;
break;
case NET_SOCKET_BIND:
res = socket_read_data( & addr, & addrlen );
res = socket_read_data(( void ** ) & addr, & addrlen );
if( res == EOK ){
fibril_rwlock_write_lock( & lock );
fibril_rwlock_write_lock( & udp_globals.lock );
399,7 → 415,7
}
break;
case NET_SOCKET_SENDTO:
res = socket_read_data( & addr, & addrlen );
res = socket_read_data(( void ** ) & addr, & addrlen );
if( res == EOK ){
fibril_rwlock_read_lock( & lock );
fibril_rwlock_read_lock( & udp_globals.lock );
412,12 → 428,12
case NET_SOCKET_RECVFROM:
fibril_rwlock_read_lock( & lock );
fibril_rwlock_read_lock( & udp_globals.lock );
res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ));
res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen );
fibril_rwlock_read_unlock( & udp_globals.lock );
fibril_rwlock_read_unlock( & lock );
if( res > 0 ){
* SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
* SOCKET_SET_ADDRESS_LENGTH( answer ) = sizeof( struct sockaddr_in );
* SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen;
answer_count = 2;
res = EOK;
}
446,12 → 462,12
return EOK;
}
 
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, int fragments, int flags ){
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, int flags ){
ERROR_DECLARE;
 
socket_core_ref socket;
struct sockaddr * address;
struct sockaddr_in * address_in;
struct sockaddr_in6 * address_in6;
packet_t packet;
packet_t next_packet;
udp_header_ref header;
458,70 → 474,79
int index;
size_t total_length;
int result;
uint16_t dest_port;
 
if( addrlen < sizeof( struct sockaddr )) return EINVAL;
address = ( struct sockaddr * ) addr;
switch( address->sa_family ){
switch( addr->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;
dest_port = address_in->sin_port;
break;
case AF_INET6:
if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
address_in6 = ( struct sockaddr_in6 * ) addr;
dest_port = address_in6->sin6_port;
break;
default:
return EAFNOSUPPORT;
}
 
// bind the socket to a random free port if not bound
while( socket->port <= 0 ){
// try to find a free port
fibril_rwlock_read_unlock( & udp_globals.lock );
fibril_rwlock_write_lock( & udp_globals.lock );
if( socket->port <= 0 ){
ERROR_PROPAGATE( socket_bind_free_port( & udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port ));
// set the next port as the search starting port number
udp_globals.last_used_port = socket->port;
}
fibril_rwlock_write_unlock( & udp_globals.lock );
fibril_rwlock_read_lock( & udp_globals.lock );
}
// 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 ));
socket = socket_cores_find( local_sockets, socket_id );
if( ! socket ) return ENOTSOCK;
 
// read the first packet fragment
result = socket_read_packet_data( & packet, sizeof( udp_header_t ), address_in );
if( result < 0 ) return result;
total_length = ( size_t ) result;
// 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;
}
// read the rest of the packet fragments
for( index = 1; index < fragments; ++ index ){
result = socket_read_packet_data( & next_packet, 0, address_in );
if( result < 0 ){
return udp_release_and_return( packet, result );
}
packet = pq_add( packet, next_packet, index, 0 );
total_length += ( size_t ) result;
}
// set the udp header
header->source = htons( socket->port );
header->dest = htons( address_in->sin_port );
header->len = htons( total_length + sizeof( udp_header_t ));
// TODO my ip address for the pseudo header checksum
header->check = 0;
// 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;
}
// send the packet
return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP, 0 );
// TODO IPv6
// bind the socket to a random free port if not bound
while( socket->port <= 0 ){
// try to find a free port
fibril_rwlock_read_unlock( & udp_globals.lock );
fibril_rwlock_write_lock( & udp_globals.lock );
if( socket->port <= 0 ){
ERROR_PROPAGATE( socket_bind_free_port( & udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port ));
// set the next port as the search starting port number
udp_globals.last_used_port = socket->port;
}
fibril_rwlock_write_unlock( & udp_globals.lock );
fibril_rwlock_read_lock( & udp_globals.lock );
}
return EAFNOSUPPORT;
 
// 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 ));
 
// 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;
// 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;
}
// read the rest of the packet fragments
for( index = 1; index < fragments; ++ index ){
result = socket_read_packet_data( & next_packet, 0, addr, addrlen );
if( result < 0 ){
return udp_release_and_return( packet, result );
}
packet = pq_add( packet, next_packet, index, 0 );
total_length += ( size_t ) result;
}
// 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;
// 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;
}
// send the packet
return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP, 0 );
}
 
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags ){
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){
ERROR_DECLARE;
 
socket_core_ref socket;
528,15 → 553,14
int packet_id;
packet_t packet;
udp_header_ref header;
struct sockaddr_in address;
struct sockaddr * addr;
size_t length;
packet_t next_packet;
uint8_t * data;
size_t fragments;
size_t * lengths;
size_t index;
int result;
size_t index;
uint8_t * addr;
 
// find the socket
socket = socket_cores_find( local_sockets, socket_id );
552,18 → 576,17
return NO_DATA;
}
header = ( udp_header_ref ) data;
// set the source address
address.sin_family = PF_INET;
address.sin_port = ntohs( header->source );
result = packet_get_addr( packet, & addr, NULL );
if( result != sizeof( address.sin_addr.s_addr )){
 
// set the source address port
result = packet_get_addr( packet, ( uint8_t ** ) & addr, NULL );
if( ERROR_OCCURRED( udp_set_address_port( addr, result, ntohs( header->source )))){
pq_release( udp_globals.net_phone, packet_id );
return EINVAL;
return ERROR_CODE;
}
address.sin_addr.s_addr = *(( uint32_t * ) addr );
bzero( & address.sin_zero, sizeof( address.sin_zero ));
* addrlen = ( size_t ) result;
// send the source address
ERROR_PROPAGATE( socket_write_data( & address, sizeof( address )));
ERROR_PROPAGATE( socket_write_data( addr, * addrlen ));
 
next_packet = pq_next( packet );
if( ! next_packet ){
// write all if only one fragment
636,7 → 659,7
return EOK;
}
 
int socket_read_packet_data( packet_ref packet, size_t prefix, struct sockaddr_in * address_in ){
int socket_read_packet_data( packet_ref packet, size_t prefix, const struct sockaddr * addr, socklen_t addrlen ){
ERROR_DECLARE;
 
ipc_callid_t callid;
657,7 → 680,7
// 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 * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){
|| ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){
pq_release( udp_globals.net_phone, packet_get_id( * packet ));
return ERROR_CODE;
}
691,5 → 714,29
}
}
 
int udp_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){
struct sockaddr_in * address_in;
struct sockaddr_in6 * address_in6;
size_t length;
 
if( addrlen < 0 ) return EINVAL;
length = ( size_t ) addrlen;
if( length < sizeof( struct sockaddr )) return EINVAL;
switch( addr->sa_family ){
case AF_INET:
if( length != sizeof( struct sockaddr_in )) return EINVAL;
address_in = ( struct sockaddr_in * ) addr;
address_in->sin_port = port;
return EOK;
case AF_INET6:
if( length != sizeof( struct sockaddr_in6 )) return EINVAL;
address_in6 = ( struct sockaddr_in6 * ) addr;
address_in6->sin6_port = port;
return EOK;
default:
return EAFNOSUPPORT;
}
}
 
/** @}
*/