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