1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2024 Gleb Smirnoff <glebius@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/socket.h> 29 #include <netinet/in.h> 30 #include <netgraph.h> 31 #include <netgraph/ng_socket.h> 32 #include <netgraph/ng_ksocket.h> 33 34 #include <errno.h> 35 36 #include <atf-c.h> 37 38 static void 39 hellocheck(int wr, int rd) 40 { 41 char sbuf[] = "Hello, peer!", rbuf[sizeof(sbuf)]; 42 43 ATF_REQUIRE(send(wr, sbuf, sizeof(sbuf), 0) == sizeof(sbuf)); 44 ATF_REQUIRE(recv(rd, rbuf, sizeof(rbuf), 0) == sizeof(sbuf)); 45 ATF_REQUIRE(strcmp(sbuf, rbuf) == 0); 46 } 47 48 #define OURHOOK "ks" 49 50 ATF_TC_WITHOUT_HEAD(udp_connect); 51 ATF_TC_BODY(udp_connect, tc) 52 { 53 struct sockaddr_in sin = { 54 .sin_family = AF_INET, 55 .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 56 .sin_len = sizeof(sin), 57 }; 58 socklen_t slen = sizeof(sin); 59 int ds, cs, us; 60 61 ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0); 62 ATF_REQUIRE(bind(us, (struct sockaddr *)&sin, sizeof(sin)) == 0); 63 ATF_REQUIRE(getsockname(us, (struct sockaddr *)&sin, &slen) == 0); 64 65 struct ngm_mkpeer mkp = { 66 .type = NG_KSOCKET_NODE_TYPE, 67 .ourhook = OURHOOK, 68 .peerhook = "inet/dgram/udp", 69 }; 70 ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0); 71 ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp, 72 sizeof(mkp)) >= 0); 73 ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE, 74 NGM_KSOCKET_CONNECT, &sin, sizeof(sin)) >= 0); 75 76 hellocheck(ds, us); 77 } 78 79 ATF_TC_WITHOUT_HEAD(udp_bind); 80 ATF_TC_BODY(udp_bind, tc) 81 { 82 struct sockaddr_in sin = { 83 .sin_family = AF_INET, 84 .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 85 .sin_len = sizeof(sin), 86 }; 87 struct ng_mesg *rep; 88 int ds, cs, us; 89 90 struct ngm_mkpeer mkp = { 91 .type = NG_KSOCKET_NODE_TYPE, 92 .ourhook = OURHOOK, 93 .peerhook = "inet/dgram/udp", 94 }; 95 ATF_REQUIRE(NgMkSockNode(NULL, &cs, &ds) == 0); 96 ATF_REQUIRE(NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, NGM_MKPEER, &mkp, 97 sizeof(mkp)) >= 0); 98 ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE, 99 NGM_KSOCKET_BIND, &sin, sizeof(sin)) >= 0); 100 ATF_REQUIRE(NgSendMsg(cs, ".:" OURHOOK, NGM_KSOCKET_COOKIE, 101 NGM_KSOCKET_GETNAME, NULL, 0) >= 0); 102 ATF_REQUIRE(NgAllocRecvMsg(cs, &rep, NULL) == sizeof(struct ng_mesg) + 103 sizeof(struct sockaddr_in)); 104 105 ATF_REQUIRE((us = socket(PF_INET, SOCK_DGRAM, 0)) > 0); 106 ATF_REQUIRE(connect(us, (struct sockaddr *)rep->data, 107 sizeof(struct sockaddr_in)) == 0); 108 109 hellocheck(us, ds); 110 } 111 112 ATF_TP_ADD_TCS(tp) 113 { 114 ATF_TP_ADD_TC(tp, udp_connect); 115 ATF_TP_ADD_TC(tp, udp_bind); 116 117 return (atf_no_error()); 118 } 119