Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4700 → Rev 4701

/branches/network/uspace/srv/net/tl/udp/udp.c
31,6 → 31,8
*/
 
/** @file
* UDP module implementation.
* @see udp.h
*/
 
#include <async.h>
65,24 → 67,130
#include "udp_header.h"
#include "udp_module.h"
 
/** Maximum UDP fragment size.
*/
#define MAX_UDP_FRAGMENT_SIZE 65535
 
/** Free ports pool start.
*/
#define UDP_FREE_PORTS_START 1025
 
/** Free ports pool end.
*/
#define UDP_FREE_PORTS_END 65535
 
/** Processes the received UDP packet.
* Is used as an entry point from the underlying IP module.
* Releases the packet on error.
* @param device_id The device identifier. Ignored parameter.
* @param packet The received packet. Input/output parameter.
* @param receiver The target service. Ignored parameter.
* @returns EOK on success.
* @returns Other error codes as defined for the udp_process_packet() function.
*/
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver );
 
/** Processes the received UDP packet.
* Notifies the destination socket application.
* @param packet The received packet. Input/output parameter.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
* @returns EINVAL if the stored packet address is not the an_addr_t.
* @returns EINVAL if the packet does not contain any data.
* @returns NO_DATA if the packet content is shorter than the user datagram header.
* @returns ENOMEM if there is not enough memory left.
* @returns EADDRNOTAVAIL if the destination socket does not exist.
* @returns Other error codes as defined for the ip_client_process_packet() function.
*/
int udp_process_packet( packet_t packet );
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
 
/** @name Socket messages processing functions
*/
/*@{*/
 
/** Processes the socket client messages.
* Runs until the client module disconnects.
* @param callid The message identifier. Input parameter.
* @param call The message parameters. Input parameter.
* @returns EOK on success.
* @see socket.h
*/
int process_client_messages( ipc_callid_t callid, ipc_call_t call );
 
/** 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.
* @param local_sockets The application local sockets. Input/output parameter.
* @param socket_id Socket identifier. Input parameter.
* @param addr The destination address. Input parameter.
* @param addrlen The address length. Input parameter.
* @param fragments The number of data fragments. Input parameter.
* @param flags Various send flags. Input parameter.
* @returns EOK on success.
* @returns EAFNOTSUPPORT if the address family is not supported.
* @returns ENOTSOCK if the socket is not found.
* @returns EINVAL if the address is invalid.
* @returns ENOTCONN if the sending socket is not and cannot be bound.
* @returns ENOMEM if there is not enough memory left.
* @returns Other error codes as defined for the socket_read_packet_data() function.
* @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 );
 
/** Receives data to the socket.
* Handles the NET_SOCKET_RECVFROM message.
* @param local_sockets The application local sockets. Input parameter.
* @param socket_id Socket identifier. Input parameter.
* @param flags Various receive flags. Input 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.
* @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.
*/
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags );
 
/*@}*/
 
/** 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.
* @param address_in The destination address to be set. 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 );
 
/** 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 );
 
/** UDP global data.
*/
udp_globals_t udp_globals;
 
/** Initializes the module.
*/
int udp_initialize( async_client_conn_t client_connection ){
ERROR_DECLARE;
 
129,18 → 237,12
 
// get packet data
length = packet_get_addr( packet, & src, & dest );
if( length < 0 ) return length;
if( length != sizeof( in_addr_t )) return EINVAL;
// TODO received ipopts?
// TODO process received ipopts?
ERROR_PROPAGATE( ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ));
 
// TODO remove debug dump:
/* uint8_t * rdata;
rdata = packet_get_data( packet );
printf( "Receiving udp packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", packet_get_id( packet ), packet_get_data_length( packet ), rdata[ 0 ], rdata[ 1 ], rdata[ 2 ], rdata[ 3 ], rdata[ 4 ], rdata[ 5 ], rdata[ 6 ], rdata[ 7 ], rdata[ 8 ], rdata[ 9 ], rdata[ 10 ], rdata[ 11 ], rdata[ 12 ], rdata[ 13 ], rdata[ 14 ], rdata[ 15 ], rdata[ 16 ], rdata[ 17 ], rdata[ 18 ], rdata[ 19 ], rdata[ 20 ], rdata[ 21 ], rdata[ 22 ], rdata[ 23 ], rdata[ 24 ], rdata[ 25 ], rdata[ 26 ], rdata[ 27 ], rdata[ 28 ], rdata[ 29 ], rdata[ 30 ], rdata[ 31 ], rdata[ 32 ], rdata[ 33 ], rdata[ 34 ], rdata[ 35 ], rdata[ 36 ], rdata[ 37 ], rdata[ 38 ], rdata[ 39 ], rdata[ 40 ], rdata[ 41 ], rdata[ 42 ], rdata[ 43 ], rdata[ 44 ], rdata[ 45 ], rdata[ 46 ], rdata[ 47 ], rdata[ 48 ], rdata[ 49 ], rdata[ 50 ], rdata[ 51 ], rdata[ 52 ], rdata[ 53 ], rdata[ 54 ], rdata[ 55 ], rdata[ 56 ], rdata[ 57 ], rdata[ 58 ], rdata[ 59 ] );
*/
length = packet_get_data_length( packet );
if( length < 0 ) return length;
if( length <= 0 ) return EINVAL;
if( length < sizeof( udp_header_t )) return NO_DATA;
data = packet_get_data( packet );
if( ! data ) return NO_DATA;
156,7 → 258,6
do{
++ fragments;
length = packet_get_data_length( packet );
if( length < 0 ) return length;
if( ! length ) return NO_DATA;
if( total_length < length ){
// cut of the suffix if too long
193,26 → 294,28
case NET_TL_RECEIVED:
fibril_rwlock_read_lock( & udp_globals.lock );
if( ! ERROR_OCCURRED( packet_translate( udp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
ERROR_CODE = udp_received_msg( IPC_GET_DEVICE( call ), packet, 0 );
ERROR_CODE = udp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_UDP );
}
fibril_rwlock_read_unlock( & udp_globals.lock );
return ERROR_CODE;
case IPC_M_CONNECT_TO_ME:
return process_client_messages( callid, call, answer, answer_count );
return process_client_messages( callid, * call );
}
return ENOTSUP;
}
 
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
int process_client_messages( ipc_callid_t callid, ipc_call_t call ){
ERROR_DECLARE;
 
int res;
bool keep_on_going = true;
socket_cores_t local_sockets;
int app_phone = IPC_GET_PHONE( call );
int app_phone = IPC_GET_PHONE( & call );
void * addr;
size_t addrlen;
fibril_rwlock_t lock;
ipc_call_t answer;
int answer_count;
 
/*
* Accept the connection
225,20 → 328,20
 
while( keep_on_going ){
// refresh data
* answer_count = 0;
IPC_SET_RETVAL( * answer, 0 );
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 );
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 );
callid = async_get_call( & call );
// printf( "message %d\n", IPC_GET_METHOD( * call ));
 
switch( IPC_GET_METHOD( * call )){
switch( IPC_GET_METHOD( call )){
case IPC_M_PHONE_HUNGUP:
keep_on_going = false;
res = EOK;
249,7 → 352,7
fibril_rwlock_write_unlock( & lock );
* SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t );
* SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE;
* answer_count = 3;
answer_count = 3;
break;
case NET_SOCKET_BIND:
if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
262,7 → 365,7
fibril_rwlock_write_unlock( & udp_globals.lock );
fibril_rwlock_write_unlock( & lock );
free( addr );
break;
break;
case NET_SOCKET_SENDTO:
if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
res = ERROR_CODE;
284,7 → 387,7
if( res > 0 ){
* SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
* SOCKET_SET_ADDRESS_LENGTH( answer ) = sizeof( struct sockaddr_in );
* answer_count = 2;
answer_count = 2;
res = EOK;
}
break;
304,18 → 407,18
 
// printf( "res = %d\n", res );
 
switch( * answer_count ){
switch( answer_count ){
case 0: ipc_answer_0( callid, res );
continue;
case 1: ipc_answer_1( callid, res, IPC_GET_ARG1( * answer ));
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 ));
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 ));
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 ));
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 ));
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;
}
}
421,7 → 524,7
if( ! socket ) return ENOTSOCK;
// get the next received packet
packet_id = dyn_fifo_value( & socket->received );
if( packet_id < 0 ) return packet_id;
if( packet_id < 0 ) return NO_DATA;
ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, packet_id ));
// get udp header
data = packet_get_data( packet );
506,7 → 609,7
if( !( data && length )) return EBADMEM;
if( ! ipc_data_write_receive( & callid, length )) return EINVAL;
* data = malloc( * length );
if( ! data ) return ENOMEM;
if( !( * data )) return ENOMEM;
if( ERROR_OCCURRED( ipc_data_write_finalize( callid, * data, * length ))){
free( data );
return ERROR_CODE;
/branches/network/uspace/srv/net/include/socket.h
45,7 → 45,9
#include "in.h"
#include "inet.h"
 
/* Supported address families. */
/** @name Address families definitions
*/
/*@{*/
/** Unspecified address family.
*/
#define AF_UNSPEC 0
177,80 → 179,270
/** Maximum address family.
*/
#define AF_MAX 34
/*@}*/
 
/* Protocol families, same as address families. */
/** @name Protocol families definitions
* Same as address families.
*/
/*@{*/
/** Unspecified protocol family.
*/
#define PF_UNSPEC AF_UNSPEC
 
#define PF_UNSPEC AF_UNSPEC
#define PF_UNIX AF_UNIX
/** Unix domain sockets protocol family.
*/
#define PF_UNIXL AF_UNIXL
 
/** POSIX name for AF_UNIX protocol family.
*/
#define PF_LOCAL AF_LOCAL
 
/** Internet IP Protocol protocol family.
*/
#define PF_INET AF_INET
 
/** Amateur Radio AX.25 protocol family.
*/
#define PF_AX25 AF_AX25
 
/** Novell IPX protocol family.
*/
#define PF_IPX AF_IPX
 
/** AppleTalk DDP protocol family.
*/
#define PF_APPLETALK AF_APPLETALK
#define PF_NETROM AF_NETROM
 
/** Amateur Radio NET/ROM protocol family.
*/
#define PF_NETROM AF_NETROM
 
/** Multiprotocol bridge protocol family.
*/
#define PF_BRIDGE AF_BRIDGE
 
/** ATM PVCs protocol family.
*/
#define PF_ATMPVC AF_ATMPVC
 
/** Reserved for X.25 project protocol family.
*/
#define PF_X25 AF_X25
 
/** IP version 6 protocol family.
*/
#define PF_INET6 AF_INET6
 
/** Amateur Radio X.25 PLP protocol family.
*/
#define PF_ROSE AF_ROSE
 
/** Reserved for DECnet project protocol family.
*/
#define PF_DECnet AF_DECnet
 
/** Reserved for 802.2LLC project protocol family.
*/
#define PF_NETBEUI AF_NETBEUI
 
/** Security callback pseudo AF protocol family.
*/
#define PF_SECURITY AF_SECURITY
 
/** PF_KEY key management API protocol family.
*/
#define PF_KEY AF_KEY
 
/** Alias to emulate 4.4BSD protocol family.
*/
#define PF_NETLINK AF_NETLINK
#define PF_ROUTE AF_ROUTE
 
/** Packet family protocol family.
*/
#define PF_PACKET AF_PACKET
 
/** Ash protocol family.
*/
#define PF_ASH AF_ASH
 
/** Acorn Econet protocol family.
*/
#define PF_ECONET AF_ECONET
 
/** ATM SVCs protocol family.
*/
#define PF_ATMSVC AF_ATMSVC
 
/** Linux SNA Project (nutters!) protocol family.
*/
#define PF_SNA AF_SNA
 
/** IRDA sockets protocol family.
*/
#define PF_IRDA AF_IRDA
 
/** PPPoX sockets protocol family.
*/
#define PF_PPPOX AF_PPPOX
 
/** Wanpipe API Sockets protocol family.
*/
#define PF_WANPIPE AF_WANPIPE
 
/** Linux LLC protocol family.
*/
#define PF_LLC AF_LLC
 
/** Controller Area Network protocol family.
*/
#define PF_CAN AF_CAN
 
/** TIPC sockets protocol family.
*/
#define PF_TIPC AF_TIPC
 
/** Bluetooth sockets protocol family.
*/
#define PF_BLUETOOTH AF_BLUETOOTH
 
/** IUCV sockets protocol family.
*/
#define PF_IUCV AF_IUCV
 
/** RxRPC sockets protocol family.
*/
#define PF_RXRPC AF_RXRPC
 
/** Maximum protocol family.
*/
#define PF_MAX AF_MAX
/*@}*/
 
/* Setsockoptions(2) level. Thanks to BSD these must match IPPROTO_xxx */
/** @name Socket option levels definitions
* Thanks to BSD these must match IPPROTO_xxx
*/
/*@{*/
 
/** IP socket option level.
*/
#define SOL_IP 0
 
/** ICMP socket option level.
*/
#define SOL_ICMP 1
 
/** TCP socket option level.
*/
#define SOL_TCP 6
 
/** UDP socket option level.
*/
#define SOL_UDP 17
 
/** IPV socket option level.
*/
#define SOL_IPV6 41
 
/** ICMPV socket option level.
*/
#define SOL_ICMPV6 58
 
/** SCTP socket option level.
*/
#define SOL_SCTP 132
#define SOL_UDPLITE 136 /* UDP-Lite (RFC 3828) */
 
/** UDP-Lite (RFC 3828) socket option level.
*/
#define SOL_UDPLITE 136
 
/** RAW socket option level.
*/
#define SOL_RAW 255
 
/** IPX socket option level.
*/
#define SOL_IPX 256
 
/** AX socket option level.
*/
#define SOL_AX25 257
 
/** ATALK socket option level.
*/
#define SOL_ATALK 258
 
/** NETROM socket option level.
*/
#define SOL_NETROM 259
 
/** ROSE socket option level.
*/
#define SOL_ROSE 260
 
/** DECNET socket option level.
*/
#define SOL_DECNET 261
 
/** X25 socket option level.
*/
#define SOL_X25 262
 
/** PACKET socket option level.
*/
#define SOL_PACKET 263
#define SOL_ATM 264 /* ATM layer (cell level) */
#define SOL_AAL 265 /* ATM Adaption Layer (packet level) */
#define SOL_IRDA 266
 
/** ATM layer (cell level) socket option level.
*/
#define SOL_ATM 264
 
/** ATM Adaption Layer (packet level) socket option level.
*/
#define SOL_AAL 265
 
/** IRDA socket option level.
*/
#define SOL_IRDA 266
 
/** NETBEUI socket option level.
*/
#define SOL_NETBEUI 267
 
/** LLC socket option level.
*/
#define SOL_LLC 268
 
/** DCCP socket option level.
*/
#define SOL_DCCP 269
 
/** NETLINK socket option level.
*/
#define SOL_NETLINK 270
 
/** TIPC socket option level.
*/
#define SOL_TIPC 271
 
/** RXRPC socket option level.
*/
#define SOL_RXRPC 272
 
/** PPPOL socket option level.
*/
#define SOL_PPPOL2TP 273
 
/** BLUETOOTH socket option level.
*/
#define SOL_BLUETOOTH 274
 
/*@}*/
 
//
/* * IPX options.
*/
//#define IPX_TYPE 1
 
/** Socket type type definition.
/** Socket types.
*/
typedef enum sock_type{
/** Stream (connection oriented) socket.
264,25 → 456,169
SOCK_RAW = 3
} sock_type_t;
 
/** Type definition of the socket length.
*/
typedef int32_t socklen_t;
 
/** @name Socket application programming interface
*/
/*@{*/
 
/** Creates a new socket.
* @param domain The socket protocol family. Input parameter.
* @param type Socket type. Input parameter.
* @param protocol Socket protocol. Input parameter.
* @returns The socket identifier on success.
* @returns EPFNOTSUPPORT if the protocol family is not supported.
* @returns ESOCKNOTSUPPORT if the socket type is not supported.
* @returns EPROTONOSUPPORT if the protocol is not supported.
* @returns ENOMEM if there is not enough memory left.
* @returns Other error codes as defined for the NET_SOCKET message.
*/
int socket( int domain, int type, int protocol );
 
/** Binds the socket to a port address.
* @param socket_id Socket identifier. Input parameter.
* @param my_addr The port address. Input parameter.
* @param addrlen The address length. Input parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the my_addr parameter is NULL.
* @returns NO_DATA if the addlen parameter is zero (0).
* @returns Other error codes as defined for the NET_SOCKET_BIND message.
*/
int bind( int socket_id, const struct sockaddr * my_addr, socklen_t addrlen );
 
/** Sets the number of connections waiting to be accepted.
* @param socket_id Socket identifier. Input parameter.
* @param backlog The maximum number of waiting sockets to be accepted. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the backlog parameter is not positive (<=0).
* @returns ENOTSOCK if the socket is not found.
* @returns Other error codes as defined for the NET_SOCKET_LISTEN message.
*/
int listen( int socket_id, int backlog );
 
/** Accepts waiting socket.
* Blocks until such a socket exists.
* @param socket_id Socket identifier. Input parameter.
* @param cliaddr The remote client address. Output parameter.
* @param addrlen The address length. Input parameter.
* @returns EOK on success.
* @returns EBADMEM if the cliaddr or addrlen parameter is NULL.
* @returns EINVAL if the backlog parameter is not positive (<=0).
* @returns ENOTSOCK if the socket is not found.
* @returns Other error codes as defined for the NET_SOCKET_ACCEPT message.
*/
int accept( int socket_id, struct sockaddr * cliaddr, socklen_t * addrlen );
 
/** Connects socket to the remote server.
* @param socket_id Socket identifier. Input parameter.
* @param serv_addr The remote server address. Input parameter.
* @param addrlen The address length. Input parameter.
* @returns EOK on success.
* @returns EBADMEM if the serv_addr parameter is NULL.
* @returns NO_DATA if the addlen parameter is zero (0).
* @returns ENOTSOCK if the socket is not found.
* @returns Other error codes as defined for the NET_SOCKET_CONNECT message.
*/
int connect( int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen );
 
/** Closes the socket.
* @param socket_id Socket identifier. Input parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns Other error codes as defined for the NET_SOCKET_CLOSE message.
*/
int closesocket( int socket_id );
 
/** Sends data via the socket.
* @param socket_id Socket identifier. Input parameter.
* @param data The data to be sent. Input parameter.
* @param datalength The data length. Input parameter.
* @param flags Various send flags. Input parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the data parameter is NULL.
* @returns NO_DATA if the datalength parameter is zero (0).
* @returns Other error codes as defined for the NET_SOCKET_SEND message.
*/
int send( int socket_id, void * data, size_t datalength, int flags );
 
/** Sends data via the socket to the remote address.
* Binds the socket to a free port if not already connected/bound.
* @param socket_id Socket identifier. Input parameter.
* @param data The data to be sent. Input parameter.
* @param datalength The data length. Input parameter.
* @param flags Various send flags. Input parameter.
* @param toaddr The destination address. Input parameter.
* @param addrlen The address length. Input parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the data or toaddr parameter is NULL.
* @returns NO_DATA if the datalength or the addrlen parameter is zero (0).
* @returns Other error codes as defined for the NET_SOCKET_SENDTO message.
*/
int sendto( int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen );
 
/** Receives data via the socket.
* @param socket_id Socket identifier. Input parameter.
* @param data The data buffer to be filled. Output parameter.
* @param datalength The data length. Input parameter.
* @param flags Various receive flags. Input parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the data parameter is NULL.
* @returns NO_DATA if the datalength parameter is zero (0).
* @returns Other error codes as defined for the NET_SOCKET_RECV message.
*/
int recv( int socket_id, void * data, size_t datalength, int flags );
 
/** Receives data via the socket.
* @param socket_id Socket identifier. Input parameter.
* @param data The data buffer to be filled. Output parameter.
* @param datalength The data length. Input parameter.
* @param flags Various receive flags. Input parameter.
* @param fromaddr The source address. Output parameter.
* @param addrlen The address length. The maximum address length is read. The actual address length is set. Input/output parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the data or fromaddr parameter is NULL.
* @returns NO_DATA if the datalength or addrlen parameter is zero (0).
* @returns Other error codes as defined for the NET_SOCKET_RECVFROM message.
*/
int recvfrom( int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen );
 
/** Gets socket option.
* @param socket_id Socket identifier. Input parameter.
* @param level The socket options level. Input parameter.
* @param optname The socket option to be get. Input parameter.
* @param value The value buffer to be filled. Output parameter.
* @param optlen The value buffer length. The maximum length is read. The actual length is set. Input/output parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the value or optlen parameter is NULL.
* @returns NO_DATA if the optlen parameter is zero (0).
* @returns Other error codes as defined for the NET_SOCKET_GETSOCKOPT message.
*/
int getsockopt( int socket_id, int level, int optname, void * value, size_t * optlen );
 
/** Sets socket option.
* @param socket_id Socket identifier. Input parameter.
* @param level The socket options level. Input parameter.
* @param optname The socket option to be set. Input parameter.
* @param value The value to be set. Input parameter.
* @param optlen The value length. Input parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the value parameter is NULL.
* @returns NO_DATA if the optlen parameter is zero (0).
* @returns Other error codes as defined for the NET_SOCKET_SETSOCKOPT message.
*/
int setsockopt( int socket_id, int level, int optname, const void * value, size_t optlen );
 
/*@}*/
 
#endif
 
/** @}
/branches/network/uspace/srv/net/socket/socket_client.c
54,47 → 54,175
 
#include "socket_messages.h"
 
/** Initial received packet queue size.
*/
#define SOCKET_INITIAL_RECEIVED_SIZE 4
 
/** Maximum received packet queue size.
*/
#define SOCKET_MAX_RECEIVED_SIZE 64
 
/** Initial waiting sockets queue size.
*/
#define SOCKET_INITIAL_ACCEPTED_SIZE 1
 
/** Maximum waiting sockets queue size.
*/
#define SOCKET_MAX_ACCEPTED_SIZE 64
 
/** Type definition of the socket specific data.
* @see socket
*/
typedef struct socket socket_t;
typedef socket_t * socket_ref;
 
/** Type definition of the socket specific data pointer.
* @see socket
*/
typedef socket_t * socket_ref;
 
/** Socket specific data.
*/
struct socket{
/** Socket identifier.
*/
int socket_id;
/** Parent module phone.
*/
int phone;
/** Parent module service.
*/
services_t service;
/** Underlying protocol header size.
* Sending and receiving optimalization.
*/
int header_size;
/** Packet data fragment size.
* Sending and receiving optimalization.
*/
int data_fragment_size;
/** Received packets queue.
*/
dyn_fifo_t received;
/** Received packets safety lock.
*/
fibril_mutex_t receive_lock;
/** Received packets signaling.
*/
fibril_condvar_t receive_signal;
/** Waiting sockets queue.
*/
dyn_fifo_t accepted;
/** Waiting sockets safety lock.
*/
fibril_mutex_t accept_lock;
/** Waiting sockets signaling.
*/
fibril_condvar_t accept_signal;
};
 
/** Sockets map.
* Maps socket identifiers to the socket specific data.
* @see int_map.h
*/
INT_MAP_DECLARE( sockets, socket_t );
 
/** Socket client library global data.
*/
static struct{
/** TCP module phone.
*/
int tcp_phone;
/** UDP module phone.
*/
int udp_phone;
/** Active sockets.
*/
sockets_ref sockets;
} socket_globals = { -1, -1, NULL };
 
INT_MAP_IMPLEMENT( sockets, socket_t );
 
/** Returns the TCP module phone.
* Connects to the TCP module if necessary.
* @returns The TCP module phone.
*/
static int socket_get_tcp_phone();
 
/** Returns the UDP module phone.
* Connects to the UDP module if necessary.
* @returns The UDP module phone.
*/
static int socket_get_tcp_phone();
 
/** Returns the active sockets.
* @returns The active sockets.
*/
static sockets_ref socket_get_sockets();
 
/** Default thread for new connections.
* @param iid The initial message identifier. Input parameter.
* @param icall The initial message call structure. Input parameter.
*/
void socket_connection( ipc_callid_t iid, ipc_call_t * icall );
 
/** Sends message to the socket parent module with specified data.
* @param socket_id Socket identifier. Input parameter.
* @param message The action message. Input parameter.
* @param arg2 The second message parameter. Input parameter.
* @param data The data to be sent. Input parameter.
* @param datalength The data length. Input parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the data parameter is NULL.
* @returns NO_DATA if the datalength parameter is zero (0).
* @returns Other error codes as defined for the spcific message.
*/
int socket_send_data( int socket_id, int message, ipcarg_t arg2, const void * data, size_t datalength );
 
/** Initializes a new socket specific data.
* @param socket The socket to be initialized. Input/output parameter.
* @param socket_id The new socket identifier. Input parameter.
* @param phone The parent module phone. Input parameter.
* @param service The parent module service. Input parameter.
*/
void socket_initialize( socket_ref socket, int socket_id, int phone, services_t service );
 
/** Clears and destroys the socket.
* @param socket The socket to be destroyed. Input parameter.
*/
void socket_destroy( socket_ref socket );
 
/** Receives data via the socket.
* @param message The action message. Input parameter.
* @param socket_id Socket identifier. Input parameter.
* @param data The data buffer to be filled. Output parameter.
* @param datalength The data length. Input parameter.
* @param flags Various receive flags. Input parameter.
* @param fromaddr The source address. May be NULL for connected sockets. Output parameter.
* @param addrlen The address length. The maximum address length is read. The actual address length is set. Used only if fromaddr is not NULL. Input/output parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the data parameter is NULL.
* @returns NO_DATA if the datalength or addrlen parameter is zero (0).
* @returns Other error codes as defined for the spcific message.
*/
int recvfrom_core( int message, int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen );
 
/** Sends data via the socket to the remote address.
* Binds the socket to a free port if not already connected/bound.
* @param message The action message. Input parameter.
* @param socket_id Socket identifier. Input parameter.
* @param data The data to be sent. Input parameter.
* @param datalength The data length. Input parameter.
* @param flags Various send flags. Input parameter.
* @param toaddr The destination address. May be NULL for connected sockets. Input parameter.
* @param addrlen The address length. Used only if toaddr is not NULL. Input parameter.
* @returns EOK on success.
* @returns ENOTSOCK if the socket is not found.
* @returns EBADMEM if the data or toaddr parameter is NULL.
* @returns NO_DATA if the datalength or the addrlen parameter is zero (0).
* @returns Other error codes as defined for the NET_SOCKET_SENDTO message.
*/
int sendto_core( int message, int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen );
 
static int socket_get_tcp_phone(){
149,7 → 277,7
switch( IPC_GET_METHOD( call )){
case NET_SOCKET_RECEIVED:
// find the socket
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call ));
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( call ));
if( ! socket ){
ERROR_CODE = ENOTSOCK;
break;
156,7 → 284,7
}
fibril_mutex_lock( & socket->receive_lock );
// push the number of received packet fragments
if( ! ERROR_OCCURRED( dyn_fifo_push( & socket->received, SOCKET_GET_DATA_FRAGMENTS( & call ), SOCKET_MAX_RECEIVED_SIZE ))){
if( ! ERROR_OCCURRED( dyn_fifo_push( & socket->received, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_MAX_RECEIVED_SIZE ))){
// signal the received packet
fibril_condvar_signal( & socket->receive_signal );
}
164,7 → 292,7
break;
case NET_SOCKET_ACCEPTED:
// find the socket
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call ));
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( call ));
if( ! socket ){
ERROR_CODE = ENOTSOCK;
break;
175,7 → 303,7
ERROR_CODE = ENOMEM;
break;
}
socket_initialize( new_socket, SOCKET_GET_SOCKET_ID( & call ), socket->phone, socket->service );
socket_initialize( new_socket, SOCKET_GET_SOCKET_ID( call ), socket->phone, socket->service );
ERROR_CODE = sockets_add( socket_get_sockets(), new_socket->socket_id, new_socket );
if( ERROR_CODE < 0 ){
free( new_socket );
195,13 → 323,13
break;
case NET_SOCKET_DATA_FRAGMENT_SIZE:
// find the socket
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call ));
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( call ));
if( ! socket ){
ERROR_CODE = ENOTSOCK;
break;
}
// set the data fragment size
socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE( & call );
socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE( call );
ERROR_CODE = EOK;
break;
default:
327,8 → 455,7
aid_t message_id;
int result;
 
if( ! cliaddr ) return EBADMEM;
if( ! addrlen ) return NO_DATA;
if(( ! cliaddr ) || ( ! addrlen )) return EBADMEM;
// find the socket
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
373,7 → 500,7
int accepted_id;
 
// destroy all accepted sockets
while(( accepted_id = dyn_fifo_pop( & socket->accepted ))){
while(( accepted_id = dyn_fifo_pop( & socket->accepted )) >= 0 ){
socket_destroy( sockets_find( socket_get_sockets(), accepted_id ));
}
dyn_fifo_destroy( & socket->received );
454,7 → 581,7
 
if( ! data ) return EBADMEM;
if( ! datalength ) return NO_DATA;
if( fromaddr && (( ! addrlen ) || ( * addrlen < sizeof( struct sockaddr_in )))) return EINVAL;
if( fromaddr && ( ! addrlen )) return EINVAL;
// find the socket
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
500,9 → 627,9
// dequeue the received packet
dyn_fifo_pop( & socket->received );
// return read data length
result = SOCKET_GET_READ_DATA_LENGTH( & answer );
result = SOCKET_GET_READ_DATA_LENGTH( answer );
// set address length
if( fromaddr && addrlen ) * addrlen = SOCKET_GET_ADDRESS_LENGTH( & answer );
if( fromaddr && addrlen ) * addrlen = SOCKET_GET_ADDRESS_LENGTH( answer );
}
fibril_mutex_unlock( & socket->receive_lock );
return result;