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