1 /* 2 * Copyright (c) 1982 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)socketvar.h 6.10 (Berkeley) 02/17/86 7 */ 8 9 /* 10 * Kernel structure per socket. 11 * Contains send and receive buffer queues, 12 * handle on protocol and pointer to protocol 13 * private data and error information. 14 */ 15 struct socket { 16 short so_type; /* generic type, see socket.h */ 17 short so_options; /* from socket call, see socket.h */ 18 short so_linger; /* time to linger while closing */ 19 short so_state; /* internal state flags SS_*, below */ 20 caddr_t so_pcb; /* protocol control block */ 21 struct protosw *so_proto; /* protocol handle */ 22 /* 23 * Variables for connection queueing. 24 * Socket where accepts occur is so_head in all subsidiary sockets. 25 * If so_head is 0, socket is not related to an accept. 26 * For head socket so_q0 queues partially completed connections, 27 * while so_q is a queue of connections ready to be accepted. 28 * If a connection is aborted and it has so_head set, then 29 * it has to be pulled out of either so_q0 or so_q. 30 * We allow connections to queue up based on current queue lengths 31 * and limit on number of queued connections for this socket. 32 */ 33 struct socket *so_head; /* back pointer to accept socket */ 34 struct socket *so_q0; /* queue of partial connections */ 35 short so_q0len; /* partials on so_q0 */ 36 struct socket *so_q; /* queue of incoming connections */ 37 short so_qlen; /* number of connections on so_q */ 38 short so_qlimit; /* max number queued connections */ 39 /* 40 * Variables for socket buffering. 41 */ 42 struct sockbuf { 43 u_short sb_cc; /* actual chars in buffer */ 44 u_short sb_hiwat; /* max actual char count */ 45 u_short sb_mbcnt; /* chars of mbufs used */ 46 u_short sb_mbmax; /* max chars of mbufs to use */ 47 u_short sb_lowat; /* low water mark (not used yet) */ 48 short sb_timeo; /* timeout (not used yet) */ 49 struct mbuf *sb_mb; /* the mbuf chain */ 50 struct proc *sb_sel; /* process selecting read/write */ 51 short sb_flags; /* flags, see below */ 52 } so_rcv, so_snd; 53 #define SB_MAX 65535 /* max chars in sockbuf */ 54 #define SB_LOCK 0x01 /* lock on data queue (so_rcv only) */ 55 #define SB_WANT 0x02 /* someone is waiting to lock */ 56 #define SB_WAIT 0x04 /* someone is waiting for data/space */ 57 #define SB_SEL 0x08 /* buffer is selected */ 58 #define SB_COLL 0x10 /* collision selecting */ 59 short so_timeo; /* connection timeout */ 60 u_short so_error; /* error affecting connection */ 61 u_short so_oobmark; /* chars to oob mark */ 62 short so_pgrp; /* pgrp for signals */ 63 }; 64 65 /* 66 * Socket state bits. 67 */ 68 #define SS_NOFDREF 0x001 /* no file table ref any more */ 69 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */ 70 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */ 71 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */ 72 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */ 73 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */ 74 #define SS_RCVATMARK 0x040 /* at mark on input */ 75 76 #define SS_PRIV 0x080 /* privileged for broadcast, raw... */ 77 #define SS_NBIO 0x100 /* non-blocking ops */ 78 #define SS_ASYNC 0x200 /* async i/o notify */ 79 80 81 /* 82 * Macros for sockets and socket buffering. 83 */ 84 85 /* how much space is there in a socket buffer (so->so_snd or so->so_rcv) */ 86 #define sbspace(sb) \ 87 (MIN((int)((sb)->sb_hiwat - (sb)->sb_cc),\ 88 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 89 90 /* do we have to send all at once on a socket? */ 91 #define sosendallatonce(so) \ 92 ((so)->so_proto->pr_flags & PR_ATOMIC) 93 94 /* can we read something from so? */ 95 #define soreadable(so) \ 96 ((so)->so_rcv.sb_cc || ((so)->so_state & SS_CANTRCVMORE) || \ 97 (so)->so_qlen || (so)->so_error) 98 99 /* can we write something to so? */ 100 #define sowriteable(so) \ 101 (sbspace(&(so)->so_snd) > 0 && \ 102 (((so)->so_state&SS_ISCONNECTED) || \ 103 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0) || \ 104 ((so)->so_state & SS_CANTSENDMORE) || \ 105 (so)->so_error) 106 107 /* adjust counters in sb reflecting allocation of m */ 108 #define sballoc(sb, m) { \ 109 (sb)->sb_cc += (m)->m_len; \ 110 (sb)->sb_mbcnt += MSIZE; \ 111 if ((m)->m_off > MMAXOFF) \ 112 (sb)->sb_mbcnt += CLBYTES; \ 113 } 114 115 /* adjust counters in sb reflecting freeing of m */ 116 #define sbfree(sb, m) { \ 117 (sb)->sb_cc -= (m)->m_len; \ 118 (sb)->sb_mbcnt -= MSIZE; \ 119 if ((m)->m_off > MMAXOFF) \ 120 (sb)->sb_mbcnt -= CLBYTES; \ 121 } 122 123 /* set lock on sockbuf sb */ 124 #define sblock(sb) { \ 125 while ((sb)->sb_flags & SB_LOCK) { \ 126 (sb)->sb_flags |= SB_WANT; \ 127 sleep((caddr_t)&(sb)->sb_flags, PZERO+1); \ 128 } \ 129 (sb)->sb_flags |= SB_LOCK; \ 130 } 131 132 /* release lock on sockbuf sb */ 133 #define sbunlock(sb) { \ 134 (sb)->sb_flags &= ~SB_LOCK; \ 135 if ((sb)->sb_flags & SB_WANT) { \ 136 (sb)->sb_flags &= ~SB_WANT; \ 137 wakeup((caddr_t)&(sb)->sb_flags); \ 138 } \ 139 } 140 141 #define sorwakeup(so) sowakeup((so), &(so)->so_rcv) 142 #define sowwakeup(so) sowakeup((so), &(so)->so_snd) 143 144 #ifdef KERNEL 145 struct socket *sonewconn(); 146 struct mbuf *sbdrop(); 147 struct mbuf *sbdroprecord(); 148 #endif 149