Add ping -l (preload) and telnet -b (bind alias), closes #528
[openwrt/svn-archive/archive.git] / openwrt / package / busybox / patches / 351-telnet-opt-srcaddr.patch
1 diff -urN busybox-1.00.orig/include/usage.h busybox-1.00/include/usage.h
2 --- busybox-1.00.orig/include/usage.h 2006-06-27 13:34:21.000000000 +0200
3 +++ busybox-1.00/include/usage.h 2006-06-27 13:56:08.000000000 +0200
4 @@ -2445,23 +2445,30 @@
5
6 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
7 #define telnet_trivial_usage \
8 - "[-a] [-l USER] HOST [PORT]"
9 + "[-a] [-l USER] [-b SRCADDR] HOST [PORT]"
10 #define telnet_full_usage \
11 "Telnet is used to establish interactive communication with another\n" \
12 "computer over a network using the TELNET protocol.\n\n" \
13 "Options:\n" \
14 "\t-a\t\tAttempt an automatic login with the USER variable.\n" \
15 "\t-l USER\t\tAttempt an automatic login with the USER argument.\n" \
16 + "\t-b SRCADDR\tUse SRCADDR as local source address.\n" \
17 "\tHOST\t\tThe official name, alias or the IP address of the\n" \
18 "\t\t\tremote host.\n" \
19 "\tPORT\t\tThe remote port number to connect to. If it is not\n" \
20 "\t\t\tspecified, the default telnet (23) port is used."
21 #else
22 #define telnet_trivial_usage \
23 - "HOST [PORT]"
24 + "[-b SRCADDR] HOST [PORT]"
25 #define telnet_full_usage \
26 "Telnet is used to establish interactive communication with another\n"\
27 - "computer over a network using the TELNET protocol."
28 + "computer over a network using the TELNET protocol.\n\n" \
29 + "Options:\n" \
30 + "\t-b SRCADDR\tUse SRCADDR as local source address.\n" \
31 + "\tHOST\t\tThe official name, alias or the IP address of the\n" \
32 + "\t\t\tremote host.\n" \
33 + "\tPORT\t\tThe remote port number to connect to. If it is not\n" \
34 + "\t\t\tspecified, the default telnet (23) port is used."
35 #endif
36
37 #ifdef CONFIG_FEATURE_TELNETD_INETD
38 diff -urN busybox-1.00.orig/networking/telnet.c busybox-1.00/networking/telnet.c
39 --- busybox-1.00.orig/networking/telnet.c 2004-09-14 19:24:58.000000000 +0200
40 +++ busybox-1.00/networking/telnet.c 2006-06-27 13:50:52.000000000 +0200
41 @@ -44,6 +44,7 @@
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 +#include <arpa/inet.h>
46 #include "busybox.h"
47
48 #if 0
49 @@ -627,7 +628,7 @@
50 extern int telnet_main(int argc, char** argv)
51 {
52 int len;
53 - struct sockaddr_in s_in;
54 + struct sockaddr_in s_in, src_in;
55 #ifdef USE_POLL
56 struct pollfd ufds[2];
57 #else
58 @@ -635,9 +636,7 @@
59 int maxfd;
60 #endif
61
62 -#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
63 int opt;
64 -#endif
65
66 #ifdef CONFIG_FEATURE_AUTOWIDTH
67 get_terminal_width_height(0, &win_width, &win_height);
68 @@ -648,6 +647,7 @@
69 #endif
70
71 memset(&G, 0, sizeof G);
72 + memset(&src_in, 0, sizeof src_in);
73
74 if (tcgetattr(0, &G.termios_def) < 0)
75 exit(1);
76 @@ -660,14 +660,38 @@
77
78 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
79 autologin = NULL;
80 - while ((opt = getopt(argc, argv, "al:")) != EOF) {
81 +#define GETOPT_STRING "al:I:"
82 +#else
83 +#define GETOPT_STRING "I:"
84 +#endif
85 + while ((opt = getopt(argc, argv, GETOPT_STRING)) != EOF) {
86 switch (opt) {
87 +#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
88 case 'l':
89 autologin = optarg;
90 break;
91 case 'a':
92 autologin = getenv("USER");
93 break;
94 +#endif
95 + case 'b':
96 + {
97 + char dummy;
98 + int i1, i2, i3, i4;
99 + if (sscanf(optarg, "%u.%u.%u.%u%c",
100 + &i1, &i2, &i3, &i4, &dummy) == 4) {
101 + unsigned char* ptr;
102 + ptr = (unsigned char*)&src_in.sin_addr;
103 + ptr[0] = i1;
104 + ptr[1] = i2;
105 + ptr[2] = i3;
106 + ptr[3] = i4;
107 + }
108 + else {
109 + bb_show_usage();
110 + }
111 + }
112 + break;
113 case '?':
114 bb_show_usage();
115 break;
116 @@ -681,12 +705,18 @@
117 bb_show_usage();
118 } else
119 bb_show_usage();
120 -#else
121 - bb_lookup_host(&s_in, argv[1]);
122 - s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
123 -#endif
124
125 - G.netfd = xconnect(&s_in);
126 + G.netfd = socket(AF_INET, SOCK_STREAM, 0);
127 + if (src_in.sin_addr.s_addr != 0) {
128 + if (bind(G.netfd, (struct sockaddr*)&src_in, sizeof(src_in)) == -1)
129 + bb_perror_msg_and_die("Unable to bind local address (%s)",
130 + inet_ntoa(src_in.sin_addr));
131 + }
132 + if (connect(G.netfd, &s_in, sizeof(s_in)) < 0) {
133 + bb_perror_msg_and_die("Unable to connect to remote host (%s)",
134 + inet_ntoa(s_in.sin_addr));
135 + }
136 + /*G.netfd = xconnect(&s_in);*/
137
138 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
139