Mercurial > dropbear
annotate netio.c @ 1078:b3b772646974
Should be AF_UNSPEC not PF_UNSPEC
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 17 Apr 2015 20:59:32 +0800 |
parents | d92597ef089e |
children | 2265d7ebfdeb c45d65392c1a |
rev | line source |
---|---|
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1 #include "netio.h" |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
2 #include "list.h" |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
3 #include "dbutil.h" |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
4 #include "session.h" |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
5 #include "debug.h" |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
6 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
7 struct dropbear_progress_connection { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
8 struct addrinfo *res; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
9 struct addrinfo *res_iter; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
10 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
11 char *remotehost, *remoteport; /* For error reporting */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
12 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
13 connect_callback cb; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
14 void *cb_data; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
15 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
16 struct Queue *writequeue; /* A queue of encrypted packets to send with TCP fastopen, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
17 or NULL. */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
18 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
19 int sock; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
20 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
21 char* errstring; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
22 }; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
23 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
24 #if defined(__linux__) && defined(TCP_DEFER_ACCEPT) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
25 static void set_piggyback_ack(int sock) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
26 /* Undocumented Linux feature - set TCP_DEFER_ACCEPT and data will be piggybacked |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
27 on the 3rd packet (ack) of the TCP handshake. Saves a IP packet. |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
28 http://thread.gmane.org/gmane.linux.network/224627/focus=224727 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
29 "Piggyback the final ACK of the three way TCP connection establishment with the data" */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
30 int val = 1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
31 /* No error checking, this is opportunistic */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
32 int err = setsockopt(sock, IPPROTO_TCP, TCP_DEFER_ACCEPT, (void*)&val, sizeof(val)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
33 if (err) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
34 { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
35 TRACE(("Failed setsockopt TCP_DEFER_ACCEPT: %s", strerror(errno))) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
36 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
37 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
38 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
39 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
40 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
41 /* Deallocate a progress connection. Removes from the pending list if iter!=NULL. |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
42 Does not close sockets */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
43 static void remove_connect(struct dropbear_progress_connection *c, m_list_elem *iter) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
44 if (c->res) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
45 freeaddrinfo(c->res); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
46 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
47 m_free(c->remotehost); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
48 m_free(c->remoteport); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
49 m_free(c->errstring); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
50 m_free(c); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
51 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
52 if (iter) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
53 list_remove(iter); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
54 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
55 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
56 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
57 static void cancel_callback(int result, int sock, void* UNUSED(data), const char* UNUSED(errstring)) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
58 if (result == DROPBEAR_SUCCESS) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
59 { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
60 m_close(sock); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
61 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
62 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
63 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
64 void cancel_connect(struct dropbear_progress_connection *c) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
65 c->cb = cancel_callback; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
66 c->cb_data = NULL; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
67 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
68 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
69 static void connect_try_next(struct dropbear_progress_connection *c) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
70 struct addrinfo *r; |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
71 int res = 0; |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
72 int fastopen = 0; |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
73 #ifdef DROPBEAR_TCP_FAST_OPEN |
1035
8ed1d37e75a6
get rid of some unnecessary code
Matt Johnston <matt@ucc.asn.au>
parents:
1034
diff
changeset
|
74 struct msghdr message; |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
75 #endif |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
76 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
77 for (r = c->res_iter; r; r = r->ai_next) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
78 { |
1067
ce21d0bfaf98
dropbear_assert() rather than assert()
Matt Johnston <matt@ucc.asn.au>
parents:
1066
diff
changeset
|
79 dropbear_assert(c->sock == -1); |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
80 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
81 c->sock = socket(c->res_iter->ai_family, c->res_iter->ai_socktype, c->res_iter->ai_protocol); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
82 if (c->sock < 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
83 continue; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
84 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
85 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
86 ses.maxfd = MAX(ses.maxfd, c->sock); |
1035
8ed1d37e75a6
get rid of some unnecessary code
Matt Johnston <matt@ucc.asn.au>
parents:
1034
diff
changeset
|
87 set_sock_nodelay(c->sock); |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
88 setnonblocking(c->sock); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
89 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
90 #if defined(__linux__) && defined(TCP_DEFER_ACCEPT) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
91 set_piggyback_ack(c->sock); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
92 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
93 |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
94 #ifdef DROPBEAR_TCP_FAST_OPEN |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
95 fastopen = (c->writequeue != NULL); |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
96 |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
97 memset(&message, 0x0, sizeof(message)); |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
98 message.msg_name = r->ai_addr; |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
99 message.msg_namelen = r->ai_addrlen; |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
100 |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
101 if (c->writequeue) { |
1072 | 102 /* 6 is arbitrary, enough to hold initial packets */ |
103 int iovlen = 6; /* Linux msg_iovlen is a size_t */ | |
1076
d92597ef089e
fastopen code was totally broken
Matt Johnston <matt@ucc.asn.au>
parents:
1073
diff
changeset
|
104 struct iovec iov[6]; |
1072 | 105 packet_queue_to_iovec(c->writequeue, iov, &iovlen); |
1076
d92597ef089e
fastopen code was totally broken
Matt Johnston <matt@ucc.asn.au>
parents:
1073
diff
changeset
|
106 message.msg_iov = iov; |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
107 message.msg_iovlen = iovlen; |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
108 res = sendmsg(c->sock, &message, MSG_FASTOPEN); |
1048
9cec9ed503bb
EINPROGRESS for sendmsg() means it's working OK
Matt Johnston <matt@ucc.asn.au>
parents:
1047
diff
changeset
|
109 if (res < 0 && errno != EINPROGRESS) { |
1065
23103e1e9548
Fix error handling for dbclient async connect
Matt Johnston <matt@ucc.asn.au>
parents:
1053
diff
changeset
|
110 m_free(c->errstring); |
23103e1e9548
Fix error handling for dbclient async connect
Matt Johnston <matt@ucc.asn.au>
parents:
1053
diff
changeset
|
111 c->errstring = m_strdup(strerror(errno)); |
1047
e6c4cd0510d9
ignore any sendmsg() errors
Matt Johnston <matt@ucc.asn.au>
parents:
1035
diff
changeset
|
112 /* Not entirely sure which kind of errors are normal - 2.6.32 seems to |
e6c4cd0510d9
ignore any sendmsg() errors
Matt Johnston <matt@ucc.asn.au>
parents:
1035
diff
changeset
|
113 return EPIPE for any (nonblocking?) sendmsg(). just fall back */ |
e6c4cd0510d9
ignore any sendmsg() errors
Matt Johnston <matt@ucc.asn.au>
parents:
1035
diff
changeset
|
114 TRACE(("sendmsg tcp_fastopen failed, falling back. %s", strerror(errno))); |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
115 /* No kernel MSG_FASTOPEN support. Fall back below */ |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
116 fastopen = 0; |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
117 /* Set to NULL to avoid trying again */ |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
118 c->writequeue = NULL; |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
119 } |
1035
8ed1d37e75a6
get rid of some unnecessary code
Matt Johnston <matt@ucc.asn.au>
parents:
1034
diff
changeset
|
120 packet_queue_consume(c->writequeue, res); |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
121 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
122 #endif |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
123 |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
124 /* Normal connect(), used as fallback for TCP fastopen too */ |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
125 if (!fastopen) { |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
126 res = connect(c->sock, r->ai_addr, r->ai_addrlen); |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
127 } |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
128 |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
129 if (res < 0 && errno != EINPROGRESS) { |
1035
8ed1d37e75a6
get rid of some unnecessary code
Matt Johnston <matt@ucc.asn.au>
parents:
1034
diff
changeset
|
130 /* failure */ |
1065
23103e1e9548
Fix error handling for dbclient async connect
Matt Johnston <matt@ucc.asn.au>
parents:
1053
diff
changeset
|
131 m_free(c->errstring); |
23103e1e9548
Fix error handling for dbclient async connect
Matt Johnston <matt@ucc.asn.au>
parents:
1053
diff
changeset
|
132 c->errstring = m_strdup(strerror(errno)); |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
133 close(c->sock); |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
134 c->sock = -1; |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
135 continue; |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
136 } else { |
1035
8ed1d37e75a6
get rid of some unnecessary code
Matt Johnston <matt@ucc.asn.au>
parents:
1034
diff
changeset
|
137 /* new connection was successful, wait for it to complete */ |
1033
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
138 break; |
ca71904cf3ee
Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents:
1032
diff
changeset
|
139 } |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
140 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
141 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
142 if (r) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
143 c->res_iter = r->ai_next; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
144 } else { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
145 c->res_iter = NULL; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
146 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
147 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
148 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
149 /* Connect via TCP to a host. */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
150 struct dropbear_progress_connection *connect_remote(const char* remotehost, const char* remoteport, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
151 connect_callback cb, void* cb_data) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
152 { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
153 struct dropbear_progress_connection *c = NULL; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
154 int err; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
155 struct addrinfo hints; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
156 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
157 c = m_malloc(sizeof(*c)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
158 c->remotehost = m_strdup(remotehost); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
159 c->remoteport = m_strdup(remoteport); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
160 c->sock = -1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
161 c->cb = cb; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
162 c->cb_data = cb_data; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
163 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
164 list_append(&ses.conn_pending, c); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
165 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
166 memset(&hints, 0, sizeof(hints)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
167 hints.ai_socktype = SOCK_STREAM; |
1078
b3b772646974
Should be AF_UNSPEC not PF_UNSPEC
Matt Johnston <matt@ucc.asn.au>
parents:
1076
diff
changeset
|
168 hints.ai_family = AF_UNSPEC; |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
169 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
170 err = getaddrinfo(remotehost, remoteport, &hints, &c->res); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
171 if (err) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
172 int len; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
173 len = 100 + strlen(gai_strerror(err)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
174 c->errstring = (char*)m_malloc(len); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
175 snprintf(c->errstring, len, "Error resolving '%s' port '%s'. %s", |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
176 remotehost, remoteport, gai_strerror(err)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
177 TRACE(("Error resolving: %s", gai_strerror(err))) |
1053 | 178 } else { |
179 c->res_iter = c->res; | |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
180 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
181 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
182 return c; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
183 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
184 |
1050 | 185 void remove_connect_pending() { |
186 while (ses.conn_pending.first) { | |
187 struct dropbear_progress_connection *c = ses.conn_pending.first->item; | |
188 remove_connect(c, ses.conn_pending.first); | |
189 } | |
190 } | |
191 | |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
192 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
193 void set_connect_fds(fd_set *writefd) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
194 m_list_elem *iter; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
195 TRACE(("enter handle_connect_fds")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
196 for (iter = ses.conn_pending.first; iter; iter = iter->next) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
197 struct dropbear_progress_connection *c = iter->item; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
198 /* Set one going */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
199 while (c->res_iter && c->sock < 0) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
200 { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
201 connect_try_next(c); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
202 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
203 if (c->sock >= 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
204 FD_SET(c->sock, writefd); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
205 } else { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
206 m_list_elem *remove_iter; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
207 /* Final failure */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
208 if (!c->errstring) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
209 c->errstring = m_strdup("unexpected failure"); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
210 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
211 c->cb(DROPBEAR_FAILURE, -1, c->cb_data, c->errstring); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
212 /* Safely remove without invalidating iter */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
213 remove_iter = iter; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
214 iter = iter->prev; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
215 remove_connect(c, remove_iter); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
216 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
217 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
218 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
219 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
220 void handle_connect_fds(fd_set *writefd) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
221 m_list_elem *iter; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
222 TRACE(("enter handle_connect_fds")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
223 for (iter = ses.conn_pending.first; iter; iter = iter->next) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
224 int val; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
225 socklen_t vallen = sizeof(val); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
226 struct dropbear_progress_connection *c = iter->item; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
227 |
1053 | 228 if (c->sock < 0 || !FD_ISSET(c->sock, writefd)) { |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
229 continue; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
230 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
231 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
232 TRACE(("handling %s port %s socket %d", c->remotehost, c->remoteport, c->sock)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
233 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
234 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &val, &vallen) != 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
235 TRACE(("handle_connect_fds getsockopt(%d) SO_ERROR failed: %s", c->sock, strerror(errno))) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
236 /* This isn't expected to happen - Unix has surprises though, continue gracefully. */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
237 m_close(c->sock); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
238 c->sock = -1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
239 } else if (val != 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
240 /* Connect failed */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
241 TRACE(("connect to %s port %s failed.", c->remotehost, c->remoteport)) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
242 m_close(c->sock); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
243 c->sock = -1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
244 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
245 m_free(c->errstring); |
1066 | 246 c->errstring = m_strdup(strerror(val)); |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
247 } else { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
248 /* New connection has been established */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
249 c->cb(DROPBEAR_SUCCESS, c->sock, c->cb_data, NULL); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
250 remove_connect(c, iter); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
251 TRACE(("leave handle_connect_fds - success")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
252 /* Must return here - remove_connect() invalidates iter */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
253 return; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
254 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
255 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
256 TRACE(("leave handle_connect_fds - end iter")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
257 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
258 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
259 void connect_set_writequeue(struct dropbear_progress_connection *c, struct Queue *writequeue) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
260 c->writequeue = writequeue; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
261 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
262 |
1072 | 263 void packet_queue_to_iovec(struct Queue *queue, struct iovec *iov, unsigned int *iov_count) { |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
264 struct Link *l; |
1034 | 265 unsigned int i; |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
266 int len; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
267 buffer *writebuf; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
268 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
269 #ifndef IOV_MAX |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
270 #define IOV_MAX UIO_MAXIOV |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
271 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
272 |
1072 | 273 *iov_count = MIN(MIN(queue->count, IOV_MAX), *iov_count); |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
274 |
1073
88043f9d40bd
Fix when iov queue is large
Matt Johnston <matt@ucc.asn.au>
parents:
1072
diff
changeset
|
275 for (l = queue->head, i = 0; i < *iov_count; l = l->link, i++) |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
276 { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
277 writebuf = (buffer*)l->item; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
278 len = writebuf->len - 1 - writebuf->pos; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
279 dropbear_assert(len > 0); |
1034 | 280 TRACE2(("write_packet writev #%d type %d len %d/%d", i, writebuf->data[writebuf->len-1], |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
281 len, writebuf->len-1)) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
282 iov[i].iov_base = buf_getptr(writebuf, len); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
283 iov[i].iov_len = len; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
284 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
285 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
286 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
287 void packet_queue_consume(struct Queue *queue, ssize_t written) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
288 buffer *writebuf; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
289 int len; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
290 while (written > 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
291 writebuf = (buffer*)examine(queue); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
292 len = writebuf->len - 1 - writebuf->pos; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
293 if (len > written) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
294 /* partial buffer write */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
295 buf_incrpos(writebuf, written); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
296 written = 0; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
297 } else { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
298 written -= len; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
299 dequeue(queue); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
300 buf_free(writebuf); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
301 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
302 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
303 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
304 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
305 void set_sock_nodelay(int sock) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
306 int val; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
307 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
308 /* disable nagle */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
309 val = 1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
310 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&val, sizeof(val)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
311 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
312 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
313 #ifdef DROPBEAR_TCP_FAST_OPEN |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
314 void set_listen_fast_open(int sock) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
315 int qlen = MAX(MAX_UNAUTH_PER_IP, 5); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
316 if (setsockopt(sock, SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) != 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
317 TRACE(("set_listen_fast_open failed for socket %d: %s", sock, strerror(errno))) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
318 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
319 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
320 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
321 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
322 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
323 void set_sock_priority(int sock, enum dropbear_prio prio) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
324 |
1034 | 325 int rc; |
326 #ifdef IPTOS_LOWDELAY | |
327 int iptos_val = 0; | |
328 #endif | |
329 #ifdef SO_PRIORITY | |
330 int so_prio_val = 0; | |
331 #endif | |
332 | |
1032
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
333 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
334 /* Don't log ENOTSOCK errors so that this can harmlessly be called |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
335 * on a client '-J' proxy pipe */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
336 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
337 /* set the TOS bit for either ipv4 or ipv6 */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
338 #ifdef IPTOS_LOWDELAY |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
339 if (prio == DROPBEAR_PRIO_LOWDELAY) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
340 iptos_val = IPTOS_LOWDELAY; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
341 } else if (prio == DROPBEAR_PRIO_BULK) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
342 iptos_val = IPTOS_THROUGHPUT; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
343 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
344 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
345 rc = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, (void*)&iptos_val, sizeof(iptos_val)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
346 if (rc < 0 && errno != ENOTSOCK) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
347 TRACE(("Couldn't set IPV6_TCLASS (%s)", strerror(errno))); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
348 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
349 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
350 rc = setsockopt(sock, IPPROTO_IP, IP_TOS, (void*)&iptos_val, sizeof(iptos_val)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
351 if (rc < 0 && errno != ENOTSOCK) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
352 TRACE(("Couldn't set IP_TOS (%s)", strerror(errno))); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
353 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
354 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
355 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
356 #ifdef SO_PRIORITY |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
357 if (prio == DROPBEAR_PRIO_LOWDELAY) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
358 so_prio_val = TC_PRIO_INTERACTIVE; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
359 } else if (prio == DROPBEAR_PRIO_BULK) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
360 so_prio_val = TC_PRIO_BULK; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
361 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
362 /* linux specific, sets QoS class. see tc-prio(8) */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
363 rc = setsockopt(sock, SOL_SOCKET, SO_PRIORITY, (void*) &so_prio_val, sizeof(so_prio_val)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
364 if (rc < 0 && errno != ENOTSOCK) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
365 dropbear_log(LOG_WARNING, "Couldn't set SO_PRIORITY (%s)", |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
366 strerror(errno)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
367 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
368 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
369 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
370 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
371 /* Listen on address:port. |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
372 * Special cases are address of "" listening on everything, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
373 * and address of NULL listening on localhost only. |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
374 * Returns the number of sockets bound on success, or -1 on failure. On |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
375 * failure, if errstring wasn't NULL, it'll be a newly malloced error |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
376 * string.*/ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
377 int dropbear_listen(const char* address, const char* port, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
378 int *socks, unsigned int sockcount, char **errstring, int *maxfd) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
379 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
380 struct addrinfo hints, *res = NULL, *res0 = NULL; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
381 int err; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
382 unsigned int nsock; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
383 struct linger linger; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
384 int val; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
385 int sock; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
386 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
387 TRACE(("enter dropbear_listen")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
388 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
389 memset(&hints, 0, sizeof(hints)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
390 hints.ai_family = AF_UNSPEC; /* TODO: let them flag v4 only etc */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
391 hints.ai_socktype = SOCK_STREAM; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
392 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
393 /* for calling getaddrinfo: |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
394 address == NULL and !AI_PASSIVE: local loopback |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
395 address == NULL and AI_PASSIVE: all interfaces |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
396 address != NULL: whatever the address says */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
397 if (!address) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
398 TRACE(("dropbear_listen: local loopback")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
399 } else { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
400 if (address[0] == '\0') { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
401 TRACE(("dropbear_listen: all interfaces")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
402 address = NULL; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
403 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
404 hints.ai_flags = AI_PASSIVE; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
405 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
406 err = getaddrinfo(address, port, &hints, &res0); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
407 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
408 if (err) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
409 if (errstring != NULL && *errstring == NULL) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
410 int len; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
411 len = 20 + strlen(gai_strerror(err)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
412 *errstring = (char*)m_malloc(len); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
413 snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
414 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
415 if (res0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
416 freeaddrinfo(res0); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
417 res0 = NULL; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
418 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
419 TRACE(("leave dropbear_listen: failed resolving")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
420 return -1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
421 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
422 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
423 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
424 nsock = 0; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
425 for (res = res0; res != NULL && nsock < sockcount; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
426 res = res->ai_next) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
427 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
428 /* Get a socket */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
429 socks[nsock] = socket(res->ai_family, res->ai_socktype, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
430 res->ai_protocol); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
431 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
432 sock = socks[nsock]; /* For clarity */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
433 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
434 if (sock < 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
435 err = errno; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
436 TRACE(("socket() failed")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
437 continue; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
438 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
439 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
440 /* Various useful socket options */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
441 val = 1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
442 /* set to reuse, quick timeout */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
443 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &val, sizeof(val)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
444 linger.l_onoff = 1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
445 linger.l_linger = 5; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
446 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&linger, sizeof(linger)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
447 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
448 #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
449 if (res->ai_family == AF_INET6) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
450 int on = 1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
451 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
452 &on, sizeof(on)) == -1) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
453 dropbear_log(LOG_WARNING, "Couldn't set IPV6_V6ONLY"); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
454 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
455 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
456 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
457 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
458 set_sock_nodelay(sock); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
459 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
460 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
461 err = errno; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
462 close(sock); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
463 TRACE(("bind(%s) failed", port)) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
464 continue; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
465 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
466 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
467 if (listen(sock, DROPBEAR_LISTEN_BACKLOG) < 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
468 err = errno; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
469 close(sock); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
470 TRACE(("listen() failed")) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
471 continue; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
472 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
473 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
474 *maxfd = MAX(*maxfd, sock); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
475 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
476 nsock++; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
477 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
478 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
479 if (res0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
480 freeaddrinfo(res0); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
481 res0 = NULL; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
482 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
483 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
484 if (nsock == 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
485 if (errstring != NULL && *errstring == NULL) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
486 int len; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
487 len = 20 + strlen(strerror(err)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
488 *errstring = (char*)m_malloc(len); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
489 snprintf(*errstring, len, "Error listening: %s", strerror(err)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
490 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
491 TRACE(("leave dropbear_listen: failure, %s", strerror(err))) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
492 return -1; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
493 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
494 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
495 TRACE(("leave dropbear_listen: success, %d socks bound", nsock)) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
496 return nsock; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
497 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
498 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
499 void get_socket_address(int fd, char **local_host, char **local_port, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
500 char **remote_host, char **remote_port, int host_lookup) |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
501 { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
502 struct sockaddr_storage addr; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
503 socklen_t addrlen; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
504 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
505 if (local_host || local_port) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
506 addrlen = sizeof(addr); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
507 if (getsockname(fd, (struct sockaddr*)&addr, &addrlen) < 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
508 dropbear_exit("Failed socket address: %s", strerror(errno)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
509 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
510 getaddrstring(&addr, local_host, local_port, host_lookup); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
511 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
512 if (remote_host || remote_port) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
513 addrlen = sizeof(addr); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
514 if (getpeername(fd, (struct sockaddr*)&addr, &addrlen) < 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
515 dropbear_exit("Failed socket address: %s", strerror(errno)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
516 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
517 getaddrstring(&addr, remote_host, remote_port, host_lookup); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
518 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
519 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
520 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
521 /* Return a string representation of the socket address passed. The return |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
522 * value is allocated with malloc() */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
523 void getaddrstring(struct sockaddr_storage* addr, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
524 char **ret_host, char **ret_port, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
525 int host_lookup) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
526 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
527 char host[NI_MAXHOST+1], serv[NI_MAXSERV+1]; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
528 unsigned int len; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
529 int ret; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
530 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
531 int flags = NI_NUMERICSERV | NI_NUMERICHOST; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
532 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
533 #ifndef DO_HOST_LOOKUP |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
534 host_lookup = 0; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
535 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
536 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
537 if (host_lookup) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
538 flags = NI_NUMERICSERV; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
539 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
540 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
541 len = sizeof(struct sockaddr_storage); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
542 /* Some platforms such as Solaris 8 require that len is the length |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
543 * of the specific structure. Some older linux systems (glibc 2.1.3 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
544 * such as debian potato) have sockaddr_storage.__ss_family instead |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
545 * but we'll ignore them */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
546 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
547 if (addr->ss_family == AF_INET) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
548 len = sizeof(struct sockaddr_in); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
549 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
550 #ifdef AF_INET6 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
551 if (addr->ss_family == AF_INET6) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
552 len = sizeof(struct sockaddr_in6); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
553 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
554 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
555 #endif |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
556 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
557 ret = getnameinfo((struct sockaddr*)addr, len, host, sizeof(host)-1, |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
558 serv, sizeof(serv)-1, flags); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
559 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
560 if (ret != 0) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
561 if (host_lookup) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
562 /* On some systems (Darwin does it) we get EINTR from getnameinfo |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
563 * somehow. Eew. So we'll just return the IP, since that doesn't seem |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
564 * to exhibit that behaviour. */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
565 getaddrstring(addr, ret_host, ret_port, 0); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
566 return; |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
567 } else { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
568 /* if we can't do a numeric lookup, something's gone terribly wrong */ |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
569 dropbear_exit("Failed lookup: %s", gai_strerror(ret)); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
570 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
571 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
572 |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
573 if (ret_host) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
574 *ret_host = m_strdup(host); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
575 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
576 if (ret_port) { |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
577 *ret_port = m_strdup(serv); |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
578 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
579 } |
0da8ba489c23
Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
580 |