if (m)
KASSERT(so->so_rcv.sb_mb == m); if (m) {
soreceive(so, paddr, uio, mp0, controlp, flagsp)	register struct socket *so;	struct mbuf **paddr;	struct uio *uio;	struct mbuf **mp0;	struct mbuf **controlp;	int *flagsp;{	register struct mbuf *m, **mp;	register int flags, len, error, s, offset;	struct protosw *pr = so->so_proto;	struct mbuf *nextrecord;	int moff, type = 0;	size_t orig_resid = uio->uio_resid;	int uio_error = 0;	int resid;	mp = mp0;	if (paddr)		*paddr = 0;	if (controlp)		*controlp = 0;	if (flagsp)		flags = *flagsp &~ MSG_EOR;	else		flags = 0;	if (so->so_state & SS_NBIO)		flags |= MSG_DONTWAIT;	if (flags & MSG_OOB) {		m = m_get(M_WAIT, MT_DATA);		error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,		    (struct mbuf *)(long)(flags & MSG_PEEK), NULL);		if (error)			goto bad;		do {			error = uiomove(mtod(m, caddr_t),			    (int) min(uio->uio_resid, m->m_len), uio);			m = m_free(m);		} while (uio->uio_resid && error == 0 && m);bad:		if (m)			m_freem(m);		return (error);	}	if (mp)		*mp = (struct mbuf *)0;	if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)		(*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL);restart:	if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)		return (error);	s = splsoftnet();	m = so->so_rcv.sb_mb;	/*	 * If we have less data than requested, block awaiting more	 * (subject to any timeout) if:	 *   1. the current count is less than the low water mark,	 *   2. MSG_WAITALL is set, and it is possible to do the entire	 *	receive operation at once if we block (resid <= hiwat), or	 *   3. MSG_DONTWAIT is not set.	 * If MSG_WAITALL is set but resid is larger than the receive buffer,	 * we have to do the receive in sections, and thus risk returning	 * a short count if a timeout or signal occurs after we start.	 */	if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&	    so->so_rcv.sb_cc < uio->uio_resid) &&	    (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||	    ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&	    m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {#ifdef DIAGNOSTIC		if (m == 0 && so->so_rcv.sb_cc)			panic("receive 1");#endif		if (so->so_error) {			if (m)				goto dontblock;			error = so->so_error;			if ((flags & MSG_PEEK) == 0)				so->so_error = 0;			goto release;		}		if (so->so_state & SS_CANTRCVMORE) {			if (m)				goto dontblock;			else				goto release;		}		for (; m; m = m->m_next)			if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {				m = so->so_rcv.sb_mb;				goto dontblock;			}		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&		    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {			error = ENOTCONN;			goto release;		}		if (uio->uio_resid == 0 && controlp == NULL)			goto release;		if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {			error = EWOULDBLOCK;			goto release;		}		sbunlock(&so->so_rcv);		error = sbwait(&so->so_rcv);		splx(s);		if (error)			return (error);		goto restart;	}dontblock:#ifdef notyet /* XXXX */	if (uio->uio_procp)		uio->uio_procp->p_stats->p_ru.ru_msgrcv++;#endif	nextrecord = m->m_nextpkt;	if (pr->pr_flags & PR_ADDR) {#ifdef DIAGNOSTIC		if (m->m_type != MT_SONAME)			panic("receive 1a");#endif		orig_resid = 0;		if (flags & MSG_PEEK) {			if (paddr)				*paddr = m_copy(m, 0, m->m_len);			m = m->m_next;		} else {			sbfree(&so->so_rcv, m);			if (paddr) {				*paddr = m;				so->so_rcv.sb_mb = m->m_next;				m->m_next = 0;				m = so->so_rcv.sb_mb;			} else {				MFREE(m, so->so_rcv.sb_mb);				m = so->so_rcv.sb_mb;			}		}	}	while (m && m->m_type == MT_CONTROL && error == 0) {		if (flags & MSG_PEEK) {			if (controlp)				*controlp = m_copy(m, 0, m->m_len);			m = m->m_next;		} else {			sbfree(&so->so_rcv, m);			if (controlp) {				if (pr->pr_domain->dom_externalize &&				    mtod(m, struct cmsghdr *)->cmsg_type ==				    SCM_RIGHTS)				   error = (*pr->pr_domain->dom_externalize)(m);				*controlp = m;				so->so_rcv.sb_mb = m->m_next;				m->m_next = 0;				m = so->so_rcv.sb_mb;			} else {				MFREE(m, so->so_rcv.sb_mb);				m = so->so_rcv.sb_mb;			}		}		if (controlp) {			orig_resid = 0;			controlp = &(*controlp)->m_next;		}	}	if (m) {		if ((flags & MSG_PEEK) == 0)			m->m_nextpkt = nextrecord;		type = m->m_type;		if (type == MT_OOBDATA)			flags |= MSG_OOB;		if (m->m_flags & M_BCAST)			flags |= MSG_BCAST;		if (m->m_flags & M_MCAST)			flags |= MSG_MCAST;	}	moff = 0;	offset = 0;	while (m && uio->uio_resid > 0 && error == 0) {		if (m->m_type == MT_OOBDATA) {			if (type != MT_OOBDATA)				break;		} else if (type == MT_OOBDATA)			break;#ifdef DIAGNOSTIC		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)			panic("receive 3");#endif		so->so_state &= ~SS_RCVATMARK;		len = uio->uio_resid;		if (so->so_oobmark && len > so->so_oobmark - offset)			len = so->so_oobmark - offset;		if (len > m->m_len - moff)			len = m->m_len - moff;		/*		 * If mp is set, just pass back the mbufs.		 * Otherwise copy them out via the uio, then free.		 * Sockbuf must be consistent here (points to current mbuf,		 * it points to next record) when we drop priority;		 * we must note any additions to the sockbuf when we		 * block interrupts again.		 */		if (mp == 0 && uio_error == 0) {			resid = uio->uio_resid;			splx(s);			uio_error =				uiomove(mtod(m, caddr_t) + moff, (int)len,					uio);			s = splsoftnet();			if (uio_error)				uio->uio_resid = resid - len;		} else			uio->uio_resid -= len;		if (len == m->m_len - moff) {			if (m->m_flags & M_EOR)				flags |= MSG_EOR;			if (flags & MSG_PEEK) {				m = m->m_next;				moff = 0;			} else {				nextrecord = m->m_nextpkt;				sbfree(&so->so_rcv, m);				if (mp) {					*mp = m;					mp = &m->m_next;					so->so_rcv.sb_mb = m = m->m_next;					*mp = (struct mbuf *)0;				} else {					MFREE(m, so->so_rcv.sb_mb);					m = so->so_rcv.sb_mb;				}				if (m)					m->m_nextpkt = nextrecord;			}		} else {			if (flags & MSG_PEEK)				moff += len;			else {				if (mp)					*mp = m_copym(m, 0, len, M_WAIT);				m->m_data += len;				m->m_len -= len;				so->so_rcv.sb_cc -= len;			}		}		if (so->so_oobmark) {			if ((flags & MSG_PEEK) == 0) {				so->so_oobmark -= len;				if (so->so_oobmark == 0) {					so->so_state |= SS_RCVATMARK;					break;				}			} else {				offset += len;				if (offset == so->so_oobmark)					break;			}		}		if (flags & MSG_EOR)			break;		/*		 * If the MSG_WAITALL flag is set (for non-atomic socket),		 * we must not quit until "uio->uio_resid == 0" or an error		 * termination.  If a signal/timeout occurs, return		 * with a short count but without error.		 * Keep sockbuf locked against other readers.		 */		while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&		    !sosendallatonce(so) && !nextrecord) {			if (so->so_error || so->so_state & SS_CANTRCVMORE)				break;			error = sbwait(&so->so_rcv);			if (error) {				sbunlock(&so->so_rcv);				splx(s);				return (0);			}			if ((m = so->so_rcv.sb_mb) != NULL)				nextrecord = m->m_nextpkt;		}	}	if (m && pr->pr_flags & PR_ATOMIC) {		flags |= MSG_TRUNC;		if ((flags & MSG_PEEK) == 0)			(void) sbdroprecord(&so->so_rcv);	}	if ((flags & MSG_PEEK) == 0) {		if (m == 0)			so->so_rcv.sb_mb = nextrecord;		if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)			(*pr->pr_usrreq)(so, PRU_RCVD, NULL,					 (struct mbuf *)(long)flags, NULL);	}	if (orig_resid == uio->uio_resid && orig_resid &&	    (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {		sbunlock(&so->so_rcv);		splx(s);		goto restart;	}	if (uio_error)		error = uio_error;	if (flagsp)		*flagsp |= flags;release:	sbunlock(&so->so_rcv);	splx(s);	return (error);}