Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
0880b18e | 2 | * Copyright (c) 1982, 1986 Regents of the University of California. |
da7c5cc6 KM |
3 | * All rights reserved. The Berkeley software License Agreement |
4 | * specifies the terms and conditions for redistribution. | |
5 | * | |
0880b18e | 6 | * @(#)uipc_socket2.c 7.1 (Berkeley) %G% |
da7c5cc6 | 7 | */ |
681ebb17 | 8 | |
94368568 JB |
9 | #include "param.h" |
10 | #include "systm.h" | |
11 | #include "dir.h" | |
12 | #include "user.h" | |
13 | #include "proc.h" | |
14 | #include "file.h" | |
15 | #include "inode.h" | |
16 | #include "buf.h" | |
17 | #include "mbuf.h" | |
18 | #include "protosw.h" | |
19 | #include "socket.h" | |
20 | #include "socketvar.h" | |
681ebb17 BJ |
21 | |
22 | /* | |
23 | * Primitive routines for operating on sockets and socket buffers | |
24 | */ | |
25 | ||
26 | /* | |
27 | * Procedures to manipulate state flags of socket | |
2deddea9 BJ |
28 | * and do appropriate wakeups. Normal sequence from the |
29 | * active (originating) side is that soisconnecting() is | |
30 | * called during processing of connect() call, | |
4c078bb2 BJ |
31 | * resulting in an eventual call to soisconnected() if/when the |
32 | * connection is established. When the connection is torn down | |
33 | * soisdisconnecting() is called during processing of disconnect() call, | |
34 | * and soisdisconnected() is called when the connection to the peer | |
35 | * is totally severed. The semantics of these routines are such that | |
36 | * connectionless protocols can call soisconnected() and soisdisconnected() | |
37 | * only, bypassing the in-progress calls when setting up a ``connection'' | |
38 | * takes no time. | |
39 | * | |
88a7a62a SL |
40 | * From the passive side, a socket is created with |
41 | * two queues of sockets: so_q0 for connections in progress | |
2deddea9 BJ |
42 | * and so_q for connections already made and awaiting user acceptance. |
43 | * As a protocol is preparing incoming connections, it creates a socket | |
44 | * structure queued on so_q0 by calling sonewconn(). When the connection | |
45 | * is established, soisconnected() is called, and transfers the | |
46 | * socket structure to so_q, making it available to accept(). | |
47 | * | |
88a7a62a | 48 | * If a socket is closed with sockets on either |
2deddea9 BJ |
49 | * so_q0 or so_q, these sockets are dropped. |
50 | * | |
88a7a62a | 51 | * If higher level protocols are implemented in |
4c078bb2 | 52 | * the kernel, the wakeups done here will sometimes |
88a7a62a | 53 | * cause software-interrupt process scheduling. |
681ebb17 | 54 | */ |
4c078bb2 | 55 | |
681ebb17 | 56 | soisconnecting(so) |
88a7a62a | 57 | register struct socket *so; |
681ebb17 BJ |
58 | { |
59 | ||
60 | so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); | |
61 | so->so_state |= SS_ISCONNECTING; | |
62 | wakeup((caddr_t)&so->so_timeo); | |
63 | } | |
64 | ||
65 | soisconnected(so) | |
88a7a62a | 66 | register struct socket *so; |
681ebb17 | 67 | { |
2deddea9 | 68 | register struct socket *head = so->so_head; |
681ebb17 | 69 | |
2deddea9 BJ |
70 | if (head) { |
71 | if (soqremque(so, 0) == 0) | |
72 | panic("soisconnected"); | |
73 | soqinsque(head, so, 1); | |
ab303321 | 74 | sorwakeup(head); |
88a7a62a | 75 | wakeup((caddr_t)&head->so_timeo); |
2deddea9 | 76 | } |
681ebb17 BJ |
77 | so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING); |
78 | so->so_state |= SS_ISCONNECTED; | |
79 | wakeup((caddr_t)&so->so_timeo); | |
f957a49a BJ |
80 | sorwakeup(so); |
81 | sowwakeup(so); | |
681ebb17 BJ |
82 | } |
83 | ||
84 | soisdisconnecting(so) | |
88a7a62a | 85 | register struct socket *so; |
681ebb17 BJ |
86 | { |
87 | ||
72857acf | 88 | so->so_state &= ~SS_ISCONNECTING; |
681ebb17 BJ |
89 | so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); |
90 | wakeup((caddr_t)&so->so_timeo); | |
4c078bb2 | 91 | sowwakeup(so); |
b454c3ea | 92 | sorwakeup(so); |
681ebb17 BJ |
93 | } |
94 | ||
95 | soisdisconnected(so) | |
88a7a62a | 96 | register struct socket *so; |
681ebb17 BJ |
97 | { |
98 | ||
99 | so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); | |
100 | so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE); | |
101 | wakeup((caddr_t)&so->so_timeo); | |
102 | sowwakeup(so); | |
103 | sorwakeup(so); | |
104 | } | |
105 | ||
2deddea9 BJ |
106 | /* |
107 | * When an attempt at a new connection is noted on a socket | |
108 | * which accepts connections, sonewconn is called. If the | |
109 | * connection is possible (subject to space constraints, etc.) | |
110 | * then we allocate a new structure, propoerly linked into the | |
111 | * data structure of the original socket, and return this. | |
112 | */ | |
113 | struct socket * | |
114 | sonewconn(head) | |
115 | register struct socket *head; | |
116 | { | |
117 | register struct socket *so; | |
88a7a62a | 118 | register struct mbuf *m; |
2deddea9 BJ |
119 | |
120 | if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2) | |
121 | goto bad; | |
cce93e4b | 122 | m = m_getclr(M_DONTWAIT, MT_SOCKET); |
5fe6f9d1 | 123 | if (m == NULL) |
2deddea9 BJ |
124 | goto bad; |
125 | so = mtod(m, struct socket *); | |
126 | so->so_type = head->so_type; | |
127 | so->so_options = head->so_options &~ SO_ACCEPTCONN; | |
128 | so->so_linger = head->so_linger; | |
f7428e88 | 129 | so->so_state = head->so_state | SS_NOFDREF; |
2deddea9 BJ |
130 | so->so_proto = head->so_proto; |
131 | so->so_timeo = head->so_timeo; | |
132 | so->so_pgrp = head->so_pgrp; | |
133 | soqinsque(head, so, 0); | |
88a7a62a SL |
134 | if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, |
135 | (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) { | |
2deddea9 | 136 | (void) soqremque(so, 0); |
30c36259 | 137 | (void) m_free(m); |
2deddea9 BJ |
138 | goto bad; |
139 | } | |
140 | return (so); | |
141 | bad: | |
142 | return ((struct socket *)0); | |
143 | } | |
144 | ||
145 | soqinsque(head, so, q) | |
146 | register struct socket *head, *so; | |
147 | int q; | |
148 | { | |
149 | ||
150 | so->so_head = head; | |
151 | if (q == 0) { | |
152 | head->so_q0len++; | |
153 | so->so_q0 = head->so_q0; | |
154 | head->so_q0 = so; | |
155 | } else { | |
156 | head->so_qlen++; | |
157 | so->so_q = head->so_q; | |
158 | head->so_q = so; | |
159 | } | |
160 | } | |
161 | ||
162 | soqremque(so, q) | |
163 | register struct socket *so; | |
164 | int q; | |
165 | { | |
166 | register struct socket *head, *prev, *next; | |
167 | ||
168 | head = so->so_head; | |
169 | prev = head; | |
170 | for (;;) { | |
171 | next = q ? prev->so_q : prev->so_q0; | |
172 | if (next == so) | |
173 | break; | |
174 | if (next == head) | |
175 | return (0); | |
176 | prev = next; | |
177 | } | |
178 | if (q == 0) { | |
179 | prev->so_q0 = next->so_q0; | |
180 | head->so_q0len--; | |
181 | } else { | |
182 | prev->so_q = next->so_q; | |
183 | head->so_qlen--; | |
184 | } | |
185 | next->so_q0 = next->so_q = 0; | |
186 | next->so_head = 0; | |
187 | return (1); | |
188 | } | |
189 | ||
4c078bb2 BJ |
190 | /* |
191 | * Socantsendmore indicates that no more data will be sent on the | |
192 | * socket; it would normally be applied to a socket when the user | |
193 | * informs the system that no more data is to be sent, by the protocol | |
194 | * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data | |
195 | * will be received, and will normally be applied to the socket by a | |
196 | * protocol when it detects that the peer will send no more data. | |
197 | * Data queued for reading in the socket may yet be read. | |
198 | */ | |
199 | ||
ae921915 BJ |
200 | socantsendmore(so) |
201 | struct socket *so; | |
202 | { | |
203 | ||
204 | so->so_state |= SS_CANTSENDMORE; | |
205 | sowwakeup(so); | |
206 | } | |
207 | ||
208 | socantrcvmore(so) | |
209 | struct socket *so; | |
210 | { | |
211 | ||
212 | so->so_state |= SS_CANTRCVMORE; | |
213 | sorwakeup(so); | |
214 | } | |
215 | ||
681ebb17 | 216 | /* |
4c078bb2 BJ |
217 | * Socket select/wakeup routines. |
218 | */ | |
219 | ||
681ebb17 BJ |
220 | /* |
221 | * Queue a process for a select on a socket buffer. | |
222 | */ | |
223 | sbselqueue(sb) | |
224 | struct sockbuf *sb; | |
225 | { | |
226 | register struct proc *p; | |
227 | ||
ae921915 | 228 | if ((p = sb->sb_sel) && p->p_wchan == (caddr_t)&selwait) |
681ebb17 BJ |
229 | sb->sb_flags |= SB_COLL; |
230 | else | |
231 | sb->sb_sel = u.u_procp; | |
232 | } | |
233 | ||
ae921915 BJ |
234 | /* |
235 | * Wait for data to arrive at/drain from a socket buffer. | |
236 | */ | |
237 | sbwait(sb) | |
238 | struct sockbuf *sb; | |
239 | { | |
240 | ||
241 | sb->sb_flags |= SB_WAIT; | |
242 | sleep((caddr_t)&sb->sb_cc, PZERO+1); | |
243 | } | |
244 | ||
681ebb17 BJ |
245 | /* |
246 | * Wakeup processes waiting on a socket buffer. | |
247 | */ | |
248 | sbwakeup(sb) | |
88a7a62a | 249 | register struct sockbuf *sb; |
681ebb17 BJ |
250 | { |
251 | ||
252 | if (sb->sb_sel) { | |
253 | selwakeup(sb->sb_sel, sb->sb_flags & SB_COLL); | |
254 | sb->sb_sel = 0; | |
255 | sb->sb_flags &= ~SB_COLL; | |
256 | } | |
257 | if (sb->sb_flags & SB_WAIT) { | |
258 | sb->sb_flags &= ~SB_WAIT; | |
388ca8bd | 259 | wakeup((caddr_t)&sb->sb_cc); |
681ebb17 BJ |
260 | } |
261 | } | |
262 | ||
8f7109aa EC |
263 | /* |
264 | * Wakeup socket readers and writers. | |
265 | * Do asynchronous notification via SIGIO | |
266 | * if the socket has the SS_ASYNC flag set. | |
267 | */ | |
268 | sowakeup(so, sb) | |
269 | register struct socket *so; | |
270 | struct sockbuf *sb; | |
271 | { | |
272 | register struct proc *p; | |
273 | ||
274 | sbwakeup(sb); | |
275 | if (so->so_state & SS_ASYNC) { | |
3aefacbd MK |
276 | if (so->so_pgrp < 0) |
277 | gsignal(-so->so_pgrp, SIGIO); | |
278 | else if (so->so_pgrp > 0 && (p = pfind(so->so_pgrp)) != 0) | |
8f7109aa EC |
279 | psignal(p, SIGIO); |
280 | } | |
281 | } | |
282 | ||
4c078bb2 BJ |
283 | /* |
284 | * Socket buffer (struct sockbuf) utility routines. | |
285 | * | |
286 | * Each socket contains two socket buffers: one for sending data and | |
287 | * one for receiving data. Each buffer contains a queue of mbufs, | |
288 | * information about the number of mbufs and amount of data in the | |
289 | * queue, and other fields allowing select() statements and notification | |
290 | * on data availability to be implemented. | |
291 | * | |
261a8548 MK |
292 | * Data stored in a socket buffer is maintained as a list of records. |
293 | * Each record is a list of mbufs chained together with the m_next | |
294 | * field. Records are chained together with the m_act field. The upper | |
295 | * level routine soreceive() expects the following conventions to be | |
296 | * observed when placing information in the receive buffer: | |
297 | * | |
298 | * 1. If the protocol requires each message be preceded by the sender's | |
299 | * name, then a record containing that name must be present before | |
300 | * any associated data (mbuf's must be of type MT_SONAME). | |
301 | * 2. If the protocol supports the exchange of ``access rights'' (really | |
302 | * just additional data associated with the message), and there are | |
303 | * ``rights'' to be received, then a record containing this data | |
304 | * should be present (mbuf's must be of type MT_RIGHTS). | |
305 | * 3. If a name or rights record exists, then it must be followed by | |
306 | * a data record, perhaps of zero length. | |
307 | * | |
4c078bb2 | 308 | * Before using a new socket structure it is first necessary to reserve |
261a8548 | 309 | * buffer space to the socket, by calling sbreserve(). This commits |
4c078bb2 | 310 | * some of the available buffer space in the system buffer pool for the |
261a8548 | 311 | * socket. The space should be released by calling sbrelease() when the |
4c078bb2 | 312 | * socket is destroyed. |
4c078bb2 BJ |
313 | */ |
314 | ||
0e18ec4a | 315 | soreserve(so, sndcc, rcvcc) |
88a7a62a | 316 | register struct socket *so; |
0e18ec4a BJ |
317 | int sndcc, rcvcc; |
318 | { | |
319 | ||
320 | if (sbreserve(&so->so_snd, sndcc) == 0) | |
321 | goto bad; | |
322 | if (sbreserve(&so->so_rcv, rcvcc) == 0) | |
323 | goto bad2; | |
324 | return (0); | |
325 | bad2: | |
326 | sbrelease(&so->so_snd); | |
327 | bad: | |
328 | return (ENOBUFS); | |
329 | } | |
330 | ||
681ebb17 BJ |
331 | /* |
332 | * Allot mbufs to a sockbuf. | |
bbfd9898 MK |
333 | * Attempt to scale cc so that mbcnt doesn't become limiting |
334 | * if buffering efficiency is near the normal case. | |
681ebb17 BJ |
335 | */ |
336 | sbreserve(sb, cc) | |
337 | struct sockbuf *sb; | |
338 | { | |
339 | ||
bbfd9898 | 340 | if ((unsigned) cc > (unsigned)SB_MAX * CLBYTES / (2 * MSIZE + CLBYTES)) |
1ceef2d8 | 341 | return (0); |
d028a086 | 342 | sb->sb_hiwat = cc; |
453677da | 343 | sb->sb_mbmax = MIN(cc * 2, SB_MAX); |
ae921915 | 344 | return (1); |
681ebb17 BJ |
345 | } |
346 | ||
347 | /* | |
348 | * Free mbufs held by a socket, and reserved mbuf space. | |
349 | */ | |
350 | sbrelease(sb) | |
351 | struct sockbuf *sb; | |
352 | { | |
353 | ||
354 | sbflush(sb); | |
d028a086 | 355 | sb->sb_hiwat = sb->sb_mbmax = 0; |
681ebb17 BJ |
356 | } |
357 | ||
358 | /* | |
261a8548 MK |
359 | * Routines to add and remove |
360 | * data from an mbuf queue. | |
c34d38f4 MK |
361 | * |
362 | * The routines sbappend() or sbappendrecord() are normally called to | |
363 | * append new mbufs to a socket buffer, after checking that adequate | |
364 | * space is available, comparing the function sbspace() with the amount | |
365 | * of data to be added. sbappendrecord() differs from sbappend() in | |
366 | * that data supplied is treated as the beginning of a new record. | |
367 | * To place a sender's address, optional access rights, and data in a | |
368 | * socket receive buffer, sbappendaddr() should be used. To place | |
369 | * access rights and data in a socket receive buffer, sbappendrights() | |
370 | * should be used. In either case, the new data begins a new record. | |
371 | * Note that unlike sbappend() and sbappendrecord(), these routines check | |
372 | * for the caller that there will be enough space to store the data. | |
373 | * Each fails if there is not enough space, or if it cannot find mbufs | |
374 | * to store additional information in. | |
375 | * | |
376 | * Reliable protocols may use the socket send buffer to hold data | |
377 | * awaiting acknowledgement. Data is normally copied from a socket | |
378 | * send buffer in a protocol with m_copy for output to a peer, | |
379 | * and then removing the data from the socket buffer with sbdrop() | |
380 | * or sbdroprecord() when the data is acknowledged by the peer. | |
681ebb17 BJ |
381 | */ |
382 | ||
383 | /* | |
261a8548 MK |
384 | * Append mbuf chain m to the last record in the |
385 | * socket buffer sb. The additional space associated | |
386 | * the mbuf chain is recorded in sb. Empty mbufs are | |
387 | * discarded and mbufs are compacted where possible. | |
681ebb17 BJ |
388 | */ |
389 | sbappend(sb, m) | |
261a8548 MK |
390 | struct sockbuf *sb; |
391 | struct mbuf *m; | |
681ebb17 | 392 | { |
e495e1cc | 393 | register struct mbuf *n; |
681ebb17 | 394 | |
261a8548 MK |
395 | if (m == 0) |
396 | return; | |
397 | if (n = sb->sb_mb) { | |
398 | while (n->m_act) | |
399 | n = n->m_act; | |
e495e1cc BJ |
400 | while (n->m_next) |
401 | n = n->m_next; | |
681ebb17 | 402 | } |
261a8548 | 403 | sbcompress(sb, m, n); |
681ebb17 BJ |
404 | } |
405 | ||
4c078bb2 | 406 | /* |
261a8548 MK |
407 | * As above, except the mbuf chain |
408 | * begins a new record. | |
4c078bb2 | 409 | */ |
261a8548 MK |
410 | sbappendrecord(sb, m0) |
411 | register struct sockbuf *sb; | |
412 | register struct mbuf *m0; | |
2b4b57cd | 413 | { |
2b4b57cd | 414 | register struct mbuf *m; |
2b4b57cd | 415 | |
261a8548 MK |
416 | if (m0 == 0) |
417 | return; | |
418 | if (m = sb->sb_mb) | |
419 | while (m->m_act) | |
420 | m = m->m_act; | |
421 | /* | |
422 | * Put the first mbuf on the queue. | |
423 | * Note this permits zero length records. | |
424 | */ | |
425 | sballoc(sb, m0); | |
426 | if (m) | |
427 | m->m_act = m0; | |
428 | else | |
429 | sb->sb_mb = m0; | |
430 | m = m0->m_next; | |
431 | m0->m_next = 0; | |
432 | sbcompress(sb, m, m0); | |
433 | } | |
434 | ||
435 | /* | |
436 | * Append address and data, and optionally, rights | |
437 | * to the receive queue of a socket. Return 0 if | |
438 | * no space in sockbuf or insufficient mbufs. | |
439 | */ | |
c34d38f4 | 440 | sbappendaddr(sb, asa, m0, rights0) |
261a8548 MK |
441 | register struct sockbuf *sb; |
442 | struct sockaddr *asa; | |
443 | struct mbuf *rights0, *m0; | |
444 | { | |
445 | register struct mbuf *m, *n; | |
446 | int space = sizeof (*asa); | |
447 | ||
c34d38f4 | 448 | for (m = m0; m; m = m->m_next) |
261a8548 | 449 | space += m->m_len; |
261a8548 MK |
450 | if (rights0) |
451 | space += rights0->m_len; | |
452 | if (space > sbspace(sb)) | |
2b4b57cd | 453 | return (0); |
c34d38f4 | 454 | MGET(m, M_DONTWAIT, MT_SONAME); |
261a8548 | 455 | if (m == 0) |
2b4b57cd | 456 | return (0); |
88a7a62a | 457 | *mtod(m, struct sockaddr *) = *asa; |
261a8548 | 458 | m->m_len = sizeof (*asa); |
5aa99659 | 459 | if (rights0 && rights0->m_len) { |
c34d38f4 MK |
460 | m->m_next = m_copy(rights0, 0, rights0->m_len); |
461 | if (m->m_next == 0) { | |
261a8548 MK |
462 | m_freem(m); |
463 | return (0); | |
464 | } | |
c34d38f4 | 465 | sballoc(sb, m->m_next); |
4f8975e4 | 466 | } |
7b92dd2b | 467 | sballoc(sb, m); |
261a8548 MK |
468 | if (n = sb->sb_mb) { |
469 | while (n->m_act) | |
470 | n = n->m_act; | |
471 | n->m_act = m; | |
88a7a62a | 472 | } else |
261a8548 | 473 | sb->sb_mb = m; |
c34d38f4 MK |
474 | if (m->m_next) |
475 | m = m->m_next; | |
476 | if (m0) | |
477 | sbcompress(sb, m0, m); | |
261a8548 MK |
478 | return (1); |
479 | } | |
480 | ||
c34d38f4 | 481 | sbappendrights(sb, m0, rights) |
261a8548 | 482 | struct sockbuf *sb; |
c34d38f4 | 483 | struct mbuf *rights, *m0; |
261a8548 MK |
484 | { |
485 | register struct mbuf *m, *n; | |
486 | int space = 0; | |
487 | ||
c34d38f4 | 488 | if (rights == 0) |
261a8548 | 489 | panic("sbappendrights"); |
c34d38f4 | 490 | for (m = m0; m; m = m->m_next) |
261a8548 | 491 | space += m->m_len; |
261a8548 MK |
492 | space += rights->m_len; |
493 | if (space > sbspace(sb)) | |
88a7a62a | 494 | return (0); |
261a8548 MK |
495 | m = m_copy(rights, 0, rights->m_len); |
496 | if (m == 0) | |
497 | return (0); | |
498 | sballoc(sb, m); | |
499 | if (n = sb->sb_mb) { | |
500 | while (n->m_act) | |
501 | n = n->m_act; | |
502 | n->m_act = m; | |
503 | } else | |
c34d38f4 MK |
504 | sb->sb_mb = m; |
505 | if (m0) | |
506 | sbcompress(sb, m0, m); | |
2b4b57cd BJ |
507 | return (1); |
508 | } | |
261a8548 MK |
509 | |
510 | /* | |
511 | * Compress mbuf chain m into the socket | |
512 | * buffer sb following mbuf n. If n | |
513 | * is null, the buffer is presumed empty. | |
514 | */ | |
515 | sbcompress(sb, m, n) | |
516 | register struct sockbuf *sb; | |
517 | register struct mbuf *m, *n; | |
518 | { | |
519 | ||
520 | while (m) { | |
521 | if (m->m_len == 0) { | |
522 | m = m_free(m); | |
523 | continue; | |
524 | } | |
525 | if (n && n->m_off <= MMAXOFF && m->m_off <= MMAXOFF && | |
c34d38f4 MK |
526 | (n->m_off + n->m_len + m->m_len) <= MMAXOFF && |
527 | n->m_type == m->m_type) { | |
261a8548 MK |
528 | bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, |
529 | (unsigned)m->m_len); | |
530 | n->m_len += m->m_len; | |
531 | sb->sb_cc += m->m_len; | |
532 | m = m_free(m); | |
533 | continue; | |
534 | } | |
535 | sballoc(sb, m); | |
536 | if (n) | |
537 | n->m_next = m; | |
538 | else | |
539 | sb->sb_mb = m; | |
540 | n = m; | |
541 | m = m->m_next; | |
542 | n->m_next = 0; | |
543 | } | |
544 | } | |
2b4b57cd | 545 | |
681ebb17 | 546 | /* |
261a8548 MK |
547 | * Free all mbufs in a sockbuf. |
548 | * Check that all resources are reclaimed. | |
681ebb17 BJ |
549 | */ |
550 | sbflush(sb) | |
88a7a62a | 551 | register struct sockbuf *sb; |
681ebb17 BJ |
552 | { |
553 | ||
554 | if (sb->sb_flags & SB_LOCK) | |
555 | panic("sbflush"); | |
acb7839f | 556 | while (sb->sb_mbcnt) |
8011f5df | 557 | sbdrop(sb, (int)sb->sb_cc); |
681ebb17 BJ |
558 | if (sb->sb_cc || sb->sb_mbcnt || sb->sb_mb) |
559 | panic("sbflush 2"); | |
560 | } | |
561 | ||
562 | /* | |
261a8548 | 563 | * Drop data from (the front of) a sockbuf. |
681ebb17 BJ |
564 | */ |
565 | sbdrop(sb, len) | |
566 | register struct sockbuf *sb; | |
567 | register int len; | |
568 | { | |
261a8548 MK |
569 | register struct mbuf *m, *mn; |
570 | struct mbuf *next; | |
681ebb17 | 571 | |
261a8548 | 572 | next = (m = sb->sb_mb) ? m->m_act : 0; |
681ebb17 | 573 | while (len > 0) { |
261a8548 MK |
574 | if (m == 0) { |
575 | if (next == 0) | |
576 | panic("sbdrop"); | |
577 | m = next; | |
578 | next = m->m_act; | |
579 | continue; | |
580 | } | |
b9f0d37f | 581 | if (m->m_len > len) { |
681ebb17 BJ |
582 | m->m_len -= len; |
583 | m->m_off += len; | |
584 | sb->sb_cc -= len; | |
585 | break; | |
586 | } | |
b9f0d37f BJ |
587 | len -= m->m_len; |
588 | sbfree(sb, m); | |
589 | MFREE(m, mn); | |
590 | m = mn; | |
681ebb17 | 591 | } |
082e4f86 | 592 | while (m && m->m_len == 0) { |
453677da | 593 | sbfree(sb, m); |
082e4f86 MK |
594 | MFREE(m, mn); |
595 | m = mn; | |
596 | } | |
261a8548 MK |
597 | if (m) { |
598 | sb->sb_mb = m; | |
599 | m->m_act = next; | |
600 | } else | |
601 | sb->sb_mb = next; | |
261a8548 MK |
602 | } |
603 | ||
604 | /* | |
605 | * Drop a record off the front of a sockbuf | |
606 | * and move the next record to the front. | |
607 | */ | |
261a8548 MK |
608 | sbdroprecord(sb) |
609 | register struct sockbuf *sb; | |
610 | { | |
611 | register struct mbuf *m, *mn; | |
612 | ||
613 | m = sb->sb_mb; | |
614 | if (m) { | |
615 | sb->sb_mb = m->m_act; | |
616 | do { | |
617 | sbfree(sb, m); | |
618 | MFREE(m, mn); | |
619 | } while (m = mn); | |
620 | } | |
681ebb17 | 621 | } |