blob: 67eea1a03d9ef595867f7506e29db30c527886e3 [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 * Convert address string with numbers and dots to binary IP address.
446 */
447PJ_DEF(pj_in_addr) pj_inet_addr(const pj_str_t *cp)
448{
449 pj_in_addr addr;
450
451 pj_inet_aton(cp, &addr);
452 return addr;
453}
454
455/*
456 * Convert address string with numbers and dots to binary IP address.
457 */
458PJ_DEF(pj_in_addr) pj_inet_addr2(const char *cp)
459{
460 pj_str_t str = pj_str((char*)cp);
461 return pj_inet_addr(&str);
462}
463
464/*
465 * Set the IP address of an IP socket address from string address,
466 * with resolving the host if necessary. The string address may be in a
467 * standard numbers and dots notation or may be a hostname. If hostname
468 * is specified, then the function will resolve the host into the IP
469 * address.
470 */
471PJ_DEF(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
472 const pj_str_t *str_addr)
473{
474 PJ_CHECK_STACK();
475
476 PJ_ASSERT_RETURN(!str_addr || str_addr->slen < PJ_MAX_HOSTNAME,
477 (addr->sin_addr.s_addr=PJ_INADDR_NONE, PJ_EINVAL));
478
479 addr->sin_family = PJ_AF_INET;
480 pj_memset(addr->sin_zero, 0, sizeof(addr->sin_zero));
481
482 if (str_addr && str_addr->slen) {
483 addr->sin_addr = pj_inet_addr(str_addr);
484 if (addr->sin_addr.s_addr == PJ_INADDR_NONE) {
485 pj_hostent he;
486 pj_status_t rc;
487
488 rc = pj_gethostbyname(str_addr, &he);
489 if (rc == 0) {
490 addr->sin_addr.s_addr = *(pj_uint32_t*)he.h_addr;
491 } else {
492 addr->sin_addr.s_addr = PJ_INADDR_NONE;
493 return rc;
494 }
495 }
496
497 } else {
498 addr->sin_addr.s_addr = 0;
499 }
500
501 return PJ_SUCCESS;
502}
503
504/*
505 * Set the IP address and port of an IP socket address.
506 * The string address may be in a standard numbers and dots notation or
507 * may be a hostname. If hostname is specified, then the function will
508 * resolve the host into the IP address.
509 */
510PJ_DEF(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
511 const pj_str_t *str_addr,
512 pj_uint16_t port)
513{
514 PJ_ASSERT_RETURN(addr, (addr->sin_addr.s_addr=PJ_INADDR_NONE, PJ_EINVAL));
515
516 addr->sin_family = PJ_AF_INET;
517 pj_memset(addr->sin_zero, 0, sizeof(addr->sin_zero));
518 pj_sockaddr_in_set_port(addr, port);
519 return pj_sockaddr_in_set_str_addr(addr, str_addr);
520}
521
522
523/*
524 * Get hostname.
525 */
526PJ_DEF(const pj_str_t*) pj_gethostname(void)
527{
528 static char buf[PJ_MAX_HOSTNAME];
529 static pj_str_t hostname;
530
531 PJ_CHECK_STACK();
532
533 if (hostname.ptr == NULL) {
534 RHostResolver & resv = PjSymbianOS::Instance()->GetResolver();
535 TRequestStatus reqStatus;
536 THostName tmpName;
537
538 resv.GetHostName(tmpName, reqStatus);
539 User::WaitForRequest(reqStatus);
540
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000541 hostname.ptr = pj_unicode_to_ansi((const wchar_t*)tmpName.Ptr(), tmpName.Length(),
Benny Prijonof260e462007-04-30 21:03:32 +0000542 buf, sizeof(buf));
543 hostname.slen = tmpName.Length();
544 }
545 return &hostname;
546}
547
548/*
549 * Get first IP address associated with the hostname.
550 */
551PJ_DEF(pj_in_addr) pj_gethostaddr(void)
552{
553 pj_sockaddr_in addr;
554 const pj_str_t *hostname = pj_gethostname();
555
556 pj_sockaddr_in_set_str_addr(&addr, hostname);
557 return addr.sin_addr;
558}
559
560
561/*
562 * Create new socket/endpoint for communication and returns a descriptor.
563 */
564PJ_DEF(pj_status_t) pj_sock_socket(int af,
565 int type,
566 int proto,
567 pj_sock_t *p_sock)
568{
569 TInt rc;
570
571 PJ_CHECK_STACK();
572
573 /* Sanity checks. */
574 PJ_ASSERT_RETURN(p_sock!=NULL, PJ_EINVAL);
575
576 /* Set proto if none is specified. */
577 if (proto == 0) {
Benny Prijono8ab968f2007-07-20 08:08:30 +0000578 if (type == pj_SOCK_STREAM())
Benny Prijonof260e462007-04-30 21:03:32 +0000579 proto = KProtocolInetTcp;
Benny Prijono8ab968f2007-07-20 08:08:30 +0000580 else if (type == pj_SOCK_DGRAM())
Benny Prijonof260e462007-04-30 21:03:32 +0000581 proto = KProtocolInetUdp;
582 }
583
584 /* Create Symbian RSocket */
585 RSocket rSock;
Benny Prijono7d3e12c2007-10-26 05:25:35 +0000586 if (PjSymbianOS::Instance()->Connection())
587 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
588 af, type, proto,
589 *PjSymbianOS::Instance()->Connection());
590 else
591 rc = rSock.Open(PjSymbianOS::Instance()->SocketServ(),
592 af, type, proto);
593
Benny Prijonof260e462007-04-30 21:03:32 +0000594 if (rc != KErrNone)
595 return PJ_RETURN_OS_ERROR(rc);
596
597
598 /* Wrap Symbian RSocket into PJLIB's CPjSocket, and return to caller */
599 CPjSocket *pjSock = new CPjSocket(rSock);
600 *p_sock = (pj_sock_t)pjSock;
601
602 return PJ_SUCCESS;
603}
604
605
606/*
607 * Bind socket.
608 */
609PJ_DEF(pj_status_t) pj_sock_bind( pj_sock_t sock,
610 const pj_sockaddr_t *addr,
611 int len)
612{
613 TInt rc;
614
615 PJ_CHECK_STACK();
616
617 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
618 PJ_ASSERT_RETURN(addr && len == sizeof(pj_sockaddr_in), PJ_EINVAL);
619
620 // Convert PJLIB's pj_sockaddr_in into Symbian's TInetAddr
621 TInetAddr inetAddr;
622 PjSymbianOS::pj2Addr(*(pj_sockaddr_in*)addr, inetAddr);
623
624 // Get the RSocket instance
625 RSocket &rSock = ((CPjSocket*)sock)->Socket();
626
627 // Bind
628 rc = rSock.Bind(inetAddr);
629
630 return (rc==KErrNone) ? PJ_SUCCESS : PJ_RETURN_OS_ERROR(rc);
631}
632
633
634/*
635 * Bind socket.
636 */
637PJ_DEF(pj_status_t) pj_sock_bind_in( pj_sock_t sock,
638 pj_uint32_t addr32,
639 pj_uint16_t port)
640{
641 pj_sockaddr_in addr;
642
643 PJ_CHECK_STACK();
644
Benny Prijonob2c96822007-05-03 13:31:21 +0000645 pj_bzero(&addr, sizeof(addr));
Benny Prijonof260e462007-04-30 21:03:32 +0000646 addr.sin_family = PJ_AF_INET;
647 addr.sin_addr.s_addr = pj_htonl(addr32);
648 addr.sin_port = pj_htons(port);
649
650 return pj_sock_bind(sock, &addr, sizeof(pj_sockaddr_in));
651}
652
653
654/*
655 * Close socket.
656 */
657PJ_DEF(pj_status_t) pj_sock_close(pj_sock_t sock)
658{
659 PJ_CHECK_STACK();
660
661 PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL);
662
663 CPjSocket *pjSock = (CPjSocket*)sock;
664
665 // This will close the socket.
666 delete pjSock;
667
668 return PJ_SUCCESS;
669}
670
671/*
672 * Get remote's name.
673 */
674PJ_DEF(pj_status_t) pj_sock_getpeername( pj_sock_t sock,
675 pj_sockaddr_t *addr,
676 int *namelen)
677{
678 PJ_CHECK_STACK();
679
680 PJ_ASSERT_RETURN(sock && addr && namelen &&
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000681 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000682
683 CPjSocket *pjSock = (CPjSocket*)sock;
684 RSocket &rSock = pjSock->Socket();
685
686 // Socket must be connected.
687 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
688
689 TInetAddr inetAddr;
690 rSock.RemoteName(inetAddr);
691
692 PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr_in*)addr);
693 *namelen = sizeof(pj_sockaddr_in);
694
695 return PJ_SUCCESS;
696}
697
698/*
699 * Get socket name.
700 */
701PJ_DEF(pj_status_t) pj_sock_getsockname( pj_sock_t sock,
702 pj_sockaddr_t *addr,
703 int *namelen)
704{
705 PJ_CHECK_STACK();
706
707 PJ_ASSERT_RETURN(sock && addr && namelen &&
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000708 *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000709
710 CPjSocket *pjSock = (CPjSocket*)sock;
711 RSocket &rSock = pjSock->Socket();
712
713 TInetAddr inetAddr;
714 rSock.LocalName(inetAddr);
715
716 PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr_in*)addr);
717 *namelen = sizeof(pj_sockaddr_in);
718
719 return PJ_SUCCESS;
720}
721
722/*
723 * Send data
724 */
725PJ_DEF(pj_status_t) pj_sock_send(pj_sock_t sock,
726 const void *buf,
727 pj_ssize_t *len,
728 unsigned flags)
729{
730 PJ_CHECK_STACK();
731 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
732
733 CPjSocket *pjSock = (CPjSocket*)sock;
734 RSocket &rSock = pjSock->Socket();
735
736 // send() should only be called to connected socket
737 PJ_ASSERT_RETURN(pjSock->IsConnected(), PJ_EINVALIDOP);
738
739 TPtrC8 data((const TUint8*)buf, (TInt)*len);
740 TRequestStatus reqStatus;
741 TSockXfrLength sentLen;
742
743 rSock.Send(data, flags, reqStatus, sentLen);
744 User::WaitForRequest(reqStatus);
745
746 if (reqStatus.Int()==KErrNone) {
747 //*len = (TInt) sentLen.Length();
748 return PJ_SUCCESS;
749 } else
750 return PJ_RETURN_OS_ERROR(reqStatus.Int());
751}
752
753
754/*
755 * Send data.
756 */
757PJ_DEF(pj_status_t) pj_sock_sendto(pj_sock_t sock,
758 const void *buf,
759 pj_ssize_t *len,
760 unsigned flags,
761 const pj_sockaddr_t *to,
762 int tolen)
763{
764 PJ_CHECK_STACK();
765 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
766
767 CPjSocket *pjSock = (CPjSocket*)sock;
768 RSocket &rSock = pjSock->Socket();
769
770 // Only supports AF_INET for now
771 PJ_ASSERT_RETURN(tolen==sizeof(pj_sockaddr_in) &&
772 ((pj_sockaddr*)to)->addr.sa_family == PJ_AF_INET,
773 PJ_EINVAL);
774
775 TInetAddr inetAddr;
776 PjSymbianOS::pj2Addr(*(pj_sockaddr_in*)to, inetAddr);
777
778 TPtrC8 data((const TUint8*)buf, (TInt)*len);
779 TRequestStatus reqStatus;
780 TSockXfrLength sentLen;
781
782 rSock.SendTo(data, inetAddr, flags, reqStatus, sentLen);
783 User::WaitForRequest(reqStatus);
784
785 if (reqStatus.Int()==KErrNone) {
786 //For some reason TSockXfrLength is not returning correctly!
787 //*len = (TInt) sentLen.Length();
788 return PJ_SUCCESS;
789 } else
790 return PJ_RETURN_OS_ERROR(reqStatus.Int());
791}
792
793/*
794 * Receive data.
795 */
796PJ_DEF(pj_status_t) pj_sock_recv(pj_sock_t sock,
797 void *buf,
798 pj_ssize_t *len,
799 unsigned flags)
800{
801 PJ_CHECK_STACK();
802
803 PJ_ASSERT_RETURN(sock && buf && len, PJ_EINVAL);
804 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
805
806 CPjSocket *pjSock = (CPjSocket*)sock;
807 RSocket &rSock = pjSock->Socket();
808
809 if (pjSock->Reader()) {
810 CPjSocketReader *reader = pjSock->Reader();
811
812 while (reader->IsActive() && !reader->HasData()) {
813 User::WaitForAnyRequest();
814 }
815
816 if (reader->HasData()) {
817 TPtr8 data((TUint8*)buf, (TInt)*len);
818 TInetAddr inetAddr;
819
820 reader->ReadData(data, &inetAddr);
821
822 *len = data.Length();
823 return PJ_SUCCESS;
824 }
825 }
826
827 TRequestStatus reqStatus;
828 TSockXfrLength recvLen;
829 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
830
831 rSock.Recv(data, flags, reqStatus, recvLen);
832 User::WaitForRequest(reqStatus);
833
834 if (reqStatus == KErrNone) {
835 //*len = (TInt)recvLen.Length();
836 *len = data.Length();
837 return PJ_SUCCESS;
838 } else {
839 *len = -1;
840 return PJ_RETURN_OS_ERROR(reqStatus.Int());
841 }
842}
843
844/*
845 * Receive data.
846 */
847PJ_DEF(pj_status_t) pj_sock_recvfrom(pj_sock_t sock,
848 void *buf,
849 pj_ssize_t *len,
850 unsigned flags,
851 pj_sockaddr_t *from,
852 int *fromlen)
853{
854 PJ_CHECK_STACK();
855
856 PJ_ASSERT_RETURN(sock && buf && len && from && fromlen, PJ_EINVAL);
857 PJ_ASSERT_RETURN(*len > 0, PJ_EINVAL);
Benny Prijonoa0c8b5c2007-05-12 15:03:23 +0000858 PJ_ASSERT_RETURN(*fromlen >= (int)sizeof(pj_sockaddr_in), PJ_EINVAL);
Benny Prijonof260e462007-04-30 21:03:32 +0000859
860 CPjSocket *pjSock = (CPjSocket*)sock;
861 RSocket &rSock = pjSock->Socket();
862
863 if (pjSock->Reader()) {
864 CPjSocketReader *reader = pjSock->Reader();
865
866 while (reader->IsActive() && !reader->HasData()) {
867 User::WaitForAnyRequest();
868 }
869
870 if (reader->HasData()) {
871 TPtr8 data((TUint8*)buf, (TInt)*len);
872 TInetAddr inetAddr;
873
874 reader->ReadData(data, &inetAddr);
875
876 *len = data.Length();
877
878 if (from && fromlen) {
879 PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr_in*)from);
880 *fromlen = sizeof(pj_sockaddr_in);
881 }
882 return PJ_SUCCESS;
883 }
884 }
885
886 TInetAddr inetAddr;
887 TRequestStatus reqStatus;
888 TSockXfrLength recvLen;
889 TPtr8 data((TUint8*)buf, (TInt)*len, (TInt)*len);
890
891 rSock.RecvFrom(data, inetAddr, flags, reqStatus, recvLen);
892 User::WaitForRequest(reqStatus);
893
894 if (reqStatus == KErrNone) {
895 //*len = (TInt)recvLen.Length();
896 *len = data.Length();
897 *fromlen = sizeof(pj_sockaddr_in);
898 PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr_in*)from);
899 return PJ_SUCCESS;
900 } else {
901 *len = -1;
902 *fromlen = -1;
903 return PJ_RETURN_OS_ERROR(reqStatus.Int());
904 }
905}
906
907/*
908 * Get socket option.
909 */
910PJ_DEF(pj_status_t) pj_sock_getsockopt( pj_sock_t sock,
911 pj_uint16_t level,
912 pj_uint16_t optname,
913 void *optval,
914 int *optlen)
915{
916 // Not supported for now.
917 PJ_UNUSED_ARG(sock);
918 PJ_UNUSED_ARG(level);
919 PJ_UNUSED_ARG(optname);
920 PJ_UNUSED_ARG(optval);
921 PJ_UNUSED_ARG(optlen);
922 return PJ_EINVALIDOP;
923}
924
925/*
926 * Set socket option.
927 */
928PJ_DEF(pj_status_t) pj_sock_setsockopt( pj_sock_t sock,
929 pj_uint16_t level,
930 pj_uint16_t optname,
931 const void *optval,
932 int optlen)
933{
934 // Not supported for now.
935 PJ_UNUSED_ARG(sock);
936 PJ_UNUSED_ARG(level);
937 PJ_UNUSED_ARG(optname);
938 PJ_UNUSED_ARG(optval);
939 PJ_UNUSED_ARG(optlen);
940 return PJ_EINVALIDOP;
941}
942
943/*
944 * Connect socket.
945 */
946PJ_DEF(pj_status_t) pj_sock_connect( pj_sock_t sock,
947 const pj_sockaddr_t *addr,
948 int namelen)
949{
950 PJ_CHECK_STACK();
951
952 PJ_ASSERT_RETURN(sock && addr && namelen, PJ_EINVAL);
953 PJ_ASSERT_RETURN(((pj_sockaddr*)addr)->addr.sa_family == PJ_AF_INET,
954 PJ_EINVAL);
955
956 CPjSocket *pjSock = (CPjSocket*)sock;
957 RSocket &rSock = pjSock->Socket();
958
959 TInetAddr inetAddr;
960 TRequestStatus reqStatus;
961
962 PjSymbianOS::pj2Addr(*(pj_sockaddr_in*)addr, inetAddr);
963
964 rSock.Connect(inetAddr, reqStatus);
965 User::WaitForRequest(reqStatus);
966
967 if (reqStatus == KErrNone) {
968 pjSock->SetConnected(true);
969 return PJ_SUCCESS;
970 } else {
971 return PJ_RETURN_OS_ERROR(reqStatus.Int());
972 }
973}
974
975
976/*
977 * Shutdown socket.
978 */
979#if PJ_HAS_TCP
980PJ_DEF(pj_status_t) pj_sock_shutdown( pj_sock_t sock,
981 int how)
982{
983 PJ_CHECK_STACK();
984
985 PJ_ASSERT_RETURN(sock, PJ_EINVAL);
986
987 CPjSocket *pjSock = (CPjSocket*)sock;
988 RSocket &rSock = pjSock->Socket();
989
990 RSocket::TShutdown aHow;
991 if (how == PJ_SD_RECEIVE)
992 aHow = RSocket::EStopInput;
993 else if (how == PJ_SHUT_WR)
994 aHow = RSocket::EStopOutput;
995 else
996 aHow = RSocket::ENormal;
997
998 TRequestStatus reqStatus;
999
1000 rSock.Shutdown(aHow, reqStatus);
1001 User::WaitForRequest(reqStatus);
1002
1003 if (reqStatus == KErrNone) {
1004 return PJ_SUCCESS;
1005 } else {
1006 return PJ_RETURN_OS_ERROR(reqStatus.Int());
1007 }
1008}
1009
1010/*
1011 * Start listening to incoming connections.
1012 */
1013PJ_DEF(pj_status_t) pj_sock_listen( pj_sock_t sock,
1014 int backlog)
1015{
1016 PJ_CHECK_STACK();
1017
1018 PJ_ASSERT_RETURN(sock && backlog, PJ_EINVAL);
1019
1020 CPjSocket *pjSock = (CPjSocket*)sock;
1021 RSocket &rSock = pjSock->Socket();
1022
1023 TInt rc = rSock.Listen((TUint)backlog);
1024
1025 if (rc == KErrNone) {
1026 return PJ_SUCCESS;
1027 } else {
1028 return PJ_RETURN_OS_ERROR(rc);
1029 }
1030}
1031
1032/*
1033 * Accept incoming connections
1034 */
1035PJ_DEF(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
1036 pj_sock_t *newsock,
1037 pj_sockaddr_t *addr,
1038 int *addrlen)
1039{
1040 PJ_CHECK_STACK();
1041
1042 PJ_ASSERT_RETURN(serverfd && newsock, PJ_EINVAL);
1043
1044 CPjSocket *pjSock = (CPjSocket*)serverfd;
1045 RSocket &rSock = pjSock->Socket();
1046
1047 // Create a 'blank' socket
1048 RSocket newSock;
1049 newSock.Open(PjSymbianOS::Instance()->SocketServ());
1050
1051 // Call Accept()
1052 TRequestStatus reqStatus;
1053
1054 rSock.Accept(newSock, reqStatus);
1055 User::WaitForRequest(reqStatus);
1056
1057 if (reqStatus != KErrNone) {
1058 return PJ_RETURN_OS_ERROR(reqStatus.Int());
1059 }
1060
1061 // Create PJ socket
1062 CPjSocket *newPjSock = new CPjSocket(newSock);
1063 newPjSock->SetConnected(true);
1064
1065 *newsock = (pj_sock_t) newPjSock;
1066
1067 if (addr && addrlen) {
1068 return pj_sock_getpeername(*newsock, addr, addrlen);
1069 }
1070
1071 return PJ_SUCCESS;
1072}
1073#endif /* PJ_HAS_TCP */
1074
1075