From: Willem Toorop Date: Tue, 1 Oct 2013 13:58:26 +0000 (+0200) Subject: Accessor functions for LDNS_RDF_TYPE_HIP rdfs X-Git-Tag: release-1.6.17rc1~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b3c5febabf60d194986d0c1331a1d10833bafdb2;p=thirdparty%2Fldns.git Accessor functions for LDNS_RDF_TYPE_HIP rdfs --- diff --git a/error.c b/error.c index 6c3aa196..82ea61a1 100644 --- a/error.c +++ b/error.c @@ -140,6 +140,9 @@ ldns_lookup_table ldns_error_str[] = { { LDNS_STATUS_TYPE_NOT_IN_BITMAP, "The RR type bitmap rdata field did not have " "a bit reserved for the specific RR type" }, + { LDNS_STATUS_INVALID_RDF_TYPE, + "The rdata field was not of the expected type" }, + { LDNS_STATUS_RDATA_OVERFLOW, "Rdata size overflow" }, { 0, NULL } }; diff --git a/ldns/error.h b/ldns/error.h index e70f8d6a..41b99ad1 100644 --- a/ldns/error.h +++ b/ldns/error.h @@ -125,6 +125,8 @@ enum ldns_enum_status { LDNS_STATUS_WIRE_RDATA_ERR, LDNS_STATUS_INVALID_TAG, LDNS_STATUS_TYPE_NOT_IN_BITMAP, + LDNS_STATUS_INVALID_RDF_TYPE, + LDNS_STATUS_RDATA_OVERFLOW, }; typedef enum ldns_enum_status ldns_status; diff --git a/ldns/rdata.h b/ldns/rdata.h index 7a9afb45..1866e8fc 100644 --- a/ldns/rdata.h +++ b/ldns/rdata.h @@ -408,6 +408,34 @@ ldns_rdf *ldns_rdf_clone(const ldns_rdf *rd); */ int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2); +/** + * Gets the algorithm value, the HIT and Public Key data from the rdf with + * type LDNS_RDF_TYPE_HIP. + * \param[in] rdf the rdf with type LDNS_RDF_TYPE_HIP + * \param[out] alg the algorithm + * \param[out] hit_size the size of the HIT data + * \param[out] hit the hit data + * \param[out] pk_size the size of the Public Key data + * \param[out] pk the Public Key data + * \return LDNS_STATUS_OK on success, and the error otherwise + */ +ldns_status ldns_rdf_hip_get_alg_hit_pk(ldns_rdf *rdf, uint8_t* alg, + uint8_t *hit_size, uint8_t** hit, + uint16_t *pk_size, uint8_t** pk); + +/** + * Creates a new LDNS_RDF_TYPE_HIP rdf from given data. + * \param[out] rdf the newly created LDNS_RDF_TYPE_HIP rdf + * \param[in] alg the algorithm + * \param[in] hit_size the size of the HIT data + * \param[in] hit the hit data + * \param[in] pk_size the size of the Public Key data + * \param[in] pk the Public Key data + * \return LDNS_STATUS_OK on success, and the error otherwise + */ +ldns_status ldns_rdf_hip_new_frm_alg_hit_pk(ldns_rdf** rdf, uint8_t alg, + uint8_t hit_size, uint8_t *hit, uint16_t pk_size, uint8_t *pk); + #ifdef __cplusplus } #endif diff --git a/rdata.c b/rdata.c index 2af1ee1e..64004031 100644 --- a/rdata.c +++ b/rdata.c @@ -518,6 +518,67 @@ ldns_rdf_address_reverse(ldns_rdf *rd) return rev; } +ldns_status +ldns_rdf_hip_get_alg_hit_pk(ldns_rdf *rdf, uint8_t* alg, + uint8_t *hit_size, uint8_t** hit, + uint16_t *pk_size, uint8_t** pk) +{ + uint8_t *data; + size_t rdf_size; + + if (! rdf || ! alg || ! hit || ! hit_size || ! pk || ! pk_size) { + return LDNS_STATUS_INVALID_POINTER; + + } else if (ldns_rdf_get_type(rdf) != LDNS_RDF_TYPE_HIP) { + return LDNS_STATUS_INVALID_RDF_TYPE; + + } + else if ((rdf_size = ldns_rdf_size(rdf)) < 6) { + return LDNS_STATUS_WIRE_RDATA_ERR; + } + data = ldns_rdf_data(rdf); + *hit_size = data[0]; + *alg = data[1]; + *pk_size = ldns_read_uint16(data + 2); + *hit = data + 4; + *pk = data + 4 + *hit_size; + if (*hit_size == 0 || *pk_size == 0 || + rdf_size < (size_t) *hit_size + *pk_size + 4) { + return LDNS_STATUS_WIRE_RDATA_ERR; + } + return LDNS_STATUS_OK; +} + +ldns_status +ldns_rdf_hip_new_frm_alg_hit_pk(ldns_rdf** rdf, uint8_t alg, + uint8_t hit_size, uint8_t *hit, + uint16_t pk_size, uint8_t *pk) +{ + uint8_t *data; + + if (! rdf) { + return LDNS_STATUS_INVALID_POINTER; + } + if (4 + hit_size + pk_size > LDNS_MAX_RDFLEN) { + return LDNS_STATUS_RDATA_OVERFLOW; + } + data = LDNS_XMALLOC(uint8_t, 4 + hit_size + pk_size); + if (data == NULL) { + return LDNS_STATUS_MEM_ERR; + } + data[0] = hit_size; + data[1] = alg; + ldns_write_uint16(data + 2, pk_size); + memcpy(data + 4, hit, hit_size); + memcpy(data + 4 + hit_size, pk, pk_size); + *rdf = ldns_rdf_new(LDNS_RDF_TYPE_HIP, 4 + hit_size + pk_size, data); + if (! *rdf) { + LDNS_FREE(data); + return LDNS_STATUS_MEM_ERR; + } + return LDNS_STATUS_OK; +} + ldns_status ldns_octet(char *word, size_t *length) {