Skip to content

Commit

Permalink
Merge pull request #102 from vlm/sin-len-fix
Browse files Browse the repository at this point in the history
FreeBSD requires sin_len field to be set.
  • Loading branch information
sustrik authored Mar 25, 2017
2 parents 0f8add9 + 2c532da commit f6a52c5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ AC_CHECK_LIB([socket], [socket])
AC_CHECK_FUNCS([epoll_create], [AC_DEFINE([HAVE_EPOLL])])
AC_CHECK_FUNCS([kqueue], [AC_DEFINE([HAVE_KQUEUE])])

dnl Check if struct sockaddr contains sa_len member
AC_CHECK_MEMBERS([struct sockaddr.sa_len], [], [], [
# include <sys/types.h>
# include <sys/socket.h>
])

################################################################################
# Libtool #
################################################################################
Expand Down
30 changes: 28 additions & 2 deletions dns/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@
* USE OR OTHER DEALINGS IN THE SOFTWARE.
* ==========================================================================
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if !defined(__FreeBSD__) && !defined(__sun)
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif

#if defined(__FreeBSD__)
#define HAVE_STRUCT_SOCKADDR_SA_LEN
#endif

#undef _BSD_SOURCE
#define _BSD_SOURCE

Expand Down Expand Up @@ -807,6 +815,9 @@ static int dns_inet_pton(int af, const void *src, void *dst) {
union { struct sockaddr_in sin; struct sockaddr_in6 sin6; } u;

u.sin.sin_family = af;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
u.sin.sin_len = dns_af_len(af);
#endif

if (0 != WSAStringToAddressA((void *)src, af, (void *)0, (struct sockaddr *)&u, &(int){ sizeof u }))
return -1;
Expand All @@ -832,6 +843,9 @@ static const char *dns_inet_ntop(int af, const void *src, void *dst, unsigned lo
memset(&u, 0, sizeof u);

u.sin.sin_family = af;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
u.sin.sin_len = dns_af_len(af);
#endif

switch (af) {
case AF_INET6:
Expand Down Expand Up @@ -3877,8 +3891,8 @@ struct dns_resolv_conf *dns_resconf_open(int *error) {
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = INADDR_ANY;
sin->sin_port = htons(53);
#if defined(SA_LEN)
sin->sin_len = sizeof *sin;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
sin->sin_len = dns_af_len(sin->sin_family);
#endif

if (0 != gethostname(resconf->search[0], sizeof resconf->search[0]))
Expand Down Expand Up @@ -6871,6 +6885,9 @@ static int dns_res_exec(struct dns_resolver *R) {
}

sin.sin_family = AF_INET;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
sin.sin_len = dns_af_len(sin.sin_family);
#endif

if ((error = dns_a_parse((struct dns_a *)&sin.sin_addr, &rr, F->hints)))
goto error;
Expand Down Expand Up @@ -7429,6 +7446,9 @@ static int dns_ai_setent(struct addrinfo **ent, union dns_any *any, enum dns_typ

sin.sin_family = AF_INET;
sin.sin_port = htons(ai->port);
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
sin.sin_len = dns_af_len(sin.sin_family);
#endif

memcpy(&sin.sin_addr, any, sizeof sin.sin_addr);

Expand All @@ -7438,6 +7458,9 @@ static int dns_ai_setent(struct addrinfo **ent, union dns_any *any, enum dns_typ

sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(ai->port);
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
sin6.sin6_len = dns_af_len(sin6.sin6_family);
#endif

memcpy(&sin6.sin6_addr, any, sizeof sin6.sin6_addr);

Expand Down Expand Up @@ -8785,6 +8808,9 @@ static int echo_port(int argc DNS_NOTUSED, char *argv[] DNS_NOTUSED) {
port.sin.sin_family = AF_INET;
port.sin.sin_port = htons(5354);
port.sin.sin_addr.s_addr = inet_addr("127.0.0.1");
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
port.sin.sin_len = dns_af_len(port.sin.sin_family);
#endif

if (-1 == (fd = socket(PF_INET, SOCK_DGRAM, 0)))
panic("socket: %s", strerror(errno));
Expand Down
12 changes: 12 additions & 0 deletions ipaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,19 @@ static int ipaddr_ipany(struct ipaddr *addr, int port, int mode)
ipv4->sin_family = AF_INET;
ipv4->sin_addr.s_addr = htonl(INADDR_ANY);
ipv4->sin_port = htons((uint16_t)port);
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
ipv4->sin_len = sizeof(struct sockaddr_in);
#endif
return 0;
}
else {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6*)addr;
ipv6->sin6_family = AF_INET6;
memcpy(&ipv6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
ipv6->sin6_port = htons((uint16_t)port);
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
ipv6->sin6_len = sizeof(struct sockaddr_in6);
#endif
return 0;
}
}
Expand All @@ -82,6 +88,9 @@ static int ipaddr_ipv4_literal(struct ipaddr *addr, const char *name,
if(dill_slow(rc != 1)) {errno = EINVAL; return -1;}
ipv4->sin_family = AF_INET;
ipv4->sin_port = htons((uint16_t)port);
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
ipv4->sin_len = sizeof(struct sockaddr_in);
#endif
return 0;
}

Expand All @@ -94,6 +103,9 @@ static int ipaddr_ipv6_literal(struct ipaddr *addr, const char *name,
if(dill_slow(rc != 1)) {errno = EINVAL; return -1;}
ipv6->sin6_family = AF_INET6;
ipv6->sin6_port = htons((uint16_t)port);
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
ipv6->sin6_len = sizeof(struct sockaddr_in6);
#endif
return 0;
}

Expand Down

0 comments on commit f6a52c5

Please sign in to comment.