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