Deleted Added
full compact
uipc_sockbuf.c (8876) uipc_sockbuf.c (12041)
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
34 * $Id: uipc_socket2.c,v 1.4 1994/10/02 17:35:33 phk Exp $
34 * $Id: uipc_socket2.c,v 1.5 1995/05/30 08:06:22 rgrimes Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
39#include <sys/proc.h>
40#include <sys/file.h>
41#include <sys/buf.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/protosw.h>
45#include <sys/stat.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/signalvar.h>
40#include <sys/proc.h>
41#include <sys/file.h>
42#include <sys/buf.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/protosw.h>
46#include <sys/stat.h>
47#include <sys/socket.h>
48#include <sys/socketvar.h>
49#include <sys/signalvar.h>
50#include <sys/sysctl.h>
49
50/*
51 * Primitive routines for operating on sockets and socket buffers
52 */
53
54/* strings for sleep message: */
55char netio[] = "netio";
56char netcon[] = "netcon";
57char netcls[] = "netcls";
58
51
52/*
53 * Primitive routines for operating on sockets and socket buffers
54 */
55
56/* strings for sleep message: */
57char netio[] = "netio";
58char netcon[] = "netcon";
59char netcls[] = "netcls";
60
59u_long sb_max = SB_MAX; /* patchable */
61u_long sb_max = SB_MAX; /* XXX should be static */
62SYSCTL_INT(_kern, KERN_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, &sb_max, 0, "")
60
61/*
62 * Procedures to manipulate state flags of socket
63 * and do appropriate wakeups. Normal sequence from the
64 * active (originating) side is that soisconnecting() is
65 * called during processing of connect() call,
66 * resulting in an eventual call to soisconnected() if/when the
67 * connection is established. When the connection is torn down
68 * soisdisconnecting() is called during processing of disconnect() call,
69 * and soisdisconnected() is called when the connection to the peer
70 * is totally severed. The semantics of these routines are such that
71 * connectionless protocols can call soisconnected() and soisdisconnected()
72 * only, bypassing the in-progress calls when setting up a ``connection''
73 * takes no time.
74 *
75 * From the passive side, a socket is created with
76 * two queues of sockets: so_q0 for connections in progress
77 * and so_q for connections already made and awaiting user acceptance.
78 * As a protocol is preparing incoming connections, it creates a socket
79 * structure queued on so_q0 by calling sonewconn(). When the connection
80 * is established, soisconnected() is called, and transfers the
81 * socket structure to so_q, making it available to accept().
82 *
83 * If a socket is closed with sockets on either
84 * so_q0 or so_q, these sockets are dropped.
85 *
86 * If higher level protocols are implemented in
87 * the kernel, the wakeups done here will sometimes
88 * cause software-interrupt process scheduling.
89 */
90
91void
92soisconnecting(so)
93 register struct socket *so;
94{
95
96 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
97 so->so_state |= SS_ISCONNECTING;
98}
99
100void
101soisconnected(so)
102 register struct socket *so;
103{
104 register struct socket *head = so->so_head;
105
106 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
107 so->so_state |= SS_ISCONNECTED;
108 if (head && soqremque(so, 0)) {
109 soqinsque(head, so, 1);
110 sorwakeup(head);
111 wakeup((caddr_t)&head->so_timeo);
112 } else {
113 wakeup((caddr_t)&so->so_timeo);
114 sorwakeup(so);
115 sowwakeup(so);
116 }
117}
118
119void
120soisdisconnecting(so)
121 register struct socket *so;
122{
123
124 so->so_state &= ~SS_ISCONNECTING;
125 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
126 wakeup((caddr_t)&so->so_timeo);
127 sowwakeup(so);
128 sorwakeup(so);
129}
130
131void
132soisdisconnected(so)
133 register struct socket *so;
134{
135
136 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
137 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
138 wakeup((caddr_t)&so->so_timeo);
139 sowwakeup(so);
140 sorwakeup(so);
141}
142
143/*
144 * When an attempt at a new connection is noted on a socket
145 * which accepts connections, sonewconn is called. If the
146 * connection is possible (subject to space constraints, etc.)
147 * then we allocate a new structure, propoerly linked into the
148 * data structure of the original socket, and return this.
149 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
150 *
151 * Currently, sonewconn() is defined as sonewconn1() in socketvar.h
152 * to catch calls that are missing the (new) second parameter.
153 */
154struct socket *
155sonewconn1(head, connstatus)
156 register struct socket *head;
157 int connstatus;
158{
159 register struct socket *so;
160 int soqueue = connstatus ? 1 : 0;
161
162 if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
163 return ((struct socket *)0);
164 MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_DONTWAIT);
165 if (so == NULL)
166 return ((struct socket *)0);
167 bzero((caddr_t)so, sizeof(*so));
168 so->so_type = head->so_type;
169 so->so_options = head->so_options &~ SO_ACCEPTCONN;
170 so->so_linger = head->so_linger;
171 so->so_state = head->so_state | SS_NOFDREF;
172 so->so_proto = head->so_proto;
173 so->so_timeo = head->so_timeo;
174 so->so_pgid = head->so_pgid;
175 (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
176 soqinsque(head, so, soqueue);
177 if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
178 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) {
179 (void) soqremque(so, soqueue);
180 (void) free((caddr_t)so, M_SOCKET);
181 return ((struct socket *)0);
182 }
183 if (connstatus) {
184 sorwakeup(head);
185 wakeup((caddr_t)&head->so_timeo);
186 so->so_state |= connstatus;
187 }
188 return (so);
189}
190
191void
192soqinsque(head, so, q)
193 register struct socket *head, *so;
194 int q;
195{
196
197 register struct socket **prev;
198 so->so_head = head;
199 if (q == 0) {
200 head->so_q0len++;
201 so->so_q0 = 0;
202 for (prev = &(head->so_q0); *prev; )
203 prev = &((*prev)->so_q0);
204 } else {
205 head->so_qlen++;
206 so->so_q = 0;
207 for (prev = &(head->so_q); *prev; )
208 prev = &((*prev)->so_q);
209 }
210 *prev = so;
211}
212
213int
214soqremque(so, q)
215 register struct socket *so;
216 int q;
217{
218 register struct socket *head, *prev, *next;
219
220 head = so->so_head;
221 prev = head;
222 for (;;) {
223 next = q ? prev->so_q : prev->so_q0;
224 if (next == so)
225 break;
226 if (next == 0)
227 return (0);
228 prev = next;
229 }
230 if (q == 0) {
231 prev->so_q0 = next->so_q0;
232 head->so_q0len--;
233 } else {
234 prev->so_q = next->so_q;
235 head->so_qlen--;
236 }
237 next->so_q0 = next->so_q = 0;
238 next->so_head = 0;
239 return (1);
240}
241
242/*
243 * Socantsendmore indicates that no more data will be sent on the
244 * socket; it would normally be applied to a socket when the user
245 * informs the system that no more data is to be sent, by the protocol
246 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
247 * will be received, and will normally be applied to the socket by a
248 * protocol when it detects that the peer will send no more data.
249 * Data queued for reading in the socket may yet be read.
250 */
251
252void
253socantsendmore(so)
254 struct socket *so;
255{
256
257 so->so_state |= SS_CANTSENDMORE;
258 sowwakeup(so);
259}
260
261void
262socantrcvmore(so)
263 struct socket *so;
264{
265
266 so->so_state |= SS_CANTRCVMORE;
267 sorwakeup(so);
268}
269
270/*
271 * Wait for data to arrive at/drain from a socket buffer.
272 */
273int
274sbwait(sb)
275 struct sockbuf *sb;
276{
277
278 sb->sb_flags |= SB_WAIT;
279 return (tsleep((caddr_t)&sb->sb_cc,
280 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio,
281 sb->sb_timeo));
282}
283
284/*
285 * Lock a sockbuf already known to be locked;
286 * return any error returned from sleep (EINTR).
287 */
288int
289sb_lock(sb)
290 register struct sockbuf *sb;
291{
292 int error;
293
294 while (sb->sb_flags & SB_LOCK) {
295 sb->sb_flags |= SB_WANT;
296 error = tsleep((caddr_t)&sb->sb_flags,
297 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
298 netio, 0);
299 if (error)
300 return (error);
301 }
302 sb->sb_flags |= SB_LOCK;
303 return (0);
304}
305
306/*
307 * Wakeup processes waiting on a socket buffer.
308 * Do asynchronous notification via SIGIO
309 * if the socket has the SS_ASYNC flag set.
310 */
311void
312sowakeup(so, sb)
313 register struct socket *so;
314 register struct sockbuf *sb;
315{
316 struct proc *p;
317
318 selwakeup(&sb->sb_sel);
319 sb->sb_flags &= ~SB_SEL;
320 if (sb->sb_flags & SB_WAIT) {
321 sb->sb_flags &= ~SB_WAIT;
322 wakeup((caddr_t)&sb->sb_cc);
323 }
324 if (so->so_state & SS_ASYNC) {
325 if (so->so_pgid < 0)
326 gsignal(-so->so_pgid, SIGIO);
327 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
328 psignal(p, SIGIO);
329 }
330}
331
332/*
333 * Socket buffer (struct sockbuf) utility routines.
334 *
335 * Each socket contains two socket buffers: one for sending data and
336 * one for receiving data. Each buffer contains a queue of mbufs,
337 * information about the number of mbufs and amount of data in the
338 * queue, and other fields allowing select() statements and notification
339 * on data availability to be implemented.
340 *
341 * Data stored in a socket buffer is maintained as a list of records.
342 * Each record is a list of mbufs chained together with the m_next
343 * field. Records are chained together with the m_nextpkt field. The upper
344 * level routine soreceive() expects the following conventions to be
345 * observed when placing information in the receive buffer:
346 *
347 * 1. If the protocol requires each message be preceded by the sender's
348 * name, then a record containing that name must be present before
349 * any associated data (mbuf's must be of type MT_SONAME).
350 * 2. If the protocol supports the exchange of ``access rights'' (really
351 * just additional data associated with the message), and there are
352 * ``rights'' to be received, then a record containing this data
353 * should be present (mbuf's must be of type MT_RIGHTS).
354 * 3. If a name or rights record exists, then it must be followed by
355 * a data record, perhaps of zero length.
356 *
357 * Before using a new socket structure it is first necessary to reserve
358 * buffer space to the socket, by calling sbreserve(). This should commit
359 * some of the available buffer space in the system buffer pool for the
360 * socket (currently, it does nothing but enforce limits). The space
361 * should be released by calling sbrelease() when the socket is destroyed.
362 */
363
364int
365soreserve(so, sndcc, rcvcc)
366 register struct socket *so;
367 u_long sndcc, rcvcc;
368{
369
370 if (sbreserve(&so->so_snd, sndcc) == 0)
371 goto bad;
372 if (sbreserve(&so->so_rcv, rcvcc) == 0)
373 goto bad2;
374 if (so->so_rcv.sb_lowat == 0)
375 so->so_rcv.sb_lowat = 1;
376 if (so->so_snd.sb_lowat == 0)
377 so->so_snd.sb_lowat = MCLBYTES;
378 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
379 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
380 return (0);
381bad2:
382 sbrelease(&so->so_snd);
383bad:
384 return (ENOBUFS);
385}
386
387/*
388 * Allot mbufs to a sockbuf.
389 * Attempt to scale mbmax so that mbcnt doesn't become limiting
390 * if buffering efficiency is near the normal case.
391 */
392int
393sbreserve(sb, cc)
394 struct sockbuf *sb;
395 u_long cc;
396{
397
398 if (cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES))
399 return (0);
400 sb->sb_hiwat = cc;
401 sb->sb_mbmax = min(cc * 2, sb_max);
402 if (sb->sb_lowat > sb->sb_hiwat)
403 sb->sb_lowat = sb->sb_hiwat;
404 return (1);
405}
406
407/*
408 * Free mbufs held by a socket, and reserved mbuf space.
409 */
410void
411sbrelease(sb)
412 struct sockbuf *sb;
413{
414
415 sbflush(sb);
416 sb->sb_hiwat = sb->sb_mbmax = 0;
417}
418
419/*
420 * Routines to add and remove
421 * data from an mbuf queue.
422 *
423 * The routines sbappend() or sbappendrecord() are normally called to
424 * append new mbufs to a socket buffer, after checking that adequate
425 * space is available, comparing the function sbspace() with the amount
426 * of data to be added. sbappendrecord() differs from sbappend() in
427 * that data supplied is treated as the beginning of a new record.
428 * To place a sender's address, optional access rights, and data in a
429 * socket receive buffer, sbappendaddr() should be used. To place
430 * access rights and data in a socket receive buffer, sbappendrights()
431 * should be used. In either case, the new data begins a new record.
432 * Note that unlike sbappend() and sbappendrecord(), these routines check
433 * for the caller that there will be enough space to store the data.
434 * Each fails if there is not enough space, or if it cannot find mbufs
435 * to store additional information in.
436 *
437 * Reliable protocols may use the socket send buffer to hold data
438 * awaiting acknowledgement. Data is normally copied from a socket
439 * send buffer in a protocol with m_copy for output to a peer,
440 * and then removing the data from the socket buffer with sbdrop()
441 * or sbdroprecord() when the data is acknowledged by the peer.
442 */
443
444/*
445 * Append mbuf chain m to the last record in the
446 * socket buffer sb. The additional space associated
447 * the mbuf chain is recorded in sb. Empty mbufs are
448 * discarded and mbufs are compacted where possible.
449 */
450void
451sbappend(sb, m)
452 struct sockbuf *sb;
453 struct mbuf *m;
454{
455 register struct mbuf *n;
456
457 if (m == 0)
458 return;
459 n = sb->sb_mb;
460 if (n) {
461 while (n->m_nextpkt)
462 n = n->m_nextpkt;
463 do {
464 if (n->m_flags & M_EOR) {
465 sbappendrecord(sb, m); /* XXXXXX!!!! */
466 return;
467 }
468 } while (n->m_next && (n = n->m_next));
469 }
470 sbcompress(sb, m, n);
471}
472
473#ifdef SOCKBUF_DEBUG
474void
475sbcheck(sb)
476 register struct sockbuf *sb;
477{
478 register struct mbuf *m;
479 register int len = 0, mbcnt = 0;
480
481 for (m = sb->sb_mb; m; m = m->m_next) {
482 len += m->m_len;
483 mbcnt += MSIZE;
484 if (m->m_flags & M_EXT)
485 mbcnt += m->m_ext.ext_size;
486 if (m->m_nextpkt)
487 panic("sbcheck nextpkt");
488 }
489 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
490 printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc,
491 mbcnt, sb->sb_mbcnt);
492 panic("sbcheck");
493 }
494}
495#endif
496
497/*
498 * As above, except the mbuf chain
499 * begins a new record.
500 */
501void
502sbappendrecord(sb, m0)
503 register struct sockbuf *sb;
504 register struct mbuf *m0;
505{
506 register struct mbuf *m;
507
508 if (m0 == 0)
509 return;
510 m = sb->sb_mb;
511 if (m)
512 while (m->m_nextpkt)
513 m = m->m_nextpkt;
514 /*
515 * Put the first mbuf on the queue.
516 * Note this permits zero length records.
517 */
518 sballoc(sb, m0);
519 if (m)
520 m->m_nextpkt = m0;
521 else
522 sb->sb_mb = m0;
523 m = m0->m_next;
524 m0->m_next = 0;
525 if (m && (m0->m_flags & M_EOR)) {
526 m0->m_flags &= ~M_EOR;
527 m->m_flags |= M_EOR;
528 }
529 sbcompress(sb, m, m0);
530}
531
532/*
533 * As above except that OOB data
534 * is inserted at the beginning of the sockbuf,
535 * but after any other OOB data.
536 */
537void
538sbinsertoob(sb, m0)
539 register struct sockbuf *sb;
540 register struct mbuf *m0;
541{
542 register struct mbuf *m;
543 register struct mbuf **mp;
544
545 if (m0 == 0)
546 return;
547 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
548 m = *mp;
549 again:
550 switch (m->m_type) {
551
552 case MT_OOBDATA:
553 continue; /* WANT next train */
554
555 case MT_CONTROL:
556 m = m->m_next;
557 if (m)
558 goto again; /* inspect THIS train further */
559 }
560 break;
561 }
562 /*
563 * Put the first mbuf on the queue.
564 * Note this permits zero length records.
565 */
566 sballoc(sb, m0);
567 m0->m_nextpkt = *mp;
568 *mp = m0;
569 m = m0->m_next;
570 m0->m_next = 0;
571 if (m && (m0->m_flags & M_EOR)) {
572 m0->m_flags &= ~M_EOR;
573 m->m_flags |= M_EOR;
574 }
575 sbcompress(sb, m, m0);
576}
577
578/*
579 * Append address and data, and optionally, control (ancillary) data
580 * to the receive queue of a socket. If present,
581 * m0 must include a packet header with total length.
582 * Returns 0 if no space in sockbuf or insufficient mbufs.
583 */
584int
585sbappendaddr(sb, asa, m0, control)
586 register struct sockbuf *sb;
587 struct sockaddr *asa;
588 struct mbuf *m0, *control;
589{
590 register struct mbuf *m, *n;
591 int space = asa->sa_len;
592
593if (m0 && (m0->m_flags & M_PKTHDR) == 0)
594panic("sbappendaddr");
595 if (m0)
596 space += m0->m_pkthdr.len;
597 for (n = control; n; n = n->m_next) {
598 space += n->m_len;
599 if (n->m_next == 0) /* keep pointer to last control buf */
600 break;
601 }
602 if (space > sbspace(sb))
603 return (0);
604 if (asa->sa_len > MLEN)
605 return (0);
606 MGET(m, M_DONTWAIT, MT_SONAME);
607 if (m == 0)
608 return (0);
609 m->m_len = asa->sa_len;
610 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
611 if (n)
612 n->m_next = m0; /* concatenate data to control */
613 else
614 control = m0;
615 m->m_next = control;
616 for (n = m; n; n = n->m_next)
617 sballoc(sb, n);
618 n = sb->sb_mb;
619 if (n) {
620 while (n->m_nextpkt)
621 n = n->m_nextpkt;
622 n->m_nextpkt = m;
623 } else
624 sb->sb_mb = m;
625 return (1);
626}
627
628int
629sbappendcontrol(sb, m0, control)
630 struct sockbuf *sb;
631 struct mbuf *control, *m0;
632{
633 register struct mbuf *m, *n;
634 int space = 0;
635
636 if (control == 0)
637 panic("sbappendcontrol");
638 for (m = control; ; m = m->m_next) {
639 space += m->m_len;
640 if (m->m_next == 0)
641 break;
642 }
643 n = m; /* save pointer to last control buffer */
644 for (m = m0; m; m = m->m_next)
645 space += m->m_len;
646 if (space > sbspace(sb))
647 return (0);
648 n->m_next = m0; /* concatenate data to control */
649 for (m = control; m; m = m->m_next)
650 sballoc(sb, m);
651 n = sb->sb_mb;
652 if (n) {
653 while (n->m_nextpkt)
654 n = n->m_nextpkt;
655 n->m_nextpkt = control;
656 } else
657 sb->sb_mb = control;
658 return (1);
659}
660
661/*
662 * Compress mbuf chain m into the socket
663 * buffer sb following mbuf n. If n
664 * is null, the buffer is presumed empty.
665 */
666void
667sbcompress(sb, m, n)
668 register struct sockbuf *sb;
669 register struct mbuf *m, *n;
670{
671 register int eor = 0;
672 register struct mbuf *o;
673
674 while (m) {
675 eor |= m->m_flags & M_EOR;
676 if (m->m_len == 0 &&
677 (eor == 0 ||
678 (((o = m->m_next) || (o = n)) &&
679 o->m_type == m->m_type))) {
680 m = m_free(m);
681 continue;
682 }
683 if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 &&
684 (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] &&
685 n->m_type == m->m_type) {
686 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
687 (unsigned)m->m_len);
688 n->m_len += m->m_len;
689 sb->sb_cc += m->m_len;
690 m = m_free(m);
691 continue;
692 }
693 if (n)
694 n->m_next = m;
695 else
696 sb->sb_mb = m;
697 sballoc(sb, m);
698 n = m;
699 m->m_flags &= ~M_EOR;
700 m = m->m_next;
701 n->m_next = 0;
702 }
703 if (eor) {
704 if (n)
705 n->m_flags |= eor;
706 else
707 printf("semi-panic: sbcompress\n");
708 }
709}
710
711/*
712 * Free all mbufs in a sockbuf.
713 * Check that all resources are reclaimed.
714 */
715void
716sbflush(sb)
717 register struct sockbuf *sb;
718{
719
720 if (sb->sb_flags & SB_LOCK)
721 panic("sbflush");
722 while (sb->sb_mbcnt)
723 sbdrop(sb, (int)sb->sb_cc);
724 if (sb->sb_cc || sb->sb_mb)
725 panic("sbflush 2");
726}
727
728/*
729 * Drop data from (the front of) a sockbuf.
730 */
731void
732sbdrop(sb, len)
733 register struct sockbuf *sb;
734 register int len;
735{
736 register struct mbuf *m, *mn;
737 struct mbuf *next;
738
739 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
740 while (len > 0) {
741 if (m == 0) {
742 if (next == 0)
743 panic("sbdrop");
744 m = next;
745 next = m->m_nextpkt;
746 continue;
747 }
748 if (m->m_len > len) {
749 m->m_len -= len;
750 m->m_data += len;
751 sb->sb_cc -= len;
752 break;
753 }
754 len -= m->m_len;
755 sbfree(sb, m);
756 MFREE(m, mn);
757 m = mn;
758 }
759 while (m && m->m_len == 0) {
760 sbfree(sb, m);
761 MFREE(m, mn);
762 m = mn;
763 }
764 if (m) {
765 sb->sb_mb = m;
766 m->m_nextpkt = next;
767 } else
768 sb->sb_mb = next;
769}
770
771/*
772 * Drop a record off the front of a sockbuf
773 * and move the next record to the front.
774 */
775void
776sbdroprecord(sb)
777 register struct sockbuf *sb;
778{
779 register struct mbuf *m, *mn;
780
781 m = sb->sb_mb;
782 if (m) {
783 sb->sb_mb = m->m_nextpkt;
784 do {
785 sbfree(sb, m);
786 MFREE(m, mn);
787 m = mn;
788 } while (m);
789 }
790}
63
64/*
65 * Procedures to manipulate state flags of socket
66 * and do appropriate wakeups. Normal sequence from the
67 * active (originating) side is that soisconnecting() is
68 * called during processing of connect() call,
69 * resulting in an eventual call to soisconnected() if/when the
70 * connection is established. When the connection is torn down
71 * soisdisconnecting() is called during processing of disconnect() call,
72 * and soisdisconnected() is called when the connection to the peer
73 * is totally severed. The semantics of these routines are such that
74 * connectionless protocols can call soisconnected() and soisdisconnected()
75 * only, bypassing the in-progress calls when setting up a ``connection''
76 * takes no time.
77 *
78 * From the passive side, a socket is created with
79 * two queues of sockets: so_q0 for connections in progress
80 * and so_q for connections already made and awaiting user acceptance.
81 * As a protocol is preparing incoming connections, it creates a socket
82 * structure queued on so_q0 by calling sonewconn(). When the connection
83 * is established, soisconnected() is called, and transfers the
84 * socket structure to so_q, making it available to accept().
85 *
86 * If a socket is closed with sockets on either
87 * so_q0 or so_q, these sockets are dropped.
88 *
89 * If higher level protocols are implemented in
90 * the kernel, the wakeups done here will sometimes
91 * cause software-interrupt process scheduling.
92 */
93
94void
95soisconnecting(so)
96 register struct socket *so;
97{
98
99 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
100 so->so_state |= SS_ISCONNECTING;
101}
102
103void
104soisconnected(so)
105 register struct socket *so;
106{
107 register struct socket *head = so->so_head;
108
109 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
110 so->so_state |= SS_ISCONNECTED;
111 if (head && soqremque(so, 0)) {
112 soqinsque(head, so, 1);
113 sorwakeup(head);
114 wakeup((caddr_t)&head->so_timeo);
115 } else {
116 wakeup((caddr_t)&so->so_timeo);
117 sorwakeup(so);
118 sowwakeup(so);
119 }
120}
121
122void
123soisdisconnecting(so)
124 register struct socket *so;
125{
126
127 so->so_state &= ~SS_ISCONNECTING;
128 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
129 wakeup((caddr_t)&so->so_timeo);
130 sowwakeup(so);
131 sorwakeup(so);
132}
133
134void
135soisdisconnected(so)
136 register struct socket *so;
137{
138
139 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
140 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
141 wakeup((caddr_t)&so->so_timeo);
142 sowwakeup(so);
143 sorwakeup(so);
144}
145
146/*
147 * When an attempt at a new connection is noted on a socket
148 * which accepts connections, sonewconn is called. If the
149 * connection is possible (subject to space constraints, etc.)
150 * then we allocate a new structure, propoerly linked into the
151 * data structure of the original socket, and return this.
152 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
153 *
154 * Currently, sonewconn() is defined as sonewconn1() in socketvar.h
155 * to catch calls that are missing the (new) second parameter.
156 */
157struct socket *
158sonewconn1(head, connstatus)
159 register struct socket *head;
160 int connstatus;
161{
162 register struct socket *so;
163 int soqueue = connstatus ? 1 : 0;
164
165 if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2)
166 return ((struct socket *)0);
167 MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_DONTWAIT);
168 if (so == NULL)
169 return ((struct socket *)0);
170 bzero((caddr_t)so, sizeof(*so));
171 so->so_type = head->so_type;
172 so->so_options = head->so_options &~ SO_ACCEPTCONN;
173 so->so_linger = head->so_linger;
174 so->so_state = head->so_state | SS_NOFDREF;
175 so->so_proto = head->so_proto;
176 so->so_timeo = head->so_timeo;
177 so->so_pgid = head->so_pgid;
178 (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
179 soqinsque(head, so, soqueue);
180 if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
181 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) {
182 (void) soqremque(so, soqueue);
183 (void) free((caddr_t)so, M_SOCKET);
184 return ((struct socket *)0);
185 }
186 if (connstatus) {
187 sorwakeup(head);
188 wakeup((caddr_t)&head->so_timeo);
189 so->so_state |= connstatus;
190 }
191 return (so);
192}
193
194void
195soqinsque(head, so, q)
196 register struct socket *head, *so;
197 int q;
198{
199
200 register struct socket **prev;
201 so->so_head = head;
202 if (q == 0) {
203 head->so_q0len++;
204 so->so_q0 = 0;
205 for (prev = &(head->so_q0); *prev; )
206 prev = &((*prev)->so_q0);
207 } else {
208 head->so_qlen++;
209 so->so_q = 0;
210 for (prev = &(head->so_q); *prev; )
211 prev = &((*prev)->so_q);
212 }
213 *prev = so;
214}
215
216int
217soqremque(so, q)
218 register struct socket *so;
219 int q;
220{
221 register struct socket *head, *prev, *next;
222
223 head = so->so_head;
224 prev = head;
225 for (;;) {
226 next = q ? prev->so_q : prev->so_q0;
227 if (next == so)
228 break;
229 if (next == 0)
230 return (0);
231 prev = next;
232 }
233 if (q == 0) {
234 prev->so_q0 = next->so_q0;
235 head->so_q0len--;
236 } else {
237 prev->so_q = next->so_q;
238 head->so_qlen--;
239 }
240 next->so_q0 = next->so_q = 0;
241 next->so_head = 0;
242 return (1);
243}
244
245/*
246 * Socantsendmore indicates that no more data will be sent on the
247 * socket; it would normally be applied to a socket when the user
248 * informs the system that no more data is to be sent, by the protocol
249 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
250 * will be received, and will normally be applied to the socket by a
251 * protocol when it detects that the peer will send no more data.
252 * Data queued for reading in the socket may yet be read.
253 */
254
255void
256socantsendmore(so)
257 struct socket *so;
258{
259
260 so->so_state |= SS_CANTSENDMORE;
261 sowwakeup(so);
262}
263
264void
265socantrcvmore(so)
266 struct socket *so;
267{
268
269 so->so_state |= SS_CANTRCVMORE;
270 sorwakeup(so);
271}
272
273/*
274 * Wait for data to arrive at/drain from a socket buffer.
275 */
276int
277sbwait(sb)
278 struct sockbuf *sb;
279{
280
281 sb->sb_flags |= SB_WAIT;
282 return (tsleep((caddr_t)&sb->sb_cc,
283 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio,
284 sb->sb_timeo));
285}
286
287/*
288 * Lock a sockbuf already known to be locked;
289 * return any error returned from sleep (EINTR).
290 */
291int
292sb_lock(sb)
293 register struct sockbuf *sb;
294{
295 int error;
296
297 while (sb->sb_flags & SB_LOCK) {
298 sb->sb_flags |= SB_WANT;
299 error = tsleep((caddr_t)&sb->sb_flags,
300 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
301 netio, 0);
302 if (error)
303 return (error);
304 }
305 sb->sb_flags |= SB_LOCK;
306 return (0);
307}
308
309/*
310 * Wakeup processes waiting on a socket buffer.
311 * Do asynchronous notification via SIGIO
312 * if the socket has the SS_ASYNC flag set.
313 */
314void
315sowakeup(so, sb)
316 register struct socket *so;
317 register struct sockbuf *sb;
318{
319 struct proc *p;
320
321 selwakeup(&sb->sb_sel);
322 sb->sb_flags &= ~SB_SEL;
323 if (sb->sb_flags & SB_WAIT) {
324 sb->sb_flags &= ~SB_WAIT;
325 wakeup((caddr_t)&sb->sb_cc);
326 }
327 if (so->so_state & SS_ASYNC) {
328 if (so->so_pgid < 0)
329 gsignal(-so->so_pgid, SIGIO);
330 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
331 psignal(p, SIGIO);
332 }
333}
334
335/*
336 * Socket buffer (struct sockbuf) utility routines.
337 *
338 * Each socket contains two socket buffers: one for sending data and
339 * one for receiving data. Each buffer contains a queue of mbufs,
340 * information about the number of mbufs and amount of data in the
341 * queue, and other fields allowing select() statements and notification
342 * on data availability to be implemented.
343 *
344 * Data stored in a socket buffer is maintained as a list of records.
345 * Each record is a list of mbufs chained together with the m_next
346 * field. Records are chained together with the m_nextpkt field. The upper
347 * level routine soreceive() expects the following conventions to be
348 * observed when placing information in the receive buffer:
349 *
350 * 1. If the protocol requires each message be preceded by the sender's
351 * name, then a record containing that name must be present before
352 * any associated data (mbuf's must be of type MT_SONAME).
353 * 2. If the protocol supports the exchange of ``access rights'' (really
354 * just additional data associated with the message), and there are
355 * ``rights'' to be received, then a record containing this data
356 * should be present (mbuf's must be of type MT_RIGHTS).
357 * 3. If a name or rights record exists, then it must be followed by
358 * a data record, perhaps of zero length.
359 *
360 * Before using a new socket structure it is first necessary to reserve
361 * buffer space to the socket, by calling sbreserve(). This should commit
362 * some of the available buffer space in the system buffer pool for the
363 * socket (currently, it does nothing but enforce limits). The space
364 * should be released by calling sbrelease() when the socket is destroyed.
365 */
366
367int
368soreserve(so, sndcc, rcvcc)
369 register struct socket *so;
370 u_long sndcc, rcvcc;
371{
372
373 if (sbreserve(&so->so_snd, sndcc) == 0)
374 goto bad;
375 if (sbreserve(&so->so_rcv, rcvcc) == 0)
376 goto bad2;
377 if (so->so_rcv.sb_lowat == 0)
378 so->so_rcv.sb_lowat = 1;
379 if (so->so_snd.sb_lowat == 0)
380 so->so_snd.sb_lowat = MCLBYTES;
381 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
382 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
383 return (0);
384bad2:
385 sbrelease(&so->so_snd);
386bad:
387 return (ENOBUFS);
388}
389
390/*
391 * Allot mbufs to a sockbuf.
392 * Attempt to scale mbmax so that mbcnt doesn't become limiting
393 * if buffering efficiency is near the normal case.
394 */
395int
396sbreserve(sb, cc)
397 struct sockbuf *sb;
398 u_long cc;
399{
400
401 if (cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES))
402 return (0);
403 sb->sb_hiwat = cc;
404 sb->sb_mbmax = min(cc * 2, sb_max);
405 if (sb->sb_lowat > sb->sb_hiwat)
406 sb->sb_lowat = sb->sb_hiwat;
407 return (1);
408}
409
410/*
411 * Free mbufs held by a socket, and reserved mbuf space.
412 */
413void
414sbrelease(sb)
415 struct sockbuf *sb;
416{
417
418 sbflush(sb);
419 sb->sb_hiwat = sb->sb_mbmax = 0;
420}
421
422/*
423 * Routines to add and remove
424 * data from an mbuf queue.
425 *
426 * The routines sbappend() or sbappendrecord() are normally called to
427 * append new mbufs to a socket buffer, after checking that adequate
428 * space is available, comparing the function sbspace() with the amount
429 * of data to be added. sbappendrecord() differs from sbappend() in
430 * that data supplied is treated as the beginning of a new record.
431 * To place a sender's address, optional access rights, and data in a
432 * socket receive buffer, sbappendaddr() should be used. To place
433 * access rights and data in a socket receive buffer, sbappendrights()
434 * should be used. In either case, the new data begins a new record.
435 * Note that unlike sbappend() and sbappendrecord(), these routines check
436 * for the caller that there will be enough space to store the data.
437 * Each fails if there is not enough space, or if it cannot find mbufs
438 * to store additional information in.
439 *
440 * Reliable protocols may use the socket send buffer to hold data
441 * awaiting acknowledgement. Data is normally copied from a socket
442 * send buffer in a protocol with m_copy for output to a peer,
443 * and then removing the data from the socket buffer with sbdrop()
444 * or sbdroprecord() when the data is acknowledged by the peer.
445 */
446
447/*
448 * Append mbuf chain m to the last record in the
449 * socket buffer sb. The additional space associated
450 * the mbuf chain is recorded in sb. Empty mbufs are
451 * discarded and mbufs are compacted where possible.
452 */
453void
454sbappend(sb, m)
455 struct sockbuf *sb;
456 struct mbuf *m;
457{
458 register struct mbuf *n;
459
460 if (m == 0)
461 return;
462 n = sb->sb_mb;
463 if (n) {
464 while (n->m_nextpkt)
465 n = n->m_nextpkt;
466 do {
467 if (n->m_flags & M_EOR) {
468 sbappendrecord(sb, m); /* XXXXXX!!!! */
469 return;
470 }
471 } while (n->m_next && (n = n->m_next));
472 }
473 sbcompress(sb, m, n);
474}
475
476#ifdef SOCKBUF_DEBUG
477void
478sbcheck(sb)
479 register struct sockbuf *sb;
480{
481 register struct mbuf *m;
482 register int len = 0, mbcnt = 0;
483
484 for (m = sb->sb_mb; m; m = m->m_next) {
485 len += m->m_len;
486 mbcnt += MSIZE;
487 if (m->m_flags & M_EXT)
488 mbcnt += m->m_ext.ext_size;
489 if (m->m_nextpkt)
490 panic("sbcheck nextpkt");
491 }
492 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
493 printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc,
494 mbcnt, sb->sb_mbcnt);
495 panic("sbcheck");
496 }
497}
498#endif
499
500/*
501 * As above, except the mbuf chain
502 * begins a new record.
503 */
504void
505sbappendrecord(sb, m0)
506 register struct sockbuf *sb;
507 register struct mbuf *m0;
508{
509 register struct mbuf *m;
510
511 if (m0 == 0)
512 return;
513 m = sb->sb_mb;
514 if (m)
515 while (m->m_nextpkt)
516 m = m->m_nextpkt;
517 /*
518 * Put the first mbuf on the queue.
519 * Note this permits zero length records.
520 */
521 sballoc(sb, m0);
522 if (m)
523 m->m_nextpkt = m0;
524 else
525 sb->sb_mb = m0;
526 m = m0->m_next;
527 m0->m_next = 0;
528 if (m && (m0->m_flags & M_EOR)) {
529 m0->m_flags &= ~M_EOR;
530 m->m_flags |= M_EOR;
531 }
532 sbcompress(sb, m, m0);
533}
534
535/*
536 * As above except that OOB data
537 * is inserted at the beginning of the sockbuf,
538 * but after any other OOB data.
539 */
540void
541sbinsertoob(sb, m0)
542 register struct sockbuf *sb;
543 register struct mbuf *m0;
544{
545 register struct mbuf *m;
546 register struct mbuf **mp;
547
548 if (m0 == 0)
549 return;
550 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
551 m = *mp;
552 again:
553 switch (m->m_type) {
554
555 case MT_OOBDATA:
556 continue; /* WANT next train */
557
558 case MT_CONTROL:
559 m = m->m_next;
560 if (m)
561 goto again; /* inspect THIS train further */
562 }
563 break;
564 }
565 /*
566 * Put the first mbuf on the queue.
567 * Note this permits zero length records.
568 */
569 sballoc(sb, m0);
570 m0->m_nextpkt = *mp;
571 *mp = m0;
572 m = m0->m_next;
573 m0->m_next = 0;
574 if (m && (m0->m_flags & M_EOR)) {
575 m0->m_flags &= ~M_EOR;
576 m->m_flags |= M_EOR;
577 }
578 sbcompress(sb, m, m0);
579}
580
581/*
582 * Append address and data, and optionally, control (ancillary) data
583 * to the receive queue of a socket. If present,
584 * m0 must include a packet header with total length.
585 * Returns 0 if no space in sockbuf or insufficient mbufs.
586 */
587int
588sbappendaddr(sb, asa, m0, control)
589 register struct sockbuf *sb;
590 struct sockaddr *asa;
591 struct mbuf *m0, *control;
592{
593 register struct mbuf *m, *n;
594 int space = asa->sa_len;
595
596if (m0 && (m0->m_flags & M_PKTHDR) == 0)
597panic("sbappendaddr");
598 if (m0)
599 space += m0->m_pkthdr.len;
600 for (n = control; n; n = n->m_next) {
601 space += n->m_len;
602 if (n->m_next == 0) /* keep pointer to last control buf */
603 break;
604 }
605 if (space > sbspace(sb))
606 return (0);
607 if (asa->sa_len > MLEN)
608 return (0);
609 MGET(m, M_DONTWAIT, MT_SONAME);
610 if (m == 0)
611 return (0);
612 m->m_len = asa->sa_len;
613 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
614 if (n)
615 n->m_next = m0; /* concatenate data to control */
616 else
617 control = m0;
618 m->m_next = control;
619 for (n = m; n; n = n->m_next)
620 sballoc(sb, n);
621 n = sb->sb_mb;
622 if (n) {
623 while (n->m_nextpkt)
624 n = n->m_nextpkt;
625 n->m_nextpkt = m;
626 } else
627 sb->sb_mb = m;
628 return (1);
629}
630
631int
632sbappendcontrol(sb, m0, control)
633 struct sockbuf *sb;
634 struct mbuf *control, *m0;
635{
636 register struct mbuf *m, *n;
637 int space = 0;
638
639 if (control == 0)
640 panic("sbappendcontrol");
641 for (m = control; ; m = m->m_next) {
642 space += m->m_len;
643 if (m->m_next == 0)
644 break;
645 }
646 n = m; /* save pointer to last control buffer */
647 for (m = m0; m; m = m->m_next)
648 space += m->m_len;
649 if (space > sbspace(sb))
650 return (0);
651 n->m_next = m0; /* concatenate data to control */
652 for (m = control; m; m = m->m_next)
653 sballoc(sb, m);
654 n = sb->sb_mb;
655 if (n) {
656 while (n->m_nextpkt)
657 n = n->m_nextpkt;
658 n->m_nextpkt = control;
659 } else
660 sb->sb_mb = control;
661 return (1);
662}
663
664/*
665 * Compress mbuf chain m into the socket
666 * buffer sb following mbuf n. If n
667 * is null, the buffer is presumed empty.
668 */
669void
670sbcompress(sb, m, n)
671 register struct sockbuf *sb;
672 register struct mbuf *m, *n;
673{
674 register int eor = 0;
675 register struct mbuf *o;
676
677 while (m) {
678 eor |= m->m_flags & M_EOR;
679 if (m->m_len == 0 &&
680 (eor == 0 ||
681 (((o = m->m_next) || (o = n)) &&
682 o->m_type == m->m_type))) {
683 m = m_free(m);
684 continue;
685 }
686 if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 &&
687 (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] &&
688 n->m_type == m->m_type) {
689 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
690 (unsigned)m->m_len);
691 n->m_len += m->m_len;
692 sb->sb_cc += m->m_len;
693 m = m_free(m);
694 continue;
695 }
696 if (n)
697 n->m_next = m;
698 else
699 sb->sb_mb = m;
700 sballoc(sb, m);
701 n = m;
702 m->m_flags &= ~M_EOR;
703 m = m->m_next;
704 n->m_next = 0;
705 }
706 if (eor) {
707 if (n)
708 n->m_flags |= eor;
709 else
710 printf("semi-panic: sbcompress\n");
711 }
712}
713
714/*
715 * Free all mbufs in a sockbuf.
716 * Check that all resources are reclaimed.
717 */
718void
719sbflush(sb)
720 register struct sockbuf *sb;
721{
722
723 if (sb->sb_flags & SB_LOCK)
724 panic("sbflush");
725 while (sb->sb_mbcnt)
726 sbdrop(sb, (int)sb->sb_cc);
727 if (sb->sb_cc || sb->sb_mb)
728 panic("sbflush 2");
729}
730
731/*
732 * Drop data from (the front of) a sockbuf.
733 */
734void
735sbdrop(sb, len)
736 register struct sockbuf *sb;
737 register int len;
738{
739 register struct mbuf *m, *mn;
740 struct mbuf *next;
741
742 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
743 while (len > 0) {
744 if (m == 0) {
745 if (next == 0)
746 panic("sbdrop");
747 m = next;
748 next = m->m_nextpkt;
749 continue;
750 }
751 if (m->m_len > len) {
752 m->m_len -= len;
753 m->m_data += len;
754 sb->sb_cc -= len;
755 break;
756 }
757 len -= m->m_len;
758 sbfree(sb, m);
759 MFREE(m, mn);
760 m = mn;
761 }
762 while (m && m->m_len == 0) {
763 sbfree(sb, m);
764 MFREE(m, mn);
765 m = mn;
766 }
767 if (m) {
768 sb->sb_mb = m;
769 m->m_nextpkt = next;
770 } else
771 sb->sb_mb = next;
772}
773
774/*
775 * Drop a record off the front of a sockbuf
776 * and move the next record to the front.
777 */
778void
779sbdroprecord(sb)
780 register struct sockbuf *sb;
781{
782 register struct mbuf *m, *mn;
783
784 m = sb->sb_mb;
785 if (m) {
786 sb->sb_mb = m->m_nextpkt;
787 do {
788 sbfree(sb, m);
789 MFREE(m, mn);
790 m = mn;
791 } while (m);
792 }
793}