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