#include "network.h"
#include "tree234.h"
+/* Solaris needs <sys/sockio.h> for SIOCATMARK. */
+#ifndef SIOCATMARK
+#include <sys/sockio.h>
+#endif
+
#ifndef X11_UNIX_PATH
# define X11_UNIX_PATH "/tmp/.X11-unix/X"
#endif
return -1;
if (as > bs)
return +1;
+ if (a < b)
+ return -1;
+ if (a > b)
+ return +1;
return 0;
}
static int sk_nextaddr(SockAddr addr)
{
#ifndef NO_IPV6
- if (addr->ai->ai_next) {
+ if (addr->ai && addr->ai->ai_next) {
addr->ai = addr->ai->ai_next;
addr->family = addr->ai->ai_family;
return TRUE;
short localport;
int fl, salen;
+ /*
+ * Remove the socket from the tree before we overwrite its
+ * internal socket id, because that forms part of the tree's
+ * sorting criterion. We'll add it back before exiting this
+ * function, whether we changed anything or not.
+ */
+ del234(sktree, sock);
+
if (sock->s >= 0)
close(sock->s);
goto ret;
}
+ cloexec(s);
+
if (sock->oobinline) {
int b = TRUE;
setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
default:
assert(0 && "unknown address family");
+ exit(1); /* XXX: GCC doesn't understand assert() on some systems. */
}
fl = fcntl(s, F_GETFL);
}
uxsel_tell(sock);
- add234(sktree, sock);
ret:
+
+ /*
+ * No matter what happened, put the socket back in the tree.
+ */
+ add234(sktree, sock);
+
if (err)
plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err);
return err;
* into local reality.
*/
address_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
- address_family == ADDRTYPE_IPV6 ? AF_INET6 : AF_UNSPEC);
+#ifndef NO_IPV6
+ address_family == ADDRTYPE_IPV6 ? AF_INET6 :
+#endif
+ AF_UNSPEC);
#ifndef NO_IPV6
/* Let's default to IPv6.
*/
s = socket(address_family, SOCK_STREAM, 0);
+#ifndef NO_IPV6
/* If the host doesn't support IPv6 try fallback to IPv4. */
if (s < 0 && address_family == AF_INET6) {
address_family = AF_INET;
s = socket(address_family, SOCK_STREAM, 0);
}
+#endif
if (s < 0) {
ret->error = strerror(errno);
return (Socket) ret;
}
+ cloexec(s);
+
ret->oobinline = 0;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
if (a.sin_addr.s_addr != (in_addr_t)(-1)) {
/* Override localhost_only with specified listen addr. */
ret->localhost_only = ipv4_is_loopback(a.sin_addr);
- got_addr = 1;
}
- addr = (struct sockaddr *)a;
+ addr = (struct sockaddr *)&a;
addrlen = sizeof(a);
retcode = 0;
#endif
sfree(s);
}
-#define PUT_32BIT_MSB_FIRST(cp, value) ( \
- (cp)[0] = (char)((value) >> 24), \
- (cp)[1] = (char)((value) >> 16), \
- (cp)[2] = (char)((value) >> 8), \
- (cp)[3] = (char)(value) )
-
-#define PUT_16BIT_MSB_FIRST(cp, value) ( \
- (cp)[0] = (char)((value) >> 8), \
- (cp)[1] = (char)(value) )
-
void *sk_getxdmdata(void *sock, int *lenp)
{
Actual_Socket s = (Actual_Socket) sock;
*/
s->writable = FALSE;
return;
- } else if (nsent == 0 ||
- err == ECONNABORTED || err == ECONNRESET) {
+ } else {
/*
- * If send() returns CONNABORTED or CONNRESET, we
- * unfortunately can't just call plug_closing(),
+ * We unfortunately can't just call plug_closing(),
* because it's quite likely that we're currently
* _in_ a call from the code we'd be calling back
* to, so we'd have to make half the SSH code
*/
s->pending_error = err;
return;
- } else {
- /* We're inside the Unix frontend here, so we know
- * that the frontend handle is unnecessary. */
- logevent(NULL, strerror(err));
- fatalbox("%s", strerror(err));
}
} else {
if (s->sending_oob) {
ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
noise_ultralight(ret);
if (ret <= 0) {
- const char *str = (ret == 0 ? "Internal networking trouble" :
- strerror(errno));
- /* We're inside the Unix frontend here, so we know
- * that the frontend handle is unnecessary. */
- logevent(NULL, str);
- fatalbox("%s", str);
+ return plug_closing(s->plug,
+ ret == 0 ? "Internal networking trouble" :
+ strerror(errno), errno, 0);
} else {
/*
* Receiving actual data on a socket means we can
else
*canonicalname = dupstr(ret->hostname);
#ifndef NO_IPV6
- ret->ais = NULL;
+ ret->ai = ret->ais = NULL;
#else
ret->addresses = NULL;
+ ret->curraddr = ret->naddresses = 0;
#endif
return ret;
}