From nobody Mon Aug 28 11:25:23 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RZ7X41292z4rRmd; Mon, 28 Aug 2023 11:25:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RZ7X40WzQz3Zrv; Mon, 28 Aug 2023 11:25:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693221924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=h47qERu0OEIgecRtepS2otfO5Qh4i7ZUacDyJt5e4fo=; b=oY3VGXTYlLYosC9U6YpUSmutR8QYt2OuKN2ZXFyLJPLItEuc5IUoLJe0XcBsM6itoVXf7P 5Lr2XYWmqw83AJW97aRy0bhrp/2sEOdEoUixXVixBIoAOsKQcXHdfm365BYEt1Q5Ou6O1R E84JhYWelS8JtDxh4enUDQV5D+Vx3+qUuja1iapGHgVmnWCI5wTxksDddQJx0mbIbmcOYW M7/i5rGz2OWfqqJEiwePLpFxkJMHPOmXH5YKXvMAPnEJXdzVjKr27qxF+Ee9zmcQaxs0Ax 5ommINPGqURmwBXvqm+P1RxJ/SB4eTjtUiFKbdGWrXKYS6p/2R1LxSF4bkDM9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693221924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=h47qERu0OEIgecRtepS2otfO5Qh4i7ZUacDyJt5e4fo=; b=EgIZiFfsTcIvbq88pLuSzbPe9lPwOHo4mrJ4CD0bvHm+u4HlRTbfHMFmKfk2ocZgzuOITK 9OQGGmtx7kOpFNGqHuVOuwK7s7uosf0aY0U31FikMeHPHwGqJpSeK4f6rNNQPSz3vo8UCf OCBg7HCOxwe3sptb1YCJm6FKAko6HymJZ8N7sU8nh9pXdo//QVRj+jCuwf/z9ZGaW9ynZY A6ITRVh68+spPJ+paCRtdTfeF+nrdtO1uiJ7CAPQkTk5ZsJ8ZRwqH31fRkEcUjLUisC372 9IcRboFDEGCIJzvTMxMaSIGiy2JPvDMdPDf99gcaxy1U1mAs6GPC/+J+U4QT+g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1693221924; a=rsa-sha256; cv=none; b=ZBpouunPKj5yTOO9ToTq2fO0Wzudo2cl2NqA8bSBj87ue3bc7XDz6wDNnWxBPmp5NB0fYn 5+CG3PwQ8+R2dPPIjw6sQ4KyzpIHrodexW9RsmZvGVLE53IMODDerLPcYadhez5ccIDGa8 dIm5JhxcSH99qKgC9ENZZhSQpdm/nA4ImnT1405ru0acexAvnUVu/KuyyE0rKFqc0tu3r1 QbUd0sG/Fcpqmn9CoKh1iHuSuORcDjTKu1o+2fbSuu0TJXlmvJxLtO+N47hxf0eLUrMiZH 467Glk366mBD0sqC4FUiD8V408xhFJX0aiv76uUaz8fvO0jKmOYoJ4BrR8Byyw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RZ7X36hxKz16QT; Mon, 28 Aug 2023 11:25:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 37SBPNkM064066; Mon, 28 Aug 2023 11:25:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 37SBPNjq064063; Mon, 28 Aug 2023 11:25:23 GMT (envelope-from git) Date: Mon, 28 Aug 2023 11:25:23 GMT Message-Id: <202308281125.37SBPNjq064063@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Pietro Cerutti Subject: git: d4b3aefdf94f - main - netcat: add --crlf to convert LF into CRLF List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: gahr X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d4b3aefdf94fe401f6bfc93a33b69e21dd7a634a Auto-Submitted: auto-generated The branch main has been updated by gahr: URL: https://cgit.FreeBSD.org/src/commit/?id=d4b3aefdf94fe401f6bfc93a33b69e21dd7a634a commit d4b3aefdf94fe401f6bfc93a33b69e21dd7a634a Author: Pietro Cerutti AuthorDate: 2023-08-17 11:42:23 +0000 Commit: Pietro Cerutti CommitDate: 2023-08-28 10:58:17 +0000 netcat: add --crlf to convert LF into CRLF This adds the --crlf option to netcat, which triggers translation of \n characters into \r\n sequences in the input -> network direction. The Linux version of nc also supports this functionality with --crlf and -C. The OpenBSD version uses -C to specify client certificates. Our version is too old and doesn't have it, but I avoided adding -C anyway to ease future syncs with upstream. Attempts to upstream the feature were unsuccessful: https://marc.info/?t=169282068500001 Differential Revision: https://reviews.freebsd.org/D41489 --- contrib/netcat/nc.1 | 3 +++ contrib/netcat/netcat.c | 50 ++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/contrib/netcat/nc.1 b/contrib/netcat/nc.1 index 9f8696135f35..2e34cdc586c3 100644 --- a/contrib/netcat/nc.1 +++ b/contrib/netcat/nc.1 @@ -42,6 +42,7 @@ .Op Fl i Ar interval .Op Fl -no-tcpopt .Op Fl -sctp +.Op Fl -crlf .Op Fl O Ar length .Op Fl P Ar proxy_username .Op Fl p Ar source_port @@ -104,6 +105,8 @@ to use IPv4 addresses only. Forces .Nm to use IPv6 addresses only. +.It Fl -crlf +Convert LF into CRLF when sending data over the network. .It Fl D Enable debugging on the socket. .It Fl d diff --git a/contrib/netcat/netcat.c b/contrib/netcat/netcat.c index 65266b99c28e..279489a45e92 100644 --- a/contrib/netcat/netcat.c +++ b/contrib/netcat/netcat.c @@ -94,6 +94,7 @@ int Nflag; /* shutdown() network socket */ int nflag; /* Don't do name look up */ int FreeBSD_Oflag; /* Do not use TCP options */ int FreeBSD_sctp; /* Use SCTP */ +int FreeBSD_crlf; /* Convert LF to CRLF */ char *Pflag; /* Proxy username */ char *pflag; /* Localport flag */ int rflag; /* Random ports flag */ @@ -136,7 +137,8 @@ void set_common_sockopts(int, int); int map_tos(char *, int *); void report_connect(const struct sockaddr *, socklen_t); void usage(int); -ssize_t drainbuf(int, unsigned char *, size_t *); +ssize_t write_wrapper(int, const void *, size_t); +ssize_t drainbuf(int, unsigned char *, size_t *, int); ssize_t fillbuf(int, unsigned char *, size_t *); #ifdef IPSEC @@ -165,6 +167,7 @@ main(int argc, char *argv[]) struct addrinfo proxyhints; char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE]; struct option longopts[] = { + { "crlf", no_argument, &FreeBSD_crlf, 1 }, { "no-tcpopt", no_argument, &FreeBSD_Oflag, 1 }, { "sctp", no_argument, &FreeBSD_sctp, 1 }, { "tun", required_argument, NULL, FREEBSD_TUN }, @@ -972,7 +975,7 @@ readwrite(int net_fd) /* try to write to network */ if (pfd[POLL_NETOUT].revents & POLLOUT && stdinbufpos > 0) { ret = drainbuf(pfd[POLL_NETOUT].fd, stdinbuf, - &stdinbufpos); + &stdinbufpos, FreeBSD_crlf); if (ret == -1) pfd[POLL_NETOUT].fd = -1; /* buffer empty - remove self from polling */ @@ -1007,7 +1010,7 @@ readwrite(int net_fd) /* try to write to stdout */ if (pfd[POLL_STDOUT].revents & POLLOUT && netinbufpos > 0) { ret = drainbuf(pfd[POLL_STDOUT].fd, netinbuf, - &netinbufpos); + &netinbufpos, 0); if (ret == -1) pfd[POLL_STDOUT].fd = -1; /* buffer empty - remove self from polling */ @@ -1037,17 +1040,41 @@ readwrite(int net_fd) } ssize_t -drainbuf(int fd, unsigned char *buf, size_t *bufpos) +write_wrapper(int fd, const void *buf, size_t buflen) { - ssize_t n; + ssize_t n = write(fd, buf, buflen); + /* don't treat EAGAIN, EINTR as error */ + return (n == -1 && (errno == EAGAIN || errno == EINTR)) ? -2 : n; +} + +ssize_t +drainbuf(int fd, unsigned char *buf, size_t *bufpos, int crlf) +{ + ssize_t n = *bufpos, n2 = 0; ssize_t adjust; + unsigned char *lf = NULL; + + if (crlf) { + lf = memchr(buf, '\n', *bufpos); + if (lf && (lf == buf || *(lf - 1) != '\r')) + n = lf - buf; + else + lf = NULL; + } + + if (n != 0) { + n = write_wrapper(fd, buf, n); + if (n <= 0) + return n; + } + + if (lf) { + n2 = write_wrapper(fd, "\r\n", 2); + if (n2 <= 0) + return n2; + n += 1; + } - n = write(fd, buf, *bufpos); - /* don't treat EAGAIN, EINTR as error */ - if (n == -1 && (errno == EAGAIN || errno == EINTR)) - n = -2; - if (n <= 0) - return n; /* adjust buffer */ adjust = *bufpos - n; if (adjust > 0) @@ -1440,6 +1467,7 @@ help(void) fprintf(stderr, "\tCommand Summary:\n\ \t-4 Use IPv4\n\ \t-6 Use IPv6\n\ + \t--crlf Convert LF into CRLF when sending data over the network\n\ \t-D Enable the debug socket option\n\ \t-d Detach from stdin\n"); #ifdef IPSEC