Skip to content

Add example for avahi_entry_group_add_record() API#592

Draft
hyh19962008 wants to merge 7 commits intoavahi:masterfrom
hyh19962008:add_record_example
Draft

Add example for avahi_entry_group_add_record() API#592
hyh19962008 wants to merge 7 commits intoavahi:masterfrom
hyh19962008:add_record_example

Conversation

@hyh19962008
Copy link
Copy Markdown

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.

@evverx
Copy link
Copy Markdown
Collaborator

evverx commented Apr 27, 2024

Thank you for the PR!

avahi_entry_group_add_record() API does not have any document or comment or example code on how to use it

It's true but it's probably because it's kind of low-level and should not normally be used when something like avahi_entry_group_add_service should do. I think examples should cover common use cases and in the case of avahi_entry_group_add_record it would probably be CNAMEs.

Other than that the compiler isn't happy (it should have been caught by the CI but it doesn't compile avahi with -Werror currently. I'll open a separate issue):

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;
@hyh19962008
Copy link
Copy Markdown
Author

Yes, most of the time avahi_entry_group_add_service is good enough, but it does not cover all the situations. For example, it can't set TTL at the moment. While avahi_entry_group_add_record gives users more control and flexibility to cover niche use cases. But I have to say it took me 3 days to get these few lines of code working properly. It's really painful to use it without any document or reference.

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.

@evverx
Copy link
Copy Markdown
Collaborator

evverx commented Apr 28, 2024

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 avahi_entry_group_add_record works) but I don't think that the code publishing services using avahi_entry_group_add_record and modifying them in the process should be included. Sorry.

@hyh19962008
Copy link
Copy Markdown
Author

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.

@evverx
Copy link
Copy Markdown
Collaborator

evverx commented Apr 29, 2024

And the way to publish different records are all slightly different

It's true but the examples aren't supposed to show how to assemble different resource records from scratch to pass them to avahi_entry_group_add_record (and it isn't necessary to do that to show how avahi_entry_group_add_record works).

there is no document or tutorial available

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.

publishing of a SRV record

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.
@hyh19962008
Copy link
Copy Markdown
Author

Alright, the crash is fixed, and removed other records except CNAME.
Maybe I can post the rest in my blog or somewhere as a tutorial.

@evverx
Copy link
Copy Markdown
Collaborator

evverx commented Apr 30, 2024

Maybe I can post the rest in my blog or somewhere as a tutorial.

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 enable_debug should be there. print_txt_record shouldn't be there either. mdns_name_to_txt_record is kind of confusing in that it doesn't convert anything to TXT records. I'm not sure why modify_callback is needed. I'm guessing it came from the client-publish-service example but it's unclear why.

I'll convert this PR to draft and add the "documentation bug" label so as not to forget that it should be documented somewhere.

@evverx evverx added documentation Change of text outside of the code. bug labels Apr 30, 2024
@evverx evverx marked this pull request as draft April 30, 2024 16:07
@hyh19962008
Copy link
Copy Markdown
Author

The major code is from client-publish-service, and agreed the modify_callback is not necessary. mdns_name_to_txt_record is renamed and enable_debug with print_txt_record are removed.

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.

2 participants