proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / emulation / svr4 / svr4_stream.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1998 Mark Newton. All rights reserved.
3 * Copyright (c) 1994, 1996 Christos Zoulas. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Christos Zoulas.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $FreeBSD: src/sys/svr4/svr4_stream.c,v 1.12.2.2 2000/11/26 04:42:27 dillon Exp $
dadab5e9 31 * $DragonFly: src/sys/emulation/svr4/Attic/svr4_stream.c,v 1.4 2003/06/25 03:56:10 dillon Exp $
984263bc
MD
32 */
33
34/*
35 * Pretend that we have streams...
36 * Yes, this is gross.
37 *
38 * ToDo: The state machine for getmsg needs re-thinking
39 */
40
41#define COMPAT_43 1
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/buf.h>
46#include <sys/malloc.h>
47#include <sys/file.h>
48#include <sys/filedesc.h>
49#include <sys/fcntl.h>
50#include <sys/filio.h>
51#include <sys/select.h>
52#include <sys/socket.h>
53#include <sys/socketvar.h>
54#include <sys/un.h>
55#include <netinet/in.h>
56#include <sys/mbuf.h>
57#include <sys/protosw.h>
58#include <sys/signal.h>
59#include <sys/signalvar.h>
60#include <sys/uio.h>
61#include <sys/ktrace.h>
62#include <sys/proc.h>
63#include <sys/stat.h>
dadab5e9 64#include <sys/file2.h>
984263bc
MD
65
66#include <sys/sysproto.h>
67
68#include <svr4/svr4.h>
69#include <svr4/svr4_types.h>
70#include <svr4/svr4_util.h>
71#include <svr4/svr4_signal.h>
72#include <svr4/svr4_proto.h>
73#include <svr4/svr4_stropts.h>
74#include <svr4/svr4_timod.h>
75#include <svr4/svr4_sockmod.h>
76#include <svr4/svr4_ioctl.h>
77#include <svr4/svr4_socket.h>
78
79/* Utils */
41c20dac 80static int clean_pipe __P((const char *));
984263bc
MD
81static void getparm __P((struct file *, struct svr4_si_sockparms *));
82
83/* Address Conversions */
84static void sockaddr_to_netaddr_in __P((struct svr4_strmcmd *,
85 const struct sockaddr_in *));
86static void sockaddr_to_netaddr_un __P((struct svr4_strmcmd *,
87 const struct sockaddr_un *));
88static void netaddr_to_sockaddr_in __P((struct sockaddr_in *,
89 const struct svr4_strmcmd *));
90static void netaddr_to_sockaddr_un __P((struct sockaddr_un *,
91 const struct svr4_strmcmd *));
92
93/* stream ioctls */
dadab5e9 94static int i_nread __P((struct file *, struct thread *, register_t *, int,
984263bc 95 u_long, caddr_t));
dadab5e9 96static int i_fdinsert __P((struct file *, struct thread *, register_t *, int,
984263bc 97 u_long, caddr_t));
dadab5e9 98static int i_str __P((struct file *, struct thread *, register_t *, int,
984263bc 99 u_long, caddr_t));
dadab5e9 100static int i_setsig __P((struct file *, struct thread *, register_t *, int,
984263bc 101 u_long, caddr_t));
dadab5e9 102static int i_getsig __P((struct file *, struct thread *, register_t *, int,
984263bc 103 u_long, caddr_t));
dadab5e9 104static int _i_bind_rsvd __P((struct file *, struct thread *, register_t *, int,
984263bc 105 u_long, caddr_t));
dadab5e9 106static int _i_rele_rsvd __P((struct file *, struct thread *, register_t *, int,
984263bc
MD
107 u_long, caddr_t));
108
109/* i_str sockmod calls */
110static int sockmod __P((struct file *, int, struct svr4_strioctl *,
dadab5e9 111 struct thread *));
984263bc 112static int si_listen __P((struct file *, int, struct svr4_strioctl *,
dadab5e9 113 struct thread *));
984263bc 114static int si_ogetudata __P((struct file *, int, struct svr4_strioctl *,
dadab5e9 115 struct thread *));
984263bc 116static int si_sockparams __P((struct file *, int, struct svr4_strioctl *,
dadab5e9 117 struct thread *));
984263bc 118static int si_shutdown __P((struct file *, int, struct svr4_strioctl *,
dadab5e9 119 struct thread *));
984263bc 120static int si_getudata __P((struct file *, int, struct svr4_strioctl *,
dadab5e9 121 struct thread *));
984263bc
MD
122
123/* i_str timod calls */
124static int timod __P((struct file *, int, struct svr4_strioctl *,
dadab5e9 125 struct thread *));
984263bc 126static int ti_getinfo __P((struct file *, int, struct svr4_strioctl *,
dadab5e9 127 struct thread *));
984263bc 128static int ti_bind __P((struct file *, int, struct svr4_strioctl *,
dadab5e9 129 struct thread *));
984263bc
MD
130
131/* infrastructure */
dadab5e9 132static int svr4_sendit __P((struct thread *td, int s, struct msghdr *mp,
984263bc
MD
133 int flags));
134
dadab5e9 135static int svr4_recvit __P((struct thread *td, int s, struct msghdr *mp,
984263bc
MD
136 caddr_t namelenp));
137
138/* <sigh> Ok, so we shouldn't use sendit() in uipc_syscalls.c because
139 * it isn't part of a "public" interface; We're supposed to use
140 * pru_sosend instead. Same goes for recvit()/pru_soreceive() for
141 * that matter. Solution: Suck sendit()/recvit() into here where we
142 * can do what we like.
143 *
144 * I hate code duplication.
145 *
146 * I will take out all the #ifdef COMPAT_OLDSOCK gumph, though.
147 */
148static int
dadab5e9
MD
149svr4_sendit(td, s, mp, flags)
150 struct thread *td;
984263bc 151 int s;
41c20dac 152 struct msghdr *mp;
984263bc
MD
153 int flags;
154{
dadab5e9 155 struct proc *p;
984263bc
MD
156 struct file *fp;
157 struct uio auio;
41c20dac
MD
158 struct iovec *iov;
159 int i;
984263bc
MD
160 struct mbuf *control;
161 struct sockaddr *to;
162 int len, error;
163 struct socket *so;
164#ifdef KTRACE
165 struct iovec *ktriov = NULL;
166 struct uio ktruio;
167#endif
168
dadab5e9
MD
169 p = td->td_proc;
170 KKASSERT(p);
171
984263bc
MD
172 error = holdsock(p->p_fd, s, &fp);
173 if (error)
174 return (error);
175 auio.uio_iov = mp->msg_iov;
176 auio.uio_iovcnt = mp->msg_iovlen;
177 auio.uio_segflg = UIO_USERSPACE;
178 auio.uio_rw = UIO_WRITE;
dadab5e9 179 auio.uio_td = td;
984263bc
MD
180 auio.uio_offset = 0; /* XXX */
181 auio.uio_resid = 0;
182 iov = mp->msg_iov;
183 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
184 if ((auio.uio_resid += iov->iov_len) < 0) {
dadab5e9 185 fdrop(fp, td);
984263bc
MD
186 return (EINVAL);
187 }
188 }
189 if (mp->msg_name) {
190 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
191 if (error) {
dadab5e9 192 fdrop(fp, td);
984263bc
MD
193 return (error);
194 }
195 } else {
196 to = 0;
197 }
198 if (mp->msg_control) {
199 if (mp->msg_controllen < sizeof(struct cmsghdr)) {
200 error = EINVAL;
201 goto bad;
202 }
203 error = sockargs(&control, mp->msg_control,
204 mp->msg_controllen, MT_CONTROL);
205 if (error)
206 goto bad;
207 } else {
208 control = 0;
209 }
210#ifdef KTRACE
dadab5e9 211 if (KTRPOINT(p->p_thread, KTR_GENIO)) {
984263bc
MD
212 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
213
214 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
215 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
216 ktruio = auio;
217 }
218#endif
219 len = auio.uio_resid;
220 so = (struct socket *)fp->f_data;
dadab5e9
MD
221 error = so->so_proto->pr_usrreqs->pru_sosend
222 (so, to, &auio, 0, control, flags, td);
984263bc
MD
223 if (error) {
224 if (auio.uio_resid != len && (error == ERESTART ||
225 error == EINTR || error == EWOULDBLOCK))
226 error = 0;
227 if (error == EPIPE)
228 psignal(p, SIGPIPE);
229 }
230 if (error == 0)
231 p->p_retval[0] = len - auio.uio_resid;
232#ifdef KTRACE
233 if (ktriov != NULL) {
234 if (error == 0) {
235 ktruio.uio_iov = ktriov;
236 ktruio.uio_resid = p->p_retval[0];
237 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
238 }
239 FREE(ktriov, M_TEMP);
240 }
241#endif
242bad:
dadab5e9 243 fdrop(fp, td);
984263bc
MD
244 if (to)
245 FREE(to, M_SONAME);
246 return (error);
247}
248
249static int
dadab5e9
MD
250svr4_recvit(td, s, mp, namelenp)
251 struct thread *td;
984263bc 252 int s;
41c20dac 253 struct msghdr *mp;
984263bc
MD
254 caddr_t namelenp;
255{
dadab5e9 256 struct proc *p = td->td_proc;
984263bc
MD
257 struct file *fp;
258 struct uio auio;
41c20dac
MD
259 struct iovec *iov;
260 int i;
984263bc
MD
261 int len, error;
262 struct mbuf *m, *control = 0;
263 caddr_t ctlbuf;
264 struct socket *so;
265 struct sockaddr *fromsa = 0;
266#ifdef KTRACE
267 struct iovec *ktriov = NULL;
268 struct uio ktruio;
269#endif
dadab5e9
MD
270 p = td->td_proc;
271 KKASSERT(p);
984263bc
MD
272
273 error = holdsock(p->p_fd, s, &fp);
274 if (error)
275 return (error);
276 auio.uio_iov = mp->msg_iov;
277 auio.uio_iovcnt = mp->msg_iovlen;
278 auio.uio_segflg = UIO_USERSPACE;
279 auio.uio_rw = UIO_READ;
dadab5e9 280 auio.uio_td = td;
984263bc
MD
281 auio.uio_offset = 0; /* XXX */
282 auio.uio_resid = 0;
283 iov = mp->msg_iov;
284 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
285 if ((auio.uio_resid += iov->iov_len) < 0) {
dadab5e9 286 fdrop(fp, td);
984263bc
MD
287 return (EINVAL);
288 }
289 }
290#ifdef KTRACE
dadab5e9 291 if (KTRPOINT(p->p_thread, KTR_GENIO)) {
984263bc
MD
292 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
293
294 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
295 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
296 ktruio = auio;
297 }
298#endif
299 len = auio.uio_resid;
300 so = (struct socket *)fp->f_data;
301 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
302 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0,
303 &mp->msg_flags);
304 if (error) {
305 if (auio.uio_resid != len && (error == ERESTART ||
306 error == EINTR || error == EWOULDBLOCK))
307 error = 0;
308 }
309#ifdef KTRACE
310 if (ktriov != NULL) {
311 if (error == 0) {
312 ktruio.uio_iov = ktriov;
313 ktruio.uio_resid = len - auio.uio_resid;
314 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
315 }
316 FREE(ktriov, M_TEMP);
317 }
318#endif
319 if (error)
320 goto out;
321 p->p_retval[0] = len - auio.uio_resid;
322 if (mp->msg_name) {
323 len = mp->msg_namelen;
324 if (len <= 0 || fromsa == 0)
325 len = 0;
326 else {
327#ifndef MIN
328#define MIN(a,b) ((a)>(b)?(b):(a))
329#endif
330 /* save sa_len before it is destroyed by MSG_COMPAT */
331 len = MIN(len, fromsa->sa_len);
332 error = copyout(fromsa,
333 (caddr_t)mp->msg_name, (unsigned)len);
334 if (error)
335 goto out;
336 }
337 mp->msg_namelen = len;
338 if (namelenp &&
339 (error = copyout((caddr_t)&len, namelenp, sizeof (int)))) {
340 goto out;
341 }
342 }
343 if (mp->msg_control) {
344 len = mp->msg_controllen;
345 m = control;
346 mp->msg_controllen = 0;
347 ctlbuf = (caddr_t) mp->msg_control;
348
349 while (m && len > 0) {
350 unsigned int tocopy;
351
352 if (len >= m->m_len)
353 tocopy = m->m_len;
354 else {
355 mp->msg_flags |= MSG_CTRUNC;
356 tocopy = len;
357 }
358
359 if ((error = copyout((caddr_t)mtod(m, caddr_t),
360 ctlbuf, tocopy)) != 0)
361 goto out;
362
363 ctlbuf += tocopy;
364 len -= tocopy;
365 m = m->m_next;
366 }
367 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
368 }
369out:
370 if (fromsa)
371 FREE(fromsa, M_SONAME);
372 if (control)
373 m_freem(control);
dadab5e9 374 fdrop(fp, td);
984263bc
MD
375 return (error);
376}
377
378#ifdef DEBUG_SVR4
379static void bufprint __P((u_char *, size_t));
380static int show_ioc __P((const char *, struct svr4_strioctl *));
381static int show_strbuf __P((struct svr4_strbuf *));
382static void show_msg __P((const char *, int, struct svr4_strbuf *,
383 struct svr4_strbuf *, int));
384
385static void
386bufprint(buf, len)
387 u_char *buf;
388 size_t len;
389{
390 size_t i;
391
392 uprintf("\n\t");
393 for (i = 0; i < len; i++) {
394 uprintf("%x ", buf[i]);
395 if (i && (i % 16) == 0)
396 uprintf("\n\t");
397 }
398}
399
400static int
401show_ioc(str, ioc)
402 const char *str;
403 struct svr4_strioctl *ioc;
404{
405 u_char *ptr = (u_char *) malloc(ioc->len, M_TEMP, M_WAITOK);
406 int error;
407
408 uprintf("%s cmd = %ld, timeout = %d, len = %d, buf = %p { ",
409 str, ioc->cmd, ioc->timeout, ioc->len, ioc->buf);
410
411 if ((error = copyin(ioc->buf, ptr, ioc->len)) != 0) {
412 free((char *) ptr, M_TEMP);
413 return error;
414 }
415
416 bufprint(ptr, ioc->len);
417
418 uprintf("}\n");
419
420 free((char *) ptr, M_TEMP);
421 return 0;
422}
423
424
425static int
426show_strbuf(str)
427 struct svr4_strbuf *str;
428{
429 int error;
430 u_char *ptr = NULL;
431 int maxlen = str->maxlen;
432 int len = str->len;
433
434 if (maxlen < 0)
435 maxlen = 0;
436
437 if (len >= maxlen)
438 len = maxlen;
439
440 if (len > 0) {
441 ptr = (u_char *) malloc(len, M_TEMP, M_WAITOK);
442
443 if ((error = copyin(str->buf, ptr, len)) != 0) {
444 free((char *) ptr, M_TEMP);
445 return error;
446 }
447 }
448
449 uprintf(", { %d, %d, %p=[ ", str->maxlen, str->len, str->buf);
450
451 if (ptr)
452 bufprint(ptr, len);
453
454 uprintf("]}");
455
456 if (ptr)
457 free((char *) ptr, M_TEMP);
458
459 return 0;
460}
461
462
463static void
464show_msg(str, fd, ctl, dat, flags)
465 const char *str;
466 int fd;
467 struct svr4_strbuf *ctl;
468 struct svr4_strbuf *dat;
469 int flags;
470{
471 struct svr4_strbuf buf;
472 int error;
473
474 uprintf("%s(%d", str, fd);
475 if (ctl != NULL) {
476 if ((error = copyin(ctl, &buf, sizeof(buf))) != 0)
477 return;
478 show_strbuf(&buf);
479 }
480 else
481 uprintf(", NULL");
482
483 if (dat != NULL) {
484 if ((error = copyin(dat, &buf, sizeof(buf))) != 0)
485 return;
486 show_strbuf(&buf);
487 }
488 else
489 uprintf(", NULL");
490
491 uprintf(", %x);\n", flags);
492}
493
494#endif /* DEBUG_SVR4 */
495
496/*
497 * We are faced with an interesting situation. On svr4 unix sockets
498 * are really pipes. But we really have sockets, and we might as
499 * well use them. At the point where svr4 calls TI_BIND, it has
500 * already created a named pipe for the socket using mknod(2).
501 * We need to create a socket with the same name when we bind,
502 * so we need to remove the pipe before, otherwise we'll get address
503 * already in use. So we *carefully* remove the pipe, to avoid
504 * using this as a random file removal tool. We use system calls
505 * to avoid code duplication.
506 */
507static int
41c20dac 508clean_pipe(const char *path)
984263bc
MD
509{
510 struct lstat_args la;
511 struct unlink_args ua;
512 struct stat st;
513 int error;
514 caddr_t sg = stackgap_init();
515 size_t l = strlen(path) + 1;
516 void *tpath;
517
518 tpath = stackgap_alloc(&sg, l);
519 SCARG(&la, ub) = stackgap_alloc(&sg, sizeof(struct stat));
520
521 if ((error = copyout(path, tpath, l)) != 0)
522 return error;
523
524 SCARG(&la, path) = tpath;
525
41c20dac 526 if ((error = lstat(&la)) != 0)
984263bc
MD
527 return 0;
528
529 if ((error = copyin(SCARG(&la, ub), &st, sizeof(st))) != 0)
530 return 0;
531
532 /*
533 * Make sure we are dealing with a mode 0 named pipe.
534 */
535 if ((st.st_mode & S_IFMT) != S_IFIFO)
536 return 0;
537
538 if ((st.st_mode & ALLPERMS) != 0)
539 return 0;
540
541 SCARG(&ua, path) = SCARG(&la, path);
542
41c20dac 543 if ((error = unlink(&ua)) != 0) {
984263bc
MD
544 DPRINTF(("clean_pipe: unlink failed %d\n", error));
545 return error;
546 }
547
548 return 0;
549}
550
551
552static void
553sockaddr_to_netaddr_in(sc, sain)
554 struct svr4_strmcmd *sc;
555 const struct sockaddr_in *sain;
556{
557 struct svr4_netaddr_in *na;
558 na = SVR4_ADDROF(sc);
559
560 na->family = sain->sin_family;
561 na->port = sain->sin_port;
562 na->addr = sain->sin_addr.s_addr;
563 DPRINTF(("sockaddr_in -> netaddr %d %d %lx\n", na->family, na->port,
564 na->addr));
565}
566
567
568static void
569sockaddr_to_netaddr_un(sc, saun)
570 struct svr4_strmcmd *sc;
571 const struct sockaddr_un *saun;
572{
573 struct svr4_netaddr_un *na;
574 char *dst, *edst = ((char *) sc) + sc->offs + sizeof(na->family) + 1 -
575 sizeof(*sc);
576 const char *src;
577
578 na = SVR4_ADDROF(sc);
579 na->family = saun->sun_family;
580 for (src = saun->sun_path, dst = na->path; (*dst++ = *src++) != '\0'; )
581 if (dst == edst)
582 break;
583 DPRINTF(("sockaddr_un -> netaddr %d %s\n", na->family, na->path));
584}
585
586
587static void
588netaddr_to_sockaddr_in(sain, sc)
589 struct sockaddr_in *sain;
590 const struct svr4_strmcmd *sc;
591{
592 const struct svr4_netaddr_in *na;
593
594
595 na = SVR4_C_ADDROF(sc);
596 memset(sain, 0, sizeof(*sain));
597 sain->sin_len = sizeof(*sain);
598 sain->sin_family = na->family;
599 sain->sin_port = na->port;
600 sain->sin_addr.s_addr = na->addr;
601 DPRINTF(("netaddr -> sockaddr_in %d %d %x\n", sain->sin_family,
602 sain->sin_port, sain->sin_addr.s_addr));
603}
604
605
606static void
607netaddr_to_sockaddr_un(saun, sc)
608 struct sockaddr_un *saun;
609 const struct svr4_strmcmd *sc;
610{
611 const struct svr4_netaddr_un *na;
612 char *dst, *edst = &saun->sun_path[sizeof(saun->sun_path) - 1];
613 const char *src;
614
615 na = SVR4_C_ADDROF(sc);
616 memset(saun, 0, sizeof(*saun));
617 saun->sun_family = na->family;
618 for (src = na->path, dst = saun->sun_path; (*dst++ = *src++) != '\0'; )
619 if (dst == edst)
620 break;
621 saun->sun_len = dst - saun->sun_path;
622 DPRINTF(("netaddr -> sockaddr_un %d %s\n", saun->sun_family,
623 saun->sun_path));
624}
625
626
627static void
628getparm(fp, pa)
629 struct file *fp;
630 struct svr4_si_sockparms *pa;
631{
632 struct svr4_strm *st = svr4_stream_get(fp);
633 struct socket *so = (struct socket *) fp->f_data;
634
635 if (st == NULL)
636 return;
637
638 pa->family = st->s_family;
639
640 switch (so->so_type) {
641 case SOCK_DGRAM:
642 pa->type = SVR4_T_CLTS;
643 pa->protocol = IPPROTO_UDP;
644 DPRINTF(("getparm(dgram)\n"));
645 return;
646
647 case SOCK_STREAM:
648 pa->type = SVR4_T_COTS; /* What about T_COTS_ORD? XXX */
649 pa->protocol = IPPROTO_IP;
650 DPRINTF(("getparm(stream)\n"));
651 return;
652
653 case SOCK_RAW:
654 pa->type = SVR4_T_CLTS;
655 pa->protocol = IPPROTO_RAW;
656 DPRINTF(("getparm(raw)\n"));
657 return;
658
659 default:
660 pa->type = 0;
661 pa->protocol = 0;
662 DPRINTF(("getparm(type %d?)\n", so->so_type));
663 return;
664 }
665}
666
667
668static int
dadab5e9 669si_ogetudata(fp, fd, ioc, td)
984263bc
MD
670 struct file *fp;
671 int fd;
672 struct svr4_strioctl *ioc;
dadab5e9 673 struct thread *td;
984263bc
MD
674{
675 int error;
676 struct svr4_si_oudata ud;
677 struct svr4_si_sockparms pa;
678
679 if (ioc->len != sizeof(ud) && ioc->len != sizeof(ud) - sizeof(int)) {
680 DPRINTF(("SI_OGETUDATA: Wrong size %d != %d\n",
681 sizeof(ud), ioc->len));
682 return EINVAL;
683 }
684
685 if ((error = copyin(ioc->buf, &ud, sizeof(ud))) != 0)
686 return error;
687
688 getparm(fp, &pa);
689
690 switch (pa.family) {
691 case AF_INET:
692 ud.tidusize = 16384;
693 ud.addrsize = sizeof(struct svr4_sockaddr_in);
694 if (pa.type == SVR4_SOCK_STREAM)
695 ud.etsdusize = 1;
696 else
697 ud.etsdusize = 0;
698 break;
699
700 case AF_LOCAL:
701 ud.tidusize = 65536;
702 ud.addrsize = 128;
703 ud.etsdusize = 128;
704 break;
705
706 default:
707 DPRINTF(("SI_OGETUDATA: Unsupported address family %d\n",
708 pa.family));
709 return ENOSYS;
710 }
711
712 /* I have no idea what these should be! */
713 ud.optsize = 128;
714 ud.tsdusize = 128;
715
716 ud.servtype = pa.type;
717
718 /* XXX: Fixme */
719 ud.so_state = 0;
720 ud.so_options = 0;
721 return copyout(&ud, ioc->buf, ioc->len);
722}
723
724
725static int
dadab5e9 726si_sockparams(fp, fd, ioc, td)
984263bc
MD
727 struct file *fp;
728 int fd;
729 struct svr4_strioctl *ioc;
dadab5e9 730 struct thread *td;
984263bc
MD
731{
732 struct svr4_si_sockparms pa;
733
734 getparm(fp, &pa);
735 return copyout(&pa, ioc->buf, sizeof(pa));
736}
737
738
739static int
dadab5e9 740si_listen(fp, fd, ioc, td)
984263bc
MD
741 struct file *fp;
742 int fd;
743 struct svr4_strioctl *ioc;
dadab5e9 744 struct thread *td;
984263bc
MD
745{
746 int error;
747 struct svr4_strm *st = svr4_stream_get(fp);
748 struct svr4_strmcmd lst;
749 struct listen_args la;
750
751 if (st == NULL)
752 return EINVAL;
753
754 if ((error = copyin(ioc->buf, &lst, ioc->len)) != 0)
755 return error;
756
757 if (lst.cmd != SVR4_TI_OLD_BIND_REQUEST) {
758 DPRINTF(("si_listen: bad request %ld\n", lst.cmd));
759 return EINVAL;
760 }
761
762 /*
763 * We are making assumptions again...
764 */
765 SCARG(&la, s) = fd;
766 DPRINTF(("SI_LISTEN: fileno %d backlog = %d\n", fd, 5));
767 SCARG(&la, backlog) = 5;
768
41c20dac 769 if ((error = listen(&la)) != 0) {
984263bc
MD
770 DPRINTF(("SI_LISTEN: listen failed %d\n", error));
771 return error;
772 }
773
774 st->s_cmd = SVR4_TI__ACCEPT_WAIT;
775 lst.cmd = SVR4_TI_BIND_REPLY;
776
777 switch (st->s_family) {
778 case AF_INET:
779 /* XXX: Fill the length here */
780 break;
781
782 case AF_LOCAL:
783 lst.len = 140;
784 lst.pad[28] = 0x00000000; /* magic again */
785 lst.pad[29] = 0x00000800; /* magic again */
786 lst.pad[30] = 0x80001400; /* magic again */
787 break;
788
789 default:
790 DPRINTF(("SI_LISTEN: Unsupported address family %d\n",
791 st->s_family));
792 return ENOSYS;
793 }
794
795
796 if ((error = copyout(&lst, ioc->buf, ioc->len)) != 0)
797 return error;
798
799 return 0;
800}
801
802
803static int
dadab5e9 804si_getudata(fp, fd, ioc, td)
984263bc
MD
805 struct file *fp;
806 int fd;
807 struct svr4_strioctl *ioc;
dadab5e9 808 struct thread *td;
984263bc
MD
809{
810 int error;
811 struct svr4_si_udata ud;
812
813 if (sizeof(ud) != ioc->len) {
814 DPRINTF(("SI_GETUDATA: Wrong size %d != %d\n",
815 sizeof(ud), ioc->len));
816 return EINVAL;
817 }
818
819 if ((error = copyin(ioc->buf, &ud, sizeof(ud))) != 0)
820 return error;
821
822 getparm(fp, &ud.sockparms);
823
824 switch (ud.sockparms.family) {
825 case AF_INET:
826 DPRINTF(("getudata_inet\n"));
827 ud.tidusize = 16384;
828 ud.tsdusize = 16384;
829 ud.addrsize = sizeof(struct svr4_sockaddr_in);
830 if (ud.sockparms.type == SVR4_SOCK_STREAM)
831 ud.etsdusize = 1;
832 else
833 ud.etsdusize = 0;
834 ud.optsize = 0;
835 break;
836
837 case AF_LOCAL:
838 DPRINTF(("getudata_local\n"));
839 ud.tidusize = 65536;
840 ud.tsdusize = 128;
841 ud.addrsize = 128;
842 ud.etsdusize = 128;
843 ud.optsize = 128;
844 break;
845
846 default:
847 DPRINTF(("SI_GETUDATA: Unsupported address family %d\n",
848 ud.sockparms.family));
849 return ENOSYS;
850 }
851
852
853 ud.servtype = ud.sockparms.type;
854 DPRINTF(("ud.servtype = %d\n", ud.servtype));
855 /* XXX: Fixme */
856 ud.so_state = 0;
857 ud.so_options = 0;
858 return copyout(&ud, ioc->buf, sizeof(ud));
859}
860
861
862static int
dadab5e9 863si_shutdown(fp, fd, ioc, td)
984263bc
MD
864 struct file *fp;
865 int fd;
866 struct svr4_strioctl *ioc;
dadab5e9 867 struct thread *td;
984263bc
MD
868{
869 int error;
870 struct shutdown_args ap;
871
872 if (ioc->len != sizeof(SCARG(&ap, how))) {
873 DPRINTF(("SI_SHUTDOWN: Wrong size %d != %d\n",
874 sizeof(SCARG(&ap, how)), ioc->len));
875 return EINVAL;
876 }
877
878 if ((error = copyin(ioc->buf, &SCARG(&ap, how), ioc->len)) != 0)
879 return error;
880
881 SCARG(&ap, s) = fd;
882
41c20dac 883 return shutdown(&ap);
984263bc
MD
884}
885
886
887static int
dadab5e9 888sockmod(fp, fd, ioc, td)
984263bc
MD
889 struct file *fp;
890 int fd;
891 struct svr4_strioctl *ioc;
dadab5e9 892 struct thread *td;
984263bc
MD
893{
894 switch (ioc->cmd) {
895 case SVR4_SI_OGETUDATA:
896 DPRINTF(("SI_OGETUDATA\n"));
dadab5e9 897 return si_ogetudata(fp, fd, ioc, td);
984263bc
MD
898
899 case SVR4_SI_SHUTDOWN:
900 DPRINTF(("SI_SHUTDOWN\n"));
dadab5e9 901 return si_shutdown(fp, fd, ioc, td);
984263bc
MD
902
903 case SVR4_SI_LISTEN:
904 DPRINTF(("SI_LISTEN\n"));
dadab5e9 905 return si_listen(fp, fd, ioc, td);
984263bc
MD
906
907 case SVR4_SI_SETMYNAME:
908 DPRINTF(("SI_SETMYNAME\n"));
909 return 0;
910
911 case SVR4_SI_SETPEERNAME:
912 DPRINTF(("SI_SETPEERNAME\n"));
913 return 0;
914
915 case SVR4_SI_GETINTRANSIT:
916 DPRINTF(("SI_GETINTRANSIT\n"));
917 return 0;
918
919 case SVR4_SI_TCL_LINK:
920 DPRINTF(("SI_TCL_LINK\n"));
921 return 0;
922
923 case SVR4_SI_TCL_UNLINK:
924 DPRINTF(("SI_TCL_UNLINK\n"));
925 return 0;
926
927 case SVR4_SI_SOCKPARAMS:
928 DPRINTF(("SI_SOCKPARAMS\n"));
dadab5e9 929 return si_sockparams(fp, fd, ioc, td);
984263bc
MD
930
931 case SVR4_SI_GETUDATA:
932 DPRINTF(("SI_GETUDATA\n"));
dadab5e9 933 return si_getudata(fp, fd, ioc, td);
984263bc
MD
934
935 default:
936 DPRINTF(("Unknown sockmod ioctl %lx\n", ioc->cmd));
937 return 0;
938
939 }
940}
941
942
943static int
dadab5e9 944ti_getinfo(fp, fd, ioc, td)
984263bc
MD
945 struct file *fp;
946 int fd;
947 struct svr4_strioctl *ioc;
dadab5e9 948 struct thread *td;
984263bc
MD
949{
950 int error;
951 struct svr4_infocmd info;
952
953 memset(&info, 0, sizeof(info));
954
955 if ((error = copyin(ioc->buf, &info, ioc->len)) != 0)
956 return error;
957
958 if (info.cmd != SVR4_TI_INFO_REQUEST)
959 return EINVAL;
960
961 info.cmd = SVR4_TI_INFO_REPLY;
962 info.tsdu = 0;
963 info.etsdu = 1;
964 info.cdata = -2;
965 info.ddata = -2;
966 info.addr = 16;
967 info.opt = -1;
968 info.tidu = 16384;
969 info.serv = 2;
970 info.current = 0;
971 info.provider = 2;
972
973 ioc->len = sizeof(info);
974 if ((error = copyout(&info, ioc->buf, ioc->len)) != 0)
975 return error;
976
977 return 0;
978}
979
980
981static int
dadab5e9 982ti_bind(fp, fd, ioc, td)
984263bc
MD
983 struct file *fp;
984 int fd;
985 struct svr4_strioctl *ioc;
dadab5e9 986 struct thread *td;
984263bc
MD
987{
988 int error;
989 struct svr4_strm *st = svr4_stream_get(fp);
990 struct sockaddr_in sain;
991 struct sockaddr_un saun;
992 caddr_t sg;
993 void *skp, *sup = NULL;
994 int sasize;
995 struct svr4_strmcmd bnd;
996 struct bind_args ba;
997
998 if (st == NULL) {
999 DPRINTF(("ti_bind: bad file descriptor\n"));
1000 return EINVAL;
1001 }
1002
1003 if ((error = copyin(ioc->buf, &bnd, ioc->len)) != 0)
1004 return error;
1005
1006 if (bnd.cmd != SVR4_TI_OLD_BIND_REQUEST) {
1007 DPRINTF(("ti_bind: bad request %ld\n", bnd.cmd));
1008 return EINVAL;
1009 }
1010
1011 switch (st->s_family) {
1012 case AF_INET:
1013 skp = &sain;
1014 sasize = sizeof(sain);
1015
1016 if (bnd.offs == 0)
1017 goto reply;
1018
1019 netaddr_to_sockaddr_in(&sain, &bnd);
1020
1021 DPRINTF(("TI_BIND: fam %d, port %d, addr %x\n",
1022 sain.sin_family, sain.sin_port,
1023 sain.sin_addr.s_addr));
1024 break;
1025
1026 case AF_LOCAL:
1027 skp = &saun;
1028 sasize = sizeof(saun);
1029 if (bnd.offs == 0)
1030 goto reply;
1031
1032 netaddr_to_sockaddr_un(&saun, &bnd);
1033
1034 if (saun.sun_path[0] == '\0')
1035 goto reply;
1036
1037 DPRINTF(("TI_BIND: fam %d, path %s\n",
1038 saun.sun_family, saun.sun_path));
1039
41c20dac 1040 if ((error = clean_pipe(saun.sun_path)) != 0)
984263bc
MD
1041 return error;
1042
1043 bnd.pad[28] = 0x00001000; /* magic again */
1044 break;
1045
1046 default:
1047 DPRINTF(("TI_BIND: Unsupported address family %d\n",
1048 st->s_family));
1049 return ENOSYS;
1050 }
1051
1052 sg = stackgap_init();
1053 sup = stackgap_alloc(&sg, sasize);
1054
1055 if ((error = copyout(skp, sup, sasize)) != 0)
1056 return error;
1057
1058 SCARG(&ba, s) = fd;
1059 DPRINTF(("TI_BIND: fileno %d\n", fd));
1060 SCARG(&ba, name) = (void *) sup;
1061 SCARG(&ba, namelen) = sasize;
1062
41c20dac 1063 if ((error = bind(&ba)) != 0) {
984263bc
MD
1064 DPRINTF(("TI_BIND: bind failed %d\n", error));
1065 return error;
1066 }
1067
1068reply:
1069 if (sup == NULL) {
1070 memset(&bnd, 0, sizeof(bnd));
1071 bnd.len = sasize + 4;
1072 bnd.offs = 0x10; /* XXX */
1073 }
1074
1075 bnd.cmd = SVR4_TI_BIND_REPLY;
1076
1077 if ((error = copyout(&bnd, ioc->buf, ioc->len)) != 0)
1078 return error;
1079
1080 return 0;
1081}
1082
1083
1084static int
dadab5e9 1085timod(fp, fd, ioc, td)
984263bc
MD
1086 struct file *fp;
1087 int fd;
1088 struct svr4_strioctl *ioc;
dadab5e9 1089 struct thread *td;
984263bc
MD
1090{
1091 switch (ioc->cmd) {
1092 case SVR4_TI_GETINFO:
1093 DPRINTF(("TI_GETINFO\n"));
dadab5e9 1094 return ti_getinfo(fp, fd, ioc, td);
984263bc
MD
1095
1096 case SVR4_TI_OPTMGMT:
1097 DPRINTF(("TI_OPTMGMT\n"));
1098 return 0;
1099
1100 case SVR4_TI_BIND:
1101 DPRINTF(("TI_BIND\n"));
dadab5e9 1102 return ti_bind(fp, fd, ioc, td);
984263bc
MD
1103
1104 case SVR4_TI_UNBIND:
1105 DPRINTF(("TI_UNBIND\n"));
1106 return 0;
1107
1108 default:
1109 DPRINTF(("Unknown timod ioctl %lx\n", ioc->cmd));
1110 return 0;
1111 }
1112}
1113
1114
1115int
dadab5e9 1116svr4_stream_ti_ioctl(fp, td, retval, fd, cmd, dat)
984263bc 1117 struct file *fp;
dadab5e9 1118 struct thread *td;
984263bc
MD
1119 register_t *retval;
1120 int fd;
1121 u_long cmd;
1122 caddr_t dat;
1123{
1124 struct svr4_strbuf skb, *sub = (struct svr4_strbuf *) dat;
1125 struct svr4_strm *st = svr4_stream_get(fp);
1126 int error;
1127 void *skp, *sup;
1128 struct sockaddr_in sain;
1129 struct sockaddr_un saun;
1130 struct svr4_strmcmd sc;
1131 int sasize;
1132 caddr_t sg;
1133 int *lenp;
1134
1135 DPRINTF(("svr4_stream_ti_ioctl\n"));
1136
1137 if (st == NULL)
1138 return EINVAL;
1139
1140 sc.offs = 0x10;
1141
1142 if ((error = copyin(sub, &skb, sizeof(skb))) != 0) {
1143 DPRINTF(("ti_ioctl: error copying in strbuf\n"));
1144 return error;
1145 }
1146
1147 switch (st->s_family) {
1148 case AF_INET:
1149 skp = &sain;
1150 sasize = sizeof(sain);
1151 break;
1152
1153 case AF_LOCAL:
1154 skp = &saun;
1155 sasize = sizeof(saun);
1156 break;
1157
1158 default:
1159 DPRINTF(("ti_ioctl: Unsupported address family %d\n",
1160 st->s_family));
1161 return ENOSYS;
1162 }
1163
1164 sg = stackgap_init();
1165 sup = stackgap_alloc(&sg, sasize);
1166 lenp = stackgap_alloc(&sg, sizeof(*lenp));
1167
1168 if ((error = copyout(&sasize, lenp, sizeof(*lenp))) != 0) {
1169 DPRINTF(("ti_ioctl: error copying out lenp\n"));
1170 return error;
1171 }
1172
1173 switch (cmd) {
1174 case SVR4_TI_GETMYNAME:
1175 DPRINTF(("TI_GETMYNAME\n"));
1176 {
1177 struct getsockname_args ap;
1178 SCARG(&ap, fdes) = fd;
1179 SCARG(&ap, asa) = sup;
1180 SCARG(&ap, alen) = lenp;
41c20dac 1181 if ((error = getsockname(&ap)) != 0) {
984263bc
MD
1182 DPRINTF(("ti_ioctl: getsockname error\n"));
1183 return error;
1184 }
1185 }
1186 break;
1187
1188 case SVR4_TI_GETPEERNAME:
1189 DPRINTF(("TI_GETPEERNAME\n"));
1190 {
1191 struct getpeername_args ap;
1192 SCARG(&ap, fdes) = fd;
1193 SCARG(&ap, asa) = sup;
1194 SCARG(&ap, alen) = lenp;
41c20dac 1195 if ((error = getpeername(&ap)) != 0) {
984263bc
MD
1196 DPRINTF(("ti_ioctl: getpeername error\n"));
1197 return error;
1198 }
1199 }
1200 break;
1201
1202 case SVR4_TI_SETMYNAME:
1203 DPRINTF(("TI_SETMYNAME\n"));
1204 return 0;
1205
1206 case SVR4_TI_SETPEERNAME:
1207 DPRINTF(("TI_SETPEERNAME\n"));
1208 return 0;
1209 default:
1210 DPRINTF(("ti_ioctl: Unknown ioctl %lx\n", cmd));
1211 return ENOSYS;
1212 }
1213
1214 if ((error = copyin(sup, skp, sasize)) != 0) {
1215 DPRINTF(("ti_ioctl: error copying in socket data\n"));
1216 return error;
1217 }
1218
1219 if ((error = copyin(lenp, &sasize, sizeof(*lenp))) != 0) {
1220 DPRINTF(("ti_ioctl: error copying in socket size\n"));
1221 return error;
1222 }
1223
1224 switch (st->s_family) {
1225 case AF_INET:
1226 sockaddr_to_netaddr_in(&sc, &sain);
1227 skb.len = sasize;
1228 break;
1229
1230 case AF_LOCAL:
1231 sockaddr_to_netaddr_un(&sc, &saun);
1232 skb.len = sasize + 4;
1233 break;
1234
1235 default:
1236 return ENOSYS;
1237 }
1238
1239
1240 if ((error = copyout(SVR4_ADDROF(&sc), skb.buf, sasize)) != 0) {
1241 DPRINTF(("ti_ioctl: error copying out socket data\n"));
1242 return error;
1243 }
1244
1245
1246 if ((error = copyout(&skb, sub, sizeof(skb))) != 0) {
1247 DPRINTF(("ti_ioctl: error copying out strbuf\n"));
1248 return error;
1249 }
1250
1251 return error;
1252}
1253
1254
1255
1256
1257static int
dadab5e9 1258i_nread(fp, td, retval, fd, cmd, dat)
984263bc 1259 struct file *fp;
dadab5e9 1260 struct thread *td;
984263bc
MD
1261 register_t *retval;
1262 int fd;
1263 u_long cmd;
1264 caddr_t dat;
1265{
1266 int error;
1267 int nread = 0;
1268
1269 /*
1270 * We are supposed to return the message length in nread, and the
1271 * number of messages in retval. We don't have the notion of number
1272 * of stream messages, so we just find out if we have any bytes waiting
1273 * for us, and if we do, then we assume that we have at least one
1274 * message waiting for us.
1275 */
dadab5e9 1276 if ((error = fo_ioctl(fp, FIONREAD, (caddr_t) &nread, td)) != 0)
984263bc
MD
1277 return error;
1278
1279 if (nread != 0)
1280 *retval = 1;
1281 else
1282 *retval = 0;
1283
1284 return copyout(&nread, dat, sizeof(nread));
1285}
1286
1287static int
dadab5e9 1288i_fdinsert(fp, td, retval, fd, cmd, dat)
984263bc 1289 struct file *fp;
dadab5e9 1290 struct thread *td;
984263bc
MD
1291 register_t *retval;
1292 int fd;
1293 u_long cmd;
1294 caddr_t dat;
1295{
1296 /*
1297 * Major hack again here. We assume that we are using this to
1298 * implement accept(2). If that is the case, we have already
1299 * called accept, and we have stored the file descriptor in
1300 * afd. We find the file descriptor that the code wants to use
1301 * in fd insert, and then we dup2() our accepted file descriptor
1302 * to it.
1303 */
1304 int error;
1305 struct svr4_strm *st = svr4_stream_get(fp);
1306 struct svr4_strfdinsert fdi;
1307 struct dup2_args d2p;
1308 struct close_args clp;
1309
1310 if (st == NULL) {
1311 DPRINTF(("fdinsert: bad file type\n"));
1312 return EINVAL;
1313 }
1314
1315 if (st->s_afd == -1) {
1316 DPRINTF(("fdinsert: accept fd not found\n"));
1317 return ENOENT;
1318 }
1319
1320 if ((error = copyin(dat, &fdi, sizeof(fdi))) != 0) {
1321 DPRINTF(("fdinsert: copyin failed %d\n", error));
1322 return error;
1323 }
1324
1325 SCARG(&d2p, from) = st->s_afd;
1326 SCARG(&d2p, to) = fdi.fd;
1327
41c20dac 1328 if ((error = dup2(&d2p)) != 0) {
984263bc
MD
1329 DPRINTF(("fdinsert: dup2(%d, %d) failed %d\n",
1330 st->s_afd, fdi.fd, error));
1331 return error;
1332 }
1333
1334 SCARG(&clp, fd) = st->s_afd;
1335
41c20dac 1336 if ((error = close(&clp)) != 0) {
984263bc
MD
1337 DPRINTF(("fdinsert: close(%d) failed %d\n",
1338 st->s_afd, error));
1339 return error;
1340 }
1341
1342 st->s_afd = -1;
1343
1344 *retval = 0;
1345 return 0;
1346}
1347
1348
1349static int
dadab5e9 1350_i_bind_rsvd(fp, td, retval, fd, cmd, dat)
984263bc 1351 struct file *fp;
dadab5e9 1352 struct thread *td;
984263bc
MD
1353 register_t *retval;
1354 int fd;
1355 u_long cmd;
1356 caddr_t dat;
1357{
1358 struct mkfifo_args ap;
1359
1360 /*
1361 * This is a supposed to be a kernel and library only ioctl.
1362 * It gets called before ti_bind, when we have a unix
1363 * socket, to physically create the socket transport and
1364 * ``reserve'' it. I don't know how this get reserved inside
1365 * the kernel, but we are going to create it nevertheless.
1366 */
1367 SCARG(&ap, path) = dat;
1368 SCARG(&ap, mode) = S_IFIFO;
1369
41c20dac 1370 return mkfifo(&ap);
984263bc
MD
1371}
1372
1373static int
dadab5e9 1374_i_rele_rsvd(fp, td, retval, fd, cmd, dat)
984263bc 1375 struct file *fp;
dadab5e9 1376 struct thread *td;
984263bc
MD
1377 register_t *retval;
1378 int fd;
1379 u_long cmd;
1380 caddr_t dat;
1381{
1382 struct unlink_args ap;
1383
1384 /*
1385 * This is a supposed to be a kernel and library only ioctl.
1386 * I guess it is supposed to release the socket.
1387 */
1388 SCARG(&ap, path) = dat;
1389
41c20dac 1390 return unlink(&ap);
984263bc
MD
1391}
1392
1393static int
dadab5e9 1394i_str(fp, td, retval, fd, cmd, dat)
984263bc 1395 struct file *fp;
dadab5e9 1396 struct thread *td;
984263bc
MD
1397 register_t *retval;
1398 int fd;
1399 u_long cmd;
1400 caddr_t dat;
1401{
1402 int error;
1403 struct svr4_strioctl ioc;
1404
1405 if ((error = copyin(dat, &ioc, sizeof(ioc))) != 0)
1406 return error;
1407
1408#ifdef DEBUG_SVR4
1409 if ((error = show_ioc(">", &ioc)) != 0)
1410 return error;
1411#endif /* DEBUG_SVR4 */
1412
1413 switch (ioc.cmd & 0xff00) {
1414 case SVR4_SIMOD:
dadab5e9 1415 if ((error = sockmod(fp, fd, &ioc, td)) != 0)
984263bc
MD
1416 return error;
1417 break;
1418
1419 case SVR4_TIMOD:
dadab5e9 1420 if ((error = timod(fp, fd, &ioc, td)) != 0)
984263bc
MD
1421 return error;
1422 break;
1423
1424 default:
1425 DPRINTF(("Unimplemented module %c %ld\n",
1426 (char) (cmd >> 8), cmd & 0xff));
1427 return 0;
1428 }
1429
1430#ifdef DEBUG_SVR4
1431 if ((error = show_ioc("<", &ioc)) != 0)
1432 return error;
1433#endif /* DEBUG_SVR4 */
1434 return copyout(&ioc, dat, sizeof(ioc));
1435}
1436
1437static int
dadab5e9 1438i_setsig(fp, td, retval, fd, cmd, dat)
984263bc 1439 struct file *fp;
dadab5e9 1440 struct thread *td;
984263bc
MD
1441 register_t *retval;
1442 int fd;
1443 u_long cmd;
1444 caddr_t dat;
1445{
1446 /*
1447 * This is the best we can do for now; we cannot generate
1448 * signals only for specific events so the signal mask gets
1449 * ignored; we save it just to pass it to a possible I_GETSIG...
1450 *
1451 * We alse have to fix the O_ASYNC fcntl bit, so the
1452 * process will get SIGPOLLs.
1453 */
dadab5e9 1454 struct proc *p = td->td_proc;
984263bc
MD
1455 struct fcntl_args fa;
1456 int error;
1457 register_t oflags, flags;
1458 struct svr4_strm *st = svr4_stream_get(fp);
1459
dadab5e9
MD
1460 KKASSERT(p);
1461
984263bc
MD
1462 if (st == NULL) {
1463 DPRINTF(("i_setsig: bad file descriptor\n"));
1464 return EINVAL;
1465 }
1466 /* get old status flags */
1467 SCARG(&fa, fd) = fd;
1468 SCARG(&fa, cmd) = F_GETFL;
41c20dac 1469 if ((error = fcntl(&fa)) != 0)
984263bc
MD
1470 return error;
1471
1472 oflags = p->p_retval[0];
1473
1474 /* update the flags */
1475 if (dat != NULL) {
1476 int mask;
1477
1478 flags = oflags | O_ASYNC;
1479 if ((error = copyin(dat, &mask, sizeof(mask))) != 0) {
1480 DPRINTF(("i_setsig: bad eventmask pointer\n"));
1481 return error;
1482 }
1483 if (mask & SVR4_S_ALLMASK) {
1484 DPRINTF(("i_setsig: bad eventmask data %x\n", mask));
1485 return EINVAL;
1486 }
1487 st->s_eventmask = mask;
1488 }
1489 else {
1490 flags = oflags & ~O_ASYNC;
1491 st->s_eventmask = 0;
1492 }
1493
1494 /* set the new flags, if changed */
1495 if (flags != oflags) {
1496 SCARG(&fa, cmd) = F_SETFL;
1497 SCARG(&fa, arg) = (long) flags;
41c20dac 1498 if ((error = fcntl(&fa)) != 0)
984263bc
MD
1499 return error;
1500 flags = p->p_retval[0];
1501 }
1502
1503 /* set up SIGIO receiver if needed */
1504 if (dat != NULL) {
1505 SCARG(&fa, cmd) = F_SETOWN;
1506 SCARG(&fa, arg) = (long) p->p_pid;
41c20dac 1507 return fcntl(&fa);
984263bc
MD
1508 }
1509 return 0;
1510}
1511
1512static int
dadab5e9 1513i_getsig(fp, td, retval, fd, cmd, dat)
984263bc 1514 struct file *fp;
dadab5e9 1515 struct thread *td;
984263bc
MD
1516 register_t *retval;
1517 int fd;
1518 u_long cmd;
1519 caddr_t dat;
1520{
1521 int error;
1522
1523 if (dat != NULL) {
1524 struct svr4_strm *st = svr4_stream_get(fp);
1525
1526 if (st == NULL) {
1527 DPRINTF(("i_getsig: bad file descriptor\n"));
1528 return EINVAL;
1529 }
1530 if ((error = copyout(&st->s_eventmask, dat,
1531 sizeof(st->s_eventmask))) != 0) {
1532 DPRINTF(("i_getsig: bad eventmask pointer\n"));
1533 return error;
1534 }
1535 }
1536 return 0;
1537}
1538
1539int
dadab5e9 1540svr4_stream_ioctl(fp, td, retval, fd, cmd, dat)
984263bc 1541 struct file *fp;
dadab5e9 1542 struct thread *td;
984263bc
MD
1543 register_t *retval;
1544 int fd;
1545 u_long cmd;
1546 caddr_t dat;
1547{
1548 *retval = 0;
1549
1550 /*
1551 * All the following stuff assumes "sockmod" is pushed...
1552 */
1553 switch (cmd) {
1554 case SVR4_I_NREAD:
1555 DPRINTF(("I_NREAD\n"));
dadab5e9 1556 return i_nread(fp, td, retval, fd, cmd, dat);
984263bc
MD
1557
1558 case SVR4_I_PUSH:
dadab5e9 1559 DPRINTF(("I_PUSH %p\n", dat));
984263bc 1560#if defined(DEBUG_SVR4)
dadab5e9 1561 show_strbuf((void *)dat);
984263bc
MD
1562#endif
1563 return 0;
1564
1565 case SVR4_I_POP:
1566 DPRINTF(("I_POP\n"));
1567 return 0;
1568
1569 case SVR4_I_LOOK:
1570 DPRINTF(("I_LOOK\n"));
1571 return 0;
1572
1573 case SVR4_I_FLUSH:
1574 DPRINTF(("I_FLUSH\n"));
1575 return 0;
1576
1577 case SVR4_I_SRDOPT:
1578 DPRINTF(("I_SRDOPT\n"));
1579 return 0;
1580
1581 case SVR4_I_GRDOPT:
1582 DPRINTF(("I_GRDOPT\n"));
1583 return 0;
1584
1585 case SVR4_I_STR:
1586 DPRINTF(("I_STR\n"));
dadab5e9 1587 return i_str(fp, td, retval, fd, cmd, dat);
984263bc
MD
1588
1589 case SVR4_I_SETSIG:
1590 DPRINTF(("I_SETSIG\n"));
dadab5e9 1591 return i_setsig(fp, td, retval, fd, cmd, dat);
984263bc
MD
1592
1593 case SVR4_I_GETSIG:
1594 DPRINTF(("I_GETSIG\n"));
dadab5e9 1595 return i_getsig(fp, td, retval, fd, cmd, dat);
984263bc
MD
1596
1597 case SVR4_I_FIND:
1598 DPRINTF(("I_FIND\n"));
1599 /*
1600 * Here we are not pushing modules really, we just
1601 * pretend all are present
1602 */
1603 *retval = 0;
1604 return 0;
1605
1606 case SVR4_I_LINK:
1607 DPRINTF(("I_LINK\n"));
1608 return 0;
1609
1610 case SVR4_I_UNLINK:
1611 DPRINTF(("I_UNLINK\n"));
1612 return 0;
1613
1614 case SVR4_I_ERECVFD:
1615 DPRINTF(("I_ERECVFD\n"));
1616 return 0;
1617
1618 case SVR4_I_PEEK:
1619 DPRINTF(("I_PEEK\n"));
1620 return 0;
1621
1622 case SVR4_I_FDINSERT:
1623 DPRINTF(("I_FDINSERT\n"));
dadab5e9 1624 return i_fdinsert(fp, td, retval, fd, cmd, dat);
984263bc
MD
1625
1626 case SVR4_I_SENDFD:
1627 DPRINTF(("I_SENDFD\n"));
1628 return 0;
1629
1630 case SVR4_I_RECVFD:
1631 DPRINTF(("I_RECVFD\n"));
1632 return 0;
1633
1634 case SVR4_I_SWROPT:
1635 DPRINTF(("I_SWROPT\n"));
1636 return 0;
1637
1638 case SVR4_I_GWROPT:
1639 DPRINTF(("I_GWROPT\n"));
1640 return 0;
1641
1642 case SVR4_I_LIST:
1643 DPRINTF(("I_LIST\n"));
1644 return 0;
1645
1646 case SVR4_I_PLINK:
1647 DPRINTF(("I_PLINK\n"));
1648 return 0;
1649
1650 case SVR4_I_PUNLINK:
1651 DPRINTF(("I_PUNLINK\n"));
1652 return 0;
1653
1654 case SVR4_I_SETEV:
1655 DPRINTF(("I_SETEV\n"));
1656 return 0;
1657
1658 case SVR4_I_GETEV:
1659 DPRINTF(("I_GETEV\n"));
1660 return 0;
1661
1662 case SVR4_I_STREV:
1663 DPRINTF(("I_STREV\n"));
1664 return 0;
1665
1666 case SVR4_I_UNSTREV:
1667 DPRINTF(("I_UNSTREV\n"));
1668 return 0;
1669
1670 case SVR4_I_FLUSHBAND:
1671 DPRINTF(("I_FLUSHBAND\n"));
1672 return 0;
1673
1674 case SVR4_I_CKBAND:
1675 DPRINTF(("I_CKBAND\n"));
1676 return 0;
1677
1678 case SVR4_I_GETBAND:
1679 DPRINTF(("I_GETBANK\n"));
1680 return 0;
1681
1682 case SVR4_I_ATMARK:
1683 DPRINTF(("I_ATMARK\n"));
1684 return 0;
1685
1686 case SVR4_I_SETCLTIME:
1687 DPRINTF(("I_SETCLTIME\n"));
1688 return 0;
1689
1690 case SVR4_I_GETCLTIME:
1691 DPRINTF(("I_GETCLTIME\n"));
1692 return 0;
1693
1694 case SVR4_I_CANPUT:
1695 DPRINTF(("I_CANPUT\n"));
1696 return 0;
1697
1698 case SVR4__I_BIND_RSVD:
1699 DPRINTF(("_I_BIND_RSVD\n"));
dadab5e9 1700 return _i_bind_rsvd(fp, td, retval, fd, cmd, dat);
984263bc
MD
1701
1702 case SVR4__I_RELE_RSVD:
1703 DPRINTF(("_I_RELE_RSVD\n"));
dadab5e9 1704 return _i_rele_rsvd(fp, td, retval, fd, cmd, dat);
984263bc
MD
1705
1706 default:
1707 DPRINTF(("unimpl cmd = %lx\n", cmd));
1708 break;
1709 }
1710
1711 return 0;
1712}
1713
1714
1715
1716int
41c20dac 1717svr4_sys_putmsg(struct svr4_sys_putmsg_args *uap)
984263bc 1718{
dadab5e9
MD
1719 struct thread *td = curthread;
1720 struct proc *p = td->td_proc;
1721 struct filedesc *fdp;
984263bc
MD
1722 struct file *fp;
1723 struct svr4_strbuf dat, ctl;
1724 struct svr4_strmcmd sc;
1725 struct sockaddr_in sain;
1726 struct sockaddr_un saun;
1727 void *skp, *sup;
1728 int sasize, *retval;
1729 struct svr4_strm *st;
1730 int error;
1731 caddr_t sg;
1732
dadab5e9
MD
1733 KKASSERT(p);
1734 fdp = p->p_fd;
984263bc
MD
1735 retval = p->p_retval;
1736 fp = fdp->fd_ofiles[SCARG(uap, fd)];
1737
1738 if (((u_int)SCARG(uap, fd) >= fdp->fd_nfiles) || (fp == NULL)) {
1739#ifdef DEBUG_SVR4
1740 uprintf("putmsg: bad fp\n");
1741#endif
1742 return EBADF;
1743 }
1744
1745#ifdef DEBUG_SVR4
1746 show_msg(">putmsg", SCARG(uap, fd), SCARG(uap, ctl),
1747 SCARG(uap, dat), SCARG(uap, flags));
1748#endif /* DEBUG_SVR4 */
1749
1750 if (((u_int)SCARG(uap, fd) >= fdp->fd_nfiles) || (fp == NULL)) {
1751#ifdef DEBUG_SVR4
1752 uprintf("putmsg: bad fp(2)\n");
1753#endif
1754 return EBADF;
1755 }
1756
1757 if (SCARG(uap, ctl) != NULL) {
1758 if ((error = copyin(SCARG(uap, ctl), &ctl, sizeof(ctl))) != 0) {
1759#ifdef DEBUG_SVR4
1760 uprintf("putmsg: copyin(): %d\n", error);
1761#endif
1762 return error;
1763 }
1764 }
1765 else
1766 ctl.len = -1;
1767
1768 if (SCARG(uap, dat) != NULL) {
1769 if ((error = copyin(SCARG(uap, dat), &dat, sizeof(dat))) != 0) {
1770#ifdef DEBUG_SVR4
1771 uprintf("putmsg: copyin(): %d (2)\n", error);
1772#endif
1773 return error;
1774 }
1775 }
1776 else
1777 dat.len = -1;
1778
1779 /*
1780 * Only for sockets for now.
1781 */
1782 if ((st = svr4_stream_get(fp)) == NULL) {
1783 DPRINTF(("putmsg: bad file type\n"));
1784 return EINVAL;
1785 }
1786
1787 if (ctl.len > sizeof(sc)) {
1788 DPRINTF(("putmsg: Bad control size %d != %d\n", ctl.len,
1789 sizeof(struct svr4_strmcmd)));
1790 return EINVAL;
1791 }
1792
1793 if ((error = copyin(ctl.buf, &sc, ctl.len)) != 0)
1794 return error;
1795
1796 switch (st->s_family) {
1797 case AF_INET:
1798 if (sc.len != sizeof(sain)) {
1799 if (sc.cmd == SVR4_TI_DATA_REQUEST) {
1800 struct write_args wa;
1801
1802 /* Solaris seems to use sc.cmd = 3 to
1803 * send "expedited" data. telnet uses
1804 * this for options processing, sending EOF,
1805 * etc. I'm sure other things use it too.
1806 * I don't have any documentation
1807 * on it, so I'm making a guess that this
1808 * is how it works. newton@atdot.dotat.org XXX
1809 */
1810 DPRINTF(("sending expedited data ??\n"));
1811 SCARG(&wa, fd) = SCARG(uap, fd);
1812 SCARG(&wa, buf) = dat.buf;
1813 SCARG(&wa, nbyte) = dat.len;
41c20dac 1814 return write(&wa);
984263bc
MD
1815 }
1816 DPRINTF(("putmsg: Invalid inet length %ld\n", sc.len));
1817 return EINVAL;
1818 }
1819 netaddr_to_sockaddr_in(&sain, &sc);
1820 skp = &sain;
1821 sasize = sizeof(sain);
1822 error = sain.sin_family != st->s_family;
1823 break;
1824
1825 case AF_LOCAL:
1826 if (ctl.len == 8) {
1827 /* We are doing an accept; succeed */
1828 DPRINTF(("putmsg: Do nothing\n"));
1829 *retval = 0;
1830 return 0;
1831 }
1832 else {
1833 /* Maybe we've been given a device/inode pair */
1834 udev_t *dev = SVR4_ADDROF(&sc);
1835 ino_t *ino = (ino_t *) &dev[1];
dadab5e9 1836 skp = svr4_find_socket(td, fp, *dev, *ino);
984263bc
MD
1837 if (skp == NULL) {
1838 skp = &saun;
1839 /* I guess we have it by name */
1840 netaddr_to_sockaddr_un(skp, &sc);
1841 }
1842 sasize = sizeof(saun);
1843 }
1844 break;
1845
1846 default:
1847 DPRINTF(("putmsg: Unsupported address family %d\n",
1848 st->s_family));
1849 return ENOSYS;
1850 }
1851
1852 sg = stackgap_init();
1853 sup = stackgap_alloc(&sg, sasize);
1854
1855 if ((error = copyout(skp, sup, sasize)) != 0)
1856 return error;
1857
1858 switch (st->s_cmd = sc.cmd) {
1859 case SVR4_TI_CONNECT_REQUEST: /* connect */
1860 {
1861 struct connect_args co;
1862
1863 SCARG(&co, s) = SCARG(uap, fd);
1864 SCARG(&co, name) = (void *) sup;
1865 SCARG(&co, namelen) = (int) sasize;
1866
41c20dac 1867 return connect(&co);
984263bc
MD
1868 }
1869
1870 case SVR4_TI_SENDTO_REQUEST: /* sendto */
1871 {
1872 struct msghdr msg;
1873 struct iovec aiov;
1874
1875 msg.msg_name = (caddr_t) sup;
1876 msg.msg_namelen = sasize;
1877 msg.msg_iov = &aiov;
1878 msg.msg_iovlen = 1;
1879 msg.msg_control = 0;
1880 msg.msg_flags = 0;
1881 aiov.iov_base = dat.buf;
1882 aiov.iov_len = dat.len;
1883#if 0
dadab5e9
MD
1884 error = so->so_proto->pr_usrreqs->pru_sosend
1885 (so, 0, uio, 0, 0, 0, uio->uio_td);
984263bc 1886#endif
dadab5e9 1887 error = svr4_sendit(td, SCARG(uap, fd), &msg,
984263bc
MD
1888 SCARG(uap, flags));
1889 DPRINTF(("sendto_request error: %d\n", error));
1890 *retval = 0;
1891 return error;
1892 }
1893
1894 default:
1895 DPRINTF(("putmsg: Unimplemented command %lx\n", sc.cmd));
1896 return ENOSYS;
1897 }
1898}
1899
1900int
41c20dac 1901svr4_sys_getmsg(struct svr4_sys_getmsg_args *uap)
984263bc 1902{
dadab5e9
MD
1903 struct thread *td = curthread;
1904 struct proc *p = td->td_proc;
1905 struct filedesc *fdp;
984263bc
MD
1906 struct file *fp;
1907 struct getpeername_args ga;
1908 struct accept_args aa;
1909 struct svr4_strbuf dat, ctl;
1910 struct svr4_strmcmd sc;
1911 int error, *retval;
1912 struct msghdr msg;
1913 struct iovec aiov;
1914 struct sockaddr_in sain;
1915 struct sockaddr_un saun;
1916 void *skp, *sup;
1917 int sasize;
1918 struct svr4_strm *st;
1919 int *flen;
1920 int fl;
1921 caddr_t sg;
1922
dadab5e9
MD
1923 KKASSERT(p);
1924 fdp = p->p_fd;
984263bc
MD
1925 retval = p->p_retval;
1926 fp = fdp->fd_ofiles[SCARG(uap, fd)];
1927
1928 if (((u_int)SCARG(uap, fd) >= fdp->fd_nfiles) || (fp == NULL))
1929 return EBADF;
1930
1931 memset(&sc, 0, sizeof(sc));
1932
1933#ifdef DEBUG_SVR4
1934 show_msg(">getmsg", SCARG(uap, fd), SCARG(uap, ctl),
1935 SCARG(uap, dat), 0);
1936#endif /* DEBUG_SVR4 */
1937
1938 if (((u_int)SCARG(uap, fd) >= fdp->fd_nfiles) || (fp == NULL))
1939 return EBADF;
1940
1941 if (SCARG(uap, ctl) != NULL) {
1942 if ((error = copyin(SCARG(uap, ctl), &ctl, sizeof(ctl))) != 0)
1943 return error;
1944 }
1945 else {
1946 ctl.len = -1;
1947 ctl.maxlen = 0;
1948 }
1949
1950 if (SCARG(uap, dat) != NULL) {
1951 if ((error = copyin(SCARG(uap, dat), &dat, sizeof(dat))) != 0)
1952 return error;
1953 }
1954 else {
1955 dat.len = -1;
1956 dat.maxlen = 0;
1957 }
1958
1959 /*
1960 * Only for sockets for now.
1961 */
1962 if ((st = svr4_stream_get(fp)) == NULL) {
1963 DPRINTF(("getmsg: bad file type\n"));
1964 return EINVAL;
1965 }
1966
1967 if (ctl.maxlen == -1 || dat.maxlen == -1) {
1968 DPRINTF(("getmsg: Cannot handle -1 maxlen (yet)\n"));
1969 return ENOSYS;
1970 }
1971
1972 switch (st->s_family) {
1973 case AF_INET:
1974 skp = &sain;
1975 sasize = sizeof(sain);
1976 break;
1977
1978 case AF_LOCAL:
1979 skp = &saun;
1980 sasize = sizeof(saun);
1981 break;
1982
1983 default:
1984 DPRINTF(("getmsg: Unsupported address family %d\n",
1985 st->s_family));
1986 return ENOSYS;
1987 }
1988
1989 sg = stackgap_init();
1990 sup = stackgap_alloc(&sg, sasize);
1991 flen = (int *) stackgap_alloc(&sg, sizeof(*flen));
1992
1993 fl = sasize;
1994 if ((error = copyout(&fl, flen, sizeof(fl))) != 0)
1995 return error;
1996
1997 switch (st->s_cmd) {
1998 case SVR4_TI_CONNECT_REQUEST:
1999 DPRINTF(("getmsg: TI_CONNECT_REQUEST\n"));
2000 /*
2001 * We do the connect in one step, so the putmsg should
2002 * have gotten the error.
2003 */
2004 sc.cmd = SVR4_TI_OK_REPLY;
2005 sc.len = 0;
2006
2007 ctl.len = 8;
2008 dat.len = -1;
2009 fl = 1;
2010 st->s_cmd = sc.cmd;
2011 break;
2012
2013 case SVR4_TI_OK_REPLY:
2014 DPRINTF(("getmsg: TI_OK_REPLY\n"));
2015 /*
2016 * We are immediately after a connect reply, so we send
2017 * a connect verification.
2018 */
2019
2020 SCARG(&ga, fdes) = SCARG(uap, fd);
2021 SCARG(&ga, asa) = (void *) sup;
2022 SCARG(&ga, alen) = flen;
2023
41c20dac 2024 if ((error = getpeername(&ga)) != 0) {
984263bc
MD
2025 DPRINTF(("getmsg: getpeername failed %d\n", error));
2026 return error;
2027 }
2028
2029 if ((error = copyin(sup, skp, sasize)) != 0)
2030 return error;
2031
2032 sc.cmd = SVR4_TI_CONNECT_REPLY;
2033 sc.pad[0] = 0x4;
2034 sc.offs = 0x18;
2035 sc.pad[1] = 0x14;
2036 sc.pad[2] = 0x04000402;
2037
2038 switch (st->s_family) {
2039 case AF_INET:
2040 sc.len = sasize;
2041 sockaddr_to_netaddr_in(&sc, &sain);
2042 break;
2043
2044 case AF_LOCAL:
2045 sc.len = sasize + 4;
2046 sockaddr_to_netaddr_un(&sc, &saun);
2047 break;
2048
2049 default:
2050 return ENOSYS;
2051 }
2052
2053 ctl.len = 40;
2054 dat.len = -1;
2055 fl = 0;
2056 st->s_cmd = sc.cmd;
2057 break;
2058
2059 case SVR4_TI__ACCEPT_OK:
2060 DPRINTF(("getmsg: TI__ACCEPT_OK\n"));
2061 /*
2062 * We do the connect in one step, so the putmsg should
2063 * have gotten the error.
2064 */
2065 sc.cmd = SVR4_TI_OK_REPLY;
2066 sc.len = 1;
2067
2068 ctl.len = 8;
2069 dat.len = -1;
2070 fl = 1;
2071 st->s_cmd = SVR4_TI__ACCEPT_WAIT;
2072 break;
2073
2074 case SVR4_TI__ACCEPT_WAIT:
2075 DPRINTF(("getmsg: TI__ACCEPT_WAIT\n"));
2076 /*
2077 * We are after a listen, so we try to accept...
2078 */
2079 SCARG(&aa, s) = SCARG(uap, fd);
2080 SCARG(&aa, name) = (void *) sup;
2081 SCARG(&aa, anamelen) = flen;
2082
41c20dac 2083 if ((error = accept(&aa)) != 0) {
984263bc
MD
2084 DPRINTF(("getmsg: accept failed %d\n", error));
2085 return error;
2086 }
2087
2088 st->s_afd = *retval;
2089
2090 DPRINTF(("getmsg: Accept fd = %d\n", st->s_afd));
2091
2092 if ((error = copyin(sup, skp, sasize)) != 0)
2093 return error;
2094
2095 sc.cmd = SVR4_TI_ACCEPT_REPLY;
2096 sc.offs = 0x18;
2097 sc.pad[0] = 0x0;
2098
2099 switch (st->s_family) {
2100 case AF_INET:
2101 sc.pad[1] = 0x28;
2102 sockaddr_to_netaddr_in(&sc, &sain);
2103 ctl.len = 40;
2104 sc.len = sasize;
2105 break;
2106
2107 case AF_LOCAL:
2108 sc.pad[1] = 0x00010000;
2109 sc.pad[2] = 0xf6bcdaa0; /* I don't know what that is */
2110 sc.pad[3] = 0x00010000;
2111 ctl.len = 134;
2112 sc.len = sasize + 4;
2113 break;
2114
2115 default:
2116 return ENOSYS;
2117 }
2118
2119 dat.len = -1;
2120 fl = 0;
2121 st->s_cmd = SVR4_TI__ACCEPT_OK;
2122 break;
2123
2124 case SVR4_TI_SENDTO_REQUEST:
2125 DPRINTF(("getmsg: TI_SENDTO_REQUEST\n"));
2126 if (ctl.maxlen > 36 && ctl.len < 36)
2127 ctl.len = 36;
2128
2129 if ((error = copyin(ctl.buf, &sc, ctl.len)) != 0)
2130 return error;
2131
2132 switch (st->s_family) {
2133 case AF_INET:
2134 sockaddr_to_netaddr_in(&sc, &sain);
2135 break;
2136
2137 case AF_LOCAL:
2138 sockaddr_to_netaddr_un(&sc, &saun);
2139 break;
2140
2141 default:
2142 return ENOSYS;
2143 }
2144
2145 msg.msg_name = (caddr_t) sup;
2146 msg.msg_namelen = sasize;
2147 msg.msg_iov = &aiov;
2148 msg.msg_iovlen = 1;
2149 msg.msg_control = 0;
2150 aiov.iov_base = dat.buf;
2151 aiov.iov_len = dat.maxlen;
2152 msg.msg_flags = 0;
2153
dadab5e9 2154 error = svr4_recvit(td, SCARG(uap, fd), &msg, (caddr_t) flen);
984263bc
MD
2155
2156 if (error) {
2157 DPRINTF(("getmsg: recvit failed %d\n", error));
2158 return error;
2159 }
2160
2161 if ((error = copyin(msg.msg_name, skp, sasize)) != 0)
2162 return error;
2163
2164 sc.cmd = SVR4_TI_RECVFROM_IND;
2165
2166 switch (st->s_family) {
2167 case AF_INET:
2168 sc.len = sasize;
2169 sockaddr_to_netaddr_in(&sc, &sain);
2170 break;
2171
2172 case AF_LOCAL:
2173 sc.len = sasize + 4;
2174 sockaddr_to_netaddr_un(&sc, &saun);
2175 break;
2176
2177 default:
2178 return ENOSYS;
2179 }
2180
2181 dat.len = *retval;
2182 fl = 0;
2183 st->s_cmd = sc.cmd;
2184 break;
2185
2186 default:
2187 st->s_cmd = sc.cmd;
2188 if (st->s_cmd == SVR4_TI_CONNECT_REQUEST) {
2189 struct read_args ra;
2190
2191 /* More wierdness: Again, I can't find documentation
2192 * to back this up, but when a process does a generic
2193 * "getmsg()" call it seems that the command field is
2194 * zero and the length of the data area is zero. I
2195 * think processes expect getmsg() to fill in dat.len
2196 * after reading at most dat.maxlen octets from the
2197 * stream. Since we're using sockets I can let
2198 * read() look after it and frob return values
2199 * appropriately (or inappropriately :-)
2200 * -- newton@atdot.dotat.org XXX
2201 */
2202 SCARG(&ra, fd) = SCARG(uap, fd);
2203 SCARG(&ra, buf) = dat.buf;
2204 SCARG(&ra, nbyte) = dat.maxlen;
41c20dac 2205 if ((error = read(&ra)) != 0) {
984263bc
MD
2206 return error;
2207 }
2208 dat.len = *retval;
2209 *retval = 0;
2210 st->s_cmd = SVR4_TI_SENDTO_REQUEST;
2211 break;
2212 }
2213 DPRINTF(("getmsg: Unknown state %x\n", st->s_cmd));
2214 return EINVAL;
2215 }
2216
2217 if (SCARG(uap, ctl)) {
2218 if (ctl.len != -1)
2219 if ((error = copyout(&sc, ctl.buf, ctl.len)) != 0)
2220 return error;
2221
2222 if ((error = copyout(&ctl, SCARG(uap, ctl), sizeof(ctl))) != 0)
2223 return error;
2224 }
2225
2226 if (SCARG(uap, dat)) {
2227 if ((error = copyout(&dat, SCARG(uap, dat), sizeof(dat))) != 0)
2228 return error;
2229 }
2230
2231 if (SCARG(uap, flags)) { /* XXX: Need translation */
2232 if ((error = copyout(&fl, SCARG(uap, flags), sizeof(fl))) != 0)
2233 return error;
2234 }
2235
2236 *retval = 0;
2237
2238#ifdef DEBUG_SVR4
2239 show_msg("<getmsg", SCARG(uap, fd), SCARG(uap, ctl),
2240 SCARG(uap, dat), fl);
2241#endif /* DEBUG_SVR4 */
2242 return error;
2243}
2244
41c20dac 2245int svr4_sys_send(struct svr4_sys_send_args *uap)
984263bc
MD
2246{
2247 struct osend_args osa;
2248 SCARG(&osa, s) = SCARG(uap, s);
2249 SCARG(&osa, buf) = SCARG(uap, buf);
2250 SCARG(&osa, len) = SCARG(uap, len);
2251 SCARG(&osa, flags) = SCARG(uap, flags);
41c20dac 2252 return osend(&osa);
984263bc
MD
2253}
2254
41c20dac 2255int svr4_sys_recv(struct svr4_sys_recv_args *uap)
984263bc
MD
2256{
2257 struct orecv_args ora;
2258 SCARG(&ora, s) = SCARG(uap, s);
2259 SCARG(&ora, buf) = SCARG(uap, buf);
2260 SCARG(&ora, len) = SCARG(uap, len);
2261 SCARG(&ora, flags) = SCARG(uap, flags);
41c20dac 2262 return orecv(&ora);
984263bc
MD
2263}
2264
2265/*
2266 * XXX This isn't necessary, but it's handy for inserting debug code into
2267 * sendto(). Let's leave it here for now...
2268 */
2269int
41c20dac 2270svr4_sys_sendto(struct svr4_sys_sendto_args *uap)
984263bc
MD
2271{
2272 struct sendto_args sa;
2273
2274 SCARG(&sa, s) = SCARG(uap, s);
2275 SCARG(&sa, buf) = SCARG(uap, buf);
2276 SCARG(&sa, len) = SCARG(uap, len);
2277 SCARG(&sa, flags) = SCARG(uap, flags);
2278 SCARG(&sa, to) = SCARG(uap, to);
2279 SCARG(&sa, tolen) = SCARG(uap, tolen);
2280
2281 DPRINTF(("calling sendto()\n"));
41c20dac 2282 return sendto(&sa);
984263bc
MD
2283}
2284