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