⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prsocket.c

📁 Netscape NSPR库源码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//*  * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape Portable Runtime (NSPR). *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1998-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */#include "primpl.h"#include <string.h>/************************************************************************//* These two functions are only used in assertions. */#if defined(DEBUG)PRBool IsValidNetAddr(const PRNetAddr *addr){    if ((addr != NULL)#ifdef XP_UNIX	    && (addr->raw.family != PR_AF_LOCAL)#endif	    && (addr->raw.family != PR_AF_INET6)	    && (addr->raw.family != PR_AF_INET)) {        return PR_FALSE;    }    return PR_TRUE;}static PRBool IsValidNetAddrLen(const PRNetAddr *addr, PRInt32 addr_len){    /*     * The definition of the length of a Unix domain socket address     * is not uniform, so we don't check it.     */    if ((addr != NULL)#ifdef XP_UNIX            && (addr->raw.family != AF_UNIX)#endif            && (PR_NETADDR_SIZE(addr) != addr_len)) {#if defined(LINUX) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 1        /*         * In glibc 2.1, struct sockaddr_in6 is 24 bytes.  In glibc 2.2         * and in the 2.4 kernel, struct sockaddr_in6 has the scope_id         * field and is 28 bytes.  It is possible for socket functions         * to return an addr_len greater than sizeof(struct sockaddr_in6).         * We need to allow that.  (Bugzilla bug #77264)         */        if ((PR_AF_INET6 == addr->raw.family)                && (sizeof(addr->ipv6) == addr_len)) {            return PR_TRUE;        }#endif        /*         * The accept(), getsockname(), etc. calls on some platforms         * do not set the actual socket address length on return.         * In this case, we verifiy addr_len is still the value we         * passed in (i.e., sizeof(PRNetAddr)).         */#if defined(QNX)        if (sizeof(PRNetAddr) == addr_len) {            return PR_TRUE;        }#endif        return PR_FALSE;    }    return PR_TRUE;}#endif /* DEBUG */static PRInt32 PR_CALLBACK SocketWritev(PRFileDesc *fd, const PRIOVec *iov,PRInt32 iov_size, PRIntervalTime timeout){	PRThread *me = _PR_MD_CURRENT_THREAD();	int w = 0;	const PRIOVec *tmp_iov;#define LOCAL_MAXIOV    8	PRIOVec local_iov[LOCAL_MAXIOV];	PRIOVec *iov_copy = NULL;	int tmp_out;	int index, iov_cnt;	int count=0, sz = 0;    /* 'count' is the return value. */	if (_PR_PENDING_INTERRUPT(me)) {		me->flags &= ~_PR_INTERRUPT;		PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);		return -1;	}	if (_PR_IO_PENDING(me)) {		PR_SetError(PR_IO_PENDING_ERROR, 0);		return -1;	}    /*     * Assume the first writev will succeed.  Copy iov's only on     * failure.     */    tmp_iov = iov;    for (index = 0; index < iov_size; index++)        sz += iov[index].iov_len;	iov_cnt = iov_size;	while (sz > 0) {		w = _PR_MD_WRITEV(fd, tmp_iov, iov_cnt, timeout);		if (w < 0) {			count = -1;			break;		}		count += w;		if (fd->secret->nonblocking) {			break;		}		sz -= w;		if (sz > 0) {			/* find the next unwritten vector */			for ( index = 0, tmp_out = count;				tmp_out >= iov[index].iov_len;				tmp_out -= iov[index].iov_len, index++){;} /* nothing to execute */			if (tmp_iov == iov) {				/*				 * The first writev failed so we				 * must copy iov's around.				 * Avoid calloc/free if there				 * are few enough iov's.				 */				if (iov_size - index <= LOCAL_MAXIOV)					iov_copy = local_iov;				else if ((iov_copy = (PRIOVec *) PR_CALLOC((iov_size - index) *					sizeof *iov_copy)) == NULL) {					PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);					return -1;				}				tmp_iov = iov_copy;			}			PR_ASSERT(tmp_iov == iov_copy);			/* fill in the first partial read */			iov_copy[0].iov_base = &(((char *)iov[index].iov_base)[tmp_out]);			iov_copy[0].iov_len = iov[index].iov_len - tmp_out;			index++;			/* copy the remaining vectors */			for (iov_cnt=1; index<iov_size; iov_cnt++, index++) {				iov_copy[iov_cnt].iov_base = iov[index].iov_base;				iov_copy[iov_cnt].iov_len = iov[index].iov_len;			}		}	}	if (iov_copy != local_iov)		PR_DELETE(iov_copy);	return count;}/************************************************************************/PR_IMPLEMENT(PRFileDesc *) PR_ImportTCPSocket(PRInt32 osfd){PRFileDesc *fd;	if (!_pr_initialized) _PR_ImplicitInitialization();	fd = PR_AllocFileDesc(osfd, PR_GetTCPMethods());	if (fd != NULL) {		_PR_MD_MAKE_NONBLOCK(fd);		_PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE);	} else		_PR_MD_CLOSE_SOCKET(osfd);	return(fd);}PR_IMPLEMENT(PRFileDesc *) PR_ImportUDPSocket(PRInt32 osfd){PRFileDesc *fd;	if (!_pr_initialized) _PR_ImplicitInitialization();	fd = PR_AllocFileDesc(osfd, PR_GetUDPMethods());	if (fd != NULL) {		_PR_MD_MAKE_NONBLOCK(fd);		_PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE);	} else		_PR_MD_CLOSE_SOCKET(osfd);	return(fd);}static const PRIOMethods* PR_GetSocketPollFdMethods(void);PR_IMPLEMENT(PRFileDesc*) PR_CreateSocketPollFd(PRInt32 osfd){    PRFileDesc *fd;    if (!_pr_initialized) _PR_ImplicitInitialization();    fd = _PR_Getfd();    if (fd == NULL) PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);    else    {        fd->secret->md.osfd = osfd;        fd->secret->inheritable = _PR_TRI_FALSE;    	fd->secret->state = _PR_FILEDESC_OPEN;        fd->methods = PR_GetSocketPollFdMethods();    }    return fd;}  /* PR_CreateSocketPollFD */PR_IMPLEMENT(PRStatus) PR_DestroySocketPollFd(PRFileDesc *fd){    if (NULL == fd)    {        PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0);        return PR_FAILURE;    }    fd->secret->state = _PR_FILEDESC_CLOSED;    _PR_Putfd(fd);    return PR_SUCCESS;}  /* PR_DestroySocketPollFd */static PRStatus PR_CALLBACK SocketConnect(    PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout){	PRInt32 rv;    /* Return value of _PR_MD_CONNECT */    const PRNetAddr *addrp = addr;#if defined(_PR_INET6)	PRNetAddr addrCopy;#endif	PRThread *me = _PR_MD_CURRENT_THREAD();	if (_PR_PENDING_INTERRUPT(me)) {		me->flags &= ~_PR_INTERRUPT;		PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);		return PR_FAILURE;	}#if defined(_PR_INET6)	if (addr->raw.family == PR_AF_INET6) {		addrCopy = *addr;		addrCopy.raw.family = AF_INET6;		addrp = &addrCopy;	}#endif	rv = _PR_MD_CONNECT(fd, addrp, PR_NETADDR_SIZE(addr), timeout);	PR_LOG(_pr_io_lm, PR_LOG_MAX, ("connect -> %d", rv));	if (rv == 0)		return PR_SUCCESS;	else		return PR_FAILURE;}static PRStatus PR_CALLBACK SocketConnectContinue(    PRFileDesc *fd, PRInt16 out_flags){    PRInt32 osfd;    int err;    if (out_flags & PR_POLL_NVAL) {        PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0);        return PR_FAILURE;    }    if ((out_flags & (PR_POLL_WRITE | PR_POLL_EXCEPT | PR_POLL_ERR)) == 0) {        PR_ASSERT(out_flags == 0);        PR_SetError(PR_IN_PROGRESS_ERROR, 0);        return PR_FAILURE;    }    osfd = fd->secret->md.osfd;#if defined(XP_UNIX)    err = _MD_unix_get_nonblocking_connect_error(osfd);    if (err != 0) {        _PR_MD_MAP_CONNECT_ERROR(err);        return PR_FAILURE;    }    return PR_SUCCESS;#elif defined(WIN32) || defined(WIN16)#if defined(WIN32)    /*     * The sleep circumvents a bug in Win32 WinSock.     * See Microsoft Knowledge Base article ID: Q165989.     */    Sleep(0);#endif /* WIN32 */    if (out_flags & PR_POLL_EXCEPT) {        int len = sizeof(err);        if (getsockopt(osfd, (int)SOL_SOCKET, SO_ERROR, (char *) &err, &len)                == SOCKET_ERROR) {            _PR_MD_MAP_GETSOCKOPT_ERROR(WSAGetLastError());            return PR_FAILURE;        }        if (err != 0) {            _PR_MD_MAP_CONNECT_ERROR(err);        } else {            PR_SetError(PR_UNKNOWN_ERROR, 0);        }        return PR_FAILURE;    }    PR_ASSERT(out_flags & PR_POLL_WRITE);    return PR_SUCCESS;#elif defined(XP_OS2)    err = _MD_os2_get_nonblocking_connect_error(osfd);    if (err != 0) {        _PR_MD_MAP_CONNECT_ERROR(err);        return PR_FAILURE;    }    return PR_SUCCESS;#elif defined(XP_MAC)    err = _MD_mac_get_nonblocking_connect_error(fd);    if (err == -1)        return PR_FAILURE;	else     		return PR_SUCCESS;#elif defined(XP_BEOS)#ifdef BONE_VERSION  /* bug 122364 */    /* temporary workaround until getsockopt(SO_ERROR) works in BONE */    if (out_flags & PR_POLL_EXCEPT) {        PR_SetError(PR_CONNECT_REFUSED_ERROR, 0);        return PR_FAILURE;    }    PR_ASSERT(out_flags & PR_POLL_WRITE);    return PR_SUCCESS;#else    err = _MD_beos_get_nonblocking_connect_error(fd);    if( err != 0 ) {        _PR_MD_MAP_CONNECT_ERROR(err);        return PR_FAILURE;    }    else        return PR_SUCCESS;#endif /* BONE_VERSION */#else    PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);    return PR_FAILURE;#endif}PR_IMPLEMENT(PRStatus) PR_GetConnectStatus(const PRPollDesc *pd){    /* Find the NSPR layer and invoke its connectcontinue method */    PRFileDesc *bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);    if (NULL == bottom) {        PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);        return PR_FAILURE;    }    return SocketConnectContinue(bottom, pd->out_flags);}static PRFileDesc* PR_CALLBACK SocketAccept(PRFileDesc *fd, PRNetAddr *addr,PRIntervalTime timeout){	PRInt32 osfd;	PRFileDesc *fd2;	PRUint32 al;	PRThread *me = _PR_MD_CURRENT_THREAD();#ifdef WINNT	PRNetAddr addrCopy;#endif	if (_PR_PENDING_INTERRUPT(me)) {		me->flags &= ~_PR_INTERRUPT;		PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);		return 0;	}	if (_PR_IO_PENDING(me)) {		PR_SetError(PR_IO_PENDING_ERROR, 0);		return 0;	}#ifdef WINNT	if (addr == NULL) {		addr = &addrCopy;	}#endif	al = sizeof(PRNetAddr);	osfd = _PR_MD_ACCEPT(fd, addr, &al, timeout);	if (osfd == -1)		return 0;	fd2 = PR_AllocFileDesc(osfd, PR_GetTCPMethods());	if (!fd2) {		_PR_MD_CLOSE_SOCKET(osfd);		return NULL;	}	fd2->secret->nonblocking = fd->secret->nonblocking;	fd2->secret->inheritable = fd->secret->inheritable;#ifdef WINNT	if (!fd2->secret->nonblocking && fd2->secret->inheritable != _PR_TRI_TRUE) {		/*		 * The new socket has been associated with an I/O		 * completion port.  There is no going back.		 */		fd2->secret->md.io_model_committed = PR_TRUE;	}	PR_ASSERT(al == PR_NETADDR_SIZE(addr));	fd2->secret->md.accepted_socket = PR_TRUE;	memcpy(&fd2->secret->md.peer_addr, addr, al);#endif	/*	 * On some platforms, the new socket created by accept()	 * inherits the nonblocking (or overlapped io) attribute	 * of the listening socket.  As an optimization, these	 * platforms can skip the following _PR_MD_MAKE_NONBLOCK	 * call.	 * 	 * On Mac, we MUST make this call, because _PR_MD_MAKE_NONBLOCK	 * (which maps to _MD_makenonblock, see macsockotpt.c)	 * installs the async notifier routine needed to make blocking	 * I/O work properly.	 */#if !defined(SOLARIS) && !defined(IRIX) && !defined(WINNT)	_PR_MD_MAKE_NONBLOCK(fd2);#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -