Add example for avahi_entry_group_add_record() API#592
Add example for avahi_entry_group_add_record() API#592hyh19962008 wants to merge 7 commits intoavahi:masterfrom
Conversation
based on client-publish-service.c
|
Thank you for the PR!
It's true but it's probably because it's kind of low-level and should not normally be used when something like Other than that the compiler isn't happy (it should have been caught by the CI but it doesn't compile avahi with client-publish-service-add-record.c:97:6: warning: no previous prototype for ‘print_txt_record’ [-Wmissing-prototypes]
97 | void print_txt_record(char *txt) {
| ^~~~~~~~~~~~~~~~
client-publish-service-add-record.c:149:7: warning: no previous prototype for ‘mdns_dns_srv_record’ [-Wmissing-prototypes]
149 | char *mdns_dns_srv_record(mdns_dns_srv_t *srv, const char *txt, int *rr_size) {
| ^~~~~~~~~~~~~~~~~~~
client-publish-service-add-record.c: In function ‘mdns_dns_srv_record’:
client-publish-service-add-record.c:152:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
152 | char *buf = avahi_malloc(*rr_size);
| ^~~~
client-publish-service-add-record.c:150:10: warning: unused variable ‘len’ [-Wunused-variable]
150 | char len = strlen(txt);
| ^~~
client-publish-service-add-record.c: In function ‘create_services’:
client-publish-service-add-record.c:164:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
164 | char full_name[128] = {0};
| ^~~~
client-publish-service-add-record.c:218:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
218 | mdns_dns_srv_t srv;
| ^~~~~~~~~~~~~~ Also to judge from blish-service-add-record
Adding service 'MegaPrinter'
Service 'MegaPrinter' successfully established.
Service name collision, renaming service to 'MegaPrinter #2'
Adding service 'MegaPrinter #2'
Service 'MegaPrinter #2' successfully established.
Service name collision, renaming service to 'MegaPrinter #3'
Adding service 'MegaPrinter #3'
Service 'MegaPrinter #3' successfully established.
Service name collision, renaming service to 'MegaPrinter #4'conflicts aren't handled well (Ideally the examples should be run by the CI too but currently they aren't. I'll open a separate issue too). |
remove UNIQUE flag in first PTR record; add CNAME example;
|
Yes, most of the time On the new commit, the compiler warnings are resolved, the name collision was because I added UNIQUE flag in the PTR record by mistake and is removed now. And CNAME example is also included. |
|
The idea behind the examples is to show how to use the API properly. They aren't supposed to show how to cover niche use cases. I'm OK with an example publishing aliases (because it should be enough to figure out how |
|
The example itself does not try to cover any niche use cases, it's trying to demonstrate how to use the API by adding a service. Because in the process, PTR, SRV, TXT, CNAME records will have to be published. And the way to publish different records are all slightly different. By the way, I think the most valuable part of this example is the publishing of a SRV record, which use a special struct to pass the rdata, and would require some hard work to look into the source when there is no document or tutorial available. |
It's true but the examples aren't supposed to show how to assemble different resource records from scratch to pass them to
Once again I'm OK with providing an example showing how to publish aliases and documenting it but it isn't a tutorial on what RRs are, what they should look like, how they should be built and so on.
It isn't built correctly because the target field contains a single-label name with no domain and it all crashes when conflicts occur: Adding service 'MegaPrinter'
Adding CNAME H_cname.local for H
Service 'MegaPrinter' successfully established.
Service name collision, renaming service to 'MegaPrinter #2'
client-publish-service-add-record: alternative.c:137: avahi_alternative_service_name: Assertion `s' failed.
Aborted (core dumped)
...
#0 0x00007ffff6eae834 in __pthread_kill_implementation () from /lib64/libc.so.6
#1 0x00007ffff6e5c8ee in raise () from /lib64/libc.so.6
#2 0x00007ffff6e448ff in abort () from /lib64/libc.so.6
#3 0x00007ffff6e4481b in __assert_fail_base.cold () from /lib64/libc.so.6
#4 0x00007ffff6e54c57 in __assert_fail () from /lib64/libc.so.6
#5 0x00007ffff77af213 in avahi_alternative_service_name (s=0x0) at alternative.c:137
#6 0x00000000004025dc in entry_group_callback (g=0x6060000008c0, state=AVAHI_ENTRY_GROUP_COLLISION, userdata=0x0) at client-publish-service-add-record.c:83
#7 0x00007ffff7f47e1b in avahi_entry_group_set_state (group=0x6060000008c0, state=AVAHI_ENTRY_GROUP_COLLISION) at entrygroup.c:49
#8 0x00007ffff7f41ffb in filter_func (bus=0x612000000040, message=0x610000000740, userdata=0x60e000000120) at client.c:219
#9 0x00007ffff7ed6f79 in dbus_connection_dispatch () from /lib64/libdbus-1.so.3
#10 0x00007ffff7f63bac in dispatch_timeout_callback (t=0x606000000380, userdata=0x6030000003d0) at ../avahi-common/dbus-watch-glue.c:105
#11 0x00007ffff77c0440 in timeout_callback (t=0x606000000380) at simple-watch.c:447
#12 0x00007ffff77c198b in avahi_simple_poll_dispatch (s=0x60e000000040) at simple-watch.c:563
#13 0x00007ffff77c235e in avahi_simple_poll_iterate (s=0x60e000000040, timeout=-1) at simple-watch.c:605
#14 0x00007ffff77c26b5 in avahi_simple_poll_loop (s=0x60e000000040) at simple-watch.c:646
#15 0x0000000000404343 in main (argc=1, argv=0x7fffffffe088) at client-publish-service-add-record.c:459 |
remove other records except CNAME;
to client-publish-record.c.
|
Alright, the crash is fixed, and removed other records except CNAME. |
I think it would probably make sense to post everything there for the time being because I'm afraid I don't have the time to review documentation PRs so many times. I took a quick look and I'm not sure I'll convert this PR to draft and add the "documentation bug" label so as not to forget that it should be documented somewhere. |
|
The major code is from |
Currently avahi_entry_group_add_record() API does not have any document or comment or example code on how to use it. People would have to dig into avahi's source to find out.