version 1.36, 2001/09/02 19:07:17 |
version 1.37, 2001/09/02 19:11:46 |
|
|
void usage __P((int)); |
void usage __P((int)); |
|
|
int |
int |
main(argc, argv) |
main(int argc, char *argv[]) |
int argc; |
|
char *argv[]; |
|
{ |
{ |
int ch, s, ret; |
int ch, s, ret; |
char *host, *uport, *endp; |
char *host, *uport, *endp; |
|
|
|
|
len = sizeof(z); |
len = sizeof(z); |
rv = recvfrom(s, buf, sizeof(buf), MSG_PEEK, |
rv = recvfrom(s, buf, sizeof(buf), MSG_PEEK, |
(struct sockaddr *)&z, &len); |
(struct sockaddr *)&z, &len); |
if (rv < 0) |
if (rv < 0) |
errx(1, "%s", strerror(errno)); |
errx(1, "%s", strerror(errno)); |
|
|
rv = connect(s, (struct sockaddr *)&z, |
rv = connect(s, (struct sockaddr *)&z, len); |
len); |
|
if (rv < 0) |
if (rv < 0) |
errx(1, "%s", strerror(errno)); |
errx(1, "%s", strerror(errno)); |
|
|
connfd = s; |
connfd = s; |
} else { |
} else { |
connfd = accept(s, (struct sockaddr *)&cliaddr, |
connfd = accept(s, (struct sockaddr *)&cliaddr, |
&len); |
&len); |
} |
} |
|
|
readwrite(connfd); |
readwrite(connfd); |
|
|
sv = NULL; |
sv = NULL; |
else { |
else { |
sv = getservbyport( |
sv = getservbyport( |
ntohs(atoi(portlist[i])), |
ntohs(atoi(portlist[i])), |
uflag ? "udp" : "tcp"); |
uflag ? "udp" : "tcp"); |
} |
} |
|
|
printf("Connection to %s %s port [%s/%s] succeeded!\n", |
printf("Connection to %s %s port [%s/%s] succeeded!\n", |
host, portlist[i], uflag ? "udp" : "tcp", |
host, portlist[i], uflag ? "udp" : "tcp", |
sv ? sv->s_name : "*"); |
sv ? sv->s_name : "*"); |
} |
} |
if (!zflag) |
if (!zflag) |
readwrite(s); |
readwrite(s); |
|
|
* port or source address if needed. Return's -1 on failure. |
* port or source address if needed. Return's -1 on failure. |
*/ |
*/ |
int |
int |
remote_connect(host, port, hints) |
remote_connect(char *host, char *port, struct addrinfo hints) |
char *host, *port; |
|
struct addrinfo hints; |
|
{ |
{ |
struct addrinfo *res, *res0; |
struct addrinfo *res, *res0; |
int s, error; |
int s, error; |
|
|
res0 = res; |
res0 = res; |
do { |
do { |
if ((s = socket(res0->ai_family, res0->ai_socktype, |
if ((s = socket(res0->ai_family, res0->ai_socktype, |
res0->ai_protocol)) < 0) |
res0->ai_protocol)) < 0) |
continue; |
continue; |
|
|
/* Bind to a local port or source address if specified */ |
/* Bind to a local port or source address if specified */ |
|
|
errx(1, "%s", gai_strerror(error)); |
errx(1, "%s", gai_strerror(error)); |
|
|
if (bind(s, (struct sockaddr *)ares->ai_addr, |
if (bind(s, (struct sockaddr *)ares->ai_addr, |
ares->ai_addrlen) < 0) { |
ares->ai_addrlen) < 0) { |
errx(1, "bind failed: %s", strerror(errno)); |
errx(1, "bind failed: %s", strerror(errno)); |
freeaddrinfo(ares); |
freeaddrinfo(ares); |
continue; |
continue; |
|
|
* address. Return's -1 on failure. |
* address. Return's -1 on failure. |
*/ |
*/ |
int |
int |
local_listen(host, port, hints) |
local_listen(char *host, char *port, struct addrinfo hints) |
char *host, *port; |
|
struct addrinfo hints; |
|
{ |
{ |
struct addrinfo *res, *res0; |
struct addrinfo *res, *res0; |
int s, ret, x = 1; |
int s, ret, x = 1; |
|
|
res0 = res; |
res0 = res; |
do { |
do { |
if ((s = socket(res0->ai_family, res0->ai_socktype, |
if ((s = socket(res0->ai_family, res0->ai_socktype, |
res0->ai_protocol)) == 0) |
res0->ai_protocol)) == 0) |
continue; |
continue; |
|
|
ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); |
ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); |
|
|
err(1, NULL); |
err(1, NULL); |
|
|
if (bind(s, (struct sockaddr *)res0->ai_addr, |
if (bind(s, (struct sockaddr *)res0->ai_addr, |
res0->ai_addrlen) == 0) |
res0->ai_addrlen) == 0) |
break; |
break; |
|
|
close(s); |
close(s); |
|
|
* Loop that polls on the network file descriptor and stdin. |
* Loop that polls on the network file descriptor and stdin. |
*/ |
*/ |
void |
void |
readwrite(nfd) |
readwrite(int nfd) |
int nfd; |
|
{ |
{ |
struct pollfd *pfd; |
struct pollfd *pfd; |
char buf[BUFSIZ]; |
char buf[BUFSIZ]; |
|
|
} |
} |
/* Deal with RFC854 WILL/WONT DO/DONT negotiation */ |
/* Deal with RFC854 WILL/WONT DO/DONT negotiation */ |
void |
void |
atelnet(nfd, buf, size) |
atelnet(int nfd, unsigned char *buf, unsigned int size) |
int nfd; |
|
unsigned char *buf; |
|
unsigned int size; |
|
{ |
{ |
int ret; |
int ret; |
unsigned char *p, *end; |
unsigned char *p, *end; |
|
|
* that we should try to connect too. |
* that we should try to connect too. |
*/ |
*/ |
void |
void |
build_ports(p) |
build_ports(char *p) |
char *p; |
|
{ |
{ |
char *n, *endp; |
char *n, *endp; |
int hi, lo, cp; |
int hi, lo, cp; |
|
|
* Also fails after around 100 ports checked. |
* Also fails after around 100 ports checked. |
*/ |
*/ |
int |
int |
udptest(s) |
udptest(int s) |
int s; |
|
{ |
{ |
int i, rv, ret; |
int i, rv, ret; |
|
|
|
|
} |
} |
|
|
void |
void |
usage(ret) |
usage(int ret) |
int ret; |
|
{ |
{ |
fprintf(stderr, "usage: nc [-46hklnrtuvz] [-i interval] [-p source port]\n"); |
fprintf(stderr, "usage: nc [-46hklnrtuvz] [-i interval] [-p source port]\n"); |
fprintf(stderr, "\t [-s ip address] [-w timeout] [-x proxy address [:port]]\n"); |
fprintf(stderr, "\t [-s ip address] [-w timeout] [-x proxy address [:port]]\n"); |