Debian Codegraph @

Xpjproject/pjlib/src/pj/sock_symbian.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 */
33 const pj_uint16_t PJ_AF_UNSPEC = KAFUnspec;
34 const pj_uint16_t PJ_AF_UNIX = 0xFFFF;
35 const pj_uint16_t PJ_AF_INET = KAfInet;
36 const pj_uint16_t PJ_AF_INET6 = KAfInet6;
37 const pj_uint16_t PJ_AF_PACKET = 0xFFFF;
38 const pj_uint16_t PJ_AF_IRDA = 0xFFFF;
39
40 /*
41 * Socket types conversion.
42 * The values here are indexed based on pj_sock_type
43 */
44 const pj_uint16_t PJ_SOCK_STREAM= KSockStream;
45 const pj_uint16_t PJ_SOCK_DGRAM = KSockDatagram;
46 const pj_uint16_t PJ_SOCK_RAW = 0xFFFF;
47 const pj_uint16_t PJ_SOCK_RDM = 0xFFFF;
48
49 /* we don't support setsockopt(), these are just dummy values */
50 const pj_uint16_t PJ_SOL_SOCKET = 0xFFFF;
51 const pj_uint16_t PJ_SOL_IP = 0xFFFF;
52 const pj_uint16_t PJ_SOL_TCP = 0xFFFF;
53 const pj_uint16_t PJ_SOL_UDP = 0xFFFF;
54 const pj_uint16_t PJ_SOL_IPV6 = 0xFFFF;
55 const pj_uint16_t PJ_SO_NOSIGPIPE = 0xFFFF;
56
57 /* TOS */
58 const pj_uint16_t PJ_IP_TOS = 0;
59 const pj_uint16_t PJ_IPTOS_LOWDELAY = 0;
60 const pj_uint16_t PJ_IPTOS_THROUGHPUT = 0;
61 const pj_uint16_t PJ_IPTOS_RELIABILITY = 0;
62 const pj_uint16_t PJ_IPTOS_MINCOST = 0;
63
64 /* Misc */
65 const pj_uint16_t PJ_TCP_NODELAY = 0xFFFF;
66 const pj_uint16_t PJ_SO_REUSEADDR = 0xFFFF;
67 const pj_uint16_t PJ_SO_PRIORITY = 0xFFFF;
68
69 /* ioctl() is also not supported. */
70 const pj_uint16_t PJ_SO_TYPE = 0xFFFF;
71 const pj_uint16_t PJ_SO_RCVBUF = 0xFFFF;
72 const pj_uint16_t PJ_SO_SNDBUF = 0xFFFF;
73
74 /* IP multicast is also not supported. */
75 const pj_uint16_t PJ_IP_MULTICAST_IF = 0xFFFF;
76 const pj_uint16_t PJ_IP_MULTICAST_TTL = 0xFFFF;
77 const pj_uint16_t PJ_IP_MULTICAST_LOOP = 0xFFFF;
78 const pj_uint16_t PJ_IP_ADD_MEMBERSHIP = 0xFFFF;
79 const pj_uint16_t PJ_IP_DROP_MEMBERSHIP = 0xFFFF;
80
81 /* Flags */
82 const int PJ_MSG_OOB = 0;
83 const int PJ_MSG_PEEK = KSockReadPeek;
84 const int PJ_MSG_DONTROUTE = 0;
85
86 /////////////////////////////////////////////////////////////////////////////
87 //
88 // CPjSocket implementation.
89 // (declaration is in os_symbian.h)
90 //
91
92 CPjSocket::~CPjSocket()
93 {
94 DestroyReader();
95 sock_.Close();
96 }
97
98
99 // Create socket reader.
100 CPjSocketReader *CPjSocket::CreateReader(unsigned max_len)
101 {
102 pj_assert(sockReader_ == NULL);
103 return sockReader_ = CPjSocketReader::NewL(*this, max_len);
104 }
105
106 // Delete socket reader when it's not wanted.
107 void CPjSocket::DestroyReader()
108 {
109 if (sockReader_) {
110 sockReader_->Cancel();
111 delete sockReader_;
112 sockReader_ = NULL;
113 }
114 }
115
116
117 /////////////////////////////////////////////////////////////////////////////
118 //
119 // CPjSocketReader implementation
120 // (declaration in os_symbian.h)
121 //
122
123
124 CPjSocketReader::CPjSocketReader(CPjSocket &sock)
125 : CActive(EPriorityStandard),
126 sock_(sock), buffer_(NULL, 0), readCb_(NULL), key_(NULL)
127 {
128 }
129
130
131 void CPjSocketReader::ConstructL(unsigned max_len)
132 {
133 isDatagram_ = sock_.IsDatagram();
134
135 TUint8 *ptr = new TUint8[max_len];
136 buffer_.Set(ptr, 0, (TInt)max_len);
137 CActiveScheduler::Add(this);
138 }
139
140 CPjSocketReader *CPjSocketReader::NewL(CPjSocket &sock, unsigned max_len)
141 {
142 CPjSocketReader *self = new (ELeave) CPjSocketReader(sock);
143 CleanupStack::PushL(self);
144 self->ConstructL(max_len);
145 CleanupStack::Pop(self);
146
147 return self;
148 }
149
150
151 CPjSocketReader::~CPjSocketReader()
152 {
153 const TUint8 *data = buffer_.Ptr();
154 delete [] data;
155 }
156
157 void CPjSocketReader::StartRecv(void (*cb)(void *key),
158 void *key,
159 TDes8 *aDesc,
160 TUint flags)
161 {
162 StartRecvFrom(cb, key, aDesc, flags, NULL);
163 }
164
165 void CPjSocketReader::StartRecvFrom(void (*cb)(void *key),
166 void *key,
167 TDes8 *aDesc,
168 TUint flags,
169 TSockAddr *fromAddr)
170 {
171 readCb_ = cb;
172 key_ = key;
173
174 if (aDesc == NULL) aDesc = &buffer_;
175 if (fromAddr == NULL) fromAddr = &recvAddr_;
176
177 sock_.Socket().RecvFrom(*aDesc, *fromAddr, flags, iStatus);
178 SetActive();
179 }
180
181 void CPjSocketReader::DoCancel()
182 {
183 sock_.Socket().CancelRecv();
184 }
185
186 void CPjSocketReader::RunL()
187 {
188 void (*old_cb)(void *key) = readCb_;
189 void *old_key = key_;
190
191 readCb_ = NULL;
192 key_ = NULL;
193
194 if (old_cb) {
195 (*old_cb)(old_key);
196 }
197 }
198
199 // Append data to aDesc, up to aDesc's maximum size.
200 // If socket is datagram based, buffer_ will be clared.
201 void CPjSocketReader::ReadData(TDes8 &aDesc, TInetAddr *addr)
202 {
203 if (isDatagram_)
204 aDesc.Zero();
205
206 if (buffer_.Length() == 0)
207 return;
208
209 TInt size_to_copy = aDesc.MaxLength() - aDesc.Length();
210 if (size_to_copy > buffer_.Length())
211 size_to_copy = buffer_.Length();
212
213 aDesc.Append(buffer_.Ptr(), size_to_copy);
214
215 if (isDatagram_)
216 buffer_.Zero();
217 else
218 buffer_.Delete(0, size_to_copy);
219
220 if (addr)
221 *addr = recvAddr_;
222 }
223
224
225
226 /////////////////////////////////////////////////////////////////////////////
227 //
228 // PJLIB's sock.h implementation
229 //
230
231 /*
232 * Convert 16-bit value from network byte order to host byte order.
233 */
234 PJ_DEF(pj_uint16_t) pj_ntohs(pj_uint16_t netshort)
235 {
236 #if PJ_IS_LITTLE_ENDIAN
237 return pj_swap16(netshort);
238 #else
239 return netshort;
240 #endif
241 }
242
243 /*
244 * Convert 16-bit value from host byte order to network byte order.
245 */
246 PJ_DEF(pj_uint16_t) pj_htons(pj_uint16_t hostshort)
247 {
248 #if PJ_IS_LITTLE_ENDIAN
249 return pj_swap16(hostshort);
250 #else
251 return hostshort;
252 #endif
253 }
254
255 /*
256 * Convert 32-bit value from network byte order to host byte order.
257 */
258 PJ_DEF(pj_uint32_t) pj_ntohl(pj_uint32_t netlong)
259 {
260 #if PJ_IS_LITTLE_ENDIAN
261 return pj_swap32(netlong);
262 #else
263 return netlong;
264 #endif
265 }
266
267 /*
268 * Convert 32-bit value from host byte order to network byte order.
269 */
270 PJ_DEF(pj_uint32_t) pj_htonl(pj_uint32_t hostlong)
271 {
272 #if PJ_IS_LITTLE_ENDIAN
273 return pj_swap32(hostlong);
274 #else
275 return netlong;
276 #endif
277 }
278
279 /*
280 * Convert an Internet host address given in network byte order
281 * to string in standard numbers and dots notation.
282 */
283 PJ_DEF(char*) pj_inet_ntoa(pj_in_addr inaddr)
284 {
285 static char str8[PJ_INET_ADDRSTRLEN];
286 TBuf<PJ_INET_ADDRSTRLEN> str16(0);
287
288 /* (Symbian IP address is in host byte order) */
289 TInetAddr temp_addr((TUint32)pj_ntohl(inaddr.s_addr), (TUint)0);
290 temp_addr.Output(str16);
291
292 return pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
293 str8, sizeof(str8));
294 }
295
296 /*
297 * This function converts the Internet host address cp from the standard
298 * numbers-and-dots notation into binary data and stores it in the structure
299 * that inp points to.
300 */
301 PJ_DEF(int) pj_inet_aton(const pj_str_t *cp, pj_in_addr *inp)
302 {
303 enum { MAXIPLEN = PJ_INET_ADDRSTRLEN };
304
305 /* Initialize output with PJ_INADDR_NONE.
306 * Some apps relies on this instead of the return value
307 * (and anyway the return value is quite confusing!)
308 */
309 inp->s_addr = PJ_INADDR_NONE;
310
311 /* Caution:
312 * this function might be called with cp->slen >= 16
313 * (i.e. when called with hostname to check if it's an IP addr).
314 */
315 PJ_ASSERT_RETURN(cp && cp->slen && inp, 0);
316 if (cp->slen >= 16) {
317 return 0;
318 }
319
320 char tempaddr8[MAXIPLEN];
321 pj_memcpy(tempaddr8, cp->ptr, cp->slen);
322 tempaddr8[cp->slen] = '\0';
323
324 wchar_t tempaddr16[MAXIPLEN];
325 pj_ansi_to_unicode(tempaddr8, pj_ansi_strlen(tempaddr8),
326 tempaddr16, sizeof(tempaddr16));
327
328 TBuf<MAXIPLEN> ip_addr((const TText*)tempaddr16);
329
330 TInetAddr addr;
331 addr.Init(KAfInet);
332 if (addr.Input(ip_addr) == KErrNone) {
333 /* Success (Symbian IP address is in host byte order) */
334 inp->s_addr = pj_htonl(addr.Address());
335 return 1;
336 } else {
337 /* Error */
338 return 0;
339 }
340 }
341
342 /*
343 * Convert text to IPv4/IPv6 address.
344 */
345 PJ_DEF(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst)
346 {
347 char tempaddr[PJ_INET6_ADDRSTRLEN];
348
349 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL);
350 PJ_ASSERT_RETURN(src && src->slen && dst, PJ_EINVAL);
351
352 /* Initialize output with PJ_IN_ADDR_NONE for IPv4 (to be
353 * compatible with pj_inet_aton()
354 */
355 if (af==PJ_AF_INET) {
356 ((pj_in_addr*)dst)->s_addr = PJ_INADDR_NONE;
357 }
358
359 /* Caution:
360 * this function might be called with cp->slen >= 46
361 * (i.e. when called with hostname to check if it's an IP addr).
362 */
363 if (src->slen >= PJ_INET6_ADDRSTRLEN) {
364 return PJ_ENAMETOOLONG;
365 }
366
367 pj_memcpy(tempaddr, src->ptr, src->slen);
368 tempaddr[src->slen] = '\0';
369
370
371 wchar_t tempaddr16[PJ_INET6_ADDRSTRLEN];
372 pj_ansi_to_unicode(tempaddr, pj_ansi_strlen(tempaddr),
373 tempaddr16, sizeof(tempaddr16));
374
375 TBuf<PJ_INET6_ADDRSTRLEN> ip_addr((const TText*)tempaddr16);
376
377 TInetAddr addr;
378 addr.Init(KAfInet6);
379 if (addr.Input(ip_addr) == KErrNone) {
380 if (af==PJ_AF_INET) {
381 /* Success (Symbian IP address is in host byte order) */
382 pj_uint32_t ip = pj_htonl(addr.Address());
383 pj_memcpy(dst, &ip, 4);
384 } else if (af==PJ_AF_INET6) {
385 const TIp6Addr & ip6 = addr.Ip6Address();
386 pj_memcpy(dst, ip6.u.iAddr8, 16);
387 } else {
388 pj_assert(!"Unexpected!");
389 return PJ_EBUG;
390 }
391 return PJ_SUCCESS;
392 } else {
393 /* Error */
394 return PJ_EINVAL;
395 }
396 }
397
398 /*
399 * Convert IPv4/IPv6 address to text.
400 */
401 PJ_DEF(pj_status_t) pj_inet_ntop(int af, const void *src,
402 char *dst, int size)
403
404 {
405 PJ_ASSERT_RETURN(src && dst && size, PJ_EINVAL);
406
407 *dst = '\0';
408
409 if (af==PJ_AF_INET) {
410
411 TBuf<PJ_INET_ADDRSTRLEN> str16;
412 pj_in_addr inaddr;
413
414 if (size < PJ_INET_ADDRSTRLEN)
415 return PJ_ETOOSMALL;
416
417 pj_memcpy(&inaddr, src, 4);
418
419 /* Symbian IP address is in host byte order */
420 TInetAddr temp_addr((TUint32)pj_ntohl(inaddr.s_addr), (TUint)0);
421 temp_addr.Output(str16);
422
423 pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
424 dst, size);
425 return PJ_SUCCESS;
426
427 } else if (af==PJ_AF_INET6) {
428 TBuf<PJ_INET6_ADDRSTRLEN> str16;
429
430 if (size < PJ_INET6_ADDRSTRLEN)
431 return PJ_ETOOSMALL;
432
433 TIp6Addr ip6;
434 pj_memcpy(ip6.u.iAddr8, src, 16);
435
436 TInetAddr temp_addr(ip6, (TUint)0);
437 temp_addr.Output(str16);
438
439 pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
440 dst, size);
441 return PJ_SUCCESS;
442
443 } else {
444 pj_assert(!"Unsupport address family");
445 return PJ_EINVAL;
446 }
447
448 }
449
450 /*
451 * Get hostname.
452 */
453 PJ_DEF(const pj_str_t*) pj_gethostname(void)
454 {
455 static char buf[PJ_MAX_HOSTNAME];
456 static pj_str_t hostname;
457
458 PJ_CHECK_STACK();
459
460 if (hostname.ptr == NULL) {
461 RHostResolver &resv = PjSymbianOS::Instance()->GetResolver(PJ_AF_INET);
462 TRequestStatus reqStatus;
463 THostName tmpName;
464
465 // Return empty hostname if access point is marked as down by app.
466 PJ_SYMBIAN_CHECK_CONNECTION2(&hostname);
467
468 resv.GetHostName(tmpName, reqStatus);
469 User::WaitForRequest(reqStatus);
470
471 hostname.ptr = pj_unicode_to_ansi((const wchar_t*)tmpName.Ptr(), tmpName.Length(),
472 buf, sizeof(buf));
473 hostname.slen = tmpName.Length();
474 }
475 return &hostname;
476 }
477
478 /*
479 * Create new socket/endpoint for communication and returns a descriptor.
480 */
481 PJ_DEF(pj_status_t) pj_sock_socket(int af,
482 int type,
483 int proto,
484 pj_sock_t *p_sock)
485 {
486 TInt rc;
487
488 PJ_CHECK_STACK();
489
490 /* Sanity checks. */
491 PJ_ASSERT_RETURN(p_sock!=NULL, PJ_EINVAL);
492
493 // Return failure if access point is marked as down by app.
494 PJ_SYMBIAN_CHECK_CONNECTION();
495
496 /* Set proto if none is specified. */
497 if (proto == 0) {
498 if (type == pj_SOCK_STREAM())
499 proto = KProtocolInetTcp;
500 else if (type == pj_SOCK_DGRAM())
501 proto = KProtocolInetUdp;
502 }
503
504 /* Create Symbian RSocket */
505 RSocket rSock;
506 if (PjSymbianOS::Instance()->Connection())
507 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
508 af, type, proto,
509 *PjSymbianOS::Instance()->Connection());
510 else
511 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
512 af, type, proto);
513
514 if (rc != KErrNone)
515 return PJ_RETURN_OS_ERROR(rc);
516
517
518 /* Wrap Symbian RSocket into PJLIB's CPjSocket, and return to caller */
519 CPjSocket *pjSock = new CPjSocket(af, type, rSock);
520 *p_sock = (pj_sock_t)pjSock;
521
522 return PJ_SUCCESS;
523 }
524
525
526 /*
527 * Bind socket.
528 */
529 PJ_DEF(pj_status_t) pj_sock_bind( pj_sock_t sock,
530 const pj_sockaddr_t *addr,
531 int len)
532 {
533 pj_status_t status;
534 TInt rc;
535
536 PJ_CHECK_STACK();
537
538 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
539 PJ_ASSERT_RETURN(addr && len>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
540
541 // Convert PJLIB's pj_sockaddr into Symbian's TInetAddr
542 TInetAddr inetAddr;
543 status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)addr, len, inetAddr);
544 if (status != PJ_SUCCESS)
545 return status;
546
547 // Get the RSocket instance
548 RSocket &rSock = ((CPjSocket*)sock)->Socket();
549
550 // Bind
551 rc = rSock.Bind(inetAddr);
552
553 return (rc==KErrNone) ? PJ_SUCCESS : PJ_RETURN_OS_ERROR(rc);
554 }
555
556
557 /*
558 * Bind socket.
559 */
560 PJ_DEF(pj_status_t) pj_sock_bind_in( pj_sock_t sock,
561 pj_uint32_t addr32,
562 pj_uint16_t port)
563 {
564 pj_sockaddr_in addr;
565
566 PJ_CHECK_STACK();
567
568 pj_bzero(&addr, sizeof(addr));
569 addr.sin_family = PJ_AF_INET;
570 addr.sin_addr.s_addr = pj_htonl(addr32);
571 addr.sin_port = pj_htons(port);
572
573 return pj_sock_bind(sock, &addr, sizeof(pj_sockaddr_in));
574 }
575
576
577 /*
578 * Close socket.
579 */
580 PJ_DEF(pj_status_t) pj_sock_close(pj_sock_t sock)
581 {
582 PJ_CHECK_STACK();
583
584 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
585
586 CPjSocket *pjSock = (CPjSocket*)sock;
587
588 // This will close the socket.
589 delete pjSock;
590
591 return PJ_SUCCESS;
592 }
593
594 /*
595 * Get remote's name.
596 */
597 PJ_DEF(pj_status_t) pj_sock_getpeername( pj_sock_t sock,
598 pj_sockaddr_t *addr,
599 int *namelen)
600 {
601 PJ_CHECK_STACK();
602
603 PJ_ASSERT_RETURN(sock && addr && namelen &&
604 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
605
606 CPjSocket *pjSock = (CPjSocket*)sock;
607 RSocket &rSock = pjSock->Socket();
608
609 // Socket must be connected.
610 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
611
612 TInetAddr inetAddr;
613 rSock.RemoteName(inetAddr);
614
615 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)addr, namelen);
616 }
617
618 /*
619 * Get socket name.
620 */
621 PJ_DEF(pj_status_t) pj_sock_getsockname( pj_sock_t sock,
622 pj_sockaddr_t *addr,
623 int *namelen)
624 {
625 PJ_CHECK_STACK();
626
627 PJ_ASSERT_RETURN(sock && addr && namelen &&
628 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
629
630 CPjSocket *pjSock = (CPjSocket*)sock;
631 RSocket &rSock = pjSock->Socket();
632
633 TInetAddr inetAddr;
634 rSock.LocalName(inetAddr);
635
636 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)addr, namelen);
637 }
638
639 /*
640 * Send data
641 */
642 PJ_DEF(pj_status_t) pj_sock_send(pj_sock_t sock,
643 const void *buf,
644 pj_ssize_t *len,
645 unsigned flags)
646 {
647 PJ_CHECK_STACK();
648 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
649
650 // Return failure if access point is marked as down by app.
651 PJ_SYMBIAN_CHECK_CONNECTION();
652
653 CPjSocket *pjSock = (CPjSocket*)sock;
654 RSocket &rSock = pjSock->Socket();
655
656 // send() should only be called to connected socket
657 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
658
659 TPtrC8 data((const TUint8*)buf, (TInt)*len);
660 TRequestStatus reqStatus;
661 TSockXfrLength sentLen;
662
663 rSock.Send(data, flags, reqStatus, sentLen);
664 User::WaitForRequest(reqStatus);
665
666 if (reqStatus.Int()==KErrNone) {
667 //*len = (TInt) sentLen.Length();
668 return PJ_SUCCESS;
669 } else
670 return PJ_RETURN_OS_ERROR(reqStatus.Int());
671 }
672
673
674 /*
675 * Send data.
676 */
677 PJ_DEF(pj_status_t) pj_sock_sendto(pj_sock_t sock,
678 const void *buf,
679 pj_ssize_t *len,
680 unsigned flags,
681 const pj_sockaddr_t *to,
682 int tolen)
683 {
684 pj_status_t status;
685
686 PJ_CHECK_STACK();
687 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
688
689 // Return failure if access point is marked as down by app.
690 PJ_SYMBIAN_CHECK_CONNECTION();
691
692 CPjSocket *pjSock = (CPjSocket*)sock;
693 RSocket &rSock = pjSock->Socket();
694
695 // Only supports AF_INET for now
696 PJ_ASSERT_RETURN(tolen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
697
698 TInetAddr inetAddr;
699 status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)to, tolen, inetAddr);
700 if (status != PJ_SUCCESS)
701 return status;
702
703 TPtrC8 data((const TUint8*)buf, (TInt)*len);
704 TRequestStatus reqStatus;
705 TSockXfrLength sentLen;
706
707 rSock.SendTo(data, inetAddr, flags, reqStatus, sentLen);
708 User::WaitForRequest(reqStatus);
709
710 if (reqStatus.Int()==KErrNone) {
711 //For some reason TSockXfrLength is not returning correctly!
712 //*len = (TInt) sentLen.Length();
713 return PJ_SUCCESS;
714 } else
715 return PJ_RETURN_OS_ERROR(reqStatus.Int());
716 }
717
718 /*
719 * Receive data.
720 */
721 PJ_DEF(pj_status_t) pj_sock_recv(pj_sock_t sock,
722 void *buf,
723 pj_ssize_t *len,
724 unsigned flags)
725 {
726 PJ_CHECK_STACK();
727
728 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
729 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
730
731 // Return failure if access point is marked as down by app.
732 PJ_SYMBIAN_CHECK_CONNECTION();
733
734 CPjSocket *pjSock = (CPjSocket*)sock;
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 return PJ_SUCCESS;
751 }
752 }
753
754 TRequestStatus reqStatus;
755 TSockXfrLength recvLen;
756 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
757
758 if (pjSock->IsDatagram()) {
759 pjSock->Socket().Recv(data, flags, reqStatus);
760 } else {
761 // Using static like this is not pretty, but we don't need to use
762 // the value anyway, hence doing it like this is probably most
763 // optimal.
764 static TSockXfrLength len;
765 pjSock->Socket().RecvOneOrMore(data, flags, reqStatus, len);
766 }
767 User::WaitForRequest(reqStatus);
768
769 if (reqStatus == KErrNone) {
770 //*len = (TInt)recvLen.Length();
771 *len = data.Length();
772 return PJ_SUCCESS;
773 } else {
774 *len = -1;
775 return PJ_RETURN_OS_ERROR(reqStatus.Int());
776 }
777 }
778
779 /*
780 * Receive data.
781 */
782 PJ_DEF(pj_status_t) pj_sock_recvfrom(pj_sock_t sock,
783 void *buf,
784 pj_ssize_t *len,
785 unsigned flags,
786 pj_sockaddr_t *from,
787 int *fromlen)
788 {
789 PJ_CHECK_STACK();
790
791 PJ_ASSERT_RETURN(sock && buf && len && from && fromlen, PJ_EINVAL);
792 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
793 PJ_ASSERT_RETURN(*fromlen >= (int)sizeof(pj_sockaddr_in), PJ_EINVAL);
794
795 // Return failure if access point is marked as down by app.
796 PJ_SYMBIAN_CHECK_CONNECTION();
797
798 CPjSocket *pjSock = (CPjSocket*)sock;
799 RSocket &rSock = pjSock->Socket();
800
801 if (pjSock->Reader()) {
802 CPjSocketReader *reader = pjSock->Reader();
803
804 while (reader->IsActive() && !reader->HasData()) {
805 User::WaitForAnyRequest();
806 }
807
808 if (reader->HasData()) {
809 TPtr8 data((TUint8*)buf, (TInt)*len);
810 TInetAddr inetAddr;
811
812 reader->ReadData(data, &inetAddr);
813
814 *len = data.Length();
815
816 if (from && fromlen) {
817 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from,
818 fromlen);
819 } else {
820 return PJ_SUCCESS;
821 }
822 }
823 }
824
825 TInetAddr inetAddr;
826 TRequestStatus reqStatus;
827 TSockXfrLength recvLen;
828 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
829
830 rSock.RecvFrom(data, inetAddr, flags, reqStatus, recvLen);
831 User::WaitForRequest(reqStatus);
832
833 if (reqStatus == KErrNone) {
834 //*len = (TInt)recvLen.Length();
835 *len = data.Length();
836 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from, fromlen);
837 } else {
838 *len = -1;
839 *fromlen = -1;
840 return PJ_RETURN_OS_ERROR(reqStatus.Int());
841 }
842 }
843
844 /*
845 * Get socket option.
846 */
847 PJ_DEF(pj_status_t) pj_sock_getsockopt( pj_sock_t sock,
848 pj_uint16_t level,
849 pj_uint16_t optname,
850 void *optval,
851 int *optlen)
852 {
853 // Not supported for now.
854 PJ_UNUSED_ARG(sock);
855 PJ_UNUSED_ARG(level);
856 PJ_UNUSED_ARG(optname);
857 PJ_UNUSED_ARG(optval);
858 PJ_UNUSED_ARG(optlen);
859 return PJ_EINVALIDOP;
860 }
861
862 /*
863 * Set socket option.
864 */
865 PJ_DEF(pj_status_t) pj_sock_setsockopt( pj_sock_t sock,
866 pj_uint16_t level,
867 pj_uint16_t optname,
868 const void *optval,
869 int optlen)
870 {
871 // Not supported for now.
872 PJ_UNUSED_ARG(sock);
873 PJ_UNUSED_ARG(level);
874 PJ_UNUSED_ARG(optname);
875 PJ_UNUSED_ARG(optval);
876 PJ_UNUSED_ARG(optlen);
877 return PJ_EINVALIDOP;
878 }
879
880 /*
881 * Connect socket.
882 */
883 PJ_DEF(pj_status_t) pj_sock_connect( pj_sock_t sock,
884 const pj_sockaddr_t *addr,
885 int namelen)
886 {
887 pj_status_t status;
888
889 PJ_CHECK_STACK();
890
891 PJ_ASSERT_RETURN(sock && addr && namelen, PJ_EINVAL);
892 PJ_ASSERT_RETURN(((pj_sockaddr*)addr)->addr.sa_family == PJ_AF_INET,
893 PJ_EINVAL);
894
895 // Return failure if access point is marked as down by app.
896 PJ_SYMBIAN_CHECK_CONNECTION();
897
898 CPjSocket *pjSock = (CPjSocket*)sock;
899 RSocket &rSock = pjSock->Socket();
900
901 TInetAddr inetAddr;
902 TRequestStatus reqStatus;
903
904 status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)addr, namelen, inetAddr);
905 if (status != PJ_SUCCESS)
906 return status;
907
908 rSock.Connect(inetAddr, reqStatus);
909 User::WaitForRequest(reqStatus);
910
911 if (reqStatus == KErrNone) {
912 pjSock->SetConnected(true);
913 return PJ_SUCCESS;
914 } else {
915 return PJ_RETURN_OS_ERROR(reqStatus.Int());
916 }
917 }
918
919
920 /*
921 * Shutdown socket.
922 */
923 #if PJ_HAS_TCP
924 PJ_DEF(pj_status_t) pj_sock_shutdown( pj_sock_t sock,
925 int how)
926 {
927 PJ_CHECK_STACK();
928
929 PJ_ASSERT_RETURN(sock, PJ_EINVAL);
930
931 CPjSocket *pjSock = (CPjSocket*)sock;
932 RSocket &rSock = pjSock->Socket();
933
934 RSocket::TShutdown aHow;
935 if (how == PJ_SD_RECEIVE)
936 aHow = RSocket::EStopInput;
937 else if (how == PJ_SHUT_WR)
938 aHow = RSocket::EStopOutput;
939 else
940 aHow = RSocket::ENormal;
941
942 TRequestStatus reqStatus;
943
944 rSock.Shutdown(aHow, reqStatus);
945 User::WaitForRequest(reqStatus);
946
947 if (reqStatus == KErrNone) {
948 return PJ_SUCCESS;
949 } else {
950 return PJ_RETURN_OS_ERROR(reqStatus.Int());
951 }
952 }
953
954 /*
955 * Start listening to incoming connections.
956 */
957 PJ_DEF(pj_status_t) pj_sock_listen( pj_sock_t sock,
958 int backlog)
959 {
960 PJ_CHECK_STACK();
961
962 PJ_ASSERT_RETURN(sock && backlog, PJ_EINVAL);
963
964 CPjSocket *pjSock = (CPjSocket*)sock;
965 RSocket &rSock = pjSock->Socket();
966
967 TInt rc = rSock.Listen((TUint)backlog);
968
969 if (rc == KErrNone) {
970 return PJ_SUCCESS;
971 } else {
972 return PJ_RETURN_OS_ERROR(rc);
973 }
974 }
975
976 /*
977 * Accept incoming connections
978 */
979 PJ_DEF(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
980 pj_sock_t *newsock,
981 pj_sockaddr_t *addr,
982 int *addrlen)
983 {
984 PJ_CHECK_STACK();
985
986 PJ_ASSERT_RETURN(serverfd && newsock, PJ_EINVAL);
987
988 CPjSocket *pjSock = (CPjSocket*)serverfd;
989 RSocket &rSock = pjSock->Socket();
990
991 // Create a 'blank' socket
992 RSocket newSock;
993 newSock.Open(PjSymbianOS::Instance()->SocketServ());
994
995 // Call Accept()
996 TRequestStatus reqStatus;
997
998 rSock.Accept(newSock, reqStatus);
999 User::WaitForRequest(reqStatus);
1000
1001 if (reqStatus != KErrNone) {
1002 return PJ_RETURN_OS_ERROR(reqStatus.Int());
1003 }
1004
1005 // Create PJ socket
1006 CPjSocket *newPjSock = new CPjSocket(pjSock->GetAf(), pjSock->GetSockType(),
1007 newSock);
1008 newPjSock->SetConnected(true);
1009
1010 *newsock = (pj_sock_t) newPjSock;
1011
1012 if (addr && addrlen) {
1013 return pj_sock_getpeername(*newsock, addr, addrlen);
1014 }
1015
1016 return PJ_SUCCESS;
1017 }
1018 #endif /* PJ_HAS_TCP */
1019
1020