xref: /csrg-svn/sys/kern/uipc_socket2.c (revision 16994)
1*16994Skarels /*	uipc_socket2.c	6.3	84/08/21	*/
24903Swnj 
34903Swnj #include "../h/param.h"
44903Swnj #include "../h/systm.h"
54903Swnj #include "../h/dir.h"
64903Swnj #include "../h/user.h"
74903Swnj #include "../h/proc.h"
84903Swnj #include "../h/file.h"
94903Swnj #include "../h/inode.h"
104903Swnj #include "../h/buf.h"
114903Swnj #include "../h/mbuf.h"
124903Swnj #include "../h/protosw.h"
134903Swnj #include "../h/socket.h"
144903Swnj #include "../h/socketvar.h"
154903Swnj 
164903Swnj /*
174903Swnj  * Primitive routines for operating on sockets and socket buffers
184903Swnj  */
194903Swnj 
204903Swnj /*
214903Swnj  * Procedures to manipulate state flags of socket
227509Sroot  * and do appropriate wakeups.  Normal sequence from the
237509Sroot  * active (originating) side is that soisconnecting() is
247509Sroot  * called during processing of connect() call,
255169Swnj  * resulting in an eventual call to soisconnected() if/when the
265169Swnj  * connection is established.  When the connection is torn down
275169Swnj  * soisdisconnecting() is called during processing of disconnect() call,
285169Swnj  * and soisdisconnected() is called when the connection to the peer
295169Swnj  * is totally severed.  The semantics of these routines are such that
305169Swnj  * connectionless protocols can call soisconnected() and soisdisconnected()
315169Swnj  * only, bypassing the in-progress calls when setting up a ``connection''
325169Swnj  * takes no time.
335169Swnj  *
3412758Ssam  * From the passive side, a socket is created with
3512758Ssam  * two queues of sockets: so_q0 for connections in progress
367509Sroot  * and so_q for connections already made and awaiting user acceptance.
377509Sroot  * As a protocol is preparing incoming connections, it creates a socket
387509Sroot  * structure queued on so_q0 by calling sonewconn().  When the connection
397509Sroot  * is established, soisconnected() is called, and transfers the
407509Sroot  * socket structure to so_q, making it available to accept().
417509Sroot  *
4212758Ssam  * If a socket is closed with sockets on either
437509Sroot  * so_q0 or so_q, these sockets are dropped.
447509Sroot  *
4512758Ssam  * If higher level protocols are implemented in
465169Swnj  * the kernel, the wakeups done here will sometimes
4712758Ssam  * cause software-interrupt process scheduling.
484903Swnj  */
495169Swnj 
504903Swnj soisconnecting(so)
5112758Ssam 	register struct socket *so;
524903Swnj {
534903Swnj 
544903Swnj 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
554903Swnj 	so->so_state |= SS_ISCONNECTING;
564903Swnj 	wakeup((caddr_t)&so->so_timeo);
574903Swnj }
584903Swnj 
594903Swnj soisconnected(so)
6012758Ssam 	register struct socket *so;
614903Swnj {
627509Sroot 	register struct socket *head = so->so_head;
634903Swnj 
647509Sroot 	if (head) {
657509Sroot 		if (soqremque(so, 0) == 0)
667509Sroot 			panic("soisconnected");
677509Sroot 		soqinsque(head, so, 1);
6812758Ssam 		sorwakeup(head);
697509Sroot 		wakeup((caddr_t)&head->so_timeo);
707509Sroot 	}
714903Swnj 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
724903Swnj 	so->so_state |= SS_ISCONNECTED;
734903Swnj 	wakeup((caddr_t)&so->so_timeo);
745578Swnj 	sorwakeup(so);
755578Swnj 	sowwakeup(so);
764903Swnj }
774903Swnj 
784903Swnj soisdisconnecting(so)
7912758Ssam 	register struct socket *so;
804903Swnj {
814903Swnj 
825248Sroot 	so->so_state &= ~SS_ISCONNECTING;
834903Swnj 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
844903Swnj 	wakeup((caddr_t)&so->so_timeo);
855170Swnj 	sowwakeup(so);
865169Swnj 	sorwakeup(so);
874903Swnj }
884903Swnj 
894903Swnj soisdisconnected(so)
9012758Ssam 	register struct socket *so;
914903Swnj {
924903Swnj 
934903Swnj 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
944903Swnj 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
954903Swnj 	wakeup((caddr_t)&so->so_timeo);
964903Swnj 	sowwakeup(so);
974903Swnj 	sorwakeup(so);
984903Swnj }
994903Swnj 
1005169Swnj /*
1017509Sroot  * When an attempt at a new connection is noted on a socket
1027509Sroot  * which accepts connections, sonewconn is called.  If the
1037509Sroot  * connection is possible (subject to space constraints, etc.)
1047509Sroot  * then we allocate a new structure, propoerly linked into the
1057509Sroot  * data structure of the original socket, and return this.
1067509Sroot  */
1077509Sroot struct socket *
1087509Sroot sonewconn(head)
1097509Sroot 	register struct socket *head;
1107509Sroot {
1117509Sroot 	register struct socket *so;
11212758Ssam 	register struct mbuf *m;
1137509Sroot 
1147509Sroot 	if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
1157509Sroot 		goto bad;
1169636Ssam 	m = m_getclr(M_DONTWAIT, MT_SOCKET);
11710138Ssam 	if (m == NULL)
1187509Sroot 		goto bad;
1197509Sroot 	so = mtod(m, struct socket *);
1207509Sroot 	so->so_type = head->so_type;
1217509Sroot 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
1227509Sroot 	so->so_linger = head->so_linger;
12310204Ssam 	so->so_state = head->so_state | SS_NOFDREF;
1247509Sroot 	so->so_proto = head->so_proto;
1257509Sroot 	so->so_timeo = head->so_timeo;
1267509Sroot 	so->so_pgrp = head->so_pgrp;
1277509Sroot 	soqinsque(head, so, 0);
12812758Ssam 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
12912758Ssam 	    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) {
1307509Sroot 		(void) soqremque(so, 0);
1318818Sroot 		(void) m_free(m);
1327509Sroot 		goto bad;
1337509Sroot 	}
1347509Sroot 	return (so);
1357509Sroot bad:
1367509Sroot 	return ((struct socket *)0);
1377509Sroot }
1387509Sroot 
1397509Sroot soqinsque(head, so, q)
1407509Sroot 	register struct socket *head, *so;
1417509Sroot 	int q;
1427509Sroot {
1437509Sroot 
1447509Sroot 	so->so_head = head;
1457509Sroot 	if (q == 0) {
1467509Sroot 		head->so_q0len++;
1477509Sroot 		so->so_q0 = head->so_q0;
1487509Sroot 		head->so_q0 = so;
1497509Sroot 	} else {
1507509Sroot 		head->so_qlen++;
1517509Sroot 		so->so_q = head->so_q;
1527509Sroot 		head->so_q = so;
1537509Sroot 	}
1547509Sroot }
1557509Sroot 
1567509Sroot soqremque(so, q)
1577509Sroot 	register struct socket *so;
1587509Sroot 	int q;
1597509Sroot {
1607509Sroot 	register struct socket *head, *prev, *next;
1617509Sroot 
1627509Sroot 	head = so->so_head;
1637509Sroot 	prev = head;
1647509Sroot 	for (;;) {
1657509Sroot 		next = q ? prev->so_q : prev->so_q0;
1667509Sroot 		if (next == so)
1677509Sroot 			break;
1687509Sroot 		if (next == head)
1697509Sroot 			return (0);
1707509Sroot 		prev = next;
1717509Sroot 	}
1727509Sroot 	if (q == 0) {
1737509Sroot 		prev->so_q0 = next->so_q0;
1747509Sroot 		head->so_q0len--;
1757509Sroot 	} else {
1767509Sroot 		prev->so_q = next->so_q;
1777509Sroot 		head->so_qlen--;
1787509Sroot 	}
1797509Sroot 	next->so_q0 = next->so_q = 0;
1807509Sroot 	next->so_head = 0;
1817509Sroot 	return (1);
1827509Sroot }
1837509Sroot 
1847509Sroot /*
1855169Swnj  * Socantsendmore indicates that no more data will be sent on the
1865169Swnj  * socket; it would normally be applied to a socket when the user
1875169Swnj  * informs the system that no more data is to be sent, by the protocol
1885169Swnj  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
1895169Swnj  * will be received, and will normally be applied to the socket by a
1905169Swnj  * protocol when it detects that the peer will send no more data.
1915169Swnj  * Data queued for reading in the socket may yet be read.
1925169Swnj  */
1935169Swnj 
1944917Swnj socantsendmore(so)
1954917Swnj 	struct socket *so;
1964917Swnj {
1974917Swnj 
1984917Swnj 	so->so_state |= SS_CANTSENDMORE;
1994917Swnj 	sowwakeup(so);
2004917Swnj }
2014917Swnj 
2024917Swnj socantrcvmore(so)
2034917Swnj 	struct socket *so;
2044917Swnj {
2054917Swnj 
2064917Swnj 	so->so_state |= SS_CANTRCVMORE;
2074917Swnj 	sorwakeup(so);
2084917Swnj }
2094917Swnj 
2104903Swnj /*
2115169Swnj  * Socket select/wakeup routines.
2124903Swnj  */
2135169Swnj 
2145169Swnj /*
2154903Swnj  * Queue a process for a select on a socket buffer.
2164903Swnj  */
2174903Swnj sbselqueue(sb)
2184903Swnj 	struct sockbuf *sb;
2194903Swnj {
2204903Swnj 	register struct proc *p;
2214903Swnj 
2224917Swnj 	if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait)
2234903Swnj 		sb->sb_flags |= SB_COLL;
2244903Swnj 	else
2254903Swnj 		sb->sb_sel = u.u_procp;
2264903Swnj }
2274903Swnj 
2284903Swnj /*
2294917Swnj  * Wait for data to arrive at/drain from a socket buffer.
2304917Swnj  */
2314917Swnj sbwait(sb)
2324917Swnj 	struct sockbuf *sb;
2334917Swnj {
2344917Swnj 
2354917Swnj 	sb->sb_flags |= SB_WAIT;
2364917Swnj 	sleep((caddr_t)&sb->sb_cc, PZERO+1);
2374917Swnj }
2384917Swnj 
2394917Swnj /*
2404903Swnj  * Wakeup processes waiting on a socket buffer.
2414903Swnj  */
2424903Swnj sbwakeup(sb)
24312758Ssam 	register struct sockbuf *sb;
2444903Swnj {
2454903Swnj 
2464903Swnj 	if (sb->sb_sel) {
2474903Swnj 		selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL);
2484903Swnj 		sb->sb_sel = 0;
2494903Swnj 		sb->sb_flags &= ~SB_COLL;
2504903Swnj 	}
2514903Swnj 	if (sb->sb_flags & SB_WAIT) {
2524903Swnj 		sb->sb_flags &= ~SB_WAIT;
2535013Swnj 		wakeup((caddr_t)&sb->sb_cc);
2544903Swnj 	}
2554903Swnj }
2564903Swnj 
2574903Swnj /*
25815829Scooper  * Wakeup socket readers and writers.
25915829Scooper  * Do asynchronous notification via SIGIO
26015829Scooper  * if the socket has the SS_ASYNC flag set.
26115829Scooper  */
26215829Scooper sowakeup(so, sb)
26315829Scooper 	register struct socket *so;
26415829Scooper 	struct sockbuf *sb;
26515829Scooper {
26615829Scooper 	register struct proc *p;
26715829Scooper 
26815829Scooper 	sbwakeup(sb);
26915829Scooper 	if (so->so_state & SS_ASYNC) {
27015829Scooper 		if (so->so_pgrp == 0)
27115829Scooper 			return;
27215829Scooper 		else if (so->so_pgrp > 0)
27315829Scooper 			gsignal(so->so_pgrp, SIGIO);
27415829Scooper 		else if ((p = pfind(-so->so_pgrp)) != 0)
27515829Scooper 			psignal(p, SIGIO);
27615829Scooper 	}
27715829Scooper }
27815829Scooper 
27915829Scooper /*
2805169Swnj  * Socket buffer (struct sockbuf) utility routines.
2815169Swnj  *
2825169Swnj  * Each socket contains two socket buffers: one for sending data and
2835169Swnj  * one for receiving data.  Each buffer contains a queue of mbufs,
2845169Swnj  * information about the number of mbufs and amount of data in the
2855169Swnj  * queue, and other fields allowing select() statements and notification
2865169Swnj  * on data availability to be implemented.
2875169Swnj  *
288*16994Skarels  * Data stored in a socket buffer is maintained as a list of records.
289*16994Skarels  * Each record is a list of mbufs chained together with the m_next
290*16994Skarels  * field.  Records are chained together with the m_act field. The upper
291*16994Skarels  * level routine soreceive() expects the following conventions to be
292*16994Skarels  * observed when placing information in the receive buffer:
293*16994Skarels  *
294*16994Skarels  * 1. If the protocol requires each message be preceded by the sender's
295*16994Skarels  *    name, then a record containing that name must be present before
296*16994Skarels  *    any associated data (mbuf's must be of type MT_SONAME).
297*16994Skarels  * 2. If the protocol supports the exchange of ``access rights'' (really
298*16994Skarels  *    just additional data associated with the message), and there are
299*16994Skarels  *    ``rights'' to be received, then a record containing this data
300*16994Skarels  *    should be present (mbuf's must be of type MT_RIGHTS).
301*16994Skarels  * 3. If a name or rights record exists, then it must be followed by
302*16994Skarels  *    a data record, perhaps of zero length.
303*16994Skarels  *
3045169Swnj  * Before using a new socket structure it is first necessary to reserve
305*16994Skarels  * buffer space to the socket, by calling sbreserve().  This commits
3065169Swnj  * some of the available buffer space in the system buffer pool for the
307*16994Skarels  * socket.  The space should be released by calling sbrelease() when the
3085169Swnj  * socket is destroyed.
3095169Swnj  *
310*16994Skarels  * The routines sbappend() or sbappendrecord() are normally called to
311*16994Skarels  * append new mbufs to a socket buffer, after checking that adequate
312*16994Skarels  * space is available, comparing the function sbspace() with the amount
313*16994Skarels  * of data to be added.  sbappendrecord() differs from sbappend() in
314*16994Skarels  * that data supplied is treated as the beginning of a new record.
3155169Swnj  * Data is normally removed from a socket buffer in a protocol by
3165169Swnj  * first calling m_copy on the socket buffer mbuf chain and sending this
3175169Swnj  * to a peer, and then removing the data from the socket buffer with
318*16994Skarels  * sbdrop() or sbdroprecord() when the data is acknowledged by the peer
319*16994Skarels  * (or immediately in the case of unreliable protocols.)
3205169Swnj  *
321*16994Skarels  * To place a sender's name, optionally, access rights, and data in a
322*16994Skarels  * socket buffer sbappendaddr() should be used.  To place access rights
323*16994Skarels  * and data in a socket buffer sbappendrights() should be used.  Note
324*16994Skarels  * that unlike sbappend() and sbappendrecord(), these routines check
3255169Swnj  * for the caller that there will be enough space to store the data.
326*16994Skarels  * Each fails if there is not enough space, or if it cannot find mbufs
327*16994Skarels  * to store additional information in.
3285169Swnj  */
3295169Swnj 
3309027Sroot soreserve(so, sndcc, rcvcc)
33112758Ssam 	register struct socket *so;
3329027Sroot 	int sndcc, rcvcc;
3339027Sroot {
3349027Sroot 
3359027Sroot 	if (sbreserve(&so->so_snd, sndcc) == 0)
3369027Sroot 		goto bad;
3379027Sroot 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
3389027Sroot 		goto bad2;
3399027Sroot 	return (0);
3409027Sroot bad2:
3419027Sroot 	sbrelease(&so->so_snd);
3429027Sroot bad:
3439027Sroot 	return (ENOBUFS);
3449027Sroot }
3459027Sroot 
3465169Swnj /*
3474903Swnj  * Allot mbufs to a sockbuf.
3484903Swnj  */
3494903Swnj sbreserve(sb, cc)
3504903Swnj 	struct sockbuf *sb;
3514903Swnj {
3524903Swnj 
3537181Swnj 	/* someday maybe this routine will fail... */
3544980Swnj 	sb->sb_hiwat = cc;
35512758Ssam 	/* * 2 implies names can be no more than 1 mbuf each */
35612758Ssam 	sb->sb_mbmax = cc<<1;
3574917Swnj 	return (1);
3584903Swnj }
3594903Swnj 
3604903Swnj /*
3614903Swnj  * Free mbufs held by a socket, and reserved mbuf space.
3624903Swnj  */
3634903Swnj sbrelease(sb)
3644903Swnj 	struct sockbuf *sb;
3654903Swnj {
3664903Swnj 
3674903Swnj 	sbflush(sb);
3684980Swnj 	sb->sb_hiwat = sb->sb_mbmax = 0;
3694903Swnj }
3704903Swnj 
3714903Swnj /*
372*16994Skarels  * Routines to add and remove
373*16994Skarels  * data from an mbuf queue.
3744903Swnj  */
3754903Swnj 
3764903Swnj /*
377*16994Skarels  * Append mbuf chain m to the last record in the
378*16994Skarels  * socket buffer sb.  The additional space associated
379*16994Skarels  * the mbuf chain is recorded in sb.  Empty mbufs are
380*16994Skarels  * discarded and mbufs are compacted where possible.
3814903Swnj  */
3824903Swnj sbappend(sb, m)
383*16994Skarels 	struct sockbuf *sb;
384*16994Skarels 	struct mbuf *m;
3854903Swnj {
3866092Sroot 	register struct mbuf *n;
3874903Swnj 
388*16994Skarels 	if (m == 0)
389*16994Skarels 		return;
390*16994Skarels 	if (n = sb->sb_mb) {
391*16994Skarels 		while (n->m_act)
392*16994Skarels 			n = n->m_act;
3936092Sroot 		while (n->m_next)
3946092Sroot 			n = n->m_next;
3954903Swnj 	}
396*16994Skarels 	sbcompress(sb, m, n);
3974903Swnj }
3984903Swnj 
3995169Swnj /*
400*16994Skarels  * As above, except the mbuf chain
401*16994Skarels  * begins a new record.
4025169Swnj  */
403*16994Skarels sbappendrecord(sb, m0)
404*16994Skarels 	register struct sockbuf *sb;
405*16994Skarels 	register struct mbuf *m0;
4064928Swnj {
4074928Swnj 	register struct mbuf *m;
4084928Swnj 
409*16994Skarels 	if (m0 == 0)
410*16994Skarels 		return;
411*16994Skarels 	if (m = sb->sb_mb)
412*16994Skarels 		while (m->m_act)
413*16994Skarels 			m = m->m_act;
414*16994Skarels 	/*
415*16994Skarels 	 * Put the first mbuf on the queue.
416*16994Skarels 	 * Note this permits zero length records.
417*16994Skarels 	 */
418*16994Skarels 	sballoc(sb, m0);
419*16994Skarels 	if (m)
420*16994Skarels 		m->m_act = m0;
421*16994Skarels 	else
422*16994Skarels 		sb->sb_mb = m0;
423*16994Skarels 	m = m0->m_next;
424*16994Skarels 	m0->m_next = 0;
425*16994Skarels 	sbcompress(sb, m, m0);
426*16994Skarels }
427*16994Skarels 
428*16994Skarels /*
429*16994Skarels  * Append address and data, and optionally, rights
430*16994Skarels  * to the receive queue of a socket.  Return 0 if
431*16994Skarels  * no space in sockbuf or insufficient mbufs.
432*16994Skarels  */
433*16994Skarels sbappendaddr(sb, asa, m0, rights0)		/* XXX */
434*16994Skarels 	register struct sockbuf *sb;
435*16994Skarels 	struct sockaddr *asa;
436*16994Skarels 	struct mbuf *rights0, *m0;
437*16994Skarels {
438*16994Skarels 	register struct mbuf *m, *n;
439*16994Skarels 	int space = sizeof (*asa);
440*16994Skarels 
4415042Swnj 	m = m0;
4425042Swnj 	if (m == 0)
4435042Swnj 		panic("sbappendaddr");
444*16994Skarels 	do {
445*16994Skarels 		space += m->m_len;
4465042Swnj 		m = m->m_next;
447*16994Skarels 	} while (m);
448*16994Skarels 	if (rights0)
449*16994Skarels 		space += rights0->m_len;
450*16994Skarels 	if (space > sbspace(sb))
4514928Swnj 		return (0);
4529636Ssam 	m = m_get(M_DONTWAIT, MT_SONAME);
453*16994Skarels 	if (m == 0)
4544928Swnj 		return (0);
45512758Ssam 	*mtod(m, struct sockaddr *) = *asa;
456*16994Skarels 	m->m_len = sizeof (*asa);
457*16994Skarels 	if (rights0) {
458*16994Skarels 		m->m_act = m_copy(rights0, 0, rights0->m_len);
459*16994Skarels 		if (m->m_act == 0) {
460*16994Skarels 			m_freem(m);
461*16994Skarels 			return (0);
462*16994Skarels 		}
463*16994Skarels 		sballoc(sb, m);
464*16994Skarels 		sballoc(sb, m->m_act);
46512758Ssam 	} else
466*16994Skarels 		sballoc(sb, m);
467*16994Skarels 	if (n = sb->sb_mb) {
468*16994Skarels 		while (n->m_act)
469*16994Skarels 			n = n->m_act;
470*16994Skarels 		n->m_act = m;
471*16994Skarels 	} else
472*16994Skarels 		sb->sb_mb = m;
473*16994Skarels 	if (m->m_act)
474*16994Skarels 		m = m->m_act;
475*16994Skarels 	sballoc(sb, m0);
476*16994Skarels 	m->m_act = m0;
477*16994Skarels 	m = m0->m_next;
478*16994Skarels 	m0->m_next = 0;
479*16994Skarels 	sbcompress(sb, m, m0);
480*16994Skarels 	return (1);
481*16994Skarels }
482*16994Skarels 
483*16994Skarels #ifdef notdef
484*16994Skarels sbappendrights(sb, rights, m0)
485*16994Skarels 	struct sockbuf *sb;
486*16994Skarels 	struct mbuf *rights, *m;
487*16994Skarels {
488*16994Skarels 	register struct mbuf *m, *n;
489*16994Skarels 	int space = 0;
490*16994Skarels 
491*16994Skarels 	m = m0;
492*16994Skarels 	if (m == 0 || rights == 0)
493*16994Skarels 		panic("sbappendrights");
494*16994Skarels 	do {
495*16994Skarels 		space += m->m_len;
496*16994Skarels 		m = m->m_next;
497*16994Skarels 	} while (m);
498*16994Skarels 	space += rights->m_len;
499*16994Skarels 	if (space > sbspace(sb))
50012758Ssam 		return (0);
501*16994Skarels 	m = m_copy(rights, 0, rights->m_len);
502*16994Skarels 	if (m == 0)
503*16994Skarels 		return (0);
504*16994Skarels 	sballoc(sb, m);
505*16994Skarels 	if (n = sb->sb_mb) {
506*16994Skarels 		while (n->m_act)
507*16994Skarels 			n = n->m_act;
508*16994Skarels 		n->m_act = m;
509*16994Skarels 	} else
510*16994Skarels 		n->m_act = m;
511*16994Skarels 	sballoc(sb, m0);
512*16994Skarels 	m->m_act = m0;
513*16994Skarels 	m = m0->m_next;
514*16994Skarels 	m0->m_next = 0;
515*16994Skarels 	sbcompress(sb, m, m0);
5164928Swnj 	return (1);
5174928Swnj }
518*16994Skarels #endif
5194928Swnj 
5204903Swnj /*
521*16994Skarels  * Compress mbuf chain m into the socket
522*16994Skarels  * buffer sb following mbuf n.  If n
523*16994Skarels  * is null, the buffer is presumed empty.
5244903Swnj  */
525*16994Skarels sbcompress(sb, m, n)
526*16994Skarels 	register struct sockbuf *sb;
527*16994Skarels 	register struct mbuf *m, *n;
528*16994Skarels {
529*16994Skarels 
530*16994Skarels 	while (m) {
531*16994Skarels 		if (m->m_len == 0) {
532*16994Skarels 			m = m_free(m);
533*16994Skarels 			continue;
534*16994Skarels 		}
535*16994Skarels 		if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF &&
536*16994Skarels 		    (n->m_off + n->m_len + m->m_len) <= MMAXOFF) {
537*16994Skarels 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
538*16994Skarels 			    (unsigned)m->m_len);
539*16994Skarels 			n->m_len += m->m_len;
540*16994Skarels 			sb->sb_cc += m->m_len;
541*16994Skarels 			m = m_free(m);
542*16994Skarels 			continue;
543*16994Skarels 		}
544*16994Skarels 		sballoc(sb, m);
545*16994Skarels 		if (n)
546*16994Skarels 			n->m_next = m;
547*16994Skarels 		else
548*16994Skarels 			sb->sb_mb = m;
549*16994Skarels 		n = m;
550*16994Skarels 		m = m->m_next;
551*16994Skarels 		n->m_next = 0;
552*16994Skarels 	}
553*16994Skarels }
554*16994Skarels 
555*16994Skarels /*
556*16994Skarels  * Free all mbufs in a sockbuf.
557*16994Skarels  * Check that all resources are reclaimed.
558*16994Skarels  */
5594903Swnj sbflush(sb)
56012758Ssam 	register struct sockbuf *sb;
5614903Swnj {
5624903Swnj 
5634903Swnj 	if (sb->sb_flags & SB_LOCK)
5644903Swnj 		panic("sbflush");
5655266Swnj 	if (sb->sb_cc)
5665266Swnj 		sbdrop(sb, sb->sb_cc);
5674903Swnj 	if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb)
5684903Swnj 		panic("sbflush 2");
5694903Swnj }
5704903Swnj 
5714903Swnj /*
572*16994Skarels  * Drop data from (the front of) a sockbuf.
5734903Swnj  */
574*16994Skarels struct mbuf *
5754903Swnj sbdrop(sb, len)
5764903Swnj 	register struct sockbuf *sb;
5774903Swnj 	register int len;
5784903Swnj {
579*16994Skarels 	register struct mbuf *m, *mn;
580*16994Skarels 	struct mbuf *next;
5814903Swnj 
582*16994Skarels 	next = (m = sb->sb_mb) ? m->m_act : 0;
5834903Swnj 	while (len > 0) {
584*16994Skarels 		if (m == 0) {
585*16994Skarels 			if (next == 0)
586*16994Skarels 				panic("sbdrop");
587*16994Skarels 			m = next;
588*16994Skarels 			next = m->m_act;
589*16994Skarels 			continue;
590*16994Skarels 		}
5915064Swnj 		if (m->m_len > len) {
5924903Swnj 			m->m_len -= len;
5934903Swnj 			m->m_off += len;
5944903Swnj 			sb->sb_cc -= len;
5954903Swnj 			break;
5964903Swnj 		}
5975064Swnj 		len -= m->m_len;
5985064Swnj 		sbfree(sb, m);
5995064Swnj 		MFREE(m, mn);
6005064Swnj 		m = mn;
6014903Swnj 	}
602*16994Skarels 	if (m) {
603*16994Skarels 		sb->sb_mb = m;
604*16994Skarels 		m->m_act = next;
605*16994Skarels 	} else
606*16994Skarels 		sb->sb_mb = next;
607*16994Skarels 	return (sb->sb_mb);
6084903Swnj }
609*16994Skarels 
610*16994Skarels /*
611*16994Skarels  * Drop a record off the front of a sockbuf
612*16994Skarels  * and move the next record to the front.
613*16994Skarels  */
614*16994Skarels struct mbuf *
615*16994Skarels sbdroprecord(sb)
616*16994Skarels 	register struct sockbuf *sb;
617*16994Skarels {
618*16994Skarels 	register struct mbuf *m, *mn;
619*16994Skarels 
620*16994Skarels 	m = sb->sb_mb;
621*16994Skarels 	if (m) {
622*16994Skarels 		sb->sb_mb = m->m_act;
623*16994Skarels 		do {
624*16994Skarels 			sbfree(sb, m);
625*16994Skarels 			MFREE(m, mn);
626*16994Skarels 		} while (m = mn);
627*16994Skarels 	}
628*16994Skarels 	return (sb->sb_mb);
629*16994Skarels }
630