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