1 /* 2 * Copyright (c) 1982, 1986 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 this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 * 12 * @(#)socketvar.h 7.3 (Berkeley) 12/30/87 13 */ 14 15 /* 16 * Kernel structure per socket. 17 * Contains send and receive buffer queues, 18 * handle on protocol and pointer to protocol 19 * private data and error information. 20 */ 21 struct socket { 22 short so_type; /* generic type, see socket.h */ 23 short so_options; /* from socket call, see socket.h */ 24 short so_linger; /* time to linger while closing */ 25 short so_state; /* internal state flags SS_*, below */ 26 caddr_t so_pcb; /* protocol control block */ 27 struct protosw *so_proto; /* protocol handle */ 28 /* 29 * Variables for connection queueing. 30 * Socket where accepts occur is so_head in all subsidiary sockets. 31 * If so_head is 0, socket is not related to an accept. 32 * For head socket so_q0 queues partially completed connections, 33 * while so_q is a queue of connections ready to be accepted. 34 * If a connection is aborted and it has so_head set, then 35 * it has to be pulled out of either so_q0 or so_q. 36 * We allow connections to queue up based on current queue lengths 37 * and limit on number of queued connections for this socket. 38 */ 39 struct socket *so_head; /* back pointer to accept socket */ 40 struct socket *so_q0; /* queue of partial connections */ 41 struct socket *so_q; /* queue of incoming connections */ 42 short so_q0len; /* partials on so_q0 */ 43 short so_qlen; /* number of connections on so_q */ 44 short so_qlimit; /* max number queued connections */ 45 short so_timeo; /* connection timeout */ 46 u_short so_error; /* error affecting connection */ 47 short so_pgrp; /* pgrp for signals */ 48 u_long so_oobmark; /* chars to oob mark */ 49 /* 50 * Variables for socket buffering. 51 */ 52 struct sockbuf { 53 u_long sb_cc; /* actual chars in buffer */ 54 u_long sb_hiwat; /* max actual char count */ 55 u_long sb_mbcnt; /* chars of mbufs used */ 56 u_long sb_mbmax; /* max chars of mbufs to use */ 57 u_long sb_lowat; /* low water mark (not used yet) */ 58 struct mbuf *sb_mb; /* the mbuf chain */ 59 struct proc *sb_sel; /* process selecting read/write */ 60 short sb_timeo; /* timeout (not used yet) */ 61 short sb_flags; /* flags, see below */ 62 } so_rcv, so_snd; 63 #define SB_MAX (64*1024) /* max chars in sockbuf */ 64 #define SB_LOCK 0x01 /* lock on data queue (so_rcv only) */ 65 #define SB_WANT 0x02 /* someone is waiting to lock */ 66 #define SB_WAIT 0x04 /* someone is waiting for data/space */ 67 #define SB_SEL 0x08 /* buffer is selected */ 68 #define SB_COLL 0x10 /* collision selecting */ 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 86 87 /* 88 * Macros for sockets and socket buffering. 89 */ 90 91 /* how much space is there in a socket buffer (so->so_snd or so->so_rcv) */ 92 #define sbspace(sb) \ 93 (MIN((long)((sb)->sb_hiwat - (sb)->sb_cc),\ 94 (long)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 95 96 /* do we have to send all at once on a socket? */ 97 #define sosendallatonce(so) \ 98 ((so)->so_proto->pr_flags & PR_ATOMIC) 99 100 /* can we read something from so? */ 101 #define soreadable(so) \ 102 ((so)->so_rcv.sb_cc || ((so)->so_state & SS_CANTRCVMORE) || \ 103 (so)->so_qlen || (so)->so_error) 104 105 /* can we write something to so? */ 106 #define sowriteable(so) \ 107 (sbspace(&(so)->so_snd) > 0 && \ 108 (((so)->so_state&SS_ISCONNECTED) || \ 109 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0) || \ 110 ((so)->so_state & SS_CANTSENDMORE) || \ 111 (so)->so_error) 112 113 /* adjust counters in sb reflecting allocation of m */ 114 #define sballoc(sb, m) { \ 115 (sb)->sb_cc += (m)->m_len; \ 116 (sb)->sb_mbcnt += MSIZE; \ 117 if ((m)->m_off > MMAXOFF) \ 118 (sb)->sb_mbcnt += CLBYTES; \ 119 } 120 121 /* adjust counters in sb reflecting freeing of m */ 122 #define sbfree(sb, m) { \ 123 (sb)->sb_cc -= (m)->m_len; \ 124 (sb)->sb_mbcnt -= MSIZE; \ 125 if ((m)->m_off > MMAXOFF) \ 126 (sb)->sb_mbcnt -= CLBYTES; \ 127 } 128 129 /* set lock on sockbuf sb */ 130 #define sblock(sb) { \ 131 while ((sb)->sb_flags & SB_LOCK) { \ 132 (sb)->sb_flags |= SB_WANT; \ 133 sleep((caddr_t)&(sb)->sb_flags, PZERO+1); \ 134 } \ 135 (sb)->sb_flags |= SB_LOCK; \ 136 } 137 138 /* release lock on sockbuf sb */ 139 #define sbunlock(sb) { \ 140 (sb)->sb_flags &= ~SB_LOCK; \ 141 if ((sb)->sb_flags & SB_WANT) { \ 142 (sb)->sb_flags &= ~SB_WANT; \ 143 wakeup((caddr_t)&(sb)->sb_flags); \ 144 } \ 145 } 146 147 #define sorwakeup(so) sowakeup((so), &(so)->so_rcv) 148 #define sowwakeup(so) sowakeup((so), &(so)->so_snd) 149 150 #ifdef KERNEL 151 struct socket *sonewconn(); 152 #endif 153