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