blob: a1bd0a305e508db000dc1da11111b4900ed17e9f [file] [log] [blame]
Benny Prijonof260e462007-04-30 21:03:32 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonof260e462007-04-30 21:03:32 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pj/sock.h>
21#include <pj/addr_resolv.h>
22#include <pj/assert.h>
23#include <pj/errno.h>
24#include <pj/os.h>
25#include <pj/string.h>
26#include <pj/unicode.h>
27
28#include "os_symbian.h"
29
30
31/*
32 * Address families.
33 */
Benny Prijono2a78a522007-11-21 14:10:46 +000034const pj_uint16_t PJ_AF_UNSPEC = KAFUnspec;
Benny Prijonof260e462007-04-30 21:03:32 +000035const pj_uint16_t PJ_AF_UNIX = 0xFFFF;
36const pj_uint16_t PJ_AF_INET = KAfInet;
37const pj_uint16_t PJ_AF_INET6 = KAfInet6;
38const pj_uint16_t PJ_AF_PACKET = 0xFFFF;
39const pj_uint16_t PJ_AF_IRDA = 0xFFFF;
40
41/*
42 * Socket types conversion.
43 * The values here are indexed based on pj_sock_type
44 */
45const pj_uint16_t PJ_SOCK_STREAM= KSockStream;
46const pj_uint16_t PJ_SOCK_DGRAM = KSockDatagram;
47const pj_uint16_t PJ_SOCK_RAW = 0xFFFF;
48const pj_uint16_t PJ_SOCK_RDM = 0xFFFF;
49
50/* setsockop() is not really supported. */
51const pj_uint16_t PJ_SOL_SOCKET = 0xFFFF;
52const pj_uint16_t PJ_SOL_IP = 0xFFFF;
53const pj_uint16_t PJ_SOL_TCP = 0xFFFF;
54const pj_uint16_t PJ_SOL_UDP = 0xFFFF;
55const pj_uint16_t PJ_SOL_IPV6 = 0xFFFF;
56
Benny Prijono8ab968f2007-07-20 08:08:30 +000057/* TOS */
58const pj_uint16_t PJ_IP_TOS = 0;
59const pj_uint16_t PJ_IPTOS_LOWDELAY = 0;
60const pj_uint16_t PJ_IPTOS_THROUGHPUT = 0;
61const pj_uint16_t PJ_IPTOS_RELIABILITY = 0;
62const pj_uint16_t PJ_IPTOS_MINCOST = 0;
63
Benny Prijonof260e462007-04-30 21:03:32 +000064/* ioctl() is also not supported. */
65const pj_uint16_t PJ_SO_TYPE = 0xFFFF;
66const pj_uint16_t PJ_SO_RCVBUF = 0xFFFF;
67const pj_uint16_t PJ_SO_SNDBUF = 0xFFFF;
68
Nanang Izzuddinb76154e2008-09-16 16:11:44 +000069/* IP multicast is also not supported. */
Benny Prijono513d1352008-09-27 09:35:34 +000070const pj_uint16_t PJ_IP_MULTICAST_IF = 0xFFFF;
71const pj_uint16_t PJ_IP_MULTICAST_TTL = 0xFFFF;
72const pj_uint16_t PJ_IP_MULTICAST_LOOP = 0xFFFF;
73const pj_uint16_t PJ_IP_ADD_MEMBERSHIP = 0xFFFF;
74const pj_uint16_t PJ_IP_DROP_MEMBERSHIP = 0xFFFF;
Nanang Izzuddinb76154e2008-09-16 16:11:44 +000075
Benny Prijono8ab968f2007-07-20 08:08:30 +000076/* Flags */
77const int PJ_MSG_OOB = 0;
78const int PJ_MSG_PEEK = KSockReadPeek;
79const int PJ_MSG_DONTROUTE = 0;
Benny Prijonof260e462007-04-30 21:03:32 +000080
81/////////////////////////////////////////////////////////////////////////////
82//
83// CPjSocket implementation.
84// (declaration is in os_symbian.h)
85//
86
87CPjSocket::~CPjSocket()
88{
Benny Prijono897f9f82007-05-03 19:56:21 +000089 DestroyReader();
Benny Prijonof260e462007-04-30 21:03:32 +000090 sock_.Close();
91}
92
93
94// Create socket reader.
95CPjSocketReader *CPjSocket::CreateReader(unsigned max_len)
96{
97 pj_assert(sockReader_ == NULL);
98 return sockReader_ = CPjSocketReader::NewL(*this, max_len);
99}
100
Benny Prijono897f9f82007-05-03 19:56:21 +0000101// Delete socket reader when it's not wanted.
102void CPjSocket::DestroyReader()
103{
104 if (sockReader_) {
Nanang Izzuddin82f7a412008-12-17 11:36:22 +0000105 sockReader_->Cancel();
Benny Prijono897f9f82007-05-03 19:56:21 +0000106 delete sockReader_;
107 sockReader_ = NULL;
108 }
109}
110
Benny Prijonof260e462007-04-30 21:03:32 +0000111
112/////////////////////////////////////////////////////////////////////////////
113//
114// CPjSocketReader implementation
115// (declaration in os_symbian.h)
116//
117
118
119CPjSocketReader::CPjSocketReader(CPjSocket &sock)
120: CActive(EPriorityStandard),
121 sock_(sock), buffer_(NULL, 0), readCb_(NULL), key_(NULL)
122{
123}
124
125
126void CPjSocketReader::ConstructL(unsigned max_len)
127{
128 TProtocolDesc aProtocol;
129 TInt err;
130
131 err = sock_.Socket().Info(aProtocol);
132 User::LeaveIfError(err);
133
134 isDatagram_ = (aProtocol.iSockType == KSockDatagram);
135
136 TUint8 *ptr = new TUint8[max_len];
137 buffer_.Set(ptr, 0, (TInt)max_len);
138 CActiveScheduler::Add(this);
139}
140
141CPjSocketReader *CPjSocketReader::NewL(CPjSocket &sock, unsigned max_len)
142{
143 CPjSocketReader *self = new (ELeave) CPjSocketReader(sock);
144 CleanupStack::PushL(self);
145 self->ConstructL(max_len);
146 CleanupStack::Pop(self);
147
148 return self;
149}
150
151
152CPjSocketReader::~CPjSocketReader()
153{
154 const TUint8 *data = buffer_.Ptr();
155 delete [] data;
156}
157
158void CPjSocketReader::StartRecv(void (*cb)(void *key),
159 void *key,
160 TDes8 *aDesc,
161 TUint flags)
162{
163 StartRecvFrom(cb, key, aDesc, flags, NULL);
164}
165
166void CPjSocketReader::StartRecvFrom(void (*cb)(void *key),
167 void *key,
168 TDes8 *aDesc,
169 TUint flags,
170 TSockAddr *fromAddr)
171{
172 readCb_ = cb;
173 key_ = key;
174
175 if (aDesc == NULL) aDesc = &buffer_;
176 if (fromAddr == NULL) fromAddr = &recvAddr_;
177
178 sock_.Socket().RecvFrom(*aDesc, *fromAddr, flags, iStatus);
Benny Prijono5d542642007-05-02 18:54:19 +0000179 SetActive();
Benny Prijonof260e462007-04-30 21:03:32 +0000180}
181
182void CPjSocketReader::DoCancel()
183{
184 sock_.Socket().CancelRecv();
185}
186
187void CPjSocketReader::RunL()
188{
189 void (*old_cb)(void *key) = readCb_;
190 void *old_key = key_;
Benny Prijonof260e462007-04-30 21:03:32 +0000191
192 readCb_ = NULL;
193 key_ = NULL;
194
195 if (old_cb) {
196 (*old_cb)(old_key);
197 }
198}
199
200// Append data to aDesc, up to aDesc's maximum size.
201// If socket is datagram based, buffer_ will be clared.
202void CPjSocketReader::ReadData(TDes8 &aDesc, TInetAddr *addr)
203{
204 if (isDatagram_)
205 aDesc.Zero();
206
207 if (buffer_.Length() == 0)
208 return;
209
210 TInt size_to_copy = aDesc.MaxLength() - aDesc.Length();
211 if (size_to_copy > buffer_.Length())
212 size_to_copy = buffer_.Length();
213
214 aDesc.Append(buffer_.Ptr(), size_to_copy);
215
216 if (isDatagram_)
217 buffer_.Zero();
218 else
219 buffer_.Delete(0, size_to_copy);
220
221 if (addr)
222 *addr = recvAddr_;
223}
224
225
226
227/////////////////////////////////////////////////////////////////////////////
228//
229// PJLIB's sock.h implementation
230//
231
232/*
233 * Convert 16-bit value from network byte order to host byte order.
234 */
235PJ_DEF(pj_uint16_t) pj_ntohs(pj_uint16_t netshort)
236{
Benny Prijonob2c96822007-05-03 13:31:21 +0000237#if PJ_IS_LITTLE_ENDIAN
238 return pj_swap16(netshort);
239#else
Benny Prijonof260e462007-04-30 21:03:32 +0000240 return netshort;
Benny Prijonob2c96822007-05-03 13:31:21 +0000241#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000242}
243
244/*
245 * Convert 16-bit value from host byte order to network byte order.
246 */
247PJ_DEF(pj_uint16_t) pj_htons(pj_uint16_t hostshort)
248{
Benny Prijonob2c96822007-05-03 13:31:21 +0000249#if PJ_IS_LITTLE_ENDIAN
250 return pj_swap16(hostshort);
251#else
Benny Prijonof260e462007-04-30 21:03:32 +0000252 return hostshort;
Benny Prijonob2c96822007-05-03 13:31:21 +0000253#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000254}
255
256/*
257 * Convert 32-bit value from network byte order to host byte order.
258 */
259PJ_DEF(pj_uint32_t) pj_ntohl(pj_uint32_t netlong)
260{
Benny Prijonob2c96822007-05-03 13:31:21 +0000261#if PJ_IS_LITTLE_ENDIAN
262 return pj_swap32(netlong);
263#else
Benny Prijonof260e462007-04-30 21:03:32 +0000264 return netlong;
Benny Prijonob2c96822007-05-03 13:31:21 +0000265#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000266}
267
268/*
269 * Convert 32-bit value from host byte order to network byte order.
270 */
271PJ_DEF(pj_uint32_t) pj_htonl(pj_uint32_t hostlong)
272{
Benny Prijonob2c96822007-05-03 13:31:21 +0000273#if PJ_IS_LITTLE_ENDIAN
274 return pj_swap32(hostlong);
275#else
276 return netlong;
277#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000278}
279
280/*
281 * Convert an Internet host address given in network byte order
282 * to string in standard numbers and dots notation.
283 */
284PJ_DEF(char*) pj_inet_ntoa(pj_in_addr inaddr)
285{
Benny Prijonocd700b12008-01-02 08:22:38 +0000286 static char str8[PJ_INET_ADDRSTRLEN];
287 TBuf<PJ_INET_ADDRSTRLEN> str16(0);
Benny Prijonof260e462007-04-30 21:03:32 +0000288
Benny Prijonob2c96822007-05-03 13:31:21 +0000289 /* (Symbian IP address is in host byte order) */
290 TInetAddr temp_addr((TUint32)pj_ntohl(inaddr.s_addr), (TUint)0);
Benny Prijonof260e462007-04-30 21:03:32 +0000291 temp_addr.Output(str16);
292
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000293 return pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
Benny Prijonof260e462007-04-30 21:03:32 +0000294 str8, sizeof(str8));
295}
296
297/*
298 * This function converts the Internet host address cp from the standard
299 * numbers-and-dots notation into binary data and stores it in the structure
300 * that inp points to.
301 */
302PJ_DEF(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp)
303{
Benny Prijonocd700b12008-01-02 08:22:38 +0000304 enum { MAXIPLEN = PJ_INET_ADDRSTRLEN };
Benny Prijonof260e462007-04-30 21:03:32 +0000305
306 /* Initialize output with PJ_INADDR_NONE.
307 * Some apps relies on this instead of the return value
308 * (and anyway the return value is quite confusing!)
309 */
310 inp->s_addr = PJ_INADDR_NONE;
311
312 /* Caution:
313 * this function might be called with cp->slen >= 16
314 * (i.e. when called with hostname to check if it's an IP addr).
315 */
316 PJ_ASSERT_RETURN(cp && cp->slen && inp, 0);
317 if (cp->slen >= 16) {
318 return 0;
319 }
320
321 char tempaddr8[MAXIPLEN];
322 pj_memcpy(tempaddr8, cp->ptr, cp->slen);
323 tempaddr8[cp->slen] = '\0';
324
325 wchar_t tempaddr16[MAXIPLEN];
326 pj_ansi_to_unicode(tempaddr8, pj_ansi_strlen(tempaddr8),
327 tempaddr16, sizeof(tempaddr16));
328
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000329 TBuf<MAXIPLEN> ip_addr((const TText*)tempaddr16);
Benny Prijonof260e462007-04-30 21:03:32 +0000330
331 TInetAddr addr;
332 addr.Init(KAfInet);
333 if (addr.Input(ip_addr) == KErrNone) {
Benny Prijonob2c96822007-05-03 13:31:21 +0000334 /* Success (Symbian IP address is in host byte order) */
335 inp->s_addr = pj_htonl(addr.Address());
Benny Prijonof260e462007-04-30 21:03:32 +0000336 return 1;
337 } else {
338 /* Error */
339 return 0;
340 }
341}
342
343/*
Benny Prijono2a78a522007-11-21 14:10:46 +0000344 * Convert text to IPv4/IPv6 address.
345 */
346PJ_DEF(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst)
347{
348 char tempaddr[PJ_INET6_ADDRSTRLEN];
349
350 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL);
351 PJ_ASSERT_RETURN(src && src->slen && dst, PJ_EINVAL);
352
353 /* Initialize output with PJ_IN_ADDR_NONE for IPv4 (to be
354 * compatible with pj_inet_aton()
355 */
356 if (af==PJ_AF_INET) {
357 ((pj_in_addr*)dst)->s_addr = PJ_INADDR_NONE;
358 }
359
360 /* Caution:
361 * this function might be called with cp->slen >= 46
362 * (i.e. when called with hostname to check if it's an IP addr).
363 */
364 if (src->slen >= PJ_INET6_ADDRSTRLEN) {
365 return PJ_ENAMETOOLONG;
366 }
367
368 pj_memcpy(tempaddr, src->ptr, src->slen);
369 tempaddr[src->slen] = '\0';
370
371
372 wchar_t tempaddr16[PJ_INET6_ADDRSTRLEN];
373 pj_ansi_to_unicode(tempaddr, pj_ansi_strlen(tempaddr),
374 tempaddr16, sizeof(tempaddr16));
375
376 TBuf<PJ_INET6_ADDRSTRLEN> ip_addr((const TText*)tempaddr16);
377
378 TInetAddr addr;
379 addr.Init(KAfInet6);
380 if (addr.Input(ip_addr) == KErrNone) {
381 if (af==PJ_AF_INET) {
382 /* Success (Symbian IP address is in host byte order) */
383 pj_uint32_t ip = pj_htonl(addr.Address());
384 pj_memcpy(dst, &ip, 4);
385 } else if (af==PJ_AF_INET6) {
386 const TIp6Addr & ip6 = addr.Ip6Address();
387 pj_memcpy(dst, ip6.u.iAddr8, 16);
388 } else {
389 pj_assert(!"Unexpected!");
390 return PJ_EBUG;
391 }
392 return PJ_SUCCESS;
393 } else {
394 /* Error */
395 return PJ_EINVAL;
396 }
397}
398
399/*
400 * Convert IPv4/IPv6 address to text.
401 */
402PJ_DEF(pj_status_t) pj_inet_ntop(int af, const void *src,
403 char *dst, int size)
404
405{
406 PJ_ASSERT_RETURN(src && dst && size, PJ_EINVAL);
407
408 *dst = '\0';
409
410 if (af==PJ_AF_INET) {
411
412 TBuf<PJ_INET_ADDRSTRLEN> str16;
413 pj_in_addr inaddr;
414
Benny Prijono9db4bd62007-12-31 11:26:21 +0000415 if (size < PJ_INET_ADDRSTRLEN)
Benny Prijono2a78a522007-11-21 14:10:46 +0000416 return PJ_ETOOSMALL;
417
418 pj_memcpy(&inaddr, src, 4);
419
420 /* Symbian IP address is in host byte order */
421 TInetAddr temp_addr((TUint32)pj_ntohl(inaddr.s_addr), (TUint)0);
422 temp_addr.Output(str16);
423
424 pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
425 dst, size);
426 return PJ_SUCCESS;
427
428 } else if (af==PJ_AF_INET6) {
429 TBuf<PJ_INET6_ADDRSTRLEN> str16;
430
Benny Prijono9db4bd62007-12-31 11:26:21 +0000431 if (size < PJ_INET6_ADDRSTRLEN)
Benny Prijono2a78a522007-11-21 14:10:46 +0000432 return PJ_ETOOSMALL;
433
434 TIp6Addr ip6;
435 pj_memcpy(ip6.u.iAddr8, src, 16);
436
437 TInetAddr temp_addr(ip6, (TUint)0);
438 temp_addr.Output(str16);
439
440 pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
441 dst, size);
442 return PJ_SUCCESS;
443
444 } else {
445 pj_assert(!"Unsupport address family");
446 return PJ_EINVAL;
447 }
448
449}
450
451/*
Benny Prijonof260e462007-04-30 21:03:32 +0000452 * Get hostname.
453 */
454PJ_DEF(const pj_str_t*) pj_gethostname(void)
455{
456 static char buf[PJ_MAX_HOSTNAME];
457 static pj_str_t hostname;
458
459 PJ_CHECK_STACK();
460
461 if (hostname.ptr == NULL) {
Benny Prijono62b86eb2007-12-01 08:52:57 +0000462 RHostResolver &resv = PjSymbianOS::Instance()->GetResolver(PJ_AF_INET);
Benny Prijonof260e462007-04-30 21:03:32 +0000463 TRequestStatus reqStatus;
464 THostName tmpName;
465
466 resv.GetHostName(tmpName, reqStatus);
467 User::WaitForRequest(reqStatus);
468
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000469 hostname.ptr = pj_unicode_to_ansi((const wchar_t*)tmpName.Ptr(), tmpName.Length(),
Benny Prijonof260e462007-04-30 21:03:32 +0000470 buf, sizeof(buf));
471 hostname.slen = tmpName.Length();
472 }
473 return &hostname;
474}
475
476/*
Benny Prijonof260e462007-04-30 21:03:32 +0000477 * Create new socket/endpoint for communication and returns a descriptor.
478 */
479PJ_DEF(pj_status_t) pj_sock_socket(int af,
480 int type,
481 int proto,
482 pj_sock_t *p_sock)
483{
484 TInt rc;
485
486 PJ_CHECK_STACK();
487
488 /* Sanity checks. */
489 PJ_ASSERT_RETURN(p_sock!=NULL, PJ_EINVAL);
490
491 /* Set proto if none is specified. */
492 if (proto == 0) {
Benny Prijono8ab968f2007-07-20 08:08:30 +0000493 if (type == pj_SOCK_STREAM())
Benny Prijonof260e462007-04-30 21:03:32 +0000494 proto = KProtocolInetTcp;
Benny Prijono8ab968f2007-07-20 08:08:30 +0000495 else if (type == pj_SOCK_DGRAM())
Benny Prijonof260e462007-04-30 21:03:32 +0000496 proto = KProtocolInetUdp;
497 }
498
499 /* Create Symbian RSocket */
500 RSocket rSock;
Benny Prijono7d3e12c2007-10-26 05:25:35 +0000501 if (PjSymbianOS::Instance()->Connection())
502 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
503 af, type, proto,
504 *PjSymbianOS::Instance()->Connection());
505 else
506 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
507 af, type, proto);
508
Benny Prijonof260e462007-04-30 21:03:32 +0000509 if (rc != KErrNone)
510 return PJ_RETURN_OS_ERROR(rc);
511
512
513 /* Wrap Symbian RSocket into PJLIB's CPjSocket, and return to caller */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000514 CPjSocket *pjSock = new CPjSocket(af, rSock);
Benny Prijonof260e462007-04-30 21:03:32 +0000515 *p_sock = (pj_sock_t)pjSock;
516
517 return PJ_SUCCESS;
518}
519
520
521/*
522 * Bind socket.
523 */
524PJ_DEF(pj_status_t) pj_sock_bind( pj_sock_t sock,
525 const pj_sockaddr_t *addr,
526 int len)
527{
Benny Prijono62b86eb2007-12-01 08:52:57 +0000528 pj_status_t status;
Benny Prijonof260e462007-04-30 21:03:32 +0000529 TInt rc;
530
531 PJ_CHECK_STACK();
532
533 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
Benny Prijono80025db2007-12-02 15:36:46 +0000534 PJ_ASSERT_RETURN(addr && len>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000535
Benny Prijono62b86eb2007-12-01 08:52:57 +0000536 // Convert PJLIB's pj_sockaddr into Symbian's TInetAddr
Benny Prijonof260e462007-04-30 21:03:32 +0000537 TInetAddr inetAddr;
Benny Prijono62b86eb2007-12-01 08:52:57 +0000538 status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)addr, len, inetAddr);
539 if (status != PJ_SUCCESS)
540 return status;
Benny Prijonof260e462007-04-30 21:03:32 +0000541
542 // Get the RSocket instance
543 RSocket &rSock = ((CPjSocket*)sock)->Socket();
544
545 // Bind
546 rc = rSock.Bind(inetAddr);
547
548 return (rc==KErrNone) ? PJ_SUCCESS : PJ_RETURN_OS_ERROR(rc);
549}
550
551
552/*
553 * Bind socket.
554 */
555PJ_DEF(pj_status_t) pj_sock_bind_in( pj_sock_t sock,
556 pj_uint32_t addr32,
557 pj_uint16_t port)
558{
559 pj_sockaddr_in addr;
560
561 PJ_CHECK_STACK();
562
Benny Prijonob2c96822007-05-03 13:31:21 +0000563 pj_bzero(&addr, sizeof(addr));
Benny Prijonof260e462007-04-30 21:03:32 +0000564 addr.sin_family = PJ_AF_INET;
565 addr.sin_addr.s_addr = pj_htonl(addr32);
566 addr.sin_port = pj_htons(port);
567
568 return pj_sock_bind(sock, &addr, sizeof(pj_sockaddr_in));
569}
570
571
572/*
573 * Close socket.
574 */
575PJ_DEF(pj_status_t) pj_sock_close(pj_sock_t sock)
576{
577 PJ_CHECK_STACK();
578
579 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
580
581 CPjSocket *pjSock = (CPjSocket*)sock;
582
583 // This will close the socket.
584 delete pjSock;
585
586 return PJ_SUCCESS;
587}
588
589/*
590 * Get remote's name.
591 */
592PJ_DEF(pj_status_t) pj_sock_getpeername( pj_sock_t sock,
593 pj_sockaddr_t *addr,
594 int *namelen)
595{
596 PJ_CHECK_STACK();
597
598 PJ_ASSERT_RETURN(sock && addr && namelen &&
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000599 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000600
601 CPjSocket *pjSock = (CPjSocket*)sock;
602 RSocket &rSock = pjSock->Socket();
603
604 // Socket must be connected.
605 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
606
607 TInetAddr inetAddr;
608 rSock.RemoteName(inetAddr);
609
Benny Prijono62b86eb2007-12-01 08:52:57 +0000610 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)addr, namelen);
Benny Prijonof260e462007-04-30 21:03:32 +0000611}
612
613/*
614 * Get socket name.
615 */
616PJ_DEF(pj_status_t) pj_sock_getsockname( pj_sock_t sock,
617 pj_sockaddr_t *addr,
618 int *namelen)
619{
620 PJ_CHECK_STACK();
621
622 PJ_ASSERT_RETURN(sock && addr && namelen &&
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000623 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000624
625 CPjSocket *pjSock = (CPjSocket*)sock;
626 RSocket &rSock = pjSock->Socket();
627
628 TInetAddr inetAddr;
629 rSock.LocalName(inetAddr);
630
Benny Prijono62b86eb2007-12-01 08:52:57 +0000631 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)addr, namelen);
Benny Prijonof260e462007-04-30 21:03:32 +0000632}
633
634/*
635 * Send data
636 */
637PJ_DEF(pj_status_t) pj_sock_send(pj_sock_t sock,
638 const void *buf,
639 pj_ssize_t *len,
640 unsigned flags)
641{
642 PJ_CHECK_STACK();
643 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
644
645 CPjSocket *pjSock = (CPjSocket*)sock;
646 RSocket &rSock = pjSock->Socket();
647
648 // send() should only be called to connected socket
649 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
650
651 TPtrC8 data((const TUint8*)buf, (TInt)*len);
652 TRequestStatus reqStatus;
653 TSockXfrLength sentLen;
654
655 rSock.Send(data, flags, reqStatus, sentLen);
656 User::WaitForRequest(reqStatus);
657
658 if (reqStatus.Int()==KErrNone) {
659 //*len = (TInt) sentLen.Length();
660 return PJ_SUCCESS;
661 } else
662 return PJ_RETURN_OS_ERROR(reqStatus.Int());
663}
664
665
666/*
667 * Send data.
668 */
669PJ_DEF(pj_status_t) pj_sock_sendto(pj_sock_t sock,
670 const void *buf,
671 pj_ssize_t *len,
672 unsigned flags,
673 const pj_sockaddr_t *to,
674 int tolen)
675{
Benny Prijono62b86eb2007-12-01 08:52:57 +0000676 pj_status_t status;
677
Benny Prijonof260e462007-04-30 21:03:32 +0000678 PJ_CHECK_STACK();
679 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
680
681 CPjSocket *pjSock = (CPjSocket*)sock;
682 RSocket &rSock = pjSock->Socket();
683
684 // Only supports AF_INET for now
Benny Prijono80025db2007-12-02 15:36:46 +0000685 PJ_ASSERT_RETURN(tolen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000686
687 TInetAddr inetAddr;
Benny Prijono62b86eb2007-12-01 08:52:57 +0000688 status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)to, tolen, inetAddr);
689 if (status != PJ_SUCCESS)
690 return status;
Benny Prijonof260e462007-04-30 21:03:32 +0000691
692 TPtrC8 data((const TUint8*)buf, (TInt)*len);
693 TRequestStatus reqStatus;
694 TSockXfrLength sentLen;
695
696 rSock.SendTo(data, inetAddr, flags, reqStatus, sentLen);
697 User::WaitForRequest(reqStatus);
698
699 if (reqStatus.Int()==KErrNone) {
700 //For some reason TSockXfrLength is not returning correctly!
701 //*len = (TInt) sentLen.Length();
702 return PJ_SUCCESS;
703 } else
704 return PJ_RETURN_OS_ERROR(reqStatus.Int());
705}
706
707/*
708 * Receive data.
709 */
710PJ_DEF(pj_status_t) pj_sock_recv(pj_sock_t sock,
711 void *buf,
712 pj_ssize_t *len,
713 unsigned flags)
714{
715 PJ_CHECK_STACK();
716
717 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
718 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
719
720 CPjSocket *pjSock = (CPjSocket*)sock;
721 RSocket &rSock = pjSock->Socket();
722
723 if (pjSock->Reader()) {
724 CPjSocketReader *reader = pjSock->Reader();
725
726 while (reader->IsActive() && !reader->HasData()) {
727 User::WaitForAnyRequest();
728 }
729
730 if (reader->HasData()) {
731 TPtr8 data((TUint8*)buf, (TInt)*len);
732 TInetAddr inetAddr;
733
734 reader->ReadData(data, &inetAddr);
735
736 *len = data.Length();
737 return PJ_SUCCESS;
738 }
739 }
740
741 TRequestStatus reqStatus;
742 TSockXfrLength recvLen;
743 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
744
745 rSock.Recv(data, flags, reqStatus, recvLen);
746 User::WaitForRequest(reqStatus);
747
748 if (reqStatus == KErrNone) {
749 //*len = (TInt)recvLen.Length();
750 *len = data.Length();
751 return PJ_SUCCESS;
752 } else {
753 *len = -1;
754 return PJ_RETURN_OS_ERROR(reqStatus.Int());
755 }
756}
757
758/*
759 * Receive data.
760 */
761PJ_DEF(pj_status_t) pj_sock_recvfrom(pj_sock_t sock,
762 void *buf,
763 pj_ssize_t *len,
764 unsigned flags,
765 pj_sockaddr_t *from,
766 int *fromlen)
767{
768 PJ_CHECK_STACK();
769
770 PJ_ASSERT_RETURN(sock && buf && len && from && fromlen, PJ_EINVAL);
771 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000772 PJ_ASSERT_RETURN(*fromlen >= (int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000773
774 CPjSocket *pjSock = (CPjSocket*)sock;
775 RSocket &rSock = pjSock->Socket();
776
777 if (pjSock->Reader()) {
778 CPjSocketReader *reader = pjSock->Reader();
779
780 while (reader->IsActive() && !reader->HasData()) {
781 User::WaitForAnyRequest();
782 }
783
784 if (reader->HasData()) {
785 TPtr8 data((TUint8*)buf, (TInt)*len);
786 TInetAddr inetAddr;
787
788 reader->ReadData(data, &inetAddr);
789
790 *len = data.Length();
791
792 if (from && fromlen) {
Benny Prijono62b86eb2007-12-01 08:52:57 +0000793 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from,
794 fromlen);
795 } else {
796 return PJ_SUCCESS;
Benny Prijonof260e462007-04-30 21:03:32 +0000797 }
Benny Prijonof260e462007-04-30 21:03:32 +0000798 }
799 }
800
801 TInetAddr inetAddr;
802 TRequestStatus reqStatus;
803 TSockXfrLength recvLen;
804 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
805
806 rSock.RecvFrom(data, inetAddr, flags, reqStatus, recvLen);
807 User::WaitForRequest(reqStatus);
808
809 if (reqStatus == KErrNone) {
810 //*len = (TInt)recvLen.Length();
811 *len = data.Length();
Benny Prijono62b86eb2007-12-01 08:52:57 +0000812 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from, fromlen);
Benny Prijonof260e462007-04-30 21:03:32 +0000813 } else {
814 *len = -1;
815 *fromlen = -1;
816 return PJ_RETURN_OS_ERROR(reqStatus.Int());
817 }
818}
819
820/*
821 * Get socket option.
822 */
823PJ_DEF(pj_status_t) pj_sock_getsockopt( pj_sock_t sock,
824 pj_uint16_t level,
825 pj_uint16_t optname,
826 void *optval,
827 int *optlen)
828{
829 // Not supported for now.
830 PJ_UNUSED_ARG(sock);
831 PJ_UNUSED_ARG(level);
832 PJ_UNUSED_ARG(optname);
833 PJ_UNUSED_ARG(optval);
834 PJ_UNUSED_ARG(optlen);
835 return PJ_EINVALIDOP;
836}
837
838/*
839 * Set socket option.
840 */
841PJ_DEF(pj_status_t) pj_sock_setsockopt( pj_sock_t sock,
842 pj_uint16_t level,
843 pj_uint16_t optname,
844 const void *optval,
845 int optlen)
846{
847 // Not supported for now.
848 PJ_UNUSED_ARG(sock);
849 PJ_UNUSED_ARG(level);
850 PJ_UNUSED_ARG(optname);
851 PJ_UNUSED_ARG(optval);
852 PJ_UNUSED_ARG(optlen);
853 return PJ_EINVALIDOP;
854}
855
856/*
857 * Connect socket.
858 */
859PJ_DEF(pj_status_t) pj_sock_connect( pj_sock_t sock,
860 const pj_sockaddr_t *addr,
861 int namelen)
862{
Benny Prijono62b86eb2007-12-01 08:52:57 +0000863 pj_status_t status;
864
Benny Prijonof260e462007-04-30 21:03:32 +0000865 PJ_CHECK_STACK();
866
867 PJ_ASSERT_RETURN(sock && addr && namelen, PJ_EINVAL);
868 PJ_ASSERT_RETURN(((pj_sockaddr*)addr)->addr.sa_family == PJ_AF_INET,
869 PJ_EINVAL);
870
871 CPjSocket *pjSock = (CPjSocket*)sock;
872 RSocket &rSock = pjSock->Socket();
873
874 TInetAddr inetAddr;
875 TRequestStatus reqStatus;
876
Benny Prijono62b86eb2007-12-01 08:52:57 +0000877 status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)addr, namelen, inetAddr);
878 if (status != PJ_SUCCESS)
879 return status;
Benny Prijonof260e462007-04-30 21:03:32 +0000880
881 rSock.Connect(inetAddr, reqStatus);
882 User::WaitForRequest(reqStatus);
883
884 if (reqStatus == KErrNone) {
885 pjSock->SetConnected(true);
886 return PJ_SUCCESS;
887 } else {
888 return PJ_RETURN_OS_ERROR(reqStatus.Int());
889 }
890}
891
892
893/*
894 * Shutdown socket.
895 */
896#if PJ_HAS_TCP
897PJ_DEF(pj_status_t) pj_sock_shutdown( pj_sock_t sock,
898 int how)
899{
900 PJ_CHECK_STACK();
901
902 PJ_ASSERT_RETURN(sock, PJ_EINVAL);
903
904 CPjSocket *pjSock = (CPjSocket*)sock;
905 RSocket &rSock = pjSock->Socket();
906
907 RSocket::TShutdown aHow;
908 if (how == PJ_SD_RECEIVE)
909 aHow = RSocket::EStopInput;
910 else if (how == PJ_SHUT_WR)
911 aHow = RSocket::EStopOutput;
912 else
913 aHow = RSocket::ENormal;
914
915 TRequestStatus reqStatus;
916
917 rSock.Shutdown(aHow, reqStatus);
918 User::WaitForRequest(reqStatus);
919
920 if (reqStatus == KErrNone) {
921 return PJ_SUCCESS;
922 } else {
923 return PJ_RETURN_OS_ERROR(reqStatus.Int());
924 }
925}
926
927/*
928 * Start listening to incoming connections.
929 */
930PJ_DEF(pj_status_t) pj_sock_listen( pj_sock_t sock,
931 int backlog)
932{
933 PJ_CHECK_STACK();
934
935 PJ_ASSERT_RETURN(sock && backlog, PJ_EINVAL);
936
937 CPjSocket *pjSock = (CPjSocket*)sock;
938 RSocket &rSock = pjSock->Socket();
939
940 TInt rc = rSock.Listen((TUint)backlog);
941
942 if (rc == KErrNone) {
943 return PJ_SUCCESS;
944 } else {
945 return PJ_RETURN_OS_ERROR(rc);
946 }
947}
948
949/*
950 * Accept incoming connections
951 */
952PJ_DEF(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
953 pj_sock_t *newsock,
954 pj_sockaddr_t *addr,
955 int *addrlen)
956{
957 PJ_CHECK_STACK();
958
959 PJ_ASSERT_RETURN(serverfd && newsock, PJ_EINVAL);
960
961 CPjSocket *pjSock = (CPjSocket*)serverfd;
962 RSocket &rSock = pjSock->Socket();
963
964 // Create a 'blank' socket
965 RSocket newSock;
966 newSock.Open(PjSymbianOS::Instance()->SocketServ());
967
968 // Call Accept()
969 TRequestStatus reqStatus;
970
971 rSock.Accept(newSock, reqStatus);
972 User::WaitForRequest(reqStatus);
973
974 if (reqStatus != KErrNone) {
975 return PJ_RETURN_OS_ERROR(reqStatus.Int());
976 }
977
978 // Create PJ socket
Benny Prijono62b86eb2007-12-01 08:52:57 +0000979 CPjSocket *newPjSock = new CPjSocket(pjSock->GetAf(), newSock);
Benny Prijonof260e462007-04-30 21:03:32 +0000980 newPjSock->SetConnected(true);
981
982 *newsock = (pj_sock_t) newPjSock;
983
984 if (addr && addrlen) {
985 return pj_sock_getpeername(*newsock, addr, addrlen);
986 }
987
988 return PJ_SUCCESS;
989}
990#endif /* PJ_HAS_TCP */
991
992