Commit | Line | Data |
---|---|---|
d8525fbd JA |
1 | /* |
2 | * Splice stdin to net | |
3 | */ | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
6 | #include <unistd.h> | |
7 | #include <netdb.h> | |
8 | #include <unistd.h> | |
9 | #include <fcntl.h> | |
10 | #include <signal.h> | |
11 | #include <netinet/in.h> | |
12 | #include <arpa/inet.h> | |
13 | #include <string.h> | |
14 | #include <sys/time.h> | |
44de503b | 15 | #include <errno.h> |
d8525fbd JA |
16 | |
17 | #include "splice.h" | |
18 | ||
f91353a3 JA |
19 | static int usage(char *name) |
20 | { | |
21 | fprintf(stderr, "%s: target port\n", name); | |
22 | return 1; | |
23 | } | |
24 | ||
d8525fbd JA |
25 | int main(int argc, char *argv[]) |
26 | { | |
27 | struct sockaddr_in addr; | |
28 | unsigned short port; | |
0592b903 | 29 | int fd, ret; |
0592b903 | 30 | |
f91353a3 JA |
31 | if (argc < 3) |
32 | return usage(argv[0]); | |
d8525fbd | 33 | |
76797253 | 34 | if (check_input_pipe()) |
f91353a3 | 35 | return usage(argv[0]); |
d8525fbd | 36 | |
0592b903 | 37 | port = atoi(argv[2]); |
d8525fbd JA |
38 | |
39 | memset(&addr, 0, sizeof(addr)); | |
40 | addr.sin_family = AF_INET; | |
41 | addr.sin_port = htons(port); | |
42 | ||
0592b903 JA |
43 | if (inet_aton(argv[1], &addr.sin_addr) != 1) { |
44 | struct hostent *hent = gethostbyname(argv[1]); | |
d8525fbd | 45 | |
49b68ef7 JA |
46 | if (!hent) |
47 | return error("gethostbyname"); | |
d8525fbd JA |
48 | |
49 | memcpy(&addr.sin_addr, hent->h_addr, 4); | |
50 | } | |
51 | ||
0592b903 | 52 | printf("Connecting to %s/%d\n", argv[1], port); |
d8525fbd JA |
53 | |
54 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
49b68ef7 JA |
55 | if (fd < 0) |
56 | return error("socket"); | |
d8525fbd | 57 | |
49b68ef7 JA |
58 | if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) |
59 | return error("connect"); | |
d8525fbd | 60 | |
d8525fbd | 61 | do { |
13b72067 | 62 | ret = ssplice(STDIN_FILENO, NULL, fd, NULL, SPLICE_SIZE, SPLICE_F_NONBLOCK); |
44de503b JA |
63 | if (ret < 0) { |
64 | if (errno == EAGAIN) { | |
65 | usleep(100); | |
66 | continue; | |
67 | } | |
49b68ef7 | 68 | return error("splice"); |
44de503b | 69 | } else if (!ret) |
d8525fbd | 70 | break; |
4d6b8191 | 71 | } while (1); |
d8525fbd | 72 | |
d8525fbd JA |
73 | close(fd); |
74 | return 0; | |
75 | } |