xref: /csrg-svn/sys/kern/sys_socket.c (revision 37478)
123386Smckusick /*
2*37478Ssklower  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
333921Sbostic  * All rights reserved.
423386Smckusick  *
533921Sbostic  * Redistribution and use in source and binary forms are permitted
634858Sbostic  * provided that the above copyright notice and this paragraph are
734858Sbostic  * duplicated in all such forms and that any documentation,
834858Sbostic  * advertising materials, and other materials related to such
934858Sbostic  * distribution and use acknowledge that the software was developed
1034858Sbostic  * by the University of California, Berkeley.  The name of the
1134858Sbostic  * University may not be used to endorse or promote products derived
1234858Sbostic  * from this software without specific prior written permission.
1334858Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434858Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534858Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633921Sbostic  *
17*37478Ssklower  *	@(#)sys_socket.c	7.4 (Berkeley) 04/22/89
1823386Smckusick  */
1912794Ssam 
2017095Sbloom #include "param.h"
2117095Sbloom #include "systm.h"
2217095Sbloom #include "dir.h"
2317095Sbloom #include "user.h"
2417095Sbloom #include "file.h"
2517095Sbloom #include "mbuf.h"
2617095Sbloom #include "protosw.h"
2717095Sbloom #include "socket.h"
2817095Sbloom #include "socketvar.h"
2917095Sbloom #include "ioctl.h"
3017095Sbloom #include "uio.h"
3117095Sbloom #include "stat.h"
3212794Ssam 
3312794Ssam #include "../net/if.h"
3412794Ssam #include "../net/route.h"
3512794Ssam 
3613045Ssam int	soo_rw(), soo_ioctl(), soo_select(), soo_close();
3712794Ssam struct	fileops socketops =
3813045Ssam     { soo_rw, soo_ioctl, soo_select, soo_close };
3912794Ssam 
4012794Ssam soo_rw(fp, rw, uio)
4112794Ssam 	struct file *fp;
4212794Ssam 	enum uio_rw rw;
4312794Ssam 	struct uio *uio;
4412794Ssam {
4512794Ssam 	int soreceive(), sosend();
4612794Ssam 
47*37478Ssklower 	return ((*(rw == UIO_READ ? soreceive : sosend))
48*37478Ssklower 	      ((struct socket *)fp->f_data, 0, uio, 0, 0, 0));
4912794Ssam }
5012794Ssam 
5112794Ssam soo_ioctl(fp, cmd, data)
5212794Ssam 	struct file *fp;
5312794Ssam 	int cmd;
5412794Ssam 	register caddr_t data;
5512794Ssam {
5612794Ssam 	register struct socket *so = (struct socket *)fp->f_data;
5712794Ssam 
5812794Ssam 	switch (cmd) {
5912794Ssam 
6012794Ssam 	case FIONBIO:
6112794Ssam 		if (*(int *)data)
6212794Ssam 			so->so_state |= SS_NBIO;
6312794Ssam 		else
6412794Ssam 			so->so_state &= ~SS_NBIO;
6513050Ssam 		return (0);
6612794Ssam 
6712794Ssam 	case FIOASYNC:
6812794Ssam 		if (*(int *)data)
6912794Ssam 			so->so_state |= SS_ASYNC;
7012794Ssam 		else
7112794Ssam 			so->so_state &= ~SS_ASYNC;
7213050Ssam 		return (0);
7312794Ssam 
7413001Ssam 	case FIONREAD:
7513001Ssam 		*(int *)data = so->so_rcv.sb_cc;
7613050Ssam 		return (0);
7713001Ssam 
7812794Ssam 	case SIOCSPGRP:
79*37478Ssklower 		so->so_pgid = *(int *)data;
8013050Ssam 		return (0);
8112794Ssam 
8212794Ssam 	case SIOCGPGRP:
83*37478Ssklower 		*(int *)data = so->so_pgid;
8413050Ssam 		return (0);
8512794Ssam 
8612794Ssam 	case SIOCATMARK:
8712794Ssam 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
8813050Ssam 		return (0);
8912794Ssam 	}
9013050Ssam 	/*
9113050Ssam 	 * Interface/routing/protocol specific ioctls:
9213050Ssam 	 * interface and routing ioctls should have a
9313050Ssam 	 * different entry since a socket's unnecessary
9413050Ssam 	 */
9513050Ssam #define	cmdbyte(x)	(((x) >> 8) & 0xff)
9613050Ssam 	if (cmdbyte(cmd) == 'i')
9718366Skarels 		return (ifioctl(so, cmd, data));
9813050Ssam 	if (cmdbyte(cmd) == 'r')
9913050Ssam 		return (rtioctl(cmd, data));
10013050Ssam 	return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
10113050Ssam 	    (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0));
10212794Ssam }
10312794Ssam 
10412794Ssam soo_select(fp, which)
10512794Ssam 	struct file *fp;
10612794Ssam 	int which;
10712794Ssam {
10812794Ssam 	register struct socket *so = (struct socket *)fp->f_data;
10912794Ssam 	register int s = splnet();
11012794Ssam 
11112794Ssam 	switch (which) {
11212794Ssam 
11312794Ssam 	case FREAD:
11412794Ssam 		if (soreadable(so)) {
11512794Ssam 			splx(s);
11612794Ssam 			return (1);
11712794Ssam 		}
11812794Ssam 		sbselqueue(&so->so_rcv);
11912794Ssam 		break;
12012794Ssam 
12112794Ssam 	case FWRITE:
12212794Ssam 		if (sowriteable(so)) {
12312794Ssam 			splx(s);
12412794Ssam 			return (1);
12512794Ssam 		}
12612794Ssam 		sbselqueue(&so->so_snd);
12712794Ssam 		break;
12824768Skarels 
12924768Skarels 	case 0:
13024768Skarels 		if (so->so_oobmark ||
13124768Skarels 		    (so->so_state & SS_RCVATMARK)) {
13224768Skarels 			splx(s);
13324768Skarels 			return (1);
13424768Skarels 		}
13524768Skarels 		sbselqueue(&so->so_rcv);
13624768Skarels 		break;
13712794Ssam 	}
13812794Ssam 	splx(s);
13912794Ssam 	return (0);
14012794Ssam }
14112794Ssam 
14213099Ssam /*ARGSUSED*/
14313045Ssam soo_stat(so, ub)
14413045Ssam 	register struct socket *so;
14512794Ssam 	register struct stat *ub;
14612794Ssam {
14712794Ssam 
14812794Ssam 	bzero((caddr_t)ub, sizeof (*ub));
14912794Ssam 	return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
15012794Ssam 	    (struct mbuf *)ub, (struct mbuf *)0,
15112794Ssam 	    (struct mbuf *)0));
15212794Ssam }
15312794Ssam 
15412794Ssam soo_close(fp)
15512794Ssam 	struct file *fp;
15612794Ssam {
15714032Ssam 	int error = 0;
15833921Sbostic 
15914032Ssam 	if (fp->f_data)
16014032Ssam 		error = soclose((struct socket *)fp->f_data);
16112794Ssam 	fp->f_data = 0;
16212794Ssam 	return (error);
16312794Ssam }
164