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