2 * A tee implementation using sys_tee. Sends out the data received over
3 * stdin to the given host:port and over stdout.
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
13 #include <sys/types.h>
19 static int do_splice(int infd, int outfd, unsigned int len, char *msg)
22 int written = splice(infd, NULL, outfd, NULL, len, 0);
33 static int usage(char *name)
35 fprintf(stderr, "... | %s: hostname:port\n", name);
39 int main(int argc, char *argv[])
41 struct sockaddr_in addr;
46 return usage(argv[0]);
48 if (check_input_pipe())
49 return usage(argv[0]);
51 hname = strdup(argv[1]);
52 p = strstr(hname, ":");
54 return usage(argv[0]);
56 memset(&addr, 0, sizeof(addr));
57 addr.sin_family = AF_INET;
58 addr.sin_port = htons(atoi(p + 1));
61 if (inet_aton(hname, &addr.sin_addr) != 1) {
62 struct hostent *hent = gethostbyname(hname);
65 return error("gethostbyname");
67 memcpy(&addr.sin_addr, hent->h_addr, 4);
70 fd = socket(AF_INET, SOCK_STREAM, 0);
72 return error("socket");
74 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
75 return error("connect");
78 int tee_len = tee(STDIN_FILENO, STDOUT_FILENO, INT_MAX, 0);
81 if (errno == EAGAIN) {
90 * Send output to file, also consumes input pipe.
92 if (do_splice(STDIN_FILENO, fd, tee_len, "splice-net"))