[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [BusyBox] Add a connect helper to libbb
From:       Bart Visscher <magick () Linux-Fan ! com>
Date:       2002-04-28 8:57:34
[Download RAW message or body]

Changelog:
* Add xconnect helper routine, it does:
	-address and port resolving
	-tries to connect to all resolved addresses until connected
	-uses getaddrinfo, so works for IPv6 too

["03-libbb-xconnect.patch" (text/x-patch)]

diff -ru -X exclude -P busybox-new-1/include/libbb.h busybox-new-2/include/libbb.h
--- busybox-new-1/include/libbb.h	Thu Apr 25 20:06:20 2002
+++ busybox-new-2/include/libbb.h	Thu Apr 25 19:59:26 2002
@@ -224,6 +224,7 @@
 extern struct hostent *xgethostbyname2(const char *name, int af);
 extern int create_icmp_socket(void);
 extern int create_icmp6_socket(void);
+extern int xconnect(const char *host, const char *port);
 
 char *dirname (char *path);
 
diff -ru -X exclude -P busybox-new-1/libbb/Makefile.in busybox-new-2/libbb/Makefile.in
--- busybox-new-1/libbb/Makefile.in	Thu Apr 25 20:06:44 2002
+++ busybox-new-2/libbb/Makefile.in	Thu Apr 25 19:59:26 2002
@@ -39,7 +39,7 @@
 	copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \
 	dirname.c make_directory.c create_icmp_socket.c u_signal_names.c arith.c \
 	simplify_path.c inet_common.c inode_hash.c xgethostbyname2.c \
-	create_icmp6_socket.c
+	create_icmp6_socket.c xconnect.c
 LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC))
 
 LIBBB_MSRC:=$(LIBBB_DIR)messages.c
diff -ru -X exclude -P busybox-new-1/libbb/xconnect.c busybox-new-2/libbb/xconnect.c
--- busybox-new-1/libbb/xconnect.c	Thu Jan  1 01:00:00 1970
+++ busybox-new-2/libbb/xconnect.c	Thu Apr 25 19:59:26 2002
@@ -0,0 +1,53 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Utility routines.
+ *
+ * Connect to host at port using address resolusion from getaddrinfo
+ *
+ */
+
+#include <unistd.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include "libbb.h"
+
+int xconnect(const char *host, const char *port)
+{
+	struct addrinfo hints;
+	struct addrinfo *res;
+	struct addrinfo *addr_info;
+	int error;
+	int s;
+
+	memset(&hints, 0, sizeof(hints));
+	/* set-up hints structure */
+	hints.ai_family = PF_UNSPEC;
+	hints.ai_socktype = SOCK_STREAM;
+	error = getaddrinfo(host, port, &hints, &res);
+	if (error||!res)
+		perror_msg_and_die(gai_strerror(error));
+	addr_info=res;
+	while (res) {
+		s=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+		if (s<0)
+		{
+			error=s;
+			res=res->ai_next;
+			continue;
+		}
+		/* try to connect() to res->ai_addr */
+		error = connect(s, res->ai_addr, res->ai_addrlen);
+		if (error >= 0)
+			break;
+		close(s);
+		res=res->ai_next;
+	}
+	freeaddrinfo(addr_info);
+	if (error < 0)
+	{
+		perror_msg_and_die("Unable to connect to remote host (%s)", host);
+	}
+	return s;
+}

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic