4 * Copyright (c) 2002,2003 Matt Johnston
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
35 #ifdef DROPBEAR_TCP_ACCEPT
37 static void cleanup_tcp(struct Listener
*listener
) {
39 struct TCPListener
*tcpinfo
= (struct TCPListener
*)(listener
->typedata
);
41 m_free(tcpinfo
->sendaddr
);
42 m_free(tcpinfo
->listenaddr
);
43 m_free(tcpinfo
->request_listenaddr
);
47 static void tcp_acceptor(struct Listener
*listener
, int sock
) {
50 struct sockaddr_storage addr
;
52 char ipstring
[NI_MAXHOST
], portstring
[NI_MAXSERV
];
53 struct TCPListener
*tcpinfo
= (struct TCPListener
*)(listener
->typedata
);
57 fd
= accept(sock
, (struct sockaddr
*)&addr
, &len
);
62 if (getnameinfo((struct sockaddr
*)&addr
, len
, ipstring
, sizeof(ipstring
),
63 portstring
, sizeof(portstring
),
64 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
69 if (send_msg_channel_open_init(fd
, tcpinfo
->chantype
) == DROPBEAR_SUCCESS
) {
70 unsigned char* addr
= NULL
;
71 unsigned int port
= 0;
73 if (tcpinfo
->tcp_type
== direct
) {
75 /* host to connect, port to connect */
76 addr
= tcpinfo
->sendaddr
;
77 port
= tcpinfo
->sendport
;
79 dropbear_assert(tcpinfo
->tcp_type
== forwarded
);
80 /* "forwarded-tcpip" */
81 /* address that was connected, port that was connected */
82 addr
= tcpinfo
->request_listenaddr
;
83 port
= tcpinfo
->listenport
;
89 buf_putstring(ses
.writepayload
, addr
, strlen(addr
));
90 buf_putint(ses
.writepayload
, port
);
93 buf_putstring(ses
.writepayload
, ipstring
, strlen(ipstring
));
95 buf_putint(ses
.writepayload
, atol(portstring
));
105 int listen_tcpfwd(struct TCPListener
* tcpinfo
) {
107 char portstring
[NI_MAXSERV
];
108 int socks
[DROPBEAR_MAX_SOCKS
];
109 struct Listener
*listener
= NULL
;
111 char* errstring
= NULL
;
113 TRACE(("enter listen_tcpfwd"))
115 /* first we try to bind, so don't need to do so much cleanup on failure */
116 snprintf(portstring
, sizeof(portstring
), "%d", tcpinfo
->listenport
);
118 nsocks
= dropbear_listen(tcpinfo
->listenaddr
, portstring
, socks
,
119 DROPBEAR_MAX_SOCKS
, &errstring
, &ses
.maxfd
);
121 dropbear_log(LOG_INFO
, "TCP forward failed: %s", errstring
);
123 TRACE(("leave listen_tcpfwd: dropbear_listen failed"))
124 return DROPBEAR_FAILURE
;
128 /* new_listener will close the socks if it fails */
129 listener
= new_listener(socks
, nsocks
, CHANNEL_ID_TCPFORWARDED
, tcpinfo
,
130 tcp_acceptor
, cleanup_tcp
);
132 if (listener
== NULL
) {
133 TRACE(("leave listen_tcpfwd: listener failed"))
134 return DROPBEAR_FAILURE
;
137 TRACE(("leave listen_tcpfwd: success"))
138 return DROPBEAR_SUCCESS
;
141 #endif /* DROPBEAR_TCP_ACCEPT */