genericopenlibs/cstdlib/LPOSIX/NETCALLS.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // connectors for re-entrant networking system calls
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "SYSIF.H"
       
    19 #include "LPOSIX.H"
       
    20 
       
    21 #include <reent.h>
       
    22 #include <sys/socket.h>
       
    23 
       
    24 extern "C" {
       
    25 
       
    26 /**
       
    27 @param family Specifies the address family used in the communications domain.	
       
    28 @param style Type of socket.
       
    29 @param protocol Protocol used with that socket.
       
    30 
       
    31 @return On Success, non-negative integer, the socket file descriptor.
       
    32 		On Failure, returns -1, eerno may be set.
       
    33 */
       
    34 EXPORT_C int socket (int family, int style, int protocol)
       
    35 	{
       
    36 	struct _reent *r = _REENT2;
       
    37 	if (!r)
       
    38 		return -1; // Memory for library globals is not allocated (errno not set).
       
    39 	return _socket_r (r, family, style, protocol);
       
    40 	}
       
    41 EXPORT_C int _socket_r (struct _reent *r, int family, int style, int protocol)
       
    42 	{
       
    43 	MSystemInterface& sysIf=Interface(r);
       
    44 	return sysIf.socket(family,style,protocol,r->_errno);
       
    45 	}
       
    46 
       
    47 /**
       
    48 Receives a message from a socket. 
       
    49 The recv() call can be used on a connection mode socket or a bound, 
       
    50 connectionless socket. If no messages are available at the socket, 
       
    51 the recv() call waits for a message to arrive unless the socket is nonblocking. 
       
    52 
       
    53 @param fd Specifies a socket descriptor to use for the send.
       
    54 @param buf Points to the buffer containing message to send.
       
    55 @param cnt Specifies the length of the message in bytes.
       
    56 @param flags Lets the sender control the way data is sent. 
       
    57 
       
    58 @return On Success, returns the number of bytes received.
       
    59 		On Failure, returns -1, eerno may be set.
       
    60 */
       
    61 EXPORT_C int recv (int fd, char *buf, size_t cnt, int flags)
       
    62 	{
       
    63 	struct _reent *r = _REENT2;
       
    64 	if (!r)
       
    65 		return -1; // Memory for library globals is not allocated (errno not set).
       
    66 	return _recvfrom_r (r, fd, buf, cnt, flags, 0, 0);
       
    67 	}
       
    68 
       
    69 /**
       
    70 @return On Succcess, returns length of message in bytes, can be 0. 
       
    71 		On Failure, returns -1, errno may be set.
       
    72 */	
       
    73 EXPORT_C int recvfrom (int fd, char *buf, size_t cnt, int flags, struct sockaddr* from, size_t* fromsize)
       
    74 	{
       
    75 	struct _reent *r = _REENT2;
       
    76 	if (!r)
       
    77 		return -1; // Memory for library globals is not allocated (errno not set).
       
    78 	return _recvfrom_r (r, fd, buf, cnt, flags, from ,fromsize);
       
    79 	}
       
    80 EXPORT_C int _recvfrom_r (struct _reent *r, int fd, char *buf, size_t nbyte, int flags, struct sockaddr* from, size_t* fromsize)
       
    81 	{
       
    82 	MSystemInterface& sysIf=Interface(r);
       
    83 	return sysIf.recvfrom(fd,buf,nbyte,flags,from,(unsigned long*)fromsize,r->_errno);
       
    84 	}
       
    85 
       
    86 /**
       
    87 Initiates transmission of a message from the specified socket to its peer.
       
    88 The send() function sends a message only when the socket is connected.
       
    89 
       
    90 @param fd Specifies a socket descriptor to use for the send.
       
    91 @param buf Points to the buffer containing message to send.
       
    92 @param cnt Specifies the length of the message in bytes.
       
    93 @param flags Lets the sender control the way data is sent.
       
    94 
       
    95 @return On Success, returns the numbers of characters sent.
       
    96 		On Failure, returns -1, errno may be set.
       
    97 */
       
    98 EXPORT_C int send (int fd, const char *buf, size_t cnt, int flags)
       
    99 	{
       
   100 	struct _reent *r = _REENT2;
       
   101 	if (!r)
       
   102 		return -1; // Memory for library globals is not allocated (errno not set).
       
   103 	return _sendto_r (r, fd, buf, cnt, flags, 0, 0);
       
   104 	}
       
   105 
       
   106 /**
       
   107 @return	On Success, returns the numbers of characters sent.
       
   108 		On Failure, returns -1, errno may be set.
       
   109 */	
       
   110 EXPORT_C int sendto (int fd, const char *buf, size_t cnt, int flags, struct sockaddr* to, size_t tosize)
       
   111 	{
       
   112 	struct _reent *r = _REENT2;
       
   113 	if (!r)
       
   114 		return -1; // Memory for library globals is not allocated (errno not set).
       
   115 	return _sendto_r (r, fd, buf, cnt, flags, to, tosize);
       
   116 	}
       
   117 EXPORT_C int _sendto_r (struct _reent *r, int fd, const char *buf, size_t nbyte, int flags, struct sockaddr* to, size_t tosize)
       
   118 	{
       
   119 	MSystemInterface& sysIf=Interface(r);
       
   120 	return sysIf.sendto(fd,buf,nbyte,flags,to,tosize,r->_errno);
       
   121 	}
       
   122 
       
   123 /**
       
   124 Shuts down all or part of a full-duplex connection on the socket associated with fd.
       
   125 
       
   126 @param fd Is the socket descriptor to shut down.
       
   127 @param how Specifies the type of shutdown.
       
   128 
       
   129 @return On Success, returns 0.
       
   130 		On Failure, returns -1, errno may be set.
       
   131 */
       
   132 EXPORT_C int shutdown (int fd, int how)
       
   133 	{
       
   134 	struct _reent *r = _REENT2;
       
   135 	if (!r)
       
   136 		return -1; // Memory for library globals is not allocated (errno not set).
       
   137 	return _shutdown_r (r, fd, how);
       
   138 	}
       
   139 EXPORT_C int _shutdown_r (struct _reent *r, int fd, int how)
       
   140 	{
       
   141 	MSystemInterface& sysIf=Interface(r);
       
   142 	return sysIf.shutdown(fd,how,r->_errno);
       
   143 	}
       
   144 
       
   145 /**
       
   146 Marks a connection-mode socket, specified by the socket argument fd,
       
   147 as accepting connections, and limits the number of outstanding connections 
       
   148 in the socket's listen queue to the value specified by the n argument. 
       
   149 The socket fd is put into 'passive' mode where incoming connection 
       
   150 requests are acknowledged and queued pending acceptance by the process.
       
   151 
       
   152 @param fd Is a descriptor identifying a bound, unconnected socket.
       
   153 @param n Is the maximum length that the queue of pending connections
       
   154 	   may grow to. If this value is SOMAXCONN, then the underlying service provider
       
   155        responsible for socket fd sets the backlog to a maximum "reasonable" value.
       
   156 
       
   157 @return On Success, returns 0. 
       
   158 		On Failure, returns -1, errno may be set.
       
   159 */
       
   160 EXPORT_C int listen (int fd, int n)
       
   161 	{
       
   162 	struct _reent *r = _REENT2;
       
   163 	if (!r)
       
   164 		return -1; // Memory for library globals is not allocated (errno not set).
       
   165 	return _listen_r (r, fd, n);
       
   166 	}
       
   167 EXPORT_C int _listen_r (struct _reent *r, int fd, int n)
       
   168 	{
       
   169 	MSystemInterface& sysIf=Interface(r);
       
   170 	return sysIf.listen(fd,n,r->_errno);
       
   171 	}
       
   172 
       
   173 /**
       
   174 accepts a connection on a socket. An incoming connection is acknowledged and associated with
       
   175 an immediately created socket. The original socket is returned to the listening state.
       
   176 
       
   177 @param fd Is the integer descriptor of the desired socket.
       
   178 @param addr Points to a sockaddr structure containing the socket address.
       
   179 @param size Points to an integer that states the address length in bytes.
       
   180 
       
   181 @return On Success, returns a non-negative integer, which is a descriptor of the accepted socket.
       
   182         Upon return, addrlen contains the actual length in bytes of the address returned. 
       
   183 		On Failure, returns -1, errno may be set.
       
   184 */
       
   185 EXPORT_C int accept (int fd, struct sockaddr *addr, size_t *size)
       
   186 	{
       
   187 	struct _reent *r = _REENT2;
       
   188 	if (!r)
       
   189 		return -1; // Memory for library globals is not allocated (errno not set).
       
   190 	return _accept_r (r, fd, addr, size);
       
   191 	}
       
   192 EXPORT_C int _accept_r (struct _reent *r, int fd, struct sockaddr *addr, size_t *size)
       
   193 	{
       
   194 	MSystemInterface& sysIf=Interface(r);
       
   195 	int newfd=sysIf.accept(fd,r->_errno);
       
   196 	if (newfd>=0)
       
   197 		sysIf.sockname(newfd,addr,(unsigned long*)size,1,r->_errno);	// getpeername
       
   198 	return newfd;
       
   199 	}
       
   200 
       
   201 /**
       
   202 Associate that socket with a port.
       
   203 
       
   204 @param fd Is the integer descriptor of the desired socket.
       
   205 @param addr Points to a sockaddr structure containing the socket address.
       
   206 @param size Points to an integer that states the address length in bytes.
       
   207 
       
   208 @return	On Success, returns 0. 
       
   209 		On Failure, returns -1, errno may be set.
       
   210 */
       
   211 EXPORT_C int bind (int fd, struct sockaddr *addr, size_t size)
       
   212 	{
       
   213 	struct _reent *r = _REENT2;
       
   214 	if (!r)
       
   215 		return -1; // Memory for library globals is not allocated (errno not set).	
       
   216 	return _bind_r (r, fd, addr, size);
       
   217 	}
       
   218 EXPORT_C int _bind_r (struct _reent *r, int fd, struct sockaddr *addr, size_t size)
       
   219 	{
       
   220 	MSystemInterface& sysIf=Interface(r);
       
   221 	return sysIf.bind(fd,addr,size,r->_errno);
       
   222 	}
       
   223 
       
   224 /**
       
   225 Used by a client program to establish communication with a remote entity
       
   226 
       
   227 @param fd Is the integer descriptor of the desired socket.
       
   228 @param addr Points to a sockaddr structure containing the socket address.
       
   229 @param size Points to an integer that states the address length in bytes.
       
   230 
       
   231 @return	On Success, returns 0. 
       
   232 		On Failure, returns -1, errno may be set.
       
   233 */
       
   234 EXPORT_C int connect (int fd, struct sockaddr *addr, size_t size)
       
   235 	{
       
   236 	struct _reent *r = _REENT2;
       
   237 	if (!r)
       
   238 		return -1; // Memory for library globals is not allocated (errno not set).
       
   239 	return _connect_r (r, fd, addr, size);
       
   240 	}
       
   241 EXPORT_C int _connect_r (struct _reent *r, int fd, struct sockaddr *addr, size_t size)
       
   242 	{
       
   243 	MSystemInterface& sysIf=Interface(r);
       
   244 	return sysIf.connect(fd,addr,size,r->_errno);
       
   245 	}
       
   246 
       
   247 /**
       
   248 Returns the current name for the specified socket. The namelen parameter should be initialized
       
   249 to indicate the amount of space pointed to by name. When returned, namelen contains the actual
       
   250 size (in bytes) of the name returned.
       
   251 
       
   252 @param fd Is the integer descriptor of the desired socket.
       
   253 @param addr Points to a sockaddr structure containing the socket address.
       
   254 @param size Points to an integer that states the address length in bytes.
       
   255 
       
   256 @return	On Success, returns 0. 
       
   257 		On Failure, returns -1, errno may be set.
       
   258 */
       
   259 EXPORT_C int getsockname (int fd, struct sockaddr *addr, size_t* size)
       
   260 	{
       
   261 	struct _reent *r = _REENT2;
       
   262 	if (!r)
       
   263 		return -1; // Memory for library globals is not allocated (errno not set).
       
   264 	return _getsockname_r (r, fd, addr, size);
       
   265 	}
       
   266 EXPORT_C int _getsockname_r (struct _reent *r, int fd, struct sockaddr *addr, size_t* size)
       
   267 	{
       
   268 	MSystemInterface& sysIf=Interface(r);
       
   269 	return sysIf.sockname(fd,addr,(unsigned long*)size,0,r->_errno);
       
   270 	}
       
   271 
       
   272 /**
       
   273 Returns the peer address of the specified socket.
       
   274 
       
   275 @return	On Success, returns 0. 
       
   276 		On Failure, returns -1, errno may be set.
       
   277 */
       
   278 EXPORT_C int getpeername (int fd, struct sockaddr *addr, size_t* size)
       
   279 	{
       
   280 	struct _reent *r = _REENT2;
       
   281 	if (!r)
       
   282 		return -1; // Memory for library globals is not allocated (errno not set).
       
   283 	return _getpeername_r (r, fd, addr, size);
       
   284 	}
       
   285 EXPORT_C int _getpeername_r (struct _reent *r, int fd, struct sockaddr *addr, size_t* size)
       
   286 	{
       
   287 	MSystemInterface& sysIf=Interface(r);
       
   288 	return sysIf.sockname(fd,addr,(unsigned long*)size,1,r->_errno);
       
   289 	}
       
   290 
       
   291 /**
       
   292 Manipulates options associated with a socket.
       
   293 
       
   294 @return	On Success, returns 0. 
       
   295    		On Failure, returns -1, errno may be set.
       
   296 */
       
   297 EXPORT_C int getsockopt (int fd, int level, int opt, void* buf, size_t* len)
       
   298 	{
       
   299 	struct _reent *r = _REENT2;
       
   300 	if (!r)
       
   301 		return -1; // Memory for library globals is not allocated (errno not set).
       
   302 	return _getsockopt_r (r, fd, level, opt, buf, len);
       
   303 	}
       
   304 EXPORT_C int _getsockopt_r (struct _reent *r, int fd, int level, int opt, void* buf, size_t* len) 
       
   305 	{
       
   306 	MSystemInterface& sysIf=Interface(r);
       
   307 	return sysIf.getsockopt(fd,level,opt,buf,(unsigned long*)len,r->_errno);
       
   308 	}
       
   309 
       
   310 /**
       
   311 Manipulates options associated with a socket. Options can exist at multiple protocol levels.
       
   312 However, the options are always present at the uppermost socket level. Options affect socket
       
   313 operations, such as the routing of packets, out-of-band data transfer, and so on.
       
   314 
       
   315 @param fd Specifies a socket for which an option should be set.
       
   316 @param level Identifies whether the operation applies to the socket itself or to the underlying
       
   317 	   protocol being used. The socket itself is identified by the symbolic constant SOL_SOCKET.
       
   318 	   Other protocol levels require the protocol number for the appropriate protocol
       
   319 	   controlling the option.
       
   320 @param opt Specifies a single option to which the request applies, 
       
   321             negative option values are not support on Symbian OS.
       
   322 @param buf Specifies a value for the option.
       
   323 @param len Specifies the length of the option value.
       
   324 
       
   325 @return	On Success, returns 0. 
       
   326 		On Failure, returns -1, errno may be set.
       
   327 */
       
   328 EXPORT_C int setsockopt (int fd, int level, int opt, void* buf, size_t len)
       
   329 	{
       
   330 	struct _reent *r = _REENT2;
       
   331 	if (!r)
       
   332 		return -1; // Memory for library globals is not allocated (errno not set).
       
   333 	return _setsockopt_r (r, fd, level, opt, buf, len);
       
   334 	}
       
   335 EXPORT_C int _setsockopt_r (struct _reent *r, int fd, int level, int opt, void* buf, size_t len) 
       
   336 	{
       
   337 	MSystemInterface& sysIf=Interface(r);
       
   338 	return sysIf.setsockopt(fd,level,opt,buf,len,r->_errno);
       
   339 	}
       
   340 
       
   341 
       
   342 } // extern "C"