Add rtems_mdns_sethostname()

Use it to set the multi-cast hostname of the default mDNS resolver
instance via sethostname().
This commit is contained in:
Sebastian Huber
2014-11-06 08:28:06 +01:00
parent 50e82a6d15
commit 7a3fe8ef43
6 changed files with 104 additions and 4 deletions

View File

@@ -40,6 +40,9 @@
extern "C" {
#endif /* __cplusplus */
/* Private variable, do not touch. Use rtems_mdns_sethostname() instead. */
extern void (*rtems_mdns_sethostname_handler)(const char *hostname);
/**
* @brief Initializes an mDNS resolver instance.
*
@@ -67,6 +70,20 @@ rtems_status_code rtems_mdns_initialize(rtems_task_priority daemon_priority,
*/
mDNS *rtems_mdns_get_instance(void);
/**
* @brief Sets the multi-cast hostname of the mDNS resolver instance.
*
* In case the mDNS resolver instance is not initialized, then this function
* has no effect.
*
* @param[in] hostname The new multi-cast hostname.
*/
static inline void
rtems_mdns_sethostname(const char *hostname)
{
(*rtems_mdns_sethostname_handler)(hostname);
}
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@@ -0,0 +1,41 @@
/*
* Copyright (c) 2014 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <rtems/mdns.h>
static void
mdns_sethostname_default(const char *hostname)
{
/* Do nothing */
}
void (*rtems_mdns_sethostname_handler)(const char *hostname) =
mdns_sethostname_default;

View File

@@ -31,6 +31,7 @@
#include <mDNSEmbeddedAPI.h>
#include <mDNSPosix.h>
#include <DNSCommon.h>
#include <sys/select.h>
#include <sys/socket.h>
@@ -54,7 +55,7 @@
#include <rtems/bsd/util.h>
#include <rtems/mdns.h>
static rtems_id daemon_id;
static rtems_id mdns_daemon_id;
static mDNS mDNSStorage;
@@ -223,7 +224,7 @@ mdns_gethostbyname(void *rval, void *cb_data, va_list ap)
ctx.task_id = rtems_task_self();
mDNS_StartQuery(&mDNSStorage, &q);
rtems_bsd_force_select_timeout(daemon_id);
rtems_bsd_force_select_timeout(mdns_daemon_id);
sc = rtems_event_transient_receive(RTEMS_WAIT,
10 * rtems_clock_get_ticks_per_second());
@@ -261,6 +262,36 @@ mdns_daemon(rtems_task_argument arg)
}
}
static void
truncate_at_first_dot(domainlabel *name)
{
int c = name->c[0];
int n = 0;
while (n < c && name->c[n + 1] != '.') {
++n;
}
name->c[0] = n;
}
static void
mdns_sethostname(const char *hostname)
{
mDNS *m = &mDNSStorage;
mDNS_Lock(m);
MakeDomainLabelFromLiteralString(&m->hostlabel, hostname);
truncate_at_first_dot(&m->hostlabel);
mDNS_Unlock(m);
mDNS_SetFQDN(m);
rtems_bsd_force_select_timeout(mdns_daemon_id);
}
rtems_status_code
rtems_mdns_initialize(rtems_task_priority daemon_priority,
CacheEntity *rrcachestorage, mDNSu32 rrcachesize)
@@ -279,12 +310,12 @@ rtems_mdns_initialize(rtems_task_priority daemon_priority,
sc = rtems_task_create(rtems_build_name('m', 'D', 'N', 'S'),
daemon_priority, 16 * 1024, RTEMS_DEFAULT_MODES,
RTEMS_DEFAULT_ATTRIBUTES, &daemon_id);
RTEMS_DEFAULT_ATTRIBUTES, &mdns_daemon_id);
if (sc != RTEMS_SUCCESSFUL) {
return (RTEMS_UNSATISFIED);
}
sc = rtems_task_start(daemon_id, mdns_daemon, 0);
sc = rtems_task_start(mdns_daemon_id, mdns_daemon, 0);
if (sc != RTEMS_SUCCESSFUL) {
return (RTEMS_UNSATISFIED);
}
@@ -311,6 +342,8 @@ rtems_mdns_initialize(rtems_task_priority daemon_priority,
}
}
rtems_mdns_sethostname_handler = mdns_sethostname;
return (RTEMS_SUCCESSFUL);
}