sys_socket.c revision 1.32 1 /* $NetBSD: sys_socket.c,v 1.32 2001/11/12 15:25:25 lukem Exp $ */
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)sys_socket.c 8.3 (Berkeley) 2/14/95
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.32 2001/11/12 15:25:25 lukem Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/file.h>
44 #include <sys/mbuf.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/ioctl.h>
49 #include <sys/stat.h>
50 #include <sys/poll.h>
51 #include <sys/proc.h>
52
53 #include <net/if.h>
54 #include <net/route.h>
55
56 struct fileops socketops =
57 { soo_read, soo_write, soo_ioctl, soo_fcntl, soo_poll, soo_stat, soo_close};
58
59 /* ARGSUSED */
60 int
61 soo_read(fp, offset, uio, cred, flags)
62 struct file *fp;
63 off_t *offset;
64 struct uio *uio;
65 struct ucred *cred;
66 int flags;
67 {
68 struct socket *so = (struct socket *) fp->f_data;
69 return ((*so->so_receive)(so, (struct mbuf **)0,
70 uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0));
71 }
72
73 /* ARGSUSED */
74 int
75 soo_write(fp, offset, uio, cred, flags)
76 struct file *fp;
77 off_t *offset;
78 struct uio *uio;
79 struct ucred *cred;
80 int flags;
81 {
82 struct socket *so = (struct socket *) fp->f_data;
83 return ((*so->so_send)(so, (struct mbuf *)0,
84 uio, (struct mbuf *)0, (struct mbuf *)0, 0));
85 }
86
87 int
88 soo_ioctl(fp, cmd, data, p)
89 struct file *fp;
90 u_long cmd;
91 caddr_t data;
92 struct proc *p;
93 {
94 struct socket *so = (struct socket *)fp->f_data;
95
96 switch (cmd) {
97
98 case FIONBIO:
99 if (*(int *)data)
100 so->so_state |= SS_NBIO;
101 else
102 so->so_state &= ~SS_NBIO;
103 return (0);
104
105 case FIOASYNC:
106 if (*(int *)data) {
107 so->so_state |= SS_ASYNC;
108 so->so_rcv.sb_flags |= SB_ASYNC;
109 so->so_snd.sb_flags |= SB_ASYNC;
110 } else {
111 so->so_state &= ~SS_ASYNC;
112 so->so_rcv.sb_flags &= ~SB_ASYNC;
113 so->so_snd.sb_flags &= ~SB_ASYNC;
114 }
115 return (0);
116
117 case FIONREAD:
118 *(int *)data = so->so_rcv.sb_cc;
119 return (0);
120
121 case SIOCSPGRP:
122 so->so_pgid = *(int *)data;
123 return (0);
124
125 case SIOCGPGRP:
126 *(int *)data = so->so_pgid;
127 return (0);
128
129 case SIOCATMARK:
130 *(int *)data = (so->so_state&SS_RCVATMARK) != 0;
131 return (0);
132 }
133 /*
134 * Interface/routing/protocol specific ioctls:
135 * interface and routing ioctls should have a
136 * different entry since a socket's unnecessary
137 */
138 if (IOCGROUP(cmd) == 'i')
139 return (ifioctl(so, cmd, data, p));
140 if (IOCGROUP(cmd) == 'r')
141 return (rtioctl(cmd, data, p));
142 return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
143 (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0, p));
144 }
145
146 int
147 soo_fcntl(fp, cmd, data, p)
148 struct file *fp;
149 u_int cmd;
150 caddr_t data;
151 struct proc *p;
152 {
153 if (cmd == F_SETFL)
154 return (0);
155 else
156 return (EOPNOTSUPP);
157 }
158
159 int
160 soo_poll(fp, events, p)
161 struct file *fp;
162 int events;
163 struct proc *p;
164 {
165 struct socket *so = (struct socket *)fp->f_data;
166 int revents = 0;
167 int s = splsoftnet();
168
169 if (events & (POLLIN | POLLRDNORM))
170 if (soreadable(so))
171 revents |= events & (POLLIN | POLLRDNORM);
172
173 if (events & (POLLOUT | POLLWRNORM))
174 if (sowriteable(so))
175 revents |= events & (POLLOUT | POLLWRNORM);
176
177 if (events & (POLLPRI | POLLRDBAND))
178 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
179 revents |= events & (POLLPRI | POLLRDBAND);
180
181 if (revents == 0) {
182 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
183 selrecord(p, &so->so_rcv.sb_sel);
184 so->so_rcv.sb_flags |= SB_SEL;
185 }
186
187 if (events & (POLLOUT | POLLWRNORM)) {
188 selrecord(p, &so->so_snd.sb_sel);
189 so->so_snd.sb_flags |= SB_SEL;
190 }
191 }
192
193 splx(s);
194 return (revents);
195 }
196
197 int
198 soo_stat(fp, ub, p)
199 struct file *fp;
200 struct stat *ub;
201 struct proc *p;
202 {
203 struct socket *so = (struct socket *)fp->f_data;
204
205 memset((caddr_t)ub, 0, sizeof(*ub));
206 ub->st_mode = S_IFSOCK;
207 return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
208 (struct mbuf *)ub, (struct mbuf *)0, (struct mbuf *)0, p));
209 }
210
211 /* ARGSUSED */
212 int
213 soo_close(fp, p)
214 struct file *fp;
215 struct proc *p;
216 {
217 int error = 0;
218
219 if (fp->f_data)
220 error = soclose((struct socket *)fp->f_data);
221 fp->f_data = 0;
222 return (error);
223 }
224