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