source:
npl/overig/netcat_openbsd/patches/0001-port-to-linux-with-libsd.patch
@
7c410f9
Last change on this file since 7c410f9 was c5c522c, checked in by , 8 years ago | |
---|---|
|
|
File size: 13.6 KB |
-
Makefile
From: Aron Xu <aron@debian.org> Date: Mon, 13 Feb 2012 15:59:31 +0800 Subject: port to linux with libsd --- Makefile | 17 ++++++++-- nc.1 | 4 +-- netcat.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------- socks.c | 46 +++++++++++++-------------- 4 files changed, 130 insertions(+), 42 deletions(-) diff --git a/Makefile b/Makefile index 150f829..96a6587 100644
a b 1 # 1 # $OpenBSD: Makefile,v 1.6 2001/09/02 18:45:41 jakob Exp $ 2 2 3 3 PROG= nc 4 4 SRCS= netcat.c atomicio.c socks.c 5 5 6 .include <bsd.prog.mk> 6 LIBS= `pkg-config --libs libbsd` -lresolv 7 OBJS= $(SRCS:.c=.o) 8 CFLAGS= -g -O2 9 LDFLAGS= -Wl,--no-add-needed 10 11 all: nc 12 nc: $(OBJS) 13 $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o nc 14 15 $(OBJS): %.o: %.c 16 $(CC) $(CFLAGS) -c $< -o $@ 17 18 clean: 19 rm -f $(OBJS) nc -
nc.1
diff --git a/nc.1 b/nc.1 index 75d1437..b7014a2 100644
a b Proxy authentication is only supported for HTTP CONNECT proxies at present. 146 146 Specifies the source port 147 147 .Nm 148 148 should use, subject to privilege restrictions and availability. 149 It is an error to use this option in conjunction with the150 .Fl l151 option.152 149 .It Fl r 153 150 Specifies that source and/or destination ports should be chosen randomly 154 151 instead of sequentially within a range or in the order that the system … … Change IPv4 TOS value. 170 167 may be one of 171 168 .Ar critical , 172 169 .Ar inetcontrol , 170 .Ar lowcost , 173 171 .Ar lowdelay , 174 172 .Ar netcontrol , 175 173 .Ar throughput , -
netcat.c
diff --git a/netcat.c b/netcat.c index cc4683a..9b2def2 100644
a b 42 42 #include <netinet/ip.h> 43 43 #include <arpa/telnet.h> 44 44 45 #ifndef IPTOS_LOWDELAY 46 # define IPTOS_LOWDELAY 0x10 47 # define IPTOS_THROUGHPUT 0x08 48 # define IPTOS_RELIABILITY 0x04 49 # define IPTOS_LOWCOST 0x02 50 # define IPTOS_MINCOST IPTOS_LOWCOST 51 #endif /* IPTOS_LOWDELAY */ 52 53 # ifndef IPTOS_DSCP_AF11 54 # define IPTOS_DSCP_AF11 0x28 55 # define IPTOS_DSCP_AF12 0x30 56 # define IPTOS_DSCP_AF13 0x38 57 # define IPTOS_DSCP_AF21 0x48 58 # define IPTOS_DSCP_AF22 0x50 59 # define IPTOS_DSCP_AF23 0x58 60 # define IPTOS_DSCP_AF31 0x68 61 # define IPTOS_DSCP_AF32 0x70 62 # define IPTOS_DSCP_AF33 0x78 63 # define IPTOS_DSCP_AF41 0x88 64 # define IPTOS_DSCP_AF42 0x90 65 # define IPTOS_DSCP_AF43 0x98 66 # define IPTOS_DSCP_EF 0xb8 67 #endif /* IPTOS_DSCP_AF11 */ 68 69 #ifndef IPTOS_DSCP_CS0 70 # define IPTOS_DSCP_CS0 0x00 71 # define IPTOS_DSCP_CS1 0x20 72 # define IPTOS_DSCP_CS2 0x40 73 # define IPTOS_DSCP_CS3 0x60 74 # define IPTOS_DSCP_CS4 0x80 75 # define IPTOS_DSCP_CS5 0xa0 76 # define IPTOS_DSCP_CS6 0xc0 77 # define IPTOS_DSCP_CS7 0xe0 78 #endif /* IPTOS_DSCP_CS0 */ 79 80 #ifndef IPTOS_DSCP_EF 81 # define IPTOS_DSCP_EF 0xb8 82 #endif /* IPTOS_DSCP_EF */ 83 84 45 85 #include <err.h> 46 86 #include <errno.h> 47 87 #include <netdb.h> … … 53 93 #include <unistd.h> 54 94 #include <fcntl.h> 55 95 #include <limits.h> 96 #include <bsd/stdlib.h> 97 #include <bsd/string.h> 56 98 #include "atomicio.h" 57 99 58 100 #ifndef SUN_LEN … … main(int argc, char *argv[]) 118 160 struct servent *sv; 119 161 socklen_t len; 120 162 struct sockaddr_storage cliaddr; 121 char *proxy ;163 char *proxy = NULL; 122 164 const char *errstr, *proxyhost = "", *proxyport = NULL; 123 165 struct addrinfo proxyhints; 124 166 char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE]; … … main(int argc, char *argv[]) 164 206 errx(1, "interval %s: %s", errstr, optarg); 165 207 break; 166 208 case 'j': 209 # if defined(SO_JUMBO) 167 210 jflag = 1; 211 # else 212 errx(1, "no jumbo frame support available"); 213 # endif 168 214 break; 169 215 case 'k': 170 216 kflag = 1; … … main(int argc, char *argv[]) 194 240 uflag = 1; 195 241 break; 196 242 case 'V': 243 # if defined(RT_TABLEID_MAX) 197 244 rtableid = (unsigned int)strtonum(optarg, 0, 198 245 RT_TABLEID_MAX, &errstr); 199 246 if (errstr) 200 247 errx(1, "rtable %s: %s", errstr, optarg); 248 # else 249 errx(1, "no alternate routing table support available"); 250 # endif 201 251 break; 202 252 case 'v': 203 253 vflag = 1; … … main(int argc, char *argv[]) 232 282 errstr, optarg); 233 283 break; 234 284 case 'S': 285 # if defined(TCP_MD5SIG) 235 286 Sflag = 1; 287 # else 288 errx(1, "no TCP MD5 signature support available"); 289 # endif 236 290 break; 237 291 case 'T': 238 292 errstr = NULL; … … main(int argc, char *argv[]) 259 313 if (argv[0] && !argv[1] && family == AF_UNIX) { 260 314 host = argv[0]; 261 315 uport = NULL; 316 } else if (!argv[0] && lflag) { 317 if (sflag) 318 errx(1, "cannot use -s and -l"); 319 if (zflag) 320 errx(1, "cannot use -z and -l"); 321 if (pflag) 322 uport=pflag; 323 } else if (!lflag && kflag) { 324 errx(1, "cannot use -k without -l"); 262 325 } else if (argv[0] && !argv[1]) { 263 326 if (!lflag) 264 327 usage(1); … … main(int argc, char *argv[]) 270 333 } else 271 334 usage(1); 272 335 273 if (lflag && sflag) 274 errx(1, "cannot use -s and -l"); 275 if (lflag && pflag) 276 errx(1, "cannot use -p and -l"); 277 if (lflag && zflag) 278 errx(1, "cannot use -z and -l"); 279 if (!lflag && kflag) 280 errx(1, "must use -l with -k"); 336 281 337 282 338 /* Get name of temporary socket for unix datagram client */ 283 339 if ((family == AF_UNIX) && uflag && !lflag) { … … main(int argc, char *argv[]) 286 342 } else { 287 343 strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX", 288 344 UNIX_DG_TMP_SOCKET_SIZE); 289 if (mk temp(unix_dg_tmp_socket_buf) == NULL)290 err(1, "mk temp");345 if (mkstemp(unix_dg_tmp_socket_buf) == -1) 346 err(1, "mkstemp"); 291 347 unix_dg_tmp_socket = unix_dg_tmp_socket_buf; 292 348 } 293 349 } … … remote_connect(const char *host, const char *port, struct addrinfo hints) 563 619 res0->ai_protocol)) < 0) 564 620 continue; 565 621 622 # if defined(RT_TABLEID_MAX) 566 623 if (rtableid) { 567 624 if (setsockopt(s, SOL_SOCKET, SO_RTABLE, &rtableid, 568 625 sizeof(rtableid)) == -1) 569 626 err(1, "setsockopt SO_RTABLE"); 570 627 } 628 # endif 571 629 572 630 /* Bind to a local port or source address if specified. */ 573 631 if (sflag || pflag) { 574 632 struct addrinfo ahints, *ares; 575 633 634 # if defined (SO_BINDANY) 576 635 /* try SO_BINDANY, but don't insist */ 577 636 setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on)); 637 # endif 578 638 memset(&ahints, 0, sizeof(struct addrinfo)); 579 639 ahints.ai_family = res0->ai_family; 580 640 ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM; … … local_listen(char *host, char *port, struct addrinfo hints) 674 734 res0->ai_protocol)) < 0) 675 735 continue; 676 736 737 # if defined(RT_TABLEID_MAX) 677 738 if (rtableid) { 678 739 if (setsockopt(s, IPPROTO_IP, SO_RTABLE, &rtableid, 679 740 sizeof(rtableid)) == -1) 680 741 err(1, "setsockopt SO_RTABLE"); 681 742 } 743 # endif 744 745 ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x)); 746 if (ret == -1) 747 err(1, NULL); 682 748 749 # if defined(SO_REUSEPORT) 683 750 ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); 684 751 if (ret == -1) 685 752 err(1, NULL); 753 # endif 686 754 687 755 set_common_sockopts(s); 688 756 … … set_common_sockopts(int s) 886 954 { 887 955 int x = 1; 888 956 957 # if defined(TCP_MD5SIG) 889 958 if (Sflag) { 890 959 if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG, 891 960 &x, sizeof(x)) == -1) 892 961 err(1, NULL); 893 962 } 963 # endif 894 964 if (Dflag) { 895 965 if (setsockopt(s, SOL_SOCKET, SO_DEBUG, 896 966 &x, sizeof(x)) == -1) 897 967 err(1, NULL); 898 968 } 969 # if defined(SO_JUMBO) 899 970 if (jflag) { 900 971 if (setsockopt(s, SOL_SOCKET, SO_JUMBO, 901 972 &x, sizeof(x)) == -1) 902 973 err(1, NULL); 903 974 } 975 # endif 904 976 if (Tflag != -1) { 905 977 if (setsockopt(s, IPPROTO_IP, IP_TOS, 906 978 &Tflag, sizeof(Tflag)) == -1) … … map_tos(char *s, int *val) 949 1021 { "cs7", IPTOS_DSCP_CS7 }, 950 1022 { "ef", IPTOS_DSCP_EF }, 951 1023 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL }, 1024 { "lowcost", IPTOS_LOWCOST }, 952 1025 { "lowdelay", IPTOS_LOWDELAY }, 953 1026 { "netcontrol", IPTOS_PREC_NETCONTROL }, 954 1027 { "reliability", IPTOS_RELIABILITY }, … … map_tos(char *s, int *val) 969 1042 void 970 1043 help(void) 971 1044 { 1045 # if defined(DEBIAN_VERSION) 1046 fprintf(stderr, "OpenBSD netcat (Debian patchlevel " DEBIAN_VERSION ")\n"); 1047 # endif 972 1048 usage(0); 973 1049 fprintf(stderr, "\tCommand Summary:\n\ 974 1050 \t-4 Use IPv4\n\ … … help(void) 978 1054 \t-h This help text\n\ 979 1055 \t-I length TCP receive buffer length\n\ 980 1056 \t-i secs\t Delay interval for lines sent, ports scanned\n\ 1057 \t-j Use jumbo frame\n\ 981 1058 \t-k Keep inbound sockets open for multiple connects\n\ 982 1059 \t-l Listen mode, for inbound connects\n\ 983 1060 \t-n Suppress name/port resolutions\n\ … … help(void) 998 1075 \t-x addr[:port]\tSpecify proxy address and port\n\ 999 1076 \t-z Zero-I/O mode [used for scanning]\n\ 1000 1077 Port numbers can be individual or ranges: lo-hi [inclusive]\n"); 1001 exit( 1);1078 exit(0); 1002 1079 } 1003 1080 1004 1081 void 1005 1082 usage(int ret) 1006 1083 { 1007 1084 fprintf(stderr, 1008 "usage: nc [-46Ddh klnrStUuvz] [-I length] [-i interval] [-O length]\n"1009 "\t [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n"1085 "usage: nc [-46DdhjklnrStUuvz] [-I length] [-i interval] [-O length]\n" 1086 "\t [-P proxy_username] [-p source_port] [-s source] [-T toskeyword]\n" 1010 1087 "\t [-V rtable] [-w timeout] [-X proxy_protocol]\n" 1011 1088 "\t [-x proxy_address[:port]] [destination] [port]\n"); 1012 1089 if (ret) -
socks.c
diff --git a/socks.c b/socks.c index 71108d5..befd0a9 100644
a b 38 38 #include <string.h> 39 39 #include <unistd.h> 40 40 #include <resolv.h> 41 #include < readpassphrase.h>41 #include <bsd/readpassphrase.h> 42 42 #include "atomicio.h" 43 43 44 44 #define SOCKS_PORT "1080" … … socks_connect(const char *host, const char *port, 167 167 buf[2] = SOCKS_NOAUTH; 168 168 cnt = atomicio(vwrite, proxyfd, buf, 3); 169 169 if (cnt != 3) 170 err(1, "write failed (%zu/3)", cnt);170 err(1, "write failed (%zu/3)", (size_t)cnt); 171 171 172 172 cnt = atomicio(read, proxyfd, buf, 2); 173 173 if (cnt != 2) 174 err(1, "read failed (%zu/3)", cnt);174 err(1, "read failed (%zu/3)", (size_t)cnt); 175 175 176 176 if (buf[1] == SOCKS_NOMETHOD) 177 177 errx(1, "authentication method negotiation failed"); … … socks_connect(const char *host, const char *port, 220 220 221 221 cnt = atomicio(vwrite, proxyfd, buf, wlen); 222 222 if (cnt != wlen) 223 err(1, "write failed (%zu/%zu)", cnt,wlen);223 err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen); 224 224 225 225 cnt = atomicio(read, proxyfd, buf, 4); 226 226 if (cnt != 4) 227 err(1, "read failed (%zu/4)", cnt);227 err(1, "read failed (%zu/4)", (size_t)cnt); 228 228 if (buf[1] != 0) 229 229 errx(1, "connection failed, SOCKS error %d", buf[1]); 230 230 switch (buf[3]) { 231 231 case SOCKS_IPV4: 232 232 cnt = atomicio(read, proxyfd, buf + 4, 6); 233 233 if (cnt != 6) 234 err(1, "read failed (% d/6)",cnt);234 err(1, "read failed (%lu/6)", (unsigned long)cnt); 235 235 break; 236 236 case SOCKS_IPV6: 237 237 cnt = atomicio(read, proxyfd, buf + 4, 18); 238 238 if (cnt != 18) 239 err(1, "read failed (% d/18)",cnt);239 err(1, "read failed (%lu/18)", (unsigned long)cnt); 240 240 break; 241 241 default: 242 242 errx(1, "connection failed, unsupported address type"); … … socks_connect(const char *host, const char *port, 256 256 257 257 cnt = atomicio(vwrite, proxyfd, buf, wlen); 258 258 if (cnt != wlen) 259 err(1, "write failed (%zu/%zu)", cnt,wlen);259 err(1, "write failed (%zu/%zu)", (size_t)cnt, (size_t)wlen); 260 260 261 261 cnt = atomicio(read, proxyfd, buf, 8); 262 262 if (cnt != 8) 263 err(1, "read failed (%zu/8)", cnt);263 err(1, "read failed (%zu/8)", (size_t)cnt); 264 264 if (buf[1] != 90) 265 265 errx(1, "connection failed, SOCKS error %d", buf[1]); 266 266 } else if (socksv == -1) { … … socks_connect(const char *host, const char *port, 272 272 273 273 /* Try to be sane about numeric IPv6 addresses */ 274 274 if (strchr(host, ':') != NULL) { 275 r = snprintf( buf, sizeof(buf),275 r = snprintf((char*)buf, sizeof(buf), 276 276 "CONNECT [%s]:%d HTTP/1.0\r\n", 277 277 host, ntohs(serverport)); 278 278 } else { 279 r = snprintf( buf, sizeof(buf),279 r = snprintf((char*)buf, sizeof(buf), 280 280 "CONNECT %s:%d HTTP/1.0\r\n", 281 281 host, ntohs(serverport)); 282 282 } 283 283 if (r == -1 || (size_t)r >= sizeof(buf)) 284 284 errx(1, "hostname too long"); 285 r = strlen( buf);285 r = strlen((char*)buf); 286 286 287 287 cnt = atomicio(vwrite, proxyfd, buf, r); 288 288 if (cnt != r) 289 err(1, "write failed (%zu/%d)", cnt,r);289 err(1, "write failed (%zu/%d)", (size_t)cnt, (int)r); 290 290 291 291 if (authretry > 1) { 292 292 char resp[1024]; 293 293 294 294 proxypass = getproxypass(proxyuser, proxyhost); 295 r = snprintf( buf, sizeof(buf), "%s:%s",295 r = snprintf((char*)buf, sizeof(buf), "%s:%s", 296 296 proxyuser, proxypass); 297 297 if (r == -1 || (size_t)r >= sizeof(buf) || 298 b64_ntop(buf, strlen( buf), resp,298 b64_ntop(buf, strlen((char*)buf), resp, 299 299 sizeof(resp)) == -1) 300 300 errx(1, "Proxy username/password too long"); 301 r = snprintf( buf, sizeof(buf), "Proxy-Authorization: "301 r = snprintf((char*)buf, sizeof((char*)buf), "Proxy-Authorization: " 302 302 "Basic %s\r\n", resp); 303 303 if (r == -1 || (size_t)r >= sizeof(buf)) 304 304 errx(1, "Proxy auth response too long"); 305 r = strlen( buf);305 r = strlen((char*)buf); 306 306 if ((cnt = atomicio(vwrite, proxyfd, buf, r)) != r) 307 err(1, "write failed (%zu/%d)", cnt, r);307 err(1, "write failed (%zu/%d)", (size_t)cnt, r); 308 308 } 309 309 310 310 /* Terminate headers */ … … socks_connect(const char *host, const char *port, 312 312 err(1, "write failed (2/%d)", r); 313 313 314 314 /* Read status reply */ 315 proxy_read_line(proxyfd, buf, sizeof(buf));315 proxy_read_line(proxyfd, (char*)buf, sizeof(buf)); 316 316 if (proxyuser != NULL && 317 strncmp( buf, "HTTP/1.0 407 ", 12) == 0) {317 strncmp((char*)buf, "HTTP/1.0 407 ", 12) == 0) { 318 318 if (authretry > 1) { 319 319 fprintf(stderr, "Proxy authentication " 320 320 "failed\n"); 321 321 } 322 322 close(proxyfd); 323 323 goto again; 324 } else if (strncmp( buf, "HTTP/1.0 200 ", 12) != 0 &&325 strncmp( buf, "HTTP/1.1 200 ", 12) != 0)324 } else if (strncmp((char*)buf, "HTTP/1.0 200 ", 12) != 0 && 325 strncmp((char*)buf, "HTTP/1.1 200 ", 12) != 0) 326 326 errx(1, "Proxy error: \"%s\"", buf); 327 327 328 328 /* Headers continue until we hit an empty line */ 329 329 for (r = 0; r < HTTP_MAXHDRS; r++) { 330 proxy_read_line(proxyfd, buf, sizeof(buf));330 proxy_read_line(proxyfd, (char*)buf, sizeof(buf)); 331 331 if (*buf == '\0') 332 332 break; 333 333 }
Note: See TracBrowser
for help on using the repository browser.