2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
5 * Copyright (C) 2005 Neil Cafferkey
6 * Copyright (C) 2005 Pavel Fedin
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)socketvar.h 7.17 (Berkeley) 5/5/91
59 #ifndef SYS_SOCKETVAR_H
60 #define SYS_SOCKETVAR_H
69 * Kernel structure per socket.
70 * Contains send and receive buffer queues,
71 * handle on protocol and pointer to protocol
72 * private data and error information.
75 short so_type
; /* generic type, see socket.h */
76 short so_options
; /* from socket call, see socket.h */
77 struct timeval so_linger
; /* time to linger while closing */
78 short so_state
; /* internal state flags SS_*, below */
79 short so_refcnt
; /* reference count for this socket */
80 caddr_t so_pcb
; /* protocol control block */
81 struct protosw
*so_proto
; /* protocol handle */
83 * Variables for connection queueing.
84 * Socket where accepts occur is so_head in all subsidiary sockets.
85 * If so_head is 0, socket is not related to an accept.
86 * For head socket so_q0 queues partially completed connections,
87 * while so_q is a queue of connections ready to be accepted.
88 * If a connection is aborted and it has so_head set, then
89 * it has to be pulled out of either so_q0 or so_q.
90 * We allow connections to queue up based on current queue lengths
91 * and limit on number of queued connections for this socket.
93 struct socket
*so_head
; /* back pointer to accept socket */
94 struct socket
*so_q0
; /* queue of partial connections */
95 struct socket
*so_q
; /* queue of incoming connections */
96 short so_q0len
; /* partials on so_q0 */
97 short so_qlen
; /* number of connections on so_q */
98 short so_qlimit
; /* max number queued connections */
99 short so_timeo
; /* connection timeout */
100 u_short so_error
; /* error affecting connection */
101 struct SocketBase
*so_pgid
; /* owner for signals */
102 u_long so_oobmark
; /* chars to oob mark */
104 * Variables for socket buffering.
107 u_long sb_cc
; /* actual chars in buffer */
108 u_long sb_hiwat
; /* max actual char count */
109 u_long sb_mbcnt
; /* chars of mbufs used */
110 u_long sb_mbmax
; /* max chars of mbufs to use */
111 long sb_lowat
; /* low water mark */
112 struct mbuf
*sb_mb
; /* the mbuf chain */
113 struct newselitem
*sb_sel
; /* chain for selecting tasks */
114 short sb_flags
; /* flags, see below */
115 struct timeval sb_timeo
; /* timeout for read/write */
117 #define SB_MAX (64*1024) /* default for max chars in sockbuf */
118 #define SB_LOCK 0x01 /* lock on data queue */
119 #define SB_WANT 0x02 /* someone is waiting to lock */
120 #define SB_WAIT 0x04 /* someone is waiting for data/space */
121 #define SB_SEL 0x08 /* someone is selecting */
122 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
123 #define SB_NOTIFY (SB_WAIT|SB_SEL|SB_ASYNC)
124 #define SB_COLL 0x20 /* collision selecting */
125 #define SB_NOINTR 0x40 /* operations not interruptible */
127 u_long so_eventmask
; /* Events mask */
128 caddr_t so_tpcb
; /* Wisc. protocol control block XXX */
131 /* Socket event descriptor */
134 struct socket
*socket
;
141 #define SS_NOFDREF 0x001 /* no file table ref any more */
142 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */
143 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
144 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
145 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
146 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
147 #define SS_RCVATMARK 0x040 /* at mark on input */
149 #define SS_PRIV 0x080 /* privileged for broadcast, raw... */
150 #define SS_NBIO 0x100 /* non-blocking ops */
151 #define SS_ASYNC 0x200 /* async i/o notify */
152 #define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */
155 * Private socket options
157 #define SO_TTCP_SHUTUP 0x8000
160 * Macros for sockets and socket buffering.
164 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
165 * This is problematical if the fields are unsigned, as the space might
166 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
167 * overflow and return 0. Should use "lmin" but it doesn't exist now.
169 #define sbspace(sb) \
170 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
171 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
173 /* do we have to send all at once on a socket? */
174 #define sosendallatonce(so) \
175 ((so)->so_proto->pr_flags & PR_ATOMIC)
177 /* can we read something from so? */
178 #define soreadable(so) \
179 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
180 ((so)->so_state & SS_CANTRCVMORE) || \
181 (so)->so_qlen || (so)->so_error)
183 /* can we write something to so? */
184 #define sowriteable(so) \
185 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
186 (((so)->so_state&SS_ISCONNECTED) || \
187 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) \
188 || ((so)->so_state & SS_CANTSENDMORE) \
191 /* adjust counters in sb reflecting allocation of m */
192 #define sballoc(sb, m) { \
193 (sb)->sb_cc += (m)->m_len; \
194 (sb)->sb_mbcnt += MSIZE; \
195 if ((m)->m_flags & M_EXT) \
196 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
199 /* adjust counters in sb reflecting freeing of m */
200 #define sbfree(sb, m) { \
201 (sb)->sb_cc -= (m)->m_len; \
202 (sb)->sb_mbcnt -= MSIZE; \
203 if ((m)->m_flags & M_EXT) \
204 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
208 * Set lock on sockbuf sb; sleep if lock is already held.
209 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
210 * Returns error without lock if sleep is interrupted.
212 #define sblock(sb, p) ((sb)->sb_flags & SB_LOCK ? sb_lock(sb, p) : \
213 ((sb)->sb_flags |= SB_LOCK, 0))
215 /* release lock on sockbuf sb */
216 #define sbunlock(sb) { \
217 (sb)->sb_flags &= ~SB_LOCK; \
218 if ((sb)->sb_flags & SB_WANT) { \
219 (sb)->sb_flags &= ~SB_WANT; \
220 wakeup((caddr_t)&(sb)->sb_flags); \
224 #define sorwakeup(so) sowakeup((so), &(so)->so_rcv)
225 #define sowwakeup(so) sowakeup((so), &(so)->so_snd)
228 extern u_long sb_max
;
230 /* strings for sleep message: */
231 extern char netio
[], netcon
[], netcls
[];
234 #endif /* !SYS_SOCKETVAR_H */