PATCH: Add -i IFACE option to wget

Bernhard Fischer rep.nop at aon.at
Wed Oct 25 07:52:17 PDT 2006


On Wed, Oct 25, 2006 at 03:02:44PM +0200, Raphaël HUCK wrote:
>Here is the patch which applies to the last version from svn (16429).
>
>--Raphael HUCK

>--- usage_old.h	2006-10-25 14:55:27.000000000 +0200
>+++ usage.h	2006-10-25 14:55:54.000000000 +0200
>@@ -3334,16 +3334,17 @@ USE_FEATURE_START_STOP_DAEMON_FANCY( \
> #define wget_trivial_usage \
> 	"[-c|--continue] [-q|--quiet] [-O|--output-document file]\n" \
> 	"\t\t[--header 'header: value'] [-Y|--proxy on/off] [-P DIR]\n" \
>-	"\t\t[-U|--user-agent agent] url"
>+	"\t\t[-U|--user-agent agent] [-I|--bind-interface interface] url"
> #define wget_full_usage \
> 	"wget retrieves files via HTTP or FTP\n\n" \
> 	"Options:\n" \
> 	"\t-c\tcontinue retrieval of aborted transfers\n" \
> 	"\t-q\tquiet mode - do not print\n" \
>-	"\t-P\tSet directory prefix to DIR\n" \
>+	"\t-P\tset directory prefix to DIR\n" \
> 	"\t-O\tsave to filename ('-' for stdout)\n" \
> 	"\t-U\tadjust 'User-Agent' field\n" \
>-	"\t-Y\tuse proxy ('on' or 'off')"
>+	"\t-Y\tuse proxy ('on' or 'off')\n" \
>+	"\t-I\tbind to interface"
> 
> #define which_trivial_usage \
> 	"[COMMAND ...]"

>--- wget_old.c	2006-10-25 14:14:44.000000000 +0200
>+++ wget.c	2006-10-25 14:35:25.000000000 +0200
>@@ -12,6 +12,7 @@
> 
> #include "busybox.h"
> #include <getopt.h>	/* for struct option */
>+#include <net/if.h> /* for struct ifreq */
> 
> struct host_info {
> 	// May be used if we ever will want to free() all xstrdup()s...
>@@ -35,6 +36,7 @@ static off_t beg_range;          /* Rang
> static off_t transferred;        /* Number of bytes transferred so far */
> #endif
> static int chunked;                     /* chunked transfer encoding */
>+static char *iface = NULL;       /* interface to bind on */
> #ifdef CONFIG_FEATURE_WGET_STATUSBAR
> static void progressmeter(int flag);
> static char *curfile;                   /* Name of current file being transferred */
>@@ -94,6 +96,7 @@ static char *base64enc(unsigned char *p,
> #define WGET_OPT_PREFIX      32
> #define WGET_OPT_PROXY       64
> #define WGET_OPT_USER_AGENT 128
>+#define WGET_OPT_INTERFACE  256
> 
> #if ENABLE_FEATURE_WGET_LONG_OPTIONS
> static const struct option wget_long_options[] = {
>@@ -105,6 +108,7 @@ static const struct option wget_long_opt
> 	{ "directory-prefix",1, NULL, 'P' },
> 	{ "proxy",           1, NULL, 'Y' },
> 	{ "user-agent",      1, NULL, 'U' },
>+	{ "bind-interface",  1, NULL, 'I' },
> 	{ 0,                 0, 0, 0 }
> };
> #endif
>@@ -141,9 +145,9 @@ int wget_main(int argc, char **argv)
> #if ENABLE_FEATURE_WGET_LONG_OPTIONS
> 	applet_long_options = wget_long_options;
> #endif
>-	opt = getopt32(argc, argv, "cq\213O:\203:P:Y:U:",
>+	opt = getopt32(argc, argv, "cq\213O:\203:P:Y:U:I:",
> 					&fname_out, &headers_llist,
>-					&dir_prefix, &proxy_flag, &user_agent);
>+					&dir_prefix, &proxy_flag, &user_agent, &iface);
> 	if (strcmp(proxy_flag, "off") == 0) {
> 		/* Use the proxy if necessary. */
> 		use_proxy = 0;
>@@ -576,8 +580,36 @@ static void parse_url(char *src_url, str
> static FILE *open_socket(struct sockaddr_in *s_in)
> {
> 	FILE *fp;
>+	int s;
>+	struct sockaddr_in addr;
>+	struct sockaddr_in *sa;
>+	struct ifreq ifr;
> 
>-	fp = fdopen(xconnect(s_in), "r+");
>+	s = socket(AF_INET, SOCK_STREAM, 0);

xsocket() ?
>+
>+	if ( iface != NULL ) {
>+		memset(&ifr, 0, sizeof(struct ifreq));
>+		strncpy(ifr.ifr_name, iface, IFNAMSIZ);
>+
>+		if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
>+			bb_error_msg_and_die("Can't bind to %s", iface);
>+
>+		memset(&addr, 0, sizeof(struct sockaddr_in));
>+		addr.sin_family = AF_INET;
>+		addr.sin_port = 0;
>+		sa = (struct sockaddr_in *)&ifr.ifr_addr;
>+		memcpy(&addr.sin_addr, &sa->sin_addr, sizeof(struct in_addr));
>+
>+		xbind(s, (struct sockaddr*)&addr, sizeof(addr));
>+	}
>+
>+	if (connect(s, (struct sockaddr *) s_in, sizeof(struct sockaddr_in)) < 0) {
>+		if (ENABLE_FEATURE_CLEAN_UP) close(s);
>+			bb_perror_msg_and_die("Can't connect to remote host (%s)",
>+		inet_ntoa(s_in->sin_addr));
>+	}
xconnect() ?
>+
>+	fp = fdopen(s, "r+");
> 	if (fp == NULL)
> 		bb_perror_msg_and_die("fdopen");

vda, there a several of these which calls for an xfdopen().


More information about the busybox mailing list