genericopenlibs/openenvcore/libc/src/Nmscalls.cpp
changeset 0 e4d67989cc36
child 54 4332f0f7be53
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: connectors for re-entrant networking system calls
       
    15 */
       
    16 // connectors for re-entrant networking system calls
       
    17 
       
    18 #include "sysif.h"
       
    19 #include "fdesc.h"
       
    20 #include <unistd.h>
       
    21 #include <stdlib.h>
       
    22 #include <sys/errno.h>
       
    23 #include "lposix.h"
       
    24 #include "sysreent.h"
       
    25 #include <sys/socket.h>
       
    26 #include <stdapis/netinet/in.h>
       
    27 #include <string.h>
       
    28 #include <es_sock.h>
       
    29 
       
    30 #include "netdb_r.h"
       
    31 #include "sysusrinclude.h"
       
    32 
       
    33 extern "C" {
       
    34 
       
    35 EXPORT_C int socket (int family, int style, int protocol)
       
    36 	{
       
    37 	return _socket_r(&errno, family, style, protocol);
       
    38 	}
       
    39 
       
    40 /*
       
    41 Receives a message from a socket. 
       
    42 The recv() call can be used on a connection mode socket or a bound, 
       
    43 connectionless socket. If no messages are available at the socket, 
       
    44 the recv() call waits for a message to arrive unless the socket is nonblocking. 
       
    45 */
       
    46 EXPORT_C int recv (int fd, void *buf, size_t cnt, int flags)
       
    47 	{
       
    48 	return _recvfrom_r(&errno, fd, (char*) buf, cnt, flags, 0, 0);
       
    49 	}
       
    50 
       
    51 
       
    52 EXPORT_C int recvfrom (int fd, void *buf, size_t cnt, int flags, struct sockaddr* from, size_t* fromsize)
       
    53 	{
       
    54 	 return _recvfrom_r(&errno, fd, (char*) buf, cnt, flags, from, fromsize);
       
    55 	}
       
    56 	
       
    57 
       
    58 /*
       
    59 initiates transmission of a message from the specified socket to its peer.
       
    60 The send() function sends a message only when the socket is connected.
       
    61 */
       
    62 EXPORT_C int send (int fd, const void *buf, size_t cnt, int flags)
       
    63 	{
       
    64 	 return _sendto_r(&errno, fd, (char*) buf, cnt, flags, 0, 0);
       
    65   	}
       
    66 
       
    67 
       
    68 EXPORT_C int sendto (int fd, const void *buf, size_t cnt, int flags, const struct sockaddr* to, size_t tosize)
       
    69 	{
       
    70 	return _sendto_r(&errno, fd, (const char*) buf, cnt, flags, (struct sockaddr*) to, tosize);
       
    71  	}
       
    72 
       
    73 
       
    74 /*
       
    75 shuts down all or part of a full-duplex connection on the socket
       
    76 associated with fd
       
    77 */
       
    78 EXPORT_C int shutdown (int fd, int how)
       
    79 	{
       
    80 	return _shutdown_r(&errno, fd , how) ;
       
    81 	}
       
    82 
       
    83 /*
       
    84 Marks a connection-mode socket, specified by the socket argument fd,
       
    85 as accepting connections, and limits the number of outstanding connections 
       
    86 in the socket's listen queue to the value specified by the n argument. 
       
    87 The socket fd is put into 'passive' mode where incoming connection 
       
    88 requests are acknowledged and queued pending acceptance by the process.
       
    89 */
       
    90 EXPORT_C int listen (int fd, int n)
       
    91 	{
       
    92 	return _listen_r(&errno, fd, n);
       
    93 	}
       
    94 
       
    95 /*
       
    96 accepts a connection on a socket. An incoming connection is acknowledged and associated with an immediately created socket. The original socket is returned to the listening state.
       
    97 */
       
    98 EXPORT_C int accept (int fd, struct sockaddr *addr, size_t *size)
       
    99 	{
       
   100 	return _accept_r(&errno, fd , addr , size);
       
   101 	}
       
   102 
       
   103 
       
   104 /*
       
   105 Associate that socket with a port.
       
   106 */
       
   107 EXPORT_C int bind (int fd, const struct sockaddr *addr, size_t size)
       
   108 	{
       
   109 	return _bind_r(&errno, fd, addr, size);
       
   110 	}
       
   111 	
       
   112 
       
   113 /*
       
   114 Used by a client program to establish communication with a remote entity
       
   115 */
       
   116 EXPORT_C int connect (int fd, const  struct sockaddr *addr, size_t size)
       
   117 	{
       
   118 	return _connect_r(&errno, fd, addr, size);
       
   119 	}
       
   120 	
       
   121 
       
   122 /*
       
   123 returns the current name for the specified socket. The namelen parameter should be initialized to indicate the amount of space pointed to by name. When returned, namelen contains the actual size (in bytes) of the name returned.
       
   124 */
       
   125 EXPORT_C int getsockname (int fd, struct sockaddr *addr, size_t* size)
       
   126 	{
       
   127 	return _getsockname_r (&errno, fd, addr, size);
       
   128 	}
       
   129 
       
   130 
       
   131 EXPORT_C int getpeername (int fd, struct sockaddr *addr, size_t* size)
       
   132 	{
       
   133 	return _getpeername_r(&errno, fd, addr, size);
       
   134 	}
       
   135 	
       
   136 
       
   137 /*
       
   138 Manipulates options associated with a socket.
       
   139 */
       
   140 EXPORT_C int getsockopt (int fd, int level, int opt, void* buf, size_t* len)
       
   141 	{
       
   142 	return _getsockopt_r(&errno, fd, level, opt, buf, len);
       
   143 	}
       
   144 	
       
   145 
       
   146 /*
       
   147 manipulates options associated with a socket. Options can exist at multiple protocol levels. However, the options are always present at the uppermost socket level. Options affect socket operations, such as the routing of packets, out-of-band data transfer, and so on.
       
   148 */
       
   149 EXPORT_C int setsockopt (int fd, int level, int opt, const  void* buf, size_t len)
       
   150 	{
       
   151 	return _setsockopt_r(&errno , fd, level, opt, (void *)buf, len);
       
   152 	}
       
   153 	
       
   154 
       
   155 EXPORT_C  uint16_t htons(uint16_t hs)
       
   156 	{
       
   157 	return  ByteOrder::Swap16(hs);
       
   158 	}
       
   159 
       
   160 EXPORT_C uint32_t htonl(uint32_t hl)
       
   161 	{
       
   162 	return ByteOrder::Swap32(hl);
       
   163 	}
       
   164 
       
   165 
       
   166 /*
       
   167 Get the internet name of this host. Actually this will always return a null 
       
   168 string with TCPIP 030 and onwards because the "name" of a mobile host
       
   169 isn't really very meaningful - in practice the IP address is chosen dynamically
       
   170 once you start doing real networking, at which time the ISP can resolve the 
       
   171 IP address into a name of some sort if you really want.
       
   172 */
       
   173 EXPORT_C int gethostname(char *name, size_t size) 
       
   174 	{	
       
   175 	return _gethostname(name, size);
       
   176 	}
       
   177 	
       
   178 
       
   179 /*
       
   180 Get the internet name of the host by address.
       
   181 */
       
   182 EXPORT_C struct hostent* gethostbyaddr (const char* addr, int length, int format)
       
   183 	{
       
   184 	return _gethostbyaddr_r(_REENT, addr, length, format);
       
   185 	}
       
   186 
       
   187 /*
       
   188 Get the internet name of the host by name.
       
   189 */
       
   190 EXPORT_C struct hostent* gethostbyname (const char* name)
       
   191 	{
       
   192 	if (name == NULL)
       
   193 		{
       
   194 		h_errno = HOST_NOT_FOUND;
       
   195 		return NULL;
       
   196 		}
       
   197 	return _gethostbyname_r(_REENT, name);
       
   198 	}
       
   199 
       
   200 static protoent* reent_function(struct _reent* rp)
       
   201 	{
       
   202 	struct protoent *p=NULL;
       
   203 	if (rp->_netdb)
       
   204 		{
       
   205 		p = (struct protoent*)(rp->_netdb);
       
   206 		BackendFree(p->p_name);
       
   207 		BackendFree(p);
       
   208 		}
       
   209 
       
   210 	p = (protoent*)BackendAlloc(sizeof(protoent));
       
   211 	rp->_netdb = p;
       
   212 	return p;
       
   213 	}
       
   214 
       
   215 int _gethostname (char* name, size_t size)
       
   216 	{
       
   217 	if (name == NULL || size == 0)
       
   218 		{
       
   219 		errno = EFAULT;
       
   220 		return -1;
       
   221 		}
       
   222 		
       
   223 	return _gethostname_r(&errno, name, size);
       
   224 	}
       
   225 
       
   226 EXPORT_C protoent* getprotobyname(const char* name)
       
   227 	{
       
   228 	if (name == NULL)
       
   229 		{
       
   230 		// set errno to something?
       
   231 		return NULL;
       
   232 		}
       
   233 
       
   234 	TProtocolDesc protoInfo;
       
   235 	if (_getprotobyname_r(&errno, name, &protoInfo) == -1)
       
   236 		{
       
   237 		return NULL;
       
   238 		}
       
   239 	
       
   240 	protoent* p = reent_function(_REENT);
       
   241 	p->p_aliases = NULL;
       
   242 
       
   243 	const int BUFSIZE = 128;
       
   244 	
       
   245 	char buf[BUFSIZE] ;
       
   246 	size_t nbytes;
       
   247 	
       
   248 	if((nbytes=wcstombs(buf,(wchar_t*)protoInfo.iName.PtrZ(),BUFSIZE)) == (size_t)-1)
       
   249 		return NULL;
       
   250 	
       
   251 	p->p_name = (char*)BackendAlloc(nbytes+1);
       
   252 	if (!p->p_name)
       
   253 		{
       
   254 		return NULL;
       
   255 		}
       
   256 	
       
   257 	strncpy(p->p_name, buf,nbytes+1);
       
   258 	p->p_proto = protoInfo.iProtocol;
       
   259 	
       
   260 	return p;    
       
   261 	}
       
   262 
       
   263 
       
   264 EXPORT_C protoent* getprotobynumber(TInt proto)
       
   265 	{
       
   266 	TProtocolDesc protoInfo;
       
   267 	if (_getprotobynumber_r(&errno, proto, &protoInfo) == -1)
       
   268 		{
       
   269 		return NULL;
       
   270 		}
       
   271 	
       
   272     protoent* p = reent_function(_REENT);
       
   273 	p->p_aliases = NULL;
       
   274 	
       
   275 	const int BUFSIZE = 128;
       
   276 	
       
   277 	char buf[BUFSIZE] ;
       
   278 	size_t nbytes;
       
   279 	
       
   280 	if((nbytes=wcstombs(buf,(wchar_t*)protoInfo.iName.PtrZ(),BUFSIZE)) == (size_t)-1)
       
   281 		return NULL;
       
   282 	
       
   283 	p->p_name = (char*)BackendAlloc(nbytes+1);
       
   284 	if (!p->p_name)
       
   285 		{
       
   286 		return NULL;
       
   287 		}
       
   288 	strncpy(p->p_name, buf,nbytes+1);
       
   289 	p->p_proto = protoInfo.iProtocol;
       
   290 	
       
   291 	return p;
       
   292 	}
       
   293 
       
   294 
       
   295 EXPORT_C int setdefaultif( const struct ifreq* ifReq )
       
   296 	{
       
   297 	return _setdefaultif_r(&errno,ifReq);
       
   298 	}
       
   299 	
       
   300 } // extern "C"