Path: blob/main/tools/regression/netinet/tcpdrop/tcpdrop.c
39491 views
/*-1* Copyright (c) 2006 Robert N. M. Watson2* Copyright (c) 2011 Juniper Networks, Inc.3* All rights reserved.4*5* Portions of this software were developed by Robert N. M. Watson under6* contract to Juniper Networks, Inc.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930/*31* TCP regression test for the tcpdrop sysctl; build a loopback TCP32* connection, drop it, and make sure both endpoints return that the33* connection has been reset.34*/3536#include <sys/types.h>37#include <sys/socket.h>38#include <sys/sysctl.h>3940#include <netinet/in.h>4142#include <err.h>43#include <errno.h>44#include <signal.h>45#include <stdio.h>46#include <stdlib.h>47#include <string.h>48#include <unistd.h>4950static int51tcp_drop(struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote)52{53struct sockaddr_storage addrs[2];5455/*56* Sysctl accepts an array of two sockaddr's, the first being the57* 'foreign' sockaddr, the second being the 'local' sockaddr.58*/5960bcopy(sin_remote, &addrs[0], sizeof(*sin_remote));61bcopy(sin_local, &addrs[1], sizeof(*sin_local));6263return (sysctlbyname("net.inet.tcp.drop", NULL, 0, addrs,64sizeof(addrs)));65}6667static void68tcp_server(pid_t partner, int listen_fd)69{70int error, accept_fd;71ssize_t len;72char ch;7374accept_fd = accept(listen_fd, NULL, NULL);75if (accept_fd < 0) {76error = errno;77(void)kill(partner, SIGTERM);78errno = error;79err(-1, "tcp_server: accept");80}8182/*83* Send one byte, make sure that worked, wait for the drop, and try84* sending another. By sending small amounts, we avoid blocking85* waiting on the remote buffer to be drained.86*/87ch = 'A';88len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL);89if (len < 0) {90error = errno;91(void)kill(partner, SIGTERM);92errno = error;93err(-1, "tcp_server: send (1)");94}95if (len != sizeof(ch)) {96(void)kill(partner, SIGTERM);97errx(-1, "tcp_server: send (1) len");98}99100sleep (10);101102ch = 'A';103len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL);104if (len >= 0) {105(void)kill(partner, SIGTERM);106errx(-1, "tcp_server: send (2): success");107} else if (errno != EPIPE) {108error = errno;109(void)kill(partner, SIGTERM);110errno = error;111err(-1, "tcp_server: send (2)");112}113114close(accept_fd);115close(listen_fd);116}117118static void119tcp_client(pid_t partner, u_short port)120{121struct sockaddr_in sin, sin_local;122int error, sock;123socklen_t slen;124ssize_t len;125char ch;126127sleep(1);128129sock = socket(PF_INET, SOCK_STREAM, 0);130if (sock < 0) {131error = errno;132(void)kill(partner, SIGTERM);133errno = error;134err(-1, "socket");135}136137bzero(&sin, sizeof(sin));138sin.sin_family = AF_INET;139sin.sin_len = sizeof(sin);140sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);141sin.sin_port = port;142143if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {144error = errno;145(void)kill(partner, SIGTERM);146errno = error;147err(-1, "connect");148}149150slen = sizeof(sin_local);151if (getsockname(sock, (struct sockaddr *)&sin_local, &slen) < 0) {152error = errno;153(void)kill(partner, SIGTERM);154errno = error;155err(-1, "getsockname");156}157158/*159* Send one byte, make sure that worked, wait for the drop, and try160* sending another. By sending small amounts, we avoid blocking161* waiting on the remote buffer to be drained.162*/163ch = 'A';164len = send(sock, &ch, sizeof(ch), MSG_NOSIGNAL);165if (len < 0) {166error = errno;167(void)kill(partner, SIGTERM);168errno = error;169err(-1, "tcp_client: send (1)");170}171if (len != sizeof(ch)) {172(void)kill(partner, SIGTERM);173errx(-1, "tcp_client: send (1) len");174}175176sleep(5);177if (tcp_drop(&sin_local, &sin) < 0) {178error = errno;179(void)kill(partner, SIGTERM);180errno = error;181err(-1, "tcp_client: tcp_drop");182}183sleep(5);184185ch = 'A';186len = send(sock, &ch, sizeof(ch), MSG_NOSIGNAL);187if (len >= 0) {188(void)kill(partner, SIGTERM);189errx(-1, "tcp_client: send (2): success");190} else if (errno != EPIPE) {191error = errno;192(void)kill(partner, SIGTERM);193errno = error;194err(-1, "tcp_client: send (2)");195}196close(sock);197}198199int200main(int argc, char *argv[])201{202pid_t child_pid, parent_pid;203struct sockaddr_in sin;204int listen_fd;205u_short port;206socklen_t len;207208listen_fd = socket(PF_INET, SOCK_STREAM, 0);209if (listen_fd < 0)210err(-1, "socket");211212/*213* We use the loopback, but let the kernel select a port for the214* server socket.215*/216bzero(&sin, sizeof(sin));217sin.sin_family = AF_INET;218sin.sin_len = sizeof(sin);219sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);220221if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)222err(-1, "bind");223224if (listen(listen_fd, -1) < 0)225err(-1, "listen");226227/*228* Query the port so that the client can use it.229*/230bzero(&sin, sizeof(sin));231sin.sin_family = AF_INET;232sin.sin_len = sizeof(sin);233len = sizeof(sin);234if (getsockname(listen_fd, (struct sockaddr *)&sin, &len) < 0)235err(-1, "getsockname");236port = sin.sin_port;237printf("Using port %d\n", ntohs(port));238239if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)240err(-1, "signal");241242parent_pid = getpid();243child_pid = fork();244if (child_pid < 0)245err(-1, "fork");246if (child_pid == 0) {247child_pid = getpid();248tcp_server(parent_pid, listen_fd);249} else250tcp_client(child_pid, port);251252return (0);253}254255256