Skip to content

Commit

Permalink
[mDNS] - Final update on mDNS
Browse files Browse the repository at this point in the history
  • Loading branch information
jelledevleeschouwer committed Jun 8, 2015
1 parent 41d8965 commit 49a19b0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 21 deletions.
44 changes: 25 additions & 19 deletions modules/pico_mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#ifdef PICO_SUPPORT_MDNS

/* --- Debugging --- */
//#define mdns_dbg(...) do {} while(0)
#define mdns_dbg dbg
#define mdns_dbg(...) do {} while(0)
//#define mdns_dbg dbg

#define PICO_MDNS_QUERY_TIMEOUT (10000) /* Ten seconds */
#define PICO_MDNS_RR_TTL_TICK (1000) /* One second */
Expand Down Expand Up @@ -50,22 +50,24 @@
(((x) & PICO_MDNS_QUESTION_FLAG_UNICAST_RES) ? 0 : 1 )

/* Resource Record flags */
#define PICO_MDNS_RECORD_HOSTNAME 0x02u
#define PICO_MDNS_RECORD_ADDITIONAL 0x08u
#define PICO_MDNS_RECORD_SEND_UNICAST 0x10u
#define PICO_MDNS_RECORD_CURRENTLY_PROBING 0x20u
#define PICO_MDNS_RECORD_PROBED 0x40u
#define PICO_MDNS_RECORD_CLAIMED 0x80u

#define IS_SHARED_RECORD(x) \
(((x)->flags & PICO_MDNS_RECORD_SHARED) ? 1 : 0)
#define IS_UNIQUE_RECORD(x) \
(((x)->flags & PICO_MDNS_RECORD_SHARED) ? 0 : 1)
#define IS_RECORD_PROBING(x) \
(((x)->flags & PICO_MDNS_RECORD_CURRENTLY_PROBING) ? 1 : 0)
#define IS_RECORD_VERIFIED(x) \
(((x)->flags & PICO_MDNS_RECORD_PROBED) ? 1 : 0)
#define IS_UNICAST_REQUESTED(x) \
(((x)->flags & PICO_MDNS_RECORD_SEND_UNICAST) ? 1 : 0)
#define IS_RECORD_VERIFIED(x) \
(((x)->flags & PICO_MDNS_RECORD_PROBED) ? 1 : 0)
#define IS_RECORD_CLAIMED(x) \
(((x)->flags & PICO_MDNS_RECORD_CLAIMED) ? 1 : 0)

/* Set and clear flags */
#define PICO_MDNS_SET_FLAG(x, b) (x = ((x) | (uint8_t)(b)))
Expand Down Expand Up @@ -1185,7 +1187,8 @@ pico_mdns_my_records_find_probed( void )
/* Iterate over MyRecords */
pico_tree_foreach(node, &MyRecords) {
record = node->keyValue;
if (record && IS_RECORD_VERIFIED(record)) {
if (record && IS_RECORD_VERIFIED(record) &&
!IS_RECORD_CLAIMED(record)) {
if ((copy = pico_mdns_record_copy(record)))
pico_tree_insert(&probed, copy);
}
Expand Down Expand Up @@ -1266,16 +1269,18 @@ pico_mdns_my_records_claimed( pico_mdns_rtree rtree,
void *arg )
{
PICO_MDNS_RTREE_DECLARE(claimed_records);
struct pico_mdns_record *record = NULL;
struct pico_mdns_record *record = NULL, *myrecord = NULL;
struct pico_tree_node *node = NULL;
uint8_t claim_id = 0;

/* Iterate over records and set the PROBED flag */
pico_tree_foreach(node, &rtree) {
if ((record = node->keyValue) && !claim_id) {
claim_id = record->claim_id;
break;
if ((record = node->keyValue)) {
if (!claim_id)
claim_id = record->claim_id;
}
if ((myrecord = pico_tree_findKey(&MyRecords, record)))
PICO_MDNS_SET_FLAG(myrecord->flags, PICO_MDNS_RECORD_CLAIMED);
}

/* If all_claimed is still true */
Expand Down Expand Up @@ -2151,7 +2156,7 @@ pico_mdns_additionals_add_nsec( pico_mdns_rtree *artree,

/* Check if there is a NSEC already for this name */
pico_tree_foreach(node, artree) {
if ((record = node->keyValue)) {
if (node != &LEAF && (record = node->keyValue)) {
type = short_be(record->record->rsuffix->rtype);
if (PICO_DNS_TYPE_NSEC == type) {
if (strcmp(record->record->rname, name) == 0)
Expand Down Expand Up @@ -2181,7 +2186,7 @@ pico_mdns_additionals_add_host( pico_mdns_rtree *artree )
pico_tree_foreach(node, &MyRecords) {
if ((record = node->keyValue) &&
IS_HOSTNAME_RECORD(record) && IS_RECORD_VERIFIED(record)) {
pico_tree_insert(artree, record);
pico_tree_insert(artree, pico_mdns_record_copy(record));
}
}

Expand Down Expand Up @@ -2226,6 +2231,7 @@ pico_mdns_gather_service_meta( pico_mdns_rtree *antree,
PICO_DNS_TYPE_PTR,
ttl, PICO_MDNS_RECORD_SHARED);
if (!ptr_record) {
mdns_dbg("Could not generate PTR record!\n");
PICO_FREE(sin);
return -1;
}
Expand All @@ -2238,6 +2244,7 @@ pico_mdns_gather_service_meta( pico_mdns_rtree *antree,
ttl, PICO_MDNS_RECORD_SHARED);
PICO_FREE(sin);
if (!meta_record) {
mdns_dbg("Could not generate META record!\n");
pico_mdns_record_delete((void **)&ptr_record);
return -1;
}
Expand Down Expand Up @@ -2316,18 +2323,17 @@ pico_mdns_reply( pico_mdns_rtree *antree, struct pico_ip4 peer )
PICO_DNS_RTREE_DECLARE(artree_dummy);
PICO_DNS_RTREE_DECLARE(artree_dns);

/* Sort the records in 2 two vectors by unicast or multicast */
if (pico_mdns_sort_unicast_multicast(antree, &antree_u, &antree_m)) {
mdns_dbg("Could not sort answers into unicast/multicast vector!\n");
return -1;
}

/* Try to gather additionals for the to send response */
if (pico_mdns_gather_additionals(antree, &artree)) {
mdns_dbg("Could not gather additionals properly!\n");
return -1;
}
/* Use sort-function to convert mDNS record tree to DNS record tree */

/* Sort the answers into multicast and unicast answers */
pico_mdns_sort_unicast_multicast(antree, &antree_u, &antree_m);

/* Convert the mDNS additional tree to a DNS additional tree to send with
* the the unicast AND the multicast response */
pico_mdns_sort_unicast_multicast(&artree, &artree_dummy, &artree_dns);

/* Send response via unicast */
Expand Down
57 changes: 55 additions & 2 deletions test/unit/modunit_pico_mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,11 @@ void add_records( void ) /* MARK: helper to add records to MyRecords s*/

/* Create an A record with URL */
record = pico_mdns_record_create(url, &rdata, 4, PICO_DNS_TYPE_A, 0,
(PICO_MDNS_RECORD_UNIQUE | PICO_MDNS_RECORD_PROBED));
(PICO_MDNS_RECORD_UNIQUE |
PICO_MDNS_RECORD_PROBED |
PICO_MDNS_RECORD_HOSTNAME));
fail_if(!record, "Record could not be created!\n");
printf("Is hostname record: %d\n", IS_HOSTNAME_RECORD(record));

/* Create 2 PTR records to URL */
record1 = pico_mdns_record_create(url, url, strlen(url),
Expand Down Expand Up @@ -1145,7 +1148,8 @@ START_TEST(tc_mdns_my_records_find_probed) /* MARK: mdns_my_records_find_probed

hits = pico_mdns_my_records_find_probed();
fail_unless(2 == pico_tree_count(&hits),
"mdns_my_records_find_probed failed!\n");
"mdns_my_records_find_probed failed %d!\n",
pico_tree_count(&hits));


printf("*********************** ending %s * \n", __func__);
Expand Down Expand Up @@ -1500,6 +1504,52 @@ START_TEST(tc_mdns_sort_unicast_multicast) /* MARK: sort_unicast_multicast */
printf("*********************** ending %s * \n", __func__);
}
END_TEST
START_TEST(tc_mdns_gather_additionals) /* MARK: gather_additionals */
{
PICO_MDNS_RTREE_DECLARE(antree);
PICO_MDNS_RTREE_DECLARE(artree);
struct pico_mdns_record *srv_record = NULL, *record = NULL;
struct pico_tree_node *node = NULL;
int ret = 0;

printf("*********************** starting %s * \n", __func__);

add_records();

srv_record = pico_mdns_record_create("test._http._tcp.local",
"\0\0\0\0\0\x50\4host\5local", 17,
PICO_DNS_TYPE_SRV, 120,
PICO_MDNS_RECORD_UNIQUE);
fail_if(!srv_record, "Could not create SRV record!\n");
pico_tree_insert(&antree, srv_record);

ret = pico_mdns_gather_additionals(&antree, &artree);
fail_if(ret, "Gather Additionals returned error!\n");
fail_unless(pico_tree_count(&antree) == 3, "ANtree should contain 3: %d",
pico_tree_count(&antree));

printf("Answers: \n");
pico_tree_foreach(node, &antree) {
if ((record = node->keyValue)) {
printf("%d - %s\n", short_be(record->record->rsuffix->rtype),
record->record->rname);
}
}

printf("Additionals: \n");
pico_tree_foreach(node, &artree) {
if ((record = node->keyValue)) {
printf("%d - %s\n", short_be(record->record->rsuffix->rtype),
record->record->rname);
}
}

fail_unless(pico_tree_count(&artree) == 3, "ARtree should contine 3: %d",
pico_tree_count(&artree));

printf("*********************** ending %s * \n", __func__);
}
END_TEST
START_TEST(tc_mdns_apply_known_answer_suppression) /* MARK: apply_k_a_s */
{
pico_dns_packet *packet = NULL;
Expand Down Expand Up @@ -1815,6 +1865,7 @@ Suite *pico_suite(void)

/* Handling query packets */
TCase *TCase_mdns_sort_unicast_multicast = tcase_create("Unit test for mdns_sort_unicast_multicast");
TCase *TCase_mdns_gather_additionals = tcase_create("Unit test for mdns_gather_additionals");
TCase *TCase_mdns_apply_known_answer_suppression = tcase_create("Unit test for mdns_apply_known_answer_suppression");

/* Address resolving functions */
Expand Down Expand Up @@ -1929,6 +1980,8 @@ Suite *pico_suite(void)
/* Handling query packets */
tcase_add_test(TCase_mdns_sort_unicast_multicast, tc_mdns_sort_unicast_multicast);
suite_add_tcase(s, TCase_mdns_sort_unicast_multicast);
tcase_add_test(TCase_mdns_gather_additionals, tc_mdns_gather_additionals);
suite_add_tcase(s, TCase_mdns_gather_additionals);
tcase_add_test(TCase_mdns_apply_known_answer_suppression, tc_mdns_apply_known_answer_suppression);
suite_add_tcase(s, TCase_mdns_apply_known_answer_suppression);

Expand Down

0 comments on commit 49a19b0

Please sign in to comment.