sl@0: // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: // connectors for re-entrant networking system calls
sl@0: // 
sl@0: //
sl@0: 
sl@0: #include "SYSIF.H"
sl@0: #include "LPOSIX.H"
sl@0: 
sl@0: #include <reent.h>
sl@0: #include <sys/socket.h>
sl@0: 
sl@0: extern "C" {
sl@0: 
sl@0: /**
sl@0: @param family Specifies the address family used in the communications domain.	
sl@0: @param style Type of socket.
sl@0: @param protocol Protocol used with that socket.
sl@0: 
sl@0: @return On Success, non-negative integer, the socket file descriptor.
sl@0: 		On Failure, returns -1, eerno may be set.
sl@0: */
sl@0: EXPORT_C int socket (int family, int style, int protocol)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _socket_r (r, family, style, protocol);
sl@0: 	}
sl@0: EXPORT_C int _socket_r (struct _reent *r, int family, int style, int protocol)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.socket(family,style,protocol,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Receives a message from a socket. 
sl@0: The recv() call can be used on a connection mode socket or a bound, 
sl@0: connectionless socket. If no messages are available at the socket, 
sl@0: the recv() call waits for a message to arrive unless the socket is nonblocking. 
sl@0: 
sl@0: @param fd Specifies a socket descriptor to use for the send.
sl@0: @param buf Points to the buffer containing message to send.
sl@0: @param cnt Specifies the length of the message in bytes.
sl@0: @param flags Lets the sender control the way data is sent. 
sl@0: 
sl@0: @return On Success, returns the number of bytes received.
sl@0: 		On Failure, returns -1, eerno may be set.
sl@0: */
sl@0: EXPORT_C int recv (int fd, char *buf, size_t cnt, int flags)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _recvfrom_r (r, fd, buf, cnt, flags, 0, 0);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: @return On Succcess, returns length of message in bytes, can be 0. 
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */	
sl@0: EXPORT_C int recvfrom (int fd, char *buf, size_t cnt, int flags, struct sockaddr* from, size_t* fromsize)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _recvfrom_r (r, fd, buf, cnt, flags, from ,fromsize);
sl@0: 	}
sl@0: EXPORT_C int _recvfrom_r (struct _reent *r, int fd, char *buf, size_t nbyte, int flags, struct sockaddr* from, size_t* fromsize)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.recvfrom(fd,buf,nbyte,flags,from,(unsigned long*)fromsize,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Initiates transmission of a message from the specified socket to its peer.
sl@0: The send() function sends a message only when the socket is connected.
sl@0: 
sl@0: @param fd Specifies a socket descriptor to use for the send.
sl@0: @param buf Points to the buffer containing message to send.
sl@0: @param cnt Specifies the length of the message in bytes.
sl@0: @param flags Lets the sender control the way data is sent.
sl@0: 
sl@0: @return On Success, returns the numbers of characters sent.
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int send (int fd, const char *buf, size_t cnt, int flags)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _sendto_r (r, fd, buf, cnt, flags, 0, 0);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: @return	On Success, returns the numbers of characters sent.
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */	
sl@0: EXPORT_C int sendto (int fd, const char *buf, size_t cnt, int flags, struct sockaddr* to, size_t tosize)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _sendto_r (r, fd, buf, cnt, flags, to, tosize);
sl@0: 	}
sl@0: EXPORT_C int _sendto_r (struct _reent *r, int fd, const char *buf, size_t nbyte, int flags, struct sockaddr* to, size_t tosize)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.sendto(fd,buf,nbyte,flags,to,tosize,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Shuts down all or part of a full-duplex connection on the socket associated with fd.
sl@0: 
sl@0: @param fd Is the socket descriptor to shut down.
sl@0: @param how Specifies the type of shutdown.
sl@0: 
sl@0: @return On Success, returns 0.
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int shutdown (int fd, int how)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _shutdown_r (r, fd, how);
sl@0: 	}
sl@0: EXPORT_C int _shutdown_r (struct _reent *r, int fd, int how)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.shutdown(fd,how,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Marks a connection-mode socket, specified by the socket argument fd,
sl@0: as accepting connections, and limits the number of outstanding connections 
sl@0: in the socket's listen queue to the value specified by the n argument. 
sl@0: The socket fd is put into 'passive' mode where incoming connection 
sl@0: requests are acknowledged and queued pending acceptance by the process.
sl@0: 
sl@0: @param fd Is a descriptor identifying a bound, unconnected socket.
sl@0: @param n Is the maximum length that the queue of pending connections
sl@0: 	   may grow to. If this value is SOMAXCONN, then the underlying service provider
sl@0:        responsible for socket fd sets the backlog to a maximum "reasonable" value.
sl@0: 
sl@0: @return On Success, returns 0. 
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int listen (int fd, int n)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _listen_r (r, fd, n);
sl@0: 	}
sl@0: EXPORT_C int _listen_r (struct _reent *r, int fd, int n)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.listen(fd,n,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: accepts a connection on a socket. An incoming connection is acknowledged and associated with
sl@0: an immediately created socket. The original socket is returned to the listening state.
sl@0: 
sl@0: @param fd Is the integer descriptor of the desired socket.
sl@0: @param addr Points to a sockaddr structure containing the socket address.
sl@0: @param size Points to an integer that states the address length in bytes.
sl@0: 
sl@0: @return On Success, returns a non-negative integer, which is a descriptor of the accepted socket.
sl@0:         Upon return, addrlen contains the actual length in bytes of the address returned. 
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int accept (int fd, struct sockaddr *addr, size_t *size)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _accept_r (r, fd, addr, size);
sl@0: 	}
sl@0: EXPORT_C int _accept_r (struct _reent *r, int fd, struct sockaddr *addr, size_t *size)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	int newfd=sysIf.accept(fd,r->_errno);
sl@0: 	if (newfd>=0)
sl@0: 		sysIf.sockname(newfd,addr,(unsigned long*)size,1,r->_errno);	// getpeername
sl@0: 	return newfd;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Associate that socket with a port.
sl@0: 
sl@0: @param fd Is the integer descriptor of the desired socket.
sl@0: @param addr Points to a sockaddr structure containing the socket address.
sl@0: @param size Points to an integer that states the address length in bytes.
sl@0: 
sl@0: @return	On Success, returns 0. 
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int bind (int fd, struct sockaddr *addr, size_t size)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).	
sl@0: 	return _bind_r (r, fd, addr, size);
sl@0: 	}
sl@0: EXPORT_C int _bind_r (struct _reent *r, int fd, struct sockaddr *addr, size_t size)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.bind(fd,addr,size,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Used by a client program to establish communication with a remote entity
sl@0: 
sl@0: @param fd Is the integer descriptor of the desired socket.
sl@0: @param addr Points to a sockaddr structure containing the socket address.
sl@0: @param size Points to an integer that states the address length in bytes.
sl@0: 
sl@0: @return	On Success, returns 0. 
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int connect (int fd, struct sockaddr *addr, size_t size)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _connect_r (r, fd, addr, size);
sl@0: 	}
sl@0: EXPORT_C int _connect_r (struct _reent *r, int fd, struct sockaddr *addr, size_t size)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.connect(fd,addr,size,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Returns the current name for the specified socket. The namelen parameter should be initialized
sl@0: to indicate the amount of space pointed to by name. When returned, namelen contains the actual
sl@0: size (in bytes) of the name returned.
sl@0: 
sl@0: @param fd Is the integer descriptor of the desired socket.
sl@0: @param addr Points to a sockaddr structure containing the socket address.
sl@0: @param size Points to an integer that states the address length in bytes.
sl@0: 
sl@0: @return	On Success, returns 0. 
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int getsockname (int fd, struct sockaddr *addr, size_t* size)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _getsockname_r (r, fd, addr, size);
sl@0: 	}
sl@0: EXPORT_C int _getsockname_r (struct _reent *r, int fd, struct sockaddr *addr, size_t* size)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.sockname(fd,addr,(unsigned long*)size,0,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Returns the peer address of the specified socket.
sl@0: 
sl@0: @return	On Success, returns 0. 
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int getpeername (int fd, struct sockaddr *addr, size_t* size)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _getpeername_r (r, fd, addr, size);
sl@0: 	}
sl@0: EXPORT_C int _getpeername_r (struct _reent *r, int fd, struct sockaddr *addr, size_t* size)
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.sockname(fd,addr,(unsigned long*)size,1,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Manipulates options associated with a socket.
sl@0: 
sl@0: @return	On Success, returns 0. 
sl@0:    		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int getsockopt (int fd, int level, int opt, void* buf, size_t* len)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _getsockopt_r (r, fd, level, opt, buf, len);
sl@0: 	}
sl@0: EXPORT_C int _getsockopt_r (struct _reent *r, int fd, int level, int opt, void* buf, size_t* len) 
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.getsockopt(fd,level,opt,buf,(unsigned long*)len,r->_errno);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Manipulates options associated with a socket. Options can exist at multiple protocol levels.
sl@0: However, the options are always present at the uppermost socket level. Options affect socket
sl@0: operations, such as the routing of packets, out-of-band data transfer, and so on.
sl@0: 
sl@0: @param fd Specifies a socket for which an option should be set.
sl@0: @param level Identifies whether the operation applies to the socket itself or to the underlying
sl@0: 	   protocol being used. The socket itself is identified by the symbolic constant SOL_SOCKET.
sl@0: 	   Other protocol levels require the protocol number for the appropriate protocol
sl@0: 	   controlling the option.
sl@0: @param opt Specifies a single option to which the request applies, 
sl@0:             negative option values are not support on Symbian OS.
sl@0: @param buf Specifies a value for the option.
sl@0: @param len Specifies the length of the option value.
sl@0: 
sl@0: @return	On Success, returns 0. 
sl@0: 		On Failure, returns -1, errno may be set.
sl@0: */
sl@0: EXPORT_C int setsockopt (int fd, int level, int opt, void* buf, size_t len)
sl@0: 	{
sl@0: 	struct _reent *r = _REENT2;
sl@0: 	if (!r)
sl@0: 		return -1; // Memory for library globals is not allocated (errno not set).
sl@0: 	return _setsockopt_r (r, fd, level, opt, buf, len);
sl@0: 	}
sl@0: EXPORT_C int _setsockopt_r (struct _reent *r, int fd, int level, int opt, void* buf, size_t len) 
sl@0: 	{
sl@0: 	MSystemInterface& sysIf=Interface(r);
sl@0: 	return sysIf.setsockopt(fd,level,opt,buf,len,r->_errno);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: } // extern "C"