1 //==========================================================================
3 // include/sys/socketvar.h
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
10 // -------------------------------------------
12 // Portions of this software may have been derived from OpenBSD or other sources,
13 // and are covered by the appropriate copyright disclaimers included herein.
15 // -------------------------------------------
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
22 // Contributors: gthomas
28 //####DESCRIPTIONEND####
30 //==========================================================================
33 /* $OpenBSD: socketvar.h,v 1.17 1999/12/08 06:50:24 itojun Exp $ */
34 /* $NetBSD: socketvar.h,v 1.18 1996/02/09 18:25:38 christos Exp $ */
37 * Copyright (c) 1982, 1986, 1990, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)socketvar.h 8.1 (Berkeley) 6/2/93
71 #ifndef _SYS_SOCKETVAR_H_
72 #define _SYS_SOCKETVAR_H_
74 #include <sys/bsdselect.h> /* for struct selinfo */
77 * Kernel structure per socket.
78 * Contains send and receive buffer queues,
79 * handle on protocol and pointer to protocol
80 * private data and error information.
83 short so_type; /* generic type, see socket.h */
84 short so_options; /* from socket call, see socket.h */
85 short so_linger; /* time to linger while closing */
86 short so_state; /* internal state flags SS_*, below */
87 void *so_pcb; /* protocol control block */
88 struct protosw *so_proto; /* protocol handle */
90 * Variables for connection queueing.
91 * Socket where accepts occur is so_head in all subsidiary sockets.
92 * If so_head is 0, socket is not related to an accept.
93 * For head socket so_q0 queues partially completed connections,
94 * while so_q is a queue of connections ready to be accepted.
95 * If a connection is aborted and it has so_head set, then
96 * it has to be pulled out of either so_q0 or so_q.
97 * We allow connections to queue up based on current queue lengths
98 * and limit on number of queued connections for this socket.
100 struct socket *so_head; /* back pointer to accept socket */
101 struct socket *so_q0; /* queue of partial connections */
102 struct socket *so_q; /* queue of incoming connections */
103 short so_q0len; /* partials on so_q0 */
104 short so_qlen; /* number of connections on so_q */
105 short so_qlimit; /* max number queued connections */
106 short so_timeo; /* connection timeout */
107 u_short so_error; /* error affecting connection */
108 pid_t so_pgid; /* pgid for signals */
109 uid_t so_siguid; /* uid of process who set so_pgid */
110 uid_t so_sigeuid; /* euid of process who set so_pgid */
111 u_long so_oobmark; /* chars to oob mark */
113 * Variables for socket buffering.
116 u_long sb_cc; /* actual chars in buffer */
117 u_long sb_hiwat; /* max actual char count */
118 u_long sb_mbcnt; /* chars of mbufs used */
119 u_long sb_mbmax; /* max chars of mbufs to use */
120 long sb_lowat; /* low water mark */
121 struct mbuf *sb_mb; /* the mbuf chain */
122 struct selinfo sb_sel; /* process selecting read/write */
123 short sb_flags; /* flags, see below */
124 short sb_timeo; /* timeout for read/write */
126 #define SB_MAX (256*1024) /* default for max chars in sockbuf */
127 #define SB_LOCK 0x01 /* lock on data queue */
128 #define SB_WANT 0x02 /* someone is waiting to lock */
129 #define SB_WAIT 0x04 /* someone is waiting for data/space */
130 #define SB_SEL 0x08 /* someone is selecting */
131 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
132 #define SB_NOINTR 0x40 /* operations not interruptible */
134 void *so_internal; /* Space for svr4 stream data */
135 void (*so_upcall) __P((struct socket *so, caddr_t arg, int waitf));
136 caddr_t so_upcallarg; /* Arg for above */
137 uid_t so_euid; /* who opened the socket */
138 uid_t so_ruid; /* who opened the socket */
144 #define SS_NOFDREF 0x001 /* no file table ref any more */
145 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */
146 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
147 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
148 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
149 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
150 #define SS_RCVATMARK 0x040 /* at mark on input */
151 #define SS_ISDISCONNECTED 0x800 /* socket disconnected from peer */
153 #define SS_PRIV 0x080 /* privileged for broadcast, raw... */
154 #define SS_NBIO 0x100 /* non-blocking ops */
155 #define SS_ASYNC 0x200 /* async i/o notify */
156 #define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */
157 #define SS_CONNECTOUT 0x1000 /* connect, not accept, at this end */
160 * Macros for sockets and socket buffering.
164 * Do we need to notify the other side when I/O is possible?
166 #define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC)) != 0)
169 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
170 * This is problematical if the fields are unsigned, as the space might
171 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
172 * overflow and return 0. Should use "lmin" but it doesn't exist now.
174 #define sbspace(sb) \
175 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
176 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
178 /* do we have to send all at once on a socket? */
179 #define sosendallatonce(so) \
180 ((so)->so_proto->pr_flags & PR_ATOMIC)
182 /* can we read something from so? */
183 #define soreadable(so) \
184 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
185 ((so)->so_state & SS_CANTRCVMORE) || \
186 (so)->so_qlen || (so)->so_error)
188 /* can we write something to so? */
189 #define sowriteable(so) \
190 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
191 (((so)->so_state&SS_ISCONNECTED) || \
192 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
193 ((so)->so_state & SS_CANTSENDMORE) || \
196 /* adjust counters in sb reflecting allocation of m */
197 #define sballoc(sb, m) { \
198 (sb)->sb_cc += (m)->m_len; \
199 (sb)->sb_mbcnt += MSIZE; \
200 if ((m)->m_flags & M_EXT) \
201 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
204 /* adjust counters in sb reflecting freeing of m */
205 #define sbfree(sb, m) { \
206 (sb)->sb_cc -= (m)->m_len; \
207 (sb)->sb_mbcnt -= MSIZE; \
208 if ((m)->m_flags & M_EXT) \
209 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
213 * Set lock on sockbuf sb; sleep if lock is already held.
214 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
215 * Returns error without lock if sleep is interrupted.
218 extern int sblock(struct sockbuf *sb, int wf);
219 extern void sbunlock(struct sockbuf *sb);
221 #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
222 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
223 ((sb)->sb_flags |= SB_LOCK), 0)
225 /* release lock on sockbuf sb */
226 #define sbunlock(sb) { \
227 (sb)->sb_flags &= ~SB_LOCK; \
228 if ((sb)->sb_flags & SB_WANT) { \
229 (sb)->sb_flags &= ~SB_WANT; \
230 wakeup((caddr_t)&(sb)->sb_flags); \
235 #define sorwakeup(so) { sowakeup((so), &(so)->so_rcv); \
236 if ((so)->so_upcall) \
237 (*((so)->so_upcall))((so), (so)->so_upcallarg, M_DONTWAIT); \
240 #define sowwakeup(so) sowakeup((so), &(so)->so_snd)
244 /* to catch callers missing new second argument to sonewconn: */
245 #define sonewconn(head, connstatus) sonewconn1((head), (connstatus))
246 struct socket *sonewconn1 __P((struct socket *head, int connstatus));
248 /* strings for sleep message: */
249 extern char netio[], netcon[], netcls[];
258 * File operations on sockets.
261 int soo_read __P((struct file *fp, struct uio *uio));
262 int soo_write __P((struct file *fp, struct uio *uio));
263 int soo_ioctl __P((struct file *fp, CYG_ADDRWORD cmd, CYG_ADDRWORD data));
264 int soo_select __P((struct file *fp, int which));
265 int soo_close __P((struct file *fp));
267 int soo_read __P((struct file *fp, struct uio *uio, struct ucred *cred));
268 int soo_write __P((struct file *fp, struct uio *uio, struct ucred *cred));
269 int soo_ioctl __P((struct file *fp, u_long cmd, caddr_t data,
271 int soo_select __P((struct file *fp, int which, struct proc *p));
272 int soo_close __P((struct file *fp, struct proc *p));
275 int soo_stat __P((struct socket *, struct stat *));
276 int uipc_usrreq __P((struct socket *, int , struct mbuf *,
277 struct mbuf *, struct mbuf *));
278 void sbappend __P((struct sockbuf *sb, struct mbuf *m));
279 int sbappendaddr __P((struct sockbuf *sb, struct sockaddr *asa,
280 struct mbuf *m0, struct mbuf *control));
281 int sbappendcontrol __P((struct sockbuf *sb, struct mbuf *m0,
282 struct mbuf *control));
283 void sbappendrecord __P((struct sockbuf *sb, struct mbuf *m0));
284 void sbcheck __P((struct sockbuf *sb));
285 void sbcompress __P((struct sockbuf *sb, struct mbuf *m, struct mbuf *n));
287 sbcreatecontrol __P((caddr_t p, int size, int type, int level));
288 void sbdrop __P((struct sockbuf *sb, int len));
289 void sbdroprecord __P((struct sockbuf *sb));
290 void sbflush __P((struct sockbuf *sb));
291 void sbinsertoob __P((struct sockbuf *sb, struct mbuf *m0));
292 void sbrelease __P((struct sockbuf *sb));
293 int sbreserve __P((struct sockbuf *sb, u_long cc));
294 int sbwait __P((struct sockbuf *sb));
295 int sb_lock __P((struct sockbuf *sb));
296 int soabort __P((struct socket *so));
297 int soaccept __P((struct socket *so, struct mbuf *nam));
298 int sobind __P((struct socket *so, struct mbuf *nam));
299 void socantrcvmore __P((struct socket *so));
300 void socantsendmore __P((struct socket *so));
301 int soclose __P((struct socket *so));
302 int soconnect __P((struct socket *so, struct mbuf *nam));
303 int soconnect2 __P((struct socket *so1, struct socket *so2));
304 int socreate __P((int dom, struct socket **aso, int type, int proto));
305 int sodisconnect __P((struct socket *so));
306 void sofree __P((struct socket *so));
307 int sogetopt __P((struct socket *so, int level, int optname,
309 void sohasoutofband __P((struct socket *so));
310 void soisconnected __P((struct socket *so));
311 void soisconnecting __P((struct socket *so));
312 void soisdisconnected __P((struct socket *so));
313 void soisdisconnecting __P((struct socket *so));
314 int solisten __P((struct socket *so, int backlog));
316 sonewconn1 __P((struct socket *head, int connstatus));
317 void soqinsque __P((struct socket *head, struct socket *so, int q));
318 int soqremque __P((struct socket *so, int q));
319 int soreceive __P((struct socket *so, struct mbuf **paddr, struct uio *uio,
320 struct mbuf **mp0, struct mbuf **controlp, int *flagsp));
321 int soreserve __P((struct socket *so, u_long sndcc, u_long rcvcc));
322 void sorflush __P((struct socket *so));
323 int sosend __P((struct socket *so, struct mbuf *addr, struct uio *uio,
324 struct mbuf *top, struct mbuf *control, int flags));
325 int sosetopt __P((struct socket *so, int level, int optname,
327 int soshutdown __P((struct socket *so, int how));
328 void sowakeup __P((struct socket *so, struct sockbuf *sb));
329 int sockargs __P((struct mbuf **, caddr_t, socklen_t, int));
332 int sendit __P((int, struct msghdr *, int, register_t *));
333 int recvit __P((int, struct msghdr *, caddr_t, register_t *));
335 int sendit __P((struct proc *, int, struct msghdr *, int, register_t *));
336 int recvit __P((struct proc *, int, struct msghdr *, caddr_t,
341 #endif // _SYS_SOCKETVAR_H_