From fffd3a67adecc26a631515b4d89f8ca8199b5eda Mon Sep 17 00:00:00 2001 From: Conrad Pankoff Date: Thu, 8 Aug 2019 18:22:34 +1000 Subject: Userland: Implement -l, -v, -N, -s, and -p for netcat --- Userland/nc.cpp | 220 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 167 insertions(+), 53 deletions(-) (limited to 'Userland/nc.cpp') diff --git a/Userland/nc.cpp b/Userland/nc.cpp index cb597a2811..e097be0039 100644 --- a/Userland/nc.cpp +++ b/Userland/nc.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -11,68 +13,174 @@ #define max(a, b) ((a > b) ? a : b) +void exit_with_usage(int rc) +{ + fprintf(stderr, "Usage: nc [-l] [-v] [-h] [-N] [-s source] [-p source_port] [destination] [port]\n"); + exit(rc); +} + int main(int argc, char** argv) { - if (argc < 3) { - fprintf(stderr, "Usage: nc \n"); - return -1; + bool should_listen = false; + bool verbose = false; + char *source_addr = nullptr; + int source_port = 0; + bool should_close = false; + + int opt; + while ((opt = getopt(argc, argv, "hlvs:p:N")) != -1) { + switch (opt) { + case 'h': + exit_with_usage(0); + break; + case 'l': + should_listen = true; + break; + case 'v': + verbose = true; + break; + case 's': + source_addr = strdup(optarg); + break; + case 'p': + source_port = atoi(optarg); + break; + case 'N': + should_close = true; + break; + } } - const char* addr_str = argv[1]; - int port = atoi(argv[2]); + int fd; - int fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd < 0) { - perror("socket"); - return 1; - } + if (should_listen) { + int listen_fd = socket(AF_INET, SOCK_STREAM, 0); + if (listen_fd < 0) { + perror("socket"); + return 1; + } - struct timeval timeout { - 3, 0 - }; - int rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - if (rc < 0) { - perror("setsockopt"); - return 1; - } - rc = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); - if (rc < 0) { - perror("setsockopt"); - return 1; - } + struct sockaddr_in sa; + memset(&sa, 0, sizeof sa); + sa.sin_family = AF_INET; + sa.sin_port = htons(source_port); + sa.sin_addr.s_addr = htonl(INADDR_ANY); + if (source_addr) { + if (inet_pton(AF_INET, source_addr, &sa.sin_addr) < 0) { + perror("inet_pton"); + return 1; + } + } - struct sockaddr_in dst_addr; - memset(&dst_addr, 0, sizeof(dst_addr)); + if (bind(listen_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) { + perror("bind"); + return 1; + } - dst_addr.sin_family = AF_INET; - dst_addr.sin_port = htons(port); - rc = inet_pton(AF_INET, addr_str, &dst_addr.sin_addr); - if (rc <= 0) { - perror("inet_pton"); - return 1; - } + if (listen(listen_fd, 1) == -1) { + perror("listen"); + return 1; + } + + char addr_str[100]; + + struct sockaddr_in sin; + socklen_t len = sizeof(sin); + + if (getsockname(listen_fd, (struct sockaddr*)&sin, &len) == -1) { + perror("getsockname"); + return 1; + } + if (verbose) + fprintf(stderr, "waiting for a connection on %s:%d\n", inet_ntop(sin.sin_family, &sin.sin_addr, addr_str, sizeof(addr_str) - 1), ntohs(sin.sin_port)); + + fd = accept(listen_fd, (struct sockaddr*)&sin, &len); + if (fd == -1) { + perror("accept"); + return 1; + } + + if (verbose) + fprintf(stderr, "got connection from %s:%d\n", inet_ntop(sin.sin_family, &sin.sin_addr, addr_str, sizeof(addr_str) - 1), ntohs(sin.sin_port)); + + if (close(listen_fd) == -1) { + perror("close"); + return 1; + }; + } else { + if ((argc - optind) < 2) { + exit_with_usage(-1); + } + + const char* addr_arg = argv[optind]; + int port_arg = atoi(argv[optind + 1]); + if (port_arg < 0) { + perror("atoi"); + return 1; + } + + fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) { + perror("socket"); + return 1; + } + + struct timeval timeout { + 3, 0 + }; + if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) { + perror("setsockopt"); + return 1; + } + if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0) { + perror("setsockopt"); + return 1; + } + + char addr_str[100]; - rc = connect(fd, (struct sockaddr*)&dst_addr, sizeof(dst_addr)); - if (rc < 0) { - perror("connect"); - return 1; + struct sockaddr_in dst_addr; + memset(&dst_addr, 0, sizeof(dst_addr)); + + dst_addr.sin_family = AF_INET; + dst_addr.sin_port = htons(port_arg); + if (inet_pton(AF_INET, addr_arg, &dst_addr.sin_addr) < 0) { + perror("inet_pton"); + return 1; + } + + if (verbose) + fprintf(stderr, "connecting to %s:%d\n", inet_ntop(dst_addr.sin_family, &dst_addr.sin_addr, addr_str, sizeof(addr_str) - 1), ntohs(dst_addr.sin_port)); + if (connect(fd, (struct sockaddr*)&dst_addr, sizeof(dst_addr)) < 0) { + perror("connect"); + return 1; + } + if (verbose) + fprintf(stderr, "connected!\n"); } + bool stdin_closed = false; + bool fd_closed = false; + fd_set readfds, writefds, exceptfds; - for (;;) { + while (!stdin_closed || !fd_closed) { FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); int highest_fd = 0; - FD_SET(STDIN_FILENO, &readfds); - FD_SET(STDIN_FILENO, &exceptfds); - highest_fd = max(highest_fd, STDIN_FILENO); - FD_SET(fd, &readfds); - FD_SET(fd, &exceptfds); - highest_fd = max(highest_fd, fd); + if (!stdin_closed) { + FD_SET(STDIN_FILENO, &readfds); + FD_SET(STDIN_FILENO, &exceptfds); + highest_fd = max(highest_fd, STDIN_FILENO); + } + if (!fd_closed) { + FD_SET(fd, &readfds); + FD_SET(fd, &exceptfds); + highest_fd = max(highest_fd, fd); + } int ready = select(highest_fd + 1, &readfds, &writefds, &exceptfds, NULL); if (ready == -1) { @@ -83,7 +191,7 @@ int main(int argc, char** argv) return 1; } - if (FD_ISSET(STDIN_FILENO, &readfds)) { + if (!stdin_closed && FD_ISSET(STDIN_FILENO, &readfds)) { char buf[1024]; int nread = read(STDIN_FILENO, buf, sizeof(buf)); if (nread < 0) { @@ -93,16 +201,20 @@ int main(int argc, char** argv) // stdin closed if (nread == 0) { - return 0; - } - - if (write(fd, buf, nread) < 0) { + stdin_closed = true; + if (verbose) + fprintf(stderr, "stdin closed\n"); + if (should_close) { + close(fd); + fd_closed = true; + } + } else if (write(fd, buf, nread) < 0) { perror("write(fd)"); return 1; } } - if (FD_ISSET(fd, &readfds)) { + if (!fd_closed && FD_ISSET(fd, &readfds)) { char buf[1024]; int nread = read(fd, buf, sizeof(buf)); if (nread < 0) { @@ -112,10 +224,12 @@ int main(int argc, char** argv) // remote end closed if (nread == 0) { - return 0; - } - - if (write(STDOUT_FILENO, buf, nread) < 0) { + close(STDIN_FILENO); + stdin_closed = true; + fd_closed = true; + if (verbose) + fprintf(stderr, "remote closed\n"); + } else if (write(STDOUT_FILENO, buf, nread) < 0) { perror("write(STDOUT_FILENO)"); return 1; } -- cgit v1.2.3