diff --git a/deps/c-ares/ares.h b/deps/c-ares/ares.h index 6d658ad2d3..b156c9932e 100644 --- a/deps/c-ares/ares.h +++ b/deps/c-ares/ares.h @@ -434,6 +434,12 @@ struct ares_srv_reply { unsigned short port; }; +struct ares_mx_reply { + struct ares_mx_reply *next; + char *host; + unsigned short priority; +}; + struct ares_txt_reply { struct ares_txt_reply *next; unsigned char *txt; @@ -475,6 +481,10 @@ CARES_EXTERN int ares_parse_srv_reply(const unsigned char* abuf, int alen, struct ares_srv_reply** srv_out); +CARES_EXTERN int ares_parse_mx_reply(const unsigned char* abuf, + int alen, + struct ares_mx_reply** mx_out); + CARES_EXTERN int ares_parse_txt_reply(const unsigned char* abuf, int alen, struct ares_txt_reply** txt_out); diff --git a/deps/c-ares/ares_data.c b/deps/c-ares/ares_data.c index 3a4c00c97d..10f3d799cc 100644 --- a/deps/c-ares/ares_data.c +++ b/deps/c-ares/ares_data.c @@ -63,6 +63,14 @@ void ares_free_data(void *dataptr) switch (ptr->type) { + case ARES_DATATYPE_MX_REPLY: + + if (ptr->data.mx_reply.next) + ares_free_data(ptr->data.mx_reply.next); + if (ptr->data.mx_reply.host) + free(ptr->data.mx_reply.host); + break; + case ARES_DATATYPE_SRV_REPLY: if (ptr->data.srv_reply.next) @@ -114,6 +122,12 @@ void *ares_malloc_data(ares_datatype type) switch (type) { + case ARES_DATATYPE_MX_REPLY: + ptr->data.mx_reply.next = NULL; + ptr->data.mx_reply.host = NULL; + ptr->data.mx_reply.priority = 0; + break; + case ARES_DATATYPE_SRV_REPLY: ptr->data.srv_reply.next = NULL; ptr->data.srv_reply.host = NULL; diff --git a/deps/c-ares/ares_data.h b/deps/c-ares/ares_data.h index 02ff1bca52..52ee4f5c7e 100644 --- a/deps/c-ares/ares_data.h +++ b/deps/c-ares/ares_data.h @@ -20,6 +20,7 @@ typedef enum { ARES_DATATYPE_SRV_REPLY, /* struct ares_srv_reply - introduced in 1.7.0 */ ARES_DATATYPE_TXT_REPLY, /* struct ares_txt_reply - introduced in 1.7.0 */ ARES_DATATYPE_ADDR_NODE, /* struct ares_addr_node - introduced in 1.7.1 */ + ARES_DATATYPE_MX_REPLY, /* struct ares_mx_reply - introduced in 1.7.2 */ #if 0 ARES_DATATYPE_ADDR6TTL, /* struct ares_addrttl */ ARES_DATATYPE_ADDRTTL, /* struct ares_addr6ttl */ @@ -56,6 +57,7 @@ struct ares_data { struct ares_txt_reply txt_reply; struct ares_srv_reply srv_reply; struct ares_addr_node addr_node; + struct ares_mx_reply mx_reply; } data; }; diff --git a/deps/c-ares/ares_parse_mx_reply.c b/deps/c-ares/ares_parse_mx_reply.c new file mode 100644 index 0000000000..186ddd348d --- /dev/null +++ b/deps/c-ares/ares_parse_mx_reply.c @@ -0,0 +1,170 @@ + +/* Copyright 1998 by the Massachusetts Institute of Technology. + * Copyright (C) 2010 Jeremy Lal + * + * Permission to use, copy, modify, and distribute this + * software and its documentation for any purpose and without + * fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting + * documentation, and that the name of M.I.T. not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" + * without express or implied warranty. + */ + +#include "ares_setup.h" + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif +#ifdef HAVE_NETINET_IN_H +# include +#endif +#ifdef HAVE_NETDB_H +# include +#endif +#ifdef HAVE_ARPA_INET_H +# include +#endif +#ifdef HAVE_ARPA_NAMESER_H +# include +#else +# include "nameser.h" +#endif +#ifdef HAVE_ARPA_NAMESER_COMPAT_H +# include +#endif + +#include +#include +#include "ares.h" +#include "ares_dns.h" +#include "ares_data.h" +#include "ares_private.h" + +int +ares_parse_mx_reply (const unsigned char *abuf, int alen, + struct ares_mx_reply **mx_out) +{ + unsigned int qdcount, ancount, i; + const unsigned char *aptr, *vptr; + int status, rr_type, rr_class, rr_len; + long len; + char *hostname = NULL, *rr_name = NULL; + struct ares_mx_reply *mx_head = NULL; + struct ares_mx_reply *mx_last = NULL; + struct ares_mx_reply *mx_curr; + + /* Set *mx_out to NULL for all failure cases. */ + *mx_out = NULL; + + /* Give up if abuf doesn't have room for a header. */ + if (alen < HFIXEDSZ) + return ARES_EBADRESP; + + /* Fetch the question and answer count from the header. */ + qdcount = DNS_HEADER_QDCOUNT (abuf); + ancount = DNS_HEADER_ANCOUNT (abuf); + if (qdcount != 1) + return ARES_EBADRESP; + if (ancount == 0) + return ARES_ENODATA; + + /* Expand the name from the question, and skip past the question. */ + aptr = abuf + HFIXEDSZ; + status = ares_expand_name (aptr, abuf, alen, &hostname, &len); + if (status != ARES_SUCCESS) + return status; + + if (aptr + len + QFIXEDSZ > abuf + alen) + { + free (hostname); + return ARES_EBADRESP; + } + aptr += len + QFIXEDSZ; + + /* Examine each answer resource record (RR) in turn. */ + for (i = 0; i < ancount; i++) + { + /* Decode the RR up to the data field. */ + status = ares_expand_name (aptr, abuf, alen, &rr_name, &len); + if (status != ARES_SUCCESS) + { + break; + } + aptr += len; + if (aptr + RRFIXEDSZ > abuf + alen) + { + status = ARES_EBADRESP; + break; + } + rr_type = DNS_RR_TYPE (aptr); + rr_class = DNS_RR_CLASS (aptr); + rr_len = DNS_RR_LEN (aptr); + aptr += RRFIXEDSZ; + + /* Check if we are really looking at a MX record */ + if (rr_class == C_IN && rr_type == T_MX) + { + /* parse the MX record itself */ + if (rr_len < 2) + { + status = ARES_EBADRESP; + break; + } + + /* Allocate storage for this MX answer appending it to the list */ + mx_curr = ares_malloc_data(ARES_DATATYPE_MX_REPLY); + if (!mx_curr) + { + status = ARES_ENOMEM; + break; + } + if (mx_last) + { + mx_last->next = mx_curr; + } + else + { + mx_head = mx_curr; + } + mx_last = mx_curr; + + vptr = aptr; + mx_curr->priority = ntohs (*((unsigned short *)vptr)); + vptr += sizeof(unsigned short); + + status = ares_expand_name (vptr, abuf, alen, &mx_curr->host, &len); + if (status != ARES_SUCCESS) + break; + } + + /* Don't lose memory in the next iteration */ + free (rr_name); + rr_name = NULL; + + /* Move on to the next record */ + aptr += rr_len; + } + + if (hostname) + free (hostname); + if (rr_name) + free (rr_name); + + /* clean up on error */ + if (status != ARES_SUCCESS) + { + if (mx_head) + ares_free_data (mx_head); + return status; + } + + /* everything looks fine, return the data */ + *mx_out = mx_head; + + return ARES_SUCCESS; +}