Commit 03604a52 authored by Lubos Slovak's avatar Lubos Slovak

Added function for concatenating domain names.

TODO: add test for this function, it was written in hurry :)

refs #5
parent 6246c420
......@@ -227,6 +227,13 @@ const struct dnslib_node *dnslib_dname_node(const dnslib_dname_t *dname) {
/*----------------------------------------------------------------------------*/
int dnslib_dname_is_fqdn(const dnslib_dname_t *dname)
{
return (dname->name[dname->size - 1] == '\0');
}
/*----------------------------------------------------------------------------*/
void dnslib_dname_free(dnslib_dname_t **dname)
{
if (dname == NULL || *dname == NULL) {
......@@ -309,3 +316,33 @@ int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2)
return 0;
}
/*----------------------------------------------------------------------------*/
dnslib_dname_t *dnslib_dname_cat(dnslib_dname_t *d1, const dnslib_dname_t *d2)
{
if (d2->size == 0) {
return d1;
}
if (dnslib_dname_is_fqdn(d1)) {
return NULL;
}
// allocate new space
uint8_t *new_dname = (uint8_t *)malloc(d1->size + d2->size);
if (new_dname == NULL) {
ERR_ALLOC_FAILED;
return NULL;
}
memcpy(new_dname, d1->name, d1->size);
new_dname += d1->size;
memcpy(new_dname, d2->name, d2->size);
uint8_t *old_name = d1->name;
d1->name = new_dname;
free(old_name);
return d1;
}
......@@ -128,6 +128,16 @@ uint dnslib_dname_size(const dnslib_dname_t *dname);
*/
const struct dnslib_node *dnslib_dname_node(const dnslib_dname_t *dname);
/*!
* \brief Checks if the given domain name is a fully-qualified domain name.
*
* \param dname Domain name to check.
*
* \retval 0 if \a dname is not a FQDN.
* \retval <> if \a dname is a FQDN.
*/
int dnslib_dname_is_fqdn(const dnslib_dname_t *dname);
/*!
* \brief Destroys the given domain name.
*
......@@ -153,6 +163,19 @@ void dnslib_dname_free(dnslib_dname_t **dname);
*/
int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2);
/*!
* \brief Concatenates two domain names.
*
* \note Member \a node is ignored, i.e. preserved.
*
* \param d1 First domain name (will be modified).
* \param d2 Second domain name (will not be modified).
*
* \return The concatenated domain name (i.e. modified \a d1) or NULL if
* the operation is not valid (e.g. \a d1 is a FQDN).
*/
dnslib_dname_t *dnslib_dname_cat(dnslib_dname_t *d1, const dnslib_dname_t *d2);
#endif /* _CUTEDNS_DNSLIB_DNAME_H_ */
/*! @} */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment