? .swp ? ? Index: nc.1 =================================================================== RCS file: /cvs/openbsd/src/usr.bin/nc/nc.1,v retrieving revision 1.45 diff -u -r1.45 nc.1 --- nc.1 31 May 2007 19:20:13 -0000 1.45 +++ nc.1 24 Oct 2007 11:19:34 -0000 @@ -34,7 +34,7 @@ .Sh SYNOPSIS .Nm nc .Bk -words -.Op Fl 46DdhklnrStUuvz +.Op Fl 46CDdhklnrStUuvz .Op Fl i Ar interval .Op Fl P Ar proxy_username .Op Fl p Ar source_port @@ -93,6 +93,8 @@ Forces .Nm to use IPv6 addresses only. +.It Fl C +Convert LF (line feed) symbol into CR LF sequence (carriage return, line feed). .It Fl D Enable debugging on the socket. .It Fl d @@ -317,7 +319,7 @@ of requests required by the server. As another example, an email may be submitted to an SMTP server using: .Bd -literal -offset indent -$ nc localhost 25 \*(Lt\*(Lt EOF +$ nc -C localhost 25 \*(Lt\*(Lt EOF HELO host.example.com MAIL FROM:\*(Ltuser@host.example.com\*(Gt RCPT TO:\*(Ltuser2@host.example.com\*(Gt Index: netcat.c =================================================================== RCS file: /cvs/openbsd/src/usr.bin/nc/netcat.c,v retrieving revision 1.89 diff -u -r1.89 netcat.c --- netcat.c 20 Feb 2007 14:11:17 -0000 1.89 +++ netcat.c 24 Oct 2007 11:19:34 -0000 @@ -82,6 +82,7 @@ int Dflag; /* sodebug */ int Sflag; /* TCP MD5 signature option */ int Tflag = -1; /* IP Type of Service */ +int Cflag = 0; /* LF -> CR LF convertion */ int timeout = -1; int family = AF_UNSPEC; @@ -91,6 +92,7 @@ void build_ports(char *); void help(void); int local_listen(char *, char *, struct addrinfo); +size_t filt_crlf(const unsigned char *, size_t, unsigned char **, size_t *); void readwrite(int); int remote_connect(const char *, const char *, struct addrinfo); int socks_connect(const char *, const char *, struct addrinfo, @@ -123,7 +125,7 @@ sv = NULL; while ((ch = getopt(argc, argv, - "46Ddhi:jklnP:p:rSs:tT:Uuvw:X:x:z")) != -1) { + "46CDdhi:jklnP:p:rSs:tT:Uuvw:X:x:z")) != -1) { switch (ch) { case '4': family = AF_INET; @@ -211,6 +213,9 @@ case 'T': Tflag = parse_iptos(optarg); break; + case 'C': + Cflag = 1; + break; default: usage(1); } @@ -575,6 +580,42 @@ } /* + * filt_crlf() + * Converts '\n' into '\r' '\n'. + * Returns 0 in case of error. That's why ``lfsz'' must be greater that 0. + */ +size_t +filt_crlf(const unsigned char *lf, size_t lfsz, unsigned char **crlf, + size_t *crlfsz) +{ + unsigned char *p, *nxt; + size_t i, newsz; + + newsz = lfsz; + for (i = 0; i < lfsz; i++) + if (lf[i] == '\n') + newsz++; + + if (*crlfsz < newsz) { + p = (unsigned char *)malloc(newsz); + if (p == NULL) + return (0); + if (*crlf != NULL) + free(*crlf); + *crlf = p; + *crlfsz = newsz; + } + + nxt = *crlf; + for (i = 0; i < lfsz; i++) { + if (lf[i] == '\n') + *nxt++ = '\r'; + *nxt++ = lf[i]; + } + return (newsz); +} + +/* * readwrite() * Loop that polls on the network file descriptor and stdin. */ @@ -586,7 +627,11 @@ int n, wfd = fileno(stdin); int lfd = fileno(stdout); int plen; + unsigned char *crlf, *p; + size_t crlfsz; + crlf = NULL; + crlfsz = 0; plen = jflag ? 8192 : 1024; /* Setup Network FD */ @@ -607,11 +652,11 @@ } if (n == 0) - return; + break; if (pfd[0].revents & POLLIN) { if ((n = read(nfd, buf, plen)) < 0) - return; + break; else if (n == 0) { shutdown(nfd, SHUT_RD); pfd[0].fd = -1; @@ -620,23 +665,37 @@ if (tflag) atelnet(nfd, buf, n); if (atomicio(vwrite, lfd, buf, n) != n) - return; + break; } } if (!dflag && pfd[1].revents & POLLIN) { if ((n = read(wfd, buf, plen)) < 0) - return; + break; else if (n == 0) { shutdown(nfd, SHUT_WR); pfd[1].fd = -1; pfd[1].events = 0; } else { - if (atomicio(vwrite, nfd, buf, n) != n) - return; + p = buf; + if (Cflag) { + n = filt_crlf(buf, n, &crlf, + &crlfsz); + if (n == 0) { + warnx("malloc"); + break; + } + p = crlf; + } + + if (atomicio(vwrite, nfd, p, n) != n) + break; } } } + + if (crlf != NULL) + free(crlf); } /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */ @@ -808,6 +867,7 @@ \t-4 Use IPv4\n\ \t-6 Use IPv6\n\ \t-D Enable the debug socket option\n\ + \t-C Convert LF symbol into CR LF sequence\n\ \t-d Detach from stdin\n\ \t-h This help text\n\ \t-i secs\t Delay interval for lines sent, ports scanned\n\