socketvar.h revision 2165
1/*-
2 * Copyright (c) 1982, 1986, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)socketvar.h	8.1 (Berkeley) 6/2/93
34 * $Id: socketvar.h,v 1.3 1994/08/18 22:35:45 wollman Exp $
35 */
36
37#ifndef _SYS_SOCKETVAR_H_
38#define _SYS_SOCKETVAR_H_
39
40#include <sys/select.h>			/* for struct selinfo */
41
42/*
43 * Kernel structure per socket.
44 * Contains send and receive buffer queues,
45 * handle on protocol and pointer to protocol
46 * private data and error information.
47 */
48struct socket {
49	short	so_type;		/* generic type, see socket.h */
50	short	so_options;		/* from socket call, see socket.h */
51	short	so_linger;		/* time to linger while closing */
52	short	so_state;		/* internal state flags SS_*, below */
53	caddr_t	so_pcb;			/* protocol control block */
54	struct	protosw *so_proto;	/* protocol handle */
55/*
56 * Variables for connection queueing.
57 * Socket where accepts occur is so_head in all subsidiary sockets.
58 * If so_head is 0, socket is not related to an accept.
59 * For head socket so_q0 queues partially completed connections,
60 * while so_q is a queue of connections ready to be accepted.
61 * If a connection is aborted and it has so_head set, then
62 * it has to be pulled out of either so_q0 or so_q.
63 * We allow connections to queue up based on current queue lengths
64 * and limit on number of queued connections for this socket.
65 */
66	struct	socket *so_head;	/* back pointer to accept socket */
67	struct	socket *so_q0;		/* queue of partial connections */
68	struct	socket *so_q;		/* queue of incoming connections */
69	short	so_q0len;		/* partials on so_q0 */
70	short	so_qlen;		/* number of connections on so_q */
71	short	so_qlimit;		/* max number queued connections */
72	short	so_timeo;		/* connection timeout */
73	u_short	so_error;		/* error affecting connection */
74	pid_t	so_pgid;		/* pgid for signals */
75	u_long	so_oobmark;		/* chars to oob mark */
76/*
77 * Variables for socket buffering.
78 */
79	struct	sockbuf {
80		u_long	sb_cc;		/* actual chars in buffer */
81		u_long	sb_hiwat;	/* max actual char count */
82		u_long	sb_mbcnt;	/* chars of mbufs used */
83		u_long	sb_mbmax;	/* max chars of mbufs to use */
84		long	sb_lowat;	/* low water mark */
85		struct	mbuf *sb_mb;	/* the mbuf chain */
86		struct	selinfo sb_sel;	/* process selecting read/write */
87		short	sb_flags;	/* flags, see below */
88		short	sb_timeo;	/* timeout for read/write */
89	} so_rcv, so_snd;
90#define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
91#define	SB_LOCK		0x01		/* lock on data queue */
92#define	SB_WANT		0x02		/* someone is waiting to lock */
93#define	SB_WAIT		0x04		/* someone is waiting for data/space */
94#define	SB_SEL		0x08		/* someone is selecting */
95#define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
96#define	SB_NOTIFY	(SB_WAIT|SB_SEL|SB_ASYNC)
97#define	SB_NOINTR	0x40		/* operations not interruptible */
98
99	caddr_t	so_tpcb;		/* Wisc. protocol control block XXX */
100	void	(*so_upcall) __P((struct socket *so, caddr_t arg, int waitf));
101	caddr_t	so_upcallarg;		/* Arg for above */
102};
103
104/*
105 * Socket state bits.
106 */
107#define	SS_NOFDREF		0x001	/* no file table ref any more */
108#define	SS_ISCONNECTED		0x002	/* socket connected to a peer */
109#define	SS_ISCONNECTING		0x004	/* in process of connecting to peer */
110#define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */
111#define	SS_CANTSENDMORE		0x010	/* can't send more data to peer */
112#define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
113#define	SS_RCVATMARK		0x040	/* at mark on input */
114
115#define	SS_PRIV			0x080	/* privileged for broadcast, raw... */
116#define	SS_NBIO			0x100	/* non-blocking ops */
117#define	SS_ASYNC		0x200	/* async i/o notify */
118#define	SS_ISCONFIRMING		0x400	/* deciding to accept connection req */
119
120
121/*
122 * Macros for sockets and socket buffering.
123 */
124
125/*
126 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
127 * This is problematical if the fields are unsigned, as the space might
128 * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
129 * overflow and return 0.  Should use "lmin" but it doesn't exist now.
130 */
131#define	sbspace(sb) \
132    ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
133	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
134
135/* do we have to send all at once on a socket? */
136#define	sosendallatonce(so) \
137    ((so)->so_proto->pr_flags & PR_ATOMIC)
138
139/* can we read something from so? */
140#define	soreadable(so) \
141    ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
142	((so)->so_state & SS_CANTRCVMORE) || \
143	(so)->so_qlen || (so)->so_error)
144
145/* can we write something to so? */
146#define	sowriteable(so) \
147    (sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
148	(((so)->so_state&SS_ISCONNECTED) || \
149	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0) || \
150     ((so)->so_state & SS_CANTSENDMORE) || \
151     (so)->so_error)
152
153/* adjust counters in sb reflecting allocation of m */
154#define	sballoc(sb, m) { \
155	(sb)->sb_cc += (m)->m_len; \
156	(sb)->sb_mbcnt += MSIZE; \
157	if ((m)->m_flags & M_EXT) \
158		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
159}
160
161/* adjust counters in sb reflecting freeing of m */
162#define	sbfree(sb, m) { \
163	(sb)->sb_cc -= (m)->m_len; \
164	(sb)->sb_mbcnt -= MSIZE; \
165	if ((m)->m_flags & M_EXT) \
166		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
167}
168
169/*
170 * Set lock on sockbuf sb; sleep if lock is already held.
171 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
172 * Returns error without lock if sleep is interrupted.
173 */
174#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
175		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
176		((sb)->sb_flags |= SB_LOCK), 0)
177
178/* release lock on sockbuf sb */
179#define	sbunlock(sb) { \
180	(sb)->sb_flags &= ~SB_LOCK; \
181	if ((sb)->sb_flags & SB_WANT) { \
182		(sb)->sb_flags &= ~SB_WANT; \
183		wakeup((caddr_t)&(sb)->sb_flags); \
184	} \
185}
186
187#define	sorwakeup(so)	{ sowakeup((so), &(so)->so_rcv); \
188			  if ((so)->so_upcall) \
189			    (*((so)->so_upcall))((so), (so)->so_upcallarg, M_DONTWAIT); \
190			}
191
192#define	sowwakeup(so)	sowakeup((so), &(so)->so_snd)
193
194#ifdef KERNEL
195extern u_long	sb_max;
196/* to catch callers missing new second argument to sonewconn: */
197#define	sonewconn(head, connstatus)	sonewconn1((head), (connstatus))
198struct	socket *sonewconn1 __P((struct socket *head, int connstatus));
199
200/* strings for sleep message: */
201extern	char netio[], netcon[], netcls[];
202
203/*
204 * File operations on sockets.
205 */
206int	soo_read __P((struct file *fp, struct uio *uio, struct ucred *cred));
207int	soo_write __P((struct file *fp, struct uio *uio, struct ucred *cred));
208int	soo_ioctl __P((struct file *fp, int com, caddr_t data, struct proc *p));
209int	soo_select __P((struct file *fp, int which, struct proc *p));
210int 	soo_close __P((struct file *fp, struct proc *p));
211#endif
212
213#endif
214