annotate netio.c @ 1385:6c92e97553f1 fuzz

Add a flag whether to longjmp, missed that last commit
author Matt Johnston <matt@ucc.asn.au>
date Thu, 01 Jun 2017 21:30:26 +0800
parents f03cfe9c76ac
children 69862e8cc405
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 /* 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
25 Does not close sockets */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 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
27 if (c->res) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 freeaddrinfo(c->res);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 m_free(c->remotehost);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 m_free(c->remoteport);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 m_free(c->errstring);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 m_free(c);
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 if (iter) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 list_remove(iter);
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 }
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 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
41 if (result == DROPBEAR_SUCCESS)
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 m_close(sock);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 }
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 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
48 c->cb = cancel_callback;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 c->cb_data = NULL;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 }
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 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
53 struct addrinfo *r;
1033
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
54 int res = 0;
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
55 int fastopen = 0;
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1152
diff changeset
56 #if DROPBEAR_CLIENT_TCP_FAST_OPEN
1035
8ed1d37e75a6 get rid of some unnecessary code
Matt Johnston <matt@ucc.asn.au>
parents: 1034
diff changeset
57 struct msghdr message;
1033
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
58 #endif
1032
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 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
61 {
1067
ce21d0bfaf98 dropbear_assert() rather than assert()
Matt Johnston <matt@ucc.asn.au>
parents: 1066
diff changeset
62 dropbear_assert(c->sock == -1);
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
1309
3b624ad2dc5e make sure socket is of the right domain
Matt Johnston <matt@ucc.asn.au>
parents: 1152
diff changeset
64 c->sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 if (c->sock < 0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 continue;
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 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
70 set_sock_nodelay(c->sock);
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 setnonblocking(c->sock);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1152
diff changeset
73 #if DROPBEAR_CLIENT_TCP_FAST_OPEN
1033
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
74 fastopen = (c->writequeue != NULL);
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
1084
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
76 if (fastopen) {
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
77 memset(&message, 0x0, sizeof(message));
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
78 message.msg_name = r->ai_addr;
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
79 message.msg_namelen = r->ai_addrlen;
1072
686cd3e8e13e avoid malloc for iovec
Matt Johnston <matt@ucc.asn.au>
parents: 1067
diff changeset
80 /* 6 is arbitrary, enough to hold initial packets */
1094
c45d65392c1a Fix pointer differ in signess warnings [-Werror=pointer-sign]
Gaël PORTAY <gael.portay@gmail.com>
parents: 1078
diff changeset
81 unsigned 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
82 struct iovec iov[6];
1072
686cd3e8e13e avoid malloc for iovec
Matt Johnston <matt@ucc.asn.au>
parents: 1067
diff changeset
83 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
84 message.msg_iov = iov;
1033
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
85 message.msg_iovlen = iovlen;
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
86 res = sendmsg(c->sock, &message, MSG_FASTOPEN);
1084
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
87 /* Returns EINPROGRESS if FASTOPEN wasn't available */
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
88 if (res < 0) {
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
89 if (errno != EINPROGRESS) {
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
90 m_free(c->errstring);
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
91 c->errstring = m_strdup(strerror(errno));
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
92 /* Not entirely sure which kind of errors are normal - 2.6.32 seems to
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
93 return EPIPE for any (nonblocking?) sendmsg(). just fall back */
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
94 TRACE(("sendmsg tcp_fastopen failed, falling back. %s", strerror(errno)));
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
95 /* No kernel MSG_FASTOPEN support. Fall back below */
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
96 fastopen = 0;
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
97 /* Set to NULL to avoid trying again */
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
98 c->writequeue = NULL;
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
99 }
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
100 } else {
2265d7ebfdeb separate client/server fastopen options
Matt Johnston <matt@ucc.asn.au>
parents: 1078
diff changeset
101 packet_queue_consume(c->writequeue, res);
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 #endif
1033
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
105
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
106 /* Normal connect(), used as fallback for TCP fastopen too */
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
107 if (!fastopen) {
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
108 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
109 }
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
110
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
111 if (res < 0 && errno != EINPROGRESS) {
1035
8ed1d37e75a6 get rid of some unnecessary code
Matt Johnston <matt@ucc.asn.au>
parents: 1034
diff changeset
112 /* failure */
1065
23103e1e9548 Fix error handling for dbclient async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1053
diff changeset
113 m_free(c->errstring);
23103e1e9548 Fix error handling for dbclient async connect
Matt Johnston <matt@ucc.asn.au>
parents: 1053
diff changeset
114 c->errstring = m_strdup(strerror(errno));
1033
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
115 close(c->sock);
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
116 c->sock = -1;
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
117 continue;
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
118 } else {
1035
8ed1d37e75a6 get rid of some unnecessary code
Matt Johnston <matt@ucc.asn.au>
parents: 1034
diff changeset
119 /* 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
120 break;
ca71904cf3ee Fixes for backwards compatibility
Matt Johnston <matt@ucc.asn.au>
parents: 1032
diff changeset
121 }
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 if (r) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 c->res_iter = r->ai_next;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 } else {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 c->res_iter = NULL;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 /* Connect via TCP to a host. */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 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
133 connect_callback cb, void* cb_data)
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 struct dropbear_progress_connection *c = NULL;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 int err;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 struct addrinfo hints;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 c = m_malloc(sizeof(*c));
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 c->remotehost = m_strdup(remotehost);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 c->remoteport = m_strdup(remoteport);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 c->sock = -1;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 c->cb = cb;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 c->cb_data = cb_data;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 list_append(&ses.conn_pending, c);
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 memset(&hints, 0, sizeof(hints));
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 hints.ai_socktype = SOCK_STREAM;
1078
b3b772646974 Should be AF_UNSPEC not PF_UNSPEC
Matt Johnston <matt@ucc.asn.au>
parents: 1076
diff changeset
150 hints.ai_family = AF_UNSPEC;
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 err = getaddrinfo(remotehost, remoteport, &hints, &c->res);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 if (err) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 int len;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 len = 100 + strlen(gai_strerror(err));
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 c->errstring = (char*)m_malloc(len);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 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
158 remotehost, remoteport, gai_strerror(err));
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 TRACE(("Error resolving: %s", gai_strerror(err)))
1053
fd3712d1ff7f Better failure handling
Matt Johnston <matt@ucc.asn.au>
parents: 1050
diff changeset
160 } else {
fd3712d1ff7f Better failure handling
Matt Johnston <matt@ucc.asn.au>
parents: 1050
diff changeset
161 c->res_iter = c->res;
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 }
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 return 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
1050
c2a50c9f509e Add cleanup
Matt Johnston <matt@ucc.asn.au>
parents: 1048
diff changeset
167 void remove_connect_pending() {
c2a50c9f509e Add cleanup
Matt Johnston <matt@ucc.asn.au>
parents: 1048
diff changeset
168 while (ses.conn_pending.first) {
c2a50c9f509e Add cleanup
Matt Johnston <matt@ucc.asn.au>
parents: 1048
diff changeset
169 struct dropbear_progress_connection *c = ses.conn_pending.first->item;
c2a50c9f509e Add cleanup
Matt Johnston <matt@ucc.asn.au>
parents: 1048
diff changeset
170 remove_connect(c, ses.conn_pending.first);
c2a50c9f509e Add cleanup
Matt Johnston <matt@ucc.asn.au>
parents: 1048
diff changeset
171 }
c2a50c9f509e Add cleanup
Matt Johnston <matt@ucc.asn.au>
parents: 1048
diff changeset
172 }
c2a50c9f509e Add cleanup
Matt Johnston <matt@ucc.asn.au>
parents: 1048
diff changeset
173
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 void set_connect_fds(fd_set *writefd) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 m_list_elem *iter;
1152
57d09741d46d avoid invalidated iterator when connection fails
Matt Johnston <matt@ucc.asn.au>
parents: 1136
diff changeset
177 TRACE(("enter set_connect_fds"))
57d09741d46d avoid invalidated iterator when connection fails
Matt Johnston <matt@ucc.asn.au>
parents: 1136
diff changeset
178 iter = ses.conn_pending.first;
57d09741d46d avoid invalidated iterator when connection fails
Matt Johnston <matt@ucc.asn.au>
parents: 1136
diff changeset
179 while (iter) {
57d09741d46d avoid invalidated iterator when connection fails
Matt Johnston <matt@ucc.asn.au>
parents: 1136
diff changeset
180 m_list_elem *next_iter = iter->next;
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 struct dropbear_progress_connection *c = iter->item;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 /* Set one going */
1152
57d09741d46d avoid invalidated iterator when connection fails
Matt Johnston <matt@ucc.asn.au>
parents: 1136
diff changeset
183 while (c->res_iter && c->sock < 0) {
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 connect_try_next(c);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 if (c->sock >= 0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 FD_SET(c->sock, writefd);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 } else {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 /* Final failure */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 if (!c->errstring) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 c->errstring = m_strdup("unexpected failure");
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 c->cb(DROPBEAR_FAILURE, -1, c->cb_data, c->errstring);
1152
57d09741d46d avoid invalidated iterator when connection fails
Matt Johnston <matt@ucc.asn.au>
parents: 1136
diff changeset
194 remove_connect(c, iter);
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 }
1152
57d09741d46d avoid invalidated iterator when connection fails
Matt Johnston <matt@ucc.asn.au>
parents: 1136
diff changeset
196 iter = next_iter;
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 }
1357
08f4fa4dc6a0 closer to working
Matt Johnston <matt@ucc.asn.au>
parents: 1355
diff changeset
198 TRACE(("leave set_connect_fds"))
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 }
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 void handle_connect_fds(fd_set *writefd) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 m_list_elem *iter;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 TRACE(("enter handle_connect_fds"))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 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
205 int val;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 socklen_t vallen = sizeof(val);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 struct dropbear_progress_connection *c = iter->item;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208
1053
fd3712d1ff7f Better failure handling
Matt Johnston <matt@ucc.asn.au>
parents: 1050
diff changeset
209 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
210 continue;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 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
214
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 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
216 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
217 /* 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
218 m_close(c->sock);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 c->sock = -1;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 } else if (val != 0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 /* Connect failed */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 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
223 m_close(c->sock);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 c->sock = -1;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 m_free(c->errstring);
1066
cce03ea8e6eb strdup strerror
Matt Johnston <matt@ucc.asn.au>
parents: 1065
diff changeset
227 c->errstring = m_strdup(strerror(val));
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 } else {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 /* New connection has been established */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 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
231 remove_connect(c, iter);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 TRACE(("leave handle_connect_fds - success"))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 /* Must return here - remove_connect() invalidates iter */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 return;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 TRACE(("leave handle_connect_fds - end iter"))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 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
241 c->writequeue = writequeue;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243
1072
686cd3e8e13e avoid malloc for iovec
Matt Johnston <matt@ucc.asn.au>
parents: 1067
diff changeset
244 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
245 struct Link *l;
1034
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
246 unsigned int i;
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 int len;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 buffer *writebuf;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 #ifndef IOV_MAX
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 #define IOV_MAX UIO_MAXIOV
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 #endif
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253
1072
686cd3e8e13e avoid malloc for iovec
Matt Johnston <matt@ucc.asn.au>
parents: 1067
diff changeset
254 *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
255
1073
88043f9d40bd Fix when iov queue is large
Matt Johnston <matt@ucc.asn.au>
parents: 1072
diff changeset
256 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
257 {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 writebuf = (buffer*)l->item;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 len = writebuf->len - 1 - writebuf->pos;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 dropbear_assert(len > 0);
1034
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
261 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
262 len, writebuf->len-1))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 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
264 iov[i].iov_len = len;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 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
269 buffer *writebuf;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 int len;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 while (written > 0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 writebuf = (buffer*)examine(queue);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 len = writebuf->len - 1 - writebuf->pos;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 if (len > written) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 /* partial buffer write */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 buf_incrpos(writebuf, written);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277 written = 0;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 } else {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 written -= len;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 dequeue(queue);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 buf_free(writebuf);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 }
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 void set_sock_nodelay(int sock) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 int val;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 /* disable nagle */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 val = 1;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 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
292 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293
1295
750ec4ec4cbe Convert #ifdef to #if, other build changes
Matt Johnston <matt@ucc.asn.au>
parents: 1152
diff changeset
294 #if DROPBEAR_SERVER_TCP_FAST_OPEN
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 void set_listen_fast_open(int sock) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 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
297 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
298 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
299 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 }
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 #endif
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 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
305
1034
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
306 int rc;
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
307 #ifdef IPTOS_LOWDELAY
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
308 int iptos_val = 0;
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
309 #endif
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
310 #ifdef SO_PRIORITY
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
311 int so_prio_val = 0;
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
312 #endif
107b013e9d9d avoid some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 1033
diff changeset
313
1383
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
314 #ifdef DROPBEAR_FUZZ
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
315 if (fuzz.fuzzing) {
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
316 TRACE(("fuzzing skips set_sock_prio"))
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
317 return;
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
318 }
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
319 #endif
1032
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 /* 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
322 * on a client '-J' proxy pipe */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 /* 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
325 #ifdef IPTOS_LOWDELAY
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 if (prio == DROPBEAR_PRIO_LOWDELAY) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 iptos_val = IPTOS_LOWDELAY;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328 } else if (prio == DROPBEAR_PRIO_BULK) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 iptos_val = IPTOS_THROUGHPUT;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 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
333 if (rc < 0 && errno != ENOTSOCK) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334 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
335 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 #endif
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 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
338 if (rc < 0 && errno != ENOTSOCK) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
339 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
340 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341 #endif
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343 #ifdef SO_PRIORITY
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 if (prio == DROPBEAR_PRIO_LOWDELAY) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 so_prio_val = TC_PRIO_INTERACTIVE;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346 } else if (prio == DROPBEAR_PRIO_BULK) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 so_prio_val = TC_PRIO_BULK;
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 /* 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
350 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
351 if (rc < 0 && errno != ENOTSOCK)
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352 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
353 strerror(errno));
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 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358 /* Listen on address:port.
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 * 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
360 * 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
361 * 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
362 * 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
363 * string.*/
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364 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
365 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
366
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367 struct addrinfo hints, *res = NULL, *res0 = NULL;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368 int err;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369 unsigned int nsock;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 struct linger linger;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371 int val;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
372 int sock;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
373
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
374 TRACE(("enter dropbear_listen"))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
376 memset(&hints, 0, sizeof(hints));
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377 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
378 hints.ai_socktype = SOCK_STREAM;
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 /* for calling getaddrinfo:
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381 address == NULL and !AI_PASSIVE: local loopback
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
382 address == NULL and AI_PASSIVE: all interfaces
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
383 address != NULL: whatever the address says */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384 if (!address) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385 TRACE(("dropbear_listen: local loopback"))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 } else {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387 if (address[0] == '\0') {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388 TRACE(("dropbear_listen: all interfaces"))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389 address = NULL;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391 hints.ai_flags = AI_PASSIVE;
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 err = getaddrinfo(address, port, &hints, &res0);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 if (err) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396 if (errstring != NULL && *errstring == NULL) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 int len;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398 len = 20 + strlen(gai_strerror(err));
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
399 *errstring = (char*)m_malloc(len);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400 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
401 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402 if (res0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403 freeaddrinfo(res0);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404 res0 = NULL;
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 TRACE(("leave dropbear_listen: failed resolving"))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 return -1;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
408 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411 nsock = 0;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412 for (res = res0; res != NULL && nsock < sockcount;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413 res = res->ai_next) {
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 /* Get a socket */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 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
417 res->ai_protocol);
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 sock = socks[nsock]; /* For clarity */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 if (sock < 0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
422 err = errno;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
423 TRACE(("socket() failed"))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 continue;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427 /* Various useful socket options */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
428 val = 1;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
429 /* set to reuse, quick timeout */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
430 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
431 linger.l_onoff = 1;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
432 linger.l_linger = 5;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433 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
434
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
435 #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436 if (res->ai_family == AF_INET6) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
437 int on = 1;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
438 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
439 &on, sizeof(on)) == -1) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
440 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
441 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 #endif
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 set_sock_nodelay(sock);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 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
448 err = errno;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 close(sock);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 TRACE(("bind(%s) failed", port))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 continue;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454 if (listen(sock, DROPBEAR_LISTEN_BACKLOG) < 0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455 err = errno;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456 close(sock);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457 TRACE(("listen() failed"))
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458 continue;
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
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 *maxfd = MAX(*maxfd, sock);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
462
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463 nsock++;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 }
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 if (res0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467 freeaddrinfo(res0);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 res0 = NULL;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
469 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471 if (nsock == 0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 if (errstring != NULL && *errstring == NULL) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473 int len;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 len = 20 + strlen(strerror(err));
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 *errstring = (char*)m_malloc(len);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476 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
477 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 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
479 return -1;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 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
483 return nsock;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486 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
487 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
488 {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 struct sockaddr_storage addr;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490 socklen_t addrlen;
1383
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
491
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
492 #if DROPBEAR_FUZZ
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
493 if (fuzz.fuzzing) {
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
494 fuzz_get_socket_address(fd, local_host, local_port, remote_host, remote_port, host_lookup);
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
495 return;
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
496 }
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
497 #endif
1032
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 if (local_host || local_port) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 addrlen = sizeof(addr);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501 if (getsockname(fd, (struct sockaddr*)&addr, &addrlen) < 0) {
1383
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
502 dropbear_exit("Failed socket address: %s", strerror(errno));
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
503 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 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
505 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 if (remote_host || remote_port) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 addrlen = sizeof(addr);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508 if (getpeername(fd, (struct sockaddr*)&addr, &addrlen) < 0) {
1383
f03cfe9c76ac Disable setnonblocking(), get_socket_address(), set_sock_priority()
Matt Johnston <matt@ucc.asn.au>
parents: 1357
diff changeset
509 dropbear_exit("Failed socket address: %s", strerror(errno));
1032
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 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
512 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515 /* 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
516 * value is allocated with malloc() */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517 void getaddrstring(struct sockaddr_storage* addr,
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518 char **ret_host, char **ret_port,
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519 int host_lookup) {
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 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
522 unsigned int len;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 int ret;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 int flags = NI_NUMERICSERV | NI_NUMERICHOST;
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 #ifndef DO_HOST_LOOKUP
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528 host_lookup = 0;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529 #endif
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 if (host_lookup) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
532 flags = NI_NUMERICSERV;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 len = sizeof(struct sockaddr_storage);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536 /* 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
537 * 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
538 * 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
539 * but we'll ignore them */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
541 if (addr->ss_family == AF_INET) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 len = sizeof(struct sockaddr_in);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 #ifdef AF_INET6
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 if (addr->ss_family == AF_INET6) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546 len = sizeof(struct sockaddr_in6);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 #endif
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 #endif
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 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
552 serv, sizeof(serv)-1, flags);
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 if (ret != 0) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 if (host_lookup) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556 /* 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
557 * 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
558 * to exhibit that behaviour. */
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 getaddrstring(addr, ret_host, ret_port, 0);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 return;
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 } else {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562 /* 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
563 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
564 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 if (ret_host) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 *ret_host = m_strdup(host);
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 if (ret_port) {
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 *ret_port = m_strdup(serv);
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 }
0da8ba489c23 Move generic network routines to netio.c
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574