Subversion Repositories HelenOS

Compare Revisions

No changes between revisions

Ignore whitespace Rev 4577 → Rev 4578

/branches/network/uspace/srv/net/inet.c
38,6 → 38,8
#include <mem.h>
#include <stdio.h>
 
#include "include/in.h"
#include "include/in6.h"
#include "include/inet.h"
#include "include/socket.h"
 
/branches/network/uspace/srv/net/include/netdb.h
0,0 → 1,78
/*
* 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 netdb
* @{
*/
 
/** @file
* Structures and interfaces according to the BSD netdb.h file.
*/
 
#ifndef __NET_NETDB_H__
#define __NET_NETDB_H__
 
#include <sys/types.h>
 
/** Structure returned by network data base library.
* All addresses are supplied in host order, and returned in network order (suitable for use in system calls).
*/
struct hostent {
/** Official host name.
*/
char * h_name;
/** Alias list.
*/
char ** h_aliases;
/** Host address type.
*/
int h_addrtype;
/** Address length.
*/
int h_length;
/** List of addresses from name server.
*/
char ** h_addr_list;
/** Address, for backward compatiblity.
*/
#define h_addr h_addr_list[ 0 ]
};
 
#define HOST_NOT_FOUND 1 /* Authoritative Answer Host not found */
#define TRY_AGAIN 2 /* Non-Authoritive Host not found, or SERVERFAIL */
#define NO_RECOVERY 3 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
#define NO_DATA 4 /* Valid name, no data record of requested type */
#define NO_ADDRESS NO_DATA /* no address, look for MX record */
 
struct hostent * gethostbyaddr( const void * address, int len, int type );
struct hostent * gethostbyname( const char * name );
 
#endif
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/include/socket_errno.h
0,0 → 1,92
/*
* 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 net
* @{
*/
 
/** @file
* Socket error codes.
* Based on BSD.
*/
 
#ifndef __NET_SOCKET_ERR_H__
#define __NET_SOCKET_ERR_H__
 
#include <errno.h>
 
////#define EINTR (-10004)
////#define EBADF (-10009)
//#define EACCES (-10013)
//#define EFAULT (-10014)
////#define EINVAL (-10022)
////#define EMFILE (-10024)
//#define EWOULDBLOCK (-10035)
/** If any API function is called while a blocking function is in progress.
*/
//#define EINPROGRESS (-10036)
//#define EALREADY (-10037)
#define ENOTSOCK (-10038)
//#define EDESTADDRREQ (-10039)
//#define EMSGSIZE (-10040)
//#define EPROTOTYPE (-10041)
//#define ENOPROTOOPT (-10042)
#define EPROTONOSUPPORT (-10043)
#define ESOCKTNOSUPPORT (-10044)
//#define EOPNOTSUPP (-10045)
#define EPFNOSUPPORT (-10046)
#define EAFNOSUPPORT (-10047)
#define EADDRINUSE (-10048)
//#define EADDRNOTAVAIL (-10049)
/** May be reported at any time if the implementation detects an underlying failure.
*/
//#define ENETDOWN (-10050)
//#define ENETUNREACH (-10051)
//#define ENETRESET (-10052)
//#define ECONNABORTED (-10053)
//#define ECONNRESET (-10054)
//#define ENOBUFS (-10055)
//#define EISCONN (-10056)
//#define ENOTCONN (-10057)
//#define ESHUTDOWN (-10058)
//#define ETOOMANYREFS (-10059)
//#define ETIMEDOUT (-10060)
//#define ECONNREFUSED (-10061)
//#define ELOOP (-10062)
////#define ENAMETOOLONG (-10063)
//#define EHOSTDOWN (-10064)
//#define EHOSTUNREACH (-10065)
//#define HOST_NOT_FOUND (-11001)
//#define TRY_AGAIN (-11002)
//#define NO_RECOVERY (-11003)
#define NO_DATA (-11004)
 
#endif
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/include/socket.h
26,18 → 26,24
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
/** @addtogroup socket
* @{
*/
 
/** @file
* Common socket constants.
* Based on the linux src/include/linux/socket.h header file.
* Socket application program interface (API).
* This is a part of the network application library.
* Based on the linux src/include/linux/socket.h header file and BSD socket interface.
*/
 
#ifndef __NET_SOCKET_H__
#define __NET_SOCKET_H__
 
#include <sys/types.h>
 
#include "in.h"
#include "inet.h"
 
/* Supported address families. */
/** Unspecified address family.
*/
172,7 → 178,7
#define AF_MAX 34
 
/* Protocol families, same as address families. */
/*
 
#define PF_UNSPEC AF_UNSPEC
#define PF_UNIX AF_UNIX
#define PF_LOCAL AF_LOCAL
207,9 → 213,9
#define PF_IUCV AF_IUCV
#define PF_RXRPC AF_RXRPC
#define PF_MAX AF_MAX
*/
 
/* Setsockoptions(2) level. Thanks to BSD these must match IPPROTO_xxx */
/*#define SOL_IP 0
#define SOL_IP 0
#define SOL_ICMP 1
#define SOL_TCP 6
#define SOL_UDP 17
216,8 → 222,8
#define SOL_IPV6 41
#define SOL_ICMPV6 58
#define SOL_SCTP 132
#define SOL_UDPLITE 136 *//* UDP-Lite (RFC 3828) */
/*#define SOL_RAW 255
#define SOL_UDPLITE 136 /* UDP-Lite (RFC 3828) */
#define SOL_RAW 255
#define SOL_IPX 256
#define SOL_AX25 257
#define SOL_ATALK 258
226,9 → 232,9
#define SOL_DECNET 261
#define SOL_X25 262
#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
#define SOL_ATM 264 /* ATM layer (cell level) */
#define SOL_AAL 265 /* ATM Adaption Layer (packet level) */
#define SOL_IRDA 266
#define SOL_NETBEUI 267
#define SOL_LLC 268
#define SOL_DCCP 269
237,12 → 243,45
#define SOL_RXRPC 272
#define SOL_PPPOL2TP 273
#define SOL_BLUETOOTH 274
*/
 
//
/* * IPX options.
*/
//#define IPX_TYPE 1
 
/** Socket type type definition.
*/
typedef enum sock_type{
/** Stream (connection oriented) socket.
*/
SOCK_STREAM = 1,
/** Datagram (connectionless oriented) socket.
*/
SOCK_DGRAM = 2,
/** Raw socket.
*/
SOCK_RAW = 3
} sock_type_t;
 
typedef int32_t socklen_t;
 
int socket( int domain, int type, int protocol );
int bind( int socket_id, const struct sockaddr * my_addr, socklen_t addrlen );
int listen( int socket_id, int backlog );
int accept( int socket_id, struct sockaddr * cliaddr, socklen_t * addrlen );
int connect( int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen );
 
int closesocket( int socket_id );
 
int send( int socket_id, void * data, size_t datalength, int flags );
int sendto( int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen );
 
int recv( int socket_id, void * data, size_t datalength, int flags );
int recvfrom( int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen );
 
int getsockopt( int socket_id, int level, int optname, void * value, size_t * optlen );
int setsockopt( int socket_id, int level, int optname, const void * value, size_t optlen );
 
#endif
 
/** @}
/branches/network/uspace/srv/net/net/Makefile
59,14 → 59,13
$(NET_BASE)il/ip/ip_client.c \
$(NET_BASE)inet.c \
$(NET_BASE)tl/tcp/tcp.c \
$(NET_BASE)tl/udp/udp.c
$(NET_BASE)tl/udp/udp.c \
$(NET_BASE)socket/socket_core.c
 
else
 
SOURCES += $(NAME)_standalone.c \
$(NET_BASE)il/arp/arp_remote.c \
$(NET_BASE)il/ip/ip_remote.c \
# $(NET_BASE)tl/tcp/tcp_remote.c
$(NET_BASE)il/ip/ip_remote.c
 
endif
 
/branches/network/uspace/srv/net/socket/socket_core.c
0,0 → 1,152
/*
* 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 socket
* @{
*/
 
/** @file
*/
 
#include "../err.h"
 
#include "../include/in.h"
#include "../include/inet.h"
 
#include "../include/socket.h"
#include "../include/socket_errno.h"
 
#include "../structures/int_map.h"
#include "../structures/packet/packet.h"
#include "../structures/packet/packet_client.h"
 
#include "socket_core.h"
 
INT_MAP_IMPLEMENT( socket_cores, socket_core_t );
 
INT_MAP_IMPLEMENT( socket_ports, socket_core_ref );
 
int socket_bind( socket_cores_ref local_sockets, socket_ports_ref global_sockets, int socket_id, void * addr, size_t addrlen ){
ERROR_DECLARE;
 
socket_core_ref socket;
socket_core_ref * socket_pointer;
struct sockaddr * address;
struct sockaddr_in * address_in;
 
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;
socket_pointer = socket_ports_find( global_sockets, address_in->sin_port );
if( * socket_pointer ){
return EADDRINUSE;
}
socket_ports_exclude( global_sockets, socket->port );
socket_pointer = ( socket_core_ref * ) malloc( sizeof( * socket_pointer ));
if( ! socket_pointer ) return ENOMEM;
* socket_pointer = socket;
ERROR_PROPAGATE( socket_ports_add( global_sockets, address_in->sin_port, socket_pointer ));
socket->port = address_in->sin_port;
break;
// TODO IPv6
default:
return EAFNOSUPPORT;
}
return EOK;
}
 
int socket_create( socket_cores_ref local_sockets, int app_phone ){
socket_core_ref socket;
int res;
 
socket = ( socket_core_ref ) malloc( sizeof( * socket ));
if( ! socket ) return ENOMEM;
socket->phone = app_phone;
socket->port = -1;
socket->device_id = -1;
socket->peer_addr = NULL;
socket->connect_size = 2;
socket->receive_size = 8;
socket->connected = ( int * ) malloc( sizeof( int ) * socket->connect_size );
if( ! socket->connected ){
free( socket );
return ENOMEM;
}
bzero( socket->connected, sizeof( int ) * socket->connect_size );
socket->received = ( int * ) malloc( sizeof( int ) * socket->receive_size );
if( ! socket->received ){
free( socket->connected );
free( socket );
return ENOMEM;
}
bzero( socket->received, sizeof( int ) * socket->receive_size );
socket->socket_id = socket_cores_count( local_sockets ) + 1;
res = socket_cores_add( local_sockets, socket->socket_id, socket );
if( res < 0 ){
free( socket->received );
free( socket->connected );
free( socket );
return res;
}
return socket->socket_id;
}
 
int socket_destroy( int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets ){
socket_core_ref socket;
size_t i;
 
socket = socket_cores_find( local_sockets, socket_id );
if( ! socket ) return ENOTSOCK;
socket_ports_exclude( global_sockets, socket->port );
if( socket->connected ){
for( i = 0; i < socket->connect_size; ++ i ){
if( socket->connected[ i ] ){
socket_destroy( packet_phone, socket->connected[ i ], local_sockets, global_sockets );
}
}
}
if( socket->received ){
for( i = 0; i < socket->receive_size; ++ i ){
if( socket->received[ i ] ){
pq_release( packet_phone, socket->received[ i ] );
}
}
}
if( socket->connected ) free( socket->connected );
if( socket->received ) free( socket->received );
socket_cores_exclude( local_sockets, socket_id );
return EOK;
}
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/socket/socket_messages.h
0,0 → 1,71
/*
* 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 socket
* @{
*/
 
/** @file
*/
 
#ifndef __NET_SOCKET_MESSAGES_H__
#define __NET_SOCKET_MESSAGES_H__
 
#include <ipc/ipc.h>
 
#include "../messages.h"
 
typedef enum{
NET_SOCKET = NET_SOCKET_FIRST,
NET_SOCKET_BIND,
NET_SOCKET_LISTEN,
NET_SOCKET_ACCEPT,
NET_SOCKET_CONNECT,
NET_SOCKET_CLOSE,
NET_SOCKET_SEND,
NET_SOCKET_SENDTO,
NET_SOCKET_RECV,
NET_SOCKET_RECVFROM,
NET_SOCKET_GETSOCKOPT,
NET_SOCKET_SETSOCKOPT,
NET_SOCKET_ACCEPTED,
NET_SOCKET_RECEIVED
} socket_messages;
 
#define SOCKET_GET_SOCKET_ID( call ) ( int ) IPC_GET_ARG1( * call )
 
#define SOCKET_GET_NEW_SOCKET_ID( call ) ( int ) IPC_GET_ARG2( * call )
#define SOCKET_GET_BACKLOG( call ) ( int ) IPC_GET_ARG2( * call )
//#define SOCKET_GET_OPT_LEVEL( call ) ( int ) IPC_GET_ARG2( * call )
#define SOCKET_GET_OPT_NAME( call ) ( int ) IPC_GET_ARG2( * call )
#define SOCKET_GET_FLAGS( call ) ( int ) IPC_GET_ARG2( * call )
 
#endif
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/socket/socket_core.h
0,0 → 1,74
/*
* 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 socket
* @{
*/
 
/** @file
*/
 
#ifndef __NET_SOCKET_CORE_H__
#define __NET_SOCKET_CORE_H__
 
#include <sys/types.h>
 
#include "../include/in.h"
#include "../include/device.h"
 
#include "../structures/int_map.h"
#include "../structures/packet/packet.h"
 
typedef struct socket_core socket_core_t;
typedef socket_core_t * socket_core_ref;
 
struct socket_core{
int socket_id;
int phone;
int port;
int * connected;
size_t connect_size;
packet_id_t * received;
size_t receive_size;
size_t mtu;
device_id_t device_id;
struct sockaddr_in * peer_addr;
};
 
INT_MAP_DECLARE( socket_cores, socket_core_t );
 
INT_MAP_DECLARE( socket_ports, socket_core_ref );
 
int socket_bind( socket_cores_ref local_sockets, socket_ports_ref global_sockets, int socket_id, void * addr, size_t addrlen );
int socket_create( socket_cores_ref local_sockets, int app_phone );
int socket_destroy( int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets );
 
#endif
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/socket/socket_client.c
0,0 → 1,399
/*
* 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 socket
* @{
*/
 
/** @file
* Socket application program interface (API) implementation.
* @see socket.h for more information.
* This is a part of the network application library.
*/
 
#include <assert.h>
#include <async.h>
//#include <fibril_sync.h>
 
#include <ipc/services.h>
 
#include "../err.h"
#include "../modules.h"
 
#include "../include/in.h"
#include "../include/socket.h"
#include "../include/socket_errno.h"
 
#include "../structures/int_map.h"
 
#include "socket_messages.h"
 
typedef struct socket socket_t;
typedef socket_t * socket_ref;
 
struct socket{
int socket_id;
int phone;
services_t service;
int max_content;
int received;
// fibril_mutex_t receive_lock;
// fibril_condvar_t receive_signal;
int accepted;
// fibril_mutex_t accept_lock;
// fibril_condvar_t accept_signal;
};
 
INT_MAP_DECLARE( sockets, socket_t );
 
static struct{
int tcp_phone;
int udp_phone;
sockets_ref sockets;
} socket_globals = { -1, -1, NULL };
 
INT_MAP_IMPLEMENT( sockets, socket_t );
 
static int socket_get_tcp_phone();
static int socket_get_tcp_phone();
static sockets_ref socket_get_sockets();
void socket_connection( ipc_callid_t iid, ipc_call_t * icall );
int socket_send_data( int socket_id, int message, ipcarg_t arg2, const void * data, size_t datalength );
void socket_initialize( socket_ref socket, int socket_id, int phone, services_t service );
 
static int socket_get_tcp_phone(){
if( socket_globals.tcp_phone < 0 ){
socket_globals.tcp_phone = bind_service( SERVICE_TCP, 0, 0, SERVICE_TCP, socket_connection );
}
return socket_globals.tcp_phone;
}
 
static int socket_get_udp_phone(){
if( socket_globals.udp_phone < 0 ){
socket_globals.udp_phone = bind_service( SERVICE_UDP, 0, 0, SERVICE_UDP, socket_connection );
}
return socket_globals.udp_phone;
}
 
static sockets_ref socket_get_sockets(){
if( ! socket_globals.sockets ){
socket_globals.sockets = ( sockets_ref ) malloc( sizeof( sockets_t ));
if( ! socket_globals.sockets ) return NULL;
if( sockets_initialize( socket_globals.sockets ) != EOK ){
free( socket_globals.sockets );
socket_globals.sockets = NULL;
}
}
return socket_globals.sockets;
}
 
void socket_initialize( socket_ref socket, int socket_id, int phone, services_t service ){
socket->socket_id = socket_id;
socket->phone = phone;
socket->service = service;
socket->received = 0;
socket->accepted = 0;
// fibril_mutex_initialize( & socket->receive_lock );
// fibril_condvar_initialize( & socket->receive_signal );
// fibril_mutex_initialize( & socket->accept_lock );
// fibril_condvar_initialize( & socket->accept_signal );
}
 
void socket_connection( ipc_callid_t iid, ipc_call_t * icall ){
ERROR_DECLARE;
 
ipc_callid_t callid;
ipc_call_t call;
socket_ref socket;
socket_ref new_socket;
 
while( true ){
 
callid = async_get_call( & call );
switch( IPC_GET_METHOD( call )){
case NET_SOCKET_RECEIVED:
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call ));
if( ! socket ){
ERROR_CODE = ENOTSOCK;
break;
}
// fibril_mutex_lock( & socket->receive_lock );
++ socket->received;
// fibril_condvar_signal( & socket->receive_signal );
// fibril_mutex_unlock( & socket->receive_lock );
ERROR_CODE = EOK;
break;
case NET_SOCKET_ACCEPTED:
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call ));
if( ! socket ){
ERROR_CODE = ENOTSOCK;
break;
}
// fibril_mutex_lock( & socket->accept_lock );
new_socket = ( socket_ref ) malloc( sizeof( socket_t ));
if( ! new_socket ){
ERROR_CODE = ENOMEM;
break;
}
socket_initialize( new_socket, SOCKET_GET_NEW_SOCKET_ID( & call ), socket->phone, socket->service );
if( ERROR_OCCURRED( sockets_add( socket_get_sockets(), new_socket->socket_id, new_socket ))){
break;
}
++ socket->accepted;
// fibril_condvar_signal( & socket->accept_signal );
// fibril_mutex_unlock( & socket->accept_lock );
break;
/* case NET_SOCKET_MTU:
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call ));
if( ! socket ){
ERROR_CODE = ENOTSOCK;
break;
}
socket->mtu =
ERROR_CODE = EOK;
break;
*/ default:
ERROR_CODE = ENOTSUP;
}
ipc_answer_0( callid, ERROR_CODE );
}
}
 
int socket( int domain, int type, int protocol ){
socket_ref socket;
int phone;
int socket_id;
services_t service;
 
switch( domain ){
case PF_INET:
switch( type ){
case SOCK_STREAM:
if( ! protocol ) protocol = IPPROTO_TCP;
switch( protocol ){
case IPPROTO_TCP:
phone = socket_get_tcp_phone();
service = SERVICE_TCP;
break;
default:
return EPROTONOSUPPORT;
}
break;
case SOCK_DGRAM:
if( ! protocol ) protocol = IPPROTO_UDP;
switch( protocol ){
case IPPROTO_UDP:
phone = socket_get_udp_phone();
service = SERVICE_UDP;
break;
default:
return EPROTONOSUPPORT;
}
break;
case SOCK_RAW:
default:
return ESOCKTNOSUPPORT;
}
break;
// TODO IPv6
default:
return EPFNOSUPPORT;
}
assert( phone );
socket = ( socket_ref ) malloc( sizeof( socket_t ));
if( ! socket ) return ENOMEM;
socket_id = async_req_3_0( phone, NET_SOCKET, 0, 0, service );
if( socket_id > 0 ){
socket_initialize( socket, socket_id, phone, service );
if( sockets_add( socket_get_sockets(), socket_id, socket ) != EOK ){
free( socket );
async_msg_3( phone, NET_SOCKET_CLOSE, socket_id, 0, service );
}
}else{
free( socket );
}
return socket_id;
}
 
int socket_send_data( int socket_id, int message, ipcarg_t arg2, const void * data, size_t datalength ){
socket_ref socket;
aid_t message_id;
ipcarg_t result;
 
if( ! data ) return EBADMEM;
if( ! datalength ) return NO_DATA;
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
message_id = async_send_3( socket->phone, message, socket->socket_id, arg2, socket->service, NULL );
ipc_data_write_start( socket->phone, data, datalength );
async_wait_for( message_id, & result );
return ( int ) result;
}
 
int bind( int socket_id, const struct sockaddr * my_addr, socklen_t addrlen ){
return socket_send_data( socket_id, NET_SOCKET_BIND, 0, my_addr, addrlen );
}
 
int listen( int socket_id, int backlog ){
socket_ref socket;
 
if( backlog <= 0 ) return EINVAL;
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
return async_req_3_0( socket->phone, NET_SOCKET_LISTEN, socket->socket_id, backlog, socket->service );
}
 
int accept( int socket_id, struct sockaddr * cliaddr, socklen_t * addrlen ){
socket_ref socket;
aid_t message_id;
ipcarg_t result;
 
if( ! cliaddr ) return EBADMEM;
if( ! addrlen ) return NO_DATA;
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
// fibril_mutex_lock( & socket->accept_lock );
while( socket->accepted <= 0 ){
// fibril_condvar_wait( & socket->accept_signal, & socket->accept_lock );
}
message_id = async_send_3( socket->phone, NET_SOCKET_ACCEPT, socket->socket_id, 0, socket->service, NULL );
ipc_data_read_start( socket->phone, cliaddr, * addrlen );
async_wait_for( message_id, & result );
if( result > 0 ){
-- socket->accepted;
}
// fibril_mutex_unlock( & socket->accept_lock );
return ( int ) result;
}
 
int connect( int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen ){
return socket_send_data( socket_id, NET_SOCKET_CONNECT, 0, serv_addr, addrlen );
}
 
int closesocket( int socket_id ){
socket_ref socket;
 
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
return async_req_3_0( socket->phone, NET_SOCKET_CLOSE, socket->socket_id, 0, socket->service );
}
 
int send( int socket_id, void * data, size_t datalength, int flags ){
return socket_send_data( socket_id, NET_SOCKET_SEND, flags, data, datalength );
}
 
int sendto( int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen ){
socket_ref socket;
aid_t message_id;
ipcarg_t result;
 
if( ! toaddr ) return EBADMEM;
if( ! addrlen ) return NO_DATA;
if( ! data ) return EBADMEM;
if( ! datalength ) return NO_DATA;
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
message_id = async_send_3( socket->phone, NET_SOCKET_SENDTO, socket->socket_id, flags, socket->service, NULL );
if( ipc_data_write_start( socket->phone, toaddr, addrlen ) == EOK ){
ipc_data_write_start( socket->phone, data, datalength );
}
async_wait_for( message_id, & result );
return ( int ) result;
}
 
int recv( int socket_id, void * data, size_t datalength, int flags ){
socket_ref socket;
aid_t message_id;
ipcarg_t result;
 
if( ! data ) return EBADMEM;
if( ! datalength ) return NO_DATA;
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
// fibril_mutex_lock( & socket->receive_lock );
while( socket->received <= 0 ){
// fibril_condvar_wait( & socket->receive_signal, & socket->receive_lock );
}
message_id = async_send_3( socket->phone, NET_SOCKET_RECV, socket->socket_id, flags, socket->service, NULL );
ipc_data_read_start( socket->phone, data, datalength );
async_wait_for( message_id, & result );
if( result > 0 ){
-- socket->received;
}
// fibril_mutex_unlock( & socket->receive_lock );
return ( int ) result;
}
 
int recvfrom( int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen ){
socket_ref socket;
aid_t message_id;
ipcarg_t result;
 
if( ! fromaddr ) return EBADMEM;
if( ! addrlen ) return NO_DATA;
if( ! data ) return EBADMEM;
if( ! datalength ) return NO_DATA;
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
// fibril_mutex_lock( & socket->receive_lock );
while( socket->received <= 0 ){
// fibril_condvar_wait( & socket->receive_signal, & socket->receive_lock );
}
message_id = async_send_3( socket->phone, NET_SOCKET_RECVFROM, socket->socket_id, flags, socket->service, NULL );
if( ipc_data_read_start( socket->phone, fromaddr, * addrlen ) == EOK ){
ipc_data_read_start( socket->phone, data, datalength );
}
async_wait_for( message_id, & result );
if( result > 0 ){
-- socket->received;
}
// fibril_mutex_unlock( & socket->receive_lock );
return ( int ) result;
}
 
int getsockopt( int socket_id, int level, int optname, void * value, size_t * optlen ){
socket_ref socket;
aid_t message_id;
ipcarg_t result;
 
if( ! value ) return EBADMEM;
if( ! optlen ) return NO_DATA;
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
message_id = async_send_3( socket->phone, NET_SOCKET_GETSOCKOPT, socket->socket_id, optname, socket->service, NULL );
ipc_data_read_start( socket->phone, value, * optlen );
async_wait_for( message_id, & result );
return ( int ) result;
}
 
int setsockopt( int socket_id, int level, int optname, const void * value, size_t optlen ){
return socket_send_data( socket_id, NET_SOCKET_SETSOCKOPT, optname, value, optlen );
}
 
/** @}
*/
Property changes:
Added: svn:mergeinfo
/branches/network/uspace/srv/net/socket/Makefile
0,0 → 1,42
#
# 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 = socket
 
NET_BASE = ../
STRUCTURES = $(NET_BASE)structures/
 
## Sources
#
 
LIB = lib$(NAME).a
SOURCES = \
$(NAME)_client.c \
$(NET_BASE)inet.c
 
include $(NET_BASE)Makefile.module
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
/branches/network/uspace/srv/net/messages.h
59,7 → 59,7
#define NET_UDP_COUNT 0
#define NET_TCP_COUNT 0
#define NET_PACKET_COUNT 5
#define NET_SOCKET_COUNT 0
#define NET_SOCKET_COUNT 14
 
#define NET_FIRST 2000
 
/branches/network/uspace/srv/net/Makefile.module
47,7 → 47,7
 
.PHONY: all clean depend disasm
 
all: $(OUTPUT) $(DISASMS)
all: $(OUTPUT) $(LIB) $(DISASMS)
 
check:
$(CC) $(DEFS) $(CFLAGS) $(CHECK_CFLAGS) *.c
63,6 → 63,9
$(OUTPUT): $(OBJECTS) $(LIBS)
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $@.map
 
$(LIB): $(OBJECTS)
$(AR) rc $(LIB) $(OBJECTS)
 
disasm: $(DISASMS)
 
%.disasm: %
/branches/network/uspace/srv/net/Makefile
33,7 → 33,8
netif/dp8390 \
nil/eth \
net \
net/start
net/start \
socket
 
DIRS_MODULAR = il/ip \
il/arp \
41,7 → 42,6
tl/tcp \
# il/rarp \
# il/icmp \
# socket
 
DIRS_ALL = $(DIRS) $(DIRS_MODULAR)