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