sys_socket.c revision 1.12 1 /* $NetBSD: sys_socket.c,v 1.12 1994/10/30 21:47:52 cgd 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.1 (Berkeley) 6/10/93
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/file.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47
48 #include <net/if.h>
49 #include <net/route.h>
50
51 struct fileops socketops =
52 { soo_read, soo_write, soo_ioctl, soo_select, soo_close };
53
54 /* ARGSUSED */
55 int
56 soo_read(fp, uio, cred)
57 struct file *fp;
58 struct uio *uio;
59 struct ucred *cred;
60 {
61
62 return (soreceive((struct socket *)fp->f_data, (struct mbuf **)0,
63 uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0));
64 }
65
66 /* ARGSUSED */
67 int
68 soo_write(fp, uio, cred)
69 struct file *fp;
70 struct uio *uio;
71 struct ucred *cred;
72 {
73
74 return (sosend((struct socket *)fp->f_data, (struct mbuf *)0,
75 uio, (struct mbuf *)0, (struct mbuf *)0, 0));
76 }
77
78 int
79 soo_ioctl(fp, cmd, data, p)
80 struct file *fp;
81 u_long cmd;
82 register caddr_t data;
83 struct proc *p;
84 {
85 register struct socket *so = (struct socket *)fp->f_data;
86
87 switch (cmd) {
88
89 case FIONBIO:
90 if (*(int *)data)
91 so->so_state |= SS_NBIO;
92 else
93 so->so_state &= ~SS_NBIO;
94 return (0);
95
96 case FIOASYNC:
97 if (*(int *)data) {
98 so->so_state |= SS_ASYNC;
99 so->so_rcv.sb_flags |= SB_ASYNC;
100 so->so_snd.sb_flags |= SB_ASYNC;
101 } else {
102 so->so_state &= ~SS_ASYNC;
103 so->so_rcv.sb_flags &= ~SB_ASYNC;
104 so->so_snd.sb_flags &= ~SB_ASYNC;
105 }
106 return (0);
107
108 case FIONREAD:
109 *(int *)data = so->so_rcv.sb_cc;
110 return (0);
111
112 case SIOCSPGRP:
113 so->so_pgid = *(int *)data;
114 return (0);
115
116 case SIOCGPGRP:
117 *(int *)data = so->so_pgid;
118 return (0);
119
120 case SIOCATMARK:
121 *(int *)data = (so->so_state&SS_RCVATMARK) != 0;
122 return (0);
123 }
124 /*
125 * Interface/routing/protocol specific ioctls:
126 * interface and routing ioctls should have a
127 * different entry since a socket's unnecessary
128 */
129 if (IOCGROUP(cmd) == 'i')
130 return (ifioctl(so, cmd, data, p));
131 if (IOCGROUP(cmd) == 'r')
132 return (rtioctl(cmd, data, p));
133 return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
134 (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0));
135 }
136
137 int
138 soo_select(fp, which, p)
139 struct file *fp;
140 int which;
141 struct proc *p;
142 {
143 register struct socket *so = (struct socket *)fp->f_data;
144 register int s = splnet();
145
146 switch (which) {
147
148 case FREAD:
149 if (soreadable(so)) {
150 splx(s);
151 return (1);
152 }
153 selrecord(p, &so->so_rcv.sb_sel);
154 so->so_rcv.sb_flags |= SB_SEL;
155 break;
156
157 case FWRITE:
158 if (sowriteable(so)) {
159 splx(s);
160 return (1);
161 }
162 selrecord(p, &so->so_snd.sb_sel);
163 so->so_snd.sb_flags |= SB_SEL;
164 break;
165
166 case 0:
167 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) {
168 splx(s);
169 return (1);
170 }
171 selrecord(p, &so->so_rcv.sb_sel);
172 so->so_rcv.sb_flags |= SB_SEL;
173 break;
174 }
175 splx(s);
176 return (0);
177 }
178
179 int
180 soo_stat(so, ub)
181 register struct socket *so;
182 register struct stat *ub;
183 {
184
185 bzero((caddr_t)ub, sizeof (*ub));
186 ub->st_mode = S_IFSOCK;
187 return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
188 (struct mbuf *)ub, (struct mbuf *)0,
189 (struct mbuf *)0));
190 }
191
192 /* ARGSUSED */
193 int
194 soo_close(fp, p)
195 struct file *fp;
196 struct proc *p;
197 {
198 int error = 0;
199
200 if (fp->f_data)
201 error = soclose((struct socket *)fp->f_data);
202 fp->f_data = 0;
203 return (error);
204 }
205