Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 1 | /* $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 Prijono | 2a78a52 | 2007-11-21 14:10:46 +0000 | [diff] [blame] | 33 | const pj_uint16_t PJ_AF_UNSPEC = KAFUnspec; |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 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 | /* setsockop() is not really supported. */ |
| 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 | |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 56 | /* TOS */ |
| 57 | const pj_uint16_t PJ_IP_TOS = 0; |
| 58 | const pj_uint16_t PJ_IPTOS_LOWDELAY = 0; |
| 59 | const pj_uint16_t PJ_IPTOS_THROUGHPUT = 0; |
| 60 | const pj_uint16_t PJ_IPTOS_RELIABILITY = 0; |
| 61 | const pj_uint16_t PJ_IPTOS_MINCOST = 0; |
| 62 | |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 63 | /* ioctl() is also not supported. */ |
| 64 | const pj_uint16_t PJ_SO_TYPE = 0xFFFF; |
| 65 | const pj_uint16_t PJ_SO_RCVBUF = 0xFFFF; |
| 66 | const pj_uint16_t PJ_SO_SNDBUF = 0xFFFF; |
| 67 | |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 68 | /* Flags */ |
| 69 | const int PJ_MSG_OOB = 0; |
| 70 | const int PJ_MSG_PEEK = KSockReadPeek; |
| 71 | const int PJ_MSG_DONTROUTE = 0; |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 72 | |
| 73 | ///////////////////////////////////////////////////////////////////////////// |
| 74 | // |
| 75 | // CPjSocket implementation. |
| 76 | // (declaration is in os_symbian.h) |
| 77 | // |
| 78 | |
| 79 | CPjSocket::~CPjSocket() |
| 80 | { |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 81 | DestroyReader(); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 82 | sock_.Close(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | // Create socket reader. |
| 87 | CPjSocketReader *CPjSocket::CreateReader(unsigned max_len) |
| 88 | { |
| 89 | pj_assert(sockReader_ == NULL); |
| 90 | return sockReader_ = CPjSocketReader::NewL(*this, max_len); |
| 91 | } |
| 92 | |
Benny Prijono | 897f9f8 | 2007-05-03 19:56:21 +0000 | [diff] [blame] | 93 | // Delete socket reader when it's not wanted. |
| 94 | void CPjSocket::DestroyReader() |
| 95 | { |
| 96 | if (sockReader_) { |
| 97 | if (sockReader_->IsActive()) |
| 98 | sockReader_->Cancel(); |
| 99 | delete sockReader_; |
| 100 | sockReader_ = NULL; |
| 101 | } |
| 102 | } |
| 103 | |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 104 | |
| 105 | ///////////////////////////////////////////////////////////////////////////// |
| 106 | // |
| 107 | // CPjSocketReader implementation |
| 108 | // (declaration in os_symbian.h) |
| 109 | // |
| 110 | |
| 111 | |
| 112 | CPjSocketReader::CPjSocketReader(CPjSocket &sock) |
| 113 | : CActive(EPriorityStandard), |
| 114 | sock_(sock), buffer_(NULL, 0), readCb_(NULL), key_(NULL) |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void 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 | |
| 134 | CPjSocketReader *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 | |
| 145 | CPjSocketReader::~CPjSocketReader() |
| 146 | { |
| 147 | const TUint8 *data = buffer_.Ptr(); |
| 148 | delete [] data; |
| 149 | } |
| 150 | |
| 151 | void 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 | |
| 159 | void 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 Prijono | 5d54264 | 2007-05-02 18:54:19 +0000 | [diff] [blame] | 172 | SetActive(); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void CPjSocketReader::DoCancel() |
| 176 | { |
| 177 | sock_.Socket().CancelRecv(); |
| 178 | } |
| 179 | |
| 180 | void CPjSocketReader::RunL() |
| 181 | { |
| 182 | void (*old_cb)(void *key) = readCb_; |
| 183 | void *old_key = key_; |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 184 | |
| 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. |
| 195 | void 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 | */ |
| 228 | PJ_DEF(pj_uint16_t) pj_ntohs(pj_uint16_t netshort) |
| 229 | { |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 230 | #if PJ_IS_LITTLE_ENDIAN |
| 231 | return pj_swap16(netshort); |
| 232 | #else |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 233 | return netshort; |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 234 | #endif |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Convert 16-bit value from host byte order to network byte order. |
| 239 | */ |
| 240 | PJ_DEF(pj_uint16_t) pj_htons(pj_uint16_t hostshort) |
| 241 | { |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 242 | #if PJ_IS_LITTLE_ENDIAN |
| 243 | return pj_swap16(hostshort); |
| 244 | #else |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 245 | return hostshort; |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 246 | #endif |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Convert 32-bit value from network byte order to host byte order. |
| 251 | */ |
| 252 | PJ_DEF(pj_uint32_t) pj_ntohl(pj_uint32_t netlong) |
| 253 | { |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 254 | #if PJ_IS_LITTLE_ENDIAN |
| 255 | return pj_swap32(netlong); |
| 256 | #else |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 257 | return netlong; |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 258 | #endif |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Convert 32-bit value from host byte order to network byte order. |
| 263 | */ |
| 264 | PJ_DEF(pj_uint32_t) pj_htonl(pj_uint32_t hostlong) |
| 265 | { |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 266 | #if PJ_IS_LITTLE_ENDIAN |
| 267 | return pj_swap32(hostlong); |
| 268 | #else |
| 269 | return netlong; |
| 270 | #endif |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Convert an Internet host address given in network byte order |
| 275 | * to string in standard numbers and dots notation. |
| 276 | */ |
| 277 | PJ_DEF(char*) pj_inet_ntoa(pj_in_addr inaddr) |
| 278 | { |
| 279 | static TBuf<20> str16; |
| 280 | static char str8[20]; |
| 281 | |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 282 | /* (Symbian IP address is in host byte order) */ |
| 283 | TInetAddr temp_addr((TUint32)pj_ntohl(inaddr.s_addr), (TUint)0); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 284 | temp_addr.Output(str16); |
| 285 | |
Benny Prijono | a0c8b5c | 2007-05-12 15:03:23 +0000 | [diff] [blame] | 286 | return pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(), |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 287 | 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 | */ |
| 295 | PJ_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 Prijono | a0c8b5c | 2007-05-12 15:03:23 +0000 | [diff] [blame] | 322 | TBuf<MAXIPLEN> ip_addr((const TText*)tempaddr16); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 323 | |
| 324 | TInetAddr addr; |
| 325 | addr.Init(KAfInet); |
| 326 | if (addr.Input(ip_addr) == KErrNone) { |
Benny Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 327 | /* Success (Symbian IP address is in host byte order) */ |
| 328 | inp->s_addr = pj_htonl(addr.Address()); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 329 | return 1; |
| 330 | } else { |
| 331 | /* Error */ |
| 332 | return 0; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* |
Benny Prijono | 2a78a52 | 2007-11-21 14:10:46 +0000 | [diff] [blame] | 337 | * Convert text to IPv4/IPv6 address. |
| 338 | */ |
| 339 | PJ_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 | */ |
| 395 | PJ_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 Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 445 | * Get hostname. |
| 446 | */ |
| 447 | PJ_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 Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 455 | RHostResolver &resv = PjSymbianOS::Instance()->GetResolver(PJ_AF_INET); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 456 | TRequestStatus reqStatus; |
| 457 | THostName tmpName; |
| 458 | |
| 459 | resv.GetHostName(tmpName, reqStatus); |
| 460 | User::WaitForRequest(reqStatus); |
| 461 | |
Benny Prijono | a0c8b5c | 2007-05-12 15:03:23 +0000 | [diff] [blame] | 462 | hostname.ptr = pj_unicode_to_ansi((const wchar_t*)tmpName.Ptr(), tmpName.Length(), |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 463 | buf, sizeof(buf)); |
| 464 | hostname.slen = tmpName.Length(); |
| 465 | } |
| 466 | return &hostname; |
| 467 | } |
| 468 | |
| 469 | /* |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 470 | * Create new socket/endpoint for communication and returns a descriptor. |
| 471 | */ |
| 472 | PJ_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 Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 486 | if (type == pj_SOCK_STREAM()) |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 487 | proto = KProtocolInetTcp; |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 488 | else if (type == pj_SOCK_DGRAM()) |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 489 | proto = KProtocolInetUdp; |
| 490 | } |
| 491 | |
| 492 | /* Create Symbian RSocket */ |
| 493 | RSocket rSock; |
Benny Prijono | 7d3e12c | 2007-10-26 05:25:35 +0000 | [diff] [blame] | 494 | 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 Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 502 | 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 Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 507 | CPjSocket *pjSock = new CPjSocket(af, rSock); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 508 | *p_sock = (pj_sock_t)pjSock; |
| 509 | |
| 510 | return PJ_SUCCESS; |
| 511 | } |
| 512 | |
| 513 | |
| 514 | /* |
| 515 | * Bind socket. |
| 516 | */ |
| 517 | PJ_DEF(pj_status_t) pj_sock_bind( pj_sock_t sock, |
| 518 | const pj_sockaddr_t *addr, |
| 519 | int len) |
| 520 | { |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 521 | pj_status_t status; |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 522 | TInt rc; |
| 523 | |
| 524 | PJ_CHECK_STACK(); |
| 525 | |
| 526 | PJ_ASSERT_RETURN(sock != 0, PJ_EINVAL); |
Benny Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame^] | 527 | PJ_ASSERT_RETURN(addr && len>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 528 | |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 529 | // Convert PJLIB's pj_sockaddr into Symbian's TInetAddr |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 530 | TInetAddr inetAddr; |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 531 | status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)addr, len, inetAddr); |
| 532 | if (status != PJ_SUCCESS) |
| 533 | return status; |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 534 | |
| 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 | */ |
| 548 | PJ_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 Prijono | b2c9682 | 2007-05-03 13:31:21 +0000 | [diff] [blame] | 556 | pj_bzero(&addr, sizeof(addr)); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 557 | 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 | */ |
| 568 | PJ_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 | */ |
| 585 | PJ_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 Prijono | a0c8b5c | 2007-05-12 15:03:23 +0000 | [diff] [blame] | 592 | *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 593 | |
| 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 Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 603 | return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)addr, namelen); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Get socket name. |
| 608 | */ |
| 609 | PJ_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 Prijono | a0c8b5c | 2007-05-12 15:03:23 +0000 | [diff] [blame] | 616 | *namelen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 617 | |
| 618 | CPjSocket *pjSock = (CPjSocket*)sock; |
| 619 | RSocket &rSock = pjSock->Socket(); |
| 620 | |
| 621 | TInetAddr inetAddr; |
| 622 | rSock.LocalName(inetAddr); |
| 623 | |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 624 | return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)addr, namelen); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Send data |
| 629 | */ |
| 630 | PJ_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 | */ |
| 662 | PJ_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 Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 669 | pj_status_t status; |
| 670 | |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 671 | 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 Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame^] | 678 | PJ_ASSERT_RETURN(tolen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 679 | |
| 680 | TInetAddr inetAddr; |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 681 | status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)to, tolen, inetAddr); |
| 682 | if (status != PJ_SUCCESS) |
| 683 | return status; |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 684 | |
| 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 | */ |
| 703 | PJ_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 | */ |
| 754 | PJ_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 Prijono | a0c8b5c | 2007-05-12 15:03:23 +0000 | [diff] [blame] | 765 | PJ_ASSERT_RETURN(*fromlen >= (int)sizeof(pj_sockaddr_in), PJ_EINVAL); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 766 | |
| 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 Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 786 | return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from, |
| 787 | fromlen); |
| 788 | } else { |
| 789 | return PJ_SUCCESS; |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 790 | } |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 791 | } |
| 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 Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 805 | return PjSymbianOS::Addr2pj(inetAddr, *(pj_sockaddr*)from, fromlen); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 806 | } else { |
| 807 | *len = -1; |
| 808 | *fromlen = -1; |
| 809 | return PJ_RETURN_OS_ERROR(reqStatus.Int()); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Get socket option. |
| 815 | */ |
| 816 | PJ_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 | */ |
| 834 | PJ_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 | */ |
| 852 | PJ_DEF(pj_status_t) pj_sock_connect( pj_sock_t sock, |
| 853 | const pj_sockaddr_t *addr, |
| 854 | int namelen) |
| 855 | { |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 856 | pj_status_t status; |
| 857 | |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 858 | 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 Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 870 | status = PjSymbianOS::pj2Addr(*(pj_sockaddr*)addr, namelen, inetAddr); |
| 871 | if (status != PJ_SUCCESS) |
| 872 | return status; |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 873 | |
| 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 |
| 890 | PJ_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 | */ |
| 923 | PJ_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 | */ |
| 945 | PJ_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 Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 972 | CPjSocket *newPjSock = new CPjSocket(pjSock->GetAf(), newSock); |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 973 | 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 | |