diff options
author | Eric Andersen <andersen@codepoet.org> | 2002-07-03 11:51:44 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2002-07-03 11:51:44 +0000 |
commit | 0b31586c7113b9b26ca0aeee9247a831b55b308c (patch) | |
tree | 7071145eeeea4dd92d18e05fce6d8e710dc67d52 /networking/telnet.c | |
parent | 51b8bd68bb22b1cc5d95e418813c2f08a194ec2b (diff) | |
download | busybox-w32-0b31586c7113b9b26ca0aeee9247a831b55b308c.tar.gz busybox-w32-0b31586c7113b9b26ca0aeee9247a831b55b308c.tar.bz2 busybox-w32-0b31586c7113b9b26ca0aeee9247a831b55b308c.zip |
A patch from Bart Visscher <magick@linux-fan.com> to add an
xconnect helper routine which does:
-address and port resolving
-tries to connect to all resolved addresses until connected
-uses getaddrinfo, so works for IPv6 too
This patch also ports rdate, telnet, and wget to use the new
xconnect function. Thanks Bart!
Diffstat (limited to 'networking/telnet.c')
-rw-r--r-- | networking/telnet.c | 96 |
1 files changed, 7 insertions, 89 deletions
diff --git a/networking/telnet.c b/networking/telnet.c index 53616c01d..86d672c2d 100644 --- a/networking/telnet.c +++ b/networking/telnet.c | |||
@@ -36,13 +36,11 @@ | |||
36 | #include <errno.h> | 36 | #include <errno.h> |
37 | #include <stdlib.h> | 37 | #include <stdlib.h> |
38 | #include <stdarg.h> | 38 | #include <stdarg.h> |
39 | #include <string.h> | ||
40 | #include <signal.h> | 39 | #include <signal.h> |
41 | #include <arpa/telnet.h> | 40 | #include <arpa/telnet.h> |
42 | #include <sys/types.h> | 41 | #include <sys/types.h> |
43 | #include <sys/socket.h> | 42 | #include <sys/socket.h> |
44 | #include <netinet/in.h> | 43 | #include <netinet/in.h> |
45 | #include <netdb.h> | ||
46 | #include "busybox.h" | 44 | #include "busybox.h" |
47 | 45 | ||
48 | #ifdef CONFIG_FEATURE_AUTOWIDTH | 46 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
@@ -122,19 +120,12 @@ static inline void iacflush(void) | |||
122 | } | 120 | } |
123 | 121 | ||
124 | /* Function prototypes */ | 122 | /* Function prototypes */ |
125 | static int getport(char * p); | ||
126 | static struct in_addr getserver(char * p); | ||
127 | static void setup_sockaddr_in(struct sockaddr_in * addr, int port); | ||
128 | static int remote_connect(struct in_addr addr, int port); | ||
129 | static void rawmode(void); | 123 | static void rawmode(void); |
130 | static void cookmode(void); | 124 | static void cookmode(void); |
131 | static void do_linemode(void); | 125 | static void do_linemode(void); |
132 | static void will_charmode(void); | 126 | static void will_charmode(void); |
133 | static void telopt(byte c); | 127 | static void telopt(byte c); |
134 | static int subneg(byte c); | 128 | static int subneg(byte c); |
135 | #if 0 | ||
136 | static int local_bind(int port); | ||
137 | #endif | ||
138 | 129 | ||
139 | /* Some globals */ | 130 | /* Some globals */ |
140 | static int one = 1; | 131 | static int one = 1; |
@@ -584,8 +575,8 @@ static void cookmode(void) | |||
584 | 575 | ||
585 | extern int telnet_main(int argc, char** argv) | 576 | extern int telnet_main(int argc, char** argv) |
586 | { | 577 | { |
587 | struct in_addr host; | 578 | char *host; |
588 | int port; | 579 | char *port; |
589 | int len; | 580 | int len; |
590 | #ifdef USE_POLL | 581 | #ifdef USE_POLL |
591 | struct pollfd ufds[2]; | 582 | struct pollfd ufds[2]; |
@@ -615,11 +606,13 @@ extern int telnet_main(int argc, char** argv) | |||
615 | cfmakeraw(&G.termios_raw); | 606 | cfmakeraw(&G.termios_raw); |
616 | 607 | ||
617 | if (argc < 2) show_usage(); | 608 | if (argc < 2) show_usage(); |
618 | port = (argc > 2)? getport(argv[2]): 23; | 609 | port = (argc > 2)? argv[2] : "23"; |
610 | |||
611 | host = argv[1]; | ||
619 | 612 | ||
620 | host = getserver(argv[1]); | 613 | G.netfd = xconnect(host, port); |
621 | 614 | ||
622 | G.netfd = remote_connect(host, port); | 615 | setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one); |
623 | 616 | ||
624 | signal(SIGINT, fgotsig); | 617 | signal(SIGINT, fgotsig); |
625 | 618 | ||
@@ -691,81 +684,6 @@ extern int telnet_main(int argc, char** argv) | |||
691 | } | 684 | } |
692 | } | 685 | } |
693 | 686 | ||
694 | static int getport(char * p) | ||
695 | { | ||
696 | unsigned int port = atoi(p); | ||
697 | |||
698 | if ((unsigned)(port - 1 ) > 65534) | ||
699 | { | ||
700 | error_msg_and_die("%s: bad port number", p); | ||
701 | } | ||
702 | return port; | ||
703 | } | ||
704 | |||
705 | static struct in_addr getserver(char * host) | ||
706 | { | ||
707 | struct in_addr addr; | ||
708 | |||
709 | struct hostent * he; | ||
710 | he = xgethostbyname(host); | ||
711 | memcpy(&addr, he->h_addr, sizeof addr); | ||
712 | |||
713 | TRACE(1, ("addr: %s\n", inet_ntoa(addr))); | ||
714 | |||
715 | return addr; | ||
716 | } | ||
717 | |||
718 | static int create_socket(void) | ||
719 | { | ||
720 | return socket(AF_INET, SOCK_STREAM, 0); | ||
721 | } | ||
722 | |||
723 | static void setup_sockaddr_in(struct sockaddr_in * addr, int port) | ||
724 | { | ||
725 | memset(addr, 0, sizeof(struct sockaddr_in)); | ||
726 | addr->sin_family = AF_INET; | ||
727 | addr->sin_port = htons(port); | ||
728 | } | ||
729 | |||
730 | #if 0 | ||
731 | static int local_bind(int port) | ||
732 | { | ||
733 | struct sockaddr_in s_addr; | ||
734 | int s = create_socket(); | ||
735 | |||
736 | setup_sockaddr_in(&s_addr, port); | ||
737 | |||
738 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); | ||
739 | |||
740 | if (bind(s, &s_addr, sizeof s_addr) < 0) | ||
741 | { | ||
742 | char * e = sys_errlist[errno]; | ||
743 | syserrorexit("bind"); | ||
744 | exit(1); | ||
745 | } | ||
746 | listen(s, 1); | ||
747 | |||
748 | return s; | ||
749 | } | ||
750 | #endif | ||
751 | |||
752 | static int remote_connect(struct in_addr addr, int port) | ||
753 | { | ||
754 | struct sockaddr_in s_addr; | ||
755 | int s = create_socket(); | ||
756 | |||
757 | setup_sockaddr_in(&s_addr, port); | ||
758 | s_addr.sin_addr = addr; | ||
759 | |||
760 | setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one); | ||
761 | |||
762 | if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0) | ||
763 | { | ||
764 | perror_msg_and_die("Unable to connect to remote host"); | ||
765 | } | ||
766 | return s; | ||
767 | } | ||
768 | |||
769 | /* | 687 | /* |
770 | Local Variables: | 688 | Local Variables: |
771 | c-file-style: "linux" | 689 | c-file-style: "linux" |