1 /* $NetBSD: uipc_socket2.c,v 1.16 1996/11/10 05:58:37 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.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/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/signalvar.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 u_long sb_max = SB_MAX; /* patchable */ 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 91 void 92 soisconnecting(so) 93 register struct socket *so; 94 { 95 96 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 97 so->so_state |= SS_ISCONNECTING; 98 } 99 100 void 101 soisconnected(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 119 void 120 soisdisconnecting(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 131 void 132 soisdisconnected(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 */ 154 struct socket * 155 sonewconn1(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 (struct proc *)0)) { 180 (void) soqremque(so, soqueue); 181 (void) free((caddr_t)so, M_SOCKET); 182 return ((struct socket *)0); 183 } 184 if (connstatus) { 185 sorwakeup(head); 186 wakeup((caddr_t)&head->so_timeo); 187 so->so_state |= connstatus; 188 } 189 return (so); 190 } 191 192 /* 193 * The following two routines (soqinsque & soqremque) have been changed 194 * to keep the queue of pending sockets in a double linked list. 195 * (Previously a singly linked list was used. This gave O(N) 196 * insertion/deletion times and was a major time consumer for sockets 197 * with large pending socket queues). The doublely-linked list gives 198 * constant insertion/deletion times with only small cost in complexity. 199 * 200 * Since a socket can be on, at most, one queue at a time both so_q and 201 * so_q0 can safely be used as (forward and backward, respectively) queue 202 * pointers. 203 * 204 * Unlike traditional doublely linked lists, the queue head is not present 205 * in the list. Instead only a single pointer to the first element is kept. 206 * Only when this first element is modified (either adding to an empty list 207 * or removing the first element) does the pointer change. If the list is 208 * empty, the pointer will be NULL. 209 * 210 * The back pointer of the first entry points to the last entry (instead of 211 * the queue head since there isn't a queue head). 212 */ 213 214 void 215 soqinsque(head, so, q) 216 register struct socket *head, *so; 217 int q; 218 { 219 register struct socket **qh; 220 221 #ifdef DIAGNOSTIC 222 if (so->so_head != NULL) 223 panic("soqinsque"); 224 #endif 225 226 so->so_head = head; 227 if (q == 0) { 228 head->so_q0len++; 229 qh = &head->so_q0; 230 } else { 231 head->so_qlen++; 232 qh = &head->so_q; 233 } 234 if ((*qh) == NULL) { 235 so->so_q = so->so_q0 = so; 236 (*qh) = so; 237 } else { 238 /* insert at tail */ 239 so->so_q = (*qh); 240 so->so_q0 = (*qh)->so_q0; 241 so->so_q0->so_q = so; 242 (*qh)->so_q0 = so; 243 } 244 } 245 246 int 247 soqremque(so, q) 248 register struct socket *so; 249 int q; 250 { 251 register struct socket *head = so->so_head; 252 register struct socket **qh; 253 254 if (head == NULL) { 255 #ifdef DIAGNOSTIC 256 if (so->so_q != NULL || so->so_q0 != NULL) 257 panic("soqremque 1"); 258 #endif 259 return (0); 260 } 261 if (q == 0) { 262 head->so_q0len--; 263 qh = &head->so_q0; 264 } else { 265 head->so_qlen--; 266 qh = &head->so_q; 267 } 268 269 #ifdef DIAGNOSTIC 270 if ((*qh) == NULL || so->so_q == NULL || so->so_q0 == NULL) 271 panic("soqremque 2"); 272 #endif 273 274 if ((*qh) == so) { 275 /* first */ 276 if (so->so_q == so) { 277 /* single entry; don't remove it from itself */ 278 (*qh) = NULL; 279 } else { 280 so->so_q0->so_q = so->so_q; 281 so->so_q->so_q0 = so->so_q0; 282 (*qh) = so->so_q; 283 } 284 } else { 285 /* in the middle (or last) but not first */ 286 so->so_q0->so_q = so->so_q; 287 so->so_q->so_q0 = so->so_q0; 288 } 289 so->so_q = so->so_q0 = NULL; 290 so->so_head = NULL; 291 return (1); 292 } 293 294 /* 295 * Socantsendmore indicates that no more data will be sent on the 296 * socket; it would normally be applied to a socket when the user 297 * informs the system that no more data is to be sent, by the protocol 298 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 299 * will be received, and will normally be applied to the socket by a 300 * protocol when it detects that the peer will send no more data. 301 * Data queued for reading in the socket may yet be read. 302 */ 303 304 void 305 socantsendmore(so) 306 struct socket *so; 307 { 308 309 so->so_state |= SS_CANTSENDMORE; 310 sowwakeup(so); 311 } 312 313 void 314 socantrcvmore(so) 315 struct socket *so; 316 { 317 318 so->so_state |= SS_CANTRCVMORE; 319 sorwakeup(so); 320 } 321 322 /* 323 * Wait for data to arrive at/drain from a socket buffer. 324 */ 325 int 326 sbwait(sb) 327 struct sockbuf *sb; 328 { 329 330 sb->sb_flags |= SB_WAIT; 331 return (tsleep((caddr_t)&sb->sb_cc, 332 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio, 333 sb->sb_timeo)); 334 } 335 336 /* 337 * Lock a sockbuf already known to be locked; 338 * return any error returned from sleep (EINTR). 339 */ 340 int 341 sb_lock(sb) 342 register struct sockbuf *sb; 343 { 344 int error; 345 346 while (sb->sb_flags & SB_LOCK) { 347 sb->sb_flags |= SB_WANT; 348 error = tsleep((caddr_t)&sb->sb_flags, 349 (sb->sb_flags & SB_NOINTR) ? 350 PSOCK : PSOCK|PCATCH, netio, 0); 351 if (error) 352 return (error); 353 } 354 sb->sb_flags |= SB_LOCK; 355 return (0); 356 } 357 358 /* 359 * Wakeup processes waiting on a socket buffer. 360 * Do asynchronous notification via SIGIO 361 * if the socket has the SS_ASYNC flag set. 362 */ 363 void 364 sowakeup(so, sb) 365 register struct socket *so; 366 register struct sockbuf *sb; 367 { 368 struct proc *p; 369 370 selwakeup(&sb->sb_sel); 371 sb->sb_flags &= ~SB_SEL; 372 if (sb->sb_flags & SB_WAIT) { 373 sb->sb_flags &= ~SB_WAIT; 374 wakeup((caddr_t)&sb->sb_cc); 375 } 376 if (so->so_state & SS_ASYNC) { 377 if (so->so_pgid < 0) 378 gsignal(-so->so_pgid, SIGIO); 379 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) 380 psignal(p, SIGIO); 381 } 382 } 383 384 /* 385 * Socket buffer (struct sockbuf) utility routines. 386 * 387 * Each socket contains two socket buffers: one for sending data and 388 * one for receiving data. Each buffer contains a queue of mbufs, 389 * information about the number of mbufs and amount of data in the 390 * queue, and other fields allowing poll() statements and notification 391 * on data availability to be implemented. 392 * 393 * Data stored in a socket buffer is maintained as a list of records. 394 * Each record is a list of mbufs chained together with the m_next 395 * field. Records are chained together with the m_nextpkt field. The upper 396 * level routine soreceive() expects the following conventions to be 397 * observed when placing information in the receive buffer: 398 * 399 * 1. If the protocol requires each message be preceded by the sender's 400 * name, then a record containing that name must be present before 401 * any associated data (mbuf's must be of type MT_SONAME). 402 * 2. If the protocol supports the exchange of ``access rights'' (really 403 * just additional data associated with the message), and there are 404 * ``rights'' to be received, then a record containing this data 405 * should be present (mbuf's must be of type MT_CONTROL). 406 * 3. If a name or rights record exists, then it must be followed by 407 * a data record, perhaps of zero length. 408 * 409 * Before using a new socket structure it is first necessary to reserve 410 * buffer space to the socket, by calling sbreserve(). This should commit 411 * some of the available buffer space in the system buffer pool for the 412 * socket (currently, it does nothing but enforce limits). The space 413 * should be released by calling sbrelease() when the socket is destroyed. 414 */ 415 416 int 417 soreserve(so, sndcc, rcvcc) 418 register struct socket *so; 419 u_long sndcc, rcvcc; 420 { 421 422 if (sbreserve(&so->so_snd, sndcc) == 0) 423 goto bad; 424 if (sbreserve(&so->so_rcv, rcvcc) == 0) 425 goto bad2; 426 if (so->so_rcv.sb_lowat == 0) 427 so->so_rcv.sb_lowat = 1; 428 if (so->so_snd.sb_lowat == 0) 429 so->so_snd.sb_lowat = MCLBYTES; 430 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 431 so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 432 return (0); 433 bad2: 434 sbrelease(&so->so_snd); 435 bad: 436 return (ENOBUFS); 437 } 438 439 /* 440 * Allot mbufs to a sockbuf. 441 * Attempt to scale mbmax so that mbcnt doesn't become limiting 442 * if buffering efficiency is near the normal case. 443 */ 444 int 445 sbreserve(sb, cc) 446 struct sockbuf *sb; 447 u_long cc; 448 { 449 450 if (cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES)) 451 return (0); 452 sb->sb_hiwat = cc; 453 sb->sb_mbmax = min(cc * 2, sb_max); 454 if (sb->sb_lowat > sb->sb_hiwat) 455 sb->sb_lowat = sb->sb_hiwat; 456 return (1); 457 } 458 459 /* 460 * Free mbufs held by a socket, and reserved mbuf space. 461 */ 462 void 463 sbrelease(sb) 464 struct sockbuf *sb; 465 { 466 467 sbflush(sb); 468 sb->sb_hiwat = sb->sb_mbmax = 0; 469 } 470 471 /* 472 * Routines to add and remove 473 * data from an mbuf queue. 474 * 475 * The routines sbappend() or sbappendrecord() are normally called to 476 * append new mbufs to a socket buffer, after checking that adequate 477 * space is available, comparing the function sbspace() with the amount 478 * of data to be added. sbappendrecord() differs from sbappend() in 479 * that data supplied is treated as the beginning of a new record. 480 * To place a sender's address, optional access rights, and data in a 481 * socket receive buffer, sbappendaddr() should be used. To place 482 * access rights and data in a socket receive buffer, sbappendrights() 483 * should be used. In either case, the new data begins a new record. 484 * Note that unlike sbappend() and sbappendrecord(), these routines check 485 * for the caller that there will be enough space to store the data. 486 * Each fails if there is not enough space, or if it cannot find mbufs 487 * to store additional information in. 488 * 489 * Reliable protocols may use the socket send buffer to hold data 490 * awaiting acknowledgement. Data is normally copied from a socket 491 * send buffer in a protocol with m_copy for output to a peer, 492 * and then removing the data from the socket buffer with sbdrop() 493 * or sbdroprecord() when the data is acknowledged by the peer. 494 */ 495 496 /* 497 * Append mbuf chain m to the last record in the 498 * socket buffer sb. The additional space associated 499 * the mbuf chain is recorded in sb. Empty mbufs are 500 * discarded and mbufs are compacted where possible. 501 */ 502 void 503 sbappend(sb, m) 504 struct sockbuf *sb; 505 struct mbuf *m; 506 { 507 register struct mbuf *n; 508 509 if (m == 0) 510 return; 511 if ((n = sb->sb_mb) != NULL) { 512 while (n->m_nextpkt) 513 n = n->m_nextpkt; 514 do { 515 if (n->m_flags & M_EOR) { 516 sbappendrecord(sb, m); /* XXXXXX!!!! */ 517 return; 518 } 519 } while (n->m_next && (n = n->m_next)); 520 } 521 sbcompress(sb, m, n); 522 } 523 524 #ifdef SOCKBUF_DEBUG 525 void 526 sbcheck(sb) 527 register struct sockbuf *sb; 528 { 529 register struct mbuf *m; 530 register int len = 0, mbcnt = 0; 531 532 for (m = sb->sb_mb; m; m = m->m_next) { 533 len += m->m_len; 534 mbcnt += MSIZE; 535 if (m->m_flags & M_EXT) 536 mbcnt += m->m_ext.ext_size; 537 if (m->m_nextpkt) 538 panic("sbcheck nextpkt"); 539 } 540 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 541 printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc, 542 mbcnt, sb->sb_mbcnt); 543 panic("sbcheck"); 544 } 545 } 546 #endif 547 548 /* 549 * As above, except the mbuf chain 550 * begins a new record. 551 */ 552 void 553 sbappendrecord(sb, m0) 554 register struct sockbuf *sb; 555 register struct mbuf *m0; 556 { 557 register struct mbuf *m; 558 559 if (m0 == 0) 560 return; 561 if ((m = sb->sb_mb) != NULL) 562 while (m->m_nextpkt) 563 m = m->m_nextpkt; 564 /* 565 * Put the first mbuf on the queue. 566 * Note this permits zero length records. 567 */ 568 sballoc(sb, m0); 569 if (m) 570 m->m_nextpkt = m0; 571 else 572 sb->sb_mb = m0; 573 m = m0->m_next; 574 m0->m_next = 0; 575 if (m && (m0->m_flags & M_EOR)) { 576 m0->m_flags &= ~M_EOR; 577 m->m_flags |= M_EOR; 578 } 579 sbcompress(sb, m, m0); 580 } 581 582 /* 583 * As above except that OOB data 584 * is inserted at the beginning of the sockbuf, 585 * but after any other OOB data. 586 */ 587 void 588 sbinsertoob(sb, m0) 589 register struct sockbuf *sb; 590 register struct mbuf *m0; 591 { 592 register struct mbuf *m; 593 register struct mbuf **mp; 594 595 if (m0 == 0) 596 return; 597 for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) { 598 again: 599 switch (m->m_type) { 600 601 case MT_OOBDATA: 602 continue; /* WANT next train */ 603 604 case MT_CONTROL: 605 if ((m = m->m_next) != NULL) 606 goto again; /* inspect THIS train further */ 607 } 608 break; 609 } 610 /* 611 * Put the first mbuf on the queue. 612 * Note this permits zero length records. 613 */ 614 sballoc(sb, m0); 615 m0->m_nextpkt = *mp; 616 *mp = m0; 617 m = m0->m_next; 618 m0->m_next = 0; 619 if (m && (m0->m_flags & M_EOR)) { 620 m0->m_flags &= ~M_EOR; 621 m->m_flags |= M_EOR; 622 } 623 sbcompress(sb, m, m0); 624 } 625 626 /* 627 * Append address and data, and optionally, control (ancillary) data 628 * to the receive queue of a socket. If present, 629 * m0 must include a packet header with total length. 630 * Returns 0 if no space in sockbuf or insufficient mbufs. 631 */ 632 int 633 sbappendaddr(sb, asa, m0, control) 634 register struct sockbuf *sb; 635 struct sockaddr *asa; 636 struct mbuf *m0, *control; 637 { 638 register struct mbuf *m, *n; 639 int space = asa->sa_len; 640 641 if (m0 && (m0->m_flags & M_PKTHDR) == 0) 642 panic("sbappendaddr"); 643 if (m0) 644 space += m0->m_pkthdr.len; 645 for (n = control; n; n = n->m_next) { 646 space += n->m_len; 647 if (n->m_next == 0) /* keep pointer to last control buf */ 648 break; 649 } 650 if (space > sbspace(sb)) 651 return (0); 652 if (asa->sa_len > MLEN) 653 return (0); 654 MGET(m, M_DONTWAIT, MT_SONAME); 655 if (m == 0) 656 return (0); 657 m->m_len = asa->sa_len; 658 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); 659 if (n) 660 n->m_next = m0; /* concatenate data to control */ 661 else 662 control = m0; 663 m->m_next = control; 664 for (n = m; n; n = n->m_next) 665 sballoc(sb, n); 666 if ((n = sb->sb_mb) != NULL) { 667 while (n->m_nextpkt) 668 n = n->m_nextpkt; 669 n->m_nextpkt = m; 670 } else 671 sb->sb_mb = m; 672 return (1); 673 } 674 675 int 676 sbappendcontrol(sb, m0, control) 677 struct sockbuf *sb; 678 struct mbuf *m0, *control; 679 { 680 register struct mbuf *m, *n; 681 int space = 0; 682 683 if (control == 0) 684 panic("sbappendcontrol"); 685 for (m = control; ; m = m->m_next) { 686 space += m->m_len; 687 if (m->m_next == 0) 688 break; 689 } 690 n = m; /* save pointer to last control buffer */ 691 for (m = m0; m; m = m->m_next) 692 space += m->m_len; 693 if (space > sbspace(sb)) 694 return (0); 695 n->m_next = m0; /* concatenate data to control */ 696 for (m = control; m; m = m->m_next) 697 sballoc(sb, m); 698 if ((n = sb->sb_mb) != NULL) { 699 while (n->m_nextpkt) 700 n = n->m_nextpkt; 701 n->m_nextpkt = control; 702 } else 703 sb->sb_mb = control; 704 return (1); 705 } 706 707 /* 708 * Compress mbuf chain m into the socket 709 * buffer sb following mbuf n. If n 710 * is null, the buffer is presumed empty. 711 */ 712 void 713 sbcompress(sb, m, n) 714 register struct sockbuf *sb; 715 register struct mbuf *m, *n; 716 { 717 register int eor = 0; 718 register struct mbuf *o; 719 720 while (m) { 721 eor |= m->m_flags & M_EOR; 722 if (m->m_len == 0 && 723 (eor == 0 || 724 (((o = m->m_next) || (o = n)) && 725 o->m_type == m->m_type))) { 726 m = m_free(m); 727 continue; 728 } 729 if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 && 730 (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] && 731 n->m_type == m->m_type) { 732 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 733 (unsigned)m->m_len); 734 n->m_len += m->m_len; 735 sb->sb_cc += m->m_len; 736 m = m_free(m); 737 continue; 738 } 739 if (n) 740 n->m_next = m; 741 else 742 sb->sb_mb = m; 743 sballoc(sb, m); 744 n = m; 745 m->m_flags &= ~M_EOR; 746 m = m->m_next; 747 n->m_next = 0; 748 } 749 if (eor) { 750 if (n) 751 n->m_flags |= eor; 752 else 753 printf("semi-panic: sbcompress\n"); 754 } 755 } 756 757 /* 758 * Free all mbufs in a sockbuf. 759 * Check that all resources are reclaimed. 760 */ 761 void 762 sbflush(sb) 763 register struct sockbuf *sb; 764 { 765 766 if (sb->sb_flags & SB_LOCK) 767 panic("sbflush"); 768 while (sb->sb_mbcnt) 769 sbdrop(sb, (int)sb->sb_cc); 770 if (sb->sb_cc || sb->sb_mb) 771 panic("sbflush 2"); 772 } 773 774 /* 775 * Drop data from (the front of) a sockbuf. 776 */ 777 void 778 sbdrop(sb, len) 779 register struct sockbuf *sb; 780 register int len; 781 { 782 register struct mbuf *m, *mn; 783 struct mbuf *next; 784 785 next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 786 while (len > 0) { 787 if (m == 0) { 788 if (next == 0) 789 panic("sbdrop"); 790 m = next; 791 next = m->m_nextpkt; 792 continue; 793 } 794 if (m->m_len > len) { 795 m->m_len -= len; 796 m->m_data += len; 797 sb->sb_cc -= len; 798 break; 799 } 800 len -= m->m_len; 801 sbfree(sb, m); 802 MFREE(m, mn); 803 m = mn; 804 } 805 while (m && m->m_len == 0) { 806 sbfree(sb, m); 807 MFREE(m, mn); 808 m = mn; 809 } 810 if (m) { 811 sb->sb_mb = m; 812 m->m_nextpkt = next; 813 } else 814 sb->sb_mb = next; 815 } 816 817 /* 818 * Drop a record off the front of a sockbuf 819 * and move the next record to the front. 820 */ 821 void 822 sbdroprecord(sb) 823 register struct sockbuf *sb; 824 { 825 register struct mbuf *m, *mn; 826 827 m = sb->sb_mb; 828 if (m) { 829 sb->sb_mb = m->m_nextpkt; 830 do { 831 sbfree(sb, m); 832 MFREE(m, mn); 833 } while ((m = mn) != NULL); 834 } 835 } 836