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