xref: /original-bsd/sys/sys/socketvar.h (revision c02b9ad6)
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 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.5 (Berkeley) 08/21/88
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 	short	so_pgrp;		/* pgrp 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 (not used yet) */
63 		struct	mbuf *sb_mb;	/* the mbuf chain */
64 		struct	proc *sb_sel;	/* process selecting read/write */
65 		short	sb_timeo;	/* timeout (not used yet) */
66 		short	sb_flags;	/* flags, see below */
67 	} so_rcv, so_snd;
68 #define	SB_MAX		(64*1024)	/* max chars in sockbuf */
69 #define	SB_LOCK		0x01		/* lock on data queue (so_rcv only) */
70 #define	SB_WANT		0x02		/* someone is waiting to lock */
71 #define	SB_WAIT		0x04		/* someone is waiting for data/space */
72 #define	SB_SEL		0x08		/* buffer is selected */
73 #define	SB_COLL		0x10		/* collision selecting */
74 };
75 
76 /*
77  * Socket state bits.
78  */
79 #define	SS_NOFDREF		0x001	/* no file table ref any more */
80 #define	SS_ISCONNECTED		0x002	/* socket connected to a peer */
81 #define	SS_ISCONNECTING		0x004	/* in process of connecting to peer */
82 #define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */
83 #define	SS_CANTSENDMORE		0x010	/* can't send more data to peer */
84 #define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
85 #define	SS_RCVATMARK		0x040	/* at mark on input */
86 
87 #define	SS_PRIV			0x080	/* privileged for broadcast, raw... */
88 #define	SS_NBIO			0x100	/* non-blocking ops */
89 #define	SS_ASYNC		0x200	/* async i/o notify */
90 #define	SS_ISCONFIRMING		0x400	/* deciding to accept connection req */
91 
92 
93 /*
94  * Macros for sockets and socket buffering.
95  */
96 
97 /* how much space is there in a socket buffer (so->so_snd or so->so_rcv) */
98 #define	sbspace(sb) \
99     (min((long)((sb)->sb_hiwat - (sb)->sb_cc),\
100 	 (long)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
101 
102 /* do we have to send all at once on a socket? */
103 #define	sosendallatonce(so) \
104     ((so)->so_proto->pr_flags & PR_ATOMIC)
105 
106 /* can we read something from so? */
107 #define	soreadable(so) \
108     ((so)->so_rcv.sb_cc || ((so)->so_state & SS_CANTRCVMORE) || \
109 	(so)->so_qlen || (so)->so_error)
110 
111 /* can we write something to so? */
112 #define	sowriteable(so) \
113     (sbspace(&(so)->so_snd) > 0 && \
114 	(((so)->so_state&SS_ISCONNECTED) || \
115 	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0) || \
116      ((so)->so_state & SS_CANTSENDMORE) || \
117      (so)->so_error)
118 
119 /* adjust counters in sb reflecting allocation of m */
120 #define	sballoc(sb, m) { \
121 	(sb)->sb_cc += (m)->m_len; \
122 	(sb)->sb_mbcnt += MSIZE; \
123 	if ((m)->m_flags & M_EXT) \
124 		(sb)->sb_mbcnt += MCLBYTES; \
125 }
126 
127 /* adjust counters in sb reflecting freeing of m */
128 #define	sbfree(sb, m) { \
129 	(sb)->sb_cc -= (m)->m_len; \
130 	(sb)->sb_mbcnt -= MSIZE; \
131 	if ((m)->m_flags & M_EXT) \
132 		(sb)->sb_mbcnt -= MCLBYTES; \
133 }
134 
135 /* set lock on sockbuf sb */
136 #define sblock(sb) { \
137 	while ((sb)->sb_flags & SB_LOCK) { \
138 		(sb)->sb_flags |= SB_WANT; \
139 		sleep((caddr_t)&(sb)->sb_flags, PZERO+1); \
140 	} \
141 	(sb)->sb_flags |= SB_LOCK; \
142 }
143 
144 /* release lock on sockbuf sb */
145 #define	sbunlock(sb) { \
146 	(sb)->sb_flags &= ~SB_LOCK; \
147 	if ((sb)->sb_flags & SB_WANT) { \
148 		(sb)->sb_flags &= ~SB_WANT; \
149 		wakeup((caddr_t)&(sb)->sb_flags); \
150 	} \
151 }
152 
153 #define	sorwakeup(so)	sowakeup((so), &(so)->so_rcv)
154 #define	sowwakeup(so)	sowakeup((so), &(so)->so_snd)
155 
156 #ifdef KERNEL
157 struct	socket *sonewconn();
158 #endif
159