aboutsummaryrefslogtreecommitdiff
path: root/toys/netcat.c
blob: 39c531d8bc30dcf837fc6a6f92f27b83c0105d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* vi: set sw=4 ts=4: */
/* nc: mini-netcat - Forward stdin/stdout to a file or network connection.
 *
 * Copyright 2007 Rob Landley <rob@landley.net>
 */

#include "toys.h"
#include "toynet.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define TT toy.netcat

static void timeout(int signum)
{
	error_exit("Timeout");
}

// Translate x.x.x.x numeric IPv4 address, or else DNS lookup an IPv4 name.
void lookup_name(char *name, uint32_t *result)
{
	struct hostent *hostbyname;

	hostbyname = gethostbyname(*toys.optargs);
	if (!hostbyname) error_exit("name lookup failed");
	*result = *(uint32_t *)*hostbyname->h_addr_list;
}

// Worry about a fancy lookup later.
void lookup_port(char *str, uint16_t *port)
{
  *port = SWAP_BE16(atoi(str));
}

void netcat_main(void)
{
	int sockfd, pollcount;
	struct pollfd pollfds[2];

	if (TT.wait) {
		signal(SIGALRM, timeout);
		alarm(TT.wait);
	}

	if (TT.filename) pollfds[0].fd = xopen(TT.filename, O_RDWR);
	else {
		int temp;
		struct sockaddr_in address;

		// The argument parsing logic can't make "<2" conditional on "-f", so...
		if (!*toys.optargs || !toys.optargs[1]) {
			toys.exithelp++;
			error_exit("Need address and port");
		}

		// Setup socket
		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		if (-1 == sockfd) perror_exit("socket");
		fcntl(sockfd, F_SETFD, FD_CLOEXEC);
		temp = 1;
		setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &temp, sizeof(temp));
		memset(&address, 0, sizeof(address));
		address.sin_family = AF_INET;
		if (TT.port) {
			address.sin_port = TT.port;
			if (-1 == bind(sockfd, &address, sizeof(address)))
				perror_exit("bind");
		}

		// Figure out where to dial out to.
		lookup_name(*toys.optargs, (uint32_t *)&address.sin_addr);
		lookup_port(toys.optargs[1], &address.sin_port);
		temp = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
		if (temp<0) perror_exit("connect");
		pollfds[0].fd = sockfd;
	}

	// We have a connection.  Disarm timeout.
	if (TT.wait) {
		alarm(0);
		signal(SIGALRM, SIG_DFL);
	}

	pollcount = 2;
	pollfds[1].fd = 0;
	pollfds[0].events = pollfds[1].events = POLLIN;

	// Poll loop copying stdin->socket and socket->stdout.
	for (;;) {
		int i;

		if (0>poll(pollfds, pollcount, -1)) perror_exit("poll");

		for (i=0; i<pollcount; i++) {
			if (pollfds[i].revents & POLLIN) {
				int len = read(pollfds[i].fd, toybuf, sizeof(toybuf));
				if (len<1) goto dohupnow;
				xwrite(i ? pollfds[0].fd : 1, toybuf, len);
			}
			if (pollfds[i].revents & POLLHUP) {
dohupnow:
				// Close half-connect.  This is needed for things like
				// "echo GET / | netcat landley.net 80" to work.
				if (i) {
					shutdown(pollfds[0].fd, SHUT_WR);
					pollcount--;
				} else goto cleanup;
			}
		}
	}
cleanup:
	close(pollfds[0].fd);
//	close(sockfd);
}