1 | /* $Id$ */ |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 1982, 1986, 1990, 1993 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * 4. Neither the name of the University nor the names of its contributors |
---|
16 | * may be used to endorse or promote products derived from this software |
---|
17 | * without specific prior written permission. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | * |
---|
31 | * @(#)socketvar.h 8.3 (Berkeley) 2/19/95 |
---|
32 | * $FreeBSD: src/sys/sys/socketvar.h,v 1.135 2004/10/18 22:19:43 rwatson Exp $ |
---|
33 | */ |
---|
34 | |
---|
35 | #ifndef _SYS_SOCKETVAR_H_ |
---|
36 | #define _SYS_SOCKETVAR_H_ |
---|
37 | |
---|
38 | #include <sys/queue.h> /* for TAILQ macros */ |
---|
39 | #include <sys/select.h> /* for struct selinfo */ |
---|
40 | |
---|
41 | /* |
---|
42 | * Kernel structure per socket. |
---|
43 | * Contains send and receive buffer queues, |
---|
44 | * handle on protocol and pointer to protocol |
---|
45 | * private data and error information. |
---|
46 | */ |
---|
47 | struct socket { |
---|
48 | short so_type; /* generic type, see socket.h */ |
---|
49 | short so_options; /* from socket call, see socket.h */ |
---|
50 | short so_linger; /* time to linger while closing */ |
---|
51 | short so_state; /* internal state flags SS_*, below */ |
---|
52 | void *so_pcb; /* protocol control block */ |
---|
53 | struct protosw *so_proto; /* protocol handle */ |
---|
54 | /* |
---|
55 | * Variables for connection queuing. |
---|
56 | * Socket where accepts occur is so_head in all subsidiary sockets. |
---|
57 | * If so_head is 0, socket is not related to an accept. |
---|
58 | * For head socket so_q0 queues partially completed connections, |
---|
59 | * while so_q is a queue of connections ready to be accepted. |
---|
60 | * If a connection is aborted and it has so_head set, then |
---|
61 | * it has to be pulled out of either so_q0 or so_q. |
---|
62 | * We allow connections to queue up based on current queue lengths |
---|
63 | * and limit on number of queued connections for this socket. |
---|
64 | */ |
---|
65 | struct socket *so_head; /* back pointer to accept socket */ |
---|
66 | TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */ |
---|
67 | TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */ |
---|
68 | TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */ |
---|
69 | short so_qlen; /* number of unaccepted connections */ |
---|
70 | short so_incqlen; /* number of unaccepted incomplete |
---|
71 | connections */ |
---|
72 | short so_qlimit; /* max number queued connections */ |
---|
73 | short so_timeo; /* connection timeout */ |
---|
74 | u_short so_error; /* error affecting connection */ |
---|
75 | pid_t so_pgid; /* pgid for signals */ |
---|
76 | u_long so_oobmark; /* chars to oob mark */ |
---|
77 | /* |
---|
78 | * Variables for socket buffering. |
---|
79 | */ |
---|
80 | struct sockbuf { |
---|
81 | u_int sb_cc; /* actual chars in buffer */ |
---|
82 | u_int sb_hiwat; /* max actual char count */ |
---|
83 | u_int sb_mbcnt; /* chars of mbufs used */ |
---|
84 | u_int sb_mbmax; /* max chars of mbufs to use */ |
---|
85 | int sb_lowat; /* low water mark */ |
---|
86 | struct mbuf *sb_mb; /* the mbuf chain */ |
---|
87 | struct selinfo sb_sel; /* process selecting read/write */ |
---|
88 | short sb_flags; /* flags, see below */ |
---|
89 | int sb_timeo; /* timeout for read/write */ |
---|
90 | void (*sb_wakeup) __P((struct socket *, caddr_t)); |
---|
91 | caddr_t sb_wakeuparg; /* arg for above */ |
---|
92 | } so_rcv, so_snd; |
---|
93 | #define SB_MAX (256*1024) /* default for max chars in sockbuf */ |
---|
94 | #define SB_LOCK 0x01 /* lock on data queue */ |
---|
95 | #define SB_WANT 0x02 /* someone is waiting to lock */ |
---|
96 | #define SB_WAIT 0x04 /* someone is waiting for data/space */ |
---|
97 | #define SB_SEL 0x08 /* someone is selecting */ |
---|
98 | #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ |
---|
99 | #define SB_NOTIFY (SB_WAIT|SB_SEL|SB_ASYNC) |
---|
100 | #define SB_NOINTR 0x40 /* operations not interruptible */ |
---|
101 | |
---|
102 | caddr_t so_tpcb; /* Wisc. protocol control block XXX */ |
---|
103 | void (*so_upcall)(struct socket *, void *arg, int); |
---|
104 | void *so_upcallarg; /* Arg for above */ |
---|
105 | uid_t so_uid; /* who opened the socket */ |
---|
106 | }; |
---|
107 | |
---|
108 | /* |
---|
109 | * Socket state bits. |
---|
110 | */ |
---|
111 | #define SS_NOFDREF 0x0001 /* no file table ref any more */ |
---|
112 | #define SS_ISCONNECTED 0x0002 /* socket connected to a peer */ |
---|
113 | #define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */ |
---|
114 | #define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */ |
---|
115 | #define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */ |
---|
116 | #define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */ |
---|
117 | #define SS_RCVATMARK 0x0040 /* at mark on input */ |
---|
118 | |
---|
119 | #define SS_PRIV 0x0080 /* privileged for broadcast, raw... */ |
---|
120 | #define SS_NBIO 0x0100 /* non-blocking ops */ |
---|
121 | #define SS_ASYNC 0x0200 /* async i/o notify */ |
---|
122 | #define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */ |
---|
123 | |
---|
124 | #define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */ |
---|
125 | #define SS_COMP 0x1000 /* unaccepted, complete connection */ |
---|
126 | |
---|
127 | |
---|
128 | /* |
---|
129 | * Macros for sockets and socket buffering. |
---|
130 | */ |
---|
131 | |
---|
132 | /* |
---|
133 | * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? |
---|
134 | * This is problematical if the fields are unsigned, as the space might |
---|
135 | * still be negative (cc > hiwat or mbcnt > mbmax). Should detect |
---|
136 | * overflow and return 0. Should use "lmin" but it doesn't exist now. |
---|
137 | */ |
---|
138 | #define sbspace(sb) \ |
---|
139 | ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \ |
---|
140 | (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) |
---|
141 | |
---|
142 | /* do we have to send all at once on a socket? */ |
---|
143 | #define sosendallatonce(so) \ |
---|
144 | ((so)->so_proto->pr_flags & PR_ATOMIC) |
---|
145 | |
---|
146 | /* can we read something from so? */ |
---|
147 | #define soreadable(so) \ |
---|
148 | ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ |
---|
149 | ((so)->so_state & SS_CANTRCVMORE) || \ |
---|
150 | (so)->so_comp.tqh_first || (so)->so_error) |
---|
151 | |
---|
152 | /* can we write something to so? */ |
---|
153 | #define sowriteable(so) \ |
---|
154 | ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ |
---|
155 | (((so)->so_state&SS_ISCONNECTED) || \ |
---|
156 | ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \ |
---|
157 | ((so)->so_state & SS_CANTSENDMORE) || \ |
---|
158 | (so)->so_error) |
---|
159 | |
---|
160 | /* adjust counters in sb reflecting allocation of m */ |
---|
161 | #define sballoc(sb, m) { \ |
---|
162 | (sb)->sb_cc += (m)->m_len; \ |
---|
163 | (sb)->sb_mbcnt += MSIZE; \ |
---|
164 | if ((m)->m_flags & M_EXT) \ |
---|
165 | (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ |
---|
166 | } |
---|
167 | |
---|
168 | /* adjust counters in sb reflecting freeing of m */ |
---|
169 | #define sbfree(sb, m) { \ |
---|
170 | (sb)->sb_cc -= (m)->m_len; \ |
---|
171 | (sb)->sb_mbcnt -= MSIZE; \ |
---|
172 | if ((m)->m_flags & M_EXT) \ |
---|
173 | (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ |
---|
174 | } |
---|
175 | |
---|
176 | /* |
---|
177 | * Set lock on sockbuf sb; sleep if lock is already held. |
---|
178 | * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. |
---|
179 | * Returns error without lock if sleep is interrupted. |
---|
180 | */ |
---|
181 | #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \ |
---|
182 | (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \ |
---|
183 | ((sb)->sb_flags |= SB_LOCK), 0) |
---|
184 | |
---|
185 | /* release lock on sockbuf sb */ |
---|
186 | #define sbunlock(sb) { \ |
---|
187 | (sb)->sb_flags &= ~SB_LOCK; \ |
---|
188 | if ((sb)->sb_flags & SB_WANT) { \ |
---|
189 | (sb)->sb_flags &= ~SB_WANT; \ |
---|
190 | wakeup((caddr_t)&(sb)->sb_flags); \ |
---|
191 | } \ |
---|
192 | } |
---|
193 | |
---|
194 | #define sorwakeup(so) { sowakeup((so), &(so)->so_rcv); \ |
---|
195 | if ((so)->so_upcall) \ |
---|
196 | (*((so)->so_upcall))((so), (so)->so_upcallarg, M_DONTWAIT); \ |
---|
197 | } |
---|
198 | |
---|
199 | #define sowwakeup(so) sowakeup((so), &(so)->so_snd) |
---|
200 | |
---|
201 | #ifdef _KERNEL |
---|
202 | extern u_long sb_max; |
---|
203 | |
---|
204 | /* to catch callers missing new second argument to sonewconn: */ |
---|
205 | #define sonewconn(head, connstatus) sonewconn1((head), (connstatus)) |
---|
206 | |
---|
207 | struct filedesc; |
---|
208 | struct mbuf; |
---|
209 | struct sockaddr; |
---|
210 | struct stat; |
---|
211 | |
---|
212 | /* |
---|
213 | * File operations on sockets. |
---|
214 | */ |
---|
215 | int soo_ioctl(struct file *fp, int cmd, caddr_t data, |
---|
216 | struct proc *p); |
---|
217 | int soo_select(struct file *fp, int which, struct proc *p); |
---|
218 | int soo_stat(struct socket *so, struct stat *ub); |
---|
219 | |
---|
220 | /* |
---|
221 | * From uipc_socket and friends |
---|
222 | */ |
---|
223 | int getsock(struct filedesc *fdp, int fdes, struct file **fpp); |
---|
224 | int sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type); |
---|
225 | void sbappend(struct sockbuf *sb, struct mbuf *m); |
---|
226 | int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, |
---|
227 | struct mbuf *m0, struct mbuf *control); |
---|
228 | int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, |
---|
229 | struct mbuf *control); |
---|
230 | void sbappendrecord(struct sockbuf *sb, struct mbuf *m0); |
---|
231 | void sbcheck(struct sockbuf *sb); |
---|
232 | void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n); |
---|
233 | struct mbuf * |
---|
234 | sbcreatecontrol(caddr_t p, int size, int type, int level); |
---|
235 | void sbdrop(struct sockbuf *sb, int len); |
---|
236 | void sbdroprecord(struct sockbuf *sb); |
---|
237 | void sbflush(struct sockbuf *sb); |
---|
238 | void sbinsertoob(struct sockbuf *sb, struct mbuf *m0); |
---|
239 | void sbrelease(struct sockbuf *sb); |
---|
240 | int sbreserve(struct sockbuf *sb, u_long cc); |
---|
241 | int sbwait(struct sockbuf *sb); |
---|
242 | int sb_lock(struct sockbuf *sb); |
---|
243 | int soabort(struct socket *so); |
---|
244 | int soaccept(struct socket *so, struct mbuf *nam); |
---|
245 | int sobind(struct socket *so, struct mbuf *nam); |
---|
246 | void socantrcvmore(struct socket *so); |
---|
247 | void socantsendmore(struct socket *so); |
---|
248 | int soclose(struct socket *so); |
---|
249 | int soconnect(struct socket *so, struct mbuf *nam); |
---|
250 | int soconnect2(struct socket *so1, struct socket *so2); |
---|
251 | int socreate(int dom, struct socket **aso, int type, int proto, |
---|
252 | struct proc *p); |
---|
253 | int sodisconnect(struct socket *so); |
---|
254 | void sofree(struct socket *so); |
---|
255 | int sogetopt(struct socket *so, int level, int optname, |
---|
256 | struct mbuf **mp); |
---|
257 | void sohasoutofband(struct socket *so); |
---|
258 | void soisconnected(struct socket *so); |
---|
259 | void soisconnecting(struct socket *so); |
---|
260 | void soisdisconnected(struct socket *so); |
---|
261 | void soisdisconnecting(struct socket *so); |
---|
262 | int solisten(struct socket *so, int backlog); |
---|
263 | struct socket * |
---|
264 | sodropablereq(struct socket *head); |
---|
265 | struct socket * |
---|
266 | sonewconn1(struct socket *head, int connstatus); |
---|
267 | int soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, |
---|
268 | struct mbuf **mp0, struct mbuf **controlp, int *flagsp); |
---|
269 | int soreserve(struct socket *so, u_long sndcc, u_long rcvcc); |
---|
270 | void sorflush(struct socket *so); |
---|
271 | int sosend(struct socket *so, struct mbuf *addr, struct uio *uio, |
---|
272 | struct mbuf *top, struct mbuf *control, int flags); |
---|
273 | int sosetopt(struct socket *so, int level, int optname, |
---|
274 | struct mbuf *m0); |
---|
275 | int soshutdown(struct socket *so, int how); |
---|
276 | void sowakeup(struct socket *so, struct sockbuf *sb); |
---|
277 | #endif /* _KERNEL */ |
---|
278 | |
---|
279 | #endif /* !_SYS_SOCKETVAR_H_ */ |
---|