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