1 /* 2 * Copyright (c) 1982, 1986, 1990 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted provided 6 * that: (1) source distributions retain this entire copyright notice and 7 * comment, and (2) distributions including binaries display the following 8 * acknowledgement: ``This product includes software developed by the 9 * University of California, Berkeley and its contributors'' in the 10 * documentation or other materials provided with the distribution and in 11 * all advertising materials mentioning features or use of this software. 12 * Neither the name of the University nor the names of its contributors may 13 * be used to endorse or promote products derived from this software without 14 * specific prior written permission. 15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 16 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 18 * 19 * @(#)socketvar.h 7.13 (Berkeley) 03/17/91 20 */ 21 22 /* 23 * Kernel structure per socket. 24 * Contains send and receive buffer queues, 25 * handle on protocol and pointer to protocol 26 * private data and error information. 27 */ 28 struct socket { 29 short so_type; /* generic type, see socket.h */ 30 short so_options; /* from socket call, see socket.h */ 31 short so_linger; /* time to linger while closing */ 32 short so_state; /* internal state flags SS_*, below */ 33 caddr_t so_pcb; /* protocol control block */ 34 struct protosw *so_proto; /* protocol handle */ 35 /* 36 * Variables for connection queueing. 37 * Socket where accepts occur is so_head in all subsidiary sockets. 38 * If so_head is 0, socket is not related to an accept. 39 * For head socket so_q0 queues partially completed connections, 40 * while so_q is a queue of connections ready to be accepted. 41 * If a connection is aborted and it has so_head set, then 42 * it has to be pulled out of either so_q0 or so_q. 43 * We allow connections to queue up based on current queue lengths 44 * and limit on number of queued connections for this socket. 45 */ 46 struct socket *so_head; /* back pointer to accept socket */ 47 struct socket *so_q0; /* queue of partial connections */ 48 struct socket *so_q; /* queue of incoming connections */ 49 short so_q0len; /* partials on so_q0 */ 50 short so_qlen; /* number of connections on so_q */ 51 short so_qlimit; /* max number queued connections */ 52 short so_timeo; /* connection timeout */ 53 u_short so_error; /* error affecting connection */ 54 pid_t so_pgid; /* pgid for signals */ 55 u_long so_oobmark; /* chars to oob mark */ 56 /* 57 * Variables for socket buffering. 58 */ 59 struct sockbuf { 60 u_long sb_cc; /* actual chars in buffer */ 61 u_long sb_hiwat; /* max actual char count */ 62 u_long sb_mbcnt; /* chars of mbufs used */ 63 u_long sb_mbmax; /* max chars of mbufs to use */ 64 long sb_lowat; /* low water mark */ 65 struct mbuf *sb_mb; /* the mbuf chain */ 66 struct proc *sb_sel; /* process selecting read/write */ 67 short sb_flags; /* flags, see below */ 68 short sb_timeo; /* timeout for read/write */ 69 } so_rcv, so_snd; 70 #define SB_MAX (64*1024) /* default for max chars in sockbuf */ 71 #define SB_LOCK 0x01 /* lock on data queue */ 72 #define SB_WANT 0x02 /* someone is waiting to lock */ 73 #define SB_WAIT 0x04 /* someone is waiting for data/space */ 74 #define SB_SEL 0x08 /* someone is selecting */ 75 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 76 #define SB_NOTIFY (SB_WAIT|SB_SEL|SB_ASYNC) 77 #define SB_COLL 0x20 /* collision selecting */ 78 #define SB_NOINTR 0x40 /* operations not interruptible */ 79 80 caddr_t so_tpcb; /* Wisc. protocol control block XXX */ 81 }; 82 83 /* 84 * Socket state bits. 85 */ 86 #define SS_NOFDREF 0x001 /* no file table ref any more */ 87 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */ 88 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */ 89 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */ 90 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */ 91 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */ 92 #define SS_RCVATMARK 0x040 /* at mark on input */ 93 94 #define SS_PRIV 0x080 /* privileged for broadcast, raw... */ 95 #define SS_NBIO 0x100 /* non-blocking ops */ 96 #define SS_ASYNC 0x200 /* async i/o notify */ 97 #define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */ 98 99 100 /* 101 * Macros for sockets and socket buffering. 102 */ 103 104 /* 105 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 106 * This is problematical if the fields are unsigned, as the space might 107 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 108 * overflow and return 0. Should use "lmin" but it doesn't exist now. 109 */ 110 #define sbspace(sb) \ 111 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \ 112 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 113 114 /* do we have to send all at once on a socket? */ 115 #define sosendallatonce(so) \ 116 ((so)->so_proto->pr_flags & PR_ATOMIC) 117 118 /* can we read something from so? */ 119 #define soreadable(so) \ 120 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 121 ((so)->so_state & SS_CANTRCVMORE) || \ 122 (so)->so_qlen || (so)->so_error) 123 124 /* can we write something to so? */ 125 #define sowriteable(so) \ 126 (sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 127 (((so)->so_state&SS_ISCONNECTED) || \ 128 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0) || \ 129 ((so)->so_state & SS_CANTSENDMORE) || \ 130 (so)->so_error) 131 132 /* adjust counters in sb reflecting allocation of m */ 133 #define sballoc(sb, m) { \ 134 (sb)->sb_cc += (m)->m_len; \ 135 (sb)->sb_mbcnt += MSIZE; \ 136 if ((m)->m_flags & M_EXT) \ 137 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 138 } 139 140 /* adjust counters in sb reflecting freeing of m */ 141 #define sbfree(sb, m) { \ 142 (sb)->sb_cc -= (m)->m_len; \ 143 (sb)->sb_mbcnt -= MSIZE; \ 144 if ((m)->m_flags & M_EXT) \ 145 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 146 } 147 148 /* 149 * Set lock on sockbuf sb; sleep if lock is already held. 150 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. 151 * Returns error without lock if sleep is interrupted. 152 */ 153 #define sblock(sb) ((sb)->sb_flags & SB_LOCK ? sb_lock(sb) : \ 154 ((sb)->sb_flags |= SB_LOCK, 0)) 155 156 /* release lock on sockbuf sb */ 157 #define sbunlock(sb) { \ 158 (sb)->sb_flags &= ~SB_LOCK; \ 159 if ((sb)->sb_flags & SB_WANT) { \ 160 (sb)->sb_flags &= ~SB_WANT; \ 161 wakeup((caddr_t)&(sb)->sb_flags); \ 162 } \ 163 } 164 165 #define sorwakeup(so) sowakeup((so), &(so)->so_rcv) 166 #define sowwakeup(so) sowakeup((so), &(so)->so_snd) 167 168 #ifdef KERNEL 169 u_long sb_max; 170 /* to catch callers missing new second argument to sonewconn: */ 171 #define sonewconn(head, connstatus) sonewconn1((head), (connstatus)) 172 struct socket *sonewconn1(); 173 174 /* strings for sleep message: */ 175 extern char netio[], netcon[], netcls[]; 176 #endif 177