socketvar.h revision 14547
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1990, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
3314505Shsu *	@(#)socketvar.h	8.3 (Berkeley) 2/19/95
3414547Sdg * $Id: socketvar.h,v 1.12 1996/03/11 02:22:23 hsu Exp $
351541Srgrimes */
361541Srgrimes
372165Spaul#ifndef _SYS_SOCKETVAR_H_
382165Spaul#define _SYS_SOCKETVAR_H_
392165Spaul
403304Sphk#include <sys/stat.h>			/* for struct stat */
413304Sphk#include <sys/filedesc.h>		/* for struct filedesc */
421541Srgrimes#include <sys/select.h>			/* for struct selinfo */
431541Srgrimes
441541Srgrimes/*
451541Srgrimes * Kernel structure per socket.
461541Srgrimes * Contains send and receive buffer queues,
471541Srgrimes * handle on protocol and pointer to protocol
481541Srgrimes * private data and error information.
491541Srgrimes */
501541Srgrimesstruct socket {
511541Srgrimes	short	so_type;		/* generic type, see socket.h */
521541Srgrimes	short	so_options;		/* from socket call, see socket.h */
531541Srgrimes	short	so_linger;		/* time to linger while closing */
541541Srgrimes	short	so_state;		/* internal state flags SS_*, below */
551541Srgrimes	caddr_t	so_pcb;			/* protocol control block */
561541Srgrimes	struct	protosw *so_proto;	/* protocol handle */
571541Srgrimes/*
5813765Smpp * Variables for connection queuing.
591541Srgrimes * Socket where accepts occur is so_head in all subsidiary sockets.
601541Srgrimes * If so_head is 0, socket is not related to an accept.
611541Srgrimes * For head socket so_q0 queues partially completed connections,
621541Srgrimes * while so_q is a queue of connections ready to be accepted.
631541Srgrimes * If a connection is aborted and it has so_head set, then
641541Srgrimes * it has to be pulled out of either so_q0 or so_q.
651541Srgrimes * We allow connections to queue up based on current queue lengths
661541Srgrimes * and limit on number of queued connections for this socket.
671541Srgrimes */
681541Srgrimes	struct	socket *so_head;	/* back pointer to accept socket */
6914547Sdg	TAILQ_HEAD(, socket) so_incomp;	/* queue of partial unaccepted connections */
7014547Sdg	TAILQ_HEAD(, socket) so_comp;	/* queue of complete unaccepted connections */
7114547Sdg	TAILQ_ENTRY(socket) so_list;	/* list of unaccepted connections */
7214547Sdg	short	so_qlen;		/* number of unaccepted connections */
731541Srgrimes	short	so_qlimit;		/* max number queued connections */
741541Srgrimes	short	so_timeo;		/* connection timeout */
751541Srgrimes	u_short	so_error;		/* error affecting connection */
761541Srgrimes	pid_t	so_pgid;		/* pgid for signals */
771541Srgrimes	u_long	so_oobmark;		/* chars to oob mark */
781541Srgrimes/*
791541Srgrimes * Variables for socket buffering.
801541Srgrimes */
811541Srgrimes	struct	sockbuf {
821541Srgrimes		u_long	sb_cc;		/* actual chars in buffer */
831541Srgrimes		u_long	sb_hiwat;	/* max actual char count */
841541Srgrimes		u_long	sb_mbcnt;	/* chars of mbufs used */
851541Srgrimes		u_long	sb_mbmax;	/* max chars of mbufs to use */
861541Srgrimes		long	sb_lowat;	/* low water mark */
871541Srgrimes		struct	mbuf *sb_mb;	/* the mbuf chain */
881541Srgrimes		struct	selinfo sb_sel;	/* process selecting read/write */
891541Srgrimes		short	sb_flags;	/* flags, see below */
901541Srgrimes		short	sb_timeo;	/* timeout for read/write */
911541Srgrimes	} so_rcv, so_snd;
921541Srgrimes#define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
931541Srgrimes#define	SB_LOCK		0x01		/* lock on data queue */
941541Srgrimes#define	SB_WANT		0x02		/* someone is waiting to lock */
951541Srgrimes#define	SB_WAIT		0x04		/* someone is waiting for data/space */
961541Srgrimes#define	SB_SEL		0x08		/* someone is selecting */
971541Srgrimes#define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
981541Srgrimes#define	SB_NOTIFY	(SB_WAIT|SB_SEL|SB_ASYNC)
991541Srgrimes#define	SB_NOINTR	0x40		/* operations not interruptible */
1001541Srgrimes
1011541Srgrimes	caddr_t	so_tpcb;		/* Wisc. protocol control block XXX */
1021541Srgrimes	void	(*so_upcall) __P((struct socket *so, caddr_t arg, int waitf));
1031541Srgrimes	caddr_t	so_upcallarg;		/* Arg for above */
1041541Srgrimes};
1051541Srgrimes
1061541Srgrimes/*
1071541Srgrimes * Socket state bits.
1081541Srgrimes */
10914547Sdg#define	SS_NOFDREF		0x0001	/* no file table ref any more */
11014547Sdg#define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
11114547Sdg#define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
11214547Sdg#define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
11314547Sdg#define	SS_CANTSENDMORE		0x0010	/* can't send more data to peer */
11414547Sdg#define	SS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
11514547Sdg#define	SS_RCVATMARK		0x0040	/* at mark on input */
1161541Srgrimes
11714547Sdg#define	SS_PRIV			0x0080	/* privileged for broadcast, raw... */
11814547Sdg#define	SS_NBIO			0x0100	/* non-blocking ops */
11914547Sdg#define	SS_ASYNC		0x0200	/* async i/o notify */
12014547Sdg#define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
1211541Srgrimes
12214547Sdg#define	SS_INCOMP		0x0800	/* unaccepted, incomplete connection */
12314547Sdg#define	SS_COMP			0x1000	/* unaccepted, complete connection */
1241541Srgrimes
12514547Sdg
1261541Srgrimes/*
1271541Srgrimes * Macros for sockets and socket buffering.
1281541Srgrimes */
1291541Srgrimes
1301541Srgrimes/*
1311541Srgrimes * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
1321541Srgrimes * This is problematical if the fields are unsigned, as the space might
1331541Srgrimes * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
1341541Srgrimes * overflow and return 0.  Should use "lmin" but it doesn't exist now.
1351541Srgrimes */
1361541Srgrimes#define	sbspace(sb) \
1371541Srgrimes    ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
1381541Srgrimes	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
1391541Srgrimes
1401541Srgrimes/* do we have to send all at once on a socket? */
1411541Srgrimes#define	sosendallatonce(so) \
1421541Srgrimes    ((so)->so_proto->pr_flags & PR_ATOMIC)
1431541Srgrimes
1441541Srgrimes/* can we read something from so? */
1451541Srgrimes#define	soreadable(so) \
1461541Srgrimes    ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
1471541Srgrimes	((so)->so_state & SS_CANTRCVMORE) || \
14814547Sdg	(so)->so_comp.tqh_first || (so)->so_error)
1491541Srgrimes
1501541Srgrimes/* can we write something to so? */
1511541Srgrimes#define	sowriteable(so) \
1523304Sphk    ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
1531541Srgrimes	(((so)->so_state&SS_ISCONNECTED) || \
1543304Sphk	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
1551541Srgrimes     ((so)->so_state & SS_CANTSENDMORE) || \
1561541Srgrimes     (so)->so_error)
1571541Srgrimes
1581541Srgrimes/* adjust counters in sb reflecting allocation of m */
1591541Srgrimes#define	sballoc(sb, m) { \
1601541Srgrimes	(sb)->sb_cc += (m)->m_len; \
1611541Srgrimes	(sb)->sb_mbcnt += MSIZE; \
1621541Srgrimes	if ((m)->m_flags & M_EXT) \
1631541Srgrimes		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
1641541Srgrimes}
1651541Srgrimes
1661541Srgrimes/* adjust counters in sb reflecting freeing of m */
1671541Srgrimes#define	sbfree(sb, m) { \
1681541Srgrimes	(sb)->sb_cc -= (m)->m_len; \
1691541Srgrimes	(sb)->sb_mbcnt -= MSIZE; \
1701541Srgrimes	if ((m)->m_flags & M_EXT) \
1711541Srgrimes		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
1721541Srgrimes}
1731541Srgrimes
1741541Srgrimes/*
1751541Srgrimes * Set lock on sockbuf sb; sleep if lock is already held.
1761541Srgrimes * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
1771541Srgrimes * Returns error without lock if sleep is interrupted.
1781541Srgrimes */
1791541Srgrimes#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
1801541Srgrimes		(((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
1811541Srgrimes		((sb)->sb_flags |= SB_LOCK), 0)
1821541Srgrimes
1831541Srgrimes/* release lock on sockbuf sb */
1841541Srgrimes#define	sbunlock(sb) { \
1851541Srgrimes	(sb)->sb_flags &= ~SB_LOCK; \
1861541Srgrimes	if ((sb)->sb_flags & SB_WANT) { \
1871541Srgrimes		(sb)->sb_flags &= ~SB_WANT; \
1881541Srgrimes		wakeup((caddr_t)&(sb)->sb_flags); \
1891541Srgrimes	} \
1901541Srgrimes}
1911541Srgrimes
1921541Srgrimes#define	sorwakeup(so)	{ sowakeup((so), &(so)->so_rcv); \
1931541Srgrimes			  if ((so)->so_upcall) \
1941541Srgrimes			    (*((so)->so_upcall))((so), (so)->so_upcallarg, M_DONTWAIT); \
1951541Srgrimes			}
1961541Srgrimes
1971541Srgrimes#define	sowwakeup(so)	sowakeup((so), &(so)->so_snd)
1981541Srgrimes
1991541Srgrimes#ifdef KERNEL
2002112Swollmanextern u_long	sb_max;
20114505Shsu
2021541Srgrimes/* to catch callers missing new second argument to sonewconn: */
2031541Srgrimes#define	sonewconn(head, connstatus)	sonewconn1((head), (connstatus))
2041541Srgrimes
2051541Srgrimes/*
2061541Srgrimes * File operations on sockets.
2071541Srgrimes */
20814505Shsuint	soo_ioctl __P((struct file *fp, int cmd, caddr_t data,
20914505Shsu	    struct proc *p));
2101541Srgrimesint	soo_select __P((struct file *fp, int which, struct proc *p));
21114505Shsuint	soo_stat __P((struct socket *so, struct stat *ub));
2123304Sphk
21314505Shsustruct mbuf;
21414505Shsustruct sockaddr;
21514505Shsu
2163304Sphk/*
2173304Sphk * From uipc_socket and friends
2183304Sphk */
21914505Shsuint	getsock __P((struct filedesc *fdp, int fdes, struct file **fpp));
22014505Shsuint	sockargs __P((struct mbuf **mp, caddr_t buf, int buflen, int type));
22114505Shsuvoid	sbappend __P((struct sockbuf *sb, struct mbuf *m));
22214505Shsuint	sbappendaddr __P((struct sockbuf *sb, struct sockaddr *asa,
22314505Shsu	    struct mbuf *m0, struct mbuf *control));
22414505Shsuint	sbappendcontrol __P((struct sockbuf *sb, struct mbuf *m0,
22514505Shsu	    struct mbuf *control));
22614505Shsuvoid	sbappendrecord __P((struct sockbuf *sb, struct mbuf *m0));
22714505Shsuvoid	sbcheck __P((struct sockbuf *sb));
22814505Shsuvoid	sbcompress __P((struct sockbuf *sb, struct mbuf *m, struct mbuf *n));
22914505Shsuvoid	sbdrop __P((struct sockbuf *sb, int len));
23014505Shsuvoid	sbdroprecord __P((struct sockbuf *sb));
23114505Shsuvoid	sbflush __P((struct sockbuf *sb));
23214505Shsuvoid	sbinsertoob __P((struct sockbuf *sb, struct mbuf *m0));
23314505Shsuvoid	sbrelease __P((struct sockbuf *sb));
23414505Shsuint	sbreserve __P((struct sockbuf *sb, u_long cc));
23514505Shsuint	sbwait __P((struct sockbuf *sb));
23614505Shsuint	sb_lock __P((struct sockbuf *sb));
23714505Shsuint	soabort __P((struct socket *so));
23814505Shsuint	soaccept __P((struct socket *so, struct mbuf *nam));
23914505Shsuint	sobind __P((struct socket *so, struct mbuf *nam));
24014505Shsuvoid	socantrcvmore __P((struct socket *so));
24114505Shsuvoid	socantsendmore __P((struct socket *so));
24214505Shsuint	soclose __P((struct socket *so));
24314505Shsuint	soconnect __P((struct socket *so, struct mbuf *nam));
24414505Shsuint	soconnect2 __P((struct socket *so1, struct socket *so2));
24514505Shsuint	socreate __P((int dom, struct socket **aso, int type, int proto,
24614505Shsu	    struct proc *p));
24714505Shsuint	sodisconnect __P((struct socket *so));
24814505Shsuvoid	sofree __P((struct socket *so));
24914505Shsuint	sogetopt __P((struct socket *so, int level, int optname,
25014505Shsu	    struct mbuf **mp));
25114505Shsuvoid	sohasoutofband __P((struct socket *so));
25214505Shsuvoid	soisconnected __P((struct socket *so));
25314505Shsuvoid	soisconnecting __P((struct socket *so));
25414505Shsuvoid	soisdisconnected __P((struct socket *so));
25514505Shsuvoid	soisdisconnecting __P((struct socket *so));
25614505Shsuint	solisten __P((struct socket *so, int backlog));
25714505Shsustruct socket *
25814505Shsu	sonewconn1 __P((struct socket *head, int connstatus));
25914505Shsuint	soreceive __P((struct socket *so, struct mbuf **paddr, struct uio *uio,
26014505Shsu	    struct mbuf **mp0, struct mbuf **controlp, int *flagsp));
26114505Shsuint	soreserve __P((struct socket *so, u_long sndcc, u_long rcvcc));
26214505Shsuvoid	sorflush __P((struct socket *so));
26314505Shsuint	sosend __P((struct socket *so, struct mbuf *addr, struct uio *uio,
26414505Shsu	    struct mbuf *top, struct mbuf *control, int flags));
26514505Shsuint	sosetopt __P((struct socket *so, int level, int optname,
26614505Shsu	    struct mbuf *m0));
26714505Shsuint	soshutdown __P((struct socket *so, int how));
26814505Shsuvoid	sowakeup __P((struct socket *so, struct sockbuf *sb));
26914505Shsu#endif /* KERNEL */
2702165Spaul
27114505Shsu#endif /* !_SYS_SOCKETVAR_H_ */
272