comparison svr-tcpfwd.c @ 64:efb5e0b335cf

TCP forwarding works.
author Matt Johnston <matt@ucc.asn.au>
date Thu, 12 Aug 2004 13:48:42 +0000
parents dcc43965928f
children b0316ce64e4b
comparison
equal deleted inserted replaced
63:dcc43965928f 64:efb5e0b335cf
1 #include "includes.h" 1 #include "includes.h"
2 #include "ssh.h" 2 #include "ssh.h"
3 #include "tcp-accept.h" 3 #include "tcpfwd.h"
4 #include "tcp-connect.h"
5 #include "dbutil.h" 4 #include "dbutil.h"
6 #include "session.h" 5 #include "session.h"
7 #include "buffer.h" 6 #include "buffer.h"
8 #include "packet.h" 7 #include "packet.h"
9 #include "listener.h" 8 #include "listener.h"
13 12
14 static void send_msg_request_success(); 13 static void send_msg_request_success();
15 static void send_msg_request_failure(); 14 static void send_msg_request_failure();
16 static int svr_cancelremotetcp(); 15 static int svr_cancelremotetcp();
17 static int svr_remotetcpreq(); 16 static int svr_remotetcpreq();
17 static int newtcpdirect(struct Channel * channel);
18 18
19 19
20 const struct ChanType svr_chan_tcpdirect = { 20 const struct ChanType svr_chan_tcpdirect = {
21 1, /* sepfds */ 21 1, /* sepfds */
22 "direct-tcpip", 22 "direct-tcpip",
176 goto out; 176 goto out;
177 } 177 }
178 178
179 tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener)); 179 tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener));
180 tcpinfo->sendaddr = bindaddr; 180 tcpinfo->sendaddr = bindaddr;
181 TRACE(("sendport = %d", port));
182 tcpinfo->sendport = port; 181 tcpinfo->sendport = port;
182 tcpinfo->listenport = port;
183 tcpinfo->chantype = &svr_chan_tcpremote; 183 tcpinfo->chantype = &svr_chan_tcpremote;
184 184
185 /* Note: bindaddr is actually ignored by listen_tcpfwd, since 185 /* Note: bindaddr is actually ignored by listen_tcpfwd, since
186 * we only want to bind to localhost */ 186 * we only want to bind to localhost */
187 ret = listen_tcpfwd(tcpinfo); 187 ret = listen_tcpfwd(tcpinfo);
194 m_free(tcpinfo); 194 m_free(tcpinfo);
195 } 195 }
196 TRACE(("leave remotetcpreq")); 196 TRACE(("leave remotetcpreq"));
197 return ret; 197 return ret;
198 } 198 }
199
200 /* Called upon creating a new direct tcp channel (ie we connect out to an
201 * address */
202 static int newtcpdirect(struct Channel * channel) {
203
204 unsigned char* desthost = NULL;
205 unsigned int destport;
206 unsigned char* orighost = NULL;
207 unsigned int origport;
208 char portstring[NI_MAXSERV];
209 int sock;
210 int len;
211 int ret = DROPBEAR_FAILURE;
212
213 if (opts.nolocaltcp) {
214 TRACE(("leave newtcpdirect: local tcp forwarding disabled"));
215 goto out;
216 }
217
218 desthost = buf_getstring(ses.payload, &len);
219 if (len > MAX_HOST_LEN) {
220 TRACE(("leave newtcpdirect: desthost too long"));
221 goto out;
222 }
223
224 destport = buf_getint(ses.payload);
225
226 orighost = buf_getstring(ses.payload, &len);
227 if (len > MAX_HOST_LEN) {
228 TRACE(("leave newtcpdirect: orighost too long"));
229 goto out;
230 }
231
232 origport = buf_getint(ses.payload);
233
234 /* best be sure */
235 if (origport > 65535 || destport > 65535) {
236 TRACE(("leave newtcpdirect: port > 65535"));
237 goto out;
238 }
239
240 snprintf(portstring, sizeof(portstring), "%d", destport);
241 sock = connect_remote(desthost, portstring, 1, NULL);
242 if (sock < 0) {
243 TRACE(("leave newtcpdirect: sock failed"));
244 goto out;
245 }
246
247 ses.maxfd = MAX(ses.maxfd, sock);
248
249 /* Note that infd is actually the "outgoing" direction on the
250 * tcp connection, vice versa for outfd.
251 * We don't set outfd, that will get set after the connection's
252 * progress succeeds */
253 channel->infd = sock;
254 channel->initconn = 1;
255
256 ret = DROPBEAR_SUCCESS;
257
258 out:
259 m_free(desthost);
260 m_free(orighost);
261 TRACE(("leave newtcpdirect: ret %d", ret));
262 return ret;
263 }
264
199 #endif 265 #endif