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