blob: f53f657e9ea1d7478e69598e888233b9144cdaf2 [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 */
Benny Prijono2a78a522007-11-21 14:10:46 +000033const pj_uint16_t PJ_AF_UNSPEC = KAFUnspec;
Benny Prijonof260e462007-04-30 21:03:32 +000034const pj_uint16_t PJ_AF_UNIX = 0xFFFF;
35const pj_uint16_t PJ_AF_INET = KAfInet;
36const pj_uint16_t PJ_AF_INET6 = KAfInet6;
37const pj_uint16_t PJ_AF_PACKET = 0xFFFF;
38const 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 */
44const pj_uint16_t PJ_SOCK_STREAM= KSockStream;
45const pj_uint16_t PJ_SOCK_DGRAM = KSockDatagram;
46const pj_uint16_t PJ_SOCK_RAW = 0xFFFF;
47const pj_uint16_t PJ_SOCK_RDM = 0xFFFF;
48
49/* setsockop() is not really supported. */
50const pj_uint16_t PJ_SOL_SOCKET = 0xFFFF;
51const pj_uint16_t PJ_SOL_IP = 0xFFFF;
52const pj_uint16_t PJ_SOL_TCP = 0xFFFF;
53const pj_uint16_t PJ_SOL_UDP = 0xFFFF;
54const pj_uint16_t PJ_SOL_IPV6 = 0xFFFF;
55
Benny Prijono8ab968f2007-07-20 08:08:30 +000056/* TOS */
57const pj_uint16_t PJ_IP_TOS = 0;
58const pj_uint16_t PJ_IPTOS_LOWDELAY = 0;
59const pj_uint16_t PJ_IPTOS_THROUGHPUT = 0;
60const pj_uint16_t PJ_IPTOS_RELIABILITY = 0;
61const pj_uint16_t PJ_IPTOS_MINCOST = 0;
62
Benny Prijonof260e462007-04-30 21:03:32 +000063/* ioctl() is also not supported. */
64const pj_uint16_t PJ_SO_TYPE = 0xFFFF;
65const pj_uint16_t PJ_SO_RCVBUF = 0xFFFF;
66const pj_uint16_t PJ_SO_SNDBUF = 0xFFFF;
67
Benny Prijono8ab968f2007-07-20 08:08:30 +000068/* Flags */
69const int PJ_MSG_OOB = 0;
70const int PJ_MSG_PEEK = KSockReadPeek;
71const int PJ_MSG_DONTROUTE = 0;
Benny Prijonof260e462007-04-30 21:03:32 +000072
73/////////////////////////////////////////////////////////////////////////////
74//
75// CPjSocket implementation.
76// (declaration is in os_symbian.h)
77//
78
79CPjSocket::~CPjSocket()
80{
Benny Prijono897f9f82007-05-03 19:56:21 +000081 DestroyReader();
Benny Prijonof260e462007-04-30 21:03:32 +000082 sock_.Close();
83}
84
85
86// Create socket reader.
87CPjSocketReader *CPjSocket::CreateReader(unsigned max_len)
88{
89 pj_assert(sockReader_ == NULL);
90 return sockReader_ = CPjSocketReader::NewL(*this, max_len);
91}
92
Benny Prijono897f9f82007-05-03 19:56:21 +000093// Delete socket reader when it's not wanted.
94void CPjSocket::DestroyReader()
95{
96 if (sockReader_) {
97 if (sockReader_->IsActive())
98 sockReader_->Cancel();
99 delete sockReader_;
100 sockReader_ = NULL;
101 }
102}
103
Benny Prijonof260e462007-04-30 21:03:32 +0000104
105/////////////////////////////////////////////////////////////////////////////
106//
107// CPjSocketReader implementation
108// (declaration in os_symbian.h)
109//
110
111
112CPjSocketReader::CPjSocketReader(CPjSocket &sock)
113: CActive(EPriorityStandard),
114 sock_(sock), buffer_(NULL, 0), readCb_(NULL), key_(NULL)
115{
116}
117
118
119void CPjSocketReader::ConstructL(unsigned max_len)
120{
121 TProtocolDesc aProtocol;
122 TInt err;
123
124 err = sock_.Socket().Info(aProtocol);
125 User::LeaveIfError(err);
126
127 isDatagram_ = (aProtocol.iSockType == KSockDatagram);
128
129 TUint8 *ptr = new TUint8[max_len];
130 buffer_.Set(ptr, 0, (TInt)max_len);
131 CActiveScheduler::Add(this);
132}
133
134CPjSocketReader *CPjSocketReader::NewL(CPjSocket &sock, unsigned max_len)
135{
136 CPjSocketReader *self = new (ELeave) CPjSocketReader(sock);
137 CleanupStack::PushL(self);
138 self->ConstructL(max_len);
139 CleanupStack::Pop(self);
140
141 return self;
142}
143
144
145CPjSocketReader::~CPjSocketReader()
146{
147 const TUint8 *data = buffer_.Ptr();
148 delete [] data;
149}
150
151void CPjSocketReader::StartRecv(void (*cb)(void *key),
152 void *key,
153 TDes8 *aDesc,
154 TUint flags)
155{
156 StartRecvFrom(cb, key, aDesc, flags, NULL);
157}
158
159void CPjSocketReader::StartRecvFrom(void (*cb)(void *key),
160 void *key,
161 TDes8 *aDesc,
162 TUint flags,
163 TSockAddr *fromAddr)
164{
165 readCb_ = cb;
166 key_ = key;
167
168 if (aDesc == NULL) aDesc = &buffer_;
169 if (fromAddr == NULL) fromAddr = &recvAddr_;
170
171 sock_.Socket().RecvFrom(*aDesc, *fromAddr, flags, iStatus);
Benny Prijono5d542642007-05-02 18:54:19 +0000172 SetActive();
Benny Prijonof260e462007-04-30 21:03:32 +0000173}
174
175void CPjSocketReader::DoCancel()
176{
177 sock_.Socket().CancelRecv();
178}
179
180void CPjSocketReader::RunL()
181{
182 void (*old_cb)(void *key) = readCb_;
183 void *old_key = key_;
Benny Prijonof260e462007-04-30 21:03:32 +0000184
185 readCb_ = NULL;
186 key_ = NULL;
187
188 if (old_cb) {
189 (*old_cb)(old_key);
190 }
191}
192
193// Append data to aDesc, up to aDesc's maximum size.
194// If socket is datagram based, buffer_ will be clared.
195void CPjSocketReader::ReadData(TDes8 &aDesc, TInetAddr *addr)
196{
197 if (isDatagram_)
198 aDesc.Zero();
199
200 if (buffer_.Length() == 0)
201 return;
202
203 TInt size_to_copy = aDesc.MaxLength() - aDesc.Length();
204 if (size_to_copy > buffer_.Length())
205 size_to_copy = buffer_.Length();
206
207 aDesc.Append(buffer_.Ptr(), size_to_copy);
208
209 if (isDatagram_)
210 buffer_.Zero();
211 else
212 buffer_.Delete(0, size_to_copy);
213
214 if (addr)
215 *addr = recvAddr_;
216}
217
218
219
220/////////////////////////////////////////////////////////////////////////////
221//
222// PJLIB's sock.h implementation
223//
224
225/*
226 * Convert 16-bit value from network byte order to host byte order.
227 */
228PJ_DEF(pj_uint16_t) pj_ntohs(pj_uint16_t netshort)
229{
Benny Prijonob2c96822007-05-03 13:31:21 +0000230#if PJ_IS_LITTLE_ENDIAN
231 return pj_swap16(netshort);
232#else
Benny Prijonof260e462007-04-30 21:03:32 +0000233 return netshort;
Benny Prijonob2c96822007-05-03 13:31:21 +0000234#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000235}
236
237/*
238 * Convert 16-bit value from host byte order to network byte order.
239 */
240PJ_DEF(pj_uint16_t) pj_htons(pj_uint16_t hostshort)
241{
Benny Prijonob2c96822007-05-03 13:31:21 +0000242#if PJ_IS_LITTLE_ENDIAN
243 return pj_swap16(hostshort);
244#else
Benny Prijonof260e462007-04-30 21:03:32 +0000245 return hostshort;
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 network byte order to host byte order.
251 */
252PJ_DEF(pj_uint32_t) pj_ntohl(pj_uint32_t netlong)
253{
Benny Prijonob2c96822007-05-03 13:31:21 +0000254#if PJ_IS_LITTLE_ENDIAN
255 return pj_swap32(netlong);
256#else
Benny Prijonof260e462007-04-30 21:03:32 +0000257 return netlong;
Benny Prijonob2c96822007-05-03 13:31:21 +0000258#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000259}
260
261/*
262 * Convert 32-bit value from host byte order to network byte order.
263 */
264PJ_DEF(pj_uint32_t) pj_htonl(pj_uint32_t hostlong)
265{
Benny Prijonob2c96822007-05-03 13:31:21 +0000266#if PJ_IS_LITTLE_ENDIAN
267 return pj_swap32(hostlong);
268#else
269 return netlong;
270#endif
Benny Prijonof260e462007-04-30 21:03:32 +0000271}
272
273/*
274 * Convert an Internet host address given in network byte order
275 * to string in standard numbers and dots notation.
276 */
277PJ_DEF(char*) pj_inet_ntoa(pj_in_addr inaddr)
278{
279 static TBuf<20> str16;
280 static char str8[20];
281
Benny Prijonob2c96822007-05-03 13:31:21 +0000282 /* (Symbian IP address is in host byte order) */
283 TInetAddr temp_addr((TUint32)pj_ntohl(inaddr.s_addr), (TUint)0);
Benny Prijonof260e462007-04-30 21:03:32 +0000284 temp_addr.Output(str16);
285
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000286 return pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
Benny Prijonof260e462007-04-30 21:03:32 +0000287 str8, sizeof(str8));
288}
289
290/*
291 * This function converts the Internet host address cp from the standard
292 * numbers-and-dots notation into binary data and stores it in the structure
293 * that inp points to.
294 */
295PJ_DEF(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp)
296{
297 enum { MAXIPLEN = 16 };
298
299 /* Initialize output with PJ_INADDR_NONE.
300 * Some apps relies on this instead of the return value
301 * (and anyway the return value is quite confusing!)
302 */
303 inp->s_addr = PJ_INADDR_NONE;
304
305 /* Caution:
306 * this function might be called with cp->slen >= 16
307 * (i.e. when called with hostname to check if it's an IP addr).
308 */
309 PJ_ASSERT_RETURN(cp && cp->slen && inp, 0);
310 if (cp->slen >= 16) {
311 return 0;
312 }
313
314 char tempaddr8[MAXIPLEN];
315 pj_memcpy(tempaddr8, cp->ptr, cp->slen);
316 tempaddr8[cp->slen] = '\0';
317
318 wchar_t tempaddr16[MAXIPLEN];
319 pj_ansi_to_unicode(tempaddr8, pj_ansi_strlen(tempaddr8),
320 tempaddr16, sizeof(tempaddr16));
321
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000322 TBuf<MAXIPLEN> ip_addr((const TText*)tempaddr16);
Benny Prijonof260e462007-04-30 21:03:32 +0000323
324 TInetAddr addr;
325 addr.Init(KAfInet);
326 if (addr.Input(ip_addr) == KErrNone) {
Benny Prijonob2c96822007-05-03 13:31:21 +0000327 /* Success (Symbian IP address is in host byte order) */
328 inp->s_addr = pj_htonl(addr.Address());
Benny Prijonof260e462007-04-30 21:03:32 +0000329 return 1;
330 } else {
331 /* Error */
332 return 0;
333 }
334}
335
336/*
Benny Prijono2a78a522007-11-21 14:10:46 +0000337 * Convert text to IPv4/IPv6 address.
338 */
339PJ_DEF(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst)
340{
341 char tempaddr[PJ_INET6_ADDRSTRLEN];
342
343 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL);
344 PJ_ASSERT_RETURN(src && src->slen && dst, PJ_EINVAL);
345
346 /* Initialize output with PJ_IN_ADDR_NONE for IPv4 (to be
347 * compatible with pj_inet_aton()
348 */
349 if (af==PJ_AF_INET) {
350 ((pj_in_addr*)dst)->s_addr = PJ_INADDR_NONE;
351 }
352
353 /* Caution:
354 * this function might be called with cp->slen >= 46
355 * (i.e. when called with hostname to check if it's an IP addr).
356 */
357 if (src->slen >= PJ_INET6_ADDRSTRLEN) {
358 return PJ_ENAMETOOLONG;
359 }
360
361 pj_memcpy(tempaddr, src->ptr, src->slen);
362 tempaddr[src->slen] = '\0';
363
364
365 wchar_t tempaddr16[PJ_INET6_ADDRSTRLEN];
366 pj_ansi_to_unicode(tempaddr, pj_ansi_strlen(tempaddr),
367 tempaddr16, sizeof(tempaddr16));
368
369 TBuf<PJ_INET6_ADDRSTRLEN> ip_addr((const TText*)tempaddr16);
370
371 TInetAddr addr;
372 addr.Init(KAfInet6);
373 if (addr.Input(ip_addr) == KErrNone) {
374 if (af==PJ_AF_INET) {
375 /* Success (Symbian IP address is in host byte order) */
376 pj_uint32_t ip = pj_htonl(addr.Address());
377 pj_memcpy(dst, &ip, 4);
378 } else if (af==PJ_AF_INET6) {
379 const TIp6Addr & ip6 = addr.Ip6Address();
380 pj_memcpy(dst, ip6.u.iAddr8, 16);
381 } else {
382 pj_assert(!"Unexpected!");
383 return PJ_EBUG;
384 }
385 return PJ_SUCCESS;
386 } else {
387 /* Error */
388 return PJ_EINVAL;
389 }
390}
391
392/*
393 * Convert IPv4/IPv6 address to text.
394 */
395PJ_DEF(pj_status_t) pj_inet_ntop(int af, const void *src,
396 char *dst, int size)
397
398{
399 PJ_ASSERT_RETURN(src && dst && size, PJ_EINVAL);
400
401 *dst = '\0';
402
403 if (af==PJ_AF_INET) {
404
405 TBuf<PJ_INET_ADDRSTRLEN> str16;
406 pj_in_addr inaddr;
407
408 if (size <= PJ_INET_ADDRSTRLEN)
409 return PJ_ETOOSMALL;
410
411 pj_memcpy(&inaddr, src, 4);
412
413 /* Symbian IP address is in host byte order */
414 TInetAddr temp_addr((TUint32)pj_ntohl(inaddr.s_addr), (TUint)0);
415 temp_addr.Output(str16);
416
417 pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
418 dst, size);
419 return PJ_SUCCESS;
420
421 } else if (af==PJ_AF_INET6) {
422 TBuf<PJ_INET6_ADDRSTRLEN> str16;
423
424 if (size <= PJ_INET6_ADDRSTRLEN)
425 return PJ_ETOOSMALL;
426
427 TIp6Addr ip6;
428 pj_memcpy(ip6.u.iAddr8, src, 16);
429
430 TInetAddr temp_addr(ip6, (TUint)0);
431 temp_addr.Output(str16);
432
433 pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(),
434 dst, size);
435 return PJ_SUCCESS;
436
437 } else {
438 pj_assert(!"Unsupport address family");
439 return PJ_EINVAL;
440 }
441
442}
443
444/*
Benny Prijonof260e462007-04-30 21:03:32 +0000445 * Get hostname.
446 */
447PJ_DEF(const pj_str_t*) pj_gethostname(void)
448{
449 static char buf[PJ_MAX_HOSTNAME];
450 static pj_str_t hostname;
451
452 PJ_CHECK_STACK();
453
454 if (hostname.ptr == NULL) {
Benny Prijono62b86eb2007-12-01 08:52:57 +0000455 RHostResolver &resv = PjSymbianOS::Instance()->GetResolver(PJ_AF_INET);
Benny Prijonof260e462007-04-30 21:03:32 +0000456 TRequestStatus reqStatus;
457 THostName tmpName;
458
459 resv.GetHostName(tmpName, reqStatus);
460 User::WaitForRequest(reqStatus);
461
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000462 hostname.ptr = pj_unicode_to_ansi((const wchar_t*)tmpName.Ptr(), tmpName.Length(),
Benny Prijonof260e462007-04-30 21:03:32 +0000463 buf, sizeof(buf));
464 hostname.slen = tmpName.Length();
465 }
466 return &hostname;
467}
468
469/*
Benny Prijonof260e462007-04-30 21:03:32 +0000470 * Create new socket/endpoint for communication and returns a descriptor.
471 */
472PJ_DEF(pj_status_t) pj_sock_socket(int af,
473 int type,
474 int proto,
475 pj_sock_t *p_sock)
476{
477 TInt rc;
478
479 PJ_CHECK_STACK();
480
481 /* Sanity checks. */
482 PJ_ASSERT_RETURN(p_sock!=NULL, PJ_EINVAL);
483
484 /* Set proto if none is specified. */
485 if (proto == 0) {
Benny Prijono8ab968f2007-07-20 08:08:30 +0000486 if (type == pj_SOCK_STREAM())
Benny Prijonof260e462007-04-30 21:03:32 +0000487 proto = KProtocolInetTcp;
Benny Prijono8ab968f2007-07-20 08:08:30 +0000488 else if (type == pj_SOCK_DGRAM())
Benny Prijonof260e462007-04-30 21:03:32 +0000489 proto = KProtocolInetUdp;
490 }
491
492 /* Create Symbian RSocket */
493 RSocket rSock;
Benny Prijono7d3e12c2007-10-26 05:25:35 +0000494 if (PjSymbianOS::Instance()->Connection())
495 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
496 af, type, proto,
497 *PjSymbianOS::Instance()->Connection());
498 else
499 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
500 af, type, proto);
501
Benny Prijonof260e462007-04-30 21:03:32 +0000502 if (rc != KErrNone)
503 return PJ_RETURN_OS_ERROR(rc);
504
505
506 /* Wrap Symbian RSocket into PJLIB's CPjSocket, and return to caller */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000507 CPjSocket *pjSock = new CPjSocket(af, rSock);
Benny Prijonof260e462007-04-30 21:03:32 +0000508 *p_sock = (pj_sock_t)pjSock;
509
510 return PJ_SUCCESS;
511}
512
513
514/*
515 * Bind socket.
516 */
517PJ_DEF(pj_status_t) pj_sock_bind( pj_sock_t sock,
518 const pj_sockaddr_t *addr,
519 int len)
520{
Benny Prijono62b86eb2007-12-01 08:52:57 +0000521 pj_status_t status;
Benny Prijonof260e462007-04-30 21:03:32 +0000522 TInt rc;
523
524 PJ_CHECK_STACK();
525
526 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
Benny Prijono80025db2007-12-02 15:36:46 +0000527 PJ_ASSERT_RETURN(addr && len>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000528
Benny Prijono62b86eb2007-12-01 08:52:57 +0000529 // Convert PJLIB's pj_sockaddr into Symbian's TInetAddr
Benny Prijonof260e462007-04-30 21:03:32 +0000530 TInetAddr inetAddr;
Benny Prijono62b86eb2007-12-01 08:52:57 +0000531 status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)addr, len, inetAddr);
532 if (status != PJ_SUCCESS)
533 return status;
Benny Prijonof260e462007-04-30 21:03:32 +0000534
535 // Get the RSocket instance
536 RSocket &rSock = ((CPjSocket*)sock)->Socket();
537
538 // Bind
539 rc = rSock.Bind(inetAddr);
540
541 return (rc==KErrNone) ? PJ_SUCCESS : PJ_RETURN_OS_ERROR(rc);
542}
543
544
545/*
546 * Bind socket.
547 */
548PJ_DEF(pj_status_t) pj_sock_bind_in( pj_sock_t sock,
549 pj_uint32_t addr32,
550 pj_uint16_t port)
551{
552 pj_sockaddr_in addr;
553
554 PJ_CHECK_STACK();
555
Benny Prijonob2c96822007-05-03 13:31:21 +0000556 pj_bzero(&addr, sizeof(addr));
Benny Prijonof260e462007-04-30 21:03:32 +0000557 addr.sin_family = PJ_AF_INET;
558 addr.sin_addr.s_addr = pj_htonl(addr32);
559 addr.sin_port = pj_htons(port);
560
561 return pj_sock_bind(sock, &addr, sizeof(pj_sockaddr_in));
562}
563
564
565/*
566 * Close socket.
567 */
568PJ_DEF(pj_status_t) pj_sock_close(pj_sock_t sock)
569{
570 PJ_CHECK_STACK();
571
572 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
573
574 CPjSocket *pjSock = (CPjSocket*)sock;
575
576 // This will close the socket.
577 delete pjSock;
578
579 return PJ_SUCCESS;
580}
581
582/*
583 * Get remote's name.
584 */
585PJ_DEF(pj_status_t) pj_sock_getpeername( pj_sock_t sock,
586 pj_sockaddr_t *addr,
587 int *namelen)
588{
589 PJ_CHECK_STACK();
590
591 PJ_ASSERT_RETURN(sock && addr && namelen &&
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000592 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000593
594 CPjSocket *pjSock = (CPjSocket*)sock;
595 RSocket &rSock = pjSock->Socket();
596
597 // Socket must be connected.
598 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
599
600 TInetAddr inetAddr;
601 rSock.RemoteName(inetAddr);
602
Benny Prijono62b86eb2007-12-01 08:52:57 +0000603 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)addr, namelen);
Benny Prijonof260e462007-04-30 21:03:32 +0000604}
605
606/*
607 * Get socket name.
608 */
609PJ_DEF(pj_status_t) pj_sock_getsockname( pj_sock_t sock,
610 pj_sockaddr_t *addr,
611 int *namelen)
612{
613 PJ_CHECK_STACK();
614
615 PJ_ASSERT_RETURN(sock && addr && namelen &&
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000616 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000617
618 CPjSocket *pjSock = (CPjSocket*)sock;
619 RSocket &rSock = pjSock->Socket();
620
621 TInetAddr inetAddr;
622 rSock.LocalName(inetAddr);
623
Benny Prijono62b86eb2007-12-01 08:52:57 +0000624 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)addr, namelen);
Benny Prijonof260e462007-04-30 21:03:32 +0000625}
626
627/*
628 * Send data
629 */
630PJ_DEF(pj_status_t) pj_sock_send(pj_sock_t sock,
631 const void *buf,
632 pj_ssize_t *len,
633 unsigned flags)
634{
635 PJ_CHECK_STACK();
636 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
637
638 CPjSocket *pjSock = (CPjSocket*)sock;
639 RSocket &rSock = pjSock->Socket();
640
641 // send() should only be called to connected socket
642 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
643
644 TPtrC8 data((const TUint8*)buf, (TInt)*len);
645 TRequestStatus reqStatus;
646 TSockXfrLength sentLen;
647
648 rSock.Send(data, flags, reqStatus, sentLen);
649 User::WaitForRequest(reqStatus);
650
651 if (reqStatus.Int()==KErrNone) {
652 //*len = (TInt) sentLen.Length();
653 return PJ_SUCCESS;
654 } else
655 return PJ_RETURN_OS_ERROR(reqStatus.Int());
656}
657
658
659/*
660 * Send data.
661 */
662PJ_DEF(pj_status_t) pj_sock_sendto(pj_sock_t sock,
663 const void *buf,
664 pj_ssize_t *len,
665 unsigned flags,
666 const pj_sockaddr_t *to,
667 int tolen)
668{
Benny Prijono62b86eb2007-12-01 08:52:57 +0000669 pj_status_t status;
670
Benny Prijonof260e462007-04-30 21:03:32 +0000671 PJ_CHECK_STACK();
672 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
673
674 CPjSocket *pjSock = (CPjSocket*)sock;
675 RSocket &rSock = pjSock->Socket();
676
677 // Only supports AF_INET for now
Benny Prijono80025db2007-12-02 15:36:46 +0000678 PJ_ASSERT_RETURN(tolen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000679
680 TInetAddr inetAddr;
Benny Prijono62b86eb2007-12-01 08:52:57 +0000681 status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)to, tolen, inetAddr);
682 if (status != PJ_SUCCESS)
683 return status;
Benny Prijonof260e462007-04-30 21:03:32 +0000684
685 TPtrC8 data((const TUint8*)buf, (TInt)*len);
686 TRequestStatus reqStatus;
687 TSockXfrLength sentLen;
688
689 rSock.SendTo(data, inetAddr, flags, reqStatus, sentLen);
690 User::WaitForRequest(reqStatus);
691
692 if (reqStatus.Int()==KErrNone) {
693 //For some reason TSockXfrLength is not returning correctly!
694 //*len = (TInt) sentLen.Length();
695 return PJ_SUCCESS;
696 } else
697 return PJ_RETURN_OS_ERROR(reqStatus.Int());
698}
699
700/*
701 * Receive data.
702 */
703PJ_DEF(pj_status_t) pj_sock_recv(pj_sock_t sock,
704 void *buf,
705 pj_ssize_t *len,
706 unsigned flags)
707{
708 PJ_CHECK_STACK();
709
710 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
711 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
712
713 CPjSocket *pjSock = (CPjSocket*)sock;
714 RSocket &rSock = pjSock->Socket();
715
716 if (pjSock->Reader()) {
717 CPjSocketReader *reader = pjSock->Reader();
718
719 while (reader->IsActive() && !reader->HasData()) {
720 User::WaitForAnyRequest();
721 }
722
723 if (reader->HasData()) {
724 TPtr8 data((TUint8*)buf, (TInt)*len);
725 TInetAddr inetAddr;
726
727 reader->ReadData(data, &inetAddr);
728
729 *len = data.Length();
730 return PJ_SUCCESS;
731 }
732 }
733
734 TRequestStatus reqStatus;
735 TSockXfrLength recvLen;
736 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
737
738 rSock.Recv(data, flags, reqStatus, recvLen);
739 User::WaitForRequest(reqStatus);
740
741 if (reqStatus == KErrNone) {
742 //*len = (TInt)recvLen.Length();
743 *len = data.Length();
744 return PJ_SUCCESS;
745 } else {
746 *len = -1;
747 return PJ_RETURN_OS_ERROR(reqStatus.Int());
748 }
749}
750
751/*
752 * Receive data.
753 */
754PJ_DEF(pj_status_t) pj_sock_recvfrom(pj_sock_t sock,
755 void *buf,
756 pj_ssize_t *len,
757 unsigned flags,
758 pj_sockaddr_t *from,
759 int *fromlen)
760{
761 PJ_CHECK_STACK();
762
763 PJ_ASSERT_RETURN(sock && buf && len && from && fromlen, PJ_EINVAL);
764 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000765 PJ_ASSERT_RETURN(*fromlen >= (int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000766
767 CPjSocket *pjSock = (CPjSocket*)sock;
768 RSocket &rSock = pjSock->Socket();
769
770 if (pjSock->Reader()) {
771 CPjSocketReader *reader = pjSock->Reader();
772
773 while (reader->IsActive() && !reader->HasData()) {
774 User::WaitForAnyRequest();
775 }
776
777 if (reader->HasData()) {
778 TPtr8 data((TUint8*)buf, (TInt)*len);
779 TInetAddr inetAddr;
780
781 reader->ReadData(data, &inetAddr);
782
783 *len = data.Length();
784
785 if (from && fromlen) {
Benny Prijono62b86eb2007-12-01 08:52:57 +0000786 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from,
787 fromlen);
788 } else {
789 return PJ_SUCCESS;
Benny Prijonof260e462007-04-30 21:03:32 +0000790 }
Benny Prijonof260e462007-04-30 21:03:32 +0000791 }
792 }
793
794 TInetAddr inetAddr;
795 TRequestStatus reqStatus;
796 TSockXfrLength recvLen;
797 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
798
799 rSock.RecvFrom(data, inetAddr, flags, reqStatus, recvLen);
800 User::WaitForRequest(reqStatus);
801
802 if (reqStatus == KErrNone) {
803 //*len = (TInt)recvLen.Length();
804 *len = data.Length();
Benny Prijono62b86eb2007-12-01 08:52:57 +0000805 return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from, fromlen);
Benny Prijonof260e462007-04-30 21:03:32 +0000806 } else {
807 *len = -1;
808 *fromlen = -1;
809 return PJ_RETURN_OS_ERROR(reqStatus.Int());
810 }
811}
812
813/*
814 * Get socket option.
815 */
816PJ_DEF(pj_status_t) pj_sock_getsockopt( pj_sock_t sock,
817 pj_uint16_t level,
818 pj_uint16_t optname,
819 void *optval,
820 int *optlen)
821{
822 // Not supported for now.
823 PJ_UNUSED_ARG(sock);
824 PJ_UNUSED_ARG(level);
825 PJ_UNUSED_ARG(optname);
826 PJ_UNUSED_ARG(optval);
827 PJ_UNUSED_ARG(optlen);
828 return PJ_EINVALIDOP;
829}
830
831/*
832 * Set socket option.
833 */
834PJ_DEF(pj_status_t) pj_sock_setsockopt( pj_sock_t sock,
835 pj_uint16_t level,
836 pj_uint16_t optname,
837 const void *optval,
838 int optlen)
839{
840 // Not supported for now.
841 PJ_UNUSED_ARG(sock);
842 PJ_UNUSED_ARG(level);
843 PJ_UNUSED_ARG(optname);
844 PJ_UNUSED_ARG(optval);
845 PJ_UNUSED_ARG(optlen);
846 return PJ_EINVALIDOP;
847}
848
849/*
850 * Connect socket.
851 */
852PJ_DEF(pj_status_t) pj_sock_connect( pj_sock_t sock,
853 const pj_sockaddr_t *addr,
854 int namelen)
855{
Benny Prijono62b86eb2007-12-01 08:52:57 +0000856 pj_status_t status;
857
Benny Prijonof260e462007-04-30 21:03:32 +0000858 PJ_CHECK_STACK();
859
860 PJ_ASSERT_RETURN(sock && addr && namelen, PJ_EINVAL);
861 PJ_ASSERT_RETURN(((pj_sockaddr*)addr)->addr.sa_family == PJ_AF_INET,
862 PJ_EINVAL);
863
864 CPjSocket *pjSock = (CPjSocket*)sock;
865 RSocket &rSock = pjSock->Socket();
866
867 TInetAddr inetAddr;
868 TRequestStatus reqStatus;
869
Benny Prijono62b86eb2007-12-01 08:52:57 +0000870 status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)addr, namelen, inetAddr);
871 if (status != PJ_SUCCESS)
872 return status;
Benny Prijonof260e462007-04-30 21:03:32 +0000873
874 rSock.Connect(inetAddr, reqStatus);
875 User::WaitForRequest(reqStatus);
876
877 if (reqStatus == KErrNone) {
878 pjSock->SetConnected(true);
879 return PJ_SUCCESS;
880 } else {
881 return PJ_RETURN_OS_ERROR(reqStatus.Int());
882 }
883}
884
885
886/*
887 * Shutdown socket.
888 */
889#if PJ_HAS_TCP
890PJ_DEF(pj_status_t) pj_sock_shutdown( pj_sock_t sock,
891 int how)
892{
893 PJ_CHECK_STACK();
894
895 PJ_ASSERT_RETURN(sock, PJ_EINVAL);
896
897 CPjSocket *pjSock = (CPjSocket*)sock;
898 RSocket &rSock = pjSock->Socket();
899
900 RSocket::TShutdown aHow;
901 if (how == PJ_SD_RECEIVE)
902 aHow = RSocket::EStopInput;
903 else if (how == PJ_SHUT_WR)
904 aHow = RSocket::EStopOutput;
905 else
906 aHow = RSocket::ENormal;
907
908 TRequestStatus reqStatus;
909
910 rSock.Shutdown(aHow, reqStatus);
911 User::WaitForRequest(reqStatus);
912
913 if (reqStatus == KErrNone) {
914 return PJ_SUCCESS;
915 } else {
916 return PJ_RETURN_OS_ERROR(reqStatus.Int());
917 }
918}
919
920/*
921 * Start listening to incoming connections.
922 */
923PJ_DEF(pj_status_t) pj_sock_listen( pj_sock_t sock,
924 int backlog)
925{
926 PJ_CHECK_STACK();
927
928 PJ_ASSERT_RETURN(sock && backlog, PJ_EINVAL);
929
930 CPjSocket *pjSock = (CPjSocket*)sock;
931 RSocket &rSock = pjSock->Socket();
932
933 TInt rc = rSock.Listen((TUint)backlog);
934
935 if (rc == KErrNone) {
936 return PJ_SUCCESS;
937 } else {
938 return PJ_RETURN_OS_ERROR(rc);
939 }
940}
941
942/*
943 * Accept incoming connections
944 */
945PJ_DEF(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
946 pj_sock_t *newsock,
947 pj_sockaddr_t *addr,
948 int *addrlen)
949{
950 PJ_CHECK_STACK();
951
952 PJ_ASSERT_RETURN(serverfd && newsock, PJ_EINVAL);
953
954 CPjSocket *pjSock = (CPjSocket*)serverfd;
955 RSocket &rSock = pjSock->Socket();
956
957 // Create a 'blank' socket
958 RSocket newSock;
959 newSock.Open(PjSymbianOS::Instance()->SocketServ());
960
961 // Call Accept()
962 TRequestStatus reqStatus;
963
964 rSock.Accept(newSock, reqStatus);
965 User::WaitForRequest(reqStatus);
966
967 if (reqStatus != KErrNone) {
968 return PJ_RETURN_OS_ERROR(reqStatus.Int());
969 }
970
971 // Create PJ socket
Benny Prijono62b86eb2007-12-01 08:52:57 +0000972 CPjSocket *newPjSock = new CPjSocket(pjSock->GetAf(), newSock);
Benny Prijonof260e462007-04-30 21:03:32 +0000973 newPjSock->SetConnected(true);
974
975 *newsock = (pj_sock_t) newPjSock;
976
977 if (addr && addrlen) {
978 return pj_sock_getpeername(*newsock, addr, addrlen);
979 }
980
981 return PJ_SUCCESS;
982}
983#endif /* PJ_HAS_TCP */
984
985