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