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