xref: /original-bsd/sys/sys/socketvar.h (revision fbed46ce)
1 /*	socketvar.h	4.16	82/04/10	*/
2 
3 /*
4  * Kernel structure per socket.
5  * Contains send and receive buffer queues,
6  * handle on protocol and pointer to protocol
7  * private data and error information.
8  */
9 struct socket {
10 	short	so_type;		/* generic type, see socket.h */
11 	short	so_options;		/* from socket call, see socket.h */
12 	short	so_linger;		/* time to linger while closing */
13 	short	so_state;		/* internal state flags SS_*, below */
14 	caddr_t	so_pcb;			/* protocol control block */
15 	struct	protosw *so_proto;	/* protocol handle */
16 	struct	sockbuf {
17 		short	sb_cc;		/* actual chars in buffer */
18 		short	sb_hiwat;	/* max actual char count */
19 		short	sb_mbcnt;	/* chars of mbufs used */
20 		short	sb_mbmax;	/* max chars of mbufs to use */
21 		short	sb_lowat;	/* low water mark (not used yet) */
22 		short	sb_timeo;	/* timeout (not used yet) */
23 		struct	mbuf *sb_mb;	/* the mbuf chain */
24 		struct	proc *sb_sel;	/* process selecting read/write */
25 		short	sb_flags;	/* flags, see below */
26 	} so_rcv, so_snd;
27 #define	SB_LOCK		0x01		/* lock on data queue (so_rcv only) */
28 #define	SB_WANT		0x02		/* someone is waiting to lock */
29 #define	SB_WAIT		0x04		/* someone is waiting for data/space */
30 #define	SB_SEL		0x08		/* buffer is selected */
31 #define	SB_COLL		0x10		/* collision selecting */
32 	short	so_timeo;		/* connection timeout */
33 	u_short	so_error;		/* error affecting connection */
34 	short	so_oobmark;		/* chars to oob mark */
35 	short	so_pgrp;		/* pgrp for signals */
36 };
37 
38 /*
39  * Socket state bits.
40  */
41 #define	SS_USERGONE		0x001	/* no file table ref any more */
42 #define	SS_ISCONNECTED		0x002	/* socket connected to a peer */
43 #define	SS_ISCONNECTING		0x004	/* in process of connecting to peer */
44 #define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */
45 #define	SS_CANTSENDMORE		0x010	/* can't send more data to peer */
46 #define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
47 #define	SS_CONNAWAITING		0x040	/* connections awaiting acceptance */
48 #define	SS_RCVATMARK		0x080	/* at mark on input */
49 
50 #define	SS_PRIV			0x100	/* privileged for broadcast, raw... */
51 #define	SS_NBIO			0x200	/* non-blocking ops */
52 #define	SS_ASYNC		0x400	/* async i/o notify */
53 
54 /*
55  * Macros for sockets and socket buffering.
56  */
57 
58 /* how much space is there in a socket buffer (so->so_snd or so->so_rcv) */
59 #define	sbspace(sb) \
60     (MIN((sb)->sb_hiwat-(sb)->sb_cc, ((sb)->sb_mbmax-(sb)->sb_mbcnt)))
61 
62 /* do we have to send all at once on a socket? */
63 #define	sosendallatonce(so) \
64     (((so)->so_state & SS_NBIO) || ((so)->so_proto->pr_flags & PR_ATOMIC))
65 
66 /* can we read something from so? */
67 #define	soreadable(so) \
68     ((so)->so_rcv.sb_cc || ((so)->so_state & (SS_CANTRCVMORE|SS_CONNAWAITING)))
69 
70 /* can we write something to so? */
71 #define	sowriteable(so) \
72     (sbspace(&(so)->so_snd) > 0 && \
73 	(((so)->so_state&SS_ISCONNECTED) || \
74 	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0) || \
75      ((so)->so_state & SS_CANTSENDMORE))
76 
77 /* adjust counters in sb reflecting allocation of m */
78 #define	sballoc(sb, m) { \
79 	(sb)->sb_cc += (m)->m_len; \
80 	(sb)->sb_mbcnt += MSIZE; \
81 	if ((m)->m_off > MMAXOFF) \
82 		(sb)->sb_mbcnt += CLBYTES; \
83 }
84 
85 /* adjust counters in sb reflecting freeing of m */
86 #define	sbfree(sb, m) { \
87 	(sb)->sb_cc -= (m)->m_len; \
88 	(sb)->sb_mbcnt -= MSIZE; \
89 	if ((m)->m_off > MMAXOFF) \
90 		(sb)->sb_mbcnt -= CLBYTES; \
91 }
92 
93 /* set lock on sockbuf sb */
94 #define sblock(sb) { \
95 	while ((sb)->sb_flags & SB_LOCK) { \
96 		(sb)->sb_flags |= SB_WANT; \
97 		sleep((caddr_t)&(sb)->sb_flags, PZERO+1); \
98 	} \
99 	(sb)->sb_flags |= SB_LOCK; \
100 }
101 
102 /* release lock on sockbuf sb */
103 #define	sbunlock(sb) { \
104 	(sb)->sb_flags &= ~SB_LOCK; \
105 	if ((sb)->sb_flags & SB_WANT) { \
106 		(sb)->sb_flags &= ~SB_WANT; \
107 		wakeup((caddr_t)&(sb)->sb_flags); \
108 	} \
109 }
110 
111 #define	sorwakeup(so)	sbwakeup(&(so)->so_rcv)
112 #define	sowwakeup(so)	sbwakeup(&(so)->so_snd)
113