1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 #include <sys/selinfo.h>
36 #include <sys/queue.h>
37
38 TAILQ_HEAD(soqhead, socket);
39
40
41
42
43
44
45
46 struct socket {
47 short so_type;
48 short so_options;
49 short so_linger;
50 short so_state;
51 void *so_pcb;
52 struct protosw *so_proto;
53
54
55
56
57
58
59
60
61
62
63
64 struct socket *so_head;
65 struct soqhead *so_onq;
66 struct soqhead so_q0;
67 struct soqhead so_q;
68 TAILQ_ENTRY(socket) so_qe;
69 short so_q0len;
70 short so_qlen;
71 short so_qlimit;
72 short so_timeo;
73 u_short so_error;
74 pid_t so_pgid;
75 uid_t so_siguid;
76 uid_t so_sigeuid;
77 u_long so_oobmark;
78
79
80
81 struct sockbuf {
82 u_long sb_cc;
83 u_long sb_datacc;
84 u_long sb_hiwat;
85 u_long sb_mbcnt;
86 u_long sb_mbmax;
87 long sb_lowat;
88 struct mbuf *sb_mb;
89 struct mbuf *sb_mbtail;
90 struct mbuf *sb_lastrecord;
91
92 struct selinfo sb_sel;
93 short sb_flags;
94 short sb_timeo;
95 } so_rcv, so_snd;
96 #define SB_MAX (256*1024)
97 #define SB_LOCK 0x01
98 #define SB_WANT 0x02
99 #define SB_WAIT 0x04
100 #define SB_SEL 0x08
101 #define SB_ASYNC 0x10
102 #define SB_NOINTR 0x40
103 #define SB_KNOTE 0x80
104
105 void *so_internal;
106 void (*so_upcall)(struct socket *so, caddr_t arg, int waitf);
107 caddr_t so_upcallarg;
108 uid_t so_euid, so_ruid;
109 gid_t so_egid, so_rgid;
110 pid_t so_cpid;
111 };
112
113 #define SB_EMPTY_FIXUP(sb) \
114 do { \
115 if ((sb)->sb_mb == NULL) { \
116 (sb)->sb_mbtail = NULL; \
117 (sb)->sb_lastrecord = NULL; \
118 } \
119 } while (0)
120
121
122
123
124 #define SS_NOFDREF 0x001
125 #define SS_ISCONNECTED 0x002
126 #define SS_ISCONNECTING 0x004
127 #define SS_ISDISCONNECTING 0x008
128 #define SS_CANTSENDMORE 0x010
129 #define SS_CANTRCVMORE 0x020
130 #define SS_RCVATMARK 0x040
131 #define SS_ISDISCONNECTED 0x800
132
133 #define SS_PRIV 0x080
134 #define SS_NBIO 0x100
135 #define SS_ASYNC 0x200
136 #define SS_ISCONFIRMING 0x400
137 #define SS_CONNECTOUT 0x1000
138 #define SS_ISSENDING 0x2000
139
140
141
142
143
144
145
146
147 #define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC| \
148 SB_KNOTE)) != 0)
149
150
151
152
153
154
155
156 #define sbspace(sb) \
157 lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt)
158
159
160 #define sosendallatonce(so) \
161 ((so)->so_proto->pr_flags & PR_ATOMIC)
162
163
164 #define soissending(so) \
165 ((so)->so_state & SS_ISSENDING)
166
167
168 #define soreadable(so) \
169 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
170 ((so)->so_state & SS_CANTRCVMORE) || \
171 (so)->so_qlen || (so)->so_error)
172
173
174 #define sowriteable(so) \
175 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
176 (((so)->so_state&SS_ISCONNECTED) || \
177 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
178 ((so)->so_state & SS_CANTSENDMORE) || (so)->so_error)
179
180
181 #define sballoc(sb, m) { \
182 (sb)->sb_cc += (m)->m_len; \
183 if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME) \
184 (sb)->sb_datacc += (m)->m_len; \
185 (sb)->sb_mbcnt += MSIZE; \
186 if ((m)->m_flags & M_EXT) \
187 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
188 }
189
190
191 #define sbfree(sb, m) { \
192 (sb)->sb_cc -= (m)->m_len; \
193 if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME) \
194 (sb)->sb_datacc -= (m)->m_len; \
195 (sb)->sb_mbcnt -= MSIZE; \
196 if ((m)->m_flags & M_EXT) \
197 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
198 }
199
200
201
202
203
204
205 #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
206 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
207 ((sb)->sb_flags |= SB_LOCK, 0))
208
209
210 #define sbunlock(sb) { \
211 (sb)->sb_flags &= ~SB_LOCK; \
212 if ((sb)->sb_flags & SB_WANT) { \
213 (sb)->sb_flags &= ~SB_WANT; \
214 wakeup((caddr_t)&(sb)->sb_flags); \
215 } \
216 }
217
218 #define sorwakeup(so) { sowakeup((so), &(so)->so_rcv); \
219 if ((so)->so_upcall) \
220 (*((so)->so_upcall))((so), (so)->so_upcallarg, M_DONTWAIT); \
221 }
222
223 #define sowwakeup(so) sowakeup((so), &(so)->so_snd)
224
225 #ifdef _KERNEL
226 extern u_long sb_max;
227 struct socket *sonewconn(struct socket *head, int connstatus);
228
229
230 extern const char netio[], netcon[], netcls[];
231
232 extern struct pool socket_pool;
233
234 struct mbuf;
235 struct sockaddr;
236 struct proc;
237 struct msghdr;
238 struct stat;
239 struct knote;
240
241
242
243
244 int soo_read(struct file *fp, off_t *, struct uio *uio,
245 struct ucred *cred);
246 int soo_write(struct file *fp, off_t *, struct uio *uio,
247 struct ucred *cred);
248 int soo_ioctl(struct file *fp, u_long cmd, caddr_t data,
249 struct proc *p);
250 int soo_poll(struct file *fp, int events, struct proc *p);
251 int soo_kqfilter(struct file *fp, struct knote *kn);
252 int soo_close(struct file *fp, struct proc *p);
253 int soo_stat(struct file *, struct stat *, struct proc *);
254 int uipc_usrreq(struct socket *, int , struct mbuf *,
255 struct mbuf *, struct mbuf *);
256 void sbappend(struct sockbuf *sb, struct mbuf *m);
257 void sbappendstream(struct sockbuf *sb, struct mbuf *m);
258 int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
259 struct mbuf *m0, struct mbuf *control);
260 int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
261 struct mbuf *control);
262 void sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
263 void sbcheck(struct sockbuf *sb);
264 void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
265 struct mbuf *
266 sbcreatecontrol(caddr_t p, int size, int type, int level);
267 void sbdrop(struct sockbuf *sb, int len);
268 void sbdroprecord(struct sockbuf *sb);
269 void sbflush(struct sockbuf *sb);
270 void sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
271 void sbrelease(struct sockbuf *sb);
272 int sbcheckreserve(u_long cnt, u_long defcnt);
273 int sbreserve(struct sockbuf *sb, u_long cc);
274 int sbwait(struct sockbuf *sb);
275 int sb_lock(struct sockbuf *sb);
276 void soinit(void);
277 int soabort(struct socket *so);
278 int soaccept(struct socket *so, struct mbuf *nam);
279 int sobind(struct socket *so, struct mbuf *nam);
280 void socantrcvmore(struct socket *so);
281 void socantsendmore(struct socket *so);
282 int soclose(struct socket *so);
283 int soconnect(struct socket *so, struct mbuf *nam);
284 int soconnect2(struct socket *so1, struct socket *so2);
285 int socreate(int dom, struct socket **aso, int type, int proto);
286 int sodisconnect(struct socket *so);
287 void sofree(struct socket *so);
288 int sogetopt(struct socket *so, int level, int optname,
289 struct mbuf **mp);
290 void sohasoutofband(struct socket *so);
291 void soisconnected(struct socket *so);
292 void soisconnecting(struct socket *so);
293 void soisdisconnected(struct socket *so);
294 void soisdisconnecting(struct socket *so);
295 int solisten(struct socket *so, int backlog);
296 struct socket *sonewconn(struct socket *head, int connstatus);
297 void soqinsque(struct socket *head, struct socket *so, int q);
298 int soqremque(struct socket *so, int q);
299 int soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
300 struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
301 int soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
302 void sorflush(struct socket *so);
303 int sosend(struct socket *so, struct mbuf *addr, struct uio *uio,
304 struct mbuf *top, struct mbuf *control, int flags);
305 int sosetopt(struct socket *so, int level, int optname,
306 struct mbuf *m0);
307 int soshutdown(struct socket *so, int how);
308 void sowakeup(struct socket *so, struct sockbuf *sb);
309 int sockargs(struct mbuf **, const void *, size_t, int);
310
311 int sendit(struct proc *, int, struct msghdr *, int, register_t *);
312 int recvit(struct proc *, int, struct msghdr *, caddr_t,
313 register_t *);
314
315 #ifdef SOCKBUF_DEBUG
316 void sblastrecordchk(struct sockbuf *, const char *);
317 #define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where))
318
319 void sblastmbufchk(struct sockbuf *, const char *);
320 #define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where))
321 #else
322 #define SBLASTRECORDCHK(sb, where)
323 #define SBLASTMBUFCHK(sb, where)
324 #endif
325
326 #endif