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