add prototypes for socket file table ops
[unix-history] / usr / src / sys / sys / socketvar.h
CommitLineData
da7c5cc6 1/*
a379a142 2 * Copyright (c) 1982, 1986, 1990 Regents of the University of California.
5b519e94 3 * All rights reserved.
da7c5cc6 4 *
8429d022
MK
5 * Redistribution and use in source and binary forms are permitted provided
6 * that: (1) source distributions retain this entire copyright notice and
7 * comment, and (2) distributions including binaries display the following
8 * acknowledgement: ``This product includes software developed by the
9 * University of California, Berkeley and its contributors'' in the
10 * documentation or other materials provided with the distribution and in
11 * all advertising materials mentioning features or use of this software.
12 * Neither the name of the University nor the names of its contributors may
13 * be used to endorse or promote products derived from this software without
14 * specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
5b519e94 18 *
1c75820b 19 * @(#)socketvar.h 7.14 (Berkeley) %G%
da7c5cc6 20 */
e5bca009
BJ
21
22/*
23 * Kernel structure per socket.
24 * Contains send and receive buffer queues,
25 * handle on protocol and pointer to protocol
26 * private data and error information.
27 */
28struct socket {
29 short so_type; /* generic type, see socket.h */
30 short so_options; /* from socket call, see socket.h */
73f9aa0a 31 short so_linger; /* time to linger while closing */
541352f0 32 short so_state; /* internal state flags SS_*, below */
e5bca009
BJ
33 caddr_t so_pcb; /* protocol control block */
34 struct protosw *so_proto; /* protocol handle */
4147b3f6
BJ
35/*
36 * Variables for connection queueing.
37 * Socket where accepts occur is so_head in all subsidiary sockets.
38 * If so_head is 0, socket is not related to an accept.
39 * For head socket so_q0 queues partially completed connections,
40 * while so_q is a queue of connections ready to be accepted.
41 * If a connection is aborted and it has so_head set, then
42 * it has to be pulled out of either so_q0 or so_q.
43 * We allow connections to queue up based on current queue lengths
44 * and limit on number of queued connections for this socket.
45 */
46 struct socket *so_head; /* back pointer to accept socket */
47 struct socket *so_q0; /* queue of partial connections */
4147b3f6 48 struct socket *so_q; /* queue of incoming connections */
c14ea81b 49 short so_q0len; /* partials on so_q0 */
4147b3f6
BJ
50 short so_qlen; /* number of connections on so_q */
51 short so_qlimit; /* max number queued connections */
c14ea81b
MK
52 short so_timeo; /* connection timeout */
53 u_short so_error; /* error affecting connection */
98bd8ad7 54 pid_t so_pgid; /* pgid for signals */
c14ea81b 55 u_long so_oobmark; /* chars to oob mark */
4147b3f6
BJ
56/*
57 * Variables for socket buffering.
58 */
e5bca009 59 struct sockbuf {
c14ea81b
MK
60 u_long sb_cc; /* actual chars in buffer */
61 u_long sb_hiwat; /* max actual char count */
62 u_long sb_mbcnt; /* chars of mbufs used */
63 u_long sb_mbmax; /* max chars of mbufs to use */
a186a691 64 long sb_lowat; /* low water mark */
e5bca009
BJ
65 struct mbuf *sb_mb; /* the mbuf chain */
66 struct proc *sb_sel; /* process selecting read/write */
67 short sb_flags; /* flags, see below */
7131efa2 68 short sb_timeo; /* timeout for read/write */
e5bca009 69 } so_rcv, so_snd;
7131efa2 70#define SB_MAX (64*1024) /* default for max chars in sockbuf */
98bd8ad7 71#define SB_LOCK 0x01 /* lock on data queue */
2752c877 72#define SB_WANT 0x02 /* someone is waiting to lock */
e5bca009 73#define SB_WAIT 0x04 /* someone is waiting for data/space */
a186a691
MK
74#define SB_SEL 0x08 /* someone is selecting */
75#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
76#define SB_NOTIFY (SB_WAIT|SB_SEL|SB_ASYNC)
77#define SB_COLL 0x20 /* collision selecting */
78#define SB_NOINTR 0x40 /* operations not interruptible */
79
80 caddr_t so_tpcb; /* Wisc. protocol control block XXX */
e5bca009
BJ
81};
82
83/*
541352f0 84 * Socket state bits.
e5bca009 85 */
4147b3f6 86#define SS_NOFDREF 0x001 /* no file table ref any more */
1294fc17
BJ
87#define SS_ISCONNECTED 0x002 /* socket connected to a peer */
88#define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
89#define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
90#define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
91#define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
4147b3f6
BJ
92#define SS_RCVATMARK 0x040 /* at mark on input */
93
94#define SS_PRIV 0x080 /* privileged for broadcast, raw... */
95#define SS_NBIO 0x100 /* non-blocking ops */
96#define SS_ASYNC 0x200 /* async i/o notify */
f89d972e 97#define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */
1294fc17 98
541352f0
BJ
99
100/*
101 * Macros for sockets and socket buffering.
102 */
103
a186a691
MK
104/*
105 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
106 * This is problematical if the fields are unsigned, as the space might
107 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
108 * overflow and return 0. Should use "lmin" but it doesn't exist now.
109 */
541352f0 110#define sbspace(sb) \
a186a691
MK
111 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
112 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
541352f0
BJ
113
114/* do we have to send all at once on a socket? */
115#define sosendallatonce(so) \
fdd9fb12 116 ((so)->so_proto->pr_flags & PR_ATOMIC)
541352f0
BJ
117
118/* can we read something from so? */
119#define soreadable(so) \
a379a142
MK
120 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
121 ((so)->so_state & SS_CANTRCVMORE) || \
cb676735 122 (so)->so_qlen || (so)->so_error)
541352f0 123
49350a1c
BJ
124/* can we write something to so? */
125#define sowriteable(so) \
a379a142 126 (sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
7af46281 127 (((so)->so_state&SS_ISCONNECTED) || \
6b47a7a0 128 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0) || \
cb676735
MK
129 ((so)->so_state & SS_CANTSENDMORE) || \
130 (so)->so_error)
49350a1c 131
541352f0
BJ
132/* adjust counters in sb reflecting allocation of m */
133#define sballoc(sb, m) { \
134 (sb)->sb_cc += (m)->m_len; \
135 (sb)->sb_mbcnt += MSIZE; \
f89d972e 136 if ((m)->m_flags & M_EXT) \
a186a691 137 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
541352f0
BJ
138}
139
140/* adjust counters in sb reflecting freeing of m */
141#define sbfree(sb, m) { \
142 (sb)->sb_cc -= (m)->m_len; \
143 (sb)->sb_mbcnt -= MSIZE; \
f89d972e 144 if ((m)->m_flags & M_EXT) \
a186a691 145 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
541352f0
BJ
146}
147
a379a142
MK
148/*
149 * Set lock on sockbuf sb; sleep if lock is already held.
150 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
151 * Returns error without lock if sleep is interrupted.
152 */
153#define sblock(sb) ((sb)->sb_flags & SB_LOCK ? sb_lock(sb) : \
da0cce7e 154 ((sb)->sb_flags |= SB_LOCK, 0))
541352f0
BJ
155
156/* release lock on sockbuf sb */
157#define sbunlock(sb) { \
158 (sb)->sb_flags &= ~SB_LOCK; \
159 if ((sb)->sb_flags & SB_WANT) { \
160 (sb)->sb_flags &= ~SB_WANT; \
161 wakeup((caddr_t)&(sb)->sb_flags); \
162 } \
163}
164
62ca62ba
EC
165#define sorwakeup(so) sowakeup((so), &(so)->so_rcv)
166#define sowwakeup(so) sowakeup((so), &(so)->so_snd)
4147b3f6
BJ
167
168#ifdef KERNEL
a379a142
MK
169u_long sb_max;
170/* to catch callers missing new second argument to sonewconn: */
171#define sonewconn(head, connstatus) sonewconn1((head), (connstatus))
172struct socket *sonewconn1();
173
174/* strings for sleep message: */
175extern char netio[], netcon[], netcls[];
4147b3f6 176#endif
1c75820b
KM
177
178/*
179 * Operations on sockets.
180 */
181struct file;
182int soo_read __P(( /* read a socket into a uio structure */
183 struct file *fp,
184 struct uio *uio,
185 struct ucred *cred));
186int soo_write __P(( /* write socket from a uio structure */
187 struct file *fp,
188 struct uio *uio,
189 struct ucred *cred));
190int soo_ioctl __P(( /* do an ioctl operation on a socket */
191 struct file *fp,
192 int com,
193 caddr_t data,
194 struct proc *p));
195int soo_select __P(( /* do a select operation on a socket */
196 struct file *fp,
197 int which,
198 struct proc *p));
199int soo_close __P(( /* close socket */
200 struct file *fp,
201 struct proc *p));