Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: mdns: add capability of providing records in runtime #60271

Merged

Conversation

konradderda
Copy link
Contributor

Current implementation of mDNS responder does makes it mandatory to have all the records set at compile time. It is not suited well for applications that have to publish/unpublish or change records in runtime, e.g. data received from the network.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @konradderda, and thank you very much for your first pull request to the Zephyr project!

A project maintainer just triggered our CI pipeline to run it against your PR and ensure it's compliant and doesn't cause any issues. You might want to take this opportunity to review the project's Contributor Expectations and make any updates to your pull request if necessary. 😊

@cfriedt
Copy link
Member

cfriedt commented Jul 12, 2023

Current implementation of mDNS responder does makes it mandatory to have all the records set at compile time.

Technically, that's not exactly the case, it only requires the pointers to memory for DNS records to be set at compile time (and subsequently the number of records).

It is not suited well for applications that have to publish/unpublish or change records in runtime, e.g. data received from the network.

The memory that each record points to can be runtime modifiable today.

In any case though, dns_sd is definitely ripe for improvement.

Copy link
Member

@cfriedt cfriedt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, with a couple of minor modifications.

Please fix the compliance issues and ensure the build is green.

If you have interest in this area, I would recommend checking out some of the items on the TODO list here #38846

DNS_SD_FOREACH(record) {
DNS_SD_COUNT(&rec_num);

while (rec_num || ext_rec_num) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use integer comparisons here instead of treating the integers as bools

@cfriedt
Copy link
Member

cfriedt commented Jul 12, 2023

cc @pdgendt

Copy link
Contributor

@rlubos rlubos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the compliance results, it reported some nits as well. Otherwise looks good.


#include <stddef.h>

#include "dns_sd.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include <zephyr/net/dns_sd.h>

* @ref DNS_SD_REGISTER_SERVICE (if any) and then go over external records.
*
* @param records An array of mDNS records
* @param count The number of elemnts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param count The number of elemnts
* @param count The number of elements

*
* @param records An array of mDNS records
* @param count The number of elemnts
* @return int 0 for OK; -EINVAL for invalid parameters.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return doesn't need type

@konradderda
Copy link
Contributor Author

Hello @cfriedt,
Thank you for your comments

The memory that each record points to can be runtime modifiable today.

That was my first idea. However, it's not possible because the text_size field of dns_sd_rec is not a pointer so it would have a fixed value and cannot be modified:

size_t text_size;

Moreover, such approach would be enough for a small number of records. With this approach the user can provide a pointer to a static array, memory slab, heap allocated memory, etc.

@konradderda konradderda force-pushed the mdns_external_records branch 2 times, most recently from 261f0fc to b8c69a8 Compare August 4, 2023 06:41
Copy link
Contributor

@rlubos rlubos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the doc build output, it looks that we needed to escape square brackets in struct dns_sd_rec documentation (i. e. < >)

rlubos
rlubos previously approved these changes Aug 7, 2023
@konradderda
Copy link
Contributor Author

@cfriedt @ssharks @tbursztyka, sorry for bothering you but it seems that this PR could be closed with an additional approval, could you please look at it? Thanks in advance.

Copy link
Collaborator

@ssharks ssharks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I observe: Is it clear to the users that the mdns_responder_set_ext_records knows it have to make the data stay at that location instead of providing a stack variable?

@konradderda
Copy link
Contributor Author

The only thing I observe: Is it clear to the users that the mdns_responder_set_ext_records knows it have to make the data stay at that location instead of providing a stack variable?

@ssharks, Thank you for the question, I think that providing a stack variable is perfectly fine as long as the scope is prolonged - e.g. we can have an array of dns_sd_rec in the main() function with while (true) loop. Another example - delegating k_work to publish records only once for some time (e.g. for 5 minutes after initialization) where there is no point to occupy the RAM for whole time. These are not common use cases and of course one can use other solutions but I believe there is no need of enforcing protection from this (+ it feels like a common sense, like not returning pointers to stack variables).

ssharks
ssharks previously approved these changes Aug 22, 2023
include/zephyr/net/mdns_responder.h Show resolved Hide resolved
include/zephyr/net/mdns_responder.h Outdated Show resolved Hide resolved
include/zephyr/net/mdns_responder.h Outdated Show resolved Hide resolved
Copy link
Member

@jukkar jukkar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes needed

@fabiobaltieri fabiobaltieri added this to the v3.6.0 milestone Oct 3, 2023
Copy link

github-actions bot commented Dec 3, 2023

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Dec 3, 2023
@jukkar jukkar removed the Stale label Dec 3, 2023
@jukkar
Copy link
Member

jukkar commented Dec 4, 2023

ping @konradderda, just wondering if you would have some time to address the latest comments?

@konradderda
Copy link
Contributor Author

@jukkar I am sorry for the delay. There is no problem with addressing the comments regarding the code. However, I had tried to write a test case for my change and I found that with the current implementation it's quite hard to do. Unfortunately, at the moment I will not be able to provide a proper test implementation.

@MaureenHelm
Copy link
Member

@konradderda will you return to this before the 3.6 merge window closes this week?

@henrikbrixandersen henrikbrixandersen removed this from the v3.6.0 milestone Feb 7, 2024
@jukkar
Copy link
Member

jukkar commented Mar 5, 2024

ping @konradderda, are you able to continue with this? The needed changes look rather small and it would be nice to get this merged as this has been pending for a long time. It would be also nicer to get this in at the beginning of the 3.7 release cycle than at the end.

@jukkar
Copy link
Member

jukkar commented Mar 5, 2024

The needed changes look rather small

Hmm, I missed the fact that unit tests would need changes too so the amount of work is bigger.

@konradderda
Copy link
Contributor Author

@jukkar @MaureenHelm I had finally got some time and prepared a test for this change with a foundations for implementation of new test cases in the futute. Also, I have applied last remarks from @cfriedt .

@konradderda konradderda force-pushed the mdns_external_records branch 3 times, most recently from 342c82a to 9d681cb Compare March 7, 2024 11:38
Comment on lines 231 to 236
if (!ifaddr) {
zassert_not_null(ifaddr, "Failed to add LL-addr");
} else {
/* we need to set the addresses preferred */
ifaddr->addr_state = NET_ADDR_PREFERRED;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify this to:

zassert_not_null(ifaddr, "Failed to add LL-addr");

/* we need to set the addresses preferred */
ifaddr->addr_state = NET_ADDR_PREFERRED;

because if the zassert fails, then the program stops so there is no need to have the if() else() branches.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*/
if (rec_num > 0) {
DNS_SD_GET(rec_num - 1, &record);
rec_num -= 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is perfectly valid code but usually -1 is done via rec_num--;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

rec_num -= 1;
} else {
record = &external_records[ext_rec_num - 1];
ext_rec_num -= 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and ext_rec_num--;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

subsys/net/lib/dns/mdns_responder.c Show resolved Hide resolved
Current implementation of mDNS responder does makes it mandatory to have
all the records set at compile time. It is not suited well for applications
that have to publish/unpublish or change records in runtime, e.g. data
received from the network.

Signed-off-by: Konrad Derda <konrad.derda@nordicsemi.no>
This commit adds a test for externally stored mDNS records for mDNS
responder.

Signed-off-by: Konrad Derda <konrad.derda@nordicsemi.no>
@fabiobaltieri fabiobaltieri merged commit 5e46a49 into zephyrproject-rtos:main Mar 22, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants