c5e438ec |
1 | /* |
2 | * Unix networking abstraction. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <assert.h> |
8 | #include <errno.h> |
9 | #include <fcntl.h> |
10 | #include <unistd.h> |
11 | #include <sys/types.h> |
12 | #include <sys/socket.h> |
13 | #include <sys/ioctl.h> |
14 | #include <arpa/inet.h> |
15 | #include <netinet/in.h> |
16 | #include <netinet/tcp.h> |
17 | #include <netdb.h> |
fc0f17db |
18 | #include <sys/un.h> |
c5e438ec |
19 | |
20 | #define DEFINE_PLUG_METHOD_MACROS |
21 | #include "putty.h" |
22 | #include "network.h" |
23 | #include "tree234.h" |
24 | |
dbf58cfa |
25 | /* Solaris needs <sys/sockio.h> for SIOCATMARK. */ |
5f86b177 |
26 | #ifndef SIOCATMARK |
dbf58cfa |
27 | #include <sys/sockio.h> |
28 | #endif |
29 | |
fc0f17db |
30 | #ifndef X11_UNIX_PATH |
31 | # define X11_UNIX_PATH "/tmp/.X11-unix/X" |
32 | #endif |
33 | |
c44e5afc |
34 | /* |
35 | * Access to sockaddr types without breaking C strict aliasing rules. |
36 | */ |
37 | union sockaddr_union { |
38 | #ifdef NO_IPV6 |
39 | struct sockaddr_in storage; |
40 | #else |
41 | struct sockaddr_storage storage; |
42 | struct sockaddr_in6 sin6; |
43 | #endif |
44 | struct sockaddr sa; |
45 | struct sockaddr_in sin; |
23e649c9 |
46 | struct sockaddr_un su; |
c44e5afc |
47 | }; |
48 | |
4efd5997 |
49 | /* |
50 | * We used to typedef struct Socket_tag *Socket. |
51 | * |
52 | * Since we have made the networking abstraction slightly more |
53 | * abstract, Socket no longer means a tcp socket (it could mean |
54 | * an ssl socket). So now we must use Actual_Socket when we know |
55 | * we are talking about a tcp socket. |
56 | */ |
57 | typedef struct Socket_tag *Actual_Socket; |
58 | |
facf7568 |
59 | /* |
60 | * Mutable state that goes with a SockAddr: stores information |
61 | * about where in the list of candidate IP(v*) addresses we've |
62 | * currently got to. |
63 | */ |
64 | typedef struct SockAddrStep_tag SockAddrStep; |
65 | struct SockAddrStep_tag { |
66 | #ifndef NO_IPV6 |
67 | struct addrinfo *ai; /* steps along addr->ais */ |
68 | #endif |
69 | int curraddr; |
70 | }; |
71 | |
c5e438ec |
72 | struct Socket_tag { |
73 | struct socket_function_table *fn; |
74 | /* the above variable absolutely *must* be the first in this structure */ |
cbe2d68f |
75 | const char *error; |
c5e438ec |
76 | int s; |
77 | Plug plug; |
78 | void *private_ptr; |
79 | bufchain output_data; |
1cfc764f |
80 | int connected; /* irrelevant for listening sockets */ |
c5e438ec |
81 | int writable; |
82 | int frozen; /* this causes readability notifications to be ignored */ |
c5e438ec |
83 | int localhost_only; /* for listening sockets */ |
84 | char oobdata[1]; |
85 | int sending_oob; |
86 | int oobpending; /* is there OOB data available to read? */ |
87 | int oobinline; |
bc06669b |
88 | enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof; |
89 | int incomingeof; |
c5e438ec |
90 | int pending_error; /* in case send() returns error */ |
91 | int listener; |
7555d6a5 |
92 | int nodelay, keepalive; /* for connect()-type sockets */ |
93 | int privport, port; /* and again */ |
94 | SockAddr addr; |
facf7568 |
95 | SockAddrStep step; |
4efd5997 |
96 | /* |
97 | * We sometimes need pairs of Socket structures to be linked: |
98 | * if we are listening on the same IPv6 and v4 port, for |
99 | * example. So here we define `parent' and `child' pointers to |
100 | * track this link. |
101 | */ |
102 | Actual_Socket parent, child; |
c5e438ec |
103 | }; |
104 | |
c5e438ec |
105 | struct SockAddr_tag { |
5025a993 |
106 | int refcount; |
cbe2d68f |
107 | const char *error; |
facf7568 |
108 | enum { UNRESOLVED, UNIX, IP } superfamily; |
05581745 |
109 | #ifndef NO_IPV6 |
7555d6a5 |
110 | struct addrinfo *ais; /* Addresses IPv6 style. */ |
792c5eb5 |
111 | #else |
7555d6a5 |
112 | unsigned long *addresses; /* Addresses IPv4 style. */ |
facf7568 |
113 | int naddresses; |
c5e438ec |
114 | #endif |
b7a189f3 |
115 | char hostname[512]; /* Store an unresolved host name. */ |
c5e438ec |
116 | }; |
117 | |
facf7568 |
118 | /* |
119 | * Which address family this address belongs to. AF_INET for IPv4; |
120 | * AF_INET6 for IPv6; AF_UNSPEC indicates that name resolution has |
121 | * not been done and a simple host name is held in this SockAddr |
122 | * structure. |
123 | */ |
124 | #ifndef NO_IPV6 |
125 | #define SOCKADDR_FAMILY(addr, step) \ |
126 | ((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \ |
127 | (addr)->superfamily == UNIX ? AF_UNIX : \ |
128 | (step).ai ? (step).ai->ai_family : AF_INET) |
129 | #else |
130 | #define SOCKADDR_FAMILY(addr, step) \ |
131 | ((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \ |
132 | (addr)->superfamily == UNIX ? AF_UNIX : AF_INET) |
133 | #endif |
134 | |
135 | /* |
136 | * Start a SockAddrStep structure to step through multiple |
137 | * addresses. |
138 | */ |
139 | #ifndef NO_IPV6 |
140 | #define START_STEP(addr, step) \ |
141 | ((step).ai = (addr)->ais, (step).curraddr = 0) |
142 | #else |
143 | #define START_STEP(addr, step) \ |
144 | ((step).curraddr = 0) |
145 | #endif |
146 | |
c5e438ec |
147 | static tree234 *sktree; |
148 | |
0ff9ea38 |
149 | static void uxsel_tell(Actual_Socket s); |
150 | |
c5e438ec |
151 | static int cmpfortree(void *av, void *bv) |
152 | { |
153 | Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv; |
154 | int as = a->s, bs = b->s; |
155 | if (as < bs) |
156 | return -1; |
157 | if (as > bs) |
158 | return +1; |
fef4049e |
159 | if (a < b) |
160 | return -1; |
161 | if (a > b) |
162 | return +1; |
c5e438ec |
163 | return 0; |
164 | } |
165 | |
166 | static int cmpforsearch(void *av, void *bv) |
167 | { |
168 | Actual_Socket b = (Actual_Socket) bv; |
f7f9fb5c |
169 | int as = *(int *)av, bs = b->s; |
c5e438ec |
170 | if (as < bs) |
171 | return -1; |
172 | if (as > bs) |
173 | return +1; |
174 | return 0; |
175 | } |
176 | |
177 | void sk_init(void) |
178 | { |
179 | sktree = newtree234(cmpfortree); |
180 | } |
181 | |
182 | void sk_cleanup(void) |
183 | { |
184 | Actual_Socket s; |
185 | int i; |
186 | |
187 | if (sktree) { |
188 | for (i = 0; (s = index234(sktree, i)) != NULL; i++) { |
189 | close(s->s); |
190 | } |
191 | } |
192 | } |
193 | |
05581745 |
194 | SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family) |
c5e438ec |
195 | { |
3d88e64d |
196 | SockAddr ret = snew(struct SockAddr_tag); |
05581745 |
197 | #ifndef NO_IPV6 |
792c5eb5 |
198 | struct addrinfo hints; |
199 | int err; |
200 | #else |
c5e438ec |
201 | unsigned long a; |
202 | struct hostent *h = NULL; |
7555d6a5 |
203 | int n; |
792c5eb5 |
204 | #endif |
c5e438ec |
205 | char realhost[8192]; |
206 | |
207 | /* Clear the structure and default to IPv4. */ |
208 | memset(ret, 0, sizeof(struct SockAddr_tag)); |
facf7568 |
209 | ret->superfamily = UNRESOLVED; |
c5e438ec |
210 | *realhost = '\0'; |
211 | ret->error = NULL; |
5025a993 |
212 | ret->refcount = 1; |
c5e438ec |
213 | |
05581745 |
214 | #ifndef NO_IPV6 |
792c5eb5 |
215 | hints.ai_flags = AI_CANONNAME; |
81b34354 |
216 | hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET : |
217 | address_family == ADDRTYPE_IPV6 ? AF_INET6 : |
218 | AF_UNSPEC); |
219 | hints.ai_socktype = SOCK_STREAM; |
792c5eb5 |
220 | hints.ai_protocol = 0; |
221 | hints.ai_addrlen = 0; |
222 | hints.ai_addr = NULL; |
223 | hints.ai_canonname = NULL; |
224 | hints.ai_next = NULL; |
7555d6a5 |
225 | err = getaddrinfo(host, NULL, &hints, &ret->ais); |
792c5eb5 |
226 | if (err != 0) { |
227 | ret->error = gai_strerror(err); |
228 | return ret; |
229 | } |
facf7568 |
230 | ret->superfamily = IP; |
792c5eb5 |
231 | *realhost = '\0'; |
facf7568 |
232 | if (ret->ais->ai_canonname != NULL) |
233 | strncat(realhost, ret->ais->ai_canonname, sizeof(realhost) - 1); |
792c5eb5 |
234 | else |
235 | strncat(realhost, host, sizeof(realhost) - 1); |
236 | #else |
72e22079 |
237 | if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) { |
792c5eb5 |
238 | /* |
239 | * Otherwise use the IPv4-only gethostbyname... (NOTE: |
240 | * we don't use gethostbyname as a fallback!) |
241 | */ |
facf7568 |
242 | if (ret->superfamily == UNRESOLVED) { |
7555d6a5 |
243 | /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */ |
244 | if ( (h = gethostbyname(host)) ) |
facf7568 |
245 | ret->superfamily = IP; |
792c5eb5 |
246 | } |
facf7568 |
247 | if (ret->superfamily == UNRESOLVED) { |
7555d6a5 |
248 | ret->error = (h_errno == HOST_NOT_FOUND || |
249 | h_errno == NO_DATA || |
250 | h_errno == NO_ADDRESS ? "Host does not exist" : |
251 | h_errno == TRY_AGAIN ? |
252 | "Temporary name service failure" : |
253 | "gethostbyname: unknown error"); |
254 | return ret; |
c5e438ec |
255 | } |
792c5eb5 |
256 | /* This way we are always sure the h->h_name is valid :) */ |
257 | strncpy(realhost, h->h_name, sizeof(realhost)); |
7555d6a5 |
258 | for (n = 0; h->h_addr_list[n]; n++); |
259 | ret->addresses = snewn(n, unsigned long); |
260 | ret->naddresses = n; |
261 | for (n = 0; n < ret->naddresses; n++) { |
262 | memcpy(&a, h->h_addr_list[n], sizeof(a)); |
263 | ret->addresses[n] = ntohl(a); |
264 | } |
c5e438ec |
265 | } else { |
266 | /* |
267 | * This must be a numeric IPv4 address because it caused a |
268 | * success return from inet_addr. |
269 | */ |
facf7568 |
270 | ret->superfamily = IP; |
c5e438ec |
271 | strncpy(realhost, host, sizeof(realhost)); |
7555d6a5 |
272 | ret->addresses = snew(unsigned long); |
273 | ret->naddresses = 1; |
274 | ret->addresses[0] = ntohl(a); |
c5e438ec |
275 | } |
792c5eb5 |
276 | #endif |
c5e438ec |
277 | realhost[lenof(realhost)-1] = '\0'; |
3d88e64d |
278 | *canonicalname = snewn(1+strlen(realhost), char); |
c5e438ec |
279 | strcpy(*canonicalname, realhost); |
280 | return ret; |
281 | } |
282 | |
e8fa8f62 |
283 | SockAddr sk_nonamelookup(const char *host) |
b7a189f3 |
284 | { |
3d88e64d |
285 | SockAddr ret = snew(struct SockAddr_tag); |
ab0873ab |
286 | ret->error = NULL; |
facf7568 |
287 | ret->superfamily = UNRESOLVED; |
b7a189f3 |
288 | strncpy(ret->hostname, host, lenof(ret->hostname)); |
289 | ret->hostname[lenof(ret->hostname)-1] = '\0'; |
e14d27a1 |
290 | #ifndef NO_IPV6 |
7555d6a5 |
291 | ret->ais = NULL; |
292 | #else |
293 | ret->addresses = NULL; |
e14d27a1 |
294 | #endif |
5025a993 |
295 | ret->refcount = 1; |
b7a189f3 |
296 | return ret; |
297 | } |
298 | |
facf7568 |
299 | static int sk_nextaddr(SockAddr addr, SockAddrStep *step) |
7555d6a5 |
300 | { |
301 | #ifndef NO_IPV6 |
facf7568 |
302 | if (step->ai && step->ai->ai_next) { |
303 | step->ai = step->ai->ai_next; |
7555d6a5 |
304 | return TRUE; |
305 | } else |
306 | return FALSE; |
307 | #else |
facf7568 |
308 | if (step->curraddr+1 < addr->naddresses) { |
309 | step->curraddr++; |
7555d6a5 |
310 | return TRUE; |
311 | } else { |
312 | return FALSE; |
313 | } |
314 | #endif |
315 | } |
316 | |
c5e438ec |
317 | void sk_getaddr(SockAddr addr, char *buf, int buflen) |
318 | { |
ffbfd093 |
319 | /* XXX not clear what we should return for Unix-domain sockets; let's |
320 | * hope the question never arises */ |
321 | assert(addr->superfamily != UNIX); |
facf7568 |
322 | if (addr->superfamily == UNRESOLVED) { |
792c5eb5 |
323 | strncpy(buf, addr->hostname, buflen); |
324 | buf[buflen-1] = '\0'; |
325 | } else { |
05581745 |
326 | #ifndef NO_IPV6 |
facf7568 |
327 | if (getnameinfo(addr->ais->ai_addr, addr->ais->ai_addrlen, buf, buflen, |
792c5eb5 |
328 | NULL, 0, NI_NUMERICHOST) != 0) { |
329 | buf[0] = '\0'; |
330 | strncat(buf, "<unknown>", buflen - 1); |
331 | } |
332 | #else |
c5e438ec |
333 | struct in_addr a; |
facf7568 |
334 | SockAddrStep step; |
335 | START_STEP(addr, step); |
336 | assert(SOCKADDR_FAMILY(addr, step) == AF_INET); |
337 | a.s_addr = htonl(addr->addresses[0]); |
c5e438ec |
338 | strncpy(buf, inet_ntoa(a), buflen); |
b7a189f3 |
339 | buf[buflen-1] = '\0'; |
792c5eb5 |
340 | #endif |
c5e438ec |
341 | } |
c5e438ec |
342 | } |
343 | |
42466758 |
344 | int sk_hostname_is_local(const char *name) |
b804e1e5 |
345 | { |
db74a69d |
346 | return !strcmp(name, "localhost") || |
347 | !strcmp(name, "::1") || |
348 | !strncmp(name, "127.", 4); |
b804e1e5 |
349 | } |
350 | |
147d3370 |
351 | #define ipv4_is_loopback(addr) \ |
352 | (((addr).s_addr & htonl(0xff000000)) == htonl(0x7f000000)) |
353 | |
354 | static int sockaddr_is_loopback(struct sockaddr *sa) |
355 | { |
c44e5afc |
356 | union sockaddr_union *u = (union sockaddr_union *)sa; |
357 | switch (u->sa.sa_family) { |
147d3370 |
358 | case AF_INET: |
c44e5afc |
359 | return ipv4_is_loopback(u->sin.sin_addr); |
147d3370 |
360 | #ifndef NO_IPV6 |
361 | case AF_INET6: |
c44e5afc |
362 | return IN6_IS_ADDR_LOOPBACK(&u->sin6.sin6_addr); |
147d3370 |
363 | #endif |
ff677091 |
364 | case AF_UNIX: |
147d3370 |
365 | return TRUE; |
366 | default: |
367 | return FALSE; |
368 | } |
369 | } |
370 | |
b804e1e5 |
371 | int sk_address_is_local(SockAddr addr) |
372 | { |
facf7568 |
373 | if (addr->superfamily == UNRESOLVED) |
792c5eb5 |
374 | return 0; /* we don't know; assume not */ |
ffbfd093 |
375 | else if (addr->superfamily == UNIX) |
376 | return 1; |
792c5eb5 |
377 | else { |
05581745 |
378 | #ifndef NO_IPV6 |
facf7568 |
379 | return sockaddr_is_loopback(addr->ais->ai_addr); |
792c5eb5 |
380 | #else |
b804e1e5 |
381 | struct in_addr a; |
facf7568 |
382 | SockAddrStep step; |
383 | START_STEP(addr, step); |
384 | assert(SOCKADDR_FAMILY(addr, step) == AF_INET); |
385 | a.s_addr = htonl(addr->addresses[0]); |
b804e1e5 |
386 | return ipv4_is_loopback(a); |
792c5eb5 |
387 | #endif |
b804e1e5 |
388 | } |
b804e1e5 |
389 | } |
390 | |
46f25814 |
391 | int sk_address_is_special_local(SockAddr addr) |
392 | { |
393 | return addr->superfamily == UNIX; |
394 | } |
395 | |
c5e438ec |
396 | int sk_addrtype(SockAddr addr) |
397 | { |
facf7568 |
398 | SockAddrStep step; |
399 | int family; |
400 | START_STEP(addr, step); |
401 | family = SOCKADDR_FAMILY(addr, step); |
402 | |
403 | return (family == AF_INET ? ADDRTYPE_IPV4 : |
05581745 |
404 | #ifndef NO_IPV6 |
facf7568 |
405 | family == AF_INET6 ? ADDRTYPE_IPV6 : |
b7a189f3 |
406 | #endif |
407 | ADDRTYPE_NAME); |
c5e438ec |
408 | } |
409 | |
410 | void sk_addrcopy(SockAddr addr, char *buf) |
411 | { |
facf7568 |
412 | SockAddrStep step; |
413 | int family; |
414 | START_STEP(addr, step); |
415 | family = SOCKADDR_FAMILY(addr, step); |
792c5eb5 |
416 | |
05581745 |
417 | #ifndef NO_IPV6 |
facf7568 |
418 | if (family == AF_INET) |
419 | memcpy(buf, &((struct sockaddr_in *)step.ai->ai_addr)->sin_addr, |
792c5eb5 |
420 | sizeof(struct in_addr)); |
facf7568 |
421 | else if (family == AF_INET6) |
422 | memcpy(buf, &((struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr, |
792c5eb5 |
423 | sizeof(struct in6_addr)); |
424 | else |
425 | assert(FALSE); |
426 | #else |
427 | struct in_addr a; |
428 | |
facf7568 |
429 | assert(family == AF_INET); |
430 | a.s_addr = htonl(addr->addresses[step.curraddr]); |
792c5eb5 |
431 | memcpy(buf, (char*) &a.s_addr, 4); |
c5e438ec |
432 | #endif |
c5e438ec |
433 | } |
434 | |
435 | void sk_addr_free(SockAddr addr) |
436 | { |
5025a993 |
437 | if (--addr->refcount > 0) |
438 | return; |
05581745 |
439 | #ifndef NO_IPV6 |
7555d6a5 |
440 | if (addr->ais != NULL) |
441 | freeaddrinfo(addr->ais); |
442 | #else |
443 | sfree(addr->addresses); |
792c5eb5 |
444 | #endif |
c5e438ec |
445 | sfree(addr); |
446 | } |
447 | |
5025a993 |
448 | SockAddr sk_addr_dup(SockAddr addr) |
449 | { |
450 | addr->refcount++; |
451 | return addr; |
452 | } |
453 | |
c5e438ec |
454 | static Plug sk_tcp_plug(Socket sock, Plug p) |
455 | { |
456 | Actual_Socket s = (Actual_Socket) sock; |
457 | Plug ret = s->plug; |
458 | if (p) |
459 | s->plug = p; |
460 | return ret; |
461 | } |
462 | |
463 | static void sk_tcp_flush(Socket s) |
464 | { |
465 | /* |
466 | * We send data to the socket as soon as we can anyway, |
467 | * so we don't need to do anything here. :-) |
468 | */ |
469 | } |
470 | |
471 | static void sk_tcp_close(Socket s); |
e0e7dff8 |
472 | static int sk_tcp_write(Socket s, const char *data, int len); |
473 | static int sk_tcp_write_oob(Socket s, const char *data, int len); |
bc06669b |
474 | static void sk_tcp_write_eof(Socket s); |
c5e438ec |
475 | static void sk_tcp_set_private_ptr(Socket s, void *ptr); |
476 | static void *sk_tcp_get_private_ptr(Socket s); |
477 | static void sk_tcp_set_frozen(Socket s, int is_frozen); |
cbe2d68f |
478 | static const char *sk_tcp_socket_error(Socket s); |
c5e438ec |
479 | |
2f92b717 |
480 | static struct socket_function_table tcp_fn_table = { |
481 | sk_tcp_plug, |
482 | sk_tcp_close, |
483 | sk_tcp_write, |
484 | sk_tcp_write_oob, |
bc06669b |
485 | sk_tcp_write_eof, |
2f92b717 |
486 | sk_tcp_flush, |
487 | sk_tcp_set_private_ptr, |
488 | sk_tcp_get_private_ptr, |
489 | sk_tcp_set_frozen, |
490 | sk_tcp_socket_error |
491 | }; |
492 | |
f7f9fb5c |
493 | Socket sk_register(OSSocket sockfd, Plug plug) |
c5e438ec |
494 | { |
c5e438ec |
495 | Actual_Socket ret; |
496 | |
497 | /* |
498 | * Create Socket structure. |
499 | */ |
3d88e64d |
500 | ret = snew(struct Socket_tag); |
2f92b717 |
501 | ret->fn = &tcp_fn_table; |
c5e438ec |
502 | ret->error = NULL; |
503 | ret->plug = plug; |
504 | bufchain_init(&ret->output_data); |
505 | ret->writable = 1; /* to start with */ |
506 | ret->sending_oob = 0; |
507 | ret->frozen = 1; |
c5e438ec |
508 | ret->localhost_only = 0; /* unused, but best init anyway */ |
509 | ret->pending_error = 0; |
510 | ret->oobpending = FALSE; |
bc06669b |
511 | ret->outgoingeof = EOF_NO; |
512 | ret->incomingeof = FALSE; |
c5e438ec |
513 | ret->listener = 0; |
4efd5997 |
514 | ret->parent = ret->child = NULL; |
7555d6a5 |
515 | ret->addr = NULL; |
a8cb2b6e |
516 | ret->connected = 1; |
c5e438ec |
517 | |
f7f9fb5c |
518 | ret->s = sockfd; |
c5e438ec |
519 | |
520 | if (ret->s < 0) { |
7555d6a5 |
521 | ret->error = strerror(errno); |
c5e438ec |
522 | return (Socket) ret; |
523 | } |
524 | |
525 | ret->oobinline = 0; |
526 | |
0ff9ea38 |
527 | uxsel_tell(ret); |
c5e438ec |
528 | add234(sktree, ret); |
529 | |
530 | return (Socket) ret; |
531 | } |
532 | |
7555d6a5 |
533 | static int try_connect(Actual_Socket sock) |
c5e438ec |
534 | { |
c5e438ec |
535 | int s; |
23e649c9 |
536 | union sockaddr_union u; |
537 | const union sockaddr_union *sa; |
7555d6a5 |
538 | int err = 0; |
c5e438ec |
539 | short localport; |
f9520103 |
540 | int salen, family; |
c5e438ec |
541 | |
fef4049e |
542 | /* |
543 | * Remove the socket from the tree before we overwrite its |
544 | * internal socket id, because that forms part of the tree's |
545 | * sorting criterion. We'll add it back before exiting this |
546 | * function, whether we changed anything or not. |
547 | */ |
548 | del234(sktree, sock); |
549 | |
7555d6a5 |
550 | if (sock->s >= 0) |
551 | close(sock->s); |
552 | |
553 | plug_log(sock->plug, 0, sock->addr, sock->port, NULL, 0); |
c5e438ec |
554 | |
555 | /* |
556 | * Open socket. |
557 | */ |
facf7568 |
558 | family = SOCKADDR_FAMILY(sock->addr, sock->step); |
559 | assert(family != AF_UNSPEC); |
560 | s = socket(family, SOCK_STREAM, 0); |
7555d6a5 |
561 | sock->s = s; |
c5e438ec |
562 | |
563 | if (s < 0) { |
7555d6a5 |
564 | err = errno; |
565 | goto ret; |
c5e438ec |
566 | } |
567 | |
db9b7dce |
568 | cloexec(s); |
89e97516 |
569 | |
7555d6a5 |
570 | if (sock->oobinline) { |
c5e438ec |
571 | int b = TRUE; |
f7aa4b36 |
572 | if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, |
573 | (void *) &b, sizeof(b)) < 0) { |
574 | err = errno; |
575 | close(s); |
576 | goto ret; |
577 | } |
c5e438ec |
578 | } |
579 | |
7555d6a5 |
580 | if (sock->nodelay) { |
c5e438ec |
581 | int b = TRUE; |
f7aa4b36 |
582 | if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, |
583 | (void *) &b, sizeof(b)) < 0) { |
584 | err = errno; |
585 | close(s); |
586 | goto ret; |
587 | } |
c5e438ec |
588 | } |
589 | |
7555d6a5 |
590 | if (sock->keepalive) { |
79bf227b |
591 | int b = TRUE; |
f7aa4b36 |
592 | if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, |
593 | (void *) &b, sizeof(b)) < 0) { |
594 | err = errno; |
595 | close(s); |
596 | goto ret; |
597 | } |
79bf227b |
598 | } |
599 | |
c5e438ec |
600 | /* |
601 | * Bind to local address. |
602 | */ |
7555d6a5 |
603 | if (sock->privport) |
c5e438ec |
604 | localport = 1023; /* count from 1023 downwards */ |
605 | else |
606 | localport = 0; /* just use port 0 (ie kernel picks) */ |
607 | |
d2c29274 |
608 | /* BSD IP stacks need sockaddr_in zeroed before filling in */ |
23e649c9 |
609 | memset(&u,'\0',sizeof(u)); |
fc0f17db |
610 | |
611 | /* We don't try to bind to a local address for UNIX domain sockets. (Why |
612 | * do we bother doing the bind when localport == 0 anyway?) */ |
facf7568 |
613 | if (family != AF_UNIX) { |
fc0f17db |
614 | /* Loop round trying to bind */ |
615 | while (1) { |
616 | int retcode; |
c5e438ec |
617 | |
05581745 |
618 | #ifndef NO_IPV6 |
facf7568 |
619 | if (family == AF_INET6) { |
fc0f17db |
620 | /* XXX use getaddrinfo to get a local address? */ |
23e649c9 |
621 | u.sin6.sin6_family = AF_INET6; |
622 | u.sin6.sin6_addr = in6addr_any; |
623 | u.sin6.sin6_port = htons(localport); |
624 | retcode = bind(s, &u.sa, sizeof(u.sin6)); |
fc0f17db |
625 | } else |
c5e438ec |
626 | #endif |
fc0f17db |
627 | { |
facf7568 |
628 | assert(family == AF_INET); |
23e649c9 |
629 | u.sin.sin_family = AF_INET; |
630 | u.sin.sin_addr.s_addr = htonl(INADDR_ANY); |
631 | u.sin.sin_port = htons(localport); |
632 | retcode = bind(s, &u.sa, sizeof(u.sin)); |
fc0f17db |
633 | } |
634 | if (retcode >= 0) { |
635 | err = 0; |
636 | break; /* done */ |
637 | } else { |
638 | err = errno; |
639 | if (err != EADDRINUSE) /* failed, for a bad reason */ |
640 | break; |
641 | } |
642 | |
643 | if (localport == 0) |
644 | break; /* we're only looping once */ |
645 | localport--; |
646 | if (localport == 0) |
647 | break; /* we might have got to the end */ |
c5e438ec |
648 | } |
fc0f17db |
649 | |
7555d6a5 |
650 | if (err) |
651 | goto ret; |
c5e438ec |
652 | } |
653 | |
654 | /* |
655 | * Connect to remote address. |
656 | */ |
facf7568 |
657 | switch(family) { |
05581745 |
658 | #ifndef NO_IPV6 |
fc0f17db |
659 | case AF_INET: |
660 | /* XXX would be better to have got getaddrinfo() to fill in the port. */ |
facf7568 |
661 | ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port = |
7555d6a5 |
662 | htons(sock->port); |
23e649c9 |
663 | sa = (const union sockaddr_union *)sock->step.ai->ai_addr; |
facf7568 |
664 | salen = sock->step.ai->ai_addrlen; |
fc0f17db |
665 | break; |
666 | case AF_INET6: |
facf7568 |
667 | ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port = |
7555d6a5 |
668 | htons(sock->port); |
23e649c9 |
669 | sa = (const union sockaddr_union *)sock->step.ai->ai_addr; |
facf7568 |
670 | salen = sock->step.ai->ai_addrlen; |
fc0f17db |
671 | break; |
792c5eb5 |
672 | #else |
fc0f17db |
673 | case AF_INET: |
23e649c9 |
674 | u.sin.sin_family = AF_INET; |
675 | u.sin.sin_addr.s_addr = htonl(sock->addr->addresses[sock->step.curraddr]); |
676 | u.sin.sin_port = htons((short) sock->port); |
677 | sa = &u; |
678 | salen = sizeof u.sin; |
fc0f17db |
679 | break; |
792c5eb5 |
680 | #endif |
fc0f17db |
681 | case AF_UNIX: |
7555d6a5 |
682 | assert(sock->port == 0); /* to catch confused people */ |
23e649c9 |
683 | assert(strlen(sock->addr->hostname) < sizeof u.su.sun_path); |
684 | u.su.sun_family = AF_UNIX; |
685 | strcpy(u.su.sun_path, sock->addr->hostname); |
686 | sa = &u; |
687 | salen = sizeof u.su; |
fc0f17db |
688 | break; |
689 | |
690 | default: |
691 | assert(0 && "unknown address family"); |
8815b68b |
692 | exit(1); /* XXX: GCC doesn't understand assert() on some systems. */ |
fc0f17db |
693 | } |
4b70e70e |
694 | |
f9520103 |
695 | nonblock(s); |
051dd789 |
696 | |
23e649c9 |
697 | if ((connect(s, &(sa->sa), salen)) < 0) { |
051dd789 |
698 | if ( errno != EINPROGRESS ) { |
7555d6a5 |
699 | err = errno; |
700 | goto ret; |
c5e438ec |
701 | } |
702 | } else { |
703 | /* |
704 | * If we _don't_ get EWOULDBLOCK, the connect has completed |
705 | * and we should set the socket as connected and writable. |
706 | */ |
7555d6a5 |
707 | sock->connected = 1; |
708 | sock->writable = 1; |
c5e438ec |
709 | } |
710 | |
7555d6a5 |
711 | uxsel_tell(sock); |
7555d6a5 |
712 | |
713 | ret: |
fef4049e |
714 | |
715 | /* |
716 | * No matter what happened, put the socket back in the tree. |
717 | */ |
718 | add234(sktree, sock); |
719 | |
7555d6a5 |
720 | if (err) |
721 | plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err); |
722 | return err; |
723 | } |
724 | |
725 | Socket sk_new(SockAddr addr, int port, int privport, int oobinline, |
726 | int nodelay, int keepalive, Plug plug) |
727 | { |
728 | Actual_Socket ret; |
729 | int err; |
730 | |
731 | /* |
732 | * Create Socket structure. |
733 | */ |
734 | ret = snew(struct Socket_tag); |
735 | ret->fn = &tcp_fn_table; |
736 | ret->error = NULL; |
737 | ret->plug = plug; |
738 | bufchain_init(&ret->output_data); |
739 | ret->connected = 0; /* to start with */ |
740 | ret->writable = 0; /* to start with */ |
741 | ret->sending_oob = 0; |
742 | ret->frozen = 0; |
7555d6a5 |
743 | ret->localhost_only = 0; /* unused, but best init anyway */ |
744 | ret->pending_error = 0; |
4efd5997 |
745 | ret->parent = ret->child = NULL; |
7555d6a5 |
746 | ret->oobpending = FALSE; |
bc06669b |
747 | ret->outgoingeof = EOF_NO; |
748 | ret->incomingeof = FALSE; |
7555d6a5 |
749 | ret->listener = 0; |
750 | ret->addr = addr; |
facf7568 |
751 | START_STEP(ret->addr, ret->step); |
7555d6a5 |
752 | ret->s = -1; |
753 | ret->oobinline = oobinline; |
754 | ret->nodelay = nodelay; |
755 | ret->keepalive = keepalive; |
756 | ret->privport = privport; |
757 | ret->port = port; |
758 | |
759 | err = 0; |
760 | do { |
761 | err = try_connect(ret); |
facf7568 |
762 | } while (err && sk_nextaddr(ret->addr, &ret->step)); |
c5e438ec |
763 | |
7555d6a5 |
764 | if (err) |
765 | ret->error = strerror(err); |
f85e6f6e |
766 | |
c5e438ec |
767 | return (Socket) ret; |
768 | } |
769 | |
4efd5997 |
770 | Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int orig_address_family) |
c5e438ec |
771 | { |
c5e438ec |
772 | int s; |
05581745 |
773 | #ifndef NO_IPV6 |
792c5eb5 |
774 | struct addrinfo hints, *ai; |
775 | char portstr[6]; |
776 | #endif |
23e649c9 |
777 | union sockaddr_union u; |
778 | union sockaddr_union *addr; |
adb18b94 |
779 | int addrlen; |
c5e438ec |
780 | Actual_Socket ret; |
781 | int retcode; |
4efd5997 |
782 | int address_family; |
c5e438ec |
783 | int on = 1; |
784 | |
785 | /* |
786 | * Create Socket structure. |
787 | */ |
3d88e64d |
788 | ret = snew(struct Socket_tag); |
2f92b717 |
789 | ret->fn = &tcp_fn_table; |
c5e438ec |
790 | ret->error = NULL; |
791 | ret->plug = plug; |
792 | bufchain_init(&ret->output_data); |
793 | ret->writable = 0; /* to start with */ |
794 | ret->sending_oob = 0; |
795 | ret->frozen = 0; |
c5e438ec |
796 | ret->localhost_only = local_host_only; |
797 | ret->pending_error = 0; |
4efd5997 |
798 | ret->parent = ret->child = NULL; |
c5e438ec |
799 | ret->oobpending = FALSE; |
bc06669b |
800 | ret->outgoingeof = EOF_NO; |
801 | ret->incomingeof = FALSE; |
c5e438ec |
802 | ret->listener = 1; |
7555d6a5 |
803 | ret->addr = NULL; |
c5e438ec |
804 | |
805 | /* |
05581745 |
806 | * Translate address_family from platform-independent constants |
807 | * into local reality. |
808 | */ |
4efd5997 |
809 | address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET : |
40040af1 |
810 | #ifndef NO_IPV6 |
4efd5997 |
811 | orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 : |
40040af1 |
812 | #endif |
813 | AF_UNSPEC); |
05581745 |
814 | |
815 | #ifndef NO_IPV6 |
816 | /* Let's default to IPv6. |
817 | * If the stack doesn't support IPv6, we will fall back to IPv4. */ |
818 | if (address_family == AF_UNSPEC) address_family = AF_INET6; |
819 | #else |
820 | /* No other choice, default to IPv4 */ |
821 | if (address_family == AF_UNSPEC) address_family = AF_INET; |
822 | #endif |
823 | |
824 | /* |
c5e438ec |
825 | * Open socket. |
826 | */ |
05581745 |
827 | s = socket(address_family, SOCK_STREAM, 0); |
828 | |
40040af1 |
829 | #ifndef NO_IPV6 |
05581745 |
830 | /* If the host doesn't support IPv6 try fallback to IPv4. */ |
831 | if (s < 0 && address_family == AF_INET6) { |
832 | address_family = AF_INET; |
833 | s = socket(address_family, SOCK_STREAM, 0); |
834 | } |
40040af1 |
835 | #endif |
c5e438ec |
836 | |
837 | if (s < 0) { |
7555d6a5 |
838 | ret->error = strerror(errno); |
c5e438ec |
839 | return (Socket) ret; |
840 | } |
841 | |
db9b7dce |
842 | cloexec(s); |
89e97516 |
843 | |
c5e438ec |
844 | ret->oobinline = 0; |
845 | |
f7aa4b36 |
846 | if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, |
847 | (const char *)&on, sizeof(on)) < 0) { |
848 | ret->error = strerror(errno); |
849 | close(s); |
850 | return (Socket) ret; |
851 | } |
c5e438ec |
852 | |
adb18b94 |
853 | retcode = -1; |
854 | addr = NULL; addrlen = -1; /* placate optimiser */ |
855 | |
856 | if (srcaddr != NULL) { |
05581745 |
857 | #ifndef NO_IPV6 |
adb18b94 |
858 | hints.ai_flags = AI_NUMERICHOST; |
859 | hints.ai_family = address_family; |
d5474756 |
860 | hints.ai_socktype = SOCK_STREAM; |
adb18b94 |
861 | hints.ai_protocol = 0; |
862 | hints.ai_addrlen = 0; |
863 | hints.ai_addr = NULL; |
864 | hints.ai_canonname = NULL; |
865 | hints.ai_next = NULL; |
062af27b |
866 | assert(port >= 0 && port <= 99999); |
adb18b94 |
867 | sprintf(portstr, "%d", port); |
868 | retcode = getaddrinfo(srcaddr, portstr, &hints, &ai); |
7f8c817c |
869 | if (retcode == 0) { |
23e649c9 |
870 | addr = (union sockaddr_union *)ai->ai_addr; |
ca9892ae |
871 | addrlen = ai->ai_addrlen; |
872 | } |
adb18b94 |
873 | #else |
23e649c9 |
874 | memset(&u,'\0',sizeof u); |
875 | u.sin.sin_family = AF_INET; |
876 | u.sin.sin_port = htons(port); |
877 | u.sin.sin_addr.s_addr = inet_addr(srcaddr); |
878 | if (u.sin.sin_addr.s_addr != (in_addr_t)(-1)) { |
adb18b94 |
879 | /* Override localhost_only with specified listen addr. */ |
23e649c9 |
880 | ret->localhost_only = ipv4_is_loopback(u.sin.sin_addr); |
adb18b94 |
881 | } |
23e649c9 |
882 | addr = &u; |
883 | addrlen = sizeof(u.sin); |
adb18b94 |
884 | retcode = 0; |
792c5eb5 |
885 | #endif |
adb18b94 |
886 | } |
6ee9b735 |
887 | |
adb18b94 |
888 | if (retcode != 0) { |
23e649c9 |
889 | memset(&u,'\0',sizeof u); |
adb18b94 |
890 | #ifndef NO_IPV6 |
891 | if (address_family == AF_INET6) { |
23e649c9 |
892 | u.sin6.sin6_family = AF_INET6; |
893 | u.sin6.sin6_port = htons(port); |
adb18b94 |
894 | if (local_host_only) |
23e649c9 |
895 | u.sin6.sin6_addr = in6addr_loopback; |
adb18b94 |
896 | else |
23e649c9 |
897 | u.sin6.sin6_addr = in6addr_any; |
898 | addr = &u; |
899 | addrlen = sizeof(u.sin6); |
adb18b94 |
900 | } else |
901 | #endif |
902 | { |
23e649c9 |
903 | u.sin.sin_family = AF_INET; |
904 | u.sin.sin_port = htons(port); |
6ee9b735 |
905 | if (local_host_only) |
23e649c9 |
906 | u.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
6ee9b735 |
907 | else |
23e649c9 |
908 | u.sin.sin_addr.s_addr = htonl(INADDR_ANY); |
909 | addr = &u; |
910 | addrlen = sizeof(u.sin); |
adb18b94 |
911 | } |
c5e438ec |
912 | } |
913 | |
23e649c9 |
914 | retcode = bind(s, &addr->sa, addrlen); |
adb18b94 |
915 | if (retcode < 0) { |
916 | close(s); |
7555d6a5 |
917 | ret->error = strerror(errno); |
c5e438ec |
918 | return (Socket) ret; |
919 | } |
920 | |
c5e438ec |
921 | if (listen(s, SOMAXCONN) < 0) { |
922 | close(s); |
7555d6a5 |
923 | ret->error = strerror(errno); |
c5e438ec |
924 | return (Socket) ret; |
925 | } |
926 | |
4efd5997 |
927 | #ifndef NO_IPV6 |
928 | /* |
929 | * If we were given ADDRTYPE_UNSPEC, we must also create an |
930 | * IPv4 listening socket and link it to this one. |
931 | */ |
932 | if (address_family == AF_INET6 && orig_address_family == ADDRTYPE_UNSPEC) { |
933 | Actual_Socket other; |
934 | |
935 | other = (Actual_Socket) sk_newlistener(srcaddr, port, plug, |
936 | local_host_only, ADDRTYPE_IPV4); |
937 | |
938 | if (other) { |
939 | if (!other->error) { |
940 | other->parent = ret; |
941 | ret->child = other; |
942 | } else { |
943 | /* If we couldn't create a listening socket on IPv4 as well |
944 | * as IPv6, we must return an error overall. */ |
945 | close(s); |
946 | sfree(ret); |
947 | return (Socket) other; |
948 | } |
949 | } |
950 | } |
951 | #endif |
952 | |
adb18b94 |
953 | ret->s = s; |
954 | |
0ff9ea38 |
955 | uxsel_tell(ret); |
c5e438ec |
956 | add234(sktree, ret); |
957 | |
958 | return (Socket) ret; |
959 | } |
960 | |
961 | static void sk_tcp_close(Socket sock) |
962 | { |
963 | Actual_Socket s = (Actual_Socket) sock; |
964 | |
4efd5997 |
965 | if (s->child) |
966 | sk_tcp_close((Socket)s->child); |
967 | |
0ff9ea38 |
968 | uxsel_del(s->s); |
c5e438ec |
969 | del234(sktree, s); |
970 | close(s->s); |
7555d6a5 |
971 | if (s->addr) |
972 | sk_addr_free(s->addr); |
c5e438ec |
973 | sfree(s); |
974 | } |
975 | |
0a4022a1 |
976 | void *sk_getxdmdata(void *sock, int *lenp) |
2f92b717 |
977 | { |
978 | Actual_Socket s = (Actual_Socket) sock; |
c44e5afc |
979 | union sockaddr_union u; |
2f92b717 |
980 | socklen_t addrlen; |
0a4022a1 |
981 | char *buf; |
982 | static unsigned int unix_addr = 0xFFFFFFFF; |
2f92b717 |
983 | |
984 | /* |
985 | * We must check that this socket really _is_ an Actual_Socket. |
986 | */ |
987 | if (s->fn != &tcp_fn_table) |
0a4022a1 |
988 | return NULL; /* failure */ |
2f92b717 |
989 | |
c44e5afc |
990 | addrlen = sizeof(u); |
991 | if (getsockname(s->s, &u.sa, &addrlen) < 0) |
0a4022a1 |
992 | return NULL; |
c44e5afc |
993 | switch(u.sa.sa_family) { |
fc0f17db |
994 | case AF_INET: |
0a4022a1 |
995 | *lenp = 6; |
996 | buf = snewn(*lenp, char); |
c44e5afc |
997 | PUT_32BIT_MSB_FIRST(buf, ntohl(u.sin.sin_addr.s_addr)); |
998 | PUT_16BIT_MSB_FIRST(buf+4, ntohs(u.sin.sin_port)); |
fc0f17db |
999 | break; |
0a4022a1 |
1000 | #ifndef NO_IPV6 |
1001 | case AF_INET6: |
1002 | *lenp = 6; |
1003 | buf = snewn(*lenp, char); |
c44e5afc |
1004 | if (IN6_IS_ADDR_V4MAPPED(&u.sin6.sin6_addr)) { |
1005 | memcpy(buf, u.sin6.sin6_addr.s6_addr + 12, 4); |
1006 | PUT_16BIT_MSB_FIRST(buf+4, ntohs(u.sin6.sin6_port)); |
0a4022a1 |
1007 | } else |
1008 | /* This is stupid, but it's what XLib does. */ |
1009 | memset(buf, 0, 6); |
1010 | break; |
1011 | #endif |
fc0f17db |
1012 | case AF_UNIX: |
0a4022a1 |
1013 | *lenp = 6; |
1014 | buf = snewn(*lenp, char); |
1015 | PUT_32BIT_MSB_FIRST(buf, unix_addr--); |
1016 | PUT_16BIT_MSB_FIRST(buf+4, getpid()); |
fc0f17db |
1017 | break; |
2f92b717 |
1018 | |
fc0f17db |
1019 | /* XXX IPV6 */ |
1020 | |
1021 | default: |
0a4022a1 |
1022 | return NULL; |
fc0f17db |
1023 | } |
2f92b717 |
1024 | |
0a4022a1 |
1025 | return buf; |
2f92b717 |
1026 | } |
1027 | |
c5e438ec |
1028 | /* |
1029 | * The function which tries to send on a socket once it's deemed |
1030 | * writable. |
1031 | */ |
1032 | void try_send(Actual_Socket s) |
1033 | { |
1034 | while (s->sending_oob || bufchain_size(&s->output_data) > 0) { |
1035 | int nsent; |
1036 | int err; |
1037 | void *data; |
1038 | int len, urgentflag; |
1039 | |
1040 | if (s->sending_oob) { |
1041 | urgentflag = MSG_OOB; |
1042 | len = s->sending_oob; |
1043 | data = &s->oobdata; |
1044 | } else { |
1045 | urgentflag = 0; |
1046 | bufchain_prefix(&s->output_data, &data, &len); |
1047 | } |
1048 | nsent = send(s->s, data, len, urgentflag); |
1049 | noise_ultralight(nsent); |
1050 | if (nsent <= 0) { |
1051 | err = (nsent < 0 ? errno : 0); |
1052 | if (err == EWOULDBLOCK) { |
1053 | /* |
1054 | * Perfectly normal: we've sent all we can for the moment. |
1055 | */ |
1056 | s->writable = FALSE; |
1057 | return; |
f3e0acda |
1058 | } else { |
c5e438ec |
1059 | /* |
f3e0acda |
1060 | * We unfortunately can't just call plug_closing(), |
c5e438ec |
1061 | * because it's quite likely that we're currently |
1062 | * _in_ a call from the code we'd be calling back |
1063 | * to, so we'd have to make half the SSH code |
1064 | * reentrant. Instead we flag a pending error on |
1065 | * the socket, to be dealt with (by calling |
1066 | * plug_closing()) at some suitable future moment. |
1067 | */ |
1068 | s->pending_error = err; |
43a1c4a4 |
1069 | /* |
1070 | * Immediately cease selecting on this socket, so that |
1071 | * we don't tight-loop repeatedly trying to do |
1072 | * whatever it was that went wrong. |
1073 | */ |
1074 | uxsel_tell(s); |
1075 | /* |
1076 | * Notify the front end that it might want to call us. |
1077 | */ |
1078 | frontend_net_error_pending(); |
c5e438ec |
1079 | return; |
c5e438ec |
1080 | } |
1081 | } else { |
1082 | if (s->sending_oob) { |
1083 | if (nsent < len) { |
1084 | memmove(s->oobdata, s->oobdata+nsent, len-nsent); |
1085 | s->sending_oob = len - nsent; |
1086 | } else { |
1087 | s->sending_oob = 0; |
1088 | } |
1089 | } else { |
1090 | bufchain_consume(&s->output_data, nsent); |
1091 | } |
1092 | } |
1093 | } |
bc06669b |
1094 | |
1095 | /* |
1096 | * If we reach here, we've finished sending everything we might |
1097 | * have needed to send. Send EOF, if we need to. |
1098 | */ |
1099 | if (s->outgoingeof == EOF_PENDING) { |
1100 | shutdown(s->s, SHUT_WR); |
1101 | s->outgoingeof = EOF_SENT; |
1102 | } |
1103 | |
1104 | /* |
1105 | * Also update the select status, because we don't need to select |
1106 | * for writing any more. |
1107 | */ |
0ff9ea38 |
1108 | uxsel_tell(s); |
c5e438ec |
1109 | } |
1110 | |
e0e7dff8 |
1111 | static int sk_tcp_write(Socket sock, const char *buf, int len) |
c5e438ec |
1112 | { |
1113 | Actual_Socket s = (Actual_Socket) sock; |
1114 | |
bc06669b |
1115 | assert(s->outgoingeof == EOF_NO); |
1116 | |
c5e438ec |
1117 | /* |
1118 | * Add the data to the buffer list on the socket. |
1119 | */ |
1120 | bufchain_add(&s->output_data, buf, len); |
1121 | |
1122 | /* |
1123 | * Now try sending from the start of the buffer list. |
1124 | */ |
1125 | if (s->writable) |
1126 | try_send(s); |
1127 | |
c2a71b0d |
1128 | /* |
1129 | * Update the select() status to correctly reflect whether or |
1130 | * not we should be selecting for write. |
1131 | */ |
1132 | uxsel_tell(s); |
1133 | |
c5e438ec |
1134 | return bufchain_size(&s->output_data); |
1135 | } |
1136 | |
e0e7dff8 |
1137 | static int sk_tcp_write_oob(Socket sock, const char *buf, int len) |
c5e438ec |
1138 | { |
1139 | Actual_Socket s = (Actual_Socket) sock; |
1140 | |
bc06669b |
1141 | assert(s->outgoingeof == EOF_NO); |
1142 | |
c5e438ec |
1143 | /* |
1144 | * Replace the buffer list on the socket with the data. |
1145 | */ |
1146 | bufchain_clear(&s->output_data); |
1147 | assert(len <= sizeof(s->oobdata)); |
1148 | memcpy(s->oobdata, buf, len); |
1149 | s->sending_oob = len; |
1150 | |
1151 | /* |
1152 | * Now try sending from the start of the buffer list. |
1153 | */ |
1154 | if (s->writable) |
1155 | try_send(s); |
1156 | |
c2a71b0d |
1157 | /* |
1158 | * Update the select() status to correctly reflect whether or |
1159 | * not we should be selecting for write. |
1160 | */ |
1161 | uxsel_tell(s); |
1162 | |
c5e438ec |
1163 | return s->sending_oob; |
1164 | } |
1165 | |
bc06669b |
1166 | static void sk_tcp_write_eof(Socket sock) |
1167 | { |
1168 | Actual_Socket s = (Actual_Socket) sock; |
1169 | |
1170 | assert(s->outgoingeof == EOF_NO); |
1171 | |
1172 | /* |
1173 | * Mark the socket as pending outgoing EOF. |
1174 | */ |
1175 | s->outgoingeof = EOF_PENDING; |
1176 | |
1177 | /* |
1178 | * Now try sending from the start of the buffer list. |
1179 | */ |
1180 | if (s->writable) |
1181 | try_send(s); |
1182 | |
1183 | /* |
1184 | * Update the select() status to correctly reflect whether or |
1185 | * not we should be selecting for write. |
1186 | */ |
1187 | uxsel_tell(s); |
1188 | } |
1189 | |
0ff9ea38 |
1190 | static int net_select_result(int fd, int event) |
c5e438ec |
1191 | { |
1192 | int ret; |
c5e438ec |
1193 | char buf[20480]; /* nice big buffer for plenty of speed */ |
1194 | Actual_Socket s; |
1195 | u_long atmark; |
1196 | |
1197 | /* Find the Socket structure */ |
f7f9fb5c |
1198 | s = find234(sktree, &fd, cmpforsearch); |
c5e438ec |
1199 | if (!s) |
1200 | return 1; /* boggle */ |
1201 | |
1202 | noise_ultralight(event); |
1203 | |
1204 | switch (event) { |
c5e438ec |
1205 | case 4: /* exceptional */ |
1206 | if (!s->oobinline) { |
1207 | /* |
1208 | * On a non-oobinline socket, this indicates that we |
1209 | * can immediately perform an OOB read and get back OOB |
1210 | * data, which we will send to the back end with |
1211 | * type==2 (urgent data). |
1212 | */ |
1213 | ret = recv(s->s, buf, sizeof(buf), MSG_OOB); |
1214 | noise_ultralight(ret); |
1215 | if (ret <= 0) { |
f3e0acda |
1216 | return plug_closing(s->plug, |
1217 | ret == 0 ? "Internal networking trouble" : |
1218 | strerror(errno), errno, 0); |
c5e438ec |
1219 | } else { |
7555d6a5 |
1220 | /* |
1221 | * Receiving actual data on a socket means we can |
1222 | * stop falling back through the candidate |
1223 | * addresses to connect to. |
1224 | */ |
1225 | if (s->addr) { |
1226 | sk_addr_free(s->addr); |
1227 | s->addr = NULL; |
1228 | } |
c5e438ec |
1229 | return plug_receive(s->plug, 2, buf, ret); |
1230 | } |
1231 | break; |
1232 | } |
1233 | |
1234 | /* |
1235 | * If we reach here, this is an oobinline socket, which |
56e5b2db |
1236 | * means we should set s->oobpending and then deal with it |
1237 | * when we get called for the readability event (which |
1238 | * should also occur). |
c5e438ec |
1239 | */ |
1240 | s->oobpending = TRUE; |
56e5b2db |
1241 | break; |
c5e438ec |
1242 | case 1: /* readable; also acceptance */ |
1243 | if (s->listener) { |
1244 | /* |
1245 | * On a listening socket, the readability event means a |
1246 | * connection is ready to be accepted. |
1247 | */ |
23e649c9 |
1248 | union sockaddr_union su; |
1249 | socklen_t addrlen = sizeof(su); |
c5e438ec |
1250 | int t; /* socket of connection */ |
1251 | |
23e649c9 |
1252 | memset(&su, 0, addrlen); |
1253 | t = accept(s->s, &su.sa, &addrlen); |
c5e438ec |
1254 | if (t < 0) { |
1255 | break; |
1256 | } |
1257 | |
f9520103 |
1258 | nonblock(t); |
068ba98c |
1259 | |
19a4130c |
1260 | if (s->localhost_only && |
23e649c9 |
1261 | !sockaddr_is_loopback(&su.sa)) { |
c5e438ec |
1262 | close(t); /* someone let nonlocal through?! */ |
f7f9fb5c |
1263 | } else if (plug_accepting(s->plug, t)) { |
c5e438ec |
1264 | close(t); /* denied or error */ |
1265 | } |
1266 | break; |
1267 | } |
1268 | |
1269 | /* |
1270 | * If we reach here, this is not a listening socket, so |
1271 | * readability really means readability. |
1272 | */ |
1273 | |
1274 | /* In the case the socket is still frozen, we don't even bother */ |
d7d6c34d |
1275 | if (s->frozen) |
c5e438ec |
1276 | break; |
c5e438ec |
1277 | |
1278 | /* |
1279 | * We have received data on the socket. For an oobinline |
1280 | * socket, this might be data _before_ an urgent pointer, |
1281 | * in which case we send it to the back end with type==1 |
1282 | * (data prior to urgent). |
1283 | */ |
1284 | if (s->oobinline && s->oobpending) { |
1285 | atmark = 1; |
1286 | if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark) |
1287 | s->oobpending = FALSE; /* clear this indicator */ |
1288 | } else |
1289 | atmark = 1; |
1290 | |
56e5b2db |
1291 | ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0); |
c5e438ec |
1292 | noise_ultralight(ret); |
1293 | if (ret < 0) { |
1294 | if (errno == EWOULDBLOCK) { |
1295 | break; |
1296 | } |
1297 | } |
1298 | if (ret < 0) { |
7555d6a5 |
1299 | /* |
1300 | * An error at this point _might_ be an error reported |
1301 | * by a non-blocking connect(). So before we return a |
1302 | * panic status to the user, let's just see whether |
1303 | * that's the case. |
1304 | */ |
1305 | int err = errno; |
1306 | if (s->addr) { |
1307 | plug_log(s->plug, 1, s->addr, s->port, strerror(err), err); |
facf7568 |
1308 | while (s->addr && sk_nextaddr(s->addr, &s->step)) { |
7555d6a5 |
1309 | err = try_connect(s); |
1310 | } |
1311 | } |
1312 | if (err != 0) |
1313 | return plug_closing(s->plug, strerror(err), err, 0); |
c5e438ec |
1314 | } else if (0 == ret) { |
bc06669b |
1315 | s->incomingeof = TRUE; /* stop trying to read now */ |
1316 | uxsel_tell(s); |
c5e438ec |
1317 | return plug_closing(s->plug, NULL, 0, 0); |
1318 | } else { |
7555d6a5 |
1319 | /* |
1320 | * Receiving actual data on a socket means we can |
1321 | * stop falling back through the candidate |
1322 | * addresses to connect to. |
1323 | */ |
1324 | if (s->addr) { |
1325 | sk_addr_free(s->addr); |
1326 | s->addr = NULL; |
1327 | } |
c5e438ec |
1328 | return plug_receive(s->plug, atmark ? 0 : 1, buf, ret); |
1329 | } |
1330 | break; |
1331 | case 2: /* writable */ |
051dd789 |
1332 | if (!s->connected) { |
1333 | /* |
1334 | * select() reports a socket as _writable_ when an |
1335 | * asynchronous connection is completed. |
1336 | */ |
1337 | s->connected = s->writable = 1; |
0ff9ea38 |
1338 | uxsel_tell(s); |
051dd789 |
1339 | break; |
1340 | } else { |
c5e438ec |
1341 | int bufsize_before, bufsize_after; |
1342 | s->writable = 1; |
1343 | bufsize_before = s->sending_oob + bufchain_size(&s->output_data); |
1344 | try_send(s); |
1345 | bufsize_after = s->sending_oob + bufchain_size(&s->output_data); |
1346 | if (bufsize_after < bufsize_before) |
1347 | plug_sent(s->plug, bufsize_after); |
1348 | } |
1349 | break; |
1350 | } |
1351 | |
1352 | return 1; |
1353 | } |
1354 | |
1355 | /* |
1356 | * Deal with socket errors detected in try_send(). |
1357 | */ |
1358 | void net_pending_errors(void) |
1359 | { |
1360 | int i; |
1361 | Actual_Socket s; |
1362 | |
1363 | /* |
1364 | * This might be a fiddly business, because it's just possible |
1365 | * that handling a pending error on one socket might cause |
1366 | * others to be closed. (I can't think of any reason this might |
1367 | * happen in current SSH implementation, but to maintain |
1368 | * generality of this network layer I'll assume the worst.) |
1369 | * |
1370 | * So what we'll do is search the socket list for _one_ socket |
1371 | * with a pending error, and then handle it, and then search |
1372 | * the list again _from the beginning_. Repeat until we make a |
1373 | * pass with no socket errors present. That way we are |
1374 | * protected against the socket list changing under our feet. |
1375 | */ |
1376 | |
1377 | do { |
1378 | for (i = 0; (s = index234(sktree, i)) != NULL; i++) { |
1379 | if (s->pending_error) { |
1380 | /* |
1381 | * An error has occurred on this socket. Pass it to the |
1382 | * plug. |
1383 | */ |
7555d6a5 |
1384 | plug_closing(s->plug, strerror(s->pending_error), |
c5e438ec |
1385 | s->pending_error, 0); |
1386 | break; |
1387 | } |
1388 | } |
1389 | } while (s); |
1390 | } |
1391 | |
1392 | /* |
1393 | * Each socket abstraction contains a `void *' private field in |
1394 | * which the client can keep state. |
1395 | */ |
1396 | static void sk_tcp_set_private_ptr(Socket sock, void *ptr) |
1397 | { |
1398 | Actual_Socket s = (Actual_Socket) sock; |
1399 | s->private_ptr = ptr; |
1400 | } |
1401 | |
1402 | static void *sk_tcp_get_private_ptr(Socket sock) |
1403 | { |
1404 | Actual_Socket s = (Actual_Socket) sock; |
1405 | return s->private_ptr; |
1406 | } |
1407 | |
1408 | /* |
1409 | * Special error values are returned from sk_namelookup and sk_new |
1410 | * if there's a problem. These functions extract an error message, |
1411 | * or return NULL if there's no problem. |
1412 | */ |
cbe2d68f |
1413 | const char *sk_addr_error(SockAddr addr) |
c5e438ec |
1414 | { |
1415 | return addr->error; |
1416 | } |
cbe2d68f |
1417 | static const char *sk_tcp_socket_error(Socket sock) |
c5e438ec |
1418 | { |
1419 | Actual_Socket s = (Actual_Socket) sock; |
1420 | return s->error; |
1421 | } |
1422 | |
1423 | static void sk_tcp_set_frozen(Socket sock, int is_frozen) |
1424 | { |
1425 | Actual_Socket s = (Actual_Socket) sock; |
1426 | if (s->frozen == is_frozen) |
1427 | return; |
1428 | s->frozen = is_frozen; |
0ff9ea38 |
1429 | uxsel_tell(s); |
c5e438ec |
1430 | } |
1431 | |
0ff9ea38 |
1432 | static void uxsel_tell(Actual_Socket s) |
c5e438ec |
1433 | { |
0ff9ea38 |
1434 | int rwx = 0; |
43a1c4a4 |
1435 | if (!s->pending_error) { |
1436 | if (s->listener) { |
1437 | rwx |= 1; /* read == accept */ |
1438 | } else { |
1439 | if (!s->connected) |
1440 | rwx |= 2; /* write == connect */ |
1441 | if (s->connected && !s->frozen && !s->incomingeof) |
1442 | rwx |= 1 | 4; /* read, except */ |
1443 | if (bufchain_size(&s->output_data)) |
1444 | rwx |= 2; /* write */ |
1445 | } |
1cfc764f |
1446 | } |
0ff9ea38 |
1447 | uxsel_set(s->s, rwx, net_select_result); |
c5e438ec |
1448 | } |
1449 | |
1450 | int net_service_lookup(char *service) |
1451 | { |
1452 | struct servent *se; |
1453 | se = getservbyname(service, NULL); |
1454 | if (se != NULL) |
1455 | return ntohs(se->s_port); |
1456 | else |
1457 | return 0; |
1458 | } |
fc0f17db |
1459 | |
42547ed9 |
1460 | char *get_hostname(void) |
1461 | { |
1462 | int len = 128; |
1463 | char *hostname = NULL; |
1464 | do { |
1465 | len *= 2; |
1466 | hostname = sresize(hostname, len, char); |
1467 | if ((gethostname(hostname, len) < 0) && |
1468 | (errno != ENAMETOOLONG)) { |
1469 | sfree(hostname); |
1470 | hostname = NULL; |
1471 | break; |
1472 | } |
1473 | } while (strlen(hostname) >= len-1); |
1474 | return hostname; |
1475 | } |
1476 | |
8def70c3 |
1477 | SockAddr platform_get_x11_unix_address(const char *sockpath, int displaynum) |
fc0f17db |
1478 | { |
1479 | SockAddr ret = snew(struct SockAddr_tag); |
1480 | int n; |
1481 | |
1482 | memset(ret, 0, sizeof *ret); |
facf7568 |
1483 | ret->superfamily = UNIX; |
48806a46 |
1484 | /* |
8def70c3 |
1485 | * In special circumstances (notably Mac OS X Leopard), we'll |
1486 | * have been passed an explicit Unix socket path. |
48806a46 |
1487 | */ |
8def70c3 |
1488 | if (sockpath) { |
48806a46 |
1489 | n = snprintf(ret->hostname, sizeof ret->hostname, |
8def70c3 |
1490 | "%s", sockpath); |
48806a46 |
1491 | } else { |
1492 | n = snprintf(ret->hostname, sizeof ret->hostname, |
1493 | "%s%d", X11_UNIX_PATH, displaynum); |
1494 | } |
8def70c3 |
1495 | |
1496 | if (n < 0) |
fc0f17db |
1497 | ret->error = "snprintf failed"; |
8def70c3 |
1498 | else if (n >= sizeof ret->hostname) |
fc0f17db |
1499 | ret->error = "X11 UNIX name too long"; |
8def70c3 |
1500 | |
7555d6a5 |
1501 | #ifndef NO_IPV6 |
facf7568 |
1502 | ret->ais = NULL; |
7555d6a5 |
1503 | #else |
1504 | ret->addresses = NULL; |
facf7568 |
1505 | ret->naddresses = 0; |
7555d6a5 |
1506 | #endif |
5025a993 |
1507 | ret->refcount = 1; |
fc0f17db |
1508 | return ret; |
1509 | } |