Mercurial > dropbear
annotate 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 |
rev | line source |
---|---|
62 | 1 #include "includes.h" |
2 #include "ssh.h" | |
3 #include "tcp-accept.h" | |
4 #include "dbutil.h" | |
5 #include "session.h" | |
6 #include "buffer.h" | |
7 #include "packet.h" | |
8 #include "listener.h" | |
9 #include "runopts.h" | |
10 | |
11 #ifndef DISABLE_TCP_ACCEPT | |
12 | |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
13 |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
14 static void cleanup_tcp(struct Listener *listener) { |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
15 |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
16 struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata); |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
17 |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
18 m_free(tcpinfo->sendaddr); |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
19 m_free(tcpinfo); |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
20 } |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
21 |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
22 static void tcp_acceptor(struct Listener *listener, int sock) { |
62 | 23 |
24 int fd; | |
25 struct sockaddr_storage addr; | |
26 int len; | |
27 char ipstring[NI_MAXHOST], portstring[NI_MAXSERV]; | |
28 struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata); | |
29 | |
30 len = sizeof(addr); | |
31 | |
32 fd = accept(sock, (struct sockaddr*)&addr, &len); | |
33 if (fd < 0) { | |
34 return; | |
35 } | |
36 | |
37 if (getnameinfo((struct sockaddr*)&addr, len, ipstring, sizeof(ipstring), | |
38 portstring, sizeof(portstring), | |
39 NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | |
40 return; | |
41 } | |
42 | |
43 if (send_msg_channel_open_init(fd, tcpinfo->chantype) == DROPBEAR_SUCCESS) { | |
44 | |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
45 buf_putstring(ses.writepayload, tcpinfo->sendaddr, |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
46 strlen(tcpinfo->sendaddr)); |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
47 buf_putint(ses.writepayload, tcpinfo->sendport); |
62 | 48 buf_putstring(ses.writepayload, ipstring, strlen(ipstring)); |
49 buf_putint(ses.writepayload, atol(portstring)); | |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
50 |
62 | 51 encrypt_packet(); |
52 | |
53 } else { | |
54 /* XXX debug? */ | |
55 close(fd); | |
56 } | |
57 } | |
58 | |
59 int listen_tcpfwd(struct TCPListener* tcpinfo) { | |
60 | |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
61 char portstring[NI_MAXSERV]; |
62 | 62 int socks[DROPBEAR_MAX_SOCKS]; |
63 struct Listener *listener = NULL; | |
64 int nsocks; | |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
65 char* errstring = NULL; |
62 | 66 |
67 TRACE(("enter listen_tcpfwd")); | |
68 | |
69 /* first we try to bind, so don't need to do so much cleanup on failure */ | |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
70 snprintf(portstring, sizeof(portstring), "%d", tcpinfo->sendport); |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
71 |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
72 /* XXX Note: we're just listening on localhost, no matter what they tell |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
73 * us. If someone wants to make it listen otherways, then change |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
74 * the "" argument. but that requires UI changes too */ |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
75 nsocks = dropbear_listen("", portstring, socks, |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
76 DROPBEAR_MAX_SOCKS, &errstring, &ses.maxfd); |
62 | 77 if (nsocks < 0) { |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
78 dropbear_log(LOG_INFO, "TCP forward failed: %s", errstring); |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
79 m_free(errstring); |
62 | 80 TRACE(("leave listen_tcpfwd: dropbear_listen failed")); |
81 return DROPBEAR_FAILURE; | |
82 } | |
83 | |
84 listener = new_listener(socks, nsocks, CHANNEL_ID_TCPFORWARDED, tcpinfo, | |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
85 tcp_acceptor, cleanup_tcp); |
62 | 86 |
87 if (listener == NULL) { | |
88 m_free(tcpinfo); | |
89 TRACE(("leave listen_tcpfwd: listener failed")); | |
90 return DROPBEAR_FAILURE; | |
91 } | |
92 | |
93 TRACE(("leave listen_tcpfwd: success")); | |
94 return DROPBEAR_SUCCESS; | |
95 } | |
96 | |
97 #endif /* DISABLE_REMOTETCPFWD */ |