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!
This commit is contained in:
Eric Andersen
2002-07-03 11:51:44 +00:00
parent 51b8bd68bb
commit 0b31586c71
4 changed files with 93 additions and 117 deletions

View File

@@ -36,13 +36,11 @@
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <arpa/telnet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "busybox.h"
#ifdef CONFIG_FEATURE_AUTOWIDTH
@@ -122,19 +120,12 @@ static inline void iacflush(void)
}
/* Function prototypes */
static int getport(char * p);
static struct in_addr getserver(char * p);
static void setup_sockaddr_in(struct sockaddr_in * addr, int port);
static int remote_connect(struct in_addr addr, int port);
static void rawmode(void);
static void cookmode(void);
static void do_linemode(void);
static void will_charmode(void);
static void telopt(byte c);
static int subneg(byte c);
#if 0
static int local_bind(int port);
#endif
/* Some globals */
static int one = 1;
@@ -584,8 +575,8 @@ static void cookmode(void)
extern int telnet_main(int argc, char** argv)
{
struct in_addr host;
int port;
char *host;
char *port;
int len;
#ifdef USE_POLL
struct pollfd ufds[2];
@@ -615,11 +606,13 @@ extern int telnet_main(int argc, char** argv)
cfmakeraw(&G.termios_raw);
if (argc < 2) show_usage();
port = (argc > 2)? getport(argv[2]): 23;
port = (argc > 2)? argv[2] : "23";
host = getserver(argv[1]);
host = argv[1];
G.netfd = xconnect(host, port);
G.netfd = remote_connect(host, port);
setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
signal(SIGINT, fgotsig);
@@ -691,81 +684,6 @@ extern int telnet_main(int argc, char** argv)
}
}
static int getport(char * p)
{
unsigned int port = atoi(p);
if ((unsigned)(port - 1 ) > 65534)
{
error_msg_and_die("%s: bad port number", p);
}
return port;
}
static struct in_addr getserver(char * host)
{
struct in_addr addr;
struct hostent * he;
he = xgethostbyname(host);
memcpy(&addr, he->h_addr, sizeof addr);
TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
return addr;
}
static int create_socket(void)
{
return socket(AF_INET, SOCK_STREAM, 0);
}
static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
{
memset(addr, 0, sizeof(struct sockaddr_in));
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
}
#if 0
static int local_bind(int port)
{
struct sockaddr_in s_addr;
int s = create_socket();
setup_sockaddr_in(&s_addr, port);
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
if (bind(s, &s_addr, sizeof s_addr) < 0)
{
char * e = sys_errlist[errno];
syserrorexit("bind");
exit(1);
}
listen(s, 1);
return s;
}
#endif
static int remote_connect(struct in_addr addr, int port)
{
struct sockaddr_in s_addr;
int s = create_socket();
setup_sockaddr_in(&s_addr, port);
s_addr.sin_addr = addr;
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
{
perror_msg_and_die("Unable to connect to remote host");
}
return s;
}
/*
Local Variables:
c-file-style: "linux"

View File

@@ -563,24 +563,16 @@ void parse_url(char *url, struct host_info *h)
FILE *open_socket(char *host, int port)
{
struct sockaddr_in s_in;
struct hostent *hp;
int fd;
FILE *fp;
char port_str[10];
memset(&s_in, 0, sizeof(s_in));
s_in.sin_family = AF_INET;
hp = xgethostbyname(host);
memcpy(&s_in.sin_addr, hp->h_addr_list[0], hp->h_length);
s_in.sin_port = htons(port);
snprintf(port_str, sizeof(port_str), "%d", port);
fd=xconnect(host, port_str);
/*
* Get the server onto a stdio stream.
*/
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
perror_msg_and_die("socket()");
if (connect(fd, (struct sockaddr *) &s_in, sizeof(s_in)) < 0)
perror_msg_and_die("connect(%s)", host);
if ((fp = fdopen(fd, "r+")) == NULL)
perror_msg_and_die("fdopen()");
@@ -826,7 +818,7 @@ progressmeter(int flag)
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: wget.c,v 1.49 2002/05/14 23:36:45 sandman Exp $
* $Id: wget.c,v 1.50 2002/07/03 11:51:44 andersen Exp $
*/