socketvar.h revision 12819
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.7 1995/11/21 12:55:14 bde Exp $
35 */
36
37#ifndef _SYS_SOCKETVAR_H_
38#define _SYS_SOCKETVAR_H_
39
40#include <sys/stat.h>			/* for struct stat */
41#include <sys/filedesc.h>		/* for struct filedesc */
42#include <sys/select.h>			/* for struct selinfo */
43
44/*
45 * Kernel structure per socket.
46 * Contains send and receive buffer queues,
47 * handle on protocol and pointer to protocol
48 * private data and error information.
49 */
50struct socket {
51	short	so_type;		/* generic type, see socket.h */
52	short	so_options;		/* from socket call, see socket.h */
53	short	so_linger;		/* time to linger while closing */
54	short	so_state;		/* internal state flags SS_*, below */
55	caddr_t	so_pcb;			/* protocol control block */
56	struct	protosw *so_proto;	/* protocol handle */
57/*
58 * Variables for connection queueing.
59 * Socket where accepts occur is so_head in all subsidiary sockets.
60 * If so_head is 0, socket is not related to an accept.
61 * For head socket so_q0 queues partially completed connections,
62 * while so_q is a queue of connections ready to be accepted.
63 * If a connection is aborted and it has so_head set, then
64 * it has to be pulled out of either so_q0 or so_q.
65 * We allow connections to queue up based on current queue lengths
66 * and limit on number of queued connections for this socket.
67 */
68	struct	socket *so_head;	/* back pointer to accept socket */
69	struct	socket *so_q0;		/* queue of partial connections */
70	struct	socket *so_q;		/* queue of incoming connections */
71	short	so_q0len;		/* partials on so_q0 */
72	short	so_qlen;		/* number of connections on so_q */
73	short	so_qlimit;		/* max number queued connections */
74	short	so_timeo;		/* connection timeout */
75	u_short	so_error;		/* error affecting connection */
76	pid_t	so_pgid;		/* pgid for signals */
77	u_long	so_oobmark;		/* chars to oob mark */
78/*
79 * Variables for socket buffering.
80 */
81	struct	sockbuf {
82		u_long	sb_cc;		/* actual chars in buffer */
83		u_long	sb_hiwat;	/* max actual char count */
84		u_long	sb_mbcnt;	/* chars of mbufs used */
85		u_long	sb_mbmax;	/* max chars of mbufs to use */
86		long	sb_lowat;	/* low water mark */
87		struct	mbuf *sb_mb;	/* the mbuf chain */
88		struct	selinfo sb_sel;	/* process selecting read/write */
89		short	sb_flags;	/* flags, see below */
90		short	sb_timeo;	/* timeout for read/write */
91	} so_rcv, so_snd;
92#define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
93#define	SB_LOCK		0x01		/* lock on data queue */
94#define	SB_WANT		0x02		/* someone is waiting to lock */
95#define	SB_WAIT		0x04		/* someone is waiting for data/space */
96#define	SB_SEL		0x08		/* someone is selecting */
97#define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
98#define	SB_NOTIFY	(SB_WAIT|SB_SEL|SB_ASYNC)
99#define	SB_NOINTR	0x40		/* operations not interruptible */
100
101	caddr_t	so_tpcb;		/* Wisc. protocol control block XXX */
102	void	(*so_upcall) __P((struct socket *so, caddr_t arg, int waitf));
103	caddr_t	so_upcallarg;		/* Arg for above */
104};
105
106/*
107 * Socket state bits.
108 */
109#define	SS_NOFDREF		0x001	/* no file table ref any more */
110#define	SS_ISCONNECTED		0x002	/* socket connected to a peer */
111#define	SS_ISCONNECTING		0x004	/* in process of connecting to peer */
112#define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */
113#define	SS_CANTSENDMORE		0x010	/* can't send more data to peer */
114#define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
115#define	SS_RCVATMARK		0x040	/* at mark on input */
116
117#define	SS_PRIV			0x080	/* privileged for broadcast, raw... */
118#define	SS_NBIO			0x100	/* non-blocking ops */
119#define	SS_ASYNC		0x200	/* async i/o notify */
120#define	SS_ISCONFIRMING		0x400	/* deciding to accept connection req */
121
122
123/*
124 * Macros for sockets and socket buffering.
125 */
126
127/*
128 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
129 * This is problematical if the fields are unsigned, as the space might
130 * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
131 * overflow and return 0.  Should use "lmin" but it doesn't exist now.
132 */
133#define	sbspace(sb) \
134    ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
135	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
136
137/* do we have to send all at once on a socket? */
138#define	sosendallatonce(so) \
139    ((so)->so_proto->pr_flags & PR_ATOMIC)
140
141/* can we read something from so? */
142#define	soreadable(so) \
143    ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
144	((so)->so_state & SS_CANTRCVMORE) || \
145	(so)->so_qlen || (so)->so_error)
146
147/* can we write something to so? */
148#define	sowriteable(so) \
149    ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
150	(((so)->so_state&SS_ISCONNECTED) || \
151	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
152     ((so)->so_state & SS_CANTSENDMORE) || \
153     (so)->so_error)
154
155/* adjust counters in sb reflecting allocation of m */
156#define	sballoc(sb, m) { \
157	(sb)->sb_cc += (m)->m_len; \
158	(sb)->sb_mbcnt += MSIZE; \
159	if ((m)->m_flags & M_EXT) \
160		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
161}
162
163/* adjust counters in sb reflecting freeing of m */
164#define	sbfree(sb, m) { \
165	(sb)->sb_cc -= (m)->m_len; \
166	(sb)->sb_mbcnt -= MSIZE; \
167	if ((m)->m_flags & M_EXT) \
168		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
169}
170
171/*
172 * Set lock on sockbuf sb; sleep if lock is already held.
173 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
174 * Returns error without lock if sleep is interrupted.
175 */
176#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
177		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
178		((sb)->sb_flags |= SB_LOCK), 0)
179
180/* release lock on sockbuf sb */
181#define	sbunlock(sb) { \
182	(sb)->sb_flags &= ~SB_LOCK; \
183	if ((sb)->sb_flags & SB_WANT) { \
184		(sb)->sb_flags &= ~SB_WANT; \
185		wakeup((caddr_t)&(sb)->sb_flags); \
186	} \
187}
188
189#define	sorwakeup(so)	{ sowakeup((so), &(so)->so_rcv); \
190			  if ((so)->so_upcall) \
191			    (*((so)->so_upcall))((so), (so)->so_upcallarg, M_DONTWAIT); \
192			}
193
194#define	sowwakeup(so)	sowakeup((so), &(so)->so_snd)
195
196#ifdef KERNEL
197extern u_long	sb_max;
198/* to catch callers missing new second argument to sonewconn: */
199#define	sonewconn(head, connstatus)	sonewconn1((head), (connstatus))
200struct	socket *sonewconn1 __P((struct socket *head, int connstatus));
201
202/* strings for sleep message: */
203extern	char netio[], netcon[], netcls[];
204
205/*
206 * File operations on sockets.
207 */
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_stat __P((struct socket *, struct stat *));
211
212/*
213 * From uipc_socket and friends
214 */
215void    soqinsque __P((struct socket *, struct socket *, int));
216void    sowakeup __P((struct socket *, struct sockbuf *));
217void    socantrcvmore __P((struct socket *));
218void    socantsendmore __P((struct socket *));
219void    sbrelease __P((struct sockbuf *));
220void    sbappend __P((struct sockbuf *, struct mbuf *));
221void    sbappendrecord __P((struct sockbuf *, struct mbuf *));
222int	sbappendcontrol __P((struct sockbuf *, struct mbuf *, struct mbuf *));
223int	sbappendaddr __P((struct sockbuf *, struct sockaddr *, struct mbuf *, struct mbuf *));
224void    sbdroprecord __P((struct sockbuf *));
225void    sbcompress __P((struct sockbuf *, struct mbuf *, struct mbuf *));
226void    sbflush __P((struct sockbuf *));
227void	sbinsertoob __P((struct sockbuf *, struct mbuf *));
228int     sbreserve __P((struct sockbuf *,u_long));
229int     soreserve __P((struct socket *,u_long,u_long));
230int     sb_lock __P((struct sockbuf *));
231int     sbwait __P((struct sockbuf *));
232void    sbdrop __P((struct sockbuf *, int));
233void    sofree __P((struct socket *));
234void    sorflush __P((struct socket *));
235int	soqremque __P((struct socket *,int));
236int     soabort __P((struct socket *));
237void    soisdisconnected __P((struct socket *));
238void    soisconnected __P((struct socket *));
239void    soisconnecting __P((struct socket *));
240void    soisdisconnecting __P((struct socket *));
241void    sohasoutofband __P((struct socket *));
242int     sodisconnect __P((struct socket *));
243int	sosend __P((struct socket *,struct mbuf *, struct uio *, struct mbuf *, struct mbuf *, int));
244int	socreate __P((int, struct socket **,int,int));
245int	getsock __P((struct filedesc *,int,struct file **));
246int	sockargs __P((struct mbuf **,caddr_t,int,int));
247int	sobind __P((struct socket *,struct mbuf *));
248int	solisten __P((struct socket *,int));
249int	soaccept __P((struct socket *,struct mbuf *));
250int	soconnect __P((struct socket *,struct mbuf *));
251int	soconnect2 __P((struct socket *,struct socket *));
252int	soclose __P((struct socket *));
253int	soshutdown __P((struct socket *,int));
254int	soreceive __P((struct socket *,struct mbuf **,struct uio *,struct mbuf **,struct mbuf **,int *));
255int	sosetopt __P((struct socket *,int, int, struct mbuf *));
256int	sogetopt __P((struct socket *,int, int, struct mbuf **));
257#endif
258
259#endif
260