Path: blob/main/tools/regression/sockets/accf_data_attach/accf_data_attach.c
48254 views
/*-1* Copyright (c) 2004 Robert N. M. Watson2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/types.h>27#include <sys/module.h>28#include <sys/socket.h>2930#include <netinet/in.h>3132#include <err.h>33#include <errno.h>34#include <fcntl.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>3940#define ACCF_NAME "dataready"4142/*43* A number of small tests to confirm that attaching ACCF_DATA accept filters44* to inet4 ports works as expected. We test:45*46* - That no accept filter is attached on a newly created socket.47* - That bind() has no affect on the accept filter state.48* - That we can't attach an accept filter to a socket that isn't in the49* listen state.50* - That after we fail to attach the filter, querying the kernel shows no51* filter attached.52* - That we can attach an accept filter to a socket that is in the listen53* state.54* - That once an accept filter is attached, we can query to make sure it is55* attached.56* - That once an accept filter is attached, we can remove it and query to57* make sure it is removed.58*/59int60main(void)61{62struct accept_filter_arg afa;63struct sockaddr_in sin;64struct linger linger;65socklen_t len;66int lso, so, i, ret;6768/* XXX: PLAIN_TEST_REQUIRE_MODULE "backport" for stable/9 */69const char *_mod_name = "accf_data";7071if (modfind(_mod_name) == -1) {72printf("1..0 # SKIP - module %s could not be resolved: %s\n",73_mod_name, strerror(errno));74_exit(0);75}76/* XXX: PLAIN_TEST_REQUIRE_MODULE for stable/9 */7778printf("1..12\n");7980/*81* Step 0. Open socket().82*/83lso = socket(PF_INET, SOCK_STREAM, 0);84if (lso == -1)85errx(-1, "not ok 1 - socket: %s", strerror(errno));86printf("ok 1 - socket\n");8788/*89* Step 1. After socket(). Should return EINVAL, since no accept90* filter should be attached.91*/92bzero(&afa, sizeof(afa));93len = sizeof(afa);94ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);95if (ret != -1)96errx(-1, "not ok 2 - getsockopt() after socket() succeeded");97if (errno != EINVAL)98errx(-1, "not ok 2 - getsockopt() after socket() failed with "99"%d (%s)", errno, strerror(errno));100printf("ok 2 - getsockopt\n");101102/*103* Step 2. Bind(). Ideally this will succeed.104*/105setsockopt(lso, SOL_SOCKET, SO_REUSEADDR, &lso, sizeof(lso));106bzero(&sin, sizeof(sin));107sin.sin_len = sizeof(sin);108sin.sin_family = AF_INET;109sin.sin_port = htons(8080);110sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);111if (bind(lso, (struct sockaddr *)&sin, sizeof(sin)) < 0)112errx(-1, "not ok 3 - bind %s", strerror(errno));113printf("ok 3 - bind\n");114115/*116* Step 3: After bind(). getsockopt() should return EINVAL, since no117* accept filter should be attached.118*/119len = sizeof(afa);120ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);121if (ret != -1)122errx(-1, "not ok 4 - getsockopt() after bind() succeeded");123if (errno != EINVAL)124errx(-1, "not ok 4 - getsockopt() after bind() failed with %d (%s)",125errno, strerror(errno));126printf("ok 4 - getsockopt\n");127128/*129* Step 4: Setsockopt() before listen(). Should fail, since it's not130* yet a listen() socket.131*/132bzero(&afa, sizeof(afa));133strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));134ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));135if (ret == 0)136errx(-1, "not ok 5 - setsockopt() before listen() succeeded");137printf("ok 5 - setsockopt\n");138139/*140* Step 5: Getsockopt() after pre-listen() setsockopt(). Should141* fail with EINVAL, since setsockopt() should have failed.142*/143len = sizeof(afa);144ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);145if (ret == 0)146errx(-1, "not ok 6 - getsockopt() after pre-listen() setsockopt() "147"succeeded");148if (errno != EINVAL)149errx(-1, "not ok 6 - pre-listen() getsockopt() failed with %d (%s)",150errno, strerror(errno));151printf("ok 6 - getsockopt\n");152153/*154* Step 6: listen().155*/156if (listen(lso, -1) < 0)157errx(-1, "not ok 7 - listen: %s", strerror(errno));158printf("ok 7 - listen\n");159160/*161* Step 7: Getsockopt() after listen(). Should fail with EINVAL,162* since we have not installed accept filter yet.163*/164len = sizeof(afa);165ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);166if (ret == 0)167errx(-1, "not ok 8 - getsockopt() after listen() but before "168"setsockopt() succeeded");169if (errno != EINVAL)170errx(-1, "not ok 8 - getsockopt() after listen() but before "171"setsockopt() failed with %d (%s)", errno, strerror(errno));172printf("ok 8 - getsockopt\n");173174/*175* Step 8: After listen(). This call to setsockopt() should succeed.176*/177bzero(&afa, sizeof(afa));178strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));179ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));180if (ret != 0)181errx(-1, "not ok 9 - setsockopt() after listen() failed with %d "182"(%s)", errno, strerror(errno));183printf("ok 9 - setsockopt\n");184185/*186* Step 9: After setsockopt(). Should succeed and identify187* ACCF_NAME.188*/189bzero(&afa, sizeof(afa));190len = sizeof(afa);191ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);192if (ret != 0)193errx(-1, "not ok 10 - getsockopt() after listen() setsockopt() "194"failed with %d (%s)", errno, strerror(errno));195if (len != sizeof(afa))196errx(-1, "not ok 10 - getsockopt() after setsockopet() after "197"listen() returned wrong size (got %d expected %zd)", len,198sizeof(afa));199if (strcmp(afa.af_name, ACCF_NAME) != 0)200errx(-1, "not ok 10 - getsockopt() after setsockopt() after "201"listen() mismatch (got %s expected %s)", afa.af_name,202ACCF_NAME);203printf("ok 10 - getsockopt\n");204205/*206* Step 10: Set listening socket to non blocking mode. Open207* connection to our listening socket and try to accept. Should208* no succeed. Write a byte of data and try again. Should accept.209*/210i = fcntl(lso, F_GETFL);211if (i < 0)212errx(-1, "not ok 11 - ioctl(F_GETFL): %s", strerror(errno));213i |= O_NONBLOCK;214if (fcntl(lso, F_SETFL, i) != 0)215errx(-1, "not ok 11 - ioctl(F_SETFL): %s", strerror(errno));216so = socket(PF_INET, SOCK_STREAM, 0);217if (so == -1)218errx(-1, "not ok 11 - socket: %s", strerror(errno));219if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)220errx(-1, "not ok 11 - connect %s", strerror(errno));221if (accept(lso, NULL, 0) != -1 && errno != EWOULDBLOCK)222errx(-1, "not ok 11 - accept #1 %s", strerror(errno));223if (write(so, "0", 1) != 1)224errx(-1, "not ok 11 - write %s", strerror(errno));225/*226* XXXGL: ugly, but we need to make sure that our write reaches227* remote side of the socket.228*/229usleep(10000);230if (accept(lso, NULL, 0) < 1)231errx(-1, "not ok 11 - accept #2 %s", strerror(errno));232if (close(so) != 0)233errx(-1, "not ok 11 - close(): %s", strerror(errno));234printf("ok 11 - accept\n");235236/*237* Step 12: reset connection before accept filter allows it.238* In this case the connection must make it to the listen239* queue, but with ECONNABORTED code.240*/241so = socket(PF_INET, SOCK_STREAM, 0);242if (so == -1)243errx(-1, "not ok 12 - socket: %s", strerror(errno));244if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)245errx(-1, "not ok 12 - connect %s", strerror(errno));246linger.l_onoff = 1;247linger.l_linger = 0;248ret = setsockopt(so, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));249if (ret != 0)250errx(-1, "not ok 12 - setsockopt(SO_LINGER) failed with %d "251"(%s)", errno, strerror(errno));252if (close(so) != 0)253errx(-1, "not ok 12 - close(): %s", strerror(errno));254if (accept(lso, NULL, 0) != -1 && errno != ECONNABORTED)255errx(-1, "not ok 12 - accept #3 %s", strerror(errno));256printf("ok 12 - accept\n");257258#if 1259/*260* XXXGL: this doesn't belong to the test itself, but is known261* to examine rarely examined paths in the kernel. Intentionally262* leave a socket on the incomplete queue, before the program263* exits.264*/265so = socket(PF_INET, SOCK_STREAM, 0);266if (so == -1)267errx(-1, "not ok 13 - socket: %s", strerror(errno));268if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)269errx(-1, "not ok 13 - connect %s", strerror(errno));270#endif271272/*273* Step 12: Remove accept filter. After removing the accept filter274* getsockopt() should fail with EINVAL.275*/276ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0);277if (ret != 0)278errx(-1, "not ok 13 - setsockopt() after listen() "279"failed with %d (%s)", errno, strerror(errno));280bzero(&afa, sizeof(afa));281len = sizeof(afa);282ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);283if (ret == 0)284errx(-1, "not ok 13 - getsockopt() after removing "285"the accept filter returns valid accept filter %s",286afa.af_name);287if (errno != EINVAL)288errx(-1, "not ok 13 - getsockopt() after removing the accept"289"filter failed with %d (%s)", errno, strerror(errno));290if (close(lso) != 0)291errx(-1, "not ok 13 - close() of listening socket: %s",292strerror(errno));293printf("ok 13 - setsockopt\n");294295return (0);296}297298299