1*8bc54e5bSmsaitoh /* $NetBSD: accf_data.c,v 1.8 2016/07/07 06:55:43 msaitoh Exp $ */
2978e1181Sad
3717f903aStls /*-
4717f903aStls * Copyright (c) 2000 Alfred Perlstein <alfred@FreeBSD.org>
5717f903aStls * All rights reserved.
6717f903aStls *
7717f903aStls * Redistribution and use in source and binary forms, with or without
8717f903aStls * modification, are permitted provided that the following conditions
9717f903aStls * are met:
10717f903aStls * 1. Redistributions of source code must retain the above copyright
11717f903aStls * notice, this list of conditions and the following disclaimer.
12717f903aStls * 2. Redistributions in binary form must reproduce the above copyright
13717f903aStls * notice, this list of conditions and the following disclaimer in the
14717f903aStls * documentation and/or other materials provided with the distribution.
15717f903aStls *
16717f903aStls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17717f903aStls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18717f903aStls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19717f903aStls * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20717f903aStls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21717f903aStls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22717f903aStls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23717f903aStls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24717f903aStls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25717f903aStls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26717f903aStls * SUCH DAMAGE.
27717f903aStls */
28717f903aStls
29717f903aStls #include <sys/cdefs.h>
30*8bc54e5bSmsaitoh __KERNEL_RCSID(0, "$NetBSD: accf_data.c,v 1.8 2016/07/07 06:55:43 msaitoh Exp $");
31717f903aStls
32717f903aStls #define ACCEPT_FILTER_MOD
33717f903aStls
34717f903aStls #include <sys/param.h>
35717f903aStls #include <sys/kernel.h>
360efea177Sad #include <sys/module.h>
37717f903aStls #include <sys/sysctl.h>
38717f903aStls #include <sys/signalvar.h>
39717f903aStls #include <sys/socketvar.h>
40978e1181Sad
41717f903aStls #include <netinet/accept_filter.h>
42717f903aStls
43e7ae23fdSchristos #include "ioconf.h"
44e7ae23fdSchristos
45226bc85bSad MODULE(MODULE_CLASS_MISC, accf_dataready, NULL);
460efea177Sad
47717f903aStls /* accept filter that holds a socket until data arrives */
48717f903aStls
49fd671f64Stls static void sohasdata(struct socket *so, void *arg, int events, int waitflag);
50717f903aStls
51717f903aStls static struct accept_filter accf_data_filter = {
52978e1181Sad .accf_name = "dataready",
53978e1181Sad .accf_callback = sohasdata,
54717f903aStls };
55717f903aStls
560efea177Sad void
accf_dataattach(int junk)570efea177Sad accf_dataattach(int junk)
58717f903aStls {
590efea177Sad
60717f903aStls }
61717f903aStls
620efea177Sad static int
accf_dataready_modcmd(modcmd_t cmd,void * arg)6356e065b9Sad accf_dataready_modcmd(modcmd_t cmd, void *arg)
64717f903aStls {
65717f903aStls
660efea177Sad switch (cmd) {
670efea177Sad case MODULE_CMD_INIT:
680efea177Sad return accept_filt_add(&accf_data_filter);
690efea177Sad
700efea177Sad case MODULE_CMD_FINI:
710efea177Sad return accept_filt_del(&accf_data_filter);
720efea177Sad
730efea177Sad default:
740efea177Sad return ENOTTY;
75717f903aStls }
760efea177Sad }
77717f903aStls
78717f903aStls static void
sohasdata(struct socket * so,void * arg,int events,int waitflag)79fd671f64Stls sohasdata(struct socket *so, void *arg, int events, int waitflag)
80717f903aStls {
81717f903aStls
82717f903aStls if (!soreadable(so))
83717f903aStls return;
84717f903aStls
85717f903aStls so->so_upcall = NULL;
86717f903aStls so->so_rcv.sb_flags &= ~SB_UPCALL;
87717f903aStls soisconnected(so);
88717f903aStls return;
89717f903aStls }
90