netcat.c (206689) | netcat.c (214047) |
---|---|
1/* $OpenBSD: netcat.c,v 1.95 2010/02/27 00:58:56 nicm Exp $ */ | 1/* $OpenBSD: netcat.c,v 1.98 2010/07/03 04:44:51 guenther Exp $ */ |
2/* 3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright --- 10 unchanged lines hidden (view full) --- 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * | 2/* 3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright --- 10 unchanged lines hidden (view full) --- 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * |
28 * $FreeBSD: head/contrib/netcat/netcat.c 206689 2010-04-15 23:21:24Z delphij $ | 28 * $FreeBSD: head/contrib/netcat/netcat.c 214047 2010-10-19 00:01:53Z delphij $ |
29 */ 30 31/* 32 * Re-written nc(1) for OpenBSD. Original implementation by 33 * *Hobbit* <hobbit@avian.org>. 34 */ 35 36#include <sys/limits.h> --- 51 unchanged lines hidden (view full) --- 88int vflag; /* Verbosity */ 89int xflag; /* Socks proxy */ 90int zflag; /* Port Scan Flag */ 91int Dflag; /* sodebug */ 92int Iflag; /* TCP receive buffer size */ 93int Oflag; /* TCP send buffer size */ 94int Sflag; /* TCP MD5 signature option */ 95int Tflag = -1; /* IP Type of Service */ | 29 */ 30 31/* 32 * Re-written nc(1) for OpenBSD. Original implementation by 33 * *Hobbit* <hobbit@avian.org>. 34 */ 35 36#include <sys/limits.h> --- 51 unchanged lines hidden (view full) --- 88int vflag; /* Verbosity */ 89int xflag; /* Socks proxy */ 90int zflag; /* Port Scan Flag */ 91int Dflag; /* sodebug */ 92int Iflag; /* TCP receive buffer size */ 93int Oflag; /* TCP send buffer size */ 94int Sflag; /* TCP MD5 signature option */ 95int Tflag = -1; /* IP Type of Service */ |
96u_int rdomain; | 96u_int rtableid; |
97 98int timeout = -1; 99int family = AF_UNSPEC; 100char *portlist[PORT_MAX+1]; 101 102void atelnet(int, unsigned char *, unsigned int); 103void build_ports(char *); 104void help(void); --- 29 unchanged lines hidden (view full) --- 134 char *proxy; 135 const char *errstr, *proxyhost = "", *proxyport = NULL; 136 struct addrinfo proxyhints; 137 struct option longopts[] = { 138 { "no-tcpopt", no_argument, &FreeBSD_Oflag, 1 }, 139 { NULL, 0, NULL, 0 } 140 }; 141 | 97 98int timeout = -1; 99int family = AF_UNSPEC; 100char *portlist[PORT_MAX+1]; 101 102void atelnet(int, unsigned char *, unsigned int); 103void build_ports(char *); 104void help(void); --- 29 unchanged lines hidden (view full) --- 134 char *proxy; 135 const char *errstr, *proxyhost = "", *proxyport = NULL; 136 struct addrinfo proxyhints; 137 struct option longopts[] = { 138 { "no-tcpopt", no_argument, &FreeBSD_Oflag, 1 }, 139 { NULL, 0, NULL, 0 } 140 }; 141 |
142 rdomain = 0; | |
143 ret = 1; 144 ipsec_count = 0; 145 s = 0; 146 socksv = 5; 147 host = NULL; 148 uport = NULL; 149 sv = NULL; 150 --- 79 unchanged lines hidden (view full) --- 230 tflag = 1; 231 break; 232 case 'u': 233 uflag = 1; 234 break; 235 case 'V': 236 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) 237 errx(1, "Multiple FIBS not supported"); | 142 ret = 1; 143 ipsec_count = 0; 144 s = 0; 145 socksv = 5; 146 host = NULL; 147 uport = NULL; 148 sv = NULL; 149 --- 79 unchanged lines hidden (view full) --- 229 tflag = 1; 230 break; 231 case 'u': 232 uflag = 1; 233 break; 234 case 'V': 235 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) 236 errx(1, "Multiple FIBS not supported"); |
238 rdomain = (unsigned int)strtonum(optarg, 0, | 237 rtableid = (unsigned int)strtonum(optarg, 0, |
239 numfibs - 1, &errstr); 240 if (errstr) | 238 numfibs - 1, &errstr); 239 if (errstr) |
241 errx(1, "FIB %s: %s", errstr, optarg); | 240 errx(1, "rtable %s: %s", errstr, optarg); |
242 break; 243 case 'v': 244 vflag = 1; 245 break; 246 case 'w': 247 timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr); 248 if (errstr) 249 errx(1, "timeout %s: %s", errstr, optarg); --- 116 unchanged lines hidden (view full) --- 366 err(1, NULL); 367 /* 368 * For UDP, we will use recvfrom() initially 369 * to wait for a caller, then use the regular 370 * functions to talk to the caller. 371 */ 372 if (uflag) { 373 int rv, plen; | 241 break; 242 case 'v': 243 vflag = 1; 244 break; 245 case 'w': 246 timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr); 247 if (errstr) 248 errx(1, "timeout %s: %s", errstr, optarg); --- 116 unchanged lines hidden (view full) --- 365 err(1, NULL); 366 /* 367 * For UDP, we will use recvfrom() initially 368 * to wait for a caller, then use the regular 369 * functions to talk to the caller. 370 */ 371 if (uflag) { 372 int rv, plen; |
374 char buf[8192]; | 373 char buf[16384]; |
375 struct sockaddr_storage z; 376 377 len = sizeof(z); | 374 struct sockaddr_storage z; 375 376 len = sizeof(z); |
378 plen = jflag ? 8192 : 1024; | 377 plen = jflag ? 16384 : 2048; |
379 rv = recvfrom(s, buf, plen, MSG_PEEK, 380 (struct sockaddr *)&z, &len); 381 if (rv < 0) 382 err(1, "recvfrom"); 383 384 rv = connect(s, (struct sockaddr *)&z, len); 385 if (rv < 0) 386 err(1, "connect"); --- 169 unchanged lines hidden (view full) --- 556 continue; 557#ifdef IPSEC 558 if (ipsec_policy[0] != NULL) 559 add_ipsec_policy(s, ipsec_policy[0]); 560 if (ipsec_policy[1] != NULL) 561 add_ipsec_policy(s, ipsec_policy[1]); 562#endif 563 | 378 rv = recvfrom(s, buf, plen, MSG_PEEK, 379 (struct sockaddr *)&z, &len); 380 if (rv < 0) 381 err(1, "recvfrom"); 382 383 rv = connect(s, (struct sockaddr *)&z, len); 384 if (rv < 0) 385 err(1, "connect"); --- 169 unchanged lines hidden (view full) --- 555 continue; 556#ifdef IPSEC 557 if (ipsec_policy[0] != NULL) 558 add_ipsec_policy(s, ipsec_policy[0]); 559 if (ipsec_policy[1] != NULL) 560 add_ipsec_policy(s, ipsec_policy[1]); 561#endif 562 |
564 if (rdomain) { 565 if (setfib(rdomain) == -1) | 563 if (rtableid) { 564 if (setfib(rtableid) == -1) |
566 err(1, "setfib"); 567 } 568 569 /* Bind to a local port or source address if specified. */ 570 if (sflag || pflag) { 571 struct addrinfo ahints, *ares; 572 573 /* try IP_BINDANY, but don't insist */ --- 55 unchanged lines hidden (view full) --- 629 errx(1, "getaddrinfo: %s", gai_strerror(error)); 630 631 res0 = res; 632 do { 633 if ((s = socket(res0->ai_family, res0->ai_socktype, 634 res0->ai_protocol)) < 0) 635 continue; 636 | 565 err(1, "setfib"); 566 } 567 568 /* Bind to a local port or source address if specified. */ 569 if (sflag || pflag) { 570 struct addrinfo ahints, *ares; 571 572 /* try IP_BINDANY, but don't insist */ --- 55 unchanged lines hidden (view full) --- 628 errx(1, "getaddrinfo: %s", gai_strerror(error)); 629 630 res0 = res; 631 do { 632 if ((s = socket(res0->ai_family, res0->ai_socktype, 633 res0->ai_protocol)) < 0) 634 continue; 635 |
637 if (rdomain) { 638 if (setfib(rdomain) == -1) | 636 if (rtableid) { 637 if (setfib(rtableid) == -1) |
639 err(1, "setfib"); 640 } 641 642 ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); 643 if (ret == -1) 644 err(1, NULL); 645#ifdef IPSEC 646 if (ipsec_policy[0] != NULL) --- 28 unchanged lines hidden (view full) --- 675/* 676 * readwrite() 677 * Loop that polls on the network file descriptor and stdin. 678 */ 679void 680readwrite(int nfd) 681{ 682 struct pollfd pfd[2]; | 638 err(1, "setfib"); 639 } 640 641 ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); 642 if (ret == -1) 643 err(1, NULL); 644#ifdef IPSEC 645 if (ipsec_policy[0] != NULL) --- 28 unchanged lines hidden (view full) --- 674/* 675 * readwrite() 676 * Loop that polls on the network file descriptor and stdin. 677 */ 678void 679readwrite(int nfd) 680{ 681 struct pollfd pfd[2]; |
683 unsigned char buf[8192]; | 682 unsigned char buf[16384]; |
684 int n, wfd = fileno(stdin); 685 int lfd = fileno(stdout); 686 int plen; 687 | 683 int n, wfd = fileno(stdin); 684 int lfd = fileno(stdout); 685 int plen; 686 |
688 plen = jflag ? 8192 : 1024; | 687 plen = jflag ? 16384 : 2048; |
689 690 /* Setup Network FD */ 691 pfd[0].fd = nfd; 692 pfd[0].events = POLLIN; 693 694 /* Set up STDIN FD. */ 695 pfd[1].fd = wfd; 696 pfd[1].events = POLLIN; --- 125 unchanged lines hidden (view full) --- 822 portlist[x] = portlist[y]; 823 portlist[y] = c; 824 } 825 } 826 } else { 827 hi = strtonum(p, 1, PORT_MAX, &errstr); 828 if (errstr) 829 errx(1, "port number %s: %s", errstr, p); | 688 689 /* Setup Network FD */ 690 pfd[0].fd = nfd; 691 pfd[0].events = POLLIN; 692 693 /* Set up STDIN FD. */ 694 pfd[1].fd = wfd; 695 pfd[1].events = POLLIN; --- 125 unchanged lines hidden (view full) --- 821 portlist[x] = portlist[y]; 822 portlist[y] = c; 823 } 824 } 825 } else { 826 hi = strtonum(p, 1, PORT_MAX, &errstr); 827 if (errstr) 828 errx(1, "port number %s: %s", errstr, p); |
830 portlist[0] = calloc(1, PORT_MAX_LEN); | 829 portlist[0] = strdup(p); |
831 if (portlist[0] == NULL) 832 err(1, NULL); | 830 if (portlist[0] == NULL) 831 err(1, NULL); |
833 portlist[0] = p; | |
834 } 835} 836 837/* 838 * udptest() 839 * Do a few writes to see if the UDP port is there. 840 * XXX - Better way of doing this? Doesn't work for IPv6. 841 * Also fails after around 100 ports checked. --- 100 unchanged lines hidden (view full) --- 942 \t-p port\t Specify local port for remote connects\n\ 943 \t-r Randomize remote ports\n\ 944 \t-S Enable the TCP MD5 signature option\n\ 945 \t-s addr\t Local source address\n\ 946 \t-T ToS\t Set IP Type of Service\n\ 947 \t-t Answer TELNET negotiation\n\ 948 \t-U Use UNIX domain socket\n\ 949 \t-u UDP mode\n\ | 832 } 833} 834 835/* 836 * udptest() 837 * Do a few writes to see if the UDP port is there. 838 * XXX - Better way of doing this? Doesn't work for IPv6. 839 * Also fails after around 100 ports checked. --- 100 unchanged lines hidden (view full) --- 940 \t-p port\t Specify local port for remote connects\n\ 941 \t-r Randomize remote ports\n\ 942 \t-S Enable the TCP MD5 signature option\n\ 943 \t-s addr\t Local source address\n\ 944 \t-T ToS\t Set IP Type of Service\n\ 945 \t-t Answer TELNET negotiation\n\ 946 \t-U Use UNIX domain socket\n\ 947 \t-u UDP mode\n\ |
950 \t-V fib Specify alternate routing table (FIB)\n\ | 948 \t-V rtable Specify alternate routing table\n\ |
951 \t-v Verbose\n\ 952 \t-w secs\t Timeout for connects and final net reads\n\ 953 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\ 954 \t-x addr[:port]\tSpecify proxy address and port\n\ 955 \t-z Zero-I/O mode [used for scanning]\n\ 956 Port numbers can be individual or ranges: lo-hi [inclusive]\n"); 957#ifdef IPSEC 958 fprintf(stderr, "See ipsec_set_policy(3) for -e argument format\n"); --- 28 unchanged lines hidden (view full) --- 987{ 988 fprintf(stderr, 989#ifdef IPSEC 990 "usage: nc [-46DdEhklnrStUuvz] [-e policy] [-I length] [-i interval] [-O length]\n" 991#else 992 "usage: nc [-46DdhklnrStUuvz] [-I length] [-i interval] [-O length]\n" 993#endif 994 "\t [-P proxy_username] [-p source_port] [-s source_ip_address] [-T ToS]\n" | 949 \t-v Verbose\n\ 950 \t-w secs\t Timeout for connects and final net reads\n\ 951 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\ 952 \t-x addr[:port]\tSpecify proxy address and port\n\ 953 \t-z Zero-I/O mode [used for scanning]\n\ 954 Port numbers can be individual or ranges: lo-hi [inclusive]\n"); 955#ifdef IPSEC 956 fprintf(stderr, "See ipsec_set_policy(3) for -e argument format\n"); --- 28 unchanged lines hidden (view full) --- 985{ 986 fprintf(stderr, 987#ifdef IPSEC 988 "usage: nc [-46DdEhklnrStUuvz] [-e policy] [-I length] [-i interval] [-O length]\n" 989#else 990 "usage: nc [-46DdhklnrStUuvz] [-I length] [-i interval] [-O length]\n" 991#endif 992 "\t [-P proxy_username] [-p source_port] [-s source_ip_address] [-T ToS]\n" |
995 "\t [-V fib] [-w timeout] [-X proxy_protocol]\n" | 993 "\t [-V rtable] [-w timeout] [-X proxy_protocol]\n" |
996 "\t [-x proxy_address[:port]] [hostname] [port]\n"); 997 if (ret) 998 exit(1); 999} | 994 "\t [-x proxy_address[:port]] [hostname] [port]\n"); 995 if (ret) 996 exit(1); 997} |