remove unused variable
[dropbear.git] / tcp-accept.c
blobcf0aa7a6c0444ab64212f9f4c792495dfcda364c
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 "runopts.h"
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);
46 static void tcp_acceptor(struct Listener *listener, int sock) {
48 int fd;
49 struct sockaddr_storage addr;
50 socklen_t len;
51 char ipstring[NI_MAXHOST], portstring[NI_MAXSERV];
52 struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata);
54 len = sizeof(addr);
56 fd = accept(sock, (struct sockaddr*)&addr, &len);
57 if (fd < 0) {
58 return;
61 if (getnameinfo((struct sockaddr*)&addr, len, ipstring, sizeof(ipstring),
62 portstring, sizeof(portstring),
63 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
64 return;
67 if (send_msg_channel_open_init(fd, tcpinfo->chantype) == DROPBEAR_SUCCESS) {
68 unsigned char* addr = NULL;
69 unsigned int port = 0;
71 if (tcpinfo->tcp_type == direct) {
72 /* "direct-tcpip" */
73 /* host to connect, port to connect */
74 addr = tcpinfo->sendaddr;
75 port = tcpinfo->sendport;
76 } else {
77 dropbear_assert(tcpinfo->tcp_type == forwarded);
78 /* "forwarded-tcpip" */
79 /* address that was connected, port that was connected */
80 addr = tcpinfo->listenaddr;
81 port = tcpinfo->listenport;
84 buf_putstring(ses.writepayload, addr, strlen(addr));
85 buf_putint(ses.writepayload, port);
87 /* originator ip */
88 buf_putstring(ses.writepayload, ipstring, strlen(ipstring));
89 /* originator port */
90 buf_putint(ses.writepayload, atol(portstring));
92 encrypt_packet();
94 } else {
95 /* XXX debug? */
96 close(fd);
100 int listen_tcpfwd(struct TCPListener* tcpinfo) {
102 char portstring[NI_MAXSERV];
103 int socks[DROPBEAR_MAX_SOCKS];
104 struct Listener *listener = NULL;
105 int nsocks;
106 char* errstring = NULL;
108 TRACE(("enter listen_tcpfwd"))
110 /* first we try to bind, so don't need to do so much cleanup on failure */
111 snprintf(portstring, sizeof(portstring), "%d", tcpinfo->listenport);
113 nsocks = dropbear_listen(tcpinfo->listenaddr, portstring, socks,
114 DROPBEAR_MAX_SOCKS, &errstring, &ses.maxfd);
115 if (nsocks < 0) {
116 dropbear_log(LOG_INFO, "TCP forward failed: %s", errstring);
117 m_free(errstring);
118 TRACE(("leave listen_tcpfwd: dropbear_listen failed"))
119 return DROPBEAR_FAILURE;
121 m_free(errstring);
123 /* new_listener will close the socks if it fails */
124 listener = new_listener(socks, nsocks, CHANNEL_ID_TCPFORWARDED, tcpinfo,
125 tcp_acceptor, cleanup_tcp);
127 if (listener == NULL) {
128 TRACE(("leave listen_tcpfwd: listener failed"))
129 return DROPBEAR_FAILURE;
132 TRACE(("leave listen_tcpfwd: success"))
133 return DROPBEAR_SUCCESS;
136 #endif /* DROPBEAR_TCP_ACCEPT */