Path: blob/main/tests/sys/netinet/tcp_implied_connect.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024 Gleb Smirnoff <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/socket.h>28#include <netinet/in.h>29#include <stdio.h>30#include <unistd.h>31#include <string.h>3233#include <atf-c.h>3435ATF_TC_WITHOUT_HEAD(tcp_implied_connect);36ATF_TC_BODY(tcp_implied_connect, tc)37{38struct sockaddr_in sin = {39.sin_family = AF_INET,40.sin_len = sizeof(sin),41};42const char buf[] = "hello";43char repl[sizeof(buf)];44socklen_t len;45int s, c, a;4647ATF_REQUIRE(s = socket(PF_INET, SOCK_STREAM, 0));48ATF_REQUIRE(c = socket(PF_INET, SOCK_STREAM, 0));4950ATF_REQUIRE(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0);51len = sizeof(sin);52ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sin, &len) == 0);53sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);54ATF_REQUIRE(listen(s, -1) == 0);55#if 056/*57* The disabled code is that you would normally do.58*/59ATF_REQUIRE(connect(c, (struct sockaddr *)&sin, sizeof(sin)) == 0);60ATF_REQUIRE(send(c, &buf, sizeof(buf), 0) == sizeof(buf));61#else62/*63* And this is implied connect.64*/65ATF_REQUIRE(sendto(c, &buf, sizeof(buf), 0, (struct sockaddr *)&sin,66sizeof(sin)) == sizeof(buf));67#endif6869ATF_REQUIRE((a = accept(s, NULL, NULL)) != 1);70ATF_REQUIRE(recv(a, &repl, sizeof(repl), 0) == sizeof(buf));71ATF_REQUIRE(strcmp(buf, repl) == 0);72}7374ATF_TP_ADD_TCS(tp)75{76ATF_TP_ADD_TC(tp, tcp_implied_connect);7778return (atf_no_error());79}808182