1 /* socketvar.h 4.15 82/03/15 */ 2 3 /* 4 * Kernel structure per socket. 5 * Contains send and receive buffer queues, 6 * handle on protocol and pointer to protocol 7 * private data and error information. 8 */ 9 struct socket { 10 short so_type; /* generic type, see socket.h */ 11 short so_options; /* from socket call, see socket.h */ 12 short so_linger; /* time to linger while closing */ 13 short so_state; /* internal state flags SS_*, below */ 14 caddr_t so_pcb; /* protocol control block */ 15 struct protosw *so_proto; /* protocol handle */ 16 struct sockbuf { 17 short sb_cc; /* actual chars in buffer */ 18 short sb_hiwat; /* max actual char count */ 19 short sb_mbcnt; /* chars of mbufs used */ 20 short sb_mbmax; /* max chars of mbufs to use */ 21 short sb_lowat; /* low water mark (not used yet) */ 22 short sb_timeo; /* timeout (not used yet) */ 23 struct mbuf *sb_mb; /* the mbuf chain */ 24 struct proc *sb_sel; /* process selecting read/write */ 25 short sb_flags; /* flags, see below */ 26 } so_rcv, so_snd; 27 #define SB_LOCK 0x01 /* lock on data queue (so_rcv only) */ 28 #define SB_WANT 0x02 /* someone is waiting to lock */ 29 #define SB_WAIT 0x04 /* someone is waiting for data/space */ 30 #define SB_SEL 0x08 /* buffer is selected */ 31 #define SB_COLL 0x10 /* collision selecting */ 32 short so_timeo; /* connection timeout */ 33 u_short so_error; /* error affecting connection */ 34 struct sockaddr so_addr; /* socket address */ 35 short so_oobmark; /* chars to oob mark */ 36 short so_pgrp; /* pgrp for signals */ 37 }; 38 39 /* 40 * Socket state bits. 41 */ 42 #define SS_USERGONE 0x001 /* no file table ref any more */ 43 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */ 44 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */ 45 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */ 46 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */ 47 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */ 48 #define SS_CONNAWAITING 0x040 /* connections awaiting acceptance */ 49 #define SS_RCVATMARK 0x080 /* at mark on input */ 50 51 #define SS_PRIV 0x100 /* privileged for broadcast, raw... */ 52 #define SS_NBIO 0x200 /* non-blocking ops */ 53 #define SS_ASYNC 0x400 /* async i/o notify */ 54 55 /* 56 * Macros for sockets and socket buffering. 57 */ 58 59 /* how much space is there in a socket buffer (so->so_snd or so->so_rcv) */ 60 #define sbspace(sb) \ 61 (MIN((sb)->sb_hiwat-(sb)->sb_cc, ((sb)->sb_mbmax-(sb)->sb_mbcnt))) 62 63 /* do we have to send all at once on a socket? */ 64 #define sosendallatonce(so) \ 65 (((so)->so_state & SS_NBIO) || ((so)->so_proto->pr_flags & PR_ATOMIC)) 66 67 /* can we read something from so? */ 68 #define soreadable(so) \ 69 ((so)->so_rcv.sb_cc || ((so)->so_state & (SS_CANTRCVMORE|SS_CONNAWAITING))) 70 71 /* can we write something to so? */ 72 #define sowriteable(so) \ 73 (sbspace(&(so)->so_snd) > 0 && \ 74 (((so)->so_state&SS_ISCONNECTED) || \ 75 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0) || \ 76 ((so)->so_state & SS_CANTSENDMORE)) 77 78 /* adjust counters in sb reflecting allocation of m */ 79 #define sballoc(sb, m) { \ 80 (sb)->sb_cc += (m)->m_len; \ 81 (sb)->sb_mbcnt += MSIZE; \ 82 if ((m)->m_off > MMAXOFF) \ 83 (sb)->sb_mbcnt += CLBYTES; \ 84 } 85 86 /* adjust counters in sb reflecting freeing of m */ 87 #define sbfree(sb, m) { \ 88 (sb)->sb_cc -= (m)->m_len; \ 89 (sb)->sb_mbcnt -= MSIZE; \ 90 if ((m)->m_off > MMAXOFF) \ 91 (sb)->sb_mbcnt -= CLBYTES; \ 92 } 93 94 /* set lock on sockbuf sb */ 95 #define sblock(sb) { \ 96 while ((sb)->sb_flags & SB_LOCK) { \ 97 (sb)->sb_flags |= SB_WANT; \ 98 sleep((caddr_t)&(sb)->sb_flags, PZERO+1); \ 99 } \ 100 (sb)->sb_flags |= SB_LOCK; \ 101 } 102 103 /* release lock on sockbuf sb */ 104 #define sbunlock(sb) { \ 105 (sb)->sb_flags &= ~SB_LOCK; \ 106 if ((sb)->sb_flags & SB_WANT) { \ 107 (sb)->sb_flags &= ~SB_WANT; \ 108 wakeup((caddr_t)&(sb)->sb_flags); \ 109 } \ 110 } 111 112 #define sorwakeup(so) sbwakeup(&(so)->so_rcv) 113 #define sowwakeup(so) sbwakeup(&(so)->so_snd) 114