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