1 /* $OpenBSD: sys_socket.c,v 1.9 2003/09/23 16:51:12 millert Exp $ */ 2 /* $NetBSD: sys_socket.c,v 1.13 1995/08/12 23:59:09 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/file.h> 38 #include <sys/proc.h> 39 #include <sys/mbuf.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/ioctl.h> 44 #include <sys/poll.h> 45 #include <sys/stat.h> 46 47 #include <net/if.h> 48 #include <net/route.h> 49 50 struct fileops socketops = { 51 soo_read, soo_write, soo_ioctl, soo_poll, soo_kqfilter, 52 soo_stat, soo_close 53 }; 54 55 /* ARGSUSED */ 56 int 57 soo_read(fp, poff, uio, cred) 58 struct file *fp; 59 off_t *poff; 60 struct uio *uio; 61 struct ucred *cred; 62 { 63 64 return (soreceive((struct socket *)fp->f_data, (struct mbuf **)0, 65 uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0)); 66 } 67 68 /* ARGSUSED */ 69 int 70 soo_write(fp, poff, uio, cred) 71 struct file *fp; 72 off_t *poff; 73 struct uio *uio; 74 struct ucred *cred; 75 { 76 77 return (sosend((struct socket *)fp->f_data, (struct mbuf *)0, 78 uio, (struct mbuf *)0, (struct mbuf *)0, 0)); 79 } 80 81 int 82 soo_ioctl(fp, cmd, data, p) 83 struct file *fp; 84 u_long cmd; 85 register caddr_t data; 86 struct proc *p; 87 { 88 register struct socket *so = (struct socket *)fp->f_data; 89 90 switch (cmd) { 91 92 case FIONBIO: 93 if (*(int *)data) 94 so->so_state |= SS_NBIO; 95 else 96 so->so_state &= ~SS_NBIO; 97 return (0); 98 99 case FIOASYNC: 100 if (*(int *)data) { 101 so->so_state |= SS_ASYNC; 102 so->so_rcv.sb_flags |= SB_ASYNC; 103 so->so_snd.sb_flags |= SB_ASYNC; 104 } else { 105 so->so_state &= ~SS_ASYNC; 106 so->so_rcv.sb_flags &= ~SB_ASYNC; 107 so->so_snd.sb_flags &= ~SB_ASYNC; 108 } 109 return (0); 110 111 case FIONREAD: 112 *(int *)data = so->so_rcv.sb_cc; 113 return (0); 114 115 case SIOCSPGRP: 116 so->so_pgid = *(int *)data; 117 so->so_siguid = p->p_cred->p_ruid; 118 so->so_sigeuid = p->p_ucred->cr_uid; 119 return (0); 120 121 case SIOCGPGRP: 122 *(int *)data = so->so_pgid; 123 return (0); 124 125 case SIOCATMARK: 126 *(int *)data = (so->so_state&SS_RCVATMARK) != 0; 127 return (0); 128 } 129 /* 130 * Interface/routing/protocol specific ioctls: 131 * interface and routing ioctls should have a 132 * different entry since a socket's unnecessary 133 */ 134 if (IOCGROUP(cmd) == 'i') 135 return (ifioctl(so, cmd, data, p)); 136 if (IOCGROUP(cmd) == 'r') 137 return (rtioctl(cmd, data, p)); 138 return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 139 (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0)); 140 } 141 142 int 143 soo_poll(fp, events, p) 144 struct file *fp; 145 int events; 146 struct proc *p; 147 { 148 struct socket *so = (struct socket *)fp->f_data; 149 int revents = 0; 150 int s = splsoftnet(); 151 152 if (events & (POLLIN | POLLRDNORM)) { 153 if (soreadable(so)) 154 revents |= events & (POLLIN | POLLRDNORM); 155 } 156 if (events & (POLLOUT | POLLWRNORM)) { 157 if (sowriteable(so)) 158 revents |= events & (POLLOUT | POLLWRNORM); 159 } 160 if (events & (POLLPRI | POLLRDBAND)) { 161 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) 162 revents |= events & (POLLPRI | POLLRDBAND); 163 } 164 if (revents == 0) { 165 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) { 166 selrecord(p, &so->so_rcv.sb_sel); 167 so->so_rcv.sb_flags |= SB_SEL; 168 } 169 if (events & (POLLOUT | POLLWRNORM)) { 170 selrecord(p, &so->so_snd.sb_sel); 171 so->so_snd.sb_flags |= SB_SEL; 172 } 173 } 174 splx(s); 175 return (revents); 176 } 177 178 int 179 soo_stat(fp, ub, p) 180 struct file *fp; 181 struct stat *ub; 182 struct proc *p; 183 { 184 struct socket *so = (struct socket *)fp->f_data; 185 186 bzero((caddr_t)ub, sizeof (*ub)); 187 ub->st_mode = S_IFSOCK; 188 return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE, 189 (struct mbuf *)ub, (struct mbuf *)0, 190 (struct mbuf *)0)); 191 } 192 193 /* ARGSUSED */ 194 int 195 soo_close(fp, p) 196 struct file *fp; 197 struct proc *p; 198 { 199 int error = 0; 200 201 if (fp->f_data) 202 error = soclose((struct socket *)fp->f_data); 203 fp->f_data = 0; 204 return (error); 205 } 206