Update contrib.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: connectors for re-entrant networking system calls
16 // connectors for re-entrant networking system calls
22 #include <sys/errno.h>
25 #include <sys/socket.h>
26 #include <stdapis/netinet/in.h>
31 #include "sysusrinclude.h"
35 EXPORT_C int socket (int family, int style, int protocol)
37 return _socket_r(&errno, family, style, protocol);
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.
46 EXPORT_C int recv (int fd, void *buf, size_t cnt, int flags)
48 return _recvfrom_r(&errno, fd, (char*) buf, cnt, flags, 0, 0);
52 EXPORT_C int recvfrom (int fd, void *buf, size_t cnt, int flags, struct sockaddr* from, size_t* fromsize)
54 return _recvfrom_r(&errno, fd, (char*) buf, cnt, flags, from, fromsize);
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.
62 EXPORT_C int send (int fd, const void *buf, size_t cnt, int flags)
64 return _sendto_r(&errno, fd, (char*) buf, cnt, flags, 0, 0);
68 EXPORT_C int sendto (int fd, const void *buf, size_t cnt, int flags, const struct sockaddr* to, size_t tosize)
70 return _sendto_r(&errno, fd, (const char*) buf, cnt, flags, (struct sockaddr*) to, tosize);
75 shuts down all or part of a full-duplex connection on the socket
78 EXPORT_C int shutdown (int fd, int how)
80 return _shutdown_r(&errno, fd , how) ;
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.
90 EXPORT_C int listen (int fd, int n)
92 return _listen_r(&errno, fd, n);
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.
98 EXPORT_C int accept (int fd, struct sockaddr *addr, size_t *size)
100 return _accept_r(&errno, fd , addr , size);
105 Associate that socket with a port.
107 EXPORT_C int bind (int fd, const struct sockaddr *addr, size_t size)
109 return _bind_r(&errno, fd, addr, size);
114 Used by a client program to establish communication with a remote entity
116 EXPORT_C int connect (int fd, const struct sockaddr *addr, size_t size)
118 return _connect_r(&errno, fd, addr, size);
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.
125 EXPORT_C int getsockname (int fd, struct sockaddr *addr, size_t* size)
127 return _getsockname_r (&errno, fd, addr, size);
131 EXPORT_C int getpeername (int fd, struct sockaddr *addr, size_t* size)
133 return _getpeername_r(&errno, fd, addr, size);
138 Manipulates options associated with a socket.
140 EXPORT_C int getsockopt (int fd, int level, int opt, void* buf, size_t* len)
142 return _getsockopt_r(&errno, fd, level, opt, buf, len);
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.
149 EXPORT_C int setsockopt (int fd, int level, int opt, const void* buf, size_t len)
151 return _setsockopt_r(&errno , fd, level, opt, (void *)buf, len);
155 EXPORT_C uint16_t htons(uint16_t hs)
157 return ByteOrder::Swap16(hs);
160 EXPORT_C uint32_t htonl(uint32_t hl)
162 return ByteOrder::Swap32(hl);
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.
173 EXPORT_C int gethostname(char *name, size_t size)
175 return _gethostname(name, size);
180 Get the internet name of the host by address.
182 EXPORT_C struct hostent* gethostbyaddr (const char* addr, int length, int format)
184 return _gethostbyaddr_r(_REENT, addr, length, format);
188 Get the internet name of the host by name.
190 EXPORT_C struct hostent* gethostbyname (const char* name)
194 h_errno = HOST_NOT_FOUND;
197 return _gethostbyname_r(_REENT, name);
200 static protoent* reent_function(struct _reent* rp)
202 struct protoent *p=NULL;
205 p = (struct protoent*)(rp->_netdb);
206 BackendFree(p->p_name);
210 p = (protoent*)BackendAlloc(sizeof(protoent));
215 int _gethostname (char* name, size_t size)
217 if (name == NULL || size == 0)
223 return _gethostname_r(&errno, name, size);
226 EXPORT_C protoent* getprotobyname(const char* name)
230 // set errno to something?
234 TProtocolDesc protoInfo;
235 if (_getprotobyname_r(&errno, name, &protoInfo) == -1)
240 protoent* p = reent_function(_REENT);
243 const int BUFSIZE = 128;
248 if((nbytes=wcstombs(buf,(wchar_t*)protoInfo.iName.PtrZ(),BUFSIZE)) == (size_t)-1)
251 p->p_name = (char*)BackendAlloc(nbytes+1);
257 strncpy(p->p_name, buf,nbytes+1);
258 p->p_proto = protoInfo.iProtocol;
264 EXPORT_C protoent* getprotobynumber(TInt proto)
266 TProtocolDesc protoInfo;
267 if (_getprotobynumber_r(&errno, proto, &protoInfo) == -1)
272 protoent* p = reent_function(_REENT);
275 const int BUFSIZE = 128;
280 if((nbytes=wcstombs(buf,(wchar_t*)protoInfo.iName.PtrZ(),BUFSIZE)) == (size_t)-1)
283 p->p_name = (char*)BackendAlloc(nbytes+1);
288 strncpy(p->p_name, buf,nbytes+1);
289 p->p_proto = protoInfo.iProtocol;
295 EXPORT_C int setdefaultif( const struct ifreq* ifReq )
297 return _setdefaultif_r(&errno,ifReq);
300 EXPORT_C int unsetdefaultif()
302 return _unsetdefaultif_r(&errno);