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