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
25 /* Solaris needs <sys/sockio.h> for SIOCATMARK. */
27 #include <sys/sockio.h>
31 # define X11_UNIX_PATH "/tmp/.X11-unix/X"
35 struct socket_function_table
*fn
;
36 /* the above variable absolutely *must* be the first in this structure */
42 int connected
; /* irrelevant for listening sockets */
44 int frozen
; /* this causes readability notifications to be ignored */
45 int frozen_readable
; /* this means we missed at least one readability
46 * notification while we were frozen */
47 int localhost_only
; /* for listening sockets */
50 int oobpending
; /* is there OOB data available to read? */
52 int pending_error
; /* in case send() returns error */
54 int nodelay
, keepalive
; /* for connect()-type sockets */
55 int privport
, port
; /* and again */
60 * We used to typedef struct Socket_tag *Socket.
62 * Since we have made the networking abstraction slightly more
63 * abstract, Socket no longer means a tcp socket (it could mean
64 * an ssl socket). So now we must use Actual_Socket when we know
65 * we are talking about a tcp socket.
67 typedef struct Socket_tag
*Actual_Socket
;
72 * Which address family this address belongs to. AF_INET for
73 * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
74 * resolution has not been done and a simple host name is held
75 * in this SockAddr structure.
79 struct addrinfo
*ais
; /* Addresses IPv6 style. */
80 struct addrinfo
*ai
; /* steps along the linked list */
82 unsigned long *addresses
; /* Addresses IPv4 style. */
83 int naddresses
, curraddr
;
85 char hostname
[512]; /* Store an unresolved host name. */
88 static tree234
*sktree
;
90 static void uxsel_tell(Actual_Socket s
);
92 static int cmpfortree(void *av
, void *bv
)
94 Actual_Socket a
= (Actual_Socket
) av
, b
= (Actual_Socket
) bv
;
95 int as
= a
->s
, bs
= b
->s
;
107 static int cmpforsearch(void *av
, void *bv
)
109 Actual_Socket b
= (Actual_Socket
) bv
;
110 int as
= *(int *)av
, bs
= b
->s
;
120 sktree
= newtree234(cmpfortree
);
123 void sk_cleanup(void)
129 for (i
= 0; (s
= index234(sktree
, i
)) != NULL
; i
++) {
135 SockAddr
sk_namelookup(const char *host
, char **canonicalname
, int address_family
)
137 SockAddr ret
= snew(struct SockAddr_tag
);
139 struct addrinfo hints
;
143 struct hostent
*h
= NULL
;
148 /* Clear the structure and default to IPv4. */
149 memset(ret
, 0, sizeof(struct SockAddr_tag
));
150 ret
->family
= 0; /* We set this one when we have resolved the host. */
155 hints
.ai_flags
= AI_CANONNAME
;
156 hints
.ai_family
= (address_family
== ADDRTYPE_IPV4 ? AF_INET
:
157 address_family
== ADDRTYPE_IPV6 ? AF_INET6
:
159 hints
.ai_socktype
= SOCK_STREAM
;
160 hints
.ai_protocol
= 0;
161 hints
.ai_addrlen
= 0;
162 hints
.ai_addr
= NULL
;
163 hints
.ai_canonname
= NULL
;
164 hints
.ai_next
= NULL
;
165 err
= getaddrinfo(host
, NULL
, &hints
, &ret
->ais
);
168 ret
->error
= gai_strerror(err
);
171 ret
->family
= ret
->ai
->ai_family
;
173 if (ret
->ai
->ai_canonname
!= NULL
)
174 strncat(realhost
, ret
->ai
->ai_canonname
, sizeof(realhost
) - 1);
176 strncat(realhost
, host
, sizeof(realhost
) - 1);
178 if ((a
= inet_addr(host
)) == (unsigned long)(in_addr_t
)(-1)) {
180 * Otherwise use the IPv4-only gethostbyname... (NOTE:
181 * we don't use gethostbyname as a fallback!)
183 if (ret
->family
== 0) {
184 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
185 if ( (h
= gethostbyname(host
)) )
186 ret
->family
= AF_INET
;
188 if (ret
->family
== 0) {
189 ret
->error
= (h_errno
== HOST_NOT_FOUND
||
190 h_errno
== NO_DATA
||
191 h_errno
== NO_ADDRESS ?
"Host does not exist" :
192 h_errno
== TRY_AGAIN ?
193 "Temporary name service failure" :
194 "gethostbyname: unknown error");
197 /* This way we are always sure the h->h_name is valid :) */
198 strncpy(realhost
, h
->h_name
, sizeof(realhost
));
199 for (n
= 0; h
->h_addr_list
[n
]; n
++);
200 ret
->addresses
= snewn(n
, unsigned long);
202 for (n
= 0; n
< ret
->naddresses
; n
++) {
203 memcpy(&a
, h
->h_addr_list
[n
], sizeof(a
));
204 ret
->addresses
[n
] = ntohl(a
);
208 * This must be a numeric IPv4 address because it caused a
209 * success return from inet_addr.
211 ret
->family
= AF_INET
;
212 strncpy(realhost
, host
, sizeof(realhost
));
213 ret
->addresses
= snew(unsigned long);
215 ret
->addresses
[0] = ntohl(a
);
219 realhost
[lenof(realhost
)-1] = '\0';
220 *canonicalname
= snewn(1+strlen(realhost
), char);
221 strcpy(*canonicalname
, realhost
);
225 SockAddr
sk_nonamelookup(const char *host
)
227 SockAddr ret
= snew(struct SockAddr_tag
);
229 ret
->family
= AF_UNSPEC
;
230 strncpy(ret
->hostname
, host
, lenof(ret
->hostname
));
231 ret
->hostname
[lenof(ret
->hostname
)-1] = '\0';
235 ret
->addresses
= NULL
;
240 static int sk_nextaddr(SockAddr addr
)
243 if (addr
->ai
&& addr
->ai
->ai_next
) {
244 addr
->ai
= addr
->ai
->ai_next
;
245 addr
->family
= addr
->ai
->ai_family
;
250 if (addr
->curraddr
+1 < addr
->naddresses
) {
259 void sk_getaddr(SockAddr addr
, char *buf
, int buflen
)
262 if (addr
->family
== AF_UNSPEC
) {
263 strncpy(buf
, addr
->hostname
, buflen
);
264 buf
[buflen
-1] = '\0';
267 if (getnameinfo(addr
->ai
->ai_addr
, addr
->ai
->ai_addrlen
, buf
, buflen
,
268 NULL
, 0, NI_NUMERICHOST
) != 0) {
270 strncat(buf
, "<unknown>", buflen
- 1);
274 assert(addr
->family
== AF_INET
);
275 a
.s_addr
= htonl(addr
->addresses
[addr
->curraddr
]);
276 strncpy(buf
, inet_ntoa(a
), buflen
);
277 buf
[buflen
-1] = '\0';
282 int sk_hostname_is_local(char *name
)
284 return !strcmp(name
, "localhost");
287 #define ipv4_is_loopback(addr) \
288 (((addr).s_addr & htonl(0xff000000)) == htonl(0x7f000000))
290 static int sockaddr_is_loopback(struct sockaddr
*sa
)
292 struct sockaddr_in
*sin
;
294 struct sockaddr_in6
*sin6
;
297 switch (sa
->sa_family
) {
299 sin
= (struct sockaddr_in
*)sa
;
300 return ipv4_is_loopback(sin
->sin_addr
);
303 sin6
= (struct sockaddr_in6
*)sa
;
304 return IN6_IS_ADDR_LOOPBACK(&sin6
->sin6_addr
);
313 int sk_address_is_local(SockAddr addr
)
316 if (addr
->family
== AF_UNSPEC
)
317 return 0; /* we don't know; assume not */
320 return sockaddr_is_loopback(addr
->ai
->ai_addr
);
323 assert(addr
->family
== AF_INET
);
324 a
.s_addr
= htonl(addr
->addresses
[addr
->curraddr
]);
325 return ipv4_is_loopback(a
);
330 int sk_addrtype(SockAddr addr
)
332 return (addr
->family
== AF_INET ? ADDRTYPE_IPV4
:
334 addr
->family
== AF_INET6 ? ADDRTYPE_IPV6
:
339 void sk_addrcopy(SockAddr addr
, char *buf
)
343 if (addr
->family
== AF_INET
)
344 memcpy(buf
, &((struct sockaddr_in
*)addr
->ai
->ai_addr
)->sin_addr
,
345 sizeof(struct in_addr
));
346 else if (addr
->family
== AF_INET6
)
347 memcpy(buf
, &((struct sockaddr_in6
*)addr
->ai
->ai_addr
)->sin6_addr
,
348 sizeof(struct in6_addr
));
354 assert(addr
->family
== AF_INET
);
355 a
.s_addr
= htonl(addr
->addresses
[addr
->curraddr
]);
356 memcpy(buf
, (char*) &a
.s_addr
, 4);
360 void sk_addr_free(SockAddr addr
)
364 if (addr
->ais
!= NULL
)
365 freeaddrinfo(addr
->ais
);
367 sfree(addr
->addresses
);
372 static Plug
sk_tcp_plug(Socket sock
, Plug p
)
374 Actual_Socket s
= (Actual_Socket
) sock
;
381 static void sk_tcp_flush(Socket s
)
384 * We send data to the socket as soon as we can anyway,
385 * so we don't need to do anything here. :-)
389 static void sk_tcp_close(Socket s
);
390 static int sk_tcp_write(Socket s
, const char *data
, int len
);
391 static int sk_tcp_write_oob(Socket s
, const char *data
, int len
);
392 static void sk_tcp_set_private_ptr(Socket s
, void *ptr
);
393 static void *sk_tcp_get_private_ptr(Socket s
);
394 static void sk_tcp_set_frozen(Socket s
, int is_frozen
);
395 static const char *sk_tcp_socket_error(Socket s
);
397 static struct socket_function_table tcp_fn_table
= {
403 sk_tcp_set_private_ptr
,
404 sk_tcp_get_private_ptr
,
409 Socket
sk_register(OSSocket sockfd
, Plug plug
)
414 * Create Socket structure.
416 ret
= snew(struct Socket_tag
);
417 ret
->fn
= &tcp_fn_table
;
420 bufchain_init(&ret
->output_data
);
421 ret
->writable
= 1; /* to start with */
422 ret
->sending_oob
= 0;
424 ret
->frozen_readable
= 0;
425 ret
->localhost_only
= 0; /* unused, but best init anyway */
426 ret
->pending_error
= 0;
427 ret
->oobpending
= FALSE
;
435 ret
->error
= strerror(errno
);
447 static int try_connect(Actual_Socket sock
)
451 struct sockaddr_in6 a6
;
453 struct sockaddr_in a
;
454 struct sockaddr_un au
;
455 const struct sockaddr
*sa
;
461 * Remove the socket from the tree before we overwrite its
462 * internal socket id, because that forms part of the tree's
463 * sorting criterion. We'll add it back before exiting this
464 * function, whether we changed anything or not.
466 del234(sktree
, sock
);
471 plug_log(sock
->plug
, 0, sock
->addr
, sock
->port
, NULL
, 0);
476 assert(sock
->addr
->family
!= AF_UNSPEC
);
477 s
= socket(sock
->addr
->family
, SOCK_STREAM
, 0);
487 if (sock
->oobinline
) {
489 setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, (void *) &b
, sizeof(b
));
494 setsockopt(s
, IPPROTO_TCP
, TCP_NODELAY
, (void *) &b
, sizeof(b
));
497 if (sock
->keepalive
) {
499 setsockopt(s
, SOL_SOCKET
, SO_KEEPALIVE
, (void *) &b
, sizeof(b
));
503 * Bind to local address.
506 localport
= 1023; /* count from 1023 downwards */
508 localport
= 0; /* just use port 0 (ie kernel picks) */
510 /* BSD IP stacks need sockaddr_in zeroed before filling in */
511 memset(&a
,'\0',sizeof(struct sockaddr_in
));
513 memset(&a6
,'\0',sizeof(struct sockaddr_in6
));
516 /* We don't try to bind to a local address for UNIX domain sockets. (Why
517 * do we bother doing the bind when localport == 0 anyway?) */
518 if(sock
->addr
->family
!= AF_UNIX
) {
519 /* Loop round trying to bind */
524 if (sock
->addr
->family
== AF_INET6
) {
525 /* XXX use getaddrinfo to get a local address? */
526 a6
.sin6_family
= AF_INET6
;
527 a6
.sin6_addr
= in6addr_any
;
528 a6
.sin6_port
= htons(localport
);
529 retcode
= bind(s
, (struct sockaddr
*) &a6
, sizeof(a6
));
533 assert(sock
->addr
->family
== AF_INET
);
534 a
.sin_family
= AF_INET
;
535 a
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
536 a
.sin_port
= htons(localport
);
537 retcode
= bind(s
, (struct sockaddr
*) &a
, sizeof(a
));
544 if (err
!= EADDRINUSE
) /* failed, for a bad reason */
549 break; /* we're only looping once */
552 break; /* we might have got to the end */
560 * Connect to remote address.
562 switch(sock
->addr
->family
) {
565 /* XXX would be better to have got getaddrinfo() to fill in the port. */
566 ((struct sockaddr_in
*)sock
->addr
->ai
->ai_addr
)->sin_port
=
568 sa
= (const struct sockaddr
*)sock
->addr
->ai
->ai_addr
;
569 salen
= sock
->addr
->ai
->ai_addrlen
;
572 ((struct sockaddr_in
*)sock
->addr
->ai
->ai_addr
)->sin_port
=
574 sa
= (const struct sockaddr
*)sock
->addr
->ai
->ai_addr
;
575 salen
= sock
->addr
->ai
->ai_addrlen
;
579 a
.sin_family
= AF_INET
;
580 a
.sin_addr
.s_addr
= htonl(sock
->addr
->addresses
[sock
->addr
->curraddr
]);
581 a
.sin_port
= htons((short) sock
->port
);
582 sa
= (const struct sockaddr
*)&a
;
587 assert(sock
->port
== 0); /* to catch confused people */
588 assert(strlen(sock
->addr
->hostname
) < sizeof au
.sun_path
);
589 memset(&au
, 0, sizeof au
);
590 au
.sun_family
= AF_UNIX
;
591 strcpy(au
.sun_path
, sock
->addr
->hostname
);
592 sa
= (const struct sockaddr
*)&au
;
597 assert(0 && "unknown address family");
598 exit(1); /* XXX: GCC doesn't understand assert() on some systems. */
601 fl
= fcntl(s
, F_GETFL
);
603 fcntl(s
, F_SETFL
, fl
| O_NONBLOCK
);
605 if ((connect(s
, sa
, salen
)) < 0) {
606 if ( errno
!= EINPROGRESS
) {
612 * If we _don't_ get EWOULDBLOCK, the connect has completed
613 * and we should set the socket as connected and writable.
624 * No matter what happened, put the socket back in the tree.
626 add234(sktree
, sock
);
629 plug_log(sock
->plug
, 1, sock
->addr
, sock
->port
, strerror(err
), err
);
633 Socket
sk_new(SockAddr addr
, int port
, int privport
, int oobinline
,
634 int nodelay
, int keepalive
, Plug plug
)
640 * Create Socket structure.
642 ret
= snew(struct Socket_tag
);
643 ret
->fn
= &tcp_fn_table
;
646 bufchain_init(&ret
->output_data
);
647 ret
->connected
= 0; /* to start with */
648 ret
->writable
= 0; /* to start with */
649 ret
->sending_oob
= 0;
651 ret
->frozen_readable
= 0;
652 ret
->localhost_only
= 0; /* unused, but best init anyway */
653 ret
->pending_error
= 0;
654 ret
->oobpending
= FALSE
;
658 ret
->oobinline
= oobinline
;
659 ret
->nodelay
= nodelay
;
660 ret
->keepalive
= keepalive
;
661 ret
->privport
= privport
;
666 err
= try_connect(ret
);
667 } while (err
&& sk_nextaddr(ret
->addr
));
670 ret
->error
= strerror(err
);
675 Socket
sk_newlistener(char *srcaddr
, int port
, Plug plug
, int local_host_only
, int address_family
)
679 struct addrinfo hints
, *ai
;
681 struct sockaddr_in6 a6
;
683 struct sockaddr
*addr
;
685 struct sockaddr_in a
;
691 * Create Socket structure.
693 ret
= snew(struct Socket_tag
);
694 ret
->fn
= &tcp_fn_table
;
697 bufchain_init(&ret
->output_data
);
698 ret
->writable
= 0; /* to start with */
699 ret
->sending_oob
= 0;
701 ret
->frozen_readable
= 0;
702 ret
->localhost_only
= local_host_only
;
703 ret
->pending_error
= 0;
704 ret
->oobpending
= FALSE
;
709 * Translate address_family from platform-independent constants
710 * into local reality.
712 address_family
= (address_family
== ADDRTYPE_IPV4 ? AF_INET
:
714 address_family
== ADDRTYPE_IPV6 ? AF_INET6
:
719 /* Let's default to IPv6.
720 * If the stack doesn't support IPv6, we will fall back to IPv4. */
721 if (address_family
== AF_UNSPEC
) address_family
= AF_INET6
;
723 /* No other choice, default to IPv4 */
724 if (address_family
== AF_UNSPEC
) address_family
= AF_INET
;
730 s
= socket(address_family
, SOCK_STREAM
, 0);
733 /* If the host doesn't support IPv6 try fallback to IPv4. */
734 if (s
< 0 && address_family
== AF_INET6
) {
735 address_family
= AF_INET
;
736 s
= socket(address_family
, SOCK_STREAM
, 0);
741 ret
->error
= strerror(errno
);
749 setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&on
, sizeof(on
));
752 addr
= NULL
; addrlen
= -1; /* placate optimiser */
754 if (srcaddr
!= NULL
) {
756 hints
.ai_flags
= AI_NUMERICHOST
;
757 hints
.ai_family
= address_family
;
758 hints
.ai_socktype
= SOCK_STREAM
;
759 hints
.ai_protocol
= 0;
760 hints
.ai_addrlen
= 0;
761 hints
.ai_addr
= NULL
;
762 hints
.ai_canonname
= NULL
;
763 hints
.ai_next
= NULL
;
764 assert(port
>= 0 && port
<= 99999);
765 sprintf(portstr
, "%d", port
);
766 retcode
= getaddrinfo(srcaddr
, portstr
, &hints
, &ai
);
769 addrlen
= ai
->ai_addrlen
;
772 memset(&a
,'\0',sizeof(struct sockaddr_in
));
773 a
.sin_family
= AF_INET
;
774 a
.sin_port
= htons(port
);
775 a
.sin_addr
.s_addr
= inet_addr(srcaddr
);
776 if (a
.sin_addr
.s_addr
!= (in_addr_t
)(-1)) {
777 /* Override localhost_only with specified listen addr. */
778 ret
->localhost_only
= ipv4_is_loopback(a
.sin_addr
);
780 addr
= (struct sockaddr
*)&a
;
788 if (address_family
== AF_INET6
) {
789 memset(&a6
,'\0',sizeof(struct sockaddr_in6
));
790 a6
.sin6_family
= AF_INET6
;
791 a6
.sin6_port
= htons(port
);
793 a6
.sin6_addr
= in6addr_loopback
;
795 a6
.sin6_addr
= in6addr_any
;
796 addr
= (struct sockaddr
*)&a6
;
797 addrlen
= sizeof(a6
);
801 memset(&a
,'\0',sizeof(struct sockaddr_in
));
802 a
.sin_family
= AF_INET
;
803 a
.sin_port
= htons(port
);
805 a
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
807 a
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
808 addr
= (struct sockaddr
*)&a
;
813 retcode
= bind(s
, addr
, addrlen
);
816 ret
->error
= strerror(errno
);
820 if (listen(s
, SOMAXCONN
) < 0) {
822 ret
->error
= strerror(errno
);
834 static void sk_tcp_close(Socket sock
)
836 Actual_Socket s
= (Actual_Socket
) sock
;
842 sk_addr_free(s
->addr
);
846 void *sk_getxdmdata(void *sock
, int *lenp
)
848 Actual_Socket s
= (Actual_Socket
) sock
;
850 struct sockaddr_in addr
;
852 struct sockaddr_storage addr
;
853 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&addr
;
855 struct sockaddr
*sa
= (struct sockaddr
*)&addr
;
856 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&addr
;
859 static unsigned int unix_addr
= 0xFFFFFFFF;
862 * We must check that this socket really _is_ an Actual_Socket.
864 if (s
->fn
!= &tcp_fn_table
)
865 return NULL
; /* failure */
867 addrlen
= sizeof(addr
);
868 if (getsockname(s
->s
, sa
, &addrlen
) < 0)
870 switch(sa
->sa_family
) {
873 buf
= snewn(*lenp
, char);
874 PUT_32BIT_MSB_FIRST(buf
, ntohl(sin
->sin_addr
.s_addr
));
875 PUT_16BIT_MSB_FIRST(buf
+4, ntohs(sin
->sin_port
));
880 buf
= snewn(*lenp
, char);
881 if (IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
)) {
882 memcpy(buf
, sin6
->sin6_addr
.s6_addr
+ 12, 4);
883 PUT_16BIT_MSB_FIRST(buf
+4, ntohs(sin6
->sin6_port
));
885 /* This is stupid, but it's what XLib does. */
891 buf
= snewn(*lenp
, char);
892 PUT_32BIT_MSB_FIRST(buf
, unix_addr
--);
893 PUT_16BIT_MSB_FIRST(buf
+4, getpid());
906 * The function which tries to send on a socket once it's deemed
909 void try_send(Actual_Socket s
)
911 while (s
->sending_oob
|| bufchain_size(&s
->output_data
) > 0) {
917 if (s
->sending_oob
) {
918 urgentflag
= MSG_OOB
;
919 len
= s
->sending_oob
;
923 bufchain_prefix(&s
->output_data
, &data
, &len
);
925 nsent
= send(s
->s
, data
, len
, urgentflag
);
926 noise_ultralight(nsent
);
928 err
= (nsent
< 0 ? errno
: 0);
929 if (err
== EWOULDBLOCK
) {
931 * Perfectly normal: we've sent all we can for the moment.
937 * We unfortunately can't just call plug_closing(),
938 * because it's quite likely that we're currently
939 * _in_ a call from the code we'd be calling back
940 * to, so we'd have to make half the SSH code
941 * reentrant. Instead we flag a pending error on
942 * the socket, to be dealt with (by calling
943 * plug_closing()) at some suitable future moment.
945 s
->pending_error
= err
;
949 if (s
->sending_oob
) {
951 memmove(s
->oobdata
, s
->oobdata
+nsent
, len
-nsent
);
952 s
->sending_oob
= len
- nsent
;
957 bufchain_consume(&s
->output_data
, nsent
);
964 static int sk_tcp_write(Socket sock
, const char *buf
, int len
)
966 Actual_Socket s
= (Actual_Socket
) sock
;
969 * Add the data to the buffer list on the socket.
971 bufchain_add(&s
->output_data
, buf
, len
);
974 * Now try sending from the start of the buffer list.
980 * Update the select() status to correctly reflect whether or
981 * not we should be selecting for write.
985 return bufchain_size(&s
->output_data
);
988 static int sk_tcp_write_oob(Socket sock
, const char *buf
, int len
)
990 Actual_Socket s
= (Actual_Socket
) sock
;
993 * Replace the buffer list on the socket with the data.
995 bufchain_clear(&s
->output_data
);
996 assert(len
<= sizeof(s
->oobdata
));
997 memcpy(s
->oobdata
, buf
, len
);
998 s
->sending_oob
= len
;
1001 * Now try sending from the start of the buffer list.
1007 * Update the select() status to correctly reflect whether or
1008 * not we should be selecting for write.
1012 return s
->sending_oob
;
1015 static int net_select_result(int fd
, int event
)
1018 char buf
[20480]; /* nice big buffer for plenty of speed */
1022 /* Find the Socket structure */
1023 s
= find234(sktree
, &fd
, cmpforsearch
);
1025 return 1; /* boggle */
1027 noise_ultralight(event
);
1030 case 4: /* exceptional */
1031 if (!s
->oobinline
) {
1033 * On a non-oobinline socket, this indicates that we
1034 * can immediately perform an OOB read and get back OOB
1035 * data, which we will send to the back end with
1036 * type==2 (urgent data).
1038 ret
= recv(s
->s
, buf
, sizeof(buf
), MSG_OOB
);
1039 noise_ultralight(ret
);
1041 return plug_closing(s
->plug
,
1042 ret
== 0 ?
"Internal networking trouble" :
1043 strerror(errno
), errno
, 0);
1046 * Receiving actual data on a socket means we can
1047 * stop falling back through the candidate
1048 * addresses to connect to.
1051 sk_addr_free(s
->addr
);
1054 return plug_receive(s
->plug
, 2, buf
, ret
);
1060 * If we reach here, this is an oobinline socket, which
1061 * means we should set s->oobpending and then deal with it
1062 * when we get called for the readability event (which
1063 * should also occur).
1065 s
->oobpending
= TRUE
;
1067 case 1: /* readable; also acceptance */
1070 * On a listening socket, the readability event means a
1071 * connection is ready to be accepted.
1074 struct sockaddr_in ss
;
1076 struct sockaddr_storage ss
;
1078 socklen_t addrlen
= sizeof(ss
);
1079 int t
; /* socket of connection */
1082 memset(&ss
, 0, addrlen
);
1083 t
= accept(s
->s
, (struct sockaddr
*)&ss
, &addrlen
);
1088 fl
= fcntl(t
, F_GETFL
);
1090 fcntl(t
, F_SETFL
, fl
| O_NONBLOCK
);
1092 if (s
->localhost_only
&&
1093 !sockaddr_is_loopback((struct sockaddr
*)&ss
)) {
1094 close(t
); /* someone let nonlocal through?! */
1095 } else if (plug_accepting(s
->plug
, t
)) {
1096 close(t
); /* denied or error */
1102 * If we reach here, this is not a listening socket, so
1103 * readability really means readability.
1106 /* In the case the socket is still frozen, we don't even bother */
1108 s
->frozen_readable
= 1;
1113 * We have received data on the socket. For an oobinline
1114 * socket, this might be data _before_ an urgent pointer,
1115 * in which case we send it to the back end with type==1
1116 * (data prior to urgent).
1118 if (s
->oobinline
&& s
->oobpending
) {
1120 if (ioctl(s
->s
, SIOCATMARK
, &atmark
) == 0 && atmark
)
1121 s
->oobpending
= FALSE
; /* clear this indicator */
1125 ret
= recv(s
->s
, buf
, s
->oobpending ?
1 : sizeof(buf
), 0);
1126 noise_ultralight(ret
);
1128 if (errno
== EWOULDBLOCK
) {
1134 * An error at this point _might_ be an error reported
1135 * by a non-blocking connect(). So before we return a
1136 * panic status to the user, let's just see whether
1141 plug_log(s
->plug
, 1, s
->addr
, s
->port
, strerror(err
), err
);
1142 while (s
->addr
&& sk_nextaddr(s
->addr
)) {
1143 err
= try_connect(s
);
1147 return plug_closing(s
->plug
, strerror(err
), err
, 0);
1148 } else if (0 == ret
) {
1149 return plug_closing(s
->plug
, NULL
, 0, 0);
1152 * Receiving actual data on a socket means we can
1153 * stop falling back through the candidate
1154 * addresses to connect to.
1157 sk_addr_free(s
->addr
);
1160 return plug_receive(s
->plug
, atmark ?
0 : 1, buf
, ret
);
1163 case 2: /* writable */
1164 if (!s
->connected
) {
1166 * select() reports a socket as _writable_ when an
1167 * asynchronous connection is completed.
1169 s
->connected
= s
->writable
= 1;
1173 int bufsize_before
, bufsize_after
;
1175 bufsize_before
= s
->sending_oob
+ bufchain_size(&s
->output_data
);
1177 bufsize_after
= s
->sending_oob
+ bufchain_size(&s
->output_data
);
1178 if (bufsize_after
< bufsize_before
)
1179 plug_sent(s
->plug
, bufsize_after
);
1188 * Deal with socket errors detected in try_send().
1190 void net_pending_errors(void)
1196 * This might be a fiddly business, because it's just possible
1197 * that handling a pending error on one socket might cause
1198 * others to be closed. (I can't think of any reason this might
1199 * happen in current SSH implementation, but to maintain
1200 * generality of this network layer I'll assume the worst.)
1202 * So what we'll do is search the socket list for _one_ socket
1203 * with a pending error, and then handle it, and then search
1204 * the list again _from the beginning_. Repeat until we make a
1205 * pass with no socket errors present. That way we are
1206 * protected against the socket list changing under our feet.
1210 for (i
= 0; (s
= index234(sktree
, i
)) != NULL
; i
++) {
1211 if (s
->pending_error
) {
1213 * An error has occurred on this socket. Pass it to the
1216 plug_closing(s
->plug
, strerror(s
->pending_error
),
1217 s
->pending_error
, 0);
1225 * Each socket abstraction contains a `void *' private field in
1226 * which the client can keep state.
1228 static void sk_tcp_set_private_ptr(Socket sock
, void *ptr
)
1230 Actual_Socket s
= (Actual_Socket
) sock
;
1231 s
->private_ptr
= ptr
;
1234 static void *sk_tcp_get_private_ptr(Socket sock
)
1236 Actual_Socket s
= (Actual_Socket
) sock
;
1237 return s
->private_ptr
;
1241 * Special error values are returned from sk_namelookup and sk_new
1242 * if there's a problem. These functions extract an error message,
1243 * or return NULL if there's no problem.
1245 const char *sk_addr_error(SockAddr addr
)
1249 static const char *sk_tcp_socket_error(Socket sock
)
1251 Actual_Socket s
= (Actual_Socket
) sock
;
1255 static void sk_tcp_set_frozen(Socket sock
, int is_frozen
)
1257 Actual_Socket s
= (Actual_Socket
) sock
;
1258 if (s
->frozen
== is_frozen
)
1260 s
->frozen
= is_frozen
;
1261 if (!is_frozen
&& s
->frozen_readable
) {
1263 recv(s
->s
, &c
, 1, MSG_PEEK
);
1265 s
->frozen_readable
= 0;
1269 static void uxsel_tell(Actual_Socket s
)
1273 rwx
|= 1; /* read == accept */
1276 rwx
|= 2; /* write == connect */
1277 if (s
->connected
&& !s
->frozen
)
1278 rwx
|= 1 | 4; /* read, except */
1279 if (bufchain_size(&s
->output_data
))
1280 rwx
|= 2; /* write */
1282 uxsel_set(s
->s
, rwx
, net_select_result
);
1285 int net_service_lookup(char *service
)
1288 se
= getservbyname(service
, NULL
);
1290 return ntohs(se
->s_port
);
1295 SockAddr
platform_get_x11_unix_address(const char *display
, int displaynum
,
1296 char **canonicalname
)
1298 SockAddr ret
= snew(struct SockAddr_tag
);
1301 memset(ret
, 0, sizeof *ret
);
1302 ret
->family
= AF_UNIX
;
1304 * Mac OS X Leopard uses an innovative X display naming
1305 * convention in which the entire display name is the path to
1306 * the Unix socket, including the trailing :0 which only
1307 * _looks_ like a display number. Heuristically, I think
1308 * detecting this by means of a leading slash ought to be
1311 if (display
[0] == '/') {
1312 n
= snprintf(ret
->hostname
, sizeof ret
->hostname
,
1315 n
= snprintf(ret
->hostname
, sizeof ret
->hostname
,
1316 "%s%d", X11_UNIX_PATH
, displaynum
);
1319 ret
->error
= "snprintf failed";
1320 else if(n
>= sizeof ret
->hostname
)
1321 ret
->error
= "X11 UNIX name too long";
1323 *canonicalname
= dupstr(ret
->hostname
);
1325 ret
->ai
= ret
->ais
= NULL
;
1327 ret
->addresses
= NULL
;
1328 ret
->curraddr
= ret
->naddresses
= 0;