1*17355Skarels /* uipc_socket2.c 6.6 84/11/14 */ 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 353*17355Skarels if ((unsigned) cc > SB_MAX) 354*17355Skarels return (0); 3557181Swnj /* someday maybe this routine will fail... */ 3564980Swnj sb->sb_hiwat = cc; 35712758Ssam /* * 2 implies names can be no more than 1 mbuf each */ 358*17355Skarels sb->sb_mbmax = MAX(cc * 2, SB_MAX); 3594917Swnj return (1); 3604903Swnj } 3614903Swnj 3624903Swnj /* 3634903Swnj * Free mbufs held by a socket, and reserved mbuf space. 3644903Swnj */ 3654903Swnj sbrelease(sb) 3664903Swnj struct sockbuf *sb; 3674903Swnj { 3684903Swnj 3694903Swnj sbflush(sb); 3704980Swnj sb->sb_hiwat = sb->sb_mbmax = 0; 3714903Swnj } 3724903Swnj 3734903Swnj /* 37416994Skarels * Routines to add and remove 37516994Skarels * data from an mbuf queue. 3764903Swnj */ 3774903Swnj 3784903Swnj /* 37916994Skarels * Append mbuf chain m to the last record in the 38016994Skarels * socket buffer sb. The additional space associated 38116994Skarels * the mbuf chain is recorded in sb. Empty mbufs are 38216994Skarels * discarded and mbufs are compacted where possible. 3834903Swnj */ 3844903Swnj sbappend(sb, m) 38516994Skarels struct sockbuf *sb; 38616994Skarels struct mbuf *m; 3874903Swnj { 3886092Sroot register struct mbuf *n; 3894903Swnj 39016994Skarels if (m == 0) 39116994Skarels return; 39216994Skarels if (n = sb->sb_mb) { 39316994Skarels while (n->m_act) 39416994Skarels n = n->m_act; 3956092Sroot while (n->m_next) 3966092Sroot n = n->m_next; 3974903Swnj } 39816994Skarels sbcompress(sb, m, n); 3994903Swnj } 4004903Swnj 4015169Swnj /* 40216994Skarels * As above, except the mbuf chain 40316994Skarels * begins a new record. 4045169Swnj */ 40516994Skarels sbappendrecord(sb, m0) 40616994Skarels register struct sockbuf *sb; 40716994Skarels register struct mbuf *m0; 4084928Swnj { 4094928Swnj register struct mbuf *m; 4104928Swnj 41116994Skarels if (m0 == 0) 41216994Skarels return; 41316994Skarels if (m = sb->sb_mb) 41416994Skarels while (m->m_act) 41516994Skarels m = m->m_act; 41616994Skarels /* 41716994Skarels * Put the first mbuf on the queue. 41816994Skarels * Note this permits zero length records. 41916994Skarels */ 42016994Skarels sballoc(sb, m0); 42116994Skarels if (m) 42216994Skarels m->m_act = m0; 42316994Skarels else 42416994Skarels sb->sb_mb = m0; 42516994Skarels m = m0->m_next; 42616994Skarels m0->m_next = 0; 42716994Skarels sbcompress(sb, m, m0); 42816994Skarels } 42916994Skarels 43016994Skarels /* 43116994Skarels * Append address and data, and optionally, rights 43216994Skarels * to the receive queue of a socket. Return 0 if 43316994Skarels * no space in sockbuf or insufficient mbufs. 43416994Skarels */ 43516994Skarels sbappendaddr(sb, asa, m0, rights0) /* XXX */ 43616994Skarels register struct sockbuf *sb; 43716994Skarels struct sockaddr *asa; 43816994Skarels struct mbuf *rights0, *m0; 43916994Skarels { 44016994Skarels register struct mbuf *m, *n; 44116994Skarels int space = sizeof (*asa); 44216994Skarels 4435042Swnj m = m0; 4445042Swnj if (m == 0) 4455042Swnj panic("sbappendaddr"); 44616994Skarels do { 44716994Skarels space += m->m_len; 4485042Swnj m = m->m_next; 44916994Skarels } while (m); 45016994Skarels if (rights0) 45116994Skarels space += rights0->m_len; 45216994Skarels if (space > sbspace(sb)) 4534928Swnj return (0); 4549636Ssam m = m_get(M_DONTWAIT, MT_SONAME); 45516994Skarels if (m == 0) 4564928Swnj return (0); 45712758Ssam *mtod(m, struct sockaddr *) = *asa; 45816994Skarels m->m_len = sizeof (*asa); 45916994Skarels if (rights0) { 46016994Skarels m->m_act = m_copy(rights0, 0, rights0->m_len); 46116994Skarels if (m->m_act == 0) { 46216994Skarels m_freem(m); 46316994Skarels return (0); 46416994Skarels } 46516994Skarels sballoc(sb, m); 46616994Skarels sballoc(sb, m->m_act); 46712758Ssam } else 46816994Skarels sballoc(sb, m); 46916994Skarels if (n = sb->sb_mb) { 47016994Skarels while (n->m_act) 47116994Skarels n = n->m_act; 47216994Skarels n->m_act = m; 47316994Skarels } else 47416994Skarels sb->sb_mb = m; 47516994Skarels if (m->m_act) 47616994Skarels m = m->m_act; 47716994Skarels sballoc(sb, m0); 47816994Skarels m->m_act = m0; 47916994Skarels m = m0->m_next; 48016994Skarels m0->m_next = 0; 48116994Skarels sbcompress(sb, m, m0); 48216994Skarels return (1); 48316994Skarels } 48416994Skarels 48516994Skarels #ifdef notdef 48616994Skarels sbappendrights(sb, rights, m0) 48716994Skarels struct sockbuf *sb; 48816994Skarels struct mbuf *rights, *m; 48916994Skarels { 49016994Skarels register struct mbuf *m, *n; 49116994Skarels int space = 0; 49216994Skarels 49316994Skarels m = m0; 49416994Skarels if (m == 0 || rights == 0) 49516994Skarels panic("sbappendrights"); 49616994Skarels do { 49716994Skarels space += m->m_len; 49816994Skarels m = m->m_next; 49916994Skarels } while (m); 50016994Skarels space += rights->m_len; 50116994Skarels if (space > sbspace(sb)) 50212758Ssam return (0); 50316994Skarels m = m_copy(rights, 0, rights->m_len); 50416994Skarels if (m == 0) 50516994Skarels return (0); 50616994Skarels sballoc(sb, m); 50716994Skarels if (n = sb->sb_mb) { 50816994Skarels while (n->m_act) 50916994Skarels n = n->m_act; 51016994Skarels n->m_act = m; 51116994Skarels } else 51216994Skarels n->m_act = m; 51316994Skarels sballoc(sb, m0); 51416994Skarels m->m_act = m0; 51516994Skarels m = m0->m_next; 51616994Skarels m0->m_next = 0; 51716994Skarels sbcompress(sb, m, m0); 5184928Swnj return (1); 5194928Swnj } 52016994Skarels #endif 5214928Swnj 5224903Swnj /* 52316994Skarels * Compress mbuf chain m into the socket 52416994Skarels * buffer sb following mbuf n. If n 52516994Skarels * is null, the buffer is presumed empty. 5264903Swnj */ 52716994Skarels sbcompress(sb, m, n) 52816994Skarels register struct sockbuf *sb; 52916994Skarels register struct mbuf *m, *n; 53016994Skarels { 53116994Skarels 53216994Skarels while (m) { 53316994Skarels if (m->m_len == 0) { 53416994Skarels m = m_free(m); 53516994Skarels continue; 53616994Skarels } 53716994Skarels if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF && 53816994Skarels (n->m_off + n->m_len + m->m_len) <= MMAXOFF) { 53916994Skarels bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 54016994Skarels (unsigned)m->m_len); 54116994Skarels n->m_len += m->m_len; 54216994Skarels sb->sb_cc += m->m_len; 54316994Skarels m = m_free(m); 54416994Skarels continue; 54516994Skarels } 54616994Skarels sballoc(sb, m); 54716994Skarels if (n) 54816994Skarels n->m_next = m; 54916994Skarels else 55016994Skarels sb->sb_mb = m; 55116994Skarels n = m; 55216994Skarels m = m->m_next; 55316994Skarels n->m_next = 0; 55416994Skarels } 55516994Skarels } 55616994Skarels 55716994Skarels /* 55816994Skarels * Free all mbufs in a sockbuf. 55916994Skarels * Check that all resources are reclaimed. 56016994Skarels */ 5614903Swnj sbflush(sb) 56212758Ssam register struct sockbuf *sb; 5634903Swnj { 5644903Swnj 5654903Swnj if (sb->sb_flags & SB_LOCK) 5664903Swnj panic("sbflush"); 5675266Swnj if (sb->sb_cc) 5685266Swnj sbdrop(sb, sb->sb_cc); 5694903Swnj if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb) 5704903Swnj panic("sbflush 2"); 5714903Swnj } 5724903Swnj 5734903Swnj /* 57416994Skarels * Drop data from (the front of) a sockbuf. 5754903Swnj */ 57616994Skarels struct mbuf * 5774903Swnj sbdrop(sb, len) 5784903Swnj register struct sockbuf *sb; 5794903Swnj register int len; 5804903Swnj { 58116994Skarels register struct mbuf *m, *mn; 58216994Skarels struct mbuf *next; 5834903Swnj 58416994Skarels next = (m = sb->sb_mb) ? m->m_act : 0; 5854903Swnj while (len > 0) { 58616994Skarels if (m == 0) { 58716994Skarels if (next == 0) 58816994Skarels panic("sbdrop"); 58916994Skarels m = next; 59016994Skarels next = m->m_act; 59116994Skarels continue; 59216994Skarels } 5935064Swnj if (m->m_len > len) { 5944903Swnj m->m_len -= len; 5954903Swnj m->m_off += len; 5964903Swnj sb->sb_cc -= len; 5974903Swnj break; 5984903Swnj } 5995064Swnj len -= m->m_len; 6005064Swnj sbfree(sb, m); 6015064Swnj MFREE(m, mn); 6025064Swnj m = mn; 6034903Swnj } 60417331Skarels while (m && m->m_len == 0) { 60517331Skarels MFREE(m, mn); 60617331Skarels m = mn; 60717331Skarels } 60816994Skarels if (m) { 60916994Skarels sb->sb_mb = m; 61016994Skarels m->m_act = next; 61116994Skarels } else 61216994Skarels sb->sb_mb = next; 61316994Skarels return (sb->sb_mb); 6144903Swnj } 61516994Skarels 61616994Skarels /* 61716994Skarels * Drop a record off the front of a sockbuf 61816994Skarels * and move the next record to the front. 61916994Skarels */ 62016994Skarels struct mbuf * 62116994Skarels sbdroprecord(sb) 62216994Skarels register struct sockbuf *sb; 62316994Skarels { 62416994Skarels register struct mbuf *m, *mn; 62516994Skarels 62616994Skarels m = sb->sb_mb; 62716994Skarels if (m) { 62816994Skarels sb->sb_mb = m->m_act; 62916994Skarels do { 63016994Skarels sbfree(sb, m); 63116994Skarels MFREE(m, mn); 63216994Skarels } while (m = mn); 63316994Skarels } 63416994Skarels return (sb->sb_mb); 63516994Skarels } 636