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