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