blob: 38960917591025837ced14fd2583f5925a1c40db [file] [log] [blame]
Benny Prijonof260e462007-04-30 21:03:32 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pj/sock.h>
20#include <pj/addr_resolv.h>
21#include <pj/assert.h>
22#include <pj/errno.h>
23#include <pj/os.h>
24#include <pj/string.h>
25#include <pj/unicode.h>
26
27#include "os_symbian.h"
28
29
30/*
31 * Address families.
32 */
33const pj_uint16_t PJ_AF_UNIX = 0xFFFF;
34const pj_uint16_t PJ_AF_INET = KAfInet;
35const pj_uint16_t PJ_AF_INET6 = KAfInet6;
36const pj_uint16_t PJ_AF_PACKET = 0xFFFF;
37const pj_uint16_t PJ_AF_IRDA = 0xFFFF;
38
39/*
40 * Socket types conversion.
41 * The values here are indexed based on pj_sock_type
42 */
43const pj_uint16_t PJ_SOCK_STREAM= KSockStream;
44const pj_uint16_t PJ_SOCK_DGRAM = KSockDatagram;
45const pj_uint16_t PJ_SOCK_RAW = 0xFFFF;
46const pj_uint16_t PJ_SOCK_RDM = 0xFFFF;
47
48/* setsockop() is not really supported. */
49const pj_uint16_t PJ_SOL_SOCKET = 0xFFFF;
50const pj_uint16_t PJ_SOL_IP = 0xFFFF;
51const pj_uint16_t PJ_SOL_TCP = 0xFFFF;
52const pj_uint16_t PJ_SOL_UDP = 0xFFFF;
53const pj_uint16_t PJ_SOL_IPV6 = 0xFFFF;
54
55/* ioctl() is also not supported. */
56const pj_uint16_t PJ_SO_TYPE = 0xFFFF;
57const pj_uint16_t PJ_SO_RCVBUF = 0xFFFF;
58const pj_uint16_t PJ_SO_SNDBUF = 0xFFFF;
59
60
61/////////////////////////////////////////////////////////////////////////////
62//
63// CPjSocket implementation.
64// (declaration is in os_symbian.h)
65//
66
67CPjSocket::~CPjSocket()
68{
Benny Prijono897f9f82007-05-03 19:56:21 +000069 DestroyReader();
Benny Prijonof260e462007-04-30 21:03:32 +000070 sock_.Close();
71}
72
73
74// Create socket reader.
75CPjSocketReader *CPjSocket::CreateReader(unsigned max_len)
76{
77 pj_assert(sockReader_ == NULL);
78 return sockReader_ = CPjSocketReader::NewL(*this, max_len);
79}
80
Benny Prijono897f9f82007-05-03 19:56:21 +000081// Delete socket reader when it's not wanted.
82void CPjSocket::DestroyReader()
83{
84 if (sockReader_) {
85 if (sockReader_->IsActive())
86 sockReader_->Cancel();
87 delete sockReader_;
88 sockReader_ = NULL;
89 }
90}
91
Benny Prijonof260e462007-04-30 21:03:32 +000092
93/////////////////////////////////////////////////////////////////////////////
94//
95// CPjSocketReader implementation
96// (declaration in os_symbian.h)
97//
98
99
100CPjSocketReader::CPjSocketReader(CPjSocket &sock)
101: CActive(EPriorityStandard),
102 sock_(sock), buffer_(NULL, 0), readCb_(NULL), key_(NULL)
103{
104}
105
106
107void CPjSocketReader::ConstructL(unsigned max_len)
108{
109 TProtocolDesc aProtocol;
110 TInt err;
111
112 err = sock_.Socket().Info(aProtocol);
113 User::LeaveIfError(err);
114
115 isDatagram_ = (aProtocol.iSockType == KSockDatagram);
116
117 TUint8 *ptr = new TUint8[max_len];
118 buffer_.Set(ptr, 0, (TInt)max_len);
119 CActiveScheduler::Add(this);
120}
121
122CPjSocketReader *CPjSocketReader::NewL(CPjSocket &sock, unsigned max_len)
123{
124 CPjSocketReader *self = new (ELeave) CPjSocketReader(sock);
125 CleanupStack::PushL(self);
126 self->ConstructL(max_len);
127 CleanupStack::Pop(self);
128
129 return self;
130}
131
132
133CPjSocketReader::~CPjSocketReader()
134{
135 const TUint8 *data = buffer_.Ptr();
136 delete [] data;
137}
138
139void CPjSocketReader::StartRecv(void (*cb)(void *key),
140 void *key,
141 TDes8 *aDesc,
142 TUint flags)
143{
144 StartRecvFrom(cb, key, aDesc, flags, NULL);
145}
146
147void CPjSocketReader::StartRecvFrom(void (*cb)(void *key),
148 void *key,
149 TDes8 *aDesc,
150 TUint flags,
151 TSockAddr *fromAddr)
152{
153 readCb_ = cb;
154 key_ = key;
155
156 if (aDesc == NULL) aDesc = &buffer_;
157 if (fromAddr == NULL) fromAddr = &recvAddr_;
158
159 sock_.Socket().RecvFrom(*aDesc, *fromAddr, flags, iStatus);
Benny Prijono5d542642007-05-02 18:54:19 +0000160 SetActive();
Benny Prijonof260e462007-04-30 21:03:32 +0000161}
162
163void CPjSocketReader::DoCancel()
164{
165 sock_.Socket().CancelRecv();
166}
167
168void CPjSocketReader::RunL()
169{
170 void (*old_cb)(void *key) = readCb_;
171 void *old_key = key_;
Benny Prijonof260e462007-04-30 21:03:32 +0000172
173 readCb_ = NULL;
174 key_ = NULL;
175
176 if (old_cb) {
177 (*old_cb)(old_key);
178 }
179}
180
181// Append data to aDesc, up to aDesc's maximum size.
182// If socket is datagram based, buffer_ will be clared.
183void CPjSocketReader::ReadData(TDes8 &aDesc, TInetAddr *addr)
184{
185 if (isDatagram_)
186 aDesc.Zero();
187
188 if (buffer_.Length() == 0)
189 return;
190
191 TInt size_to_copy = aDesc.MaxLength() - aDesc.Length();
192 if (size_to_copy > buffer_.Length())
193 size_to_copy = buffer_.Length();
194
195 aDesc.Append(buffer_.Ptr(), size_to_copy);
196
197 if (isDatagram_)
198 buffer_.Zero();
199 else
200 buffer_.Delete(0, size_to_copy);
201
202 if (addr)
203 *addr = recvAddr_;
204}
205
206
207
208/////////////////////////////////////////////////////////////////////////////
209//
210// PJLIB's sock.h implementation
211//
212
213/*
214 * Convert 16-bit value from network byte order to host byte order.
215 */
216PJ_DEF(pj_uint16_t) pj_ntohs(pj_uint16_t netshort)
217{
Benny Prijonob2c96822007-05-03 13:31:21 +0000218#if PJ_IS_LITTLE_ENDIAN
219 return pj_swap16(netshort);
220#else
Benny Prijonof260e462007-04-30 21:03:32 +0000221 return netshort;
Benny Prijonob2c96822007-05-03 13:31:21 +0000222#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000223}
224
225/*
226 * Convert 16-bit value from host byte order to network byte order.
227 */
228PJ_DEF(pj_uint16_t) pj_htons(pj_uint16_t hostshort)
229{
Benny Prijonob2c96822007-05-03 13:31:21 +0000230#if PJ_IS_LITTLE_ENDIAN
231 return pj_swap16(hostshort);
232#else
Benny Prijonof260e462007-04-30 21:03:32 +0000233 return hostshort;
Benny Prijonob2c96822007-05-03 13:31:21 +0000234#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000235}
236
237/*
238 * Convert 32-bit value from network byte order to host byte order.
239 */
240PJ_DEF(pj_uint32_t) pj_ntohl(pj_uint32_t netlong)
241{
Benny Prijonob2c96822007-05-03 13:31:21 +0000242#if PJ_IS_LITTLE_ENDIAN
243 return pj_swap32(netlong);
244#else
Benny Prijonof260e462007-04-30 21:03:32 +0000245 return netlong;
Benny Prijonob2c96822007-05-03 13:31:21 +0000246#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000247}
248
249/*
250 * Convert 32-bit value from host byte order to network byte order.
251 */
252PJ_DEF(pj_uint32_t) pj_htonl(pj_uint32_t hostlong)
253{
Benny Prijonob2c96822007-05-03 13:31:21 +0000254#if PJ_IS_LITTLE_ENDIAN
255 return pj_swap32(hostlong);
256#else
257 return netlong;
258#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000259}
260
261/*
262 * Convert an Internet host address given in network byte order
263 * to string in standard numbers and dots notation.
264 */
265PJ_DEF(char*) pj_inet_ntoa(pj_in_addr inaddr)
266{
267 static TBuf<20> str16;
268 static char str8[20];
269
Benny Prijonob2c96822007-05-03 13:31:21 +0000270 /* (Symbian IP address is in host byte order) */
271 TInetAddr temp_addr((TUint32)pj_ntohl(inaddr.s_addr), (TUint)0);
Benny Prijonof260e462007-04-30 21:03:32 +0000272 temp_addr.Output(str16);
273
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000274 return pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
Benny Prijonof260e462007-04-30 21:03:32 +0000275 str8, sizeof(str8));
276}
277
278/*
279 * This function converts the Internet host address cp from the standard
280 * numbers-and-dots notation into binary data and stores it in the structure
281 * that inp points to.
282 */
283PJ_DEF(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp)
284{
285 enum { MAXIPLEN = 16 };
286
287 /* Initialize output with PJ_INADDR_NONE.
288 * Some apps relies on this instead of the return value
289 * (and anyway the return value is quite confusing!)
290 */
291 inp->s_addr = PJ_INADDR_NONE;
292
293 /* Caution:
294 * this function might be called with cp->slen >= 16
295 * (i.e. when called with hostname to check if it's an IP addr).
296 */
297 PJ_ASSERT_RETURN(cp && cp->slen && inp, 0);
298 if (cp->slen >= 16) {
299 return 0;
300 }
301
302 char tempaddr8[MAXIPLEN];
303 pj_memcpy(tempaddr8, cp->ptr, cp->slen);
304 tempaddr8[cp->slen] = '\0';
305
306 wchar_t tempaddr16[MAXIPLEN];
307 pj_ansi_to_unicode(tempaddr8, pj_ansi_strlen(tempaddr8),
308 tempaddr16, sizeof(tempaddr16));
309
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000310 TBuf<MAXIPLEN> ip_addr((const TText*)tempaddr16);
Benny Prijonof260e462007-04-30 21:03:32 +0000311
312 TInetAddr addr;
313 addr.Init(KAfInet);
314 if (addr.Input(ip_addr) == KErrNone) {
Benny Prijonob2c96822007-05-03 13:31:21 +0000315 /* Success (Symbian IP address is in host byte order) */
316 inp->s_addr = pj_htonl(addr.Address());
Benny Prijonof260e462007-04-30 21:03:32 +0000317 return 1;
318 } else {
319 /* Error */
320 return 0;
321 }
322}
323
324/*
325 * Convert address string with numbers and dots to binary IP address.
326 */
327PJ_DEF(pj_in_addr) pj_inet_addr(const pj_str_t *cp)
328{
329 pj_in_addr addr;
330
331 pj_inet_aton(cp, &addr);
332 return addr;
333}
334
335/*
336 * Convert address string with numbers and dots to binary IP address.
337 */
338PJ_DEF(pj_in_addr) pj_inet_addr2(const char *cp)
339{
340 pj_str_t str = pj_str((char*)cp);
341 return pj_inet_addr(&str);
342}
343
344/*
345 * Set the IP address of an IP socket address from string address,
346 * with resolving the host if necessary. The string address may be in a
347 * standard numbers and dots notation or may be a hostname. If hostname
348 * is specified, then the function will resolve the host into the IP
349 * address.
350 */
351PJ_DEF(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
352 const pj_str_t *str_addr)
353{
354 PJ_CHECK_STACK();
355
356 PJ_ASSERT_RETURN(!str_addr || str_addr->slen < PJ_MAX_HOSTNAME,
357 (addr->sin_addr.s_addr=PJ_INADDR_NONE, PJ_EINVAL));
358
359 addr->sin_family = PJ_AF_INET;
360 pj_memset(addr->sin_zero, 0, sizeof(addr->sin_zero));
361
362 if (str_addr && str_addr->slen) {
363 addr->sin_addr = pj_inet_addr(str_addr);
364 if (addr->sin_addr.s_addr == PJ_INADDR_NONE) {
365 pj_hostent he;
366 pj_status_t rc;
367
368 rc = pj_gethostbyname(str_addr, &he);
369 if (rc == 0) {
370 addr->sin_addr.s_addr = *(pj_uint32_t*)he.h_addr;
371 } else {
372 addr->sin_addr.s_addr = PJ_INADDR_NONE;
373 return rc;
374 }
375 }
376
377 } else {
378 addr->sin_addr.s_addr = 0;
379 }
380
381 return PJ_SUCCESS;
382}
383
384/*
385 * Set the IP address and port of an IP socket address.
386 * The string address may be in a standard numbers and dots notation or
387 * may be a hostname. If hostname is specified, then the function will
388 * resolve the host into the IP address.
389 */
390PJ_DEF(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
391 const pj_str_t *str_addr,
392 pj_uint16_t port)
393{
394 PJ_ASSERT_RETURN(addr, (addr->sin_addr.s_addr=PJ_INADDR_NONE, PJ_EINVAL));
395
396 addr->sin_family = PJ_AF_INET;
397 pj_memset(addr->sin_zero, 0, sizeof(addr->sin_zero));
398 pj_sockaddr_in_set_port(addr, port);
399 return pj_sockaddr_in_set_str_addr(addr, str_addr);
400}
401
402
403/*
404 * Get hostname.
405 */
406PJ_DEF(const pj_str_t*) pj_gethostname(void)
407{
408 static char buf[PJ_MAX_HOSTNAME];
409 static pj_str_t hostname;
410
411 PJ_CHECK_STACK();
412
413 if (hostname.ptr == NULL) {
414 RHostResolver & resv = PjSymbianOS::Instance()->GetResolver();
415 TRequestStatus reqStatus;
416 THostName tmpName;
417
418 resv.GetHostName(tmpName, reqStatus);
419 User::WaitForRequest(reqStatus);
420
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000421 hostname.ptr = pj_unicode_to_ansi((const wchar_t*)tmpName.Ptr(), tmpName.Length(),
Benny Prijonof260e462007-04-30 21:03:32 +0000422 buf, sizeof(buf));
423 hostname.slen = tmpName.Length();
424 }
425 return &hostname;
426}
427
428/*
429 * Get first IP address associated with the hostname.
430 */
431PJ_DEF(pj_in_addr) pj_gethostaddr(void)
432{
433 pj_sockaddr_in addr;
434 const pj_str_t *hostname = pj_gethostname();
435
436 pj_sockaddr_in_set_str_addr(&addr, hostname);
437 return addr.sin_addr;
438}
439
440
441/*
442 * Create new socket/endpoint for communication and returns a descriptor.
443 */
444PJ_DEF(pj_status_t) pj_sock_socket(int af,
445 int type,
446 int proto,
447 pj_sock_t *p_sock)
448{
449 TInt rc;
450
451 PJ_CHECK_STACK();
452
453 /* Sanity checks. */
454 PJ_ASSERT_RETURN(p_sock!=NULL, PJ_EINVAL);
455
456 /* Set proto if none is specified. */
457 if (proto == 0) {
458 if (type == PJ_SOCK_STREAM)
459 proto = KProtocolInetTcp;
460 else if (type == PJ_SOCK_DGRAM)
461 proto = KProtocolInetUdp;
462 }
463
464 /* Create Symbian RSocket */
465 RSocket rSock;
466 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(), af, type, proto);
467 if (rc != KErrNone)
468 return PJ_RETURN_OS_ERROR(rc);
469
470
471 /* Wrap Symbian RSocket into PJLIB's CPjSocket, and return to caller */
472 CPjSocket *pjSock = new CPjSocket(rSock);
473 *p_sock = (pj_sock_t)pjSock;
474
475 return PJ_SUCCESS;
476}
477
478
479/*
480 * Bind socket.
481 */
482PJ_DEF(pj_status_t) pj_sock_bind( pj_sock_t sock,
483 const pj_sockaddr_t *addr,
484 int len)
485{
486 TInt rc;
487
488 PJ_CHECK_STACK();
489
490 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
491 PJ_ASSERT_RETURN(addr && len == sizeof(pj_sockaddr_in), PJ_EINVAL);
492
493 // Convert PJLIB's pj_sockaddr_in into Symbian's TInetAddr
494 TInetAddr inetAddr;
495 PjSymbianOS::pj2Addr(*(pj_sockaddr_in*)addr, inetAddr);
496
497 // Get the RSocket instance
498 RSocket &rSock = ((CPjSocket*)sock)->Socket();
499
500 // Bind
501 rc = rSock.Bind(inetAddr);
502
503 return (rc==KErrNone) ? PJ_SUCCESS : PJ_RETURN_OS_ERROR(rc);
504}
505
506
507/*
508 * Bind socket.
509 */
510PJ_DEF(pj_status_t) pj_sock_bind_in( pj_sock_t sock,
511 pj_uint32_t addr32,
512 pj_uint16_t port)
513{
514 pj_sockaddr_in addr;
515
516 PJ_CHECK_STACK();
517
Benny Prijonob2c96822007-05-03 13:31:21 +0000518 pj_bzero(&addr, sizeof(addr));
Benny Prijonof260e462007-04-30 21:03:32 +0000519 addr.sin_family = PJ_AF_INET;
520 addr.sin_addr.s_addr = pj_htonl(addr32);
521 addr.sin_port = pj_htons(port);
522
523 return pj_sock_bind(sock, &addr, sizeof(pj_sockaddr_in));
524}
525
526
527/*
528 * Close socket.
529 */
530PJ_DEF(pj_status_t) pj_sock_close(pj_sock_t sock)
531{
532 PJ_CHECK_STACK();
533
534 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
535
536 CPjSocket *pjSock = (CPjSocket*)sock;
537
538 // This will close the socket.
539 delete pjSock;
540
541 return PJ_SUCCESS;
542}
543
544/*
545 * Get remote's name.
546 */
547PJ_DEF(pj_status_t) pj_sock_getpeername( pj_sock_t sock,
548 pj_sockaddr_t *addr,
549 int *namelen)
550{
551 PJ_CHECK_STACK();
552
553 PJ_ASSERT_RETURN(sock && addr && namelen &&
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000554 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000555
556 CPjSocket *pjSock = (CPjSocket*)sock;
557 RSocket &rSock = pjSock->Socket();
558
559 // Socket must be connected.
560 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
561
562 TInetAddr inetAddr;
563 rSock.RemoteName(inetAddr);
564
565 PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr_in*)addr);
566 *namelen = sizeof(pj_sockaddr_in);
567
568 return PJ_SUCCESS;
569}
570
571/*
572 * Get socket name.
573 */
574PJ_DEF(pj_status_t) pj_sock_getsockname( pj_sock_t sock,
575 pj_sockaddr_t *addr,
576 int *namelen)
577{
578 PJ_CHECK_STACK();
579
580 PJ_ASSERT_RETURN(sock && addr && namelen &&
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000581 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000582
583 CPjSocket *pjSock = (CPjSocket*)sock;
584 RSocket &rSock = pjSock->Socket();
585
586 TInetAddr inetAddr;
587 rSock.LocalName(inetAddr);
588
589 PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr_in*)addr);
590 *namelen = sizeof(pj_sockaddr_in);
591
592 return PJ_SUCCESS;
593}
594
595/*
596 * Send data
597 */
598PJ_DEF(pj_status_t) pj_sock_send(pj_sock_t sock,
599 const void *buf,
600 pj_ssize_t *len,
601 unsigned flags)
602{
603 PJ_CHECK_STACK();
604 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
605
606 CPjSocket *pjSock = (CPjSocket*)sock;
607 RSocket &rSock = pjSock->Socket();
608
609 // send() should only be called to connected socket
610 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
611
612 TPtrC8 data((const TUint8*)buf, (TInt)*len);
613 TRequestStatus reqStatus;
614 TSockXfrLength sentLen;
615
616 rSock.Send(data, flags, reqStatus, sentLen);
617 User::WaitForRequest(reqStatus);
618
619 if (reqStatus.Int()==KErrNone) {
620 //*len = (TInt) sentLen.Length();
621 return PJ_SUCCESS;
622 } else
623 return PJ_RETURN_OS_ERROR(reqStatus.Int());
624}
625
626
627/*
628 * Send data.
629 */
630PJ_DEF(pj_status_t) pj_sock_sendto(pj_sock_t sock,
631 const void *buf,
632 pj_ssize_t *len,
633 unsigned flags,
634 const pj_sockaddr_t *to,
635 int tolen)
636{
637 PJ_CHECK_STACK();
638 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
639
640 CPjSocket *pjSock = (CPjSocket*)sock;
641 RSocket &rSock = pjSock->Socket();
642
643 // Only supports AF_INET for now
644 PJ_ASSERT_RETURN(tolen==sizeof(pj_sockaddr_in) &&
645 ((pj_sockaddr*)to)->addr.sa_family == PJ_AF_INET,
646 PJ_EINVAL);
647
648 TInetAddr inetAddr;
649 PjSymbianOS::pj2Addr(*(pj_sockaddr_in*)to, inetAddr);
650
651 TPtrC8 data((const TUint8*)buf, (TInt)*len);
652 TRequestStatus reqStatus;
653 TSockXfrLength sentLen;
654
655 rSock.SendTo(data, inetAddr, flags, reqStatus, sentLen);
656 User::WaitForRequest(reqStatus);
657
658 if (reqStatus.Int()==KErrNone) {
659 //For some reason TSockXfrLength is not returning correctly!
660 //*len = (TInt) sentLen.Length();
661 return PJ_SUCCESS;
662 } else
663 return PJ_RETURN_OS_ERROR(reqStatus.Int());
664}
665
666/*
667 * Receive data.
668 */
669PJ_DEF(pj_status_t) pj_sock_recv(pj_sock_t sock,
670 void *buf,
671 pj_ssize_t *len,
672 unsigned flags)
673{
674 PJ_CHECK_STACK();
675
676 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
677 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
678
679 CPjSocket *pjSock = (CPjSocket*)sock;
680 RSocket &rSock = pjSock->Socket();
681
682 if (pjSock->Reader()) {
683 CPjSocketReader *reader = pjSock->Reader();
684
685 while (reader->IsActive() && !reader->HasData()) {
686 User::WaitForAnyRequest();
687 }
688
689 if (reader->HasData()) {
690 TPtr8 data((TUint8*)buf, (TInt)*len);
691 TInetAddr inetAddr;
692
693 reader->ReadData(data, &inetAddr);
694
695 *len = data.Length();
696 return PJ_SUCCESS;
697 }
698 }
699
700 TRequestStatus reqStatus;
701 TSockXfrLength recvLen;
702 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
703
704 rSock.Recv(data, flags, reqStatus, recvLen);
705 User::WaitForRequest(reqStatus);
706
707 if (reqStatus == KErrNone) {
708 //*len = (TInt)recvLen.Length();
709 *len = data.Length();
710 return PJ_SUCCESS;
711 } else {
712 *len = -1;
713 return PJ_RETURN_OS_ERROR(reqStatus.Int());
714 }
715}
716
717/*
718 * Receive data.
719 */
720PJ_DEF(pj_status_t) pj_sock_recvfrom(pj_sock_t sock,
721 void *buf,
722 pj_ssize_t *len,
723 unsigned flags,
724 pj_sockaddr_t *from,
725 int *fromlen)
726{
727 PJ_CHECK_STACK();
728
729 PJ_ASSERT_RETURN(sock && buf && len && from && fromlen, PJ_EINVAL);
730 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000731 PJ_ASSERT_RETURN(*fromlen >= (int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000732
733 CPjSocket *pjSock = (CPjSocket*)sock;
734 RSocket &rSock = pjSock->Socket();
735
736 if (pjSock->Reader()) {
737 CPjSocketReader *reader = pjSock->Reader();
738
739 while (reader->IsActive() && !reader->HasData()) {
740 User::WaitForAnyRequest();
741 }
742
743 if (reader->HasData()) {
744 TPtr8 data((TUint8*)buf, (TInt)*len);
745 TInetAddr inetAddr;
746
747 reader->ReadData(data, &inetAddr);
748
749 *len = data.Length();
750
751 if (from && fromlen) {
752 PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr_in*)from);
753 *fromlen = sizeof(pj_sockaddr_in);
754 }
755 return PJ_SUCCESS;
756 }
757 }
758
759 TInetAddr inetAddr;
760 TRequestStatus reqStatus;
761 TSockXfrLength recvLen;
762 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
763
764 rSock.RecvFrom(data, inetAddr, flags, reqStatus, recvLen);
765 User::WaitForRequest(reqStatus);
766
767 if (reqStatus == KErrNone) {
768 //*len = (TInt)recvLen.Length();
769 *len = data.Length();
770 *fromlen = sizeof(pj_sockaddr_in);
771 PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr_in*)from);
772 return PJ_SUCCESS;
773 } else {
774 *len = -1;
775 *fromlen = -1;
776 return PJ_RETURN_OS_ERROR(reqStatus.Int());
777 }
778}
779
780/*
781 * Get socket option.
782 */
783PJ_DEF(pj_status_t) pj_sock_getsockopt( pj_sock_t sock,
784 pj_uint16_t level,
785 pj_uint16_t optname,
786 void *optval,
787 int *optlen)
788{
789 // Not supported for now.
790 PJ_UNUSED_ARG(sock);
791 PJ_UNUSED_ARG(level);
792 PJ_UNUSED_ARG(optname);
793 PJ_UNUSED_ARG(optval);
794 PJ_UNUSED_ARG(optlen);
795 return PJ_EINVALIDOP;
796}
797
798/*
799 * Set socket option.
800 */
801PJ_DEF(pj_status_t) pj_sock_setsockopt( pj_sock_t sock,
802 pj_uint16_t level,
803 pj_uint16_t optname,
804 const void *optval,
805 int optlen)
806{
807 // Not supported for now.
808 PJ_UNUSED_ARG(sock);
809 PJ_UNUSED_ARG(level);
810 PJ_UNUSED_ARG(optname);
811 PJ_UNUSED_ARG(optval);
812 PJ_UNUSED_ARG(optlen);
813 return PJ_EINVALIDOP;
814}
815
816/*
817 * Connect socket.
818 */
819PJ_DEF(pj_status_t) pj_sock_connect( pj_sock_t sock,
820 const pj_sockaddr_t *addr,
821 int namelen)
822{
823 PJ_CHECK_STACK();
824
825 PJ_ASSERT_RETURN(sock && addr && namelen, PJ_EINVAL);
826 PJ_ASSERT_RETURN(((pj_sockaddr*)addr)->addr.sa_family == PJ_AF_INET,
827 PJ_EINVAL);
828
829 CPjSocket *pjSock = (CPjSocket*)sock;
830 RSocket &rSock = pjSock->Socket();
831
832 TInetAddr inetAddr;
833 TRequestStatus reqStatus;
834
835 PjSymbianOS::pj2Addr(*(pj_sockaddr_in*)addr, inetAddr);
836
837 rSock.Connect(inetAddr, reqStatus);
838 User::WaitForRequest(reqStatus);
839
840 if (reqStatus == KErrNone) {
841 pjSock->SetConnected(true);
842 return PJ_SUCCESS;
843 } else {
844 return PJ_RETURN_OS_ERROR(reqStatus.Int());
845 }
846}
847
848
849/*
850 * Shutdown socket.
851 */
852#if PJ_HAS_TCP
853PJ_DEF(pj_status_t) pj_sock_shutdown( pj_sock_t sock,
854 int how)
855{
856 PJ_CHECK_STACK();
857
858 PJ_ASSERT_RETURN(sock, PJ_EINVAL);
859
860 CPjSocket *pjSock = (CPjSocket*)sock;
861 RSocket &rSock = pjSock->Socket();
862
863 RSocket::TShutdown aHow;
864 if (how == PJ_SD_RECEIVE)
865 aHow = RSocket::EStopInput;
866 else if (how == PJ_SHUT_WR)
867 aHow = RSocket::EStopOutput;
868 else
869 aHow = RSocket::ENormal;
870
871 TRequestStatus reqStatus;
872
873 rSock.Shutdown(aHow, reqStatus);
874 User::WaitForRequest(reqStatus);
875
876 if (reqStatus == KErrNone) {
877 return PJ_SUCCESS;
878 } else {
879 return PJ_RETURN_OS_ERROR(reqStatus.Int());
880 }
881}
882
883/*
884 * Start listening to incoming connections.
885 */
886PJ_DEF(pj_status_t) pj_sock_listen( pj_sock_t sock,
887 int backlog)
888{
889 PJ_CHECK_STACK();
890
891 PJ_ASSERT_RETURN(sock && backlog, PJ_EINVAL);
892
893 CPjSocket *pjSock = (CPjSocket*)sock;
894 RSocket &rSock = pjSock->Socket();
895
896 TInt rc = rSock.Listen((TUint)backlog);
897
898 if (rc == KErrNone) {
899 return PJ_SUCCESS;
900 } else {
901 return PJ_RETURN_OS_ERROR(rc);
902 }
903}
904
905/*
906 * Accept incoming connections
907 */
908PJ_DEF(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
909 pj_sock_t *newsock,
910 pj_sockaddr_t *addr,
911 int *addrlen)
912{
913 PJ_CHECK_STACK();
914
915 PJ_ASSERT_RETURN(serverfd && newsock, PJ_EINVAL);
916
917 CPjSocket *pjSock = (CPjSocket*)serverfd;
918 RSocket &rSock = pjSock->Socket();
919
920 // Create a 'blank' socket
921 RSocket newSock;
922 newSock.Open(PjSymbianOS::Instance()->SocketServ());
923
924 // Call Accept()
925 TRequestStatus reqStatus;
926
927 rSock.Accept(newSock, reqStatus);
928 User::WaitForRequest(reqStatus);
929
930 if (reqStatus != KErrNone) {
931 return PJ_RETURN_OS_ERROR(reqStatus.Int());
932 }
933
934 // Create PJ socket
935 CPjSocket *newPjSock = new CPjSocket(newSock);
936 newPjSock->SetConnected(true);
937
938 *newsock = (pj_sock_t) newPjSock;
939
940 if (addr && addrlen) {
941 return pj_sock_getpeername(*newsock, addr, addrlen);
942 }
943
944 return PJ_SUCCESS;
945}
946#endif /* PJ_HAS_TCP */
947
948