srelay bind to device (#2193)
[openwrt/svn-archive/archive.git] / net / srelay / patches / 004-bindtodevice.patch
1 diff -u srelay-0.4.6.orig/main.c srelay-0.4.6/main.c
2 --- srelay-0.4.6.orig/main.c 2003-03-26 20:45:12.000000000 +0000
3 +++ srelay-0.4.6/main.c 2007-08-09 13:53:30.000000000 +0100
4 @@ -43,6 +43,7 @@
5 char *ident = "srelay";
6 char *pidfile = PIDFILE;
7 char *pwdfile = PWDFILE;
8 +char *bindtodevice = NULL;
9 pid_t master_pid;
10
11 #if USE_THREAD
12 @@ -74,6 +75,9 @@
13 fprintf(stderr, "options:\n"
14 "\t-c file\tconfig file\n"
15 "\t-i i/f\tlisten interface IP[:PORT]\n"
16 +#ifdef SO_BINDTODEVICE
17 + "\t-J i/f\toutbound interface name\n"
18 +#endif
19 "\t-m num\tmax child/thread\n"
20 "\t-o min\tidle timeout minutes\n"
21 "\t-p file\tpid file\n"
22 @@ -125,7 +129,7 @@
23
24 uid = getuid();
25
26 - while((ch = getopt(ac, av, "a:c:i:m:o:p:u:frstbvh?")) != -1)
27 + while((ch = getopt(ac, av, "a:c:i:J:m:o:p:u:frstbvh?")) != -1)
28 switch (ch) {
29 case 'a':
30 if (optarg != NULL) {
31 @@ -180,6 +184,14 @@
32 }
33 break;
34
35 +#ifdef SO_BINDTODEVICE
36 + case 'J':
37 + if (optarg != NULL) {
38 + bindtodevice = strdup(optarg);
39 + }
40 + break;
41 +#endif
42 +
43 case 'o':
44 if (optarg != NULL) {
45 idle_timeout = atol(optarg);
46 diff -u srelay-0.4.6.orig/socks.c srelay-0.4.6/socks.c
47 --- srelay-0.4.6.orig/socks.c 2003-04-13 22:13:25.000000000 +0100
48 +++ srelay-0.4.6/socks.c 2007-08-09 14:44:24.000000000 +0100
49 @@ -990,6 +990,24 @@
50 return(-1);
51 }
52
53 +#ifdef SO_BINDTODEVICE
54 +#include <net/if.h>
55 +static int do_bindtodevice(int cs, char *dev)
56 +{
57 + int rc;
58 + struct ifreq interface;
59 +
60 + strncpy(interface.ifr_name, dev, IFNAMSIZ);
61 + setreuid(PROCUID, 0);
62 + rc = setsockopt(cs, SOL_SOCKET, SO_BINDTODEVICE,
63 + (char *)&interface, sizeof(interface));
64 + setreuid(0, PROCUID);
65 + if (rc < 0)
66 + msg_out(crit, "setsockopt SO_BINDTODEVICE(%s) failed: %d", dev, errno);
67 + return(rc);
68 +}
69 +#endif
70 +
71 int socks_direct_conn(int ver, struct socks_req *req)
72 {
73 int cs, acs = 0;
74 @@ -1037,6 +1055,14 @@
75 continue;
76 }
77
78 +#ifdef SO_BINDTODEVICE
79 + if (bindtodevice && do_bindtodevice(cs, bindtodevice) < 0) {
80 + save_errno = errno;
81 + close(cs);
82 + continue;
83 + }
84 +#endif
85 +
86 if (connect(cs, res->ai_addr, res->ai_addrlen) < 0) {
87 /* connect fail */
88 save_errno = errno;
89 @@ -1096,6 +1122,14 @@
90 return(-1);
91 }
92
93 +#ifdef SO_BINDTODEVICE
94 + if (bindtodevice && do_bindtodevice(acs, bindtodevice) < 0) {
95 + GEN_ERR_REP(req->s, ver);
96 + close(acs);
97 + return(-1);
98 + }
99 +#endif
100 +
101 if (bind_sock(acs, req, &ba) != 0) {
102 GEN_ERR_REP(req->s, ver);
103 return(-1);
104 @@ -1351,6 +1385,14 @@
105 continue;
106 }
107
108 +#ifdef SO_BINDTODEVICE
109 + if (bindtodevice && do_bindtodevice(cs, bindtodevice) < 0) {
110 + save_errno = errno;
111 + close(cs);
112 + continue;
113 + }
114 +#endif
115 +
116 if (connect(cs, res->ai_addr, res->ai_addrlen) < 0) {
117 /* connect fail */
118 save_errno = errno;
119 diff -u srelay-0.4.6.orig/srelay.h srelay-0.4.6/srelay.h
120 --- srelay-0.4.6.orig/srelay.h 2003-04-14 06:36:15.000000000 +0100
121 +++ srelay-0.4.6/srelay.h 2007-08-09 13:46:06.000000000 +0100
122 @@ -266,6 +266,7 @@
123 extern char *ident;
124 extern char *pidfile;
125 extern char *pwdfile;
126 +extern char *bindtodevice;
127 extern int max_child;
128 extern int cur_child;
129 extern char method_tab[];