| 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> |
| 15 | #include <errno.h> |
| 16 | |
| 17 | #include "splice.h" |
| 18 | |
| 19 | static int usage(char *name) |
| 20 | { |
| 21 | fprintf(stderr, "%s: target port\n", name); |
| 22 | return 1; |
| 23 | } |
| 24 | |
| 25 | int main(int argc, char *argv[]) |
| 26 | { |
| 27 | struct sockaddr_in addr; |
| 28 | unsigned short port; |
| 29 | int fd, ret; |
| 30 | |
| 31 | if (argc < 3) |
| 32 | return usage(argv[0]); |
| 33 | |
| 34 | if (check_input_pipe()) |
| 35 | return usage(argv[0]); |
| 36 | |
| 37 | port = atoi(argv[2]); |
| 38 | |
| 39 | memset(&addr, 0, sizeof(addr)); |
| 40 | addr.sin_family = AF_INET; |
| 41 | addr.sin_port = htons(port); |
| 42 | |
| 43 | if (inet_aton(argv[1], &addr.sin_addr) != 1) { |
| 44 | struct hostent *hent = gethostbyname(argv[1]); |
| 45 | |
| 46 | if (!hent) |
| 47 | return error("gethostbyname"); |
| 48 | |
| 49 | memcpy(&addr.sin_addr, hent->h_addr, 4); |
| 50 | } |
| 51 | |
| 52 | printf("Connecting to %s/%d\n", argv[1], port); |
| 53 | |
| 54 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 55 | if (fd < 0) |
| 56 | return error("socket"); |
| 57 | |
| 58 | if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) |
| 59 | return error("connect"); |
| 60 | |
| 61 | do { |
| 62 | ret = ssplice(STDIN_FILENO, NULL, fd, NULL, SPLICE_SIZE, SPLICE_F_NONBLOCK); |
| 63 | if (ret < 0) { |
| 64 | if (errno == EAGAIN) { |
| 65 | usleep(100); |
| 66 | continue; |
| 67 | } |
| 68 | return error("splice"); |
| 69 | } else if (!ret) |
| 70 | break; |
| 71 | } while (1); |
| 72 | |
| 73 | close(fd); |
| 74 | return 0; |
| 75 | } |