2 * Unix networking abstraction.
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>
20 #define DEFINE_PLUG_METHOD_MACROS
26 # define X11_UNIX_PATH "/tmp/.X11-unix/X"
29 #define ipv4_is_loopback(addr) (inet_netof(addr) == IN_LOOPBACKNET)
32 struct socket_function_table
*fn
;
33 /* the above variable absolutely *must* be the first in this structure */
41 int frozen
; /* this causes readability notifications to be ignored */
42 int frozen_readable
; /* this means we missed at least one readability
43 * notification while we were frozen */
44 int localhost_only
; /* for listening sockets */
47 int oobpending
; /* is there OOB data available to read? */
49 int pending_error
; /* in case send() returns error */
54 * We used to typedef struct Socket_tag *Socket.
56 * Since we have made the networking abstraction slightly more
57 * abstract, Socket no longer means a tcp socket (it could mean
58 * an ssl socket). So now we must use Actual_Socket when we know
59 * we are talking about a tcp socket.
61 typedef struct Socket_tag
*Actual_Socket
;
66 * Which address family this address belongs to. AF_INET for
67 * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
68 * resolution has not been done and a simple host name is held
69 * in this SockAddr structure.
73 struct addrinfo
*ai
; /* Address IPv6 style. */
75 unsigned long address
; /* Address IPv4 style. */
77 char hostname
[512]; /* Store an unresolved host name. */
80 static tree234
*sktree
;
82 static void uxsel_tell(Actual_Socket s
);
84 static int cmpfortree(void *av
, void *bv
)
86 Actual_Socket a
= (Actual_Socket
) av
, b
= (Actual_Socket
) bv
;
87 int as
= a
->s
, bs
= b
->s
;
95 static int cmpforsearch(void *av
, void *bv
)
97 Actual_Socket b
= (Actual_Socket
) bv
;
98 int as
= *(int *)av
, bs
= b
->s
;
108 sktree
= newtree234(cmpfortree
);
111 void sk_cleanup(void)
117 for (i
= 0; (s
= index234(sktree
, i
)) != NULL
; i
++) {
123 const char *error_string(int error
)
125 return strerror(error
);
128 SockAddr
sk_namelookup(const char *host
, char **canonicalname
, int address_family
)
130 SockAddr ret
= snew(struct SockAddr_tag
);
132 struct addrinfo hints
;
136 struct hostent
*h
= NULL
;
140 /* Clear the structure and default to IPv4. */
141 memset(ret
, 0, sizeof(struct SockAddr_tag
));
142 ret
->family
= 0; /* We set this one when we have resolved the host. */
147 hints
.ai_flags
= AI_CANONNAME
;
148 hints
.ai_family
= (address_family
== ADDRTYPE_IPV4 ? AF_INET
:
149 address_family
== ADDRTYPE_IPV6 ? AF_INET6
:
151 hints
.ai_socktype
= SOCK_STREAM
;
152 hints
.ai_protocol
= 0;
153 hints
.ai_addrlen
= 0;
154 hints
.ai_addr
= NULL
;
155 hints
.ai_canonname
= NULL
;
156 hints
.ai_next
= NULL
;
157 err
= getaddrinfo(host
, NULL
, &hints
, &ret
->ai
);
159 ret
->error
= gai_strerror(err
);
162 ret
->family
= ret
->ai
->ai_family
;
164 if (ret
->ai
->ai_canonname
!= NULL
)
165 strncat(realhost
, ret
->ai
->ai_canonname
, sizeof(realhost
) - 1);
167 strncat(realhost
, host
, sizeof(realhost
) - 1);
169 if ((a
= inet_addr(host
)) == (unsigned long) INADDR_NONE
) {
171 * Otherwise use the IPv4-only gethostbyname... (NOTE:
172 * we don't use gethostbyname as a fallback!)
174 if (ret
->family
== 0) {
175 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
176 if ( (h
= gethostbyname(host
)) )
177 ret
->family
= AF_INET
;
179 if (ret
->family
== 0) {
180 ret
->error
= (h_errno
== HOST_NOT_FOUND
||
181 h_errno
== NO_DATA
||
182 h_errno
== NO_ADDRESS ?
"Host does not exist" :
183 h_errno
== TRY_AGAIN ?
184 "Temporary name service failure" :
185 "gethostbyname: unknown error");
188 memcpy(&a
, h
->h_addr
, sizeof(a
));
189 /* This way we are always sure the h->h_name is valid :) */
190 strncpy(realhost
, h
->h_name
, sizeof(realhost
));
193 * This must be a numeric IPv4 address because it caused a
194 * success return from inet_addr.
196 ret
->family
= AF_INET
;
197 strncpy(realhost
, host
, sizeof(realhost
));
199 ret
->address
= ntohl(a
);
201 realhost
[lenof(realhost
)-1] = '\0';
202 *canonicalname
= snewn(1+strlen(realhost
), char);
203 strcpy(*canonicalname
, realhost
);
207 SockAddr
sk_nonamelookup(const char *host
)
209 SockAddr ret
= snew(struct SockAddr_tag
);
211 ret
->family
= AF_UNSPEC
;
212 strncpy(ret
->hostname
, host
, lenof(ret
->hostname
));
213 ret
->hostname
[lenof(ret
->hostname
)-1] = '\0';
220 void sk_getaddr(SockAddr addr
, char *buf
, int buflen
)
223 if (addr
->family
== AF_UNSPEC
) {
224 strncpy(buf
, addr
->hostname
, buflen
);
225 buf
[buflen
-1] = '\0';
228 if (getnameinfo(addr
->ai
->ai_addr
, addr
->ai
->ai_addrlen
, buf
, buflen
,
229 NULL
, 0, NI_NUMERICHOST
) != 0) {
231 strncat(buf
, "<unknown>", buflen
- 1);
235 assert(addr
->family
== AF_INET
);
236 a
.s_addr
= htonl(addr
->address
);
237 strncpy(buf
, inet_ntoa(a
), buflen
);
238 buf
[buflen
-1] = '\0';
243 int sk_hostname_is_local(char *name
)
245 return !strcmp(name
, "localhost");
248 int sk_address_is_local(SockAddr addr
)
251 if (addr
->family
== AF_UNSPEC
)
252 return 0; /* we don't know; assume not */
255 if (addr
->family
== AF_INET
)
256 return ipv4_is_loopback(
257 ((struct sockaddr_in
*)addr
->ai
->ai_addr
)->sin_addr
);
258 else if (addr
->family
== AF_INET6
)
259 return IN6_IS_ADDR_LOOPBACK(
260 &((struct sockaddr_in6
*)addr
->ai
->ai_addr
)->sin6_addr
);
265 assert(addr
->family
== AF_INET
);
266 a
.s_addr
= htonl(addr
->address
);
267 return ipv4_is_loopback(a
);
272 int sk_addrtype(SockAddr addr
)
274 return (addr
->family
== AF_INET ? ADDRTYPE_IPV4
:
276 addr
->family
== AF_INET6 ? ADDRTYPE_IPV6
:
281 void sk_addrcopy(SockAddr addr
, char *buf
)
285 if (addr
->family
== AF_INET
)
286 memcpy(buf
, &((struct sockaddr_in
*)addr
->ai
->ai_addr
)->sin_addr
,
287 sizeof(struct in_addr
));
288 else if (addr
->family
== AF_INET6
)
289 memcpy(buf
, &((struct sockaddr_in6
*)addr
->ai
->ai_addr
)->sin6_addr
,
290 sizeof(struct in6_addr
));
296 assert(addr
->family
== AF_INET
);
297 a
.s_addr
= htonl(addr
->address
);
298 memcpy(buf
, (char*) &a
.s_addr
, 4);
302 void sk_addr_free(SockAddr addr
)
306 if (addr
->ai
!= NULL
)
307 freeaddrinfo(addr
->ai
);
312 static Plug
sk_tcp_plug(Socket sock
, Plug p
)
314 Actual_Socket s
= (Actual_Socket
) sock
;
321 static void sk_tcp_flush(Socket s
)
324 * We send data to the socket as soon as we can anyway,
325 * so we don't need to do anything here. :-)
329 static void sk_tcp_close(Socket s
);
330 static int sk_tcp_write(Socket s
, const char *data
, int len
);
331 static int sk_tcp_write_oob(Socket s
, const char *data
, int len
);
332 static void sk_tcp_set_private_ptr(Socket s
, void *ptr
);
333 static void *sk_tcp_get_private_ptr(Socket s
);
334 static void sk_tcp_set_frozen(Socket s
, int is_frozen
);
335 static const char *sk_tcp_socket_error(Socket s
);
337 static struct socket_function_table tcp_fn_table
= {
343 sk_tcp_set_private_ptr
,
344 sk_tcp_get_private_ptr
,
349 Socket
sk_register(OSSocket sockfd
, Plug plug
)
354 * Create Socket structure.
356 ret
= snew(struct Socket_tag
);
357 ret
->fn
= &tcp_fn_table
;
360 bufchain_init(&ret
->output_data
);
361 ret
->writable
= 1; /* to start with */
362 ret
->sending_oob
= 0;
364 ret
->frozen_readable
= 0;
365 ret
->localhost_only
= 0; /* unused, but best init anyway */
366 ret
->pending_error
= 0;
367 ret
->oobpending
= FALSE
;
373 ret
->error
= error_string(errno
);
385 Socket
sk_new(SockAddr addr
, int port
, int privport
, int oobinline
,
386 int nodelay
, int keepalive
, Plug plug
)
390 struct sockaddr_in6 a6
;
392 struct sockaddr_in a
;
393 struct sockaddr_un au
;
394 const struct sockaddr
*sa
;
401 * Create Socket structure.
403 ret
= snew(struct Socket_tag
);
404 ret
->fn
= &tcp_fn_table
;
407 bufchain_init(&ret
->output_data
);
408 ret
->connected
= 0; /* to start with */
409 ret
->writable
= 0; /* to start with */
410 ret
->sending_oob
= 0;
412 ret
->frozen_readable
= 0;
413 ret
->localhost_only
= 0; /* unused, but best init anyway */
414 ret
->pending_error
= 0;
415 ret
->oobpending
= FALSE
;
421 assert(addr
->family
!= AF_UNSPEC
);
422 s
= socket(addr
->family
, SOCK_STREAM
, 0);
426 ret
->error
= error_string(errno
);
430 ret
->oobinline
= oobinline
;
433 setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, (void *) &b
, sizeof(b
));
438 setsockopt(s
, IPPROTO_TCP
, TCP_NODELAY
, (void *) &b
, sizeof(b
));
443 setsockopt(s
, SOL_SOCKET
, SO_KEEPALIVE
, (void *) &b
, sizeof(b
));
447 * Bind to local address.
450 localport
= 1023; /* count from 1023 downwards */
452 localport
= 0; /* just use port 0 (ie kernel picks) */
454 /* BSD IP stacks need sockaddr_in zeroed before filling in */
455 memset(&a
,'\0',sizeof(struct sockaddr_in
));
457 memset(&a6
,'\0',sizeof(struct sockaddr_in6
));
460 /* We don't try to bind to a local address for UNIX domain sockets. (Why
461 * do we bother doing the bind when localport == 0 anyway?) */
462 if(addr
->family
!= AF_UNIX
) {
463 /* Loop round trying to bind */
468 if (addr
->family
== AF_INET6
) {
469 /* XXX use getaddrinfo to get a local address? */
470 a6
.sin6_family
= AF_INET6
;
471 a6
.sin6_addr
= in6addr_any
;
472 a6
.sin6_port
= htons(localport
);
473 retcode
= bind(s
, (struct sockaddr
*) &a6
, sizeof(a6
));
477 assert(addr
->family
== AF_INET
);
478 a
.sin_family
= AF_INET
;
479 a
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
480 a
.sin_port
= htons(localport
);
481 retcode
= bind(s
, (struct sockaddr
*) &a
, sizeof(a
));
488 if (err
!= EADDRINUSE
) /* failed, for a bad reason */
493 break; /* we're only looping once */
496 break; /* we might have got to the end */
500 ret
->error
= error_string(err
);
506 * Connect to remote address.
508 switch(addr
->family
) {
511 /* XXX would be better to have got getaddrinfo() to fill in the port. */
512 ((struct sockaddr_in
*)addr
->ai
->ai_addr
)->sin_port
=
514 sa
= (const struct sockaddr
*)addr
->ai
->ai_addr
;
515 salen
= addr
->ai
->ai_addrlen
;
518 ((struct sockaddr_in
*)addr
->ai
->ai_addr
)->sin_port
=
520 sa
= (const struct sockaddr
*)addr
->ai
->ai_addr
;
521 salen
= addr
->ai
->ai_addrlen
;
525 a
.sin_family
= AF_INET
;
526 a
.sin_addr
.s_addr
= htonl(addr
->address
);
527 a
.sin_port
= htons((short) port
);
528 sa
= (const struct sockaddr
*)&a
;
533 assert(port
== 0); /* to catch confused people */
534 assert(strlen(addr
->hostname
) < sizeof au
.sun_path
);
535 memset(&au
, 0, sizeof au
);
536 au
.sun_family
= AF_UNIX
;
537 strcpy(au
.sun_path
, addr
->hostname
);
538 sa
= (const struct sockaddr
*)&au
;
543 assert(0 && "unknown address family");
546 fl
= fcntl(s
, F_GETFL
);
548 fcntl(s
, F_SETFL
, fl
| O_NONBLOCK
);
550 if ((connect(s
, sa
, salen
)) < 0) {
551 if ( errno
!= EINPROGRESS
) {
552 ret
->error
= error_string(errno
);
557 * If we _don't_ get EWOULDBLOCK, the connect has completed
558 * and we should set the socket as connected and writable.
572 Socket
sk_newlistener(char *srcaddr
, int port
, Plug plug
, int local_host_only
, int address_family
)
576 struct addrinfo hints
, *ai
;
579 struct sockaddr_in a
;
586 * Create Socket structure.
588 ret
= snew(struct Socket_tag
);
589 ret
->fn
= &tcp_fn_table
;
592 bufchain_init(&ret
->output_data
);
593 ret
->writable
= 0; /* to start with */
594 ret
->sending_oob
= 0;
596 ret
->frozen_readable
= 0;
597 ret
->localhost_only
= local_host_only
;
598 ret
->pending_error
= 0;
599 ret
->oobpending
= FALSE
;
603 * Translate address_family from platform-independent constants
604 * into local reality.
606 address_family
= (address_family
== ADDRTYPE_IPV4 ? AF_INET
:
607 address_family
== ADDRTYPE_IPV6 ? AF_INET6
: AF_UNSPEC
);
610 /* Let's default to IPv6.
611 * If the stack doesn't support IPv6, we will fall back to IPv4. */
612 if (address_family
== AF_UNSPEC
) address_family
= AF_INET6
;
614 /* No other choice, default to IPv4 */
615 if (address_family
== AF_UNSPEC
) address_family
= AF_INET
;
621 s
= socket(address_family
, SOCK_STREAM
, 0);
623 /* If the host doesn't support IPv6 try fallback to IPv4. */
624 if (s
< 0 && address_family
== AF_INET6
) {
625 address_family
= AF_INET
;
626 s
= socket(address_family
, SOCK_STREAM
, 0);
631 ret
->error
= error_string(errno
);
637 setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&on
, sizeof(on
));
639 /* BSD IP stacks need sockaddr_in zeroed before filling in */
640 memset(&a
,'\0',sizeof(struct sockaddr_in
));
643 memset(&a6
,'\0',sizeof(struct sockaddr_in6
));
645 hints
.ai_flags
= AI_NUMERICHOST
;
646 hints
.ai_family
= address_family
;
647 hints
.ai_socktype
= 0;
648 hints
.ai_protocol
= 0;
649 hints
.ai_addrlen
= 0;
650 hints
.ai_addr
= NULL
;
651 hints
.ai_canonname
= NULL
;
652 hints
.ai_next
= NULL
;
653 sprintf(portstr
, "%d", port
);
654 if (srcaddr
!= NULL
&& getaddrinfo(srcaddr
, portstr
, &hints
, &ai
) == 0)
655 retcode
= bind(s
, ai
->ai_addr
, ai
->ai_addrlen
);
660 * FIXME: Need two listening sockets, in principle, one for v4
664 a6
.sin6_addr
= in6addr_loopback
;
666 a6
.sin6_addr
= in6addr_any
;
667 a6
.sin6_port
= htons(port
);
673 a
.sin_family
= AF_INET
;
676 * Bind to source address. First try an explicitly
680 a
.sin_addr
.s_addr
= inet_addr(srcaddr
);
681 if (a
.sin_addr
.s_addr
!= INADDR_NONE
) {
682 /* Override localhost_only with specified listen addr. */
683 ret
->localhost_only
= ipv4_is_loopback(a
.sin_addr
);
689 * ... and failing that, go with one of the standard ones.
693 a
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
695 a
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
698 a
.sin_port
= htons((short)port
);
699 retcode
= bind(s
, (struct sockaddr
*) &a
, sizeof(a
));
709 ret
->error
= error_string(err
);
714 if (listen(s
, SOMAXCONN
) < 0) {
716 ret
->error
= error_string(errno
);
726 static void sk_tcp_close(Socket sock
)
728 Actual_Socket s
= (Actual_Socket
) sock
;
736 int sk_getxdmdata(void *sock
, unsigned long *ip
, int *port
)
738 Actual_Socket s
= (Actual_Socket
) sock
;
739 struct sockaddr_in addr
;
743 * We must check that this socket really _is_ an Actual_Socket.
745 if (s
->fn
!= &tcp_fn_table
)
746 return 0; /* failure */
748 addrlen
= sizeof(addr
);
749 if (getsockname(s
->s
, (struct sockaddr
*)&addr
, &addrlen
) < 0)
751 switch(addr
.sin_family
) {
753 *ip
= ntohl(addr
.sin_addr
.s_addr
);
754 *port
= ntohs(addr
.sin_port
);
758 * For a Unix socket, we return 0xFFFFFFFF for the IP address and
759 * our current pid for the port. Bizarre, but such is life.
761 *ip
= ntohl(0xFFFFFFFF);
775 * The function which tries to send on a socket once it's deemed
778 void try_send(Actual_Socket s
)
780 while (s
->sending_oob
|| bufchain_size(&s
->output_data
) > 0) {
786 if (s
->sending_oob
) {
787 urgentflag
= MSG_OOB
;
788 len
= s
->sending_oob
;
792 bufchain_prefix(&s
->output_data
, &data
, &len
);
794 nsent
= send(s
->s
, data
, len
, urgentflag
);
795 noise_ultralight(nsent
);
797 err
= (nsent
< 0 ? errno
: 0);
798 if (err
== EWOULDBLOCK
) {
800 * Perfectly normal: we've sent all we can for the moment.
804 } else if (nsent
== 0 ||
805 err
== ECONNABORTED
|| err
== ECONNRESET
) {
807 * If send() returns CONNABORTED or CONNRESET, we
808 * unfortunately can't just call plug_closing(),
809 * because it's quite likely that we're currently
810 * _in_ a call from the code we'd be calling back
811 * to, so we'd have to make half the SSH code
812 * reentrant. Instead we flag a pending error on
813 * the socket, to be dealt with (by calling
814 * plug_closing()) at some suitable future moment.
816 s
->pending_error
= err
;
819 /* We're inside the Unix frontend here, so we know
820 * that the frontend handle is unnecessary. */
821 logevent(NULL
, error_string(err
));
822 fatalbox("%s", error_string(err
));
825 if (s
->sending_oob
) {
827 memmove(s
->oobdata
, s
->oobdata
+nsent
, len
-nsent
);
828 s
->sending_oob
= len
- nsent
;
833 bufchain_consume(&s
->output_data
, nsent
);
840 static int sk_tcp_write(Socket sock
, const char *buf
, int len
)
842 Actual_Socket s
= (Actual_Socket
) sock
;
845 * Add the data to the buffer list on the socket.
847 bufchain_add(&s
->output_data
, buf
, len
);
850 * Now try sending from the start of the buffer list.
856 * Update the select() status to correctly reflect whether or
857 * not we should be selecting for write.
861 return bufchain_size(&s
->output_data
);
864 static int sk_tcp_write_oob(Socket sock
, const char *buf
, int len
)
866 Actual_Socket s
= (Actual_Socket
) sock
;
869 * Replace the buffer list on the socket with the data.
871 bufchain_clear(&s
->output_data
);
872 assert(len
<= sizeof(s
->oobdata
));
873 memcpy(s
->oobdata
, buf
, len
);
874 s
->sending_oob
= len
;
877 * Now try sending from the start of the buffer list.
883 * Update the select() status to correctly reflect whether or
884 * not we should be selecting for write.
888 return s
->sending_oob
;
891 static int net_select_result(int fd
, int event
)
895 char buf
[20480]; /* nice big buffer for plenty of speed */
899 /* Find the Socket structure */
900 s
= find234(sktree
, &fd
, cmpforsearch
);
902 return 1; /* boggle */
904 noise_ultralight(event
);
907 case 4: /* exceptional */
910 * On a non-oobinline socket, this indicates that we
911 * can immediately perform an OOB read and get back OOB
912 * data, which we will send to the back end with
913 * type==2 (urgent data).
915 ret
= recv(s
->s
, buf
, sizeof(buf
), MSG_OOB
);
916 noise_ultralight(ret
);
918 const char *str
= (ret
== 0 ?
"Internal networking trouble" :
919 error_string(errno
));
920 /* We're inside the Unix frontend here, so we know
921 * that the frontend handle is unnecessary. */
925 return plug_receive(s
->plug
, 2, buf
, ret
);
931 * If we reach here, this is an oobinline socket, which
932 * means we should set s->oobpending and then deal with it
933 * when we get called for the readability event (which
934 * should also occur).
936 s
->oobpending
= TRUE
;
938 case 1: /* readable; also acceptance */
941 * On a listening socket, the readability event means a
942 * connection is ready to be accepted.
944 struct sockaddr_in isa
;
945 int addrlen
= sizeof(struct sockaddr_in
);
946 int t
; /* socket of connection */
948 memset(&isa
, 0, sizeof(struct sockaddr_in
));
950 t
= accept(s
->s
,(struct sockaddr
*)&isa
,(socklen_t
*) &addrlen
);
955 if (s
->localhost_only
&& !ipv4_is_loopback(isa
.sin_addr
)) {
956 close(t
); /* someone let nonlocal through?! */
957 } else if (plug_accepting(s
->plug
, t
)) {
958 close(t
); /* denied or error */
964 * If we reach here, this is not a listening socket, so
965 * readability really means readability.
968 /* In the case the socket is still frozen, we don't even bother */
970 s
->frozen_readable
= 1;
975 * We have received data on the socket. For an oobinline
976 * socket, this might be data _before_ an urgent pointer,
977 * in which case we send it to the back end with type==1
978 * (data prior to urgent).
980 if (s
->oobinline
&& s
->oobpending
) {
982 if (ioctl(s
->s
, SIOCATMARK
, &atmark
) == 0 && atmark
)
983 s
->oobpending
= FALSE
; /* clear this indicator */
987 ret
= recv(s
->s
, buf
, s
->oobpending ?
1 : sizeof(buf
), 0);
988 noise_ultralight(ret
);
990 if (errno
== EWOULDBLOCK
) {
995 return plug_closing(s
->plug
, error_string(errno
), errno
, 0);
996 } else if (0 == ret
) {
997 return plug_closing(s
->plug
, NULL
, 0, 0);
999 return plug_receive(s
->plug
, atmark ?
0 : 1, buf
, ret
);
1002 case 2: /* writable */
1003 if (!s
->connected
) {
1005 * select() reports a socket as _writable_ when an
1006 * asynchronous connection is completed.
1008 s
->connected
= s
->writable
= 1;
1012 int bufsize_before
, bufsize_after
;
1014 bufsize_before
= s
->sending_oob
+ bufchain_size(&s
->output_data
);
1016 bufsize_after
= s
->sending_oob
+ bufchain_size(&s
->output_data
);
1017 if (bufsize_after
< bufsize_before
)
1018 plug_sent(s
->plug
, bufsize_after
);
1027 * Deal with socket errors detected in try_send().
1029 void net_pending_errors(void)
1035 * This might be a fiddly business, because it's just possible
1036 * that handling a pending error on one socket might cause
1037 * others to be closed. (I can't think of any reason this might
1038 * happen in current SSH implementation, but to maintain
1039 * generality of this network layer I'll assume the worst.)
1041 * So what we'll do is search the socket list for _one_ socket
1042 * with a pending error, and then handle it, and then search
1043 * the list again _from the beginning_. Repeat until we make a
1044 * pass with no socket errors present. That way we are
1045 * protected against the socket list changing under our feet.
1049 for (i
= 0; (s
= index234(sktree
, i
)) != NULL
; i
++) {
1050 if (s
->pending_error
) {
1052 * An error has occurred on this socket. Pass it to the
1055 plug_closing(s
->plug
, error_string(s
->pending_error
),
1056 s
->pending_error
, 0);
1064 * Each socket abstraction contains a `void *' private field in
1065 * which the client can keep state.
1067 static void sk_tcp_set_private_ptr(Socket sock
, void *ptr
)
1069 Actual_Socket s
= (Actual_Socket
) sock
;
1070 s
->private_ptr
= ptr
;
1073 static void *sk_tcp_get_private_ptr(Socket sock
)
1075 Actual_Socket s
= (Actual_Socket
) sock
;
1076 return s
->private_ptr
;
1080 * Special error values are returned from sk_namelookup and sk_new
1081 * if there's a problem. These functions extract an error message,
1082 * or return NULL if there's no problem.
1084 const char *sk_addr_error(SockAddr addr
)
1088 static const char *sk_tcp_socket_error(Socket sock
)
1090 Actual_Socket s
= (Actual_Socket
) sock
;
1094 static void sk_tcp_set_frozen(Socket sock
, int is_frozen
)
1096 Actual_Socket s
= (Actual_Socket
) sock
;
1097 if (s
->frozen
== is_frozen
)
1099 s
->frozen
= is_frozen
;
1100 if (!is_frozen
&& s
->frozen_readable
) {
1102 recv(s
->s
, &c
, 1, MSG_PEEK
);
1104 s
->frozen_readable
= 0;
1108 static void uxsel_tell(Actual_Socket s
)
1112 rwx
|= 2; /* write == connect */
1113 if (s
->connected
&& !s
->frozen
)
1114 rwx
|= 1 | 4; /* read, except */
1115 if (bufchain_size(&s
->output_data
))
1116 rwx
|= 2; /* write */
1118 rwx
|= 1; /* read == accept */
1119 uxsel_set(s
->s
, rwx
, net_select_result
);
1122 int net_service_lookup(char *service
)
1125 se
= getservbyname(service
, NULL
);
1127 return ntohs(se
->s_port
);
1132 SockAddr
platform_get_x11_unix_address(int displaynum
, char **canonicalname
)
1134 SockAddr ret
= snew(struct SockAddr_tag
);
1137 memset(ret
, 0, sizeof *ret
);
1138 ret
->family
= AF_UNIX
;
1139 n
= snprintf(ret
->hostname
, sizeof ret
->hostname
,
1140 "%s%d", X11_UNIX_PATH
, displaynum
);
1142 ret
->error
= "snprintf failed";
1143 else if(n
>= sizeof ret
->hostname
)
1144 ret
->error
= "X11 UNIX name too long";
1146 *canonicalname
= dupstr(ret
->hostname
);