Commit aa88825a authored by Bruno Chevalier's avatar Bruno Chevalier

MDNS amount of probing and announcement packets adjusted

parent 8388b725
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -21,7 +21,7 @@ int pico_mdns_init( const char *hostname,
\subsubsection*{Parameters}
\begin{itemize}[noitemsep]
\item \texttt{hostname} - Hostname to register for this machine. Should end with '.local'.
\item \texttt{hostname} - Hostname to register for this machine. Should end with \'.local\'.
\item \texttt{address} - IPv4-address of this machines interface to generate a hostname record from.
\item \texttt{cb$\_$initialised} - Callback-function that is called when the initialisation process is done. This will also get called when asynchronous conflicts occur for successfully registered records during run-time. The mDNS-record tree contains the registered records, the char-buffer contains the registered hostname and the void-pointer contains the passed argument.
\item \texttt{arg} - Argument for callback supplied by user. This can be used if you want to pass some variable into your callback function.
......@@ -69,16 +69,16 @@ char *url = pico_mdns_get_hostname();
\subsection{pico$\_$mdns$\_$set$\_$hostname}
\subsubsection*{Description}
Sets the hostname for this machine. Claims automatically a unique A record with the IPv4-address of this host. The hostname won't be set directly when this functions returns, but only if the claiming of the unique record succeeded. Init-callback specified when initialising the module will be called when the hostname-record is successfully registered.
Tries to claim a hostname for this machine. Claims automatically a unique A record with the IPv4-address of this host. The hostname won't be set directly when this functions returns, but only if the claiming of the unique record succeeded. Init-callback specified when initialising the module will be called when the hostname-record is successfully registered.
\subsubsection*{Function prototype}
\begin{verbatim}
int pico_mdns_set_hostname( const char *url, void *arg );
int pico_mdns_tryclaim_hostname( const char *url, void *arg );
\end{verbatim}
\subsubsection*{Parameters}
\begin{itemize}[noitemsep]
\item \texttt{url} - URL to set the hostname to. Should end with '.local'.
\item \texttt{url} - URL to set the hostname to. Should end with \'.local\'.
\item \texttt{arg} - Argument for init-callback supplied by user. This can be used if you want to pass some variable into your callback function.
\end{itemize}
......@@ -93,7 +93,7 @@ Returns 0 when the host started registering the hostname-record successfully, re
\subsubsection*{Example}
\begin{verbatim}
int ret = pico_mdns_set_hostname("device.local", NULL);
int ret = pico_mdns_tryclaim_hostname("device.local", NULL);
\end{verbatim}
......
......@@ -35,10 +35,12 @@ static int tapdev_link_state = 0;
static void sig_handler(int signo)
{
if (signo == SIGUSR1)
tapdev_link_state = 0;
if (signo == SIGUSR2)
tapdev_link_state = 1;
if (signo == SIGUSR1){
tapdev_link_state = 0;
}
if (signo == SIGUSR2){
tapdev_link_state = 1;
}
}
static int tap_link_state(__attribute__((unused)) struct pico_device *self)
......@@ -62,8 +64,9 @@ static int pico_tap_poll(struct pico_device *dev, int loop_score)
pfd.fd = tap->fd;
pfd.events = POLLIN;
do {
if (poll(&pfd, 1, 0) <= 0)
if (poll(&pfd, 1, 0) <= 0){
return loop_score;
}
len = (int)read(tap->fd, buf, TUN_MTU);
if (len > 0) {
......@@ -79,8 +82,9 @@ static int pico_tap_poll(struct pico_device *dev, int loop_score)
void pico_tap_destroy(struct pico_device *dev)
{
struct pico_device_tap *tap = (struct pico_device_tap *) dev;
if(tap->fd > 0)
if(tap->fd > 0){
close(tap->fd);
}
}
#ifndef __FreeBSD__
......@@ -156,8 +160,9 @@ static int tap_get_mac(char *name, uint8_t *mac)
root = ifap;
while(ifap) {
if (strcmp(name, ifap->ifa_name) == 0)
if (strcmp(name, ifap->ifa_name) == 0){
sdl = (struct sockaddr_dl *) ifap->ifa_addr;
}
if (sdl->sdl_type == IFT_ETHER) {
memcpy(mac, LLADDR(sdl), 6);
......@@ -178,16 +183,18 @@ struct pico_device *pico_tap_create(char *name)
uint8_t mac[6] = {};
struct sigaction sa;
if (!tap)
if (!tap){
return NULL;
}
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sa.sa_handler = sig_handler;
if ((sigaction(SIGUSR1, &sa, NULL) == 0) &&
(sigaction(SIGUSR2, &sa, NULL) == 0))
tap->dev.link_state = &tap_link_state;
(sigaction(SIGUSR2, &sa, NULL) == 0)){
tap->dev.link_state = &tap_link_state;
}
tap->dev.overhead = 0;
tap->fd = tap_open(name);
......
......@@ -52,9 +52,10 @@ pico_dns_sd_kv_vector_strlen( kv_vector *vector )
iterator = pico_dns_sd_kv_vector_get(vector, i);
len = (uint16_t) (len + 1u + /* Length byte */
strlen(iterator->key) /* Length of the key */);
if (iterator->value)
if (iterator->value){
len = (uint16_t) (len + 1u /* '=' char */ +
strlen(iterator->value) /* Length of value */);
}
}
return len;
}
......@@ -147,8 +148,9 @@ pico_dns_sd_txt_record_create( const char *url,
uint16_t len = (uint16_t)(pico_dns_sd_kv_vector_strlen(&key_value_pairs) + 1u);
/* If kv-vector is empty don't bother to create a TXT record */
if (len <= 1)
if (len <= 1){
return NULL;
}
/* Provide space for the txt buf */
if (!(txt = (char *)PICO_ZALLOC(len))) {
......@@ -399,8 +401,9 @@ pico_dns_sd_register_service( const char *name,
/* Try to create a service URL to create records with */
if (!(url = pico_dns_sd_create_service_url(name, type)) || !txt_data || !hostname) {
if (url)
if (url){
PICO_FREE(url);
}
pico_err = PICO_ERR_EINVAL;
return -1;
}
......@@ -421,8 +424,9 @@ pico_dns_sd_register_service( const char *name,
/* Erase the key-value pair vector, it's no longer needed */
pico_dns_sd_kv_vector_erase(txt_data);
if (txt_record)
if (txt_record){
pico_tree_insert(&rtree, txt_record);
}
pico_tree_insert(&rtree, srv_record);
......
This diff is collapsed.
......@@ -17,8 +17,24 @@
#define PICO_MDNS_ALLOW_CACHING 1 /* Enable caching on this host */
#define PICO_MDNS_DEFAULT_TTL 120 /* Default TTL of mDNS records */
#define PICO_MDNS_SERVICE_TTL 120 /* Default TTL of SRV/TXT/PTR/NSEC */
#define PICO_MDNS_PROBE_COUNT 4 /* Amount of probes to send */
#define PICO_MDNS_ANNOUNCEMENT_COUNT 3 /* Amount of announcements to send */
#define PICO_MDNS_PROBE_COUNT 3
/* Amount of probes to send:
RFC6762: 8.1. Probing:
250 ms after the first query, the host should send a second; then,
250 ms after that, a third. If, by 250 ms after the third probe, no
conflicting Multicast DNS responses have been received, the host may
move to the next step, announcing.
*/
#define PICO_MDNS_ANNOUNCEMENT_COUNT 3
/* Amount of announcements to send: (we've opted for 1 extra for robustness)
RFC6762: 8.3. Announcing:
The Multicast DNS responder MUST send at least two unsolicited
responses, one second apart. To provide increased robustness against
packet loss, a responder MAY send up to eight unsolicited responses,
provided that the interval between unsolicited responses increases by
at least a factor of two with every response sent.
*/
/* ****************************************************************************/
#define PICO_MDNS_DEST_ADDR4 "224.0.0.251"
......@@ -27,6 +43,10 @@
#define PICO_MDNS_RECORD_UNIQUE 0x00u
#define PICO_MDNS_RECORD_SHARED 0x01u
/* To indicate if we reclaim or not */
#define PICO_MDNS_RECLAIM 1
#define PICO_MDNS_NO_RECLAIM 0
/* Flag to check for when records are returned, to determine the hostname */
#define PICO_MDNS_RECORD_HOSTNAME 0x02u
#define IS_HOSTNAME_RECORD(x) \
......@@ -134,19 +154,20 @@ pico_mdns_claim( pico_mdns_rtree record_tree,
void *arg );
/* ****************************************************************************
* Sets the hostname for this machine. Claims automatically a unique A record
* with the IPv4-address of this host. The hostname won't be set directly when
* this functions returns, but only if the claiming of the unique record succ-
* eeded. Init-callback will be called when the hostname-record is successfully
* Tries to claim a hostname for this machine. Claims automatically a
* unique A record with the IPv4-address of this host.
* The hostname won't be set directly when this functions returns,
* but only if the claiming of the unique record succeeded.
* Init-callback will be called when the hostname-record is successfully
* registered.
*
* @param url URL to set the hostname to.
* @param arg Argument to pass to the init-callback.
* @return 0 when the host started registering the hostname-record successfully,
* Returns something else when it didn't succeeded.
* Returns something else when it didn't succeeded.
* ****************************************************************************/
int
pico_mdns_set_hostname( const char *url, void *arg );
pico_mdns_tryclaim_hostname( const char *url, void *arg );
/* ****************************************************************************
* Get the current hostname for this machine.
......
......@@ -1077,8 +1077,9 @@ MOCKABLE uint32_t pico_timer_add(pico_time expire, void (*timer)(pico_time, void
struct pico_timer_ref tref;
/* zero is guard for timers */
if (tmr_id == 0u)
if (tmr_id == 0u){
tmr_id++;
}
if (!t) {
pico_err = PICO_ERR_ENOMEM;
......
......@@ -48,8 +48,9 @@ void app_dns_sd(char *arg, struct pico_ip4 address)
char *hostname;
char *nxt = arg;
if (!nxt)
if (!nxt){
exit(255);
}
nxt = cpy_arg(&hostname, nxt);
if(!hostname) {
......
......@@ -13,9 +13,10 @@
#include "check.h"
Suite *pico_suite(void);
void add_records( void ); /* MARK: helper to add records to MyRecords s*/
void add_records(void); /* MARK: helper to add records to MyRecords s*/
int mdns_init(void); /* MARK: Initialise mDNS module */
static int amount_callback_executed = 0;
void callback( pico_mdns_rtree *tree,char *str, void *arg);
void callback( pico_mdns_rtree *tree,
char *str,
......@@ -25,6 +26,7 @@ void callback( pico_mdns_rtree *tree,
IGNORE_PARAMETER(str);
IGNORE_PARAMETER(arg);
/* Do nothing, because fail_unless and fail_if don't work here */
amount_callback_executed++;
}
int mdns_init() /* MARK: Initialise mDNS module */
......@@ -39,8 +41,9 @@ int mdns_init() /* MARK: Initialise mDNS module */
};
mock = pico_mock_create(NULL);
if (!mock)
if (!mock){
return -1;
}
pico_ipv4_link_add(mock->dev, local, netmask);
......@@ -1414,6 +1417,7 @@ START_TEST(tc_mdns_my_records_claimed) /* MARK: mdns_my_records_claimed */
printf("*********************** ending %s * \n", __func__);
}
END_TEST
#if PICO_MDNS_ALLOW_CACHING == 1
START_TEST(tc_mdns_cache_add_record) /* MARK: mdns_cache_add_record */
{
struct pico_mdns_record *record = NULL, *found = NULL;
......@@ -1442,6 +1446,7 @@ START_TEST(tc_mdns_cache_add_record) /* MARK: mdns_cache_add_record */
printf("*********************** ending %s * \n", __func__);
}
END_TEST
#endif
START_TEST(tc_pico_tree_merge)
{
PICO_MDNS_RTREE_DECLARE(src);
......@@ -1768,14 +1773,29 @@ START_TEST(tc_mdns_send_query_packet) /* MARK: send_query_packet */
END_TEST
START_TEST(tc_mdns_getrecord) /* MARK: getrecord */
{
#if PICO_MDNS_ALLOW_CACHING == 1
struct pico_mdns_record *record = NULL, *found = NULL;
struct pico_ip4 rdata = {
long_be(0x00FFFFFF)
};
char url[] = "foo.local";
#endif
int ret = 0;
printf("*********************** starting %s * \n", __func__);
/*
If caching is enabled:
If the record is cached:
getrecord should get the record from the cache, execute the callback and return 0 when the callback is finished.
Else:
getrecord should send a query and return 0 when the query is sent.
Else:
getrecord should send a query and return 0 when the query is sent.
*/
#if PICO_MDNS_ALLOW_CACHING == 1
/* Create an A record with URL */
record = pico_mdns_record_create(url, &rdata, 4, PICO_DNS_TYPE_A, 80,
PICO_MDNS_RECORD_UNIQUE);
......@@ -1786,13 +1806,18 @@ START_TEST(tc_mdns_getrecord) /* MARK: getrecord */
"mdns_cache_add_record returned error!\n");
found = pico_tree_findKey(&Cache, record);
fail_unless((int)found, "mdns_cache_add_record failed!\n");
#endif
/* Init */
pico_stack_init();
mdns_init();
#if PICO_MDNS_ALLOW_CACHING == 1
amount_callback_executed=0;
ret = pico_mdns_getrecord("foo.local", PICO_DNS_TYPE_A, callback, NULL);
fail_unless(1 == amount_callback_executed, "mdns_getrecord failed with cache record, callback not executed!\n");
fail_unless(0 == ret, "mdns_getrecord failed with cache record!\n");
#endif
ret = pico_mdns_getrecord("bar.local", PICO_DNS_TYPE_A, callback, NULL);
fail_unless(0 == ret, "mdns_getrecord failed!\n");
......@@ -1937,7 +1962,7 @@ START_TEST(tc_mdns_claim) /* MARK: mdns_claim */
printf("*********************** ending %s * \n", __func__);
}
END_TEST
START_TEST(tc_mdns_set_hostname) /* MARK: set_hostname */
START_TEST(tc_mdns_tryclaim_hostname) /* MARK: tryclaim_hostname */
{
int ret = 0;
......@@ -1945,8 +1970,8 @@ START_TEST(tc_mdns_set_hostname) /* MARK: set_hostname */
pico_stack_init();
mdns_init();
ret = pico_mdns_set_hostname("test.local", NULL);
fail_unless(0 == ret, "mdns_set_hostname failed!\n");
ret = pico_mdns_tryclaim_hostname("test.local", NULL);
fail_unless(0 == ret, "mdns_tryclaim_hostname failed!\n");
printf("*********************** ending %s * \n", __func__);
}
......@@ -2043,7 +2068,7 @@ Suite *pico_suite(void)
TCase *TCase_mdns_claim = tcase_create("Unit test for mnds_claim");
/* API functions */
TCase *TCase_mdns_set_hostname = tcase_create("Unit test for mdns_set_hostname");
TCase *TCase_mdns_tryclaim_hostname = tcase_create("Unit test for mdns_tryclaim_hostname");
TCase *TCase_mdns_get_hostname = tcase_create("Unit test for mdns_get_hostname");
TCase *TCase_pico_tree_merge = tcase_create("Unit test for pico_tree_merge");
......@@ -2169,8 +2194,8 @@ Suite *pico_suite(void)
suite_add_tcase(s, TCase_mdns_claim);
/* API functions */
tcase_add_test(TCase_mdns_set_hostname, tc_mdns_set_hostname);
suite_add_tcase(s, TCase_mdns_set_hostname);
tcase_add_test(TCase_mdns_tryclaim_hostname, tc_mdns_tryclaim_hostname);
suite_add_tcase(s, TCase_mdns_tryclaim_hostname);
tcase_add_test(TCase_mdns_get_hostname, tc_mdns_get_hostname);
suite_add_tcase(s, TCase_mdns_get_hostname);
......
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