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