xref: /csrg-svn/sys/kern/sys_socket.c (revision 12794)
1*12794Ssam /*	sys_socket.c	4.1	83/05/27	*/
2*12794Ssam 
3*12794Ssam #include "../h/param.h"
4*12794Ssam #include "../h/systm.h"
5*12794Ssam #include "../h/dir.h"
6*12794Ssam #include "../h/user.h"
7*12794Ssam #include "../h/file.h"
8*12794Ssam #include "../h/mbuf.h"
9*12794Ssam #include "../h/protosw.h"
10*12794Ssam #include "../h/socket.h"
11*12794Ssam #include "../h/socketvar.h"
12*12794Ssam #include "../h/ioctl.h"
13*12794Ssam #include "../h/uio.h"
14*12794Ssam #include "../h/stat.h"
15*12794Ssam 
16*12794Ssam #include "../net/if.h"
17*12794Ssam #include "../net/route.h"
18*12794Ssam 
19*12794Ssam int	soo_rw(), soo_ioctl(), soo_select(), soo_stat(), soo_close();
20*12794Ssam struct	fileops socketops =
21*12794Ssam     { soo_rw, soo_ioctl, soo_select, soo_stat, soo_close };
22*12794Ssam 
23*12794Ssam soo_rw(fp, rw, uio)
24*12794Ssam 	struct file *fp;
25*12794Ssam 	enum uio_rw rw;
26*12794Ssam 	struct uio *uio;
27*12794Ssam {
28*12794Ssam 	int soreceive(), sosend();
29*12794Ssam 
30*12794Ssam 	return (
31*12794Ssam 	    (*(rw==UIO_READ?soreceive:sosend))
32*12794Ssam 	      ((struct socket *)fp->f_data, 0, uio, 0, 0));
33*12794Ssam }
34*12794Ssam 
35*12794Ssam soo_ioctl(fp, cmd, data)
36*12794Ssam 	struct file *fp;
37*12794Ssam 	int cmd;
38*12794Ssam 	register caddr_t data;
39*12794Ssam {
40*12794Ssam 	register struct socket *so = (struct socket *)fp->f_data;
41*12794Ssam 
42*12794Ssam 	switch (cmd) {
43*12794Ssam 
44*12794Ssam 	case FIONBIO:
45*12794Ssam 		if (*(int *)data)
46*12794Ssam 			so->so_state |= SS_NBIO;
47*12794Ssam 		else
48*12794Ssam 			so->so_state &= ~SS_NBIO;
49*12794Ssam 		break;
50*12794Ssam 
51*12794Ssam 	case FIOASYNC:
52*12794Ssam 		if (*(int *)data)
53*12794Ssam 			so->so_state |= SS_ASYNC;
54*12794Ssam 		else
55*12794Ssam 			so->so_state &= ~SS_ASYNC;
56*12794Ssam 		break;
57*12794Ssam 
58*12794Ssam 	case SIOCSPGRP:
59*12794Ssam 		so->so_pgrp = *(int *)data;
60*12794Ssam 		break;
61*12794Ssam 
62*12794Ssam 	case SIOCGPGRP:
63*12794Ssam 		*(int *)data = so->so_pgrp;
64*12794Ssam 		break;
65*12794Ssam 
66*12794Ssam 	case SIOCATMARK:
67*12794Ssam 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
68*12794Ssam 		break;
69*12794Ssam 
70*12794Ssam 	/* routing table update calls */
71*12794Ssam 	case SIOCADDRT:
72*12794Ssam 	case SIOCDELRT:
73*12794Ssam 		if (!suser())
74*12794Ssam 			return (u.u_error);
75*12794Ssam 		return (rtrequest(cmd, (struct rtentry *)data));
76*12794Ssam 
77*12794Ssam 	/* interface parameter requests */
78*12794Ssam 	case SIOCSIFADDR:
79*12794Ssam 	case SIOCSIFFLAGS:
80*12794Ssam 	case SIOCSIFDSTADDR:
81*12794Ssam 		if (!suser())
82*12794Ssam 			return (u.u_error);
83*12794Ssam 		return (ifrequest(cmd, data));
84*12794Ssam 
85*12794Ssam 	case SIOCGIFADDR:
86*12794Ssam 	case SIOCGIFFLAGS:
87*12794Ssam 	case SIOCGIFDSTADDR:
88*12794Ssam 		return (ifrequest(cmd, data));
89*12794Ssam 
90*12794Ssam 	case SIOCGIFCONF:
91*12794Ssam 		return (ifconf(cmd, data));
92*12794Ssam 
93*12794Ssam 	/* type/protocol specific ioctls */
94*12794Ssam 	default:
95*12794Ssam 		return (ENOTTY);		/* XXX */
96*12794Ssam 	}
97*12794Ssam 	return (0);
98*12794Ssam }
99*12794Ssam 
100*12794Ssam soo_select(fp, which)
101*12794Ssam 	struct file *fp;
102*12794Ssam 	int which;
103*12794Ssam {
104*12794Ssam 	register struct socket *so = (struct socket *)fp->f_data;
105*12794Ssam 	register int s = splnet();
106*12794Ssam 
107*12794Ssam 	switch (which) {
108*12794Ssam 
109*12794Ssam 	case FREAD:
110*12794Ssam 		if (soreadable(so)) {
111*12794Ssam 			splx(s);
112*12794Ssam 			return (1);
113*12794Ssam 		}
114*12794Ssam 		sbselqueue(&so->so_rcv);
115*12794Ssam 		break;
116*12794Ssam 
117*12794Ssam 	case FWRITE:
118*12794Ssam 		if (sowriteable(so)) {
119*12794Ssam 			splx(s);
120*12794Ssam 			return (1);
121*12794Ssam 		}
122*12794Ssam 		sbselqueue(&so->so_snd);
123*12794Ssam 		break;
124*12794Ssam 	}
125*12794Ssam 	splx(s);
126*12794Ssam 	return (0);
127*12794Ssam }
128*12794Ssam 
129*12794Ssam soo_stat(fp, ub)
130*12794Ssam 	struct file *fp;
131*12794Ssam 	register struct stat *ub;
132*12794Ssam {
133*12794Ssam 	register struct socket *so = (struct socket *)fp->f_data;
134*12794Ssam 
135*12794Ssam 	bzero((caddr_t)ub, sizeof (*ub));
136*12794Ssam #ifdef notdef
137*12794Ssam 	return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
138*12794Ssam 	    (struct mbuf *)ub, (struct mbuf *)0,
139*12794Ssam 	    (struct mbuf *)0));
140*12794Ssam #endif
141*12794Ssam 	return (0);
142*12794Ssam }
143*12794Ssam 
144*12794Ssam soo_close(fp)
145*12794Ssam 	struct file *fp;
146*12794Ssam {
147*12794Ssam 	int error = soclose((struct socket *)fp->f_data);
148*12794Ssam 
149*12794Ssam 	fp->f_data = 0;
150*12794Ssam 	return (error);
151*12794Ssam }
152