From 2bd272b4e4f779ca0a1dd4b52bb710be5c68a7bd Mon Sep 17 00:00:00 2001 From: lurchi Date: Fri, 29 Jun 2018 00:01:48 +0200 Subject: [PATCH] introduce API functions for duplicating the DNSPARSER record types --- src/include/gnunet_dnsparser_lib.h | 53 +++++++++++++ src/util/dnsparser.c | 116 +++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) diff --git a/src/include/gnunet_dnsparser_lib.h b/src/include/gnunet_dnsparser_lib.h index ba1392510..0fc6ac19c 100644 --- a/src/include/gnunet_dnsparser_lib.h +++ b/src/include/gnunet_dnsparser_lib.h @@ -82,6 +82,7 @@ #define GNUNET_DNSPARSER_TYPE_OPENPGPKEY 61 #define GNUNET_DNSPARSER_TYPE_TKEY 249 #define GNUNET_DNSPARSER_TYPE_TSIG 250 +#define GNUNET_DNSPARSER_TYPE_ALL 255 #define GNUNET_DNSPARSER_TYPE_URI 256 #define GNUNET_DNSPARSER_TYPE_TA 32768 @@ -840,6 +841,58 @@ GNUNET_DNSPARSER_parse_srv (const char *udp_payload, size_t udp_payload_length, size_t *off); +/* ***************** low-level duplication API ******************** */ + +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_Record * +GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r); + + +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_SoaRecord * +GNUNET_DNSPARSER_duplicate_soa_record (const struct GNUNET_DNSPARSER_SoaRecord *r); + + +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_CertRecord * +GNUNET_DNSPARSER_duplicate_cert_record (const struct GNUNET_DNSPARSER_CertRecord *r); + + +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_MxRecord * +GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r); + + +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_SrvRecord * +GNUNET_DNSPARSER_duplicate_srv_record (const struct GNUNET_DNSPARSER_SrvRecord *r); + + /* ***************** low-level deallocation API ******************** */ /** diff --git a/src/util/dnsparser.c b/src/util/dnsparser.c index cce68f2ee..57d0a014c 100644 --- a/src/util/dnsparser.c +++ b/src/util/dnsparser.c @@ -758,6 +758,122 @@ GNUNET_DNSPARSER_parse (const char *udp_payload, } +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_Record * +GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r) +{ + struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof (*r)); + + dup->name = GNUNET_strdup (r->name); + switch (r->type) + { + case GNUNET_DNSPARSER_TYPE_NS: + case GNUNET_DNSPARSER_TYPE_CNAME: + case GNUNET_DNSPARSER_TYPE_PTR: + { + dup->data.hostname = GNUNET_strdup (r->data.hostname); + break; + } + case GNUNET_DNSPARSER_TYPE_SOA: + { + dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa); + break; + } + case GNUNET_DNSPARSER_TYPE_CERT: + { + dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert); + break; + } + case GNUNET_DNSPARSER_TYPE_MX: + { + dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx); + break; + } + case GNUNET_DNSPARSER_TYPE_SRV: + { + dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv); + break; + } + default: + { + dup->data.raw.data = GNUNET_memdup (r->data.raw.data, + r->data.raw.data_len); + } + } + return dup; +} + + +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_SoaRecord * +GNUNET_DNSPARSER_duplicate_soa_record (const struct GNUNET_DNSPARSER_SoaRecord *r) +{ + struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof (*r)); + + dup->mname = GNUNET_strdup (r->mname); + dup->rname = GNUNET_strdup (r->rname); + return dup; +} + + +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_CertRecord * +GNUNET_DNSPARSER_duplicate_cert_record (const struct GNUNET_DNSPARSER_CertRecord *r) +{ + struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof (*r)); + + dup->certificate_data = GNUNET_strdup (r->certificate_data); + return dup; +} + + +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_MxRecord * +GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r) +{ + struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof (*r)); + + dup->mxhost = GNUNET_strdup (r->mxhost); + return dup; +} + + +/** + * Duplicate (deep-copy) the given DNS record + * + * @param r the record + * @return the newly allocated record + */ +struct GNUNET_DNSPARSER_SrvRecord * +GNUNET_DNSPARSER_duplicate_srv_record (const struct GNUNET_DNSPARSER_SrvRecord *r) +{ + struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof (*r)); + + dup->target = GNUNET_strdup (r->target); + return dup; +} + + /** * Free memory taken by a packet. * -- 2.25.1