mirror of https://github.com/c-ares/c-ares.git
parent
f28874d323
commit
63918fca76
7 changed files with 286 additions and 0 deletions
@ -0,0 +1,79 @@ |
||||
.\" |
||||
.\" Copyright 1998 by the Massachusetts Institute of Technology. |
||||
.\" |
||||
.\" 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. |
||||
.\" |
||||
.TH ARES_PARSE_MX_REPLY 3 "4 August 2009" |
||||
.SH NAME |
||||
ares_parse_mx_reply \- Parse a reply to a DNS query of type MX |
||||
.SH SYNOPSIS |
||||
.nf |
||||
.B #include <ares.h> |
||||
.PP |
||||
.B int ares_parse_mx_reply(const unsigned char* \fIabuf\fP, int \fIalen\fP, |
||||
.B struct ares_mx_reply** \fImx_out\fP); |
||||
.fi |
||||
.SH DESCRIPTION |
||||
The |
||||
.B ares_parse_mx_reply |
||||
function parses the response to a query of type MX into a |
||||
linked list of |
||||
.I struct ares_mx_reply |
||||
The parameters |
||||
.I abuf |
||||
and |
||||
.I alen |
||||
give the contents of the response. The result is stored in allocated |
||||
memory and a pointer to it stored into the variable pointed to by |
||||
.IR mx_out . |
||||
It is the caller's responsibility to free the resulting |
||||
.IR mx_out |
||||
structure when it is no longer needed using the function |
||||
.B ares_free_data |
||||
.PP |
||||
The structure |
||||
.I ares_mx_reply |
||||
contains the following fields: |
||||
.sp |
||||
.in +4n |
||||
.nf |
||||
struct ares_mx_reply { |
||||
struct ares_mx_reply *next; |
||||
char *host; |
||||
unsigned short priority; |
||||
}; |
||||
.fi |
||||
.in |
||||
.PP |
||||
.SH RETURN VALUES |
||||
.B ares_parse_mx_reply |
||||
can return any of the following values: |
||||
.TP 15 |
||||
.B ARES_SUCCESS |
||||
The response was successfully parsed. |
||||
.TP 15 |
||||
.B ARES_EBADRESP |
||||
The response was malformatted. |
||||
.TP 15 |
||||
.B ARES_ENODATA |
||||
The response did not contain an answer to the query. |
||||
.TP 15 |
||||
.B ARES_ENOMEM |
||||
Memory was exhausted. |
||||
.SH AVAILABILITY |
||||
This function was first introduced in c-ares version 1.7.2. |
||||
.SH SEE ALSO |
||||
.BR ares_query (3) |
||||
.BR ares_free_data (3) |
||||
.SH AUTHOR |
||||
Written by Jeremy Lal <kapouer@melix.org> |
@ -0,0 +1,170 @@ |
||||
|
||||
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
||||
* Copyright (C) 2010 Jeremy Lal <kapouer@melix.org> |
||||
* |
||||
* 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 <sys/socket.h> |
||||
#endif |
||||
#ifdef HAVE_NETINET_IN_H |
||||
# include <netinet/in.h> |
||||
#endif |
||||
#ifdef HAVE_NETDB_H |
||||
# include <netdb.h> |
||||
#endif |
||||
#ifdef HAVE_ARPA_INET_H |
||||
# include <arpa/inet.h> |
||||
#endif |
||||
#ifdef HAVE_ARPA_NAMESER_H |
||||
# include <arpa/nameser.h> |
||||
#else |
||||
# include "nameser.h" |
||||
#endif |
||||
#ifdef HAVE_ARPA_NAMESER_COMPAT_H |
||||
# include <arpa/nameser_compat.h> |
||||
#endif |
||||
|
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#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; |
||||
} |
Loading…
Reference in new issue