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