comparison tcp-accept.c @ 63:dcc43965928f

- A nice cleaner structure for tcp (acceptor) forwarding. - still a checkpoint-ish commit - sorted out listening on localhost only
author Matt Johnston <matt@ucc.asn.au>
date Wed, 11 Aug 2004 17:26:47 +0000
parents 20563735e8b5
children efb5e0b335cf
comparison
equal deleted inserted replaced
62:20563735e8b5 63:dcc43965928f
8 #include "listener.h" 8 #include "listener.h"
9 #include "runopts.h" 9 #include "runopts.h"
10 10
11 #ifndef DISABLE_TCP_ACCEPT 11 #ifndef DISABLE_TCP_ACCEPT
12 12
13 static void accept_tcp(struct Listener *listener, int sock) { 13
14 static void cleanup_tcp(struct Listener *listener) {
15
16 struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata);
17
18 m_free(tcpinfo->sendaddr);
19 m_free(tcpinfo);
20 }
21
22 static void tcp_acceptor(struct Listener *listener, int sock) {
14 23
15 int fd; 24 int fd;
16 struct sockaddr_storage addr; 25 struct sockaddr_storage addr;
17 int len; 26 int len;
18 char ipstring[NI_MAXHOST], portstring[NI_MAXSERV]; 27 char ipstring[NI_MAXHOST], portstring[NI_MAXSERV];
31 return; 40 return;
32 } 41 }
33 42
34 if (send_msg_channel_open_init(fd, tcpinfo->chantype) == DROPBEAR_SUCCESS) { 43 if (send_msg_channel_open_init(fd, tcpinfo->chantype) == DROPBEAR_SUCCESS) {
35 44
36 buf_putstring(ses.writepayload, tcpinfo->addr, strlen(tcpinfo->addr)); 45 buf_putstring(ses.writepayload, tcpinfo->sendaddr,
37 buf_putint(ses.writepayload, tcpinfo->port); 46 strlen(tcpinfo->sendaddr));
47 buf_putint(ses.writepayload, tcpinfo->sendport);
38 buf_putstring(ses.writepayload, ipstring, strlen(ipstring)); 48 buf_putstring(ses.writepayload, ipstring, strlen(ipstring));
39 buf_putint(ses.writepayload, atol(portstring)); 49 buf_putint(ses.writepayload, atol(portstring));
50
40 encrypt_packet(); 51 encrypt_packet();
41 52
42 } else { 53 } else {
43 /* XXX debug? */ 54 /* XXX debug? */
44 close(fd); 55 close(fd);
45 } 56 }
46 } 57 }
47 58
48 static void cleanup_tcp(struct Listener *listener) {
49
50 struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata);
51
52 m_free(tcpinfo->addr);
53 m_free(tcpinfo);
54 }
55
56
57 int listen_tcpfwd(struct TCPListener* tcpinfo) { 59 int listen_tcpfwd(struct TCPListener* tcpinfo) {
58 60
59 char portstring[6]; /* "65535\0" */ 61 char portstring[NI_MAXSERV];
60 int socks[DROPBEAR_MAX_SOCKS]; 62 int socks[DROPBEAR_MAX_SOCKS];
61 struct Listener *listener = NULL; 63 struct Listener *listener = NULL;
62 int nsocks; 64 int nsocks;
65 char* errstring = NULL;
63 66
64 TRACE(("enter listen_tcpfwd")); 67 TRACE(("enter listen_tcpfwd"));
65 68
66 /* first we try to bind, so don't need to do so much cleanup on failure */ 69 /* first we try to bind, so don't need to do so much cleanup on failure */
67 snprintf(portstring, sizeof(portstring), "%d", tcpinfo->port); 70 snprintf(portstring, sizeof(portstring), "%d", tcpinfo->sendport);
68 nsocks = dropbear_listen(tcpinfo->addr, portstring, socks, 71
69 DROPBEAR_MAX_SOCKS, NULL, &ses.maxfd); 72 /* XXX Note: we're just listening on localhost, no matter what they tell
73 * us. If someone wants to make it listen otherways, then change
74 * the "" argument. but that requires UI changes too */
75 nsocks = dropbear_listen("", portstring, socks,
76 DROPBEAR_MAX_SOCKS, &errstring, &ses.maxfd);
70 if (nsocks < 0) { 77 if (nsocks < 0) {
78 dropbear_log(LOG_INFO, "TCP forward failed: %s", errstring);
79 m_free(errstring);
71 TRACE(("leave listen_tcpfwd: dropbear_listen failed")); 80 TRACE(("leave listen_tcpfwd: dropbear_listen failed"));
72 return DROPBEAR_FAILURE; 81 return DROPBEAR_FAILURE;
73 } 82 }
74 83
75 listener = new_listener(socks, nsocks, CHANNEL_ID_TCPFORWARDED, tcpinfo, 84 listener = new_listener(socks, nsocks, CHANNEL_ID_TCPFORWARDED, tcpinfo,
76 accept_tcp, cleanup_tcp); 85 tcp_acceptor, cleanup_tcp);
77 86
78 if (listener == NULL) { 87 if (listener == NULL) {
79 m_free(tcpinfo); 88 m_free(tcpinfo);
80 TRACE(("leave listen_tcpfwd: listener failed")); 89 TRACE(("leave listen_tcpfwd: listener failed"));
81 return DROPBEAR_FAILURE; 90 return DROPBEAR_FAILURE;