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