From 6b0ee4ef79387cd2bf2d8bd813c5305182512ec4 Mon Sep 17 00:00:00 2001 From: bluhm Date: Thu, 4 Feb 2021 18:46:25 +0000 Subject: [PATCH] Implement the nc(1) -D socket debug option also in tcpbench(1). This allows to analyse TCP connections. OK claudio@ --- usr.bin/tcpbench/tcpbench.1 | 10 ++++++---- usr.bin/tcpbench/tcpbench.c | 24 ++++++++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/usr.bin/tcpbench/tcpbench.1 b/usr.bin/tcpbench/tcpbench.1 index 9f7a7063971..13232f0eccd 100644 --- a/usr.bin/tcpbench/tcpbench.1 +++ b/usr.bin/tcpbench/tcpbench.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tcpbench.1,v 1.28 2020/05/04 12:13:09 sthen Exp $ +.\" $OpenBSD: tcpbench.1,v 1.29 2021/02/04 18:46:25 bluhm Exp $ .\" .\" Copyright (c) 2008 Damien Miller .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: May 4 2020 $ +.Dd $Mdocdate: February 4 2021 $ .Dt TCPBENCH 1 .Os .Sh NAME @@ -24,7 +24,7 @@ .Nm .Fl l .Nm -.Op Fl 46RUuv +.Op Fl 46DRUuv .Op Fl B Ar buf .Op Fl b Ar sourceaddr .Op Fl k Ar kvars @@ -39,7 +39,7 @@ .Nm .Bk -words .Fl s -.Op Fl 46Uuv +.Op Fl 46DUuv .Op Fl B Ar buf .Op Fl k Ar kvars .Op Fl p Ar port @@ -111,6 +111,8 @@ stream. .It Fl b Ar sourceaddr Specify the IP address to send the packets from, which is useful on machines with multiple interfaces. +.It Fl D +Enable debugging on the socket. .It Fl k Ar kvars Specify one or more kernel variables to monitor; multiple variables must be separated with commas. diff --git a/usr.bin/tcpbench/tcpbench.c b/usr.bin/tcpbench/tcpbench.c index 18da6be0653..c56752ce10e 100644 --- a/usr.bin/tcpbench/tcpbench.c +++ b/usr.bin/tcpbench/tcpbench.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tcpbench.c,v 1.63 2020/05/04 12:13:09 sthen Exp $ */ +/* $OpenBSD: tcpbench.c,v 1.64 2021/02/04 18:46:25 bluhm Exp $ */ /* * Copyright (c) 2008 Damien Miller @@ -65,6 +65,7 @@ /* Our tcpbench globals */ struct { + int Dflag; /* Socket debug */ int Sflag; /* Socket buffer size */ u_int rflag; /* Report rate (ms) */ int sflag; /* True if server */ @@ -198,10 +199,10 @@ usage(void) { fprintf(stderr, "usage: tcpbench -l\n" - " tcpbench [-46RUuv] [-B buf] [-b sourceaddr] [-k kvars] [-n connections]\n" + " tcpbench [-46DRUuv] [-B buf] [-b sourceaddr] [-k kvars] [-n connections]\n" " [-p port] [-r interval] [-S space] [-T toskeyword]\n" " [-t secs] [-V rtable] hostname\n" - " tcpbench -s [-46Uuv] [-B buf] [-k kvars] [-p port] [-r interval]\n" + " tcpbench -s [-46DUuv] [-B buf] [-k kvars] [-p port] [-r interval]\n" " [-S space] [-T toskeyword] [-V rtable] [hostname]\n"); exit(1); } @@ -857,6 +858,11 @@ server_init(struct addrinfo *aitop) warn("socket"); continue; } + if (ptb->Dflag) { + if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, + &ptb->Dflag, sizeof(ptb->Dflag))) + err(1, "setsockopt SO_DEBUG"); + } if (ptb->Tflag != -1 && ai->ai_family == AF_INET) { if (setsockopt(sock, IPPROTO_IP, IP_TOS, &ptb->Tflag, sizeof(ptb->Tflag))) @@ -970,6 +976,11 @@ client_init(struct addrinfo *aitop, int nconn, struct addrinfo *aib) warn("socket"); continue; } + if (ptb->Dflag) { + if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, + &ptb->Dflag, sizeof(ptb->Dflag))) + err(1, "setsockopt SO_DEBUG"); + } if (aib != NULL) { saddr_ntop(aib->ai_addr, aib->ai_addrlen, tmp, sizeof(tmp)); @@ -1138,6 +1149,7 @@ main(int argc, char **argv) setvbuf(stdout, NULL, _IOLBF, 0); ptb = &tcpbench; ptb->dummybuf_len = 0; + ptb->Dflag = 0; ptb->Sflag = ptb->sflag = ptb->vflag = ptb->Rflag = ptb->Uflag = 0; ptb->kvmh = NULL; ptb->kvars = NULL; @@ -1147,7 +1159,8 @@ main(int argc, char **argv) aib = NULL; secs = 0; - while ((ch = getopt(argc, argv, "46b:B:hlk:n:p:Rr:sS:t:T:uUvV:")) != -1) { + while ((ch = getopt(argc, argv, "46b:B:Dhlk:n:p:Rr:sS:t:T:uUvV:")) + != -1) { switch (ch) { case '4': family = PF_INET; @@ -1158,6 +1171,9 @@ main(int argc, char **argv) case 'b': srcbind = optarg; break; + case 'D': + ptb->Dflag = 1; + break; case 'l': list_kvars(); exit(0); -- 2.20.1