os/ossrv/genericopenlibs/cstdlib/LPOSIX/NETCALLS.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LPOSIX/NETCALLS.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,342 @@
     1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// connectors for re-entrant networking system calls
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include "SYSIF.H"
    1.22 +#include "LPOSIX.H"
    1.23 +
    1.24 +#include <reent.h>
    1.25 +#include <sys/socket.h>
    1.26 +
    1.27 +extern "C" {
    1.28 +
    1.29 +/**
    1.30 +@param family Specifies the address family used in the communications domain.	
    1.31 +@param style Type of socket.
    1.32 +@param protocol Protocol used with that socket.
    1.33 +
    1.34 +@return On Success, non-negative integer, the socket file descriptor.
    1.35 +		On Failure, returns -1, eerno may be set.
    1.36 +*/
    1.37 +EXPORT_C int socket (int family, int style, int protocol)
    1.38 +	{
    1.39 +	struct _reent *r = _REENT2;
    1.40 +	if (!r)
    1.41 +		return -1; // Memory for library globals is not allocated (errno not set).
    1.42 +	return _socket_r (r, family, style, protocol);
    1.43 +	}
    1.44 +EXPORT_C int _socket_r (struct _reent *r, int family, int style, int protocol)
    1.45 +	{
    1.46 +	MSystemInterface& sysIf=Interface(r);
    1.47 +	return sysIf.socket(family,style,protocol,r->_errno);
    1.48 +	}
    1.49 +
    1.50 +/**
    1.51 +Receives a message from a socket. 
    1.52 +The recv() call can be used on a connection mode socket or a bound, 
    1.53 +connectionless socket. If no messages are available at the socket, 
    1.54 +the recv() call waits for a message to arrive unless the socket is nonblocking. 
    1.55 +
    1.56 +@param fd Specifies a socket descriptor to use for the send.
    1.57 +@param buf Points to the buffer containing message to send.
    1.58 +@param cnt Specifies the length of the message in bytes.
    1.59 +@param flags Lets the sender control the way data is sent. 
    1.60 +
    1.61 +@return On Success, returns the number of bytes received.
    1.62 +		On Failure, returns -1, eerno may be set.
    1.63 +*/
    1.64 +EXPORT_C int recv (int fd, char *buf, size_t cnt, int flags)
    1.65 +	{
    1.66 +	struct _reent *r = _REENT2;
    1.67 +	if (!r)
    1.68 +		return -1; // Memory for library globals is not allocated (errno not set).
    1.69 +	return _recvfrom_r (r, fd, buf, cnt, flags, 0, 0);
    1.70 +	}
    1.71 +
    1.72 +/**
    1.73 +@return On Succcess, returns length of message in bytes, can be 0. 
    1.74 +		On Failure, returns -1, errno may be set.
    1.75 +*/	
    1.76 +EXPORT_C int recvfrom (int fd, char *buf, size_t cnt, int flags, struct sockaddr* from, size_t* fromsize)
    1.77 +	{
    1.78 +	struct _reent *r = _REENT2;
    1.79 +	if (!r)
    1.80 +		return -1; // Memory for library globals is not allocated (errno not set).
    1.81 +	return _recvfrom_r (r, fd, buf, cnt, flags, from ,fromsize);
    1.82 +	}
    1.83 +EXPORT_C int _recvfrom_r (struct _reent *r, int fd, char *buf, size_t nbyte, int flags, struct sockaddr* from, size_t* fromsize)
    1.84 +	{
    1.85 +	MSystemInterface& sysIf=Interface(r);
    1.86 +	return sysIf.recvfrom(fd,buf,nbyte,flags,from,(unsigned long*)fromsize,r->_errno);
    1.87 +	}
    1.88 +
    1.89 +/**
    1.90 +Initiates transmission of a message from the specified socket to its peer.
    1.91 +The send() function sends a message only when the socket is connected.
    1.92 +
    1.93 +@param fd Specifies a socket descriptor to use for the send.
    1.94 +@param buf Points to the buffer containing message to send.
    1.95 +@param cnt Specifies the length of the message in bytes.
    1.96 +@param flags Lets the sender control the way data is sent.
    1.97 +
    1.98 +@return On Success, returns the numbers of characters sent.
    1.99 +		On Failure, returns -1, errno may be set.
   1.100 +*/
   1.101 +EXPORT_C int send (int fd, const char *buf, size_t cnt, int flags)
   1.102 +	{
   1.103 +	struct _reent *r = _REENT2;
   1.104 +	if (!r)
   1.105 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.106 +	return _sendto_r (r, fd, buf, cnt, flags, 0, 0);
   1.107 +	}
   1.108 +
   1.109 +/**
   1.110 +@return	On Success, returns the numbers of characters sent.
   1.111 +		On Failure, returns -1, errno may be set.
   1.112 +*/	
   1.113 +EXPORT_C int sendto (int fd, const char *buf, size_t cnt, int flags, struct sockaddr* to, size_t tosize)
   1.114 +	{
   1.115 +	struct _reent *r = _REENT2;
   1.116 +	if (!r)
   1.117 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.118 +	return _sendto_r (r, fd, buf, cnt, flags, to, tosize);
   1.119 +	}
   1.120 +EXPORT_C int _sendto_r (struct _reent *r, int fd, const char *buf, size_t nbyte, int flags, struct sockaddr* to, size_t tosize)
   1.121 +	{
   1.122 +	MSystemInterface& sysIf=Interface(r);
   1.123 +	return sysIf.sendto(fd,buf,nbyte,flags,to,tosize,r->_errno);
   1.124 +	}
   1.125 +
   1.126 +/**
   1.127 +Shuts down all or part of a full-duplex connection on the socket associated with fd.
   1.128 +
   1.129 +@param fd Is the socket descriptor to shut down.
   1.130 +@param how Specifies the type of shutdown.
   1.131 +
   1.132 +@return On Success, returns 0.
   1.133 +		On Failure, returns -1, errno may be set.
   1.134 +*/
   1.135 +EXPORT_C int shutdown (int fd, int how)
   1.136 +	{
   1.137 +	struct _reent *r = _REENT2;
   1.138 +	if (!r)
   1.139 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.140 +	return _shutdown_r (r, fd, how);
   1.141 +	}
   1.142 +EXPORT_C int _shutdown_r (struct _reent *r, int fd, int how)
   1.143 +	{
   1.144 +	MSystemInterface& sysIf=Interface(r);
   1.145 +	return sysIf.shutdown(fd,how,r->_errno);
   1.146 +	}
   1.147 +
   1.148 +/**
   1.149 +Marks a connection-mode socket, specified by the socket argument fd,
   1.150 +as accepting connections, and limits the number of outstanding connections 
   1.151 +in the socket's listen queue to the value specified by the n argument. 
   1.152 +The socket fd is put into 'passive' mode where incoming connection 
   1.153 +requests are acknowledged and queued pending acceptance by the process.
   1.154 +
   1.155 +@param fd Is a descriptor identifying a bound, unconnected socket.
   1.156 +@param n Is the maximum length that the queue of pending connections
   1.157 +	   may grow to. If this value is SOMAXCONN, then the underlying service provider
   1.158 +       responsible for socket fd sets the backlog to a maximum "reasonable" value.
   1.159 +
   1.160 +@return On Success, returns 0. 
   1.161 +		On Failure, returns -1, errno may be set.
   1.162 +*/
   1.163 +EXPORT_C int listen (int fd, int n)
   1.164 +	{
   1.165 +	struct _reent *r = _REENT2;
   1.166 +	if (!r)
   1.167 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.168 +	return _listen_r (r, fd, n);
   1.169 +	}
   1.170 +EXPORT_C int _listen_r (struct _reent *r, int fd, int n)
   1.171 +	{
   1.172 +	MSystemInterface& sysIf=Interface(r);
   1.173 +	return sysIf.listen(fd,n,r->_errno);
   1.174 +	}
   1.175 +
   1.176 +/**
   1.177 +accepts a connection on a socket. An incoming connection is acknowledged and associated with
   1.178 +an immediately created socket. The original socket is returned to the listening state.
   1.179 +
   1.180 +@param fd Is the integer descriptor of the desired socket.
   1.181 +@param addr Points to a sockaddr structure containing the socket address.
   1.182 +@param size Points to an integer that states the address length in bytes.
   1.183 +
   1.184 +@return On Success, returns a non-negative integer, which is a descriptor of the accepted socket.
   1.185 +        Upon return, addrlen contains the actual length in bytes of the address returned. 
   1.186 +		On Failure, returns -1, errno may be set.
   1.187 +*/
   1.188 +EXPORT_C int accept (int fd, struct sockaddr *addr, size_t *size)
   1.189 +	{
   1.190 +	struct _reent *r = _REENT2;
   1.191 +	if (!r)
   1.192 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.193 +	return _accept_r (r, fd, addr, size);
   1.194 +	}
   1.195 +EXPORT_C int _accept_r (struct _reent *r, int fd, struct sockaddr *addr, size_t *size)
   1.196 +	{
   1.197 +	MSystemInterface& sysIf=Interface(r);
   1.198 +	int newfd=sysIf.accept(fd,r->_errno);
   1.199 +	if (newfd>=0)
   1.200 +		sysIf.sockname(newfd,addr,(unsigned long*)size,1,r->_errno);	// getpeername
   1.201 +	return newfd;
   1.202 +	}
   1.203 +
   1.204 +/**
   1.205 +Associate that socket with a port.
   1.206 +
   1.207 +@param fd Is the integer descriptor of the desired socket.
   1.208 +@param addr Points to a sockaddr structure containing the socket address.
   1.209 +@param size Points to an integer that states the address length in bytes.
   1.210 +
   1.211 +@return	On Success, returns 0. 
   1.212 +		On Failure, returns -1, errno may be set.
   1.213 +*/
   1.214 +EXPORT_C int bind (int fd, struct sockaddr *addr, size_t size)
   1.215 +	{
   1.216 +	struct _reent *r = _REENT2;
   1.217 +	if (!r)
   1.218 +		return -1; // Memory for library globals is not allocated (errno not set).	
   1.219 +	return _bind_r (r, fd, addr, size);
   1.220 +	}
   1.221 +EXPORT_C int _bind_r (struct _reent *r, int fd, struct sockaddr *addr, size_t size)
   1.222 +	{
   1.223 +	MSystemInterface& sysIf=Interface(r);
   1.224 +	return sysIf.bind(fd,addr,size,r->_errno);
   1.225 +	}
   1.226 +
   1.227 +/**
   1.228 +Used by a client program to establish communication with a remote entity
   1.229 +
   1.230 +@param fd Is the integer descriptor of the desired socket.
   1.231 +@param addr Points to a sockaddr structure containing the socket address.
   1.232 +@param size Points to an integer that states the address length in bytes.
   1.233 +
   1.234 +@return	On Success, returns 0. 
   1.235 +		On Failure, returns -1, errno may be set.
   1.236 +*/
   1.237 +EXPORT_C int connect (int fd, struct sockaddr *addr, size_t size)
   1.238 +	{
   1.239 +	struct _reent *r = _REENT2;
   1.240 +	if (!r)
   1.241 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.242 +	return _connect_r (r, fd, addr, size);
   1.243 +	}
   1.244 +EXPORT_C int _connect_r (struct _reent *r, int fd, struct sockaddr *addr, size_t size)
   1.245 +	{
   1.246 +	MSystemInterface& sysIf=Interface(r);
   1.247 +	return sysIf.connect(fd,addr,size,r->_errno);
   1.248 +	}
   1.249 +
   1.250 +/**
   1.251 +Returns the current name for the specified socket. The namelen parameter should be initialized
   1.252 +to indicate the amount of space pointed to by name. When returned, namelen contains the actual
   1.253 +size (in bytes) of the name returned.
   1.254 +
   1.255 +@param fd Is the integer descriptor of the desired socket.
   1.256 +@param addr Points to a sockaddr structure containing the socket address.
   1.257 +@param size Points to an integer that states the address length in bytes.
   1.258 +
   1.259 +@return	On Success, returns 0. 
   1.260 +		On Failure, returns -1, errno may be set.
   1.261 +*/
   1.262 +EXPORT_C int getsockname (int fd, struct sockaddr *addr, size_t* size)
   1.263 +	{
   1.264 +	struct _reent *r = _REENT2;
   1.265 +	if (!r)
   1.266 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.267 +	return _getsockname_r (r, fd, addr, size);
   1.268 +	}
   1.269 +EXPORT_C int _getsockname_r (struct _reent *r, int fd, struct sockaddr *addr, size_t* size)
   1.270 +	{
   1.271 +	MSystemInterface& sysIf=Interface(r);
   1.272 +	return sysIf.sockname(fd,addr,(unsigned long*)size,0,r->_errno);
   1.273 +	}
   1.274 +
   1.275 +/**
   1.276 +Returns the peer address of the specified socket.
   1.277 +
   1.278 +@return	On Success, returns 0. 
   1.279 +		On Failure, returns -1, errno may be set.
   1.280 +*/
   1.281 +EXPORT_C int getpeername (int fd, struct sockaddr *addr, size_t* size)
   1.282 +	{
   1.283 +	struct _reent *r = _REENT2;
   1.284 +	if (!r)
   1.285 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.286 +	return _getpeername_r (r, fd, addr, size);
   1.287 +	}
   1.288 +EXPORT_C int _getpeername_r (struct _reent *r, int fd, struct sockaddr *addr, size_t* size)
   1.289 +	{
   1.290 +	MSystemInterface& sysIf=Interface(r);
   1.291 +	return sysIf.sockname(fd,addr,(unsigned long*)size,1,r->_errno);
   1.292 +	}
   1.293 +
   1.294 +/**
   1.295 +Manipulates options associated with a socket.
   1.296 +
   1.297 +@return	On Success, returns 0. 
   1.298 +   		On Failure, returns -1, errno may be set.
   1.299 +*/
   1.300 +EXPORT_C int getsockopt (int fd, int level, int opt, void* buf, size_t* len)
   1.301 +	{
   1.302 +	struct _reent *r = _REENT2;
   1.303 +	if (!r)
   1.304 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.305 +	return _getsockopt_r (r, fd, level, opt, buf, len);
   1.306 +	}
   1.307 +EXPORT_C int _getsockopt_r (struct _reent *r, int fd, int level, int opt, void* buf, size_t* len) 
   1.308 +	{
   1.309 +	MSystemInterface& sysIf=Interface(r);
   1.310 +	return sysIf.getsockopt(fd,level,opt,buf,(unsigned long*)len,r->_errno);
   1.311 +	}
   1.312 +
   1.313 +/**
   1.314 +Manipulates options associated with a socket. Options can exist at multiple protocol levels.
   1.315 +However, the options are always present at the uppermost socket level. Options affect socket
   1.316 +operations, such as the routing of packets, out-of-band data transfer, and so on.
   1.317 +
   1.318 +@param fd Specifies a socket for which an option should be set.
   1.319 +@param level Identifies whether the operation applies to the socket itself or to the underlying
   1.320 +	   protocol being used. The socket itself is identified by the symbolic constant SOL_SOCKET.
   1.321 +	   Other protocol levels require the protocol number for the appropriate protocol
   1.322 +	   controlling the option.
   1.323 +@param opt Specifies a single option to which the request applies, 
   1.324 +            negative option values are not support on Symbian OS.
   1.325 +@param buf Specifies a value for the option.
   1.326 +@param len Specifies the length of the option value.
   1.327 +
   1.328 +@return	On Success, returns 0. 
   1.329 +		On Failure, returns -1, errno may be set.
   1.330 +*/
   1.331 +EXPORT_C int setsockopt (int fd, int level, int opt, void* buf, size_t len)
   1.332 +	{
   1.333 +	struct _reent *r = _REENT2;
   1.334 +	if (!r)
   1.335 +		return -1; // Memory for library globals is not allocated (errno not set).
   1.336 +	return _setsockopt_r (r, fd, level, opt, buf, len);
   1.337 +	}
   1.338 +EXPORT_C int _setsockopt_r (struct _reent *r, int fd, int level, int opt, void* buf, size_t len) 
   1.339 +	{
   1.340 +	MSystemInterface& sysIf=Interface(r);
   1.341 +	return sysIf.setsockopt(fd,level,opt,buf,len,r->_errno);
   1.342 +	}
   1.343 +
   1.344 +
   1.345 +} // extern "C"