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