1*17331Skarels /* uipc_socket2.c 6.5 84/11/02 */ 24903Swnj 317103Sbloom #include "param.h" 417103Sbloom #include "systm.h" 517103Sbloom #include "dir.h" 617103Sbloom #include "user.h" 717103Sbloom #include "proc.h" 817103Sbloom #include "file.h" 917103Sbloom #include "inode.h" 1017103Sbloom #include "buf.h" 1117103Sbloom #include "mbuf.h" 1217103Sbloom #include "protosw.h" 1317103Sbloom #include "socket.h" 1417103Sbloom #include "socketvar.h" 154903Swnj 164903Swnj /* 174903Swnj * Primitive routines for operating on sockets and socket buffers 184903Swnj */ 194903Swnj 204903Swnj /* 214903Swnj * Procedures to manipulate state flags of socket 227509Sroot * and do appropriate wakeups. Normal sequence from the 237509Sroot * active (originating) side is that soisconnecting() is 247509Sroot * called during processing of connect() call, 255169Swnj * resulting in an eventual call to soisconnected() if/when the 265169Swnj * connection is established. When the connection is torn down 275169Swnj * soisdisconnecting() is called during processing of disconnect() call, 285169Swnj * and soisdisconnected() is called when the connection to the peer 295169Swnj * is totally severed. The semantics of these routines are such that 305169Swnj * connectionless protocols can call soisconnected() and soisdisconnected() 315169Swnj * only, bypassing the in-progress calls when setting up a ``connection'' 325169Swnj * takes no time. 335169Swnj * 3412758Ssam * From the passive side, a socket is created with 3512758Ssam * two queues of sockets: so_q0 for connections in progress 367509Sroot * and so_q for connections already made and awaiting user acceptance. 377509Sroot * As a protocol is preparing incoming connections, it creates a socket 387509Sroot * structure queued on so_q0 by calling sonewconn(). When the connection 397509Sroot * is established, soisconnected() is called, and transfers the 407509Sroot * socket structure to so_q, making it available to accept(). 417509Sroot * 4212758Ssam * If a socket is closed with sockets on either 437509Sroot * so_q0 or so_q, these sockets are dropped. 447509Sroot * 4512758Ssam * If higher level protocols are implemented in 465169Swnj * the kernel, the wakeups done here will sometimes 4712758Ssam * cause software-interrupt process scheduling. 484903Swnj */ 495169Swnj 504903Swnj soisconnecting(so) 5112758Ssam register struct socket *so; 524903Swnj { 534903Swnj 544903Swnj so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 554903Swnj so->so_state |= SS_ISCONNECTING; 564903Swnj wakeup((caddr_t)&so->so_timeo); 574903Swnj } 584903Swnj 594903Swnj soisconnected(so) 6012758Ssam register struct socket *so; 614903Swnj { 627509Sroot register struct socket *head = so->so_head; 634903Swnj 647509Sroot if (head) { 657509Sroot if (soqremque(so, 0) == 0) 667509Sroot panic("soisconnected"); 677509Sroot soqinsque(head, so, 1); 6812758Ssam sorwakeup(head); 697509Sroot wakeup((caddr_t)&head->so_timeo); 707509Sroot } 714903Swnj so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING); 724903Swnj so->so_state |= SS_ISCONNECTED; 734903Swnj wakeup((caddr_t)&so->so_timeo); 745578Swnj sorwakeup(so); 755578Swnj sowwakeup(so); 764903Swnj } 774903Swnj 784903Swnj soisdisconnecting(so) 7912758Ssam register struct socket *so; 804903Swnj { 814903Swnj 825248Sroot so->so_state &= ~SS_ISCONNECTING; 834903Swnj so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 844903Swnj wakeup((caddr_t)&so->so_timeo); 855170Swnj sowwakeup(so); 865169Swnj sorwakeup(so); 874903Swnj } 884903Swnj 894903Swnj soisdisconnected(so) 9012758Ssam register struct socket *so; 914903Swnj { 924903Swnj 934903Swnj so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 944903Swnj so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE); 954903Swnj wakeup((caddr_t)&so->so_timeo); 964903Swnj sowwakeup(so); 974903Swnj sorwakeup(so); 984903Swnj } 994903Swnj 1005169Swnj /* 1017509Sroot * When an attempt at a new connection is noted on a socket 1027509Sroot * which accepts connections, sonewconn is called. If the 1037509Sroot * connection is possible (subject to space constraints, etc.) 1047509Sroot * then we allocate a new structure, propoerly linked into the 1057509Sroot * data structure of the original socket, and return this. 1067509Sroot */ 1077509Sroot struct socket * 1087509Sroot sonewconn(head) 1097509Sroot register struct socket *head; 1107509Sroot { 1117509Sroot register struct socket *so; 11212758Ssam register struct mbuf *m; 1137509Sroot 1147509Sroot if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2) 1157509Sroot goto bad; 1169636Ssam m = m_getclr(M_DONTWAIT, MT_SOCKET); 11710138Ssam if (m == NULL) 1187509Sroot goto bad; 1197509Sroot so = mtod(m, struct socket *); 1207509Sroot so->so_type = head->so_type; 1217509Sroot so->so_options = head->so_options &~ SO_ACCEPTCONN; 1227509Sroot so->so_linger = head->so_linger; 12310204Ssam so->so_state = head->so_state | SS_NOFDREF; 1247509Sroot so->so_proto = head->so_proto; 1257509Sroot so->so_timeo = head->so_timeo; 1267509Sroot so->so_pgrp = head->so_pgrp; 1277509Sroot soqinsque(head, so, 0); 12812758Ssam if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 12912758Ssam (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) { 1307509Sroot (void) soqremque(so, 0); 1318818Sroot (void) m_free(m); 1327509Sroot goto bad; 1337509Sroot } 1347509Sroot return (so); 1357509Sroot bad: 1367509Sroot return ((struct socket *)0); 1377509Sroot } 1387509Sroot 1397509Sroot soqinsque(head, so, q) 1407509Sroot register struct socket *head, *so; 1417509Sroot int q; 1427509Sroot { 1437509Sroot 1447509Sroot so->so_head = head; 1457509Sroot if (q == 0) { 1467509Sroot head->so_q0len++; 1477509Sroot so->so_q0 = head->so_q0; 1487509Sroot head->so_q0 = so; 1497509Sroot } else { 1507509Sroot head->so_qlen++; 1517509Sroot so->so_q = head->so_q; 1527509Sroot head->so_q = so; 1537509Sroot } 1547509Sroot } 1557509Sroot 1567509Sroot soqremque(so, q) 1577509Sroot register struct socket *so; 1587509Sroot int q; 1597509Sroot { 1607509Sroot register struct socket *head, *prev, *next; 1617509Sroot 1627509Sroot head = so->so_head; 1637509Sroot prev = head; 1647509Sroot for (;;) { 1657509Sroot next = q ? prev->so_q : prev->so_q0; 1667509Sroot if (next == so) 1677509Sroot break; 1687509Sroot if (next == head) 1697509Sroot return (0); 1707509Sroot prev = next; 1717509Sroot } 1727509Sroot if (q == 0) { 1737509Sroot prev->so_q0 = next->so_q0; 1747509Sroot head->so_q0len--; 1757509Sroot } else { 1767509Sroot prev->so_q = next->so_q; 1777509Sroot head->so_qlen--; 1787509Sroot } 1797509Sroot next->so_q0 = next->so_q = 0; 1807509Sroot next->so_head = 0; 1817509Sroot return (1); 1827509Sroot } 1837509Sroot 1847509Sroot /* 1855169Swnj * Socantsendmore indicates that no more data will be sent on the 1865169Swnj * socket; it would normally be applied to a socket when the user 1875169Swnj * informs the system that no more data is to be sent, by the protocol 1885169Swnj * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 1895169Swnj * will be received, and will normally be applied to the socket by a 1905169Swnj * protocol when it detects that the peer will send no more data. 1915169Swnj * Data queued for reading in the socket may yet be read. 1925169Swnj */ 1935169Swnj 1944917Swnj socantsendmore(so) 1954917Swnj struct socket *so; 1964917Swnj { 1974917Swnj 1984917Swnj so->so_state |= SS_CANTSENDMORE; 1994917Swnj sowwakeup(so); 2004917Swnj } 2014917Swnj 2024917Swnj socantrcvmore(so) 2034917Swnj struct socket *so; 2044917Swnj { 2054917Swnj 2064917Swnj so->so_state |= SS_CANTRCVMORE; 2074917Swnj sorwakeup(so); 2084917Swnj } 2094917Swnj 2104903Swnj /* 2115169Swnj * Socket select/wakeup routines. 2124903Swnj */ 2135169Swnj 2145169Swnj /* 2154903Swnj * Queue a process for a select on a socket buffer. 2164903Swnj */ 2174903Swnj sbselqueue(sb) 2184903Swnj struct sockbuf *sb; 2194903Swnj { 2204903Swnj register struct proc *p; 2214903Swnj 2224917Swnj if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait) 2234903Swnj sb->sb_flags |= SB_COLL; 2244903Swnj else 2254903Swnj sb->sb_sel = u.u_procp; 2264903Swnj } 2274903Swnj 2284903Swnj /* 2294917Swnj * Wait for data to arrive at/drain from a socket buffer. 2304917Swnj */ 2314917Swnj sbwait(sb) 2324917Swnj struct sockbuf *sb; 2334917Swnj { 2344917Swnj 2354917Swnj sb->sb_flags |= SB_WAIT; 2364917Swnj sleep((caddr_t)&sb->sb_cc, PZERO+1); 2374917Swnj } 2384917Swnj 2394917Swnj /* 2404903Swnj * Wakeup processes waiting on a socket buffer. 2414903Swnj */ 2424903Swnj sbwakeup(sb) 24312758Ssam register struct sockbuf *sb; 2444903Swnj { 2454903Swnj 2464903Swnj if (sb->sb_sel) { 2474903Swnj selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL); 2484903Swnj sb->sb_sel = 0; 2494903Swnj sb->sb_flags &= ~SB_COLL; 2504903Swnj } 2514903Swnj if (sb->sb_flags & SB_WAIT) { 2524903Swnj sb->sb_flags &= ~SB_WAIT; 2535013Swnj wakeup((caddr_t)&sb->sb_cc); 2544903Swnj } 2554903Swnj } 2564903Swnj 2574903Swnj /* 25815829Scooper * Wakeup socket readers and writers. 25915829Scooper * Do asynchronous notification via SIGIO 26015829Scooper * if the socket has the SS_ASYNC flag set. 26115829Scooper */ 26215829Scooper sowakeup(so, sb) 26315829Scooper register struct socket *so; 26415829Scooper struct sockbuf *sb; 26515829Scooper { 26615829Scooper register struct proc *p; 26715829Scooper 26815829Scooper sbwakeup(sb); 26915829Scooper if (so->so_state & SS_ASYNC) { 27015829Scooper if (so->so_pgrp == 0) 27115829Scooper return; 27215829Scooper else if (so->so_pgrp > 0) 27315829Scooper gsignal(so->so_pgrp, SIGIO); 27415829Scooper else if ((p = pfind(-so->so_pgrp)) != 0) 27515829Scooper psignal(p, SIGIO); 27615829Scooper } 27715829Scooper } 27815829Scooper 27915829Scooper /* 2805169Swnj * Socket buffer (struct sockbuf) utility routines. 2815169Swnj * 2825169Swnj * Each socket contains two socket buffers: one for sending data and 2835169Swnj * one for receiving data. Each buffer contains a queue of mbufs, 2845169Swnj * information about the number of mbufs and amount of data in the 2855169Swnj * queue, and other fields allowing select() statements and notification 2865169Swnj * on data availability to be implemented. 2875169Swnj * 28816994Skarels * Data stored in a socket buffer is maintained as a list of records. 28916994Skarels * Each record is a list of mbufs chained together with the m_next 29016994Skarels * field. Records are chained together with the m_act field. The upper 29116994Skarels * level routine soreceive() expects the following conventions to be 29216994Skarels * observed when placing information in the receive buffer: 29316994Skarels * 29416994Skarels * 1. If the protocol requires each message be preceded by the sender's 29516994Skarels * name, then a record containing that name must be present before 29616994Skarels * any associated data (mbuf's must be of type MT_SONAME). 29716994Skarels * 2. If the protocol supports the exchange of ``access rights'' (really 29816994Skarels * just additional data associated with the message), and there are 29916994Skarels * ``rights'' to be received, then a record containing this data 30016994Skarels * should be present (mbuf's must be of type MT_RIGHTS). 30116994Skarels * 3. If a name or rights record exists, then it must be followed by 30216994Skarels * a data record, perhaps of zero length. 30316994Skarels * 3045169Swnj * Before using a new socket structure it is first necessary to reserve 30516994Skarels * buffer space to the socket, by calling sbreserve(). This commits 3065169Swnj * some of the available buffer space in the system buffer pool for the 30716994Skarels * socket. The space should be released by calling sbrelease() when the 3085169Swnj * socket is destroyed. 3095169Swnj * 31016994Skarels * The routines sbappend() or sbappendrecord() are normally called to 31116994Skarels * append new mbufs to a socket buffer, after checking that adequate 31216994Skarels * space is available, comparing the function sbspace() with the amount 31316994Skarels * of data to be added. sbappendrecord() differs from sbappend() in 31416994Skarels * that data supplied is treated as the beginning of a new record. 3155169Swnj * Data is normally removed from a socket buffer in a protocol by 3165169Swnj * first calling m_copy on the socket buffer mbuf chain and sending this 3175169Swnj * to a peer, and then removing the data from the socket buffer with 31816994Skarels * sbdrop() or sbdroprecord() when the data is acknowledged by the peer 31916994Skarels * (or immediately in the case of unreliable protocols.) 3205169Swnj * 32116994Skarels * To place a sender's name, optionally, access rights, and data in a 32216994Skarels * socket buffer sbappendaddr() should be used. To place access rights 32316994Skarels * and data in a socket buffer sbappendrights() should be used. Note 32416994Skarels * that unlike sbappend() and sbappendrecord(), these routines check 3255169Swnj * for the caller that there will be enough space to store the data. 32616994Skarels * Each fails if there is not enough space, or if it cannot find mbufs 32716994Skarels * to store additional information in. 3285169Swnj */ 3295169Swnj 3309027Sroot soreserve(so, sndcc, rcvcc) 33112758Ssam register struct socket *so; 3329027Sroot int sndcc, rcvcc; 3339027Sroot { 3349027Sroot 3359027Sroot if (sbreserve(&so->so_snd, sndcc) == 0) 3369027Sroot goto bad; 3379027Sroot if (sbreserve(&so->so_rcv, rcvcc) == 0) 3389027Sroot goto bad2; 3399027Sroot return (0); 3409027Sroot bad2: 3419027Sroot sbrelease(&so->so_snd); 3429027Sroot bad: 3439027Sroot return (ENOBUFS); 3449027Sroot } 3459027Sroot 3465169Swnj /* 3474903Swnj * Allot mbufs to a sockbuf. 3484903Swnj */ 3494903Swnj sbreserve(sb, cc) 3504903Swnj struct sockbuf *sb; 3514903Swnj { 3524903Swnj 3537181Swnj /* someday maybe this routine will fail... */ 3544980Swnj sb->sb_hiwat = cc; 35512758Ssam /* * 2 implies names can be no more than 1 mbuf each */ 35612758Ssam sb->sb_mbmax = cc<<1; 3574917Swnj return (1); 3584903Swnj } 3594903Swnj 3604903Swnj /* 3614903Swnj * Free mbufs held by a socket, and reserved mbuf space. 3624903Swnj */ 3634903Swnj sbrelease(sb) 3644903Swnj struct sockbuf *sb; 3654903Swnj { 3664903Swnj 3674903Swnj sbflush(sb); 3684980Swnj sb->sb_hiwat = sb->sb_mbmax = 0; 3694903Swnj } 3704903Swnj 3714903Swnj /* 37216994Skarels * Routines to add and remove 37316994Skarels * data from an mbuf queue. 3744903Swnj */ 3754903Swnj 3764903Swnj /* 37716994Skarels * Append mbuf chain m to the last record in the 37816994Skarels * socket buffer sb. The additional space associated 37916994Skarels * the mbuf chain is recorded in sb. Empty mbufs are 38016994Skarels * discarded and mbufs are compacted where possible. 3814903Swnj */ 3824903Swnj sbappend(sb, m) 38316994Skarels struct sockbuf *sb; 38416994Skarels struct mbuf *m; 3854903Swnj { 3866092Sroot register struct mbuf *n; 3874903Swnj 38816994Skarels if (m == 0) 38916994Skarels return; 39016994Skarels if (n = sb->sb_mb) { 39116994Skarels while (n->m_act) 39216994Skarels n = n->m_act; 3936092Sroot while (n->m_next) 3946092Sroot n = n->m_next; 3954903Swnj } 39616994Skarels sbcompress(sb, m, n); 3974903Swnj } 3984903Swnj 3995169Swnj /* 40016994Skarels * As above, except the mbuf chain 40116994Skarels * begins a new record. 4025169Swnj */ 40316994Skarels sbappendrecord(sb, m0) 40416994Skarels register struct sockbuf *sb; 40516994Skarels register struct mbuf *m0; 4064928Swnj { 4074928Swnj register struct mbuf *m; 4084928Swnj 40916994Skarels if (m0 == 0) 41016994Skarels return; 41116994Skarels if (m = sb->sb_mb) 41216994Skarels while (m->m_act) 41316994Skarels m = m->m_act; 41416994Skarels /* 41516994Skarels * Put the first mbuf on the queue. 41616994Skarels * Note this permits zero length records. 41716994Skarels */ 41816994Skarels sballoc(sb, m0); 41916994Skarels if (m) 42016994Skarels m->m_act = m0; 42116994Skarels else 42216994Skarels sb->sb_mb = m0; 42316994Skarels m = m0->m_next; 42416994Skarels m0->m_next = 0; 42516994Skarels sbcompress(sb, m, m0); 42616994Skarels } 42716994Skarels 42816994Skarels /* 42916994Skarels * Append address and data, and optionally, rights 43016994Skarels * to the receive queue of a socket. Return 0 if 43116994Skarels * no space in sockbuf or insufficient mbufs. 43216994Skarels */ 43316994Skarels sbappendaddr(sb, asa, m0, rights0) /* XXX */ 43416994Skarels register struct sockbuf *sb; 43516994Skarels struct sockaddr *asa; 43616994Skarels struct mbuf *rights0, *m0; 43716994Skarels { 43816994Skarels register struct mbuf *m, *n; 43916994Skarels int space = sizeof (*asa); 44016994Skarels 4415042Swnj m = m0; 4425042Swnj if (m == 0) 4435042Swnj panic("sbappendaddr"); 44416994Skarels do { 44516994Skarels space += m->m_len; 4465042Swnj m = m->m_next; 44716994Skarels } while (m); 44816994Skarels if (rights0) 44916994Skarels space += rights0->m_len; 45016994Skarels if (space > sbspace(sb)) 4514928Swnj return (0); 4529636Ssam m = m_get(M_DONTWAIT, MT_SONAME); 45316994Skarels if (m == 0) 4544928Swnj return (0); 45512758Ssam *mtod(m, struct sockaddr *) = *asa; 45616994Skarels m->m_len = sizeof (*asa); 45716994Skarels if (rights0) { 45816994Skarels m->m_act = m_copy(rights0, 0, rights0->m_len); 45916994Skarels if (m->m_act == 0) { 46016994Skarels m_freem(m); 46116994Skarels return (0); 46216994Skarels } 46316994Skarels sballoc(sb, m); 46416994Skarels sballoc(sb, m->m_act); 46512758Ssam } else 46616994Skarels sballoc(sb, m); 46716994Skarels if (n = sb->sb_mb) { 46816994Skarels while (n->m_act) 46916994Skarels n = n->m_act; 47016994Skarels n->m_act = m; 47116994Skarels } else 47216994Skarels sb->sb_mb = m; 47316994Skarels if (m->m_act) 47416994Skarels m = m->m_act; 47516994Skarels sballoc(sb, m0); 47616994Skarels m->m_act = m0; 47716994Skarels m = m0->m_next; 47816994Skarels m0->m_next = 0; 47916994Skarels sbcompress(sb, m, m0); 48016994Skarels return (1); 48116994Skarels } 48216994Skarels 48316994Skarels #ifdef notdef 48416994Skarels sbappendrights(sb, rights, m0) 48516994Skarels struct sockbuf *sb; 48616994Skarels struct mbuf *rights, *m; 48716994Skarels { 48816994Skarels register struct mbuf *m, *n; 48916994Skarels int space = 0; 49016994Skarels 49116994Skarels m = m0; 49216994Skarels if (m == 0 || rights == 0) 49316994Skarels panic("sbappendrights"); 49416994Skarels do { 49516994Skarels space += m->m_len; 49616994Skarels m = m->m_next; 49716994Skarels } while (m); 49816994Skarels space += rights->m_len; 49916994Skarels if (space > sbspace(sb)) 50012758Ssam return (0); 50116994Skarels m = m_copy(rights, 0, rights->m_len); 50216994Skarels if (m == 0) 50316994Skarels return (0); 50416994Skarels sballoc(sb, m); 50516994Skarels if (n = sb->sb_mb) { 50616994Skarels while (n->m_act) 50716994Skarels n = n->m_act; 50816994Skarels n->m_act = m; 50916994Skarels } else 51016994Skarels n->m_act = m; 51116994Skarels sballoc(sb, m0); 51216994Skarels m->m_act = m0; 51316994Skarels m = m0->m_next; 51416994Skarels m0->m_next = 0; 51516994Skarels sbcompress(sb, m, m0); 5164928Swnj return (1); 5174928Swnj } 51816994Skarels #endif 5194928Swnj 5204903Swnj /* 52116994Skarels * Compress mbuf chain m into the socket 52216994Skarels * buffer sb following mbuf n. If n 52316994Skarels * is null, the buffer is presumed empty. 5244903Swnj */ 52516994Skarels sbcompress(sb, m, n) 52616994Skarels register struct sockbuf *sb; 52716994Skarels register struct mbuf *m, *n; 52816994Skarels { 52916994Skarels 53016994Skarels while (m) { 53116994Skarels if (m->m_len == 0) { 53216994Skarels m = m_free(m); 53316994Skarels continue; 53416994Skarels } 53516994Skarels if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF && 53616994Skarels (n->m_off + n->m_len + m->m_len) <= MMAXOFF) { 53716994Skarels bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 53816994Skarels (unsigned)m->m_len); 53916994Skarels n->m_len += m->m_len; 54016994Skarels sb->sb_cc += m->m_len; 54116994Skarels m = m_free(m); 54216994Skarels continue; 54316994Skarels } 54416994Skarels sballoc(sb, m); 54516994Skarels if (n) 54616994Skarels n->m_next = m; 54716994Skarels else 54816994Skarels sb->sb_mb = m; 54916994Skarels n = m; 55016994Skarels m = m->m_next; 55116994Skarels n->m_next = 0; 55216994Skarels } 55316994Skarels } 55416994Skarels 55516994Skarels /* 55616994Skarels * Free all mbufs in a sockbuf. 55716994Skarels * Check that all resources are reclaimed. 55816994Skarels */ 5594903Swnj sbflush(sb) 56012758Ssam register struct sockbuf *sb; 5614903Swnj { 5624903Swnj 5634903Swnj if (sb->sb_flags & SB_LOCK) 5644903Swnj panic("sbflush"); 5655266Swnj if (sb->sb_cc) 5665266Swnj sbdrop(sb, sb->sb_cc); 5674903Swnj if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb) 5684903Swnj panic("sbflush 2"); 5694903Swnj } 5704903Swnj 5714903Swnj /* 57216994Skarels * Drop data from (the front of) a sockbuf. 5734903Swnj */ 57416994Skarels struct mbuf * 5754903Swnj sbdrop(sb, len) 5764903Swnj register struct sockbuf *sb; 5774903Swnj register int len; 5784903Swnj { 57916994Skarels register struct mbuf *m, *mn; 58016994Skarels struct mbuf *next; 5814903Swnj 58216994Skarels next = (m = sb->sb_mb) ? m->m_act : 0; 5834903Swnj while (len > 0) { 58416994Skarels if (m == 0) { 58516994Skarels if (next == 0) 58616994Skarels panic("sbdrop"); 58716994Skarels m = next; 58816994Skarels next = m->m_act; 58916994Skarels continue; 59016994Skarels } 5915064Swnj if (m->m_len > len) { 5924903Swnj m->m_len -= len; 5934903Swnj m->m_off += len; 5944903Swnj sb->sb_cc -= len; 5954903Swnj break; 5964903Swnj } 5975064Swnj len -= m->m_len; 5985064Swnj sbfree(sb, m); 5995064Swnj MFREE(m, mn); 6005064Swnj m = mn; 6014903Swnj } 602*17331Skarels while (m && m->m_len == 0) { 603*17331Skarels MFREE(m, mn); 604*17331Skarels m = mn; 605*17331Skarels } 60616994Skarels if (m) { 60716994Skarels sb->sb_mb = m; 60816994Skarels m->m_act = next; 60916994Skarels } else 61016994Skarels sb->sb_mb = next; 61116994Skarels return (sb->sb_mb); 6124903Swnj } 61316994Skarels 61416994Skarels /* 61516994Skarels * Drop a record off the front of a sockbuf 61616994Skarels * and move the next record to the front. 61716994Skarels */ 61816994Skarels struct mbuf * 61916994Skarels sbdroprecord(sb) 62016994Skarels register struct sockbuf *sb; 62116994Skarels { 62216994Skarels register struct mbuf *m, *mn; 62316994Skarels 62416994Skarels m = sb->sb_mb; 62516994Skarels if (m) { 62616994Skarels sb->sb_mb = m->m_act; 62716994Skarels do { 62816994Skarels sbfree(sb, m); 62916994Skarels MFREE(m, mn); 63016994Skarels } while (m = mn); 63116994Skarels } 63216994Skarels return (sb->sb_mb); 63316994Skarels } 634