1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
inherit "module"; 
inherit "socket"; 
#include <module.h> 
 
/* 
 * This software is (C) 1998 Francesco Chemolli. 
 * See COPYING in the server directory for license information. 
 * This software comes with NO WARRANTY of ANY KIND, EITHER IMPLICIT 
 * OR EXPLICIT. Use at your own risk. 
 * 
 * This is a TCP port forwarding module for the Roxen webserver. 
 * Using it is very simple, just add it to your virtual server of choice. 
 */ 
 
//#define TCPFORWARDER_DEBUG 
/* 
 * Define this to enable debugging. It will also be turned on if DEBUGLVL is 
 * >= 22 (the same as http proxy) 
 */ 
 
/* 
 * Notice that Connection is not a class proper, as the module accesses its 
 * variables directly. But it's not worth the effort for such a simple 
 * thing... 
 */ 
 
constant cvs_version="$Id$"; 
 
 
 
constant module_type = MODULE_ZERO; 
constant module_name = "TCP Port Forwarder: "; 
constant module_doc  = "A basic port-forwarder" 
  "&copy; 1998 Francesco Chemolli " 
  "&lt;kinkie@kame.usr.dsi.unimi.it&gt;,<br />\n" 
  "See COPYING in the server directory for license information."; 
constant module_unique  = 0; 
 
multiset(Connection) connections=(<>); 
int total_connections_number=0, total_transferred_kb=0; 
 
 
#if DEBUG > 22 
#define TCPFORWARDER_DEBUG 
#endif 
 
#ifdef TCPFORWARDER_DEBUG 
#define debug_perror report_debug 
#else 
#define debug_perror 
#endif 
 
#define THROW(X) throw( ({X,backtrace()}) ) 
 
Stdio.Port accept_port; 
 
constant no_delayed_load = 1; 
 
/* 
 * A bidirectional pipe over HTTP. 
 */ 
class Connection 
{ 
  array(object) fdescs; 
  mapping buffer; 
  object mastermodule, master_id; 
  int traffic=0; 
 
  object otherfd (object fd) 
  { 
    if (fd==fdescs[0]) 
      return fdescs[1]; 
    else 
      return fdescs[0]; 
  } 
 
  void send(object to_fd, string data) 
  { 
    int sent=0; 
    debug_perror("Connection::send("+data+")\n"); 
    if(!strlen(buffer[to_fd])) 
      buffer[to_fd] = data[(sent=to_fd->write(data))..]; 
    else 
      buffer[to_fd] += data; 
    traffic += sent; 
  } 
 
  void got_data(object f, string data) 
  { 
    debug_perror ("Got data from "+(f?f->query_address():"unknown")+": "+data+"\n"); 
    send(otherfd(f),data); 
  } 
 
  void client_closed() 
  { 
    debug_perror("Connection: Client closed connection.\n"); 
    destruct(this_object()); 
  } 
 
  void write_more(object f) 
  { 
    debug_perror("Write_more..\n"); 
    if(strlen(buffer[f])) 
    { 
      int written = otherfd(f)->write(buffer[f]); 
      traffic += written; 
      debug_perror((string)written); 
      if(written == 0) 
        client_closed(); 
      else 
        buffer[f] = (buffer[f])[written..]; 
    } 
    debug_perror("\n"); 
  } 
 
  //s=source filedes, d=dest filedes, m=the instantiating object 
  void create(object s, object d, object m) 
  { 
    fdescs=({s,d}); 
    buffer=([s:"",d:""]); 
    s->set_nonblocking(lambda( mixed a, string d ) { got_data( s, d ); }, 
                       lambda(){ write_more(s); },   client_closed ); 
    d->set_nonblocking(lambda( mixed a, string b ) { got_data( d, b ); }, 
                       lambda(){ write_more(d); },   client_closed ); 
    mastermodule=m; 
    debug_perror("Got connection from "+s->query_address()+ 
                 " to " + d->query_address()+"\n"); 
  } 
 
  void destroy() 
  { 
    debug_perror("Destroying connection\n"); 
    fdescs[0]->close(); 
    fdescs[1]->close(); 
    mastermodule->connections-=(<this_object()>); 
    mastermodule->total_transferred_kb+=(traffic/1024); 
  } 
}; 
 
 
string status() { 
  object req; 
  string retval; 
  if (!accept_port) { 
    retval="<B>No port open</B><br>"; 
  } 
  else if (!sizeof(connections)) { 
    retval="<B>No connections</B><br>"; 
  } else { 
    retval="<B>"+sizeof(connections)+" connections</B><BR>\n"; 
    retval += "<TABLE border=1 cellpadding=\"10\"><TR><TH align=center>From</TH><TH>To</TH><TH>Traffic</TH></TR>"; 
    foreach(indices(connections),req) { 
      retval+=sprintf("<TR><TD>%s</TD><TD>%s</TD><TD>%d</TD></TR>", 
                      req->fdescs[0]->query_address(), 
                      req->fdescs[1]->query_address(), 
                      req->traffic 
                     ); 
    } 
    retval += "</TABLE>"; 
  } 
  retval +="I've managed "+total_connections_number+" connections, " 
    "transferring about "+total_transferred_kb+" Kb."; 
  return retval; 
} 
 
void create() { 
  defvar("port", 4711, "Port", TYPE_INT, 
         "The port to wait for connections on."); 
  defvar("host", "localhost", "Remote Host", TYPE_STRING, 
         "The hostname to forward connections to."); 
  defvar("r_port", 4711, "Remote Port", TYPE_INT, 
         "The port on the remote host to connect to."); 
} 
 
void start() 
{ 
  object privs; 
  int port; 
 
  port=query("port"); 
  if (accept_port) //I wonder why (at least on my setup) stop isn't called.. 
    stop(); 
  debug_perror("Opening port "+port+"\n"); 
  accept_port=Stdio.Port(); 
  if (!accept_port) 
    THROW("Can't create a port to listen on"); 
  if (port<1024) 
    privs=Privs("Opening forwarded port"); 
  if (!(accept_port->bind(port,got_connection))) 
  { 
    int err_no = accept_port->errno(); 
    string err = strerror(accept_port->errno()); 
    destruct(accept_port); 
    accept_port = 0; 
    report_error("Can't bind (errno="+err_no+": \""+err+"\")\n");; 
  } 
  privs=0; 
} 
 
void stop() 
{ 
  debug_perror("Stopping module\n"); 
  if (accept_port) destruct(accept_port); 
  accept_port=0; //double-check there's no more references 
  foreach(indices(connections),object foo) destruct(foo); 
} 
 
void got_connection (mixed port) { 
  object in; 
  in=accept_port->accept(); 
  if (!in) 
    THROW("Couldn't accept connection"); 
  total_connections_number++; 
  async_connect(query("host"),query("r_port"),connected,in); 
} 
 
void connected (object out, object in) 
{ 
  if( out ) 
    connections[ Connection(in,out,this_object()) ] = 1; 
  else 
    report_debug("Cannot connect to "+query("host")+":"+query("r_port") ); 
} 
 
string query_name() 
{ 
  return sprintf("%d to %s/%d", query("port"), query("host"), query("r_port")); 
}