socketvar.h revision 2165
11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1982, 1986, 1990, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 3. All advertising materials mentioning features or use of this software 141541Srgrimes * must display the following acknowledgement: 151541Srgrimes * This product includes software developed by the University of 161541Srgrimes * California, Berkeley and its contributors. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 331541Srgrimes * @(#)socketvar.h 8.1 (Berkeley) 6/2/93 342165Spaul * $Id: socketvar.h,v 1.3 1994/08/18 22:35:45 wollman Exp $ 351541Srgrimes */ 361541Srgrimes 372165Spaul#ifndef _SYS_SOCKETVAR_H_ 382165Spaul#define _SYS_SOCKETVAR_H_ 392165Spaul 401541Srgrimes#include <sys/select.h> /* for struct selinfo */ 411541Srgrimes 421541Srgrimes/* 431541Srgrimes * Kernel structure per socket. 441541Srgrimes * Contains send and receive buffer queues, 451541Srgrimes * handle on protocol and pointer to protocol 461541Srgrimes * private data and error information. 471541Srgrimes */ 481541Srgrimesstruct socket { 491541Srgrimes short so_type; /* generic type, see socket.h */ 501541Srgrimes short so_options; /* from socket call, see socket.h */ 511541Srgrimes short so_linger; /* time to linger while closing */ 521541Srgrimes short so_state; /* internal state flags SS_*, below */ 531541Srgrimes caddr_t so_pcb; /* protocol control block */ 541541Srgrimes struct protosw *so_proto; /* protocol handle */ 551541Srgrimes/* 561541Srgrimes * Variables for connection queueing. 571541Srgrimes * Socket where accepts occur is so_head in all subsidiary sockets. 581541Srgrimes * If so_head is 0, socket is not related to an accept. 591541Srgrimes * For head socket so_q0 queues partially completed connections, 601541Srgrimes * while so_q is a queue of connections ready to be accepted. 611541Srgrimes * If a connection is aborted and it has so_head set, then 621541Srgrimes * it has to be pulled out of either so_q0 or so_q. 631541Srgrimes * We allow connections to queue up based on current queue lengths 641541Srgrimes * and limit on number of queued connections for this socket. 651541Srgrimes */ 661541Srgrimes struct socket *so_head; /* back pointer to accept socket */ 671541Srgrimes struct socket *so_q0; /* queue of partial connections */ 681541Srgrimes struct socket *so_q; /* queue of incoming connections */ 691541Srgrimes short so_q0len; /* partials on so_q0 */ 701541Srgrimes short so_qlen; /* number of connections on so_q */ 711541Srgrimes short so_qlimit; /* max number queued connections */ 721541Srgrimes short so_timeo; /* connection timeout */ 731541Srgrimes u_short so_error; /* error affecting connection */ 741541Srgrimes pid_t so_pgid; /* pgid for signals */ 751541Srgrimes u_long so_oobmark; /* chars to oob mark */ 761541Srgrimes/* 771541Srgrimes * Variables for socket buffering. 781541Srgrimes */ 791541Srgrimes struct sockbuf { 801541Srgrimes u_long sb_cc; /* actual chars in buffer */ 811541Srgrimes u_long sb_hiwat; /* max actual char count */ 821541Srgrimes u_long sb_mbcnt; /* chars of mbufs used */ 831541Srgrimes u_long sb_mbmax; /* max chars of mbufs to use */ 841541Srgrimes long sb_lowat; /* low water mark */ 851541Srgrimes struct mbuf *sb_mb; /* the mbuf chain */ 861541Srgrimes struct selinfo sb_sel; /* process selecting read/write */ 871541Srgrimes short sb_flags; /* flags, see below */ 881541Srgrimes short sb_timeo; /* timeout for read/write */ 891541Srgrimes } so_rcv, so_snd; 901541Srgrimes#define SB_MAX (256*1024) /* default for max chars in sockbuf */ 911541Srgrimes#define SB_LOCK 0x01 /* lock on data queue */ 921541Srgrimes#define SB_WANT 0x02 /* someone is waiting to lock */ 931541Srgrimes#define SB_WAIT 0x04 /* someone is waiting for data/space */ 941541Srgrimes#define SB_SEL 0x08 /* someone is selecting */ 951541Srgrimes#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 961541Srgrimes#define SB_NOTIFY (SB_WAIT|SB_SEL|SB_ASYNC) 971541Srgrimes#define SB_NOINTR 0x40 /* operations not interruptible */ 981541Srgrimes 991541Srgrimes caddr_t so_tpcb; /* Wisc. protocol control block XXX */ 1001541Srgrimes void (*so_upcall) __P((struct socket *so, caddr_t arg, int waitf)); 1011541Srgrimes caddr_t so_upcallarg; /* Arg for above */ 1021541Srgrimes}; 1031541Srgrimes 1041541Srgrimes/* 1051541Srgrimes * Socket state bits. 1061541Srgrimes */ 1071541Srgrimes#define SS_NOFDREF 0x001 /* no file table ref any more */ 1081541Srgrimes#define SS_ISCONNECTED 0x002 /* socket connected to a peer */ 1091541Srgrimes#define SS_ISCONNECTING 0x004 /* in process of connecting to peer */ 1101541Srgrimes#define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */ 1111541Srgrimes#define SS_CANTSENDMORE 0x010 /* can't send more data to peer */ 1121541Srgrimes#define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */ 1131541Srgrimes#define SS_RCVATMARK 0x040 /* at mark on input */ 1141541Srgrimes 1151541Srgrimes#define SS_PRIV 0x080 /* privileged for broadcast, raw... */ 1161541Srgrimes#define SS_NBIO 0x100 /* non-blocking ops */ 1171541Srgrimes#define SS_ASYNC 0x200 /* async i/o notify */ 1181541Srgrimes#define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */ 1191541Srgrimes 1201541Srgrimes 1211541Srgrimes/* 1221541Srgrimes * Macros for sockets and socket buffering. 1231541Srgrimes */ 1241541Srgrimes 1251541Srgrimes/* 1261541Srgrimes * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 1271541Srgrimes * This is problematical if the fields are unsigned, as the space might 1281541Srgrimes * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 1291541Srgrimes * overflow and return 0. Should use "lmin" but it doesn't exist now. 1301541Srgrimes */ 1311541Srgrimes#define sbspace(sb) \ 1321541Srgrimes ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \ 1331541Srgrimes (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 1341541Srgrimes 1351541Srgrimes/* do we have to send all at once on a socket? */ 1361541Srgrimes#define sosendallatonce(so) \ 1371541Srgrimes ((so)->so_proto->pr_flags & PR_ATOMIC) 1381541Srgrimes 1391541Srgrimes/* can we read something from so? */ 1401541Srgrimes#define soreadable(so) \ 1411541Srgrimes ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 1421541Srgrimes ((so)->so_state & SS_CANTRCVMORE) || \ 1431541Srgrimes (so)->so_qlen || (so)->so_error) 1441541Srgrimes 1451541Srgrimes/* can we write something to so? */ 1461541Srgrimes#define sowriteable(so) \ 1471541Srgrimes (sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 1481541Srgrimes (((so)->so_state&SS_ISCONNECTED) || \ 1491541Srgrimes ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0) || \ 1501541Srgrimes ((so)->so_state & SS_CANTSENDMORE) || \ 1511541Srgrimes (so)->so_error) 1521541Srgrimes 1531541Srgrimes/* adjust counters in sb reflecting allocation of m */ 1541541Srgrimes#define sballoc(sb, m) { \ 1551541Srgrimes (sb)->sb_cc += (m)->m_len; \ 1561541Srgrimes (sb)->sb_mbcnt += MSIZE; \ 1571541Srgrimes if ((m)->m_flags & M_EXT) \ 1581541Srgrimes (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 1591541Srgrimes} 1601541Srgrimes 1611541Srgrimes/* adjust counters in sb reflecting freeing of m */ 1621541Srgrimes#define sbfree(sb, m) { \ 1631541Srgrimes (sb)->sb_cc -= (m)->m_len; \ 1641541Srgrimes (sb)->sb_mbcnt -= MSIZE; \ 1651541Srgrimes if ((m)->m_flags & M_EXT) \ 1661541Srgrimes (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 1671541Srgrimes} 1681541Srgrimes 1691541Srgrimes/* 1701541Srgrimes * Set lock on sockbuf sb; sleep if lock is already held. 1711541Srgrimes * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. 1721541Srgrimes * Returns error without lock if sleep is interrupted. 1731541Srgrimes */ 1741541Srgrimes#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \ 1751541Srgrimes (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \ 1761541Srgrimes ((sb)->sb_flags |= SB_LOCK), 0) 1771541Srgrimes 1781541Srgrimes/* release lock on sockbuf sb */ 1791541Srgrimes#define sbunlock(sb) { \ 1801541Srgrimes (sb)->sb_flags &= ~SB_LOCK; \ 1811541Srgrimes if ((sb)->sb_flags & SB_WANT) { \ 1821541Srgrimes (sb)->sb_flags &= ~SB_WANT; \ 1831541Srgrimes wakeup((caddr_t)&(sb)->sb_flags); \ 1841541Srgrimes } \ 1851541Srgrimes} 1861541Srgrimes 1871541Srgrimes#define sorwakeup(so) { sowakeup((so), &(so)->so_rcv); \ 1881541Srgrimes if ((so)->so_upcall) \ 1891541Srgrimes (*((so)->so_upcall))((so), (so)->so_upcallarg, M_DONTWAIT); \ 1901541Srgrimes } 1911541Srgrimes 1921541Srgrimes#define sowwakeup(so) sowakeup((so), &(so)->so_snd) 1931541Srgrimes 1941541Srgrimes#ifdef KERNEL 1952112Swollmanextern u_long sb_max; 1961541Srgrimes/* to catch callers missing new second argument to sonewconn: */ 1971541Srgrimes#define sonewconn(head, connstatus) sonewconn1((head), (connstatus)) 1981541Srgrimesstruct socket *sonewconn1 __P((struct socket *head, int connstatus)); 1991541Srgrimes 2001541Srgrimes/* strings for sleep message: */ 2011541Srgrimesextern char netio[], netcon[], netcls[]; 2021541Srgrimes 2031541Srgrimes/* 2041541Srgrimes * File operations on sockets. 2051541Srgrimes */ 2061541Srgrimesint soo_read __P((struct file *fp, struct uio *uio, struct ucred *cred)); 2071541Srgrimesint soo_write __P((struct file *fp, struct uio *uio, struct ucred *cred)); 2081541Srgrimesint soo_ioctl __P((struct file *fp, int com, caddr_t data, struct proc *p)); 2091541Srgrimesint soo_select __P((struct file *fp, int which, struct proc *p)); 2101541Srgrimesint soo_close __P((struct file *fp, struct proc *p)); 2111541Srgrimes#endif 2122165Spaul 2132165Spaul#endif 214