dropbear 2015.71
[tomato.git] / release / src / router / dropbear / tcp-accept.c
blob445692026ec8c331444c3c01861b8261e6670d1a
1 /*
2 * Dropbear SSH
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE. */
25 #include "includes.h"
26 #include "ssh.h"
27 #include "tcpfwd.h"
28 #include "dbutil.h"
29 #include "session.h"
30 #include "buffer.h"
31 #include "packet.h"
32 #include "listener.h"
33 #include "listener.h"
34 #include "runopts.h"
36 #ifdef DROPBEAR_TCP_ACCEPT
38 static void cleanup_tcp(struct Listener *listener) {
40 struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata);
42 m_free(tcpinfo->sendaddr);
43 m_free(tcpinfo->listenaddr);
44 m_free(tcpinfo->request_listenaddr);
45 m_free(tcpinfo);
48 int tcp_prio_inithandler(struct Channel* channel)
50 TRACE(("tcp_prio_inithandler channel %d", channel->index))
51 channel->prio = DROPBEAR_CHANNEL_PRIO_UNKNOWABLE;
52 return 0;
55 static void tcp_acceptor(struct Listener *listener, int sock) {
57 int fd;
58 struct sockaddr_storage addr;
59 socklen_t len;
60 char ipstring[NI_MAXHOST], portstring[NI_MAXSERV];
61 struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata);
63 len = sizeof(addr);
65 fd = accept(sock, (struct sockaddr*)&addr, &len);
66 if (fd < 0) {
67 return;
70 if (getnameinfo((struct sockaddr*)&addr, len, ipstring, sizeof(ipstring),
71 portstring, sizeof(portstring),
72 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
73 m_close(fd);
74 return;
77 if (send_msg_channel_open_init(fd, tcpinfo->chantype) == DROPBEAR_SUCCESS) {
78 char* addr = NULL;
79 unsigned int port = 0;
81 if (tcpinfo->tcp_type == direct) {
82 /* "direct-tcpip" */
83 /* host to connect, port to connect */
84 addr = tcpinfo->sendaddr;
85 port = tcpinfo->sendport;
86 } else {
87 dropbear_assert(tcpinfo->tcp_type == forwarded);
88 /* "forwarded-tcpip" */
89 /* address that was connected, port that was connected */
90 addr = tcpinfo->request_listenaddr;
91 port = tcpinfo->listenport;
94 if (addr == NULL) {
95 addr = "localhost";
97 buf_putstring(ses.writepayload, addr, strlen(addr));
98 buf_putint(ses.writepayload, port);
100 /* originator ip */
101 buf_putstring(ses.writepayload, ipstring, strlen(ipstring));
102 /* originator port */
103 buf_putint(ses.writepayload, atol(portstring));
105 encrypt_packet();
107 } else {
108 /* XXX debug? */
109 close(fd);
113 int listen_tcpfwd(struct TCPListener* tcpinfo) {
115 char portstring[NI_MAXSERV];
116 int socks[DROPBEAR_MAX_SOCKS];
117 struct Listener *listener = NULL;
118 int nsocks;
119 char* errstring = NULL;
121 TRACE(("enter listen_tcpfwd"))
123 /* first we try to bind, so don't need to do so much cleanup on failure */
124 snprintf(portstring, sizeof(portstring), "%d", tcpinfo->listenport);
126 nsocks = dropbear_listen(tcpinfo->listenaddr, portstring, socks,
127 DROPBEAR_MAX_SOCKS, &errstring, &ses.maxfd);
128 if (nsocks < 0) {
129 dropbear_log(LOG_INFO, "TCP forward failed: %s", errstring);
130 m_free(errstring);
131 TRACE(("leave listen_tcpfwd: dropbear_listen failed"))
132 return DROPBEAR_FAILURE;
134 m_free(errstring);
136 /* new_listener will close the socks if it fails */
137 listener = new_listener(socks, nsocks, CHANNEL_ID_TCPFORWARDED, tcpinfo,
138 tcp_acceptor, cleanup_tcp);
140 if (listener == NULL) {
141 TRACE(("leave listen_tcpfwd: listener failed"))
142 return DROPBEAR_FAILURE;
145 TRACE(("leave listen_tcpfwd: success"))
146 return DROPBEAR_SUCCESS;
149 #endif /* DROPBEAR_TCP_ACCEPT */