X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/ffbfd0935a4a75c4e3418f1010d526fbd93139a3..3fe551b2cde6cbc1946b48c311f45310de416cc5:/unix/uxnet.c diff --git a/unix/uxnet.c b/unix/uxnet.c index 8e95a65e..d649e4c7 100644 --- a/unix/uxnet.c +++ b/unix/uxnet.c @@ -31,6 +31,21 @@ # define X11_UNIX_PATH "/tmp/.X11-unix/X" #endif +/* + * Access to sockaddr types without breaking C strict aliasing rules. + */ +union sockaddr_union { +#ifdef NO_IPV6 + struct sockaddr_in storage; +#else + struct sockaddr_storage storage; + struct sockaddr_in6 sin6; +#endif + struct sockaddr sa; + struct sockaddr_in sin; + struct sockaddr_un su; +}; + /* * We used to typedef struct Socket_tag *Socket. * @@ -338,19 +353,13 @@ int sk_hostname_is_local(char *name) static int sockaddr_is_loopback(struct sockaddr *sa) { - struct sockaddr_in *sin; -#ifndef NO_IPV6 - struct sockaddr_in6 *sin6; -#endif - - switch (sa->sa_family) { + union sockaddr_union *u = (union sockaddr_union *)sa; + switch (u->sa.sa_family) { case AF_INET: - sin = (struct sockaddr_in *)sa; - return ipv4_is_loopback(sin->sin_addr); + return ipv4_is_loopback(u->sin.sin_addr); #ifndef NO_IPV6 case AF_INET6: - sin6 = (struct sockaddr_in6 *)sa; - return IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr); + return IN6_IS_ADDR_LOOPBACK(&u->sin6.sin6_addr); #endif case AF_UNIX: return TRUE; @@ -516,12 +525,8 @@ Socket sk_register(OSSocket sockfd, Plug plug) static int try_connect(Actual_Socket sock) { int s; -#ifndef NO_IPV6 - struct sockaddr_in6 a6; -#endif - struct sockaddr_in a; - struct sockaddr_un au; - const struct sockaddr *sa; + union sockaddr_union u; + const union sockaddr_union *sa; int err = 0; short localport; int fl, salen, family; @@ -578,10 +583,7 @@ static int try_connect(Actual_Socket sock) localport = 0; /* just use port 0 (ie kernel picks) */ /* BSD IP stacks need sockaddr_in zeroed before filling in */ - memset(&a,'\0',sizeof(struct sockaddr_in)); -#ifndef NO_IPV6 - memset(&a6,'\0',sizeof(struct sockaddr_in6)); -#endif + memset(&u,'\0',sizeof(u)); /* We don't try to bind to a local address for UNIX domain sockets. (Why * do we bother doing the bind when localport == 0 anyway?) */ @@ -593,18 +595,18 @@ static int try_connect(Actual_Socket sock) #ifndef NO_IPV6 if (family == AF_INET6) { /* XXX use getaddrinfo to get a local address? */ - a6.sin6_family = AF_INET6; - a6.sin6_addr = in6addr_any; - a6.sin6_port = htons(localport); - retcode = bind(s, (struct sockaddr *) &a6, sizeof(a6)); + u.sin6.sin6_family = AF_INET6; + u.sin6.sin6_addr = in6addr_any; + u.sin6.sin6_port = htons(localport); + retcode = bind(s, &u.sa, sizeof(u.sin6)); } else #endif { assert(family == AF_INET); - a.sin_family = AF_INET; - a.sin_addr.s_addr = htonl(INADDR_ANY); - a.sin_port = htons(localport); - retcode = bind(s, (struct sockaddr *) &a, sizeof(a)); + u.sin.sin_family = AF_INET; + u.sin.sin_addr.s_addr = htonl(INADDR_ANY); + u.sin.sin_port = htons(localport); + retcode = bind(s, &u.sa, sizeof(u.sin)); } if (retcode >= 0) { err = 0; @@ -635,32 +637,31 @@ static int try_connect(Actual_Socket sock) /* XXX would be better to have got getaddrinfo() to fill in the port. */ ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port = htons(sock->port); - sa = (const struct sockaddr *)sock->step.ai->ai_addr; + sa = (const union sockaddr_union *)sock->step.ai->ai_addr; salen = sock->step.ai->ai_addrlen; break; case AF_INET6: ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port = htons(sock->port); - sa = (const struct sockaddr *)sock->step.ai->ai_addr; + sa = (const union sockaddr_union *)sock->step.ai->ai_addr; salen = sock->step.ai->ai_addrlen; break; #else case AF_INET: - a.sin_family = AF_INET; - a.sin_addr.s_addr = htonl(sock->addr->addresses[sock->step.curraddr]); - a.sin_port = htons((short) sock->port); - sa = (const struct sockaddr *)&a; - salen = sizeof a; + u.sin.sin_family = AF_INET; + u.sin.sin_addr.s_addr = htonl(sock->addr->addresses[sock->step.curraddr]); + u.sin.sin_port = htons((short) sock->port); + sa = &u; + salen = sizeof u.sin; break; #endif case AF_UNIX: assert(sock->port == 0); /* to catch confused people */ - assert(strlen(sock->addr->hostname) < sizeof au.sun_path); - memset(&au, 0, sizeof au); - au.sun_family = AF_UNIX; - strcpy(au.sun_path, sock->addr->hostname); - sa = (const struct sockaddr *)&au; - salen = sizeof au; + assert(strlen(sock->addr->hostname) < sizeof u.su.sun_path); + u.su.sun_family = AF_UNIX; + strcpy(u.su.sun_path, sock->addr->hostname); + sa = &u; + salen = sizeof u.su; break; default: @@ -672,7 +673,7 @@ static int try_connect(Actual_Socket sock) if (fl != -1) fcntl(s, F_SETFL, fl | O_NONBLOCK); - if ((connect(s, sa, salen)) < 0) { + if ((connect(s, &(sa->sa), salen)) < 0) { if ( errno != EINPROGRESS ) { err = errno; goto ret; @@ -750,11 +751,10 @@ Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, i #ifndef NO_IPV6 struct addrinfo hints, *ai; char portstr[6]; - struct sockaddr_in6 a6; #endif - struct sockaddr *addr; + union sockaddr_union u; + union sockaddr_union *addr; int addrlen; - struct sockaddr_in a; Actual_Socket ret; int retcode; int address_family; @@ -839,52 +839,51 @@ Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, i sprintf(portstr, "%d", port); retcode = getaddrinfo(srcaddr, portstr, &hints, &ai); if (retcode == 0) { - addr = ai->ai_addr; + addr = (union sockaddr_union *)ai->ai_addr; addrlen = ai->ai_addrlen; } #else - memset(&a,'\0',sizeof(struct sockaddr_in)); - a.sin_family = AF_INET; - a.sin_port = htons(port); - a.sin_addr.s_addr = inet_addr(srcaddr); - if (a.sin_addr.s_addr != (in_addr_t)(-1)) { + memset(&u,'\0',sizeof u); + u.sin.sin_family = AF_INET; + u.sin.sin_port = htons(port); + u.sin.sin_addr.s_addr = inet_addr(srcaddr); + if (u.sin.sin_addr.s_addr != (in_addr_t)(-1)) { /* Override localhost_only with specified listen addr. */ - ret->localhost_only = ipv4_is_loopback(a.sin_addr); + ret->localhost_only = ipv4_is_loopback(u.sin.sin_addr); } - addr = (struct sockaddr *)&a; - addrlen = sizeof(a); + addr = &u; + addrlen = sizeof(u.sin); retcode = 0; #endif } if (retcode != 0) { + memset(&u,'\0',sizeof u); #ifndef NO_IPV6 if (address_family == AF_INET6) { - memset(&a6,'\0',sizeof(struct sockaddr_in6)); - a6.sin6_family = AF_INET6; - a6.sin6_port = htons(port); + u.sin6.sin6_family = AF_INET6; + u.sin6.sin6_port = htons(port); if (local_host_only) - a6.sin6_addr = in6addr_loopback; + u.sin6.sin6_addr = in6addr_loopback; else - a6.sin6_addr = in6addr_any; - addr = (struct sockaddr *)&a6; - addrlen = sizeof(a6); + u.sin6.sin6_addr = in6addr_any; + addr = &u; + addrlen = sizeof(u.sin6); } else #endif { - memset(&a,'\0',sizeof(struct sockaddr_in)); - a.sin_family = AF_INET; - a.sin_port = htons(port); + u.sin.sin_family = AF_INET; + u.sin.sin_port = htons(port); if (local_host_only) - a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + u.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); else - a.sin_addr.s_addr = htonl(INADDR_ANY); - addr = (struct sockaddr *)&a; - addrlen = sizeof(a); + u.sin.sin_addr.s_addr = htonl(INADDR_ANY); + addr = &u; + addrlen = sizeof(u.sin); } } - retcode = bind(s, addr, addrlen); + retcode = bind(s, &addr->sa, addrlen); if (retcode < 0) { close(s); ret->error = strerror(errno); @@ -949,14 +948,7 @@ static void sk_tcp_close(Socket sock) void *sk_getxdmdata(void *sock, int *lenp) { Actual_Socket s = (Actual_Socket) sock; -#ifdef NO_IPV6 - struct sockaddr_in addr; -#else - struct sockaddr_storage addr; - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr; -#endif - struct sockaddr *sa = (struct sockaddr *)&addr; - struct sockaddr_in *sin = (struct sockaddr_in *)&addr; + union sockaddr_union u; socklen_t addrlen; char *buf; static unsigned int unix_addr = 0xFFFFFFFF; @@ -967,23 +959,23 @@ void *sk_getxdmdata(void *sock, int *lenp) if (s->fn != &tcp_fn_table) return NULL; /* failure */ - addrlen = sizeof(addr); - if (getsockname(s->s, sa, &addrlen) < 0) + addrlen = sizeof(u); + if (getsockname(s->s, &u.sa, &addrlen) < 0) return NULL; - switch(sa->sa_family) { + switch(u.sa.sa_family) { case AF_INET: *lenp = 6; buf = snewn(*lenp, char); - PUT_32BIT_MSB_FIRST(buf, ntohl(sin->sin_addr.s_addr)); - PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin->sin_port)); + PUT_32BIT_MSB_FIRST(buf, ntohl(u.sin.sin_addr.s_addr)); + PUT_16BIT_MSB_FIRST(buf+4, ntohs(u.sin.sin_port)); break; #ifndef NO_IPV6 case AF_INET6: *lenp = 6; buf = snewn(*lenp, char); - if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { - memcpy(buf, sin6->sin6_addr.s6_addr + 12, 4); - PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin6->sin6_port)); + if (IN6_IS_ADDR_V4MAPPED(&u.sin6.sin6_addr)) { + memcpy(buf, u.sin6.sin6_addr.s6_addr + 12, 4); + PUT_16BIT_MSB_FIRST(buf+4, ntohs(u.sin6.sin6_port)); } else /* This is stupid, but it's what XLib does. */ memset(buf, 0, 6); @@ -1173,17 +1165,13 @@ static int net_select_result(int fd, int event) * On a listening socket, the readability event means a * connection is ready to be accepted. */ -#ifdef NO_IPV6 - struct sockaddr_in ss; -#else - struct sockaddr_storage ss; -#endif - socklen_t addrlen = sizeof(ss); + union sockaddr_union su; + socklen_t addrlen = sizeof(su); int t; /* socket of connection */ int fl; - memset(&ss, 0, addrlen); - t = accept(s->s, (struct sockaddr *)&ss, &addrlen); + memset(&su, 0, addrlen); + t = accept(s->s, &su.sa, &addrlen); if (t < 0) { break; } @@ -1193,7 +1181,7 @@ static int net_select_result(int fd, int event) fcntl(t, F_SETFL, fl | O_NONBLOCK); if (s->localhost_only && - !sockaddr_is_loopback((struct sockaddr *)&ss)) { + !sockaddr_is_loopback(&su.sa)) { close(t); /* someone let nonlocal through?! */ } else if (plug_accepting(s->plug, t)) { close(t); /* denied or error */ @@ -1395,6 +1383,23 @@ int net_service_lookup(char *service) return 0; } +char *get_hostname(void) +{ + int len = 128; + char *hostname = NULL; + do { + len *= 2; + hostname = sresize(hostname, len, char); + if ((gethostname(hostname, len) < 0) && + (errno != ENAMETOOLONG)) { + sfree(hostname); + hostname = NULL; + break; + } + } while (strlen(hostname) >= len-1); + return hostname; +} + SockAddr platform_get_x11_unix_address(const char *sockpath, int displaynum) { SockAddr ret = snew(struct SockAddr_tag);