Read & Write

Once the connection is established, it is time to transfer data. The readwrite is the core function of netcat, and thankfully, it is well-commented, and refrain you from losing. In this post, I will only cherry-pick code snippets that I think is important.

(1) There is a -d option that if set, your netcat program can not send data inputted from stdin:

  1. ......
  2. int dflag; /* detached, no stdin */
  3. /* don't read from stdin if requested */
  4. if (dflag)
  5. stdin_fd = -1;

(2) readwrite uses poll to manage 3 file descriptors: stdin, stdout, and network socket, and monitor whether there is data from stdin or network socket. If there is no need for “high-priority” (e.g., out-of-band) data, POLLIN is enough, otherwise the monitor events should be POLLIN|POLLPRI. This is similar for POLLOUT and POLLWRBAND:

  1. ......
  2. /* stdin */
  3. pfd[POLL_STDIN].fd = stdin_fd;
  4. pfd[POLL_STDIN].events = POLLIN;
  5. /* network out */
  6. pfd[POLL_NETOUT].fd = net_fd;
  7. pfd[POLL_NETOUT].events = 0;
  8. /* network in */
  9. pfd[POLL_NETIN].fd = net_fd;
  10. pfd[POLL_NETIN].events = POLLIN;
  11. /* stdout */
  12. pfd[POLL_STDOUT].fd = stdout_fd;
  13. pfd[POLL_STDOUT].events = 0;
  14. ......
  15. /* poll */
  16. num_fds = poll(pfd, 4, timeout);
  17. /* treat poll errors */
  18. if (num_fds == -1)
  19. err(1, "polling error");
  20. /* timeout happened */
  21. if (num_fds == 0)
  22. return;

There are 3 values(POLLERR, POLLNVAL and POLLHUP) which are only used in struct pollfd‘s revents. If POLLERR or POLLNVAL is detected, it’s not necessary to poll this file descriptor furthermore:

  1. /* treat socket error conditions */
  2. for (n = 0; n < 4; n++) {
  3. if (pfd[n].revents & (POLLERR|POLLNVAL)) {
  4. pfd[n].fd = -1;
  5. }
  6. }

POLLHUP means the device or network socket has been disconnected, and it is exclusive with POLLOUT, but not input events:POLLIN, POLLRDNORM, POLLRDBAND, and POLLPRI. That’s because the buffer may still has data to be read:

  1. /* reading is possible after HUP */
  2. if (pfd[POLL_STDIN].events & POLLIN &&
  3. pfd[POLL_STDIN].revents & POLLHUP &&
  4. !(pfd[POLL_STDIN].revents & POLLIN))
  5. pfd[POLL_STDIN].fd = -1;
  6. ......
  7. if (pfd[POLL_NETOUT].revents & POLLHUP) {
  8. if (Nflag)
  9. shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
  10. pfd[POLL_NETOUT].fd = -1;
  11. }

BTW, the Nflag is set through -N option, which means shutting down the network socket if there is no any input or the network connection is already disconnected:

  1. int Nflag; /* shutdown() network socket */
  2. ......
  3. /* stdin gone and queue empty? */
  4. if (pfd[POLL_STDIN].fd == -1 && stdinbufpos == 0) {
  5. if (pfd[POLL_NETOUT].fd != -1 && Nflag)
  6. shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
  7. pfd[POLL_NETOUT].fd = -1;
  8. }

(3) The following code shows how to read from stdin and send data to remote:

  1. ......
  2. #define BUFSIZE 16384
  3. .....
  4. unsigned char stdinbuf[BUFSIZE];
  5. size_t stdinbufpos = 0;
  6. ......
  7. /* try to read from stdin */
  8. if (pfd[POLL_STDIN].revents & POLLIN && stdinbufpos < BUFSIZE) {
  9. ret = fillbuf(pfd[POLL_STDIN].fd, stdinbuf,
  10. &stdinbufpos, NULL);
  11. if (ret == TLS_WANT_POLLIN)
  12. pfd[POLL_STDIN].events = POLLIN;
  13. else if (ret == TLS_WANT_POLLOUT)
  14. pfd[POLL_STDIN].events = POLLOUT;
  15. else if (ret == 0 || ret == -1)
  16. pfd[POLL_STDIN].fd = -1;
  17. /* read something - poll net out */
  18. if (stdinbufpos > 0)
  19. pfd[POLL_NETOUT].events = POLLOUT;
  20. /* filled buffer - remove self from polling */
  21. if (stdinbufpos == BUFSIZE)
  22. pfd[POLL_STDIN].events = 0;
  23. }
  24. /* try to write to network */
  25. if (pfd[POLL_NETOUT].revents & POLLOUT && stdinbufpos > 0) {
  26. ret = drainbuf(pfd[POLL_NETOUT].fd, stdinbuf,
  27. &stdinbufpos, tls_ctx);
  28. if (ret == TLS_WANT_POLLIN)
  29. pfd[POLL_NETOUT].events = POLLIN;
  30. else if (ret == TLS_WANT_POLLOUT)
  31. pfd[POLL_NETOUT].events = POLLOUT;
  32. else if (ret == -1)
  33. pfd[POLL_NETOUT].fd = -1;
  34. /* buffer empty - remove self from polling */
  35. if (stdinbufpos == 0)
  36. pfd[POLL_NETOUT].events = 0;
  37. /* buffer no longer full - poll stdin again */
  38. if (stdinbufpos < BUFSIZE)
  39. pfd[POLL_STDIN].events = POLLIN;
  40. }

Please ignore all TLS* stuff now. It just means using libtls instead of traditional read/write APIs to receive/send data, and I will cover libtls in future. stdinbufpos denotes the first empty position in buffer, therefore, netcat knows how many bytes could be sent.

fillbuf and drainbuf code is like following:

  1. ssize_t
  2. fillbuf(int fd, unsigned char *buf, size_t *bufpos, struct tls *tls)
  3. {
  4. size_t num = BUFSIZE - *bufpos;
  5. ssize_t n;
  6. if (tls)
  7. n = tls_read(tls, buf + *bufpos, num);
  8. else {
  9. n = read(fd, buf + *bufpos, num);
  10. /* don't treat EAGAIN, EINTR as error */
  11. if (n == -1 && (errno == EAGAIN || errno == EINTR))
  12. n = TLS_WANT_POLLIN;
  13. }
  14. if (n <= 0)
  15. return n;
  16. *bufpos += n;
  17. return n;
  18. }
  19. ssize_t
  20. drainbuf(int fd, unsigned char *buf, size_t *bufpos, struct tls *tls)
  21. {
  22. ssize_t n;
  23. ssize_t adjust;
  24. if (tls)
  25. n = tls_write(tls, buf, *bufpos);
  26. else {
  27. n = write(fd, buf, *bufpos);
  28. /* don't treat EAGAIN, EINTR as error */
  29. if (n == -1 && (errno == EAGAIN || errno == EINTR))
  30. n = TLS_WANT_POLLOUT;
  31. }
  32. if (n <= 0)
  33. return n;
  34. /* adjust buffer */
  35. adjust = *bufpos - n;
  36. if (adjust > 0)
  37. memmove(buf, buf + n, adjust);
  38. *bufpos -= n;
  39. return n;
  40. }

You should get 2 takeaways from preceding code:
a) EAGAIN and EINTR are not considered as real errors;
b) Don’t forget to adjust bufpos and buf, i.e., stdinbufpos and stdinbuf accordingly.

As for reading data from network socket and outputting it to stdout flow, it is very similar, so there is no need to say more here.

(4) There is a -W recvlimit option which specifies after receiving how many packets, the program should terminate:

  1. ......
  2. int recvcount, recvlimit;
  3. ......
  4. /* try to read from network */
  5. if (pfd[POLL_NETIN].revents & POLLIN && netinbufpos < BUFSIZE) {
  6. ......
  7. if (recvlimit > 0 && ++recvcount >= recvlimit) {
  8. if (pfd[POLL_NETIN].fd != -1)
  9. shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
  10. pfd[POLL_NETIN].fd = -1;
  11. pfd[POLL_STDIN].fd = -1;
  12. }
  13. ......
  14. }