andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "primpl.h" andre@0: andre@0: #include andre@0: andre@0: /************************************************************************/ andre@0: andre@0: /* These two functions are only used in assertions. */ andre@0: #if defined(DEBUG) andre@0: andre@0: PRBool IsValidNetAddr(const PRNetAddr *addr) andre@0: { andre@0: if ((addr != NULL) andre@0: #if defined(XP_UNIX) || defined(XP_OS2) andre@0: && (addr->raw.family != PR_AF_LOCAL) andre@0: #endif andre@0: && (addr->raw.family != PR_AF_INET6) andre@0: && (addr->raw.family != PR_AF_INET)) { andre@0: return PR_FALSE; andre@0: } andre@0: return PR_TRUE; andre@0: } andre@0: andre@0: static PRBool IsValidNetAddrLen(const PRNetAddr *addr, PRInt32 addr_len) andre@0: { andre@0: /* andre@0: * The definition of the length of a Unix domain socket address andre@0: * is not uniform, so we don't check it. andre@0: */ andre@0: if ((addr != NULL) andre@0: #if defined(XP_UNIX) || defined(XP_OS2) andre@0: && (addr->raw.family != AF_UNIX) andre@0: #endif andre@0: && (PR_NETADDR_SIZE(addr) != addr_len)) { andre@0: #if defined(LINUX) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 1 andre@0: /* andre@0: * In glibc 2.1, struct sockaddr_in6 is 24 bytes. In glibc 2.2 andre@0: * and in the 2.4 kernel, struct sockaddr_in6 has the scope_id andre@0: * field and is 28 bytes. It is possible for socket functions andre@0: * to return an addr_len greater than sizeof(struct sockaddr_in6). andre@0: * We need to allow that. (Bugzilla bug #77264) andre@0: */ andre@0: if ((PR_AF_INET6 == addr->raw.family) andre@0: && (sizeof(addr->ipv6) == addr_len)) { andre@0: return PR_TRUE; andre@0: } andre@0: #endif andre@0: /* andre@0: * The accept(), getsockname(), etc. calls on some platforms andre@0: * do not set the actual socket address length on return. andre@0: * In this case, we verifiy addr_len is still the value we andre@0: * passed in (i.e., sizeof(PRNetAddr)). andre@0: */ andre@0: #if defined(QNX) andre@0: if (sizeof(PRNetAddr) == addr_len) { andre@0: return PR_TRUE; andre@0: } andre@0: #endif andre@0: return PR_FALSE; andre@0: } andre@0: return PR_TRUE; andre@0: } andre@0: andre@0: #endif /* DEBUG */ andre@0: andre@0: static PRInt32 PR_CALLBACK SocketWritev(PRFileDesc *fd, const PRIOVec *iov, andre@0: PRInt32 iov_size, PRIntervalTime timeout) andre@0: { andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: int w = 0; andre@0: const PRIOVec *tmp_iov; andre@0: #define LOCAL_MAXIOV 8 andre@0: PRIOVec local_iov[LOCAL_MAXIOV]; andre@0: PRIOVec *iov_copy = NULL; andre@0: int tmp_out; andre@0: int index, iov_cnt; andre@0: int count=0, sz = 0; /* 'count' is the return value. */ andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return -1; andre@0: } andre@0: andre@0: /* andre@0: * Assume the first writev will succeed. Copy iov's only on andre@0: * failure. andre@0: */ andre@0: tmp_iov = iov; andre@0: for (index = 0; index < iov_size; index++) andre@0: sz += iov[index].iov_len; andre@0: andre@0: iov_cnt = iov_size; andre@0: andre@0: while (sz > 0) { andre@0: andre@0: w = _PR_MD_WRITEV(fd, tmp_iov, iov_cnt, timeout); andre@0: if (w < 0) { andre@0: count = -1; andre@0: break; andre@0: } andre@0: count += w; andre@0: if (fd->secret->nonblocking) { andre@0: break; andre@0: } andre@0: sz -= w; andre@0: andre@0: if (sz > 0) { andre@0: /* find the next unwritten vector */ andre@0: for ( index = 0, tmp_out = count; andre@0: tmp_out >= iov[index].iov_len; andre@0: tmp_out -= iov[index].iov_len, index++){;} /* nothing to execute */ andre@0: andre@0: if (tmp_iov == iov) { andre@0: /* andre@0: * The first writev failed so we andre@0: * must copy iov's around. andre@0: * Avoid calloc/free if there andre@0: * are few enough iov's. andre@0: */ andre@0: if (iov_size - index <= LOCAL_MAXIOV) andre@0: iov_copy = local_iov; andre@0: else if ((iov_copy = (PRIOVec *) PR_CALLOC((iov_size - index) * andre@0: sizeof *iov_copy)) == NULL) { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return -1; andre@0: } andre@0: tmp_iov = iov_copy; andre@0: } andre@0: andre@0: PR_ASSERT(tmp_iov == iov_copy); andre@0: andre@0: /* fill in the first partial read */ andre@0: iov_copy[0].iov_base = &(((char *)iov[index].iov_base)[tmp_out]); andre@0: iov_copy[0].iov_len = iov[index].iov_len - tmp_out; andre@0: index++; andre@0: andre@0: /* copy the remaining vectors */ andre@0: for (iov_cnt=1; indexsecret->af = AF_INET; andre@0: #endif andre@0: } else andre@0: _PR_MD_CLOSE_SOCKET(osfd); andre@0: return(fd); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc *) PR_ImportUDPSocket(PROsfd osfd) andre@0: { andre@0: PRFileDesc *fd; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: fd = PR_AllocFileDesc(osfd, PR_GetUDPMethods()); andre@0: if (fd != NULL) { andre@0: _PR_MD_MAKE_NONBLOCK(fd); andre@0: _PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE); andre@0: } else andre@0: _PR_MD_CLOSE_SOCKET(osfd); andre@0: return(fd); andre@0: } andre@0: andre@0: andre@0: static const PRIOMethods* PR_GetSocketPollFdMethods(void); andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_CreateSocketPollFd(PROsfd osfd) andre@0: { andre@0: PRFileDesc *fd; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: fd = _PR_Getfd(); andre@0: andre@0: if (fd == NULL) PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: else andre@0: { andre@0: fd->secret->md.osfd = osfd; andre@0: fd->secret->inheritable = _PR_TRI_FALSE; andre@0: fd->secret->state = _PR_FILEDESC_OPEN; andre@0: fd->methods = PR_GetSocketPollFdMethods(); andre@0: } andre@0: andre@0: return fd; andre@0: } /* PR_CreateSocketPollFD */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_DestroySocketPollFd(PRFileDesc *fd) andre@0: { andre@0: if (NULL == fd) andre@0: { andre@0: PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: fd->secret->state = _PR_FILEDESC_CLOSED; andre@0: _PR_Putfd(fd); andre@0: return PR_SUCCESS; andre@0: } /* PR_DestroySocketPollFd */ andre@0: andre@0: static PRStatus PR_CALLBACK SocketConnect( andre@0: PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PRInt32 rv; /* Return value of _PR_MD_CONNECT */ andre@0: const PRNetAddr *addrp = addr; andre@0: #if defined(_PR_INET6) andre@0: PRNetAddr addrCopy; andre@0: #endif andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: #if defined(_PR_INET6) andre@0: if (addr->raw.family == PR_AF_INET6) { andre@0: addrCopy = *addr; andre@0: addrCopy.raw.family = AF_INET6; andre@0: addrp = &addrCopy; andre@0: } andre@0: #endif andre@0: andre@0: rv = _PR_MD_CONNECT(fd, addrp, PR_NETADDR_SIZE(addr), timeout); andre@0: PR_LOG(_pr_io_lm, PR_LOG_MAX, ("connect -> %d", rv)); andre@0: if (rv == 0) andre@0: return PR_SUCCESS; andre@0: else andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK SocketConnectContinue( andre@0: PRFileDesc *fd, PRInt16 out_flags) andre@0: { andre@0: PROsfd osfd; andre@0: int err; andre@0: andre@0: if (out_flags & PR_POLL_NVAL) { andre@0: PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: if ((out_flags & (PR_POLL_WRITE | PR_POLL_EXCEPT | PR_POLL_ERR)) == 0) { andre@0: PR_ASSERT(out_flags == 0); andre@0: PR_SetError(PR_IN_PROGRESS_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: osfd = fd->secret->md.osfd; andre@0: andre@0: #if defined(XP_UNIX) andre@0: andre@0: err = _MD_unix_get_nonblocking_connect_error(osfd); andre@0: if (err != 0) { andre@0: _PR_MD_MAP_CONNECT_ERROR(err); andre@0: return PR_FAILURE; andre@0: } andre@0: return PR_SUCCESS; andre@0: andre@0: #elif defined(WIN32) || defined(WIN16) andre@0: andre@0: if (out_flags & PR_POLL_EXCEPT) { andre@0: int len = sizeof(err); andre@0: if (getsockopt(osfd, (int)SOL_SOCKET, SO_ERROR, (char *) &err, &len) andre@0: == SOCKET_ERROR) { andre@0: _PR_MD_MAP_GETSOCKOPT_ERROR(WSAGetLastError()); andre@0: return PR_FAILURE; andre@0: } andre@0: if (err != 0) { andre@0: _PR_MD_MAP_CONNECT_ERROR(err); andre@0: } else { andre@0: PR_SetError(PR_UNKNOWN_ERROR, 0); andre@0: } andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: PR_ASSERT(out_flags & PR_POLL_WRITE); andre@0: return PR_SUCCESS; andre@0: andre@0: #elif defined(XP_OS2) andre@0: andre@0: err = _MD_os2_get_nonblocking_connect_error(osfd); andre@0: if (err != 0) { andre@0: _PR_MD_MAP_CONNECT_ERROR(err); andre@0: return PR_FAILURE; andre@0: } andre@0: return PR_SUCCESS; andre@0: andre@0: #elif defined(XP_BEOS) andre@0: andre@0: #ifdef BONE_VERSION /* bug 122364 */ andre@0: /* temporary workaround until getsockopt(SO_ERROR) works in BONE */ andre@0: if (out_flags & PR_POLL_EXCEPT) { andre@0: PR_SetError(PR_CONNECT_REFUSED_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: PR_ASSERT(out_flags & PR_POLL_WRITE); andre@0: return PR_SUCCESS; andre@0: #else andre@0: err = _MD_beos_get_nonblocking_connect_error(fd); andre@0: if( err != 0 ) { andre@0: _PR_MD_MAP_CONNECT_ERROR(err); andre@0: return PR_FAILURE; andre@0: } andre@0: else andre@0: return PR_SUCCESS; andre@0: #endif /* BONE_VERSION */ andre@0: andre@0: #else andre@0: PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); andre@0: return PR_FAILURE; andre@0: #endif andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetConnectStatus(const PRPollDesc *pd) andre@0: { andre@0: /* Find the NSPR layer and invoke its connectcontinue method */ andre@0: PRFileDesc *bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER); andre@0: andre@0: if (NULL == bottom) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: return SocketConnectContinue(bottom, pd->out_flags); andre@0: } andre@0: andre@0: static PRFileDesc* PR_CALLBACK SocketAccept(PRFileDesc *fd, PRNetAddr *addr, andre@0: PRIntervalTime timeout) andre@0: { andre@0: PROsfd osfd; andre@0: PRFileDesc *fd2; andre@0: PRUint32 al; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: #ifdef WINNT andre@0: PRNetAddr addrCopy; andre@0: #endif andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return 0; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return 0; andre@0: } andre@0: andre@0: #ifdef WINNT andre@0: if (addr == NULL) { andre@0: addr = &addrCopy; andre@0: } andre@0: #endif andre@0: al = sizeof(PRNetAddr); andre@0: osfd = _PR_MD_ACCEPT(fd, addr, &al, timeout); andre@0: if (osfd == -1) andre@0: return 0; andre@0: andre@0: fd2 = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); andre@0: if (!fd2) { andre@0: _PR_MD_CLOSE_SOCKET(osfd); andre@0: return NULL; andre@0: } andre@0: andre@0: fd2->secret->nonblocking = fd->secret->nonblocking; andre@0: fd2->secret->inheritable = fd->secret->inheritable; andre@0: #ifdef WINNT andre@0: if (!fd2->secret->nonblocking && fd2->secret->inheritable != _PR_TRI_TRUE) { andre@0: /* andre@0: * The new socket has been associated with an I/O andre@0: * completion port. There is no going back. andre@0: */ andre@0: fd2->secret->md.io_model_committed = PR_TRUE; andre@0: } andre@0: PR_ASSERT(al == PR_NETADDR_SIZE(addr)); andre@0: fd2->secret->md.accepted_socket = PR_TRUE; andre@0: memcpy(&fd2->secret->md.peer_addr, addr, al); andre@0: #endif andre@0: andre@0: /* andre@0: * On some platforms, the new socket created by accept() andre@0: * inherits the nonblocking (or overlapped io) attribute andre@0: * of the listening socket. As an optimization, these andre@0: * platforms can skip the following _PR_MD_MAKE_NONBLOCK andre@0: * call. andre@0: */ andre@0: #if !defined(SOLARIS) && !defined(IRIX) && !defined(WINNT) andre@0: _PR_MD_MAKE_NONBLOCK(fd2); andre@0: #endif andre@0: andre@0: #ifdef _PR_INET6 andre@0: if (addr && (AF_INET6 == addr->raw.family)) andre@0: addr->raw.family = PR_AF_INET6; andre@0: #endif andre@0: PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); andre@0: PR_ASSERT(IsValidNetAddrLen(addr, al) == PR_TRUE); andre@0: andre@0: return fd2; andre@0: } andre@0: andre@0: #ifdef WINNT andre@0: PR_IMPLEMENT(PRFileDesc*) PR_NTFast_Accept(PRFileDesc *fd, PRNetAddr *addr, andre@0: PRIntervalTime timeout) andre@0: { andre@0: PROsfd osfd; andre@0: PRFileDesc *fd2; andre@0: PRIntn al; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: PRNetAddr addrCopy; andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return 0; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return 0; andre@0: } andre@0: andre@0: if (addr == NULL) { andre@0: addr = &addrCopy; andre@0: } andre@0: al = PR_NETADDR_SIZE(addr); andre@0: osfd = _PR_MD_FAST_ACCEPT(fd, addr, &al, timeout, PR_TRUE, NULL, NULL); andre@0: if (osfd == -1) { andre@0: return 0; andre@0: } andre@0: andre@0: fd2 = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); andre@0: if (!fd2) { andre@0: _PR_MD_CLOSE_SOCKET(osfd); andre@0: } else { andre@0: fd2->secret->nonblocking = fd->secret->nonblocking; andre@0: fd2->secret->md.io_model_committed = PR_TRUE; andre@0: PR_ASSERT(al == PR_NETADDR_SIZE(addr)); andre@0: fd2->secret->md.accepted_socket = PR_TRUE; andre@0: memcpy(&fd2->secret->md.peer_addr, addr, al); andre@0: #ifdef _PR_INET6 andre@0: if (AF_INET6 == addr->raw.family) andre@0: addr->raw.family = PR_AF_INET6; andre@0: #endif andre@0: #ifdef _PR_NEED_SECRET_AF andre@0: fd2->secret->af = fd->secret->af; andre@0: #endif andre@0: } andre@0: return fd2; andre@0: } andre@0: #endif /* WINNT */ andre@0: andre@0: andre@0: static PRStatus PR_CALLBACK SocketBind(PRFileDesc *fd, const PRNetAddr *addr) andre@0: { andre@0: PRInt32 result; andre@0: const PRNetAddr *addrp = addr; andre@0: #if defined(_PR_INET6) andre@0: PRNetAddr addrCopy; andre@0: #endif andre@0: andre@0: PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); andre@0: andre@0: #ifdef XP_UNIX andre@0: if (addr->raw.family == AF_UNIX) { andre@0: /* Disallow relative pathnames */ andre@0: if (addr->local.path[0] != '/') { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: } andre@0: #endif /* XP_UNIX */ andre@0: andre@0: #if defined(_PR_INET6) andre@0: if (addr->raw.family == PR_AF_INET6) { andre@0: addrCopy = *addr; andre@0: addrCopy.raw.family = AF_INET6; andre@0: addrp = &addrCopy; andre@0: } andre@0: #endif andre@0: result = _PR_MD_BIND(fd, addrp, PR_NETADDR_SIZE(addr)); andre@0: if (result < 0) { andre@0: return PR_FAILURE; andre@0: } andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK SocketListen(PRFileDesc *fd, PRIntn backlog) andre@0: { andre@0: PRInt32 result; andre@0: andre@0: result = _PR_MD_LISTEN(fd, backlog); andre@0: if (result < 0) { andre@0: return PR_FAILURE; andre@0: } andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK SocketShutdown(PRFileDesc *fd, PRIntn how) andre@0: { andre@0: PRInt32 result; andre@0: andre@0: result = _PR_MD_SHUTDOWN(fd, how); andre@0: if (result < 0) { andre@0: return PR_FAILURE; andre@0: } andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK SocketRecv(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, andre@0: PRIntervalTime timeout) andre@0: { andre@0: PRInt32 rv; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if ((flags != 0) && (flags != PR_MSG_PEEK)) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return -1; andre@0: } andre@0: andre@0: PR_LOG(_pr_io_lm, PR_LOG_MAX, andre@0: ("recv: fd=%p osfd=%" PR_PRIdOSFD " buf=%p amount=%d flags=%d", andre@0: fd, fd->secret->md.osfd, buf, amount, flags)); andre@0: andre@0: #ifdef _PR_HAVE_PEEK_BUFFER andre@0: if (fd->secret->peekBytes != 0) { andre@0: rv = (amount < fd->secret->peekBytes) ? andre@0: amount : fd->secret->peekBytes; andre@0: memcpy(buf, fd->secret->peekBuffer, rv); andre@0: if (flags == 0) { andre@0: /* consume the bytes in the peek buffer */ andre@0: fd->secret->peekBytes -= rv; andre@0: if (fd->secret->peekBytes != 0) { andre@0: memmove(fd->secret->peekBuffer, andre@0: fd->secret->peekBuffer + rv, andre@0: fd->secret->peekBytes); andre@0: } andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* allocate peek buffer, if necessary */ andre@0: if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) { andre@0: PR_ASSERT(0 == fd->secret->peekBytes); andre@0: /* impose a max size on the peek buffer */ andre@0: if (amount > _PR_PEEK_BUFFER_MAX) { andre@0: amount = _PR_PEEK_BUFFER_MAX; andre@0: } andre@0: if (fd->secret->peekBufSize < amount) { andre@0: if (fd->secret->peekBuffer) { andre@0: PR_Free(fd->secret->peekBuffer); andre@0: } andre@0: fd->secret->peekBufSize = amount; andre@0: fd->secret->peekBuffer = PR_Malloc(amount); andre@0: if (NULL == fd->secret->peekBuffer) { andre@0: fd->secret->peekBufSize = 0; andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return -1; andre@0: } andre@0: } andre@0: } andre@0: #endif andre@0: andre@0: rv = _PR_MD_RECV(fd, buf, amount, flags, timeout); andre@0: PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv -> %d, error = %d, os error = %d", andre@0: rv, PR_GetError(), PR_GetOSError())); andre@0: andre@0: #ifdef _PR_HAVE_PEEK_BUFFER andre@0: if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) { andre@0: if (rv > 0) { andre@0: memcpy(fd->secret->peekBuffer, buf, rv); andre@0: fd->secret->peekBytes = rv; andre@0: } andre@0: } andre@0: #endif andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK SocketRead(PRFileDesc *fd, void *buf, PRInt32 amount) andre@0: { andre@0: return SocketRecv(fd, buf, amount, 0, PR_INTERVAL_NO_TIMEOUT); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK SocketSend(PRFileDesc *fd, const void *buf, PRInt32 amount, andre@0: PRIntn flags, PRIntervalTime timeout) andre@0: { andre@0: PRInt32 temp, count; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return -1; andre@0: } andre@0: andre@0: count = 0; andre@0: while (amount > 0) { andre@0: PR_LOG(_pr_io_lm, PR_LOG_MAX, andre@0: ("send: fd=%p osfd=%" PR_PRIdOSFD " buf=%p amount=%d", andre@0: fd, fd->secret->md.osfd, buf, amount)); andre@0: temp = _PR_MD_SEND(fd, buf, amount, flags, timeout); andre@0: if (temp < 0) { andre@0: count = -1; andre@0: break; andre@0: } andre@0: andre@0: count += temp; andre@0: if (fd->secret->nonblocking) { andre@0: break; andre@0: } andre@0: buf = (const void*) ((const char*)buf + temp); andre@0: andre@0: amount -= temp; andre@0: } andre@0: PR_LOG(_pr_io_lm, PR_LOG_MAX, ("send -> %d", count)); andre@0: return count; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK SocketWrite(PRFileDesc *fd, const void *buf, PRInt32 amount) andre@0: { andre@0: return SocketSend(fd, buf, amount, 0, PR_INTERVAL_NO_TIMEOUT); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK SocketClose(PRFileDesc *fd) andre@0: { andre@0: if (!fd || !fd->secret andre@0: || (fd->secret->state != _PR_FILEDESC_OPEN andre@0: && fd->secret->state != _PR_FILEDESC_CLOSED)) { andre@0: PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: if (fd->secret->state == _PR_FILEDESC_OPEN) { andre@0: if (_PR_MD_CLOSE_SOCKET(fd->secret->md.osfd) < 0) { andre@0: return PR_FAILURE; andre@0: } andre@0: fd->secret->state = _PR_FILEDESC_CLOSED; andre@0: } andre@0: andre@0: #ifdef _PR_HAVE_PEEK_BUFFER andre@0: if (fd->secret->peekBuffer) { andre@0: PR_ASSERT(fd->secret->peekBufSize > 0); andre@0: PR_DELETE(fd->secret->peekBuffer); andre@0: fd->secret->peekBufSize = 0; andre@0: fd->secret->peekBytes = 0; andre@0: } andre@0: #endif andre@0: andre@0: PR_FreeFileDesc(fd); andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK SocketAvailable(PRFileDesc *fd) andre@0: { andre@0: PRInt32 rv; andre@0: #ifdef _PR_HAVE_PEEK_BUFFER andre@0: if (fd->secret->peekBytes != 0) { andre@0: return fd->secret->peekBytes; andre@0: } andre@0: #endif andre@0: rv = _PR_MD_SOCKETAVAILABLE(fd); andre@0: return rv; andre@0: } andre@0: andre@0: static PRInt64 PR_CALLBACK SocketAvailable64(PRFileDesc *fd) andre@0: { andre@0: PRInt64 rv; andre@0: #ifdef _PR_HAVE_PEEK_BUFFER andre@0: if (fd->secret->peekBytes != 0) { andre@0: LL_I2L(rv, fd->secret->peekBytes); andre@0: return rv; andre@0: } andre@0: #endif andre@0: LL_I2L(rv, _PR_MD_SOCKETAVAILABLE(fd)); andre@0: return rv; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK SocketSync(PRFileDesc *fd) andre@0: { andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK SocketSendTo( andre@0: PRFileDesc *fd, const void *buf, PRInt32 amount, andre@0: PRIntn flags, const PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PRInt32 temp, count; andre@0: const PRNetAddr *addrp = addr; andre@0: #if defined(_PR_INET6) andre@0: PRNetAddr addrCopy; andre@0: #endif andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return -1; andre@0: } andre@0: andre@0: PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); andre@0: #if defined(_PR_INET6) andre@0: if (addr->raw.family == PR_AF_INET6) { andre@0: addrCopy = *addr; andre@0: addrCopy.raw.family = AF_INET6; andre@0: addrp = &addrCopy; andre@0: } andre@0: #endif andre@0: andre@0: count = 0; andre@0: while (amount > 0) { andre@0: temp = _PR_MD_SENDTO(fd, buf, amount, flags, andre@0: addrp, PR_NETADDR_SIZE(addr), timeout); andre@0: if (temp < 0) { andre@0: count = -1; andre@0: break; andre@0: } andre@0: count += temp; andre@0: if (fd->secret->nonblocking) { andre@0: break; andre@0: } andre@0: buf = (const void*) ((const char*)buf + temp); andre@0: amount -= temp; andre@0: } andre@0: return count; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK SocketRecvFrom(PRFileDesc *fd, void *buf, PRInt32 amount, andre@0: PRIntn flags, PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PRInt32 rv; andre@0: PRUint32 al; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return -1; andre@0: } andre@0: andre@0: al = sizeof(PRNetAddr); andre@0: rv = _PR_MD_RECVFROM(fd, buf, amount, flags, addr, &al, timeout); andre@0: #ifdef _PR_INET6 andre@0: if (addr && (AF_INET6 == addr->raw.family)) andre@0: addr->raw.family = PR_AF_INET6; andre@0: #endif andre@0: return rv; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK SocketAcceptRead(PRFileDesc *sd, PRFileDesc **nd, andre@0: PRNetAddr **raddr, void *buf, PRInt32 amount, andre@0: PRIntervalTime timeout) andre@0: { andre@0: PRInt32 rv; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return -1; andre@0: } andre@0: /* The socket must be in blocking mode. */ andre@0: if (sd->secret->nonblocking) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: *nd = NULL; andre@0: andre@0: #if defined(WINNT) andre@0: { andre@0: PROsfd newSock; andre@0: PRNetAddr *raddrCopy; andre@0: andre@0: if (raddr == NULL) { andre@0: raddr = &raddrCopy; andre@0: } andre@0: rv = _PR_MD_ACCEPT_READ(sd, &newSock, raddr, buf, amount, timeout); andre@0: if (rv < 0) { andre@0: rv = -1; andre@0: } else { andre@0: /* Successfully accepted and read; create the new PRFileDesc */ andre@0: *nd = PR_AllocFileDesc(newSock, PR_GetTCPMethods()); andre@0: if (*nd == 0) { andre@0: _PR_MD_CLOSE_SOCKET(newSock); andre@0: /* PR_AllocFileDesc() has invoked PR_SetError(). */ andre@0: rv = -1; andre@0: } else { andre@0: (*nd)->secret->md.io_model_committed = PR_TRUE; andre@0: (*nd)->secret->md.accepted_socket = PR_TRUE; andre@0: memcpy(&(*nd)->secret->md.peer_addr, *raddr, andre@0: PR_NETADDR_SIZE(*raddr)); andre@0: #ifdef _PR_INET6 andre@0: if (AF_INET6 == *raddr->raw.family) andre@0: *raddr->raw.family = PR_AF_INET6; andre@0: #endif andre@0: } andre@0: } andre@0: } andre@0: #else andre@0: rv = PR_EmulateAcceptRead(sd, nd, raddr, buf, amount, timeout); andre@0: #endif andre@0: return rv; andre@0: } andre@0: andre@0: #ifdef WINNT andre@0: PR_IMPLEMENT(PRInt32) PR_NTFast_AcceptRead(PRFileDesc *sd, PRFileDesc **nd, andre@0: PRNetAddr **raddr, void *buf, PRInt32 amount, andre@0: PRIntervalTime timeout) andre@0: { andre@0: PRInt32 rv; andre@0: PROsfd newSock; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: PRNetAddr *raddrCopy; andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return -1; andre@0: } andre@0: *nd = NULL; andre@0: andre@0: if (raddr == NULL) { andre@0: raddr = &raddrCopy; andre@0: } andre@0: rv = _PR_MD_FAST_ACCEPT_READ(sd, &newSock, raddr, buf, amount, andre@0: timeout, PR_TRUE, NULL, NULL); andre@0: if (rv < 0) { andre@0: rv = -1; andre@0: } else { andre@0: /* Successfully accepted and read; create the new PRFileDesc */ andre@0: *nd = PR_AllocFileDesc(newSock, PR_GetTCPMethods()); andre@0: if (*nd == 0) { andre@0: _PR_MD_CLOSE_SOCKET(newSock); andre@0: /* PR_AllocFileDesc() has invoked PR_SetError(). */ andre@0: rv = -1; andre@0: } else { andre@0: (*nd)->secret->md.io_model_committed = PR_TRUE; andre@0: (*nd)->secret->md.accepted_socket = PR_TRUE; andre@0: memcpy(&(*nd)->secret->md.peer_addr, *raddr, andre@0: PR_NETADDR_SIZE(*raddr)); andre@0: #ifdef _PR_INET6 andre@0: if (AF_INET6 == *raddr->raw.family) andre@0: *raddr->raw.family = PR_AF_INET6; andre@0: #endif andre@0: #ifdef _PR_NEED_SECRET_AF andre@0: (*nd)->secret->af = sd->secret->af; andre@0: #endif andre@0: } andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRInt32) PR_NTFast_AcceptRead_WithTimeoutCallback( andre@0: PRFileDesc *sd, PRFileDesc **nd, andre@0: PRNetAddr **raddr, void *buf, PRInt32 amount, andre@0: PRIntervalTime timeout, andre@0: _PR_AcceptTimeoutCallback callback, andre@0: void *callbackArg) andre@0: { andre@0: PRInt32 rv; andre@0: PROsfd newSock; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: PRNetAddr *raddrCopy; andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return -1; andre@0: } andre@0: *nd = NULL; andre@0: andre@0: if (raddr == NULL) { andre@0: raddr = &raddrCopy; andre@0: } andre@0: rv = _PR_MD_FAST_ACCEPT_READ(sd, &newSock, raddr, buf, amount, andre@0: timeout, PR_TRUE, callback, callbackArg); andre@0: if (rv < 0) { andre@0: rv = -1; andre@0: } else { andre@0: /* Successfully accepted and read; create the new PRFileDesc */ andre@0: *nd = PR_AllocFileDesc(newSock, PR_GetTCPMethods()); andre@0: if (*nd == 0) { andre@0: _PR_MD_CLOSE_SOCKET(newSock); andre@0: /* PR_AllocFileDesc() has invoked PR_SetError(). */ andre@0: rv = -1; andre@0: } else { andre@0: (*nd)->secret->md.io_model_committed = PR_TRUE; andre@0: (*nd)->secret->md.accepted_socket = PR_TRUE; andre@0: memcpy(&(*nd)->secret->md.peer_addr, *raddr, andre@0: PR_NETADDR_SIZE(*raddr)); andre@0: #ifdef _PR_INET6 andre@0: if (AF_INET6 == *raddr->raw.family) andre@0: *raddr->raw.family = PR_AF_INET6; andre@0: #endif andre@0: #ifdef _PR_NEED_SECRET_AF andre@0: (*nd)->secret->af = sd->secret->af; andre@0: #endif andre@0: } andre@0: } andre@0: return rv; andre@0: } andre@0: #endif /* WINNT */ andre@0: andre@0: #ifdef WINNT andre@0: PR_IMPLEMENT(void) andre@0: PR_NTFast_UpdateAcceptContext(PRFileDesc *socket, PRFileDesc *acceptSocket) andre@0: { andre@0: _PR_MD_UPDATE_ACCEPT_CONTEXT( andre@0: socket->secret->md.osfd, acceptSocket->secret->md.osfd); andre@0: } andre@0: #endif /* WINNT */ andre@0: andre@0: static PRInt32 PR_CALLBACK SocketSendFile( andre@0: PRFileDesc *sd, PRSendFileData *sfd, andre@0: PRTransmitFileFlags flags, PRIntervalTime timeout) andre@0: { andre@0: PRInt32 rv; andre@0: PRThread *me = _PR_MD_CURRENT_THREAD(); andre@0: andre@0: if (_PR_PENDING_INTERRUPT(me)) { andre@0: me->flags &= ~_PR_INTERRUPT; andre@0: PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: if (_PR_IO_PENDING(me)) { andre@0: PR_SetError(PR_IO_PENDING_ERROR, 0); andre@0: return -1; andre@0: } andre@0: /* The socket must be in blocking mode. */ andre@0: if (sd->secret->nonblocking) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: #if defined(WINNT) andre@0: rv = _PR_MD_SENDFILE(sd, sfd, flags, timeout); andre@0: if ((rv >= 0) && (flags == PR_TRANSMITFILE_CLOSE_SOCKET)) { andre@0: /* andre@0: * This should be kept the same as SocketClose, except andre@0: * that _PR_MD_CLOSE_SOCKET(sd->secret->md.osfd) should andre@0: * not be called because the socket will be recycled. andre@0: */ andre@0: PR_FreeFileDesc(sd); andre@0: } andre@0: #else andre@0: rv = PR_EmulateSendFile(sd, sfd, flags, timeout); andre@0: #endif /* WINNT */ andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK SocketTransmitFile(PRFileDesc *sd, PRFileDesc *fd, andre@0: const void *headers, PRInt32 hlen, PRTransmitFileFlags flags, andre@0: PRIntervalTime timeout) andre@0: { andre@0: PRSendFileData sfd; andre@0: andre@0: sfd.fd = fd; andre@0: sfd.file_offset = 0; andre@0: sfd.file_nbytes = 0; andre@0: sfd.header = headers; andre@0: sfd.hlen = hlen; andre@0: sfd.trailer = NULL; andre@0: sfd.tlen = 0; andre@0: andre@0: return(SocketSendFile(sd, &sfd, flags, timeout)); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK SocketGetName(PRFileDesc *fd, PRNetAddr *addr) andre@0: { andre@0: PRInt32 result; andre@0: PRUint32 addrlen; andre@0: andre@0: addrlen = sizeof(PRNetAddr); andre@0: result = _PR_MD_GETSOCKNAME(fd, addr, &addrlen); andre@0: if (result < 0) { andre@0: return PR_FAILURE; andre@0: } andre@0: #ifdef _PR_INET6 andre@0: if (AF_INET6 == addr->raw.family) andre@0: addr->raw.family = PR_AF_INET6; andre@0: #endif andre@0: PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); andre@0: PR_ASSERT(IsValidNetAddrLen(addr, addrlen) == PR_TRUE); andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK SocketGetPeerName(PRFileDesc *fd, PRNetAddr *addr) andre@0: { andre@0: PRInt32 result; andre@0: PRUint32 addrlen; andre@0: andre@0: addrlen = sizeof(PRNetAddr); andre@0: result = _PR_MD_GETPEERNAME(fd, addr, &addrlen); andre@0: if (result < 0) { andre@0: return PR_FAILURE; andre@0: } andre@0: #ifdef _PR_INET6 andre@0: if (AF_INET6 == addr->raw.family) andre@0: addr->raw.family = PR_AF_INET6; andre@0: #endif andre@0: PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); andre@0: PR_ASSERT(IsValidNetAddrLen(addr, addrlen) == PR_TRUE); andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRInt16 PR_CALLBACK SocketPoll( andre@0: PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags) andre@0: { andre@0: *out_flags = 0; andre@0: return in_flags; andre@0: } /* SocketPoll */ andre@0: andre@0: static PRIOMethods tcpMethods = { andre@0: PR_DESC_SOCKET_TCP, andre@0: SocketClose, andre@0: SocketRead, andre@0: SocketWrite, andre@0: SocketAvailable, andre@0: SocketAvailable64, andre@0: SocketSync, andre@0: (PRSeekFN)_PR_InvalidInt, andre@0: (PRSeek64FN)_PR_InvalidInt64, andre@0: (PRFileInfoFN)_PR_InvalidStatus, andre@0: (PRFileInfo64FN)_PR_InvalidStatus, andre@0: SocketWritev, andre@0: SocketConnect, andre@0: SocketAccept, andre@0: SocketBind, andre@0: SocketListen, andre@0: SocketShutdown, andre@0: SocketRecv, andre@0: SocketSend, andre@0: (PRRecvfromFN)_PR_InvalidInt, andre@0: (PRSendtoFN)_PR_InvalidInt, andre@0: SocketPoll, andre@0: SocketAcceptRead, andre@0: SocketTransmitFile, andre@0: SocketGetName, andre@0: SocketGetPeerName, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: _PR_SocketGetSocketOption, andre@0: _PR_SocketSetSocketOption, andre@0: SocketSendFile, andre@0: SocketConnectContinue, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt andre@0: }; andre@0: andre@0: static PRIOMethods udpMethods = { andre@0: PR_DESC_SOCKET_UDP, andre@0: SocketClose, andre@0: SocketRead, andre@0: SocketWrite, andre@0: SocketAvailable, andre@0: SocketAvailable64, andre@0: SocketSync, andre@0: (PRSeekFN)_PR_InvalidInt, andre@0: (PRSeek64FN)_PR_InvalidInt64, andre@0: (PRFileInfoFN)_PR_InvalidStatus, andre@0: (PRFileInfo64FN)_PR_InvalidStatus, andre@0: SocketWritev, andre@0: SocketConnect, andre@0: (PRAcceptFN)_PR_InvalidDesc, andre@0: SocketBind, andre@0: SocketListen, andre@0: SocketShutdown, andre@0: SocketRecv, andre@0: SocketSend, andre@0: SocketRecvFrom, andre@0: SocketSendTo, andre@0: SocketPoll, andre@0: (PRAcceptreadFN)_PR_InvalidInt, andre@0: (PRTransmitfileFN)_PR_InvalidInt, andre@0: SocketGetName, andre@0: SocketGetPeerName, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: _PR_SocketGetSocketOption, andre@0: _PR_SocketSetSocketOption, andre@0: (PRSendfileFN)_PR_InvalidInt, andre@0: (PRConnectcontinueFN)_PR_InvalidStatus, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt andre@0: }; andre@0: andre@0: andre@0: static PRIOMethods socketpollfdMethods = { andre@0: (PRDescType) 0, andre@0: (PRCloseFN)_PR_InvalidStatus, andre@0: (PRReadFN)_PR_InvalidInt, andre@0: (PRWriteFN)_PR_InvalidInt, andre@0: (PRAvailableFN)_PR_InvalidInt, andre@0: (PRAvailable64FN)_PR_InvalidInt64, andre@0: (PRFsyncFN)_PR_InvalidStatus, andre@0: (PRSeekFN)_PR_InvalidInt, andre@0: (PRSeek64FN)_PR_InvalidInt64, andre@0: (PRFileInfoFN)_PR_InvalidStatus, andre@0: (PRFileInfo64FN)_PR_InvalidStatus, andre@0: (PRWritevFN)_PR_InvalidInt, andre@0: (PRConnectFN)_PR_InvalidStatus, andre@0: (PRAcceptFN)_PR_InvalidDesc, andre@0: (PRBindFN)_PR_InvalidStatus, andre@0: (PRListenFN)_PR_InvalidStatus, andre@0: (PRShutdownFN)_PR_InvalidStatus, andre@0: (PRRecvFN)_PR_InvalidInt, andre@0: (PRSendFN)_PR_InvalidInt, andre@0: (PRRecvfromFN)_PR_InvalidInt, andre@0: (PRSendtoFN)_PR_InvalidInt, andre@0: SocketPoll, andre@0: (PRAcceptreadFN)_PR_InvalidInt, andre@0: (PRTransmitfileFN)_PR_InvalidInt, andre@0: (PRGetsocknameFN)_PR_InvalidStatus, andre@0: (PRGetpeernameFN)_PR_InvalidStatus, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRGetsocketoptionFN)_PR_InvalidStatus, andre@0: (PRSetsocketoptionFN)_PR_InvalidStatus, andre@0: (PRSendfileFN)_PR_InvalidInt, andre@0: (PRConnectcontinueFN)_PR_InvalidStatus, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt andre@0: }; andre@0: andre@0: PR_IMPLEMENT(const PRIOMethods*) PR_GetTCPMethods() andre@0: { andre@0: return &tcpMethods; andre@0: } andre@0: andre@0: PR_IMPLEMENT(const PRIOMethods*) PR_GetUDPMethods() andre@0: { andre@0: return &udpMethods; andre@0: } andre@0: andre@0: static const PRIOMethods* PR_GetSocketPollFdMethods() andre@0: { andre@0: return &socketpollfdMethods; andre@0: } /* PR_GetSocketPollFdMethods */ andre@0: andre@0: #if !defined(_PR_INET6) || defined(_PR_INET6_PROBE) andre@0: PR_EXTERN(PRStatus) _pr_push_ipv6toipv4_layer(PRFileDesc *fd); andre@0: andre@0: #if defined(_PR_INET6_PROBE) andre@0: andre@0: extern PRBool _pr_ipv6_is_present(void); andre@0: andre@0: PR_IMPLEMENT(PRBool) _pr_test_ipv6_socket() andre@0: { andre@0: PROsfd osfd; andre@0: andre@0: osfd = _PR_MD_SOCKET(AF_INET6, SOCK_STREAM, 0); andre@0: if (osfd != -1) { andre@0: _PR_MD_CLOSE_SOCKET(osfd); andre@0: return PR_TRUE; andre@0: } andre@0: return PR_FALSE; andre@0: } andre@0: #endif /* _PR_INET6_PROBE */ andre@0: andre@0: #endif andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_Socket(PRInt32 domain, PRInt32 type, PRInt32 proto) andre@0: { andre@0: PROsfd osfd; andre@0: PRFileDesc *fd; andre@0: PRInt32 tmp_domain = domain; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: if (PR_AF_INET != domain andre@0: && PR_AF_INET6 != domain andre@0: #if defined(XP_UNIX) || defined(XP_OS2) andre@0: && PR_AF_LOCAL != domain andre@0: #endif andre@0: ) { andre@0: PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: andre@0: #if defined(_PR_INET6_PROBE) andre@0: if (PR_AF_INET6 == domain) andre@0: domain = _pr_ipv6_is_present() ? AF_INET6 : AF_INET; andre@0: #elif defined(_PR_INET6) andre@0: if (PR_AF_INET6 == domain) andre@0: domain = AF_INET6; andre@0: #else andre@0: if (PR_AF_INET6 == domain) andre@0: domain = AF_INET; andre@0: #endif /* _PR_INET6 */ andre@0: osfd = _PR_MD_SOCKET(domain, type, proto); andre@0: if (osfd == -1) { andre@0: return 0; andre@0: } andre@0: if (type == SOCK_STREAM) andre@0: fd = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); andre@0: else andre@0: fd = PR_AllocFileDesc(osfd, PR_GetUDPMethods()); andre@0: /* andre@0: * Make the sockets non-blocking andre@0: */ andre@0: if (fd != NULL) { andre@0: _PR_MD_MAKE_NONBLOCK(fd); andre@0: _PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE); andre@0: #ifdef _PR_NEED_SECRET_AF andre@0: fd->secret->af = domain; andre@0: #endif andre@0: #if defined(_PR_INET6_PROBE) || !defined(_PR_INET6) andre@0: /* andre@0: * For platforms with no support for IPv6 andre@0: * create layered socket for IPv4-mapped IPv6 addresses andre@0: */ andre@0: if (PR_AF_INET6 == tmp_domain && PR_AF_INET == domain) { andre@0: if (PR_FAILURE == _pr_push_ipv6toipv4_layer(fd)) { andre@0: PR_Close(fd); andre@0: fd = NULL; andre@0: } andre@0: } andre@0: #endif andre@0: } else andre@0: _PR_MD_CLOSE_SOCKET(osfd); andre@0: andre@0: return fd; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc *) PR_NewTCPSocket(void) andre@0: { andre@0: PRInt32 domain = AF_INET; andre@0: andre@0: return PR_Socket(domain, SOCK_STREAM, 0); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_NewUDPSocket(void) andre@0: { andre@0: PRInt32 domain = AF_INET; andre@0: andre@0: return PR_Socket(domain, SOCK_DGRAM, 0); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc *) PR_OpenTCPSocket(PRIntn af) andre@0: { andre@0: return PR_Socket(af, SOCK_STREAM, 0); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_OpenUDPSocket(PRIntn af) andre@0: { andre@0: return PR_Socket(af, SOCK_DGRAM, 0); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_NewTCPSocketPair(PRFileDesc *f[]) andre@0: { andre@0: #ifdef XP_UNIX andre@0: PRInt32 rv, osfd[2]; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: rv = _PR_MD_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, osfd); andre@0: if (rv == -1) { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: f[0] = PR_AllocFileDesc(osfd[0], PR_GetTCPMethods()); andre@0: if (!f[0]) { andre@0: _PR_MD_CLOSE_SOCKET(osfd[0]); andre@0: _PR_MD_CLOSE_SOCKET(osfd[1]); andre@0: /* PR_AllocFileDesc() has invoked PR_SetError(). */ andre@0: return PR_FAILURE; andre@0: } andre@0: f[1] = PR_AllocFileDesc(osfd[1], PR_GetTCPMethods()); andre@0: if (!f[1]) { andre@0: PR_Close(f[0]); andre@0: _PR_MD_CLOSE_SOCKET(osfd[1]); andre@0: /* PR_AllocFileDesc() has invoked PR_SetError(). */ andre@0: return PR_FAILURE; andre@0: } andre@0: _PR_MD_MAKE_NONBLOCK(f[0]); andre@0: _PR_MD_INIT_FD_INHERITABLE(f[0], PR_FALSE); andre@0: _PR_MD_MAKE_NONBLOCK(f[1]); andre@0: _PR_MD_INIT_FD_INHERITABLE(f[1], PR_FALSE); andre@0: return PR_SUCCESS; andre@0: #elif defined(WINNT) andre@0: /* andre@0: * A socket pair is often used for interprocess communication, andre@0: * so we need to make sure neither socket is associated with andre@0: * the I/O completion port; otherwise it can't be used by a andre@0: * child process. andre@0: * andre@0: * The default implementation below cannot be used for NT andre@0: * because PR_Accept would have associated the I/O completion andre@0: * port with the listening and accepted sockets. andre@0: */ andre@0: SOCKET listenSock; andre@0: SOCKET osfd[2]; andre@0: struct sockaddr_in selfAddr, peerAddr; andre@0: int addrLen; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: osfd[0] = osfd[1] = INVALID_SOCKET; andre@0: listenSock = socket(AF_INET, SOCK_STREAM, 0); andre@0: if (listenSock == INVALID_SOCKET) { andre@0: goto failed; andre@0: } andre@0: selfAddr.sin_family = AF_INET; andre@0: selfAddr.sin_port = 0; andre@0: selfAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* BugZilla: 35408 */ andre@0: addrLen = sizeof(selfAddr); andre@0: if (bind(listenSock, (struct sockaddr *) &selfAddr, andre@0: addrLen) == SOCKET_ERROR) { andre@0: goto failed; andre@0: } andre@0: if (getsockname(listenSock, (struct sockaddr *) &selfAddr, andre@0: &addrLen) == SOCKET_ERROR) { andre@0: goto failed; andre@0: } andre@0: if (listen(listenSock, 5) == SOCKET_ERROR) { andre@0: goto failed; andre@0: } andre@0: osfd[0] = socket(AF_INET, SOCK_STREAM, 0); andre@0: if (osfd[0] == INVALID_SOCKET) { andre@0: goto failed; andre@0: } andre@0: selfAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); andre@0: andre@0: /* andre@0: * Only a thread is used to do the connect and accept. andre@0: * I am relying on the fact that connect returns andre@0: * successfully as soon as the connect request is put andre@0: * into the listen queue (but before accept is called). andre@0: * This is the behavior of the BSD socket code. If andre@0: * connect does not return until accept is called, we andre@0: * will need to create another thread to call connect. andre@0: */ andre@0: if (connect(osfd[0], (struct sockaddr *) &selfAddr, andre@0: addrLen) == SOCKET_ERROR) { andre@0: goto failed; andre@0: } andre@0: /* andre@0: * A malicious local process may connect to the listening andre@0: * socket, so we need to verify that the accepted connection andre@0: * is made from our own socket osfd[0]. andre@0: */ andre@0: if (getsockname(osfd[0], (struct sockaddr *) &selfAddr, andre@0: &addrLen) == SOCKET_ERROR) { andre@0: goto failed; andre@0: } andre@0: osfd[1] = accept(listenSock, (struct sockaddr *) &peerAddr, &addrLen); andre@0: if (osfd[1] == INVALID_SOCKET) { andre@0: goto failed; andre@0: } andre@0: if (peerAddr.sin_port != selfAddr.sin_port) { andre@0: /* the connection we accepted is not from osfd[0] */ andre@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); andre@0: goto failed; andre@0: } andre@0: closesocket(listenSock); andre@0: andre@0: f[0] = PR_AllocFileDesc(osfd[0], PR_GetTCPMethods()); andre@0: if (!f[0]) { andre@0: closesocket(osfd[0]); andre@0: closesocket(osfd[1]); andre@0: /* PR_AllocFileDesc() has invoked PR_SetError(). */ andre@0: return PR_FAILURE; andre@0: } andre@0: f[1] = PR_AllocFileDesc(osfd[1], PR_GetTCPMethods()); andre@0: if (!f[1]) { andre@0: PR_Close(f[0]); andre@0: closesocket(osfd[1]); andre@0: /* PR_AllocFileDesc() has invoked PR_SetError(). */ andre@0: return PR_FAILURE; andre@0: } andre@0: _PR_MD_INIT_FD_INHERITABLE(f[0], PR_FALSE); andre@0: _PR_MD_INIT_FD_INHERITABLE(f[1], PR_FALSE); andre@0: return PR_SUCCESS; andre@0: andre@0: failed: andre@0: if (listenSock != INVALID_SOCKET) { andre@0: closesocket(listenSock); andre@0: } andre@0: if (osfd[0] != INVALID_SOCKET) { andre@0: closesocket(osfd[0]); andre@0: } andre@0: if (osfd[1] != INVALID_SOCKET) { andre@0: closesocket(osfd[1]); andre@0: } andre@0: return PR_FAILURE; andre@0: #else /* not Unix or NT */ andre@0: /* andre@0: * default implementation andre@0: */ andre@0: PRFileDesc *listenSock; andre@0: PRNetAddr selfAddr, peerAddr; andre@0: PRUint16 port; andre@0: andre@0: f[0] = f[1] = NULL; andre@0: listenSock = PR_NewTCPSocket(); andre@0: if (listenSock == NULL) { andre@0: goto failed; andre@0: } andre@0: PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &selfAddr); /* BugZilla: 35408 */ andre@0: if (PR_Bind(listenSock, &selfAddr) == PR_FAILURE) { andre@0: goto failed; andre@0: } andre@0: if (PR_GetSockName(listenSock, &selfAddr) == PR_FAILURE) { andre@0: goto failed; andre@0: } andre@0: port = ntohs(selfAddr.inet.port); andre@0: if (PR_Listen(listenSock, 5) == PR_FAILURE) { andre@0: goto failed; andre@0: } andre@0: f[0] = PR_NewTCPSocket(); andre@0: if (f[0] == NULL) { andre@0: goto failed; andre@0: } andre@0: #ifdef _PR_CONNECT_DOES_NOT_BIND andre@0: /* andre@0: * If connect does not implicitly bind the socket (e.g., on andre@0: * BeOS), we have to bind the socket so that we can get its andre@0: * port with getsockname later. andre@0: */ andre@0: PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &selfAddr); andre@0: if (PR_Bind(f[0], &selfAddr) == PR_FAILURE) { andre@0: goto failed; andre@0: } andre@0: #endif andre@0: PR_InitializeNetAddr(PR_IpAddrLoopback, port, &selfAddr); andre@0: andre@0: /* andre@0: * Only a thread is used to do the connect and accept. andre@0: * I am relying on the fact that PR_Connect returns andre@0: * successfully as soon as the connect request is put andre@0: * into the listen queue (but before PR_Accept is called). andre@0: * This is the behavior of the BSD socket code. If andre@0: * connect does not return until accept is called, we andre@0: * will need to create another thread to call connect. andre@0: */ andre@0: if (PR_Connect(f[0], &selfAddr, PR_INTERVAL_NO_TIMEOUT) andre@0: == PR_FAILURE) { andre@0: goto failed; andre@0: } andre@0: /* andre@0: * A malicious local process may connect to the listening andre@0: * socket, so we need to verify that the accepted connection andre@0: * is made from our own socket f[0]. andre@0: */ andre@0: if (PR_GetSockName(f[0], &selfAddr) == PR_FAILURE) { andre@0: goto failed; andre@0: } andre@0: f[1] = PR_Accept(listenSock, &peerAddr, PR_INTERVAL_NO_TIMEOUT); andre@0: if (f[1] == NULL) { andre@0: goto failed; andre@0: } andre@0: if (peerAddr.inet.port != selfAddr.inet.port) { andre@0: /* the connection we accepted is not from f[0] */ andre@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); andre@0: goto failed; andre@0: } andre@0: PR_Close(listenSock); andre@0: return PR_SUCCESS; andre@0: andre@0: failed: andre@0: if (listenSock) { andre@0: PR_Close(listenSock); andre@0: } andre@0: if (f[0]) { andre@0: PR_Close(f[0]); andre@0: } andre@0: if (f[1]) { andre@0: PR_Close(f[1]); andre@0: } andre@0: return PR_FAILURE; andre@0: #endif andre@0: } andre@0: andre@0: PR_IMPLEMENT(PROsfd) andre@0: PR_FileDesc2NativeHandle(PRFileDesc *fd) andre@0: { andre@0: if (fd) { andre@0: fd = PR_GetIdentitiesLayer(fd, PR_NSPR_IO_LAYER); andre@0: } andre@0: if (!fd) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return -1; andre@0: } andre@0: return fd->secret->md.osfd; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) andre@0: PR_ChangeFileDescNativeHandle(PRFileDesc *fd, PROsfd handle) andre@0: { andre@0: if (fd) andre@0: fd->secret->md.osfd = handle; andre@0: } andre@0: andre@0: /* andre@0: ** Select compatibility andre@0: ** andre@0: */ andre@0: andre@0: PR_IMPLEMENT(void) PR_FD_ZERO(PR_fd_set *set) andre@0: { andre@0: memset(set, 0, sizeof(PR_fd_set)); andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PR_FD_SET(PRFileDesc *fh, PR_fd_set *set) andre@0: { andre@0: PR_ASSERT( set->hsize < PR_MAX_SELECT_DESC ); andre@0: andre@0: set->harray[set->hsize++] = fh; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PR_FD_CLR(PRFileDesc *fh, PR_fd_set *set) andre@0: { andre@0: PRUint32 index, index2; andre@0: andre@0: for (index = 0; indexhsize; index++) andre@0: if (set->harray[index] == fh) { andre@0: for (index2=index; index2 < (set->hsize-1); index2++) { andre@0: set->harray[index2] = set->harray[index2+1]; andre@0: } andre@0: set->hsize--; andre@0: break; andre@0: } andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRInt32) PR_FD_ISSET(PRFileDesc *fh, PR_fd_set *set) andre@0: { andre@0: PRUint32 index; andre@0: for (index = 0; indexhsize; index++) andre@0: if (set->harray[index] == fh) { andre@0: return 1; andre@0: } andre@0: return 0; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PR_FD_NSET(PROsfd fd, PR_fd_set *set) andre@0: { andre@0: PR_ASSERT( set->nsize < PR_MAX_SELECT_DESC ); andre@0: andre@0: set->narray[set->nsize++] = fd; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PR_FD_NCLR(PROsfd fd, PR_fd_set *set) andre@0: { andre@0: PRUint32 index, index2; andre@0: andre@0: for (index = 0; indexnsize; index++) andre@0: if (set->narray[index] == fd) { andre@0: for (index2=index; index2 < (set->nsize-1); index2++) { andre@0: set->narray[index2] = set->narray[index2+1]; andre@0: } andre@0: set->nsize--; andre@0: break; andre@0: } andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRInt32) PR_FD_NISSET(PROsfd fd, PR_fd_set *set) andre@0: { andre@0: PRUint32 index; andre@0: for (index = 0; indexnsize; index++) andre@0: if (set->narray[index] == fd) { andre@0: return 1; andre@0: } andre@0: return 0; andre@0: } andre@0: andre@0: andre@0: #if !defined(NEED_SELECT) andre@0: #include "obsolete/probslet.h" andre@0: andre@0: #define PD_INCR 20 andre@0: andre@0: static PRPollDesc *_pr_setfd( andre@0: PR_fd_set *set, PRInt16 flags, PRPollDesc *polldesc) andre@0: { andre@0: PRUintn fsidx, pdidx; andre@0: PRPollDesc *poll = polldesc; andre@0: andre@0: if (NULL == set) return poll; andre@0: andre@0: /* First set the pr file handle osfds */ andre@0: for (fsidx = 0; fsidx < set->hsize; fsidx++) andre@0: { andre@0: for (pdidx = 0; 1; pdidx++) andre@0: { andre@0: if ((PRFileDesc*)-1 == poll[pdidx].fd) andre@0: { andre@0: /* our vector is full - extend and condition it */ andre@0: poll = (PRPollDesc*)PR_Realloc( andre@0: poll, (pdidx + 1 + PD_INCR) * sizeof(PRPollDesc)); andre@0: if (NULL == poll) goto out_of_memory; andre@0: memset( andre@0: poll + pdidx * sizeof(PRPollDesc), andre@0: 0, PD_INCR * sizeof(PRPollDesc)); andre@0: poll[pdidx + PD_INCR].fd = (PRFileDesc*)-1; andre@0: } andre@0: if ((NULL == poll[pdidx].fd) andre@0: || (poll[pdidx].fd == set->harray[fsidx])) andre@0: { andre@0: /* PR_ASSERT(0 == (poll[pdidx].in_flags & flags)); */ andre@0: /* either empty or prevously defined */ andre@0: poll[pdidx].fd = set->harray[fsidx]; /* possibly redundant */ andre@0: poll[pdidx].in_flags |= flags; /* possibly redundant */ andre@0: break; andre@0: } andre@0: } andre@0: } andre@0: andre@0: #if 0 andre@0: /* Second set the native osfds */ andre@0: for (fsidx = 0; fsidx < set->nsize; fsidx++) andre@0: { andre@0: for (pdidx = 0; ((PRFileDesc*)-1 != poll[pdidx].fd); pdidx++) andre@0: { andre@0: if ((PRFileDesc*)-1 == poll[pdidx].fd) andre@0: { andre@0: /* our vector is full - extend and condition it */ andre@0: poll = PR_Realloc( andre@0: poll, (pdidx + PD_INCR) * sizeof(PRPollDesc)); andre@0: if (NULL == poll) goto out_of_memory; andre@0: memset( andre@0: poll + pdidx * sizeof(PRPollDesc), andre@0: 0, PD_INCR * sizeof(PRPollDesc)); andre@0: poll[(pdidx + PD_INCR)].fd = (PRFileDesc*)-1; andre@0: } andre@0: if ((NULL == poll[pdidx].fd) andre@0: || (poll[pdidx].fd == set->narray[fsidx])) andre@0: { andre@0: /* either empty or prevously defined */ andre@0: poll[pdidx].fd = set->narray[fsidx]; andre@0: PR_ASSERT(0 == (poll[pdidx].in_flags & flags)); andre@0: poll[pdidx].in_flags |= flags; andre@0: break; andre@0: } andre@0: } andre@0: } andre@0: #endif /* 0 */ andre@0: andre@0: return poll; andre@0: andre@0: out_of_memory: andre@0: if (NULL != polldesc) PR_DELETE(polldesc); andre@0: return NULL; andre@0: } /* _pr_setfd */ andre@0: andre@0: #endif /* !defined(NEED_SELECT) */ andre@0: andre@0: PR_IMPLEMENT(PRInt32) PR_Select( andre@0: PRInt32 unused, PR_fd_set *pr_rd, PR_fd_set *pr_wr, andre@0: PR_fd_set *pr_ex, PRIntervalTime timeout) andre@0: { andre@0: andre@0: #if !defined(NEED_SELECT) andre@0: PRInt32 npds = 0; andre@0: /* andre@0: ** Find out how many fds are represented in the three lists. andre@0: ** Then allocate a polling descriptor for the logical union andre@0: ** (there can't be any overlapping) and call PR_Poll(). andre@0: */ andre@0: andre@0: PRPollDesc *copy, *poll; andre@0: andre@0: static PRBool warning = PR_TRUE; andre@0: if (warning) warning = _PR_Obsolete( "PR_Select()", "PR_Poll()"); andre@0: andre@0: /* try to get an initial guesss at how much space we need */ andre@0: npds = 0; andre@0: if ((NULL != pr_rd) && ((pr_rd->hsize + pr_rd->nsize - npds) > 0)) andre@0: npds = pr_rd->hsize + pr_rd->nsize; andre@0: if ((NULL != pr_wr) && ((pr_wr->hsize + pr_wr->nsize - npds) > 0)) andre@0: npds = pr_wr->hsize + pr_wr->nsize; andre@0: if ((NULL != pr_ex) && ((pr_ex->hsize + pr_ex->nsize - npds) > 0)) andre@0: npds = pr_ex->hsize + pr_ex->nsize; andre@0: andre@0: if (0 == npds) andre@0: { andre@0: PR_Sleep(timeout); andre@0: return 0; andre@0: } andre@0: andre@0: copy = poll = (PRPollDesc*)PR_Calloc(npds + PD_INCR, sizeof(PRPollDesc)); andre@0: if (NULL == poll) goto out_of_memory; andre@0: poll[npds + PD_INCR - 1].fd = (PRFileDesc*)-1; andre@0: andre@0: poll = _pr_setfd(pr_rd, PR_POLL_READ, poll); andre@0: if (NULL == poll) goto out_of_memory; andre@0: poll = _pr_setfd(pr_wr, PR_POLL_WRITE, poll); andre@0: if (NULL == poll) goto out_of_memory; andre@0: poll = _pr_setfd(pr_ex, PR_POLL_EXCEPT, poll); andre@0: if (NULL == poll) goto out_of_memory; andre@0: unused = 0; andre@0: while (NULL != poll[unused].fd && (PRFileDesc*)-1 != poll[unused].fd) andre@0: { andre@0: ++unused; andre@0: } andre@0: andre@0: PR_ASSERT(unused > 0); andre@0: npds = PR_Poll(poll, unused, timeout); andre@0: andre@0: if (npds > 0) andre@0: { andre@0: /* Copy the results back into the fd sets */ andre@0: if (NULL != pr_rd) pr_rd->nsize = pr_rd->hsize = 0; andre@0: if (NULL != pr_wr) pr_wr->nsize = pr_wr->hsize = 0; andre@0: if (NULL != pr_ex) pr_ex->nsize = pr_ex->hsize = 0; andre@0: for (copy = &poll[unused - 1]; copy >= poll; --copy) andre@0: { andre@0: if (copy->out_flags & PR_POLL_NVAL) andre@0: { andre@0: PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0); andre@0: npds = -1; andre@0: break; andre@0: } andre@0: if (copy->out_flags & PR_POLL_READ) andre@0: if (NULL != pr_rd) pr_rd->harray[pr_rd->hsize++] = copy->fd; andre@0: if (copy->out_flags & PR_POLL_WRITE) andre@0: if (NULL != pr_wr) pr_wr->harray[pr_wr->hsize++] = copy->fd; andre@0: if (copy->out_flags & PR_POLL_EXCEPT) andre@0: if (NULL != pr_ex) pr_ex->harray[pr_ex->hsize++] = copy->fd; andre@0: } andre@0: } andre@0: PR_DELETE(poll); andre@0: andre@0: return npds; andre@0: out_of_memory: andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return -1; andre@0: andre@0: #endif /* !defined(NEED_SELECT) */ andre@0: andre@0: }